Repository navigation
Description
During a restart, Nerve refuses or drops work that it already accepted and recorded as done. The person sees "Answered" or "scheduled", but the agent never gets the input. Other paths run work a second time, although it may already have had side effects.
What happens
NTF-2: An answer is recorded as "Answered", but its turn is refused by the restart
- A session asks a question while one of its turns runs, or while another turn waits in its queue.
- The person answers.
handle_answerchecksaccepting_turns, then sets the row toanswered(nerve/notifications/service.py:L595-L605). - It starts
engine.runas a background task (nerve/notifications/service.py:L920-L930). The run lock is held, so the turn waits in the session queue (nerve/agent/engine.py:L3936-L3949). - A restart starts. Shutdown calls
stop_accepting_turns()(nerve/gateway/server.py:L1022). A drain waits only for running sessions, not for queued turns (nerve/agent/engine.py:L2045-L2061). - The running turn ends. The queued answer turn gets the lock and is refused (nerve/agent/engine.py:L3974-L3977). The done-callback only logs the error (nerve/notifications/service.py:L932-L942). No startup step delivers answered rows again.
Consequence: the answer never reaches the agent. Every card shows "Answered", and a new answer is refused because the row is not pending (nerve/notifications/service.py:L573). Dismissal and expiry notes use the same dispatch and are lost the same way.
Reproduced: test_repro_answer_committed_then_turn_refused (an engine stub raises the same NotAcceptingTurnsError).
WFL-5: A review-loop leg cut by shutdown is retried with no decision from a person
WorkflowRunService.stop()cancels only the monitor. Running_executetasks continue (nerve/workflows/service.py:L173-L183). Thenengine.shutdown()disconnects every client beforeclose_db()(nerve/gateway/server.py:L1114-L1122).- The leg's turn ends through the generic error path as
failed(nerve/agent/engine.py:L4534-L4609). _executesets the run tofailedwithoutcome.describe()as the error, and notifies for a run outside a loop (nerve/workflows/service.py:L614-L631). The text does not contain "nerve restart".- At the next start, loop recovery finds a restart only by that substring (nerve/workflows/review_loop.py:L1997). This run goes to the normal queue (nerve/workflows/review_loop.py:L2053).
- The normal path retries the implementer while
attempt_no < 2(nerve/workflows/review_loop.py:L1166-L1181).auto_reissue_implementerisFalseby default (nerve/config.py:L2802), because a re-run can apply side effects twice (nerve/workflows/review_loop.py:L2040-L2049).
Consequence: an implementer leg runs again after a restart with no decision from a person. Its side effects can apply twice.
Reproduced: code reading. It needs _execute to write the row before close_db().
DMN-4: A restart during a turn can start a new runtime and records "Agent error"
- Shutdown does not cancel or wait for running turn tasks. Web turns are detached tasks (nerve/gateway/server.py:L1417-L1430).
engine.shutdown()only disconnects clients and marks sessions idle (nerve/agent/engine.py:L1036-L1051). - The turn's stream fails. With no content yet, a crash-retry branch builds a new client and sends the user message again (nerve/agent/engine.py:L4392-L4407, nerve/agent/engine.py:L4453-L4472). No branch checks
_shutting_down. If the new client registers before_clients.clear()runs (nerve/agent/engine.py:L1051), shutdown drops it with no disconnect. - With content, the turn takes the generic error path:
mark_error, an "Agent error: ..." message and statusfailed(nerve/agent/engine.py:L4534-L4609). The "gateway shutdown" label applies only to a cancel (nerve/agent/engine.py:L4475-L4478).
Consequence: the chat shows "Agent error" instead of an interruption note. A runtime process can start during shutdown and get the user message again. Writes after close_db() fail.
Reproduced: code reading.
WFL-13: A leg that starts during a drain is marked as interrupted after the restart
With gateway.drain_timeout_seconds > 0 (default 0, nerve/config.py:L940), a leg that ends during the drain starts the next leg. _dispatch_leg checks only _stopping (nerve/workflows/review_loop.py:L965), and the new run stays pending (nerve/workflows/service.py:L386-L387). At the next start, recovery marks every pending run failed, "interrupted by nerve restart" (nerve/workflows/service.py:L140-L145). The loop then asks a person about double side effects for an implementer leg that never ran (nerve/workflows/review_loop.py:L2042-L2049).
Reproduced: code reading.
SES-11: A claimed wakeup whose turn is refused or fails is never retried
The sweep checks accepting_turns once, then awaits the query and the claim (nerve/cron/service.py:L2153-L2170). The claim sets the row to fired, a final state (nerve/db/wakeups.py:L74-L85). A drain that starts in between makes run() refuse the turn (nerve/agent/engine.py:L3929-L3930). Any other failure of the run is only logged (nerve/cron/service.py:L2216-L2224). Nothing puts the row back to pending. This includes "Run later" messages that a person wrote.
Reproduced: code reading.
DMN-9: Sessions queued with nerve restart --resume can be lost or resumed much later
The CLI appends the ids to the resume queue file before the restart (nerve/cli.py:L1129-L1135). The new daemon deletes the file first, then runs one full turn per id, one after the other (nerve/agent/engine.py:L2902-L2959). If the daemon stops before it reaches an id, that run fails, the error is only logged, and the id is gone. If the restart does not happen (for example docker compose restart fails, nerve/cli.py:L1143-L1147), the ids stay in the file and run at a later, unrelated start.
Reproduced: code reading.
HST-6: A crash after a hosted event is accepted runs its turn again
The hosted channel accepts an event when it creates the turn task (nerve/channels/hosted/channel.py:L1061-L1072). The page is acknowledged only after all of its rows (nerve/channels/hosted/intake.py:L302-L305). After a SIGKILL or OOM before the acknowledgement, the gateway sends the event again. The duplicate cache is in memory and starts empty (nerve/channels/hosted/intake.py:L105-L110), so the agent answers and acts on the message twice. A graceful shutdown is not affected. InboxReader.stop documents the trade-off (nerve/channels/hosted/intake.py:L223-L231).
Reproduced: code reading.
Expected
Work that Nerve accepted runs once. If a restart stops it before it starts, it stays in a state from which the next start runs it. Work that may have had side effects is not run again without a person's decision.
Fix outline:
- NTF-2: keep the question
pending(or add anansweringstate) until the turn holds the run lock. Put it back topendingonNotAcceptingTurnsError. Another option: store the injected message and deliver undelivered answers at startup. Test: answer while a turn runs, stop accepting turns, end the running turn, and assert that the question is stillpending. - WFL-5: mark a leg cut by shutdown with a structured field (for example
interrupted_by = 'restart'), and classify by that field, not by a substring. Test: a leg whose turn fails during shutdown parks asrestartand is not re-issued. - DMN-4: in shutdown, cancel the registered turn tasks and wait for them, with a time limit, before
close_db(). Make both crash-retry branches refuse when_shutting_downis set. Test: a turn whose stream fails aftershutdown()starts does not build a new client. - WFL-13: do not dispatch a leg while
engine.accepting_turnsis false. Keep never-startedpendingrunspendingacross a restart. - SES-11: set a wakeup to
firedonly when its turn starts, or put it back topendingon refusal or failure. Aclaimed_atlease that startup recovers is another option. - DMN-9: remove an id from the queue file only when its turn starts. Stamp each entry so that a later, unrelated start ignores it.
- HST-6: store accepted
(connection_id, event_id)keys in SQLite, with a TTL, before the channel returnsaccepted.
This comes from reading the code at 85bb5129. The audit reproduced NTF-2 with a test against that commit.
🤖 Generated with Claude Code
Activity