Skip to content

fix(subagents): distinguish child termination from wait expiry - #135491

Closed
zeroaltitude wants to merge 4 commits into
openclaw:mainfrom
zeroaltitude:fix/subagent-timeout-disposition
Closed

zeroaltitude wants to merge 4 commits into
openclaw:mainfrom
zeroaltitude:fix/subagent-timeout-disposition

Conversation

@zeroaltitude

Copy link
Copy Markdown
Contributor

What Problem This Solves

A parent can receive status: timed out when only its wait expired and the child is still running. In the incident behind this change, the child continued for another 15 minutes, committed, and pushed while the parent—reasonably believing it dead—started a replacement in the same worktree.

Why This Change Was Made

The subagent outcome now carries an explicit disposition (exited, killed, or still-running) from the producer that owns the evidence. Wait expiry is provisional, preserves the child session, warns the parent not to start a replacement, and yields to the child’s later real terminal callback. Genuine hard timeouts and kills remain terminal.

User Impact

Parents stop treating a patience deadline as child death, preventing duplicate agents from editing one worktree and ensuring the actual final completion can still arrive.

Evidence

The observed run emitted a 90-minute timeout with zero tokens, then remained live for about 15 more minutes and completed successfully. Regression coverage exercises producer dispositions, provisional publication, late real completion, output wording, session preservation, and duplicate-expiry convergence. Validation on head 45e8a24621d included 1,110 subagent tests plus focused internal-event, media, and attempt-execution suites, both core typechecks, SDK checks, lint/format, and diff validation.

Written by Tank (Claude), an AI agent operated by Eddie Abrams (zeroaltitude).

…ired

A visible subagent spawned with runTimeoutSeconds=5400 delivered this to its
parent at the 90-minute mark:

    status: timed out
    Child result: (no output)
    Stats: runtime 1h30m - tokens 0 (in 0 / out 0)

The child had not stopped. It ran ~15 more minutes, committed, pushed, and
sealed a report. Three independent signals in one event all said "dead", so the
parent reasonably spawned a successor into the live child's git worktree; two
agents then worked one branch for ~12 minutes with two near-miss tree
corruptions that git cannot detect (a half-reverted tree moments before a
confident commit, and one agent's unreviewed commit sitting on the other's
shared HEAD).

Root cause, two halves.

First, subagent-registry-run-wait.ts gated the terminal publication on
`isTerminalWaitTimeout || hardRunTimeoutEndedAt !== undefined`. Those disjuncts
carry unrelated evidence: the first means agent.wait returned a terminal
snapshot (endedAt/stopReason/livenessState) and the run really stopped; the
second is clock arithmetic on our own stored deadline and says nothing about the
run. Both collapsed into `outcome: { status: "timeout" }`, the only signal
downstream, so the announce rendered "timed out", the result fell back to
"(no output)", and the stats line read the child's not-yet-flushed usage
counters as "tokens 0". agent-run-terminal-outcome.ts already separates these
("hard_timeout" vs wait-layer "timed_out"); the subagent projection discarded
the distinction.

Second, having published that, shouldPreservePublishedExplicitRunTimeout in
subagent-registry-lifecycle-completion.ts made it an immovable terminal winner.
The child's real completion 15 minutes later was dropped on the floor, which is
why no corrected event ever arrived.

Records the disposition as a fact at each producer that owns it, rather than
leaving parents to reconstruct liveness from journalctl provenance lines and a
worktree reflog. AgentRunDisposition ("exited" | "killed" | "still-running")
rides on the run outcome and on the completion event, written only where
something other than a plain terminal state was observed:

  - the wait manager, when the stored deadline expired with no snapshot;
  - applySubagentWaitOutcome, splitting hard_timeout (exited) from a bare wait
    timeout (still-running) and cancellation (killed);
  - the announce flow, when this process can still see the run executing;
  - the kill path in completeSubagentRunAttempt.

A still-running event now says "still running; the wait for it expired, it did
not", carries a machine-readable `disposition:` line, reports "(no result yet;
child still running)" instead of "(no output)", labels its stats "waited 1h30m"
with "child tokens not yet reported" instead of a zeroed total, and instructs
the parent not to start a replacement or touch the state the child still owns.
The child's session is no longer deleted under it, and the publication is
treated as provisional: when the run's own terminal callback finally lands, the
delivery and cleanup bookkeeping is released so the parent hears the real
ending. A second expiry cannot re-announce, so it converges.

Production LOC +119. The growth is the recorded fact itself and its comments;
there is no way to express "the child is alive" through a shape whose only
liveness channel was a four-value status enum.

Two existing tests encoded the old behavior and were retargeted, not weakened:
"keeps published explicit timeout stable when late lifecycle timeout arrives"
now supplies a real terminal snapshot so it still guards duplicate-callback
idempotency, and a new sibling proves a wait-expiry publication yields to the
child's own ending.

Validation (topic worktree):
  pnpm tsgo:core                                     exit 0
  pnpm tsgo:core:test                                exit 0
  pnpm vitest run src/agents/subagents               40 files, 1110 passed
  pnpm vitest run src/agents/internal-events.test.ts 3 passed
  pnpm vitest run src/agents/tools/media-generate-background-shared.test.ts
                                                     27 passed
  pnpm vitest run src/agents/command/attempt-execution.shared.test.ts
                                                     10 passed
  pnpm plugin-sdk:surface:check                      exit 0
  pnpm oxlint <touched files>                        exit 0
  oxfmt --check <touched files>                      clean
  git diff --check                                   clean

Every new regression test was verified to fail on pre-fix code for the intended
reason (production files stashed): 3 wait-manager, 5 announce-event, 1
lifecycle-supersede, 1 event-rendering.

Empirical re-validation on a live gateway is deliberately not done here: this
wording is visible to every agent and the rollout is the operator's call.

Beads: openclaw-kkv1
Two import-block conflicts, both from main moving the subagent classifier into its
own module (classifySubagentTerminalOutcome in subagents/subagent-terminal-outcome.ts,
openclaw#125407) so the base classifier is no longer imported at these sites.

Resolved by usage rather than by preference. For each file the conflict block was
excised before counting, so the counts reflect real call sites and not the markers
themselves:

  subagent-announce-output.ts      base classifier called: no   AgentRunDisposition: yes
  subagent-registry-run-wait.ts    base classifier called: no   AgentRunDisposition: yes

So both files take main's narrowed import of buildAgentRunTerminalOutcomeFromWaitResult,
drop classifyAgentRunTerminalOutcome as genuinely unused after main's refactor, and keep
this branch's `AgentRunDisposition` type import, which has real usages and carries the
branch's disposition tracking.

Accepting main's refactor while preserving branch utility, per instruction.

NOT YET VALIDATED: pnpm install still running in this worktree; no typecheck yet.
# Conflicts:
#	src/agents/subagents/announce/subagent-announce-output.ts
@clawsweeper

clawsweeper Bot commented Sep 1, 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 agents Agent runtime and tooling size: L r: too-many-prs Auto-close: author has more than twenty active PRs. labels Sep 1, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing this PR because the author has more than 20 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling r: too-many-prs Auto-close: author has more than twenty active PRs. size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant