Files
magpie/internal/sessions/cursor_snapshot_test.go
T
yetone 0e39c8e7e9 sessions: reading Cursor's history no longer writes its database to the temp folder again and again (#1988, kangxi94)
v0.1.1224 reads Cursor's editor chats from its global state.vscdb, which
is often several GB. A store with a live -wal is read from a copy, and
every listing, count, manage page, transcript, context read and each
chat of a search made a fresh full copy in $TMPDIR/magpie-cursor-*.
A visit to Sessions wrote the file out several times (28 in the test
below): heavy disk writes with low CPU, and Time Machine's local
snapshots can hold on to deleted copies' blocks, which would shrink free
space (not reproduced on the reporter's machine). A magpie killed mid-read also left
its copy behind for good.

- A store of 16 MB or more is copied once and the copy is shared by
  every reader while the store and its -wal keep their size and time;
  a change makes one new copy, and a copy goes 30s after its last reader.
- On macOS the copy is an APFS clone (clonefile), so it takes no space
  until Cursor writes; elsewhere, or when cloning fails, it is copied.
- The first copy a process makes sweeps magpie-cursor-* folders older
  than an hour (a shared copy is touched on every reuse).

Run:
- TestCursorEditorCopiedOncePerChange: 6 editor chats, list + Agents +
  transcript + search of each, twice: 1 copy (28 with sharing turned off
  by hand), 2 after one change, none left. TestCursorLeftCopiesSwept
  goes red with the sweep turned off.
- go test -race ./internal/sessions/ ok; -run TestCursor -race -count=10 ok.
- gofmt -l clean; go vet -tags nogui for darwin, linux, windows.
- Sandbox HOME with a 300 MB Cursor-shaped state.vscdb kept live by a
  writer every 5s: v0.1.1224 made 1-3 full copies per endpoint and 5
  for one search (11s, searched 0 of 30); this build made 2 over
  sessions, stats, overview, manage, context, two searches and a second
  listing (search 30 of 30 in ms), and removed a planted day-old
  magpie-cursor-* folder.
2026-10-13 18:16:18 +08:00

137 lines
4.6 KiB
Go

package sessions
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
)
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())
minWas, lingerWas := cursorSnapMin, cursorSnapLinger
cursorSnapMin, cursorSnapLinger = 0, 100*time.Millisecond
t.Cleanup(func() { cursorSnapMin, cursorSnapLinger = minWas, 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)
}
// a visit to Sessions: the list, the counts, each chat, a search
visit := func() {
ss := List(All)
Agents()
for _, id := range ids {
s, ok := findSession(ss, "cursor", id)
if !ok {
t.Fatalf("chat %s not listed", 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)
}
}
}
before := cursorCopies.Load()
visit()
visit()
if n := cursorCopies.Load() - before; n != 1 {
t.Fatalf("an unchanged state.vscdb was copied %d times, want 1", n)
}
put("composerData:"+ids[0], map[string]any{"composerId": ids[0], "name": "Renamed", "createdAt": at.UnixMilli(), "lastUpdatedAt": at.Add(2 * time.Minute).UnixMilli(), "fullConversationHeadersOnly": []any{map[string]any{"bubbleId": "u"}, map[string]any{"bubbleId": "a"}}})
if s := find(t, List(All), "cursor", ids[0]); s.Title != "Renamed" {
t.Fatalf("a change to the store wasn't read: %q", s.Title)
}
visit()
if n := cursorCopies.Load() - before; n != 2 {
t.Fatalf("copies after one change: %d, want 2", n)
}
deadline := time.Now().Add(5 * time.Second)
for len(cursorTempCopies(t)) > 0 {
if time.Now().After(deadline) {
t.Fatalf("copies left in the temp folder: %v", cursorTempCopies(t))
}
time.Sleep(20 * time.Millisecond)
}
}
// 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())
left := filepath.Join(os.TempDir(), "magpie-cursor-left")
fresh := filepath.Join(os.TempDir(), "magpie-cursor-fresh")
for _, d := range []string{left, fresh} {
if err := os.MkdirAll(d, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d, "store.db"), make([]byte, 1024), 0600); err != nil {
t.Fatal(err)
}
}
old := time.Now().Add(-2 * cursorSnapStale)
if err := os.Chtimes(left, old, old); err != nil {
t.Fatal(err)
}
sweepCursorSnaps()
if _, err := os.Stat(left); !os.IsNotExist(err) {
t.Fatalf("a left copy is still there: %v", err)
}
if _, err := os.Stat(fresh); err != nil {
t.Fatalf("a copy in use was removed: %v", err)
}
}