console: record a post-activation fleet at its first box (#519)
Once the hostname history is active, a fleet created since activation could never take an instance without the offline invalidate and complete cycle. Its first add-box now records it as examined, its targets being its bucket and that box. The #404 growth rule is unchanged: its next add-box is N9. A bucket shared with a closed fleet leaves it unrecorded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Human
Prompt
Can we take #519?
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
addFleetBoxRecorded wraps the add-box write under the history lock: N9 for recorded fleets, record-at-first-box for unrecorded ones, detach on a failed mode write.
Agent notes
Recording at fleets create (the issue's first suggestion) would make the first add-box N9; recording with an empty target set would make the coverage check refuse once a box joins. Hence first add-box, targets after the add.
4 files changed,
+223
−22
docs/superpowers/specs/2026-10-26-nearest-region-without-claims-design.md+2 −0internal/console/fleets.go+12 −9internal/console/hostsused.go+71 −13internal/console/hostsused_coverage_test.go+138 −0
CI passed — run details
diff --git a/docs/superpowers/specs/2026-10-26-nearest-region-without-claims-design.md b/docs/superpowers/specs/2026-10-26-nearest-region-without-claims-design.md| index 973d04b27..2e01c894f 100644 |
| --- a/docs/superpowers/specs/2026-10-26-nearest-region-without-claims-design.md |
| +++ b/docs/superpowers/specs/2026-10-26-nearest-region-without-claims-design.md |
| @@ -54,6 +54,8 @@ Deleted history and noncurrent versions are not sources, because they are harmle |
| **After completion, unexamined targets are refused.** A region, pool or fleet that was not in the roster gets S4 for data-kind creates, and the nearest-region picker skips it, until its own inventory is imported. |
| +**A fleet created after completion is recorded at its first box (#519, 2026-11-02).** Activation records every fleet on its roster, so a fleet the history has not recorded was created since. No box of it existed before completion, so none holds its old data. Its first add-box, with the console serving, adds the box and records the fleet as examined, its targets being its bucket and that box. Its next add-box is N9 like any recorded fleet's. A bucket that a closed fleet also uses was never scanned: such a fleet is left unrecorded and completes offline, as before. A failed write of the mode detaches the box again; its token was never shown. |
| + |
| ## 3a. The activation window (accepted 2026-10-26) |
| A one-time maintenance window wraps the inventory. It closes round 3's P1: a marked allocation started before activation and still pending could otherwise publish over old data after `complete` (recovery's "our slot, absent record" branch, lifetimes_recovery.go:204). |
diff --git a/internal/console/fleets.go b/internal/console/fleets.go| index 39b54b7f0..681e6c5a7 100644 |
| --- a/internal/console/fleets.go |
| +++ b/internal/console/fleets.go |
| @@ -409,26 +409,29 @@ func (s *Server) handleAPIFleetAddBox(w http.ResponseWriter, r *http.Request) { |
| "invalid box label — 1-32 characters of a-z, 0-9 and hyphen") |
| return |
| } |
| - // A fleet the hostname history recorded takes no new or reactivated |
| - // box while it is active (N9, Paul's ruling on #404): the box was not |
| - // scanned. AddFleetBox both adds and reactivates, so this one check |
| - // covers both. |
| - if ref := s.fleetGrowthRefusal(r.Context(), fleet.Name); ref != nil { |
| - writeDoorRefusalJSON(w, r, ref) |
| - return |
| - } |
| plaintext, hash, err := mintFleetToken() |
| if err != nil { |
| log.Printf("console: fleet add-box %s/%s: mint token: %v", acct.Sqid, fleet.Name, err) |
| jsonError(w, http.StatusInternalServerError, "could not mint a token — try again") |
| return |
| } |
| + // The hostname history's fleet rules wrap the write |
| + // (addFleetBoxRecorded): a fleet it recorded takes no new or |
| + // reactivated box while it is active (N9, Paul's ruling on #404), and |
| + // a fleet created since activation is recorded at its first box (#519). |
| + // AddFleetBox both adds and reactivates, so this covers both. |
| + // |
| // The fleet is already proven to exist (apiFleet), so AddFleetBox's own |
| // "no fleet" case is unreachable here — but a tx/busy/disk failure is |
| // not, and must not be rendered as the ordinary duplicate-box 409: |
| // errors.Is against the sentinel is what tells the two apart, rather |
| // than assuming every failure here is the uniqueness guard. |
| - if err := reg.AddFleetBox(registry.FleetBox{Fleet: fleet.Name, Label: label, TokenHash: hash, Status: "active"}); err != nil { |
| + ref, err := s.addFleetBoxRecorded(r.Context(), reg, *fleet, registry.FleetBox{Fleet: fleet.Name, Label: label, TokenHash: hash, Status: "active"}) |
| + if ref != nil { |
| + writeDoorRefusalJSON(w, r, ref) |
| + return |
| + } |
| + if err != nil { |
| if errors.Is(err, registry.ErrFleetBoxActive) { |
| jsonError(w, http.StatusConflict, "fleet "+fleet.Name+" already has an active box "+label) |
| return |
diff --git a/internal/console/hostsused.go b/internal/console/hostsused.go| index e38c0160d..947958ccc 100644 |
| --- a/internal/console/hostsused.go |
| +++ b/internal/console/hostsused.go |
| @@ -218,7 +218,7 @@ func (s *Server) hostsTargetRefusal(ctx context.Context, region string) *doorRef |
| // hostsCoverageRefusal is the rest of S4 once active: the region's or |
| // fleet's targets now are all ones activation scanned (a fleet's or |
| // region's target set is fixed once recorded). A box added to a fleet |
| -// since is refused at its own door (fleetGrowthRefusal); a box that joins |
| +// since is refused at its own door (addFleetBoxRecorded); a box that joins |
| // a region does so from infra, so a create there is refused here until |
| // the history is completed again. Targets that cannot be read refuse (S5). |
| // It reads the edges files (one snapshot, which also names the place), or |
| @@ -299,21 +299,79 @@ func (s *Server) gateDataCreate(ctx context.Context, host, region string) *doorR |
| return s.hostsCoverageRefusal(ctx, region) |
| } |
| -// fleetGrowthRefusal is the add-box door's check (Paul's ruling on #404): |
| -// once the history is active, a fleet activation recorded (examined or |
| -// closed) takes no new or reactivated box. Growing it means invalidating |
| -// the history, adding the box, and completing the history again. A mode |
| -// that cannot be read refuses. |
| -func (s *Server) fleetGrowthRefusal(ctx context.Context, fleet string) *doorRefusal { |
| - st, err := s.hostsState(ctx) |
| +const msgFleetBoxNotAdded = "could not add the box — try again" |
| + |
| +// addFleetBoxRecorded is the add-box door's write, with the history's |
| +// fleet rules around it. Once the history is active: |
| +// |
| +// - A fleet the history recorded (examined or closed) takes no new or |
| +// reactivated box (N9, Paul's ruling on #404). Growing it means |
| +// invalidating the history, adding the box, and completing the |
| +// history again. |
| +// - A fleet the history has not recorded was created since activation |
| +// (activation records every fleet on its roster, and refuses a |
| +// roster that changed under it). No box of it existed before, so |
| +// none can hold its old data, and no record can be placed on it |
| +// until it is recorded (S4). Its first add-box records it (#519): |
| +// the box is added, then the fleet is examined, its targets as they |
| +// stand after the add. Its next add-box is N9 like any other. |
| +// The one exception is a bucket a closed fleet also uses, which was |
| +// never scanned: the box is added and the fleet left unrecorded, as |
| +// before #519, to be completed offline. |
| +// |
| +// It all runs under the lock every admission takes, so two add-boxes on a |
| +// new fleet cannot both record it. A write of the mode that fails detaches |
| +// the box again (its token was never shown, so nothing has dialled in with |
| +// it), and the retry reactivates it. A mode that cannot be read refuses. |
| +// err is AddFleetBox's, for the door to tell a duplicate from a failure. |
| +func (s *Server) addFleetBoxRecorded(ctx context.Context, reg consoleRegistry, fl registry.Fleet, box registry.FleetBox) (*doorRefusal, error) { |
| + s.hosts.mu.Lock() |
| + defer s.hosts.mu.Unlock() |
| + if err := s.loadHostsLocked(ctx); err != nil { |
| + log.Printf("console: hosts used: read the mode to add a box to fleet %s: %v", fl.Name, err) |
| + return &doorRefusal{http.StatusServiceUnavailable, msgFleetBoxNotAdded}, nil |
| + } |
| + st, key := s.hosts.state, "fleet:"+fl.Name |
| + if st.Mode != provision.HostsModeActive { |
| + return nil, reg.AddFleetBox(box) |
| + } |
| + if slices.Contains(st.Examined, key) || slices.Contains(st.Closed, key) { |
| + return &doorRefusal{http.StatusConflict, msgFleetInventoried(fl.Name)}, nil |
| + } |
| + if err := reg.AddFleetBox(box); err != nil { |
| + return nil, err |
| + } |
| + for _, c := range st.Closed { |
| + if ts, ok := st.FleetTargets[c]; !ok || (fl.Bucket != "" && slices.Contains(ts, "bucket:"+fl.Bucket)) { |
| + log.Printf("console: hosts used: fleet %s is left unrecorded: its bucket may be %s's, which was closed, not scanned", fl.Name, c) |
| + return nil, nil |
| + } |
| + } |
| + undo := func(why string, err error) *doorRefusal { |
| + log.Printf("console: hosts used: record new fleet %s: %s: %v", fl.Name, why, err) |
| + if derr := reg.SetFleetBoxStatus(fl.Name, box.Label, "detached"); derr != nil { |
| + log.Printf("console: hosts used: record new fleet %s: detach box %s again: %v", fl.Name, box.Label, derr) |
| + } |
| + return &doorRefusal{http.StatusServiceUnavailable, msgFleetBoxNotAdded} |
| + } |
| + targets, err := rosterFleet(reg, fl) |
| if err != nil { |
| - log.Printf("console: hosts used: read the mode to add a box to fleet %s: %v", fleet, err) |
| - return &doorRefusal{http.StatusServiceUnavailable, "could not add the box — try again"} |
| + return undo("read its targets", err), nil |
| } |
| - if st.Mode == provision.HostsModeActive && (slices.Contains(st.Examined, "fleet:"+fleet) || slices.Contains(st.Closed, "fleet:"+fleet)) { |
| - return &doorRefusal{http.StatusConflict, msgFleetInventoried(fleet)} |
| + next := st |
| + next.Examined = sortedUnique(append(slices.Clone(st.Examined), key)) |
| + next.FleetTargets = make(map[string][]string, len(st.FleetTargets)+1) |
| + for k, ts := range st.FleetTargets { |
| + next.FleetTargets[k] = ts |
| } |
| - return nil |
| + next.FleetTargets[key] = sortedUnique(targets) |
| + etag, err := provision.WriteHostsState(ctx, s.cfg.Releases, next, s.hosts.etag) |
| + if err != nil { |
| + return undo("write the mode", err), nil |
| + } |
| + s.hosts.state, s.hosts.etag = next, etag |
| + log.Printf("console: hosts used: fleet %s recorded at its first box, targets %v", fl.Name, next.FleetTargets[key]) |
| + return nil, nil |
| } |
| // admitDataCreate is a data-kind create door's admission: S5 once |
diff --git a/internal/console/hostsused_coverage_test.go b/internal/console/hostsused_coverage_test.go| index 6dc47d00e..082802ef4 100644 |
| --- a/internal/console/hostsused_coverage_test.go |
| +++ b/internal/console/hostsused_coverage_test.go |
| @@ -9,6 +9,7 @@ package console |
| import ( |
| "context" |
| "net/http" |
| + "slices" |
| "strings" |
| "testing" |
| @@ -251,3 +252,140 @@ func TestCoverageRefusalReadsOneSnapshot(t *testing.T) { |
| t.Fatalf("reads = %d lists, %d gets, want 1 list and at most %d gets", counter.lists, counter.gets, len(files)) |
| } |
| } |
| + |
| +// TestNewFleetRecordedAtFirstBox (#519): once active, a fleet created since |
| +// activation is recorded at its first add-box, examined with its bucket and |
| +// that box as its targets, so an app can be created placed on it and a |
| +// data-kind create there passes S4, with the console serving. Its next |
| +// add-box is N9, and the record survives a console start. |
| +func TestNewFleetRecordedAtFirstBox(t *testing.T) { |
| + fx, token := placeFixture(t, "code-new-fleet") |
| + activeAsNow(t, fx.srv) |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", map[string]string{"name": "classic", "bucket": "classic-data"}, token); status != http.StatusOK { |
| + t.Fatalf("create fleet = %d %v", status, body) |
| + } |
| + if ref := fx.srv.hostsTargetRefusal(context.Background(), "fleet/classic"); ref == nil { |
| + t.Fatal("a create on a new fleet with no box was admitted") |
| + } |
| + add := func(label string) (int, map[string]any) { |
| + return postJSON(t, fleetsURL(fx, fx.sqid)+"/classic/boxes", map[string]string{"label": label}, token) |
| + } |
| + if status, out := add("box-1"); status != http.StatusOK { |
| + t.Fatalf("first add-box on a new fleet = %d %v, want 200", status, out) |
| + } |
| + check := func(when string) { |
| + t.Helper() |
| + st, err := fx.srv.hostsState(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if st.Mode != provision.HostsModeActive || !slices.Contains(st.Examined, "fleet:classic") || !slices.Contains(st.Examined, "fleet:pi") { |
| + t.Fatalf("%s: state = %+v, want active with classic and pi examined", when, st) |
| + } |
| + if got, want := st.FleetTargets["fleet:classic"], []string{"box:fleet/classic/box-1", "bucket:classic-data"}; !slices.Equal(got, want) { |
| + t.Fatalf("%s: classic's targets = %v, want %v", when, got, want) |
| + } |
| + if _, ok := st.FleetTargets["fleet:pi"]; !ok { |
| + t.Fatalf("%s: pi's targets were dropped: %+v", when, st.FleetTargets) |
| + } |
| + if ref := fx.srv.gateDataCreate(context.Background(), "x.go.tito.io", "fleet/classic"); ref != nil { |
| + t.Fatalf("%s: a data-kind create on the recorded fleet = %+v, want admitted", when, ref) |
| + } |
| + } |
| + check("recorded") |
| + if status, out := add("box-2"); status != http.StatusConflict || out["error"] != msgFleetInventoried("classic") { |
| + t.Fatalf("second add-box = %d %v, want 409 N9", status, out) |
| + } |
| + if got := activeFleetBoxes(t, fx, "classic"); !slices.Equal(got, []string{"box-1"}) { |
| + t.Fatalf("active boxes = %v, want [box-1]", got) |
| + } |
| + status, _, out := rawReq(t, http.MethodPost, fx.ts.URL+"/api/cli/apps/"+fx.sqid+"/new", token, `{"name":"placed","place":"fleet/classic"}`) |
| + if status != http.StatusOK && status != http.StatusCreated { |
| + t.Fatalf("app create placed on the recorded fleet = %d %s, want it created", status, out) |
| + } |
| + // A console start reads the same record. |
| + fx.srv.hosts.reset() |
| + fx.srv.hosts.mu.Lock() |
| + fx.srv.hosts.verified = true |
| + fx.srv.hosts.mu.Unlock() |
| + check("after a start") |
| +} |
| + |
| +// TestNewFleetNotRecorded: a new fleet's first add-box leaves it |
| +// unrecorded, as before #519, when its bucket is a closed fleet's (never |
| +// scanned) and before the history is active; a write of the mode that |
| +// fails detaches the box again, and the retry reactivates and records it. |
| +func TestNewFleetNotRecorded(t *testing.T) { |
| + newFleet := func(t *testing.T, code, bucket string) (*consoleFixture, func(string) (int, map[string]any)) { |
| + fx, token := placeFixture(t, code) |
| + if status, body := postJSON(t, fleetsURL(fx, fx.sqid)+"/new", map[string]string{"name": "classic", "bucket": bucket}, token); status != http.StatusOK { |
| + t.Fatalf("create fleet = %d %v", status, body) |
| + } |
| + return fx, func(label string) (int, map[string]any) { |
| + return postJSON(t, fleetsURL(fx, fx.sqid)+"/classic/boxes", map[string]string{"label": label}, token) |
| + } |
| + } |
| + unrecorded := func(t *testing.T, fx *consoleFixture) { |
| + t.Helper() |
| + st, err := fx.srv.hostsState(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if slices.Contains(st.Examined, "fleet:classic") || st.FleetTargets["fleet:classic"] != nil { |
| + t.Fatalf("classic was recorded: %+v", st) |
| + } |
| + if ref := fx.srv.hostsTargetRefusal(context.Background(), "fleet/classic"); st.Mode == provision.HostsModeActive && ref == nil { |
| + t.Fatal("a create on the unrecorded fleet was admitted") |
| + } |
| + } |
| + t.Run("closed bucket", func(t *testing.T) { |
| + fx, add := newFleet(t, "code-new-fleet-closed", "pi-fleet-data") |
| + st, err := HostsActivation{Releases: fx.srv.cfg.Releases, Registry: fx.srv.cfg.Registry}.Roster(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + delete(st.Fleets, "fleet:classic") // as if created since |
| + forceHostsState(t, fx.srv, provision.HostsState{Mode: provision.HostsModeActive, Closed: []string{"fleet:pi"}, FleetTargets: st.Fleets, RegionTargets: []string{}}) |
| + for _, label := range []string{"box-1", "box-2"} { |
| + if status, out := add(label); status != http.StatusOK { |
| + t.Fatalf("add-box %s = %d %v, want 200", label, status, out) |
| + } |
| + } |
| + unrecorded(t, fx) |
| + }) |
| + t.Run("not active", func(t *testing.T) { |
| + fx, add := newFleet(t, "code-new-fleet-incomplete", "classic-data") |
| + if status, out := add("box-1"); status != http.StatusOK { |
| + t.Fatalf("add-box = %d %v, want 200", status, out) |
| + } |
| + unrecorded(t, fx) |
| + }) |
| + t.Run("write fails", func(t *testing.T) { |
| + fx, add := newFleet(t, "code-new-fleet-write", "classic-data") |
| + bucket := wrapBucket(fx) |
| + st, err := HostsActivation{Releases: fx.srv.cfg.Releases, Registry: fx.srv.cfg.Registry}.Roster(context.Background()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + delete(st.Fleets, "fleet:classic") |
| + forceHostsState(t, fx.srv, provision.HostsState{Mode: provision.HostsModeActive, Examined: []string{"fleet:pi"}, FleetTargets: st.Fleets, RegionTargets: []string{}}) |
| + bucket.setHook(failNth("put", provision.HostsModeKey, 1, false)) |
| + if status, out := add("box-1"); status != http.StatusServiceUnavailable || out["error"] != msgFleetBoxNotAdded { |
| + t.Fatalf("add-box with the mode write failing = %d %v, want 503", status, out) |
| + } |
| + bucket.setHook(nil) |
| + if got := activeFleetBoxes(t, fx, "classic"); len(got) != 0 { |
| + t.Fatalf("active boxes after the failed write = %v, want none", got) |
| + } |
| + unrecorded(t, fx) |
| + if status, out := add("box-1"); status != http.StatusOK { |
| + t.Fatalf("retried add-box = %d %v, want 200", status, out) |
| + } |
| + if got := activeFleetBoxes(t, fx, "classic"); !slices.Equal(got, []string{"box-1"}) { |
| + t.Fatalf("active boxes after the retry = %v, want [box-1]", got) |
| + } |
| + if ref := fx.srv.gateDataCreate(context.Background(), "x.go.tito.io", "fleet/classic"); ref != nil { |
| + t.Fatalf("a data-kind create after the retry = %+v, want admitted", ref) |
| + } |
| + }) |
| +} |