fix(compression): prevent orphan sessions when state.db write fails (#33906) - #34048
Closed
zccyman wants to merge 1 commit into
Closed
fix(compression): prevent orphan sessions when state.db write fails (#33906)#34048zccyman wants to merge 1 commit into
zccyman wants to merge 1 commit into
Conversation
…ousResearch#33906) When context compression triggers session rotation, the old session is ended in state.db and a new one created. If create_session() fails (e.g. transient SQLite lock contention), the agent previously continued with the new session_id but no state.db row — producing an orphan session invisible in WebUI and session list. Changes: - Reorder: generate new session_id first, but only assign it to agent.session_id AFTER create_session() succeeds - Add 3-attempt retry with backoff for sqlite3.OperationalError - On failure: rollback agent.session_id to old value and reopen the old session in state.db so the agent stays on a registered session - Guard rollback with locals().get() to handle early failures before old_session_id is assigned Tests: 6 new tests covering success path, retry path, rollback on exhaustion, rollback on generic errors, early-failure edge case, and session_id ordering invariant.
Contributor
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
Prevent orphan sessions when context compression's
create_session()call fails (e.g. transient SQLite lock contention). The agent previously continued with a newsession_idbut nostate.dbrow — producing an orphan session invisible in WebUI and/sessions.Closes #33906
Problem
When context compression triggers a session rotation in
conversation_compression.py:end_session())session_idis generated and immediately assigned toagent.session_idcreate_session()writes the new row tostate.dbIf step 3 fails (SQLite lock, disk I/O error), the agent continues with a
session_idthat has nostate.dbentry. The session is invisible in:/api/sessionsreturns only state.db rows)/sessionsCLI command/resumecan't find it)The entire try/except block only logs a warning — no rollback of the mutated
session_id.Solution
Three changes in
agent/conversation_compression.py:new_session_idfirst, but only assign it toagent.session_idaftercreate_session()succeedssqlite3.OperationalError(lock contention) with 0.1s × attempt delayagent.session_idto old value andreopen_session()the old session so the agent stays on a registered sessionEdge case handled: if
end_session()itself fails (beforeold_session_idis assigned), rollback useslocals().get()instead of direct reference to avoidUnboundLocalError.Files Changed
agent/conversation_compression.pytests/agent/test_compression_orphan_session.pyTest Results
Design Decisions
end_session()already marked it as ended, soreopen_session()is requiredlocals().get()guard: avoidsUnboundLocalErrorwhenend_session()fails beforeold_session_id = agent.session_idis reached