Conversation
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.
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. |
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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.
- Restored the
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 receivedYou can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit 46bd57c. Configure here.
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>
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 #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).What this slice adds
A
UserPromptSubmithook 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 forSerializedAgentState,loadSnapshot(),consumeSnapshot(),buildRecoveryNote().persist()now returns the path on success.src/lib/agent-interface.ts: newcreateUserPromptSubmitHook(state)factory; wired intobuildHooksConfigalongside the existing PreCompact and Stop hooks.Reconciliation notes
The original slice referenced
state.serializationPath(). Slice 3 (#267) introduced the canonical namestate.snapshotPath(). During rebase, theserializationPath()alias was removed and call sites updated tosnapshotPath()to keep one name for one thing.Test plan
pnpm tsc --noEmit✅pnpm test— 1306 passed (was 1295 after slice 3) — +11 fromagent-state-hydrate.test.tspnpm lint— 0 errors (1 pre-existing warning unrelated to this PR)mainpost-fix(agent): ride out gateway 400-terminated cascades #266, post-feat: structured status via report_status MCP tool (Bet 2 slice 2) #172, post-feat: AgentState persistence for PreCompact recovery (Bet 2 slice 3) #267🤖 Generated with Claude Code
Note
Medium Risk
Adds a new
UserPromptSubmithook 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
AgentStateto disk and, on the nextUserPromptSubmit, consuming that snapshot to prepend a generated recovery note (modified files, last status, compaction count) viaadditionalContext.agent-statenow validates on-disk snapshots with a zod schema and exposesloadSnapshot,consumeSnapshot, andbuildRecoveryNote;persist()returns the snapshot path and logs success/failure. The agent retry loop also resetsagentStatebetween 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.