Skip to content

feat: UserPromptSubmit hydrates recovery note after compaction (Bet 2 slice 4)#268

Merged
kelsonpw merged 2 commits intomainfrom
kelsonpw/agent-loop-hydrate-recovered
Apr 26, 2026
Merged

feat: UserPromptSubmit hydrates recovery note after compaction (Bet 2 slice 4)#268
kelsonpw merged 2 commits intomainfrom
kelsonpw/agent-loop-hydrate-recovered

Conversation

@kelsonpw
Copy link
Copy Markdown
Collaborator

@kelsonpw kelsonpw commented Apr 26, 2026

Replaces #174 (auto-closed when its base branch was deleted on the merge of #267 / slice 3). Same single commit, rebased onto current main (post-#266, post-#172, post-#267).

Tracker: #143
Bet 2: Agent loop overhaul

What this slice adds

A UserPromptSubmit hook that, when the SDK fires it after a compaction, reads the snapshot persisted by the (forthcoming) PreCompact hook and prepends a recovery note to the user's next prompt. The note lists modified files and the last reported status, so the agent doesn't lose track of what it was doing across a compaction boundary.

The snapshot is consumed (read + deleted) so hydration fires at most once per compaction cycle. When no snapshot exists, the hook is a no-op.

Scope

  • src/lib/agent-state.ts: adds zod schema for SerializedAgentState, loadSnapshot(), consumeSnapshot(), buildRecoveryNote(). persist() now returns the path on success.
  • src/lib/agent-interface.ts: new createUserPromptSubmitHook(state) factory; wired into buildHooksConfig alongside the existing PreCompact and Stop hooks.
  • 11 new unit tests covering schema validation (happy path, malformed JSON, missing fields, wrong schema version), consume-once semantics, and the recovery-note formatter.

Reconciliation notes

The original slice referenced state.serializationPath(). Slice 3 (#267) introduced the canonical name state.snapshotPath(). During rebase, the serializationPath() alias was removed and call sites updated to snapshotPath() to keep one name for one thing.

Test plan

🤖 Generated with Claude Code


Note

Medium Risk
Adds a new UserPromptSubmit hook that mutates the next prompt by injecting persisted recovery context after compaction; incorrect hydration or snapshot parsing could affect agent behavior across turns. Changes are localized but touch the core agent run loop and retry/reset behavior.

Overview
Adds post-compaction prompt hydration by persisting AgentState to disk and, on the next UserPromptSubmit, consuming that snapshot to prepend a generated recovery note (modified files, last status, compaction count) via additionalContext.

agent-state now validates on-disk snapshots with a zod schema and exposes loadSnapshot, consumeSnapshot, and buildRecoveryNote; persist() returns the snapshot path and logs success/failure. The agent retry loop also resets agentState between attempts to avoid leaking recovery data.

Includes new unit tests covering snapshot load/consume failure modes, recovery note formatting, and one-time hydration semantics for the new hook.

Reviewed by Cursor Bugbot for commit b5f3636. Bugbot is set up for automated code reviews on this repo. Configure here.

When Claude compacts its context mid-run, the wizard feeds it back a short
recovery note — what files have been edited, last status, compaction count —
so the agent re-orients and continues the run instead of re-writing files
or skipping steps.

- src/lib/agent-state.ts gains loadSnapshot, consumeSnapshot, buildRecoveryNote
  helpers. loadSnapshot uses zod validation so a malformed file returns null
  cleanly (no uncaught JSON errors). consumeSnapshot deletes the file so
  hydration fires at most once per compaction.
- New createUserPromptSubmitHook(state) factory in agent-interface.ts that
  injects the recovery note via hookSpecificOutput.additionalContext.
- Wired into buildHooksConfig so every UserPromptSubmit gets a chance to
  hydrate.
- Stops resetting AgentState between retries so a mid-retry compaction still
  sees the files modified in the prior attempt.
- +11 hydrate tests covering the full load/consume/build/hook pipeline.

Bet 2 slice 4. Recreated from #127 onto kelsonpw/agent-loop-precompact-recovered
after the 2026-04-20 history reset.
@kelsonpw kelsonpw requested a review from a team April 26, 2026 01:44
@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.

Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Stale agent state carried across retry attempts
    • Restored the agentState.reset() call in the per-attempt cleanup block that was accidentally removed in commit 46bd57c, preventing stale modifiedFiles, lastStatus, and compactionCount from leaking across retry attempts.

Create PR

Or push these changes by commenting:

@cursor push b3c4183894
Preview (b3c4183894)
diff --git a/src/lib/agent-interface.ts b/src/lib/agent-interface.ts
--- a/src/lib/agent-interface.ts
+++ b/src/lib/agent-interface.ts
@@ -1535,6 +1535,7 @@
         recentStatuses.length = 0;
         authErrorDetected = false;
         reportedError = null;
+        agentState.reset();
       }
 
       // Fresh prompt stream per attempt — stdin stays open until result received

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 46bd57c. Configure here.

Comment thread src/lib/agent-interface.ts
Bugbot caught: the per-attempt cleanup block was missing
agentState.reset(), so modifiedFiles / lastStatus / compactionCount
from a stalled attempt would leak into the next attempt's snapshot
and inflate the recovery note. Aligns with the existing cleanup of
collectedText / recentStatuses / authErrorDetected / reportedError.

Co-Authored-By: Cursor Bugbot <bugbot@cursor.com>
@kelsonpw kelsonpw merged commit 2127196 into main Apr 26, 2026
10 checks passed
@kelsonpw kelsonpw deleted the kelsonpw/agent-loop-hydrate-recovered branch April 26, 2026 02:04
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