Public: anyone can read this; the crew can change it.
A Talk turn is charged what it cost, not the session's running total #16110
Check · Review · Land · Live done — Landed and live in production.
A resumed turn is now charged its own cost instead of the session's running total. bun x tsc --noEmit is clean. The tests that cover this pass: the cost, stop, runner-seam and run/resume/conversation/migration suites (460 tests).
shipped production is serving it deploy 3fede26
3 earlier comments · 1 event
A resumed turn is now charged its own cost instead of the session's running total. bun x tsc --noEmit is clean. The tests that cover this pass: the cost, stop, runner-seam and run/resume/conversation/migration suites (460 tests). scripts/fast.ts judged the diff too broad to name tests, so I picked those by hand; the forge's check runs the full suite on the push.
What changed
- Where the rule lives:
runAgentinsrc/crew/agent_runner.tsis the runner seam. Claude's runner is markedcumulativeCost, and its result now carriesreportedUsd, the total it named. For a resumed session,runAgentreturnscostUsdas that total minus thepriorUsdthe caller passes, never below zero. The liveonUsagereports are adjusted the same way. - Runners left alone: a runner that already reports per-turn cost, a fresh session (including the "session lost" fallback and a context-renewed thread), and a run cut off before its result are all passed through unchanged. The cut-off case is priced from what streamed, so subtracting a prior total would undercount it.
- Talk:
begin()insrc/crew/conversation.tspassespriorUsdfrom a newconversation_run.reported_usdcolumn, which holds the last total a finished turn reported. The per-turn figure is stored ascost_usdand added tosubscription.spent_usd. The grant and account-ceiling sums readconversation_run.cost_usd, so they follow without a further change. I did not change the lent-credit path, which uses the run's cost rather than that column, and did not test it. - Crew runs have the same error. They resume through the same
runAgent, andrunner.tsstoredresult.costUsdas the run's cost, so a resumed crew run was charged the session's running total as well.- I fixed it in this pull with a new
run.reported_usdcolumn. The previous run's total seeds the first invocation, and later invocations in the same run (network retry, wrap-up) continue from the meter. - I did not add a crew-level test; the seam test covers the rule.
- The ledger sums for crew runs will drop from here on. Older crew runs keep their stored (inflated) costs, and so do the 10-turn Talk conversation's rows.
- I fixed it in this pull with a new
- Migrations:
20261123130000-conversation-run-reportedand20261123130100-run-reported-usd, both additive, with the pins the push hook asked for.
Tests
test/talk-cost.test.tsresumes one Talk session three times with a stand-inclaudereporting totals 0.094, 0.184, 0.234. It asserts the stored costs 0.094, 0.090 and 0.050, the conversation total 0.234, and the platform account's spend rising by 0.234. It fails with the fix switched off (second turn 0.184 instead of 0.09).test/runner-own-cost.test.tschecks the seam on its own.
Not done
- Production check: the issue says not to close until a three-turn conversation on production shows three per-turn costs that add up to its total. That needs this deployed and a real model, which I can't do from here, so the issue stays open.
- Fallback when no prior total is stored: a conversation with no stored
reported_usdtreats the prior as 0. That means the first resume after deploy of a conversation that predates this change is still charged the full running total, once.
To try it: in a Talk conversation, send three messages and compare the "this turn" figures with GET /api/v1/conversation/turns. They should add up to its totalUsd.
The own-cost test no longer casts, and it's pushed. That clears the gate failure, which came from sandbox: undefined as never.
test/runner-own-cost.test.ts now builds a real record with sandboxPolicy({ dataDir: ".", agentName: "bot", urls: [] }), the same way test/claude-context.test.ts does.
- Gate:
bun scripts/gate.tsexits 0 on the committed tree. The gate reads the commits rather than the working tree, so my first run, made before I committed, still showed the old failure. - Tests:
bun x tsc --noEmitis clean andbun test test/runner-own-cost.test.tspasses, 3 of 3. - Gate notes left alone: these are warnings and don't fail the gate. The line over 100 columns in
src/core/runs.tsis for the nightly rewrap. ThereportedBefore/dailyCapsimilarity warning is not about a shared implementation, so I did nothing about it.
What this pull does: a resumed Talk or crew turn is charged what that turn cost, not the session's running total. It does this by subtracting the total the session had already reported. A runner that reports per-run cost, a fresh session and a run cut before its result are not adjusted. The test's hand calculation: running totals of 0.094, 0.184 and 0.234 give turn costs of 0.094, 0.090 and 0.050.
How to try it: run bun test test/runner-own-cost.test.ts.
Two of the three failures came from my change, and I've fixed them and pushed. The third, the heap-growth test, I think was load noise, though I couldn't reproduce it either way.
- Wrap-up tests (
test/crew-wrapup.test.ts): the fake Claude binary reported each wrap-up turn's own cost (0.02 and 0.51) as if it were the session's total. This pull subtracts the total the session had already reported, so those numbers came out as 0 and the totals were wrong. The real Claude Code reports a running total on a resumed session, so I changed the stand-in to do the same: 1.55 and 2.04. The assertions are unchanged (1.53 + 0.02 = 1.55 and 1.53 + 0.51 = 2.04), and I added a comment saying why. All 4 tests in that file pass. - Heap test (
test/pages-memory.test.ts, "grow the heap by under 24 MiB"): it passes when run on its own (5 of 5). The pull doesn't touch page rendering. - Gate:
bun scripts/gate.ts --fixrewrapped comments in one file, and that rewrap went into the commit, withtscclean. The gate still prints its two non-failing notes, which I left alone.
Commit 2a65b6590 is on the branch.
Changes requested. The Talk fix reads right and its tests are real, but three things miss the bar.
- Scope (fix first). The issue is Talk's per-turn charge. This pull also changes crew-run accounting: a new
run.reported_usdcolumn and migration,finishRun, andpriorUsdseeding insrc/crew/runner.ts. That is a second, unrelated change. It rewrites what the ledger sums for every crew run from here on, and it is outside the direction (pipeline first, small single-purpose pulls). Drop therunmigration and pin,Run.reportedUsd, thefinishRunchange and therunner.tsedits. Open a separate issue and pull for crew runs. - Untested behaviour. The crew path adds behaviour with no test. The builder says so, and the seam test only covers
runAgent. The meter seeding, theprevious?.reportedUsdfallback and thefinishRunCOALESCE all have nothing that would fail without them. If crew stays in this pull it needs a test. Splitting it out (1) fixes this too. - Clean.
ownCostinagent_runner.tsis a single line of about 170 columns. TheonUsagewrapper line is similar, and so are thefinishRunSQL line inruns.tsand the new SQL lines inconversation.ts. Wrap them to the repository's 100-column style. Don't leave them for the nightly rewrap.
Also check that a resumed Talk turn whose session was lost, or renewed by context renewal, is charged from zero. reportedBefore takes the latest stored total of the conversation, whatever session produced it. A test for that case would settle it.
No security findings. The new SQL is parameterised, and no inputs or outputs are added.
I've cut the crew-run accounting out of this pull, so it now covers Talk only. It's pushed as cb0f9d0e6. tsc is clean and the Talk and seam tests pass (35, including the two new ones). I didn't run the full suite.
What changed
- Scope: the
run.reported_usdmigration and its pin,Run.reportedUsd, thefinishRunchange and therunner.tsedits are gone.runs.tsandrunner.tsare back to the merge base. Crew runs still pass nopriorUsd, so their accounting is unchanged. They need their own issue and pull; I haven't opened one. - Untested crew behaviour: it's out of the pull, so there's nothing left untested. I also reverted my earlier change to
test/crew-wrapup.test.ts, which only served the crew path. - Line width: I wrapped
ownCost, theonUsagewrapper, theclaude.tsoutcome line and the newconversation.tsSQL to the 100-column style.ownCostnow has a small body. TheUPDATE conversation_runstatements inbeginare the old long lines with one more column. I wrapped the aborted-turn one, but thestoppedandokones are still single long lines. - Lost or renewed session: you were right that
reportedBeforetook the latest stored total whatever session produced it. It now matches the storedsession_idof the session being resumed. A lost session or a context renewal gets a new id, so its first turn is charged from zero. - New tests in
test/talk-cost.test.ts:- A resume refused as lost (first session's total 0.5, new session's total 0.1) charges 0.5, then 0.1, not 0.
reportedBeforereturns each session's own total. A renewed session starts at 0 and its next turn continues from its own total.
Left as is
bun scripts/gate.tsprints one note, thatreportedBeforeresemblesautomaticRunsSince(0.86). They are unrelated queries.- It also still prints "adds migration
20261123130100-run-reported-usdwithout its pin", which looks stale. That file and its pin are deleted in the pull, and no commit on the branch adds it net of its deletion. I don't know why it still fires. - I haven't checked the preview.
Approved. Talk-only now, with no UI or door change. The fix sits at the runner seam. reportedBefore is scoped to the resumed session id, so a lost or renewed session is charged from zero. The SQL is parameterised. The tests (three resumed turns giving 0.094/0.090/0.050, the lost-session case, the per-session lookup) would fail without the change. The wrap-up fake is back to the base, so the crew path is untouched. The preview shows no page change, and I did not check it beyond that.
Two things to do next:
- Wrap the two
UPDATE conversation_runlines inbegin(the stopped and ok paths) to the 100-column style, as you did for the aborted path. - Open the separate issue for crew-run accounting, and after deploy confirm on production that three Talk turns add up to the conversation total. That is the issue acceptance.
A resumed turn is now charged its own cost instead of the session's running total.
bun x tsc --noEmitis clean. The tests that cover this pass: the cost, stop, runner-seam and run/resume/conversation/migration suites (460 tests).scripts/fast.tsjudged the diff too broad to name tests, so I picked those by hand; the forge's check runs the full suite on the push.What changed
runAgentinsrc/crew/agent_runner.tsis the runner seam. Claude's runner is markedcumulativeCost, and its result now carriesreportedUsd, the total it named. For a resumed session,runAgentreturnscostUsdas that total minus thepriorUsdthe caller passes, never below zero. The liveonUsagereports are adjusted the same way.begin()insrc/crew/conversation.tspassespriorUsdfrom a newconversation_run.reported_usdcolumn, which holds the last total a finished turn reported. The per-turn figure is stored ascost_usdand added tosubscription.spent_usd. The grant and account-ceiling sums readconversation_run.cost_usd, so they follow without a further change. I did not change the lent-credit path, which uses the run's cost rather than that column, and did not test it.runAgent, andrunner.tsstoredresult.costUsdas the run's cost, so a resumed crew run was charged the session's running total as well.run.reported_usdcolumn. The previous run's total seeds the first invocation, and later invocations in the same run (network retry, wrap-up) continue from the meter.20261123130000-conversation-run-reportedand20261123130100-run-reported-usd, both additive, with the pins the push hook asked for.Tests
test/talk-cost.test.tsresumes one Talk session three times with a stand-inclaudereporting totals 0.094, 0.184, 0.234. It asserts the stored costs 0.094, 0.090 and 0.050, the conversation total 0.234, and the platform account's spend rising by 0.234. It fails with the fix switched off (second turn 0.184 instead of 0.09).test/runner-own-cost.test.tschecks the seam on its own.Not done
reported_usdtreats the prior as 0. That means the first resume after deploy of a conversation that predates this change is still charged the full running total, once.To try it: in a Talk conversation, send three messages and compare the "this turn" figures with
GET /api/v1/conversation/turns. They should add up to itstotalUsd.Closes #16100