Files
magpie/internal/sessions/codex_provider_stuck_test.go
T
yetone 71a806749d sessions: a paginated Codex rollout moves to a longer provider id by its row (@xMopf7RF on X)
@xMopf7RF on X (tweet 2121441393424113800): the Sessions page's Move
refused an old Codex session with "rollout-2026-03-09T09-25-….jsonl.zst
keeps its history by byte, and "magpie" is longer than the provider it
names: Codex couldn't open it moved", and offered nothing else.

Since 67b12956a a paginated rollout's session_meta is only rewritten at
its own length (Codex keeps places in it by byte), and MoveCodexProvider
refused a longer id outright. But Codex reads a paginated thread's
provider from its row in the state database, never from its file
(codex-rs read_thread; RetagPagedCodexThreads already moves such threads
by the row alone, tried with the real codex 0.166). So now
rewriteCodexProvider leaves such a line as it was and says so (kept),
and moveCodexPaths moves the thread by its row. Every caller gets it:
the Sessions page's Move, Resume's move first, and the sync's
MoveCodexThreads/MoveRecentCodexThreads, which met the same refusal for
a provider shorter than magpie.

With no row to move it by, it can't move at all: it is refused as
sessions.StuckError, every file put back and no trash copy left, and the
API says stuck with the provider it stays on. The page then says why,
that the session stays on that provider unchanged and shows in Codex when
Codex uses it again, and that Delete clears it into magpie's trash, where
it can be restored. New string in zh, zh-TW, ja and de.

Run, at 7951989c6 (on origin/main e0889719c):
- go test -count=1 -run 'TestMoveCodexProvider|TestRetagPaged|TestPortCodex'
  ./internal/sessions/ on mote: ok.
- Old code back (codex_provider.go from e0889719c, StuckError stubbed):
  TestMoveCodexProviderPagedByRow and TestMoveCodexProviderPagedNoRow
  FAIL with the reporter's exact error text; green with the change.
- node --test sessions-codex-provider-stuck, sessions-codex-provider,
  sessions-codex-provider-pick, i18n-keys, i18n-en-no-cjk on mote,
  Chromium and WebKit, five languages, 900px and 420px: 42 of 42 pass.
  The new GUI file with the old client.js: 0 of 20 pass.
- gofmt -l on the changed packages: nothing.
- Full suite (vet x3, builds x3, go test -tags nogui ./... -timeout 45m)
  queued on mote's suite lock; pushed before it ran, per the owner's
  ship-at-once rule. Its result goes in a follow-up.

Not tried: a real Codex resuming a thread moved this way. The row-only
move is the one RetagPagedCodexThreads makes, which was tried with codex
0.166.0 exec resume (docs/subsystems/agent-wiring.md). Not reproduced:
the no-row case with real Codex, where Codex 0.166 keeps a row for every
paginated thread; it is covered by a fixture only.
2026-11-14 11:42:17 +08:00

116 lines
4.2 KiB
Go

package sessions
import (
"bytes"
"database/sql"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/klauspost/compress/zstd"
)
// pagedPackedSetup is @xMopf7RF's case (X, 11-14): a packed, paginated
// rollout from months ago whose session_meta names a provider shorter
// than magpie, with Codex on magpie now. It gives the rollout's path and
// its bytes, and with row, the state database Codex keeps its row in.
func pagedPackedSetup(t *testing.T, row bool) (path string, b []byte, db *sql.DB) {
t.Helper()
_, codex := manageSetup(t)
src := filepath.Join(codex, "sessions", "2026", "09", "20", "rollout-2026-09-20T15-48-28-"+mgCX+".jsonl")
plain, err := os.ReadFile(src)
if err != nil {
t.Fatal(err)
}
os.Remove(src)
plain = bytes.Replace(plain, []byte(`"model_provider":"openai"`), []byte(`"model_provider":"zai","history_mode":"paginated"`), 1)
enc, _ := zstd.NewWriter(nil)
b = enc.EncodeAll(plain, nil)
path = filepath.Join(codex, "sessions", "2026", "03", "09", "rollout-2026-03-09T09-25-41-"+mgCX+".jsonl.zst")
os.MkdirAll(filepath.Dir(path), 0o755)
if err := os.WriteFile(path, b, 0o644); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-200 * 24 * time.Hour).Truncate(time.Second)
os.Chtimes(path, old, old)
os.WriteFile(filepath.Join(codex, "config.toml"), []byte("model_provider = \"magpie\"\n\n[model_providers.magpie]\nname = \"magpie\"\n"), 0o644)
if !row {
return path, b, nil
}
db, err = sql.Open("sqlite", filepath.Join(codex, "state_5.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
if _, err := db.Exec(`CREATE TABLE threads (id TEXT PRIMARY KEY, rollout_path TEXT NOT NULL, model_provider TEXT NOT NULL, history_mode TEXT NOT NULL DEFAULT 'legacy');
INSERT INTO threads VALUES (?, ?, 'zai', 'paginated')`, mgCX, path); err != nil {
t.Fatal(err)
}
return path, b, db
}
// A paginated rollout keeps its history by byte, so "magpie" can't be
// written in place of a shorter id in it. The move was refused for it
// ("keeps its history by byte, and "magpie" is longer than the provider
// it names: Codex couldn't open it moved", @xMopf7RF on X), though Codex
// reads such a thread's provider from its row, never from its file: it
// moves by its row, its file as it was, and moves back the same way.
func TestMoveCodexProviderPagedByRow(t *testing.T) {
path, b, db := pagedPackedSetup(t, true)
row := func() string {
var s string
if err := db.QueryRow(`SELECT model_provider FROM threads WHERE id = ?`, mgCX).Scan(&s); err != nil {
t.Fatal(err)
}
return s
}
if m, _ := findManaged(ListAgent("codex"), mgCX); m.Provider != "zai" || m.UsesProvider != "magpie" {
t.Fatalf("listed %q, uses %q", m.Provider, m.UsesProvider)
}
mv, err := MoveCodexProvider(mgCX, "magpie")
if err != nil {
t.Fatal(err)
}
if row() != "magpie" || len(mv.DB) != 1 || len(mv.Files) != 0 || mv.From != "zai" {
t.Fatalf("row %q, moved %+v", row(), mv)
}
if now, _ := os.ReadFile(path); !bytes.Equal(now, b) {
t.Fatal("the paginated rollout was written")
}
if m, _ := findManaged(ListAgent("codex"), mgCX); m.Provider != "magpie" {
t.Fatalf("listed on %q after the move", m.Provider)
}
if _, err := MoveCodexProvider(mgCX, "zai"); err != nil || row() != "zai" {
t.Fatalf("back: %v, row %q", err, row())
}
if now, _ := os.ReadFile(path); !bytes.Equal(now, b) {
t.Fatal("moving back wrote the rollout")
}
}
// With no row in Codex's database to move it by, such a thread can't be
// moved: the error says why and what can be done, and nothing is left
// changed, not even a copy in magpie's trash.
func TestMoveCodexProviderPagedNoRow(t *testing.T) {
path, b, _ := pagedPackedSetup(t, false)
_, err := MoveCodexProvider(mgCX, "magpie")
var stuck *StuckError
if !errors.As(err, &stuck) || stuck.From != "zai" || stuck.To != "magpie" {
t.Fatalf("err %v", err)
}
for _, want := range []string{"history by byte", "no row", "stays on zai", "Delete"} {
if !strings.Contains(err.Error(), want) {
t.Fatalf("%q doesn't say %q", err, want)
}
}
if now, _ := os.ReadFile(path); !bytes.Equal(now, b) {
t.Fatal("a refused move changed the file")
}
if left := providerTrash(t); len(left) != 0 {
t.Fatalf("trash kept %v", left)
}
}