Files
magpie/internal/agent/codex_effort_test.go
T
yetone 77aabb9a46 agent, codexcat: the effort new sessions are set to lasts a switch of model in Codex, Claude Code and Grok Build, at the model's nearest level where it lacks it (Creat.wu on Discord)
Creat.wu set magpie's "New sessions" effort to the highest level. A new
session started at it, but picking another model (a routing group) dropped
the effort back to the model's default.

Codex: on a switch, the TUI's /model highlights and saves the picked model's
preset default_reasoning_effort. The desktop app keeps the current effort only
if the new model lists it, and otherwise uses defaultReasoningEffort. Both come
from magpie's catalog default_reasoning_level, which codexcat.DefaultEffort
always set to the middle level. A group's levels are the intersection of its
members' levels, so a group often lacks xhigh or max.
- The catalog default is now codexcat.ModelDefault: the model_reasoning_effort
  set in config.toml when the model takes it. Otherwise it is the model's
  nearest level (catalog.NearestEffort: by rank, a tie going up, never
  "none"), so xhigh on low/high/max gives max. When the effort is left at
  Default, the default is still the middle of the road.
- This covers both magpie-models.json and the list the gateway hands a
  signed-in Codex. The effort is now part of that list's tag, so Codex asks
  for the list again.
- Setting the effort in magpie rewrites the list, or marks Codex's cached
  list stale.
- An effort the model picked in magpie doesn't take now settles on the
  nearest level, not the middle.
- The gateway's fitEffort uses the same NearestEffort.

Claude Code: Opus 5.5 and later read only
modelSettings.<model>.effortLevel. A level set while on one of them was
written only there, so a routing group or another vendor's model picked
afterwards (which reads the top effortLevel) started at its own default.
- The top effortLevel is now written too.
- A reset takes the top one out only when it is the level written beside
  the model's own; a top level the user set themselves stays.
- max was already written to the env and applied to every model.

Grok Build: picking a model without the chosen default_reasoning_effort
deleted the key. It now keeps the model's nearest level.

Checked and not affected:
- WorkBuddy, ZCode, Kimi and MiniMax keep per-model levels (keptEffort).
- Qoder and Command Code keep one level for each model by their own design.
- AtomCode keeps one model table each.

What was run:
- Real codex 0.162.1, `codex debug models` with CODEX_HOME in the
  scratchpad, on a catalog from CatalogAt(..., "xhigh"). It renders
  default_reasoning_level max for low/high/max, high for a low/medium/high
  group and xhigh for a model that has xhigh.
- New tests TestCodex/Grok/ClaudeNewSessionEffortLastsModelSwitch,
  TestNearestEffort and TestModelDefault. The three agent tests, plus the
  updated TestCodexCustomProviderModelHasEfforts and
  TestClaudeEffortModelSettings, fail on the old code: the source files were
  checked out from the parent with the tests kept.
- gofmt -l is clean. go vet -tags nogui ./internal/... passes for GOOS
  darwin, linux and windows.
- go test -tags nogui: internal/agent, catalog, codexcat and provider pass in
  full before the rebase.
- In gateway, only TestOTelWaterfallIncludesUnbilledFallback failed. The
  root span was in a second export, which is #1794, fixed on main by
  6ae6b3402.
- After rebasing on origin/main, catalog and codexcat pass in full. gateway
  -run 'Effort|OTel|Reasoning' and agent -run 'Effort|Codex|Claude|Grok'
  pass.

Not tried: the Codex desktop app's picker by hand. Its behaviour was read
from ChatGPT.app's app.asar.
2026-10-12 23:18:32 +08:00

161 lines
5.4 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// A custom provider serving a model models.dev knows from others (a
// Volcengine endpoint's glm-5.3-flash) hands Codex the reasoning levels
// those give it: the catalog entry had none, so the Codex app offered no
// effort and magpie's own control showed a level Codex never used.
func TestCodexCustomProviderModelHasEfforts(t *testing.T) {
home, read := codexHome(t, "", "model_reasoning_effort = \"none\"\n")
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{
"zai":{"models":{"glm-5.3-flash":{"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":["low","high","max"]}]},
"glm-4.5-air":{"id":"glm-4.5-air"}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "volc", Name: "Volc", Key: "k", Chat: "http://127.0.0.1:1/api/v3",
Models: []string{"glm-5.3-flash", "glm-4.5-air"}}); err != nil {
t.Fatal(err)
}
cx := codex(home)
if err := cx.Fields[0].Set("volc/glm-5.3-flash"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_reasoning_effort = "low"`) {
t.Fatalf("effort not settled on the nearest level:\n%s", cfg)
}
b, _ := os.ReadFile(filepath.Join(home, ".codex", "magpie-models.json"))
var cat struct {
Models []struct {
Slug string `json:"slug"`
Default *string `json:"default_reasoning_level"`
Levels []struct {
Effort string `json:"effort"`
} `json:"supported_reasoning_levels"`
} `json:"models"`
}
if err := json.Unmarshal(b, &cat); err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, m := range cat.Models {
var ls []string
for _, l := range m.Levels {
ls = append(ls, l.Effort)
}
d := ""
if m.Default != nil {
d = *m.Default
}
got[m.Slug] = strings.Join(ls, ",") + " / " + d
}
if got["volc/glm-5.3-flash"] != "low,high,max / low" {
t.Errorf("glm-5.3-flash: %q", got["volc/glm-5.3-flash"])
}
// a model no one says reasons keeps none
if got["volc/glm-4.5-air"] != " / " {
t.Errorf("glm-4.5-air: %q", got["volc/glm-4.5-air"])
}
effort := cx.Fields[1]
var opts []string
ef := effort.Options(map[string]string{"model": "volc/glm-5.3-flash"})
for _, o := range ef {
opts = append(opts, o.Value)
}
if strings.Join(opts, ",") != ",low,high,max" {
t.Errorf("effort options: %v", opts)
}
// Default says what Codex takes then: the catalog's default
if ef[0].Takes != "high" {
t.Errorf("default takes %q", ef[0].Takes)
}
// unset, the control shows Default, which is what was picked
if err := effort.Set(""); err != nil {
t.Fatal(err)
}
if v := effort.Get(); v != "" {
t.Errorf("unset effort shows %q", v)
}
// a level the model doesn't take, left from before, is settled when
// the catalog is synced
if err := effort.Set("none"); err != nil {
t.Fatal(err)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_reasoning_effort = "low"`) {
t.Errorf("sync left the effort:\n%s", cfg)
}
}
// Default on Codex's effort control stays Default (lgtm on Discord: picked
// on Codex · WSL Ubuntu, it turned into medium): an effort left unset is
// not written in when a model is picked or the catalog synced, and the
// control reads it as Default, with the level Codex then takes beside it —
// for Codex's own model, its own default (gpt-6.1-sol's low), not medium.
func TestCodexDefaultEffortStaysDefault(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), []byte(`{"models":[
{"slug":"gpt-6.1-sol","display_name":"GPT-6.1 Sol","priority":1,"default_reasoning_level":"low",
"supported_reasoning_levels":[{"effort":"low"},{"effort":"medium"},{"effort":"high"}]}]}`), 0o644)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{
"zai":{"models":{"glm-5.3-flash":{"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":["low","high","max"]}]}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "volc", Name: "Volc", Key: "k", Chat: "http://127.0.0.1:1/api/v3",
Models: []string{"glm-5.3-flash"}}); err != nil {
t.Fatal(err)
}
cx := codex(home)
model, effort := cx.Fields[0], cx.Fields[1]
unset := func(when string) {
t.Helper()
if cfg := read(); strings.Contains(cfg, "model_reasoning_effort") {
t.Errorf("%s: Default written over:\n%s", when, cfg)
}
if v := effort.Get(); v != "" {
t.Errorf("%s: Default shows as %q", when, v)
}
}
if err := model.Set("volc/glm-5.3-flash"); err != nil {
t.Fatal(err)
}
unset("magpie's model picked")
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
unset("catalog synced")
if err := model.Set("gpt-6.1-sol"); err != nil {
t.Fatal(err)
}
unset("Codex's own model picked")
if d := effort.Options(map[string]string{"model": "gpt-6.1-sol"})[0]; d.Value != "" || d.Takes != "low" {
t.Errorf("Codex's own model's default: %+v", d)
}
// a level set that the model doesn't take is still settled
if err := effort.Set("max"); err != nil {
t.Fatal(err)
}
if err := model.Set("gpt-6.1-sol"); err != nil {
t.Fatal(err)
}
if v := effort.Get(); v == "max" || v == "" {
t.Errorf("max left on a model without it: %q", v)
}
}