Repository navigation
Conversation
StreamScannerHandler accepted a line starting with "[DONE]" but then sliced five bytes off every accepted line, turning a bare "[DONE]" into "]" that was forwarded as a data frame while the scanner kept reading past the end of the stream. Trim only the optional "data:" field name so both forms reach the terminator check.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Scanner handling and tests relay/helper/stream_scanner.go, relay/helper/stream_scanner_test.go |
The scanner removes only an optional data: prefix. Tests verify that bare and prefixed [DONE] lines stop the stream after the preceding payload, and that payload whitespace is preserved. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers: calcium-ion
Merge Risk: 
Minimal · up to 9ab2f
-
The change appears ready to merge after normal checks; no outstanding issue is identified.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Title check
-
Explanation
The title names the main change: the stream scanner now ends the stream when it reads a bare [DONE] line in StreamScannerHandler. It also names the affected area, relay. The raw summary and objectives both describe the stream terminator fix in relay/helper/stream_scanner.go as the core change, with tests added for bare and prefixed [DONE] lines. The title is specific, concise, and free of noise. The fix: prefix removal is now optional, so bare [DONE] lines reach the existing terminator check.
Full details: Linked Issues check
-
Explanation
Issue
#7929requires that a bare[DONE]line end the scan and not be forwarded as a]data frame. At the base,StreamScannerHandler(relay/helper/stream_scanner.go, around line 304-307) accepts a bare[DONE]line, becausedata[:6] == "[DONE]"passes the gate. It then runsdata = data[5:]unconditionally, which yields]. That value is not[DONE]-prefixed, so it is sent todataChan. At head the line isdata = strings.TrimPrefix(data, "data:"). A bare[DONE]stays intact and reaches thestrings.HasPrefix(data, "[DONE]")check, which setsStreamEndReasonDoneand returns. Fordata:-prefixed lines,TrimPrefixremoves the same 5 bytes thatdata[5:]removed, so payload handling is unchanged. Thelen(data) < 6guard and the empty-line skip are unchanged. The issue's reproduction body (data: {"id":1},[DONE],data: {"id":2}) now forwards only{"id":1}and stops. Tests in relay/helper/stream_scanner_test.go cover this:TestStreamScannerHandler_BareDoneStopsStream(bare anddata: [DONE]forms, asserting one delivered frame,StreamEndReasonDone, andReceivedResponseCount == 1) andTestStreamScannerHandler_PayloadPreservedVerbatim. The test results in the PR description were not re-run in this review. The conclusion rests on source reading of the diff and the loop logic.
Full details: Out of Scope Changes check
-
Explanation
The whole-PR diff against the merge base touches two files. relay/helper/stream_scanner.go changes one statement,
data = data[5:]tostrings.TrimPrefix(data, "data:"), and adds a 3-line comment that explains the change. relay/helper/stream_scanner_test.go adds two tests for the same bare[DONE]termination and payload preservation fordata:lines. Both changes connect directly to#7929. The tests support the fix and are not unrelated work. The diff does not change the accepted-line gate, client frame formatting, pass-through paths, or other SSE parsing. The PR description refers to related PRs (#8023,#7777,#6798), but the reviewed code does not include their changes. Their presence or absence does not affect this assessment.
-
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Warning
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.
A rabbit watched the data flow,
Then saw the bare [DONE] line glow.
The stream stops where the marker lies,
No stray bracket hops outside.
Payload spaces stay as they go.
Comment @coderabbitai help to get the list of available commands.
一句话:
StreamScannerHandler对每个被接受的 SSE 行无条件执行data = data[5:],裸[DONE]行被切成]并当作数据帧转发,扫描也不在结束符处停止;本 PR 把这一步改成只去掉可选的data:前缀(1 行),裸[DONE]与data: [DONE]行为一致。是不是重复:main 上该缺陷仍在(
relay/helper/stream_scanner.go);同题的 #7932 已被维护者关闭、#6798 属于另一个 issue 且停更 2 个月,详见「Research → Duplicate / prior art」。Agent
Links
relay/helper/stream_scanner.go)User request
Quote the request the user made to the agent as faithfully as possible.
Keep the original language and line breaks. Do not rewrite, summarize,
translate, or turn it into a PR description.
Out of scope — refuse
If the change matches any item below, tell the user this repository does not
accept it and do not open a PR.
Open gate — do not open unless all are satisfied
If any item fails, tell the user and stop. Do not open the PR.
go buildor tests passed: yesKind
Issue facts
Take these from the linked issue. If a needed item is empty, ask the user that question.
relay/helper/stream_scanner.go的StreamScannerHandler对每个被接受的 SSE 行无条件执行data = data[5:]。裸[DONE]行(无data:前缀)会被切成"]"并作为一帧转发给客户端,扫描循环也不在[DONE]处结束。[DONE]后继续扫描;客户端在正常数据帧之外多收到一个内容为"]"的帧。[DONE]行(SSE 允许的形式)结束流时触发。stream_scanner.go,不依赖客户端或某个具体上游;用本地StreamScannerHandler单测即可复现。relay/helper/stream_scanner.go,各 chat/completions 流式端点的公共路径,任意流式模型,SSE,透传未开启);billing、frontend、deployment 不适用。Change
relay/helper/stream_scanner.go的扫描循环里,先用data[:5] != "data:" && data[:6] != "[DONE]"过滤出可接受的行(relay/helper/stream_scanner.go:304),然后无条件执行data = data[5:](原第 307 行)。SSE 允许省略字段名,因此
[DONE]可以裸写:data: [DONE]→ 去掉data:后为[DONE],被strings.HasPrefix(data, "[DONE]")识别为结束符(relay/helper/stream_scanner.go:312),扫描终止。[DONE](裸行)→ 被切成],HasPrefix("[DONE]")不成立,于是]被当成一帧数据dataChan <- data转发给客户端,扫描循环也不会在结束符处返回。本 PR 把
data = data[5:]换成data = strings.TrimPrefix(data, "data:"):data:前缀的行:TrimPrefix只去掉字段名,剩下的载荷与原来data[5:]的结果完全一致(包括data:后面带空格的情况,后面紧跟strings.TrimSpace)。[DONE]行:没有data:前缀可去,TrimPrefix原样返回[DONE],于是能被第 312 行的结束符判断识别,扫描在正确的地方终止,]不会再被转发。其余逻辑(长度守卫
len(data) < 6、空行跳过、ReceivedResponseCount统计、结束原因StreamEndReasonDone)保持不变。改动只涉及 1 行有效代码。Research
Duplicate / prior art
gh search prs --repo QuantumNous/new-api "7929"→ fix(relay): end stream on bare [DONE] line in StreamScannerHandler #7932(已关闭)。gh pr list --repo QuantumNous/new-api --state open --search "stream_scanner DONE"→ 0 条;--search "SSE [DONE]"→ 无直接对应 PR,最接近的是 fix(relay): forward bare-JSON stream frames without "data:" prefix #6798(开放中)。gh api "repos/QuantumNous/new-api/commits?path=relay/helper/stream_scanner.go"→ 最近一次改动是 2026-10-22 的 fix(relay): 正确结束已完成的 Responses 流 #7956 相关提交(流式保活),与本 issue 无关。data = data[5:]换成strings.TrimPrefix(data, "data:")),标题为「fix(relay): end stream on bare [DONE] line in StreamScannerHandler」,正文Fixes #7929。该 PR 在当天 05:40 被维护者 seefs001 关闭:未合并、未留评论,关闭原因在 PR 内没有记录,我不做推测。main 上该 bug 至今仍未修复(relay/helper/stream_scanner.go在此之后的提交是 fix(relay): 正确结束已完成的 Responses 流 #7956 的 Responses 流收尾与 fix(relay): Claude 流式请求在准备和重试期间持续保活 #7857 的流式保活,均与[DONE]终止符无关)。data:前缀的裸 JSON 帧),但它的改法把data = data[5:]换成if strings.HasPrefix(data, "data:") { data = data[5:] },因此顺带覆盖了裸[DONE]。relay/helper/stream_scanner.go里同一个 hunk 的相邻两行(第 304 行的放行条件、第 307 行的前缀剥离)。fix(relay): forward bare-JSON stream frames without "data:" prefix #6798 把无条件切片改成「有data:才切」,这一步本身就让裸[DONE]不再被截成],也就是说就 流式转发:裸 [DONE] 行未按结束处理,被切成 "]" 作为数据帧下发 #7929 这个症状而言,fix(relay): forward bare-JSON stream frames without "data:" prefix #6798 的 diff 已经顺带修掉了。go test ./relay/helper -run TestStreamScannerHandler -count=1→ 裸[DONE]与data: [DONE]两个子测试均 PASS,relay/helper包整体ok。data:前缀和[DONE]),只把前缀剥离换成strings.TrimPrefix,即只修 流式转发:裸 [DONE] 行未按结束处理,被切成 "]" 作为数据帧下发 #7929 报告的终止符问题,不引入 [Bug] Anthropic stream 空 body:上游无 "data:" 前缀的裸 JSON 帧被 StreamScannerHandler 丢弃(deepseek-v4-pro) #6797 的「裸 JSON 帧也要转发」这一额外行为变更。改动面 1 行 vs fix(relay): forward bare-JSON stream frames without "data:" prefix #6798 的 2 行。strings.TrimPrefix与if strings.HasPrefix写法等价);若维护者认为 fix(relay): forward bare-JSON stream frames without "data:" prefix #6798 已覆盖本问题而希望合并为一个改动,请直接 close 本 PR,本 PR 不含 fix(relay): forward bare-JSON stream frames without "data:" prefix #6798 的行为。Docs and code
Open them. Do not write "already checked" without sources.
https://docs.newapi.ai/en/docs/api/ai-model/chat/openai/createchatcompletion、https://docs.newapi.ai/en/docs/api/ai-model/chat/createmessage(及中文版):未提及data:帧格式或[DONE]。https://docs.newapi.ai/llms.txt(全站页面索引):全站没有 SSE/streaming 说明页。README.md:138:「Use OpenAI Chat Completions, Responses, Anthropic Messages, and Gemini APIs; stream responses and use tools…」;:157提到 RelayKit「provides request, response, and streaming conversion」——README 只说明支持流式,不规定帧格式。docs/plugin-api/v1.md:558「### Optional SSE response reader」与:624「### Optional host-managed SSE accumulation」讲的是宿主 → 插件的提交流,不是上游 → 客户端的转发路径。docs/openapi/relay.json:未定义 SSE 帧格式。relay/helper/stream_scanner.go:301:if len(data) < 6 { continue }——所以data[:6]不会越界panic,本 issue 不是安全问题。relay/helper/stream_scanner.go:304:先按data[:5] != "data:" && data[:6] != "[DONE]"放行,说明代码本身已经认为裸[DONE]是合法输入;问题出在紧接着的无条件切片。relay/helper/stream_scanner.go:312:strings.HasPrefix(data, "[DONE]")才是真正的终止符判断,说明下游本来就按「去掉前缀后的内容」来识别终止符,与本 PR 的改法一致。[DONE]当终止符:relay/channel/openai/chat_via_responses.go:79(if data == "" || data == "[DONE]")、relay/channel/cloudflare/relay_cloudflare.go:50(if data == "[DONE]")、controller/channel-test.go:626,649(bytes.Equal(payload, []byte("[DONE]"))。本 PR 的改法与这些位置的行为对齐。Alternatives considered
[DONE],是则直接走终止符分支,否则照旧切 5 字节(把判断上移到:304)。strings.TrimPrefix(data, "data:")(本次采用)。data[:6] != "[DONE]"的判断结果参与后续流程,代码更长;Option B 一行即可,且TrimPrefix的语义恰好是「有data:前缀才去掉」,与 SSE 里字段名可省略的规则一致。两种写法对data: [DONE]与普通data: {...}行的行为完全相同,Option B 的额外风险为零。Files
relay/helper/stream_scanner.godata:字段名,1 行relay/helper/stream_scanner_test.goTestStreamScannerHandler_BareDoneStopsStream(裸[DONE]与data: [DONE]两种形式)与TestStreamScannerHandler_PayloadPreservedVerbatim(载荷不被改写)Behavior
[DONE]行结束时,客户端会额外收到一帧内容为]的数据;扫描器不在结束符处停止,继续读上游剩余内容。[DONE]与data: [DONE]行为一致,都在结束符处终止扫描并置结束原因为StreamEndReasonDone,客户端只收到真实数据帧。data:前缀的转发形式。Verification
Only what was actually run. Do not state only that
go buildor tests passed.Each item needs the command or steps and the observed result.
go vet ./...(根模块)→ 退出码 0,无输出。go build ./...(根模块)→ 退出码 0。(main.go:44的//go:embed web/dist需要web/dist/index.html,本地按 CI 的做法先建占位文件;web/dist被 .gitignore 忽略,不在提交里。)go test ./relay/helper -count=1→ok github.com/QuantumNous/new-api/relay/helper 5.891s(含新增的两个测试)。go test ./... -count=1(根模块全量)→ 除controller外全部ok;controller的失败见「Not verified」。GOWORK=off go vet ./...→ 0;GOWORK=off go build ./...→ 0;GOWORK=off go test ./... -count=1→ 0。GOWORK=off go vet ./...→ 0;GOWORK=off go build ./...→ 0;GOWORK=off go test ./... -count=1→ 0。gofmt -l relay/helper/stream_scanner.go relay/helper/stream_scanner_test.go→ 无输出。TestStreamScannerHandler_BareDoneStopsStream用响应体data: {"x":1}\n\n[DONE]\n\ndata: should_not_appear\n\n走StreamScannerHandler,断言只收到["{\"x\":1}"]、结束原因为done、ReceivedResponseCount == 1;子测试分别覆盖裸[DONE]与data: [DONE]两种写法。relay/helper/stream_scanner.go换回 main 版本、保留新测试后重跑 → FAIL,实际收到["{\"x\":1}", "]", "should_not_appear"]——正是 issue 流式转发:裸 [DONE] 行未按结束处理,被切成 "]" 作为数据帧下发 #7929 描述的「多收到一帧]」且「[DONE]之后继续扫描」。改回本 PR 的版本后 → PASS。TestStreamScannerHandler_PayloadPreservedVerbatim确认带data:前缀的载荷在改动后逐字节不变(TrimPrefix不会动到载荷内容)。go test ./relay/helper -count=1也全部通过。httptest构造响应体直接驱动扫描器。[DONE]的上游);测试在 handler 层用构造的响应体覆盖。go test ./... -count=1在 Windows 上只有controller包失败(52 个测试),报错为TempDir RemoveAll cleanup: unlinkat ...\audit.db: The process cannot access the file because it is being used by another process——sqlite 文件锁在临时目录清理阶段的 Windows 专有问题。在未修改的 main 上单独跑go test ./controller/... -count=1得到同一批失败(同一批测试名、同一原因),本 PR 未触碰controller涉及的任何代码。relay/channel包整体ok;同批测试在另两次并行跑中出现过TestUpstreamGetBody_HTTP2*失败(Windows socket 报错An established connection was aborted by the software in your host machine),单独跑go test ./relay/channel -run 'TestUpstreamGetBody' -count=1→ 4 个测试全部 PASS,属并行负载下的抖动,与本改动无关。Risks
data:才切 5 字节」变成「有data:才去前缀」。对data: {...}、data: [DONE]的行为与原来逐字节一致(TrimPrefix去掉的就是原来被切掉的那 5 个字符),差别只在没有data:前缀的行。data:前缀且不是[DONE]的行(例如裸 JSON 行{"type":...})在改动后仍然会被data[:5] != "data:" && data[:6] != "[DONE]"过滤掉,不会被误当作数据帧转发——这一点没有变化。[DONE]与后续内容,修复后扫描在[DONE]处停止,后续内容不再被读取,与 SSE 语义一致。[DONE]后还会把]和后续帧转给客户端,现在不会。data:前缀」的输入,可把两个改动合并到一个 PR 讨论;本 PR 只针对 流式转发:裸 [DONE] 行未按结束处理,被切成 "]" 作为数据帧下发 #7929 报告的终止符问题。Scope check