Files
magpie/internal/sessions/codex_packed_move_test.go
T
yetone e9bde2520d codex: OpenAI's threads follow Codex onto magpie from a provider of the user's, and a move the budget stopped asks for the next sync (Lice9735 on X)
Lice9735 on X, Windows, magpie 0.1.3233, Codex app 26.1104: an old chat
(10-13) answered "模型提供商不匹配 — 此聊天使用的是 OpenAI,但你当前
的提供商是 magpie。请切换至 OpenAI 以继续", only a new chat worked,
after d620ff6b5 and d2c29abe7. Their config.toml has a provider table
of their own, openai-http.

Cause. The banner is the app-server's modelProviderMismatch on
thread/resume (app 26.1103: providerBlock is set only from that error;
codex 0.167 load_for_resume compares the stored thread's provider, the
row for a paginated thread, the rollout's session_meta for a legacy
one, with config.toml's). With Codex on a provider of the user's at the
takeover, the stash's codex.provider is that provider, and bringOver
moved only its threads: OpenAI's stayed on openai by design ("whose
threads bringBack would hand them to"). The Codex app lists them and
refuses each one on magpie. A second gap on the same path: the
rewriting moves (MovePackedCodexThreads, MoveRecentCodexThreads) go
128 MB a sync, newest first, and the rest waited for a sync nothing
asked for, which can be hours away.

- bringOver, with Codex on a provider of the user's before magpie (not
  OpenAI, magpie or cc-switch-official, and no profile naming OpenAI),
  moves OpenAI's threads to magpie too: in place, a paginated one by its
  row, the last 30 days rewritten. They are recorded in codex.lent with
  that provider as To, as lendOwn records its own: the Disconnect gives
  every magpie thread to that provider (bringBack), where Codex resumes
  them, and giveLent gives these back to OpenAI once Codex is on OpenAI
  again. One the app holds open waits (codexThreadsLater on openai).
- MovePackedCodexThreads and MoveRecentCodexThreads say more when the
  budget stopped them with a thread left, and bringOver, bringBeside and
  lendOwn ask for the next sync at once (codexThreadsAgain). The three
  share one mover now (codexOnto); bringBeside also waits on a held
  thread as the other two do. packedBudget is a var, for the test.
- Tests: TestCodexOpenAIThreadsFollowMagpieFromOwnProvider (new: an
  openai-http table named "OpenAI", a plain and a packed OpenAI thread,
  moved, lent, to openai-http at the Disconnect, back to OpenAI after;
  none while a profile names openai), TestCodexThreadsLeftByTheBudgetAskAgain
  (new), TestMovePackedCodexThreads checks more. TestCodexThreadsFollowMagpieIn's
  two user-provider subtests now expect OpenAI's thread on magpie, then
  on that provider after the Disconnect.
- docs/subsystems/agent-wiring.md says so.

Run:
- Real codex-cli 0.166.0 on mote, sandbox HOME and CODEX_HOME, a fake
  Responses server: a thread started on the built-in openai (API key,
  openai_base_url), a second after switching to model_provider
  openai-http (name "OpenAI"); both paginated. On openai-http the first
  was refused ("This chat uses openai, but your current provider is
  openai-http"). A throwaway test (not committed) ran magpie's takeover
  (Set fake/m1, Sync) on a copy of that home: with origin/main's code
  the openai row stayed and `codex exec resume` on magpie (the copy's
  base URLs pointed at the fake) failed with exactly "This chat uses
  openai, but your current provider is magpie"
  (modelProviderMismatch); with this change the row was magpie,
  codex.lent {"to":"openai-http",...}, and the resume answered.
- New and changed tests red on origin/main 191f3c0af (the openai thread
  on "openai", want "magpie"; after the Disconnect "openai", want the
  user's provider).
- Broken by hand: bringOver's `if more` dropped ->
  TestCodexThreadsLeftByTheBudgetAskAgain red ("0 sync(s) asked for");
  `more = true` turned false -> TestMovePackedCodexThreads red.
- mote: gofmt -l clean; GOOS=windows go vet and GOOS=linux go vet
  -tags nogui of internal/agent and internal/sessions ok;
  go test ./internal/sessions ./internal/agent ok at this head
  (this change on e6cdeac9c; rebased after onto 5dbdfc1bd, which
  changes internal/provider only), agent 292.2s, sessions 47.4s. Pushed and tagged
  without waiting for the full `go test -timeout 45m ./...`, per the
  owner's rule to ship each verified fix at once; it runs on mote after
  another session's suite, and a red it shows is filed or fixed in a
  follow-up.
- win-run (xiaomi-gpu), go test ./internal/sessions ./internal/agent
  (at the commit's parent before a rebase onto abe7b0063, which touches
  neither package): every test this change adds or touches passes; 29
  reds, 28 of them red the same at origin/main 191f3c0af run alone
  there. The 29th, TestHermesProfilesFollowMagpieModel, passes alone
  3/3 on this tree and is in #4115.

Not tried: the Codex app itself (the CLI's app-server gives the same
error the app shows); Windows with a real Codex; a legacy (non-paginated)
thread with a real Codex, covered by the unit tests only; a history
over 128 MB with real files. The chat the app has loaded at the switch
goes on once the app lets it go: fully quit the Codex app and reopen it
after magpie updates.
2026-11-07 18:08:39 +08:00

209 lines
7.7 KiB
Go

package sessions
import (
"bytes"
"database/sql"
"os"
"path/filepath"
"testing"
"time"
"github.com/klauspost/compress/zstd"
)
// 毅 on Discord: Codex 0.166's app refused to reopen a chat from before
// magpie ("This chat uses openai, but your current provider is magpie").
// RelabelCodexThreads moves the plain rollouts only; a packed one (.zst)
// stayed on OpenAI. MovePackedCodexThreads moves the packed ones with a
// turn since the time given, newest first, a copy of each kept, until the
// files moved come to the budget; an older one, a plain one (Relabel's)
// and one of another provider stay.
func TestMovePackedCodexThreads(t *testing.T) {
_, home := manageSetup(t)
const (
newest = "019a0000-0000-7000-8000-0000000000e1"
newer = "019a0000-0000-7000-8000-0000000000e2"
old = "019a0000-0000-7000-8000-0000000000e3"
plain = "019a0000-0000-7000-8000-0000000000e4"
theirs = "019a0000-0000-7000-8000-0000000000e5"
)
week := time.Now().Add(-8 * 24 * time.Hour).Truncate(time.Second)
paths := map[string]string{}
for _, id := range []string{newest, newer, old, plain, theirs} {
p := "openai"
if id == theirs {
p = "custom"
}
paths[id] = cxRollout(t, home, "sessions", id, p, week)
if id != plain {
pack(t, paths[id], false)
}
}
db := filepath.Join(home, "state_5.sqlite")
d, err := sql.Open("sqlite", db)
if err != nil {
t.Fatal(err)
}
if _, err := d.Exec(`CREATE TABLE threads (id TEXT PRIMARY KEY, rollout_path TEXT NOT NULL, model_provider TEXT NOT NULL, title TEXT NOT NULL, archived INTEGER NOT NULL DEFAULT 0, updated_at INTEGER NOT NULL)`); err != nil {
t.Fatal(err)
}
now := time.Now()
for id, ago := range map[string]time.Duration{newest: 8, newer: 9, old: 40, plain: 8, theirs: 8} {
p := "openai"
if id == theirs {
p = "custom"
}
// the row names the plain path, as Codex keeps it once packed
if _, err := d.Exec(`INSERT INTO threads VALUES (?, ?, ?, 't', 0, ?)`, id, paths[id], p, now.Add(-ago*24*time.Hour).Unix()); err != nil {
t.Fatal(err)
}
}
d.Close()
since := now.Add(-30 * 24 * time.Hour)
// a budget the first thread's file reaches: that one alone, and it
// says one is left, for its caller to come back for it
moved, more, err := MovePackedCodexThreads(home, "openai", "magpie", since, 1)
if err != nil {
t.Fatal(err)
}
if len(moved) != 1 || moved[0].ID != newest || moved[0].Backup == "" {
t.Fatalf("moved %+v, want the newest alone", moved)
}
if !more {
t.Fatal("stopped by the budget with a thread left, and said none was")
}
if _, err := os.Stat(filepath.Join(moved[0].Backup, "move.json")); err != nil {
t.Fatalf("no copy kept: %v", err)
}
// the next call goes on with the rest
if moved, more, err = MovePackedCodexThreads(home, "openai", "magpie", since, 64<<20); err != nil || len(moved) != 1 || moved[0].ID != newer || more {
t.Fatalf("again: %+v more %v %v", moved, more, err)
}
want := map[string]string{newest: "magpie", newer: "magpie", old: "openai", plain: "openai", theirs: "custom"}
for id, prov := range want {
if got, _, _ := threadProvider(t, db, id); got != prov {
t.Errorf("row %s on %q, want %q", id, got, prov)
}
f := paths[id]
if id != plain {
f += zstSuffix
}
if got := codexMetaProvider(f); got != prov {
t.Errorf("file %s on %q, want %q", filepath.Base(f), got, prov)
}
if fi, _ := os.Stat(f); !fi.ModTime().Equal(week) {
t.Errorf("%s: time %v, want %v kept", filepath.Base(f), fi.ModTime(), week)
}
}
// the messages are there, and the file is one Codex reads (zstd)
b, _ := os.ReadFile(paths[newest] + zstSuffix)
z, _ := zstd.NewReader(nil)
defer z.Close()
out, err := z.DecodeAll(b, nil)
if err != nil || !bytes.Contains(out, []byte("remember: the build uses make")) {
t.Fatalf("packed rollout after the move: %v\n%s", err, out)
}
// nothing left to move
if moved, more, err = MovePackedCodexThreads(home, "openai", "magpie", since, 64<<20); err != nil || len(moved) != 0 || more {
t.Fatalf("a third time: %+v more %v %v", moved, more, err)
}
}
// A state database whose threads have no updated_at (a Codex before it)
// moves none of them.
func TestMovePackedCodexThreadsNoUpdatedAt(t *testing.T) {
_, home := manageSetup(t)
const id = "019a0000-0000-7000-8000-0000000000e6"
day := time.Now().Add(-8 * 24 * time.Hour)
path := cxRollout(t, home, "sessions", id, "openai", day)
pack(t, path, false)
db := cxThreadRows(t, home, map[string]string{id: "openai"})
d, err := sql.Open("sqlite", db)
if err != nil {
t.Fatal(err)
}
if _, err := d.Exec(`UPDATE threads SET rollout_path = ?`, path); err != nil {
t.Fatal(err)
}
d.Close()
if moved, _, err := MovePackedCodexThreads(home, "openai", "magpie", time.Time{}, 64<<20); err != nil || len(moved) != 0 {
t.Fatalf("moved %+v %v", moved, err)
}
if got, _, _ := threadProvider(t, db, id); got != "openai" {
t.Fatalf("row on %q", got)
}
}
// hxy920830 on X: Codex had been on a provider of the user's own, a
// relay's table, and once magpie was its provider the app listed those
// threads and refused to open one ("This chat uses myrelay, but your
// current provider is magpie"). An id of another length than magpie's
// can't be written in place, so MoveRecentCodexThreads moves the recent
// ones, plain or packed, each rewritten with a copy kept; an older one
// and another provider's stay.
func TestMoveRecentCodexThreads(t *testing.T) {
_, home := manageSetup(t)
const (
plain = "019a0000-0000-7000-8000-0000000000f1"
packed = "019a0000-0000-7000-8000-0000000000f2"
old = "019a0000-0000-7000-8000-0000000000f3"
theirs = "019a0000-0000-7000-8000-0000000000f4"
)
week := time.Now().Add(-8 * 24 * time.Hour).Truncate(time.Second)
paths := map[string]string{}
on := map[string]string{plain: "myrelay", packed: "myrelay", old: "myrelay", theirs: "custom"}
for id, p := range on {
paths[id] = cxRollout(t, home, "sessions", id, p, week)
}
pack(t, paths[packed], false)
db := filepath.Join(home, "state_5.sqlite")
d, err := sql.Open("sqlite", db)
if err != nil {
t.Fatal(err)
}
if _, err := d.Exec(`CREATE TABLE threads (id TEXT PRIMARY KEY, rollout_path TEXT NOT NULL, model_provider TEXT NOT NULL, title TEXT NOT NULL, archived INTEGER NOT NULL DEFAULT 0, updated_at INTEGER NOT NULL)`); err != nil {
t.Fatal(err)
}
now := time.Now()
for id, ago := range map[string]time.Duration{plain: 8, packed: 9, old: 40, theirs: 8} {
if _, err := d.Exec(`INSERT INTO threads VALUES (?, ?, ?, 't', 0, ?)`, id, paths[id], on[id], now.Add(-ago*24*time.Hour).Unix()); err != nil {
t.Fatal(err)
}
}
d.Close()
since := now.Add(-30 * 24 * time.Hour)
moved, _, err := MoveRecentCodexThreads(home, "myrelay", "magpie", since, 64<<20)
if err != nil {
t.Fatal(err)
}
if len(moved) != 2 || moved[0].ID != plain || moved[1].ID != packed {
t.Fatalf("moved %+v, want the plain then the packed one", moved)
}
for _, mv := range moved {
if _, err := os.Stat(filepath.Join(mv.Backup, "move.json")); err != nil {
t.Fatalf("%s: no copy kept: %v", mv.ID, err)
}
}
want := map[string]string{plain: "magpie", packed: "magpie", old: "myrelay", theirs: "custom"}
for id, prov := range want {
if got, _, _ := threadProvider(t, db, id); got != prov {
t.Errorf("row %s on %q, want %q", id, got, prov)
}
f := paths[id]
if id == packed {
f += zstSuffix
}
if got := codexMetaProvider(f); got != prov {
t.Errorf("file %s on %q, want %q", filepath.Base(f), got, prov)
}
}
if b, _ := os.ReadFile(paths[plain]); !bytes.Contains(b, []byte("remember: the build uses make")) {
t.Fatalf("plain rollout after the move:\n%s", b)
}
// the packed-only move of the same leaves a plain thread
if moved, _, err = MovePackedCodexThreads(home, "custom", "magpie", since, 64<<20); err != nil || len(moved) != 0 {
t.Fatalf("packed-only: %+v %v", moved, err)
}
}