carlos / platform Public

provision: Record.Runtime; rootfs rails; runtime and rootfs hibernation fixed in WriteRecord

Record gains Runtime (omitempty, so binary records keep their bytes).
ValidateShape refuses an unknown runtime, and a rootfs-v1 record with no
hibernate, a kind other than instance, an upstream, a unit, a role,
replicas, a pool or a seed (D5, D6). RuntimeFromManifest is the mint
helper. WriteRecord, every record writer's Put, calls runtimeFixed: a
create passes, the exact tombstone of the stored bytes passes (retirement
ends the life), and otherwise the runtime must be byte-equal and a stored
rootfs-v1 record's hibernate present on both sides and equal, or the write
is ErrRuntimeProtected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev f56208824cac8ce453bb5509af634fad4fdb1934 parent add21b9
4 files changed, +422 −2
  • internal/provision/provision.go +8 −1
  • internal/provision/record_sequence.go +6 −1
  • internal/provision/runtime.go +135 −0
  • internal/provision/runtime_test.go +273 −0
diff --git a/internal/provision/provision.go b/internal/provision/provision.go
index 836a3b7ee..0cf2a88fa 100644
--- a/internal/provision/provision.go
+++ b/internal/provision/provision.go
@@ -117,6 +117,13 @@ type Record struct {
// plain bool would read those as false and un-hibernate the fleet on
// the first pass after deploy. nil = HibernateOrDefault's derivation.
Hibernate *bool `json:"hibernate,omitempty"`
+ // Runtime is how the instance's process runs: "" (every record before
+ // the field, and every binary instance) is today's launch,
+ // RuntimeRootfsV1 a rootfs image (registry.Route.Runtime). Set at mint
+ // from the release (RuntimeFromManifest) and fixed after it, with a
+ // rootfs record's Hibernate: WriteRecord refuses any write that moves
+ // either (ErrRuntimeProtected). ValidateShape refuses an unknown value.
+ Runtime string `json:"runtime,omitempty"`
// Environment is the named config bundle this route binds to ("" = the
// app's default bundle alone). The second member-facing axis of
// `carlos route`, and owned by this record for the same reason Channel
@@ -731,7 +738,7 @@ func ValidateShape(rec Record) error {
if len(rec.AddrsOrDefault()) > 0 && rec.Hibernate != nil && *rec.Hibernate {
return errors.New("addr with hibernate: an addr-backed route proxies to a process the platform does not spawn and has no socket to wake, so arming the activator for it would wedge the host — drop hibernate")
}
- return nil
+ return validateRuntime(rec)
}
// validAddr checks one upstream's shape: host:port, with the port an
diff --git a/internal/provision/record_sequence.go b/internal/provision/record_sequence.go
index f8c307f18..785045c86 100644
--- a/internal/provision/record_sequence.go
+++ b/internal/provision/record_sequence.go
@@ -116,7 +116,9 @@ type preparedRecord struct {
// Legacy absent counters and no claim context retain the exact body bytes.
// A before marked by a migration (incarnation_migrating) is refused with
// ErrRecordMigrating unless ctx carries WithMigration of that marker; a
-// create-only write reads no before and is unaffected.
+// create-only write reads no before and is unaffected. A write that moves
+// the runtime, or a rootfs record's hibernation, is refused with
+// ErrRuntimeProtected (runtimeFixed): this guard is every writer's.
func WriteRecord(ctx context.Context, s release.Store, key string, before, body []byte, etag string) (string, uint64, error) {
if !IsRecordKey(key) {
return "", 0, ErrRecordSequence
@@ -132,6 +134,9 @@ func WriteRecord(ctx context.Context, s release.Store, key string, before, body
if err != nil {
return "", 0, err
}
+ if err := runtimeFixed(before, body); err != nil {
+ return "", 0, err
+ }
enabled, _ := ctx.Value(recordSequenceKey{}).(bool)
_, present := fields["record_seq"]
p, nested := ctx.Value(preparedRecordKey{}).(*preparedRecord)
diff --git a/internal/provision/runtime.go b/internal/provision/runtime.go
new file mode 100644
index 000000000..3d90c4350
--- /dev/null
+++ b/internal/provision/runtime.go
@@ -0,0 +1,135 @@
+package provision
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// RuntimeRootfsV1 is Record.Runtime for a rootfs image: the registry's
+// value, so the record and the row it converges onto cannot disagree on the
+// spelling. Plan 4's create door writes provision.RuntimeRootfsV1.
+const RuntimeRootfsV1 = registry.RuntimeRootfsV1
+
+// RuntimeFromManifest is the runtime and hibernation a record is minted with
+// for a release: a rootfs release gives RuntimeRootfsV1 and a pointer to a
+// copy of its Hibernate; anything else (nil included) gives "", nil, which
+// leaves a binary record exactly as it is minted today.
+func RuntimeFromManifest(m *release.Manifest) (runtime string, hibernate *bool) {
+ if m == nil || !m.IsRootfs() {
+ return "", nil
+ }
+ h := m.Hibernate
+ return RuntimeRootfsV1, &h
+}
+
+// validateRuntime is ValidateShape's runtime rails: an unknown runtime
+// refuses (ValidRuntime), and a rootfs-v1 record is an instance whose
+// process the activator spawns from its image, with its hibernation stated,
+// and nothing a rootfs instance does not have in v1: no upstream, no unit,
+// no replica role or placements, no pool (D6) and no seed (D5).
+func validateRuntime(rec Record) error {
+ if !registry.ValidRuntime(rec.Runtime) {
+ return fmt.Errorf("runtime %q: want \"\" or %q", rec.Runtime, RuntimeRootfsV1)
+ }
+ if rec.Runtime != RuntimeRootfsV1 {
+ return nil
+ }
+ switch {
+ case rec.Hibernate == nil:
+ return errors.New("runtime rootfs-v1 with no hibernate: a rootfs record states its hibernation at mint, from the release")
+ case rec.KindOrDefault() != "instance":
+ return fmt.Errorf("runtime rootfs-v1 with kind %s: a rootfs image runs as an instance", rec.KindOrDefault())
+ case len(rec.AddrsOrDefault()) > 0:
+ return errors.New("runtime rootfs-v1 with addr: the activator spawns a rootfs instance, so it proxies to no upstream")
+ case rec.Unit != "":
+ return errors.New("runtime rootfs-v1 with unit: a rootfs instance's units are the platform's own")
+ case rec.Role != "":
+ return fmt.Errorf("runtime rootfs-v1 with role %s: a rootfs record is never a replica", rec.Role)
+ case len(rec.Replicas) > 0:
+ return errors.New("runtime rootfs-v1 with replicas: a rootfs record names no replica placements")
+ case rec.Pool != "":
+ return errors.New("runtime rootfs-v1 with pool: a rootfs record is never pooled")
+ case rec.Seed != nil:
+ return errors.New("runtime rootfs-v1 with seed: seeds and imports are not offered for rootfs records")
+ }
+ return nil
+}
+
+// ErrRuntimeProtected is WriteRecord's refusal of a write that changes an
+// instance's runtime, or a rootfs instance's hibernation, after its mint
+// (spec §5.0). Either changes only by deletion and a new mint.
+var ErrRuntimeProtected = errors.New("provision: an instance's runtime, and a rootfs instance's hibernation, are fixed when it is created")
+
+// runtimeFields is all runtimeFixed reads of a record body. Decoding with
+// encoding/json, as DecodeRecord does, gives the same answer every reader of
+// the record gets, case-insensitive key matches included.
+type runtimeFields struct {
+ Runtime string `json:"runtime"`
+ Hibernate *bool `json:"hibernate"`
+}
+
+// runtimeFixed is WriteRecord's guard: before is the stored body the write
+// is If-Match'd on ({} for a create), body the one it writes. A create
+// passes, and so does the exact tombstone of before (retirement ends the
+// life; the hostname's next life is a new mint). Otherwise the runtime must
+// be byte-equal, and a stored rootfs-v1 record's hibernate must be present
+// in both and equal. A body that does not decode is ErrRecordSequence, as
+// RecordSequence answers it.
+func runtimeFixed(before, body []byte) error {
+ prior, _, err := RecordSequence(before)
+ if err != nil {
+ return err
+ }
+ if len(prior) == 0 {
+ return nil
+ }
+ if isTombstoneOf(before, body) {
+ return nil
+ }
+ var was, now runtimeFields
+ if json.Unmarshal(before, &was) != nil || json.Unmarshal(body, &now) != nil {
+ return ErrRecordSequence
+ }
+ if was.Runtime != now.Runtime {
+ return ErrRuntimeProtected
+ }
+ if was.Runtime == RuntimeRootfsV1 && (was.Hibernate == nil || now.Hibernate == nil || *was.Hibernate != *now.Hibernate) {
+ return ErrRuntimeProtected
+ }
+ return nil
+}
+
+// isTombstoneOf reports whether body is TombstoneBody(before), with
+// record_seq left to WriteRecord's own sequencing (a store wrapper's nested
+// WriteRecord sees the advanced counter).
+func isTombstoneOf(before, body []byte) bool {
+ tomb, err := TombstoneBody(before)
+ if err != nil {
+ return false
+ }
+ want, _, err := RecordSequence(tomb)
+ if err != nil {
+ return false
+ }
+ got, _, err := RecordSequence(body)
+ if err != nil {
+ return false
+ }
+ delete(want, "record_seq")
+ delete(got, "record_seq")
+ if len(want) != len(got) {
+ return false
+ }
+ for k, v := range want {
+ g, ok := got[k]
+ if !ok || !bytes.Equal(g, v) {
+ return false
+ }
+ }
+ return true
+}
diff --git a/internal/provision/runtime_test.go b/internal/provision/runtime_test.go
new file mode 100644
index 000000000..8d6f2ca0d
--- /dev/null
+++ b/internal/provision/runtime_test.go
@@ -0,0 +1,273 @@
+package provision
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/release"
+)
+
+func boolPtr(b bool) *bool { return &b }
+
+// rootfsTestRecord is a rootfs-v1 instance record as Plan 4's create door
+// mints it: an instance, hibernation stated, no upstream, unit, role,
+// replicas, pool or seed.
+func rootfsTestRecord(host string, hibernate bool) Record {
+ return Record{
+ Schema: 1, Host: host, Slug: "shop", App: "shop", Account: "bab",
+ Region: "ie", Channel: "stable", CreatedAt: time.Date(2026, 10, 26, 9, 0, 0, 0, time.UTC),
+ Runtime: RuntimeRootfsV1, Hibernate: boolPtr(hibernate),
+ }
+}
+
+func TestValidateShapeRuntime(t *testing.T) {
+ base := func() Record { return rootfsTestRecord("a.shop.example.com", false) }
+ ok := []struct {
+ name string
+ rec Record
+ }{
+ {"binary", Record{}},
+ {"binary with hibernate", Record{Hibernate: boolPtr(true)}},
+ {"rootfs-v1 hibernate false", base()},
+ {"rootfs-v1 hibernate true", rootfsTestRecord("a.shop.example.com", true)},
+ }
+ for _, c := range ok {
+ if err := ValidateShape(c.rec); err != nil {
+ t.Errorf("%s: ValidateShape = %v, want nil", c.name, err)
+ }
+ }
+ refused := []struct {
+ name string
+ mut func(*Record)
+ }{
+ {"unknown runtime", func(r *Record) { r.Runtime = "rootfs-v2" }},
+ {"unknown runtime on a binary shape", func(r *Record) { r.Runtime = "Rootfs-v1"; r.Hibernate = nil }},
+ {"nil hibernate", func(r *Record) { r.Hibernate = nil }},
+ {"kind static", func(r *Record) { r.Kind = "static" }},
+ {"kind service", func(r *Record) { r.Kind = "service"; r.Addr = "127.0.0.1:8090" }},
+ {"addr", func(r *Record) { r.Addr = "127.0.0.1:8090" }},
+ {"addrs", func(r *Record) { r.Addrs = []string{"127.0.0.1:8090"} }},
+ {"unit", func(r *Record) { r.Unit = "shop.service" }},
+ {"role replica", func(r *Record) { r.Role = "replica" }},
+ {"replicas", func(r *Record) { r.Replicas = []string{"uk"} }},
+ {"pool", func(r *Record) { r.Pool = "shop" }},
+ {"seed", func(r *Record) { r.Seed = &SeedSource{} }},
+ }
+ for _, c := range refused {
+ rec := base()
+ c.mut(&rec)
+ if err := ValidateShape(rec); err == nil {
+ t.Errorf("%s: ValidateShape = nil, want a refusal", c.name)
+ }
+ }
+ // Kind "instance" spelled out is the default, not a refusal.
+ rec := base()
+ rec.Kind = "instance"
+ if err := ValidateShape(rec); err != nil {
+ t.Errorf("kind instance spelled out: ValidateShape = %v, want nil", err)
+ }
+}
+
+func TestRuntimeFromManifest(t *testing.T) {
+ for _, hib := range []bool{false, true} {
+ m := &release.Manifest{Kind: release.KindRootfs, Hibernate: hib}
+ rt, h := RuntimeFromManifest(m)
+ if rt != RuntimeRootfsV1 || h == nil || *h != hib {
+ t.Fatalf("rootfs hibernate %v: got (%q, %v), want (%q, &%v)", hib, rt, h, RuntimeRootfsV1, hib)
+ }
+ m.Hibernate = !hib
+ if *h != hib {
+ t.Errorf("rootfs hibernate %v: the pointer followed the manifest; want a copy", hib)
+ }
+ }
+ if rt, h := RuntimeFromManifest(&release.Manifest{Hibernate: true}); rt != "" || h != nil {
+ t.Errorf("binary manifest: got (%q, %v), want (\"\", nil)", rt, h)
+ }
+ if rt, h := RuntimeFromManifest(nil); rt != "" || h != nil {
+ t.Errorf("nil manifest: got (%q, %v), want (\"\", nil)", rt, h)
+ }
+ if RuntimeRootfsV1 != "rootfs-v1" {
+ t.Errorf("RuntimeRootfsV1 = %q", RuntimeRootfsV1)
+ }
+}
+
+func TestOrdinaryWritersCannotChangeRuntime(t *testing.T) {
+ ctx := context.Background()
+ s := dirStore(t)
+ rec := rootfsTestRecord("a.shop.example.com", false)
+ if err := CreateRecord(ctx, s, rec); err != nil {
+ t.Fatal(err)
+ }
+ key := RecordKey(rec.Account, rec.App, rec.Host)
+ stored, _, err := s.Get(ctx, key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ refused := []struct {
+ name string
+ mut func(*Record)
+ }{
+ {"runtime cleared", func(r *Record) { r.Runtime = "" }},
+ {"runtime changed", func(r *Record) { r.Runtime = "rootfs-v2" }},
+ {"hibernate true", func(r *Record) { r.Hibernate = boolPtr(true) }},
+ {"hibernate nil", func(r *Record) { r.Hibernate = nil }},
+ }
+ for _, c := range refused {
+ cur, etag, err := GetRecordETag(ctx, s, rec.Account, rec.App, rec.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ c.mut(cur)
+ if err := UpdateRecord(ctx, s, *cur, etag); !errors.Is(err, ErrRuntimeProtected) {
+ t.Errorf("UpdateRecord, %s: err = %v, want ErrRuntimeProtected", c.name, err)
+ }
+ if _, err := updateRecord(ctx, s, rec.Account, rec.App, rec.Host, func(r *Record) error { c.mut(r); return nil }); !errors.Is(err, ErrRuntimeProtected) {
+ t.Errorf("updateRecord, %s: err = %v, want ErrRuntimeProtected", c.name, err)
+ }
+ if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, stored) {
+ t.Fatalf("%s: stored body moved: %s (%v), want %s", c.name, now, err, stored)
+ }
+ }
+
+ cur, etag, err := GetRecordETag(ctx, s, rec.Account, rec.App, rec.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ cur.Environment = "staging"
+ if err := UpdateRecord(ctx, s, *cur, etag); err != nil {
+ t.Fatalf("UpdateRecord, environment: %v", err)
+ }
+ got, err := GetRecord(ctx, s, rec.Account, rec.App, rec.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got.Environment != "staging" || got.Runtime != RuntimeRootfsV1 || got.Hibernate == nil || *got.Hibernate {
+ t.Fatalf("after the environment write: environment %q runtime %q hibernate %v, want staging, rootfs-v1, false", got.Environment, got.Runtime, got.Hibernate)
+ }
+
+ // A binary record's hibernation stays an ordinary setting, and it
+ // cannot take a runtime after its mint.
+ bin := Record{Schema: 1, Host: "b.shop.example.com", Slug: "shop", App: "shop", Account: "bab", Channel: "stable", CreatedAt: rec.CreatedAt}
+ if err := CreateRecord(ctx, s, bin); err != nil {
+ t.Fatal(err)
+ }
+ for _, hib := range []bool{true, false} {
+ cur, etag, err := GetRecordETag(ctx, s, bin.Account, bin.App, bin.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ cur.Hibernate = boolPtr(hib)
+ if err := UpdateRecord(ctx, s, *cur, etag); err != nil {
+ t.Fatalf("binary hibernate %v: %v", hib, err)
+ }
+ }
+ cur, etag, err = GetRecordETag(ctx, s, bin.Account, bin.App, bin.Host)
+ if err != nil {
+ t.Fatal(err)
+ }
+ cur.Runtime = RuntimeRootfsV1
+ if err := UpdateRecord(ctx, s, *cur, etag); !errors.Is(err, ErrRuntimeProtected) {
+ t.Fatalf("binary record taking a runtime: err = %v, want ErrRuntimeProtected", err)
+ }
+}
+
+func TestRawWriteThatDropsRuntimeIsRefused(t *testing.T) {
+ ctx := context.Background()
+ s := dirStore(t)
+ rec := rootfsTestRecord("a.shop.example.com", true)
+ if err := CreateRecord(ctx, s, rec); err != nil {
+ t.Fatal(err)
+ }
+ key := RecordKey(rec.Account, rec.App, rec.Host)
+ stored, etag, err := s.Get(ctx, key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ without := func(drop ...string) []byte {
+ var m map[string]json.RawMessage
+ if err := json.Unmarshal(stored, &m); err != nil {
+ t.Fatal(err)
+ }
+ for _, k := range drop {
+ delete(m, k)
+ }
+ b, err := json.Marshal(m)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return b
+ }
+ // An old writer's shape: its struct has no runtime field.
+ if _, _, err := WriteRecord(ctx, s, key, stored, without("runtime"), etag); !errors.Is(err, ErrRuntimeProtected) {
+ t.Errorf("drop runtime: err = %v, want ErrRuntimeProtected", err)
+ }
+ if _, _, err := WriteRecord(ctx, s, key, stored, without("hibernate"), etag); !errors.Is(err, ErrRuntimeProtected) {
+ t.Errorf("drop hibernate: err = %v, want ErrRuntimeProtected", err)
+ }
+ // A case alias decodes like the field it names, for every reader.
+ alias := bytes.Replace(stored, []byte(`"runtime":"rootfs-v1"`), []byte(`"runtime":"rootfs-v1","RUNTIME":""`), 1)
+ if bytes.Equal(alias, stored) {
+ t.Fatal("test body: no runtime key to alias")
+ }
+ if _, _, err := WriteRecord(ctx, s, key, stored, alias, etag); !errors.Is(err, ErrRuntimeProtected) {
+ t.Errorf("aliased runtime: err = %v, want ErrRuntimeProtected", err)
+ }
+ // A body whose runtime does not decode answers as RecordSequence does.
+ bad := bytes.Replace(stored, []byte(`"runtime":"rootfs-v1"`), []byte(`"runtime":5`), 1)
+ if _, _, err := WriteRecord(ctx, s, key, stored, bad, etag); !errors.Is(err, ErrRecordSequence) {
+ t.Errorf("undecodable runtime: err = %v, want ErrRecordSequence", err)
+ }
+ // Nearly a tombstone (retired, runtime gone, a field kept) is not one.
+ var tm map[string]json.RawMessage
+ _ = json.Unmarshal(without("runtime", "hibernate"), &tm)
+ tm["retired"] = json.RawMessage("true")
+ near, _ := json.Marshal(tm)
+ if _, _, err := WriteRecord(ctx, s, key, stored, near, etag); !errors.Is(err, ErrRuntimeProtected) {
+ t.Errorf("retired body that is not the tombstone: err = %v, want ErrRuntimeProtected", err)
+ }
+ if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, stored) {
+ t.Fatalf("stored body moved: %s (%v)", now, err)
+ }
+ // The stored bytes written back unchanged (a fence) pass.
+ etag, _, err = WriteRecord(ctx, s, key, stored, stored, etag)
+ if err != nil {
+ t.Fatalf("identity write: %v", err)
+ }
+ // Retirement is the end of the life, not an ordinary write: the exact
+ // tombstone of the stored bytes passes, sequenced or not.
+ tomb, err := TombstoneBody(stored)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, _, err := WriteRecord(WithRecordSequence(ctx), s, key, stored, tomb, etag); err != nil {
+ t.Fatalf("tombstone: %v", err)
+ }
+ now, _, err := s.Get(ctx, key)
+ if err != nil || strings.Contains(string(now), `"runtime"`) || !strings.Contains(string(now), `"retired":true`) {
+ t.Fatalf("after the tombstone: %s (%v)", now, err)
+ }
+}
+
+func TestBinaryRecordBytesUnchanged(t *testing.T) {
+ rec := Record{Schema: 1, Host: "a.shop.example.com", Slug: "shop", App: "shop", Account: "bab", Channel: "stable", Hibernate: boolPtr(true)}
+ body, err := json.Marshal(rec)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(body), `"runtime"`) {
+ t.Fatalf("binary record marshals a runtime key: %s", body)
+ }
+ rec.Runtime = RuntimeRootfsV1
+ body, err = json.Marshal(rec)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(body), `"runtime":"rootfs-v1"`) {
+ t.Fatalf("rootfs record marshals no runtime: %s", body)
+ }
+}