diff --git a/internal/sessions/agy_held_other.go b/internal/sessions/agy_held_other.go new file mode 100644 index 000000000..82efb6957 --- /dev/null +++ b/internal/sessions/agy_held_other.go @@ -0,0 +1,7 @@ +//go:build !unix + +package sessions + +// agyHeld: how agy locks a conversation it has open on Windows hasn't been +// seen, so there the minute since its files were written decides alone. +func agyHeld(string) bool { return false } diff --git a/internal/sessions/agy_held_unix.go b/internal/sessions/agy_held_unix.go new file mode 100644 index 000000000..673e1e87c --- /dev/null +++ b/internal/sessions/agy_held_unix.go @@ -0,0 +1,30 @@ +//go:build unix + +package sessions + +import ( + "errors" + "io/fs" + "os" + "syscall" +) + +// agyHeld says whether a running agy may hold the presence lock p: it +// flocks presence/.lock for as long as it has the conversation open. +// No lock is not held; a lock that can't be opened or tried is unknown, and +// counts as held. +func agyHeld(p string) bool { + f, err := os.Open(p) + if errors.Is(err, fs.ErrNotExist) { + return false + } + if err != nil { + return true + } + defer f.Close() + if syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) != nil { + return true + } + syscall.Flock(int(f.Fd()), syscall.LOCK_UN) + return false +} diff --git a/internal/sessions/agy_manage.go b/internal/sessions/agy_manage.go new file mode 100644 index 000000000..7be38ea9d --- /dev/null +++ b/internal/sessions/agy_manage.go @@ -0,0 +1,24 @@ +package sessions + +import "path/filepath" + +// agyDataDir is the agy data folder a conversation's files are in: a +// transcript is brain//.system_generated/logs/transcript_full.jsonl in +// it, a conversation listed from its row alone conversations/.db. +func agyDataDir(fs []file) string { + for _, f := range fs { + if !f.main { + continue + } + up := 5 + if agyRowOnly(f.path) { + up = 2 + } + d := f.path + for range up { + d = filepath.Dir(d) + } + return d + } + return "" +} diff --git a/internal/sessions/agy_manage_other_test.go b/internal/sessions/agy_manage_other_test.go new file mode 100644 index 000000000..98ee35abf --- /dev/null +++ b/internal/sessions/agy_manage_other_test.go @@ -0,0 +1,7 @@ +//go:build !unix + +package sessions + +import "os" + +func syscallFlock(*os.File) error { return nil } diff --git a/internal/sessions/agy_manage_test.go b/internal/sessions/agy_manage_test.go new file mode 100644 index 000000000..178a75207 --- /dev/null +++ b/internal/sessions/agy_manage_test.go @@ -0,0 +1,162 @@ +package sessions + +import ( + "database/sql" + "errors" + "os" + "path/filepath" + "runtime" + "testing" + "time" +) + +// agyConversation writes the files agy keeps of conversation id beside its +// brain folder, shaped as a real agy 1.4.12 left them: its trajectory +// database (SQLite, with the -wal and -shm agy leaves while it has it +// open), its annotations (title:"…" text), and its presence lock (empty). +func agyConversation(t *testing.T, dir, id string) []string { + t.Helper() + files := map[string]string{ + filepath.Join("conversations", id+".db"): "SQLite format 3\x00", + filepath.Join("conversations", id+".db-wal"): "", + filepath.Join("conversations", id+".db-shm"): "\x00\x00", + filepath.Join("annotations", id+".pbtxt"): `title:"Hello there."`, + filepath.Join("presence", id+".lock"): "", + } + var out []string + for rel, body := range files { + p := filepath.Join(dir, rel) + os.MkdirAll(filepath.Dir(p), 0o755) + if err := os.WriteFile(p, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + out = append(out, p) + } + return out +} + +// ysm119 on Discord: agy's sessions couldn't be deleted on the Sessions +// page, Codex's could. A conversation goes to the trash as agy's own +// delete (its /resume picker's) removes it: its brain folder, its +// conversations/.db with SQLite's files beside it, and its +// annotations; nothing of another conversation's, and not the summaries +// agy keeps open, whose row agy doesn't list once the files are gone. +// Restore puts them back. +func TestAgyDeleteRestore(t *testing.T) { + dir := putAgy(t) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "config")) + agyConversation(t, dir, agyTwoTurns) + others := agyConversation(t, dir, agyRead) + others = append(others, filepath.Join(dir, "brain", agyRead), filepath.Join(dir, "conversation_summaries.db")) + ageTree(dir) + Reset() + + if !Deletable("agy") { + t.Fatal("agy not deletable") + } + m, ok := findManaged(ListAgent("agy"), agyTwoTurns) + if !ok || !m.Deletable { + t.Fatalf("listed as %+v", m) + } + conv := filepath.Join(dir, "conversations", agyTwoTurns+".db") + mine := []string{ + filepath.Join(dir, "brain", agyTwoTurns), conv, conv + "-wal", conv + "-shm", + filepath.Join(dir, "annotations", agyTwoTurns+".pbtxt"), + } + + // one being written to is left alone + now := time.Now() + os.Chtimes(conv+"-wal", now, now) + if _, err := Delete("agy", agyTwoTurns); !errors.Is(err, ErrActive) { + t.Fatalf("a conversation just written: %v", err) + } + ageTree(dir) + + // and so is one a running agy has open, however long since it wrote + if runtime.GOOS != "windows" { + lock, err := os.Open(filepath.Join(dir, "presence", agyTwoTurns+".lock")) + if err != nil { + t.Fatal(err) + } + if err := syscallFlock(lock); err != nil { + t.Fatal(err) + } + _, err = Delete("agy", agyTwoTurns) + lock.Close() + if !errors.Is(err, ErrActive) { + t.Fatalf("a conversation agy has open: %v", err) + } + } + + tr, err := Delete("agy", agyTwoTurns) + if err != nil { + t.Fatal(err) + } + if len(tr.Items) != len(mine) { + t.Fatalf("trashed %+v, want %d items", tr.Items, len(mine)) + } + for _, p := range mine { + if _, err := os.Lstat(p); err == nil { + t.Fatalf("%s is still there", p) + } + } + for _, p := range others { + if _, err := os.Stat(p); err != nil { + t.Fatalf("another's %s: %v", p, err) + } + } + Reset() + if _, ok := findManaged(ListAgent("agy"), agyTwoTurns); ok { + t.Fatal("still listed after the delete") + } + + if _, err := Restore(tr.Key); err != nil { + t.Fatal(err) + } + for _, p := range mine { + if _, err := os.Stat(p); err != nil { + t.Fatalf("%s isn't back: %v", p, err) + } + } + Reset() + if back, ok := findManaged(ListAgent("agy"), agyTwoTurns); !ok || back.Title != m.Title { + t.Fatalf("the restored conversation: %+v", back) + } +} + +// A conversation listed from its row alone (no transcript, #1718) is +// deleted by its conversations/.db, and its row then lists nothing. +func TestAgyDeleteRowOnly(t *testing.T) { + dir := putAgy(t) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "config")) + const id = "7d1e2f3a-4b5c-4d6e-8f90-a1b2c3d4e5f6" + db, err := sql.Open("sqlite", filepath.Join(dir, "conversation_summaries.db")) + if err != nil { + t.Fatal(err) + } + if _, err := db.Exec(`INSERT INTO conversation_summaries (conversation_id, title, last_modified_time, workspace_uris, last_user_input_time) VALUES (?, 'Older talk', '2026-10-12 04:41:53.5+00:00', '["file:///work/old"]', '2026-10-12 04:41:53.5+00:00')`, id); err != nil { + t.Fatal(err) + } + db.Close() + agyConversation(t, dir, id) + ageTree(dir) + Reset() + + if m, ok := findManaged(ListAgent("agy"), id); !ok || !m.Deletable { + t.Fatalf("listed as %+v", m) + } + tr, err := Delete("agy", id) + if err != nil { + t.Fatal(err) + } + if len(tr.Items) != 4 { + t.Fatalf("trashed %+v, want its database, -wal, -shm and annotations", tr.Items) + } + Reset() + if _, ok := findManaged(ListAgent("agy"), id); ok { + t.Fatal("its row is still listed") + } + if _, ok := findManaged(ListAgent("agy"), agyTwoTurns); !ok { + t.Fatal("another conversation went with it") + } +} diff --git a/internal/sessions/agy_manage_unix_test.go b/internal/sessions/agy_manage_unix_test.go new file mode 100644 index 000000000..ac45291f6 --- /dev/null +++ b/internal/sessions/agy_manage_unix_test.go @@ -0,0 +1,13 @@ +//go:build unix + +package sessions + +import ( + "os" + "syscall" +) + +// syscallFlock holds f's lock as agy holds a conversation's presence lock. +func syscallFlock(f *os.File) error { + return syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) +} diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index a02e47c01..0ae32fcbe 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -26,7 +26,12 @@ import ( // DeepSeek Harness's (a session's folder, and its listing's cached row) and // Droid's (a transcript and its .settings.json, and its subagents'; Droid's // own session index is a cache it rebuilds from those files, as it does -// when it deletes an empty draft itself; zachzhao1984 on Discord). +// when it deletes an empty draft itself; zachzhao1984 on Discord) and +// Antigravity CLI's (agy: a conversation's brain folder, conversations/.db +// and annotations/.pbtxt, what agy's own delete removes; ysm119 on +// Discord). agy's conversation_summaries.db and jetbox_summaries_proto.pb, +// which it keeps open, keep the row, and agy lists no conversation whose +// files are gone, as a restore brings it back to its /resume list. // Hermes's and Alma's are rows in a database the agent keeps open and // writes, so they are only listed, and so are OpenCode's and ZCode's, and // Cline's, whose sessions.db lists a session whose files are gone. Grok @@ -70,7 +75,7 @@ type AgentCount struct { // those kept as files of their own, in a layout magpie knows whole. func Deletable(agent string) bool { switch agent { - case "claude", "qoder", "qoder-cn", "workbuddy", "codebuddy", "codex", "pi", "raft", "omp", "cursor", "dsh", "droid": + case "claude", "qoder", "qoder-cn", "workbuddy", "codebuddy", "codex", "pi", "raft", "omp", "cursor", "dsh", "droid", "agy": return true } return false @@ -411,6 +416,11 @@ func Delete(agent, id string) (Trashed, error) { return Trashed{}, ErrActive } } + // agy holds a conversation's presence lock while it has it open, idle + // for longer than a minute too + if agent == "agy" && agyHeld(filepath.Join(agyDataDir(fs), "presence", id+".lock")) { + return Trashed{}, ErrActive + } t := Trashed{Agent: agent, ID: id, Title: s.Title, Cwd: s.Cwd, Last: s.Last, Deleted: now} name := now.UTC().Format("20060102T150405.000000000") + "-" + id dir := filepath.Join(TrashDir(), agent, name) @@ -482,6 +492,22 @@ func sessionPaths(agent, id string, fs []file) []string { } return out } + if agent == "agy" { + // what agy's own delete (its /resume picker) removes: the + // conversation's brain folder, its trajectory database with the + // journal files SQLite keeps beside it, and its annotations + dir := agyDataDir(fs) + if dir == "" { + return nil + } + add(filepath.Join(dir, "brain", id)) + db := filepath.Join(dir, "conversations", id+".db") + for _, p := range []string{db, db + "-wal", db + "-shm", db + "-journal"} { + add(p) + } + add(filepath.Join(dir, "annotations", id+".pbtxt")) + return out + } if agent == "dsh" { // each session's own folder (the parent's and its subagents'), and // the row dsh caches of it for its listing; none at all when one