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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

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).

Notes

shipped production is serving it deploy 3fede26

opened this

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: runAgent in src/crew/agent_runner.ts is the runner seam. Claude's runner is marked cumulativeCost, and its result now carries reportedUsd, the total it named. For a resumed session, runAgent returns costUsd as that total minus the priorUsd the caller passes, never below zero. The live onUsage reports 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() in src/crew/conversation.ts passes priorUsd from a new conversation_run.reported_usd column, which holds the last total a finished turn reported. The per-turn figure is stored as cost_usd and added to subscription.spent_usd. The grant and account-ceiling sums read conversation_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, and runner.ts stored result.costUsd as 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_usd column. 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.
  • Migrations: 20261123130000-conversation-run-reported and 20261123130100-run-reported-usd, both additive, with the pins the push hook asked for.

Tests

  • test/talk-cost.test.ts resumes one Talk session three times with a stand-in claude reporting 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.ts checks 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_usd treats 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.

Closes #16100

3 earlier comments · 1 event
commented

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: runAgent in src/crew/agent_runner.ts is the runner seam. Claude's runner is marked cumulativeCost, and its result now carries reportedUsd, the total it named. For a resumed session, runAgent returns costUsd as that total minus the priorUsd the caller passes, never below zero. The live onUsage reports 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() in src/crew/conversation.ts passes priorUsd from a new conversation_run.reported_usd column, which holds the last total a finished turn reported. The per-turn figure is stored as cost_usd and added to subscription.spent_usd. The grant and account-ceiling sums read conversation_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, and runner.ts stored result.costUsd as 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_usd column. 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.
  • Migrations: 20261123130000-conversation-run-reported and 20261123130100-run-reported-usd, both additive, with the pins the push hook asked for.

Tests

  • test/talk-cost.test.ts resumes one Talk session three times with a stand-in claude reporting 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.ts checks 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_usd treats 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.

commented

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.ts exits 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 --noEmit is clean and bun test test/runner-own-cost.test.ts passes, 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.ts is for the nightly rewrap. The reportedBefore/dailyCap similarity 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.

commented

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 --fix rewrapped comments in one file, and that rewrap went into the commit, with tsc clean. The gate still prints its two non-failing notes, which I left alone.

Commit 2a65b6590 is on the branch.

Changes requested — before the latest push

Changes requested. The Talk fix reads right and its tests are real, but three things miss the bar.

  1. Scope (fix first). The issue is Talk's per-turn charge. This pull also changes crew-run accounting: a new run.reported_usd column and migration, finishRun, and priorUsd seeding in src/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 the run migration and pin, Run.reportedUsd, the finishRun change and the runner.ts edits. Open a separate issue and pull for crew runs.
  2. Untested behaviour. The crew path adds behaviour with no test. The builder says so, and the seam test only covers runAgent. The meter seeding, the previous?.reportedUsd fallback and the finishRun COALESCE 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.
  3. Clean. ownCost in agent_runner.ts is a single line of about 170 columns. The onUsage wrapper line is similar, and so are the finishRun SQL line in runs.ts and the new SQL lines in conversation.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.

commented

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_usd migration and its pin, Run.reportedUsd, the finishRun change and the runner.ts edits are gone. runs.ts and runner.ts are back to the merge base. Crew runs still pass no priorUsd, 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, the onUsage wrapper, the claude.ts outcome line and the new conversation.ts SQL to the 100-column style. ownCost now has a small body. The UPDATE conversation_run statements in begin are the old long lines with one more column. I wrapped the aborted-turn one, but the stopped and ok ones are still single long lines.
  • Lost or renewed session: you were right that reportedBefore took the latest stored total whatever session produced it. It now matches the stored session_id of 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.
    • reportedBefore returns 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.ts prints one note, that reportedBefore resembles automaticRunsSince (0.86). They are unrelated queries.
  • It also still prints "adds migration 20261123130100-run-reported-usd without 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

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:

  1. Wrap the two UPDATE conversation_run lines in begin (the stopped and ok paths) to the 100-column style, as you did for the aborted path.
  2. 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.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs