edge: startup sweep touches only entries it made; the first lock-aware start sweeps nothing (Astra round 2)
- P1: a release may be named .extract-x or .sweep-x-1 (valid versions), so debris is recognised by exact shape (.extract-<n>, .sweep-<name>-<n>) and release trees by <version>@<digest>; every non-current release tree is renamed before the sweep returns. - P1: an edge from before this change takes no lock, so a start that creates the lock file sweeps nothing; from the next start every edge on the cache is known to lock. - P2: a lock that cannot be held (open or shared) is retried, then an ALERT; static sites keep serving. - P2: names the edge did not make (an operator's directory) are never touched; pre-#253 bare-version trees are recognised and swept. Census rows regenerated for setAside and sweepStaticCache.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+231
−107
docs/superpowers/notes/census-proposal-static-sweep.md+11 −6internal/edge/static_sweep_test.go+98 −49internal/edge/static_sweep_unix.go+115 −45internal/provision/testdata/plan14-root-reads-chain.tsv+2 −2internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv+4 −4internal/provision/testdata/plan14-root-reads-static-sweep.tsv+1 −1
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-static-sweep.md b/docs/superpowers/notes/census-proposal-static-sweep.md| index f20cb80e9..4da5648d4 100644 |
| --- a/docs/superpowers/notes/census-proposal-static-sweep.md |
| +++ b/docs/superpowers/notes/census-proposal-static-sweep.md |
| @@ -6,21 +6,26 @@ 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: `c0d9880432c2cd59e42c6259494db4d7b6476a0fb486aaeedada10beb5879fec` → `c0d9880432c2cd59e42c6259494db4d7b6476a0fb486aaeedada10beb5879fec`. |
| +Seal: `d3ad8fdaa8b4cac055ecfa0e7c5a16a830052cb7e7c1b38ab1136488e5ff915f` → `d3ad8fdaa8b4cac055ecfa0e7c5a16a830052cb7e7c1b38ab1136488e5ff915f`. |
| 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_unix.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 `c0d98804…9fec`. |
| +`internal/edge/static_sweep_unix.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 `d3ad8fda…915f`. |
| | unit | reads | proposed disposition | |
| |---|---|---| |
| | `sweepStaticCache` | `OpenFile` of `<cacheDir>/.edge.lock` (created if absent), then `flock` on it | `legacy`/`accepted-legacy`, J4 footing (`root-input`), as the edge's other static units are | |
| -| `renameUnserved` | `ReadDir` of the cache, each account and each app; `Readlink` of each app's `current`; `Lstat` of each entry | the same | |
| +| `setAside` | `ReadDir` of the cache, each account and each app; `Readlink` of each app's `current`; `Lstat` of each entry | the same | |
| -Both run in the agent (`carlos-edge.service`, user carlos), not root. They run once, at startup, from `EnableStatic`, before any static handler exists, and only when this process takes the cache's lock exclusively. Every running edge holds that lock shared for its life, so the sweep never runs while another edge serves from the cache. |
| +Both run in the agent (`carlos-edge.service`, user carlos), not root. They run once, at startup, from `EnableStatic`, before any static handler exists. Every running edge holds the cache's lock shared for its life. A start sweeps only if it can take the lock exclusively. A start that creates the lock file sweeps nothing, because an edge from before this change may still be serving without taking the lock. |
| -In each app directory, the sweep keeps the tree the `current` link names and renames every other directory to `.sweep-<name>-<stamp>`. It never touches files or links. The renamed trees, and earlier `.sweep-` and `.extract-` leftovers, are removed in the background. Nothing removed is lost: every tree is a cache of a signed release, and `ensureExtracted` verifies the release again before extracting a fresh copy. |
| +The sweep touches only entries the edge made, recognised by name: |
| +- release trees (`<version>@<64-hex digest>`), and pre-#253 trees named by a bare version; |
| +- temp extractions (`.extract-<n>`); |
| +- an earlier sweep's leftovers (`.sweep-<name>-<n>`). |
| -The first design, a sweep at each version swap, was dropped after Astra's review: it raced handlers selecting trees and edge processes overlapping on a restart. |
| +Each app keeps the tree its `current` link names. Every other release tree is renamed to `.sweep-<name>-<stamp>` before the sweep returns, then removed in the background with the debris. Files, links and anything it doesn't recognise are left alone. Nothing removed is lost: every tree is a cache of a signed release, and `ensureExtracted` verifies the release again before extracting a fresh copy. |
| + |
| +Astra's reviews shaped this design: round 1 dropped a sweep at each version swap, and round 2 brought in the first-start rule and the strict name rules. |
diff --git a/internal/edge/static_sweep_test.go b/internal/edge/static_sweep_test.go| index 6b38c36c6..64065bd25 100644 |
| --- a/internal/edge/static_sweep_test.go |
| +++ b/internal/edge/static_sweep_test.go |
| @@ -6,12 +6,16 @@ import ( |
| "os" |
| "path/filepath" |
| "sort" |
| + "strings" |
| "syscall" |
| "testing" |
| ) |
| +// tree is a cache entry name as ensureExtracted makes it. |
| +func tree(version string) string { return version + "@" + strings.Repeat("ab", 32) } |
| + |
| // cacheTree makes <cache>/<acct>/<app>/<name>/index.html. |
| -func cacheTree(t *testing.T, cache, acct, app, name string) string { |
| +func cacheTree(t *testing.T, cache, acct, app, name string) { |
| t.Helper() |
| dir := filepath.Join(cache, acct, app, name) |
| if err := os.MkdirAll(dir, 0o755); err != nil { |
| @@ -20,7 +24,6 @@ func cacheTree(t *testing.T, cache, acct, app, name string) string { |
| 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) { |
| @@ -30,6 +33,15 @@ func pointAt(t *testing.T, cache, acct, app, name string) { |
| } |
| } |
| +// seenLock marks the cache as used by a lock-aware edge before, so this |
| +// start may sweep (first start: sweepStaticCache). |
| +func seenLock(t *testing.T, cache string) { |
| + t.Helper() |
| + if err := os.WriteFile(filepath.Join(cache, staticLockName), nil, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| func entries(t *testing.T, dir string) []string { |
| t.Helper() |
| es, err := os.ReadDir(dir) |
| @@ -44,37 +56,45 @@ func entries(t *testing.T, dir string) []string { |
| return names |
| } |
| -// 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 sweep(t *testing.T, cache string) { |
| + t.Helper() |
| + lock, done := sweepStaticCache(cache) |
| + if lock == nil { |
| + t.Fatal("no serving lock") |
| + } |
| + t.Cleanup(func() { lock.Close() }) |
| + <-done |
| +} |
| + |
| +// With no other edge holding the cache, every release tree but each app's |
| +// current one goes, and so does the edge's own debris (a temp extraction, |
| +// an earlier sweep's leftovers) and pre-#253 bare-version trees. Anything |
| +// the sweep does not recognise as its own (an operator's directory, a |
| +// file, a link) is left alone. |
| func TestStaticStartupSweepKeepsOnlyEachAppsCurrentTree(t *testing.T) { |
| cache := t.TempDir() |
| - for _, v := range []string{"v1@a", "v2@b", "v3@c"} { |
| - cacheTree(t, cache, "bab", "apt", v) |
| + seenLock(t, cache) |
| + for _, v := range []string{"v1", "v2", "v3"} { |
| + cacheTree(t, cache, "bab", "apt", tree(v)) |
| } |
| - pointAt(t, cache, "bab", "apt", "v3@c") |
| + pointAt(t, cache, "bab", "apt", tree("v3")) |
| 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 |
| + cacheTree(t, cache, "bab", "apt", ".sweep-"+tree("v0")+"-17") |
| + cacheTree(t, cache, "bab", "apt", "v0.5.0-1") // a bare version: the key before #253 |
| + cacheTree(t, cache, "bab", "apt", "backup 2026") |
| if err := os.WriteFile(filepath.Join(cache, "bab", "apt", "note.txt"), []byte("x"), 0o644); err != nil { |
| t.Fatal(err) |
| } |
| + cacheTree(t, cache, "bac", "docs", tree("v7")) |
| + pointAt(t, cache, "bac", "docs", tree("v7")) |
| + cacheTree(t, cache, "bac", "nolink", tree("v1")) // no current link: no tree is kept |
| - lock, done := sweepStaticCache(cache) |
| - if lock == nil { |
| - t.Fatal("no serving lock") |
| - } |
| - defer lock.Close() |
| - <-done |
| + sweep(t, cache) |
| - if got, want := entries(t, filepath.Join(cache, "bab", "apt")), []string{"current", "note.txt", "v3@c"}; !equal(got, want) { |
| + if got, want := entries(t, filepath.Join(cache, "bab", "apt")), []string{"backup 2026", "current", "note.txt", tree("v3")}; strings.Join(got, "|") != strings.Join(want, "|") { |
| t.Errorf("bab/apt = %v, want %v", got, want) |
| } |
| - if got, want := entries(t, filepath.Join(cache, "bac", "docs")), []string{"current", "v7@d"}; !equal(got, want) { |
| + if got, want := entries(t, filepath.Join(cache, "bac", "docs")), []string{"current", tree("v7")}; strings.Join(got, "|") != strings.Join(want, "|") { |
| t.Errorf("bac/docs = %v, want %v", got, want) |
| } |
| if got := entries(t, filepath.Join(cache, "bac", "nolink")); len(got) != 0 { |
| @@ -82,14 +102,62 @@ func TestStaticStartupSweepKeepsOnlyEachAppsCurrentTree(t *testing.T) { |
| } |
| } |
| +// A release may be named like the edge's debris (.extract-x, .sweep-x-1 |
| +// are valid version names). Its tree is a release tree, recognised by its |
| +// @digest, and is never taken for debris: kept when current, renamed out |
| +// of reach before it is removed otherwise. |
| +func TestStaticStartupSweepNeverTakesAReleaseForDebris(t *testing.T) { |
| + cache := t.TempDir() |
| + seenLock(t, cache) |
| + cacheTree(t, cache, "bab", "a", tree(".sweep-x-1")) |
| + pointAt(t, cache, "bab", "a", tree(".sweep-x-1")) |
| + cacheTree(t, cache, "bab", "b", tree(".extract-9")) |
| + cacheTree(t, cache, "bab", "b", tree("v2")) |
| + pointAt(t, cache, "bab", "b", tree("v2")) |
| + |
| + lock, done := sweepStaticCache(cache) |
| + defer lock.Close() |
| + // Before the background removal ends, the non-current release is |
| + // already gone from its own name: nothing can select it. |
| + if _, err := os.Stat(filepath.Join(cache, "bab", "b", tree(".extract-9"))); !os.IsNotExist(err) { |
| + t.Errorf("a non-current release tree was still at its own name after the sweep returned: %v", err) |
| + } |
| + <-done |
| + if got := entries(t, filepath.Join(cache, "bab", "a")); strings.Join(got, "|") != tree(".sweep-x-1")+"|current" { |
| + t.Errorf("bab/a = %v, want the current release kept", got) |
| + } |
| + if got := entries(t, filepath.Join(cache, "bab", "b")); strings.Join(got, "|") != "current|"+tree("v2") { |
| + t.Errorf("bab/b = %v", got) |
| + } |
| +} |
| + |
| +// The first lock-aware edge on a cache creates the lock file and sweeps |
| +// nothing: an edge from before this change may still be serving from it |
| +// without taking the lock. From the next start on, every edge using the |
| +// cache is known to take it. |
| +func TestStaticStartupSweepFirstStartOnlyCreatesTheLock(t *testing.T) { |
| + cache := t.TempDir() |
| + cacheTree(t, cache, "bab", "apt", tree("v1")) |
| + cacheTree(t, cache, "bab", "apt", tree("v2")) |
| + pointAt(t, cache, "bab", "apt", tree("v2")) |
| + sweep(t, cache) |
| + if got := entries(t, filepath.Join(cache, "bab", "apt")); len(got) != 3 { |
| + t.Errorf("bab/apt = %v, want nothing swept on the first lock-aware start", got) |
| + } |
| + if _, err := os.Stat(filepath.Join(cache, staticLockName)); err != nil { |
| + t.Fatalf("no lock file: %v", err) |
| + } |
| +} |
| + |
| // 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) |
| + cacheTree(t, cache, "bab", "apt", tree("v1")) |
| + cacheTree(t, cache, "bab", "apt", tree("v2")) |
| + pointAt(t, cache, "bab", "apt", tree("v2")) |
| + seenLock(t, cache) |
| + other, err := os.OpenFile(filepath.Join(cache, staticLockName), os.O_RDWR, 0o600) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -97,14 +165,8 @@ func TestStaticStartupSweepSkipsWhileAnotherEdgeServes(t *testing.T) { |
| if err := syscall.Flock(int(other.Fd()), syscall.LOCK_SH); err != nil { |
| t.Fatal(err) |
| } |
| - |
| - lock, done := sweepStaticCache(cache) |
| - if lock == nil { |
| - t.Fatal("no serving lock") |
| - } |
| - defer lock.Close() |
| - <-done |
| - if got, want := entries(t, filepath.Join(cache, "bab", "apt")), []string{"current", "v1@a", "v2@b"}; !equal(got, want) { |
| + sweep(t, cache) |
| + if got := entries(t, filepath.Join(cache, "bab", "apt")); len(got) != 3 { |
| t.Errorf("bab/apt = %v, want everything kept while another edge serves", got) |
| } |
| } |
| @@ -113,9 +175,8 @@ func TestStaticStartupSweepSkipsWhileAnotherEdgeServes(t *testing.T) { |
| // starting beside it cannot sweep either. |
| func TestStaticStartupSweepLeavesASharedLock(t *testing.T) { |
| cache := t.TempDir() |
| - lock, done := sweepStaticCache(cache) |
| - defer lock.Close() |
| - <-done |
| + seenLock(t, cache) |
| + sweep(t, cache) |
| probe, err := os.OpenFile(filepath.Join(cache, staticLockName), os.O_RDWR, 0o600) |
| if err != nil { |
| t.Fatal(err) |
| @@ -128,15 +189,3 @@ func TestStaticStartupSweepLeavesASharedLock(t *testing.T) { |
| t.Fatalf("a second edge could not share the lock: %v", err) |
| } |
| } |
| - |
| -func equal(a, b []string) bool { |
| - if len(a) != len(b) { |
| - return false |
| - } |
| - 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| index dc8afe8ab..6ea4d1ccd 100644 |
| --- a/internal/edge/static_sweep_unix.go |
| +++ b/internal/edge/static_sweep_unix.go |
| @@ -3,59 +3,129 @@ |
| package edge |
| import ( |
| + "errors" |
| "fmt" |
| "log" |
| "os" |
| "path/filepath" |
| + "regexp" |
| "strings" |
| "syscall" |
| "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| // 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. |
| +// handlers that pick trees (Astra review of static-sweep), so the sweep |
| +// runs once, at startup, before any handler exists, and only when no other |
| +// edge process can be serving from this cache: |
| +// - Every running edge holds a shared flock on staticLockName for its |
| +// life. A starting edge sweeps only if it can take it exclusively. |
| +// - An edge from before this change takes no lock, so the first edge to |
| +// create the lock file sweeps nothing: from its next start on, every |
| +// edge using the cache is known to take the lock. |
| +// |
| +// Only entries the edge itself made are touched, recognised by name: |
| +// release trees (<version>@<digest>, siteDirName), pre-#253 trees named by |
| +// a bare version, temp extractions (.extract-<n>, os.MkdirTemp's) and an |
| +// earlier sweep's leftovers (.sweep-<name>-<n>). Each app keeps the tree |
| +// its current link names. Every other release tree is renamed to a .sweep- |
| +// name before the sweep returns, so no handler built after it can select |
| +// it (ensureExtracted then extracts a fresh, verified copy if a pointer |
| +// names that version), and is removed in the background with the debris. |
| const staticLockName = ".edge.lock" |
| -// sweepStaticCache sweeps cacheDir if this is the only edge using it, and |
| +var ( |
| + siteTreeName = regexp.MustCompile(`^(.+)@[0-9a-f]{64}$`) |
| + extractTmpName = regexp.MustCompile(`^\.extract-[0-9]+$`) |
| + sweptName = regexp.MustCompile(`^\.sweep-.+-[0-9]+$`) |
| +) |
| + |
| +// staticEntry is how the sweep reads one name in an app directory. |
| +type staticEntry int |
| + |
| +const ( |
| + entryUnknown staticEntry = iota // not the edge's: never touched |
| + entryTree // a release tree, set aside unless current |
| + entryDebris // the edge's own leftovers: removed |
| +) |
| + |
| +func classifyStaticEntry(name string) staticEntry { |
| + if m := siteTreeName.FindStringSubmatch(name); m != nil && release.ValidVersion(m[1]) { |
| + return entryTree |
| + } |
| + if extractTmpName.MatchString(name) || sweptName.MatchString(name) { |
| + return entryDebris |
| + } |
| + if !strings.HasPrefix(name, ".") && release.ValidVersion(name) { |
| + return entryTree // a pre-#253 tree, keyed by the bare version |
| + } |
| + return entryUnknown |
| +} |
| + |
| +// sweepStaticCache sweeps cacheDir if no other edge can be 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. |
| +// (nil when no lock could be held), and a channel closed once the |
| +// background removal is done. A lock it cannot hold is an ALERT, not a |
| +// refusal to serve: static sites stay up, and the only exposure is a second |
| +// edge starting beside this one while it serves. |
| 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) |
| + fail := func(f *os.File, err error) (*os.File, <-chan struct{}) { |
| + log.Printf("ALERT: edge: static cache %s: no lock held (%v): not swept, and a second edge starting beside this one could sweep trees it serves", cacheDir, err) |
| + if f != nil { |
| + f.Close() |
| + } |
| close(done) |
| return nil, done |
| } |
| - lock, err := os.OpenFile(filepath.Join(cacheDir, staticLockName), os.O_RDWR|os.O_CREATE, 0o600) |
| + if err := os.MkdirAll(cacheDir, 0o755); err != nil { |
| + return fail(nil, err) |
| + } |
| + path := filepath.Join(cacheDir, staticLockName) |
| + first := false |
| + lock, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600) |
| + if err == nil { |
| + first = true |
| + } else if errors.Is(err, os.ErrExist) { |
| + lock, err = os.OpenFile(path, os.O_RDWR, 0o600) |
| + } |
| if err != nil { |
| - log.Printf("edge: static cache %s: lock: %v; not swept", cacheDir, err) |
| - close(done) |
| - return nil, done |
| + return fail(nil, err) |
| } |
| fd := int(lock.Fd()) |
| + shared := func() error { |
| + var err error |
| + for range 5 { |
| + if err = syscall.Flock(fd, syscall.LOCK_SH); err == nil { |
| + return nil |
| + } |
| + time.Sleep(200 * time.Millisecond) |
| + } |
| + return err |
| + } |
| + if first { |
| + log.Printf("edge: static cache %s: first start with the lock; sweeping from the next start", cacheDir) |
| + if err := shared(); err != nil { |
| + return fail(lock, err) |
| + } |
| + close(done) |
| + return lock, done |
| + } |
| 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) |
| + if err := shared(); err != nil { |
| + return fail(lock, 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) |
| + doomed := setAside(cacheDir) |
| + if err := shared(); err != nil { |
| + return fail(lock, err) |
| } |
| go func() { |
| defer close(done) |
| @@ -68,17 +138,16 @@ func sweepStaticCache(cacheDir string) (*os.File, <-chan struct{}) { |
| removed++ |
| } |
| if removed > 0 { |
| - log.Printf("edge: static cache %s: swept %d tree(s) no app's current link names", cacheDir, removed) |
| + log.Printf("edge: static cache %s: swept %d entr(ies) 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 { |
| +// setAside renames, in every <acct>/<app> directory, each release tree the |
| +// current link does not name to a .sweep- name, and returns those paths and |
| +// the edge's debris for removal. It touches nothing it does not recognise. |
| +func setAside(cacheDir string) []string { |
| var doomed []string |
| accts, err := os.ReadDir(cacheDir) |
| if err != nil { |
| @@ -99,30 +168,31 @@ func renameUnserved(cacheDir string) []string { |
| } |
| appDir := filepath.Join(cacheDir, a.Name(), ap.Name()) |
| cur, _ := os.Readlink(filepath.Join(appDir, "current")) |
| - trees, err := os.ReadDir(appDir) |
| + names, err := os.ReadDir(appDir) |
| if err != nil { |
| continue |
| } |
| - for _, tr := range trees { |
| - name := tr.Name() |
| + for _, n := range names { |
| + name := n.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) |
| + if name == cur { |
| continue |
| } |
| - if strings.HasPrefix(name, ".") { |
| + fi, err := os.Lstat(path) |
| + if err != nil || !fi.IsDir() { |
| 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 |
| + switch classifyStaticEntry(name) { |
| + case entryDebris: |
| + doomed = append(doomed, path) |
| + case entryTree: |
| + 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) |
| } |
| - doomed = append(doomed, to) |
| } |
| } |
| } |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 9d8259785..4002eada8 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -21,5 +21,5 @@ 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 c0d9880432c2cd59e42c6259494db4d7b6476a0fb486aaeedada10beb5879fec |
| -plan14-root-reads-static-sweep.tsv c0d9880432c2cd59e42c6259494db4d7b6476a0fb486aaeedada10beb5879fec |
| +plan14-root-reads-static-sweep-new.tsv d3ad8fdaa8b4cac055ecfa0e7c5a16a830052cb7e7c1b38ab1136488e5ff915f |
| +plan14-root-reads-static-sweep.tsv d3ad8fdaa8b4cac055ecfa0e7c5a16a830052cb7e7c1b38ab1136488e5ff915f |
diff --git a/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv b/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv| index ec0cd5085..e3b0b59ca 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-static-sweep-new.tsv |
| @@ -1,5 +1,5 @@ |
| # Census overlay static-sweep-new, hand-written: the two new units census-propose leaves to a decision (the static cache startup sweep). census-ruling-static-sweep, asked of the census owner with the approval card (docs/superpowers/notes/census-proposal-static-sweep.md). |
| -@legacy 435245bcb853d5f5c462f90ef60551650427980c33b177c1e1b9aa42bab50ddb internal/edge/static_sweep_unix.go renameUnserved 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; once at startup, before any static handler exists and only under an exclusive flock no other edge holds, reads its own static cache (<cacheDir>/<acct>/<app>: entries, each current link, each entry lstat) and renames every directory but the current link target to a .sweep- name for removal, with earlier .sweep- and .extract- leftovers; never touches files or links; every tree is a cache of a signed release that ensureExtracted re-verifies before extracting it again; no root decision file:Lstat@762,file:ReadDir@282,file:ReadDir@599,file:ReadDir@84,file:Readlink@536 9782b5863da22fd871d880f669c4edd34a2e41a2e1465d9b1e85f14e66aa7f54 |
| -@legacy 45230ff8fe9ff826e8fd8dc359b0f26fe6634c4245ba85f89bf9e9c6ca1a4094 internal/edge/static_sweep_unix.go sweepStaticCache 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; opens or creates <cacheDir>/.edge.lock and flocks it (exclusive non-blocking to sweep, then shared for the process life); the lock decides only whether this start sweeps; no root decision file:OpenFile@270 0ac57f7880540bafbd015364d4ff5db8bb9a739afd1d5d2b600ffec6feb509f1 |
| -internal/edge/static_sweep_unix.go renameUnserved 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; once at startup, before any static handler exists and only under an exclusive flock no other edge holds, reads its own static cache (<cacheDir>/<acct>/<app>: entries, each current link, each entry lstat) and renames every directory but the current link target to a .sweep- name for removal, with earlier .sweep- and .extract- leftovers; never touches files or links; every tree is a cache of a signed release that ensureExtracted re-verifies before extracting it again; no root decision file:Lstat@762,file:ReadDir@282,file:ReadDir@599,file:ReadDir@84,file:Readlink@536 9782b5863da22fd871d880f669c4edd34a2e41a2e1465d9b1e85f14e66aa7f54 |
| -internal/edge/static_sweep_unix.go sweepStaticCache 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; opens or creates <cacheDir>/.edge.lock and flocks it (exclusive non-blocking to sweep, then shared for the process life); the lock decides only whether this start sweeps; no root decision file:OpenFile@270 0ac57f7880540bafbd015364d4ff5db8bb9a739afd1d5d2b600ffec6feb509f1 |
| +@legacy 14f932cd814fdd6e6120f38e59b7dc10897ad0bfc122528fd00c374c7ac81803 internal/edge/static_sweep_unix.go setAside 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; once at startup, before any static handler exists, only under an exclusive flock (sweepStaticCache), reads its own static cache (<cacheDir>/<acct>/<app>: entries, each current link, each entry lstat) and renames each release tree (<version>@<digest> or a pre-#253 bare version) the current link does not name to a .sweep- name for removal, with its own debris (.extract-<n>, .sweep-<name>-<n>); never touches files, links, or names it does not recognise; every tree is a cache of a signed release that ensureExtracted re-verifies before extracting it again; no root decision file:Lstat@795,file:ReadDir@276,file:ReadDir@593,file:ReadDir@78,file:Readlink@530 29fef16d6b05b3dc73148f45de083593704a381ba8e23b222148287edc8eebec |
| +@legacy bb227e2c3bf92531e7a1e6a04d17601eb6525a3ebe9f49cb55f6323066e07ebd internal/edge/static_sweep_unix.go sweepStaticCache 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; opens or creates <cacheDir>/.edge.lock and flocks it: a start that creates it sweeps nothing (an older edge may not lock), otherwise exclusive non-blocking to sweep, then shared for the process life; the lock decides only whether this start sweeps; no root decision file:OpenFile@556,file:OpenFile@701 0c2857704f81442d833c121e5f7f9b726324c52557d66a50a87d3b4c7c632fdf |
| +internal/edge/static_sweep_unix.go setAside 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; once at startup, before any static handler exists, only under an exclusive flock (sweepStaticCache), reads its own static cache (<cacheDir>/<acct>/<app>: entries, each current link, each entry lstat) and renames each release tree (<version>@<digest> or a pre-#253 bare version) the current link does not name to a .sweep- name for removal, with its own debris (.extract-<n>, .sweep-<name>-<n>); never touches files, links, or names it does not recognise; every tree is a cache of a signed release that ensureExtracted re-verifies before extracting it again; no root decision file:Lstat@795,file:ReadDir@276,file:ReadDir@593,file:ReadDir@78,file:Readlink@530 29fef16d6b05b3dc73148f45de083593704a381ba8e23b222148287edc8eebec |
| +internal/edge/static_sweep_unix.go sweepStaticCache 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; opens or creates <cacheDir>/.edge.lock and flocks it: a start that creates it sweeps nothing (an older edge may not lock), otherwise exclusive non-blocking to sweep, then shared for the process life; the lock decides only whether this start sweeps; no root decision file:OpenFile@556,file:OpenFile@701 0c2857704f81442d833c121e5f7f9b726324c52557d66a50a87d3b4c7c632fdf |
diff --git a/internal/provision/testdata/plan14-root-reads-static-sweep.tsv b/internal/provision/testdata/plan14-root-reads-static-sweep.tsv| index cc4674016..361ccb6f9 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-static-sweep.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-static-sweep.tsv |
| @@ -1,3 +1,3 @@ |
| # 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 9d8ff80aa1b71953d2452257a20590c10a19bcf874e0d43730520b969adde57f |
| +@coverage internal/edge 1af8976107f77e11dda358180292bc21bdd6898a2f090c5a25ffad8768775818 |