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.
159 lines
4.1 KiB
Go
159 lines
4.1 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
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|