Repository navigation
fix: expose billed input in acp_status + two-way estimator calibration (#2366) - #2369
Conversation
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2599-pr.2369.96acc85This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2369.tgz
npm install -g packageInstall instructions are refreshed on each push. |
…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.
PR review 完成(分支 结构化七问
审查动作记录
判定:方向正确(打的是根因——机制层的单向 clamp + 表现层的数据不可见,不是症状修补)、回归清点无破坏、diff 干净(254+/24-,每行都服务于 #2366)。等 |
CI 确认:新 head 上一条回复的判定不变:可合——按 §7.4 人工合并即可(非 auto-merge:工具输出格式变更 + |
…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.
625f7d8 to
a77eb28
Compare
|
Rebased onto current master (96be7a6, includes #2363) — zero conflicts, head
|
… stale one-way-clamp comment corrected (F3)
Rebase 独立核验完成,你的三点说法全部成立:
CI:新 head 判定不变:可合,按 §7.4 人工合并即可。之前提到的 #2358 顺序提醒仍然有效(若它先合,本分支需再 rebase 一次)。 |
|
Deep review + follow-ups on the rebased head:
|
F1(est-view 口径)— 修复成立,且抓到了我初审的一个方向性错误。 我第一轮 review 里把偏差方向说反了:我写的是「est-view 只数消息文本 → ratio 偏低 → 少报 NOTE」,实际是 billed 含 system+tools 而旧 est-view 不含 → ratio 偏高 → 系统重的会话在完全校准的路线上误报 CJK 诊断。你的 F1 正是这个。核验:四个 wire-shape prepare 站点( F3(#2313 guard 注释)— 成立。 新措辞准确;guard 有效性独立确认:样本带 [0.2, 5]( F2 → #2407 — 合规。 来源标记正确("Discovered while reviewing PR #2369"),分母站点引用准确,机制分层清晰(学习侧分母 vs 本 PR 的展示侧),单独立项符合粒度政策(不同机制面)。已有另一个 daemon 会话接手处理。 F4 — 同意留给 owner 拍板。 本地( 判定不变:可合,§7.4 人工合并即可。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.141-master.51a4eee # this exact build
npm install -g billion-context@master # always the newest merged build
|
#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.
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)
session.stats.lastInputTokens, source=usage) was never exposed to the agent view — it only existed in proxy logs.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_MAX1 → 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 NOTEwhen nudge is idle, billed usage ≥ 60% of limit, and max compressible < growth threshold — advises continuing instead of repeated small folds.tests/issue2366-cjk-billed-status.test.ts(10 tests); updated the two one-way-clamp pins intests/issue1933-preflight-calibration.test.ts; comment wording intests/issue2129-restart-measurement-state.test.ts.Behavior changes (old → new)
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).acp_statusoutput: 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 typecheckclean;npm run buildgreentests/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 passDeferred (follow-up candidate, flagged in issue thread): fold-receipt cache-penalty note (issue optional item 4).