sessions: Antigravity CLI (agy) sessions can be deleted (ysm119 on Discord)

ysm119 (#help) could delete Codex sessions on the Sessions page but not
agy's, and wanted to clear out empty ones. A delete now moves a
conversation to magpie's trash the way agy's own /resume delete (F4)
removes it: brain/<id>/, conversations/<id>.db with the -wal, -shm and
-journal SQLite leaves beside it, and annotations/<id>.pbtxt. Restore puts
them back.

What agy keeps of a conversation, read from real agy 1.4.12 in a sandbox
HOME against a fake Gemini endpoint (the live ~/.gemini was only read):
the files above, presence/<id>.lock, a row in conversation_summaries.db,
an entry in jetbox_summaries_proto.pb, and cache/last_conversations.json.
agy's own delete removes the files, the row and the jetbox entry. It
leaves the lock and last_conversations.json. magpie leaves the row and
jetbox entry too, because agy keeps both files open and jetbox is an
unknown protobuf. agy's /resume list leaves out a row whose files are
gone, so it never lists a deleted conversation.

A conversation agy has open is not deleted: agy flocks presence/<id>.lock
for as long as it has it open, idle too, and a held lock (or one that
can't be tried) is ErrActive. On Windows how agy locks it hasn't been seen,
so the minute since a write decides there alone.

Siblings: Hermes, Alma, OpenCode, ZCode, Cline, Grok Build, Kimi Code and
Claude Desktop (Cowork) sessions still can't be deleted. Each keeps an
index magpie would have to edit or leave stale; filed as #4595 with what
each needs.

Run at d018b9302 (on origin/main c30d3b7b6), then rebased onto 2cca11176,
which changes internal/gateway only; go test ./internal/sessions passed
again at the pushed head:
- mote: gofmt -l internal/sessions clean; go vet ./internal/sessions
  (darwin, GOOS=linux -tags nogui, GOOS=windows -tags nogui); go build
  ./... and the linux/windows nogui builds: ok.
- mote: go test -tags nogui -count=1 -timeout 45m ./...: 82 packages ok,
  internal/gui red only on TestSettingsSaveKeepsWhatItDoesNotSend (#4514)
  and TestColdTurnsTaken (#4510), both filed before and untouched here.
  go test ./internal/gui alone also showed TestMainWindowCorners (#3949).
- Windows (win-run): go test -run Agy ./internal/sessions: both new tests
  pass. TestAgySessions and TestAgyRowOnly are red there already (#4115,
  a /work cwd read as \work).
- Real agy 1.4.12 in a sandbox HOME, with a test binary built from this
  tree: Delete of one of three conversations moved its brain folder,
  .db and .pbtxt. agy's /resume then listed 2 of 3 and Other "No
  conversations available". Restore brought it back, and /resume listed 3.
  Before this, with the files moved by hand, `agy --conversation <id>`
  resumed a restored conversation and appended to it.

Broken by hand (each red in TestAgyDeleteRestore/TestAgyDeleteRowOnly):
agy taken out of Deletable; the presence-lock check turned off (a
conversation agy has open was deleted); only <id>.db moved, not its
-wal/-shm (a fresh -wal wasn't seen as recent, and they'd be left behind).

Not tried: agy on Windows or Linux for real; the GUI suite (no GUI file
changed: the Sessions page shows Delete from /api/sessions/manage's
deletable flag).
This commit is contained in:
yetone
2026-10-31 23:10:48 +08:00
parent 2cca111764
commit 29d0ab39fb
7 changed files with 271 additions and 2 deletions
+7
View File
@@ -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 }
+30
View File
@@ -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/<id>.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
}
+24
View File
@@ -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/<id>/.system_generated/logs/transcript_full.jsonl in
// it, a conversation listed from its row alone conversations/<id>.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 ""
}
@@ -0,0 +1,7 @@
//go:build !unix
package sessions
import "os"
func syscallFlock(*os.File) error { return nil }
+162
View File
@@ -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/<id>.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/<id>.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")
}
}
+13
View File
@@ -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)
}
+28 -2
View File
@@ -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/<id>.db
// and annotations/<id>.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