Skip to content

Resume subagents that died on a transient failure (stream-idle stall) instead of bricking them #318

Closed

Description

@JAORMX

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:

StateFailed stays NOT resumable here even though loadAndReopen now recovers a failed MAIN session via Recover (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)

  1. resolveResumeSession: add case session.StateFailed: → loaded.Recover(), mirroring the Reopen/Interrupt arms (including the same "not in a resumable state" error wrapping when the transition itself fails). Recover runs closeOutInterruptedTurn(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.
  2. teamsupervisor.go: recover a StopError member instead of benching it, so nonResumable is reserved for a genuinely unrecoverable session (a Recover that itself fails).
  3. Keep the existing resume ergonomics: fresh fork + staleness note, tighten-only limits, Rehome, and the in-flight guard.
  4. Update the falsified doc-comment rationale at subagent.go:2884 rather than leaving it contradicting the code.

Acceptance

  • A read-write subagent killed by a stream-idle stall is resumable by agentId and continues against the real tree.
  • A recovered child whose failed turn orphaned a tool call replays without a dangling tool_use/function_call (ValidateToolPairing holds).
  • A Recover failure 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

  1. added
    bug
    on Jul 29, 2026
  2. added 20 commits that reference this issue on Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions