Skip to content

fix(relay): stop streaming on exact bare [DONE] markers - #8074

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/stream-bare-done-7929
Oct 28, 2026
Merged

seefs001 merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/stream-bare-done-7929

Conversation

@seefs001

@seefs001 seefs001 commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Agent

  • Tool: Pi(AI 辅助)
  • Tool version: 1.4.0
  • Model (full id): gpt-6-astra
  • Host (CLI / IDE / GitHub coding agent / other): CLI
  • Date (UTC): 2026-10-28

Links

User request

  • Verbatim:
你在 /Users/seefs/work/new-api(已是本地 checkout,禁止 git clone / 重复下载整仓)。任务:修 GitHub #7929 / Linear SEE-37,开干净 PR。

## Bug
流式转发时裸行 `[DONE]` 未当结束,被 `data[5:]`(或等价)切成 `]` 作为数据帧下发,客户端脏数据/流不结束。
期望:整行 trim 后等于 `[DONE]` 时按流结束处理,停止扫描并正确收尾;不要把 `]` 当下发内容。

## 做法
1. 先查 main / 已有 PR(含 #8036 等同题)是否已有可用修复;有则可基于重做或引用,勿重复脏提交。
2. 改动尽量小:优先 stream_scanner / 同类 SSE 扫描路径。
3. 加/改单测:证明裸 `[DONE]` 结束;非 `[DONE]` 前缀不被误杀。
4. 跑相关单测,记录命令与输出摘要。

## Git / PR(允许写 GitHub)
- 分支:可用 Linear 建议名或 `fix/stream-bare-done-7929`
- 按本机 remote 推到 fork(通常 seefs001),向 QuantumNous/new-api 开 PR
- **先读** `.agents/github/PR.md`,严格按它写完整 PR body:
  - Closes #7929
  - 勾选 Bug fix;短、人话、可复核
  - Proof of Work:写清命令与观察(单测名/输出摘要),禁止只写 go build passed
  - Checklist 全勾且真实
  - 禁止大段 AI 废话、禁止留模板占位说明段落

## 完成时回复(固定结构)
1) PR URL
2) 分支名
3) 关键 diff 摘要(文件+改了什么)
4) 单测命令与结果摘要
5) 是否基于/引用了已有 PR
6) 阻塞或风险(若有)

不要改 Linear;不要擅自合 PR。
  • Later constraints or corrections from the user (quote, or none): 无

Out of scope — refuse

  • Matched: no
  • If yes, what was told to the user: 不适用。修复公共扫描器,不是透传或特定渠道兼容。

Open gate — do not open unless all are satisfied

  • Out of scope (including pass-through-only forwarding): no
  • Usage / configuration / integration (answered instead of opening): no
  • Required issue facts present without invention: yes
  • Verification is actual commands or steps and observed results, not only go build or tests passed: yes
  • Body is short and factual; no unfiltered AI-generated text: yes
  • Open: yes
  • If no, what was told to the user: 不适用

Kind

  • Bug fix

Issue facts

  • Actual behavior: 裸 [DONE] 被切成 ],扫描继续。
  • Impact: 多交付脏帧,并处理结束标记之后的数据。
  • Frequency: 每次上游发送裸 [DONE] 时。
  • Evidence that the problem is in new-api rather than the client or upstream: 流式转发:裸 [DONE] 行未按结束处理,被切成 "]" 作为数据帧下发 #7929 指向公共扫描器的无条件切片;本次在未改生产代码时用单测复现。
  • Applicable types and their fields: relay,POST /v1/chat/completions 公共流式路径,任意流式模型,非透传;部署为源码 main 206e0fa5c(issue 报告)。billing/frontend 不适用。本次基线为 main 49271d2d8。裸 [DONE] 是兼容输入,不将它描述为 SSE 标准终止符。

Change

用 strings.CutPrefix 只移除实际存在的 data:,再 trim。无前缀行只有完整等于 [DONE] 才接受。结束判断改成相等比较,复用现有 done 与清理路径。

Research

Duplicate / prior art

Docs and code

  • https://docs.newapi.ai/ : 已打开首页及 ChatCompletions API,确认支持 stream;该页未规定 [DONE] 解析。
  • https://deepwiki.com/QuantumNous/new-api : 尝试访问,HTTP 429,未取得正文。
  • README / repo docs: README.md 的 Capabilities / Protocols and endpoints 声明支持流式;.agents/github/PR.md 为本正文模板。
  • Code paths and what they imply for this change: StreamScannerHandler 无条件切掉 5 字节;OpenAI 流式回调消费其交付数据。修复应在公共扫描器,不是客户端配置。

Alternatives considered

Files

Path Why
relay/helper/stream_scanner.go 安全剥离可选前缀,精确识别 [DONE]
relay/helper/stream_scanner_test.go 扩展现有 TestStreamScannerHandler_DoneStopsScanner 为 7 个表驱动用例

Behavior

  • Before: 裸终止符交付 ] 和后续帧;data: [DONE]extra 反而提前终止。
  • After: 裸、带空白和 data: 形式的完整 [DONE] 结束扫描;裸 [DONE]extra 忽略,data: [DONE]extra 正常交付,两者均不终止。
  • Explicit non-goals / leftover work: 不支持裸 JSON,不改其他扫描器、计费、鉴权或数据库。

Verification

Proof of Work

均在 /Users/seefs/work/new-api 执行,Go 1.27.2,darwin/arm64。

  • go test ./relay/helper -run '^TestStreamScannerHandler_DoneStopsScanner$' -count=1 -v
    • 修复前:7 个子测试中 4 个失败;bare 实际收到 ["before", "]", "after"],data_prefix_is_not_done 仅收到 ["before"]。
    • 修复后:7/7 PASS,包输出 ok .../relay/helper 0.657s。检查交付帧、EndReason=done、无结束错误及帧数。
  • go test ./relay/helper -count=1 → ok .../relay/helper 3.005s。
  • go test -race ./relay/helper -run '^TestStreamScannerHandler_DoneStopsScanner$' -count=1 -v → 7/7 PASS,ok .../relay/helper 1.723s,无竞态报告。
  • gofmt -l relay/helper/stream_scanner.go relay/helper/stream_scanner_test.go、git diff --check → 均无输出。
  • Manual steps and observed result: 未做人工端到端请求;以上为本地 handler 复现与回归验证。
  • UI: 无 UI 改动,不需要截图。
  • Tests added or updated: 更新现有测试,覆盖裸标记、空白/CRLF、有无 data: 后空格,以及两个非终止前缀用例。
  • Databases / providers / platforms exercised: 本地内存响应体,无真实上游或数据库;macOS arm64。
  • Not verified: 真实上游端到端请求、全仓测试及其他操作系统。

Risks

  • Failure modes: 共享扫描器影响所有调用方;[DONE]extra 不再提前终止是有意修正,已有定向测试。fix(relay): forward bare-JSON stream frames without "data:" prefix #6798 若先合并,同一代码块可能冲突。
  • Billing / quota / auth impact: 不修改这些逻辑;结束标记之后的数据不再交给调用方。
  • Follow-ups: 无本 PR 必需的后续改动。

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex / pass-through-only forwarding): no

Checklist

  • 只修改扫描器及其现有测试文件
  • 从最新已获取 main 重做,检查并引用已有 PR
  • 已观察修复前失败和修复后通过,记录命令与输出
  • 已检查格式及 diff,明确未验证范围

Summary by CodeRabbit

  • Bug Fixes
    • Improved streaming response handling so completion is recognized only when the [DONE] marker appears by itself, including with optional whitespace or a data: prefix.
    • Marker-like text with additional trailing content is now treated as response data rather than as a completion signal. Empty values are skipped.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7121fc6d-4563-49a6-b51b-7cc85f2800d1
Commits

Reviewing files that changed from the base of the PR and between 49271d2 and fe314d7.

Files selected for processing: 2
  • relay/helper/stream_scanner.go
  • relay/helper/stream_scanner_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The stream scanner now strips data: only when it is a prefix, trims the payload, skips empty values, and treats only an exact [DONE] payload as completion. Tests cover bare and prefixed markers, whitespace, carriage returns, and marker-like strings with trailing text.

Changes

SSE completion marker handling

Layer / File(s) Summary
Normalize and match completion markers
relay/helper/stream_scanner.go, relay/helper/stream_scanner_test.go
The scanner conditionally strips data:, trims the payload, skips empty values, and recognizes only an exact [DONE] marker. Table-driven tests check bare and prefixed markers, whitespace, carriage returns, and trailing text.

Priority: Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • QuantumNous/new-api#8036: Both PRs update StreamScannerHandler to handle bare [DONE] lines. This PR also requires an exact marker match.

Suggested reviewers: calcium-ion

Merge Risk: Minimal · up to fe314

No actionable merge-blocking risk was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: stopping streaming on exact bare [DONE] markers.
Linked Issues check Passed Linked issue #7929 has one coding requirement: a bare [DONE] line must end the stream and must not be forwarded as a data frame ("]"). The reviewed head changes StreamScannerHandler in relay/helper/st…
Out of Scope Changes check Passed The diff touches two files: relay/helper/stream_scanner.go (+4/-7 in the loop) and relay/helper/stream_scanner_test.go (the existing DoneStopsScanner test). No other scanner, billing, auth, or databas…
Full details: Linked Issues check

Explanation

Linked issue #7929 has one coding requirement: a bare [DONE] line must end the stream and must not be forwarded as a data frame ("]"). The reviewed head changes StreamScannerHandler in relay/helper/stream_scanner.go. It applies strings.CutPrefix("data:") and TrimSpace, then accepts an unprefixed line only when the trimmed value equals [DONE]. A matching line reaches the existing else branch, which calls SetEndReason(StreamEndReasonDone) and returns. The old data[5:] slice that produced "]" is removed. Automated coverage: TestStreamScannerHandler_DoneStopsScanner in relay/helper/stream_scanner_test.go is now table-driven. It covers bare [DONE], whitespace/CR variants, data:-prefixed forms, and the non-terminating cases [DONE]extra and data: [DONE]extra. Each case asserts the delivered frames, EndReason=Done, no end error, and the response count. The test file imports and helpers are present. I read the code and test expectations and checked them against the ScanLines CR handling. I did not execute the tests. The author reports 7/7 PASS, and that report is not independently verified here. The issue's reproduction body (data: {"id":1} / [DONE] / data: {"id":2}) maps to the bare case. Issue #7929 has no non-coding requirements that need assessment.

Full details: Out of Scope Changes check

Explanation

The diff touches two files: relay/helper/stream_scanner.go (+4/-7 in the loop) and relay/helper/stream_scanner_test.go (the existing DoneStopsScanner test). No other scanner, billing, auth, or database code changes. The one behavior change beyond the bare-marker case is intentional. A data: [DONE]extra line was previously treated as completion. It is now delivered as data, and a bare [DONE]extra line is ignored. This follows from the exact-equality rule for the done marker in the same predicate that the issue requires. The change is covered by the new test cases. The PR does not add bare JSON support or other channel-specific logic. Changes are connected to the issue objective and no unrelated change was found in the reviewed diff.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the stream at night,
Bare DONE marks the ending right.
No stray bracket hops along,
Trimmed-up markers end the song.
The scanner rests; the tests say so.

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

@seefs001
seefs001 merged commit 55515db into QuantumNous:main Oct 28, 2026
3 checks passed
ustoolscn pushed a commit to ustoolscn/new-api that referenced this pull request Oct 29, 2026
kyuhwi-choi pushed a commit to attodev/new-api that referenced this pull request Oct 30, 2026
woncc added a commit to 86669666/new-api that referenced this pull request Nov 1, 2026
…#8074) (#105)

(cherry picked from commit 55515db)

Co-authored-by: Seefs <40468931+seefs001@users.noreply.github.com>
NaturalSelect pushed a commit to NaturalSelect/new-api that referenced this pull request Nov 5, 2026
shirosoralumie648 added a commit to shirosoralumie648/newapi-rev that referenced this pull request Nov 5, 2026
Sync 75 upstream commit(s) via merge (fork history preserved).

Upstream commits:
- 7061447 fix(auth): 校验 Redis 鉴权缓存完整性 (QuantumNous#8200)
- ba5f736 fix(gemini): bill embeddings from upstream usageMetadata (QuantumNous#8182)
- 94b5216 feat(vertex): add per-channel v1beta1 endpoint switch (QuantumNous#8179)
- 83b18fe feat(channel): return created IDs with input mappings (QuantumNous#8164)
- 52c1730 feat(gemini): add Argon fields and reasoning mappings (QuantumNous#8177)
- 9b473a3 fix(twofa): restart the failure count after a lockout expires (QuantumNous#8172)
- 45746ee feat(relay): add opt-in HTTP/2 health checks (QuantumNous#8059)
- c046559 feat(plugins): bill gemini omni by the tokens vertex reports (QuantumNous#8153)
- 651d5a2 fix(xai): preserve token details in stream usage conversion (QuantumNous#6420)
- 70e6af6 perf: read and edit request JSON and base64 with moego (QuantumNous#8148)
- d0abbf6 feat(plugins): generate video with Gemini Omni on Vertex AI channels (QuantumNous#8142)
- ea343c4 fix(plugins): charge relayed streams even when the reserve or task insert fails (QuantumNous#8121)
- fe57cba fix(plugins): prefer Kling's returned price (list_price) for Kling 4.0 cash billing (QuantumNous#8125)
- 1424ffd fix(plugins): load plugins that declare their own /{key} address (QuantumNous#8119)
- 8b253de fix(plugins): keep Kling 4.0 durations in range and map every video size (QuantumNous#8118)
- c59f6fd feat(plugins): serve task plugins at the vendors' own paths and answer as the vendors do (QuantumNous#8117)
- 37d23dc fix(profile): honor global sidebar visibility (QuantumNous#7429)
- 7743627 fix(deps): upgrade DOMPurify to 3.4.17 (QuantumNous#8116)
- 883eeb8 feat(plugins): warn about task models a plugin cannot charge
- 1e8c707 chore(deps): bump @codemirror/language from 6.12.4 to 6.13.1 in /web (QuantumNous#8102)
- 6e78cb3 chore(deps): bump marked from 18.0.6 to 18.1.2 in /web (QuantumNous#8103)
- 9fc70bb chore(deps-dev): bump @tanstack/react-query-devtools in /web (QuantumNous#8104)
- cef7820 chore(deps-dev): bump knip from 6.27.0 to 6.43.0 in /web (QuantumNous#8105)
- 1695171 chore(deps): bump tailwind-merge from 3.6.0 to 3.7.0 in /web (QuantumNous#8106)
- 7fa815e chore(deps-dev): bump fast-uri from 3.1.7 to 3.1.9 in /electron (QuantumNous#8007)
- ee0f0ac chore(deps-dev): bump electron from 41.10.6 to 42.11.16 in /electron (QuantumNous#8008)
- 837b814 chore(deps): bump undici in /electron (QuantumNous#8006)
- 3150dfe chore(deps): bump github.com/go-jose/go-jose/v4 from 4.1.4 to 4.1.6 (QuantumNous#8112)
- a3851c2 fix: save Turnstile keys before enabling (QuantumNous#7960)
- 4193734 fix(logger): keep gin access and recovery logs on the current log file after rotation (QuantumNous#8098)
- f366017 Merge commit from fork
- 829f082 fix: unmount authenticated layout on sign-out to stop credential-less refetches (QuantumNous#8110)
- 9eda50a Merge commit from fork
- a2defd2 Merge commit from fork
- e449cc7 feat: sample immediate task successes in model performance metrics (QuantumNous#8067)
- fcc241c fix: start affinity fallback at the highest channel priority (QuantumNous#8078)
- b08ee86 fix(cloudflare): preserve embedding response data (QuantumNous#8041)
- 26c488c fix(relay): allow Azure channels to opt in to Responses WebSocket (QuantumNous#8047) (QuantumNous#8075)
- 55515db fix(relay): terminate streams on exact bare done markers (QuantumNous#8074)
- 49271d2 fix(aws): preserve safeguards in Bedrock Invoke requests (QuantumNous#8071)
- 5478811 fix: count completed non-stream relays after context cancellation (QuantumNous#7844)
- ad56112 fix(relay): apply GPT-6 chat parameter rules to gpt-6.1-sol and gpt-6.1-terra (QuantumNous#8064)
- 01da3a5 perf(relay): stop recompiling param override regexes on every request (QuantumNous#8061)
- 34003e9 test: stabilize channel affinity cache keys (QuantumNous#8009)
- 0165fef fix(relaykit): preserve Gemini text alongside tool calls (QuantumNous#7998)
- 4d962e4 fix(relaykit): preserve tool argument numbers in Claude responses (QuantumNous#7996)
- f1569d1 fix(pricing): classify dynamic per-request billing correctly (QuantumNous#8010)
- bc795cc fix:修复stop为非字符串和[]string的协议转换问题 (QuantumNous#7935)
- 9823ebc fix(billing): bill cache_creation_input_tokens reported in prompt_tokens_details (QuantumNous#7909)
- e835b1b chore(deps): bump axios from 1.18.1 to 1.20.0 in /web (QuantumNous#7835)
- 5c8032d chore(deps): bump react-i18next from 17.0.10 to 17.1.0 in /web (QuantumNous#7981)
- 0b667ca chore(deps): bump i18next from 26.3.6 to 26.5.3 in /web (QuantumNous#7979)
- 4105bd0 chore(deps): bump input-otp from 1.4.2 to 1.5.0 in /web (QuantumNous#7980)
- b6b85ca chore(deps): bump react-resizable-panels from 4.12.2 to 4.14.6 in /web (QuantumNous#7982)
- 1dafb19 fix(gemini): preserve top-level imageConfig in chat conversion
- 0034b24 fix(task): wait up to 72 hours before timing out tasks
- a404460 chore(web): upgrade Bun to 1.4.4 and use bun check
- 33c61a2 fix: prevent stale values when switching edit drawer records (QuantumNous#7989)
- 30748c6 fix(logging): 隐藏访问日志中的查询凭据 (QuantumNous#7972)
- 7fb6e57 fix(channels): preserve advanced custom body passthrough settings (QuantumNous#7971)
- e9d4d63 feat(billing): bill OpenAI 12-hour prompt cache writes at their own price (QuantumNous#7965)
- d0b840e fix(relay): keep the client's JSON Schema property order
- d8e6561 feat(web): copy model names from usage log details
- 8802113 feat(task): let ordinary channels compete on shared plugin endpoints
- eb169e7 feat(pricing): adjust synced upstream prices by the site exchange rate
- e2321a5 chore(web): sort locale keys
- db80ac8 fix(task): let task plugins validate their own requests
- dbe97be feat(task): pass client request headers to task plugin drivers
- 556517f feat(task): accept multipart bodies on native plugin routes
- 94cc3ff fix(billing): bill Claude cache writes that arrive without a 5m/1h split
- 7e65967 fix(relay): stop cutting off slow clients during large stream events
- 0c2af97 fix(relay): finish delivered Responses streams without false disconnect errors (QuantumNous#7956)
- 7d8b912 fix: upgrade Go to 1.26.10 for CVE-2026-111943 (QuantumNous#7942)
- 4d36f2c chore(deps-dev): bump vitest from 4.1.10 to 5.0.4 in /web (QuantumNous#7838)
- eea05e0 chore(deps): bump github.com/klauspost/compress from 1.18.3 to 1.18.7 (QuantumNous#7931)

Local tests not run (CI-only).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

流式转发:裸 [DONE] 行未按结束处理,被切成 "]" 作为数据帧下发

1 participant