Skip to content

fix(cost): price a legacy session's spend from its journal, not its totals - #732

Merged
huichen merged 2 commits into
mainfrom
fix/cost-split-backfill
Sep 20, 2026
Merged

huichen merged 2 commits into
mainfrom
fix/cost-split-backfill

Conversation

@huichen

@huichen huichen commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

Problem

The spend panel showed rows that contradicted the total next to them:

amount
输入 1,724,048 ¥120.6834
输出 511,483 ¥179.0191
缓存读取 232,206,080 ¥1,625.4426
合计 ¥8.4141

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_state fell 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 to azure/gpt-6-astra for 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_changed events — and cache the result in its session_info so 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 usage events, so none depends on the removed fallback.

Tests

  • A legacy session (totals, no recorded split) whose journal has one request on a model priced ¥1/M and another on one priced ¥100/M replays to ¥101, not the ¥200 its totals would give at the current model. Zeroing the replay (the old behaviour) fails it.
  • A session with a recorded split keeps it, even when its journal would replay to something else — so a reload never overwrites live accumulation.
  • get_state reports 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).

…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
huichen enabled auto-merge (squash) September 20, 2026 01:38
@huichen
huichen merged commit 872d056 into main Sep 20, 2026
12 checks passed
@huichen
huichen deleted the fix/cost-split-backfill branch September 20, 2026 01:54
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).
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.

1 participant