carlos / platform Public

console: instance create mints rootfs records from the creating release and refuses off the account's own boxes (rootfs spec §5.0, §5.6, §5.7)

A socket-backed create of an app whose creating release is rootfs is
stamped Runtime rootfs-v1 with the release's Hibernate, before
ValidateShape, and refuses a seeded or imported create (RFS-4-C09), a
replica (C10), a placement off the account's own boxes or in a pool
(C01, C02), and a placement with no ready box (C05, C06, C07).

The first-kind boundary (row 3.2): the door holds no lock. When it finds
the app unfixed it marks the request context, and both record writers
(createRecordMarked, createRecordWithLifetimeLocked; the seed and import
doors share them) read the kind again under their own app lock before
writing anything. A first rootfs ship that fixed the kind meanwhile gets
the door's try-again sentence and no binary record.

shouldStampHA is false for a record with a runtime (row 3.3).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 953a6494904afc2c71ec1bd89b202c2ce26ed7bb parent dbae21b
7 files changed, +579 −3
  • approved_rootfs_copy_test.go +3 −0
  • internal/console/apiinstances.go +38 −0
  • internal/console/hostsused.go +4 −0
  • internal/console/lifetimes_create.go +5 −0
  • internal/console/rootfs.go +92 −0
  • internal/console/rootfs_test.go +432 −1
  • internal/console/seedmove.go +5 −2
diff --git a/approved_rootfs_copy_test.go b/approved_rootfs_copy_test.go
index 12e4ad14d..964d1e149 100644
--- a/approved_rootfs_copy_test.go
+++ b/approved_rootfs_copy_test.go
@@ -195,6 +195,9 @@ var rootfsCopyBuilt = []string{
// Plan 4 Task 2: what the creating release says, and where a rootfs row
// may run (internal/console/rootfs.go)
"RFS-4-C01", "RFS-4-C02", "RFS-4-C05", "RFS-4-C06", "RFS-4-C07", "RFS-4-C08",
+ // Plan 4 Task 3: instance create refuses a seeded or replica rootfs
+ // instance (internal/console/rootfs.go)
+ "RFS-4-C09", "RFS-4-C10",
}
func mustParseRootfsRecord(t *testing.T) []copyRow {
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go
index c97208429..d1c75c881 100644
--- a/internal/console/apiinstances.go
+++ b/internal/console/apiinstances.go
@@ -1630,6 +1630,34 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request)
jsonError(w, ref.Status, ref.Msg)
return
}
+ // Rootfs apps (rootfs spec §5.0, §5.6, §5.7; Plan 4 Task 3). Only a
+ // socket-backed instance is a process the platform starts, so only its
+ // create asks what the creating release is: a static or addr-backed
+ // record of a rootfs app is minted as today, with no extra read. Once
+ // the hostname history is active, a fleet must also be examined before
+ // a data create may target it (hostsTargetRefusal, below), rootfs or not
+ // (row 3.7, accepted).
+ var head rootfsHead
+ if (provision.Record{Kind: strings.TrimSpace(req.Kind), Addrs: req.Addrs}).DerivesPaths() {
+ var ref *doorRefusal
+ if head, ref = s.rootfsHeadFor(ctx, acct.Sqid, app, channel); ref != nil {
+ jsonError(w, ref.Status, ref.Msg)
+ return
+ }
+ // The first-kind boundary (rootfs.go): no first ship has fixed the
+ // app's kind yet, so the record writer reads it again under the
+ // app lock. Every later context of this request derives from r.
+ if head.Unfixed {
+ r = r.WithContext(withUnfixedFirstKind(r.Context(), acct.Sqid, app))
+ ctx = withUnfixedFirstKind(ctx, acct.Sqid, app)
+ }
+ if head.Rootfs {
+ if ref := s.rootfsCreateRefusal(ctx, reg, acct, app, placed, region, m.Pool, probeRec.Role, seeded); ref != nil {
+ jsonError(w, ref.Status, ref.Msg)
+ return
+ }
+ }
+ }
rec := provision.Record{
Schema: 1,
Host: host,
@@ -1654,6 +1682,16 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request)
// everything else before this point.
rec.Role = probeRec.Role
rec.Replicas = probeRec.Replicas
+ // Kind and hibernation are fixed at creation (spec §5.0): a record whose
+ // creating release is rootfs is minted rootfs-v1 with that release's
+ // hibernation stated, before ValidateShape (Plan 3's rules), buildClaim
+ // and the record write all see it. The rule is
+ // provision.RuntimeFromManifest's, stamped here by hand because the head
+ // may be a first ship's kind marker with no manifest yet (row 3.4).
+ if head.Rootfs && rec.DerivesPaths() {
+ h := head.Hibernate
+ rec.Runtime, rec.Hibernate = provision.RuntimeRootfsV1, &h
+ }
// Every instance record draws its own asset generation at create (edge
// asset cache design §6). Drawn here, once, so the seed and import doors
// below and the claim path's buildClaim all write this same token.
diff --git a/internal/console/hostsused.go b/internal/console/hostsused.go
index d0701716b..99bb51f21 100644
--- a/internal/console/hostsused.go
+++ b/internal/console/hostsused.go
@@ -559,6 +559,10 @@ func recordUse(rec provision.Record) provision.HostUse {
// (placeAppTarget).
func (s *Server) createRecordMarked(ctx context.Context, rec provision.Record) error {
defer s.lockApp(rec.Account, rec.App)() // record writes hold the app lock (recordlock.go)
+ // The first-kind boundary (rootfs.go): under the lock, before the mark.
+ if err := s.firstKindRecheck(ctx, rec); err != nil {
+ return err
+ }
if rec.DataKind() {
if ref := s.gateDataCreate(ctx, rec.Host, rec.Region); ref != nil {
return &hostsUsedError{*ref}
diff --git a/internal/console/lifetimes_create.go b/internal/console/lifetimes_create.go
index 43292ad4f..900311efc 100644
--- a/internal/console/lifetimes_create.go
+++ b/internal/console/lifetimes_create.go
@@ -61,6 +61,11 @@ func (s *Server) createRecordWithLifetimeLocked(ctx context.Context, rec provisi
if len(rec.Maintenance) != 0 {
return nil, provision.ErrMaintenanceProjection
}
+ // The first-kind boundary (rootfs.go): under the app lock the caller
+ // holds, before anything is read for the protocol or persisted.
+ if err := s.firstKindRecheck(ctx, rec); err != nil {
+ return nil, err
+ }
ctx = provision.WithLifetimes(ctx, s.judge)
host := rec.Host
// Step 0: the app's lifecycle, under the app lock.
diff --git a/internal/console/rootfs.go b/internal/console/rootfs.go
index f0b946b9d..f5773c02b 100644
--- a/internal/console/rootfs.go
+++ b/internal/console/rootfs.go
@@ -2,11 +2,13 @@ package console
import (
"context"
+ "errors"
"fmt"
"log"
"net/http"
"time"
+ "github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/status"
@@ -34,6 +36,10 @@ const (
rootfsReadyUnreadableMsg = "could not check which boxes can run a rootfs app. try again"
// rootfsReleasesUnreadableMsg is RFS-4-C08.
rootfsReleasesUnreadableMsg = "could not read this app's releases. try again"
+ // rootfsSeededMsg is RFS-4-C09, filled with the app.
+ rootfsSeededMsg = "%s is a rootfs app, and a new rootfs instance cannot start from a copy of another database yet. create it empty, then load the data from your app"
+ // rootfsReplicaMsg is RFS-4-C10, filled with the app.
+ rootfsReplicaMsg = "%s is a rootfs app, and a rootfs app has no replicas yet"
)
// rootfsHead is what the release an instance would be created from says
@@ -179,3 +185,89 @@ func (s *Server) rootfsRegionName(ctx context.Context, region string) string {
func boxRootfsReady(b status.Box) bool {
return b.Metrics != nil && b.Metrics.RootfsReady
}
+
+// rootfsCreateRefusal is the create door's refusals for a socket-backed
+// instance of an app whose creating release is rootfs (Plan 4 Task 3), in
+// this order: a seeded create (C09: copying a database into a new rootfs
+// instance is not in v1, and the seed doors write their own records, so
+// they are never reached), a replica (C10: one box per instance, spec §2),
+// then the placement (C01, C02) and a ready box there (C05, C06, C07).
+// region is the record's region, which is placed itself for a placed app.
+//
+// Claim consoles (row 3.6, accepted): with cfg.HostClaim the door's
+// chooser picks a box without asking about rootfs readiness, but a box with
+// claim control armed publishes rootfs_ready false, so C05 or C06 refuses
+// the create here first.
+func (s *Server) rootfsCreateRefusal(ctx context.Context, reg consoleRegistry, acct *Account, app, placed, region, pool, role string, seeded bool) *doorRefusal {
+ switch {
+ case seeded:
+ return &doorRefusal{http.StatusUnprocessableEntity, fmt.Sprintf(rootfsSeededMsg, app)}
+ case role == "replica":
+ return &doorRefusal{http.StatusUnprocessableEntity, fmt.Sprintf(rootfsReplicaMsg, app)}
+ }
+ if ref := s.rootfsPlacementRefusal(reg, acct, app, placed, pool); ref != nil {
+ return ref
+ }
+ return s.rootfsReadyRefusal(ctx, reg, placed, region)
+}
+
+// The first-kind boundary (Astra R2 finding 2; Plan 4 row 3.2). The create
+// door reads the app's kind before any lock. When no release and no kind
+// marker fixed it yet (rootfsHead.Unfixed), a first rootfs ship may fix it
+// rootfs (its marker, release.EstablishFirstKind) between that read and the
+// record write, and the door would mint a binary record for a rootfs app:
+// permanent (§5.0), and never served. So the door marks its request context
+// with withUnfixedFirstKind, and every console writer of an instance record
+// (createRecordMarked, createRecordWithLifetimeLocked; the seed and import
+// doors write through the same two) asks firstKindRecheck while holding the
+// app's lock (s.lockApp), before it writes anything. A first ship that fixed
+// the kind before the writer took the lock is seen there and the create
+// refuses with the door's try-again sentence, writing nothing; the retry
+// reads the marker and mints rootfs. A first ship that takes the lock after
+// the write sees the record and counts it (Plan 4 Task 5).
+//
+// The door does not hold the lock itself: every writer already takes it, the
+// lock does not re-enter, and the account lock (planLock) must come first.
+// An app whose kind is fixed is never marked: its kind cannot change.
+//
+// Claim consoles (row 3.6, accepted): the claim path (beginClaimCreate,
+// finishClaimCreate) writes its record without the app lock, so the
+// boundary does not hold there. Claims are shelved, and a box with claim
+// control armed publishes rootfs_ready false, so no rootfs instance can be
+// created on such a console anyway.
+
+// unfixedFirstKind names the app a create door found unfixed.
+type unfixedFirstKind struct{ acct, app string }
+
+type unfixedFirstKindKey struct{}
+
+// withUnfixedFirstKind marks ctx as a create of acct/app whose door read no
+// release and no kind marker.
+func withUnfixedFirstKind(ctx context.Context, acct, app string) context.Context {
+ return context.WithValue(ctx, unfixedFirstKindKey{}, unfixedFirstKind{acct, app})
+}
+
+// errFirstKindFixedRootfs is firstKindRecheck's refusal: a first rootfs ship
+// fixed the app's kind after the door read it. The writers return it before
+// any write; the doors answer it with their try-again sentence.
+var errFirstKindFixedRootfs = errors.New("console: a first rootfs ship fixed the app's kind after the create door read it, so this binary record was not written")
+
+// firstKindRecheck is the boundary's check, made by a record writer holding
+// rec's app lock: for a socket-backed record with no runtime, created
+// through a door that found its app unfixed, the app's kind read again. Now
+// rootfs refuses (errFirstKindFixedRootfs); a kind it cannot read refuses
+// too (fail closed). Every other write passes with no read.
+func (s *Server) firstKindRecheck(ctx context.Context, rec provision.Record) error {
+ door, ok := ctx.Value(unfixedFirstKindKey{}).(unfixedFirstKind)
+ if !ok || door != (unfixedFirstKind{rec.Account, rec.App}) || rec.Runtime != "" || !rec.DerivesPaths() {
+ return nil
+ }
+ info, err := release.EstablishedKind(ctx, s.cfg.Releases, rec.Account, rec.App)
+ if err != nil {
+ return fmt.Errorf("console: read %s/%s's kind again under the app lock: %w", rec.Account, rec.App, err)
+ }
+ if info.Kind == release.KindRootfs {
+ return errFirstKindFixedRootfs
+ }
+ return nil
+}
diff --git a/internal/console/rootfs_test.go b/internal/console/rootfs_test.go
index 8ae3aee72..76b97cce4 100644
--- a/internal/console/rootfs_test.go
+++ b/internal/console/rootfs_test.go
@@ -2,14 +2,18 @@ package console
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"net/http"
"strings"
+ "sync"
+ "sync/atomic"
"testing"
"time"
"github.com/carlosframework/platform/internal/geo"
+ "github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/status"
@@ -71,6 +75,13 @@ func refStatus(ref *doorRefusal) int {
// registry, the owned fleets home and away (through the real door), and a
// fleet theirs that belongs to another account.
func rootfsDoorWorld(t *testing.T, code string) (*consoleFixture, *registry.Registry) {
+ t.Helper()
+ fx, _, reg := rootfsDoorWorldToken(t, code)
+ return fx, reg
+}
+
+// rootfsDoorWorldToken is rootfsDoorWorld with the owner's CLI token.
+func rootfsDoorWorldToken(t *testing.T, code string) (*consoleFixture, string, *registry.Registry) {
t.Helper()
fx, token := fleetFixture(t, code)
if _, err := fx.st.CreateAppIn(OpsAccountID, "shop", "paul@keymail.dev", time.Now()); err != nil {
@@ -87,7 +98,7 @@ func rootfsDoorWorld(t *testing.T, code string) (*consoleFixture, *registry.Regi
if _, err := reg.CreateFleet(registry.Fleet{Name: "theirs", Account: "zzz", Bucket: "theirs-data"}); err != nil {
t.Fatal(err)
}
- return fx, reg
+ return fx, token, reg
}
// fleetFailReg is the registry with GetFleet and ListFleetBoxes failing.
@@ -299,3 +310,423 @@ func TestRootfsReadyRefusesWhenItCannotLook(t *testing.T) {
}
}
}
+
+// ---- Plan 4 Task 3: instance create mints rootfs records ----
+
+const rootfsShopHost = "shop.example.com"
+
+// rootfsShopManifest is shop's instances manifest (row 2.9): no pool, no
+// seed, hosts under example.com, following channel edge.
+func rootfsShopManifest() provision.Manifest {
+ return provision.Manifest{Schema: 1, App: "shop", Domain: "example.com", Channel: "edge"}
+}
+
+// rootfsCreateWorld is rootfsDoorWorld with shop's instances enabled from
+// m, and the owner's token.
+func rootfsCreateWorld(t *testing.T, code string, m provision.Manifest) (*consoleFixture, string, *registry.Registry) {
+ t.Helper()
+ fx, token, reg := rootfsDoorWorldToken(t, code)
+ enableInstances(t, fx, "shop", m)
+ return fx, token, reg
+}
+
+// placeShop places shop on fleet through the place door. A rootfs app is
+// placed before its first ship: the door refuses a rootfs-kind app (C24).
+func placeShop(t *testing.T, fx *consoleFixture, token, fleet string) {
+ t.Helper()
+ if code, _, raw := rawReq(t, http.MethodPost, placeURL(fx, "shop"), token, `{"target":"fleet/`+fleet+`"}`); code != http.StatusOK {
+ t.Fatalf("place shop on %s = %d %s", fleet, code, raw)
+ }
+}
+
+// fleetBoxNow registers label on fleet, active, with a fresh heartbeat.
+func fleetBoxNow(t *testing.T, fx *consoleFixture, reg *registry.Registry, fleet, label string, ready bool) {
+ t.Helper()
+ registerFleetBox(t, reg, fleet, label)
+ writeFleetBox(t, fx, fleet, label, "", ready, time.Now())
+}
+
+// shipShop ships rootfs release r1 of shop and points channel edge at it.
+func shipShop(t *testing.T, fx *consoleFixture, hibernate bool) {
+ t.Helper()
+ shipRootfsRelease(t, fx, "shop", "r1", hibernate)
+ seedChannel(t, fx, "shop", "edge", "r1")
+}
+
+// placedRootfsApp is shop placed on fleet home with one box (ready or not),
+// manifest m, and rootfs r1 on edge.
+func placedRootfsApp(t *testing.T, code string, m provision.Manifest, ready, hibernate bool) (*consoleFixture, string) {
+ t.Helper()
+ fx, token, reg := rootfsCreateWorld(t, code, m)
+ placeShop(t, fx, token, "home")
+ fleetBoxNow(t, fx, reg, "home", "pi-1", ready)
+ shipShop(t, fx, hibernate)
+ return fx, token
+}
+
+// rootfsReadyFleetApp is shop on fleet home with a ready box and rootfs r1
+// on edge.
+func rootfsReadyFleetApp(t *testing.T, code string, hibernate bool) (*consoleFixture, string) {
+ t.Helper()
+ return placedRootfsApp(t, code, rootfsShopManifest(), true, hibernate)
+}
+
+func readyApp(t *testing.T) (*consoleFixture, string) {
+ return rootfsReadyFleetApp(t, "code-rfs-ready-app", false)
+}
+
+func notReadyFleetApp(t *testing.T) (*consoleFixture, string) {
+ return placedRootfsApp(t, "code-rfs-not-ready", rootfsShopManifest(), false, false)
+}
+
+func pooledRootfsApp(t *testing.T) (*consoleFixture, string) {
+ m := rootfsShopManifest()
+ m.Pool = "gold"
+ return placedRootfsApp(t, "code-rfs-pooled", m, true, false)
+}
+
+// unplacedRootfsApp is shop with no placement and rootfs r1 on edge.
+func unplacedRootfsApp(t *testing.T) (*consoleFixture, string) {
+ t.Helper()
+ fx, token, _ := rootfsCreateWorld(t, "code-rfs-unplaced", rootfsShopManifest())
+ shipShop(t, fx, false)
+ return fx, token
+}
+
+func createShop(t *testing.T, fx *consoleFixture, token, body string) (int, string) {
+ t.Helper()
+ code, _, raw := rawReq(t, http.MethodPost, instancesURL(fx, fx.sqid, "shop"), token, body)
+ return code, raw
+}
+
+// storedShop reads shop's record of host, and its stored JSON's keys.
+func storedShop(t *testing.T, fx *consoleFixture, host string) (*provision.Record, map[string]json.RawMessage) {
+ t.Helper()
+ rec, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "shop", host)
+ if err != nil {
+ t.Fatalf("read %s: %v", host, err)
+ }
+ raw, _, err := fx.rel.Get(context.Background(), provision.RecordKey(fx.sqid, "shop", host))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var keys map[string]json.RawMessage
+ if err := json.Unmarshal(raw, &keys); err != nil {
+ t.Fatal(err)
+ }
+ return rec, keys
+}
+
+func noShopRecord(t *testing.T, fx *consoleFixture, host string) {
+ t.Helper()
+ if _, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "shop", host); err == nil {
+ t.Fatalf("a refused create wrote %s", host)
+ }
+}
+
+func TestRootfsCreateMintsRuntimeAndAlwaysOn(t *testing.T) {
+ fx, token := rootfsReadyFleetApp(t, "code-rfs-create", false)
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com","channel":"edge"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ rec, _ := storedShop(t, fx, rootfsShopHost)
+ if rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || *rec.Hibernate {
+ t.Fatalf("stored record %+v; want rootfs-v1 with an explicit hibernate false", rec)
+ }
+ if rec.Region != "fleet/home" || rec.HA {
+ t.Fatalf("region %q, HA %v", rec.Region, rec.HA)
+ }
+}
+
+func TestRootfsCreateTakesHibernateFromTheRelease(t *testing.T) {
+ fx, token := rootfsReadyFleetApp(t, "code-rfs-create-hib", true)
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ if rec, _ := storedShop(t, fx, rootfsShopHost); rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || !*rec.Hibernate {
+ t.Fatalf("stored record %+v; want rootfs-v1 with hibernate true", rec)
+ }
+}
+
+func TestRootfsCreateRefusals(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ setup func(t *testing.T) (*consoleFixture, string)
+ body string
+ status int
+ want string
+ }{
+ {"unplaced on a shared deployment", unplacedRootfsApp, `{"host":"shop.example.com"}`, http.StatusConflict, fmt.Sprintf(rootfsNotOwnBoxesMsg, "shop")},
+ {"an explicit region on a shared deployment", unplacedRootfsApp, `{"host":"shop.example.com","region":"uk"}`, http.StatusConflict, fmt.Sprintf(rootfsNotOwnBoxesMsg, "shop")},
+ {"pooled manifest", pooledRootfsApp, `{"host":"shop.example.com"}`, http.StatusConflict, fmt.Sprintf(rootfsPoolMsg, "shop")},
+ {"no ready box on the fleet", notReadyFleetApp, `{"host":"shop.example.com"}`, http.StatusConflict, fmt.Sprintf(rootfsFleetNotReadyMsg, "home")},
+ {"seeded from a sibling", readyApp, `{"host":"shop.example.com","from":{"host":"www.example.com"}}`, http.StatusUnprocessableEntity, fmt.Sprintf(rootfsSeededMsg, "shop")},
+ {"imported", readyApp, `{"host":"shop.example.com","import":{"sha256":"` + strings.Repeat("a", 64) + `","bytes":1}}`, http.StatusUnprocessableEntity, fmt.Sprintf(rootfsSeededMsg, "shop")},
+ // Row 3.5: a fleet-placed app's replica is refused first by the
+ // door's own 400, so C10 is reached on an unplaced app, where it
+ // comes before C01.
+ {"replica", unplacedRootfsApp, `{"host":"shop.example.com","role":"replica","region":"uk"}`, http.StatusUnprocessableEntity, fmt.Sprintf(rootfsReplicaMsg, "shop")},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ fx, token := tc.setup(t)
+ code, out := createShop(t, fx, token, tc.body)
+ if code != tc.status || errorText(out) != tc.want {
+ t.Fatalf("create = %d %s; want %d %q", code, out, tc.status, tc.want)
+ }
+ noShopRecord(t, fx, rootfsShopHost)
+ })
+ }
+}
+
+// D9 and spec §5.7: a single-tenant deployment allows any box, but never a
+// pool.
+func TestRootfsCreateOnASingleTenantDeployment(t *testing.T) {
+ fx, token := unplacedRootfsApp(t)
+ fx.srv.cfg.DeploymentSingleTenant = true
+ writeFleetBox(t, fx, "", "box-1", "", true, time.Now())
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ if rec, _ := storedShop(t, fx, rootfsShopHost); rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || *rec.Hibernate || rec.Region != "" {
+ t.Fatalf("stored record %+v", rec)
+ }
+
+ fx, token, _ = rootfsCreateWorld(t, "code-rfs-single-pool", func() provision.Manifest {
+ m := rootfsShopManifest()
+ m.Pool = "gold"
+ return m
+ }())
+ shipShop(t, fx, false)
+ fx.srv.cfg.DeploymentSingleTenant = true
+ writeFleetBox(t, fx, "", "box-1", "", true, time.Now())
+ code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`)
+ if code != http.StatusConflict || errorText(out) != fmt.Sprintf(rootfsPoolMsg, "shop") {
+ t.Fatalf("pooled create = %d %s; want 409 C02", code, out)
+ }
+ noShopRecord(t, fx, rootfsShopHost)
+}
+
+// Row 3.3: a rootfs record is never born HA (one box per instance, C7),
+// even on a single-tenant deployment whose home is an HA region, where the
+// same console stamps a binary instance HA.
+func TestRootfsCreateIsNeverBornHA(t *testing.T) {
+ fx, token := unplacedRootfsApp(t)
+ fx.srv.cfg.DeploymentSingleTenant = true
+ fx.srv.cfg.HomeRegion = "ie"
+ fx.srv.cfg.HARegions = []string{"ie"}
+ writeFleetBox(t, fx, "", "box-1", "ie", true, time.Now())
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ if rec, keys := storedShop(t, fx, rootfsShopHost); rec.Runtime != provision.RuntimeRootfsV1 || rec.HA || keys["ha"] != nil {
+ t.Fatalf("stored record %+v; want rootfs-v1 and not HA", rec)
+ }
+ // The control: kass, a binary app, in the same HA home.
+ enableInstances(t, fx, "kass", provision.Manifest{Schema: 1, App: "kass", Domain: "example.com", Channel: "stable"})
+ if code, _, out := rawReq(t, http.MethodPost, instancesURL(fx, fx.sqid, "kass"), token, `{"host":"kass.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("kass create = %d %s", code, out)
+ }
+ if rec, err := provision.GetRecord(context.Background(), fx.rel, fx.sqid, "kass", "kass.example.com"); err != nil || !rec.HA || rec.Runtime != "" {
+ t.Fatalf("kass record %+v, %v; want binary and HA", rec, err)
+ }
+}
+
+// shipBinaryRelease ships binary release v1 of shop and points edge at it.
+func shipBinaryRelease(t *testing.T, fx *consoleFixture) {
+ t.Helper()
+ m := release.Manifest{Schema: 1, App: "shop", Version: "v1", Kind: "binary", BuiltAt: "2026-10-24T10:00:00Z",
+ Artifacts: map[string]release.Artifact{"linux-amd64": {File: "shop"}}}
+ if err := release.Ship(context.Background(), fx.rel, fx.sqid, m, map[string][]byte{"linux-amd64": []byte("binary v1")}); err != nil {
+ t.Fatal(err)
+ }
+ seedChannel(t, fx, "shop", "edge", "v1")
+}
+
+// A binary app's records are minted exactly as before: no runtime and no
+// hibernate key in the stored JSON, placed or not.
+func TestBinaryCreateIsUnchanged(t *testing.T) {
+ for _, placed := range []bool{false, true} {
+ t.Run(fmt.Sprintf("placed=%v", placed), func(t *testing.T) {
+ fx, token, _ := rootfsCreateWorld(t, "code-rfs-binary-create", rootfsShopManifest())
+ if placed {
+ placeShop(t, fx, token, "home")
+ }
+ shipBinaryRelease(t, fx)
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ rec, keys := storedShop(t, fx, rootfsShopHost)
+ if rec.Runtime != "" || rec.Hibernate != nil || keys["runtime"] != nil || keys["hibernate"] != nil {
+ t.Fatalf("stored record %+v (keys %v); want no runtime and no hibernate", rec, keys)
+ }
+ })
+ }
+}
+
+// A static record of a rootfs app is not a process the platform starts: it
+// is minted plain, and none of the rootfs refusals apply (this app is
+// unplaced on a shared deployment, which C01 refuses for an instance).
+func TestRootfsCreateStaticRecordStaysPlain(t *testing.T) {
+ fx, token := unplacedRootfsApp(t)
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com","kind":"static"}`); code != http.StatusCreated {
+ t.Fatalf("static create = %d %s", code, out)
+ }
+ rec, keys := storedShop(t, fx, rootfsShopHost)
+ if rec.Kind != "static" || rec.Runtime != "" || keys["runtime"] != nil || keys["hibernate"] != nil {
+ t.Fatalf("stored record %+v; want a plain static record", rec)
+ }
+}
+
+// Plans-caps 3b and 4c leave always-on rootfs instances alone (row 2.5):
+// with a plan catalog on, the create still mints hibernate false.
+func TestRootfsCreateUnderPlansMintsAlwaysOn(t *testing.T) {
+ fx, token := rootfsReadyFleetApp(t, "code-rfs-create-plans", false)
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ ops, _ := fx.st.AccountBySqid(fx.sqid)
+ if _, err := fx.st.SetAccountPlan(ops.ID, "personal", nil, PlanSourceOperator, nil, "r", "ops@example.com", planNow); err != nil {
+ t.Fatal(err)
+ }
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ if rec, _ := storedShop(t, fx, rootfsShopHost); rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || *rec.Hibernate {
+ t.Fatalf("stored record %+v", rec)
+ }
+}
+
+// establishShopRootfs fixes shop's kind rootfs as a first ship does before
+// it publishes: the marker only, no manifest.
+func establishShopRootfs(t *testing.T, fx *consoleFixture) {
+ t.Helper()
+ m := release.Manifest{Schema: 1, App: "shop", Version: "r1", Kind: release.KindRootfs,
+ Artifacts: map[string]release.Artifact{release.RootfsArtifactKey: {File: release.RootfsArtifactFile, SHA256: strings.Repeat("a", 64), Size: 1}},
+ Processes: map[string]release.Process{release.ProcessWeb: {Command: []string{"/rails/bin/web"}}}}
+ if err := release.EstablishFirstKind(context.Background(), fx.rel, fx.sqid, &m); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// Astra R2 finding 2, the door's half: a first ship that has fixed the
+// kind and not yet published decides the create, never binary.
+func TestCreateHonoursTheMarkerOfAnUnpublishedFirstShip(t *testing.T) {
+ fx, token, reg := rootfsCreateWorld(t, "code-rfs-marker-placed", rootfsShopManifest())
+ placeShop(t, fx, token, "home")
+ fleetBoxNow(t, fx, reg, "home", "pi-1", true)
+ establishShopRootfs(t, fx)
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("create = %d %s", code, out)
+ }
+ if rec, _ := storedShop(t, fx, rootfsShopHost); rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || *rec.Hibernate {
+ t.Fatalf("stored record %+v", rec)
+ }
+
+ fx, token, _ = rootfsCreateWorld(t, "code-rfs-marker-unplaced", rootfsShopManifest())
+ establishShopRootfs(t, fx)
+ code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`)
+ if code != http.StatusConflict || errorText(out) != fmt.Sprintf(rootfsNotOwnBoxesMsg, "shop") {
+ t.Fatalf("unplaced create = %d %s; want 409 C01", code, out)
+ }
+ noShopRecord(t, fx, rootfsShopHost)
+}
+
+// markerReadStore counts reads of one key.
+type markerReadStore struct {
+ release.Store
+ key string
+ n atomic.Int32
+}
+
+func (s *markerReadStore) Get(ctx context.Context, key string) ([]byte, string, error) {
+ if key == s.key {
+ s.n.Add(1)
+ }
+ return s.Store.Get(ctx, key)
+}
+
+// raceTheFirstShip holds shop's app lock as a first rootfs ship does, runs
+// create (a request to the door) until it has read shop's kind and is
+// waiting for the lock, then writes the rootfs kind marker under the lock
+// and lets go. It returns what create answered.
+func raceTheFirstShip(t *testing.T, fx *consoleFixture, create func() string) string {
+ t.Helper()
+ reads := &markerReadStore{Store: fx.srv.cfg.Releases, key: release.AppKindMarkerKey(fx.sqid, "shop")}
+ fx.srv.cfg.Releases = reads
+ t.Cleanup(func() { fx.srv.cfg.Releases = reads.Store })
+ waiting := make(chan struct{})
+ var once sync.Once
+ h := func(kind, key, event string) {
+ if kind == "app" && key == fx.sqid+"/shop" && event == lockWaiting && reads.n.Load() > 0 {
+ once.Do(func() { close(waiting) })
+ }
+ }
+ lifetimeLockHook.Store(&h)
+ t.Cleanup(func() { lifetimeLockHook.Store(nil) })
+ unlock := fx.srv.lockApp(fx.sqid, "shop")
+ answered := make(chan string, 1)
+ go func() { answered <- create() }()
+ select {
+ case <-waiting:
+ case <-time.After(10 * time.Second):
+ unlock()
+ t.Fatalf("the create never waited for the app lock after reading the kind; it answered %s", <-answered)
+ }
+ putKindMarker(t, fx, release.KindRootfs)
+ unlock()
+ lifetimeLockHook.Store(nil)
+ return <-answered
+}
+
+// Row 3.2, Review Focus: a first rootfs ship that fixes an unfixed app's
+// kind after the create door read it, and before the record writer took
+// the app lock, never gets a binary record of a rootfs app. The writer
+// reads the kind again under its lock, refuses with the door's try-again
+// sentence and writes nothing; the retry mints rootfs. Both writers: the
+// lifetimes-off create and the lifetime create.
+func TestCreateRechecksTheFirstKindUnderTheWritersLock(t *testing.T) {
+ for _, lifetimes := range []bool{false, true} {
+ t.Run(fmt.Sprintf("lifetimes=%v", lifetimes), func(t *testing.T) {
+ var clock *lifetimesClock
+ if lifetimes {
+ clock = useLifetimesClock(t)
+ }
+ fx, token, reg := rootfsCreateWorld(t, "code-rfs-race", rootfsShopManifest())
+ placeShop(t, fx, token, "home")
+ fleetBoxNow(t, fx, reg, "home", "pi-1", true)
+ if lifetimes {
+ turnLifetimesOn(t, fx.srv, clock)
+ }
+ got := raceTheFirstShip(t, fx, func() string {
+ code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`)
+ return fmt.Sprintf("%d %s", code, errorText(out))
+ })
+ if want := fmt.Sprintf("%d %s", http.StatusBadGateway, "could not create the instance — try again"); got != want {
+ t.Fatalf("create = %s, want %s", got, want)
+ }
+ noShopRecord(t, fx, rootfsShopHost)
+ if code, out := createShop(t, fx, token, `{"host":"shop.example.com"}`); code != http.StatusCreated {
+ t.Fatalf("retry = %d %s", code, out)
+ }
+ if rec, _ := storedShop(t, fx, rootfsShopHost); rec.Runtime != provision.RuntimeRootfsV1 || rec.Hibernate == nil || !*rec.Hibernate {
+ t.Fatalf("retry stored %+v; want rootfs-v1 with the marker's hibernate true", rec)
+ }
+ })
+ }
+}
+
+// The seed doors write through the same writers, so a --from create of an
+// unfixed app that a first rootfs ship overtakes writes nothing either, and
+// its retry is the rootfs refusal C09.
+func TestSeedCreateRechecksTheFirstKindUnderTheWritersLock(t *testing.T) {
+ f := newFromFixture(t, 1)
+ got := raceTheFirstShip(t, f.fx, func() string {
+ code, out := f.create(t, fromBody)
+ return fmt.Sprintf("%d %v", code, out["error"])
+ })
+ if !strings.HasPrefix(got, fmt.Sprint(http.StatusBadGateway)+" ") {
+ t.Fatalf("seed create = %s, want 502", got)
+ }
+ noShopRecord(t, f.fx, "canary.shop.example.com")
+ if code, out := f.create(t, fromBody); code != http.StatusUnprocessableEntity || out["error"] != fmt.Sprintf(rootfsSeededMsg, "shop") {
+ t.Fatalf("retry = %d %v; want 422 C09", code, out)
+ }
+}
diff --git a/internal/console/seedmove.go b/internal/console/seedmove.go
index 8458834dd..acfe29394 100644
--- a/internal/console/seedmove.go
+++ b/internal/console/seedmove.go
@@ -40,9 +40,12 @@ func (s *Server) haRegion(region string) bool {
// shouldStampHA is the one place a create decides a record is born HA: an
// instance-kind record with no role, in an HA region. Static records and
// replicas are never stamped: boxes ignore non-instance kinds, and a
-// stamped static would make its app's moves and places refuse.
+// stamped static would make its app's moves and places refuse. A record
+// with a runtime is never stamped either (Plan 4 row 3.3): a rootfs instance
+// runs on one box (spec C7), even on a single-tenant deployment whose region
+// is in CONSOLE_HA_REGIONS.
func (s *Server) shouldStampHA(rec provision.Record) bool {
- return rec.KindOrDefault() == "instance" && rec.Role == "" && s.haRegion(rec.Region)
+ return rec.KindOrDefault() == "instance" && rec.Role == "" && rec.Runtime == "" && s.haRegion(rec.Region)
}
// haMoveSentence is RHA-P1a-C1 (approved): name is the host's region.