From 9bfbd6e45e95706fd27fabc6ffdd55752dd56e1d Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 04:36:40 +0800 Subject: [PATCH] sessions: Sessions loads again with a large Cursor store, which every listing read whole from disk (#1988, kangxi94) v0.1.1227 (0e39c8e7e) reads a live Cursor state.vscdb from an APFS clone made whenever Cursor has written, which stopped the repeated copies. But the editor's chat list was found with `key LIKE 'composerData:%'`. LIKE is case-insensitive, so SQLite can't use the key's index and scans the whole table: every bubble and checkpoint, often several GB. Before, the copy had just been written and sat in the page cache; a fresh clone doesn't, so each listing read the whole store from disk again. Cursor writes every few seconds, so nearly every listing got a fresh clone, and /api/sessions and /api/sessions/manage took longer than the pages' polls: Sessions and Usage > Sessions showed skeletons for good. The list is now read by a range on the key's index (cursorComposersQuery, key >= 'composerData:' AND key < 'composerData;'), which reads only the chats' rows. Run: - TestCursorComposersQueryUsesTheKeyIndex: Cursor's own schema, keys around the range's edges; the plan must be a search on the index and only composerData: keys come back. Red with the LIKE put back ("SCAN cursorDiskKV"). - sqlite3 on a fresh clone of a 2.2 GB live-written store: LIKE 3.74s cold, 0.11s warm; the range 0.06s. A second repro (4.3 GB store, 1M small keys, 500 chats): range 0.01s vs LIKE 3.8s; on main /api/sessions took 40-60s and /api/sessions/manage 86-121s every round. - Built binaries against the 2.2 GB store with a writer every 2s, same sandbox, machine at load ~25 from other work: /api/sessions 17/9/3.7s and manage 16s with the fix, 34/26/9.4s and 31s on main. The rest is not the Cursor scan (a sample shows no sqlite work); not measured on an idle machine. - go test ./internal/sessions ./internal/gui ok; gofmt; go vet darwin, linux, windows. Not tried on the reporter's machine or a real Cursor store. --- internal/sessions/cursor_editor.go | 11 +++++- internal/sessions/cursor_editor_test.go | 51 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) 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) + } +}