release, host: run the demo cleanup at the stage write, not in adoptHost
WithBeforeStage now takes the VersionPath about to be written and runs in the pass's stage write check, after the generation pin check and only for a path that holds nothing yet. adoptHost is unchanged, and RunAdopt keeps its line count (code-health). The host side maps the path back to its demo app.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+88
−52
internal/host/agent.go+7 −11internal/host/demoretention.go+42 −10internal/host/demoretention_linux_test.go+2 −2internal/host/demoretention_test.go+3 −3internal/release/adopt.go+27 −17internal/release/beforestage_test.go+7 −9
diff --git a/internal/host/agent.go b/internal/host/agent.go| index a4907e4d7..ad7269f54 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -5239,11 +5239,10 @@ func statusLoop(reg *registry.Registry, svc *activator.Service, buildStates func |
| MemTotalBytes: memTotal, |
| MemAvailBytes: memAvail, |
| Load1: readLoad1(), |
| - DiskFreeBytes: readDiskFreeBytes(), |
| - // A demo box's room on /data and /run and its kept demo builds |
| - // (demo disk bound, slice 1); nil on any other box. |
| - DemoDisk: readDemoDisk(), |
| - Release: version.Build, |
| + // DemoDisk: a demo box's room on /data and /run and its kept |
| + // demo builds (demo disk bound, slice 1); nil on any other box. |
| + DiskFreeBytes: readDiskFreeBytes(), DemoDisk: readDemoDisk(), |
| + Release: version.Build, |
| // carlos.prev's release, so the console can tell whether a |
| // failed apply here could restore a build below the |
| // stateless replicas floor (#511 R16). |
| @@ -6370,12 +6369,9 @@ func runAdoptUnderRootRelease(args []string, buildClaims ClaimFactory, ensureUni |
| // pending-output cleanup before a demo app's new output is staged, and |
| // retention after a pass that adopted a demo version, or on the hourly |
| // pass. Both only under this pass's lock, held until the pass returns. |
| - demoRet := productionDemoRetention(reg, generationPins, passLockErr == nil) |
| - passErr := demoRet.wrap(ctx, !*stageOnly, func(stage release.PassOption) error { |
| - return pr.around(ctx, func() error { |
| - return release.Pass(ctx, reg, store, pub, release.OptDir(), restart, append(passOpts, stage)...) |
| - }) |
| - }) |
| + passErr := pr.around(ctx, productionDemoRetention(reg, generationPins, passLockErr == nil).around(ctx, !*stageOnly, func(stage release.PassOption) error { |
| + return release.Pass(ctx, reg, store, pub, release.OptDir(), restart, append(passOpts, stage)...) |
| + })) |
| if passErr != nil { |
| log.Printf("adopt: %v", passErr) |
| } |
diff --git a/internal/host/demoretention.go b/internal/host/demoretention.go| index 522a5acfc..b30401919 100644 |
| --- a/internal/host/demoretention.go |
| +++ b/internal/host/demoretention.go |
| @@ -476,12 +476,21 @@ func (r *demoRetention) writeRecord(res demoRetentionResult) { |
| // staged: every staged output of that app that is neither adopted nor |
| // pinned (a failed job's leftover) goes first. If that cannot run in full, |
| // the stage is refused and nothing is deleted; the next attempt retries. |
| -func (r *demoRetention) beforeStage(ctx context.Context, rt registry.Route, ver string) error { |
| - if r == nil { |
| +// |
| +// path is the VersionPath about to be written. A path that is not a demo |
| +// app's (<ver>/<demo account>/tmp-demo-<name> of a demo route) is left to |
| +// stage. |
| +func (r *demoRetention) beforeStage(ctx context.Context, path string) error { |
| + if r == nil || !r.box.BoxMode() { |
| return nil |
| } |
| - k, ok := r.demoApp(rt) |
| - if !ok { |
| + rel, err := filepath.Rel(filepath.Join(r.optDir, "versions"), path) |
| + parts := strings.Split(rel, string(filepath.Separator)) |
| + if err != nil || len(parts) != 3 || !release.ValidVersion(parts[0]) { |
| + return nil |
| + } |
| + ver, k := parts[0], demoAppKey{parts[1], parts[2]} |
| + if release.VersionPath(r.optDir, k.acct, ver, k.app) != filepath.Clean(path) || !r.demoAppListed(k) { |
| return nil |
| } |
| if !r.locked { |
| @@ -503,6 +512,27 @@ func (r *demoRetention) beforeStage(ctx context.Context, rt registry.Route, ver |
| return nil |
| } |
| +// demoAppListed: a demo route runs k. A registry that does not list |
| +// counts as listed, so the cleanup's own read refuses the stage. |
| +func (r *demoRetention) demoAppListed(k demoAppKey) bool { |
| + if k.acct != r.box.Account || !strings.HasPrefix(k.app, demo.AppPrefix) { |
| + return false |
| + } |
| + if r.reg == nil { |
| + return true |
| + } |
| + routes, err := r.reg.List() |
| + if err != nil { |
| + return true |
| + } |
| + for _, rt := range routes { |
| + if got, ok := r.demoApp(rt); ok && got == k { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| // demoAdoptions is the demo routes' adopted versions by host, read before |
| // a pass so afterPass can tell whether it adopted one. nil when there is |
| // nothing to compare (no demo box, or the registry did not list). |
| @@ -545,12 +575,14 @@ func (r *demoRetention) afterPass(ctx context.Context, hourly bool, before map[s |
| r.run(ctx) |
| } |
| -// wrap is one release pass as RunAdopt runs it on any box: pass gets the |
| +// around is one release pass as RunAdopt runs it on any box: pass gets the |
| // pending-output cleanup as its WithBeforeStage option, and retention runs |
| // after it when it adopted a demo version, or on the hourly pass. |
| -func (r *demoRetention) wrap(ctx context.Context, hourly bool, pass func(stage release.PassOption) error) error { |
| - before := r.demoAdoptions() |
| - err := pass(release.WithBeforeStage(r.beforeStage)) |
| - r.afterPass(ctx, hourly, before) |
| - return err |
| +func (r *demoRetention) around(ctx context.Context, hourly bool, pass func(stage release.PassOption) error) func() error { |
| + return func() error { |
| + before := r.demoAdoptions() |
| + err := pass(release.WithBeforeStage(r.beforeStage)) |
| + r.afterPass(ctx, hourly, before) |
| + return err |
| + } |
| } |
diff --git a/internal/host/demoretention_linux_test.go b/internal/host/demoretention_linux_test.go| index a004514e6..1bbb40206 100644 |
| --- a/internal/host/demoretention_linux_test.go |
| +++ b/internal/host/demoretention_linux_test.go |
| @@ -135,9 +135,9 @@ func TestDemoRetentionConcurrentAdoptionIsSerialized(t *testing.T) { |
| defer unlock2() |
| second := b.retention(true) |
| ctx := context.Background() |
| - if err := second.wrap(ctx, false, func(stage release.PassOption) error { |
| + if err := second.around(ctx, false, func(stage release.PassOption) error { |
| return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, nil, release.BakeDir(b.data), stage) |
| - }); err != nil { |
| + })(); err != nil { |
| t.Error(err) |
| } |
| done <- b.outputs("shop", app) |
diff --git a/internal/host/demoretention_test.go b/internal/host/demoretention_test.go| index aa351528d..974bc0219 100644 |
| --- a/internal/host/demoretention_test.go |
| +++ b/internal/host/demoretention_test.go |
| @@ -24,7 +24,7 @@ import ( |
| // retentionBox is a demo box for the retention witnesses: a real registry, |
| // a real release store and version store, root's adopt pass as RunAdopt |
| -// runs it (release.Pass inside demoRetention.wrap), and root generation |
| +// runs it (release.Pass inside demoRetention.around), and root generation |
| // pins from whatever source the test hands it. |
| type retentionBox struct { |
| t *testing.T |
| @@ -97,9 +97,9 @@ func (b *retentionBox) ship(acct, app, ver string) { |
| func (b *retentionBox) pass(r *demoRetention, hourly bool) { |
| b.t.Helper() |
| ctx := context.Background() |
| - err := r.wrap(ctx, hourly, func(stage release.PassOption) error { |
| + err := r.around(ctx, hourly, func(stage release.PassOption) error { |
| return release.Pass(ctx, b.reg, b.store, b.pub, b.opt, b.restart, release.BakeDir(b.data), stage) |
| - }) |
| + })() |
| if err != nil { |
| b.t.Fatalf("pass: %v", err) |
| } |
diff --git a/internal/release/adopt.go b/internal/release/adopt.go| index ffecb90d7..de02ebb80 100644 |
| --- a/internal/release/adopt.go |
| +++ b/internal/release/adopt.go |
| @@ -117,7 +117,7 @@ type passConfig struct { |
| // the registry rows, the live links and the generations file still |
| // refers to. nil adds nothing. |
| pins PinSource |
| - beforeStage func(context.Context, registry.Route, string) error |
| + beforeStage func(context.Context, string) error |
| generationPins PinSource |
| // stage is what ensureStagedChecked needs to judge a staged copy it is |
| @@ -489,16 +489,34 @@ func WithPinSource(src PinSource) PassOption { |
| return func(c *passConfig) { c.pins = src } |
| } |
| -// WithBeforeStage runs fn before the pass stages a version of a route that |
| -// the version store does not hold yet (no file at its VersionPath). An error |
| -// leaves the version unstaged for this pass and the route where it was; the |
| -// next pass asks again. Root's adopt pass sets it for the demo disk bound's |
| -// pending-output cleanup (internal/host/demoretention.go): a demo app keeps |
| -// at most one output that was never adopted. |
| -func WithBeforeStage(fn func(ctx context.Context, rt registry.Route, ver string) error) PassOption { |
| +// WithBeforeStage runs fn just before the pass writes a new output into the |
| +// version store: path is the VersionPath it is about to stage, which holds |
| +// nothing yet (a restage of a copy already there does not call it). An |
| +// error fails the stage, so the version is not adopted this pass and the |
| +// route stays where it was; the next pass asks again. Root's adopt pass sets |
| +// it for the demo disk bound's pending-output cleanup |
| +// (internal/host/demoretention.go): a demo app keeps at most one output that |
| +// was never adopted. |
| +func WithBeforeStage(fn func(ctx context.Context, path string) error) PassOption { |
| return func(c *passConfig) { c.beforeStage = fn } |
| } |
| +// stageWriteCheck is the pass's check before a staged write to path: the |
| +// generation pins (generationWriteCheck), then WithBeforeStage's callback |
| +// for a path that holds nothing yet. |
| +func (cfg *passConfig) stageWriteCheck(ctx context.Context, optDir, path string) error { |
| + if err := cfg.generationWriteCheck(ctx, optDir, path); err != nil { |
| + return err |
| + } |
| + if cfg.beforeStage == nil { |
| + return nil |
| + } |
| + if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) { |
| + return err |
| + } |
| + return cfg.beforeStage(ctx, path) |
| +} |
| + |
| // stageBlocked reports whether err is the version store refusing a write to |
| // THIS uid, rather than any of the many other ways staging fails. errors.Is |
| // unwraps the *os.PathError that mkdir/openat return. |
| @@ -685,7 +703,7 @@ func PassFilter(ctx context.Context, reg *registry.Registry, s Store, pub ed2551 |
| // release, so a healthy pass never pays for it, and it reads fresh then, |
| // so a host this pass has already repointed counts. |
| cfg.stage = stageCheck{ |
| - beforeWrite: func(path string) error { return cfg.generationWriteCheck(ctx, optDir, path) }, |
| + beforeWrite: func(path string) error { return cfg.stageWriteCheck(ctx, optDir, path) }, |
| verified: map[string]string{}, |
| refs: func() (StagedPaths, error) { |
| rows, err := reg.List() |
| @@ -1629,14 +1647,6 @@ func adoptHost(ctx context.Context, work hostwrite.Work, reg *registry.Registry, |
| // for any build but the head itself: a non-head build has no pointer |
| // digest, and staging it unbound on a digested channel is the #253 |
| // replay (invariant 3). |
| - if cfg.beforeStage != nil && ValidVersion(ver) { |
| - if _, err := os.Lstat(VersionPath(optDir, RouteAccount(rt), ver, rt.App)); errors.Is(err, os.ErrNotExist) { |
| - if err := cfg.beforeStage(ctx, rt, ver); err != nil { |
| - fmt.Printf(" %s: %s: %v; not staged\n", rt.Host, ver, err) |
| - return |
| - } |
| - } |
| - } |
| var m *Manifest |
| // The row's runtime rides with the stage check (a copy: verified stays |
| // the pass's shared map), so this row stages only the kind it runs. |
diff --git a/internal/release/beforestage_test.go b/internal/release/beforestage_test.go| index 588e94997..0188c2c10 100644 |
| --- a/internal/release/beforestage_test.go |
| +++ b/internal/release/beforestage_test.go |
| @@ -5,15 +5,13 @@ import ( |
| "errors" |
| "os" |
| "testing" |
| - |
| - "github.com/carlosframework/platform/internal/registry" |
| ) |
| // The demo disk bound's pending-output cleanup (plan slice 1, step 4): the |
| -// box runs WithBeforeStage's callback before it downloads a version it has |
| -// not staged, and a callback that fails leaves the version unstaged and the |
| -// route where it was. A version already in the store never calls it: there |
| -// is no new output to make room for. |
| +// box runs WithBeforeStage's callback with the path before it writes a |
| +// version it has not staged, and a callback that fails leaves the version |
| +// unstaged and the route where it was. A version already in the store never |
| +// calls it: there is no new output to make room for. |
| func TestPassBeforeStageRunsOnlyForANewOutputAndRefusesTheStage(t *testing.T) { |
| s := NewDirStore(t.TempDir()) |
| ctx := context.Background() |
| @@ -25,8 +23,8 @@ func TestPassBeforeStageRunsOnlyForANewOutputAndRefusesTheStage(t *testing.T) { |
| var calls []string |
| refuse := errors.New("cleanup could not read a pin source") |
| hook := func(fail bool) PassOption { |
| - return WithBeforeStage(func(_ context.Context, rt registry.Route, ver string) error { |
| - calls = append(calls, rt.Host+" "+ver) |
| + return WithBeforeStage(func(_ context.Context, path string) error { |
| + calls = append(calls, path) |
| if fail { |
| return refuse |
| } |
| @@ -41,7 +39,7 @@ func TestPassBeforeStageRunsOnlyForANewOutputAndRefusesTheStage(t *testing.T) { |
| if err := Pass(ctx, reg, s, pub, opt, nil, hook(true)); err != nil { |
| t.Fatalf("Pass: %v", err) |
| } |
| - if len(calls) != 1 || calls[0] != "shop.example.com v1" { |
| + if len(calls) != 1 || calls[0] != VersionPath(opt, "bab", "v1", "shop") { |
| t.Fatalf("calls = %v, want one for v1", calls) |
| } |
| if _, err := os.Lstat(VersionPath(opt, "bab", "v1", "shop")); !errors.Is(err, os.ErrNotExist) { |