Skip to content

fix: expose billed input in acp_status + two-way estimator calibration (#2366) - #2369

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-08_cjk-billed-status
Oct 8, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-10-08_cjk-billed-status

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2366

Problem

On CJK-heavy routes, upstream billing runs 2.4–4.0× above bili's local chars/4 estimate (defaultCountTokens). Every surface the agent can see (acp_status breakdown, nudge reason) was estimate-based, so the session looked "healthy" (53K/200K) while billing 150K+; by the time pressure became real, compressible mass was exhausted → small-benefit fold loop + prefix-cache collapse (issue evidence: 47 folds / 3h averaging 6.1K freed; cache hit 100%→12%).

Root cause (two layers)

  1. Presentation: the usage-grade billed input (session.stats.lastInputTokens, source=usage) was never exposed to the agent view — it only existed in proxy logs.
  2. Mechanism: the per-route estimator calibration (DSH: 切换模型后,preflight 在窗口远未满时提前触发压缩 #1933/[Bug] 上下文展示估算 206K,usage 返回后为 98K:希望校准估算并区分安全上界 #2117/Restart loses measurement state: buildSession() omits stats fields buildRecord() persists (k̂, usage anchor, baseline origin, localInputEstimate) #2129) was deliberately one-way — CALIBRATION_CLAMP_MAX = 1 — so under-estimating routes (samples >1) always clamped back to the raw proxy. The fix(preflight): calibrate local estimates against provider usage, provenance-gate the baseline, scale the fold budget (#1933) #1940 premise ("overflow arm covers inflation") left the session deadlocked exactly as reported.

Changes (host-only; no kernel/**; no new config surface)

  • src/cache-ledger.ts: CALIBRATION_CLAMP_MAX 1 → 4. Two-way bounded calibration; all existing evidence gates unchanged (≥2 samples, ×2 consistency band, sample band [0.2, 5] still rejects non-corresponding reports first, route+model keying, MIN_ESTIMATE floor). Max 4 covers the measured CJK band and stays below the sample band max so wild reports are rejected before the clamp engages.
  • src/acp-status.ts: additive sections after the base report (scope-mode output unchanged):
    • BILLED INPUT (<source>): N tok (+ age suffix when stale ≥2m, + est-view total … ratio X.X×) whenever a usage-grade anchor exists; NOTE at ratio ≥ 1.5 telling the agent to judge pressure from BILLED INPUT.
    • PRESSURE NOTE when nudge is idle, billed usage ≥ 60% of limit, and max compressible < growth threshold — advises continuing instead of repeated small folds.
  • Tests: new tests/issue2366-cjk-billed-status.test.ts (10 tests); updated the two one-way-clamp pins in tests/issue1933-preflight-calibration.test.ts; comment wording in tests/issue2129-restart-measurement-state.test.ts.

Behavior changes (old → new)

  1. CALIBRATION_CLAMP_MAX: 1 → 4. Under-estimating routes now publish k̂ up to 4 (previously forced to 1 = raw proxy). Effect: calibrated estimates on such routes inflate toward billing truth → preflight gate / output clamp engage earlier in billing terms. Deflate direction (k̂<1) unchanged; persisted factors ≤1 remain valid; restore validation reads the constants dynamically (Restart loses measurement state: buildSession() omits stats fields buildRecord() persists (k̂, usage anchor, baseline origin, localInputEstimate) #2129).
  2. acp_status output: gains up to two conditional sections (BILLED INPUT line+NOTE, PRESSURE NOTE). Sessions without a usage-grade anchor render byte-identical to before; scope= mode returns base only, as before. Breakdown numbers and nudge decision logic are untouched.

Not auto-merge eligible per §7.4 (tool-output format change + default-value change) → human review required.

Verification

  • npm run typecheck clean; npm run build green
  • Targeted suites: tests/issue2366-cjk-billed-status.test.ts, tests/acp-status.test.ts, tests/issue1933-preflight-calibration.test.ts, tests/issue2129-restart-measurement-state.test.ts → 30/30 pass

Deferred (follow-up candidate, flagged in issue thread): fold-receipt cache-penalty note (issue optional item 4).

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-08_cjk-billed-status (a0952c9)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2599-pr.2369.96acc85

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-pr2369.tgz
npm install -g package

Install instructions are refreshed on each push.

ranxianglei pushed a commit that referenced this pull request Oct 7, 2026
…on two-way

Review of #2369: three pre-existing comment blocks still described the retired
CLAMP_MAX=1 contract ('one-way, deflate only', 'clamped to [0.25, 1]',
'violating the one-way deflate contract'). The code now publishes k̂ in
[0.25, 4]; align the wording with the facts. Comment-only, no behavior change.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

PR review 完成(分支 2026-10-08_cjk-billed-status @ 625f7d8f9,基于当前 master 74cf5246d,无 stale-base)。结论:可合——待新 head 的 CI 全绿后人工合并(§7.4 明确非 auto-merge:工具输出格式变更 + 默认值变更)。

结构化七问

  1. 是什么问题:CJK 重负载路由上本地 chars/4 估计比真实计费低 2.4–4.0×,而 agent 可见的全部表面(acp_status、nudge reason)都是估计口径——会话在计费 150K+ 时看起来「53K/200K 很健康」,等压力真实到来时可压缩质量已耗尽 → 小收益折叠循环 + 前缀缓存崩塌(issue 证据:3h 47 次折叠平均仅省 6.1K;缓存命中 100%→12%)。
  2. 什么原因导致的:两层。表现层:usage 级计费输入(session.stats.lastInputTokens, source=usage)只存在于代理日志,从未暴露给 agent 视图。机制层:per-route 估计器校准(DSH: 切换模型后,preflight 在窗口远未满时提前触发压缩 #1933/[Bug] 上下文展示估算 206K,usage 返回后为 98K:希望校准估算并区分安全上界 #2117/Restart loses measurement state: buildSession() omits stats fields buildRecord() persists (k̂, usage anchor, baseline origin, localInputEstimate) #2129)被 PR fix(preflight): calibrate local estimates against provider usage, provenance-gate the baseline, scale the fold budget (#1933) #1940 刻意设计成单向(CALIBRATION_CLAMP_MAX = 1,只允许 deflate)——低估路由(样本 >1)的证据被收进 ring 后在发布时丢弃,永远发 k̂=1。「overflow arm 兜住膨胀」的前提在 CJK 路由上被实测证伪,会话正好死锁在 issue 描述的状态。
  3. 怎么修复的:两个机制层面动作,无新配置面、host-only、不碰 kernel。(a) 校准改为双向有界:clamp 上限 1→4,全部既有证据门不变(≥2 个同路由同模型样本、×2 一致性带、[0.2, 5] 样本合理带先拒非对应报告、MIN_ESTIMATE 2000、window 3)——4 覆盖实测 CJK 带且低于样本带上限 5,异常报告在 clamp 生效前就被拒。(b) acp_status 增量暴露已有测量:BILLED INPUT 行(用 statusInputBaseline 的溯源契约,与 nudge 决策同一数字;≥2m 加 age 后缀;ratio≥1.5 加 NOTE)+ PRESSURE NOTE(nudge idle && 计费占比≥60% && maxPending<增长阈值,建议继续任务而非反复小折叠)。
  4. 是否有回归问题:未发现。逐项审过既有行为:
    • 全部 k̂ 消费点(preflight trigger/rounds/fit:src/server.ts:5678-5699,5931、src/preflight.ts:1010-1113;输出钳制:src/server/budget.ts estimateInputTokens/clampOutgoingOutput 经 src/server.ts:4179/4414/4796;[Bug] 上下文展示估算 206K,usage 返回后为 98K:希望校准估算并区分安全上界 #2117 显示路径 contextEstimateTokens;restore 校验 src/persist.ts:788)——没有任何一处假设 k̂≤1,k̂>1 只是把本地估计向计费真值抬高,决策更早触发是有意的;deflate 方向(k̂<1)逐字节不变。
    • 持久化兼容:restore 校验动态读常量,历史文件里 ≤1 的因子继续有效,(1,4] 升级后合法(设计意图),=5 仍被拒且测试钉住(issue2129 更新后的 pin 保留牙口)。
    • acp_status:基础报告、scope= 模式(短路在新代码之前)、nudge 重算调用(tokenCount 仍是 statusInputBaseline)均未动;无 usage 锚点的会话输出与 master 一致(有测试钉住);breakdown 数字与 decideNudge 逻辑未触碰,kernel/ 零改动(kernel-guard 绿)。
    • 一个已知的轻微语义偏差(非阻塞):est-view total 只数消息文本(与报告自身 breakdown 口径一致,不含 system/tools 开销),所以大 manifest 客户端上显示的 ratio 会略低估真实分歧——偏差方向是少出 NOTE 而非误报,且标签如实写了口径。
    • 热文件提醒:feat: attribute host system-prompt rewrites in the cache ledger (#2350) #2358 也改 src/cache-ledger.ts(recordCacheSample 区,master 行 ~887-907 结束 + settleUsageReport 内一个小 hunk),与本 PR 常量块(master 行 905-932)相邻——两者谁后合谁需要 rebase,无语义冲突。
  5. 是否破坏缓存:前缀缓存机制、重试、去重键、增量游标全部未触碰。间接效应:校准后的 preflight 在低估路由上更早触发 → 折叠变得更早更大而非更晚更小——这正是本 issue 要消除的折叠循环的反面,属预期的压力管理权衡。
  6. 是否改了设计方向:没有。是既有 DSH: 切换模型后,preflight 在窗口远未满时提前触发压缩 #1933/[Bug] 上下文展示估算 206K,usage 返回后为 98K:希望校准估算并区分安全上界 #2117/Restart loses measurement state: buildSession() omits stats fields buildRecord() persists (k̂, usage anchor, baseline origin, localInputEstimate) #2129 校准机制向另一侧的对称扩展 + 把已有测量数据搬到 agent 视图;架构边界(host-only、无 kernel、无配置面)不变。
  7. 是否引入了新的配置:无 env/config/CLI 新增。仅内部常量 PRESSURE_NOTE_USAGE_FRACTION = 0.6(硬编码,不进配置面,与 issue「config 项或自动校准」二选一中选了后者保持一致)和已在 PR 正文披露的行为变更 CALIBRATION_CLAMP_MAX 1→4。

审查动作记录

  • 顺手修掉的小问题(已推送到 PR 分支,commit 625f7d8f9,comment-only):三处存量注释仍在描述已退役的单向契约——src/persist.ts:781-786("violating the one-way deflate contract")、src/session.ts:190-195("clamped to [0.25, 1] — one-way by design … publish k̂=1 … overflow arm covers them")、src/server.ts:5671-5677("clamped 0.25–1 — one-way, deflate only")。代码事实已是 [0.25, 4] 双向,措辞与事实不符即修。typecheck 复验干净。
  • 定向验证(本地收窄范围):typecheck 干净;tests/issue2366-cjk-billed-status.test.ts + tests/acp-status.test.ts + tests/issue1933-preflight-calibration.test.ts + tests/issue2129-restart-measurement-state.test.ts + tests/issue2117-context-estimate-provenance.test.ts(PR 清单外主动补跑)→ 41/41 通过。
  • 测试有效性:10 个新测试断言有牙——精确数值 pin(均值 2.75 不截断、4.75 截断到 4、越带样本被拒且 ring deepEqual 不变、跨路由保持 raw、输出串含 ratio 格式/age 边界/scope 模式/pressure-note 触发与静默/普通会话无新增段);两处更新 pin 与新契约一致且保留拒绝牙口。小缺口(低风险):锚点会话输出对 master 只有「无新增段」断言,无逐字节 golden 对比——鉴于纯增量结构可接受。
  • CI:原 head 0703a0f2d 22/24 检查绿(ubuntu 双 node 版本全量、kernel-guard、docs-sync/coverage、全部 e2e lane),2 个 windows-latest test 作业检查时在跑;新 head 625f7d8f9 已推送,CI 重跑中。
  • 遗留项确认:fold-receipt 缓存惩罚注记(issue 可选项 4)按 PR 正文列为 deferred follow-up,未混入本 PR,范围干净;后续做时单独立项即可。

判定:方向正确(打的是根因——机制层的单向 clamp + 表现层的数据不可见,不是症状修补)、回归清点无破坏、diff 干净(254+/24-,每行都服务于 #2366)。等 625f7d8f9 CI 全绿后可合。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

CI 确认:新 head 625f7d8f9(含注释修正 commit)全部 27/27 检查通过(ubuntu/windows 双 node 版本全量测试、kernel-guard、docs-sync/coverage、build-artifact、全部 e2e lane),PR mergeable: true。

上一条回复的判定不变:可合——按 §7.4 人工合并即可(非 auto-merge:工具输出格式变更 + CALIBRATION_CLAMP_MAX 1→4 默认值变更,均已在 PR 正文披露旧值→新值+原因)。合并顺序提醒:若 #2358(同样触碰 src/cache-ledger.ts)先合,本分支 rebase 一下再合;两者无语义冲突。

awork added 2 commits October 8, 2026 17:56
…on two-way

Review of #2369: three pre-existing comment blocks still described the retired
CLAMP_MAX=1 contract ('one-way, deflate only', 'clamped to [0.25, 1]',
'violating the one-way deflate contract'). The code now publishes k̂ in
[0.25, 4]; align the wording with the facts. Comment-only, no behavior change.
@ranxianglei
ranxianglei force-pushed the 2026-10-08_cjk-billed-status branch from 625f7d8 to a77eb28 Compare October 8, 2026 10:01
@ranxianglei

Copy link
Copy Markdown
Owner Author

Rebased onto current master (96be7a6, includes #2363) — zero conflicts, head a77eb28b.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

Rebase 独立核验完成,你的三点说法全部成立:

  1. 基线:merge-base = 96be7a68b = 当前 master HEAD,分支上只有两个 PR commit(65ba6e7f1 fix + a77eb28b9 docs 修正),无其他残留。
  2. [cache-ledger] seam 取证对非 Anthropic 线格式失明:msgIndex 回退 0 被当真实索引展示,#2059 追加判定臂失效 #2339 seam 未被触碰:对 true merge-base 的合并 diff 恰好只含既定的 8 个文件;git diff origin/master...head -- src/cache-ledger.ts | grep -c msgIndex = 0——[cache-ledger] seam 取证对非 Anthropic 线格式失明:msgIndex 回退 0 被当真实索引展示,#2059 追加判定臂失效 #2339 的 msgIndex null-semantics 区(seamLcp、msgIndex ?? "?" 日志行等)与 master 逐字节一致。「revert」外观确认是 stale-base(fork 点 74cf524 早于 fix: seam forensics reads all three wire shapes; msgIndex null when position unknowable (#2339) #2341/[cache-ledger] seam 取证对非 Anthropic 线格式失明:msgIndex 回退 0 被当真实索引展示,#2059 追加判定臂失效 #2339)造成的 raw-diff 假象,不是内容问题。
  3. PR 自身内容 rebase 后完整:CALIBRATION_CLAMP_MAX = 4(cache-ledger.ts:982)、BILLED INPUT/PRESSURE NOTE 段、三处注释修正(persist/session/server)全部在位;行间漂移(server.ts 等)全部来自 master 侧新合入的 fix: intrinsic termination gate for runaway enumeration (#2346) #2363/fix: seam forensics reads all three wire shapes; msgIndex null when position unknowable (#2339) #2341 内容,非本 PR 改动。

CI:新 head a77eb28b 全量 28/28 检查通过(ubuntu/windows 双 node 版本全量测试、kernel-guard、docs-sync/coverage、build-artifact、全部 e2e lane)。

判定不变:可合,按 §7.4 人工合并即可。之前提到的 #2358 顺序提醒仍然有效(若它先合,本分支需再 rebase 一次)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Deep review + follow-ups on the rebased head:

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

a0952c95 独立核验完成,四项 follow-up 全部确认:

F1(est-view 口径)— 修复成立,且抓到了我初审的一个方向性错误。 我第一轮 review 里把偏差方向说反了:我写的是「est-view 只数消息文本 → ratio 偏低 → 少报 NOTE」,实际是 billed 含 system+tools 而旧 est-view 不含 → ratio 偏高 → 系统重的会话在完全校准的路线上误报 CJK 诊断。你的 F1 正是这个。核验:四个 wire-shape prepare 站点(prepare-anthropic.ts:321、prepare-google.ts:237、prepare-openai.ts:319、prepare-responses.ts:460)都持续刷新 metadata.systemPromptTokens;k̂ 学习分母 lastLocalTextEstimate(prepare-anthropic.ts:344-346)= 消息文本估计 + countSystemAndToolsTokens + imageReserveFor——修复后的 est-view(文本 + systemPromptTokens)在文本+系统+工具三项上与配对口径逐一对齐。新 pin test 有牙(精确字符串 + note 缺席断言)。
残留一处小口径差:est-view 未含 imageReserveFor 项,图片重会话仍可能略偏高的 ratio(方向同 F1、量级小得多)。按粒度政策建议作为 #2407 的同族 checklist 项挂进去(都是「billed-vs-local 口径对齐」失败域),不单立项——你定。

F3(#2313 guard 注释)— 成立。 新措辞准确;guard 有效性独立确认:样本带 [0.2, 5](cache-ledger.ts:964-965,gate 在 :1042)会在 24–34× shim 样本到达 clamp 之前先拒掉,clamp max 4 只是兜底。顺带认领:stale one-way 注释共四处,我的 docs commit 修了三处(persist/session/server),这第四处漏了,多谢补上。

F2 → #2407 — 合规。 来源标记正确("Discovered while reviewing PR #2369"),分母站点引用准确,机制分层清晰(学习侧分母 vs 本 PR 的展示侧),单独立项符合粒度政策(不同机制面)。已有另一个 daemon 会话接手处理。

F4 — 同意留给 owner 拍板。 maxPending < nudgeGrowthTokens 的语义(可折叠质量不足一个 nudge-growth 步长)本身站得住,纯措辞问题,不阻塞合并。

本地(a0952c95):typecheck clean;定向四套(issue2366 + issue1933 + issue2129 + acp-status)31/31 通过(你跑的三套 25/25 一致,我多带了一套 acp-status)。
CI:a0952c95 全量 28/28 检查通过。

判定不变:可合,§7.4 人工合并即可。

@ranxianglei
ranxianglei merged commit 51a4eee into master Oct 8, 2026
28 checks passed
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.141-master.51a4eee (rolling master tag).

npm install -g billion-context@0.0.141-master.51a4eee  # 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.

ranxianglei pushed a commit that referenced this pull request Oct 8, 2026
#2407)

Follow-up to #2366/#2369 review (issue #2407): the calibration pair's denominator (lastLocalTextEstimate) counted visible text + system/tools + image reserve only, while signature-only thinking blocks (#1320) are billed upstream but project as empty text locally — projectThinkingMass puts that mass into CoreMessage.thinkingTokens, which estimateCoreMessages never sees. On extended-thinking routes with a stable thinking share, k̂ therefore inflated beyond the text tokenizer caliber and passed every evidence gate (composition-stable); the two-way clamp from #2366 turned that into a bounded (<=4x, baseline-floored) earlier-fire channel at the preflight trigger, fit gate, and output clamp — the exact #1933 over-triggering damage class the one-way design was protecting against.

Fix (issue's suggested option A): new estimateCoreMessagesWithThinking (kernel countMessageTokens per message, lockstep with kernel metering) used ONLY at the three lastLocalTextEstimate store sites (prepare-anthropic/openai/responses). Non-thinking sessions are byte-identical (thinkingTokens unset => estimators equal); openai/responses lanes are no-ops today but keep cross-lane caliber uniform. Decision sites untouched — still usage-baseline-floored.

Behavior change (disclosed): k̂ denominator semantics — text+sys/tools+images -> +projected thinking mass. Effect: extended-thinking routes learn k̂ ~ pure tokenizer caliber (~1 for consistent billing) instead of an inflated composition-dependent factor up to the 4x clamp.
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.

长会话中token用量被系统性低估(CJK 文本),导致agent误判上下文压力、进入「小收益折叠」循环

1 participant