Files
magpie/internal/gateway/codex_review_spent_test.go
T
yetone f2b9ec354a gateway: Codex's auto-review goes on where its conversation does when the sign-in is out (riverleaf88 on X)
riverleaf88 on X: with Codex's ChatGPT allowance spent and magpie routing
Codex on to another model, Codex's automatic review still failed.

Codex's guardian (codex-rs guardian-v2 sync_reviewer) asks the same
provider, so magpie's /backend-api/codex/responses, on codex-auto-review
(approval_review_preferred_model) with x-openai-subagent: guardian. On a
spent sign-in codexFallsBack and serve's groupsAfter looked for fallbacks
and routing groups of codex/codex-auto-review, a model no group names, so
the review was handed ChatGPT's 429 while the conversation's turns went
on to the group.

- codexReviewAfter: an auto-review on the codex sign-in takes the model
  its conversation's own turns go on (codexLeads). A codex model brings
  that model's fallbacks and groups (fallbacksOnly on the lead too); a
  magpie model the caller may use (servedSession) is tried itself.
  codexFallsBack and groupsAfter add them.
- codexHolds: once a 429 naming a reset held the account, later turns
  (main turns too) were answered with the held refusal before any
  fallback. The held branch now hands a /responses turn with fallbacks to
  codexFallBack as the first spent turn was.
- docs/subsystems/gateway-routing.md says both.

Run (mote): go test -count=1 -run 'TestCodexReviewSpent|TestCodexHeld|
TestCodexOneAccount|TestCodexSpent|Hold|Held' ./internal/gateway/ ok
71.2s; gofmt -l clean.
New tests red on the old code: TestCodexReviewSpentFollowsTheGroup and
...FollowsMagpieModel (review got 429 usage_limit_reached); the held case
in TestCodexHeldTurnFollowsTheGroup.
Broken by hand: codexReviewAfter returning nil turns
TestCodexReviewSpentFollowsTheGroup, ...FollowsMagpieModel and
TestCodexHeldTurnFollowsTheGroup red; dropping the held-branch fallback
turns TestCodexHeldTurnFollowsTheGroup and ...FollowsTheGroup red.
Not tried: a real exhausted ChatGPT account; guardian-v2's async scorer;
the multi-account pool path past serve's candidate loop is covered only
through the one-account tests.
Also run (mote, at this tree): go vet ./internal/gateway/ for darwin,
GOOS=linux -tags nogui and GOOS=windows: clean. go test -count=1
-timeout 45m ./internal/gateway/: ok 1037.9s (base 2219fdd49).
Rebased on 662546e78 (agentpath change only): go vet ./internal/gateway/
and -run 'TestCodex|Hold|Held|Fallback|Group': ok 289.7s.
2026-11-10 16:43:25 +08:00

130 lines
5.4 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Codex's auto-review of an approval, as codex-rs's guardian sends it: on
// its reviewer model, codex-auto-review, whatever the conversation is on,
// under x-openai-subagent: guardian and x-codex-guardian: reviewer.
const reviewTurn = `{"model":"codex-auto-review","stream":true,"input":"review this approval"}`
// reviewOn posts body to s's Codex route as Codex's guardian does (guardian
// true) or as one of the conversation's own turns.
func reviewOn(s *Server, body string, guardian bool) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-1")
if guardian {
req.Header.Set("x-openai-subagent", "guardian")
req.Header.Set("x-codex-guardian", "reviewer")
}
s.Handler().ServeHTTP(rec, req)
return rec
}
// Codex's one ChatGPT account out of its allowance, and Codex routed on
// through a routing group of the user's with Codex's model first and an
// API after it: the conversation's turns go on to the API, and so does
// its auto-review, which asks codex-auto-review, a model no group has
// (riverleaf88 on X: "在codex耗尽额度时,即使路由到别的模型,在codex中自动审查会失败").
// Before, the review was handed the backend's refusal, and Codex failed it.
func TestCodexReviewSpentFollowsTheGroup(t *testing.T) {
for _, x := range codexSpent {
t.Run(x.name, func(t *testing.T) {
b := &spentBackend{refuse: x.refuse}
spentAccount(t, b, false)
a := &scripted{replies: []reply{{200, "", chatSSEReply("from the group")}}}
scriptedOn(t, "a", provider.Chat, a)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"codex/gpt-5.5", "a/m"}}); err != nil {
t.Fatal(err)
}
s := New()
if rec := reviewOn(s, oneAccountTurn, false); rec.Code != 200 || !strings.Contains(rec.Body.String(), "from the group") {
t.Fatalf("turn: %d %q", rec.Code, rec.Body.String())
}
rec := reviewOn(s, reviewTurn, true)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "from the group") || strings.Contains(rec.Body.String(), "usage_limit_reached") {
t.Fatalf("review: %d %q", rec.Code, rec.Body.String())
}
if a.n != 2 {
t.Fatalf("the group's API asked %d times, ChatGPT %d", a.n, b.n.Load())
}
if b.n.Load() > 2 {
// the spent account isn't asked again for the review on
// the conversation's model, the group's first member
t.Fatalf("ChatGPT asked %d times", b.n.Load())
}
})
}
}
// The conversation on one of magpie's models, picked in the Codex app
// beside the spent ChatGPT sign-in: its auto-review, on codex-auto-review
// through the sign-in, goes on to that model when the account is out.
func TestCodexReviewSpentFollowsMagpieModel(t *testing.T) {
for _, x := range codexSpent {
t.Run(x.name, func(t *testing.T) {
b := &spentBackend{refuse: x.refuse}
spentAccount(t, b, false)
a := &scripted{replies: []reply{{200, "", chatSSEReply("from magpie's model")}}}
scriptedOn(t, "a", provider.Chat, a)
s := New()
if rec := reviewOn(s, `{"model":"a/m","stream":true,"input":"ping"}`, false); rec.Code != 200 || b.n.Load() != 0 {
t.Fatalf("turn: %d %q, ChatGPT asked %d", rec.Code, rec.Body.String(), b.n.Load())
}
rec := reviewOn(s, reviewTurn, true)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "from magpie's model") || b.n.Load() != 1 || a.n != 2 {
t.Fatalf("review: %d %q, ChatGPT asked %d, a %d", rec.Code, rec.Body.String(), b.n.Load(), a.n)
}
})
}
}
// With nowhere else for the conversation to go (no fallback, no group, no
// turn of magpie's to follow), the review still has the backend's own
// refusal, as it had.
func TestCodexReviewSpentNowhereElseRelayed(t *testing.T) {
b := &spentBackend{refuse: codexSpent[0].refuse}
spentAccount(t, b, false)
a := &scripted{replies: []reply{{200, "", chatSSEReply("never")}}}
scriptedOn(t, "a", provider.Chat, a)
rec := reviewOn(New(), reviewTurn, true)
if rec.Code != 429 || !strings.Contains(rec.Body.String(), "usage_limit_reached") || b.n.Load() != 1 || a.n != 0 {
t.Fatalf("%d %q, ChatGPT asked %d, a %d", rec.Code, rec.Body.String(), b.n.Load(), a.n)
}
}
// The account held after ChatGPT's 429 named when it is back
// (codexHolds): the next turn, and the review after it, go on to the
// group as the turn that held it did, never handed the refusal again from
// the hold while it lasts.
func TestCodexHeldTurnFollowsTheGroup(t *testing.T) {
for _, x := range codexSpent[:2] {
t.Run(x.name, func(t *testing.T) {
b := &spentBackend{refuse: x.refuse}
spentAccount(t, b, false)
a := &scripted{replies: []reply{{200, "", chatSSEReply("from the group")}}}
scriptedOn(t, "a", provider.Chat, a)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"codex/gpt-5.5", "a/m"}}); err != nil {
t.Fatal(err)
}
s := New()
for i, body := range []string{oneAccountTurn, oneAccountTurn, reviewTurn} {
rec := reviewOn(s, body, body == reviewTurn)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "from the group") {
t.Fatalf("request %d: %d %q", i, rec.Code, rec.Body.String())
}
}
if b.n.Load() != 1 || a.n != 3 {
t.Fatalf("ChatGPT asked %d times, the group's API %d", b.n.Load(), a.n)
}
})
}
}