Skip to content

fix[go]: skip LLM call for shared chatbot session handshake - #17095

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix-chatbot-share-blank-message
Jul 20, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix-chatbot-share-blank-message

Conversation

@euvre

@euvre euvre commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

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 question without a session_id on load purely as a handshake to obtain a session (frontend fetchSessionId in web/src/pages/next-chats/hooks/use-send-shared-message.ts). The Python async_iframe_completion (api/db/services/conversation_service.py) treats a request without session_id as session creation only — it persists the prologue-seeded session and streams the prologue back without invoking the LLM. The Go port of ChatbotCompletion (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 without session_id now 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 a session_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.

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.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 20, 2026
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

ChatbotCompletion now treats requests without a session_id as a session-initialization handshake: it persists the session, streams the dialog prologue and a terminal frame, and skips LLM execution. A regression test verifies the SSE output and stored history.

Changes

Chatbot completion session lifecycle

Layer / File(s) Summary
Prologue-only session initialization
internal/service/bot_completion.go, internal/service/bot_completion_history_test.go
Session-less requests now return the prologue and Done SSE frames without calling the LLM; tests verify the new session ID and persisted assistant-only history.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: haruko386

Poem

I’m a rabbit with a prologue to send,
No LLM call begins at the end.
A fresh session hops into view,
One assistant turn, then Done comes through.
Follow-up questions can make the next bend.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: skipping the LLM call for shared chatbot session handshakes.
Description check ✅ Passed The description provides the required Summary with context, root cause, fix, and regression test details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@dosubot dosubot Bot added the 🐞 bug Something isn't working, pull request that fix bug. label Jul 20, 2026
@euvre euvre added the ci Continue Integration label Jul 20, 2026
@euvre
euvre requested a review from wangq8 July 20, 2026 06:48

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
internal/service/bot_completion.go (1)

349-358: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Write 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

📥 Commits

Reviewing files that changed from the base of the PR and between 6b8a76c and 6bfa7cd.

📒 Files selected for processing (2)
  • internal/service/bot_completion.go
  • internal/service/bot_completion_history_test.go

Comment on lines +342 to +348
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 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 to async_iframe_completion and 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 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 nil for llmService is 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.

@wangq8
wangq8 merged commit 624b4b0 into infiniflow:main Jul 20, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants