Files
magpie/internal/gateway/remote_tier_test.go
T
yetone 659ca3f026 gateway: through a Remote magpie the log records the tier the other magpie sent (JackyFill on X)
Codex -> magpie1 -> (Remote magpie) -> magpie2 -> a ChatGPT account, with
Fast on in magpie2 alone: magpie2 sent service_tier priority and logged
the served tier unknown (the ChatGPT backend says "auto"/"default"
whether priority ran or not, servedTierAt). magpie1 only saw that
"default" in the relayed body, from a host that isn't chatgpt.com, and
logged tier_sent "" and tier_served "default".

A try asked by another magpie (magpieAsks) now says on its reply's
header the tier it sent its vendor (X-Magpie-Tier-Sent, "none" for
none) and, when that vendor's "auto"/"default" tell nothing,
X-Magpie-Tier-Unsaid: 1 (stampTier, from holdWriter.writeHeader). The
asking magpie takes them from a Remote magpie's reply alone (tierThere
in heardReply; a vendor's X-Magpie- headers are still never passed) and
records them as the try's tier_sent and an unknown tier_served, in the
usage log and on the route. A chain passes the farthest magpie's on.
docs/subsystems/gateway-routing.md says so.

Run (wsl-run, Linux, at this head): new TestRemoteMagpieTierRecorded
(one server as both magpies, its key's tier fast then standard, the
backend answering auto/default) green; broken by hand: with the four
source files at origin/main it fails with exactly the report, "sent ''
served 'default', want sent 'priority'"; the whole internal/gateway package ok (932s, -timeout 45m) at
5a28b17c6 (on origin/main 943404477); go vet ./internal/gateway for
linux, windows and darwin clean; gofmt clean.
Rebased after onto 9f4e1120f (agent, provider, gui only; no internal/gateway
file) and run again there: the new test, the tier and remote-group tests
and vet of internal/gateway ok.

Not covered: a reply whose headers a keepalive sent before the try
answered carries neither header, and magpie1 keeps its own record.
Not tried: two real computers or a real ChatGPT account; the full
./... suite; the Windows box.
2026-11-14 18:47:33 +08:00

113 lines
3.4 KiB
Go

package gateway
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
// JackyFill on X: Codex → magpie1 → (Remote magpie) → magpie2 → a ChatGPT
// account, Fast on in magpie2 alone (here its gateway key's tier).
// magpie1's log said "default", the ChatGPT backend's word in the body
// that tells nothing, and nothing of the priority magpie2 sent. It now
// records what magpie2 sent its vendor, and the served tier unknown, as
// magpie2 does; a request magpie2 sends at none says none.
func TestRemoteMagpieTierRecorded(t *testing.T) {
tried := tierAccounts(t, nil, nil, func(string) (string, string) { return "auto", "default" })
to, _ := url.Parse(provider.CodexBase)
old := provider.CodexBase
provider.CodexBase = "https://chatgpt.com/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = old })
cfg := settings.Load()
cfg.LAN = true
if err := settings.Save(cfg); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "Remote"})
if err != nil {
t.Fatal(err)
}
keys, _ := access.List()
var keyID string
for _, k := range keys {
if k.Name == "Remote" {
keyID = k.ID
}
}
s := New()
s.client = &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
if r.URL.Host == "chatgpt.com" {
r.URL.Scheme, r.URL.Host = to.Scheme, to.Host
}
return http.DefaultTransport.RoundTrip(r)
})}
h := s.Handler()
there := httptest.NewServer(h)
t.Cleanup(there.Close)
id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Key: secret, Preset: provider.RemoteMagpiePreset, Chat: strings.TrimPrefix(there.URL, "http://")})
if err != nil {
t.Fatal(err)
}
office, err := provider.Find(id)
if err != nil {
t.Fatal(err)
}
if _, err := office.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
for _, c := range []struct{ set, want string }{{"fast", "priority"}, {"standard", ""}} {
if _, err := access.Update("tier-key", access.Change{Key: keyID, ServiceTier: c.set}); err != nil {
t.Fatal(err)
}
model := id + "/codex/gpt-6-luna"
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"`+model+`","stream":true,"input":"ping"}`)))
if rec.Code != 200 {
t.Fatalf("%s: %d %s", c.set, rec.Code, rec.Body)
}
if got := tried(); len(got) == 0 || got[len(got)-1] != "acct-plus:"+c.want {
t.Fatalf("%s: backend asked %v, want acct-plus:%s", c.set, got, c.want)
}
var here *usage.Record
recs := usage.Load(time.Time{})
for i := len(recs) - 1; i >= 0; i-- {
if recs[i].Provider == id {
here = &recs[i]
break
}
}
if here == nil {
t.Fatalf("%s: no record of the request here: %+v", c.set, recs)
}
if here.TierSent != c.want || here.TierServed != "" {
t.Errorf("%s: recorded here sent %q served %q, want sent %q served unknown", c.set, here.TierSent, here.TierServed, c.want)
}
s.trace.mu.Lock()
var tr *Route
for _, r := range s.trace.routes {
if r.Model == model {
tr = r
}
}
var try Try
if tr != nil && len(tr.Tries) > 0 {
try = tr.Tries[len(tr.Tries)-1]
}
s.trace.mu.Unlock()
if tr == nil || try.Tier != c.want || try.TierServed != "" {
t.Errorf("%s: route's try %+v, want tier %q", c.set, try, c.want)
}
}
}