Files
magpie/internal/sessions/cursor_snapshot.go
T
yetone 1dc20e73fc sessions: a live Cursor's state.vscdb is read in place, not copied (#1988)
Beside a running Cursor, every listing, count, transcript and search
read the editor's state.vscdb from a copy in the temp folder, made again
each time Cursor had written since the last. The store is often several
GB, and on Windows, where no clone makes a copy free, Echo-Huang0010
measured ~10.6 GB written every few minutes.

While the store has a non-empty -wal and a -shm beside it, Cursor's own
processes share it as readers through the -shm, and magpie now opens it
the same way, mode=ro with a 3s busy timeout: no copy. Nothing magpie
shows comes from the store's file times (each chat carries
lastUpdatedAt), so a reader's mark in -shm changes nothing it lists.
With no -shm beside a live -wal, or if that open fails, it falls back to
the copy as before. CLI chat stores keep the copy path. OpenUntouched's
open is factored out as openSQLite.

Thanks to Echo-Huang0010 for the report and the measurements; their
patch capped the copy at 64 MiB, which would leave a larger history
unread, so this takes the in-place read instead.

The Cursor tests set TMPDIR only, which Windows' os.TempDir doesn't read
(TMP, TEMP), so TestCursorLeftCopiesSwept's folder landed in the real
temp folder and the next test counted it. cursorTempDir sets all three.

Run at this head:
- mote: go test -count=3 -run Cursor ./internal/sessions ok;
  go test -timeout 45m ./internal/sessions ./internal/agent ok;
  go vet ./internal/sessions for darwin, linux, windows ok; gofmt clean.
- xiaomi-gpu (win-run): go test -count=3 -run Cursor ./internal/sessions
  ok (red before cursorTempDir, from the stray folder).
- Broken by hand: in-place reads off, TestCursorEditorReadInPlaceWhileLive
  fails "a live state.vscdb was copied 3 times, want 0".
- Windows, two processes: a writer holding a WAL store (~40 MB) with
  busy_timeout(0) wrote 200 times while a magpie process listed 40 times
  and read transcripts. 0 failed writes, every list saw the latest title
  (40 of 40), 0 copies in the temp folder.

Not tried: a real Cursor install. Neither test machine has one, so the
writer was a stand-in process, not Cursor itself.
2026-10-28 14:09:05 +08:00

163 lines
4.2 KiB
Go

package sessions
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
)
// A Cursor store with a live -wal is read from a copy (see openCursorStore).
// Cursor's editor keeps every chat in one state.vscdb, often several GB, and
// a listing, the stats, the manage page, a transcript and a search over its
// chats each opened it again, so one visit to Sessions wrote the whole file
// to the temp folder five or more times (#1988). A copy of a large store is
// now kept while the store and its -wal are unchanged and shared by every
// reader, and is removed cursorSnapLinger after its last reader lets go.
var (
cursorSnapLinger = 30 * time.Second
cursorSnapMin int64 = 16 << 20
// a magpie-cursor-* folder older than this is a copy a killed magpie
// 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 {
path, dir, stamp string
db *sql.DB
users int
last time.Time
gone bool
}
var cursorSnaps struct {
sync.Mutex
m map[string]*cursorSnap
sweep sync.Once
}
// cursorStamp names a store's state by its and its -wal's size and time.
func cursorStamp(path string) (stamp string, size int64, ok bool) {
for _, side := range []string{"", "-wal"} {
fi, err := os.Stat(path + side)
if err != nil {
return "", 0, false
}
stamp += fmt.Sprintf("%d/%d;", fi.Size(), fi.ModTime().UnixNano())
size += fi.Size()
}
return stamp, size, true
}
// sharedCursorStore hands out the kept copy of path while stamp still
// matches, and makes one otherwise.
func sharedCursorStore(path, stamp string, open func(string, string) (*sql.DB, error)) (*sql.DB, func(), error) {
cursorSnaps.Lock()
defer cursorSnaps.Unlock()
if s := cursorSnaps.m[path]; s != nil {
if s.stamp == stamp {
s.users++
now := time.Now()
os.Chtimes(s.dir, now, now)
return s.db, s.release(), nil
}
delete(cursorSnaps.m, path)
s.gone = true
if s.users == 0 {
s.drop()
}
}
db, dir, err := snapshotCursorStore(path, open)
if err != nil {
return nil, nil, err
}
s := &cursorSnap{path: path, dir: dir, stamp: stamp, db: db, users: 1}
if cursorSnaps.m == nil {
cursorSnaps.m = map[string]*cursorSnap{}
}
cursorSnaps.m[path] = s
return db, s.release(), nil
}
func (s *cursorSnap) release() func() {
var once sync.Once
return func() {
once.Do(func() {
cursorSnaps.Lock()
defer cursorSnaps.Unlock()
s.users--
s.last = time.Now()
if s.users > 0 {
return
}
if s.gone {
s.drop()
return
}
time.AfterFunc(cursorSnapLinger, func() {
cursorSnaps.Lock()
defer cursorSnaps.Unlock()
if s.gone || s.users > 0 || time.Since(s.last) < cursorSnapLinger {
return
}
if cursorSnaps.m[s.path] == s {
delete(cursorSnaps.m, s.path)
}
s.gone = true
s.drop()
})
})
}
}
func (s *cursorSnap) drop() {
s.db.Close()
os.RemoveAll(s.dir)
}
// snapshotCursorStore copies path and its -wal into a folder of magpie's
// own, cloning where the file system can so the copy takes no space.
func snapshotCursorStore(path string, open func(string, string) (*sql.DB, error)) (*sql.DB, string, error) {
cursorSnaps.sweep.Do(sweepCursorSnaps)
tmp, err := os.MkdirTemp("", "magpie-cursor-")
if err != nil {
return nil, "", err
}
copied := filepath.Join(tmp, "store.db")
for _, side := range []string{"", "-wal"} {
if cloneFile(path+side, copied+side) == nil {
continue
}
os.Remove(copied + side)
if err := copyFile(path+side, copied+side); err != nil {
os.RemoveAll(tmp)
return nil, "", err
}
}
cursorCopies.Add(1)
db, err := open(copied, "_pragma=busy_timeout(3000)")
if err != nil {
os.RemoveAll(tmp)
return nil, "", err
}
return db, tmp, nil
}
// sweepCursorSnaps removes copies an earlier magpie was killed holding.
func sweepCursorSnaps() {
old, _ := filepath.Glob(filepath.Join(os.TempDir(), "magpie-cursor-*"))
for _, d := range old {
if fi, err := os.Stat(d); err == nil && fi.IsDir() && time.Since(fi.ModTime()) > cursorSnapStale {
os.RemoveAll(d)
}
}
}