Managed fleets 1b task 1: the record carries a box and an assignment time
Record gains FleetBox, FleetBoxAt and FleetBoxMoved. SetRecordFleetBox writes them only while the record's Region, FleetBox and CreatedAt equal the caller's expectation, checked inside the update; otherwise ErrFleetBoxMoved. A relabel from one non-empty label to another sets FleetBoxMoved, which is never cleared. SetRecordRegion clears the box and its time when the region changes. Census: SetRecordFleetBox joins the typed-writer names, new overlay managedFleets1bWriteCensusInventory, a rejection witness.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
5 files changed,
+307
−2
internal/provision/fleetbox.go+48 −0internal/provision/fleetbox_test.go+201 −0internal/provision/managed_fleets_1b_census_test.go+42 −0internal/provision/plan9_b8_census_test.go+3 −2internal/provision/provision.go+13 −0
CI failed — run details
diff --git a/internal/provision/fleetbox.go b/internal/provision/fleetbox.go| new file mode 100644 |
| index 000000000..075b239fa |
| --- /dev/null |
| +++ b/internal/provision/fleetbox.go |
| @@ -0,0 +1,48 @@ |
| +package provision |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// ErrFleetBoxMoved is SetRecordFleetBox's refusal: the record is no longer |
| +// the one the caller decided about. Its region, its box label or its |
| +// creation time differs from the caller's expectation, so the label the |
| +// caller chose is not written. |
| +var ErrFleetBoxMoved = errors.New("provision: the record changed since the fleet box was chosen") |
| + |
| +// FleetBoxExpect is what a caller read before choosing a box: the record's |
| +// Region and FleetBox as read ("" when unlabelled), and its CreatedAt, which |
| +// tells a record apart from a later one created at the same host. |
| +type FleetBoxExpect struct { |
| + Region, Label string |
| + CreatedAt time.Time |
| +} |
| + |
| +// SetRecordFleetBox assigns the instance to the fleet box named label, at |
| +// the time at (stored as RFC 3339 nanoseconds in UTC). The write happens only |
| +// while the current record's Region, FleetBox and CreatedAt equal want; the |
| +// check runs inside each attempt of the update, so a lost race that is |
| +// retried is judged against the record the retry read. Otherwise it returns |
| +// ErrFleetBoxMoved and writes nothing. |
| +// |
| +// A relabel from one non-empty label to a different non-empty label also sets |
| +// FleetBoxMoved, which nothing here ever clears. The first labelling (from |
| +// "") leaves it false. |
| +func SetRecordFleetBox(ctx context.Context, s release.Store, acct, app, host string, want FleetBoxExpect, label string, at time.Time) error { |
| + _, err := updateRecord(ctx, s, acct, app, host, func(rec *Record) error { |
| + if rec.Region != want.Region || rec.FleetBox != want.Label || !rec.CreatedAt.Equal(want.CreatedAt) { |
| + return ErrFleetBoxMoved |
| + } |
| + if rec.FleetBox != "" && label != "" && rec.FleetBox != label { |
| + rec.FleetBoxMoved = true |
| + } |
| + rec.FleetBox = label |
| + rec.FleetBoxAt = at.UTC().Format(time.RFC3339Nano) |
| + return nil |
| + }) |
| + return err |
| +} |
diff --git a/internal/provision/fleetbox_test.go b/internal/provision/fleetbox_test.go| new file mode 100644 |
| index 000000000..b60e4681c |
| --- /dev/null |
| +++ b/internal/provision/fleetbox_test.go |
| @@ -0,0 +1,201 @@ |
| +package provision |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// fleetBoxFixture creates one fleet-placed record on a fresh store and |
| +// returns the store and the record as written. |
| +func fleetBoxFixture(t *testing.T) (release.Store, Record) { |
| + t.Helper() |
| + s := release.NewDirStore(t.TempDir()) |
| + rec := Record{ |
| + Schema: 1, Host: "web.shop.example.com", Slug: "web", App: "shop", Account: "acme", |
| + Pool: "shop", Region: "fleet/pi", Channel: "stable", |
| + CreatedAt: time.Date(2026, 10, 30, 9, 0, 0, 123456789, time.UTC), CreatedBy: "alice", |
| + } |
| + if err := CreateRecord(context.Background(), s, rec); err != nil { |
| + t.Fatalf("CreateRecord: %v", err) |
| + } |
| + return s, rec |
| +} |
| + |
| +func fleetBoxGet(t *testing.T, s release.Store) *Record { |
| + t.Helper() |
| + got, err := GetRecord(context.Background(), s, "acme", "shop", "web.shop.example.com") |
| + if err != nil { |
| + t.Fatalf("GetRecord: %v", err) |
| + } |
| + return got |
| +} |
| + |
| +func TestRecordFleetBoxRoundTrip(t *testing.T) { |
| + rec := Record{Schema: 1, Host: "h", FleetBox: "pi-1", FleetBoxAt: "2026-10-30T09:00:00.123456789Z", FleetBoxMoved: true} |
| + raw, err := json.Marshal(rec) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, key := range []string{`"fleet_box":"pi-1"`, `"fleet_box_at":"2026-10-30T09:00:00.123456789Z"`, `"fleet_box_moved":true`} { |
| + if !strings.Contains(string(raw), key) { |
| + t.Errorf("encoded record lacks %s: %s", key, raw) |
| + } |
| + } |
| + back, err := DecodeRecord(raw) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if back.FleetBox != rec.FleetBox || back.FleetBoxAt != rec.FleetBoxAt || !back.FleetBoxMoved { |
| + t.Fatalf("round trip = %+v, want the three fields back", back) |
| + } |
| + bare, err := json.Marshal(Record{Schema: 1, Host: "h"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if strings.Contains(string(bare), "fleet_box") { |
| + t.Fatalf("an unlabelled record writes a fleet_box key: %s", bare) |
| + } |
| +} |
| + |
| +func TestSetRecordFleetBoxIsConditional(t *testing.T) { |
| + ctx := context.Background() |
| + s, rec := fleetBoxFixture(t) |
| + at := time.Date(2026, 10, 30, 10, 0, 0, 5, time.FixedZone("x", 3600)) |
| + |
| + want := FleetBoxExpect{Region: "fleet/pi", Label: "", CreatedAt: rec.CreatedAt} |
| + if err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, "pi-1", at); err != nil { |
| + t.Fatalf("first label: %v", err) |
| + } |
| + got := fleetBoxGet(t, s) |
| + if got.FleetBox != "pi-1" || got.FleetBoxAt != at.UTC().Format(time.RFC3339Nano) { |
| + t.Fatalf("after label: box %q at %q", got.FleetBox, got.FleetBoxAt) |
| + } |
| + if got.Region != "fleet/pi" || got.Channel != "stable" || got.CreatedBy != "alice" || !got.CreatedAt.Equal(rec.CreatedAt) { |
| + t.Fatalf("label touched more than the box fields: %+v", got) |
| + } |
| + |
| + // The same stale expectation (label "") now loses: the record says pi-1. |
| + err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, "pi-2", at.Add(time.Hour)) |
| + if !errors.Is(err, ErrFleetBoxMoved) { |
| + t.Fatalf("stale want: err = %v, want ErrFleetBoxMoved", err) |
| + } |
| + if got := fleetBoxGet(t, s); got.FleetBox != "pi-1" { |
| + t.Fatalf("stale want changed the label to %q", got.FleetBox) |
| + } |
| + |
| + // A current expectation wins. |
| + want.Label = "pi-1" |
| + if err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, "pi-2", at.Add(time.Hour)); err != nil { |
| + t.Fatalf("current want: %v", err) |
| + } |
| + if got := fleetBoxGet(t, s); got.FleetBox != "pi-2" { |
| + t.Fatalf("label = %q, want pi-2", got.FleetBox) |
| + } |
| +} |
| + |
| +func TestSetRecordFleetBoxLosesToAPlacementChange(t *testing.T) { |
| + ctx := context.Background() |
| + s, rec := fleetBoxFixture(t) |
| + want := FleetBoxExpect{Region: "fleet/pi", CreatedAt: rec.CreatedAt} |
| + if err := SetRecordRegion(ctx, s, "acme", "shop", rec.Host, "ie"); err != nil { |
| + t.Fatal(err) |
| + } |
| + err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, "pi-1", time.Now()) |
| + if !errors.Is(err, ErrFleetBoxMoved) { |
| + t.Fatalf("err = %v, want ErrFleetBoxMoved", err) |
| + } |
| + if got := fleetBoxGet(t, s); got.FleetBox != "" || got.FleetBoxAt != "" { |
| + t.Fatalf("record labelled after a lost write: %q at %q", got.FleetBox, got.FleetBoxAt) |
| + } |
| +} |
| + |
| +func TestSetRecordFleetBoxLosesToARecreate(t *testing.T) { |
| + ctx := context.Background() |
| + s, rec := fleetBoxFixture(t) |
| + want := FleetBoxExpect{Region: "fleet/pi", CreatedAt: rec.CreatedAt} |
| + |
| + // The same host is deleted and created again under a new CreatedAt. |
| + if err := s.Delete(ctx, RecordKey("acme", "shop", rec.Host)); err != nil { |
| + t.Fatalf("delete: %v", err) |
| + } |
| + again := rec |
| + again.CreatedAt = rec.CreatedAt.Add(time.Minute) |
| + if err := CreateRecord(ctx, s, again); err != nil { |
| + t.Fatalf("recreate: %v", err) |
| + } |
| + err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, "pi-1", time.Now()) |
| + if !errors.Is(err, ErrFleetBoxMoved) { |
| + t.Fatalf("err = %v, want ErrFleetBoxMoved", err) |
| + } |
| + if got := fleetBoxGet(t, s); got.FleetBox != "" { |
| + t.Fatalf("recreated record labelled %q by a stale write", got.FleetBox) |
| + } |
| +} |
| + |
| +func TestMovedIsSetByABoxToBoxRelabelOnly(t *testing.T) { |
| + ctx := context.Background() |
| + s, rec := fleetBoxFixture(t) |
| + at := time.Date(2026, 10, 30, 10, 0, 0, 0, time.UTC) |
| + label := func(from, to string) { |
| + t.Helper() |
| + want := FleetBoxExpect{Region: "fleet/pi", Label: from, CreatedAt: rec.CreatedAt} |
| + if err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, to, at); err != nil { |
| + t.Fatalf("%q -> %q: %v", from, to, err) |
| + } |
| + at = at.Add(time.Minute) |
| + } |
| + label("", "pi-1") |
| + if got := fleetBoxGet(t, s); got.FleetBoxMoved { |
| + t.Fatal("the first labelling set FleetBoxMoved") |
| + } |
| + label("pi-1", "pi-2") |
| + if got := fleetBoxGet(t, s); !got.FleetBoxMoved { |
| + t.Fatal("a pi-1 to pi-2 relabel did not set FleetBoxMoved") |
| + } |
| + label("pi-2", "pi-1") |
| + if got := fleetBoxGet(t, s); !got.FleetBoxMoved || got.FleetBox != "pi-1" { |
| + t.Fatalf("after pi-2 to pi-1: moved %v box %q, want true and pi-1", got.FleetBoxMoved, got.FleetBox) |
| + } |
| + |
| + // A region change clears the label and time but leaves the flag as it is. |
| + if err := SetRecordRegion(ctx, s, "acme", "shop", rec.Host, "ie"); err != nil { |
| + t.Fatal(err) |
| + } |
| + got := fleetBoxGet(t, s) |
| + if got.FleetBox != "" || got.FleetBoxAt != "" || !got.FleetBoxMoved { |
| + t.Fatalf("after a region change: box %q at %q moved %v, want empty, empty, true", got.FleetBox, got.FleetBoxAt, got.FleetBoxMoved) |
| + } |
| +} |
| + |
| +func TestSetRecordRegionClearsTheBox(t *testing.T) { |
| + ctx := context.Background() |
| + s, rec := fleetBoxFixture(t) |
| + want := FleetBoxExpect{Region: "fleet/pi", CreatedAt: rec.CreatedAt} |
| + if err := SetRecordFleetBox(ctx, s, "acme", "shop", rec.Host, want, "pi-1", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + // The same region is no change: the box stays. |
| + if err := SetRecordRegion(ctx, s, "acme", "shop", rec.Host, "fleet/pi"); err != nil { |
| + t.Fatal(err) |
| + } |
| + got := fleetBoxGet(t, s) |
| + if got.FleetBox != "pi-1" || got.FleetBoxAt == "" { |
| + t.Fatalf("an unchanged region cleared the box: %q at %q", got.FleetBox, got.FleetBoxAt) |
| + } |
| + |
| + // A different region clears both fields. |
| + if err := SetRecordRegion(ctx, s, "acme", "shop", rec.Host, "ie"); err != nil { |
| + t.Fatal(err) |
| + } |
| + got = fleetBoxGet(t, s) |
| + if got.Region != "ie" || got.FleetBox != "" || got.FleetBoxAt != "" { |
| + t.Fatalf("after a region change: region %q box %q at %q", got.Region, got.FleetBox, got.FleetBoxAt) |
| + } |
| +} |
diff --git a/internal/provision/managed_fleets_1b_census_test.go b/internal/provision/managed_fleets_1b_census_test.go| new file mode 100644 |
| index 000000000..c9b61129f |
| --- /dev/null |
| +++ b/internal/provision/managed_fleets_1b_census_test.go |
| @@ -0,0 +1,42 @@ |
| +package provision |
| + |
| +// Record-write census entries for managed fleets 1b (branch managed-fleets-1b; |
| +// spec docs/superpowers/specs/2026-10-28-managed-fleets-design.md, 1b |
| +// addendum rule 6), layered on fleetPublicRoutesWriteCensusInventory. |
| +// Reviewed by the task author against the source. |
| +// |
| +// Task 1, one re-pin and one new unit: |
| +// - internal/provision/provision.go SetRecordRegion (record-writer): when |
| +// the region changes it also clears the record's FleetBox and |
| +// FleetBoxAt (a box is a place inside a region) and leaves |
| +// FleetBoxMoved as it is. Same updateRecord scaffold, same refusals. |
| +// - internal/provision/fleetbox.go SetRecordFleetBox (record-writer, new): |
| +// writes FleetBox and FleetBoxAt, and sets FleetBoxMoved on a relabel |
| +// from one non-empty label to another, only while the record's Region, |
| +// FleetBox and CreatedAt equal the caller's expectation (checked inside |
| +// the updateRecord mutation, so inside each retry); otherwise |
| +// ErrFleetBoxMoved. It reaches the store through updateRecord only. |
| +func managedFleets1bWriteCensusInventory() []censusEntry { |
| + entries := append([]censusEntry(nil), fleetPublicRoutesWriteCensusInventory()...) |
| + replacements := []censusEntry{ |
| + // Task 1: a region change clears the box; the same scaffold. |
| + {"internal/provision/provision.go", "SetRecordRegion", "record-writer", "key:SetRecordRegion,key:updateRecord", "f9daa5f51657cedf65d654f2c9386c240e6cd4ef1f5072a0a854da3bf437efad"}, |
| + // Task 1: the conditional box setter. |
| + {"internal/provision/fleetbox.go", "SetRecordFleetBox", "record-writer", "key:SetRecordFleetBox,key:updateRecord", "f2617c61887a841e7a1de2bafdbd1927f368b7451f2da349030ed98d0313906e"}, |
| + } |
| + for _, r := range replacements { |
| + found := false |
| + for i, e := range entries { |
| + if e.File == r.File && e.Unit == r.Unit { |
| + if e.Class != r.Class { |
| + panic("managed-fleets-1b re-pin changes the class of " + r.File + ":" + r.Unit) |
| + } |
| + entries[i], found = r, true |
| + } |
| + } |
| + if !found { |
| + entries = append(entries, r) |
| + } |
| + } |
| + return entries |
| +} |
diff --git a/internal/provision/plan9_b8_census_test.go b/internal/provision/plan9_b8_census_test.go| index 893794f4d..3127b67cb 100644 |
| --- a/internal/provision/plan9_b8_census_test.go |
| +++ b/internal/provision/plan9_b8_census_test.go |
| @@ -53,7 +53,7 @@ var censusMethods = map[string]bool{ |
| // typed-record-writer touchpoint. Keep these names aligned with the APIs above. |
| var ( |
| censusKeySuffixes = []string{"recordkey", "recordsprefix", "sidecarsprefix", "sidecarkey", "sidecarmanifestkey", "writerecord", "sequencedrecord"} |
| - censusKeyExact = map[string]bool{"createrecord": true, "updaterecord": true, "setrecordregion": true, "setrecordmove": true, "setrecordrun": true, "setrecordseed": true, "setrecordassetgen": true, "mintrecordassetgen": true, "createsidecarrecord": true, "claimrawupdaterecord": true} |
| + censusKeyExact = map[string]bool{"createrecord": true, "updaterecord": true, "setrecordregion": true, "setrecordmove": true, "setrecordrun": true, "setrecordseed": true, "setrecordfleetbox": true, "setrecordassetgen": true, "mintrecordassetgen": true, "createsidecarrecord": true, "claimrawupdaterecord": true} |
| censusLitWords = []string{"instances", "sidecars"} |
| ) |
| @@ -366,7 +366,7 @@ func TestPlan9B8RecordWriteCensus(t *testing.T) { |
| if len(got) < 100 { |
| t.Fatalf("census found only %d units under %s; the walk is broken", len(got), root) |
| } |
| - inventory := fleetPublicRoutesWriteCensusInventory() |
| + inventory := managedFleets1bWriteCensusInventory() |
| problems := append(censusDiff(inventory, got), censusPolicy(inventory)...) |
| if len(problems) > 0 { |
| t.Fatalf("record-write census rejected %d site(s); update the explicit inventory only after reviewing the source:\n %s", len(problems), strings.Join(problems, "\n ")) |
| @@ -421,6 +421,7 @@ func Reviewed(ctx context.Context, s Store, k string) error { |
| {"records prefix", base + "\nfunc Pref(a, b string) string { return provision.RecordsPrefix(a, b) }\n", "unlisted site x.go:Pref [key:RecordsPrefix]"}, |
| {"box callsite", base + "\nfunc Box(ctx context.Context, s Store, k string, b []byte) { provision.WriteRecord(ctx, s, k, nil, b, \"\") }\n", "unlisted site x.go:Box [key:WriteRecord]"}, |
| {"typed writer callsite", base + "\nfunc Typed(ctx context.Context, s Store) { provision.SetRecordMove(ctx, s, \"a\", \"b\", \"c\", nil) }\n", "unlisted site x.go:Typed [key:SetRecordMove]"}, |
| + {"fleet box writer callsite", base + "\nfunc Boxed(ctx context.Context, s Store) { provision.SetRecordFleetBox(ctx, s, \"a\", \"b\", \"c\", provision.FleetBoxExpect{}, \"pi-1\", time.Time{}) }\n", "unlisted site x.go:Boxed [key:SetRecordFleetBox]"}, |
| {"record path literal", base + "\nfunc Lit(h string) string { return \"apps/a/b/instances/\" + h + \".json\" }\n", "unlisted site x.go:Lit [lit:instances]"}, |
| {"sidecar path literal", base + "\nconst sidecarDir = \"sidecars\"\n", "unlisted site x.go:const sidecarDir [lit:sidecars]"}, |
| {"reflective name", base + "\nfunc Reflect(v any) { _ = reflect.ValueOf(v).MethodByName(\"Put\") }\n", "unlisted site x.go:Reflect [str:Put]"}, |
diff --git a/internal/provision/provision.go b/internal/provision/provision.go| index 1c1f147b0..67bfb04c3 100644 |
| --- a/internal/provision/provision.go |
| +++ b/internal/provision/provision.go |
| @@ -56,6 +56,16 @@ type Record struct { |
| CreatedAt time.Time `json:"created_at"` |
| CreatedBy string `json:"created_by,omitempty"` |
| + // FleetBox is the label of the fleet box this instance is assigned to, |
| + // FleetBoxAt when it was assigned (RFC 3339 nanoseconds, UTC), and |
| + // FleetBoxMoved whether a relabel ever took it from one box to another. |
| + // Written only through SetRecordFleetBox (fleetbox.go) and cleared, the |
| + // first two, by a region change in SetRecordRegion. Additive and |
| + // omitempty: every record written before them reads as unassigned. |
| + FleetBox string `json:"fleet_box,omitempty"` |
| + FleetBoxAt string `json:"fleet_box_at,omitempty"` |
| + FleetBoxMoved bool `json:"fleet_box_moved,omitempty"` |
| + |
| // HA marks a host born high-availability (region HA P1a): the console's |
| // create door stamps it when the record's region is one of |
| // CONSOLE_HA_REGIONS. Every box honours it, whatever its own |
| @@ -1032,6 +1042,9 @@ func SetRecordRegion(ctx context.Context, s release.Store, acct, app, host, regi |
| if rec.HA { |
| return &HAMoveError{Region: rec.Region} |
| } |
| + // A box is a place inside a region: another region, no box. |
| + // FleetBoxMoved stays as it is. |
| + rec.FleetBox, rec.FleetBoxAt = "", "" |
| } |
| rec.Region = region |
| return nil |