fix(review): contract-correct live token emissions (live-review findings)

main

Commit

peyton-alt2mo ago

A live full-crew dogfood review of this PR (claude x2 + codex, all request-changes) converged on the same contract violations; every finding is reproduced by a failing test first.

Claude: mid-run emissions were per-API-call context snapshots, but the Tokens contract is cumulative running totals. The parser now sums input once per unique message id (the same usage block repeats on every content-block envelope of one call), so mid-run values are true running totals converging exactly to the result aggregate — the fixture pins 56277 -> 112903 -> 169637 == result. Duplicate per-envelope emissions are gone; the false 'consumers render input-only Tokens differently' comment is corrected.

Codex: the rollout tailer was stopped in a defer that ran after the terminal Tokens/Finished sends, so a lagging tailer tick could emit after Finished and make the final recorded totals nondeterministic (reproduced 13/20 before the fix). The tailer is now stopped and awaited before every terminal emission. Single-source authority: rollout token_count totals are session-cumulative while turn.completed usage is per-turn scale, so per-turn stdout emissions are suppressed once the tailer has emitted (it remains the fallback when no rollout is found). The unconditional post-loop backstop could only ever emit Tokens{0,0} and erase the tailer's genuine totals; it is now gated on non-zero usage and no tailer values. The tailer also does a final catch-up drain on stop, stops (with a debug log) on persistent non-EOF read errors instead of silently re-polling, and logs rollout-discovery exhaustion.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com Entire-Checkpoint: 01KWYN394Q3PX5FRV70DGM53ZW

Checkpoints

test(review): pin that codex $name skills survive the legacy repair + native invocation

Claude CodeFable 5
View session
Checkpoint 1