Repository navigation
fix: stop leaking prompt_cache_key into anthropic upstreams (#1403) - #1404
Conversation
The omp extension stamped a raw session UUID as top-level prompt_cache_key on every chat-shaped payload regardless of where it was headed. Strict-schema upstreams behind a blind TLS tunnel (e.g. opencode zen's /v1/messages) reject unknown fields with 400 "Extra inputs are not permitted" — 100% failure for every anthropic-protocol model through `bili omp`. Producer fix (src/agent): stampPromptCacheKey now stamps only when the destination will actually reach the proxy's processing pipeline (proxyBaseFromUrl / BILLION_CONTEXT_PROXY origin / BILI_MITM_HOSTS membership). The pck stamp is load-bearing identity (#268) but is useless — and harmful — for hosts the proxy cannot see. Defense in depth (src/server): scrubAnthropicPck strips the field from anthropic bodies on every verbatim forward path (side request #388, chain verdict #1086, passthrough marks #1117/#920, non-conversation relay #1284, final fallback covering --passthrough and route passthrough #661). Bodies without the field stay byte-identical; openai/responses wires are untouched (dsh native pck forwarding intact). Launcher exports BILI_MITM_HOSTS to pi/omp children so the extension sees exactly the proxy's MITM whitelist. Wire-contract ledger: WC-007 records that top-level prompt_cache_key is not part of the Anthropic Messages API (provenance: #1403). Behavior changes (old -> new): - omp pck stamping: unconditional -> destination-gated. Blind-tunneled destinations lose pck identity (anonymous prefix-affinity sessions, #309) instead of corrupting the upstream request with a foreign field. - Verbatim anthropic forwards: client bytes verbatim including any top-level prompt_cache_key -> field stripped (re-serialization only when present). The #661 byte-for-byte guarantee now applies to pck-free bodies (the normal case). - New additive env var BILI_MITM_HOSTS exported to pi/omp children. Tests: new tests/anthropic-pck-verbatim.test.ts (strict-upstream A/B for all four leak paths), route-passthrough assertions updated to semantic strip checks plus a byte-exact no-pck test, plugin-agent matrix extended with destination-gate cases, WC-007 self-check probe. Full suite green (2800 tests / 0 fail), typecheck clean, build OK, e2e zero-token preflight pass. Closes #1403
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1404Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1404.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 PR #1404 review — 可合并(人工合并) Reviewed head ① Original issue direction — correct, and better than the options proposed in #1403. ② Regression review — pre-existing behaviors on touched paths:
③ Code review.
④ Test validity — real teeth.
⑤ Diff cleanliness — every line serves the fix; no unrelated churn, no whitespace noise, version untouched, single linear commit. Clean. Behavior-change disclosure (old→new + why):
Hot-file contention note (§7.2): six open PRs also touch Auto-merge gate: NOT eligible — outbound wire/message-shape change on anthropic forwards + product trade-off (identity vs leak). Must stay human-merged, as expected. 结论:修复了 omp 插件无条件向 anthropic 请求体注入 |
|
Deep review: ✅ merge-ready. Verified on the branch head locally — typecheck clean, full suite 2800 tests / 0 fail, the new suite's five branch tests (80/80 relevant) all green. Producer gate ( Proxy scrub ( Launcher mirror — exactly the proxy's whitelist. Ledger: WC-007 registered with provenance — follows the ledger-only-grows rule. Non-blocking observations:
|
[bot] 🏷 收到,与你的结论完全一致——两条非阻塞观察我都同意,且已在上一轮 review 里各自留了痕(残差边界记为「launch-time snapshot 的固有边界、fail-safe 方向」;建议 #3 在 producer gate 下确实失去存在前提,不实现是对的)。 补一点你这条没覆盖的:合并顺序。当前还有 9 个 open PR 与 #1404 共享热文件—— 按仓库规则我不执行合并——PR 链接:#1404 ,请人工合并。 中文摘要:双方独立复核一致(生产端门控 fail-closed、服务端六条 verbatim 路径兜底剥离、whitelist 镜像逐层对齐、WC-007 入册),无阻塞项,仅剩合并顺序注意,可以合并。 |
What changed
bili ompstamped a raw session UUID as top-levelprompt_cache_keyon every chat-shaped payload (stampPromptCacheKey, src/agent/pi.ts), regardless of where the request was headed. Strict-schema upstreams behind a blind TLS tunnel (opencode zen'shttps://…/zen/v1/messages, anthropic protocol) reject unknown fields:→ 100% failure for every anthropic-protocol model through
bili omp. Reproduced locally on v0.1.154 master before fixing (strict fake upstream + drive harness in the issue thread).Two-layer fix, per the root-cause principle (fix the producer, plus defense-in-depth at the decision point):
Producer (src/agent) —
stampPromptCacheKeynow stamps only when the destination will actually reach the proxy's processing pipeline:proxyBaseFromUrl(baseUrl)defined (the/bili/lane), OR origin ===BILLION_CONTEXT_PROXY, OR hostname ∈BILI_MITM_HOSTS(exact/subdomain). New helpersdestinationRoutedThroughProxy/mitmHostsFromEnvin src/agent/shared.ts. Rationale: the pck stamp is load-bearing identity for the proxy (#268) but is useless for hosts the proxy cannot see — and harmful, because it corrupts the upstream request. This deliberately does NOT align withpromptCache.routing: that setting governs the proxy's own openai-wire sticky-routing injection; pck is an identity signal on both wires, and routing it out would degrade every anthropic upstream (including api.anthropic.com) to anonymous affinity sessions.Defense in depth (src/server) — new
scrubAnthropicPck()strips top-levelprompt_cache_keyfrom anthropic bodies on every verbatim forward path: side request #388, chain verdict #1086, passthrough mark #1117, plugin-bypass header #920, non-conversation relay #1284, and the final fallback (which covers--passthroughglobal mode and route passthrough #661). On the anthropic wire a top-levelprompt_cache_keyis never legitimate API content (the processed path already strips it inprepareAnthropic), so stripping is always safe. Bodies without the field pass byte-identical (parse → delete → re-serialize only when the own-prop exists); openai/responses wires untouched (dsh native pck forwarding preserved — tests/openai-pck-identity.test.ts stays green).Launcher — exports new additive env var
BILI_MITM_HOSTSto pi/omp children (= DEFAULT_MITM_DOMAINS ∪ config-file ∪ BILI_MITM_DOMAINS ∪ extra domains ∪ auto-discovered hosts), mirroring exactly what the proxy'sisMitmHost()sees.Wire-contract ledger (#1304) — WC-007 added: top-level
prompt_cache_keyis not part of the Anthropic Messages API (provenance: #1403 production 400, opencode zen, 2026-09-26), with validator clause + self-check probe.Behavior changes (old → new)
prompt_cache_keyBILI_MITM_HOSTSEvidence (A/B, same harness, same strict upstream)
pck occurrences in forwarded bodies: 0across all cases after the fix.Pre-flight
npm run typecheckcleannpm test: 2800 tests, 0 fail, 12 skippednpm run build: success, dist-import-annotations OKE2E_CHECK=1) pass; full E2E runs in CI (diff touches request-pipeline hot files)New regression suite:
tests/anthropic-pck-verbatim.test.ts(strict-upstream A/B for all four leak paths);tests/route-passthrough.test.tsassertions updated to semantic strip checks + byte-exact no-pck test; plugin-agent matrix extended with destination-gate cases (external host → not stamped;BILI_MITM_HOSTS/BILLION_CONTEXT_PROXYmatch → stamped).Closes #1403