Files
magpie/internal/agent/codex_threads_over_test.go
T
yetone de54b4ff99 codex: threads started on OpenAI are listed while magpie is Codex's provider
notnico64 on Discord (Linux, magpie desktop): with Codex connected to
magpie, `codex resume` said "No sessions yet". Codex's resume picker
(tui/src/resume_picker.rs picker_provider_filter) lists only threads
whose model_provider is the one Codex is on now, with no opt-out, and
the filesystem scan checks each rollout's session_meta, not just the
state DB row. With no ChatGPT sign-in (also an API key, magpie API, an
account out of allowance, another machine/WSL) magpie writes
model_provider = "magpie", so every thread started on "openai" vanished
from the list. Nothing in it is Linux-only; macOS does the same.

Now, when magpie becomes Codex's provider and Codex was on OpenAI
before (not on a provider of the user's), the threads on "openai" move
to "magpie": the state DB row and the rollout's session_meta, rewritten
in place (both ids are 6 bytes), the file's mtime kept so the picker's
order holds, with no trash copy. At the Disconnect bringBack relabels
them back the same way before its usual mover. Threads Codex has
already packed (.zst, a week after the last turn) are left alone:
rewriting gigabytes of history at a switch isn't worth it (the owner's
~/.codex/sessions is 51GB, 11,591 of 12,255 rollouts packed). Files
written within ActiveWindow, outside this Codex home, or without their
id in the name are skipped too.

Run:
- Real Codex 0.162.1 in a sandbox HOME/CODEX_HOME (never ~/.codex):
  without model_provider the picker listed only the openai thread;
  with model_provider = "magpie" only the magpie one (the report).
  After RelabelCodexThreads(home, "openai", "magpie") the same picker
  listed both threads.
- TestCodexThreadsFollowMagpieIn (new) is red on the old code:
  "connected: thread ...c1 on "openai", want "magpie"".
- go test -tags nogui -timeout 45m ./internal/agent ./internal/sessions
  at this head: ok agent 480s, ok sessions 41s (rebased on 9298d2a7c)
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
  both packages clean.

Broken by hand: dropping the prior-provider guard turns the
"provider of the user's" subtest red; dropping the Chtimes turns the
mtime check red.

Not tried: the reporter's own machine and why magpie became their
provider; Linux itself; the Codex desktop app's thread list; a full
`go test ./...`. Packed threads stay out of the list while magpie is
the provider.
2026-10-26 22:38:57 +08:00

121 lines
4.4 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. Threads Codex
// has packed (.zst) stay, a provider of the user's stays, and OpenAI's
// stay when Codex was on another provider before magpie.
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")
packThread(t, codexThreadOn(t, dir, packed, "openai"))
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: "openai", theirs: "custom"} {
if p := codexRowOn(t, dir, id); p != want {
t.Errorf("connected: thread %s on %q, want %q", id, p, want)
}
}
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)
}
// 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)
}