Skip to content

fix(agents): settle stale subagent runs from their own execution after Gateway restarts - #185106

Merged
steipete merged 3 commits into
mainfrom
steipete/w54-stale-run-attribution
Oct 26, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/w54-stale-run-attribution

Conversation

@steipete

@steipete steipete commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes false subagent failures and stuck parent batches after a Gateway crash when child sessions have since been reused.

User Impact

Parents receive the result attributable to their own child run. Interrupted plain spawns settle promptly after restart without changing a newer turn on the same session. No configuration changes or migration are required.

Why This Change Was Made

After a Gateway crash, the registry sweeper could settle a stale subagent run with another execution's timestamps. settleStaleActiveSubagentRun reconciled from the session's current state and checked only timestamp freshness. When a later execution on the same session had ended, the stale row borrowed that execution's end time without its reply. The completion owner then correctly rejected it: subagent run ended before producing a final reply. The parent received a false failure even though the mapped execution had written a visible reply. In live three-level fleet runs with Gateway kills, this produced about two false failures per run after #184925.

Fix:

  • One shared recovery mapping (resolveRecoveredSubagentExecutions) from a recovered execution to its registered run. Both the lifecycle listener (added in fix(agents): recover interrupted nested requester wakes after Gateway restart #184925) and the sweeper use it.

  • The sweeper settles a stale run only from evidence attributable to that run or its mapped recovered execution. The in-process agent-job snapshot is preferred, since it carries the reply. Otherwise the persisted session completion is used when lastRunId matches, together with that execution's persisted reply. This keeps prompt settlement for runs that ended in a previous Gateway boot: a run finished, the Gateway died before the registry recorded it, and the next boot settles it as ok.

  • Unattributable evidence is no longer borrowed. Such rows stay with their existing owners and the existing bounded stale cutoff, then use the existing visible failure path.

  • Registry restart recovery now settles an interrupted plain spawn run whose session was reused by later turns before restart recovery ran. The ownership check correctly refuses to mark the newer turn interrupted, but recovery then skipped the old row because it had no interruption marker. It stayed running forever under the dead lifecycle generation, and with attributable-only settlement its parent batch would wait for the 2 h stale cutoff. The row is now finalized as interrupted-recovery and the parent is informed; the newer session is preserved.

Exact task ownership is unchanged. Later parent follow-ups (sessions_send) are not credited to older runs. There is one parent completion per registered run.

Evidence

  • New regressions (real SQLite and session state, runtime teardown and reinit): a stale pause-wake whose mapped execution ended with a visible reply settles ok with that reply; an unmapped stale row is not settled from borrowed timestamps; duplicate events or sweeps deliver once; a run that ended successfully before a restart, with no agent-job snapshot, settles promptly as ok with its reply. Red on the respective bases, green after.
  • Crabbox, 18 owning and consumer suites twice: 1,020 test executions passed. oxfmt, oxlint and typecheck are clean.
  • Regression for the reused-session interrupted spawn: red on the previous commit (row stays running, delivery pending), green after.
  • Live Crabbox deep crash scenario (three-level fleets on both agent runtimes, Gateway kill -9 at +120 s and +300 s):
  • Known inherited failure: subagent-registry.test.ts "suppresses stale timeout announces when the same child run later finishes successfully" fails identically on origin/main 4a1cda8 (1 failed | 102 passed) and is tracked separately.

CI result

The exact-head CI run 40412838433 found one inherited failure in server.goal-child-authority.product.test.ts: the line 620 transcript assertion expects two user messages but also sees two hidden internal_system goal-continuation messages. The same test fails identically on main 605f1aa3a0d using node scripts/run-vitest.mjs src/gateway/server.goal-child-authority.product.test.ts --maxWorkers=1. The test file is byte-identical on main and the tested PR merge. Landing uses the authorized native inherited-failure route; no assertion or runtime code was changed to bypass this failure.

…ution

Share requester-wake recovery execution mapping between the lifecycle listener
and registry sweep. Resolve terminal timestamps and prepared replies through
the exact agent-job record, preserving the registered run's parent obligation
instead of borrowing a later session execution's timestamps.

Leave unknown execution identity with existing recovery and timeout owners,
then report the existing bounded stale-run failure. Keep completion ownership
and duplicate delivery suppression unchanged.

Cover recovery claims and receipts, unrelated later parent follow-ups,
duplicate events and sweep passes, bounded unknown-run failure, and yielded
collector results. Update existing sweep fixtures to supply exact terminal
evidence while retaining their lifecycle, retry, and routing assertions.

Validation: all 18 W51/W54 and affected consumer suites passed twice on the
Crabbox runner (1,016 test executions). New wake regressions failed on W51 base
and passed with the fix. Box oxfmt, oxlint, targeted typecheck, and independent
review passed.
…ous boot

Prefer the live agent-job snapshot, then recover terminal state only when
session.lastRunId matches the registered or mapped recovered execution.
Read that exact execution's persisted visible final so a Gateway restart
does not strand successful child work or borrow a later run's completion.

Keep the existing settlement and required-final owners. No schema changes.
Cover registered and recovered executions across SQLite runtime restart,
including restored replies and exactly one parent completion.

Validation: all 18 targeted suites twice, remote oxfmt/oxlint and targeted
typecheck. Both new restart cases fail on dde16336890 and pass with this fix.
Recover a retired plain spawn from its registry row when a later turn already
owns the child session. Finalize through interrupted recovery and notify its
parent without changing the newer session or borrowing its completion evidence.
Keep hidden recovery successors with their existing task-lineage owner.

Cover later active and completed turns through SQLite shutdown/reopen, assert
recovery ownership during delivery, and prevent duplicate delivery after another
restart. Document the retained-task ownership boundary.

Validation: both new regression cases fail on f23f0bc and pass twice here.
Run the W54 18-suite set twice; 17 suites pass, while the timeout-then-success
reply case in subagent-registry.test.ts fails identically on the unchanged base
(missing completion.terminalReply). Remote format, lint, targeted typecheck,
docs inventory, and independent review pass.
@clawsweeper

clawsweeper Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: L labels Oct 26, 2026
@steipete
steipete marked this pull request as ready for review October 26, 2026 22:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T22:32:18.706934Z 46a6406 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@steipete
steipete merged commit c893f1e into main Oct 26, 2026
214 of 221 checks passed
@steipete
steipete deleted the steipete/w54-stale-run-attribution branch October 26, 2026 22:45
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
…r Gateway restarts (openclaw#185106)

* fix(agents): settle stale subagent runs from their own recovered execution

Share requester-wake recovery execution mapping between the lifecycle listener
and registry sweep. Resolve terminal timestamps and prepared replies through
the exact agent-job record, preserving the registered run's parent obligation
instead of borrowing a later session execution's timestamps.

Leave unknown execution identity with existing recovery and timeout owners,
then report the existing bounded stale-run failure. Keep completion ownership
and duplicate delivery suppression unchanged.

Cover recovery claims and receipts, unrelated later parent follow-ups,
duplicate events and sweep passes, bounded unknown-run failure, and yielded
collector results. Update existing sweep fixtures to supply exact terminal
evidence while retaining their lifecycle, retry, and routing assertions.

Validation: all 18 W51/W54 and affected consumer suites passed twice on the
Crabbox runner (1,016 test executions). New wake regressions failed on W51 base
and passed with the fix. Box oxfmt, oxlint, targeted typecheck, and independent
review passed.

* fix(agents): keep persisted completion evidence for runs from a previous boot

Prefer the live agent-job snapshot, then recover terminal state only when
session.lastRunId matches the registered or mapped recovered execution.
Read that exact execution's persisted visible final so a Gateway restart
does not strand successful child work or borrow a later run's completion.

Keep the existing settlement and required-final owners. No schema changes.
Cover registered and recovered executions across SQLite runtime restart,
including restored replies and exactly one parent completion.

Validation: all 18 targeted suites twice, remote oxfmt/oxlint and targeted
typecheck. Both new restart cases fail on dde16336890 and pass with this fix.

* fix(agents): settle interrupted spawn runs after session reuse

Recover a retired plain spawn from its registry row when a later turn already
owns the child session. Finalize through interrupted recovery and notify its
parent without changing the newer session or borrowing its completion evidence.
Keep hidden recovery successors with their existing task-lineage owner.

Cover later active and completed turns through SQLite shutdown/reopen, assert
recovery ownership during delivery, and prevent duplicate delivery after another
restart. Document the retained-task ownership boundary.

Validation: both new regression cases fail on f23f0bc and pass twice here.
Run the W54 18-suite set twice; 17 suites pass, while the timeout-then-success
reply case in subagent-registry.test.ts fails identically on the unchanged base
(missing completion.terminalReply). Remote format, lint, targeted typecheck,
docs inventory, and independent review pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant