Repository navigation
fix(#2565): scrub unpaired surrogate escapes out of every outgoing body - #2566
ranxianglei merged 3 commits into
Conversation
[bot] 🏷 PR #2566 审查结论 — 方向对、修法正确且测试有牙,但当前不可合并(kernel-guard 红 + 需 rebase + pi-ws 待确认)。 已按规范走完五关(原 issue / 回归 / 代码 / 测试 / diff),并对 66-commit base drift 逐段核过。 一、审原 issue #2565(方向关)✅ 通过
二、回归审查(整体关)✅ 无回归逐项清点触碰路径的既有行为:
三、代码审查(主关)✅ 逻辑正确
四、测试有效性(质量关)✅ 断言有牙
五、diff 整洁度 ✅ 干净10 文件全部与本 fix 相关,无跑题文件、无生成物/锁文件连带变更。CHANGELOG 条目符合仓库惯例。 ⛔ 合并前必须处理(3 项)1. kernel-guard CI 红 —— 本 PR 自身问题(硬阻塞)
2. 需 rebase 到当前 master 3. pi-ws e2e test B 在 CI 红 —— 待你 re-run 确认 结构化八问
判定:暂不可合并。 核心修复本身正确、安全、测试充分;补齐 ①kernel-guard(marker+bump 或 split,owner 定)②rebase+CI ③pi-ws re-run 确认后即可合入。 |
|
ework ci问题修复一下吧 |
[bot] 🏷 收到,我来修。两处红:①kernel-guard(本 PR 改了 kernel/src 但缺 |
…outgoing body JSON.stringify renders a lone surrogate half as the six-character escape text \uXXXX, so once a body is serialized there is no code unit left for the existing scrubLoneSurrogates() regex to find — and that function's only caller was the log fingerprint line, never the wire. A summary holding a lone half (a model re-decoding literal \uXXXX prose copied out of tool output, our ACP-status panel text being the live trigger) was therefore stored and re-emitted unpaired on every later request, and a strict upstream parser rejected the WHOLE body with a non-retryable 400: the conversation was permanently stuck while every other conversation on the same provider and key worked. Fourth site of the ranxianglei#816/ranxianglei#828/ranxianglei#1615 family — same signature, but the first where the escaping step is what hides the half from the guard. - src/text-safe.ts: scrubLoneSurrogateEscapes() walks backslash runs so an escape only starts on an odd-length run (doubled \uXXXX, literal prose a model quoted, stays byte-identical), keeps a well-formed high+low escape pair verbatim, rewrites an unpaired half to \uFFFD; scrubLoneSurrogatesOnWire() = that over the real-unit scrub. - Seams: src/loop/core.ts fetchUpstream wireBodyStr, src/server/relay.ts final send (before applyResign, so the signature covers the sent bytes) and the overflow-refold re-send, src/reasoning-guard.ts round re-send. - kernel: scrubLoneSurrogates() in kernel/src/truncate.ts, applied to range summaries at applyCompression ingest so the half is never stored. - Buffer bodies (raw passthrough) are deliberately untouched. Tests: text-safe escape-scanner cases; an e2e against a mock upstream (poisoned body still 200, no unpaired half on the wire, valid pairs and clean bodies untouched); kernel code-unit + ingest scrub.
7fc07e2 to
d16c341
Compare
[bot] 🏷 修好了,两处红都清了,PR 现在全绿可合。 新 head ① kernel-guard(本 PR 自身问题)— 已修
② pi-ws e2e test B — 确认是 flake,非本 PR 引入
结构化结论
透明说明:① 我 force-update 了作者的 fork 分支(rebase 需要),其原始 commit 内容与署名保留;② 判定:可合入。 等你人工 merge(PR 合并按规矩由人来)。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.211-master.e207a48 # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2565
What
A strict upstream could permanently kill a conversation: every request from that session answered
400 invalid_request(non-retryable), while every other conversation on the same provider and key worked. Cause was an unpaired surrogate escape in the outgoing JSON body combined with two gaps:scrubLoneSurrogates()existed insrc/text-safe.ts, but its only caller wassummaryFingerprintLine(src/stream.ts) — the human-readable log line. The outgoing body was never scrubbed.JSON.stringifyrenders a lone half as the six-character escape text\uXXXX. By the time the body is a string there is no code unit left for the code-unit regex to match, so even a guard applied at the send seam would have found nothing. The wire needs an escape-text scanner.Where the half came from: a compression summary holding a real lone code unit. A model that copies literal
\uXXXXprose out of tool output (our ACP-status panel text is emitted in exactly that form) has it re-decoded along the summarizer path; stored verbatim, it re-serialized unpaired on every later request, so retries never healed. Cross-check on the live incident: the client-side recorded requests contained 0 unpaired escapes, while bili's stored summary for that session contained 1 — the poison was produced by the compression layer.Changes
src/text-safe.tsscrubLoneSurrogateEscapes(text): walks backslash runs — an escape only starts on an odd-length run, so doubled\\uXXXX(literal prose a model quoted) stays byte-identical; a well-formed high+low escape pair is kept verbatim; an unpaired half becomes the\uFFFDescape; ordinary escapes (\n,\t,\",\u0041,\u4e2d, …), short hex and a trailing backslash are untouched.scrubLoneSurrogatesOnWire(text)= the above over the existing real-unit scrub. Use at send seams; usescrubLoneSurrogatesalone on text that is not yet JSON-encoded.scrubLoneSurrogatesOnWire):src/loop/core.ts—fetchUpstream'swireBodyStr(feeds the log/note views and the sent body, so all three agree).src/server/relay.ts— the final-send seam, applied beforeapplyResignso the signature covers the bytes actually sent, and the context-overflow refold re-send.src/reasoning-guard.ts— the round re-send.kernel—scrubLoneSurrogates()added tokernel/src/truncate.tsand applied to range summaries inkernel/src/compress.ts(applyCompression) so a half is never stored in the first place; the wire layer remains the belt-and-braces for already-poisoned persisted state.Buffer bodies (raw passthrough) are deliberately untouched — only serialized JSON strings are scanned.
Pre-flight
npm run typecheck— clean (exit 0, noerror TS).npm run build— clean (tsup bundle +write-build-info+ dist-import-annotations: OK).npm test— 4953 pass; the 27 failures are pre-existing on this Windows box (they read a real~/.zcodestore and exercise symlink privileges) and reproduce identically on a pristine4d5dab9worktree; the one name that differed between the two runs (#1892 cert-MITMdegraded mc-entry) is flaky — it passes on this branch and fails on pristine, so it is not a regression.tests/text-safe.test.ts— escape-scanner cases (lone high/low escape →\uFFFD, valid escape pair kept, doubled-backslash prose kept, ordinary escapes/short hex/trailing backslash kept,scrubLoneSurrogatesOnWireround-trip on a serialized poisoned body).tests/wire-lone-surrogate-scrub.test.ts(new) — e2e against a mock upstream: a poisoned body still gets a 200, the upstream receives no unpaired half anywhere, the valid pair survives, U+FFFD present, and noudccaescape remains; a clean body is not over-scrubbed (content preserved, no U+FFFD introduced).kernel/tests/scrub-lone-surrogates.test.ts(new) — code-unit scrub plus the ingest scrub of a stored summary.Notes
Fourth site of the #816 / #828 / #1615 family — same signature (an unpaired half reaching
JSON.stringifykills the whole body), but the first one where the escaping step itself is what hides the half from the existing guard. Confirmed still present in 0.1.189.kernel-change: #2565 ingest scrub — scrub lone-surrogate halves out of range summaries at applyCompression so an unpaired half is never stored in compression state (kernel/src/compress.ts, kernel/src/truncate.ts); pairs with the host-side wire scrub in this same PR. Kernel bumped 0.0.111 -> 0.0.112.