Skip to content

feat(session): persist the terminal failure cause on the session snapshot (#332) - #405

Merged
JAORMX merged 1 commit into
mainfrom
issue-332-bg-cause-race
Aug 6, 2026
Merged

JAORMX merged 1 commit into
mainfrom
issue-332-bg-cause-race

Conversation

@JAORMX

@JAORMX JAORMX commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #332. A background subagent whose end-emit loses the race with the run-end seal (drainChildren's abortEmits) had its StopError cause dropped from every durable surface: the parent's subagent.end event never lands, child runs are never relayed to the EventLog, and the persisted child snapshot carried only state: failed / stop_reason: error. The cause was unrecoverable.

This PR takes the issue's option 2: persist the terminal cause on the child snapshot, so persistChild captures it independent of the emit race. Option 1 (widen the drain grace) was rejected in the plan — the parked emit is aborted, not delayed, so a wider grace can't deliver it, and touching the timing would risk the pinned TestSealUnblocksEmitParkedSend / TestCancelUnwedgesStalledTeamRun invariants.

The change (the Permanent-analog, end to end)

  • engine/session/session.go — Session.RecordLastError / Session.LastError: stamps the terminal cause on a failed session (StateFailed-only guard mirroring RecordFailurePermanence; cleared on Recover/resetToIdle). Normalised to one line + clamped to 400 runes by the session-local normaliseSnapshotError/maxSnapshotErrorRunes — the deliberate mirror of the event-side subagentCausePayload/maxSubagentCausePreview (session can't import engine/agent), pinned byte-for-byte by a two-sided agreement test (TestSnapshotCauseMirrorsEventCauseContract + TestSnapshotAndEventCauseAgreement).
  • engine/adapter/sessnap/sessnap.go — Snapshot.LastError (json:"last_error,omitempty", additive — same contract as Permanent), threaded through RestoreState's new trailing param via a recordFailedStateFlags helper.
  • engine/adapter/eventsource/eventsource.go — the fold captures EvResult.Error on a StopError terminal into the restored session.
  • engine/agent/subagent.go — recordChildCauseOnSnapshot called before persistChild on BOTH the foreground (run()) and background (driveBackground) paths — belt-and-suspenders for the former (its emit is synchronous), load-bearing for the latter (its emit can be dropped post-seal).

The drain/seal timing machinery is unchanged. The pre-run endOnError site (a fork/session-build failure, no child session exists) rides the event only — the accepted boundary, documented in the plan. No proto change: the cause already rides SubagentPayload.Cause / TeamPayload.Cause on the wire; the snapshot field is engine-domain only.

Gates

  • task api:update (engine/api/session.txt) — RecordLastError/LastError classified Added (minor) per engine/COMPATIBILITY.md, with the engine/CHANGELOG.md entry.
  • task lint && task test — green (root + engine + provider modules + standalone proofs; api-compat gate passes).
  • task docs — llms.txt regenerated (unchanged), strict link gate clean.
  • go run ./cmd/mecademo — still prints a full offline session.
  • No ADR — an additive bug fix mirroring Permanent, not a timing-guarantee change.

Tests

  • engine/agent/background_cause_race_test.go (new): the mutation-kill for the background path (snapshot LastError carries the cause; removing the RecordLastError call fails it), the foreground mirror, and the agreement test.
  • engine/session/session_test.go: RecordLastError state guard (rejects non-failed), clear-on-Recover/Reopen, normalise+clamp, and the session-side agreement pin.
  • engine/adapter/sessnap/sessnap_test.go: the LastError round-trip trio mirroring Permanent (round-trip / omitempty-empty / pre-field backward-compat).
  • engine/adapter/eventsource/eventsource_test.go: TestFoldRoundTripsFailurePermanence now asserts the folded LastError per case (incl. the clean-terminal negative).

Review

Ran the dev-pipeline panel (spec / standards / domain: secure-code, software-architect, code-duplication, library-reuse + a QA test-adequacy pass). Cross-confirmed gaps fixed before opening: the sessnap LastError round-trip tests (the mutation-kill for the marshal path), the eventsource fold assertion, the two-sided byte-for-byte clamp-agreement test, and the deletion of a permanently-skipped placeholder test. The panel's one open suggestion — surfacing LastError in InspectSubagent's rendered output — is the architect-documented follow-up (a model-facing UX change), deliberately out of scope for this durability fix.

…shot (#332)

A background subagent whose end-emit loses the race with the run-end seal
(drainChildren's abortEmits) had its StopError cause dropped from every
durable surface: the parent's subagent.end event never lands, child runs
are never relayed to the EventLog, and the persisted child snapshot
carried only state/stop_reason. The cause was unrecoverable.

Persist it on the child snapshot, the Permanent-analog:
Session.RecordLastError/LastError stamps the terminal cause on a failed
session (StateFailed-only guard, cleared on Recover/resetToIdle),
normalised to one line + clamped to 400 runes (the session-local mirror
of the event-side subagentCausePayload, pinned byte-for-byte by a
two-sided agreement test). It round-trips on sessnap.Snapshot.LastError
(RestoreState trailing param, additive omitempty) and the eventsource
fold captures EvResult.Error on a StopError terminal. engine/agent
records the cause before persistChild on BOTH the foreground run() and
background driveBackground paths — belt-and-suspenders for the former,
load-bearing for the latter. The drain/seal timing invariants are
unchanged (the fix is durability on the snapshot, not winning the emit
race); the pre-run endOnError site (no child session) rides the event
only — the accepted boundary.

Engine public surface: RecordLastError/LastError are Added (minor) per
COMPATIBILITY.md — api/session.txt regenerated, CHANGELOG entry added.
No proto change (the cause already rides SubagentPayload.Cause /
TeamPayload.Cause on the wire).

Co-Authored-By: mecatl <[email protected]>
@JAORMX
JAORMX merged commit ef8d62c into main Aug 6, 2026
15 checks passed
@JAORMX
JAORMX deleted the issue-332-bg-cause-race branch August 6, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant