Files
magpie/internal/agent/codex_threads_paged_test.go
T
yetone d620ff6b54 codex: an old OpenAI thread follows Codex onto magpie by its row, and back at the Disconnect (Parry_Moody, Cyrus)
Parry_Moody and Cyrus on Discord: Codex on OpenAI through an API key
before magpie, and for two days the Codex app answered every older
conversation "继续聊天未成功: This chat uses openai, but your current
provider is magpie. Switch to openai to continue."

Cause, from Codex's source (rust-v0.167.0-alpha.3.1, same in 0.166.0):
every thread Codex 0.166 starts is paginated (history_mode 'paginated'
in its state-DB row), and for those thread-store read_thread takes the
provider from the row, never the rollout ("Paginated history may contain
only a suffix, so its display metadata lives in SQLite"). The app sends
thread/resume with modelProvider null (ChatGPT.app 26.1021, Kct in
app-shared), so the row alone decides, and load_for_resume refuses a row
other than the current provider. bringOver moved threads by rewriting
files: plain ones in place, packed ones only from the last 30 days and
128 MB a sync. An older or packed OpenAI thread kept an openai row.

Now sessions.RetagPagedCodexThreads moves every paginated thread on a
provider by its row alone: any age, packed or not, an id of any length,
no file read, written or copied. Each thread is held by its writer lock
(an open one stays: its writer would write the provider back), one
written to in the last minute waits, 200 rows a transaction. It runs
after the in-place relabel in bringOver, bringBack (so a Disconnect no
longer copies a whole paginated history to the trash), bringOfficial and
bringBeside, and giveBeside by ids (RetagPagedCodexThreadIDs). Legacy
threads keep the file path. The Sessions page lists a paginated thread
on its row's provider (codexPagedProviders, memoised on the DB and WAL
stamp), so it isn't offered a Move it had.

Run:
- real codex-cli 0.166.0 (npm latest) on mote in a sandbox CODEX_HOME,
  a fake Responses server: a thread started with OpenAI (API key,
  openai_base_url) is paginated, row openai; with model_provider magpie
  `codex exec resume` failed with exactly "This chat uses openai, but
  your current provider is magpie" (modelProviderMismatch). After
  magpie's RetagPagedCodexThreads on that home (a throwaway test calling
  it, not committed) it resumed and answered; the rollout still names
  openai. Reversed (magpie -> openai) with config back on OpenAI, it
  resumed again. The first retag try moved nothing: the rollout had been
  written <1 minute before, the ActiveWindow skip, as intended.
- mote: gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
  for ./internal/sessions ./internal/agent; go test ./internal/sessions
  ./internal/agent: ok (sessions 20.1s, agent 194.4s) on mote, plus go vet on darwin, linux -tags nogui and windows

Broken by hand:
- RetagPagedCodexThreads returning nil (the old behaviour):
  TestCodexPagedThreadsFollowByRow red (250 packed 90-day-old threads
  "on openai, want magpie"), TestRetagPagedCodexThreads red.
- the writer-lock hold skipped: TestCodexPagedThreadsFollowByRow red,
  the open thread moved to magpie.

Not tried: the Codex app GUI itself (the app goes through the same
app-server thread/resume, whose modelProvider it sends null); Windows
and Linux runs (vet only). Codex rebuilds a row from its rollout when a
search listing reconciles it, which puts a retagged thread back on its
file's provider until the next sync; not seen, read from the source.
A legacy thread packed over 30 days still waits for Sessions' Move.
2026-11-05 16:11:14 +08:00

141 lines
4.7 KiB
Go

package agent
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/filelock"
"github.com/yetone/magpie/internal/settings"
)
// Parry_Moody and Cyrus on Discord: Codex on OpenAI through an API key
// before magpie, and for two days every older conversation in the Codex
// app said "继续聊天未成功: This chat uses openai, but your current
// provider is magpie. Switch to openai to continue". Codex 0.166 on keeps
// a thread paginated (history_mode "paginated"), and reads its provider
// from its row in the state database, not from its files (codex-rs
// thread-store read_thread: "Paginated history may contain only a suffix,
// so its display metadata lives in SQLite"); resume refuses a provider
// other than the one Codex is on. The file moves left every packed thread
// older than 30 days, and the rest of a big history past a sync's budget,
// on OpenAI.
//
// Every paginated thread of OpenAI's now moves to magpie by its row
// alone, of any age, its files neither rewritten nor copied, and back at
// the Disconnect the same way. One a Codex has open stays, as its writer
// would put its provider back, and so does a legacy thread past 30 days
// (its provider is its file's, the Sessions page moves it).
func TestCodexPagedThreadsFollowByRow(t *testing.T) {
const (
old = "019a0000-0000-7000-8000-0000000000d1"
open = "019a0000-0000-7000-8000-0000000000d2"
legacy = "019a0000-0000-7000-8000-0000000000d3"
theirs = "019a0000-0000-7000-8000-0000000000d4"
)
home, _ := codexHome(t, "", "model = \"gpt-5.4\"\n")
dir := filepath.Join(home, ".codex")
db, err := sql.Open("sqlite", filepath.Join(dir, "state_5.sqlite"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
// as Codex 0.166's migration 0040 adds it
if _, err := db.Exec(`CREATE TABLE threads (id TEXT PRIMARY KEY, rollout_path TEXT NOT NULL, model_provider TEXT NOT NULL, model TEXT, updated_at INTEGER NOT NULL DEFAULT 0, history_mode TEXT NOT NULL DEFAULT 'legacy')`); err != nil {
t.Fatal(err)
}
// a history past a chunk, all packed and three months old
var many []string
for i := range 250 {
many = append(many, fmt.Sprintf("019a0000-0000-7000-8000-%012x", 0xe00+i))
}
ago := time.Now().Add(-90 * 24 * time.Hour)
stamp := map[string]time.Time{}
for _, id := range append(append([]string{old, open, legacy}, many...), theirs) {
p := "openai"
if id == theirs {
p = "custom"
}
path := codexThreadOn(t, dir, id, p)
os.Chtimes(path, ago, ago)
packThread(t, path)
fi, _ := os.Stat(path + ".zst")
stamp[id] = fi.ModTime()
mode := "paginated"
if id == legacy {
mode = "legacy"
}
if _, err := db.Exec(`UPDATE threads SET history_mode = ?, updated_at = ? WHERE id = ?`, mode, ago.Unix(), id); err != nil {
t.Fatal(err)
}
}
// a Codex has open (codex-rs rollout writer_lock.rs)
locks := filepath.Join(dir, "thread-writer-locks")
os.MkdirAll(locks, 0o755)
lf, err := os.OpenFile(filepath.Join(locks, open+".lock"), os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
t.Fatal(err)
}
if got, err := filelock.TryLock(lf); !got {
t.Fatal("lock:", err)
}
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 an API key", p)
}
want := map[string]string{old: "magpie", open: "openai", legacy: "openai", theirs: "custom"}
for _, id := range many {
want[id] = "magpie"
}
check := func(when string) {
t.Helper()
for id, w := range want {
if p := codexRowOn(t, dir, id); p != w {
t.Errorf("%s: thread %s on %q, want %q", when, id, p, w)
}
}
for id, at := range stamp {
z := filepath.Join(dir, "sessions", "2026", "10", "21", "rollout-2026-10-21T10-00-00-"+id+".jsonl.zst")
fi, err := os.Stat(z)
if err != nil || !fi.ModTime().Equal(at) {
t.Fatalf("%s: thread %s's file written: %v", when, id, err)
}
on := "openai"
if id == theirs {
on = "custom"
}
if p := zstMetaProvider(t, z); p != on {
t.Fatalf("%s: thread %s's file names %q", when, id, p)
}
}
if l, _ := filepath.Glob(filepath.Join(settings.Dir(), "trash", "codex-provider", "*")); len(l) > 0 {
t.Fatalf("%s: copied to the trash: %v", when, l)
}
}
check("connected")
// let go, the open one follows at the next sync
filelock.Unlock(lf)
lf.Close()
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
want[open] = "magpie"
check("let go")
if err := cx.Unwire(); err != nil {
t.Fatal(err)
}
for id, w := range want {
if w == "magpie" {
want[id] = "openai"
}
}
check("disconnected")
}