codex: OpenAI's threads follow a provider of the user's at the top, where Codex resumes them

harris on Discord (the Codex app, after switching from CC Switch):
"This chat uses openai, but your current provider is custom. Switch to
openai to continue." CC Switch left its "custom" as config.toml's
model_provider, which magpie keeps since #4347, and Codex 0.166 resumes
a thread only on the provider it is on now. Every thread started on
OpenAI before was refused, and switching to OpenAI would refuse
custom's threads instead, so the user was stuck. Nothing moved them:
bringOver moves only onto magpie, bringBeside only from such a provider
onto OpenAI.

lendOwn (each Sync) moves OpenAI's threads onto that provider, as
bringBeside does the other way: in place at OpenAI's length, by the row
for a paginated thread, the last 30 days rewritten with a copy kept
otherwise. The ids go in stash codex.lent; giveLent returns exactly
those to OpenAI once Codex is on it again (CC Switch's official card),
keeps the record while Codex is on that provider or on magpie, and
drops it on any other. Nothing moves while a profile names openai,
after the user disconnected Codex, or from a distro's Codex.
TestCodexThreadsComeBackOffMagpie/by_hand expected an OpenAI thread to
stay on OpenAI with custom set by hand at the top, the stuck state
itself; it now expects it on custom. docs/subsystems/agent-wiring.md
says so.

Run (mote, go 1.26.8): go test -count=1 ./internal/agent/ ok (250s);
GOOS=windows, GOOS=linux and native go vet ./internal/agent/ ok;
gofmt -l clean.
Broken by hand: Sync without lendOwn, TestCodexOwnProviderTakesOpenAIThreads
red (thread left on "openai", rollout not on custom); without the
disconnected and profile guards, TestCodexOwnProviderLeavesOpenAIThreads
red for both cases.
Not tried: a real Codex app resuming a moved thread on a real relay;
harris's config.toml wasn't seen, so the "custom" here is CC Switch's
relay table as it writes it. A thread moved from the ChatGPT backend to
a relay carries its encrypted reasoning, which a relay on another
account may not accept; the thread then fails on the relay as a
switch to it by hand would.
This commit is contained in:
yetone
2026-11-06 00:51:01 +08:00
parent 845e324b13
commit 1d704f8190
4 changed files with 205 additions and 3 deletions
File diff suppressed because one or more lines are too long
+111
View File
@@ -1359,6 +1359,115 @@ func codexIn(at place) *Agent {
// writes nothing of its own back (paynezhuang on Discord: the base URL
// was back after every update)
disconnected := func() bool { return stashShared()[at.key("codex"+disconnectedKey)] == "1" }
// lent are the threads lendOwn moved off OpenAI, and the provider of
// the user's they went to
type lentMove struct {
To string `json:"to"`
IDs []string `json:"ids"`
}
lent := func() lentMove {
var l lentMove
json.Unmarshal([]byte(stashShared()[at.key("codex.lent")]), &l)
return l
}
// lendOwn is bringBeside the other way: Codex on a provider of the
// user's at the top (CC Switch's "custom", left there when the user
// came to magpie, which keeps it since #4347), the threads it started
// on OpenAI before could not be continued at all. Codex 0.166 resumes
// a thread only on the provider it is on now ("This chat uses openai,
// but your current provider is custom. Switch to openai to continue",
// harris on Discord, the Codex app after CC Switch), and switching
// would refuse every thread of theirs instead. Those threads move to
// that provider, where Codex resumes them, as bringBeside moves that
// provider's onto OpenAI: in place when the id is OpenAI's length, the
// last 30 days of them rewritten with a copy kept otherwise, a
// paginated one by its row. Each one moved is kept (codex.lent) for
// giveLent to give back once Codex is on OpenAI again. None while a
// profile is chosen (Codex lists that one's threads) or names OpenAI,
// none once the user disconnected Codex from magpie, and none from a
// distro's Codex.
lendOwn := func() {
if at.spell != nil || disconnected() {
return
}
to := get("model_provider")
if to == "" || to == "openai" || to == magpieID || to == ccSwitchOfficial || isCCSwitchMirror(to) || sessions.CodexProviderIn(dir) != to {
return
}
names, _ := edit.TOMLTables(path)
if profileNamed(names)["openai"] {
return
}
var ids []string
if len(to) == len("openai") {
moved, err := sessions.RelabelCodexThreads(dir, "openai", to)
ids = append(ids, moved...)
if err != nil {
log.Printf("codex: moving OpenAI's threads to %s: %v", to, err)
}
}
retagged, err := sessions.RetagPagedCodexThreads(dir, "openai", to)
ids = append(ids, retagged...)
if err != nil {
log.Printf("codex: moving OpenAI's paginated threads to %s: %v", to, err)
}
move := sessions.MovePackedCodexThreads
if len(to) != len("openai") {
move = sessions.MoveRecentCodexThreads
}
packed, err := move(dir, "openai", to, time.Now().Add(-packedSince), packedBudget)
for _, m := range packed {
ids = append(ids, m.ID)
}
if err != nil {
log.Printf("codex: moving OpenAI's threads to %s: %v", to, err)
}
if len(ids) == 0 {
return
}
log.Printf("codex: %d thread(s) started on OpenAI moved to %s, the provider Codex is on, where it resumes them", len(ids), to)
l := lent()
if l.To != to {
l = lentMove{To: to}
}
for _, id := range ids {
if !slices.Contains(l.IDs, id) {
l.IDs = append(l.IDs, id)
}
}
out, _ := json.Marshal(l)
stash(map[string]string{at.key("codex.lent"): string(out)})
}
// giveLent gives the threads lendOwn moved back to OpenAI once Codex
// is on OpenAI again (CC Switch's official card, the user's provider
// taken out): those still on the provider they went to. While Codex is
// on that provider, or on magpie (bringOver took them there, and
// bringBack gives them to the provider Codex goes back to), the record
// stays; on any other provider it goes, and they stay where they are.
giveLent := func() {
l := lent()
if l.To == "" {
return
}
now := sessions.CodexProviderIn(dir)
if now == l.To || now == magpieID {
return
}
if now == "openai" && at.spell == nil {
retagged, err := sessions.RetagPagedCodexThreadIDs(dir, l.To, "openai", l.IDs)
if err != nil {
log.Printf("codex: moving paginated threads back from %s to OpenAI: %v", l.To, err)
}
moved, err := sessions.MoveCodexThreadIDs(dir, l.To, "openai", l.IDs)
if n := len(moved) + len(retagged); n > 0 {
log.Printf("codex: %d thread(s) started on OpenAI moved back from %s", n, l.To)
}
if err != nil {
log.Printf("codex: moving threads back from %s to OpenAI: %v", l.To, err)
}
}
forget(at.key("codex.lent"))
}
// Codex on one of its own models goes through magpie too while more of
// its ChatGPT accounts are on there, so one out of its allowance hands
// the turn to the next; with none, or once the user disconnected Codex,
@@ -1849,6 +1958,8 @@ func codexIn(at place) *Agent {
Sync: func() error {
// after the sync, which may have put Codex back beside its
// sign-in or made magpie its provider, and whatever it failed at
defer lendOwn()
defer giveLent()
defer followBeside()
defer bringBeside()
defer bringOfficial()
+89
View File
@@ -0,0 +1,89 @@
package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
)
// harris on Discord (the Codex app, after CC Switch): "This chat uses
// openai, but your current provider is custom. Switch to openai to
// continue". CC Switch left its "custom" at the top of config.toml, which
// magpie keeps since #4347, and Codex 0.166 resumes a thread only on the
// provider it is on: every thread started on OpenAI before was refused,
// and switching to OpenAI would refuse custom's instead. A sync moves
// OpenAI's threads to the provider Codex is on, and gives them back once
// Codex is on OpenAI again; custom's own threads stay on custom.
func TestCodexOwnProviderTakesOpenAIThreads(t *testing.T) {
const (
openai = "019a0000-0000-7000-8000-0000000000e1"
theirs = "019a0000-0000-7000-8000-0000000000e2"
)
cfg := "model_provider = \"custom\"\nmodel = \"gpt-5.4\"\n\n" +
"[model_providers.custom]\nname = \"custom\"\nbase_url = \"https://relay.example/v1\"\nwire_api = \"responses\"\nrequires_openai_auth = true\n"
home, read := codexHome(t, chatGPTAuth, cfg)
dir := filepath.Join(home, ".codex")
cfgPath := filepath.Join(dir, "config.toml")
openaiPath := codexThreadOn(t, dir, openai, "openai")
codexThreadOn(t, dir, theirs, "custom")
cx := codex(home)
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if p, _ := edit.GetTOMLTop(cfgPath, "model_provider"); p != "custom" {
t.Fatalf("model_provider %q after a sync, want the user's custom kept:\n%s", p, read())
}
for id, want := range map[string]string{openai: "custom", theirs: "custom"} {
if p := codexRowOn(t, dir, id); p != want {
t.Errorf("synced: thread %s on %q, want %q", id, p, want)
}
}
if b, _ := os.ReadFile(openaiPath); !strings.Contains(string(b), `"model_provider":"custom"`) {
t.Fatalf("rollout not on custom:\n%s", b)
}
// CC Switch's official card: Codex is on OpenAI again
if err := edit.DelTOMLTop(cfgPath, "model_provider"); err != nil {
t.Fatal(err)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
for id, want := range map[string]string{openai: "openai", theirs: "custom"} {
if p := codexRowOn(t, dir, id); p != want {
t.Errorf("back on OpenAI: thread %s on %q, want %q", id, p, want)
}
}
if _, ok := stashLoad()[here(home).key("codex.lent")]; ok {
t.Error("record left after the threads went back")
}
}
// None moves while a profile names OpenAI, which the user picks to go on
// with those threads there, nor once the user disconnected Codex.
func TestCodexOwnProviderLeavesOpenAIThreads(t *testing.T) {
const id = "019a0000-0000-7000-8000-0000000000e3"
table := "[model_providers.custom]\nname = \"custom\"\nbase_url = \"https://relay.example/v1\"\nwire_api = \"responses\"\n"
for name, c := range map[string]struct {
cfg string
stash map[string]string
}{
"profile": {cfg: "model_provider = \"custom\"\n\n" + table + "\n[profiles.official]\nmodel_provider = \"openai\"\n"},
"disconnected": {cfg: "model_provider = \"custom\"\n\n" + table, stash: map[string]string{"codex" + disconnectedKey: "1"}},
} {
home, _ := codexHome(t, chatGPTAuth, c.cfg)
dir := filepath.Join(home, ".codex")
codexThreadOn(t, dir, id, "openai")
if c.stash != nil {
stash(c.stash)
}
if err := codex(home).Sync(); err != nil {
t.Fatal(err)
}
if p := codexRowOn(t, dir, id); p != "openai" {
t.Errorf("%s: thread on %q, want it left on openai", name, p)
}
}
}
+4 -2
View File
@@ -133,11 +133,13 @@ func TestCodexThreadsComeBackOffMagpie(t *testing.T) {
t.Errorf("thread %s on %q, want %q", id, p, want)
}
}
// CC Switch's provider set by hand: a thread left on magpie goes there
// CC Switch's provider set by hand: a thread left on magpie goes
// there, and so does OpenAI's, which Codex resumes there alone
// (lendOwn)
codexThreadOn(t, dir, before, "magpie")
os.WriteFile(path, []byte("model = \"gpt-5.4\"\nmodel_provider = \"custom\"\n"+table+"\n[model_providers.custom]\nname = \"custom\"\nbase_url = \"https://relay.example/v1\"\n"), 0o644)
cx.Sync()
for id, want := range map[string]string{mine: "openai", theirs: "custom", before: "custom"} {
for id, want := range map[string]string{mine: "custom", theirs: "custom", before: "custom"} {
if p := codexRowOn(t, dir, id); p != want {
t.Errorf("thread %s on %q, want %q", id, p, want)
}