diff --git a/internal/agent/codex.go b/internal/agent/codex.go index 2ac14a53a..bcdcc660d 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -550,6 +550,17 @@ func codexIn(at place) *Agent { if to == magpieID { return } + // in place first where it can be (bringOver's, and those started + // on magpie that Codex hasn't packed): no copy of each, no rewrite + if len(to) == len(magpieID) { + moved, err := sessions.RelabelCodexThreads(dir, magpieID, to) + if len(moved) > 0 { + log.Printf("codex: %d thread(s) on magpie moved back to %s, where Codex lists them", len(moved), to) + } + if err != nil { + log.Println("codex: moving magpie's threads back:", err) + } + } moved, err := sessions.MoveCodexThreads(dir, magpieID, to) if len(moved) > 0 { log.Printf("codex: %d thread(s) started on magpie moved to %s, where Codex lists them", len(moved), to) @@ -558,6 +569,36 @@ func codexIn(at place) *Agent { log.Println("codex: moving magpie's threads:", err) } } + // bringOver is bringBack the other way: while magpie is Codex's + // provider, the threads Codex started on OpenAI before are listed + // where Codex is now. Codex's resume picker and the app's thread list + // show the threads of the provider Codex is on alone, so with magpie + // its provider (no ChatGPT sign-in, an API key, magpie API, an account + // out of its allowance) `codex resume` said "No sessions yet" over a + // history that was all there (notnico64 on Discord). They move in + // place (RelabelCodexThreads): those Codex has packed, a week after + // their last turn, stay on OpenAI, as a rewrite of every one would + // copy the whole history. bringBack moves them back as Codex leaves + // magpie. Only OpenAI's, and only when Codex was on OpenAI before + // magpie: threads of a provider of the user's (CC Switch's) stay, and + // so do OpenAI's when Codex was on another provider, whose threads + // bringBack would hand them to. None while a profile is chosen, whose + // provider is the one Codex lists, and none from a distro's Codex. + bringOver := func() { + if at.spell != nil || !asProvider() || sessions.CodexProviderIn(dir) != magpieID { + return + } + if p := stashLoad()[at.key("codex.provider")]; p != "" && p != "openai" { + return + } + moved, err := sessions.RelabelCodexThreads(dir, "openai", magpieID) + if len(moved) > 0 { + log.Printf("codex: %d thread(s) started on OpenAI moved to magpie, where Codex lists them now", len(moved)) + } + if err != nil { + log.Println("codex: moving OpenAI's threads to magpie:", err) + } + } // mirrorsTaken are the mirrors takeMirrors pointed at magpie (#1796) mirrorsTaken := func() []string { var ids []string @@ -1162,6 +1203,8 @@ func codexIn(at place) *Agent { if err := takeTables(); err != nil { return err } + // the threads from before, where Codex lists them now + bringOver() return settle() } // joined, a model of Codex's own is picked beside magpie's, as in @@ -1290,7 +1333,8 @@ func codexIn(at place) *Agent { }, Sync: func() error { // after the sync, which may have put Codex back beside its - // sign-in, and whatever it failed at + // sign-in or made magpie its provider, and whatever it failed at + defer bringOver() defer bringBack() // model_catalog_json naming magpie's list with the list gone, // as an older magpie's Disconnect could leave it (zwaydot on diff --git a/internal/agent/codex_threads_over_test.go b/internal/agent/codex_threads_over_test.go new file mode 100644 index 000000000..b1902241a --- /dev/null +++ b/internal/agent/codex_threads_over_test.go @@ -0,0 +1,120 @@ +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) +} diff --git a/internal/sessions/codex_provider.go b/internal/sessions/codex_provider.go index 52102c6b3..152932da0 100644 --- a/internal/sessions/codex_provider.go +++ b/internal/sessions/codex_provider.go @@ -593,3 +593,161 @@ func moveCodexThread(path, id, to string) (bool, error) { rows, _ := res.RowsAffected() return rows > 0, nil } + +// RelabelCodexThreads moves the threads of a Codex folder its state +// databases keep on the provider from to the provider to, where that can +// be done in place: ids of one length (Codex's "openai" and magpie's +// "magpie"), and a rollout Codex keeps as plain lines. The id in the +// first line's session_meta, which Codex's thread/list and resume picker +// filter on, is written over byte for byte, the file's time kept, and the +// thread's row follows. Nothing else of the file is read or written, so a +// history of thousands of threads moves in moments where MoveCodexThreads +// would copy and rewrite every one of them; moving it back is the undo, +// and no copy is kept. Rollouts Codex has packed (.zst, a week after +// their last turn) are left on from, and so is a thread written to in the +// last minute, one whose file names another provider, and one outside the +// folder's sessions. It says the ids it moved and the first error it met, +// having gone on past it. +func RelabelCodexThreads(home, from, to string) ([]string, error) { + if !codexProviderRe.MatchString(from) || !codexProviderRe.MatchString(to) || from == to || len(from) != len(to) { + return nil, fmt.Errorf("can't relabel Codex's threads from %q to %q", from, to) + } + type row struct{ db, id, path string } + var rows []row + for _, p := range codexStateDBs(home) { + u := url.URL{Scheme: "file", Path: filepath.ToSlash(p), RawQuery: "mode=ro&_pragma=busy_timeout(2000)"} + if len(u.Path) >= 2 && u.Path[1] == ':' { + u.Path = "/" + u.Path + } + db, err := sql.Open("sqlite", u.String()) + if err != nil { + continue + } + rs, err := db.Query(`SELECT id, rollout_path FROM threads WHERE model_provider = ?`, from) + if err == nil { + for rs.Next() { + var id, path string + if rs.Scan(&id, &path) == nil && safeID.MatchString(id) { + rows = append(rows, row{p, id, path}) + } + } + rs.Close() + } + db.Close() + } + if len(rows) == 0 { + return nil, nil + } + storeMu.Lock() + defer storeMu.Unlock() + mu.Lock() + defer mu.Unlock() + loadCache() + var moved []string + var first error + fail := func(err error) { + if first == nil { + first = err + } + } + now := time.Now() + for _, r := range rows { + path := filepath.Clean(r.path) + if !filepath.IsAbs(path) || packed(path) || !strings.Contains(filepath.Base(path), r.id) || !sameDir(codexHomeOf(path), home) { + continue + } + fi, err := os.Lstat(path) + if err != nil || !fi.Mode().IsRegular() || now.Sub(fi.ModTime()) < ActiveWindow { + continue + } + // a packed twin beside it: Codex is packing it now + if _, err := os.Lstat(path + zstSuffix); err == nil { + continue + } + ok, err := relabelHead(path, from, to, fi.ModTime()) + if err != nil { + fail(fmt.Errorf("thread %s: %w", r.id, err)) + continue + } + if !ok { + continue + } + delete(cache, path) + if _, err := moveCodexThread(r.db, r.id, to); err != nil { + fail(fmt.Errorf("thread %s: the file moved, but Codex's %s didn't: %w", r.id, filepath.Base(r.db), err)) + continue + } + moved = append(moved, r.id) + } + if len(moved) > 0 { + saveCache() + } + return moved, first +} + +// relabelHead writes to over from as the model_provider of a rollout's +// first line, its session_meta, in place, and puts the file's time back. +// It says whether the file now names to: false, with no error, for a file +// whose first line isn't a session_meta naming from or to. +func relabelHead(path, from, to string, mod time.Time) (bool, error) { + f, err := os.OpenFile(path, os.O_RDWR, 0) + if err != nil { + return false, err + } + // the first line, up to 1MB: a session_meta carries the base + // instructions, tens of KB + line, err := bufio.NewReaderSize(io.LimitReader(f, 1<<20), 64<<10).ReadBytes('\n') + if err != nil { + f.Close() + if errors.Is(err, io.EOF) { + return false, nil + } + return false, err + } + end := len(line) - 1 + line = line[:end] + if gjson.GetBytes(line, "type").String() != "session_meta" { + f.Close() + return false, nil + } + r := gjson.GetBytes(line, "payload.model_provider") + if r.Type != gjson.String || r.Index <= 0 || r.Index+len(r.Raw) > end { + f.Close() + return false, nil + } + if r.Str == to { + f.Close() + return true, nil + } + want, _ := json.Marshal(from) + if r.Str != from || r.Raw != string(want) { + f.Close() + return false, nil + } + val, _ := json.Marshal(to) + if _, err := f.WriteAt(val, int64(r.Index)); err != nil { + f.Close() + return false, err + } + if err := f.Close(); err != nil { + return false, err + } + return true, os.Chtimes(path, mod, mod) +} + +// sameDir says whether a and b are one folder, however spelled: a Codex +// home reached through a link, or in another case on Windows. +func sameDir(a, b string) bool { + if a == "" || b == "" { + return false + } + if filepath.Clean(a) == filepath.Clean(b) { + return true + } + fa, err := os.Stat(a) + if err != nil { + return false + } + fb, err := os.Stat(b) + return err == nil && os.SameFile(fa, fb) +}