Skip to content

fix: anthropic proxy-mode round-2 preserves client cache_control (#2499) - #2515

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-09_anthropic-round2-client-cache-control
Oct 9, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-10-09_anthropic-round2-client-cache-control

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2499

What & why

In proxy mode on the anthropic wire, when the client manages its own caching (it stamps cache_control itself — Claude Code always does), the compress-loop round-2 rebuild dropped every message-level breakpoint. buildRequest passed only bili's own marks (prepared.anthropicCacheMarks) to coreToAnthropic; for a client-managed session that value is undefined, so the rebuilt messages carried zero cache_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: cached pinned 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 cacheControls through Prepared → pickAdapter → createAnthropicAdapter, and apply in buildRequest:

const messages = coreToAnthropic(coreMessages, cacheMarks ?? clientCacheControls);

This is the exact expression the steady path already uses (anthropicCacheMarks ?? cacheControls). Consequences:

  • bili-managed sessions (cacheMarks defined) — unchanged.
  • neither-cases — unchanged.
  • client-managed (the bug) — now re-stamps its own trigger-turn breakpoint; ttl preserved 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 (Prepared field + call site), src/loop/index.ts (pickAdapter param), src/loop/adapter-anthropic.ts (apply).

Behavior change (flagged)

Proxy-mode anthropic round-2 requests now carry the client's own message-level cache_control breakpoints (previously: none when the client manages caching). Affects only proxy-mode anthropic sessions whose client sends its own cache_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 emitting compress tool_use → text):

  • client-managed: asserts the round-2 body has exactly ONE message-level cache_control, on the trigger turn, value deep-equal to the client's {type:"ephemeral",ttl:"1h"}, and zero system marks added by bili.
  • control (bili-managed, no client marks): round-2 still carries bili's own system + message breakpoints.

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.

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
awork added 2 commits October 9, 2026 19:13
…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.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2515 review 完成 + 冲突已解除。PR 原处于 merge conflict(master 的 #1440 P2 cut 3/4 把 forward()/armFailureShrink/resolveFakeCompletion 从 src/server.ts 搬进了 src/server/relay.ts,与 PR 在旧位置的改动相撞)。已解冲突并推送(merge commit,无 force-push),现在 mergeable: true,CI 已开跑(28 个 check 排队中),mergeable_state: blocked 仅等 CI。按八问汇报:

1. 是什么问题:anthropic 线、proxy 模式下,客户端自管缓存(自己打 cache_control,Claude Code 每条都带)时,compress 循环 round-2 重建把客户端的 message 级断点全部丢掉——折叠后前缀从未写入 prefix cache,下一条请求只能命中 system/tools 前缀(#2499 日志里 cached 钉死在 62100)。

2. 什么原因导致的:round-2 的 buildRequest 只把 bili 自己的 cacheMarks 传给 coreToAnthropic;客户端自管会话该值为 undefined,重建出的 messages 零标记。而稳定路径用的是 anthropicCacheMarks ?? cacheControls(回退到从客户端消息收割的标记)——round-2 恰好缺这个回退。与 #1605/#1597 同族(同一段历史被渲染两次结果不一致),之前差在字节形状,这次差在缓存标记。

3. 影响范围与严重程度:触发条件 = anthropic 线 + proxy 模式 + 客户端自带 cache_control;plugin 模式不受影响(折叠后的下一条由客户端自己发)。纯成本问题,不损坏数据、不影响功能正确性:每次折叠缓存重建成本约高 45%(1h TTL 下 3×→~2.1×)。

4. 怎么修复的:把客户端收割的 cacheControls 沿 Prepared.anthropicClientCacheControls → pickAdapter → createAnthropicAdapter 透传,round-2 应用 cacheMarks ?? clientCacheControls —— 与稳定路径完全相同的表达式。只动 host,不动 kernel。核对了 kernel 侧机制(kernel/src/wire/anthropic.ts):收割与应用本来就是 Map<string, unknown>,ttl 逐字节保留;system 处理不变(客户端 system 标记留在 originalSystem,round-2 断点集合是 round-1 的子集,总数不超 Anthropic 4 断点上限)。

5. 是否有回归问题:逐项核过既有行为——bili 自管(cacheMarks 有值)走 ?? 左支不变;完全无标记场景回退到空 map,行为中性不变;唯一变化是「客户端自管」支从 0 断点变还原客户端自己的断点(即修复目标,已在 PR 描述披露)。另核了回归测试有效性:临时撤掉 adapter 那一行重跑,client-managed 用例确实失败(对照组仍过),恢复后 2/2 过——测试有牙,不是恒真。

6. 是否破坏缓存:不破坏,这正是修复目标。不新增断点、不挪动既有断点位置,round-2 集合 ⊆ round-1 集合。

7. 是否改了设计方向:没有。沿用 #1637「client-managed caching stays untouched」原则,只是让 round-2 也遵守它。

8. 是否引入新配置:没有。无新 env/config/默认值。

本次 review 动作(均已推送,head a2b8310b7):

  1. 代码+测试审查通过(见上);
  2. 解冲突:删除 src/server.ts 里被 master 搬走的陈旧函数副本(逐函数 diff 验证与 relay.ts 版本除 master 自身后续改进外逐字一致,未丢任何内容),把本 PR 在该区域唯一的实质改动(prepared.anthropicClientCacheControls 实参)移植到新调用点 src/server/relay.ts:1288;
  3. 顺手小修(独立标注 commit):新测试里一处陈旧注释——写「三个单块回合 m00001..m00003、触发轮 C(m00003)」,实际 buildTurns 发七个回合、触发轮是 m00007;纯注释,无行为变化;
  4. 本地定向预检(rebase 后 head):tsc --noEmit 干净;issue2499(2/2)+ anthropic-cache-control + anthropic-round2-stream + e2e-anthropic(5/5)共 18/18 过;npm run build 成功。全量套件留给 CI。

行为变更声明(PR 已披露,复核确认):proxy 模式 anthropic round-2 请求从 0 个 message 级断点 → 还原客户端自己的断点。仅影响自带 cache_control 的 proxy 模式 anthropic 会话。

判定:方向正确(打的是根因——缺回退那一行,不是症状层拦截)、diff 干净、两模式推理完整、非自动合并档(wire/message-shape 变更,§7.4 gate 8)。待 CI 全绿后可由你合并:#2515

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-09_anthropic-round2-client-cache-control (a2b8310)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2713-pr.2515.6011e12

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-pr2515.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] 🏷 CI 全绿,PR 已就绪可合并。

  • head a2b8310b7(解冲突 merge commit + 注释小修):28/28 check 全部 success,零失败零跳过;
  • mergeable: true,mergeable_state: clean。

我这边 review + 解除冲突的工作到此收工:方向、回归面、两模式推理、测试有效性均已核过(详见上一条八问回复)。合并动作按规则留给你:#2515

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到冲突告警,已核实:当前无冲突,未做任何改动。

刚拉取 master 并查了 API,现状与告警不符:

  • PR fix: anthropic proxy-mode round-2 preserves client cache_control (#2499) #2515:mergeable: true,mergeable_state: **clean**,head a2b8310b7(我上次推送的解冲突版本),base 7268bba1f;
  • origin/master 仍停在 7268bba1f——自上次同步后 master 没有任何新提交(7268bba1f..origin/master 为空),不存在新的冲突源;
  • CI 复核:head 上 28/28 check 全部 success;本地工作区与推送的 head 一致、干净。

判断:这条 dirty 采样大概率是 GitHub 在推送后异步重算 mergeability 的窗口期被平台采到了中间态(重算期间 mergeable 会短暂为 unknown/dirty)。按「冲突解完状态会自动转干净,不重复 rebase」的纪律,没有再制造无意义的 merge/rebase 提交。

结论:无需处理,PR 保持可合并状态,等你合并:#2515 。若之后 master 真有新提交再次触发冲突,我会按同样流程(sync → 解冲突 → 定向验证 → push)再报一次。

@ranxianglei
ranxianglei merged commit 4889c89 into master Oct 9, 2026
28 checks passed
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.179-master.4889c89 (rolling master tag).

npm install -g billion-context@0.0.179-master.4889c89  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] anthropic 线 proxy 模式:压缩回合的 round-2 重建丢失客户端 cache_control,折叠后下一条请求只能命中 system 前缀

1 participant