Skip to content

Commit 40f5a6e

Browse files
committed
Merge pull request #123: prevent task claiming race condition in s11
Add double-checked validation inside _claim_lock: verify task owner, status, and blockedBy before claiming. Check claim_task return value in idle loop to skip failed claims.
2 parents 5f2b0f2 + 42fac6a commit 40f5a6e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

agents/s11_autonomous_agents.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ def claim_task(task_id: int, owner: str) -> str:
142142
if not path.exists():
143143
return f"Error: Task {task_id} not found"
144144
task = json.loads(path.read_text())
145+
if task.get("owner"):
146+
existing_owner = task.get("owner") or "someone else"
147+
return f"Error: Task {task_id} has already been claimed by {existing_owner}"
148+
if task.get("status") != "pending":
149+
status = task.get("status")
150+
return f"Error: Task {task_id} cannot be claimed because its status is '{status}'"
151+
if task.get("blockedBy"):
152+
return f"Error: Task {task_id} is blocked by other task(s) and cannot be claimed yet"
145153
task["owner"] = owner
146154
task["status"] = "in_progress"
147155
path.write_text(json.dumps(task, indent=2))
@@ -274,7 +282,9 @@ def _loop(self, name: str, role: str, prompt: str):
274282
unclaimed = scan_unclaimed_tasks()
275283
if unclaimed:
276284
task = unclaimed[0]
277-
claim_task(task["id"], name)
285+
result = claim_task(task["id"], name)
286+
if result.startswith("Error:"):
287+
continue
278288
task_prompt = (
279289
f"<auto-claimed>Task #{task['id']}: {task['subject']}\n"
280290
f"{task.get('description', '')}</auto-claimed>"

0 commit comments

Comments
 (0)