fix(core): preserve session state on abort and error / session expiration (replaces #1055) - #1181
Open
Utkarsh-X wants to merge 1 commit into
Open
Conversation
…erruption and compaction
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.
Fixes #1054
Reopens #1055 (rebased onto the new main history following @victorxheng's note).
What this fixes
When a user interrupts a run with Esc or when a session expires, sending a follow-up message immediately was reading a stale/null in-memory
previousRunStateRef. This caused the SDK to construct a fresh empty session state (messageHistory = []), wiping conversation context.What's included (addressing the #1055 review feedback)
send-message.test.ts): Added 3 dedicated tests verifying:createRunConfig.previousRun."continue".runChatIsCurrent === false) never leak state into other chats.use-send-message.ts): Consolidated the assignment into a simple 5-linesyncRunStateclosure used uniformly across abort, completion, and error handlers.state.runState, so updating it alongsidestreamStatus = 'idle'causes no visual layout flash.I intentionally kept this as minimal and safe as possible to solve the demonstrated bug without bloating the change surface or touching unrelated client/storage APIs. That said, if you'd prefer any tweaks or want me to look into anything further here, just let me know and I'm happy to research and adjust!
Verification
bun test cli/src/hooks/helpers/__tests__/send-message.test.ts(49 passed, including the 3 new regression tests)bun test sdk/src/__tests__/run-cancellation.test.ts(16 passed)bun test cli/src/utils/__tests__/run-state-storage.test.ts(37 passed)bun test packages/agent-runtime/src/__tests__/compact-history.test.ts(31 passed)bun test agents/__tests__/context-pruner.test.ts(69 passed)bun run build:sdk(ESM, CJS, and TypeScript declarations bundled with 0 errors)bun run build:freebuff(Native executable built with exit code 0)