Repository navigation
fix: count Anthropic cache tokens in plugin + loop usage accounting (#790) - #791
Conversation
59fbba6 to
b1061ec
Compare
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-791Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr791.tgz
npm install -g packageThis comment is automatically updated on each push. |
[bot] 🏷 PR review 完成,已直接在分支上修复冲突并重新验证。 查重:无重复。相关历史项(#779/#783 DeepSeek diff 整洁度:干净。5 个文件( 发现并已修复的问题(直接补了 commit):PR 基于 bfb0b68 开出后 master 前进了(#748/#775/#784 等合入),GitHub 报 逻辑审核要点:
验证(rebase 后在 b1061ec 上重跑):typecheck ✓ · build ✓ · 全量测试 1585 pass / 0 fail / 2 skipped(E2E-gated)✓(连跑两次均干净;首次运行出现过 1 例未复现的失败,疑似负载下的计时敏感用例,后续两轮稳定)。新增 8 组回归测试覆盖了 issue 最小复现(zero start + full delta → 56040/53696)、重复快照不累加、零回显不覆盖、全命中轮次、plugin/loop 双路径及 openai 语义不变量,质量合格。 遗留: 中文摘要:修复了 Anthropic 缓存写入/读取 token 在 plugin 与 loop 两条记账路径漏计导致的上下文大小塌缩(#790);review 中顺手把 PR rebase 到最新 master 解决了与 #779 的一处冲突(两边改动都保留),重跑 typecheck/全量测试/build 全部通过,可以合并(建议合并前补跑一次 e2e)。 |
Fixes #790
Problem
Anthropic reports its input context as three segments: fresh (
input_tokens) + cache-read (cache_read_input_tokens) + cache-write (cache_creation_input_tokens). The context size is the sum of all three; the cache hit is the read segment only.The plugin passthrough and the compress-loop paths never accounted for two of those segments:
usageFromSseEvent(plugin SSE):message_startparsed onlyinput_tokens+cache_read_input_tokens;message_deltaignored both cache fields entirely — so an upstream that delivers full usage inmessage_delta(after an all-zeromessage_start) was recorded asinput=344 cached=0instead ofinput=56040 cached=53696, collapsinglastInputTokensand breaking compression triggering.promptInputTotal) had no notion of the write segment at all, so even when a field was parsed it never reached the reported context size.src/loop/adapter-anthropic.tsnever parsedcache_creation_input_tokensandrecordUsagecomputed the total without it. Fixed in this PR because it is the identical defect on the other compression mode (AGENTS.md §6: wire changes must be reasoned about in both modes).cache_creation_input_tokensto the sample. (Proxy-mode non-streaming JSON already summed all three viausageTotals— no change needed there.)Changes
src/util.ts—promptInputTotal(protocol, input, cached, creation?): under split semantics (Anthropic / OpenAI 代理压缩不上报改写量,宿主上下文会计塌缩至 ~0(omps recordAnchoredHistoryRewrite 零调用);且持久化 lastInputTokens 恢复无钳制,旧版负值升级后复活 #408 violation) the write segment is additive; under total-includes-cached wires (openai/responses) it is deliberately NOT added back.src/plugin.ts—UsageSamplegainscreationTokens;usageFromSseEventparses all three fields in both anthropic branches (message_start: record whatever is present — zero is a real value for a fully cache-hit turn;message_delta: adopt only> 0, extending the existing relay zero-echo guard so a zeroed echo cannot clobber start values);mergeUsageSamplecarries the field (per-field overwrite ⇒ omitted keeps prior, repeated snapshots don't accumulate);applyUsageSamplefolds it into the total;pipePluginJsonparses the field. Cache writes count toward context size, never toward hits (cachedTokensstays = read only).src/loop/core.ts— usage event/recordUsagecarrycreationTokensintopromptInputTotal.src/loop/adapter-anthropic.ts— parse the field onmessage_start; onmessage_deltaadopt it in the complete-authoritative branch (#299atomic adoption, keep-prior-if-absent) or> 0in the incomplete branch.tests/host-usage-postfold.test.ts— 8 regression tests: the issue's exact minimal repro (zero start + full delta → 56040/53696), real-shape stream, repeated-snapshot non-accumulation, zero-echo non-clobbering, fully cache-hit turn (startinput_tokens: 0), plugin JSON (anthropic + openai total-semantics unchanged), and both loop-path variants.Verification
[acp-usage] input=344 cached=0 (cache hit 0%)→[acp-usage] input=56040 cached=53696 (cache hit 96%)— matches the expected stats from the issue.npm run typecheck✓ ·npm test✓ (1520 pass / 0 fail, E2E-gated ones skipped) ·npm run build✓tests/e2e/e2e-codex.test.tsnot run here: the sandbox network perimeter forbids probing the local upstream this suite targets. The change touches no forwarded bytes (unit tests assert verbatim forwarding), only internal accounting.