Repository navigation
Description
Summary
A subagent that dies on a transient provider failure — most notably the terminal 180s stream-idle stall — lands in StateFailed, and resume: refuses it permanently:
Subagent: subagent "subagent-Subagent_440" ended in a failed state and is not resumable; start a fresh subagent instead
For a mode:"read-write" (direct-write, ADR 0041) child this is expensive and unsafe: the child has already edited the real workspace, so its partial edits are stranded in the tree with no continuation path. The parent's only option is to re-delegate to a fresh agent that must first rediscover the state of a half-edited tree.
session.Session.Recover() already exists, is exported, and does exactly the right thing — it is simply not wired into the child resume path.
Evidence — two independent sessions, same signature
Both are read-write children on z-ai/glm-5.2 (router category large), reconstructed from the persisted snapshots under $XDG_STATE_HOME/mecatui/sessions/:
subagent-Subagent_10 |
subagent-Subagent_440 |
|
|---|---|---|
| turns / tool calls | 58 / 83 (limits 500 / 2000) | 52 / 66 |
| cumulative input | 4.97M (4.31M cache-read) | 3.55M (3.46M cache-read) |
| history at death | 424 KB ≈ 106K tok | 321 KB ≈ 80K tok |
ConsecutiveFailures |
0 | 0 |
| wall clock | 887s (14m47s) | — |
| last persisted record | a normal, non-error tool result | a normal, non-error tool result |
| snapshot | state:"failed", stop_reason:"error" |
state:"failed", stop_reason:"error" |
No limit was hit, the history is well-formed and fully paired, and each died on the next provider call after a clean tool result — i.e. runTurn returned an error at engine/agent/loop.go:1114. In both sessions the parent then attempted resume: and was refused, discarding ~15 minutes and ~5M tokens of applied work.
Root cause
Site 1 — engine/agent/subagent.go:2899 resolveResumeSession. The per-state switch handles StateCompleted→Reopen, StateCancelled→Interrupt, StateIdle→as-is, and hard-refuses StateFailed. The doc-comment states the rationale:
StateFailedstays NOT resumable here even thoughloadAndReopennow recovers a failed MAIN session viaRecover(issue #51). A subagent is a one-shot delegated task: a failed child carries no accumulated-user-context cost, so the parent re-delegates instead of retrying a broken transcript.
The premise is false for a long-running direct-write child. The accumulated cost is 50+ turns of exploration plus mutations already applied to the real tree — strictly more expensive to discard than a main session's transcript.
Site 2 — engine/agent/teamsupervisor.go:1132. The same root cause: if stop != session.StopError { m.sess.Reopen() }, then m.nonResumable = true. A team member that hits one transient stall is permanently benched for the remainder of the team run.
Why a stall becomes failed rather than cancelled: llmresilience synthesizes *StreamIdleError (terminal, never retried — no-replay-after-first-chunk). It unwraps to context.DeadlineExceeded, not context.Canceled, and the run ctx is never cancelled (only the inner per-attempt ctx). So the loop's errors.Is(err, context.Canceled) || ctx.Err() != nil guard at loop.go:1115 misses it and it falls through to StopError → sess.Fail().
Note the contrast: a per-call timeout_ms expiry does cancel the run ctx, so it lands in StateCancelled and is already resumable via Interrupt. A network hiccup is treated more harshly than an operator-configured timeout — the inversion this issue fixes.
Proposed fix
Recover()'s own doc-comment already describes this scenario as the motivating case:
so a transient provider failure (an upstream 5xx that exhausted the resilience layer's retries) degrades to "retryable" instead of permanently bricking the session (issue #51)
resolveResumeSession: addcase session.StateFailed:→loaded.Recover(), mirroring theReopen/Interruptarms (including the same "not in a resumable state" error wrapping when the transition itself fails).RecoverrunscloseOutInterruptedTurn(recoverCloseOutMessage), so any tool call orphaned by the failed turn gets a synthetic error result and the replayed history stays provider-valid — the failure-accurate wording, never the cancellation wording.teamsupervisor.go: recover aStopErrormember instead of benching it, sononResumableis reserved for a genuinely unrecoverable session (aRecoverthat itself fails).- Keep the existing resume ergonomics: fresh fork + staleness note, tighten-only limits,
Rehome, and the in-flight guard. - Update the falsified doc-comment rationale at
subagent.go:2884rather than leaving it contradicting the code.
Acceptance
- A
read-writesubagent killed by a stream-idle stall is resumable byagentIdand continues against the real tree. - A recovered child whose failed turn orphaned a tool call replays without a dangling
tool_use/function_call(ValidateToolPairingholds). - A
Recoverfailure still yields a model-addressable tool error — no silent retry of a genuinely broken transcript. - Recovery makes retry possible, not guaranteed (per
Recover's contract): a permanent-cause child re-fails cleanly. - A team member that hits one transient stall still participates in later rounds.
Related: #51 (Session.Recover for main sessions), ADR 0041 (direct-write subagent). The companion diagnosability issue is filed separately — with today's code the reason a child failed is unrecoverable, which is why this took a snapshot-forensics session to diagnose.
Activity
fix(subagent,team): name the resume mode, fence the failure body, dro…
fix(llmresilience): log the breaker rejection; stop calling an operat…
docs: fold ADR 0077's amendment into its Decision; disclose the membe…
fix(subagent): enumerate the resume-note matrix; a writable resume is…
fix(agent): normalise before matching, cover every result arm, and ke…
fix(subagent,llmresilience): one EvSubagentEnd constructor, an unbrea…
docs: finish the ADR-0077 fold and correct four claims the code does …
fix(subagent): name a next action on the structured-output terminal, …
fix(agent): tag the framing markers by surface, and close the judge, …
fix(mecatui): strip the Cf class the engine's own fence normalises away
docs: correct the structured-output justification and the pre-#319 di…
fix(subagent): recover a failed delegated child instead of refusing t…
fix(subagent): close the review findings on the failure-cause + faile…
feat(team): retry a member that hit one transient stall instead of be…
fix(subagent): accurate next actions on every failure terminal; drop …
test(jq): de-flake the heap-watchdog guard by fixing its ORACLE, not …
fix(subagent,team): name the resume mode, fence the failure body, dro…
fix(llmresilience): log the breaker rejection; stop calling an operat…
docs: fold ADR 0077's amendment into its Decision; disclose the membe…
fix(subagent): enumerate the resume-note matrix; a writable resume is…
fix(agent): normalise before matching, cover every result arm, and ke…
fix(subagent,llmresilience): one EvSubagentEnd constructor, an unbrea…
docs: finish the ADR-0077 fold and correct four claims the code does …
fix(subagent): name a next action on the structured-output terminal, …
fix(agent): tag the framing markers by surface, and close the judge, …
fix(mecatui): strip the Cf class the engine's own fence normalises away
docs: correct the structured-output justification and the pre-#319 di…