Repository navigation
fix(cost): price a legacy session's spend from its journal, not its totals - #732
Merged
Merged
Conversation
…otals The per-category rows were still derived from the session's token totals at the *current* model whenever the session carried no accumulated split — which is every session recorded before the split was accumulated per request. Switching the model (even briefly, then back) re-billed the whole history at the new model's rates: a real session showing a ¥8.4141 total and ¥1925.1451 of rows. Price such a session's journal instead, request by request, each at the rates of the model that served it (`model_changed` events say when the model changed), and cache the result in its `session_info` so it is replayed once. Drop the derive-from-totals fallback from `get_state` entirely: the rows it invented contradicted the total it sat next to, which is the one thing that panel must never do. That session now reports ¥8.8986 against the same ¥8.4141 total — the ordinary estimate-versus-invoice gap the panel already discloses, rather than a contradiction. The journal records model *changes*, not the model on each request, so requests before the first change are billed at the session's current model: the same assumption the old fallback made for everything, but here covering only the span before the first switch.
huichen
enabled auto-merge (squash)
September 20, 2026 01:38
huichen
added a commit
that referenced
this pull request
Sep 20, 2026
…le history (#735) #732 replayed a session's journal only when its `session_info` carried no split at all. But the first build that accumulated a split (#729) started accumulating wherever a session already was, so sessions that existed then got a split that covers only the tail of their history — and a split that exists is exactly what suppressed the replay. A live example: a session showing ¥8.4141 when it was upgraded now reports rows summing to ¥0.908176 against a ¥9.3223 total — and 9.322278 − 8.414102 is that same ¥8.4141, i.e. the rows cover only what was spent after the upgrade. Mark a split as covering the whole history (`cost_split_complete`) and replay unless that mark is present, rather than unless the split is absent: - the replay path writes the mark together with the split it cached; - the run-end and manual-compaction writes carry it forward, so a load does not replay again; - a session built in this process starts from zero counters, so its split covers its whole history by construction and it carries the mark from its first write. Against the local session database this turns every session into a replay (no stored mark exists yet — the previous build never wrote one) and the session above goes from ¥0.9082 to ¥9.6968 of rows for its ¥9.3223 total. ## Still not matching, and why Ten of those sixty sessions remain off by more than 50%, in the other direction: their **total** is the undercount. They ran on `azure/gpt-6-astra` while that model had no prices configured, so every one of those requests was charged at an estimate of zero and the session's accumulated total never included them. Replaying their journal now prices the same requests at today's rates for that model (70 / 350 / 7 / 90 per 1M), which is the first time those requests have been priced at all. That gap is not something this change can close: the total is what was accumulated at the time, and re-deriving it would rewrite history rather than report it. Worth a separate decision. ## Tests - A session whose stored split covers only its tail (a partial `input` of 3.0 next to a journal that prices to 101) is replayed and marked complete; ignoring the mark fails it with the stale 3.0. - A session whose split is marked complete keeps it, even when its journal would replay to something else. - A session with no split at all still replays (regression guard for #732). `cargo fmt -p future-agent --check`, `cargo clippy -p future-agent --all-targets -- -D warnings`, `cargo test -p future-agent` (1795 passed).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The spend panel showed rows that contradicted the total next to them:
The rows sum to ¥1925.1451 — 229× the total. The rows exactly equal the session's token totals × one model's rates (
azure/gpt-6-astra: 70 / 350 / 7 / 90), and the total equals the same totals × another model's rates (future/deepseek-flash: 1 / 4 / 0.02).Cause
#729 made the split accumulate per request and persist it. But that only holds for requests made after it shipped: every session recorded earlier carries no split, and
get_statefell back to the old behaviour — derive the rows from the session's token totals at the current model's rates.That session ran 1015 requests on
future/deepseek-flash. Its model was then switched toazure/gpt-6-astrafor 32 seconds (no requests ran in that window) and back. Opening the panel inside that window priced its entire history at the azure rates. The total — accumulated per request since before that change — was untouched and correct.Leaving it as-is was not an option either: once such a session runs again, its split starts accumulating from that point only, so the rows would have undercounted its eight yuan of history by orders of magnitude in the other direction.
Fix
Price a legacy session's journal instead — request by request, each at the rates of the model that served it, tracking
model_changedevents — and cache the result in itssession_infoso it is replayed once per session. Requests before the first switch are billed at the session's current model; the journal records changes, not the model on each request, so that span is the one thing that cannot be recovered.The derive-from-totals fallback is gone from
get_state. A figure that contradicts the total it sits beside is worse than no figure: with nothing priced, the rows read as unset and the panel falls back to showing tokens, which is honest.For the session above, the rows now sum to ¥8.8986 against the same ¥8.4141 total — the ordinary estimate-versus-invoice gap the panel already discloses (its prices are our price list; the total is what the provider billed).
Checked against the local session database before choosing this: of the 29 sessions with a journalled model switch, 8 had a breakdown off by more than ¥0.05 and up to 229×; all 59 sessions with any spend have
usageevents, so none depends on the removed fallback.Tests
get_statereports the accumulated split rather than re-pricing totals.cargo fmt -p future-agent --check,cargo clippy -p future-agent --all-targets -- -D warnings,cargo test -p future-agent(1792 passed).