Files
magpie/internal/gateway/codex_spawn_models_test.go
T
yetone 5d33f060b7 gateway: Codex's spawn_agent names every model a subagent may be put on, not only the first five (Gearclickist on Discord)
Gearclickist turned on "Codex subagents on any model", but when Codex
dispatched subagents only a few models could be picked. Codex itself caps
the list: spawn_agent's description names only the first five
show_in_picker models of its catalog (codex-rs child_config.rs
MAX_SPAWN_AGENT_MODEL_OVERRIDES, multi_agents_spec.rs
spawn_agent_models_description), Codex's own ahead of magpie's. Yet it
takes any catalog model by name (find_spawn_agent_model_name). So the lead
saw none of magpie's models.

spawnAgentModels (codex_spawn_models.go) adds the rest of the catalog
Codex was handed after Codex's own list, under a line of its own, at most
200. It covers spawn_agent at the top level and in Codex's multi_agent_v1
namespace, at CodexPath (codexBackend) and at /v1/responses for Codex
(handle).
- At CodexPath the list is Codex's own listed models less those hidden on
  the Agents page, then CodexListed unless Codex is held to its own.
- At /v1 it is CodexCatalog under the key's allowance.
- A lead the ChatGPT backend answers in multi-agent V2 seals its
  subagents' tasks for a ChatGPT account (#619), so it is offered Codex's
  own only.
Only the tool's description changes, never base_instructions. With V1 on,
it does change in requests to the ChatGPT backend too.

Not covered: the model_catalog_in_context feature (under development, off
by default) puts a five-model catalog in context, and it is not widened.

Run:
- go test ./internal/gateway ./internal/agent -count=1 under a sandbox
  HOME: ok.
- TestSpawnAgentNamesEveryModel uses 40 models on a real Codex 0.162.0
  request (testdata/workdir/codex-0.162.0.json). Its 4 subtests are
  CodexPath, /v1, a sealed V2 lead and a V1 lead, and all pass.
  - With spawnAgentModels returning body by hand: CodexPath, /v1 and V1
    go red.
  - With the sealed rule taken out: V2 goes red.
- go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows; gofmt -l
  is clean.

Not verified: a real Codex lead picking one of magpie's models past the
fifth from the longer list.
2026-10-12 23:30:08 +08:00

145 lines
4.9 KiB
Go

package gateway
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// spawnDescription is the description of the spawn_agent tool in a
// Responses request body, "" when it has none.
func spawnDescription(t *testing.T, body []byte) string {
t.Helper()
var q struct {
Tools []struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Tools []struct {
Name string `json:"name"`
Description string `json:"description"`
} `json:"tools"`
} `json:"tools"`
}
if err := json.Unmarshal(body, &q); err != nil {
t.Fatalf("%v: %.300s", err, body)
}
for _, tl := range q.Tools {
// a translated request has it flat, multi_agent_v1__spawn_agent
if tl.Name == "spawn_agent" || strings.HasSuffix(tl.Name, "__spawn_agent") {
return tl.Description
}
for _, in := range tl.Tools {
if in.Name == "spawn_agent" {
return in.Description
}
}
}
return ""
}
// codexSpawnRequest is a real Codex 0.162.0 request, its spawn_agent
// naming the five models Codex names at most, on model.
func codexSpawnRequest(t *testing.T, model string) string {
t.Helper()
b, err := os.ReadFile("testdata/workdir/codex-0.162.0.json")
if err != nil {
t.Fatal(err)
}
return string(withModel(b, model))
}
// Codex's spawn_agent names only the first five models of its catalog
// to the lead (codex-rs MAX_SPAWN_AGENT_MODEL_OVERRIDES), though it takes
// any model of it: with 40 of magpie's in the catalog, the lead saw none
// of them (Gearclickist on Discord: "Codex subagents on any model" on,
// and only a few models could be picked). The tool now names every model
// a subagent of that lead can work on, at CodexPath and at /v1; for a
// lead the ChatGPT backend answers in multi-agent V2, whose subagents'
// tasks only a ChatGPT account opens, only Codex's own.
func TestSpawnAgentNamesEveryModel(t *testing.T) {
var models []string
for i := 1; i <= 40; i++ {
models = append(models, fmt.Sprintf("m%d", i))
}
f := &fake{t: t, reply: sse(`data: {"type":"response.created","response":{"id":"r1"}}`,
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":9,"output_tokens":2}}}`)}
up := setup(t, provider.Responses, f)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Responses: up.URL + "/v1", Models: models}); err != nil {
t.Fatal(err)
}
var upstream []byte
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
upstream, _ = io.ReadAll(r.Body)
w.Header()["Content-Type"] = nil
io.WriteString(w, sse(`data: {"type":"response.created","response":{"id":"r1"}}`,
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":9,"output_tokens":2}}}`))
})
namesAll := func(t *testing.T, desc string) {
t.Helper()
for _, m := range models {
if !strings.Contains(desc, "- `fake/"+m+"`") {
t.Fatalf("spawn_agent doesn't name fake/%s:\n%s", m, desc)
}
}
// Codex's own five kept, and its text after the list
head, tail, _ := strings.Cut(desc, spawnMore)
list, rest, ok := strings.Cut(tail, "\n Spawn a sub-agent")
if !ok || !strings.Contains(head, "- `gpt-6.1-sol`") || !strings.Contains(head, "- `gpt-5.6-sol`") ||
!strings.HasPrefix(rest, " for a well-scoped task.") {
t.Fatalf("description:\n%s", desc)
}
for _, l := range strings.Split(strings.TrimPrefix(list, "\n"), "\n") {
if !strings.HasPrefix(l, "- `") {
t.Fatalf("line %q in the list:\n%s", l, desc)
}
}
}
t.Run("CodexPath, a lead on magpie's model", func(t *testing.T) {
code, body := codexPost(t, codexSpawnRequest(t, "fake/m1"))
if code != 200 {
t.Fatalf("%d %s", code, body)
}
namesAll(t, spawnDescription(t, f.got))
})
t.Run("/v1, Codex with magpie as its provider", func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(codexSpawnRequest(t, "fake/m2")))
req.Header.Set("User-Agent", "codex_cli_rs/0.162.0")
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
namesAll(t, spawnDescription(t, f.got))
})
t.Run("a lead on Codex's own model, V2", func(t *testing.T) {
code, body := codexPost(t, codexSpawnRequest(t, "gpt-6.1-sol"))
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if d := spawnDescription(t, upstream); d == "" || strings.Contains(d, "fake/") || strings.Contains(d, spawnMore) {
t.Fatalf("a sealed lead's subagents offered magpie's models:\n%s", d)
}
})
t.Run("a lead on Codex's own model, V1", func(t *testing.T) {
if err := provider.SetCodexAgentsV1(true); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { provider.SetCodexAgentsV1(false) })
code, body := codexPost(t, codexSpawnRequest(t, "gpt-6.1-sol"))
if code != 200 {
t.Fatalf("%d %s", code, body)
}
namesAll(t, spawnDescription(t, upstream))
})
}