Files
magpie/internal/gateway/tier_record_test.go
T
yetone d088030315 gateway: a long Codex request's facts, tier and metadata skip the walks over its conversation (#3394)
A Codex Responses request carries the whole conversation, and the gateway
walked it several times per request for what it reads at the top level:

- requestSessionMetadata searched the body for "client_metadata", which
  Codex sends last, before asking topMember. A long body now asks the
  memoized split straight away.
- bodyTier asked gjson for speed and service_tier, walking the body each
  time; a long body now asks topMember too (topValue).
- userWords ran its reminder and "## My request" regexes on every text;
  each now runs only when its marker is present.
- readFacts (turn count, work folder) ran the full parse. It now parses
  in facts mode (readResponses): views over the raw bytes, no parts for
  assistant replies, and tool outputs as bare ToolResult parts, which is
  all turnIn and workPlace read.

Bench (a fake ChatGPT backend, ~2MB Codex body, client_metadata last),
per request: 31.7ms, 24.2MB, 71.2k allocs on origin/main; 19.2-21.4ms,
17.2MB, 59.6k allocs with these and the provider commit after it.

Real account: a sandbox HOME with only the deepseek provider, 310KB
Responses bodies to deepseek/deepseek-flash through `magpie serve`:
base 15ms process CPU per request (19/20 ok), new 13ms (20/20, then
8/8 again).

TestResponsesFactsAsTheWholeParse compares readFacts with the full
parse's turnIn and workPlace over constructed bodies and five real Codex
fixtures. Broken by hand twice (user parts skipped; ToolResult turned
into Text): red both times. tier_record_test adds four long-body cases
(tier after the conversation, a nested one ignored, Anthropic speed
fast, a Chat numeric tier).

Run at this head (720e491d2 + these two commits): gofmt clean, go build,
GOOS=linux and GOOS=windows go vet -tags nogui on gateway and provider;
go test -run 'Facts|Tier|Session|Responses|Metadata|UserWords|Prompt|Memo'
./internal/gateway ok. The full gateway and provider packages are
running and are reported on #3394.
2026-10-26 16:13:27 +08:00

56 lines
2.5 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"}`, ""},
// a long body, asked through its split (topValue): the tier after the
// conversation, one inside it that isn't the request's, none at all
{provider.Responses, `{"input":[{"role":"user","content":"` + strings.Repeat("words ", 8000) + `"}],"service_tier":"priority"}`, "priority"},
{provider.Responses, `{"input":[{"role":"user","content":"x","service_tier":"flex","pad":"` + strings.Repeat("words ", 8000) + `"}]}`, ""},
{provider.Anthropic, `{"messages":[{"role":"user","content":"` + strings.Repeat("words ", 8000) + `"}],"speed":"fast"}`, "fast"},
{provider.Chat, `{"messages":[{"role":"user","content":"` + strings.Repeat("words ", 8000) + `"}],"service_tier":1}`, ""},
} {
if got := bodyTier(c.proto, []byte(c.body)); got != c.want {
t.Errorf("%s %s: %q", c.proto, c.body, got)
}
}
}