Skip to content

fix: make chain-integrity recognition header-only by default (#1683) - #1690

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-29_chain-egress-stamp-off
Sep 29, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-29_chain-egress-stamp-off

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 29, 2026 •

Copy link
Copy Markdown
Owner

What

The <bili-chain …/> chain-integrity checkpoint carrier was stamped onto every request bili processes as a model-visible message — a trailing user message on OpenAI/Responses, a trailing text part on Anthropic/Google. The terminal model reads the messages array, so it treated the empty carrier as phantom/untrusted user input and burned tokens commenting on it each turn (~40–60 tokens + per-turn derailment). Reported in #1683 (Claude Code → cc-switch → bili → claude-opus-5, v0.1.172).

Follow-up (owner direction after the egress fix): the body-marker heuristics false-positive on real content — CCR/file-introduced text and model-echoed tags can look like a checkpoint — so by default ONLY the x-bili-hop header should drive chain recognition. Both body-marker paths (egress emission + inbound body-content detection) are now opt-in.

Root cause

Not "the tag leaks" — the tag was intended to be there. The carrier contract (#1357/#1395/#1421) was designed to satisfy exactly two constraints — survive transit through middleboxes + pass upstream role-alternation validation — and never considered that the terminal model reads the same messages array. No position is simultaneously transit-surviving, validation-passing, and model-invisible:

And separately, scanning the request BODY for a checkpoint marker is a heuristic over user/model content — it cannot distinguish a real foreign checkpoint from CCR/file text or a model-echoed tag. So the body marker is not a reliable recognition signal either. The fix makes both body-marker behaviors opt-in and keeps the header as the default signal.

Change

Two independent default flips, each behind its own opt-in, decoupled from each other and from the header path:

  • Egress emission → opt-in. src/server.ts: the gate at the stampOutbound call site now requires opts.chainEgressStamp === true instead of opts.chainContentDetection !== false. New config chainEgressStamp (env BILI_CHAIN_STAMP=1), default OFF.
  • Inbound body-content detection → OFF by default. src/config.ts: loadOptions now resolves chainContentDetection to false when unset (was true); env BILI_CHAIN_CONTENT=1 / file "chainContentDetection": true opts back in. This disables both the first-processor-wins body-marker passthrough and the advisory artifact-seed; the x-bili-hop header path is independent of this flag and is unchanged.
  • The server-side gates read opts.chainContentDetection !== false, so production (which always funnels through loadOptions) gets the concrete false; direct-opts unit tests that omit the flag still see undefined → on, which is why the enabled-path regression coverage keeps running.

Config surface

Owner-gated area, flagged per policy. Two rows change:

Key Type Default Env override
chainEgressStamp boolean false (new) BILI_CHAIN_STAMP=1
chainContentDetection boolean false (was true) BILI_CHAIN_CONTENT=1
  • Why the existing surface can't express it: chainContentDetection used one switch for several concerns (inbound body recognition + egress emission). [Bug]Chain-integrity checkpoint tag <bili-chain …/> is injected as a visible trailing user-role message — models read it as phantom user input and burn tokens commenting on it #1683 needs emission independent of recognition, and the owner needs body-recognition off by default while the header stays on. One boolean cannot express all of that.
  • Mechanisms considered: (a) re-shape the carrier into an invisible slot — rejected, none exists (trilemma above); (b) keep one coupled boolean — rejected, can't turn off emission/recognition without losing header interop; (c) dedicated booleans + header as the default signal — chosen.
  • Compat/migration: no persisted-format change. Unset = the new leaner default (header-only). Anyone who wants the previous behavior sets both BILI_CHAIN_STAMP=1 and BILI_CHAIN_CONTENT=1 (or the file equivalents) to restore body-stamp emission + body-marker recognition exactly as before.

Behavior change (old → new)

Tests

  • tests/issue1683-chain-egress-off.test.ts: default opts → processed openai and google outbounds carry NO carrier (kernel did run); chainEgressStamp:true → stamp restored (verdict valid); body-detection on → a valid external checkpoint forwards byte-identical; body-detection off (new default) → a foreign body-carrier is processed, not passed through; x-bili-hop header alone → byte-identical passthrough even with body-detection off.
  • tests/issue1086-chain-self-exemption.test.ts T9/T9b: T9 pins chainContentDetection default-OFF precedence (unset→false, =1→true, =0→false, file true→true, env wins over file); T9b pins chainEgressStamp the same way.
  • tests/chain-checkpoint-step2.test.ts T3 and tests/chain-checkpoint-step3.test.ts E3/E4/E6 pass { chainEgressStamp: true } to keep asserting the (now opt-in) re-stamp path; step2 rig gained overrides plumbing.
  • tests/issue1542-chain-embedded-strip.test.ts split into default (polluted body → zero carriers reach the LLM) and opt-in (stripped + one fresh trailing carrier), both modes.

Pre-flight (local): npm run typecheck clean · npm test 3249 tests / 3237 pass / 0 fail / 12 gated skips · npm run build OK.

Closes #1683

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-29_chain-egress-stamp-off (e5c94fc)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1645-pr.1690.a5775a0

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,开始按 PR Review 规范审查 #1690(原 issue #1683 的方向关已在 triage 中通过、owner 已拍板「先实现一个看看」)。正在:拉分支核对代码 → 回归清点(入站识别 / hop 透传 / #1542 剥离 / #1634 回声过滤)→ 测试有效性 → diff 整洁度 → CI 状态。结论稍后回帖。

ranxianglei pushed a commit that referenced this pull request Sep 29, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — mergeable (human merge; not auto-merge eligible: default flip + new config field + hot file src/server.ts).

① 原 issue(方向关)— pass

#1683 floor-2 triage already verified the mechanism (stampOutbound gated only by chainContentDetection, carrier rendered as model-visible trailing user message) and established the trilemma (no position survives transit + validation + invisibility). Owner拍板 off-by-default("先实现一个看看"/"可以暂时关闭"). The fix acts at the decision point — the single stampOutbound chokepoint — not by filtering payload in transit. Direction matches the approved design exactly.

② 回归审查(逐项既有行为)

Existing behavior Verdict
Egress stamping, all 4 wires Intentional change, disclosed in PR body (old: every processed request stamped → new: unstamped unless BILI_CHAIN_STAMP=1). Tradeoff stated: multi-bili relay behind a hop-stripping middlebox loses the digest guard until opt-in. Verified no-carrier-by-default on openai+google (suite) AND anthropic+responses (live probe against mock upstream; responses shows compress-tool injection, anthropic 70B→17.2KiB growth proves kernel pass).
Inbound detection / enforcement / hop-marker stripping Unchanged, still chainContentDetection !== false at src/server.ts:1428/:1470; T3/E3/E4/E6 still pin byte-identical forwarding of valid checkpoints.
#1086 self-exemption precedence T9b rewritten to pin env/file precedence under new default-OFF.
#1542 strip-on-forward Split into default (zero carriers) + opt-in (exactly one fresh trailing carrier) pairs — both modes pinned.
Config surface chainEgressStamp / BILI_CHAIN_STAMP added to both CONFIGURATION.md and .zh-CN.md; resolution mirrors the stableSystemAnchor/forkAdoption pattern (env wins, file must be explicit true). Web UI doesn't enumerate these keys → no missing-key regression.
Persistence format / session binding / update.ts / kernel pin / version / lockfile Untouched.

③ 代码

One call site for stampOutbound (src/server.ts:5208) → flipping the gate at :5206 makes the change wire-agnostic by construction. Resolution at src/config.ts:959 is the established idiom. No as any/@ts-ignore/console.error. Comment cites #1683 accurately.

④ 测试有效性

Real teeth: default-mode tests assert BOTH "processed" (compress tool present) AND "no <bili-chain in outbound" — they catch either stamping regression or processing breakage. Opt-in tests assert exactly one fresh trailing carrier whose digest self-verifies (verdict === "valid"). 4-wire coverage complete. Full suite local: 3247 tests / 3235 pass / 0 fail / 12 skipped; GitHub CI on head: 13/13 green incl. real-client e2e lanes + hermetic registry.

⑤ diff 整洁度

9 files, every line on-purpose, single linear commit, no whitespace churn, no version bump. ✅

Fixed in passing (direct commit on the branch): tests/chain-checkpoint-step3.test.ts:377 E5 title claimed the kill switch "disables both enforcement and outbound stamping" — stale under the new semantics (that assertion passes because egress is default-OFF, not because the kill switch controls it). Renamed; assertions untouched. Pushed as c4e897ed, affected files re-run 19/19 green; CI re-runs on the new head.

One semantic to confirm (flagging, not blocking): the switches are now fully independent — BILI_CHAIN_CONTENT=0 + BILI_CHAIN_STAMP=1 will still stamp outbound (old behavior: kill switch also stopped stamping). That matches the PR docs ("Independent of BILI_CHAIN_CONTENT"), but if the kill switch should stay total, the gate becomes chainEgressStamp === true && chainContentDetection !== false — a one-liner if you want it.

一句话中文摘要:审了让 chain-integrity 出站盖章改为默认关闭的 PR——方向与 owner 拍板一致、入站检测/强制行为未动且四条 wire 均验证默认无 carrier、测试断言有牙可兜底、diff 干净、CI 13/13 全绿,顺手修了一个过时的测试名(E5 标题),可以合并。

The <bili-chain .../> checkpoint carrier sits in a model-visible slot (trailing
user message on openai/responses, trailing text part on anthropic/google), so
models read it as phantom input and burn tokens commenting on it (#1683). And
scanning the request body for a checkpoint marker false-positives on
CCR/file-introduced text and model-echoed tags. So by default ONLY the
x-bili-hop header drives chain recognition; both body-marker behaviors are
opt-in and decoupled from each other and from the header path:

- Egress emission: new `chainEgressStamp` / BILI_CHAIN_STAMP, default OFF. The
  stampOutbound call-site gate now requires it === true.
- Inbound body-content detection: `chainContentDetection` / BILI_CHAIN_CONTENT
  default flipped ON -> OFF in loadOptions (first-processor-wins body-marker
  passthrough + advisory artifact-seed). x-bili-hop passthrough unaffected.

Old -> new: processed requests were always stamped + recognized foreign
body-markers; now neither unless opted in. Header-based chains unchanged.

Closes #1683
@ranxianglei
ranxianglei force-pushed the 2026-09-29_chain-egress-stamp-off branch from c4e897e to e5c94fc Compare September 29, 2026 13:41
@ranxianglei ranxianglei changed the title fix: make chain-integrity egress stamp opt-in (default off) fix: make chain-integrity recognition header-only by default (#1683) Sep 29, 2026
@ranxianglei
ranxianglei merged commit 1ffae95 into master Sep 29, 2026
13 checks passed
@ranxianglei
ranxianglei deleted the 2026-09-29_chain-egress-stamp-off 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

1 participant