Files
magpie/internal/plugin/testdata/pi/extensions
yetone a64b3a8d46 plugin: a pi provider's message_start tells the prompt's usage pi's start named, so Claude Code's context meter isn't 0 until the turn ends (#1887)
What: pi.js wrote message_start with usageOf() and no argument, so every
field was 0 until message_delta. Claude Code's context meter reads the
usage the assistant message has so far, and a turn interrupted before
message_delta was stored with 0. message_start now carries the vendor's
prompt (input, cache read, cache write) and the output pi's start named
(startUsage): from pi's start event's partial.usage, as the issue
describes, or, as pi-ai's anthropic-messages pushes start before it reads
the vendor's message_start (start's usage is still 0 then), from the
partial message the events after it share. That message is pi's own,
still being written (by the time magpie reads it pi may have pushed the
whole turn), so only its prompt is taken from it, never its output.
message_delta is unchanged: the whole turn's cumulative usage from done.

Siblings, the gateway's translators to Anthropic's stream:
- Gemini / Code Assist (codeassist.go): usageMetadata rides on the first
  chunk, but KStart was emitted before reading it. KStart now carries the
  prompt (input, cached); output stays 0 there.
- Chat Completions (chat.go): most streams count only at their end, so
  nothing to tell; a stream that counts on its first chunk now has its
  prompt on KStart.
- Responses: response.created has no usage. Anthropic-to-Anthropic and the
  Claude subscription already carry it. Cursor, Kiro, Devin, Qoder,
  Command Code send KStart before any usage is known.

Ledger: no double count. The usage sniffer and the collector both take
each field's latest non-zero value (Usage.add replaces, it doesn't sum),
and continuation/search take a try's KStart usage as its own (this =
ev.Usage) and KUsage replaces it. message_delta's input equals
message_start's, which Anthropic's clients also take as the same count.

Run (Go 1.26.8, darwin):
- TestPiPackage with a fake pi stream as the issue's (start with
  partial.usage input 11, cache 3/2, output 0, text, done with output 7):
  message_start 11/3/2/0, message_delta 11/3/2/7; and pi-ai's order
  ("late": start with zero usage, then the shared message counted):
  message_start 11/3/2/0. Red with message_start back on usageOf(), and
  the late case red when only start's usage is read.
- TestPromptUsageOnMessageStart (Gemini, chat counting per chunk, chat
  counting at the end): red with codeassist.go and chat.go stashed.
- go test ./internal/plugin and ./internal/gateway in full (-timeout 28m;
  the default 10m timed out under load average ~20). The disk filled
  during the run (3.7G free, not this change's): every failure was
  ENOSPC, and each of them (37 gateway, TestFakePlugin) passed on rerun.
- gofmt -l; go vet for darwin, linux and windows on both packages.
Not tried against a real pi vendor or Claude Code's meter.

Reported-by: guoqchen1001
2026-10-13 10:29:31 +08:00
..