fix: disconnect old session on Telegram /attach switch - #29
Conversation
When a user ran /attach <new-session> without first /detaching, the old session kept its client registration in the daemon and continued streaming into the same Telegram chat. With 7 sessions and cycling between them, multiple old sessions accumulated as registered clients simultaneously — causing interleaved output and stale streaming/stop-button state. Fix: #handleAttach now calls disconnectClient + clears streaming state before attaching to a different session. Also deletes the ⏹ Stop button for the old session (its idle status_change won't arrive after disconnect). #handleDetach gets the same streaming + stop-button cleanup for consistency. Tests: - session-client-tracking.test.ts — real Session objects; covers the regression shape (both sessions hold the client without detach), the fix (detach-then-attach routes only to the new session), and the disconnectClient sweep pattern across N sessions. - telegram-session-switch.test.ts — mirrors UserState state machine; covers all switch/detach state transitions offline (no Grammy dep). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughExtends Telegram attach and detach handlers to clean up streaming buffer content and delete any active stop-control message when switching or leaving sessions. Two new test files are added: one for ChangesTelegram session attach/detach cleanup
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #29 +/- ##
===========================================
- Coverage 80.61% 58.34% -22.27%
===========================================
Files 45 46 +1
Lines 4782 6943 +2161
===========================================
+ Hits 3855 4051 +196
- Misses 927 2892 +1965
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/tests/telegram-session-switch.test.ts (1)
24-112: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy liftAvoid testing a duplicated state machine copy.
performSwitch/performDetachmirror production behavior, so tests can still pass when real handler logic drifts. Extract the cleanup/switch logic into shared pure helpers and import them here to test the real contract.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/tests/telegram-session-switch.test.ts` around lines 24 - 112, The tests are exercising a duplicated copy of the session switch/detach state machine instead of the real contract, so drift in `#handleAttach` and `#handleDetach` can go unnoticed. Extract the cleanup/switch behavior into shared pure helpers used by TelegramFrontend, then update the test to import and verify those helpers directly rather than maintaining local mirrors like performSwitch and performDetach.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/tests/telegram-session-switch.test.ts`:
- Around line 24-112: The tests are exercising a duplicated copy of the session
switch/detach state machine instead of the real contract, so drift in
`#handleAttach` and `#handleDetach` can go unnoticed. Extract the cleanup/switch
behavior into shared pure helpers used by TelegramFrontend, then update the test
to import and verify those helpers directly rather than maintaining local
mirrors like performSwitch and performDetach.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: a5a8c6da-0788-4ef8-9344-a703e66bf236
📒 Files selected for processing (3)
src/frontends/telegram/index.tssrc/tests/session-client-tracking.test.tssrc/tests/telegram-session-switch.test.ts
Patch release rolling up six fixes/doc changes since v0.1.1: - file explorer switches workdir on session change (#37) - interrupted tool calls marked cancelled, not failed (#35) - writeBatch guarded against in-flight streams; no TUI double-print (#33) - TUI reconnects + re-mints token on JWT expiry (#34) - Telegram /attach disconnects old session before switching (#29) - richer README badges (#27) Bumps package.json to 0.1.2 so the release.yml tag check passes, and backfills the previously-undocumented 0.1.1 entry in the CHANGELOG. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
#handleAttachnever calleddisconnectClientbefore attaching to a new session. Since every Telegram user has a fixedclientId(telegram:<userId>), switching sessions left the old session's client registration intact — causing it to keep streaming into the same chat. With 7 sessions cycled through, multiple old sessions accumulated as registered clients simultaneously.#handleAttach: CallsdisconnectClient, clears the streaming buffer, and deletes the dangling ⏹ Stop button before attaching to a different session (the Stop button normally gets deleted by the old session'sidlestatus_change, which won't arrive after we disconnect).#handleDetach: Consistently clears streaming buffer and stop-button state (previously only nulledattachedSessionId/attachedSessionName).Test plan
src/tests/session-client-tracking.test.ts— 14 tests using realSessionobjects (no SDK); covers the regression shape (both sessions hold the client without detach), the fix (detach-then-attach routes only to new session), scrollback replay going to the correct send function, and thedisconnectClientsweep across N sessionssrc/tests/telegram-session-switch.test.ts— 17 tests mirroring theUserStatestate machine offline (no Grammy); covers first-attach, same-session re-attach, cross-session switch, streaming buffer isolation, stop-button deletion, and cycling through 7 sessionsbun test src/tests src/daemon— 494 pass, 0 fail🤖 Generated with Claude Code
Summary by CodeRabbit