Skip to content

feat(#1527): cost per run where a user looks — one line in the run summary, cost in the run log and sequant logs, from the metrics record, labelled "SDK estimate" - #1597

Merged
admarble merged 7 commits into
mainfrom
feature/1527-feat-cli-cost-per-run-where-a-user-looks-one-line-
Oct 27, 2026
Merged

admarble merged 7 commits into
mainfrom
feature/1527-feat-cli-cost-per-run-where-a-user-looks-one-line-

Conversation

@admarble

@admarble admarble commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Issue #1527 is implemented and pushed on branch feature/1527-feat-cli-cost-per-run-where-a-user-looks-one-line-. The orchestrator opens the PR, so none is open from here.

The run summary now prints cost $6.45 (SDK estimate) · 69,641 tokens under the tally, followed by a muted line, SDK estimate, not a billing statement. sequant logs <run> prints the same line, and the run log carries an optional run-level cost of { usd, tokens } with version still 1. A run with no record prints cost unrecorded, never $0.00. A dry run prints no cost line.

How it's built. The spec found that the run log is written before metrics are recorded, so the cost didn't exist yet when the log was finalized. I moved the totals into a new RunOrchestrator.collectUsage and call it once before the log is written. The log, the summary and metrics.json all take their number from that one total. recordMetrics still works with its old six arguments, because existing tests call it that way.

AC evidence

  • AC-1: run-renderer.cost.test.ts has 4 tests, all passing: cost and tokens, no record, tokens only, and the dry-run case.
  • AC-2: grep -c 'cost: ' on run-log-schema.ts gives 1, and grep -c 'version: z.literal(1)' gives 1.
  • AC-3: logs.cost.test.ts has 3 tests, all passing: with cost, without cost, and schema back-compat.
  • AC-4: the costUSD grep over src/lib/cli-ui and src/commands/logs.ts gives 0, and cost appears in run-renderer-types.ts.
  • AC-5: SDK estimate appears in src/lib/cli-ui/format.ts, not in run-renderer.ts. The AC glob counts it, but the spec's tightened grep (run-renderer.ts ≥ 1) would not. I did not edit the AC text.
  • AC-6: docs/reference/run-command.md has a new Cost section. The CHANGELOG has an entry.

Checks: npm run build and npm run lint are clean. The affected set was 132 test files. I ran it in 3 shards, then re-ran the failing directories after fixing them: src/lib/workflow/run-orchestrator*, src/lib/cli-ui and the tautology detector all pass. I did not re-run all three shards in full after the fixes.

Two existing tests changed

Not done at exec time, done since (see "Runner verification" below)

  • The parity test is written: run-orchestrator.phase-policy.test.ts asserts the log's cost equals the metrics record's costUSD and tokensUsed, and that the hook-file fallback is read once for both.
  • The spec's Open Questions are settled on the issue (owner's word, 2026-10-26): AC-4 pairs the zero-reader grep with a positive grep on run-renderer-types.ts, AC-5 counts format.ts only, and AC-4's parenthetical names the orchestrator's usage helper.
  • The abort-signal finalize path writes cost for the issues that had finished, through costSoFar on the lazily read orchestrator; both execution paths record, and the parallel push is mutation-verified.

Still true

  • Weakest part: the "Cost" doc says a separate review session is not part of the figure. That follows from the number being the sum of the run's phase usage rows, but I did not test it against a real run.

Mutation records for the PR body

  • Mutation-verified: AC-1 — cost unrecorded branch in formatCostLine returns cost $0.00; src/lib/cli-ui/run-renderer.cost.test.ts failed; restored.
  • Mutation-verified: AC-3 — deleted the cost line from displayLogSummary; src/commands/logs.cost.test.ts failed; restored.

I did not run the parseMutationMarkers check from /qa §6i on these markers.

Next: /qa on #1527.

Fixes #1527

Runner verification (2026-10-27, final head 9231b67)

Branch after the sequant_run passes: QA 1 on 0ddfe2467 (AC_MET_BUT_NOT_A_PLUS, four findings), then owner-directed fixes 58b245b36 (MCP sequant_logs carries cost; cost on the signal path through costSoFar; toRunCost as the one derivation; parity test log vs metrics; RunResult JSDoc), 835cf11ac (caveat line only under a recorded cost, in the summary as in sequant logs; abort-path test; MCP description names the estimate), merge of main d6daedff5, c92b9cec0 (QA 2: the parallel path records finished issues too), 9231b67d3 (QA 3: costSoFar through the real execute() on both paths). QA 4 was NEEDS_VERIFICATION only because CI had not reported; QA 5 ran after test and canary passed (see the verdict comment on the issue). QA 5 on 9231b67d3: READY_FOR_MERGE, 6/6 met, no gaps.

Issue text tightened with the owner's word (2026-10-26): AC-4 pairs the zero-reader grep with grep -c 'cost' src/lib/cli-ui/run-renderer-types.ts → ≥ 1; AC-5 counts src/lib/cli-ui/format.ts only; AC-4's parenthetical names the orchestrator's usage helper.

ACs re-run on 9231b67d3:

AC Command Result
AC-1 npx vitest run src/lib/cli-ui/run-renderer.cost.test.ts 4 passed, 0 failed
AC-2 grep -cE 'cost: ' run-log-schema.ts; grep -c 'version: z.literal(1)' 1; 1
AC-3 npx vitest run src/commands/logs.cost.test.ts 4 passed, 0 failed
AC-4 zero-reader grep; grep -c 'cost' run-renderer-types.ts 0; 2
AC-5 grep -c 'SDK estimate' src/lib/cli-ui/format.ts 3
AC-6 grep -c 'SDK estimate' docs/reference/run-command.md 2

Mutations reproduced on 9231b67d3: AC-1 (cost unrecorded branch returns cost $0.00) fails exactly prints cost unrecorded when the run has no record, 1 failed of 4; AC-3 (cost line deleted from displayLogSummary) fails prints the run's cost and tokens and its sibling, 2 failed of 4. Both restored. The costSoFar execute-path test was mutation-verified in 9231b67d3's commit: deleting the parallel push fails exactly the parallel case.

Checks: tsc and eslint clean on the branch; freeze grep (plugin/, .claude/skills/, templates/skills/) 0 files; session-link and Co-Authored-By grep 0 in commits and diff; 0 behind main. CI on 9231b67d3: test failed once on ready-gate.test.ts > selects the aider driver (5,441 ms against a 5 s timeout, outside this diff, 3/3 local passes), re-run attempt 2 passed; canary passed; knip-advisory is the pre-existing 207-export list with none of this branch's symbols.

Not in this PR: the MCP server pin is [email protected], which predates #1520 and this change, so sequant_run over MCP shows neither the Read-first comment nor the cost until a release; #1529's AC-2 (grep -c 'SDK estimate' → 1) should match the cost line itself (proposed: grep -cE 'cost \$[0-9]+\.[0-9]{2} \(SDK estimate\)' "$SUMMARY" → 1), owner's call when #1529 launches.


🤖 Generated by sequant run

One total computed before the log is written; summary, log and metrics share it. Labelled SDK estimate; absent reads cost unrecorded.

Entire-Checkpoint: 705275ea0e04
…y snapshots and the worktree-anchor gate region

Entire-Checkpoint: 9bcde7444a9d
…rity test for log vs metrics; RunResult JSDoc back in place

QA follow-ups on PR #1597. The MCP logs tool names log.cost in its field-by-field JSON. toRunCost is the one derivation of the run log's cost block from collectUsage's totals, and the signal-path finalizer takes the cost of the issues that had finished through the lazily read orchestrator ref. The parity test pins that the log's cost equals the metrics record's costUSD and tokensUsed, and that the hook-file fallback is read once for both.

Entire-Checkpoint: f21a3dab0b5b
…ary as in sequant logs; abort-path cost test; MCP description names the estimate

The summary printed the SDK-estimate caveat under cost unrecorded, where there is no figure to qualify, while sequant logs printed it only under a figure. Both now gate it on a recorded cost; the two NonTTY snapshots lose that line. The shutdown wiring test covers the signal-path cost through the lazily read orchestrator ref, and the sequant_logs tool description says what its cost field is.

Entire-Checkpoint: e2b8c7c2a17a
…oo; parallel is the default

QA 2 on PR #1597: executeSequential pushed into completed and executeParallel did not, so an aborted default run logged no cost for the issues that had finished, against what the schema JSDoc says.

Entire-Checkpoint: 0928797f5b3e
…parallel push is mutation-verified

QA 3 on PR #1597: the parallel-path recording had no regression test. Drives execute() with runIssueWithLogging mocked, once sequential and once parallel, and asserts costSoFar sums what returned. Deleting the parallel push fails exactly the parallel case.

Entire-Checkpoint: d1255812fb7e
@admarble
admarble merged commit d6d66a9 into main Oct 27, 2026
16 of 19 checks passed
@admarble
admarble deleted the feature/1527-feat-cli-cost-per-run-where-a-user-looks-one-line- branch October 27, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant