Skip to content

Fix: session context persistence#11

Open
michaelansel wants to merge 1 commit intoRichardAtCT:mainfrom
michaelansel:fix/session-context-persistence
Open

Fix: session context persistence#11
michaelansel wants to merge 1 commit intoRichardAtCT:mainfrom
michaelansel:fix/session-context-persistence

Conversation

@michaelansel
Copy link

Fixed two critical issues causing conversation context to reset on every message:

  1. Session continuation detection: Replaced unreliable is_new_session flag with robust temp_ prefix check

    • The is_new_session flag persisted incorrectly in cached session objects
    • Now checks if session_id starts with "temp_" to determine if it's a new session
    • Ensures --resume flag is properly used for continuing conversations
  2. Backend consistency: Added backend tracking to prevent SDK/subprocess session conflicts

    • SDK and subprocess have separate session storage and cannot resume each other's sessions
    • Added backend field to ClaudeSession to track which backend created each session
    • Sessions now stick with their original backend (SDK or subprocess) for their lifetime
    • Prevents context loss when SDK fails and falls back to subprocess

Before this fix:

  • First message: Creates session (subprocess fallback due to SDK error)
  • Second message: SDK tries to resume subprocess session → fails → context lost

After this fix:

  • First message: Creates session with backend='subprocess'
  • Second message: Uses subprocess directly → context maintained ✓

🤖 Generated with Claude Code

Fixed two critical issues causing conversation context to reset on every message:

1. Session continuation detection: Replaced unreliable `is_new_session` flag with robust `temp_` prefix check
   - The `is_new_session` flag persisted incorrectly in cached session objects
   - Now checks if session_id starts with "temp_" to determine if it's a new session
   - Ensures `--resume` flag is properly used for continuing conversations

2. Backend consistency: Added backend tracking to prevent SDK/subprocess session conflicts
   - SDK and subprocess have separate session storage and cannot resume each other's sessions
   - Added `backend` field to ClaudeSession to track which backend created each session
   - Sessions now stick with their original backend (SDK or subprocess) for their lifetime
   - Prevents context loss when SDK fails and falls back to subprocess

Before this fix:
- First message: Creates session (subprocess fallback due to SDK error)
- Second message: SDK tries to resume subprocess session → fails → context lost

After this fix:
- First message: Creates session with backend='subprocess'
- Second message: Uses subprocess directly → context maintained ✓

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@michaelansel
Copy link
Author

I ran into what seems like the same problems as #6 , but Claude came up with a different solution. I think it might be worth merging the two, so I'm submitting this now as an interim hold until I have time to do the integration/comparison.

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.

1 participant