feat: AgentState persistence for PreCompact recovery (Bet 2 slice 3)#267
Merged
feat: AgentState persistence for PreCompact recovery (Bet 2 slice 3)#267
Conversation
Adds per-attempt AgentState bag that tracks modified files, last structured status, and compaction count. Serializes to a deterministic tmpdir path so a future post-compaction UserPromptSubmit hook can hydrate the agent back with the context that compaction dropped. - New src/lib/agent-state.ts — AgentState class with setAttemptId, recordModifiedFile, recordStatus, recordCompaction, snapshot, persist, snapshotPath, reset. Schema versioned as amplitude-wizard-agent-state/1. - Instantiated per runAgent call; reset between retry attempts. - Wired into StatusReporter.onStatus so the persisted snapshot always carries the most recent status message. - +11 tests (agent-state.test.ts) covering dedup/sort, status tracking, compaction count, JSON schema, tmpdir path, reset semantics. Bet 2 slice 3. Recreated from #126 onto kelsonpw/agent-loop-status-recovered after the 2026-04-20 history reset. Hook-factory wiring for PreCompact and PostToolUse depends on Bet 1 ToolCallCounters landing first (PR #149); this slice ships the persistence primitive so later slices can wire it up with zero merge friction.
Contributor
🧙 Wizard CIRun the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands: Test all apps:
Test all apps in a directory:
Test an individual app:
Show more apps
Results will be posted here when complete. |
This was referenced Apr 26, 2026
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.
Replaces #173 (auto-closed when its base branch was deleted on the merge of #172). Same single commit, rebased onto current
main(post-#266, post-#172).What this slice adds
A new
AgentStaterecovery bag (src/lib/agent-state.ts) that holds per-attempt context — modified files, last reported status, attempt id — so a future PreCompact hook can persist a snapshot before the SDK compacts the conversation. Slice 4 (#174 follow-up) hydrates from this snapshot viaUserPromptSubmit.Scope
src/lib/agent-state.ts— pure data class, 10 unit testssrc/lib/__tests__/agent-state.test.tsagent-interface.ts: instantiateagentStateper run, set attempt id at the top of each retry, record status reports into it. No behavior change yet — the snapshot is built but not yet persisted (that's the PreCompact hook in a follow-up slice).Test plan
pnpm tsc --noEmit✅pnpm test— 1295 passed (was 1285 after feat: structured status via report_status MCP tool (Bet 2 slice 2) #172) — +10 fromagent-state.test.tspnpm lint✅ (1 pre-existing unrelated warning)mainafter fix(agent): ride out gateway 400-terminated cascades #266 (gateway resilience) and feat: structured status via report_status MCP tool (Bet 2 slice 2) #172 (report_status)🤖 Generated with Claude Code
Note
Low Risk
Low risk: adds a new state snapshot utility and wires it to status reporting/retry resets without changing core agent decision logic; primary impact is small tmpdir file I/O when
persist()is invoked.Overview
Adds a new
AgentStatemodule that tracks per-attempt recovery context (modified files, last structured status, compaction count, run/attempt IDs) and can persist a schema-versioned JSON snapshot to a deterministic tmpdir path.Wires
AgentStateintorunAgentso each retry attempt sets anattemptId, recordsreport_statusupdates, and resets state between retries, and includes new Vitest coverage validating dedupe/sort behavior and snapshot persistence semantics.Reviewed by Cursor Bugbot for commit eb8889a. Bugbot is set up for automated code reviews on this repo. Configure here.