diff --git a/internal/agent/codex.go b/internal/agent/codex.go index 201d04453..2ac14a53a 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -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 "" diff --git a/internal/agent/codex_thread_tables_test.go b/internal/agent/codex_thread_tables_test.go index eefaba18d..795b9cba6 100644 --- a/internal/agent/codex_thread_tables_test.go +++ b/internal/agent/codex_thread_tables_test.go @@ -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()) + } + } +}