fix: prevent silent session fallback in interactive PTY mode#21
Merged
Conversation
Claude Code's --resume flag behaves differently in interactive mode vs -p mode:
- In -p mode: properly errors when session doesn't exist
- In interactive mode: silently falls back to most recent session
This caused Codez sessions to sometimes switch to a different Claude session
when the stored session was no longer on disk (cleaned up, path changed, etc).
Add claudeSessionExistsOnDisk() utility to check if ~/.claude/projects/{path-hash}/{id}.jsonl
exists before spawning the PTY with --resume. If the session doesn't exist,
fall back to --session-id (create fresh) instead, avoiding silent fallback.
Includes comprehensive tests for path hashing and session file detection.
Before spawning an interactive PTY with --resume, verify that the Claude session still exists on disk. If not found, use --session-id to create a fresh session instead of letting Claude silently fall back to the most recent session. Logs a warning when a stale session is detected and recovered.
Expand test-clear-session-id.mjs with three new tests: - Test 5: Proves --resume with non-existent UUID silently falls back in interactive mode (the root cause of the session-switching bug) - Test 6: Validates that --session-id with existing UUID errors - Test 7: Confirms --session-id behavior in interactive mode These tests demonstrate the distinction between -p and interactive modes and document the expected behavior when session validation is in place.
Owner
Author
|
@greptile |
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
dakl
added a commit
that referenced
this pull request
Mar 27, 2026
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
Fixed a bug where Codez sessions would silently switch to a different Claude Code session when resuming.
Root cause: Claude Code's
--resumeflag behaves differently in interactive mode vs-pmode:When a stored Claude session was no longer on disk (cleaned up, path changed, etc), interactive
--resume <missing-id>would fall back instead of error, causing users to see a completely different conversation.Solution
Added
claudeSessionExistsOnDisk()utility to validate that~/.claude/projects/{path-hash}/{session-id}.jsonlexists before spawning an interactive PTY with--resume. If the session file is missing, fall back to--session-id(create fresh) instead, preventing the silent fallback.The fix also logs a warning when a stale session is detected and recovered.
Changes
Testing
All 223 unit tests pass. New tests validate:
--resume <bad-uuid>silently falls back in interactive mode (the bug)Related earlier work:
🤖 Generated with Claude Code