Skip to content

Abandoned background subagent loses its failure cause to the post-seal emit race #332

Closed

Description

@JAORMX

Summary

Found during the fix-verification audit of PR #320 (issue #319). A narrow race lets a background subagent's failure cause die before it reaches the parent's durable .events.jsonl.

The race

At run end, drainChildren (engine/agent/loop.go) cancels + joins live background children in two phases (childDrainCap ~10s, then abortEmits() + childDrainGrace ~1s), then SEALS the child registry — after which every child emit routes through safeEmit and a post-seal emit is a no-op.

The lossy shape: a background child determines StopError (a terminal provider error) at roughly the same instant the parent's drain cap expires. driveChild returns the cause, but the child's subagentEndEvent emit (engine/agent/subagent.go ~2605) fires AFTER the seal — so the cause never reaches the parent's event stream or its .events.jsonl.

The child IS persisted by persistChild (SessionStore.Save) — but only its snapshot (state: failed, stop_reason: error). session.Session has no cause field, and child runs are never relayed (EventLog.Append happens at the relay; children are not), so the child's own EvResult.Error is not durable either. The cause is unrecoverable from any durable log.

Why it's narrow

In the common flow cancelLiveBackground cancels the child's ctx first, so the child ends StopCancelled — no cause by design (a cancellation). The lossy case only arises when the child ALREADY determined StopError but its end-emit lands post-seal. This is a genuine shape (a provider error concurrent with the drain cap expiring) but outside #319's synchronous evidence.

Possible directions

  • Widen drainChildren's grace for a child that has ALREADY reached its terminal result (its doneCh is about to close) vs one still mid-turn — a terminal child needs only to flush one end event, not finish a turn.
  • Or persist the terminal cause onto the child snapshot itself (a last_error field on Session), so persistChild captures it independent of the emit race — this also fixes the "child runs are never relayed" gap for the terminal cause specifically.
  • Or accept and document: the cause is best-effort on the background path, and the synchronous path (A failed subagent's actual cause is dropped four times — the error shown is the child's last chat line #319's scope) is the guaranteed one.

Related: #319, PR #320.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions