Repository navigation
[PR] fix(#5303): restore the costUsd spend ledger on session reload - #5338
Conversation
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.7522-pr.5338.3c62cdaThis exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr5338.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复。 #5343 是 #5312(外来折叠块永生)唯一在途的修复 PR;它实现的是宿主侧 Option A,与 Option B(内核血缘字段,挂在 #5312 等拍板)机制不同、刻意分开。无并列实现。 我独立审了这条 PR(没有照抄 issue 结论),并把分支 rebase 到当前 master、解掉冲突后推回。下面按结构化八问答。 结构化八问1. 是什么问题 2. 什么原因导致的(我在当前 master 逐行验证过) 3. 影响范围与严重程度 4. 怎么修复的 5. 是否有回归问题 6. 是否破坏缓存 7. 是否改了设计方向 8. 是否引入了新的配置 冲突收敛 & 验证
非阻塞发现(留 owner 定夺,未另开 issue——来源链受限)
判定代码正确、安全不变量经端到端验证成立、纯增量、冲突已收敛到当前 master。建议:CI 在新 head 转绿后由你合并(我无权 merge)。上面发现 1 若你想现在收,我可以直接补一个带测试的修复 commit;发现 2 可忽略。 |
…whitelist buildRecord persists stats via spread (costUsd rides to disk), but buildSession rebuilt stats from a hand-picked whitelist that dropped costUsd — every cold load (restart, LRU eviction) silently zeroed the historical USD ledger, and the next save wrote that loss into BOTH stores (JSON envelope + index.db stats_json projection). Restore it through a shared validator (billing.validCostUsd, also used by the panel): finite non-negative fields survive, partially-valid blobs zero-fill missing lanes, all-invalid reads as no-ledger. Absent stays absent for pre-billing files.
f07cf76 to
550a687
Compare
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.959-master.85d46ac # this exact build
npm install -g billion-context@master # always the newest merged build
|
…e environment (corrects 9eb1aa3) 9eb1aa3 regenerated outside the suite's preload chain, so it captured the display-gate ON bytes (default since #5368) while every CI leg pins BILI_MESSAGE_TIMESTAMPS=0 via tests/preload-test-env.mjs — the committed digests could never match the pinned runners. This run uses the exact CI invocation (node --import ./tests/preload-storage-baseline.mjs --import ./tests/preload-test-env.mjs --import tsx), so wire bodies are back on the pre-flip gate-off values and the stateDigest fields pick up the persisted-state contract moves that landed on master after the goldens' last refresh (#5338 cost ledger, #5412 async latch parity, #5343 block archive). Turns=15/foldAt=9 unchanged; verified green locally under the same preloads (storage-engine-proofs + golden-storage-render).
Related: #5303 (persistence half of the reported family — the projection silently dropping
costUsd; sister-lane presentation semantics are a product decision, tracked in #5336)What / why
buildRecordpersistsstatsby spread, socostUsd(the USD spend ledger from #4899/#4916) always reaches disk — in BOTH the JSON v3 snapshot and theindex.dbstats_jsonprojection, since one record feeds both writers. ButbuildSession, the single restore funnel for every cold load (process restart, LRU eviction undersessions.max, legacy + unified paths alike), rebuiltstatsfrom a hand-picked whitelist that omittedcostUsd. Every cold load therefore silently zeroed the historical spend ledger — and the next save wrote that zero into both stores again, making the loss permanent.Net effect on v0.1.209+ (where billing shipped): after any restart or eviction, panels/ledger show post-restart spend only; pre-restart cost is unreconstructable from either store. This is the "two stores disagree / no durable retention" findings in #5303: the db is not losing fields at write time — the field is dropped at reload and the drop is then propagated.
Fix
src/billing.ts: exportvalidCostUsd()— the validator the panel already ran locally, now shared by writer-side consumers. Object gate; each lane finite ≥ 0; all-four-invalid reads as no-ledger (undefined); partially-valid blobs zero-fill missing lanes (lossless: the settlement writer always emits all four lanes initialized to 0).src/persist.ts: restorecostUsd: validCostUsd(stats.costUsd)through thebuildSessionwhitelist (+ documented field onPersistedSession.stats). Absent stays absent — pre-billing files gain no fabricated ledger.src/web/sessions-data.ts: delete the local duplicate validator, import the shared one.Behavior change (disclosed per repo policy)
session.stats.costUsdsilently reset on every cold load; the next save persisted the reset into the JSON snapshot AND the db projection ⇒ cumulative historical cost destroyed.Tests
New
tests/issue5303-costusd-restore.test.ts(4 tests, pinned to the legacy envelope path exactly like the #2129 restart test): round-trip survival of a realistic lane ledger; continuity (restored base + price stamp + fresh usage accumulates onto, not resets); corrupt-blob handling (all-invalid dropped / partial zero-filled / sibling counters survive); absent-stays-absent. Adjacent suites green locally:billing-web,billing-prices,issue2129-restart-measurement-state(27/27 with the new file); typecheck clean; full suite = CI on this head.查重: 不重复 — no prior issue/PR covers the costUsd restore gap; nearest neighbors are #2478/#2482 (savings display pricing source) and #4700/#4723 (
tokensSavedon tool-driven folds) — different fields, different mechanisms.