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

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

d429662→main· peyton-alt·1w ago·5 files·+381 added/-129 removed

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

Sessions

01KWYN394Q3PX5FRV70DGM53ZWView transcript

Changes

5

55 unmodified lines  
...  
// On a scanner error (torn stream), emits RunError then Finished{Success:false}.  
//  
// Live-token semantics: Claude's assistant envelopes carry a usage snapshot  
...  
// output count). Surface input only — consumers render input-only Tokens differently from  
// finalized {In, Out} pairs. The final tally comes from the  
// `result` envelope below.  
...  
}`

## Summary of Findings

Tokens{In: <sum>, Out: 0} were emitted on each assistant envelope to show context size growing across multi-turn runs. The true output is only surfaced on `result` (aggregate across all turns in the run). The running sum converges to the `result` aggregate, which is emitted last with the true {In, Out}.