Files
magpie/internal/agent/codex_lend_threads_test.go
T
yetone 1d704f8190 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.
2026-11-06 00:51:01 +08:00

90 lines
3.4 KiB
Go

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)
}
}
}