Repository navigation
[ENG-20801] fix(agents): sub-agent resumed twice after a gateway restart - #269
Conversation
After a gateway restart, two orphan-recovery scans can overlap: one is scheduled when sub-agent runs are restored from disk, and one by the post-ready startup sidecar. The 1 s schedule debounce merges them only when they are queued less than 1 s apart, which is not the case on a host that starts several channel providers during boot. Each scan kept its own set of resumed sessions and cleared abortedLastRun only after the resume call and the store write finished. A second scan that read the store in that window resumed the same child session again, so the child repeated its interrupted work, including any tool calls, and the parent waited for the second run. Claim a child session key in a process-wide set before resuming it, and release the claim when that iteration ends. Before claiming, re-read the child entry from the session store instead of the scan's own cache, and use that entry for the recovery gate and the resume. A scan that was waiting on another child then does not act on a stale abortedLastRun after the other scan has already resumed the session. Three older gaps are left as is and noted in the code: a resume that the gateway accepts after the call timeout is started again by the retry, a failed store write after an accepted resume lets a later scan resume again, and the unconditional clear can drop an abort mark that the resumed run wrote meanwhile.
|
@cubic-dev-ai review |
@orswn I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Turn on auto-fix | Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Turn on auto-fix | Re-trigger cubic
The in-process claim stops two scans from resuming one child at the same time, but a resume can still be sent twice for one interruption: - the gateway accepts the resume but answers after the 10 s call timeout, and the retry sends a new random idempotency key; - the resume is accepted but the store write that clears abortedLastRun fails, and a later scan resumes again. Derive the resume idempotency key from the child session key and its recovery marker (last accepted attempt time and count). The key stays the same for every resume of one interruption, so the gateway returns the run it already accepted instead of starting another one. It changes once an accepted resume is recorded, so a later interruption still gets a new run. The session id is left out because the gateway can rotate it while accepting the resume. A scan that finds another scan's claim no longer drops the child: it counts it as deferred and its schedule retries, without finalizing it. Before, if the claim holder then failed on its last retry, the child ended as failed even though the other schedule could still resume it.
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.
Fix all with cubic | Turn on auto-fix | Re-trigger cubic
The resume idempotency key did not change across an in-process gateway restart when the previous attempt was never recorded, for example after a failed store write or an explicit session reset. The new runtime has an empty dedupe map, so it started a new run under the old run id. agent.wait results are cached at module scope for 10 minutes, and only the new run's start event drops them, so the registry could read the old run's terminal result and end the record while the new run kept going. Add the agent event lifecycle generation to the key. It rotates on every in-process restart, so retries inside one gateway lifecycle still dedupe and a restart always gets a fresh run id. The key is now computed right before the resume call so it uses the generation that is current when the call is sent. Note a pre-existing gap at the skip after a fresh store read: a resume that timed out after the gateway accepted it, and whose run already started, is not remapped, so the registry keeps the old run id until the sweeper ends it.
The rotated session id test compared the two resume keys without checking them, so it would also pass if no idempotency key was sent. Assert both keys have the UUID shape first.
At boot the registry marks crashed sub-agent sessions with abortedLastRun so orphan recovery resumes them. The marking loop iterates the live run map and awaits a store write per child. On a slow host the post-ready recovery scan can resume a child during those awaits. The resume replaces the child's run in the map, and the loop then visits the replacement. It reads the child entry from the loop's store cache, loaded before the resume, so the running child looks crashed and is marked again. A later scan can then resume it a second time. Snapshot the restored runs before the loop. A run that is not in the snapshot, or whose record changed, came from a resume or a new spawn during the loop: it already has a completion wait, so hold it and do not mark it.
Orphan recovery resumes a child with a 10 s gateway call. If the gateway accepts the resume after the call has timed out, the resumed run starts and its start write clears the child's abort flag. The retry then reads the cleared flag and skips the child, so the registry keeps the old run. The sweeper or the last-attempt finalize later ends that old run as a failure while the resumed run still works, and the parent can start the task again. Remember the idempotency key of each resume call that fails, bound to the run it was sent for. The gateway uses that key as the run id. When a later scan finds the same run with the flag cleared and computes the same key, it remaps the registry to that run instead of skipping. A different run on the child, such as a steer, or a key from an earlier gateway lifecycle, is not taken over. A failed call on the schedule's last attempt is not covered, because no later scan visits the child.
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
|
@cubic-dev-ai review |
@orswn I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 4 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
When a scan takes over a sub-agent resume that the gateway accepted after the call timed out, it did not write the recovery marker that a confirmed resume writes. The resume key is derived from that marker, so a later interruption of the same child in the same gateway lifecycle sent the same key, and the gateway returned the already accepted run instead of starting a new one. Record the attempt on takeover, with the attempt number of the call that timed out. Keep the attempt with the remembered call, and forget the call once its run is finalized as failed.
|
@cubic-dev-ai review |
@orswn I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 4 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Cover a resume that replaces a child's run while the boot loop awaits the crashed mark of that same run. The loop iterates the live run map, so it still reaches the replacement, finds it missing from the snapshot of restored runs, and holds it. Only one completion wait is armed for the new run.
…4-boon.3 (#271) Cut the fork release that ships #269 (ENG-20801), the only commit on boon since v2026.6.34-boon.2. An interrupted native sub-agent is now resumed once after a gateway restart, instead of twice on a loaded host. Bump root package.json and npm-shrinkwrap.json, all 135 plugin manifests, and the version and openclaw peer floor in every extension npm-shrinkwrap.json. Add the 2026.6.34-boon.3 CHANGELOG.md section. No dependency or source changes.
|
Post-release check on The parent spawns one sub-agent that runs
Run 2 gateway log, UTC: Run 2 recording, 30 s, sped up: ENG-20801-boon3-subagent-restart.mp4 |

Related: ENG-20801, #266
What Problem This Solves
Fixes an issue where a sub-agent that was interrupted by a gateway restart would be resumed twice after the gateway came back. The sub-agent repeated its interrupted work, including any tool calls with side effects, and the parent's answer arrived late.
Seen on a test host running
2026.6.34-boon.2afteropenclaw gateway restartwhile a sub-agent was running a 90 s shell command:The command ran twice. The parent got one answer about 94 s later than needed.
Why This Change Was Made
Two recovery scans run at boot: one when sub-agent runs are restored from disk and one from the post-ready startup sidecar. The 1 s debounce merges them only when they are queued close together. On a host that starts Slack, Teams, and Google Chat during boot they were about 1.7 s apart. Each scan cleared
abortedLastRunonly after its resume finished, so the second scan resumed the same child again. A loaded gateway can also accept a resume after the 10 s call timeout; the retry then sent a new random idempotency key and started a second run.The fix has these parts:
agent.waitresults stay cached at module scope for 10 minutes, so reusing the old run id could let the registry read the old run's result and end the new run's record early. The session id is not part of the key because other writers can change it before a retry.Scheduling and the debounce are unchanged.
Not covered:
User Impact
A sub-agent cut off by a restart is resumed once. Its tool calls are not repeated, and the parent's answer is not delayed by a second run.
Evidence
Local reproduction before the fix
Local gateway on the previous build, CPU limited to 5% during the restart to match a loaded host. Prompt: a sub-agent runs
sleep 90; echo <token>; the gateway is restarted while the command runs.gateway timeout after 10000ms)resumed orphaned sessionlines for the one childsleep 90after the restartlane wait exceededon the child sessionRandomized interleaving test
A local randomized test (not committed) runs 2 to 3 overlapping recovery schedules over 1 to 3 interrupted children with random delays. It injects store write failures, gateway failures before acceptance, gateway timeouts after acceptance, and session id rotation. The mocked gateway dedupes on the idempotency key as the real handler does, and an accepted run clears the abort flag after a random delay, as a real run start does. A seed fails when any child is started more than once or is left with
abortedLastRunset.On this head the gateway dedupe absorbed 909 repeated resumes across the faulty runs. No child was started twice. The test also checks that the registry tracks the run that actually started: 0 misses on this head, against 42 of 300 seeds on the commit before late-accepted resumes were tracked. The test models one gateway lifecycle; the restart case is covered by the unit test above.
Unit tests
New tests in
src/agents/subagent-orphan-recovery.test.ts:resumes an interrupted child once when a second scan overlaps the first resumeresumes each interrupted child once when overlapping scans cover two childrenlets a later scan retry after a failed resume releases the claimreuses the idempotency key when a resume is retried after a gateway timeoutreuses the idempotency key when the timed-out resume rotated the session idreuses the idempotency key when the clear write fails after an accepted resumeuses a new idempotency key for a new interruption after an accepted resumeuses a new idempotency key after a gateway restart when the attempt was not recordeduses different idempotency keys for different childrentracks a resume the gateway accepted after the call timed outrecords the attempt of a tracked late-accepted resume so a new interruption gets a new keyforgets a timed-out resume once its run is finalized as faileddoes not take over a steer run that replaced the run of a timed-out resumedoes not track a timed-out resume from an earlier gateway lifecycleretries a scheduled scan that deferred a child behind another scan's claimNew tests in
src/agents/subagent-registry.test.ts:does not mark the new run of a child resumed while restored runs are being markedholds the new run of a child resumed while its own crashed mark is being writtenLocal runs:
subagent-orphan-recovery.test.tsagentandagent-wait-dedupetests (dedupe behavior relied on)src/infra/agent-events.test.ts(lifecycle generation)oxfmt --check,oxlint,node scripts/run-tsgo.mjs,git diff --checkRuntime check after the fix
Same local setup and prompt as the reproduction. Run on
16ce993e997(claim, stable key, deferred retry). Run ids come from the registry and the run files;sleep 90runs come from a process sampler in the gateway's cgroup.sleep 90after each restartOn
8b58aec2b22(adds the boot-marking fix):sleep 90after each restart0
lane wait exceededlines in all 13 runs, including the runs below. Before the fix the same lane waited 94,897 ms.The two-children run also logged a crashed mark for child A's resumed run, 15 s after its resume. No second resume followed only because that run was dispatched 2 ms after the mark, and a run start clears the mark. That race is the boot-marking fix above. In the two later two-children runs the boot loop was still running 2.6 s and 3.9 s after child A's resume, and it marked only original runs: 0 crashed marks on resumed runs.
On
54a60a957c9(adds late-accepted resume tracking). The head after it changes only that path, which did not run here:sleep 90after each restartEach child's registry row matched the run after the restart and ended complete. No
lost active execution contextorAutomatic recovery failedlines. Neither timed-out call had been accepted, so the late-accepted path did not run here; the unit tests and the randomized test cover it.An in-process restart was not exercised: under systemd the restart signal restarted the whole process.