Files
magpie/internal/sessions/cursor.go
T
yetone c51b0ca9fa sessions: the Antigravity app connected to WSL has its usage read from the distro (@guyng35955 on X)
@guyng35955 on X: on Windows, with the Antigravity app connected to a
WSL Ubuntu, magpie showed no Antigravity usage.

Why. Antigravity 2.x (dist/wsl.js and languageServer.js in its
app.asar) runs a Linux language server in the distro when it is
connected to WSL ($HOME/.antigravity-server/bin/<version>/
language_server, started through wsl.exe with --app_data_dir
antigravity), so the conversations go to the distro's
~/.gemini/antigravity, not %USERPROFILE%\.gemini\antigravity. magpie
read Antigravity's conversations only from this computer's folder:
- sessions.wslAgents had no antigravity (nor agy, whose
  ~/.gemini/antigravity-cli in a distro wasn't read either);
- internal/agent kept a distro for the Sessions page only when one of
  wslKinds or the Copilot CLI's folder was there, so a distro with only
  the app's data never reached sessions.WSLHomes;
- and, found on the Windows box: no SQLite database under
  \\wsl.localhost opens in place. Every form (file: URI, plain path,
  mode=ro, provider.OpenReadOnly) answers SQLITE_BUSY after its 3s
  busy_timeout, since the share doesn't take the locks a WAL database
  needs. So listing the files alone would have shown nothing still.

What changed.
- agent: wslKept keeps a distro with ~/.gemini/antigravity(-insiders),
  and the probe looks for them (no agent row for them).
- sessions: wslAgents reads the app's conversations (insiders under
  their own key) and agy's in a distro; their summary rows name the
  distro's folders as Linux does (agyFolder with goos "linux", so
  /home/me/site, not a Windows path). The distro's calls count in
  Calls (wslFiles("antigravity"), wslCallFiles("agy")). The listing
  kept on disk (sessions-wsl.json) carries each file's row, rev and
  read-only, so a stopped distro keeps its titles and folders. A
  distro's ~/.gemini that is this computer's own (a link) is read once,
  as this computer's (sameDir).
- sessions: openAppStore opens agy's and the app's databases read only
  in place, or, under \\wsl.localhost or \\wsl$, as OpenUntouched does
  (immutable with no -wal, else from a copy of the db and its -wal).
- docs/subsystems/agent-wiring.md says which distros are kept for reads
  only.

Not changed: session-read consent ("allow selected" names agents found
on this computer) doesn't look in WSL, as before for every WSL agent.
OpenCode's and ZCode's SQLite readers aren't used for a distro (not in
wslAgents), so they keep provider.OpenReadOnly.

Run (at the tree rebased on dc85a7ca5; then rebased on c8290df25, which
changes nothing under internal/sessions or internal/agent):
- mote: gofmt -l internal (clean); GOOS=linux go vet -tags nogui ./...
  and GOOS=windows go vet ./... ok; go test -tags nogui -count=1
  -timeout 45m ./internal/sessions/ ./internal/agent/: ok (sessions 104s,
  agent 223s).
- Windows 11 (xiaomi-gpu, win-run): go test -run TestWSL
  ./internal/sessions/ ok.
- Real \\wsl.localhost read on xiaomi-gpu: a fixture conversation
  (WAL) and conversation_summaries.db, and agy's conversations, written
  in the WSL distro magpie-desktop's real ~/.gemini from inside Linux;
  a throwaway test on Windows with WSLHomes at
  \\wsl.localhost\magpie-desktop\home\yeton. Before openAppStore: agy
  listed with no folders, the app's conversation missing, 0
  antigravity calls, listing 6.4s (each open waits 3s for BUSY).
  After: the conversation listed with the distro, its title, folder
  /home/me/site, tokens and 4 calls; agy's folders read; listing
  0.21s. With a writer in the distro holding a title only in the -wal
  (wal_autocheckpoint=0, db held open), Windows read the -wal's title.
  The fixture and binaries were removed after.

Broken by hand:
- the three wslAgents entries and the Calls line out:
  TestWSLAntigravityApp red (no antigravity session from the distro);
- sameDir check out: TestWSLAntigravityLinkedHomeOnce red (listed as
  the distro's);
- the listing's row (m.agy, m.rev) not carried: TestWSLAntigravityApp
  red (no title or folder);
- wslKept without the Antigravity dirs: TestWSLAntigravityHome red
  (no homes).

Not tried: a real Antigravity sign-in connected to WSL (the box's app
isn't signed in, and the connection needs its GUI), so the language
server's own files are this fixture, laid out as its source writes
them; the full suite (five full suites were queued on mote's
suite.lock and two on mica's).
2026-11-14 12:27:25 +08:00

536 lines
16 KiB
Go

package sessions
import (
"database/sql"
"encoding/binary"
"encoding/hex"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"net/url"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/provider"
)
// Cursor's CLI (cursor-agent) keeps a chat in a folder of its own under its
// config folder's chats/, in a folder per working directory named by the
// MD5 of its path: chats/<md5(cwd)>/<chat id>/. store.db there is a SQLite
// file of two tables, as its run store makes them
// (cursor-sdk-local-runtime's sqlite-blob-store): meta (key, value), whose
// one row "0" is the chat's metadata as JSON written in hex — agentId, name
// (its title; "New Agent" until it has one), createdAt (milliseconds),
// lastUsedModel, latestRootBlobId (hex) and, for a subagent's chat,
// subagentInfo {parentAgentId, rootParentAgentId, toolCallId, typeName} —
// and blobs (id, data), content-addressed by the hex of their SHA-256. The
// root blob is a ConversationStateStructure (agent.v1, protobuf): field 1,
// root_prompt_messages_json, the ids of the conversation's messages in
// order; field 13, summary_archives, the ids of the archives a compaction
// left (ConversationSummaryArchive, whose field 1 names the messages it
// summed up); field 26 the time the conversation started. A message's blob
// is the AI SDK's message as JSON: role (system, user, assistant, tool) and
// content, words or parts (text, reasoning, tool-call with toolName, …).
// What the user typed is in <user_query> in a user message, beside the
// context Cursor puts in it (<user_info>, <rules>, …). Newer builds write a
// meta.json beside store.db too: title, createdAtMs, updatedAtMs,
// hasConversation, isSubagent and the cwd, which store.db doesn't name. The
// requests go to Cursor's servers only, which keep what they cost, so a
// chat here says nothing of tokens. `cursor-agent --resume <id>` picks one
// up again, in its folder: the chats are looked up by it.
// CursorDir is Cursor's CLI config folder: $CURSOR_CONFIG_DIR, else cursor
// in $XDG_CONFIG_HOME, else ~/.cursor.
func CursorDir() string {
if d := strings.TrimSpace(appdir.Getenv("CURSOR_CONFIG_DIR")); d != "" {
return d
}
if d := strings.TrimSpace(appdir.Getenv("XDG_CONFIG_HOME")); d != "" {
return filepath.Join(d, "cursor")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".cursor")
}
// cursorDataDir is where Cursor keeps its projects' files, the chats'
// transcripts among them: $CURSOR_DATA_DIR, else ~/.cursor.
func cursorDataDir() string {
if d := strings.TrimSpace(appdir.Getenv("CURSOR_DATA_DIR")); d != "" {
return d
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".cursor")
}
var cursorSlugBad = lazyre.New(`[^a-zA-Z0-9]+`)
// cursorTranscripts are the transcripts Cursor writes of a chat, from its
// store, under projects/<the cwd made a slug>/agent-transcripts/: <id>/
// (its own and its subagents'), and <id>.jsonl and <id>.txt of older builds.
func cursorTranscripts(cwd, id string) []string {
slug := strings.Trim(cursorSlugBad.ReplaceAllString(cwd, "-"), "-")
if slug == "" || !safeID.MatchString(id) {
return nil
}
dir := filepath.Join(cursorDataDir(), "projects", slug, "agent-transcripts")
return []string{filepath.Join(dir, id), filepath.Join(dir, id+".jsonl"), filepath.Join(dir, id+".txt")}
}
// cursorMetaFile is a chat's meta.json.
type cursorMetaFile struct {
Title string `json:"title"`
Created int64 `json:"createdAtMs"`
Updated int64 `json:"updatedAtMs"`
Subagent bool `json:"isSubagent"`
Cwd string `json:"cwd"`
HasConvers bool `json:"hasConversation"`
}
func readCursorMetaFile(dir string) (cursorMetaFile, bool) {
var m cursorMetaFile
b, err := os.ReadFile(filepath.Join(dir, "meta.json"))
return m, err == nil && json.Unmarshal(b, &m) == nil
}
// cursorMeta is a chat's row "0" in store.db's meta.
type cursorMeta struct {
AgentID string `json:"agentId"`
Name string `json:"name"`
CreatedAt int64 `json:"createdAt"`
Model string `json:"lastUsedModel"`
Root string `json:"latestRootBlobId"`
Subagent *struct {
Parent string `json:"parentAgentId"`
RootParent string `json:"rootParentAgentId"`
} `json:"subagentInfo"`
}
// readCursorMeta reads row "0": JSON in hex, or (should a build write it so)
// the JSON itself.
func readCursorMeta(db *sql.DB) (cursorMeta, bool) {
var m cursorMeta
var v string
if db.QueryRow(`SELECT value FROM meta WHERE key = '0'`).Scan(&v) != nil {
return m, false
}
v = strings.TrimSpace(v)
b := []byte(v)
if !strings.HasPrefix(v, "{") {
d, err := hex.DecodeString(v)
if err != nil {
return m, false
}
b = d
}
return m, json.Unmarshal(b, &m) == nil
}
// cursorParents keeps, by a chat's store, the chat a subagent's ran under,
// so the stores are opened again only when they change.
var cursorParents struct {
sync.Mutex
m map[string]cursorParent
}
type cursorParent struct {
size int64
mod time.Time
of string
}
// cursorParentOf is the chat a subagent's chat ran under, "" for one of the
// user's own. A meta.json that says it isn't a subagent's is taken at its
// word; otherwise store.db is asked.
func cursorParentOf(f file) string {
dir := filepath.Dir(f.path)
if m, ok := readCursorMetaFile(dir); ok && !m.Subagent {
return ""
}
cursorParents.Lock()
defer cursorParents.Unlock()
if p, ok := cursorParents.m[f.path]; ok && p.size == f.size && p.mod.Equal(f.mod.Time()) {
return p.of
}
of := ""
if db, done, err := openCursorStore(f.path); err == nil {
if m, ok := readCursorMeta(db); ok && m.Subagent != nil {
of = m.Subagent.RootParent
if of == "" {
of = m.Subagent.Parent
}
}
done()
}
if cursorParents.m == nil {
cursorParents.m = map[string]cursorParent{}
}
cursorParents.m[f.path] = cursorParent{f.size, f.mod.Time(), of}
return of
}
// openCursorStore opens a chat's store.db to read it without writing a byte
// in Cursor's folder. store.db is in WAL mode, and SQLite opening it even
// read-only makes its -wal and -shm files beside it when they aren't there
// (Cursor deletes them when it closes the chat) and writes its read marks
// in -shm when they are; both would change the folder and make the chat
// look written to a moment ago. A store with no log to replay is opened
// immutable, which touches nothing; one with a log is read from a copy of
// the two files, in a folder of magpie's own that close removes; a large
// store's copy is shared while it is unchanged (cursor_snapshot.go).
func openCursorStore(path string) (db *sql.DB, closeDB func(), err error) {
return OpenUntouched(path)
}
// OpenUntouched opens another app's SQLite database at path to read it,
// as openCursorStore does, without writing a byte beside it: immutable
// with no log to replay, from a copy of it and its log with one. A
// database in WAL mode can't be opened read-only while its app is closed
// (SQLite would make its -shm file), so a reader that needs it read
// whether the app runs or not opens it this way. closeDB removes the copy.
func OpenUntouched(path string) (db *sql.DB, closeDB func(), err error) {
open := openSQLite
if fi, err := os.Stat(path + "-wal"); err != nil || fi.Size() == 0 {
db, err := open(path, "immutable=1")
if err != nil {
return nil, nil, err
}
return db, func() { db.Close() }, nil
}
if stamp, size, ok := cursorStamp(path); ok && size >= cursorSnapMin {
return sharedCursorStore(path, stamp, open)
}
db, tmp, err := snapshotCursorStore(path, open)
if err != nil {
return nil, nil, err
}
return db, func() { db.Close(); os.RemoveAll(tmp) }, nil
}
// openAppStore opens another app's SQLite database at path to read it:
// read only, in place; or, in a WSL distro read through \\wsl.localhost
// (or \\wsl$), as OpenUntouched does. SQLite can't take the locks a WAL
// database needs over that share, so an open in place there answers
// SQLITE_BUSY, every time, after its busy_timeout (the Antigravity app
// connected to WSL, @guyng35955 on X; tried on Windows 11). done closes it.
func openAppStore(path string) (db *sql.DB, done func(), err error) {
if onWSLShare(path) {
return OpenUntouched(path)
}
db, err = provider.OpenReadOnly(path)
if err != nil {
return nil, nil, err
}
return db, func() { db.Close() }, nil
}
// onWSLShare is whether path is in a WSL distro, read through Windows'
// share of it.
func onWSLShare(path string) bool {
p := strings.ToLower(strings.ReplaceAll(path, `\`, "/"))
return strings.HasPrefix(p, "//wsl.localhost/") || strings.HasPrefix(p, "//wsl$/")
}
// openSQLite opens the SQLite database at p, with query as its URI's.
func openSQLite(p, query string) (*sql.DB, error) {
u := url.URL{Scheme: "file", Path: filepath.ToSlash(p), RawQuery: query}
if len(u.Path) >= 2 && u.Path[1] == ':' {
u.Path = "/" + u.Path
}
db, err := sql.Open("sqlite", u.String())
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
db.Close()
return nil, err
}
return db, nil
}
func copyFile(from, to string) error {
in, err := os.Open(from)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(to)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
out.Close()
return err
}
return out.Close()
}
func cursorFiles() []file {
if !Reads("cursor") {
return nil
}
paths, _ := filepath.Glob(filepath.Join(CursorDir(), "chats", "*", "*", "store.db"))
var out []file
for _, p := range paths {
dir := filepath.Dir(p)
id := filepath.Base(dir)
if !safeID.MatchString(id) {
continue
}
f := file{agent: "cursor", key: "cursor:" + id, path: p, main: true}
if !stat(&f) {
continue
}
// what isn't in store.db yet is in its write-ahead log; the title
// and the cwd are in meta.json
for _, side := range []string{p + "-wal", filepath.Join(dir, "meta.json")} {
m := file{path: side}
if stat(&m) {
f.size += m.size
if m.mod.After(f.mod) {
f.mod = m.mod
}
}
}
if parent := cursorParentOf(f); parent != "" && parent != id && safeID.MatchString(parent) {
f.key, f.main = "cursor:"+parent, false
}
out = append(out, f)
}
return append(out, cursorEditorFiles(out)...)
}
// pbFields reads a protobuf message's fields of one number that are bytes
// (wire type 2), and its varints of another, without its schema.
func pbFields(b []byte, bytesField, varField int) (vals [][]byte, varint uint64) {
for len(b) > 0 {
key, n := binary.Uvarint(b)
if n <= 0 {
return
}
b = b[n:]
num, typ := int(key>>3), key&7
switch typ {
case 0:
v, n := binary.Uvarint(b)
if n <= 0 {
return
}
b = b[n:]
if num == varField {
varint = v
}
case 1:
if len(b) < 8 {
return
}
b = b[8:]
case 2:
l, n := binary.Uvarint(b)
if n <= 0 || uint64(len(b)-n) < l {
return
}
if num == bytesField {
vals = append(vals, b[n:n+int(l)])
}
b = b[n+int(l):]
case 5:
if len(b) < 4 {
return
}
b = b[4:]
default:
return
}
}
return
}
// cursorMessage is a message as the AI SDK keeps it.
type cursorMessage struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
}
type cursorPart struct {
Type string `json:"type"`
Text string `json:"text"`
ToolName string `json:"toolName"`
}
// cursorMessages are a chat's messages in order: those compactions summed
// up first, then the conversation's own.
func cursorMessages(db *sql.DB, root string) []cursorMessage {
blob := func(id []byte) []byte {
var data []byte
if db.QueryRow(`SELECT data FROM blobs WHERE id = ?`, hex.EncodeToString(id)).Scan(&data) != nil {
return nil
}
return data
}
rootID, err := hex.DecodeString(root)
if err != nil || len(rootID) == 0 {
return nil
}
state := blob(rootID)
if state == nil {
return nil
}
var ids [][]byte
archives, _ := pbFields(state, 13, 0)
for _, a := range archives {
if data := blob(a); data != nil {
summed, _ := pbFields(data, 1, 0)
ids = append(ids, summed...)
}
}
own, _ := pbFields(state, 1, 0)
ids = append(ids, own...)
var out []cursorMessage
for _, id := range ids {
var m cursorMessage
if data := blob(id); data != nil && json.Unmarshal(data, &m) == nil {
out = append(out, m)
}
}
return out
}
// cursorStarted is when the conversation of a root blob started, if it says.
func cursorStarted(db *sql.DB, root string) time.Time {
id, err := hex.DecodeString(root)
if err != nil || len(id) == 0 {
return time.Time{}
}
var data []byte
if db.QueryRow(`SELECT data FROM blobs WHERE id = ?`, hex.EncodeToString(id)).Scan(&data) != nil {
return time.Time{}
}
_, at := pbFields(data, -1, 26)
return ms(int64(at))
}
var (
cursorQuery = lazyre.New(`(?s)<user_query>(.*?)</user_query>`)
// the context Cursor puts in a user message beside what was typed
cursorContext = lazyre.New(`(?s)<(user_info|project_layout|rules|always_applied_workspace_rules|agent_requestable_workspace_rules|user_rules|agent_skills|available_skills|open_and_recently_viewed_files|system_reminder|system-reminder|instructions_update|mcp_instructions|mcp_file_system|mcp_file_system_servers|git_status|agent_transcripts|cursor_rules_context|attached_files|system_notification|task_notification|agent_notification)(?:\s[^>]*)?>.*?</(user_info|project_layout|rules|always_applied_workspace_rules|agent_requestable_workspace_rules|user_rules|agent_skills|available_skills|open_and_recently_viewed_files|system_reminder|system-reminder|instructions_update|mcp_instructions|mcp_file_system|mcp_file_system_servers|git_status|agent_transcripts|cursor_rules_context|attached_files|system_notification|task_notification|agent_notification)>`)
)
// cursorParts are a message's content as parts: words are one text part.
func cursorParts(raw json.RawMessage) []cursorPart {
var s string
if json.Unmarshal(raw, &s) == nil {
return []cursorPart{{Type: "text", Text: s}}
}
var parts []cursorPart
json.Unmarshal(raw, &parts)
return parts
}
// cursorTyped is what was typed in a user message: its <user_query>, else
// its words out of the context Cursor put in it; context alone is none.
func cursorTyped(parts []cursorPart) (typed, other string) {
var texts []string
for _, p := range parts {
if p.Type == "text" {
texts = append(texts, p.Text)
}
}
all := strings.Join(texts, "\n")
if qs := cursorQuery.FindAllStringSubmatch(all, -1); len(qs) > 0 {
var q []string
for _, m := range qs {
q = append(q, m[1])
}
return strings.TrimSpace(strings.Join(q, "\n")), ""
}
rest := strings.TrimSpace(cursorContext.ReplaceAllString(all, ""))
if strings.HasPrefix(rest, "<") {
return "", rest
}
return rest, ""
}
// parseCursor reads a chat's store whole.
func parseCursor(f file) *state {
if f.readOnly {
return parseCursorEditor(f)
}
s := &state{Size: f.size, Mod: f.mod.UnixNano()}
dir := filepath.Dir(f.path)
last := f.mod
if m, ok := readCursorMetaFile(dir); ok {
s.Cwd = m.Cwd
if f.main {
s.own().Named = cursorTitle(m.Title)
}
s.saw(ms(m.Created), f.main)
if u := ms(m.Updated); !u.IsZero() {
last = modOf(u)
}
}
db, done, err := openCursorStore(f.path)
if err != nil {
s.saw(last.Time(), f.main)
return s
}
defer done()
meta, ok := readCursorMeta(db)
if !ok {
s.saw(last.Time(), f.main)
return s
}
s.ID = filepath.Base(dir)
if s.more().Named == "" && f.main {
s.own().Named = cursorTitle(meta.Name)
}
s.saw(ms(meta.CreatedAt), f.main)
s.saw(cursorStarted(db, meta.Root), f.main)
s.saw(last.Time(), f.main)
// the messages carry no time of their own: they count on the day the
// chat was last at work
date := dateOf(s.Last)
for _, m := range cursorMessages(db, meta.Root) {
parts := cursorParts(m.Content)
switch m.Role {
case "user":
typed, other := cursorTyped(parts)
if typed == "" {
if f.main && s.Title == "" && s.more().First == "" && other != "" {
s.own().First = untagged(other)
}
continue
}
if f.main {
s.day(date).Prompts++
if s.Title == "" {
s.Title = title(typed)
}
}
case "assistant":
if f.main {
s.day(date).Replies++
}
for _, p := range parts {
if p.Type == "tool-call" {
s.tool(s.Last, p.ToolName, "")
}
}
}
}
return s
}
// cursorTitle is Cursor's title for a chat, none while it has the one it
// starts with.
func cursorTitle(t string) string {
if t = title(t); t == "New Agent" {
return ""
}
return t
}