Skip to content

fix(core): preserve session state on abort and error / session expiration (replaces #1055) - #1181

Open
Utkarsh-X wants to merge 1 commit into
CodebuffAI:mainfrom
Utkarsh-X:fix/preserve-session-state-on-abort
Open

fix(core): preserve session state on abort and error / session expiration (replaces #1055)#1181
Utkarsh-X wants to merge 1 commit into
CodebuffAI:mainfrom
Utkarsh-X:fix/preserve-session-state-on-abort

Conversation

@Utkarsh-X

Copy link
Copy Markdown

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)

  1. Regression tests (send-message.test.ts): Added 3 dedicated tests verifying:
    • User abort (Esc) preserves turn history + tool calls in createRunConfig.previousRun.
    • Session expiration / gate errors preserve history on "continue".
    • Mid-run chat switches (runChatIsCurrent === false) never leak state into other chats.
  2. DRY state sync (use-send-message.ts): Consolidated the assignment into a simple 5-line syncRunState closure used uniformly across abort, completion, and error handlers.
  3. Clean scope: Reverted the previous prompt text tweaks so this PR is strictly isolated to the session state bug (3 files total).
  4. Re-render safety: Checked store subscriptions — zero React components subscribe to state.runState, so updating it alongside streamStatus = '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

  • Test Suites (202 / 202 passed across touched areas):
    • 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)
  • Builds:
    • bun run build:sdk (ESM, CJS, and TypeScript declarations bundled with 0 errors)
    • bun run build:freebuff (Native executable built with exit code 0)

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.

Session state is lost after free session expires or run is interrupted

1 participant