Repository navigation
fix: strip model-emitted bili internal artifacts from output streams (#1634) - #1649
Conversation
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.1597-pr.1649.b0d1a1fThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr1649.tgz
npm install -g packageInstall instructions are refreshed on each push. |
…1634) The proxy injects three artifact families into model-visible payloads (outbound checkpoint carriers, forged-compaction handoff user messages, captured summary blocks). Under sustained context pressure models restate these verbatim as their answer; the existing sanitizer only knew ACP render tags and [ACP] marker lines, so all four observed shapes passed through to the client transcript and the next turn answered metadata instead of the user (long sessions stall). Adds a third streaming filter stage (createBiliArtifactFilter) plus whole- text support folded into stripAcpTags, composed into every wire's filter stack and guarded at every hadEcho/rewrite site: - well-formed <bili-chain .../> spans (validated by the frozen parser, literal or \u003c-escaped form) are stripped anywhere in prose; - line-start '[bili] context summary after compaction:' and '[Compressed conversation section]' blocks are cut to end of field; - unterminated/over-cap openings release as prose byte-exact (#1039/#644); - request-side stamped carriers are untouched; #1542 inbound backstop unchanged. Behavior change (model output only): the four shapes above are now stripped on all wires where they previously passed through verbatim. Degenerate-turn retry integration preserved: a fully-forged turn yields zero visible text and fires the existing one-time continuation nudge. Debug observability: [stamp]/[inject] debug logs at every outbound append site (chain stamping, ephemeral nudge, image note, forged-summary developer re-injection). ACP_DUMP_REQ remains the byte-level forensic.
5a88def to
fc00bf6
Compare
[bot] 🏷 PR Review — Reviewed at head ① Direction (原 issue): root-cause layering is correct — bili injects internal bookkeeping into model-visible payloads, and under sustained pressure models restate it verbatim; the client history then perpetuates the stall loop. Fixing at the output side is the right layer: the injection side (stamped carriers, compaction handoff, forged-summary re-injection) must stay visible to the model for compression to work, and the request side must keep carrying checkpoints. Scope matches exactly what was settled in the #1634 thread (well-formed-only strip, header-block-to-end-of-field, mid-line mentions untouched, no config surface). ② Regression audit — existing behaviors on touched paths, item by item:
Regressions found: none (0). ③ Code: clean design — one shared module, stripper keys off the same constants its injectors emit (byte-match by construction), frozen parser One known limitation (non-blocking, probed and confirmed): left-to-right scanning means a closed-but-invalid chain opener (e.g. missing digest) shields any well-formed span that follows it in the same text/buffer — the good one leaks once. This is the conservative side of the agreed 宁漏剥不误伤 policy (real-world likelihood low: model would have to emit both an invalid and a valid carrier in one turn). Optional hardening if you care: when a terminator IS found but parsing fails, it's safe to resume scanning after that terminator (the span is bounded) — unterminated openers can't be skipped safely, which the 640-char cap already bounds. Happy to take that as a tiny follow-up if you want it pinned; otherwise leave as-is. Minor nit: PR body says 15 tests, the file has 16 ④ Tests: high-quality, teeth everywhere — exact-equality assertions, deliberate false-positive traps ( ⑤ Diff: single linear commit, +526/−60 over 14 files, every line serves the stated purpose (filter module, composition sites, hadEcho/rewrite gates, debug logs, constant exports, tests). No whitespace churn, no unrelated files. Clean. Flagged behavior change (as disclosed in the PR body, confirmed accurate): model output on all wires — well-formed Verdict: mergeable. ✅ 一句话:修复了模型在长会话压力下把 bili 内部压缩元数据(链检查点标签、伪造摘要头块)原样复读进输出导致会话卡死的问题——全 wire 输出侧新增统一的内部产物剥离层(流式+非流式,与注入侧共享常量保证字节级一致),请求侧载体、工具参数逐字节保真、degenerate-retry 集成均验证无回归,测试有牙、diff 干净,可以合并。 |
Fixes #1634
Problem
Long sessions stall because the model's answer consists of compression metadata instead of a reply to the user. Three artifact families that the proxy itself injects into model-visible payloads get restated verbatim by models under sustained context pressure:
<bili-chain v="1" processor="…" …/>checkpoint carrier (outbound stamping,src/chain-checkpoint.ts)[bili] context summary after compaction:handoff user message (src/codex-compact.ts, responses wire)[Compressed conversation section] — topicforged-summary blocks (src/codex-compact.ts, re-injected into the developer message every turn)plus a fourth observed shape: model prose imitating the summary structure.
The existing output sanitizer (
src/loop/tag-echo-filter.ts) only knew ACP render tags (<acp tokens="…" type="…">mNNNNN</acp>) and[ACP]marker lines, so all of these shapes passed through verbatim into the client transcript → the next turn answered the metadata instead of the user → stall.Fix
New third streaming filter stage
createBiliArtifactFilterplus whole-text support folded intostripAcpTags, composed into every wire's filter stack (openai/anthropic/responses/google adapters, plugin pipes incl. the reasoning-summary arg stream) and guarded at every hadEcho/rewrite site:<bili-chain …/>spans (validated by the frozen parserparseChainCheckpoint; literal or\u003c-escaped form) are stripped anywhere in prose; malformed/truncated spans are left as prose[bili] context summary after compaction:and[Compressed conversation section]blocks are cut to end of text field; mid-line mentions are untouchedCODEX_FORGED_HANDOFF_HEADER,FORGED_SUMMARY_HEADER,TAG_OPEN) are now exported from their defining modules — the stripper matches its injector byte-for-byte by constructionDegenerate-turn retry integration preserved (load-bearing per the issue thread): a fully-forged turn yields zero visible text and fires the existing one-time continuation nudge.
Debug observability (issue fix 5):
[stamp]debug log at the outbound chain-stamping site,[inject]debug logs at the ephemeral-nudge / image-note / forged-summary developer re-injection sites.ACP_DUMP_REQremains the byte-level forensic.Behavior change (flagged)
Model output only, all wires: the four shapes above previously passed through to the client verbatim; they are now stripped. Request-side payloads are unchanged. No config-surface change (N/A — no new fields, env vars, or CLI flags; no config schema or persistence-format touch).
Verification (rebased head on master = v0.1.171)
npm run typecheckclean; full suite 3205 tests / 3189 pass / 0 fail / 16 skip;npm run buildcleantests/bili-artifact-strip.test.ts(15 tests): whole-text well-formedness matrix (full strip / missing-digest keep / attr-reorder strip / truncated keep), escaped form, header blocks (position 0 / after\n/ mid-line mention kept), streaming splits across every boundary class, over-cap release, pure-prose byte-exact passthrough, shared-constants pin, passthrough-pin (request-side stamped carriers survive untouched), degenerate-retry parity (fully-forged turn → zero visible text → retry gate fires)tests/fix-437-idle-timeout.test.tsfails under high machine load (it drivesfetchWithTimeoutdirectly against a raw HTTP server; no code path my diff touches) and passes consistently when idle.一句话:修复了压缩元数据(链检查点标签、伪造摘要头)泄漏进模型输出导致长会话卡死的问题——全部 wire 输出侧新增统一的内部产物剥离层(流式+非流式),注入侧与请求侧载体保持不变。