Skip to content

Tasks and plans: one task can get two open plans, and plan and task states drift #672

Open

Description

@alex-clickhouse

Two plan proposals for one task can both succeed. The task can then get two implementation sessions, or be closed while its implementation runs. Other paths leave a plan and its task in states that disagree, for example a pending plan on a done task. Some of these states have no exit except a manual edit.

What happens

TSK-1: Two open plans for one task

  1. Two plan_propose calls for one task run at the same time, for example the task planner job and a chat session. Each reads the open plans and finds none (nerve/agent/tools/handlers/plans.py:L141-L145).
  2. Each computes version 1 and inserts its plan (nerve/agent/tools/handlers/plans.py:L147-L170). The check and the insert are separate awaits, with no lock and no transaction.
  3. The schema does not stop the second insert: plans has only non-unique indexes on task_id and status (nerve/db/migrations/v012_plans.py:L27-L28).
  4. plan_update has the same gap. It supersedes the old version (nerve/agent/tools/handlers/plans.py:L217-L220) before it inserts the new one (nerve/agent/tools/handlers/plans.py:L224). A plan_propose between the two steps sees no open plan and inserts a second plan.
  5. approve_plan checks only the row of the plan that it approves (nerve/agent/plan_service.py:L410-L416). Approving the second plan starts a second implementation session for the same task.
  6. Declining the second plan calls task_done (nerve/agent/plan_service.py:L703-L707). task_done marks every implementing plan of the task done (nerve/agent/tools/handlers/tasks.py:L963-L966) and moves the task to done/, while the first implementation still runs.

Consequence: one task is implemented twice, or the task is closed under a running implementation. The Plans page shows two "v1" cards.
Reproduced: test_repro_concurrent_propose_two_pending_plans_two_impl_sessions, test_repro_plan_update_racing_propose_two_pending, test_repro_declining_the_twin_plan_closes_a_task_under_implementation.

TSK-2: Approve ignores a failed task update and leaves the task in "Plan in review"

  1. The in_progress status is not protected (is_system=0, nerve/db/migrations/v030_task_statuses.py:L32-L33). The delete route refuses only a protected status or a status that a task uses (nerve/gateway/routes/tasks.py:L575-L587).
  2. A person deletes in_progress. Later a plan is proposed, and the task moves to plan_review. The person approves the plan.
  3. approve_plan calls task_update with status="in_progress" and the note that links the implementation session (nerve/agent/plan_service.py:L431-L438).
  4. task_update returns an error result before it writes anything (nerve/agent/tools/handlers/tasks.py:L575-L578).
  5. approve_plan does not read the result. Its rollback runs only on an exception (nerve/agent/plan_service.py:L453-L459). The plan stays implementing and the implementation turn runs. record_plan_approval has the same gap (nerve/agent/plan_service.py:L601-L604).

Consequence: the card stays in "Plan in review" while its plan is implementing. The task file has no link to the implementation session. Other error results are lost the same way, for example a refused reopen of a done task whose file is missing (nerve/agent/tools/handlers/tasks.py:L1029-L1034).
Reproduced: test_repro_deleted_in_progress_status_approve_leaves_plan_review.

TSK-4: Closing a task leaves its pending plan open

  1. A plan is pending and the task is in plan_review. The person drags the card to Done.
  2. The move route calls task_update (nerve/gateway/routes/tasks.py:L291-L299), which calls task_done (nerve/agent/tools/handlers/tasks.py:L580-L584). An agent that calls task_done takes the same path.
  3. task_done closes only implementing plans, and failed plans of its own session (nerve/agent/tools/handlers/tasks.py:L960-L966). It does not dismiss the plan notifications. Decline does (nerve/agent/plan_service.py:L714-L716).
  4. The plan stays pending on the Plans page, and its "Plan ready for review" notification stays open.
  5. A later approve moves the task to in_progress, which reopens it and moves its file back to active/. Then it reads the file at the done/ path that it captured before the reopen (nerve/agent/plan_service.py:L400, nerve/agent/plan_service.py:L443-L450). That file is gone, so the task content is empty.

Consequence: a closed task asks for a decision. Approving it reopens the task with no warning, and the implementation brief has no task body.
Reproduced: test_repro_pending_plan_survives_task_done_and_approve_reopens_with_empty_prompt.

TSK-5: The task file and the task row drift when the second write fails

  1. task_done without a revision token moves the file to done/, then writes the row (nerve/agent/tools/handlers/tasks.py:L938-L943). Decline, the board and most agent calls use this path.
  2. The row write fails, for example on a locked database, a disk error or a crash. The row keeps its old status and its active/ path. The file is no longer at that path.
  3. task_read answers "Task file not found" (nerve/agent/tools/handlers/tasks.py:L766).
  4. A task_update note finds no file and is dropped, but the call reports "updated" (nerve/agent/tools/handlers/tasks.py:L628-L631, nerve/agent/tools/handlers/tasks.py:L713-L728).
  5. A retried task_done finds no file and writes only the row (nerve/agent/tools/handlers/tasks.py:L952-L958). The row becomes done, with its path still on the missing active/ file. A reopen then refuses every time (nerve/agent/tools/handlers/tasks.py:L1029-L1034).
  6. The token path (row first, then file, nerve/agent/tools/handlers/tasks.py:L944-L951) and reopen (nerve/agent/tools/handlers/tasks.py:L1049-L1058) have the opposite failure.
  7. Nothing repairs the drift. TaskManager.reindex has no caller (nerve/tasks/manager.py:L42).

Consequence: the task stays in done (or in its old status) with a file that no tool can reach, and notes are lost. The only exit is to move the file by hand.
Reproduced: test_repro_task_done_row_write_failure_strands_file (with an injected failure of the row write).

TSK-6: An interrupted approve leaves the plan implementing with no turn

  1. approve_plan claims the plan as implementing (nerve/agent/plan_service.py:L410-L414). Session creation, the task update and the dispatch follow (nerve/agent/plan_service.py:L425-L497).
  2. The rollback catches Exception only (nerve/agent/plan_service.py:L453), so a CancelledError skips it. A gateway shutdown disconnects every client (nerve/agent/engine.py:L1039-L1044) and can cancel a plan_approve tool call in this span. A crash in this span has the same result.
  3. No startup step returns such a plan to pending. plan_propose then refuses the task with "already has a pending or implementing plan" (nerve/agent/tools/handlers/plans.py:L141-L145).
  4. plan_update has the same gap between its supersede and its insert (nerve/agent/tools/handlers/plans.py:L217-L224). Then the task stays in plan_review with no plan to review. The planner job selects only pending tasks (nerve/setup_writer.py:L159-L162), so it does not return to the task.

Consequence: the task can stay in plan_review with no way forward. The only exits are task_done or a manual edit.
Reproduced: code reading.

TSK-7: A drain during approve reverts the plan but not the task

approve_plan checks that turns are accepted (nerve/agent/plan_service.py:L404). Then it moves the task to in_progress with an "Implementation started" note and dismisses the notifications (nerve/agent/plan_service.py:L431-L438, nerve/agent/plan_service.py:L460-L462). If a graceful restart starts before engine.run, only the plan goes back to pending (nerve/agent/plan_service.py:L479-L486).
Consequence: after the restart, the plan is pending, but the task is in in_progress with a note about an implementation that did not start, and with no notification.
Reproduced: code reading.

TSK-8: Deleting a status races a move into it

The delete route counts the tasks in the status, then deletes it in a separate statement (nerve/gateway/routes/tasks.py:L580-L587). task_update checks the status (nerve/agent/tools/handlers/tasks.py:L575-L578) several awaits before it writes it (nerve/agent/tools/handlers/tasks.py:L688-L690, nerve/agent/tools/handlers/tasks.py:L714-L719). A move between the two steps leaves the task on a status that does not exist.
Consequence: the board shows lanes only for configured statuses (nerve/gateway/routes/tasks.py:L168-L197), so the task can disappear from the board.
Reproduced: code reading.

TSK-9: task_update with status=done drops the other edits of the call

task_update sends a done transition to task_done with only the note and the revision token (nerve/agent/tools/handlers/tasks.py:L580-L584). The title, deadline and tags of the same call are discarded, and the call reports "marked as done". The PATCH route forwards the same payload (nerve/gateway/routes/tasks.py:L465-L479).
Reproduced: code reading.

TSK-11: The plan_decline description does not say that it closes the task

The description says only "Decline a pending plan" (nerve/agent/tools/handlers/plans.py:L508-L513). The plan_update description names "plan_decline + plan_propose" as the alternative (nerve/agent/tools/handlers/plans.py:L475-L480). An agent that follows that pattern closes the task, then proposes a plan on the done task. plan_propose does not refuse a done task, and does not move it out of done (nerve/db/task_statuses.py:L30). The result is a pending plan on a done task, with the same effects as closing a task in plan_review.
Reproduced: code reading.

TSK-12: A second answer to a pending question can replace the first

resolve() and deny() write the outcome on a pending entry even when its event is already set (nerve/agent/interactive.py:L367-L395). The waiting tool reads the entry only when it resumes (nerve/agent/interactive.py:L316-L325). If an answer and then a close, or a second answer from another tab, arrive before that, the later one wins.
Reproduced: code reading.

Expected

One task has at most one plan in pending or implementing. A plan and its task agree: an approved plan has its task out of plan_review, and a done task has no pending plan. A task row and its file agree on the file location. A state that a crash or a cancel can leave behind has a recovery step.

Fix outline:

  • TSK-1: do the open-plan check, the supersede and the insert in one transaction, or under the per-task lock. Add a partial unique index on plans(task_id) WHERE status IN ('pending', 'implementing'). Approve refuses when another plan of the task is implementing. Decline does not close a task that has another open plan. Test: two concurrent plan_propose calls give one plan and one clear refusal.
  • TSK-2: protect in_progress, or make the approve target status configurable. In approve_plan and record_plan_approval, treat an error result from task_update as a failure and revert the plan. Test: approve with in_progress deleted keeps the plan pending and reports the error.
  • TSK-4, TSK-11: task_done supersedes or declines the pending plans of the task and dismisses their notifications. plan_propose refuses a done task. approve_plan reads the task again after the status update. The plan_decline description says that it closes the task. Test: close a task in plan_review, then check that no plan is pending.
  • TSK-5: keep the row and the file location consistent across a failure: write the intended path in the row transaction and reconcile at startup the rows whose file is missing. task_update reports an error, not "updated", when it cannot write the note. Test: inject a row-write failure in task_done, then run the startup step and check that row and file agree.
  • TSK-6, TSK-7: revert the claim on any exit (except BaseException, or shield the claimed section), and revert the task status and note with it. At startup, return to pending every implementing plan whose implementation session has no turn. Put the supersede and the insert of plan_update in one transaction. Test: cancel approve_plan after the claim and check that the plan is pending.
  • TSK-8: delete the status with WHERE NOT EXISTS (SELECT 1 FROM tasks WHERE status = ?) in one transaction, and check the status inside the task write transaction.
  • TSK-9: apply the title, deadline and tag edits before the done transition, or refuse the mixed call.
  • TSK-12: resolve() and deny() return False once the event is set, so the first outcome is final.

This comes from reading the code at 85bb5129. The audit reproduced TSK-1, TSK-2, TSK-4 and TSK-5 with tests against that commit.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions