fix(core): preserve session state on abort and error / session expiration (replaces #1055) - #1181
Conversation
|
The core insight is reasonable: The problem is the tests in Worth also double-checking that Right direction, but please add a test that actually calls into the hook/module under test rather than reimplementing its logic in the spec file. |
66f89ba to
469883b
Compare
…erruption and compaction
469883b to
10fb2a1
Compare
|
Addressed the feedback. Replaced the 3 manual tests with 3 hook-level integration tests in The 3 tests cover:
All 4 tests in the file pass (3 new + 1 existing upstream test). |
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)