Skip to content

[PR] fix: revert self-contradictory inclusive billing stamp to split (#5515) - #5518

Merged
ranxianglei merged 2 commits into
masterfrom
2026-11-02_fix-inclusive-selfcheck
Nov 2, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-11-02_fix-inclusive-selfcheck

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

问题

日志里出现 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")。

行为变更披露(旧→新)

  • 旧:anthropic 车道上 inclusive 声明被无条件信任——一张撒谎/过期的 semantic="openai" 声明会让 total 只剩 fresh input,命中率可超 100%,nudge 计量偏低。
  • 新:仅当 input >= cached 时信任;否则回退 split。
  • 影响面:只有「声明 inclusive 却报 input<cached」这种自相矛盾的帧会改变结果(这类帧本来就必错);所有正常流量(真 Anthropic 拆分 / 真 inclusive relay)输出不变。

测试

新增 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

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
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-02_fix-inclusive-selfcheck (5343832)

Latest build — install this exact version

npm install -g billion-context@0.0.7838-pr.5518.f75af88

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
5343832 billion-context@0.0.7838-pr.5518.f75af88 build
66de4b6 billion-context@0.0.7832-pr.5518.e544c36 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr5518.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix: revert self-contradictory inclusive billing stamp to split (#5515) [PR] fix: revert self-contradictory inclusive billing stamp to split (#5515) Nov 2, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重:不重复。检索 open PR,仅本 PR(#5518)实现该自洽性守卫;#3107 是无关的诊断功能。无并列/竞争修复。

审核对象:src/util.ts 的 promptInputTotal(anthropic 车道 inclusive 声明的信任判定)+ 新增 tests/issue5515-inclusive-selfcheck.test.ts。head 66de4b6 基于当前 master(领先 1 commit,基线新鲜,无 stale-base)。

技术结论(结构化八问)

  1. 是什么问题:DeepSeek 后端的 anthropic-wire relay 同时做两件矛盾的事——按 Anthropic 拆分口径报数(input_tokens=1062 只算 fresh、cache_read_input_tokens=74240 单列),却又盖上 semantic="openai"(声称 inclusive)。promptInputTotal 无条件信任这张声明、不把 cached 加回 total → total=1062,命中率 = 74240/1062 ≈ 6991%;同一个偏小 total 还喂给 nudge 计量器,大上下文下偏低、可能推迟折叠。
  2. 什么原因:[Bug] anthropic lane: cache-hit % halved when the upstream relays an OpenAI-semantics backend (billing_usage.semantic=openai, input_tokens inclusive) #4128 只防住「用数字反推 inclusive」那一半,漏了「声明与数字自相矛盾」这一半。inclusive 定义上 input ⊇ cached,必然 input >= cached;一旦 input < cached,声明自我否定。
  3. 影响范围与严重程度:只有「声明 inclusive 却 input<cached」这种自相矛盾帧会变结果(这类帧本来就必错);真 Anthropic 拆分 / 真 inclusive relay 逐字节不变。属数值正确性缺陷(p2):命中率/计费台账/nudge 计量算错,非崩溃、非数据丢失、非缓存泄露(真实 KV cache 正常)。
  4. 怎么修的:在信任声明处补一个自洽前提——
    anthropicInclusive = protocol==="anthropic" && declaredInclusive===true && !(typeof cached==="number" && input<cached)
    不满足即回退 split(cached+creation 加回)。真正的 inclusive relay 恒有 input>=cached,正确流量不受影响。
  5. 是否有回归:逐项对照新表达式——[Bug] anthropic lane: cache-hit % halved when the upstream relays an OpenAI-semantics backend (billing_usage.semantic=openai, input_tokens inclusive) #4128 全部断言(input>=cached 的 inclusive 不变、未盖章保持 split、semantic="anthropic" 显式 split)、代理压缩不上报改写量,宿主上下文会计塌缩至 ~0(omps recordAnchoredHistoryRewrite 零调用);且持久化 lastInputTokens 恢复无钳制,旧版负值升级后复活 #408 openai/responses split-shape 反推、[Bug] Anthropic plugin passthrough 漏计最终 usage 中的缓存 token,导致上下文大小异常下降 #790 anthropic cache-write additive、usage-totals 各协议矩阵,均字节不变,无一命中 declaredInclusive===true && input<cached 组合。本地定向跑 56 pass / 0 fail(kernel build 后含 host-usage-postfold);npm run typecheck 干净;CI 30 checks 0 failed(仅剩 1 个 windows test shard 在跑,其余全绿/skip)。高危模式逐项过:非 anthropic 车道不受影响(守卫要求 protocol==="anthropic");边界用 < 非 <=,input==cached 不触发。无回归。
  6. 是否破坏缓存:不涉及——纯输入侧数值归一,不动缓存命中/重试/游标/去重键。
  7. 是否改设计方向:不涉及——沿用 [Bug] anthropic lane: cache-hit % halved when the upstream relays an OpenAI-semantics backend (billing_usage.semantic=openai, input_tokens inclusive) #4128「单一决策点 + 显式声明优先 + 不做数字反推」的既有方向,只是给「信任声明」补了一个自洽前提。
  8. 是否引入新配置:不涉及。

测试有效性:7 例覆盖双向(触发 + 不触发)、带 creation、边界 input==cached、未盖章、非 anthropic 车道、usageTotals 端到端(DeepSeek 形状不再少算 + 真 inclusive 不变);断言有牙、回归能兜底。合格。

⚠️ 已就地修掉的一处交付缺陷(原为阻断项)

原 PR 正文 Closes #5515 引用错了 issue,按现状合误会误关一个无关的在办工作项、且关不掉真正的 bug:

判定

代码改动正确、最小、测试充分,可合——待最后一个 windows test shard 转绿即可由 owner 人工合并。交付侧唯一硬伤(Closes 引用错位)已就地修正,不再阻断。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

收工摘要(完整八问审核见上一条):

…real GitHub thread

The bug report is GitHub issue #5513 (mirrored from ework issue #5515);
#5515 on GitHub is an unrelated open issue. Test file name and comment
markers now use the repo-conventional GitHub number.

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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 绿)。

八问

  1. 是什么问题: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。
  2. 什么原因导致的:#4128 只防了「从数字推断语义」,没防「声明被自己的数字否定」——信任判定缺自洽检查;说谎/陈旧的 stamp(DeepSeek relay split 上报却打 openai 标)恰好落在无防护的信任点上。
  3. 影响范围与严重程度:携带自相矛盾 inclusive stamp 的 anthropic 车道流量(如 DeepSeek relay):命中率指标 >100%(日志噪音/误报)、nudge growth meter 少计(大上下文下可能推迟折叠)、USD 计费少计。不影响真实缓存行为与数据正确性,不影响合法 relay(input≥cached)。p2(指标漂移+meter 少计,可用有绕行)。
  4. 怎么修复的:在唯一信任点加一道自洽闸门——inclusive 声明仅在 input >= cached 时可信,否则回退 split。单决策点、所有消费方自动继承;不动数字推断(#4128 原则保留)、不动其他车道。
  5. 是否有回归问题:逐项审过既有行为——非 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。仅自相矛盾帧结果改变,正常流量输出不变。
  6. 是否破坏缓存:不涉及。只改 usage 统计计算,不触 wire 字节、消息身份、prefix-cache key、重试/增量游标、去重键。
  7. 是否改了设计方向:没有。在 #4128 决策框架内精化(声明信任+数字防御),补缺失的一半,不是新机制。
  8. 是否引入新配置:没有。

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。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) Review lane #5518 审结 APPROVE(可合):独立核完全 diff + 所有消费路径(usageTotals/plugin 车道/USD 计费/诊断共用 promptInputTotal 单决策点),非 anthropic 车道与合法 inclusive relay(input≥cached)字节级不变,仅自相矛盾 stamp(input<cached)回退 split;本地定向 22/22 绿 + 回归测试牙齿核查(base 上恰好 3 个 bug 形状用例失败);CI 镜像缓存 0 failed。已顺手修一处小问题(测试文件名/引用/PR 正文 Closes 误用内部镜像号 #5515,改为 GitHub 号 #5513,否则合并会误关无关 issue)commit 5343832。完整八问+行为变更披露(旧→新)已贴 PR review(GitHub 禁自批,以正式 review 评论给出 APPROVE);§7.4 默认决策点不走 auto-merge,待人工合并;一行结论已报 #4599。

@ranxianglei
ranxianglei merged commit 96f6800 into master Nov 2, 2026
47 checks passed
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1003-master.96f6800 (rolling master tag).

npm install -g billion-context@0.0.1003-master.96f6800  # 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.

Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Nov 2, 2026
…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.
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.

Cache hit rate 4000% | 缓存命中率 4000%

1 participant