Repository navigation
fix[go]: stop leaking [DONE] sentinel into streamed chat answers - #17084
Conversation
Model drivers emit the OpenAI-style [DONE] marker as their final sender call. The channel adapter chatStreamWithContext forwarded it verbatim, so AskService (search AI summary) and the mindmap handler accumulated it as answer text, leaving a trailing [DONE] in the rendered summary. Treat the sentinel as end-of-stream in the adapter: abort the driver loop, never forward it into the channel.
📝 WalkthroughWalkthroughThe chat streaming service now treats ChangesChat stream completion
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
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.
🧹 Nitpick comments (1)
internal/service/model_chat_test.go (1)
35-43: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueSimulate driver trailing logic to catch early exits.
If you adjust the main implementation to return
nilinstead oferrStreamDone, consider enhancing this mock driver to verify that trailing logic runs successfully. You can add a boolean flag (e.g.,UsagePopulated bool) that is set totrueafter the loop finishes, and assert it in the test to guarantee that the driver wasn't prematurely aborted.♻️ Proposed refactor for the mock driver
type doneEmittingDriver struct { *modelModule.DummyModel deltas []string + usagePopulated bool } func (d *doneEmittingDriver) ChatStreamlyWithSender(modelName string, messages []modelModule.Message, apiConfig *modelModule.APIConfig, modelConfig *modelModule.ChatConfig, modelUsage *common.ModelUsage, sender func(*string, *string) error) error { for _, delta := range d.deltas { if err := sender(&delta, nil); err != nil { return err } } done := streamDoneSentinel - return sender(&done, nil) + if err := sender(&done, nil); err != nil { + return err + } + d.usagePopulated = true + return nil }🤖 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/model_chat_test.go` around lines 35 - 43, Enhance the mock driver method doneEmittingDriver.ChatStreamlyWithSender by adding a usage-population flag and setting it only after all deltas have been sent successfully, before emitting the completion sentinel. Update the related test to assert this flag, ensuring trailing driver logic executes and is not prematurely terminated.
🤖 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 `@internal/service/model_chat_test.go`:
- Around line 35-43: Enhance the mock driver method
doneEmittingDriver.ChatStreamlyWithSender by adding a usage-population flag and
setting it only after all deltas have been sent successfully, before emitting
the completion sentinel. Update the related test to assert this flag, ensuring
trailing driver logic executes and is not prematurely terminated.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d5f000d6-f50d-4a39-80ff-da51d1c409e5
📒 Files selected for processing (2)
internal/service/model_chat.gointernal/service/model_chat_test.go
wangq8
left a comment
There was a problem hiding this comment.
Review: PR #17084
Change: Introduces an errStreamDone sentinel error in chatStreamWithContext to abort the driver loop when the [DONE] marker is encountered, preventing it from leaking into channel consumers (AskService, mindmap handler). Adds model_chat_test.go.
Assessment:
-
Correctness: The approach is sound —
[DONE]is a transport-level signal, not answer text. Using a sentinel error to cleanly abort the loop is idiomatic Go. -
Edge cases: The sentinel check with
errors.Is(err, errStreamDone)is correct sinceerrStreamDoneis a package-levelerrors.Newvalue. The existingcontext.Canceled/DeadlineExceededpath is preserved alongside it. -
Data loss: The comment correctly notes that drivers populate
ToolCallsResult/UsageResultbefore emitting[DONE], so no data is lost. -
Tests: The test file uses a
doneEmittingDrivermock that faithfully replicates the real driver behavior. It asserts"Hello world"output without the sentinel. Good coverage of the core scenario. It would be nice to also test an empty-delta stream (only[DONE]), but not a blocker. -
Nitpick (non-blocking): In
doneEmittingDriver.ChatStreamlyWithSender, ifsenderreturns an error mid-stream (includingerrStreamDonefrom our new check), the remaining deltas are skipped. That's fine — just noting the behavior in case any driver relies on all deltas being delivered before[DONE]. Given the comment that usage is populated before[DONE], this seems safe.
Verdict: LGTM — well-structured fix with good test coverage.
Summary
In Go mode, model drivers emit the OpenAI-style
[DONE]marker as their finalsendercall (mirroring the Python convention, where consumers break on it). The channel adapterchatStreamWithContext(internal/service/model_chat.go) forwarded every delta verbatim into the channel, including this terminal sentinel.As a result, channel consumers accumulated
[DONE]as answer text:AskService(search AI summary) appended it to the final answer, so the rendered AI summary ended with a stray[DONE].internal/handler/mindmap.go) had the same exposure.Fix: treat
[DONE]as end-of-stream in the adapter — abort the driver loop via a sentinel error and never forward the marker into the channel. Drivers populateToolCallsResult/UsageResultbefore sending[DONE], so nothing is lost.Adds
internal/service/model_chat_test.gocovering that the sentinel is stripped while content deltas pass through intact.bash build.sh --test ./internal/service/...passes.