diff --git a/internal/sessions/cursor.go b/internal/sessions/cursor.go index 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 := openSQLite if 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.go index 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) b func 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.go index 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 reuse cursorSnapStale = time.Hour cursorCopies 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.go index a1a29c8b3..23318fb38 100644 --- a/internal/sessions/cursor_snapshot_test.go +++ b/internal/sessions/cursor_snapshot_test.go @@ -11,19 +11,78 @@ 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 } +// 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) + } + p := filepath.Join(config, "Cursor", "User", "globalStorage", "state.vscdb") + if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { + t.Fatal(err) + } + db, err := sql.Open("sqlite", p) + if err != nil { + t.Fatal(err) + } + db.SetMaxOpenConns(1) + 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) { + 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) + for i := range 6 { + id := fmt.Sprintf("0000000%d-1111-2222-3333-444444444444", i) + ids = append(ids, id) + put("composerData:"+id, map[string]any{"composerId": id, "name": "Chat " + id[:8], "createdAt": at.UnixMilli(), "lastUpdatedAt": at.Add(time.Minute).UnixMilli(), "fullConversationHeadersOnly": []any{map[string]any{"bubbleId": "u"}, map[string]any{"bubbleId": "a"}}}) + put("bubbleId:"+id+":u", map[string]any{"type": 1, "text": "question " + id, "createdAt": at.Format(time.RFC3339Nano)}) + put("bubbleId:"+id+":a", map[string]any{"type": 2, "text": "answer", "createdAt": at.Add(time.Minute).Format(time.RFC3339Nano)}) + } + 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) - t.Setenv("TMPDIR", t.TempDir()) + 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 @@ -39,43 +98,12 @@ func TestCursorEditorCopiedOncePerChange(t *testing.T) { linger(time.Hour) t.Cleanup(func() { cursorSnapMin = minWas; linger(lingerWas) }) - config, err := os.UserConfigDir() - if err != nil { - t.Fatal(err) - } - p := filepath.Join(config, "Cursor", "User", "globalStorage", "state.vscdb") - if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { - t.Fatal(err) - } - db, err := sql.Open("sqlite", p) - if err != nil { - t.Fatal(err) - } - db.SetMaxOpenConns(1) - defer 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) { - 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 - for i := range 6 { - id := fmt.Sprintf("0000000%d-1111-2222-3333-444444444444", i) - ids = append(ids, id) - put("composerData:"+id, map[string]any{"composerId": id, "name": "Chat " + id[:8], "createdAt": at.UnixMilli(), "lastUpdatedAt": at.Add(time.Minute).UnixMilli(), "fullConversationHeadersOnly": []any{map[string]any{"bubbleId": "u"}, map[string]any{"bubbleId": "a"}}}) - put("bubbleId:"+id+":u", map[string]any{"type": 1, "text": "question " + id, "createdAt": at.Format(time.RFC3339Nano)}) - put("bubbleId:"+id+":a", map[string]any{"type": 2, "text": "answer", "createdAt": at.Add(time.Minute).Format(time.RFC3339Nano)}) - } - if fi, err := os.Stat(p + "-wal"); err != nil || fi.Size() == 0 { - t.Fatalf("fixture has no live -wal: %v", err) - } + // 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 search visit := 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} {