Repository navigation
[PR] fix: revert self-contradictory inclusive billing stamp to split (#5515) - #5518
Conversation
promptInputTotal trusted an explicit billing_usage.semantic="openai" (inclusive) stamp unconditionally on the anthropic lane (#4128). A DeepSeek-backed relay that reports Anthropic-split numbers (fresh-only input_tokens + separate cache_read_input_tokens) yet stamps semantic="openai" left the cached segment out of the total, so the reported input under-counted and the cache hit rate exploded to 1796-6991% (and the nudge meter showed usage=0% on a ~8%-full context). An inclusive input contains its cache-read subset by definition, so it can never be smaller than it. Trust the declaration only while input >= cached; a stamp claiming inclusive while reporting input < cached is self-refuting and reverts to split (cached + creation added back into the total). A genuine inclusive relay always has input >= cached, so correct traffic is byte-identical. Fixes #5515
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.7838-pr.5518.f75af88This exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr5518.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复。检索 open PR,仅本 PR(#5518)实现该自洽性守卫;#3107 是无关的诊断功能。无并列/竞争修复。 审核对象: 技术结论(结构化八问)
测试有效性:7 例覆盖双向(触发 + 不触发)、带 creation、边界
|
(ai: vllm-qwen/qwen3.8-27b) 收工摘要(完整八问审核见上一条):
|
ranxianglei
left a comment
There was a problem hiding this comment.
(ai: vllm-qwen/qwen3.8-27b)
Review 结论:APPROVE(可合) —— 独立核完全部 diff + 所有消费路径与既有行为,定向测试全绿,无阻塞项。GitHub 禁止自批(作者与审核同为 ranxianglei),故以正式 review 评论形式给出 APPROVE 判定。
独立核验(未照抄 issue 结论)
- 通读全 diff(1 commit,2 文件,+87/−1):
src/util.ts唯一改动是promptInputTotal的信任判定加自洽闸门declaredInclusive === true && !(typeof cached === "number" && input < cached);其余为新测试文件。 - 全分支情形分析:非 anthropic 车道(openai/responses/google)在
protocol === "anthropic"处短路,字节级不变;anthropic 无 stamp 走原路径(#4128「不从数字推断语义」原则保留);stamped 且 input≥cached(合法 inclusive relay——inclusive 必含 cached 子集,input≥cached 恒成立)输出与 #4128 完全一致;仅 stamped 且 input<cached 改变行为——该组合可证明自相矛盾(声明被自己的数字否定),回退 split(cached+creation 加回 total);边界 input==cached 保持信任(不可区分,与 #4128 立场一致)。该闸与 #408 对 OpenAI wire 的 numeric-violation 防御同构——同一逻辑补上「声明自反」这半边。 - 所有消费方共用这一个决策点,单点修复全覆盖:usageTotals(relay ledger / [acp-usage])、plugin 车道 applyUsageSample(sample.declaredInclusive 来自逐帧 SSE stamp,mergeUsageSample shrink-guard 与本闸互不干扰)、billInputUsage(USD 计费:修前这类帧 fresh=max(0,1062−74240)=0、cacheRead 被 min() 截到 1062;修后 fresh=1062 + cacheRead=74240 正确计价)、warnCacheCollapse 诊断(修前假 ratio>1 误置 sawHigh=true,修后更准——诊断改善非回归)。全仓 grep 确认无其他 billing_usage 读取方。
- 本地验证:新测试文件 + #4128 套件 = 22 pass / 0 fail;typecheck 绿。回归测试牙齿核查:把 src/util.ts 换回 base fad9805 → 恰好 3 个 bug 形状用例失败、4 个保持用例仍过 → 测试真能抓到本 bug,不是恒真断言。
- CI(镜像缓存读,不轮询 gh):原 head 66de4b6 29/30 completed success/skipped + 1 windows shard 在跑;顺手小修后新 head 5343832 当前 18/30 完成 0 failed、其余在跑(改动仅为测试文件改名+注释编号,本地 7/7 绿)。
八问
- 是什么问题:anthropic 车道上 relay 打了
billing_usage.semantic="openai"(宣称 input_tokens 为 inclusive 口径),但数字本身是 split 口径(input_tokens=1062 仅 fresh、cache_read_input_tokens=74240 另计)时,#4128 无条件信声明 → 上报 input=1062 → 命中率 74240/1062≈6991%(>100% 不可能值),nudge meter 读 usage=0%(1062/936000)而非 ~8%,USD 计费少计 fresh input。 - 什么原因导致的:#4128 只防了「从数字推断语义」,没防「声明被自己的数字否定」——信任判定缺自洽检查;说谎/陈旧的 stamp(DeepSeek relay split 上报却打 openai 标)恰好落在无防护的信任点上。
- 影响范围与严重程度:携带自相矛盾 inclusive stamp 的 anthropic 车道流量(如 DeepSeek relay):命中率指标 >100%(日志噪音/误报)、nudge growth meter 少计(大上下文下可能推迟折叠)、USD 计费少计。不影响真实缓存行为与数据正确性,不影响合法 relay(input≥cached)。p2(指标漂移+meter 少计,可用有绕行)。
- 怎么修复的:在唯一信任点加一道自洽闸门——inclusive 声明仅在
input >= cached时可信,否则回退 split。单决策点、所有消费方自动继承;不动数字推断(#4128 原则保留)、不动其他车道。 - 是否有回归问题:逐项审过既有行为——非 anthropic 车道字节级不变(protocol 短路);anthropic 无 stamp 不变;stamped input≥cached 与 #4128 输出一致;边界 input==cached 保持信任;mergeUsageSample shrink-guard/SSE plumbing 不变(#4128 的 15 条测试在 head 全绿);warnCacheCollapse sawHigh 由误报转准确=诊断改善。无 fail-fast→吞错、阈值/时序/输出格式漂移等高危模式。有意行为变更披露(旧→新):OLD anthropic 车道 inclusive stamp 无条件信任(自相矛盾 stamp → total=fresh-only → hit>100%、nudge/计费少计);NEW 仅 input≥cached 时信任,否则回退 split。仅自相矛盾帧结果改变,正常流量输出不变。
- 是否破坏缓存:不涉及。只改 usage 统计计算,不触 wire 字节、消息身份、prefix-cache key、重试/增量游标、去重键。
- 是否改了设计方向:没有。在 #4128 决策框架内精化(声明信任+数字防御),补缺失的一半,不是新机制。
- 是否引入新配置:没有。
Gate note
触碰默认决策点(是否信任上游声明)→ §7.4「fallback/default-value change」→ 不走 auto-merge,需人工合并。
已顺手修:回归测试文件名与文件内/注释引用误用内部镜像编号 #5515(GitHub 上指向无关 open issue #5515),改为 GitHub 编号 #5513(tests/issue5513-inclusive-selfcheck.test.ts);同时把 PR 正文 "Closes #5515" 改为 "Closes #5513"(否则合并会误关无关 issue #5515);本地 7/7 绿 + commit 5343832。注:原 commit message 里的 "(#5515)" 保留不改写历史。
结论:可合(APPROVE),待人工 merge。
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) Review lane #5518 审结 APPROVE(可合):独立核完全 diff + 所有消费路径(usageTotals/plugin 车道/USD 计费/诊断共用 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.1003-master.96f6800 # this exact build
npm install -g billion-context@master # always the newest merged build
|
…fcheck revert + ranxianglei#5549 blog post Nine files: src/util.ts, tests/issue5513-inclusive-selfcheck.test.ts, and the blog site (articles.json + four html pages + two posts). Zero overlap with this lane's 16. The lane's non-streaming pipePluginJson egress and the "chat" builtin seed entry are both intact after the merge.
问题
日志里出现
cache hit 6991%、1796%(标题 4000%)这种超过 100% 的缓存命中率。真实缓存是正常的(DeepSeek relay 确实命中了 KV cache),错的是数字:上报的 input 被算小了。根因
promptInputTotal(#4128 起)在 anthropic 车道上无条件信任上游显式声明billing_usage.semantic="openai"(即 input_tokens 是 inclusive、已含 cache_read),于是不把 cached 加回 total。但一个 DeepSeek 后端的 relay 会同时做两件矛盾的事:按 Anthropic 的拆分口径报数(input_tokens=1062 只算 fresh,cache_read_input_tokens=74240 单列),却又盖上
semantic="openai"(声称 inclusive)。inclusive 语义下 input 必然 ≥ cached(包含关系),所以这张自相矛盾的声明让 total=1062(cached 没加回),命中率 = 74240/1062 ≈ 6991%。#4128 只防住了「用数字反推」那一半,漏了对端「声明与数字自相矛盾」这一半。连带影响:同一个偏小的 total 喂给 nudge 计量器,日志显示
usage=0% (1062/936000),而真实计费输入约 75K(~8%),大上下文下可能推迟折叠。修复
在信任该声明处加一个自洽性守卫:inclusive 声明只有在
input >= cached时才可信;一旦input < cached,声明自我否定,回退到 split(把 cached+creation 加回 total)。真正的 inclusive relay 恒有input >= cached,所以正确流量逐字节不变。非 anthropic 车道不受影响(守卫要求 protocol==="anthropic")。行为变更披露(旧→新)
semantic="openai"声明会让 total 只剩 fresh input,命中率可超 100%,nudge 计量偏低。input >= cached时信任;否则回退 split。测试
新增
tests/issue5515-inclusive-selfcheck.test.ts(7 例):自相矛盾声明回退 split、带 creation 也全拆、合法 inclusive 不变、边界 input==cached 不触发、未盖章保持 split、非 anthropic 车道忽略守卫、usageTotals 端到端(DeepSeek 形状不再少算 + 真 inclusive 不变)。定向验证:
tests/issue5515-inclusive-selfcheck.test.ts+tests/issue4128-inclusive-usage.test.ts共 22 pass / 0 fail(含 #4128 全部既有断言,证明无回归);npm run typecheck通过。门禁说明
本改动触碰一个默认决策点(是否信任上游声明),按 §7.4 属「fallback/default-value change」,不走 auto-merge,请人工 review。
Closes #5513