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).
158 lines
5.6 KiB
Go
158 lines
5.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// notnico64 on Discord: with Codex connected to magpie, `codex resume`
|
|
// said "No sessions yet". Codex's resume picker (and the app's thread
|
|
// list) shows only the threads of the provider Codex is on, and with no
|
|
// ChatGPT sign-in magpie is Codex's provider, so every thread started on
|
|
// OpenAI before was out of the list. Tried with Codex 0.162.1's real
|
|
// picker: model_provider = "magpie" listed the magpie thread alone.
|
|
//
|
|
// Now the threads Codex started on OpenAI move to magpie while it is
|
|
// Codex's provider, in place (the id is one length), the file's time
|
|
// kept, and back at the Disconnect, with no copy of each. A provider of
|
|
// the user's stays, and OpenAI's stay when Codex was on another provider
|
|
// before magpie.
|
|
//
|
|
// Threads Codex has packed (.zst) stayed on OpenAI, which Codex 0.166's
|
|
// app refuses to resume on magpie ("This chat uses openai, but your
|
|
// current provider is magpie", 毅 on Discord). A packed one with a turn
|
|
// in the last 30 days moves too, rewritten with a copy kept, and back.
|
|
func TestCodexThreadsFollowMagpieIn(t *testing.T) {
|
|
const (
|
|
plain = "019a0000-0000-7000-8000-0000000000c1"
|
|
packed = "019a0000-0000-7000-8000-0000000000c2"
|
|
theirs = "019a0000-0000-7000-8000-0000000000c3"
|
|
)
|
|
t.Run("on OpenAI before", func(t *testing.T) {
|
|
home, _ := codexHome(t, "", "model = \"gpt-5.4\"\n")
|
|
dir := filepath.Join(home, ".codex")
|
|
plainPath := codexThreadOn(t, dir, plain, "openai")
|
|
packedPath := codexThreadOn(t, dir, packed, "openai")
|
|
packThread(t, packedPath)
|
|
codexThreadOn(t, dir, theirs, "custom")
|
|
before, _ := os.Stat(plainPath)
|
|
cx := codex(home)
|
|
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _ := edit.GetTOMLTop(filepath.Join(dir, "config.toml"), "model_provider"); p != "magpie" {
|
|
t.Fatalf("provider %q, want magpie as Codex's provider with no sign-in", p)
|
|
}
|
|
// listed where Codex is now, at the switch itself
|
|
for id, want := range map[string]string{plain: "magpie", packed: "magpie", theirs: "custom"} {
|
|
if p := codexRowOn(t, dir, id); p != want {
|
|
t.Errorf("connected: thread %s on %q, want %q", id, p, want)
|
|
}
|
|
}
|
|
if p := zstMetaProvider(t, packedPath+".zst"); p != "magpie" {
|
|
t.Fatalf("packed rollout on %q, want magpie", p)
|
|
}
|
|
if l, _ := filepath.Glob(filepath.Join(settings.Dir(), "trash", "codex-provider", "*"+packed, "move.json")); len(l) != 1 {
|
|
t.Fatalf("packed thread moved with no copy kept: %v", l)
|
|
}
|
|
b, _ := os.ReadFile(plainPath)
|
|
if !strings.Contains(string(b), `"model_provider":"magpie"`) {
|
|
t.Fatalf("rollout still on OpenAI:\n%s", b)
|
|
}
|
|
after, _ := os.Stat(plainPath)
|
|
if !after.ModTime().Equal(before.ModTime()) || after.Size() != before.Size() {
|
|
t.Fatalf("file changed beyond its provider: %v %d, was %v %d", after.ModTime(), after.Size(), before.ModTime(), before.Size())
|
|
}
|
|
// a sync leaves them so
|
|
if err := cx.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := codexRowOn(t, dir, plain); p != "magpie" {
|
|
t.Fatalf("after a sync: %q", p)
|
|
}
|
|
if err := cx.Unwire(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for id, want := range map[string]string{plain: "openai", packed: "openai", theirs: "custom"} {
|
|
if p := codexRowOn(t, dir, id); p != want {
|
|
t.Errorf("disconnected: thread %s on %q, want %q", id, p, want)
|
|
}
|
|
}
|
|
if b2, _ := os.ReadFile(plainPath); string(b2) != strings.Replace(string(b), `"model_provider":"magpie"`, `"model_provider":"openai"`, 1) {
|
|
t.Fatalf("rollout not as it was:\n%s", b2)
|
|
}
|
|
if p := zstMetaProvider(t, packedPath+".zst"); p != "openai" {
|
|
t.Fatalf("packed rollout back on %q, want openai", p)
|
|
}
|
|
// moved in place both ways: no copy of the history in the trash
|
|
if l, _ := filepath.Glob(filepath.Join(settings.Dir(), "trash", "codex-provider", "*"+plain)); len(l) > 0 {
|
|
t.Fatalf("copied: %v", l)
|
|
}
|
|
})
|
|
t.Run("on a provider of the user's before", func(t *testing.T) {
|
|
cfg := "model = \"m\"\nmodel_provider = \"custom\"\n\n[model_providers.custom]\nname = \"custom\"\nbase_url = \"https://relay.example/v1\"\nwire_api = \"responses\"\n"
|
|
home, _ := codexHome(t, "", cfg)
|
|
dir := filepath.Join(home, ".codex")
|
|
codexThreadOn(t, dir, plain, "openai")
|
|
codexThreadOn(t, dir, theirs, "custom")
|
|
cx := codex(home)
|
|
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cx.Sync()
|
|
for id, want := range map[string]string{plain: "openai", theirs: "custom"} {
|
|
if p := codexRowOn(t, dir, id); p != want {
|
|
t.Errorf("thread %s on %q, want %q", id, p, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// packThread packs a rollout as Codex does a week after its last turn:
|
|
// the .zst beside it, the plain file gone, its row still naming the plain
|
|
// path.
|
|
func packThread(t *testing.T, path string) {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fi, _ := os.Stat(path)
|
|
z, _ := zstd.NewWriter(nil)
|
|
if err := os.WriteFile(path+".zst", z.EncodeAll(b, nil), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
z.Close()
|
|
os.Chtimes(path+".zst", fi.ModTime(), fi.ModTime())
|
|
os.Remove(path)
|
|
}
|
|
|
|
// zstMetaProvider is the model_provider of a packed rollout's session_meta.
|
|
func zstMetaProvider(t *testing.T, path string) string {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
z, _ := zstd.NewReader(nil)
|
|
defer z.Close()
|
|
out, err := z.DecodeAll(b, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
line, _, _ := strings.Cut(string(out), "\n")
|
|
_, rest, ok := strings.Cut(line, `"model_provider":"`)
|
|
if !ok {
|
|
t.Fatalf("no provider in %s", line)
|
|
}
|
|
p, _, _ := strings.Cut(rest, `"`)
|
|
return p
|
|
}
|