Files
magpie/internal/gateway/flat_model_test.go
T
yetone 1bc0ed404a minimax-code: magpie's models have one slash in the whole ref, so SubAgents take them (#1387, worksyang)
MiniMax Code 3.1.0 checks a SubAgent's model against /^[^/\s]+\/[^/\s]+$/
(uQn in @minimax-ai/code's chunk-2XPMNZVR.js), and magpie wrote its models
under custom_provider.magpie by catalog id, so
custom_provider:magpie/bb-codex/gpt-6.1-sol was refused
(AGENT_CONFIG_INVALID).

- The writer keys each model by its catalog id with "~" for each slash
  (bb-codex~gpt-6.1-sol, provider.FlatID), when exactly one served id
  spells that way (provider.Flattened); else it keeps the slashed id.
- The gateway takes such an id back to that one provider's model or group
  in validateModel and unprefixed (unflat / provider.Unflat): only an id
  with "~", no slash, that resolves as nothing else; two ids spelled alike
  are neither. Never a bare-name first hit. Key limits, middleware and
  usage see the catalog id, so a key held to bb-codex/* is refused
  other~gpt-6.1-sol on every route, count_tokens too.
- A config an older magpie wrote moves on the next sync: the user's keys on
  a model go to its new key, defaultModel, defaultLightModel and
  memory.captureModel/consolidationModel follow, and the slashed key stays
  with enabled: false, so a session begun on it still resumes (checked
  with mcode exec --continue). Get still reads magpie/<catalog id>, and
  Disconnect still puts back the stashed default and variant.

Other agents: no installed agent bundle here (cline, command-code,
qwen-code, deepseek, pi, omp, codebuff, openclaw) has this regex; native
agents were not checked.

Verified:
- go test -tags nogui ./internal/agent ./internal/gateway ./internal/provider
  (temp HOME), gofmt -l, go vet darwin/linux/windows.
- New TestMiniMaxCodeOneSlash uses the reporter's regex and ids;
  TestFlatModelIDsReachTheirProvider has two providers serving
  gpt-6.1-sol. Breaking unflat, mcodeKey, the key carry-over, the legacy
  enabled:false and the ref rewrite each turned the tests red.
- Real run: built magpie serve in a temp HOME on 47812, two fake upstreams;
  `magpie mcode magpie/other/gpt-6.1-sol` wrote other~gpt-6.1-sol; real
  mcode 0.6.5 exec reached other's upstream with gpt-6.1-sol (usage:
  other/gpt-6.1-sol); its agent loader accepted an agent.md with
  custom_provider:magpie/other~gpt-6.1-sol and refused the two-slash one;
  `magpie mcode default` put minimax/MiniMax-M2.7 back.
2026-10-09 14:08:07 +08:00

112 lines
4.6 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/provider"
)
// #1387 (worksyang): MiniMax Code names a model under magpie with one
// slash in the whole, so magpie writes "bb-codex/gpt-6.1-sol" there as
// "bb-codex~gpt-6.1-sol". The gateway takes that to the one provider's
// model it stands for — not the first provider serving gpt-6.1-sol, as a
// bare name is — on every route, a gateway key's models holding it as
// they hold the id.
func TestFlatModelIDsReachTheirProvider(t *testing.T) {
fresh(t)
reply := func(id string) *fake {
return &fake{t: t, ctype: "application/json", reply: `{"id":"` + id + `","choices":[{"message":{"role":"assistant","content":"hi"},"finish_reason":"stop"}]}`}
}
codex, other := reply("from-codex"), reply("from-other")
for _, x := range []struct {
p provider.Provider
f *fake
}{
{provider.Provider{ID: "bb-codex", Name: "BB Codex", Key: "k", Models: []string{"gpt-6.1-sol"}}, codex},
{provider.Provider{ID: "other", Name: "Other", Key: "k", Models: []string{"gpt-6.1-sol", "glm-5.3"}}, other},
} {
up := httptest.NewServer(x.f)
t.Cleanup(up.Close)
x.p.Chat = up.URL + "/v1"
if err := provider.Save(x.p); err != nil {
t.Fatal(err)
}
}
keys, secrets := newCaller(t, "Held", "Free")
if _, err := access.Update("models-key", access.Change{Key: keys[0].ID, Models: []string{"bb-codex/*"}}); err != nil {
t.Fatal(err)
}
h := New().Handler()
do := func(secret, method, path, body string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, path, strings.NewReader(body))
r.Header.Set("Authorization", "Bearer "+secret)
r.Header.Set("User-Agent", "minimax-code")
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
messages := func(secret, model string) *httptest.ResponseRecorder {
return do(secret, "POST", "/v1/chat/completions", `{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`)
}
sentModel := func(f *fake) string {
var b struct{ Model string }
json.Unmarshal(f.got, &b)
return b.Model
}
// "other~gpt-6.1-sol" is other's, though bb-codex comes first by name
if w := messages(secrets[1], "other~gpt-6.1-sol"); w.Code != 200 || other.calls != 1 || codex.calls != 0 || sentModel(other) != "gpt-6.1-sol" {
t.Fatalf("other~gpt-6.1-sol: %d %s (codex %d, other %d, sent %q)", w.Code, w.Body.String(), codex.calls, other.calls, sentModel(other))
}
if rec := lastUsage(t); rec.Provider != "other" || rec.Model != "gpt-6.1-sol" || rec.Requested != "other/gpt-6.1-sol" {
t.Fatalf("recorded as %+v", rec)
}
if w := messages(secrets[1], "bb-codex~gpt-6.1-sol"); w.Code != 200 || codex.calls != 1 || sentModel(codex) != "gpt-6.1-sol" {
t.Fatalf("bb-codex~gpt-6.1-sol: %d %s", w.Code, w.Body.String())
}
// as are the routing groups magpie found
groups := 0
for _, e := range provider.Catalog() {
if e.Group == "" {
continue
}
groups++
if w := messages(secrets[1], provider.FlatID(e.ID)); w.Code != 200 {
t.Fatalf("%s: %d %s", provider.FlatID(e.ID), w.Code, w.Body.String())
}
}
if groups == 0 {
t.Fatal("no group for gpt-6.1-sol, served by two providers")
}
// a flat id no model has is none
if w := messages(secrets[1], "nowhere~gpt-6.1-sol"); w.Code != 404 {
t.Fatalf("nowhere~gpt-6.1-sol: %d %s", w.Code, w.Body.String())
}
if w := do(secrets[1], "GET", "/v1/models/bb-codex~gpt-6.1-sol", ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"bb-codex/gpt-6.1-sol"`) {
t.Fatalf("model: %d %s", w.Code, w.Body.String())
}
// a key held to bb-codex: refused other's by its flat id too, on every
// route, and given bb-codex's
calls := other.calls
for _, path := range []string{"/v1/chat/completions", "/v1/messages", "/v1/messages/count_tokens", "/v1/responses"} {
w := do(secrets[0], "POST", path, `{"model":"other~gpt-6.1-sol","max_tokens":10,"messages":[{"role":"user","content":"hi"}],"input":"hi"}`)
if w.Code != 403 || other.calls != calls {
t.Fatalf("held key on %s: %d %s", path, w.Code, w.Body.String())
}
}
if w := do(secrets[0], "GET", "/v1/models/other~gpt-6.1-sol", ""); w.Code == 200 {
t.Fatalf("held key found other's model: %s", w.Body.String())
}
if w := messages(secrets[0], "bb-codex~gpt-6.1-sol"); w.Code != 200 {
t.Fatalf("held key on its own model: %d %s", w.Code, w.Body.String())
}
if w := do(secrets[0], "POST", "/v1/messages/count_tokens", `{"model":"bb-codex~gpt-6.1-sol","messages":[{"role":"user","content":"hi"}]}`); w.Code != 200 {
t.Fatalf("held key counting its own model: %d %s", w.Code, w.Body.String())
}
}