agents: a Codex thread started on any provider table goes through magpie when reopened, not only CC Switch's

A report the owner relayed from WeChat (10-26): in Codex, with apikeyfan's
gpt-6.1-sol on, an old thread failed every turn with "unexpected status 404
Not Found: Model "apikeyfan/gpt-6.1-sol" is not supported by any configured
account in this group, url: https://slb.apikey.fan/responses". A new thread
worked, and updating magpie didn't help. The url is Codex's own call: Codex
reopens a thread on the provider it was started on, and that thread's table
pointed straight at the relay (no /v1, so not a table magpie wrote), so the
magpie model id went to the relay. 6c5ed2acf pointed only CC Switch's
"custom"/"cc-switch-N" tables at magpie; a table of the user's own, or one
named after a CC Switch card, kept going to the relay.

Now any [model_providers.<id>] table a Codex thread was started on (the
state database's threads) goes through magpie while a magpie model is on,
the same way: base_url to magpie's /v1, the old URL kept in the stash and
given back with Codex's own model or a reset, the table's other keys as they
were. Left alone: tables no thread names, tables a profile names, magpie's,
Codex's built-ins and CC Switch's OpenAI mirror. The state database is read
only when a table outside CC Switch's ids has a base_url, once per check.

Run at this head (4a33cf0a9 + this):
- TestCodexTakesThreadTables (new) is red on the old codex.go: "thread's
  table not through magpie". TestCodexWritesTablesThreadsName, changed to
  expect the user's thread table through magpie, is red on the old code too.
- go test -tags nogui ./internal/agent -run Codex: ok.
- gofmt -l, go vet (darwin, GOOS=windows), go build (darwin, GOOS=linux): ok.

Not tried: the reporter's own config.toml (its table's id isn't known, so a
quoted header or a profile-named table would still be left alone), the full
suite, and a real Codex app reopening a thread.
This commit is contained in:
yetone
2026-10-26 22:02:03 +08:00
parent 4a33cf0a98
commit 4e3078ee47
2 changed files with 94 additions and 13 deletions
+30 -11
View File
@@ -592,25 +592,44 @@ func codexIn(at place) *Agent {
}
return named
}
// CC Switch's provider tables in Codex's config, each one's base URL
// by its id. A thread keeps the provider it was
// started on, and CC Switch moves every third-party thread onto its
// "custom" one, so one reopened there still went to the relay, a
// magpie model picked in it too, which the relay didn't know. While a
// magpie model is on, those tables go through magpie as well; a table
// a profile names is the user's to pick, and stays as it is.
// The provider tables in Codex's config a reopened thread goes to,
// each one's base URL by its id: CC Switch's, and any other a Codex
// thread was started on. A thread keeps the provider it was started
// on, and CC Switch moves every third-party thread onto its "custom"
// one, so one reopened there still went to the relay, a magpie model
// picked in it too, which the relay didn't know. A table of the user's
// own, or one a newer CC Switch names after its card, did the same:
// an old thread sent "apikeyfan/gpt-6.1-sol" straight to the relay
// ("not supported by any configured account") while a new one worked.
// While a magpie model is on, those tables go through magpie as well;
// a table a profile names is the user's to pick, and stays as it is,
// and so does one no thread was started on.
ccSwitchTables := func() map[string]string {
names, _ := edit.TOMLTables(path)
named := profileNamed(names)
out := map[string]string{}
var threads []string
read := false
for _, n := range names {
id, ok := strings.CutPrefix(n, "model_providers.")
if !ok || !ccSwitchProvider.MatchString(id) || named[id] || isCCSwitchMirror(id) {
if !ok || id == magpieID || codexBuiltinProvider[id] || named[id] || isCCSwitchMirror(id) {
continue
}
if t, _ := edit.GetTOMLTable(path, n); t["base_url"] != "" {
out[id] = t["base_url"]
t, _ := edit.GetTOMLTable(path, n)
if t["base_url"] == "" {
continue
}
if !ccSwitchProvider.MatchString(id) {
// the state database is read only when there is a table
// it can decide, and once
if !read {
threads, read = sessions.CodexThreadProviders(dir), true
}
if !slices.Contains(threads, id) {
continue
}
}
out[id] = t["base_url"]
}
return out
}
@@ -1465,7 +1484,7 @@ func codexIn(at place) *Agent {
}
for id, u := range ccSwitchTables() {
if u != at.v1() {
return "Codex's [model_providers." + id + "] (CC Switch's) sends to " + u + ", not magpie's gateway: a Codex thread started on it goes there when reopened, a magpie model picked in it too"
return "Codex's [model_providers." + id + "] sends to " + u + ", not magpie's gateway: a Codex thread started on it goes there when reopened, a magpie model picked in it too"
}
}
return ""
+64 -2
View File
@@ -71,8 +71,10 @@ func TestCodexWritesTablesThreadsName(t *testing.T) {
if !strings.Contains(read(), inline) || strings.Contains(read(), "[model_providers.inl]") {
t.Fatalf("auth %q: inline provider:\n%s", auth, read())
}
if !strings.Contains(read(), mine) || strings.Count(read(), "[model_providers.custom]") != 1 {
t.Fatalf("auth %q: user's table changed or custom twice:\n%s", auth, read())
// the user's own table a thread was started on goes through magpie
// too, its other keys as they were (TestCodexTakesThreadTables)
if m := table("mine"); m["base_url"] != v1 || m["name"] != "mine" || strings.Count(read(), "[model_providers.custom]") != 1 {
t.Fatalf("auth %q: user's table not through magpie or custom twice:\n%s", auth, read())
}
if d := cx.Check(); d != "" {
t.Fatalf("auth %q: check: %s", auth, d)
@@ -102,3 +104,63 @@ func TestCodexThreadTablesOnlyWired(t *testing.T) {
t.Fatalf("\n%s", read())
}
}
// A Codex thread started on a provider table of the user's own (or one a
// newer CC Switch names after its card) is reopened on that table. With a
// magpie model on, Codex sent "apikeyfan/gpt-6.1-sol" straight to the
// relay, which answered 404 "not supported by any configured account",
// while a new thread worked (a report relayed by the owner, 10-26). The
// table goes through magpie while a magpie model is on and gets its own
// base URL back with Codex's own model; a table no thread was started on,
// and one a profile names, stay as they are.
func TestCodexTakesThreadTables(t *testing.T) {
for _, auth := range []string{`{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, ""} {
conf := "model = \"gpt-5.4\"\n\n" +
"[model_providers.apikeyfan]\nname = \"apikeyfan\"\nbase_url = \"https://slb.relay.example\"\nwire_api = \"responses\"\nexperimental_bearer_token = \"sk-relay\"\n\n" +
"[model_providers.unused]\nname = \"unused\"\nbase_url = \"https://unused.example/v1\"\n\n" +
"[model_providers.work]\nname = \"work\"\nbase_url = \"https://work.example/v1\"\n\n" +
"[profiles.w]\nmodel_provider = \"work\"\n"
home, read := codexHome(t, auth, conf)
dir := filepath.Join(home, ".codex")
codexThreads(t, dir, "apikeyfan", "work")
path := filepath.Join(dir, "config.toml")
table := func(id string) map[string]string {
tb, err := edit.GetTOMLTable(path, "model_providers."+id)
if err != nil {
t.Fatal(err)
}
return tb
}
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
v1 := here(home).v1()
if a := table("apikeyfan"); a["base_url"] != v1 || a["experimental_bearer_token"] != "sk-relay" || a["wire_api"] != "responses" {
t.Fatalf("auth %q: thread's table not through magpie:\n%s", auth, read())
}
if table("unused")["base_url"] != "https://unused.example/v1" || table("work")["base_url"] != "https://work.example/v1" {
t.Fatalf("auth %q: other tables changed:\n%s", auth, read())
}
if d := cx.Check(); d != "" {
t.Fatalf("auth %q: check: %s", auth, d)
}
// pointed away again: the row says so
if err := edit.SetTOMLKey(path, "model_providers.apikeyfan", "base_url", "https://slb.relay.example"); err != nil {
t.Fatal(err)
}
if d := cx.Check(); !strings.Contains(d, "[model_providers.apikeyfan]") {
t.Fatalf("auth %q: check: %q", auth, d)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
// Codex's own model: the relay's URL is back
if err := cx.Fields[0].Set("gpt-5.4"); err != nil {
t.Fatal(err)
}
if table("apikeyfan")["base_url"] != "https://slb.relay.example" {
t.Fatalf("auth %q: own model:\n%s", auth, read())
}
}
}