Refuse a second session on another session's checkpoint thread - #34
Merged
Conversation
SessionStore.create() guarded id reuse and let thread_id reuse straight through, and sharing a thread was an approval-gate bypass entirely inside the session API: the second session got a fresh record with no holds, resumed the first one's checkpointed boundary, and — because interrupt_before does not re-fire for a boundary it has already stopped at — ran the held gated node with no approval ever given, while the first session's record still said the hold was open. The guard is a SELECT inside the existing BEGIN IMMEDIATE transaction, so the refusal (ThreadInUseError, naming both the thread and the session holding it) is atomic with the insert: no session row and no transition row left behind, including under two processes racing on one store. No schema change — a UNIQUE index taken at open would brick an existing store already carrying duplicates. The default path, thread id equals session id, is unchanged: ids are unique, and a reused id still lands as SessionExistsError. Co-Authored-By: Claude Fable 5 <noreply@anthropic.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.
What
SessionStore.create()guarded session-id reuse and let thread-id reuse straight through, and sharing a checkpoint thread was an approval-gate bypass entirely inside the session API: the second session got a fresh record with no holds, resumed the first one's checkpointed boundary, and — becauseinterrupt_beforedoes not re-fire for a boundary it has already stopped at — ran the held gated node with no approval ever given, while the first session's record still said the hold was open.How
grapharc/session/store.py— inside the existingBEGIN IMMEDIATEtransaction,create()now doesSELECT id FROM sessions WHERE thread_id = ?before the insert and refuses a collision. Check-then-insert happens under one write lock, so the refusal is atomic with the insert: no session row and no transition row left behind, including under two processes racing on one store. Thecreate()docstring and theIntegrityErrorcomment now state the guarantee the comment already believed: a checkpoint thread belongs to exactly one session.grapharc/session/errors.py— a new sibling ofSessionExistsError,ThreadInUseError, carryingthread_idandsession_idand naming both in its message (thread 'shared' already belongs to session 'a'). Exported fromgrapharc.session.SessionExistsErrorexactly as before (the thread check lets a holder with the same id fall through to the insert).UNIQUEindex: an existing store on disk may already carry duplicates, and creating the index at open would brick it. The transactional check needs no migration. The HTTP layer'sCreateSessionRequest.thread_idis untouched (belongs to server: the HTTP API does not use the durable session layer #7).Tests
Two new tests in
tests/test_session.py, both red without the source change:test_reusing_another_sessions_thread_is_refused— the collision raisesThreadInUseErrornaming both sides, and leaves no session row and no transition row behind.test_a_second_session_on_one_thread_cannot_run_a_held_gated_node— the issue's repro as a regression test: session A holdsapply, session B on the same thread is refused atcreate(), the gated node never runs, and A's hold is still the truth.README/cookbook only describe the other bypass (driving
session.graph.invoke(...)directly), not this one, so no doc text needed updating.Verification
pytest -q: 1617 passed, 0 failedruff check .: cleanFixes #18
🤖 Generated with Claude Code