Files
magpie/internal/agent/codex_ccswitch_official_test.go
T
yetone 0ee424e3ee codex: a thread Codex 0.166 won't resume for its provider moves with Codex (毅, AcMilan on Discord)
Codex 0.166 (openai/codex#57840) refuses to resume a thread whose saved
provider isn't the configured one: "This chat uses openai, but your
current provider is magpie. Switch to openai to continue." (毅), and
"This chat uses cc-switch-official, but your current provider is
openai" (AcMilan). The rollout's session_meta provider wins over the
state DB row, and no -c override gets past it (openai/codex#58865).

Cause on magpie's side: bringOver relabelled only plain rollouts in
place. A thread Codex had packed to .jsonl.zst (a week after its last
turn) stayed on openai, so with magpie as provider it can't resume.
cc-switch-official is the id CC Switch 3.17-3.20 gave the official
account; CC Switch 4 moves those threads back to openai itself (#7948
there), but a user who left CC Switch keeps them stranded.

- sessions.MovePackedCodexThreads: packed threads on `from` updated in
  the last 30 days, newest first, up to 128 MB per sync, each moved by
  the existing packed rewrite with its copy kept in the trash. Rows
  are checked against the file (plain absent, .zst present, meta on
  `from`, same home). A state DB without updated_at moves nothing.
- bringOver calls it after RelabelCodexThreads; bringBack already
  moves packed threads back.
- bringOfficial: while magpie wires Codex and Codex is on openai or
  magpie, cc-switch-official threads move there, unless the user's
  top-level model_provider or a profile names cc-switch-official.
- Other providers and packed threads older than 30 days are left; the
  Sessions page's Move or Resume takes them.

Run (mote, at the pushed base c7bbd3faf):
- gofmt -l clean; go vet darwin, GOOS=linux, GOOS=windows on
  internal/agent and internal/sessions: ok.
- go test ./internal/agent ./internal/sessions: ok.
- Real codex-cli 0.166.0 app-server in a sandbox CODEX_HOME with a
  fake Responses server: an openai thread packed, Codex on magpie,
  thread/resume gave 毅's error word for word; RelabelCodexThreads
  alone left it; after MovePackedCodexThreads the resume said
  provider magpie and a turn started. A cc-switch-official thread with
  Codex on openai gave AcMilan's error; after the move it resumed on
  openai and a turn started.

Broken by hand (each red, then restored):
- budget check off: TestMovePackedCodexThreads red (both moved).
- the packed call in bringOver removed: TestCodexThreadsFollowMagpieIn
  red (packed thread stayed on openai).
- defer bringOfficial removed: TestCodexMovesCCSwitchOfficialThreads red.

Not tried: the Codex App GUI itself; agent Sync against a real Codex
(unit tests only); Windows and Linux runs; threads on other provider
ids, which 0.166 also refuses (left to the Sessions page).
2026-11-04 15:12:08 +08:00

78 lines
3.0 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
)
// AcMilan on Discord: beside the ChatGPT sign-in, Codex 0.166's app
// refused to reopen a chat ("This chat uses cc-switch-official, but your
// current provider is openai. Switch to cc-switch-official to
// continue"). CC Switch 3.17 to 3.20 sent the official account through
// its own proxy under that id; CC Switch 4 moves those threads back to
// OpenAI itself. magpie's sync moves them to the provider Codex is on,
// and leaves config.toml's table as it is; none moves while a profile
// picks that id, and a thread of a provider of the user's stays.
func TestCodexMovesCCSwitchOfficialThreads(t *testing.T) {
const (
official = "019a0000-0000-7000-8000-0000000000f1"
theirs = "019a0000-0000-7000-8000-0000000000f2"
)
table := "\n[model_providers.cc-switch-official]\nname = \"OpenAI\"\nbase_url = \"http://127.0.0.1:15721/v1\"\nwire_api = \"responses\"\nrequires_openai_auth = true\n"
signedIn := `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`
t.Run("beside the sign-in", func(t *testing.T) {
home, read := codexHome(t, signedIn, "model = \"gpt-5.4\"\n"+table)
dir := filepath.Join(home, ".codex")
path := codexThreadOn(t, dir, official, "cc-switch-official")
codexThreadOn(t, dir, theirs, "custom")
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(dir, "config.toml")
if p, _ := edit.GetTOMLTop(cfg, "model_provider"); p != "" {
t.Fatalf("provider %q, want Codex's own beside the sign-in:\n%s", p, read())
}
for id, want := range map[string]string{official: "openai", theirs: "custom"} {
if p := codexRowOn(t, dir, id); p != want {
t.Errorf("thread %s on %q, want %q", id, p, want)
}
}
if b, _ := os.ReadFile(path); !strings.Contains(string(b), `"model_provider":"openai"`) {
t.Fatalf("rollout:\n%s", b)
}
if tb, err := edit.GetTOMLTable(cfg, "model_providers.cc-switch-official"); err != nil || tb["name"] != "OpenAI" {
t.Fatalf("CC Switch's table changed: %v %v\n%s", tb, err, read())
}
})
t.Run("a profile picks it", func(t *testing.T) {
home, _ := codexHome(t, signedIn, "model = \"gpt-5.4\"\n"+table+"\n[profiles.ccs]\nmodel_provider = \"cc-switch-official\"\n")
dir := filepath.Join(home, ".codex")
codexThreadOn(t, dir, official, "cc-switch-official")
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
cx.Sync()
if p := codexRowOn(t, dir, official); p != "cc-switch-official" {
t.Fatalf("moved while a profile picks it: %q", p)
}
})
t.Run("not connected", func(t *testing.T) {
home, _ := codexHome(t, signedIn, "model = \"gpt-5.4\"\n"+table)
dir := filepath.Join(home, ".codex")
codexThreadOn(t, dir, official, "cc-switch-official")
codex(home).Sync()
if p := codexRowOn(t, dir, official); p != "cc-switch-official" {
t.Fatalf("moved with Codex not on magpie: %q", p)
}
})
}