Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
sessions: a live Cursor's state.vscdb is read in place, not copied (#1988)
yetone1dc20e73fc45
diff --git a/internal/sessions/cursor.go b/internal/sessions/cursor.goindex 0b1da34e3..08524425d 100644--- a/internal/sessions/cursor.go+++ b/internal/sessions/cursor.go@@ -192,21 +192,7 @@ func openCursorStore(path string) (db *sql.DB, closeDB func(), err error) {// (SQLite would make its -shm file), so a reader that needs it read// whether the app runs or not opens it this way. closeDB removes the copy.func OpenUntouched(path string) (db *sql.DB, closeDB func(), err error) {- open := func(p, query string) (*sql.DB, error) {- u := url.URL{Scheme: "file", Path: filepath.ToSlash(p), RawQuery: query}- if len(u.Path) >= 2 && u.Path[1] == ':' {- u.Path = "/" + u.Path- }- db, err := sql.Open("sqlite", u.String())- if err != nil {- return nil, err- }- if err := db.Ping(); err != nil {- db.Close()- return nil, err- }- return db, nil- }+ open := openSQLiteif fi, err := os.Stat(path + "-wal"); err != nil || fi.Size() == 0 {db, err := open(path, "immutable=1")if err != nil {@@ -224,6 +210,23 @@ func OpenUntouched(path string) (db *sql.DB, closeDB func(), err error) {return db, func() { db.Close(); os.RemoveAll(tmp) }, nil}+// openSQLite opens the SQLite database at p, with query as its URI's.+func openSQLite(p, query string) (*sql.DB, error) {+ u := url.URL{Scheme: "file", Path: filepath.ToSlash(p), RawQuery: query}+ if len(u.Path) >= 2 && u.Path[1] == ':' {+ u.Path = "/" + u.Path+ }+ db, err := sql.Open("sqlite", u.String())+ if err != nil {+ return nil, err+ }+ if err := db.Ping(); err != nil {+ db.Close()+ return nil, err+ }+ return db, nil+}+func copyFile(from, to string) error {in, err := os.Open(from)if err != nil {diff --git a/internal/sessions/cursor_editor.go b/internal/sessions/cursor_editor.goindex bad238d15..2422a9a2d 100644--- a/internal/sessions/cursor_editor.go+++ b/internal/sessions/cursor_editor.go@@ -34,7 +34,7 @@ func openCursorEditorDB(path string) *sql.DB {if db := dbs[path]; db != nil {return db}- db, done, err := openCursorStore(path)+ db, done, err := openCursorEditorStore(path)if err != nil {return nil}@@ -43,6 +43,30 @@ func openCursorEditorDB(path string) *sql.DB {return db}+// openCursorEditorStore opens the editor's state.vscdb to read it. While+// Cursor runs, its own processes share the store as readers of its -wal+// through its -shm, and magpie reads it as one more, in place: a copy of a+// store of several GB was written to the temp folder each time Cursor had+// written since the last, ~10.6 GB every few minutes on Windows, where no+// clone makes a copy free (Echo-Huang0010, #1988). Nothing magpie shows is+// read from the store's file times, so a reader's mark in -shm moves no+// chat; each chat carries its own (lastUpdatedAt). With no -shm beside a+// live -wal there is no Cursor running to share it with, and it is opened+// as a chat's store is (openCursorStore).+func openCursorEditorStore(path string) (*sql.DB, func(), error) {+ wal, werr := os.Stat(path + "-wal")+ _, serr := os.Stat(path + "-shm")+ if !cursorReadInPlace || werr != nil || wal.Size() == 0 || serr != nil {+ return openCursorStore(path)+ }+ db, err := openSQLite(path, "mode=ro&_pragma=busy_timeout(3000)")+ if err != nil {+ return openCursorStore(path)+ }+ cursorInPlace.Add(1)+ return db, func() { db.Close() }, nil+}+// cursorComposersQuery lists the editor's chats by a range on the key's// index. LIKE is case-insensitive, so SQLite can't use the index for it and// reads the whole table: every bubble and checkpoint, often several GB. A@@ -215,7 +239,7 @@ func editorMessages(db *sql.DB, id string, add func(string, string, time.Time) bfunc readCursorEditor(path string, add func(string, string, time.Time) bool) error {if dbPath, id, ok := cursorEditorDatabasePath(path); ok {- db, done, err := openCursorStore(dbPath)+ db, done, err := openCursorEditorStore(dbPath)if err != nil {return err}diff --git a/internal/sessions/cursor_snapshot.go b/internal/sessions/cursor_snapshot.goindex 7b1949ce5..fa6a1e655 100644--- a/internal/sessions/cursor_snapshot.go+++ b/internal/sessions/cursor_snapshot.go@@ -24,6 +24,10 @@ var (// left behind; a shared copy in use is touched on every reusecursorSnapStale = time.HourcursorCopies atomic.Int64+ // editor stores read in place beside a live Cursor (openCursorEditorStore)+ cursorInPlace atomic.Int64+ // false only in a test of the copy path+ cursorReadInPlace = true)type cursorSnap struct {diff --git a/internal/sessions/cursor_snapshot_test.go b/internal/sessions/cursor_snapshot_test.goindex a1a29c8b3..23318fb38 100644--- a/internal/sessions/cursor_snapshot_test.go+++ b/internal/sessions/cursor_snapshot_test.go@@ -11,34 +11,29 @@ import ("time")+// cursorTempDir gives the test a temp folder of its own: TMPDIR is+// os.TempDir on Unix, TMP and TEMP on Windows, where a TMPDIR alone left+// TestCursorLeftCopiesSwept's copy in the real temp folder for the next+// test to count.+func cursorTempDir(t *testing.T) {+ t.Helper()+ d := t.TempDir()+ for _, k := range []string{"TMPDIR", "TMP", "TEMP"} {+ t.Setenv(k, d)+ }+}+func cursorTempCopies(t *testing.T) []string {t.Helper()d, _ := filepath.Glob(filepath.Join(os.TempDir(), "magpie-cursor-*"))return d}-// #1988: Cursor's state.vscdb is often several GB. Every listing, count,-// transcript and search read it from a fresh copy in the temp folder, so a-// visit to Sessions wrote it out again and again. Reads of an unchanged-// store share one copy, a change makes one new copy, and nothing is left.-func TestCursorEditorCopiedOncePerChange(t *testing.T) {- cursorSetup(t)- t.Setenv("TMPDIR", t.TempDir())- // The copy lingers past any pause a loaded runner makes between two- // reads while copies are counted (a 100ms linger let a slow runner drop- // it between List and the next visit, and count a third copy); it is- // cut short only to see the last one removed. The timers read the- // linger under cursorSnaps' lock, so it is set under it.- linger := func(d time.Duration) {- cursorSnaps.Lock()- cursorSnapLinger = d- cursorSnaps.Unlock()- }- minWas, lingerWas := cursorSnapMin, cursorSnapLinger- cursorSnapMin = 0- linger(time.Hour)- t.Cleanup(func() { cursorSnapMin = minWas; linger(lingerWas) })-+// cursorLiveEditor writes the editor's state.vscdb with six chats, as a+// running Cursor holds it: in WAL mode, with its rows still in the -wal and+// the -shm beside it. put writes a row, as Cursor does, until the test ends.+func cursorLiveEditor(t *testing.T) (put func(string, any), ids []string, at time.Time) {+ t.Helper()config, err := os.UserConfigDir()if err != nil {t.Fatal(err)@@ -52,20 +47,19 @@ func TestCursorEditorCopiedOncePerChange(t *testing.T) {t.Fatal(err)}db.SetMaxOpenConns(1)- defer db.Close()+ t.Cleanup(func() { db.Close() })for _, q := range []string{"PRAGMA journal_mode=WAL", "PRAGMA wal_autocheckpoint=0", "CREATE TABLE cursorDiskKV (key TEXT PRIMARY KEY, value BLOB)"} {if _, err := db.Exec(q); err != nil {t.Fatal(err)}}- put := func(k string, v any) {+ put = func(k string, v any) {b, _ := json.Marshal(v)if _, err := db.Exec("INSERT OR REPLACE INTO cursorDiskKV VALUES (?,?)", k, b); err != nil {t.Fatal(err)}}- at := time.Now().Add(-time.Hour)- var ids []string+ at = time.Now().Add(-time.Hour)for i := range 6 {id := fmt.Sprintf("0000000%d-1111-2222-3333-444444444444", i)ids = append(ids, id)@@ -76,6 +70,40 @@ func TestCursorEditorCopiedOncePerChange(t *testing.T) {if fi, err := os.Stat(p + "-wal"); err != nil || fi.Size() == 0 {t.Fatalf("fixture has no live -wal: %v", err)}+ if _, err := os.Stat(p + "-shm"); err != nil {+ t.Fatalf("fixture has no -shm: %v", err)+ }+ return put, ids, at+}++// #1988: Cursor's state.vscdb is often several GB. Every listing, count,+// transcript and search read it from a fresh copy in the temp folder, so a+// visit to Sessions wrote it out again and again. Reads of an unchanged+// store share one copy, a change makes one new copy, and nothing is left.+func TestCursorEditorCopiedOncePerChange(t *testing.T) {+ cursorSetup(t)+ cursorTempDir(t)+ // The copy lingers past any pause a loaded runner makes between two+ // reads while copies are counted (a 100ms linger let a slow runner drop+ // it between List and the next visit, and count a third copy); it is+ // cut short only to see the last one removed. The timers read the+ // linger under cursorSnaps' lock, so it is set under it.+ linger := func(d time.Duration) {+ cursorSnaps.Lock()+ cursorSnapLinger = d+ cursorSnaps.Unlock()+ }+ minWas, lingerWas := cursorSnapMin, cursorSnapLinger+ cursorSnapMin = 0+ linger(time.Hour)+ t.Cleanup(func() { cursorSnapMin = minWas; linger(lingerWas) })++ // the copy path: a store with a -wal no running Cursor shares+ inPlaceWas := cursorReadInPlace+ cursorReadInPlace = false+ t.Cleanup(func() { cursorReadInPlace = inPlaceWas })++ put, ids, at := cursorLiveEditor(t)// a visit to Sessions: the list, the counts, each chat, a searchvisit := func() {@@ -123,10 +151,53 @@ func TestCursorEditorCopiedOncePerChange(t *testing.T) {}}+// #1988 (Echo-Huang0010): beside a running Cursor, a visit to Sessions+// copied a ~10.6 GB state.vscdb to the temp folder each time Cursor had+// written since the last copy. The store is read where it is, as one more+// of the readers Cursor's own processes are: nothing is copied, a row+// Cursor writes is read at once, and Cursor can still write after it.+func TestCursorEditorReadInPlaceWhileLive(t *testing.T) {+ cursorSetup(t)+ cursorTempDir(t)+ minWas := cursorSnapMin+ cursorSnapMin = 0+ t.Cleanup(func() { cursorSnapMin = minWas })++ put, ids, at := cursorLiveEditor(t)+ copies, inPlace := cursorCopies.Load(), cursorInPlace.Load()+ for round := range 3 {+ name := fmt.Sprintf("Round %d", round)+ put("composerData:"+ids[0], map[string]any{"composerId": ids[0], "name": name, "createdAt": at.UnixMilli(), "lastUpdatedAt": at.Add(time.Duration(round+2) * time.Minute).UnixMilli(), "fullConversationHeadersOnly": []any{map[string]any{"bubbleId": "u"}, map[string]any{"bubbleId": "a"}}})+ ss := List(All)+ if s := find(t, ss, "cursor", ids[0]); s.Title != name {+ t.Fatalf("round %d: Cursor's write wasn't read: %q", round, s.Title)+ }+ Agents()+ for _, id := range ids {+ s := find(t, ss, "cursor", id)+ if tr, err := TranscriptOf(s); err != nil || len(tr.Parts) != 2 {+ t.Fatalf("transcript %+v: %v", tr, err)+ }+ if _, _, err := SearchSession(context.Background(), s, "nothing like this"); err != nil {+ t.Fatal(err)+ }+ }+ }+ if n := cursorCopies.Load() - copies; n != 0 {+ t.Fatalf("a live state.vscdb was copied %d times, want 0", n)+ }+ if cursorInPlace.Load() == inPlace {+ t.Fatal("the store was never opened in place")+ }+ if left := cursorTempCopies(t); len(left) > 0 {+ t.Fatalf("copies in the temp folder: %v", left)+ }+}+// A copy left by a magpie that was killed mid-read is removed; one a// running magpie made a moment ago is not.func TestCursorLeftCopiesSwept(t *testing.T) {- t.Setenv("TMPDIR", t.TempDir())+ cursorTempDir(t)left := filepath.Join(os.TempDir(), "magpie-cursor-left")fresh := filepath.Join(os.TempDir(), "magpie-cursor-fresh")for _, d := range []string{left, fresh} {