mirror of
https://github.com/block/buzz.git
synced 2026-11-24 18:21:59 +00:00
buzz-agent emitted its `usage_update` notification in exactly one place: after `ctx.run()` returned. Until then a turn's token counters lived only in the prompt task's stack frame. A turn that was killed mid-flight therefore reported nothing at all — the provider had already billed every round it completed, and no consumer ever saw any of it. That is not a corner case for anything that terminates a turn on a clock. Measured on a long-horizon benchmark run that relaunches its agent between phases, 97% of the recorded usage rows came back all zeros, and the run's recorded cost was $79.23 against the $485 the provider's own ledger billed for the same model over the same window — a 5.9x undercount, concentrated on the seat being relaunched. Nothing was truncated after the fact; the tokens were never captured in the first place. The run loop now emits a session-cumulative `usage_update` after every usage-bearing provider response, so an interrupted turn has reported everything but its single in-flight request. Emitting more than once per turn is already part of the contract — buzz-acp's UsageTracker advances its committed baseline only at publish time, and goose behaves the same way, which is why the tracker was written to tolerate it. The turn-start session baseline is snapshotted into RunCtx so the mid-turn figure stays session-cumulative rather than turn-local; a turn-local number would be discarded by a high-water-mark consumer and lose the turn entirely. It is taken by value rather than as a handle because the loop reports once per round, and taking the sessions lock on each of those would serialise concurrent sessions behind one another's provider round-trips. Both call sites now build the payload through one helper so their wire shapes cannot drift — a divergence there would present as tokens silently vanishing, which is the failure this reporting exists to prevent. A SIGTERM handler was the obvious shape and does not work: at signal time the counters are not anywhere a handler could reach. Making usage durable during the turn is what fixes it. Signed-off-by: Atish Patel <atish@squareup.com> Co-authored-by: Claude Code <noreply@anthropic.com>