Repository navigation
fix[go]: skip LLM call for shared chatbot session handshake - #17095
Conversation
Opening a shared chatbot link sends an empty question without a session_id just to obtain a session (frontend fetchSessionId). The Go ChatbotCompletion ran the LLM with that empty user turn, so the bot replied to a message the user never sent. Mirror python async_iframe_completion: a request without session_id only persists the prologue-seeded session and streams the prologue back; the LLM runs exclusively for follow-up turns that carry a session_id.
📝 WalkthroughWalkthrough
ChangesChatbot completion session lifecycle
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
internal/service/bot_completion.go (1)
349-358: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueWrite to the buffered channel synchronously.
Since the channel is explicitly buffered to hold the exact number of frames being emitted (2), spinning up a goroutine is unnecessary. You can write the frames synchronously and close the channel before returning, which simplifies the flow and avoids a minor allocation.
♻️ Proposed refactor
- out := make(chan ChatbotSSEFrame, 2) - go func() { - defer close(out) - out <- ChatbotSSEFrame{ - Data: prologue, - Reference: map[string]any{}, - SessionID: session.ID, - } - out <- ChatbotSSEFrame{Done: true} - }() + out := make(chan ChatbotSSEFrame, 2) + out <- ChatbotSSEFrame{ + Data: prologue, + Reference: map[string]any{}, + SessionID: session.ID, + } + out <- ChatbotSSEFrame{Done: true} + close(out)🤖 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 `@internal/service/bot_completion.go` around lines 349 - 358, Replace the goroutine in the ChatbotSSEFrame channel setup with synchronous writes of both frames, then close out before returning. Preserve the existing buffer size, frame contents, ordering, and session metadata.
🤖 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.
Inline comments:
In `@internal/service/bot_completion.go`:
- Around line 342-348: Remove migration and compatibility references from
comments at internal/service/bot_completion.go:342-348 and
internal/service/bot_completion_history_test.go:329-334. In both sites, delete
Python-parity references and historical previous-implementation wording while
retaining only the current share-page opening-handshake, session persistence,
and prologue behavior explanation.
---
Nitpick comments:
In `@internal/service/bot_completion.go`:
- Around line 349-358: Replace the goroutine in the ChatbotSSEFrame channel
setup with synchronous writes of both frames, then close out before returning.
Preserve the existing buffer size, frame contents, ordering, and session
metadata.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: af095bd2-e2ff-45bc-9530-d95256a9fe6b
📒 Files selected for processing (2)
internal/service/bot_completion.gointernal/service/bot_completion_history_test.go
| // Mirror python async_iframe_completion | ||
| // (conversation_service.py:324-334): a request without a | ||
| // session_id is the share page's opening handshake — the | ||
| // front-end sends an empty question only to obtain a session. | ||
| // Persist the prologue-seeded session and stream the prologue | ||
| // back WITHOUT invoking the LLM; running the model here would | ||
| // fabricate a reply to a message the user never sent. |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove migration history and compatibility wording from comments.
Both comments include Python-parity cross-references ("Mirror python...") and historical migration notes ("The previous Go implementation..."). As per coding guidelines, keep documentation focused on the current runtime path and do not add compatibility wording or migration history.
internal/service/bot_completion.go#L342-L348: Remove the references toasync_iframe_completionand Python files, retaining only the explanation of the share page's opening handshake and prologue behavior.internal/service/bot_completion_history_test.go#L329-L334: Remove the "Mirrors python..." sentence and the explanation of the previous Go implementation's behavior.
📍 Affects 2 files
internal/service/bot_completion.go#L342-L348(this comment)internal/service/bot_completion_history_test.go#L329-L334
🤖 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 `@internal/service/bot_completion.go` around lines 342 - 348, Remove migration
and compatibility references from comments at
internal/service/bot_completion.go:342-348 and
internal/service/bot_completion_history_test.go:329-334. In both sites, delete
Python-parity references and historical previous-implementation wording while
retaining only the current share-page opening-handshake, session persistence,
and prologue behavior explanation.
Source: Coding guidelines
wangq8
left a comment
There was a problem hiding this comment.
Review: PR #17095 — Skip LLM call for shared chatbot session handshake
This is a well-scoped and clearly documented fix. Here is my analysis:
Root cause & fix
The root cause is correctly identified: the Go ChatbotCompletion port unconditionally ran the LLM on every request, while the Python async_iframe_completion treats a request without session_id as a session-creation-only handshake. The fix mirrors the Python behavior by returning early with the prologue when no session_id is present.
Code quality
- The change is minimal (+98/-2) and tightly scoped to the handshake path.
- The comments are thorough and reference both the Python implementation line numbers and the frontend handshake — excellent for maintainability.
- The early return with a buffered channel (cap 2) is clean and correct.
- Normal follow-up turns (with
session_id) are completely unaffected.
Regression & security risk
Negligible. The guard condition (session_id == "") only triggers on the handshake path. No new code paths are exposed.
Tests
The regression test TestBotService_ChatbotCompletion_NewSessionSkipsLLM is excellent:
- Passing
nilforllmServiceis a clever way to prove the LLM is not invoked — any LLM call would panic, making the test a clear pass/fail signal. - It validates both the frame sequence (prologue + done) and the persisted session state (exactly one assistant prologue turn, no empty user message).
- It exercises the exact scenario described in the PR: opening a share link with no
session_id.
Minor observation
The fix treats all requests without session_id as handshakes, even if they carry a non-empty question. This matches the Python behavior (conversation_service.py line 324), so parity is correct — but it is worth noting that the question field is silently dropped in this path. This is fine given the architecture (handshake-by-absence-of-session_id is the contract between frontend and backend).
Verdict
LGTM. The fix is correct, well-documented, and properly tested. No blocking issues.
Summary
Opening a shared chatbot link immediately shows a fabricated assistant reply such as "it looks like you sent a blank message", even though the user never sent anything.
Root cause: the shared chat page sends an empty
questionwithout asession_idon load purely as a handshake to obtain a session (frontendfetchSessionIdinweb/src/pages/next-chats/hooks/use-send-shared-message.ts). The Pythonasync_iframe_completion(api/db/services/conversation_service.py) treats a request withoutsession_idas session creation only — it persists the prologue-seeded session and streams the prologue back without invoking the LLM. The Go port ofChatbotCompletion(internal/service/bot_completion.go) instead always ran the LLM, feeding the empty string as a user turn, so the model answered a message that was never sent.Fix: mirror the Python behavior in
ChatbotCompletion. A request withoutsession_idnow only creates the prologue-seeded session and streams the prologue frame plus the done marker; the LLM runs exclusively for follow-up turns that carry asession_id.Adds a regression test (
TestBotService_ChatbotCompletion_NewSessionSkipsLLM) asserting that the handshake returns only the prologue frame, persists exactly one assistant (prologue) turn, and never reaches the LLM path.