Files
magpie/internal/gateway/draw_group_mixed_test.go
T
yetone 965ae78e1e gateway: a routing group draws on its own image member, Codex's tool too
changqiu4890 on Discord: the group smart-split has three Codex CPA chat
models, DeepSeek-V4.1-Flash and Codex CPA's gpt-image-2.5, and dsh,
Codex and Pi are set to it. Image requests went to other providers'
image models.

Two causes in magpie:

- drawingTries tried every member of a group named for a drawing in the
  order chat routing plans them, so the chat members came first.
  group/smart-split drew on cpa/gpt-6-astra, which was asked as a chat
  for an image, before it reached the group's image member. A group
  with members that make the kind (drawsModel, or servesKind for
  video) is now tried on those members alone. The fallback between
  them stays as it was.
- Codex's image tool drew on the provider that served the turn, else
  on Settings' pick. A group's image member was never used: a turn on
  DeepSeek, or on the relay, drew with Settings' model or with the
  relay's other GPT Image. codexDrawingProvider now also returns the
  routing group the turn asked for. When that group draws (GroupDraws),
  the drawing goes to its image member through resolveDrawing, which
  keeps the member's provider.

The reporter's Routing screenshot also shows explicit requests from a
client magpie names "Mozilla": x0-15/gpt-image-2.5, codex/gpt-image-2.5,
and bare gpt-image-1 and gpt-image-1.5. magpie routes a request that
names a provider to that provider, and a bare id to the first provider
whose Drawers list it. Nothing in such a request links it to the agent's
group, so this commit leaves those requests alone.

Tests:
- TestMixedGroupDrawsOnItsImageMemberOnly (new): the reporter's group
  shape on fake upstreams, with another relay listing gpt-image-2.5
  first. Red before the fix (drew on cpa/gpt-6-astra), green after.
- TestCodexDrawingTakesItsGroupsImageMember (new): red with the group
  branch turned off by hand (drew on chat/gpt-image-2), green with it.
- TestMixedGroupMakesVideoOnItsVideoMemberOnly (new): a guard for the
  video sibling. It passes with the filter turned off as well, because
  video's mediaMembers already keeps only video members (KindGroup).

Run on mote (mote-run) at this tree:
- gofmt -l
- go vet ./internal/gateway on darwin, GOOS=linux -tags nogui and
  GOOS=windows
- go test -count=1 -run 'Group|Draw|Image|Video|Media' ./internal/gateway
- go test -count=1 -timeout 45m ./internal/gateway ./internal/provider

Not tried:
- a real Codex CPA relay or a real dsh session
- the image MCP's default for a non-Codex agent set to a group: the MCP
  call carries no link to the agent's chat route, so it still uses
  Settings' pick
- chat routing to a group's image member when the member's provider has
  no picks: the member's provider is still the pinned one, and this
  commit doesn't change it
2026-11-05 17:04:16 +08:00

78 lines
2.9 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// changqiu4890 on Discord: a routing group of chat models (three on a
// relay, one on DeepSeek) and one image model, the relay's gpt-image-2.5,
// and another relay that lists gpt-image-2.5 too. Asked to draw, the group
// draws on its own image member, on the relay it is pinned to: never on a
// chat member's images API, never on the other relay's model of that name.
func TestMixedGroupDrawsOnItsImageMemberOnly(t *testing.T) {
s, _ := easeled(t)
t.Setenv("MAGPIE_PUBLIC_URL", "")
t.Cleanup(s.imageStore.running.Wait)
var mu sync.Mutex
asked := map[string][]string{}
vendor := func(id string) *httptest.Server {
e := &easel{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
mu.Lock()
asked[id] = append(asked[id], r.URL.Path+" "+string(body))
mu.Unlock()
r.Body = io.NopCloser(strings.NewReader(string(body)))
e.ServeHTTP(w, r)
}))
t.Cleanup(srv.Close)
return srv
}
cpa, other, ds := vendor("cpa"), vendor("other"), vendor("ds")
for _, p := range []struct {
p provider.Provider
live []catalog.Model
}{
// the other relay first, so a bare gpt-image-2.5 finds it first
{provider.Provider{ID: "other", Name: "Other", Chat: other.URL + "/v1", Key: "key"}, []catalog.Model{{ID: "gpt-image-2.5", Draws: true}}},
{provider.Provider{ID: "cpa", Name: "CPA", Chat: cpa.URL + "/v1", Key: "key", Models: []string{"gpt-6-astra", "gpt-6.1-sol"}}, []catalog.Model{{ID: "gpt-6-astra"}, {ID: "gpt-6.1-sol"}, {ID: "gpt-image-2.5", Draws: true}}},
{provider.Provider{ID: "ds", Name: "DeepSeek", Chat: ds.URL + "/v1", Key: "key"}, []catalog.Model{{ID: "deepseek-v4.1-flash"}}},
} {
if err := provider.Save(p.p); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive(p.p.ID, p.p.Chat, p.live); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{ID: "smart-split", Name: "Smart split", Routing: "order", Members: []string{
"cpa/gpt-6-astra", "cpa/gpt-6.1-sol", "ds/deepseek-v4.1-flash", "cpa/gpt-image-2.5",
}}); err != nil {
t.Fatal(err)
}
if !GroupDraws("group/smart-split") {
t.Fatal("the group doesn't draw")
}
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"group/smart-split","prompt":"a magpie"}`)
if code != 200 || a.Model != "cpa/gpt-image-2.5" {
t.Fatalf("group/smart-split: %d %s", code, raw)
}
mu.Lock()
defer mu.Unlock()
if len(asked["other"]) != 0 || len(asked["ds"]) != 0 {
t.Fatalf("another provider was asked: other %v, ds %v", asked["other"], asked["ds"])
}
if len(asked["cpa"]) != 1 || !strings.Contains(asked["cpa"][0], `"model":"gpt-image-2.5"`) {
t.Fatalf("the relay was asked %v, not its image model once", asked["cpa"])
}
}