Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Remove snapshot directories left by an interrupted backfill

Claudeb341cc62aa61
diff --git a/cli/beacon/internal/sessionfs/sessionfs.go b/cli/beacon/internal/sessionfs/sessionfs.go
index 056795cd..c5df9197 100644
--- a/cli/beacon/internal/sessionfs/sessionfs.go
+++ b/cli/beacon/internal/sessionfs/sessionfs.go
@@ -36,6 +36,7 @@ import (
"strings"
"sync"
"sync/atomic"
+ "time"
)
// maxRefusalsKept bounds the refusals a confinement remembers by path; the count is exact.
@@ -546,10 +547,13 @@ func (c *Confinement) snapshotDir() (string, error) {
parent := c.scratchParent
if parent == "" {
parent = os.TempDir()
- } else if err := os.MkdirAll(parent, 0o700); err != nil {
- return "", fmt.Errorf("create SQLite snapshot directory: %w", err)
+ } else {
+ if err := os.MkdirAll(parent, 0o700); err != nil {
+ return "", fmt.Errorf("create SQLite snapshot directory: %w", err)
+ }
+ removeStaleSnapshotDirs(parent, time.Now())
}
- dir, err := os.MkdirTemp(parent, "beacon-backfill-sqlite-")
+ dir, err := os.MkdirTemp(parent, snapshotDirPrefix)
if err != nil {
return "", fmt.Errorf("create SQLite snapshot directory: %w", err)
}
@@ -557,6 +561,33 @@ func (c *Confinement) snapshotDir() (string, error) {
return dir, nil
}
+const snapshotDirPrefix = "beacon-backfill-sqlite-"
+
+// staleSnapshotAge is how old a snapshot directory left in the scratch parent must be before a
+// later sweep removes it. A sweep cleans up its own copies when it ends normally, but a run killed
+// mid-copy (Ctrl+C, an installer timeout) cannot, and its copies are private user data. The age
+// keeps a concurrent sweep in another process from losing the directory it is reading.
+const staleSnapshotAge = time.Hour
+
+// removeStaleSnapshotDirs deletes snapshot directories older than staleSnapshotAge under parent.
+// Only the configured scratch parent is swept, never os.TempDir().
+func removeStaleSnapshotDirs(parent string, now time.Time) {
+ entries, err := os.ReadDir(parent)
+ if err != nil {
+ return
+ }
+ for _, entry := range entries {
+ if !entry.IsDir() || !strings.HasPrefix(entry.Name(), snapshotDirPrefix) {
+ continue
+ }
+ info, err := entry.Info()
+ if err != nil || now.Sub(info.ModTime()) < staleSnapshotAge {
+ continue
+ }
+ _ = os.RemoveAll(filepath.Join(parent, entry.Name()))
+ }
+}
+
func copyInto(path string, src io.Reader) (int64, error) {
dst, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
diff --git a/cli/beacon/internal/sessionfs/stale_test.go b/cli/beacon/internal/sessionfs/stale_test.go
new file mode 100644
index 00000000..0e966643
--- /dev/null
+++ b/cli/beacon/internal/sessionfs/stale_test.go
@@ -0,0 +1,46 @@
+package sessionfs
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// A sweep killed mid-copy leaves its snapshot directory behind; the next sweep removes it once it
+// is old enough, and leaves recent directories (another process's live sweep) and anything that is
+// not a snapshot directory alone.
+func TestSnapshotDirRemovesStaleSnapshotsFromKilledSweeps(t *testing.T) {
+ parent := t.TempDir()
+ now := time.Now()
+ mk := func(name string, age time.Duration) string {
+ dir := filepath.Join(parent, name)
+ if err := os.MkdirAll(dir, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "state.vscdb"), []byte("copy"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chtimes(dir, now.Add(-age), now.Add(-age)); err != nil {
+ t.Fatal(err)
+ }
+ return dir
+ }
+ stale := mk(snapshotDirPrefix+"old", 2*staleSnapshotAge)
+ live := mk(snapshotDirPrefix+"live", time.Minute)
+ other := mk("unrelated", 2*staleSnapshotAge)
+
+ c := &Confinement{scratchParent: parent}
+ dir, err := c.snapshotDir()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(stale); !os.IsNotExist(err) {
+ t.Errorf("stale snapshot directory survived: %v", err)
+ }
+ for _, keep := range []string{live, other, dir} {
+ if _, err := os.Stat(keep); err != nil {
+ t.Errorf("%s was removed: %v", keep, err)
+ }
+ }
+}