carlos / platform Public

release: WithBeforeStage, a hook before a new output is staged

Root's adopt pass runs it before staging a version the store does not
hold yet. An error leaves the version unstaged and the route where it
was; the next pass asks again. For the demo disk bound's pending-output
cleanup (plan slice 1, step 4).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 2e5e222c3dda3227be1af649429157eac4152067 parent 4799995
2 files changed, +91 −0
  • internal/release/adopt.go +19 −0
  • internal/release/beforestage_test.go +72 −0
diff --git a/internal/release/adopt.go b/internal/release/adopt.go
index 19cb0b806..ffecb90d7 100644
--- a/internal/release/adopt.go
+++ b/internal/release/adopt.go
@@ -117,6 +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
generationPins PinSource
// stage is what ensureStagedChecked needs to judge a staged copy it is
@@ -488,6 +489,16 @@ 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 {
+ return func(c *passConfig) { c.beforeStage = fn }
+}
+
// 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.
@@ -1618,6 +1629,14 @@ 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
new file mode 100644
index 000000000..588e94997
--- /dev/null
+++ b/internal/release/beforestage_test.go
@@ -0,0 +1,72 @@
+package release
+
+import (
+ "context"
+ "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.
+func TestPassBeforeStageRunsOnlyForANewOutputAndRefusesTheStage(t *testing.T) {
+ s := NewDirStore(t.TempDir())
+ ctx := context.Background()
+ priv, pub := testKeys(t)
+ opt := t.TempDir()
+ reg := testRegistry(t)
+ addRoute(t, reg, "shop.example.com", "shop", "canary/x", true)
+
+ 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)
+ if fail {
+ return refuse
+ }
+ return nil
+ })
+ }
+
+ shipRelease(t, s, "bab", "shop", "v1", adoptPlatform, []byte("one"))
+ if err := Promote(ctx, s, priv, "bab", "shop", "canary/x", "v1"); err != nil {
+ t.Fatal(err)
+ }
+ 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" {
+ t.Fatalf("calls = %v, want one for v1", calls)
+ }
+ if _, err := os.Lstat(VersionPath(opt, "bab", "v1", "shop")); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("v1 staged although its cleanup failed: %v", err)
+ }
+ if rt, _ := reg.Get("shop.example.com"); rt == nil || rt.Version != "" {
+ t.Fatalf("route moved although nothing was staged: %+v", rt)
+ }
+
+ // The retry runs the cleanup first again, and stages once it passes.
+ if err := Pass(ctx, reg, s, pub, opt, nil, hook(false)); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ if len(calls) != 2 {
+ t.Fatalf("calls = %v, want the retry to run the cleanup", calls)
+ }
+ if rt, _ := reg.Get("shop.example.com"); rt == nil || rt.Version != "v1" {
+ t.Fatalf("route = %+v, want v1 once the cleanup passed", rt)
+ }
+
+ // Already staged and live: nothing new to stage, no call.
+ if err := Pass(ctx, reg, s, pub, opt, nil, hook(true)); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ if len(calls) != 2 {
+ t.Fatalf("calls = %v, want none for a version already staged", calls)
+ }
+}