Skip to content

Fix conversation context loss between Telegram messages#6

Open
RichardAtCT wants to merge 1 commit intomainfrom
fix/conversation-context-issue-5
Open

Fix conversation context loss between Telegram messages#6
RichardAtCT wants to merge 1 commit intomainfrom
fix/conversation-context-issue-5

Conversation

@RichardAtCT
Copy link
Owner

Summary

Fixes conversation context not being maintained between messages in the Telegram integration.

Problem

The Telegram bot was losing conversation context between messages, treating each message as a new conversation instead of continuing the existing session. This prevented follow-up questions and iterative development workflows.

Root Cause

The session manager was incorrectly creating new sessions even when valid session IDs were provided. This happened when sessions weren't found in the active cache (e.g., after bot restart or cache eviction), causing the system to treat existing Claude sessions as "new sessions."

Solution

Key Changes

  • Fixed session resumption logic: Properly distinguish between resuming existing Claude sessions vs starting fresh
  • Improved storage handling: Sessions loaded from storage are correctly marked with is_new_session=False
  • Enhanced logging: Added comprehensive session flow debugging throughout the system
  • Better session ID handling: Handle both temporary and Claude-generated session IDs appropriately

Files Modified

  • src/claude/session.py - Fixed get_or_create_session logic
  • src/claude/facade.py - Added session debugging and validation
  • src/claude/integration.py - Added resume command logging
  • src/claude/sdk_integration.py - Enhanced SDK session logging

Testing

Verified with comprehensive test that confirms:

  • ✅ New sessions start correctly without --resume
  • ✅ Follow-up messages use --resume with session ID
  • ✅ Sessions persist to storage properly
  • ✅ Sessions resume correctly after simulated bot restart
  • ✅ All existing tests continue to pass

Test Plan

  • Unit tests pass
  • Conversation context test passes
  • Manual testing with actual Telegram bot
  • Verify sessions persist across bot restarts
  • Test with multiple concurrent users

Closes #5

🤖 Generated with Claude Code

The bot was losing conversation context between messages because the session
manager was incorrectly creating new sessions even when valid session IDs
were provided. This happened when sessions weren't in the active cache
(e.g., after bot restart).

Key fixes:
- Properly distinguish between resuming existing Claude sessions vs starting fresh
- Load existing sessions from storage with is_new_session=False
- Add comprehensive logging for session flow debugging
- Use --resume flag correctly for continuing conversations
- Handle both temporary and Claude-generated session IDs appropriately

Verified with comprehensive test that confirms:
✅ New sessions start correctly without --resume
✅ Follow-up messages use --resume with session ID
✅ Sessions persist to storage properly
✅ Sessions resume correctly after simulated bot restart

Fixes: #5

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

Co-Authored-By: Claude <noreply@anthropic.com>
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.

Conversation Context Not Maintained Between Messages in Telegram Integration

1 participant