carlos / platform Public

provision: reconcile mints rootfs rows nested and seedless; never converges their runtime or hibernation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d35af56bb1284c0471aa09ea07aba2b162666a41 parent 5773b78
2 files changed, +431 −1
  • internal/provision/reconcile.go +54 −1
  • internal/provision/reconcile_rootfs_test.go +377 −0
diff --git a/internal/provision/reconcile.go b/internal/provision/reconcile.go
index 050ee234d..5a3bfde87 100644
--- a/internal/provision/reconcile.go
+++ b/internal/provision/reconcile.go
@@ -308,6 +308,13 @@ func productionClear(ctx context.Context, s release.Store, pub ed25519.PublicKey
// elsewhere), while one wrongly promoted to everywhere is a box proxying to an
// upstream it cannot reach.
func everywhereClass(rec Record) bool {
+ // A record with a runtime is home-boxed whatever else it says: its
+ // process runs from an image on the box that holds its lease (a rootfs
+ // instance), and an unknown runtime runs nowhere. First and absolute,
+ // ahead of the replica check, and the same test as everywhereRoute's.
+ if rec.Runtime != "" {
+ return false
+ }
// A replica is the residence rule made structural (#162): it belongs to
// the box its placement names and nowhere else, and this widening is the
// mint gate for every non-fleet box. ValidateShape refuses a replica
@@ -374,6 +381,11 @@ func everywhereClass(rec Record) bool {
// documents as human-gated, which is worse. The conservative gate stands; the
// gap is tracked in the bookkeeping issue, not papered over here.
func everywhereRoute(rt registry.Route) bool {
+ // everywhereClass's runtime check, read off the row: a row with any
+ // runtime is home-boxed.
+ if rt.Runtime != "" {
+ return false
+ }
// The replica mark is everywhereClass's role check, read off the row
// (the record whose class it was may be exactly what is gone) — the
// same FIRST-and-absolute position, for the same reason.
@@ -501,6 +513,7 @@ func RouteFor(rec Record, dataDir, seed string, nested bool, policy *UpdatePolic
// instead (mintHeld, ReconcileConfig.PairOf).
Incarnation: rec.Incarnation,
ServingSeq: rec.MoveSeq,
+ Runtime: rec.Runtime,
}
if rec.DerivesPaths() {
rt.Socket = DefaultSocket(rec.Host)
@@ -513,6 +526,16 @@ func RouteFor(rec Record, dataDir, seed string, nested bool, policy *UpdatePolic
rt.DBPath = NestedDBPath(dataDir, rec.Host)
}
}
+ // A rootfs row is never flat, whatever nested says (anti-goal §2): on a
+ // box whose layout is not nested it is minted anyway, and Plan 2's start
+ // refusal says why it does not run. It carries no seed (D5): the
+ // manifest's seed is a binary's argv, and ValidateShape refuses a seed
+ // on a rootfs record.
+ if rt.Rootfs() {
+ rt.Socket = NestedSocket(rec.Host)
+ rt.DBPath = NestedDBPath(dataDir, rec.Host)
+ rt.Seed, rt.SeedSource = "", ""
+ }
rt.Addr, rt.Addrs, rt.Health = upstreamCols(rec)
return rt
}
@@ -689,6 +712,10 @@ func shapeMismatch(existing, want registry.Route) string {
return fmt.Sprintf("account %q/%q", existing.Account, want.Account)
case existing.Kind != want.Kind:
return fmt.Sprintf("kind %q/%q", existing.Kind, want.Kind)
+ // A hand-added row is never adopted across runtimes: at the same paths
+ // a binary row and a rootfs record are different processes.
+ case existing.Runtime != want.Runtime:
+ return fmt.Sprintf("runtime %q/%q", existing.Runtime, want.Runtime)
case existing.Addr != want.Addr:
return fmt.Sprintf("addr %q/%q", existing.Addr, want.Addr)
case existing.Addrs != want.Addrs:
@@ -1399,6 +1426,14 @@ func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg
}
seed, seedIsKnown = "", true
}
+ // Nor does a record or row with a runtime (D5): the manifest's seed
+ // is a binary's argv, and RouteFor mints a rootfs row without it, so
+ // the owned arm's seed converge must not write it on a later pass.
+ // Keyed on the row too, so a record that lost its runtime does not
+ // seed the rootfs row it still describes.
+ if rec.Runtime != "" || (ok && existing.Runtime != "") {
+ seed, seedIsKnown = "", true
+ }
// Existing rows across lifetimes (self-serve moves plan 1 Task 7),
// rule (0): a predecessor's row held by an op this record does not
// carry is retired by that op first, whatever the record asks; the
@@ -1556,7 +1591,25 @@ func Reconcile(ctx context.Context, s release.Store, reg *registry.Registry, cfg
// under the instance tree (design §8.2, downgrade rules). A row
// with a layout journal is mid-move whatever its paths say, and
// recovery needs it managed just the same.
- if rec.Hibernate != nil && existing.Hibernate != *rec.Hibernate && !*rec.Hibernate &&
+ //
+ // A row's runtime, and a rootfs row's hibernation, are fixed
+ // at mint (spec §5.0): no record converges either, and the
+ // skip keys on any runtime on either side, as
+ // registry.SetHibernate's refusal does (an unknown runtime is
+ // fixed and inert). An older console's rewrite that drops
+ // "runtime" therefore never changes what the box runs. The
+ // disagreement is logged and leaves applied alone: the box's
+ // answer is final, and a record counted unapplied for it would
+ // keep the app's report incomplete for good.
+ if existing.Runtime != "" || rec.Runtime != "" {
+ if existing.Runtime != rec.Runtime {
+ log.Printf("provision: %s: record runtime %q, row %q: the runtime is fixed at mint; leaving the row's runtime and hibernation alone",
+ rec.Host, rec.Runtime, existing.Runtime)
+ } else if rec.Hibernate != nil && existing.Hibernate != *rec.Hibernate {
+ log.Printf("provision: %s: record hibernate %v, row %v: a rootfs row's hibernation is fixed at mint; leaving it alone",
+ rec.Host, *rec.Hibernate, existing.Hibernate)
+ }
+ } else if rec.Hibernate != nil && existing.Hibernate != *rec.Hibernate && !*rec.Hibernate &&
(NestedPaths(cfg.DataDir, existing) || LayoutJournalExists(cfg.DataDir, rec.Host)) {
log.Printf("provision: %s: record asks to clear hibernate but the row's paths are nested; revert the layout first", rec.Host)
applied = false
diff --git a/internal/provision/reconcile_rootfs_test.go b/internal/provision/reconcile_rootfs_test.go
new file mode 100644
index 000000000..a4a8f0402
--- /dev/null
+++ b/internal/provision/reconcile_rootfs_test.go
@@ -0,0 +1,377 @@
+package provision
+
+import (
+ "bytes"
+ "encoding/json"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// Rootfs apps Plan 3 Task 4: reconcile materializes a record's runtime. A
+// rootfs record mints a row with its runtime and hibernation, at the nested
+// paths and with no seed, and no later pass converges either column.
+
+const rootfsHost = "bab.shop.example.com"
+
+// rootfsSetup writes the shop app's manifest, with a seed, so a test can
+// show a rootfs row never takes it (D5).
+func rootfsSetup(t *testing.T) (*release.DirStore, string) {
+ t.Helper()
+ s := dirStore(t)
+ sqid := writeAccountManifest(t, s, acctID, "Shop")
+ if err := PutManifest(ctx, s, sqid, "shop", Manifest{
+ Schema: 1, App: "shop", Domain: "shop.example.com", Seed: []string{"seed"},
+ }); err != nil {
+ t.Fatal(err)
+ }
+ return s, sqid
+}
+
+func rootfsRecord(sqid, host string, hibernate bool) Record {
+ h := hibernate
+ return Record{Schema: 1, Host: host, Slug: strings.SplitN(host, ".", 2)[0], App: "shop",
+ Account: sqid, Region: "ie", Channel: "stable", Runtime: RuntimeRootfsV1,
+ Hibernate: &h, CreatedAt: time.Now()}
+}
+
+func rootfsCfg(rep *ReconcileReport) ReconcileConfig {
+ return ReconcileConfig{Region: "ie", DataDir: "/data", Report: rep}
+}
+
+// rewriteRecord rewrites a stored record's body with a raw Put, around
+// WriteRecord's guard: what an older console's re-marshal, or anything else
+// with bucket write access, can leave in the bucket.
+func rewriteRecord(t *testing.T, s release.Store, key string, mut func(map[string]any)) {
+ t.Helper()
+ body, _, err := s.Get(ctx, key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var m map[string]any
+ if err := json.Unmarshal(body, &m); err != nil {
+ t.Fatal(err)
+ }
+ mut(m)
+ putReplace(t, s, key, mustJSON(t, m))
+}
+
+func reconcileOnce(t *testing.T, s release.Store, reg *registry.Registry, cfg ReconcileConfig) {
+ t.Helper()
+ if err := Reconcile(ctx, s, reg, cfg); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestReconcileMintsRootfsNested(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, false)); err != nil {
+ t.Fatal(err)
+ }
+ reg := regWith(t)
+ // cfg.Layout is nil: a flat-mode box. A rootfs row is never flat.
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ want := registry.Route{
+ Host: rootfsHost, Slug: "bab", Kind: "instance",
+ Socket: NestedSocket(rootfsHost), DBPath: NestedDBPath("/data", rootfsHost),
+ App: "shop", Account: sqid, Region: "ie", Channel: "stable",
+ Provisioned: true, Status: "ready", Runtime: registry.RuntimeRootfsV1,
+ }
+ got := *mustGet(t, reg, rootfsHost)
+ got.CreatedAt = 0
+ if got != want {
+ t.Fatalf("minted route = %+v\nwant %+v", got, want)
+ }
+ // The app's instances.json seed is ignored at mint and on every later
+ // pass: the owned arm never converges it onto a rootfs row.
+ for pass := 2; pass <= 3; pass++ {
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ got := *mustGet(t, reg, rootfsHost)
+ if got.Seed != "" || got.SeedSource != "" {
+ t.Fatalf("pass %d: rootfs row took a seed: seed %q source %q", pass, got.Seed, got.SeedSource)
+ }
+ }
+}
+
+func TestReconcileMintsHibernatingRootfs(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, true)); err != nil {
+ t.Fatal(err)
+ }
+ reg := regWith(t)
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ got := mustGet(t, reg, rootfsHost)
+ if !got.Hibernate || got.Runtime != registry.RuntimeRootfsV1 {
+ t.Fatalf("row hibernate %v runtime %q, want true %q", got.Hibernate, got.Runtime, registry.RuntimeRootfsV1)
+ }
+ if got.Socket != NestedSocket(rootfsHost) || got.DBPath != NestedDBPath("/data", rootfsHost) {
+ t.Fatalf("row paths socket %q db %q, want nested", got.Socket, got.DBPath)
+ }
+ if got.Seed != "" || got.SeedSource != "" {
+ t.Fatalf("rootfs row took a seed: %q %q", got.Seed, got.SeedSource)
+ }
+}
+
+// A converge would go through registry.SetHibernate, which refuses a row
+// with a runtime (ErrRuntimeFixed): the call's failure is logged as
+// "converge hibernate on" and leaves the record unapplied. So "never
+// called" is witnessed by both: no such line, and the app's report
+// complete on every pass.
+func TestReconcileNeverConvergesRootfsHibernation(t *testing.T) {
+ cases := []struct {
+ name string
+ minted bool // the record's hibernation at mint
+ flipped bool // what the raw rewrite stores
+ logWord string
+ }{
+ {"record asks to hibernate", false, true, "record hibernate true, row false"},
+ // The reverse is the case today's nested-row guard would leave
+ // unapplied (a nested row keeps its flag): it must not reach it.
+ {"record asks to stop hibernating", true, false, "record hibernate false, row true"},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ rec := rootfsRecord(sqid, rootfsHost, c.minted)
+ if err := CreateRecord(ctx, s, rec); err != nil {
+ t.Fatal(err)
+ }
+ reg := regWith(t)
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ before := *mustGet(t, reg, rootfsHost)
+ rewriteRecord(t, s, RecordKey(sqid, "shop", rootfsHost), func(m map[string]any) {
+ m["hibernate"] = c.flipped
+ })
+ logged := captureLog(t)
+ var rep ReconcileReport
+ reconcileOnce(t, s, reg, rootfsCfg(&rep))
+ after := *mustGet(t, reg, rootfsHost)
+ if after != before {
+ t.Fatalf("row changed:\n before %+v\n after %+v", before, after)
+ }
+ out := logged.String()
+ if strings.Contains(out, "converge hibernate") || strings.Contains(out, "converged "+rootfsHost+" hibernate") {
+ t.Fatalf("the pass tried to converge hibernation:\n%s", out)
+ }
+ if n := strings.Count(out, c.logWord); n != 1 {
+ t.Fatalf("want one line naming %q, got %d:\n%s", c.logWord, n, out)
+ }
+ if !rep.Complete[sqid+"/shop"] {
+ t.Fatalf("the disagreement left the app's report incomplete (resident retention would wait forever):\n%s", out)
+ }
+ })
+ }
+ // An unknown runtime is fixed and inert like rootfs-v1: the skip keys
+ // on any runtime, not on Rootfs(). The record here is binary (a
+ // record with the unknown runtime never passes ValidateShape).
+ t.Run("unknown runtime row", func(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ off := false
+ rec := Record{Schema: 1, Host: rootfsHost, Slug: "bab", App: "shop", Account: sqid,
+ Region: "ie", Channel: "stable", Hibernate: &off, CreatedAt: time.Now()}
+ if err := CreateRecord(ctx, s, rec); err != nil {
+ t.Fatal(err)
+ }
+ reg := regWith(t, registry.Route{Host: rootfsHost, Slug: "bab", Kind: "instance",
+ Socket: NestedSocket(rootfsHost), DBPath: NestedDBPath("/data", rootfsHost),
+ App: "shop", Account: sqid, Region: "ie", Channel: "stable", Hibernate: true,
+ Provisioned: true, Runtime: "rootfs-v2"})
+ logged := captureLog(t)
+ var rep ReconcileReport
+ reconcileOnce(t, s, reg, rootfsCfg(&rep))
+ got := mustGet(t, reg, rootfsHost)
+ if got.Runtime != "rootfs-v2" || !got.Hibernate {
+ t.Fatalf("row runtime %q hibernate %v, want rootfs-v2 true", got.Runtime, got.Hibernate)
+ }
+ out := logged.String()
+ if strings.Contains(out, "converge hibernate") {
+ t.Fatalf("the pass tried to converge hibernation:\n%s", out)
+ }
+ if !strings.Contains(out, `record runtime "", row "rootfs-v2"`) {
+ t.Fatalf("no runtime line:\n%s", out)
+ }
+ if !rep.Complete[sqid+"/shop"] {
+ t.Fatalf("report incomplete:\n%s", out)
+ }
+ })
+}
+
+// Review Focus 1: an older console's UpdateRecord re-marshals its own
+// struct and drops "runtime". The box keeps the row's runtime and
+// hibernation, logs the mismatch, converges every other column, and
+// retires nothing.
+func TestStrippedRecordLeavesRootfsRowAlone(t *testing.T) {
+ cases := []struct {
+ name string
+ mut func(map[string]any)
+ }{
+ {"runtime dropped", func(m map[string]any) {
+ delete(m, "runtime")
+ m["channel"] = "beta"
+ }},
+ {"runtime dropped and hibernate flipped", func(m map[string]any) {
+ delete(m, "runtime")
+ m["hibernate"] = true
+ m["channel"] = "beta"
+ }},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, false)); err != nil {
+ t.Fatal(err)
+ }
+ reg := regWith(t)
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ rewriteRecord(t, s, RecordKey(sqid, "shop", rootfsHost), c.mut)
+ for pass := 1; pass <= 3; pass++ {
+ logged := captureLog(t)
+ var rep ReconcileReport
+ reconcileOnce(t, s, reg, rootfsCfg(&rep))
+ got, err := reg.Get(rootfsHost)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got == nil {
+ t.Fatalf("pass %d: the host was retired", pass)
+ }
+ if got.Runtime != registry.RuntimeRootfsV1 || got.Hibernate {
+ t.Fatalf("pass %d: row runtime %q hibernate %v, want %q false", pass, got.Runtime, got.Hibernate, registry.RuntimeRootfsV1)
+ }
+ if got.Socket != NestedSocket(rootfsHost) || got.DBPath != NestedDBPath("/data", rootfsHost) {
+ t.Fatalf("pass %d: row paths moved: socket %q db %q", pass, got.Socket, got.DBPath)
+ }
+ if got.Seed != "" || got.SeedSource != "" {
+ t.Fatalf("pass %d: row took a seed: %q %q", pass, got.Seed, got.SeedSource)
+ }
+ if got.Channel != "beta" {
+ t.Fatalf("pass %d: channel %q, want the record's beta converged", pass, got.Channel)
+ }
+ out := logged.String()
+ line := `provision: ` + rootfsHost + `: record runtime "", row "rootfs-v1": the runtime is fixed at mint; leaving the row's runtime and hibernation alone`
+ if n := strings.Count(out, line); n != 1 {
+ t.Fatalf("pass %d: want the runtime line once, got %d:\n%s", pass, n, out)
+ }
+ if strings.Contains(out, "converge hibernate") {
+ t.Fatalf("pass %d: the pass tried to converge hibernation:\n%s", pass, out)
+ }
+ if !rep.Complete[sqid+"/shop"] {
+ t.Fatalf("pass %d: report incomplete:\n%s", pass, out)
+ }
+ }
+ })
+ }
+}
+
+func TestReconcileRefusesUnknownRuntime(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ const fresh, held = "new.shop.example.com", "old.shop.example.com"
+ off := false
+ for _, host := range []string{fresh, held} {
+ if err := CreateRecord(ctx, s, Record{Schema: 1, Host: host, Slug: strings.SplitN(host, ".", 2)[0],
+ App: "shop", Account: sqid, Region: "ie", Channel: "stable", Hibernate: &off,
+ CreatedAt: time.Now()}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ reg := regWith(t)
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ if err := reg.Delete(fresh); err != nil {
+ t.Fatal(err)
+ }
+ before := *mustGet(t, reg, held)
+ for _, host := range []string{fresh, held} {
+ rewriteRecord(t, s, RecordKey(sqid, "shop", host), func(m map[string]any) {
+ m["runtime"] = "rootfs-v2"
+ m["channel"] = "beta"
+ })
+ }
+ logged := captureLog(t)
+ var rep ReconcileReport
+ reconcileOnce(t, s, reg, rootfsCfg(&rep))
+ if rt, err := reg.Get(fresh); err != nil || rt != nil {
+ t.Fatalf("an unknown-runtime record was minted: %+v %v", rt, err)
+ }
+ after, err := reg.Get(held)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if after == nil {
+ t.Fatal("an unknown-runtime record retired its host's row")
+ }
+ if *after != before {
+ t.Fatalf("an unknown-runtime record converged its row:\n before %+v\n after %+v", before, *after)
+ }
+ if !strings.Contains(logged.String(), `runtime "rootfs-v2"`) {
+ t.Fatalf("no refusal names the runtime:\n%s", logged.String())
+ }
+ if rep.Complete[sqid+"/shop"] {
+ t.Fatal("an unusable record left its app complete")
+ }
+}
+
+func TestAdoptionRefusesAcrossRuntimes(t *testing.T) {
+ s, sqid := rootfsSetup(t)
+ if err := CreateRecord(ctx, s, rootfsRecord(sqid, rootfsHost, false)); err != nil {
+ t.Fatal(err)
+ }
+ // A hand-added binary row at exactly the paths the rootfs record
+ // derives: only the runtime tells them apart.
+ hand := registry.Route{Host: rootfsHost, Slug: "bab", Kind: "instance",
+ Socket: NestedSocket(rootfsHost), DBPath: NestedDBPath("/data", rootfsHost),
+ App: "shop", Account: sqid, Region: "ie", Channel: "stable", Hibernate: true}
+ reg := regWith(t, hand)
+ before := *mustGet(t, reg, rootfsHost)
+ logged := captureLog(t)
+ reconcileOnce(t, s, reg, rootfsCfg(nil))
+ after := *mustGet(t, reg, rootfsHost)
+ if after != before {
+ t.Fatalf("the hand-added row was touched:\n before %+v\n after %+v", before, after)
+ }
+ if !bytes.Contains(logged.Bytes(), []byte(`runtime ""/"rootfs-v1"`)) {
+ t.Fatalf("the refusal does not name the runtime:\n%s", logged.String())
+ }
+}
+
+// A rootfs record or row is home-boxed whatever else it says, and the two
+// gates agree on it (TestEverywhereClassAgreesAcrossRecordAndRow's rule).
+// The fixtures are shapes that would otherwise be everywhere-class, so only
+// the runtime answers.
+func TestRootfsRecordIsHomeBoxed(t *testing.T) {
+ for _, runtime := range []string{RuntimeRootfsV1, "rootfs-v2"} {
+ for _, rec := range []Record{
+ {Host: "site.shop.example.com", App: "shop", Account: "bab", Channel: "stable", Kind: "static", Runtime: runtime},
+ {Host: "pub.shop.example.com", App: "shop", Account: "bab", Channel: "stable", Kind: "service", Addr: "203.0.113.10:443", Runtime: runtime},
+ } {
+ base := rec
+ base.Runtime = ""
+ if !everywhereClass(base) || !everywhereRoute(RouteFor(base, "/data", "", false, nil)) {
+ t.Fatalf("fixture %s is not everywhere-class without a runtime", rec.Host)
+ }
+ if everywhereClass(rec) {
+ t.Errorf("everywhereClass(%s, runtime %q) = true, want home-boxed", rec.Host, runtime)
+ }
+ row := RouteFor(rec, "/data", "", false, nil)
+ if everywhereRoute(row) {
+ t.Errorf("everywhereRoute(%s, runtime %q) = true, want home-boxed", rec.Host, runtime)
+ }
+ row.Runtime = runtime
+ if everywhereRoute(row) {
+ t.Errorf("everywhereRoute(row %s, runtime %q) = true, want home-boxed", rec.Host, runtime)
+ }
+ }
+ }
+ // The real shape: a valid rootfs record and the row it mints.
+ h := false
+ rec := Record{Schema: 1, Host: rootfsHost, Slug: "bab", App: "shop", Account: "bab",
+ Region: "ie", Channel: "stable", Runtime: RuntimeRootfsV1, Hibernate: &h}
+ if err := ValidateShape(rec); err != nil {
+ t.Fatal(err)
+ }
+ if everywhereClass(rec) || everywhereRoute(RouteFor(rec, "/data", "", true, nil)) {
+ t.Fatal("a rootfs record is not home-boxed")
+ }
+}