fix: session lifecycle and chat history reliability#49
Draft
cursor[bot] wants to merge 2 commits into
Draft
Conversation
- Merge DB history with live journal by seq when switching sessions - Reject send_message while spawning_sessions is active - Skip Completed status in reader_loop when user stopped the session - Retry flush_batch buffer on BEGIN failure instead of discarding rows Co-authored-by: José Fernando <xinnaider@users.noreply.github.com>
Co-authored-by: José Fernando <xinnaider@users.noreply.github.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.
Summary
Restores four critical correctness fixes that were identified on a prior automation branch but never merged to main.
Bugs fixed
1. Chat history clobber (data loss)
Impact: Live messages disappear when switching sessions or when
loadHistoryreturns after newersession:outputevents.Root cause:
CentralPanel.loadHistoryreplaced the entire journal store with the DB snapshot instead of merging byseq.Fix: Reintroduce
mergeJournalBySeq— live entries win on seq conflicts.2. Silent follow-up message loss
Impact: User sends a follow-up while the agent is still spawning; message appears in chat but is never delivered.
Root cause:
send_messageadded the message to journal/DB then spawned, butdo_spawnsilently skipped whenspawning_sessionsalready contained the session id.Fix: Return an error when
spawning_sessionsis active.3. Stop race overwrites Stopped → Completed
Impact: User stops a session; status briefly shows Stopped then flips to Completed when the CLI process exits.
Root cause:
reader_loopunconditionally wroteCompletedto DB on process exit.Fix: Check
session_status_allows_completionbefore finalizing.4. Session output loss on DB lock
Impact: JSONL output lines silently dropped when SQLite BEGIN fails under contention.
Root cause:
flush_batchcleared the buffer on BEGIN failure.Fix: Keep buffer intact for retry on next flush.
Validation
npx vitest run ui/lib/journal-merge.test.ts— 4 tests passcargo test --lib should_not_finalize/should_finalize/should_reject_send_message— 3 tests pass