diff --git a/internal/sessions/cursor_editor.go b/internal/sessions/cursor_editor.go index 5a31fc3f2..0c1746eb1 100644 --- a/internal/sessions/cursor_editor.go +++ b/internal/sessions/cursor_editor.go @@ -43,6 +43,15 @@ func openCursorEditorDB(path string) *sql.DB { return db } +// 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 +// live store is read from a fresh copy whenever Cursor has written, and a +// fresh copy (an APFS clone on macOS) isn't in the page cache, so every +// listing read the whole store from disk again and Sessions never finished +// loading (#1988). ';' is the character after ':'. +const cursorComposersQuery = "SELECT value FROM cursorDiskKV WHERE key >= 'composerData:' AND key < 'composerData;'" + func cursorEditorDB() string { dir, err := os.UserConfigDir() if err != nil { @@ -62,7 +71,7 @@ func cursorEditorFiles(cli []file) []file { p := cursorEditorDB() if fileExists(p) { if db := openCursorEditorDB(p); db != nil { - rows, err := db.Query("SELECT value FROM cursorDiskKV WHERE key LIKE 'composerData:%'") + rows, err := db.Query(cursorComposersQuery) if err == nil { for rows.Next() { var b []byte diff --git a/internal/sessions/cursor_editor_test.go b/internal/sessions/cursor_editor_test.go index 0463f6b40..b37d07773 100644 --- a/internal/sessions/cursor_editor_test.go +++ b/internal/sessions/cursor_editor_test.go @@ -213,3 +213,54 @@ func TestCursorEditorDatabase(t *testing.T) { t.Fatal("editor database changed") } } + +// #1988: the chat list must come off the key's index. A LIKE over the key +// scans every bubble in a store of several GB, from a copy that is cold in +// the page cache each time Cursor has written, and Sessions never loaded. +func TestCursorComposersQueryUsesTheKeyIndex(t *testing.T) { + db, err := sql.Open("sqlite", filepath.Join(t.TempDir(), "state.vscdb")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + // Cursor's own schema + if _, err := db.Exec("CREATE TABLE cursorDiskKV (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB)"); err != nil { + t.Fatal(err) + } + for _, k := range []string{"bubbleId:a:1", "checkpointId:a:1", "composerData", "composerData:a", "composerData:b:c", "composerData;x", "composerDataZ", "composerdata:lower", "inlineDiffs-a"} { + if _, err := db.Exec("INSERT INTO cursorDiskKV VALUES (?,?)", k, k); err != nil { + t.Fatal(err) + } + } + rows, err := db.Query("EXPLAIN QUERY PLAN " + cursorComposersQuery) + if err != nil { + t.Fatal(err) + } + var plan []string + for rows.Next() { + var id, parent, unused int + var detail string + if err := rows.Scan(&id, &parent, &unused, &detail); err != nil { + t.Fatal(err) + } + plan = append(plan, detail) + } + rows.Close() + if p := strings.Join(plan, "; "); !strings.Contains(p, "USING INDEX") || strings.Contains(p, "SCAN") { + t.Fatalf("the chat list reads %q, want a search on the key's index", p) + } + rows, err = db.Query(cursorComposersQuery) + if err != nil { + t.Fatal(err) + } + var got []string + for rows.Next() { + var v string + rows.Scan(&v) + got = append(got, v) + } + rows.Close() + if strings.Join(got, ",") != "composerData:a,composerData:b:c" { + t.Fatalf("listed %v, want only the composerData: keys", got) + } +}