Skip to content

fix(#541): hoist TextDecoder out of SSE read loops in all three adapters - #542

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-05_fix-541-sse-decoder
Sep 5, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-05_fix-541-sse-decoder

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

iterSseChunks (openai), iterSseEvents (anthropic) and iterSseEvents (responses) each created a new TextDecoder inside their read loop. With { stream: true }, an incomplete multi-byte UTF-8 sequence is held in the decoder instance's internal buffer — which was discarded every iteration. The next chunk's orphan continuation bytes (0x80–0xBF) then decoded as U+FFFD, corrupting exactly one CJK character per boundary split (issue #541: random 1–2 U+FFFD mid-character in compressed long sessions).

Fix: hoist const decoder = new TextDecoder() out of the loop in all three adapters — same pattern already used by observeSseTerminalState (src/stream-terminal.ts:40) and both SSE loops in src/plugin.ts. One line net effect per site.

Sites

  • src/loop/adapter-openai.ts — iterSseChunks
  • src/loop/adapter-anthropic.ts — iterSseEvents
  • src/loop/adapter-responses.ts — iterSseEvents

A repo-wide scan confirmed these are the only in-loop decoders (stream-terminal.ts and plugin.ts were already correct).

Regression tests

New F8 tests in tests/loop-adapters.test.ts: for each adapter, build a real SSE payload containing CJK text (残留), encode to bytes, split the stream into two chunks right after the lead byte of the second character (worst case: two orphan continuation bytes), run through parseStream, and assert the text round-trips exactly with no U+FFFD.

Verified the guard bites: with the fix stashed, all three F8 tests fail (残留 → 残\uFFFD\uFFFD); with the fix, they pass.

Pre-flight

  • npm run typecheck — pass
  • npm test — 1034/1035 pass; the single failure (launcher.test.ts "resolveClientCommand: codex/claude resolve to themselves") is pre-existing in this environment (sandbox has /usr/bin/codex, test expects bare codex) — reproduced identically on pristine master without this change
  • npm run build — pass
  • E2E (tests/e2e/e2e-codex.test.ts): preflight (E2E_CHECK=1) passes; full run blocked here because the default local sglang upstream (127.0.0.1:8199) is not running in this sandbox — ECONNREFUSED on forward, reproduced identically on pristine master (A/B verified). Needs the manual-dispatch CI or a machine with the local upstream up.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-05_fix-541-sse-decoder (4151575)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-542

Each push to this PR publishes a new version under the pr-542 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr542.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 PR 审核完成:通过,可合并。以下结论均为本沙箱独立复验(未仅采信 PR 描述)。

查重: #542 是既有 bug 报告 #541 的修复 PR(标题即 fix(#541)),非重复项;仓库内检索无其他同类 issue/PR。#541 线程中已确认过机制(循环内 new TextDecoder() 使 {stream:true} 的实例缓冲每轮被丢弃,下一块的孤儿续字节 → U+FFFD),与本次修复点一致。

独立验证:

  1. A/B 复验回归测试有效: 将三个 adapter 还原为 origin/master 版本后,F8×3 全部失败(残留 → 残\uFFFD\uFFFD);恢复修复后全部通过。测试确实能抓住这个 bug。
  2. 全仓扫描复核: grep new TextDecoder / .decode( 覆盖 src/,修复后已无循环内 decoder。其余出现点:src/server.ts:2771(错误体一次性完整解码,非流式)、src/server.ts:2979(tag-echo 诊断解码,原始字节经 res.write(chunk) 原样写客户端、不进写路径);src/plugin.ts:592/821 两处 SSE loop 与 src/stream-terminal.ts:40 原本就正确。"仅此三处"的声明成立。
  3. 模式一致性: hoist 写法与 observeSseTerminalState 完全一致;每次 generator 调用各自创建 decoder,无跨请求状态泄漏。
  4. 测试质量: F8×3 构造真实 SSE payload(CJK 残留),在第二字符 lead byte 之后切块(最坏情况:两个孤儿续字节 E7|95 99),经 parseStream 断言精确还原且无 U+FFFD。切分点选得对。

Pre-flight 复跑结果(本分支):

结论: 变更最小(三处各净 +1 行)、打在根因(decoder 实例生命周期)而非症状、回归测试经 A/B 证实有效、pre-flight 干净。分支直接基于最新 master,version 未动,commit message 合规。可以合并——按仓库规则 merge 仅限人工,请自行合并: #542

@ranxianglei
ranxianglei merged commit 4def206 into master Sep 5, 2026
6 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-05_fix-541-sse-decoder branch October 15, 2026 05:32
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.

1 participant