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
TestPiPackage installed pi the way ensurePi does, `bun add
@earendil-works/pi-coding-agent` at npm's latest, so it ran on whatever
pi's packages were that minute. pi-codemode 1.2.0 (2026-10-12 23:00:01Z)
dropped MCP_TYPESCRIPT_PREAMBLE and mcpStructuredContentSchema, which
pi-coding-agent 1.0.x and 1.1.0 import from it; their ^1.x range takes
1.2.0. pi-coding-agent 1.2.0 (no longer importing them) came out at
23:03:25Z. A full run that reached TestPiPackage in between got
pi-coding-agent 1.1.0 + pi-codemode 1.2.0 and pi didn't load; the run
alone came after. Nothing is shared between tests here (each has its
own plugins folder, and bunCommand puts Bun's cache under the test's
HOME), so "only in the full run" was the clock, not the suite.
The test now writes testdata/pi-lock's package.json and bun.lock (pi
1.2.0, every package at its npm integrity) into the plugins folder and
runs `bun install --frozen-lockfile`; Add then finds pi there. The
"pi is installed with a pi package" half moves to TestEnsurePi, with bun
stubbed: installed once for a pi package, not again with pi there, not
for an OpenCode plugin. The BUN_INSTALL_CACHE_DIR the test set did
nothing (bunCommand sets its own after it) and is gone.
Users: magpie installs pi at latest (1.2.0 with 1.2.0 now), and bun
keeps a plugins folder's lock when other plugins are added, so a folder
installed before the 1.2.0 release keeps 1.1.0 with 1.1.0. Only a pi
install that resolved in those ~3-8 minutes (CDN metadata cache) got the
broken pair. No magpie change.
Run:
- with the lock swapped for pi-coding-agent 1.1.0 (codemode resolved to
1.2.0): TestPiPackage fails with the issue's exact "Export named
'MCP_TYPESCRIPT_PREAMBLE' not found"; with 1.1.0 + codemode 1.1.0 and
with the 1.2.0 lock committed it passes
- TestEnsurePi fails with ensurePi's "pi is there" check removed
- go test ./internal/plugin/ -count=1 three times: ok (140s, 121s, 112s)
- go test ./internal/plugin/ -run 'TestPiPackage|TestEnsurePi' -count=5: ok
- go test -tags nogui ./internal/...: ok
- gofmt -l; go vet darwin, linux -tags nogui, windows: clean