forked niri forked from okami.mom/niri

fix(bridge): emit usage on streaming completions; count cacheWrite in contextSize #60

targeting main from deleted fork:fix/bridge-streaming-usage
Merged opened by and assigned to callie.on-her.computer
targeting main from deleted fork:fix/bridge-streaming-usage

Fixes per-turn token accounting reporting zero ([tokens] +0 total=0), which broke context-size tracking and mis-gated compaction.

Root cause (bridge): the generic streaming SSE handler never emitted a usage chunk despite the client requesting stream_options.include_usage. Token counts on the 'done' event were discarded. Anything streaming through the bridge (claude+thinking) saw zero tokens.

  • add oaiSseUsageChunk() (OAI terminal chunk) and emit on 'done'. prompt_tokens = input + cacheRead + cacheWrite.
  • forward cache_write_tokens in prompt_tokens_details in both stream + non-stream paths.

Harness: applyUsage computed contextSize = input + cacheRead, omitting cacheWrite → undercounted whenever the prompt cache was created/extended. Fixed.

Tests: oaiSseUsageChunk unit + applyUsage cacheWrite regression. bridge 81/81, runtime 91/91, tsc clean.

Commits 0