Skip to content

[ENG-20801] fix(agents): resume sub-agents interrupted by a gateway restart - #266

Merged
orswn merged 8 commits into
boonfrom
okka/ENG-20801/resume-interrupted-subagents
Oct 7, 2026
Merged

orswn merged 8 commits into
boonfrom
okka/ENG-20801/resume-interrupted-subagents

Conversation

@orswn

@orswn orswn commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: ENG-20801

What Problem This Solves

Fixes an issue where background sub-agents were lost when the Gateway crashed, stopped, or restarted at the drain cap while they were running. The child ended with "subagent run lost active execution context" or "gateway closed (1012): service restart", and its work was gone. On a restart at the drain cap, the user also got a "Background task cancelled" notice and a false "My last reply to you may not have gone through" notice. The parent then offered to start the whole sub-agent again from scratch.

Why This Change Was Made

The orphan recovery that resumes a sub-agent in place already exists. It runs for a child whose run is unended and whose session has abortedLastRun. Restart interruptions never reached that state:

  • after kill -9, the registry sweeper finalized the child about 60 s after boot;
  • at the drain cap and on SIGTERM stop, the run wait saw the 1012 socket close and finalized it as an error.

This change makes those interruptions reach the existing recovery path:

  • Close. Every stop or restart request marks the gateway as closing. While it is closing, a non-ok end of a native sub-agent run stays unended and gets abortedLastRun. One predicate covers the lifecycle listener, the run wait, the run timeout and the sweeper.
    • Explicit kills and steers still finalize.
    • ACP and plugin runs are excluded.
    • A run held at close stays for orphan recovery, even when the registry sweeper runs after an in-process restart.
    • The task registry uses the same predicate. The sub-agent task is not cancelled, so no "Background task cancelled" notice goes out.
  • Boot. Registry restore marks a child session that a crash left running, before boot waits are armed.
    • A run that a crash left running is not resumed after 2 h.
    • Runs past their runTimeoutSeconds deadline are not resumed either. They end as a timeout, with one outcome.
  • Resume. When recovery replaces the run, the sub-agent task follows the new run id. Before, the old task stayed running, and every later restart waited the full deferral timeout.
    • The resume prompt now says that tool calls may have been cut off, and asks the child to check what already ran before it repeats a side effect.
  • Parent. A parent resumed by main-session recovery is told which sub-agents are still running or resuming, so it does not start them again.
  • Switch. agents.defaults.subagents.restartResume defaults to true. With false, interrupted runs end with an error, as before.
  • Logs. Info lines record each keep, mark, deadline skip and age skip, with the run id and the child session key.

This diverges from upstream. Upstream 2026.9.x stopped relaunching interrupted sub-agents and leaves continuation to the parent (openclaw#153243). This fork keeps the child. A resumed child repeats at most the one tool call that the restart cut, and only after it is told to check. A restart today loses the whole child. A later upstream sync must carry or drop this change.

Not in this PR:

  • A resumed turn is written to the internal run trajectory, not to the child session transcript. After a second restart, the child's history does not include the first resume's tool calls. A TODO(okka) marks it.
  • The parent note can name a sub-agent that is past its deadline or the 2 h crash bound. That sub-agent later ends with an error completion. A TODO(okka) marks it.
  • No release, no tag and no version bump.

User Impact

  • A background sub-agent cut by a crash, a restart or a systemctl restart resumes after boot and delivers its real result. Its Work Status row stays running and closes as done.
  • No "Background task cancelled" or false "may not have gone through" notice for a restart.
  • The parent no longer offers to start a sub-agent again that is still resuming.
  • A sub-agent whose resume keeps failing ends with an error that names the restart.
  • Boxes that do not want this can set agents.defaults.subagents.restartResume: false.

Evidence

  • Focused Vitest: 32 files, 638 tests passing, run with node scripts/run-vitest.mjs. The CI sub-agent shard passed 5 runs in a row locally with 2 workers, and once more on the final commit (986 tests). The new tests were written first and failed before the change, or were proven by mutation.
  • CI: every check passes except security-fast. That check fails on pnpm audit advisories in dependencies this PR does not change. It fails the same way on [ENG-20522] fix(pdf): large plan sets read nothing past the page cap and scans get no page images #265.
  • tsgo core and core-test, oxfmt --check, oxlint, the import-cycle check, the config baseline check and git diff --check all pass. node scripts/build-all.mjs exits 0.
  • Live check on a production-parity box config:
    • this branch, the Boon Web channel plugin and Boon;
    • a real model;
    • 12 scenarios, each run once.
Fault while a background sub-agent runs Before After
kill -9 child lost about 68 s after the kill; false done mark resumed 13.5 s after the kill; real result 272 s after the kill; one Work Status row, done
SIGUSR1 at the 20 s drain cap "Background task cancelled", "may not have gone through" and "cut off" notices no extra notices; resumed 34 s after the signal; real result
systemctl restart child finalized as an error (by code; not measured) resumed 8.6 s after the restart; real result
kill -9 twice, 59 s apart n/a resumed twice; real result; no stale task
SIGUSR1 with no work, after a resume the stale task held the restart for 300 s restart starts at once; no deferral
kill -9, restartResume: false child lost child lost, as before; no resume

Parent-turn restart recovery was re-checked on the same build. It is unchanged.

The table comes from a build just before the decision logs were added. A re-check on the final code ran kill -9, the drain cap and a cut completion turn once each. All three resumed and delivered. The keep and mark log lines appeared with their ids.

Screenshots. The full table with the log lines is in a comment below.

Sub-agent cut by kill -9: still running after the restart, then the real result and a done row

Sub-agent cut by a restart at the 20 s drain cap: no notices, then the real result

Parent turn cut while a sub-agent runs: Continue, parent says the sub-agent is still running, then its result

Resume switched off: the sub-agent is lost and the row shows stopped

A gateway restart, stop or crash ended every running native sub-agent.
After kill -9 the registry sweeper finalized the child about 60 s after
boot with "subagent run lost active execution context". At the restart
drain cap and on SIGTERM stop, the run-manager wait saw the 1012 socket
close and finalized it as an error. The task registry then announced
"Background task cancelled" for it. The child's work was lost and the
user had to ask for a full re-run.

The orphan recovery that resumes a child with abortedLastRun already
exists, but restart interruptions never reached it. Make them reach it:

- Mark the gateway as closing at the start of every stop or restart
  request. While it is closing, a non-ok, non-explicit end of a native
  sub-agent run keeps the run unended and sets abortedLastRun on the
  child session. The listener, the wait path, the timeout path and the
  sweeper share one predicate. Explicit kills and steers still finalize.
  ACP and plugin runs are excluded.
- The task registry uses the same predicate. A held sub-agent task is
  not patched to cancelled. The cli task for the same run still is.
- On boot, a restored run whose child session was left "running" by a
  crash is marked before boot waits are armed. Runs older than 2 h, or
  past their run deadline, are not resumed. A marked run gets no boot
  wait, so it cannot end as a timeout and then resume.
- When orphan recovery replaces the run, the sub-agent task is re-keyed
  to the new run id. Before, the old task stayed running forever and
  every later restart waited the full drain deferral.
- The resume prompt tells the child that tool calls may have been cut
  off, and to check what already ran before it repeats a side effect.
- A parent resumed by main-session recovery is told which sub-agents
  are still running or resuming, so it does not start them again.
- Log each keep, boot mark, deadline skip and age skip with the run id
  and the child session key.
- agents.defaults.subagents.restartResume (default true) turns all of
  this off.

Refs: ENG-20801
@orswn
orswn requested a review from a team October 6, 2026 07:21

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 30 files

Tip: instead of fixing issues one by one fix them all with cubic

Re-trigger cubic

Comment thread src/agents/subagent-restart-resume.ts Resolved
Comment thread docs/gateway/config-tools.md Resolved
Comment thread src/agents/subagent-orphan-recovery.ts Resolved
Comment thread src/process/command-queue.ts Resolved
Comment thread docs/.generated/config-baseline.json Resolved
Registry restore always deferred its boot waits and the orphan scan
behind the async restart-resume pass. Restores with no run that could
need a resume mark paid that delay too, and the announce loop guard
test timed out on a slow runner.

Take the async pass only when restart resume is on and at least one
restored native sub-agent run is unended and not paused by
sessions_yield. Otherwise arm the boot waits synchronously.

Also list the explained warning-comment suppression in the production
lint-suppression allowlist.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

Live check: background sub-agents through a gateway restart.

Setup:

  • this branch, the Boon Web channel plugin (getboon/anychat-boon-web#157) and Boon (getboon/boon#15406);
  • a production-parity box config;
  • Claude Opus 4.8 with medium thinking;
  • the child runs sleep 240 (or sleep 90 in the re-check) and then prints a marker, so the restart lands mid-tool;
  • times are ms after the fault.
Fault Result Numbers
kill -9 at child +60 s resumed in place; real marker; one Work Status row, running then done resumed +13,461; result +272,203
SIGUSR1 at child +30 s, 20 s drain cap no "Background task cancelled", no "may not have gone through", no "cut off" notice; resumed; real marker forced +20,053; resumed +34,082; result +288,982
systemctl restart at child +30 s resumed; real marker resumed +8,604; result +208,987
kill -9 twice, 59 s apart resumed twice; real marker; no stale task resume 2 at +13,666 after kill 2
SIGUSR1 with no work after a resume the task followed the new run id, so nothing held the restart restart began at +4
kill -9 at parent tool, child running parent halt and Continue. The continued answer said the child was still running, and it was not started again. Child result arrived. 1 spawn result +158,219
kill -9 during the completion turn the turn replayed with the "cut off" note; one final row final +81,059
kill -9 with restartResume: false as before: child lost and reported as stopped lost +68,053
Parent-turn recovery, flag on and off unchanged; correct token both times

Re-check on the final code, kill -9 and drain cap, one run each:

  • marked crashed subagent session for restart resume with runId and childSessionKey, logged at boot +8,379;
  • kept subagent run for restart resume with outcomeStatus: "error" and source: "listener" and "wait", logged at +20,110;
  • both children resumed and delivered their marker.

Known behavior:

  • A resumed child runs the tool that the restart cut again. The child first checks what is still running. 2 runs of the same sleep per restart were seen.
  • The old CLI task of a crashed run stays running until the 5 min task sweep marks it lost.

Each case ran once.

Sub-agent cut by kill -9: still running after the restart, then the real result and a done row

Sub-agent cut by a restart at the 20 s drain cap: no notices, then the real result

Parent turn cut while a sub-agent runs: Continue, parent says the sub-agent is still running, then its result

Resume switched off: the sub-agent is lost and the row shows stopped

Document that a run past its deadline ends as a timeout, that a run a
crash left running for more than 2 h is not resumed, and that after a
second restart the child may not see its first resume's tool calls.

Report isGatewayClosing() as false for a command queue singleton from
an older restart generation that lacks the field, instead of
undefined.

Reset the task registry after every steer restart test, and check that
the orphan scan sees the held run in the persistence test.

Mark the parent note gap: it can list sub-agents that will end with an
error rather than resume.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 6, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@orswn I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 31 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread src/process/command-queue.ts Resolved
Comment thread src/agents/subagent-registry.test.ts Resolved
An in-process restart clears the gateway closing flag when it resets
the command lanes. The registry sweeper keeps running across that
reset, so a run held at close could reach its session completion check
and be finalized as killed before orphan recovery resumed it.

Make the sweeper leave an interrupted run whose session has
abortedLastRun to orphan recovery, before it looks at the session
completion. Orphan recovery resumes the run or ends it when its retry
gate refuses.

Replace the broad logging mock in the registry test with an explicit
factory.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 6, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@orswn I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 31 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread src/agents/subagent-registry.ts Resolved
Comment thread src/agents/subagent-registry.ts Resolved
…g them

A run whose runTimeoutSeconds deadline had passed could still be held
at gateway close, because the hold looked only at the closing flag.
After an in-process restart the restore pass does not run again, so the
sweeper then handed that run to orphan recovery and it ran past its
deadline.

Do not hold a run past its deadline at close; it ends as a timeout.
Have the sweeper leave a held run to orphan recovery only while the run
is within its deadline.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 6, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@orswn I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 31 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread src/agents/subagent-restart-resume.ts Resolved
Comment thread src/agents/subagent-registry.test.ts Resolved
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

Relation to the runtime guardian design

The runtime guardian design said "Do not change openclaw itself in this version." It expected an interrupted sub-agent to stop with its parent, and the continued parent turn to spawn it again.

This PR changes that part. A restart dropped background sub-agent work that was already under way. Spawning the sub-agent again loses its progress and repeats its work.

This PR adds:

  • An interrupted native sub-agent resumes in place after the next boot.
  • A per-box switch agents.defaults.subagents.restartResume, default true.
  • The resume text says that a cut tool call may have partly run.
  • A continued parent turn is told which sub-agents still run, so it does not spawn them again.

The rest of the design is in getboon/boon#15406 and getboon/anychat-boon-web#157:

  • the drain;
  • the turn hand-off;
  • the automatic continuation.

The full comparison with the design is here: https://github.com/getboon/boon/pull/15406#issuecomment-6015434868

…resume tests

The boot restore tests compared a session updatedAt ten minutes in the
past with the process start time. Fake timers do not fake
process.uptime(), so in a worker that had run for over ten minutes the
crash branch flipped. Pin the uptime in that block and restore it after.

The repeated-resume test seeded the wedged state it was meant to
produce, so it passed even with the attempt limit changed. Seed only the
attempts and assert the wedge write.

Correct the restart-resume module comment: the task registry only skips
its own terminal patch for a held run, and the run registry's finalize
settles the task.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 6, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@orswn I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 31 files

You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread src/agents/subagent-restart-resume.ts Resolved
The resume age read sessionStartedAt, startedAt and createdAt with
plain fallbacks, so a non-finite value produced a NaN age. The stale
run cutoff already uses getSubagentSessionStartedAt, which skips
non-finite values.

Use the same helper for the resume age, so the two 2-hour bounds read
one start time. With no finite start time the age is infinite and the
run is not resumed, as before.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 6, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@orswn I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 31 files

You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread src/agents/subagent-restart-resume.ts Resolved
Comment thread src/agents/subagent-restart-resume.ts Resolved
…ound

The stale-run check prunes a run only when it is older than two hours,
but the boot pass treated a run of exactly two hours as too old. Such a
run was neither pruned nor resumed. Accept the boundary, as the stale
check does.

Reword the boot action descriptions so "too-old" and "none" no longer
read the same.

Refs: ENG-20801
@orswn

orswn commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 6, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@orswn I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 31 files

You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Turn on auto-fix | Re-trigger cubic

@boon-hermes-bot boon-hermes-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at head a9562c7c0c against origin/boon (8 commits, 31 files, +1741/-43).

Local verification (worktree at head, pnpm install --frozen-lockfile --ignore-scripts):

  1. All 11 touched test files pass via node scripts/run-vitest.mjs: 6 shards, 368 tests visible in the tail, exit 0. Includes subagent-registry.test.ts boot-restore block, subagent-restart-resume.test.ts, task-registry.test.ts, command-queue.test.ts, run-loop.test.ts.
  2. Mutation check: with the mark-and-hold action neutered in resolveRestoredSubagentRunResumeAction, exactly 5 tests fail across subagent-restart-resume.test.ts and subagent-registry.test.ts (incl. "marks a running session left by a crash and leaves the run to orphan recovery"). The new tests pin the resume decision. File restored after the check.
  3. pnpm tsgo:core exit 0; scoped oxlint on changed prod files exit 0; oxfmt --check clean; git diff --check clean.

CI at exact head: all check-runs success except neutral CodeQL. No security-fast run exists on this head; the pnpm-audit failure the body mentions is not present at a9562c7c0c.

Cubic thread adjudication (27 resolved; 17 resolved-not-outdated re-checked against head code):

  • Claimed fixes verified in the tree: lane-reset sweeper keep (82ad500e72 — the execution.status === "interrupted" && abortedLastRun === true guard is in sweepSubagentRuns), process.uptime() stub in the boot-restore block (49ca356583, lines 3123-3137), getActiveRuns() containment assert in the persistence test, wedge gate self-tombstoning in subagent-orphan-recovery.test.ts.
  • "Not changing" replies hold up in code: the 2 h bound applies only to crash-left-running runs (resolveRestoredSubagentRunResumeAction checks age only on the mark-and-hold leg, hold is unbounded — matches the reply); the task-registry path omitting run is settled by the run registry's finalize (safeFinalizeSubagentTaskRun / failTaskRunByRunId); explicit kills still finalize via isExplicitAgentAbortStopReason / cause: "explicit-kill"; registerSubagentRun does create the running task (createRunningTaskRun in subagent-registry-run-manager.ts:719), refuting the steer-restart test concern.
  • docs/.generated/config-baseline*.json is tracked on this fork's boon branch (release commits update it), so including it is correct here.

Code reads: the close predicate (shouldKeepSubagentRunUnendedOnGatewayClose) is single-sourced across lifecycle listener, run wait, timeout, sweeper, and task registry; ok outcomes and explicit kills finalize normally; restartResume defaults to true via !== false with zod schema + docs (docs/tools/subagents.md, docs/gateway/config-tools.md) aligned; rekeySubagentTaskRunId points the live task at the replacement run. The two declared gaps (resumed turns not in the child transcript; parent note can list soon-to-timeout runs) are marked TODO(okka) and disclosed in the body.

The live-restart evidence table is the author's own box run; I did not reproduce kill -9 / drain-cap scenarios locally. Everything verifiable from the tree checks out. Approving.

@orswn
orswn merged commit 9d0a37b into boon Oct 7, 2026
146 of 150 checks passed
@orswn
orswn deleted the okka/ENG-20801/resume-interrupted-subagents branch October 7, 2026 04:50
orswn added a commit that referenced this pull request Oct 7, 2026
…4-boon.2 (#267)

Cut the fork release that ships #266 (ENG-20801), the only commit on
boon since v2026.6.34-boon.1. An interrupted native sub-agent now
resumes after a gateway crash, stop or restart instead of ending with a
lost-context error.

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.2 CHANGELOG.md section.
No dependency or source changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants