Files
magpie/internal/gateway/tier_record_test.go
T
yetone 30869e35a7 usage, gateway: each request's log row says the service tier asked, the one each try was sent at, and the one the vendor said it ran at (#1784, zhenghaowen0967)
What changed
- usage.Record has tier_asked, tier_sent and tier_served, and the CSV gets
  them as its last three columns.
  - tier_asked is the client body's service_tier (Claude's speed fast reads
    "fast").
  - tier_sent is the tier the try's last request actually went out at. It
    is noted in forwardOnce after every rewrite (a fast pick's priority,
    Ultrafast turned to priority on a plan without it, buildResponses
    dropping a tier the host can't take) onto the per-attempt wireAt. So a
    handed-on try's own row has its own tier.
  - tier_served is only what the reply said: service_tier at the top level,
    in response.service_tier, or in an Anthropic usage. It is read by the
    relay sniffer and by both translated readers, and is empty when the
    reply says nothing. It is never filled in from the ask.
- Codex's own relayed sign-in (codex_backend) records the same three.
- /v1/magpie/route: Route.tier (asked), and each Try has tier and
  tierServed. The session route has asked_tier, tier, tier_served and each
  try's tier and tier_served.
- docs/reference.md (CSV columns) and gateway-routing.md describe them.

Why
zhenghaowen0967 asked that a request whose Ultrafast was sent as priority
to an account without it show that in usage, per try, without calling
the served tier confirmed unless the reply says so.

What was run
- TestTierRecordedPerTry. With Pro out, the first try is recorded
  ultrafast, the second priority, and the final record has asked=ultrafast,
  sent=priority and served empty while the fake doesn't echo. With an
  echoing fake, served is what it said ("ultrafast", and "default" when
  none was asked). It also checks the CSV columns, and the Codex relay path
  with priority.
- TestUpstreamTierShapes covers the Chat chunk, Responses
  response.completed, a Responses body, an Anthropic message_start and
  message_delta usage, null, an over-long tier and non-JSON, as well as
  bodyTier per protocol.
- Broke it by hand:
  - with forwardOnce's noteWireTier removed, TestTierRecordedPerTry goes
    red (route tries' tier empty, TierSent empty);
  - with the sniffer's Tier line removed, TestUpstreamTierShapes goes red,
    and so does the Codex relay case of TestTierRecordedPerTry.
- go test -tags nogui ./internal/gateway ./internal/usage ./internal/cli;
  ledger_test's CSV expectations gain the three empty columns.
- gofmt -l; go vet on darwin; GOOS=linux and GOOS=windows go vet -tags
  nogui.

Not done
- The served-tier fixtures are built from the shapes OpenAI and Anthropic
  document, not from saved replies. Whether chatgpt.com's Codex backend
  echoes service_tier wasn't tried against a real account.
- Bridges that don't go through forwardOnce (the Claude subscription,
  Cursor's built-in) leave tier_sent empty.
- There is no price multiplier for priority/ultrafast and no GUI column.
2026-10-13 14:22:53 +08:00

50 lines
1.8 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #1784: the served tier is read only where a reply says it, in the shapes
// OpenAI's and Anthropic's APIs document, and never taken from the ask.
func TestUpstreamTierShapes(t *testing.T) {
for _, c := range []struct{ body, want string }{
{`{"id":"c1","object":"chat.completion.chunk","service_tier":"default","choices":[]}`, "default"},
{`{"type":"response.completed","response":{"id":"r1","service_tier":"priority","usage":{}}}`, "priority"},
{`{"id":"r1","object":"response","service_tier":"flex"}`, "flex"},
{`{"type":"message_start","message":{"id":"m1","usage":{"input_tokens":3,"service_tier":"priority"}}}`, "priority"},
{`{"type":"message_delta","usage":{"output_tokens":3,"service_tier":"standard"}}`, "standard"},
{`{"type":"response.completed","response":{"id":"r1","usage":{}}}`, ""},
{`{"service_tier":null}`, ""},
{`{"service_tier":"` + strings.Repeat("x", 40) + `"}`, ""},
{`not json "service_tier"`, ""},
} {
if got := upstreamTier([]byte(c.body)); got != c.want {
t.Errorf("%s: %q, want %q", c.body, got, c.want)
}
}
s := &usageSniffer{}
s.parse([]byte(`{"type":"message_start","message":{"usage":{"input_tokens":3,"service_tier":"priority"}}}`))
if s.u.Tier != "priority" {
t.Errorf("sniffer: %+v", s.u)
}
for _, c := range []struct {
proto provider.Protocol
body, want string
}{
{provider.Anthropic, `{"speed":"fast"}`, "fast"},
{provider.Anthropic, `{"service_tier":"standard_only"}`, "standard_only"},
{provider.Responses, `{"service_tier":"ultrafast"}`, "ultrafast"},
{provider.Chat, `{"service_tier":1}`, ""},
{provider.Gemini, `{"service_tier":"priority"}`, ""},
} {
if got := bodyTier(c.proto, []byte(c.body)); got != c.want {
t.Errorf("%s %s: %q", c.proto, c.body, got)
}
}
}