fix: restore immediate bot activity indicators#1956
Conversation
📝 WalkthroughWalkthroughEnhanced Slack and Telegram bot implementations to manage "thinking" and "typing" indicators throughout the message lifecycle, including ensuring indicators appear on message arrival, clearing them on errors, and clearing them during session cancellation. Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Slack/Telegram Client
participant Bot as Bot Service
participant Agent as Agent Service
participant Chat as Chat Platform
rect rgba(100, 200, 100, 0.5)
Note over Client,Chat: New Message Flow (with thinking indicator)
Client->>Bot: Incoming message
Bot->>Chat: Ensure/start thinking indicator
Bot->>Agent: Enqueue chat message
Agent-->>Bot: Enqueue result
end
rect rgba(200, 100, 100, 0.5)
Note over Client,Chat: Error Recovery Flow
Bot->>Agent: Create session / Enqueue message (fails)
Agent-->>Bot: Error response
Bot->>Chat: Clear pending indicator
Bot-->>Client: Send error reply
end
rect rgba(100, 100, 200, 0.5)
Note over Client,Chat: Cancel Flow (pre-session)
Client->>Bot: Cancel command
Bot->>Chat: Clear pending indicator
Bot->>Agent: Cancel session (if exists)
Agent-->>Bot: Cancel result
Bot-->>Client: Acknowledgement
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
internal/service/telegram/bot.go (1)
191-206: DuplicatestopTypingLoopcall afterCancelSession.Line 193 stops the typing loop immediately after clearing pending messages. Line 205 calls
stopTypingLoopagain afterCancelSession. SincestopTypingLoopis idempotent (setstypingCancel/typingDonetonilunder lock), the second call is a harmless no-op but redundant—nothing between lines 193 and 205 restarts the loop.🔧 Optional: Remove the redundant call
case "cancel": b.clearPendingMessages(cs) b.stopTypingLoop(cs) sid, ownerUID := cs.ActiveSession() if sid == "" { b.sendText(chatID, "No active session.") return } if err := b.agentAPI.CancelSession(ctx, sid, ownerUID); err != nil { b.sendText(chatID, "Failed to cancel session: "+err.Error()) return } - b.stopTypingLoop(cs) b.sendText(chatID, "Session cancelled.")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/service/telegram/bot.go` around lines 191 - 206, The duplicate stopTypingLoop call is redundant: remove the second stopTypingLoop invocation after b.agentAPI.CancelSession to avoid unnecessary no-op; keep the initial b.stopTypingLoop(cs) right after b.clearPendingMessages(cs) and leave the CancelSession call and subsequent sendText/ActiveSession logic unchanged (look for stopTypingLoop, clearPendingMessages, ActiveSession, CancelSession, and sendText in this case "cancel" branch to locate the code).internal/service/telegram/bot_test.go (1)
356-385: Test is valid but consider addingagentAPIfor robustness.The test omits
agentAPIfrom theBotstruct. This works because the test relies on the early return at line 196-198 when no active session exists, never reachingb.agentAPI.CancelSession(...). However, if the/cancellogic is later modified to accessagentAPIbefore checkingActiveSession(), this test would panic.Consider adding the fake agent service for defensive test design:
🔧 Optional: Add agentAPI to test setup
func TestBot_HandleCommand_CancelStopsPreFlushTypingWithoutSession(t *testing.T) { t.Parallel() api := &fakeTelegramAPI{} logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + service := newFakeTelegramAgentService("ignored") bot := &Bot{ + agentAPI: service, botAPI: api, logger: logger, allowedChats: map[int64]struct{}{123: {}}, }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/service/telegram/bot_test.go` around lines 356 - 385, The test TestBot_HandleCommand_CancelStopsPreFlushTypingWithoutSession constructs a Bot without populating bot.agentAPI which could cause a panic if /cancel logic changes; update the test setup to assign a fake agent service to the Bot (e.g., create and set a fakeAgentAPI instance on bot.agentAPI) so bot.handleCommand has a non-nil agentAPI during execution and the existing assertions around getOrCreateChat, startTypingLoop, and assertTypingStops remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@internal/service/telegram/bot_test.go`:
- Around line 356-385: The test
TestBot_HandleCommand_CancelStopsPreFlushTypingWithoutSession constructs a Bot
without populating bot.agentAPI which could cause a panic if /cancel logic
changes; update the test setup to assign a fake agent service to the Bot (e.g.,
create and set a fakeAgentAPI instance on bot.agentAPI) so bot.handleCommand has
a non-nil agentAPI during execution and the existing assertions around
getOrCreateChat, startTypingLoop, and assertTypingStops remain unchanged.
In `@internal/service/telegram/bot.go`:
- Around line 191-206: The duplicate stopTypingLoop call is redundant: remove
the second stopTypingLoop invocation after b.agentAPI.CancelSession to avoid
unnecessary no-op; keep the initial b.stopTypingLoop(cs) right after
b.clearPendingMessages(cs) and leave the CancelSession call and subsequent
sendText/ActiveSession logic unchanged (look for stopTypingLoop,
clearPendingMessages, ActiveSession, CancelSession, and sendText in this case
"cancel" branch to locate the code).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 33f0ef6e-00c8-4b67-a0fc-027ec2d9b8eb
📒 Files selected for processing (4)
internal/service/slack/bot.gointernal/service/slack/bot_test.gointernal/service/telegram/bot.gointernal/service/telegram/bot_test.go
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1956 +/- ##
==========================================
+ Coverage 67.96% 68.06% +0.09%
==========================================
Files 475 475
Lines 61304 61308 +4
==========================================
+ Hits 41668 41729 +61
+ Misses 15660 15610 -50
+ Partials 3976 3969 -7
... and 13 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Summary
Root Cause
The inbound batching refactor delayed the first visible activity indicator until after the debounce flush. That created dead air on new messages and could make the indicator effectively invisible for fast responses.
Testing
Summary by CodeRabbit
Release Notes
New Features
Bug Fixes