Repository navigation
fix: anthropic proxy-mode round-2 preserves client cache_control (#2499) - #2515
Conversation
In proxy mode the compress-loop round-2 rebuild passed only bili's own cache marks (prepared.anthropicCacheMarks) to coreToAnthropic. For a client-managed session that value is undefined (the client stamps its own cache_control), so the rebuilt messages carried ZERO cache_control breakpoints — the post-fold prefix was never written to the prefix cache and the next request only hit the system/tools prefix. Thread the client's harvested cacheControls through Prepared -> pickAdapter -> createAnthropicAdapter and apply `cacheMarks ?? clientCacheControls` in buildRequest — the exact expression the steady path already uses, so bili-managed and neither-cases are unchanged and only the client-managed case now re-stamps its own trigger-turn breakpoint. System handling is untouched (client's own system marks stay in originalSystem). Closes #2499
…ontrol Conflict resolution for #2515: master moved forward()/armFailureShrink/ resolveFakeCompletion out of src/server.ts into src/server/relay.ts (#1440 P2 cuts 3/4). Dropped the stale server.ts copies (verified byte-equivalent to relay.ts apart from master-side improvements) and ported this PR's only functional hunk in that zone — the prepared.anthropicClientCacheControls argument — to the pickAdapter call site now living in src/server/relay.ts.
…review nit) The harness comment claimed three single-block turns (m00001..m00003) with the marked trigger at m00003; buildTurns actually sends seven turns (m00001..m00007) and the trigger is m00007. Comment-only, no behavior.
[bot] 🏷 PR #2515 review 完成 + 冲突已解除。PR 原处于 merge conflict(master 的 #1440 P2 cut 3/4 把 1. 是什么问题:anthropic 线、proxy 模式下,客户端自管缓存(自己打 2. 什么原因导致的:round-2 的 3. 影响范围与严重程度:触发条件 = anthropic 线 + proxy 模式 + 客户端自带 4. 怎么修复的:把客户端收割的 5. 是否有回归问题:逐项核过既有行为——bili 自管( 6. 是否破坏缓存:不破坏,这正是修复目标。不新增断点、不挪动既有断点位置,round-2 集合 ⊆ round-1 集合。 7. 是否改了设计方向:没有。沿用 #1637「client-managed caching stays untouched」原则,只是让 round-2 也遵守它。 8. 是否引入新配置:没有。无新 env/config/默认值。 本次 review 动作(均已推送,head
行为变更声明(PR 已披露,复核确认):proxy 模式 anthropic round-2 请求从 0 个 message 级断点 → 还原客户端自己的断点。仅影响自带 判定:方向正确(打的是根因——缺回退那一行,不是症状层拦截)、diff 干净、两模式推理完整、非自动合并档(wire/message-shape 变更,§7.4 gate 8)。待 CI 全绿后可由你合并:#2515 |
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2713-pr.2515.6011e12This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2515.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 收到冲突告警,已核实:当前无冲突,未做任何改动。 刚拉取 master 并查了 API,现状与告警不符:
判断:这条 dirty 采样大概率是 GitHub 在推送后异步重算 mergeability 的窗口期被平台采到了中间态(重算期间 mergeable 会短暂为 unknown/dirty)。按「冲突解完状态会自动转干净,不重复 rebase」的纪律,没有再制造无意义的 merge/rebase 提交。 结论:无需处理,PR 保持可合并状态,等你合并:#2515 。若之后 master 真有新提交再次触发冲突,我会按同样流程(sync → 解冲突 → 定向验证 → push)再报一次。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.179-master.4889c89 # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2499
What & why
In proxy mode on the anthropic wire, when the client manages its own caching (it stamps
cache_controlitself — Claude Code always does), the compress-loop round-2 rebuild dropped every message-level breakpoint.buildRequestpassed only bili's own marks (prepared.anthropicCacheMarks) tocoreToAnthropic; for a client-managed session that value isundefined, so the rebuilt messages carried zerocache_control. The post-fold prefix was therefore never written to the prefix cache, and the next request only hit the system/tools prefix (issue log:cachedpinned at 62100 = system+tools size across all 5 observed folds).This is the same family as #1605/#1597 (the same history rendered twice — round 2 vs steady path — disagreeing); previously the difference was byte shape, here it is the cache marks.
Fix (host-only, no kernel change)
Thread the client's harvested
cacheControlsthroughPrepared→pickAdapter→createAnthropicAdapter, and apply inbuildRequest:This is the exact expression the steady path already uses (
anthropicCacheMarks ?? cacheControls). Consequences:cacheMarksdefined) — unchanged.ttlpreserved byte-for-byte (Map<string, unknown>accepted by the kernel applier).System handling is untouched: a client-managed session keeps its own system marks in
originalSystem; bili adds none, so the total never exceeds Anthropic's 4-breakpoint budget (the round-2 mark set is a subset of round-1's).Files:
src/server/prepare-anthropic.ts(harvest + return),src/server.ts(Preparedfield + call site),src/loop/index.ts(pickAdapterparam),src/loop/adapter-anthropic.ts(apply).Behavior change (flagged)
Proxy-mode anthropic round-2 requests now carry the client's own message-level
cache_controlbreakpoints (previously: none when the client manages caching). Affects only proxy-mode anthropic sessions whose client sends its owncache_control. Plugin mode unaffected (clients send the post-fold request themselves). No new config/env field. Cost impact per issue:45% less cache-rebuild cost per fold (3×→2.1× at 1h TTL).Review tier
Wire/message-shape change on the request path → not auto-merge eligible (§7.4 gate 8); human review required. Reasoned in both compression modes (§6): plugin mode never reaches this path; proxy mode is where the fix lands.
Tests
New regression
tests/issue2499-anthropic-round2-cache.test.ts(proxy rig, scripted upstream emittingcompresstool_use → text):cache_control, on the trigger turn, value deep-equal to the client's{type:"ephemeral",ttl:"1h"}, and zero system marks added by bili.Fails without the fix; passes with it.
Pre-flight (local, directed — full suite left to CI)
node --import tsx --test tests/issue2499-anthropic-round2-cache.test.ts tests/anthropic-cache-control.test.ts tests/anthropic-round2-stream.test.ts tests/e2e-anthropic.test.ts→ 18/18 pass.npx tsc --noEmit --project tsconfig.json→ clean.