Repository navigation
feat(session): persist the terminal failure cause on the session snapshot (#332) - #405
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #332. A background subagent whose end-emit loses the race with the run-end seal (
drainChildren'sabortEmits) had itsStopErrorcause dropped from every durable surface: the parent'ssubagent.endevent never lands, child runs are never relayed to theEventLog, and the persisted child snapshot carried onlystate: 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
persistChildcaptures 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 pinnedTestSealUnblocksEmitParkedSend/TestCancelUnwedgesStalledTeamRuninvariants.The change (the
Permanent-analog, end to end)engine/session/session.go—Session.RecordLastError/Session.LastError: stamps the terminal cause on afailedsession (StateFailed-only guard mirroringRecordFailurePermanence; cleared onRecover/resetToIdle). Normalised to one line + clamped to 400 runes by the session-localnormaliseSnapshotError/maxSnapshotErrorRunes— the deliberate mirror of the event-sidesubagentCausePayload/maxSubagentCausePreview(session can't importengine/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 asPermanent), threaded throughRestoreState's new trailing param via arecordFailedStateFlagshelper.engine/adapter/eventsource/eventsource.go— the fold capturesEvResult.Erroron aStopErrorterminal into the restored session.engine/agent/subagent.go—recordChildCauseOnSnapshotcalled beforepersistChildon 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
endOnErrorsite (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 ridesSubagentPayload.Cause/TeamPayload.Causeon the wire; the snapshot field is engine-domain only.Gates
task api:update(engine/api/session.txt) —RecordLastError/LastErrorclassified Added (minor) perengine/COMPATIBILITY.md, with theengine/CHANGELOG.mdentry.task lint && task test— green (root + engine + provider modules + standalone proofs; api-compat gate passes).task docs—llms.txtregenerated (unchanged), strict link gate clean.go run ./cmd/mecademo— still prints a full offline session.Permanent, not a timing-guarantee change.Tests
engine/agent/background_cause_race_test.go(new): the mutation-kill for the background path (snapshotLastErrorcarries the cause; removing theRecordLastErrorcall fails it), the foreground mirror, and the agreement test.engine/session/session_test.go:RecordLastErrorstate guard (rejects non-failed), clear-on-Recover/Reopen, normalise+clamp, and the session-side agreement pin.engine/adapter/sessnap/sessnap_test.go: theLastErrorround-trip trio mirroringPermanent(round-trip / omitempty-empty / pre-field backward-compat).engine/adapter/eventsource/eventsource_test.go:TestFoldRoundTripsFailurePermanencenow asserts the foldedLastErrorper 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
LastErrorround-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 — surfacingLastErrorinInspectSubagent's rendered output — is the architect-documented follow-up (a model-facing UX change), deliberately out of scope for this durability fix.