Skip to content

feat: AgentState persistence for PreCompact recovery (Bet 2 slice 3)#267

Merged
kelsonpw merged 1 commit intomainfrom
kelsonpw/agent-loop-precompact-recovered
Apr 26, 2026
Merged

feat: AgentState persistence for PreCompact recovery (Bet 2 slice 3)#267
kelsonpw merged 1 commit intomainfrom
kelsonpw/agent-loop-precompact-recovered

Conversation

@kelsonpw
Copy link
Copy Markdown
Collaborator

@kelsonpw kelsonpw commented Apr 26, 2026

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).

Tracker: #143
Bet 2: Agent loop overhaul

What this slice adds

A new AgentState recovery 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 via UserPromptSubmit.

Scope

  • New file: src/lib/agent-state.ts — pure data class, 10 unit tests
  • New file: src/lib/__tests__/agent-state.test.ts
  • agent-interface.ts: instantiate agentState per 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

🤖 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 AgentState module 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 AgentState into runAgent so each retry attempt sets an attemptId, records report_status updates, 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.

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.
@kelsonpw kelsonpw requested a review from a team April 26, 2026 01:28
@github-actions
Copy link
Copy Markdown
Contributor

🧙 Wizard CI

Run 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:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci django
  • /wizard-ci fastapi
  • /wizard-ci flask
  • /wizard-ci javascript-node
  • /wizard-ci javascript-web
  • /wizard-ci next-js
  • /wizard-ci python
  • /wizard-ci react-router
  • /wizard-ci vue

Test an individual app:

  • /wizard-ci django/django3-saas
  • /wizard-ci fastapi/fastapi3-ai-saas
  • /wizard-ci flask/flask3-social-media
Show more apps
  • /wizard-ci javascript-node/express-todo
  • /wizard-ci javascript-node/fastify-blog
  • /wizard-ci javascript-node/hono-links
  • /wizard-ci javascript-node/koa-notes
  • /wizard-ci javascript-node/native-http-contacts
  • /wizard-ci javascript-web/saas-dashboard
  • /wizard-ci next-js/15-app-router-saas
  • /wizard-ci next-js/15-app-router-todo
  • /wizard-ci next-js/15-pages-router-saas
  • /wizard-ci next-js/15-pages-router-todo
  • /wizard-ci python/meeting-summarizer
  • /wizard-ci react-router/react-router-v7-project
  • /wizard-ci react-router/rrv7-starter
  • /wizard-ci react-router/saas-template
  • /wizard-ci react-router/shopper
  • /wizard-ci vue/movies

Results will be posted here when complete.

@kelsonpw kelsonpw merged commit e638af4 into main Apr 26, 2026
13 checks passed
@kelsonpw kelsonpw deleted the kelsonpw/agent-loop-precompact-recovered branch April 26, 2026 01:36
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.

2 participants