Files
magpie/internal/codexcat/codexcat_test.go
T
yetone 2503520f82 codexcat: each Codex model takes official Codex's own instructions for it (#3759, #4008, #4783)
Every magpie model in Codex's catalog (model_catalog_json) carried
gpt-5.2-codex_prompt.md ("You are Codex, based on GPT-5"), so a magpie
GPT-6 model, the auto group's included, ran on a GPT-5.2-era prompt while
official Codex sends GPT-6's own template. magpie still writes no words of
its own into the prompt and leaves the identity line alone: each entry now
takes the text official Codex sends that model, word for word.

- A model's slug (the id's last segment) is matched as Codex 0.166
  matches one (longest prefix of an official slug): first in the entries
  the user's ChatGPT account handed this Codex (models_cache.json), whose
  base_instructions and model_messages go over as sent; then in Codex
  rust-v0.166.0's bundled models.json (instructions_template only, as an
  older Codex may not read 0.166's model_messages), embedded as
  codex_instructions.json.
- Any other model, and a routing group, takes Codex's prompt for a model
  it doesn't know (models-manager/prompt.md, model_info_from_slug), which
  replaces codex_prompt.md: that is what official Codex sends it.
- ownEntry with no base_instructions, and internal/provider's
  codexInstructions fallback, take the same official text.
- testdata holds Codex's models.json and prompt.md at rust-v0.166.0;
  TestBundledIsCodexOwn fails when the embedded copies drift from them.
- GET /v1/codex/models (a remote Codex's model_catalog_url): entries are
  now ~21 KB, so Codex before 0.166's 1 MiB cap holds about 48 of them
  (was ~125). Codex 0.166 reads the catalog whole (models_endpoint.rs
  passes no body limit), and it sends ?client_version=, so the cap now
  applies only to a Codex below 0.166 or one that doesn't say
  (codexReadsWholeCatalog). docs/reference.md says so.
- docs/subsystems/agent-wiring.md: a "Codex instructions" row. The
  Antigravity cloak's comment no longer calls the GPT-5 prompt magpie's.

Run (mote's disk is full, 100% with other sessions' runs, so on WSL
magpie-desktop through wsl-run):
- go test ./internal/codexcat/: ok. go vet -tags nogui ./... and
  GOOS=windows go vet ./...: ok. gofmt -l: clean.
- go test -tags nogui -count=1 -timeout 45m codexcat, provider, agent,
  gui, gateway (every package that imports codexcat): codexcat, provider,
  gui ok. agent: TestGrokInstalled and TestOmpDetectedWithPiDir red, the
  same at origin/main 276c806db on the same box (#3548, #4033: WSL binfmt
  and install detection). gateway: TestCodexCatalogFitsCodexsLimit and
  TestCodexCatalogKeepsTheKeysUsedModelsFirst red from this change (the
  1 MiB cap, fixed above; both ok after, with every TestCodexCatalog*,
  TestCodexListed*, TestCodexApp*), and TestUnroutedHeldReportSaved, red
  only in the full run, ok alone here and at origin/main (#4300).
- Real Codex 0.166.0 (npm) in a sandbox HOME/CODEX_HOME, model_catalog_json
  from this codexcat, against a fake Responses gateway on 127.0.0.1:38611
  capturing each request: 0.166 sends the instructions as the first
  developer message (no top-level instructions). For magpie/gpt-6-astra it
  equals gpt-6-astra's instructions_template in Codex's models.json, byte
  for byte; for magpie/deepseek-v4-pro it equals prompt.md.

- After rebasing on origin/main 9da9bfabd: go vet (linux, windows) of
  codexcat, gateway, provider ok; codexcat ok; -run
  "Codex|Antigravity|Grok" in gateway and provider ok. Then rebased
  onto 546cdbde6 (7 commits in redact, gateway translation and a GUI
  test, none in codexcat or the Codex catalog) and not run again.

Broken by hand:
- The cap held for every client_version: both catalog tests FAIL (47
  listed for 0.166.0, 0.167.0-alpha.1 and 1.0.0).
- Old codex_prompt.md and Prompt everywhere: TestBundledIsCodexOwn,
  TestThirdPartyModelsTakeCodexOwnInstructions (all 7 slugs started "You
  are Codex, based on GPT-5"), TestThirdPartyModelsTakeCachedEntry,
  TestOwnEntryInstructions and TestCodexCatalogKeepsOwnEntries FAIL.
- The same real Codex run on origin/main's codexcat: both models' first
  developer message is the 7.5K GPT-5 prompt, differing from the official
  text at byte 15 (astra) and 8 (deepseek).

Not tried:
- GOOS=darwin go vet (needs mote, full).
- Reading the bundled catalog from the user's installed Codex binary;
  the snapshot is 0.166's, and a Codex release with new templates needs
  testdata and codex_instructions.json refreshed.
- An Antigravity account with Codex's unknown-model prompt ("You are a
  coding agent running in the Codex CLI"): the cloak breaks only the
  GPT-5 identity lines tried before.
- A remote Codex below 0.166 with many models through a gateway key: it
  lists fewer than before; the used ones FitOwn moves into the first 100
  can fall past the 48 that fit.
- A real ChatGPT account's models_cache.json entries (tested with a
  fixture cache).
2026-11-01 22:30:51 +08:00

204 lines
7.8 KiB
Go

package codexcat
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// Codex's own models, routed through magpie, keep what Codex knows of them
// — image input, context window, tools — under magpie's id; a third-party
// model gets the generic entry.
func TestCodexCatalogKeepsOwnEntries(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), []byte(`{"models":[
{"slug":"gpt-5.5","display_name":"GPT-5.5","priority":3,"visibility":"list","input_modalities":["text","image"],
"context_window":272000,"upgrade":{"model":"gpt-6"},"availability_nux":{"message":"new"}}]}`), 0o644)
var got struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(Catalog([]catalog.Model{
{ID: "deepseek/deepseek-chat", Name: "deepseek-chat · DeepSeek"},
{ID: "codex/gpt-5.5", Name: "GPT-5.5 · Codex", Efforts: []string{"low", "high"}},
}), &got); err != nil || len(got.Models) != 2 {
t.Fatalf("%v %v", err, got)
}
third, own := got.Models[0], got.Models[1]
if third["slug"] != "deepseek/deepseek-chat" || third["base_instructions"] != Prompt || len(third["input_modalities"].([]any)) != 1 {
t.Errorf("third-party entry: %v", third)
}
if own["slug"] != "codex/gpt-5.5" || own["display_name"] != "GPT-5.5 · Codex" || own["priority"] != float64(2) ||
own["context_window"] != float64(272000) || len(own["input_modalities"].([]any)) != 2 {
t.Errorf("own entry: %v", own)
}
if own["base_instructions"] != codexOwn(t)["gpt-5.5"] {
t.Error("own entry without Codex's own base_instructions for it")
}
if _, ok := own["upgrade"]; ok {
t.Error("upgrade prompt kept")
}
if _, ok := own["availability_nux"]; ok {
t.Error("start-up notice kept")
}
}
// A model that takes images says so, and Codex lets images be attached.
func TestCodexCatalogImages(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
var got struct {
Models []struct {
Modalities []string `json:"input_modalities"`
} `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "a/text", Name: "text"},
{ID: "a/vision", Name: "vision", Images: true},
}), &got)
if len(got.Models) != 2 || len(got.Models[0].Modalities) != 1 || len(got.Models[1].Modalities) != 2 || got.Models[1].Modalities[1] != "image" {
t.Errorf("%+v", got.Models)
}
}
// A model magpie describes has Codex search its MCP tools rather than send
// every one in each request (#258), with its prompt, context window, and
// neither code mode nor Responses Lite. Its whole window is told, and a
// window above the working one is compacted at that (auto_compact_token_
// limit), unless settings.FullContext.
func TestCodexCatalogToolSearch(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config"))
var got struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "group/auto-gemini-3-8-flash", Name: "auto", Context: 996147, Efforts: []string{"low", "medium", "high"}, Images: true},
}), &got)
if len(got.Models) != 1 {
t.Fatalf("%v", got)
}
e := got.Models[0]
if e["supports_search_tool"] != true || e["base_instructions"] != Prompt || e["context_window"] != float64(996147) || e["auto_compact_token_limit"] != float64(settings.WorkingWindow) || e["max_context_window"] != nil {
t.Errorf("entry: %v", e)
}
if err := settings.Save(settings.Settings{FullContext: true}); err != nil {
t.Fatal(err)
}
var full struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "group/auto-gemini-3-8-flash", Name: "auto", Context: 996147},
}), &full)
if e := full.Models[0]; e["context_window"] != float64(996147) || e["auto_compact_token_limit"] != nil || e["max_context_window"] != nil {
t.Errorf("full context: %v", e)
}
for _, off := range []string{"tool_mode", "use_responses_lite", "multi_agent_version", "model_messages"} {
if v, ok := e[off]; ok {
t.Errorf("%s = %v", off, v)
}
}
}
// Codex 0.147 refuses a catalog whose entries don't say whether a model
// takes parallel tool calls (#298), and later Codex ask for them anyway.
func TestCodexCatalogParallelToolCalls(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
var got struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{{ID: "fake/m1", Name: "m1"}, {ID: "group/auto", Name: "auto"}}), &got)
if len(got.Models) != 2 {
t.Fatalf("%v", got)
}
for _, e := range got.Models {
if e["supports_parallel_tool_calls"] != true {
t.Errorf("%v: supports_parallel_tool_calls = %v", e["slug"], e["supports_parallel_tool_calls"])
}
}
}
// Fast mode is offered for a ChatGPT account's GPT models, with Codex's own
// tiers when it lists the model, and for no one else's.
func TestCodexCatalogServiceTiers(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), []byte(`{"models":[
{"slug":"gpt-6-astra","display_name":"GPT-6 Astra","service_tiers":[{"id":"priority","name":"Fast","description":"2x speed, increased usage"}]}]}`), 0o644)
var got struct {
Models []struct {
Slug string `json:"slug"`
Tiers []struct {
ID string `json:"id"`
Description string `json:"description"`
} `json:"service_tiers"`
} `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "codex/gpt-6-astra", Name: "GPT-6 Astra · Codex"},
{ID: "codex/gpt-6-sol", Name: "GPT-6 Sol · Codex"},
{ID: "copilot/gpt-6-sol", Name: "GPT-6 Sol · Copilot"},
{ID: "openrouter/openai/gpt-6-sol", Name: "GPT-6 Sol · OpenRouter"},
}), &got)
if len(got.Models) != 4 {
t.Fatalf("%+v", got.Models)
}
if ts := got.Models[0].Tiers; len(ts) != 1 || ts[0].ID != "priority" || ts[0].Description != "2x speed, increased usage" {
t.Errorf("own entry tiers: %+v", ts)
}
if ts := got.Models[1].Tiers; len(ts) != 1 || ts[0].ID != "priority" {
t.Errorf("uncached account model tiers: %+v", ts)
}
for _, m := range got.Models[2:] {
if m.Tiers == nil || len(m.Tiers) != 0 {
t.Errorf("%s offers tiers: %+v", m.Slug, m.Tiers)
}
}
}
// Codex turns a pasted image away for a model without "image" itself, so
// /model says such a model is text-only and where that is changed, and the
// entry still reads as magpie's (KimYx0207 on X).
func TestCodexCatalogSaysTextOnly(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
var got struct {
Models []struct {
Slug string `json:"slug"`
Description string `json:"description"`
Modalities []string `json:"input_modalities"`
} `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "probe/text", Name: "Text"},
{ID: "probe/sees", Name: "Sees", Images: true},
{ID: "group/words", Name: "Words"},
{ID: "auto/found", Name: "Found"}, // a group magpie found, as it is listed
}), &got)
if len(got.Models) != 4 {
t.Fatalf("%+v", got.Models)
}
text, sees, group, found := got.Models[0], got.Models[1], got.Models[2], got.Models[3]
if !strings.Contains(text.Description, "text only") || !strings.Contains(text.Description, "Names & levels") || !catalog.MagpieAdded("", text.Slug, text.Description) {
t.Errorf("text-only model: %q", text.Description)
}
if sees.Description != "Sees via magpie" || group.Description != "Words via magpie" || found.Description != "Found via magpie" {
t.Errorf("a model that sees, a group and a found one: %q, %q, %q", sees.Description, group.Description, found.Description)
}
}