Repository navigation
fix: meter signature-only thinking mass (Claude Code) — #1320 - #1326
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1326Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1326.tgz
npm install -g packageInstall instructions are refreshed on each push. |
9e66503 to
2faa53e
Compare
[bot] 🏷 Review complete — 可以合并(merge 操作留给人工)。 What I did to the PR
① Original issue (#1320) — directionConfirmed: root cause is a metering blind spot, not a wire defect. Signature-only thinking blocks round-trip faithfully ( ② Regression review — existing behaviors on touched paths
③ Code review
④ Test validityThe 11 tests have teeth: exact share arithmetic (not tautologies), a kernel-level repro that asserts the pre-fix symptom (compressible mass below 50K → idle nudge) vs post-fix arming on identical growth, wire-fidelity deep-equal, and a real-pipeline gauge E2E through an actual HTTP server with dynamic ports ( ⑤ Diff cleanliness4 files, +431/−6, every line on-purpose, single linear commit, no lockfile churn (kernel pin comes from master's own #1316, not this PR). Coordination noteTwo open PRs also touch 中文摘要:修复了 Claude Code signature-only thinking block 导致本地计量全盲的问题(nudge 一直 idle、压缩回执虚报省量),方案是在计量决策点把 provider 实测残差投影到签名块上而非改线包字节;已 rebase 到最新 master(含 kernel 0.0.90)并验证 seam 未变,本地与 CI 全套检查(含 e2e-codex)全绿,行为变更仅影响带签名的 Anthropic 会话的计量数值、线包字节不变,可以合并。 |
|
Coexistence verification on current master head: both this and the sibling claude-native estimation PR (#1325/#1326) cherry-picked cleanly onto today's master and pass the full gate together — typecheck ✓ / 2608 tests 0 fail (10 e2e-gated skips) / build ✓. Shared-file regions are disjoint (window resolution vs prepareAnthropic thinking projection), any merge order works. |
[bot] 🏷 收到,coexistence 结论与我 review 时的 coordination note 一致。补充:master 又前进了(#1328 overflow-rescue、#1298 fingerprint receipts、#1329 sentinel、#1331 kernel 0.0.90→0.0.91 均已合入),而本分支还停在 |
2faa53e to
0987a31
Compare
Closes #1320
Root cause
Claude Code round-trips extended-thinking blocks as signature-only entries (
{type:"thinking", signature}with no visible text). The provider restores and bills the hidden thinking tokens server-side, so itsusage.input_tokensis correct — but locallyanthropicToCoreconverts them to empty-text reasoning messages, and every per-message meter (nudge compressible mass, blockcompressedTokensreceipts, context breakdown/gauge) sees 0 tokens for them. Verified against master0cef2d47+ acp-kernel 0.0.89: the wire round-trip is faithful (signature re-emitted bycoreToAnthropic); only local metering is blind.Fix (decision-point projection, not a payload mask)
New
projectThinkingMass()insrc/server/budget.ts, called inprepareAnthropicright afteranthropicToCore:CoreMessage.thinkingTokensseam —countMessageTokenscounts it at every metering site already (nudge ranges,applyCompressionreceipts, render tags, context breakdown), so all surfaces pick it up with no further changesunprunedTokensswitched to the samecountMessageTokenscaliberNo kernel change needed (the seam already exists in pinned acp-kernel 0.0.89) — no cross-repo release ordering.
For Anthropic sessions carrying signature-only thinking blocks (i.e. Claude Code):
~N tokens saved,tokensCompressed): previously understated by the thinking share of folded ranges → now include it (issue numbers "~17k saved" vs ~41k real are corrected).Non-Anthropic wires and sessions without signed thinking: unchanged (projection no-ops).
The projection lags one turn (it uses the previous turn's provider total — the same contract as the nudge's own "previous turn input_tokens" input); steady-state Claude Code sessions converge from their 3rd turn on.
Known residuals (documented, out of scope)
output_tokensapportionment not done — gap-based global consistency instead (self-bounding).prepareCountTokens(read-only/messages/count_tokenspreview path) not touched.Folded-in observation (#1310 obs 4 / #684 sentinel)
warnAnthropicThinkingPairsfires when some assistant tool_use turns lack any thinking block while others carry one — this over-fires on legitimate Claude Code histories (turns that simply had no thinking; plus bili's own #651 deliberate reasoning drops). Left untouched here (a logging-behavior change deserves its own scope); filed as a separate issue.Pre-flight
tests/issue1320-thinking-mass.test.ts(11 tests): projection arithmetic (exact-residual weighting, clamps, exclusions); kernel-level bug repro asserting the unprojected run's compressible mass sits below the 50K threshold with an idle nudge while the projected run arms tier-1 on identical growth; wire-fidelity deep-equal; real-pipeline gauge E2E through the proxy.src/server.ts).Requires human review: changes nudge/metering semantics on a hot file — outside the auto-merge gate (§7.4).