agents: Codex leaves a model unchecked in its provider's list (JNDliuliuM on X)

JNDliuliuM: after picking new models for a provider, Codex CLI didn't
change, and after a restart it was still on the old model. magpie wrote
the new list, but Codex's `model` (config.toml, or magpie.config.toml for
`codex -p magpie`) stayed on the model the user had unchecked. Resolve
still takes any provider/model, so Codex kept starting on it and asking
the vendor for it.

Tried with the real codex-cli 0.165.0 in a sandbox CODEX_HOME, profile
wiring: the list was swapped from sub/old-a, sub/old-b to sub/new-x,
sub/new-y. After a restart, /model listed only the new ones, but the
status line still read sub/old-a, as the profile named it.

What changed:
- provider.Unpicked(id) reports a model the user has since unchecked:
  the provider has picks, its served models lack the model, and it isn't
  a decision model. This is the same rule ResolveRoute already applies
  to routing groups (#3911).
- agent.pickedInstead moves such a model to the first model of that
  provider that magpie lists for the agent now.
- Codex's Sync calls it before its other model branches, through set, as
  picking the model does. The profile's Sync calls it through put. The
  Codex app reads the same config.toml, so it follows too.
- Left alone:
  - a provider with no picks, whose list is the vendor's and may lack
    the model (#2642);
  - a provider with none of its models listed any more.

Run on mote:
- TestCodexLeavesAModelUncheckedInItsProvider (as provider, beside a
  ChatGPT sign-in, profile) and TestCodexKeepsAModelOfAProviderWithNoPicks;
- go test ./internal/agent/ ./internal/provider/;
- go vet for darwin, linux and windows on both packages;
- gofmt.
Result: internal/agent ok (470s). internal/provider is red only on
TestHostBuildsNoList (a Gemini provider's Host allocates 5 times), which
is #6479 and was already red on main.

Broken by hand: with the Codex Sync call disabled, the "as its provider"
and "beside the sign-in" subtests are red ("model = sub/old after sub/old
was unchecked"). With the profile call disabled, the "by its profile"
subtest is red.

Not tried:
- the fixed magpie with a real Codex;
- the Codex app.

Not changed, filed as #6504: other agents' model fields, and Codex's
subagent and compaction models.
This commit is contained in:
yetone
2026-11-13 17:37:48 +08:00
parent f4a73672ef
commit 80c22eecba
6 changed files with 163 additions and 0 deletions
File diff suppressed because one or more lines are too long
+8
View File
@@ -2129,6 +2129,14 @@ func codexIn(at place) *Agent {
if err := failover(); err != nil {
return err
}
// a magpie model the user has since unchecked in its
// provider's list: Codex, the app and the CLI, goes on to the
// provider's first pick, as picking it here does (pickedInstead)
if m := get("model"); isMagpie(m) {
if to := pickedInstead("codex", m); to != "" {
return set(to)
}
}
// magpie API on one of Codex's own models, as an older magpie
// left it unwired: wired again, as picking it does now (#701);
// not one the user disconnected, which this left the same
+4
View File
@@ -168,6 +168,10 @@ func codexProfileIn(at place) *Agent {
}
return nil
}
// a model the user has since unchecked in its provider's list
if to := pickedInstead("codex", get("model")); to != "" {
return put(to)
}
t, _ := edit.GetTOMLTable(path, table)
if t["base_url"] != at.v1() || t["experimental_bearer_token"] != codexKeptToken(path, table, at.gwKey()) {
return put(get("model"))
+32
View File
@@ -0,0 +1,32 @@
package agent
import (
"strings"
"github.com/yetone/magpie/internal/provider"
)
// pickedInstead is the model an agent's model id moves to when the user
// has since unchecked it in its provider's model list (provider.Unpicked):
// the first of that provider's models magpie lists for the agent now. ""
// leaves it where it is: a model still picked, one of a provider with no
// picks (its list is the vendor's, and a request may name a model it
// lacks), or a provider with none of its models listed any more.
//
// JNDliuliuM on X: picking new models for a provider changed Codex's list,
// but Codex CLI, restarted too, still started on the old model, which
// magpie had left in its config, and Resolve still served it. The user
// picked that model through magpie and has since taken it off the
// provider's list, so the provider's picks are what they chose last.
func pickedInstead(agent, id string) string {
pid, ok := provider.Unpicked(id)
if !ok {
return ""
}
for _, m := range sharedModels(agent) {
if strings.HasPrefix(m.ID, pid+"/") && m.ID != id {
return m.ID
}
}
return ""
}
+93
View File
@@ -0,0 +1,93 @@
package agent
import (
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// JNDliuliuM on X: new models picked for a provider changed Codex's list,
// but Codex CLI, restarted, still started on the old model: its config
// kept the model the user had since unchecked. It goes on to the
// provider's first pick, in each way magpie wires Codex: as its provider,
// beside a ChatGPT sign-in (the CLI and the app read the same config) and
// by its profile alone (codex -p magpie).
func TestCodexLeavesAModelUncheckedInItsProvider(t *testing.T) {
signedIn := `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`
for _, c := range []struct {
name, auth, scope string
file string
}{
{"as its provider", "", "", "config.toml"},
{"beside the sign-in", signedIn, "", "config.toml"},
{"by its profile", "", "profile", codexProfileFile},
} {
t.Run(c.name, func(t *testing.T) {
home, _ := codexHome(t, c.auth, "")
if c.scope != "" {
stash(map[string]string{codexScopeKey: c.scope})
}
sub := provider.Provider{ID: "sub", Name: "Sub", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"old", "keep"}}
if err := provider.Save(sub); err != nil {
t.Fatal(err)
}
if err := codex(home).Fields[0].Set("sub/old"); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".codex", c.file)
model := func() string { v, _ := edit.GetTOMLTop(path, "model"); return v }
if model() != "sub/old" {
t.Fatalf("not set to begin with:\n%s", readFile(path))
}
// another of the provider's picks taken out: Codex stays
sub.Models = []string{"old", "new"}
if err := provider.Save(sub); err != nil {
t.Fatal(err)
}
SyncCatalog()
if got := model(); got != "sub/old" {
t.Fatalf("model = %q while sub/old is still picked", got)
}
// the one Codex is on taken out, a new one picked
sub.Models = []string{"new", "keep"}
if err := provider.Save(sub); err != nil {
t.Fatal(err)
}
SyncCatalog()
if got := model(); got != "sub/new" {
t.Fatalf("model = %q after sub/old was unchecked, want the provider's first pick sub/new:\n%s", got, readFile(path))
}
if l := readFile(filepath.Join(home, ".codex", "magpie-models.json")); c.auth == "" && (!strings.Contains(l, `"sub/new"`) || strings.Contains(l, `"sub/old"`)) {
t.Errorf("Codex's list is not the provider's picks:\n%s", l)
}
})
}
}
// A provider with no picks lists the vendor's models, and a request may name
// one its list lacks (#2642): Codex on such a model stays on it.
func TestCodexKeepsAModelOfAProviderWithNoPicks(t *testing.T) {
home := syncHome(t)
if err := provider.Save(provider.Provider{ID: "open", Name: "Open", Key: "k", Chat: "http://127.0.0.1:1/v1"}); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "sub", Name: "Sub", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a"}}); err != nil {
t.Fatal(err)
}
if err := codex(home).Fields[0].Set("open/unlisted"); err != nil {
t.Fatal(err)
}
SyncCatalog()
path := filepath.Join(home, ".codex", "config.toml")
if v, _ := edit.GetTOMLTop(path, "model"); v != "open/unlisted" {
t.Errorf("model = %q, want open/unlisted kept:\n%s", v, readFile(path))
}
if pid, ok := provider.Unpicked("open/unlisted"); ok {
t.Errorf("Unpicked(open/unlisted) = %q, true for a provider with no picks", pid)
}
}
+25
View File
@@ -1428,6 +1428,31 @@ func ResolveRoute(id string) (Provider, string, bool) {
return p, m, true
}
// Unpicked reports whether id, "provider/model", names a model the user
// has since unchecked in that provider's list (unpicked), and which
// provider: Resolve still takes it, so an agent left on it goes on asking
// the vendor for the old model, while the user picked the provider's
// others (JNDliuliuM on X: Codex CLI still on it after a restart).
func Unpicked(id string) (pid string, ok bool) {
entries := providerEntries()
p, m, found := resolveIn(entries, ThroughSelf(strings.TrimSuffix(strings.TrimSpace(id), "[1m]")))
if !found {
return "", false
}
find := func(pid, model string) int {
for i := range entries {
if entries[i].Provider.ID == pid && entries[i].Model == model {
return i
}
}
return -1
}
if !unpicked(find, p, m) {
return "", false
}
return p.ID, true
}
// groupEntries are the catalog's groups: each with a member ready, named
// as the user named it, answering for its first member when an agent asks
// what the model can do, and reasoning when any member does — a member