Files
magpie/internal/gateway/codex_tier_test.go
T
yetone add0dadd4f codex: a service tier set in Codex's settings goes on every request of Codex's (JackyFill on X)
JackyFill asked for a choice in Codex's agent settings that rewrites
service_tier: follow the request, force Fast, force Ultra Fast. Codex's
settings on the Agents page now have "Codex service tier", picked from
the app menu: As Codex asks (the default, nothing changes), Standard,
Fast or Ultrafast.

The values are Codex's own request values (codex-rs protocol
config_types.rs ServiceTier, tui service_tier_resolution.rs): Fast is
"priority", Ultra Fast is "ultrafast", and Standard sends no
service_tier. Flex isn't offered: the ChatGPT backend's requests drop
it (codex_request.go).

settings.CodexServiceTier ("", "default", "priority", "ultrafast";
anything else refused) is set through POST
/api/settings/codex-service-tier. codexTiered rewrites the body where
Codex's requests come in, on both ways: codexBackend (Codex signed in,
/backend-api/codex) and the main handler's Codex-on-Responses block,
which a websocket turn goes through too. It applies only when the
request is Codex's (fromCodex or agentOf == codex). Everything after
treats it as Codex's own ask: an account whose list lacks Ultrafast is
put after a mate that has it or sent priority. A bare model (Codex's own
sign-in, whose plan magpie doesn't know) gets ultrafast only where one
of magpie's Codex accounts lists it on the model (UltraModels), else
priority. The body is edited with topMember/withoutTopMember, no full
decode. Fast picks still apply after, unchanged.

Run (mote):
- TestCodexServiceTierSet, TestWithServiceTier: ok. The test signs in a
  Plus and a Pro ChatGPT account against a fake backend whose lists offer
  gpt-6-luna's Ultrafast to Pro only, and checks the tier sent for each
  setting x what Codex asked, through /v1/responses (account:tier tried)
  and on /backend-api/codex for a model with Ultrafast and one without;
  OpenCode's own priority is left alone; "flex" is refused.
- go test -count=1 -timeout 45m ./internal/settings ./internal/provider
  ./internal/gui ./internal/gateway: ok but internal/gui, which was red
  twice: TestCoreScriptBudget (1,535,075 > 1,533,802) and
  TestSettingsSaveKeepsWhatItDoesNotSend (a save of the Settings page
  dropped the tier). Paid for by eight Codex-row comments cut in app.js
  (now 1,533,416), no raise; settingsapi.Apply keeps CodexServiceTier
  as it keeps the other Codex rows. At the pushed head: go test
  ./internal/settingsapi ./internal/settings ./internal/gui ok, the two
  gateway tier tests ok.
- go vet darwin; GOOS=linux -tags nogui; GOOS=windows (gateway,
  provider, settings, gui): clean. gofmt -l: clean.
- GUI, chromium + webkit: codex-service-tier (new, en/zh/zh-TW/ja/de at
  900 and 400px: 20/20), agent-prefs (56/56, Codex's row list updated),
  i18n-keys, i18n-en-no-cjk, settings-search, codex-titles,
  codex-subagent-model, settings-general, codex-sheet-providers,
  click-scroll: all pass; at the pushed head codex-service-tier,
  agent-prefs, i18n-keys, settings-search, codex-titles,
  codex-subagent-model, settings-general, click-scroll: 166/166.

Broken by hand:
- codexTiered returning the body as it came: TestCodexServiceTierSet red
  (17 lines).
- the Ultrafast-to-Fast downgrade for a bare model removed: red on
  gpt-6-sol (sent ultrafast, want priority).
- the GUI posting to the wrong path, and the row left out of Codex's
  AGENT_PREFS: codex-service-tier red in chromium and webkit.
- settingsapi without the CodexServiceTier keep:
  TestSettingsSaveKeepsWhatItDoesNotSend red.

Not tried: a real ChatGPT account's Ultrafast or Fast request with the
setting on (only the fake backend), a real Codex websocket session, and
Windows/Linux runs of the GUI.
2026-11-10 15:24:40 +08:00

115 lines
4.6 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// JackyFill on X: Codex's settings in magpie set the service tier every
// request of Codex's goes at, whatever Codex asked: as Codex asks it
// (unset), none (standard), priority (Fast) or ultrafast (Ultrafast).
// Another agent's request goes as it asked, and Ultrafast on a model no
// account lists it on goes as priority. Both of Codex's ways in: with
// magpie as its provider (/v1/responses) and signed in, on the ChatGPT
// backend's route.
func TestCodexServiceTierSet(t *testing.T) {
tried := tierAccounts(t, nil, nil)
s := New()
last := func() string {
t.Helper()
got := tried()
if len(got) == 0 {
t.Fatal("the ChatGPT backend was asked nothing")
}
return got[len(got)-1]
}
body := func(model, tier string) string {
b := `{"model":"` + model + `","stream":true,"input":"ping"`
if tier != "" {
b += `,"service_tier":"` + tier + `"`
}
return b + "}"
}
// through magpie's Codex provider, whose accounts' lists magpie knows
viaProvider := func(agent, tier string) string {
t.Helper()
postFrom(t, s, agent, "/v1/responses", body("codex/gpt-6-luna", tier))
return last()
}
// on the backend's route, as Codex signed in to ChatGPT sends
viaBackend := func(model, tier string) string {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body(model, tier)))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-plus")
req.Header.Set("User-Agent", "codex_cli_rs/0.170.0 (Mac OS 26.6.0; arm64) kitty")
req.Header.Set("originator", "codex_cli_rs")
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("backend route: %d %s", rec.Code, rec.Body)
}
return strings.SplitN(last(), ":", 2)[1]
}
for _, c := range []struct {
set, asked string
provider string // account:tier tried last
luna, sol string // the tier sent for a model a plan offers Ultrafast on, and one none does
}{
{"", "priority", "acct-plus:priority", "priority", "priority"},
{"", "", "acct-plus:", "", ""},
{settings.CodexTierStandard, "priority", "acct-plus:", "", ""},
{settings.CodexTierStandard, "ultrafast", "acct-plus:", "", ""},
{"priority", "", "acct-plus:priority", "priority", "priority"},
{"priority", "ultrafast", "acct-plus:priority", "priority", "priority"},
{"ultrafast", "", "acct-pro:ultrafast", "ultrafast", "priority"},
{"ultrafast", "priority", "acct-pro:ultrafast", "ultrafast", "priority"},
} {
if err := provider.SetCodexServiceTier(c.set); err != nil {
t.Fatal(err)
}
if got := viaProvider("codex", c.asked); got != c.provider {
t.Errorf("set %q, Codex asked %q through /v1/responses: tried %s, want %s", c.set, c.asked, got, c.provider)
}
if got := viaBackend("gpt-6-luna", c.asked); got != c.luna {
t.Errorf("set %q, Codex asked %q for gpt-6-luna on the backend's route: sent %q, want %q", c.set, c.asked, got, c.luna)
}
if got := viaBackend("gpt-6-sol", c.asked); got != c.sol {
t.Errorf("set %q, Codex asked %q for gpt-6-sol on the backend's route: sent %q, want %q", c.set, c.asked, got, c.sol)
}
}
// the rule is Codex's alone
provider.SetCodexServiceTier(settings.CodexTierStandard)
if got := viaProvider("opencode", "priority"); got != "acct-plus:priority" {
t.Errorf("OpenCode's priority tried %s", got)
}
// a value magpie doesn't know is refused
if err := provider.SetCodexServiceTier("flex"); err == nil {
t.Error("flex was taken")
}
}
// withServiceTier sets, replaces or takes out the top-level tier alone,
// every other byte as it came; a nested service_tier is not the request's.
func TestWithServiceTier(t *testing.T) {
for _, c := range []struct{ in, tier, want string }{
{`{"model":"m"}`, "priority", `{"service_tier":"priority","model":"m"}`},
{`{}`, "priority", `{"service_tier":"priority"}`},
{` { } `, "priority", ` {"service_tier":"priority" } `},
{`{"model":"m","service_tier":"ultrafast"}`, "priority", `{"service_tier":"priority","model":"m"}`},
{`{"model":"m","service_tier":"priority"}`, "priority", `{"model":"m","service_tier":"priority"}`},
{`{"model":"m","service_tier":"priority"}`, "", `{"model":"m"}`},
{`{"model":"m"}`, "", `{"model":"m"}`},
{`{"model":"m","metadata":{"service_tier":"flex"}}`, "", `{"model":"m","metadata":{"service_tier":"flex"}}`},
{`not json`, "priority", `not json`},
} {
if got := string(withServiceTier([]byte(c.in), c.tier)); got != c.want {
t.Errorf("withServiceTier(%s, %q) = %s, want %s", c.in, c.tier, got, c.want)
}
}
}