edge: sweep the static cache once at startup, only when no other edge serves it
Replaces the swap-time sweep (Astra review: it raced handlers picking trees, and edge processes overlapping on a restart). Every running edge now holds a shared flock on <cache>/.edge.lock for its life. A starting edge, before any static handler exists (EnableStatic), takes that lock exclusively or skips the sweep and says so. If it is alone, each <acct>/<app> keeps the tree its current link names; every other tree is renamed to .sweep-<name>-<stamp> at once, so nothing built afterwards can select it, and removed in the background along with earlier .sweep- and .extract- leftovers. Files and links are never touched. A pointer that names a swept version gets a fresh copy from ensureExtracted, which verifies the signed release first. Unix only (flock); elsewhere nothing is swept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+252
−257
docs/superpowers/notes/census-proposal-static-sweep.md+0 −22internal/edge/edge.go+10 −1internal/edge/static.go+1 −8internal/edge/static_sweep.go+0 −105internal/edge/static_sweep_other.go+13 −0internal/edge/static_sweep_test.go+98 −111internal/edge/static_sweep_unix.go+130 −0internal/provision/testdata/plan14-root-reads-chain.tsv+0 −2internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv+0 −5internal/provision/testdata/plan14-root-reads-static-sweep.tsv+0 −3
diff --git a/docs/superpowers/notes/census-proposal-static-sweep.md b/docs/superpowers/notes/census-proposal-static-sweep.md| deleted file mode 100644 |
| index 65f07d879..000000000 |
| --- a/docs/superpowers/notes/census-proposal-static-sweep.md |
| +++ /dev/null |
| @@ -1,22 +0,0 @@ |
| -# Census approval: static-sweep |
| - |
| -Ruling `census-ruling-static-sweep`. Every change below keeps the disposition it already has, so it needs an approval, not a decision. |
| - |
| -Envelopes re-pinned: `internal/edge`. |
| - |
| -Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Escalate if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. An escalation goes to the census owner as a decision. |
| - |
| -Seal: `a010097458a79647251ddf0bc3ed1fcbb3ca78e2f0e1f520c1df35563a16a0c6` → `a010097458a79647251ddf0bc3ed1fcbb3ca78e2f0e1f520c1df35563a16a0c6`. |
| - |
| -Overlay: `internal/provision/testdata/plan14-root-reads-static-sweep.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
| - |
| -## The decision: two new units, written by hand |
| - |
| -`internal/edge/static_sweep.go` adds two units that read the disk, so census-propose leaves them to a decision. Their rows are in `internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv`, chained before the overlay above. They are `@legacy` rows, so the seal moves to `a0100974…a0c6`. |
| - |
| -| unit | reads | proposed disposition | |
| -|---|---|---| |
| -| `staticSite.sweepTrees` | `Readlink`, `ReadDir`, `Lstat` of its own app cache directory (`<cacheDir>/<acct>/<app>`) | `legacy`/`accepted-legacy`, J4 footing (`root-input`), as the static-redirects units are | |
| -| `staticSite.treeMissing` | `Stat` of the tree it serves | the same | |
| - |
| -Both run in the agent (`carlos-edge.service`, user carlos), not root. The sweep removes only extracted trees in that app's own cache directory, and only ones that are all of these: not named by the `current` link, not served by any handler in this process, not the tree the swap just replaced, and more than an hour old. It skips dot-named temp extractions and anything that is not a directory. Every tree is a cache of a signed release: `ensureExtracted` verifies the manifest digest and the archive's SHA-256 before it extracts one again, and `treeMissing` makes a handler do that if its tree has gone. |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 4427c05cb..272d5ffbd 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -155,7 +155,10 @@ type Edge struct { |
| staticStore release.Store |
| staticPub ed25519.PublicKey |
| staticCacheDir string |
| - staticRecheck time.Duration |
| + // staticLock is this process's shared hold on the static cache |
| + // (sweepStaticCache): a starting edge sweeps only when it is alone. |
| + staticLock *os.File |
| + staticRecheck time.Duration |
| // staticRatchet is #253's ratchet for static pointer reads: marks under |
| // <dataDir>/pointer-digests, root's under /opt read too. |
| staticRatchet *release.DigestRatchet |
| @@ -433,6 +436,12 @@ func (e *Edge) EnableStatic(store release.Store, pub ed25519.PublicKey, cacheDir |
| e.staticPub = pub |
| e.staticCacheDir = cacheDir |
| e.staticRecheck = staticRecheck |
| + // Before any static handler exists: old extracted trees go if this is |
| + // the only edge using the cache (static_sweep_unix.go). The lock is |
| + // held, shared, for the process's life. |
| + if cacheDir != "" && e.staticLock == nil { |
| + e.staticLock, _ = sweepStaticCache(cacheDir) |
| + } |
| e.armStoreDoor() |
| } |
diff --git a/internal/edge/static.go b/internal/edge/static.go| index 5350a7462..2f2fd0907 100644 |
| --- a/internal/edge/static.go |
| +++ b/internal/edge/static.go |
| @@ -252,9 +252,7 @@ func (s *staticSite) currentDir(ctx context.Context) (servedTree, error) { |
| if err := s.ratchet.Admit(s.acct, cp); err != nil { |
| return s.stale(err) |
| } |
| - if want := cp.Digest; cp.Version != s.version || (want != "" && want != s.digest) || s.treeMissing() { |
| - // treeMissing: the served tree went from disk (another process's |
| - // sweep, a hand cleanup); ensureExtracted brings it back. |
| + if want := cp.Digest; cp.Version != s.version || (want != "" && want != s.digest) { |
| name, digest, err := s.ensureExtracted(ctx, cp.Version, want) |
| if err != nil { |
| return s.stale(fmt.Errorf("stage %s: %w", cp.Version, err)) |
| @@ -262,13 +260,8 @@ func (s *staticSite) currentDir(ctx context.Context) (servedTree, error) { |
| if err := s.pointCurrent(name); err != nil { |
| return s.stale(fmt.Errorf("swap current -> %s: %w", name, err)) |
| } |
| - replaced := s.name |
| s.version, s.digest, s.name = cp.Version, digest, name |
| s.redirects = s.loadRedirects(s.dir(name), cp.Version) |
| - s.noteServing(name) |
| - if replaced != name { |
| - s.sweepTrees(replaced) |
| - } |
| } |
| s.checkedAt = time.Now() |
| return s.served(), nil |
diff --git a/internal/edge/static_sweep.go b/internal/edge/static_sweep.go| deleted file mode 100644 |
| index 8b19321c5..000000000 |
| --- a/internal/edge/static_sweep.go |
| +++ /dev/null |
| @@ -1,105 +0,0 @@ |
| -package edge |
| - |
| -import ( |
| - "log" |
| - "os" |
| - "path/filepath" |
| - "strings" |
| - "sync" |
| - "time" |
| -) |
| - |
| -// Every version of a static site a box ever served used to stay extracted |
| -// under its app's cache directory: annie-welding held 51 copies of the apt |
| -// site, 20 GB of a 59 GB disk (2026-10-25), and each apt version carries the |
| -// whole package pool, so each new one was bigger. A version swap now sweeps |
| -// the app's directory (sweepTrees). Nothing it removes is lost: a tree is a |
| -// cache of a signed release, and ensureExtracted fetches, verifies and |
| -// extracts it again if a pointer ever names it. |
| - |
| -// staticSweepGrace keeps any tree changed this recently: another process |
| -// (an edge restarting beside this one) may have just extracted it. |
| -var staticSweepGrace = time.Hour |
| - |
| -// staticSweepSync runs the sweep inline (tests only); in production it runs |
| -// off the request, since removing a large tree takes seconds. |
| -var staticSweepSync bool |
| - |
| -// staticServing is every tree a handler in this process serves, by app |
| -// directory and handler: handlers for different channels of one app share |
| -// the directory, and one channel's sweep must not remove another's tree. A |
| -// handler that is dropped keeps its entry, which only keeps a tree longer. |
| -var staticServing = struct { |
| - sync.Mutex |
| - m map[string]map[*staticSite]string |
| -}{m: map[string]map[*staticSite]string{}} |
| - |
| -func (s *staticSite) noteServing(name string) { |
| - staticServing.Lock() |
| - defer staticServing.Unlock() |
| - dir := s.appDir() |
| - if staticServing.m[dir] == nil { |
| - staticServing.m[dir] = map[*staticSite]string{} |
| - } |
| - staticServing.m[dir][s] = name |
| -} |
| - |
| -// sweepTrees removes the trees in this app's directory that nothing needs: |
| -// every tree is kept that the current link names, that any handler here |
| -// serves, that this handler just replaced (requests may still be reading |
| -// it), or that changed within staticSweepGrace. Dot-named entries are |
| -// extractions in progress and are never touched, nor is anything that is |
| -// not a plain directory. |
| -func (s *staticSite) sweepTrees(replaced string) { |
| - dir := s.appDir() |
| - keep := map[string]bool{replaced: true} |
| - if cur, err := os.Readlink(filepath.Join(dir, "current")); err == nil { |
| - keep[cur] = true |
| - } |
| - staticServing.Lock() |
| - for _, name := range staticServing.m[dir] { |
| - keep[name] = true |
| - } |
| - staticServing.Unlock() |
| - sweep := func() { |
| - entries, err := os.ReadDir(dir) |
| - if err != nil { |
| - return |
| - } |
| - removed := 0 |
| - for _, e := range entries { |
| - name := e.Name() |
| - if keep[name] || strings.HasPrefix(name, ".") || name == "current" { |
| - continue |
| - } |
| - path := filepath.Join(dir, name) |
| - fi, err := os.Lstat(path) |
| - if err != nil || !fi.IsDir() || time.Since(fi.ModTime()) < staticSweepGrace { |
| - continue |
| - } |
| - if err := os.RemoveAll(path); err != nil { |
| - log.Printf("edge: static %s/%s: sweep %s: %v", s.app, s.channel, name, err) |
| - continue |
| - } |
| - removed++ |
| - } |
| - if removed > 0 { |
| - log.Printf("edge: static %s/%s: swept %d tree(s) nothing serves", s.app, s.channel, removed) |
| - } |
| - } |
| - if staticSweepSync { |
| - sweep() |
| - return |
| - } |
| - go sweep() |
| -} |
| - |
| -// treeMissing reports whether the tree this handler serves has gone from |
| -// disk (swept by another process, or removed by hand). |
| -func (s *staticSite) treeMissing() bool { |
| - if s.name == "" { |
| - return false |
| - } |
| - _, err := os.Stat(s.dir(s.name)) |
| - return os.IsNotExist(err) |
| -} |
diff --git a/internal/edge/static_sweep_other.go b/internal/edge/static_sweep_other.go| new file mode 100644 |
| index 000000000..2be635b30 |
| --- /dev/null |
| +++ b/internal/edge/static_sweep_other.go |
| @@ -0,0 +1,13 @@ |
| +//go:build !unix |
| + |
| +package edge |
| + |
| +import "os" |
| + |
| +// sweepStaticCache needs flock to know no other edge serves the cache; with |
| +// none here, nothing is swept (static_sweep_unix.go). |
| +func sweepStaticCache(string) (*os.File, <-chan struct{}) { |
| + done := make(chan struct{}) |
| + close(done) |
| + return nil, done |
| +} |
diff --git a/internal/edge/static_sweep_test.go b/internal/edge/static_sweep_test.go| index d451b1406..6b38c36c6 100644 |
| --- a/internal/edge/static_sweep_test.go |
| +++ b/internal/edge/static_sweep_test.go |
| @@ -1,155 +1,142 @@ |
| +//go:build unix |
| + |
| package edge |
| import ( |
| - "context" |
| - "crypto/ed25519" |
| - "net/http/httptest" |
| "os" |
| "path/filepath" |
| "sort" |
| - "strings" |
| + "syscall" |
| "testing" |
| - "time" |
| - |
| - "github.com/carlosframework/platform/internal/registry" |
| - "github.com/carlosframework/platform/internal/release" |
| ) |
| -// noGrace makes every extracted tree old enough to sweep, and waits for a |
| -// swap's sweep to finish before a test looks at the disk. |
| -func noGrace(t *testing.T) { |
| +// cacheTree makes <cache>/<acct>/<app>/<name>/index.html. |
| +func cacheTree(t *testing.T, cache, acct, app, name string) string { |
| + t.Helper() |
| + dir := filepath.Join(cache, acct, app, name) |
| + if err := os.MkdirAll(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(dir, "index.html"), []byte(name), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + return dir |
| +} |
| + |
| +func pointAt(t *testing.T, cache, acct, app, name string) { |
| t.Helper() |
| - old := staticSweepGrace |
| - staticSweepGrace = 0 |
| - staticSweepSync = true |
| - t.Cleanup(func() { staticSweepGrace, staticSweepSync = old, false }) |
| + if err := os.Symlink(name, filepath.Join(cache, acct, app, "current")); err != nil { |
| + t.Fatal(err) |
| + } |
| } |
| -func treesIn(t *testing.T, appDir string) []string { |
| +func entries(t *testing.T, dir string) []string { |
| t.Helper() |
| - entries, err := os.ReadDir(appDir) |
| + es, err := os.ReadDir(dir) |
| if err != nil { |
| t.Fatal(err) |
| } |
| var names []string |
| - for _, e := range entries { |
| - if e.IsDir() { |
| - names = append(names, strings.SplitN(e.Name(), "@", 2)[0]) |
| - } |
| + for _, e := range es { |
| + names = append(names, e.Name()) |
| } |
| sort.Strings(names) |
| return names |
| } |
| -func sweepFixture(t *testing.T) (release.Store, func(string), *staticSite, *httptest.Server) { |
| - store, promote, h, srv, _, _ := sweepFixtureKeys(t) |
| - return store, promote, h, srv |
| -} |
| +// At startup, with no other edge holding the cache, every extracted tree |
| +// but each app's current one goes, and so does crash debris (a temp |
| +// extraction, an earlier sweep's leftovers). The sweep's renames are done |
| +// before it returns, so no handler built after it can select a swept tree; |
| +// the removal itself finishes in the background (waited for here). |
| +func TestStaticStartupSweepKeepsOnlyEachAppsCurrentTree(t *testing.T) { |
| + cache := t.TempDir() |
| + for _, v := range []string{"v1@a", "v2@b", "v3@c"} { |
| + cacheTree(t, cache, "bab", "apt", v) |
| + } |
| + pointAt(t, cache, "bab", "apt", "v3@c") |
| + cacheTree(t, cache, "bab", "apt", ".extract-123") |
| + cacheTree(t, cache, "bab", "apt", ".sweep-v0@z-1") |
| + cacheTree(t, cache, "bac", "docs", "v7@d") |
| + pointAt(t, cache, "bac", "docs", "v7@d") |
| + cacheTree(t, cache, "bac", "nolink", "v1@e") // no current: nothing is kept |
| + if err := os.WriteFile(filepath.Join(cache, "bab", "apt", "note.txt"), []byte("x"), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| -func sweepFixtureKeys(t *testing.T) (release.Store, func(string), *staticSite, *httptest.Server, ed25519.PrivateKey, ed25519.PublicKey) { |
| - t.Helper() |
| - store := release.NewDirStore(filepath.Join(t.TempDir(), "store")) |
| - priv, pub := testKeys(t) |
| - promote := func(v string) { |
| - shipSite(t, store, "site", v, map[string]string{"index.html": "home " + v}) |
| - if err := release.Promote(context.Background(), store, priv, release.OpsAccount, "site", "canary/x", v); err != nil { |
| - t.Fatal(err) |
| - } |
| + lock, done := sweepStaticCache(cache) |
| + if lock == nil { |
| + t.Fatal("no serving lock") |
| } |
| - promote("v1") |
| - rt := registry.Route{Host: "site.example.com", Kind: "static", App: "site", Channel: "canary/x", Status: "ready"} |
| - h := newStaticHandler(rt, store, pub, filepath.Join(t.TempDir(), "cache")).(*staticSite) |
| - h.recheck = 0 |
| - srv := httptest.NewServer(h) |
| - t.Cleanup(srv.Close) |
| - return store, promote, h, srv, priv, pub |
| -} |
| + defer lock.Close() |
| + <-done |
| -// Extracted versions used to stay on the box forever: annie-welding held 51 |
| -// copies of the apt site, 20 GB (2026-10-25). A swap now removes the trees |
| -// nothing serves, keeping the new one and the one it replaced (requests |
| -// still in flight on it). |
| -func TestStaticSwapSweepsTreesNothingServes(t *testing.T) { |
| - noGrace(t) |
| - _, promote, h, srv := sweepFixture(t) |
| - for _, v := range []string{"v1", "v2", "v3", "v4"} { |
| - if v != "v1" { |
| - promote(v) |
| - } |
| - if code, body := getPath(t, srv.URL, "site.example.com", "/"); code != 200 || body != "home "+v { |
| - t.Fatalf("GET / on %s = %d %q", v, code, body) |
| - } |
| + if got, want := entries(t, filepath.Join(cache, "bab", "apt")), []string{"current", "note.txt", "v3@c"}; !equal(got, want) { |
| + t.Errorf("bab/apt = %v, want %v", got, want) |
| } |
| - if got := treesIn(t, h.appDir()); strings.Join(got, ",") != "v3,v4" { |
| - t.Fatalf("trees = %v, want the served v4 and the replaced v3", got) |
| + if got, want := entries(t, filepath.Join(cache, "bac", "docs")), []string{"current", "v7@d"}; !equal(got, want) { |
| + t.Errorf("bac/docs = %v, want %v", got, want) |
| + } |
| + if got := entries(t, filepath.Join(cache, "bac", "nolink")); len(got) != 0 { |
| + t.Errorf("bac/nolink = %v, want empty", got) |
| } |
| } |
| -// Two channels of one app share the app's directory: one channel's swap |
| -// never removes the tree the other is serving. |
| -func TestStaticSweepKeepsWhatAnotherChannelServes(t *testing.T) { |
| - noGrace(t) |
| - store, promote, h, srv, priv, pub := sweepFixtureKeys(t) |
| - // A second handler, another channel, the same app and cache dir, serving v1. |
| - if err := release.Promote(context.Background(), store, priv, release.OpsAccount, "site", "stable", "v1"); err != nil { |
| +// Another edge process serving from the same cache holds its lock: the |
| +// sweep does nothing, and still returns a shared lock for this process. |
| +func TestStaticStartupSweepSkipsWhileAnotherEdgeServes(t *testing.T) { |
| + cache := t.TempDir() |
| + cacheTree(t, cache, "bab", "apt", "v1@a") |
| + cacheTree(t, cache, "bab", "apt", "v2@b") |
| + pointAt(t, cache, "bab", "apt", "v2@b") |
| + other, err := os.OpenFile(filepath.Join(cache, staticLockName), os.O_RDWR|os.O_CREATE, 0o600) |
| + if err != nil { |
| t.Fatal(err) |
| } |
| - rt2 := registry.Route{Host: "stable.example.com", Kind: "static", App: "site", Channel: "stable", Status: "ready"} |
| - h2 := newStaticHandler(rt2, store, pub, h.cacheDir).(*staticSite) |
| - h2.recheck = 0 |
| - srv2 := httptest.NewServer(h2) |
| - t.Cleanup(srv2.Close) |
| - if code, body := getPath(t, srv2.URL, "stable.example.com", "/"); code != 200 || body != "home v1" { |
| - t.Fatalf("stable GET / = %d %q", code, body) |
| - } |
| - for _, v := range []string{"v2", "v3", "v4"} { |
| - promote(v) |
| - getPath(t, srv.URL, "site.example.com", "/") |
| + defer other.Close() |
| + if err := syscall.Flock(int(other.Fd()), syscall.LOCK_SH); err != nil { |
| + t.Fatal(err) |
| } |
| - if code, body := getPath(t, srv2.URL, "stable.example.com", "/"); code != 200 || body != "home v1" { |
| - t.Fatalf("stable GET / after canary's sweeps = %d %q, want its v1 still served", code, body) |
| + |
| + lock, done := sweepStaticCache(cache) |
| + if lock == nil { |
| + t.Fatal("no serving lock") |
| } |
| - if got := strings.Join(treesIn(t, h.appDir()), ","); !strings.Contains(got, "v1") { |
| - t.Fatalf("trees = %s: the other channel's v1 was swept", got) |
| + defer lock.Close() |
| + <-done |
| + if got, want := entries(t, filepath.Join(cache, "bab", "apt")), []string{"current", "v1@a", "v2@b"}; !equal(got, want) { |
| + t.Errorf("bab/apt = %v, want everything kept while another edge serves", got) |
| } |
| } |
| -// A tree that went missing (swept by another process, removed by hand) is |
| -// extracted again at the next check, never served as a 404. |
| -func TestStaticServedTreeThatWentMissingIsExtractedAgain(t *testing.T) { |
| - _, _, h, srv := sweepFixture(t) |
| - if code, _ := getPath(t, srv.URL, "site.example.com", "/"); code != 200 { |
| - t.Fatalf("first GET / = %d", code) |
| - } |
| - if err := os.RemoveAll(h.dir(h.name)); err != nil { |
| +// This process's lock is shared once the sweep is done: a second edge |
| +// starting beside it cannot sweep either. |
| +func TestStaticStartupSweepLeavesASharedLock(t *testing.T) { |
| + cache := t.TempDir() |
| + lock, done := sweepStaticCache(cache) |
| + defer lock.Close() |
| + <-done |
| + probe, err := os.OpenFile(filepath.Join(cache, staticLockName), os.O_RDWR, 0o600) |
| + if err != nil { |
| t.Fatal(err) |
| } |
| - if code, body := getPath(t, srv.URL, "site.example.com", "/"); code != 200 || body != "home v1" { |
| - t.Fatalf("GET / after its tree went = %d %q, want it extracted again", code, body) |
| + defer probe.Close() |
| + if err := syscall.Flock(int(probe.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err == nil { |
| + t.Fatal("an exclusive lock was granted while this edge serves") |
| + } |
| + if err := syscall.Flock(int(probe.Fd()), syscall.LOCK_SH|syscall.LOCK_NB); err != nil { |
| + t.Fatalf("a second edge could not share the lock: %v", err) |
| } |
| } |
| -// Within the grace a tree is kept: another process may have just |
| -// extracted it. A temp extraction (dot-named) and the current link are |
| -// never touched. |
| -func TestStaticSweepKeepsRecentTreesAndTemps(t *testing.T) { |
| - staticSweepSync = true |
| - t.Cleanup(func() { staticSweepSync = false }) |
| - _, promote, h, srv := sweepFixture(t) |
| - getPath(t, srv.URL, "site.example.com", "/") |
| - tmp := filepath.Join(h.appDir(), ".extract-123") |
| - if err := os.MkdirAll(tmp, 0o755); err != nil { |
| - t.Fatal(err) |
| +func equal(a, b []string) bool { |
| + if len(a) != len(b) { |
| + return false |
| } |
| - old := time.Now().Add(-48 * time.Hour) |
| - if err := os.Chtimes(tmp, old, old); err != nil { |
| - t.Fatal(err) |
| - } |
| - promote("v2") |
| - getPath(t, srv.URL, "site.example.com", "/") |
| - promote("v3") |
| - getPath(t, srv.URL, "site.example.com", "/") |
| - if got := strings.Join(treesIn(t, h.appDir()), ","); got != ".extract-123,v1,v2,v3" { |
| - t.Fatalf("trees = %s, want every tree kept inside the grace, and the temp", got) |
| + for i := range a { |
| + if a[i] != b[i] { |
| + return false |
| + } |
| } |
| + return true |
| } |
diff --git a/internal/edge/static_sweep_unix.go b/internal/edge/static_sweep_unix.go| new file mode 100644 |
| index 000000000..dc8afe8ab |
| --- /dev/null |
| +++ b/internal/edge/static_sweep_unix.go |
| @@ -0,0 +1,130 @@ |
| +//go:build unix |
| + |
| +package edge |
| + |
| +import ( |
| + "fmt" |
| + "log" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "syscall" |
| + "time" |
| +) |
| + |
| +// Every version of a static site a box served stayed extracted under |
| +// <cacheDir>/<acct>/<app>/ forever: annie-welding held 51 copies of the apt |
| +// site, 20 GB of 59 (2026-10-25). Sweeping on the serving path races the |
| +// handlers that pick trees (Astra review of static-sweep, two P1s), so the |
| +// sweep runs once, at startup, before any handler exists, and only when no |
| +// other edge process serves from this cache. Every running edge holds a |
| +// shared flock on staticLockName for its lifetime; a starting edge sweeps |
| +// only if it can take that lock exclusively. Each app keeps the tree its |
| +// current link names; every other tree is renamed to a dot-name at once (a |
| +// handler never selects a dot-name, and ensureExtracted then extracts a |
| +// fresh copy if a pointer names that version) and removed in the |
| +// background. Nothing removed is lost: a tree is a cache of a signed |
| +// release, which ensureExtracted verifies before extracting it again. |
| +const staticLockName = ".edge.lock" |
| + |
| +// sweepStaticCache sweeps cacheDir if this is the only edge using it, and |
| +// returns this process's lock on it, held shared until the process exits |
| +// (nil if it could not be opened), and a channel closed once the |
| +// background removal is done. |
| +func sweepStaticCache(cacheDir string) (*os.File, <-chan struct{}) { |
| + done := make(chan struct{}) |
| + if err := os.MkdirAll(cacheDir, 0o755); err != nil { |
| + log.Printf("edge: static cache %s: %v; not swept", cacheDir, err) |
| + close(done) |
| + return nil, done |
| + } |
| + lock, err := os.OpenFile(filepath.Join(cacheDir, staticLockName), os.O_RDWR|os.O_CREATE, 0o600) |
| + if err != nil { |
| + log.Printf("edge: static cache %s: lock: %v; not swept", cacheDir, err) |
| + close(done) |
| + return nil, done |
| + } |
| + fd := int(lock.Fd()) |
| + if err := syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB); err != nil { |
| + log.Printf("edge: static cache %s: another edge process serves from it; not swept this start", cacheDir) |
| + if err := syscall.Flock(fd, syscall.LOCK_SH); err != nil { |
| + log.Printf("edge: static cache %s: shared lock: %v", cacheDir, err) |
| + } |
| + close(done) |
| + return lock, done |
| + } |
| + doomed := renameUnserved(cacheDir) |
| + if err := syscall.Flock(fd, syscall.LOCK_SH); err != nil { |
| + log.Printf("edge: static cache %s: shared lock: %v", cacheDir, err) |
| + } |
| + go func() { |
| + defer close(done) |
| + removed := 0 |
| + for _, p := range doomed { |
| + if err := os.RemoveAll(p); err != nil { |
| + log.Printf("edge: static cache: remove %s: %v", p, err) |
| + continue |
| + } |
| + removed++ |
| + } |
| + if removed > 0 { |
| + log.Printf("edge: static cache %s: swept %d tree(s) no app's current link names", cacheDir, removed) |
| + } |
| + }() |
| + return lock, done |
| +} |
| + |
| +// renameUnserved renames, in every <acct>/<app> directory, each directory |
| +// entry other than the current link's target to a .sweep- name, and |
| +// returns those paths and any earlier sweep's or crashed extraction's |
| +// leftovers for removal. Files and links are never touched. |
| +func renameUnserved(cacheDir string) []string { |
| + var doomed []string |
| + accts, err := os.ReadDir(cacheDir) |
| + if err != nil { |
| + return nil |
| + } |
| + stamp := time.Now().UnixNano() |
| + for _, a := range accts { |
| + if !a.IsDir() || strings.HasPrefix(a.Name(), ".") { |
| + continue |
| + } |
| + apps, err := os.ReadDir(filepath.Join(cacheDir, a.Name())) |
| + if err != nil { |
| + continue |
| + } |
| + for _, ap := range apps { |
| + if !ap.IsDir() || strings.HasPrefix(ap.Name(), ".") { |
| + continue |
| + } |
| + appDir := filepath.Join(cacheDir, a.Name(), ap.Name()) |
| + cur, _ := os.Readlink(filepath.Join(appDir, "current")) |
| + trees, err := os.ReadDir(appDir) |
| + if err != nil { |
| + continue |
| + } |
| + for _, tr := range trees { |
| + name := tr.Name() |
| + path := filepath.Join(appDir, name) |
| + fi, err := os.Lstat(path) |
| + if err != nil || !fi.IsDir() || name == cur { |
| + continue |
| + } |
| + if strings.HasPrefix(name, ".sweep-") || strings.HasPrefix(name, ".extract-") { |
| + doomed = append(doomed, path) |
| + continue |
| + } |
| + if strings.HasPrefix(name, ".") { |
| + continue |
| + } |
| + to := filepath.Join(appDir, fmt.Sprintf(".sweep-%s-%d", name, stamp)) |
| + if err := os.Rename(path, to); err != nil { |
| + log.Printf("edge: static cache: set aside %s: %v", path, err) |
| + continue |
| + } |
| + doomed = append(doomed, to) |
| + } |
| + } |
| + } |
| + return doomed |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 3ad4aaec4..024e21cd2 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -21,5 +21,3 @@ plan14-root-reads-edge-request-records.tsv 84cb1e17c2f9cabddcc1e6ed5b39b00ffa703 |
| plan14-root-reads-data-lifecycle-336.tsv 539ae875b4a089bc6512ae2e047fa9e353e8b760c18312b81ec3400e5e1f6d48 |
| plan14-root-reads-plans-4a-store.tsv 72dc835c04caf5b2b49cc6ecbfd865bb3123b997ee932e8ab7c0df239935a26b |
| plan14-root-reads-plans-4-notices-grace.tsv 43e430a3b3edf9333bfe2c64f24ca9e4c04475dc45ae4983d4e8bcab6a9c0bc4 |
| -plan14-root-reads-static-sweep-new.tsv a010097458a79647251ddf0bc3ed1fcbb3ca78e2f0e1f520c1df35563a16a0c6 |
| -plan14-root-reads-static-sweep.tsv a010097458a79647251ddf0bc3ed1fcbb3ca78e2f0e1f520c1df35563a16a0c6 |
diff --git a/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv b/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv| deleted file mode 100644 |
| index 705d627c0..000000000 |
| --- a/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv |
| +++ /dev/null |
| @@ -1,5 +0,0 @@ |
| -# Census overlay static-sweep-new, hand-written: the two new units census-propose leaves to a decision (the static site tree sweep). census-ruling-static-sweep, asked of the census owner with the approval card (docs/superpowers/notes/census-proposal-static-sweep.md). |
| -@legacy 5bb899128c0ae4758794dbd9b98f10299f891d59cfb9d0be3ec0a337d770288e internal/edge/static_sweep.go staticSite.sweepTrees legacy accepted-legacy accepted-legacy;rev3=census-ruling-static-sweep;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=root-input; on a version swap, reads its own app cache directory (<cacheDir>/<acct>/<app>: the current link, the entries, each entry's lstat) and removes extracted trees that no handler here serves, the current link does not name, the swap did not just replace, and that are older than an hour; skips dot-named and non-directory entries; every tree is a cache of a signed release that ensureExtracted re-verifies before extracting it again; no root decision file:Lstat@610,file:ReadDir@357,file:Readlink@127 a0111e2bde1734cbd657c358c87ca123f4150467c5f8b1d169fd5beaee00e19f |
| -@legacy db6fbd4600cf5c045a23b3aae72afa95bcea1faee6fb2388181a6c62935bcca1 internal/edge/static_sweep.go staticSite.treeMissing legacy accepted-legacy accepted-legacy;rev3=census-ruling-static-sweep;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=root-input; stats the tree this handler serves at each pointer check, and a missing one is extracted again through ensureExtracted; no root decision file:Stat@90 e3a7319d3d1276c848d6477ded7ed67e8bc35159f1256636f7b26b8d90bb46b4 |
| -internal/edge/static_sweep.go staticSite.sweepTrees legacy accepted-legacy accepted-legacy;rev3=census-ruling-static-sweep;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=root-input; on a version swap, reads its own app cache directory (<cacheDir>/<acct>/<app>: the current link, the entries, each entry's lstat) and removes extracted trees that no handler here serves, the current link does not name, the swap did not just replace, and that are older than an hour; skips dot-named and non-directory entries; every tree is a cache of a signed release that ensureExtracted re-verifies before extracting it again; no root decision file:Lstat@610,file:ReadDir@357,file:Readlink@127 a0111e2bde1734cbd657c358c87ca123f4150467c5f8b1d169fd5beaee00e19f |
| -internal/edge/static_sweep.go staticSite.treeMissing legacy accepted-legacy accepted-legacy;rev3=census-ruling-static-sweep;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=root-input; stats the tree this handler serves at each pointer check, and a missing one is extracted again through ensureExtracted; no root decision file:Stat@90 e3a7319d3d1276c848d6477ded7ed67e8bc35159f1256636f7b26b8d90bb46b4 |
diff --git a/internal/provision/testdata/plan14-root-reads-static-sweep.tsv b/internal/provision/testdata/plan14-root-reads-static-sweep.tsv| deleted file mode 100644 |
| index b3f65363f..000000000 |
| --- a/internal/provision/testdata/plan14-root-reads-static-sweep.tsv |
| +++ /dev/null |
| @@ -1,3 +0,0 @@ |
| -# Census overlay static-sweep, written by census-propose (#369). census-ruling-static-sweep, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-static-sweep.md. |
| -# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| -@coverage internal/edge c2ebb7b4bc5dcdbc3266911c78142ad2a1c6d5ebcfdd59b050d5b423cc3fdfc3 |