carlos / platform Public

console: label a one-box fleet's records so stateless releases deploy

Fix stateless replicas Plan 1 dev smoke F8 (#511). First labelling was
behind the two-box activation gate, leaving a one-box fleet's bound
legacy records unlabelled and its stateless coordinator requests fenced.

Let the existing first-labelling path use a complete one-active-box view,
with all binding, lifetime, deployment, sidecar and destination checks.
Refresh that view after replica reconciliation so its inputs stay the
same. Keep create-time placement and moves behind the two-box gate.
The coordinator and provision writers remain unchanged.

Exercise the real relabel job and edge tunnel-host coordinator through a
complete release, refusal cases and a one-box fleet that must not move a
gone box's record. Verify the three guard mutations are detected. Pin
the added one-box inventory reads while preserving the two-box budget.
Co-Authored-By: Codex (gpt-6-astra) <noreply@openai.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev ad35f1a58cc7712b82f7b0485c2db29599839f0b parent 6209ce4
5 files changed, +260 −10
  • internal/console/fleetbox.go +11 −5
  • internal/console/fleetrelabel.go +16 −5
  • internal/console/fleetrelabel_test.go +24 −0
  • internal/console/replicacoordinator_test.go +203 −0
  • internal/console/replicaplacement_test.go +6 −0

Intent

Human

<task-notification> <task-id>b8yfh5gv7</task-id> <tool-use-id>toolu_0117Rca67rJTPsyX7F6WPuCG</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/195ef15b-aac6-42d6-b49b-41ffb7a4abbc/tasks/b8yfh5gv7.output</output-file> <status>completed</status> <summary>Background command "Wait for Astra Task 1a to finish or stall" completed (exit code 0)</summary> </task-notification>

Attributed to this commit: 11.3 g CO₂e (1,359,436 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on stateless-coordinator-oneboxfleet, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/console/fleetbox.go b/internal/console/fleetbox.go
index 6bc30ced3..c56a5ab33 100644
--- a/internal/console/fleetbox.go
+++ b/internal/console/fleetbox.go
@@ -44,8 +44,8 @@ import (
// Load counts assigned copies, or one for a legacy box label. Static
// records count toward neither (fleetLabelsRecord). Records is the
// inventory the view was built from (every record placed on the fleet,
-// static ones included), read only when the fleet has two or more active
-// boxes; the relabel worker and the drain act on it.
+// static ones included). The relabel pass requests this inventory even on
+// one box; request-time callers may skip it while labelling is inactive.
type fleetView struct {
Boxes []fleetBoxView
Active, HasSidecars, DeploymentCurrent bool
@@ -124,14 +124,20 @@ func (b fleetBoxView) destinationFor(rootfs bool) bool {
return !rootfs || (b.RootfsReady && b.AtRootfsFloor)
}
-// labelActive is the activation gate: the console labels this fleet's
-// records only when it has two or more active boxes and was read whole, has
+// labelActive is the create-time activation gate: the console labels new
+// records only when the fleet has two or more active boxes and was read whole, has
// no sidecars, the deployment and the console are at the label floor, and
// every record on it is one the platform can move.
func (v fleetView) labelActive() bool {
return v.Active && !v.HasSidecars && v.DeploymentCurrent && !v.Unmovable
}
+// First labelling follows an existing binding and needs no second box.
+// Create-time placement still uses labelActive.
+func (v fleetView) firstLabelActive() bool {
+ return (v.Active || (v.Complete && len(v.Boxes) == 1)) && !v.HasSidecars && v.DeploymentCurrent && !v.Unmovable
+}
+
// chooseDestination is rule 1's choice: among the boxes that are fresh, not
// draining, at the label floor and not exclude (and, for a rootfs record,
// rootfs-ready at the rootfs floor), the one holding the fewest records,
@@ -282,7 +288,7 @@ func (s *Server) loadFleetViewAt(ctx context.Context, fleet string, now time.Tim
return s.loadFleetViewRequiredAt(ctx, fleet, now, false)
}
-// Count admission needs the complete view even when labelling is inactive.
+// Count admission and the relabel pass need the complete view even on one box.
func (s *Server) loadFleetViewRequiredAt(ctx context.Context, fleet string, now time.Time, requireComplete bool) (fleetView, error) {
v := fleetView{Load: map[string]int{}}
reg, err := s.openPassRegistry()
diff --git a/internal/console/fleetrelabel.go b/internal/console/fleetrelabel.go
index dc9a125ca..1309f25cc 100644
--- a/internal/console/fleetrelabel.go
+++ b/internal/console/fleetrelabel.go
@@ -32,7 +32,7 @@ import (
// longer an active box of the fleet moves to chooseDestination(rootfs,
// its box) (rules 4 and 5). Moving needs no open activation gate
// (controller ruling 1).
-// - And when the activation gate is open (labelActive): an unlabelled
+// - When first labelling is allowed (firstLabelActive): an unlabelled
// record is labelled to the box its binding
// (fleets/<f>/bindings/<host>.json) names, when that binding was
// written for this record and the box is a valid destination. A record
@@ -124,6 +124,17 @@ func (s *Server) fleetRelabelPass(ctx context.Context, now time.Time) error {
jobFailed(ctx, err)
log.Printf("console: replica placement %s: %v", f.Name, err)
}
+ // Only first labelling needs the inventory skipped by a one-box view.
+ // Keep replica reconciliation above on its existing view.
+ if len(v.Boxes) == 1 && !v.Complete {
+ v, err = s.loadFleetViewRequiredAt(ctx, f.Name, now, true)
+ if err != nil {
+ log.Printf("console: fleet relabel %s: view: %v", f.Name, err)
+ jobFailed(ctx, err)
+ unlock()
+ continue
+ }
+ }
if _, err := s.relabelFleet(ctx, f.Name, v, now); err != nil {
log.Printf("console: fleet relabel %s: %v", f.Name, err)
jobFailed(ctx, err)
@@ -150,11 +161,8 @@ func (s *Server) relabelFleet(ctx context.Context, fleet string, v fleetView, no
fleet, box, releaseOrUnknown(rel), release.FleetBoxLabelFloor)
return 0, nil
}
- if !v.Active {
- return 0, nil
- }
var plan []relabel
- if v.labelActive() {
+ if v.firstLabelActive() {
first, err := s.planFirstLabelling(ctx, fleet, v)
if err != nil {
return 0, err
@@ -164,6 +172,9 @@ func (s *Server) relabelFleet(ctx context.Context, fleet string, v fleetView, no
}
plan = append(plan, first...)
}
+ if !v.Active {
+ return s.writeRelabels(ctx, fleet, plan, now), nil
+ }
for _, rec := range v.Records {
if rec.ReplicaAssignment != nil || !fleetLabelsRecord(rec) || rec.FleetBox == "" {
continue
diff --git a/internal/console/fleetrelabel_test.go b/internal/console/fleetrelabel_test.go
index e3cf902f4..36b3f0bc7 100644
--- a/internal/console/fleetrelabel_test.go
+++ b/internal/console/fleetrelabel_test.go
@@ -774,3 +774,27 @@ func TestDrainRefusedOnAHostClaimConsole(t *testing.T) {
t.Fatalf("drainBox on a host-claim console = %v", err)
}
}
+
+func TestRelabelOneBoxFleetDoesNotMoveGoneBoxRecords(t *testing.T) {
+ fx, _, reg := fleetBoxWorld(t, "onebox-no-moves", "pi-1", "pi-2")
+ if err := reg.SetFleetBoxStatus("pi", "pi-2", "detached"); err != nil {
+ t.Fatal(err)
+ }
+ putFleetRecord(t, fx, "shop", "gone.example.com", func(r *provision.Record) { r.FleetBox = "pi-2" })
+ putFleetRecord(t, fx, "shop", "bound.example.com", nil)
+ bindTo(t, fx, "bound.example.com", "pi-1", time.Now())
+ ctx := context.Background()
+ key := provision.RecordKey(fx.sqid, "shop", "gone.example.com")
+ before, _, err := fx.rel.Get(ctx, key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := fx.srv.RunFleetRelabelPass(ctx, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ wantLabel(t, shopRec(t, fx, "bound.example.com"), "pi-1")
+ after, _, err := fx.rel.Get(ctx, key)
+ if err != nil || !bytes.Equal(before, after) {
+ t.Fatalf("one-box pass moved or rewrote the gone box's record: %s, %v", after, err)
+ }
+}
diff --git a/internal/console/replicacoordinator_test.go b/internal/console/replicacoordinator_test.go
index beabddf68..58ca8cf2c 100644
--- a/internal/console/replicacoordinator_test.go
+++ b/internal/console/replicacoordinator_test.go
@@ -4,14 +4,19 @@ import (
"bytes"
"context"
"encoding/json"
+ "errors"
"io"
"net/http"
+ "net/http/httptest"
"strings"
+ "sync/atomic"
"testing"
"time"
+ "github.com/carlosframework/platform/internal/edge"
"github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/replicas"
)
@@ -334,3 +339,201 @@ func TestReplicaRetiredACKsUseLatestRevision(t *testing.T) {
t.Fatalf("ACKs: %+v %v", got, err)
}
}
+
+func TestReplicaCoordinatorOneBoxFleetFirstLabelling(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ change func(*provision.Record, *fleetBinding)
+ want int
+ reads int32
+ }{
+ {name: "matching", want: 200, reads: 1},
+ {name: "newer", change: func(_ *provision.Record, b *fleetBinding) { b.BoundAt++ }, want: 200, reads: 1},
+ {name: "detached-box", change: func(_ *provision.Record, b *fleetBinding) { b.Box = "b2" }, want: 404, reads: 1},
+ {name: "other-box", change: func(_ *provision.Record, b *fleetBinding) { b.Box = "missing" }, want: 404, reads: 1},
+ {name: "older", change: func(_ *provision.Record, b *fleetBinding) { b.BoundAt-- }, want: 404, reads: 1},
+ {name: "other-host", change: func(_ *provision.Record, b *fleetBinding) { b.Host = "other.example.com" }, want: 404, reads: 1},
+ {name: "other-fleet", change: func(_ *provision.Record, b *fleetBinding) { b.Fleet = "away" }, want: 404, reads: 1},
+ {name: "missing-binding", want: 404, reads: 1},
+ {name: "binding-read-error", want: 404, reads: 1},
+ {name: "sidecars", want: 404},
+ {name: "deployment-not-current", want: 404},
+ {name: "below-label-floor", want: 404},
+ {name: "rootfs-not-ready", want: 404, reads: 1},
+ {name: "below-rootfs-floor", want: 404},
+ {name: "draining", want: 404, reads: 1},
+ {name: "stale-heartbeat", want: 404, reads: 1},
+ {name: "incomplete", want: 404},
+ {name: "unmovable", change: func(r *provision.Record, _ *fleetBinding) { r.Addr = "127.0.0.1:1234" }, want: 404},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ fx := newIxFx(t, "replica-onebox")
+ token, _, _ := ixFleetWorld(t, fx, fx.acct)
+ ctx, now := context.Background(), time.Now()
+ if err := fx.st.SetAppPlacement(fx.acctID, "shop", "fleet/home"); err != nil {
+ t.Fatal(err)
+ }
+ fx.srv.consoleBuild = release.FleetBoxLabelFloor
+ writeFleetBoxOn(t, fx.consoleFixture, "home", "b1", "", true, now, release.FleetBoxLabelFloor)
+ reg, err := registry.Open(fx.regPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { reg.Close() })
+ boxes, err := reg.ListFleetBoxes("home")
+ if err != nil {
+ t.Fatal(err)
+ }
+ active := 0
+ for _, b := range boxes {
+ if b.Status == "active" {
+ active++
+ }
+ }
+ if active != 1 {
+ t.Fatalf("active boxes = %d, want 1", active)
+ }
+ rec, _ := ixRecord(t, fx, ixHost)
+ binding := fleetBinding{Fleet: "home", Host: ixHost, Box: "b1", BoundAt: rec.CreatedAt.UnixMilli()}
+ if tc.change != nil {
+ tc.change(&rec, &binding)
+ ixPutRecord(t, fx, rec)
+ }
+ switch tc.name {
+ case "sidecars":
+ if err := provision.CreateSidecarRecord(ctx, fx.rel, provision.SidecarRecord{Schema: 1, Name: "bot", Host: provision.SidecarHost("bot", "shop", fx.acct), App: "shop", Account: fx.acct, Region: "fleet/home", Channel: "stable", CreatedAt: now}); err != nil {
+ t.Fatal(err)
+ }
+ case "deployment-not-current":
+ fx.srv.consoleBuild = "v0.32.0"
+ case "below-label-floor":
+ writeFleetBoxOn(t, fx.consoleFixture, "home", "b1", "", true, now, "v0.32.0")
+ case "rootfs-not-ready":
+ writeFleetBoxOn(t, fx.consoleFixture, "home", "b1", "", false, now, release.FleetBoxLabelFloor)
+ case "below-rootfs-floor":
+ writeFleetBoxOn(t, fx.consoleFixture, "home", "b1", "", true, now, "v0.28.16")
+ case "draining":
+ if _, err := reg.SetFleetBoxDraining("home", "b1", now.UnixNano()); err != nil {
+ t.Fatal(err)
+ }
+ case "stale-heartbeat":
+ writeFleetBoxOn(t, fx.consoleFixture, "home", "b1", "", true, now.Add(-time.Hour), release.FleetBoxLabelFloor)
+ }
+ inc, err := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ bindingKey := "fleets/home/bindings/" + ixHost + ".json"
+ if tc.name != "missing-binding" {
+ body, err := json.Marshal(binding)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := fx.rel.Put(ctx, bindingKey, body, ""); err != nil {
+ t.Fatal(err)
+ }
+ }
+ var reads atomic.Int32
+ ds := fx.rel.(*deletableStore)
+ ds.setGetHook(func(_ context.Context, key string) error {
+ if key == bindingKey {
+ reads.Add(1)
+ if tc.name == "binding-read-error" {
+ return errors.New("injected binding read failure")
+ }
+ }
+ if tc.name == "incomplete" && key == provision.RecordKey(fx.acct, "shop", ixHost) {
+ return errors.New("injected inventory read failure")
+ }
+ return nil
+ })
+ t.Cleanup(func() { ds.setGetHook(nil) })
+ if err := fx.srv.RunFleetRelabelPass(ctx, now); err != nil {
+ t.Fatal(err)
+ }
+ if n := reads.Load(); n != tc.reads {
+ t.Fatalf("pass binding reads = %d, want %d", n, tc.reads)
+ }
+ ds.setGetHook(func(_ context.Context, key string) error {
+ if key == bindingKey {
+ reads.Add(1)
+ }
+ return nil
+ })
+ after, _ := ixRecord(t, fx, ixHost)
+ if tc.want == 200 {
+ wantLabel(t, &after, "b1")
+ boxes, err = reg.ListFleetBoxes("home")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, b := range boxes {
+ if b.Label == "b1" && b.LabelledAt == 0 {
+ t.Fatal("first labelling did not mark the box labelled")
+ }
+ }
+ } else {
+ wantNoLabel(t, &after)
+ }
+ if err := reg.Upsert(registry.Route{Host: "console.test", App: "console", Unit: "console.service", Kind: "service", Addr: fx.ts.Listener.Addr().String(), Status: "ready"}); err != nil {
+ t.Fatal(err)
+ }
+ e, err := edge.New(reg, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ e.EnableTunnels(edge.NewTunnelHub("tunnel.test", nil))
+ post := func(door string, payload any) []byte {
+ t.Helper()
+ _, before := ixRecord(t, fx, ixHost)
+ body, err := json.Marshal(payload)
+ if err != nil {
+ t.Fatal(err)
+ }
+ reads.Store(0)
+ req := httptest.NewRequest(http.MethodPost, "https://tunnel.test/api/fleet/replicas/"+fx.acct+"/shop/"+ixHost+"/"+door, bytes.NewReader(body))
+ req.Header.Set("Authorization", "Bearer "+token)
+ req.Header.Set("Carlos-Tunnel-Name", "home/b1")
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ e.ServeHTTP(w, req)
+ if n := reads.Load(); n != 0 {
+ t.Errorf("%s: binding reads = %d, want 0", door, n)
+ }
+ if w.Code != tc.want || tc.want == 404 && w.Body.String() != "404 page not found\n" {
+ t.Fatalf("%s: %d %s, want %d", door, w.Code, w.Body.String(), tc.want)
+ }
+ if tc.want == 404 {
+ _, after := ixRecord(t, fx, ixHost)
+ if !bytes.Equal(before, after) {
+ t.Fatal("refused request changed the record")
+ }
+ }
+ return w.Body.Bytes()
+ }
+ raw := post("coordinator", map[string]any{"incarnation": inc, "op": "acquire", "boot_id": "boot"})
+ if tc.want == 404 {
+ post("release", map[string]any{"incarnation": inc, "op": "begin"})
+ return
+ }
+ var reply replicas.CoordinatorReply
+ if err := json.Unmarshal(raw, &reply); err != nil {
+ t.Fatal(err)
+ }
+ if reply.Lease.Box != "b1" || reply.RemainingMS <= 0 {
+ t.Fatalf("coordinator reply: %+v", reply)
+ }
+ var deploy replicas.Deploy
+ for _, op := range []string{"begin", "releasing", "released"} {
+ raw = post("release", map[string]any{"incarnation": inc, "op": op, "id": "onebox-deploy", "term": reply.Lease.Term, "revision": deploy.Revision, "target": replicas.Build{Version: "v2", Digest: "digest"}})
+ if err := json.Unmarshal(raw, &deploy); err != nil {
+ t.Fatal(err)
+ }
+ }
+ after, _ = ixRecord(t, fx, ixHost)
+ if after.FleetBox != "b1" || after.ReplicaDeploy == nil || after.ReplicaDeploy.Phase != "released" {
+ t.Fatalf("released record: %+v", after)
+ }
+ })
+ }
+}
diff --git a/internal/console/replicaplacement_test.go b/internal/console/replicaplacement_test.go
index 0754df905..b7cc1ba5b 100644
--- a/internal/console/replicaplacement_test.go
+++ b/internal/console/replicaplacement_test.go
@@ -297,6 +297,12 @@ func TestReplicaPlacementLegacyReadBudget(t *testing.T) {
if pass == 0 {
wantGets[provision.RecordKey(fx.sqid, "shop", "a.example.com")] = 1
}
+ } else {
+ wantLists = map[string]int{"fleets/pi/status/boxes/": 1, "status/boxes/": 1, provision.RecordsPrefix(fx.sqid, "shop"): 1, provision.SidecarsPrefix(fx.sqid, "shop"): 1}
+ wantGets = map[string]int{"fleets/pi/status/boxes/pi-1.json": 1}
+ if pass == 0 {
+ wantGets[provision.RecordKey(fx.sqid, "shop", "a.example.com")] = 1
+ }
}
if !reflect.DeepEqual(cs.gets, wantGets) || !reflect.DeepEqual(cs.lists, wantLists) || len(cs.puts) != 0 {
t.Fatalf("pass %d: gets %v lists %v puts %v; expected gets %v lists %v", pass, cs.gets, cs.lists, cs.puts, wantGets, wantLists)