carlos / platform Public

console: the first rootfs ship counts pending lifetime allocations, and recovery never publishes a binary body into a rootfs app (Astra Task 5 r1 P1)

A lifetime create whose slot or record write failed leaves its binary
body durable in a pending allocation, which recovery publishes later.
firstRootfsShipRefusal now counts those allocations under the app lock
beside the records (C11 naming the host), and refuses with C79 when they
cannot be listed or a body does not decode. As a backstop, recovery
cancels an allocation of a binary instance whose record has not landed
when the app's established kind is rootfs, as it cancels one whose app is
gone (allocationLostFirstKind); a kind it cannot read leaves it pending.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 32e3ebae7c1ca8e0032b7f59cd9f74c7da38f622 parent f8c8e42
3 files changed, +168 −11
  • internal/console/lifetimes_recovery.go +42 −1
  • internal/console/rootfs.go +42 −10
  • internal/console/rootfs_test.go +84 −0
diff --git a/internal/console/lifetimes_recovery.go b/internal/console/lifetimes_recovery.go
index 9cfbdeb0f..676d682b0 100644
--- a/internal/console/lifetimes_recovery.go
+++ b/internal/console/lifetimes_recovery.go
@@ -2,6 +2,7 @@ package console
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"log"
@@ -138,7 +139,10 @@ func (s *Server) recoverOrphanLocked(ctx context.Context, r Retirement) {
// - the app lifetime that authorised the allocation is gone (no apps row,
// another lifetime, trashed, or the store's trash marker): the
// allocation's own slot is retired as an orphan cancelling it, in one
-// transaction; with no slot of its own it is void;
+// transaction; with no slot of its own it is void. So is an allocation
+// whose record has not landed when its body is a binary instance of an
+// app whose established kind is rootfs (allocationLostFirstKind); a
+// kind that cannot be read leaves it pending;
// - its slot and a record at N under this key: committed;
// - its slot and a stale or corrupt body: step 5 on the current ETag, then
// 6 and 7;
@@ -175,6 +179,22 @@ func (s *Server) recoverAllocationLocked(ctx context.Context, a Allocation) {
return
}
ours := allocationOwnsSlot(slot, a)
+ landed := ours && obs.Present && obs.Rec != nil && !obs.Rec.Retired && obs.Rec.Incarnation == a.Incarnation
+ if alive && !landed {
+ // The first-kind backstop (rootfs spec §5.0; Plan 4 Task 5): a
+ // binary body is never published into an app a rootfs first
+ // ship has fixed. Such an allocation is cancelled as one whose
+ // app is gone; a kind it cannot read leaves it pending.
+ lost, err := s.allocationLostFirstKind(ctx, a)
+ if err != nil {
+ s.lifetimeLogHourly(logKey, "console: lifetime recovery: allocation %s of %s: read the app's kind: %v (pending)", a.OpID, a.Host, err)
+ return
+ }
+ if lost {
+ s.lifetimeLogHourly(logKey, "console: lifetime recovery: allocation %s of %s is a binary instance of an app whose first release is rootfs; it is cancelled", a.OpID, a.Host)
+ alive = false
+ }
+ }
if !alive {
if ours {
if err := s.retireSlotOrphanLocked(ctx, a.Host, slot, obs.SlotETag, a.OpID); err != nil && !errors.Is(err, errLifetimeSettling) {
@@ -218,6 +238,27 @@ func (s *Server) recoverAllocationLocked(ctx context.Context, a Allocation) {
}
}
+// allocationLostFirstKind reports whether a's body is a binary instance
+// (binaryInstance) of an app whose established kind is rootfs: an instance
+// that can never serve (§5.0). The ship's first-kind slot counts pending
+// allocations and refuses (firstRootfsShipRefusal), so this is a backstop
+// for a kind fixed without that check. A body that does not decode, or a
+// kind that cannot be read, is an error, never an answer.
+func (s *Server) allocationLostFirstKind(ctx context.Context, a Allocation) (bool, error) {
+ var rec provision.Record
+ if err := json.Unmarshal(a.Body, &rec); err != nil {
+ return false, fmt.Errorf("decode its body: %w", err)
+ }
+ if !binaryInstance(rec) {
+ return false, nil
+ }
+ info, err := release.EstablishedKind(ctx, s.cfg.Releases, a.Account, a.App)
+ if err != nil {
+ return false, err
+ }
+ return info.Kind == release.KindRootfs, nil
+}
+
// allocationAppAlive reports whether the app lifetime that authorised a is
// still the app's: its apps row exists under the same lifetime, untrashed,
// and the store has no trash marker for it. A read that fails is an error,
diff --git a/internal/console/rootfs.go b/internal/console/rootfs.go
index dff477943..90b6c5bfa 100644
--- a/internal/console/rootfs.go
+++ b/internal/console/rootfs.go
@@ -2,9 +2,11 @@ package console
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"log"
+ "maps"
"net/http"
"slices"
"time"
@@ -315,24 +317,54 @@ func (s *Server) firstKindRecheck(ctx context.Context, rec provision.Record) err
// process the platform starts (DerivesPaths) and have no runtime, read
// with the strict judged listing the place door uses (placeRecords, row
// 5.2): a record that does not read could be one of them, so it refuses
-// with RFS-1-C79 at 503, fail closed. A tombstone is no instance. The
-// caller holds the app lock (fixFirstRootfsKind), so no record write lands
-// between this count and the kind marker.
+// with RFS-1-C79 at 503, fail closed. A tombstone is no instance.
+//
+// It counts the app's pending lifetime allocations too (Astra Task 5 r1
+// P1): a create whose slot or record write failed has its binary body
+// durable in the allocation, and recovery writes that body later, so it is
+// an instance made before the first release as much as a record is. A
+// body that does not decode, or allocations that cannot be listed, refuse
+// with C79 as well.
+//
+// The caller holds the app lock (fixFirstRootfsKind), so no record write
+// and no allocation lands between this count and the kind marker.
func (s *Server) firstRootfsShipRefusal(ctx context.Context, acct *Account, app string) *doorRefusal {
+ unchecked := func(what string, err error) *doorRefusal {
+ log.Printf("console: api ship %s/%s: count the instances made before the first release: %s: %v", acct.Sqid, app, what, err)
+ return &doorRefusal{http.StatusServiceUnavailable, shipKindUnchecked}
+ }
recs, err := placeRecords(ctx, s.cfg.Releases, provision.JudgeFrom(ctx), acct.Sqid, app)
if err != nil {
- log.Printf("console: api ship %s/%s: count the instances made before the first release: %v", acct.Sqid, app, err)
- return &doorRefusal{http.StatusServiceUnavailable, shipKindUnchecked}
+ return unchecked("records", err)
}
- var hosts []string
+ binary := map[string]bool{}
for _, rec := range recs {
- if !rec.Retired && rec.DerivesPaths() && rec.Runtime == "" {
- hosts = append(hosts, rec.Host)
+ if binaryInstance(rec) {
+ binary[rec.Host] = true
+ }
+ }
+ allocs, err := s.store.PendingLifetimesForApp(acct.Sqid, app)
+ if err != nil {
+ return unchecked("pending allocations", err)
+ }
+ for _, a := range allocs {
+ var rec provision.Record
+ if err := json.Unmarshal(a.Body, &rec); err != nil {
+ return unchecked("allocation "+a.OpID, err)
+ }
+ if binaryInstance(rec) {
+ binary[a.Host] = true
}
}
- if len(hosts) == 0 {
+ if len(binary) == 0 {
return nil
}
- slices.Sort(hosts)
+ hosts := slices.Sorted(maps.Keys(binary))
return &doorRefusal{http.StatusConflict, fmt.Sprintf(rootfsFirstShipMsg, app, len(hosts), hosts[0])}
}
+
+// binaryInstance reports whether rec is an instance the platform starts
+// (DerivesPaths) minted for a binary app: no runtime, and not a tombstone.
+func binaryInstance(rec provision.Record) bool {
+ return !rec.Retired && rec.DerivesPaths() && rec.Runtime == ""
+}
diff --git a/internal/console/rootfs_test.go b/internal/console/rootfs_test.go
index a3f9ac740..0e8d337fc 100644
--- a/internal/console/rootfs_test.go
+++ b/internal/console/rootfs_test.go
@@ -1171,3 +1171,87 @@ func firstRootfsShipWaitsForAnInFlightCreate(t *testing.T, lifetimes bool) {
}
shopFirstShipLeftNothing(t, fx, "r1")
}
+
+// ltShipReady readies l's console for rootfs ships through the door.
+func ltShipReady(t *testing.T, l ltOn) {
+ t.Helper()
+ liftShipBudget(l.fx)
+ scratch := filepath.Join(t.TempDir(), "scratch", "seeds")
+ if err := clearSeedScratch(scratch); err != nil {
+ t.Fatal(err)
+ }
+ l.fx.srv.seedScratch = newSeedScratch(scratch, func(string) (int64, error) { return 64 << 30, nil })
+}
+
+// Astra Task 5 r1 P1: a lifetime create whose slot or record write failed
+// is durable as a pending allocation with a binary body, and recovery
+// publishes that body later. The first rootfs ship counts it as an
+// instance made before the first release: C11 naming it, nothing written.
+// A restarted console's recovery then publishes the binary record, which
+// the next first ship counts in its turn.
+func TestFirstRootfsShipCountsAPendingAllocation(t *testing.T) {
+ for _, step := range []string{"slot", "record"} {
+ t.Run(step, func(t *testing.T) {
+ l := newLtOn(t)
+ ltShipReady(t, l)
+ l.pendingCreate(t, ltOne, step)
+ want := fmt.Sprintf(rootfsFirstShipMsg, ltApp, 1, ltOne)
+ status, body := postShip(t, shopShipURL(l.fx), l.member, shopRootfsShipParts(t, "r1", false))
+ if status != http.StatusConflict || body["error"] != want {
+ t.Fatalf("first ship over a pending allocation = %d %v; want 409 C11", status, body)
+ }
+ shopFirstShipLeftNothing(t, l.fx, "r1")
+
+ recoverOn(t, l.restart(t))
+ oneAlloc(t, l.fx.st, ltOne, lifetimeCommitted)
+ raw, _ := rawOf(t, l.fx.rel, ltApp, ltOne)
+ if rec := decodeRec(t, raw); rec.Runtime != "" || rec.Retired {
+ t.Fatalf("recovery published %+v; want the allocation's binary record", rec)
+ }
+ status, body = postShip(t, shopShipURL(l.fx), l.member, shopRootfsShipParts(t, "r1", false))
+ if status != http.StatusConflict || body["error"] != want {
+ t.Fatalf("first ship after recovery = %d %v; want 409 C11", status, body)
+ }
+ shopFirstShipLeftNothing(t, l.fx, "r1")
+ })
+ }
+}
+
+// The backstop: should an app's kind be fixed rootfs past the ship's
+// count (here the marker is written directly), recovery never publishes a
+// pending allocation's binary body into it. A restarted console's recovery
+// cancels the allocation instead, as it does one whose app is gone, and
+// the hostname is free again.
+func TestRecoveryCancelsABinaryAllocationOfARootfsApp(t *testing.T) {
+ for _, step := range []string{"slot", "record"} {
+ t.Run(step, func(t *testing.T) {
+ l := newLtOn(t)
+ l.pendingCreate(t, ltOne, step)
+ putKindMarker(t, l.fx, release.KindRootfs)
+ recoverOn(t, l.restart(t))
+ if raw, _ := rawOf(t, l.fx.rel, ltApp, ltOne); raw != nil {
+ if rec := decodeRec(t, raw); !rec.Retired {
+ t.Fatalf("recovery published %+v into a rootfs app", rec)
+ }
+ }
+ oneAlloc(t, l.fx.st, ltOne, lifetimeVoid)
+ })
+ }
+}
+
+// The allocation count fails closed: a pending allocation whose body does
+// not decode could be a binary instance, so the first ship is C79 at 503
+// and writes nothing.
+func TestFirstRootfsShipRefusesOnAnAllocationThatDoesNotRead(t *testing.T) {
+ l := newLtOn(t)
+ ltShipReady(t, l)
+ if err := l.fx.st.BeginAllocation(Allocation{OpID: "op-unreadable", Account: "bab", App: ltApp, Host: ltOne,
+ Incarnation: 1, Body: []byte("{not json")}); err != nil {
+ t.Fatal(err)
+ }
+ status, body := postShip(t, shopShipURL(l.fx), l.member, shopRootfsShipParts(t, "r1", false))
+ if status != http.StatusServiceUnavailable || body["error"] != shipKindUnchecked {
+ t.Fatalf("ship = %d %v; want 503 C79", status, body)
+ }
+ shopFirstShipLeftNothing(t, l.fx, "r1")
+}