console: hide detached fleet boxes from system status
carlos fleets detach-box leaves the box's report object under fleets/<fleet>/status/boxes/, so system status listed a detached box as UNREACHABLE for as long as the object stayed (a dozen stale drill rows on dev, cleaned by hand). systemBoxes now drops a fleet box whose registry row is detached. That is the reader behind system status, the status layout door, the egress grant check, the instance page's layout word and the promote's platform set. The alert engine's fleet read (mailer_unarmed, the only engine rule that reads fleet reports; box_silent judges platform boxes only) drops them too, so a detached box still reporting cannot raise an alert. The registry read is the console's local SQLite, no bucket request, and nothing is deleted from the bucket, so already stale objects stop showing and a box detached while still running can keep rewriting its object without a race. A report under a label the registry has no row for is kept, add-box reactivation shows the box again, and platform boxes are untouched. A registry read that fails shows every report, as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+204
−4
internal/console/alertmailer_test.go+62 −0internal/console/alertsignals.go+18 −2internal/console/apisystemupdate.go+55 −2internal/console/apisystemupdate_test.go+69 −0
CI passed — run details
diff --git a/internal/console/alertmailer_test.go b/internal/console/alertmailer_test.go| index 9f854b4ba..de65d2fc6 100644 |
| --- a/internal/console/alertmailer_test.go |
| +++ b/internal/console/alertmailer_test.go |
| @@ -280,3 +280,65 @@ func TestEnginePassMailerUnarmedFleetBox(t *testing.T) { |
| t.Fatalf("%d mailer_unarmed incidents open with the registry unreadable, want 1", n) |
| } |
| } |
| + |
| +// TestEnginePassSkipsDetachedFleetBox: a box the registry holds detached |
| +// is no longer the fleet's, so its report raises nothing, even one still |
| +// fresh from a machine that runs on after the detach. Its active neighbour, |
| +// reporting the same unarmed route, still fires; and no fleet box report |
| +// raises box_silent, which judges the platform's own boxes only. |
| +func TestEnginePassSkipsDetachedFleetBox(t *testing.T) { |
| + ef := newEngineFixture(t, nil) |
| + ef.operator(t, opA) |
| + ctx := context.Background() |
| + if _, err := ef.st.CreateApp("shop", "test", alertT0); err != nil { |
| + t.Fatal(err) |
| + } |
| + b, err := json.Marshal(mailwire.SenderView{ID: mailwire.SenderAppID, Submission: &mailwire.Submission{On: true, At: alertT0}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + putAlertBody(t, ef.rel, mailwire.SenderKey(opsSqid, "shop", mailwire.SenderAppID), b) |
| + if err := provision.CreateRecord(ctx, ef.rel, provision.Record{Schema: 1, Host: "shop.example.com", Slug: "shop", |
| + App: "shop", Account: opsSqid, Channel: "stable", CreatedAt: alertT0}); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg := openTestReg(t, ef.consoleFixture) |
| + t.Cleanup(func() { reg.Close() }) |
| + if _, err := reg.CreateFleet(registry.Fleet{Name: "own", Account: opsSqid, Bucket: "own-bucket"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, label := range []string{"fbox", "dbox"} { |
| + if err := reg.AddFleetBox(registry.FleetBox{Fleet: "own", Label: label, TokenHash: strings.Repeat("a", 64)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := reg.SetFleetBoxStatus("own", "dbox", "detached"); err != nil { |
| + t.Fatal(err) |
| + } |
| + ef.fleetServices(t, map[string]bool{"keymail": true}) |
| + beat := func(at time.Time) { |
| + ef.heartbeat(t, "box-1", at, &status.BoxMetrics{MailerArmed: true, RouteVersions: map[string]string{"shop.example.com": "v1"}}) |
| + for _, label := range []string{"fbox", "dbox"} { |
| + body, err := json.Marshal(status.Box{GeneratedAt: at, Label: label, Metrics: &status.BoxMetrics{ |
| + RouteServing: map[string]string{"shop.example.com": "v1"}}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + putAlertBody(t, ef.rel, "fleets/own/status/boxes/"+label+".json", body) |
| + } |
| + } |
| + beat(alertT0) |
| + ef.passAt(t, 0) |
| + beat(alertT0.Add(4 * time.Minute)) |
| + ef.passesTo(t, time.Minute, 5*time.Minute) |
| + fire := ef.mailsTo(opA) |
| + if len(fire) != 1 || !strings.Contains(fire[0], "fleets/own/fbox") || strings.Contains(fire[0], "dbox") { |
| + t.Fatalf("A got %v, want one mailer_unarmed fire for fleets/own/fbox, none for the detached dbox", fire) |
| + } |
| + if n := ef.count(t, `SELECT COUNT(*) FROM alert_incidents WHERE closed_at IS NULL AND rule = 'mailer_unarmed'`); n != 1 { |
| + t.Fatalf("%d mailer_unarmed incidents open, want 1 (fbox only)", n) |
| + } |
| + if n := ef.count(t, `SELECT COUNT(*) FROM alert_incidents WHERE rule = 'box_silent'`); n != 0 { |
| + t.Fatalf("%d box_silent incidents, want 0", n) |
| + } |
| +} |
diff --git a/internal/console/alertsignals.go b/internal/console/alertsignals.go| index 326146986..2395973be 100644 |
| --- a/internal/console/alertsignals.go |
| +++ b/internal/console/alertsignals.go |
| @@ -466,10 +466,14 @@ func (s *Server) mailerUnarmedPass(ctx context.Context, rows []status.Box, now t |
| if unread == nil { |
| unread = map[string]bool{} |
| } |
| - var fleets []registry.Fleet |
| + var ( |
| + fleets []registry.Fleet |
| + reg consoleRegistry |
| + ) |
| fleetsRead := true |
| if s.cfg.Registry != "" { |
| - reg, err := s.openPassRegistry() |
| + var err error |
| + reg, err = s.openPassRegistry() |
| if err == nil { |
| fleets, err = reg.ListFleets() |
| } |
| @@ -501,6 +505,18 @@ func (s *Server) mailerUnarmedPass(ctx context.Context, rows []status.Box, now t |
| fleetsRead = false |
| continue |
| } |
| + // A box the registry holds detached is no longer the fleet's: its |
| + // report is neither signalled nor unknown, whatever it says. |
| + detached, err := detachedFleetBoxes(ctx, reg, f.Name) |
| + if err != nil { |
| + alertReadFailed(ctx, fmt.Errorf("list fleet %s boxes: %w", f.Name, err)) |
| + fleetsRead = false |
| + continue |
| + } |
| + fb = withoutDetached(fb, detached) |
| + for label := range detached { |
| + delete(funread, label) |
| + } |
| for _, b := range fb { |
| b.Label = "fleets/" + f.Name + "/" + b.Label |
| owner[b.Label] = f.Account |
diff --git a/internal/console/apisystemupdate.go b/internal/console/apisystemupdate.go| index 1a2c0b5eb..1e6ad729d 100644 |
| --- a/internal/console/apisystemupdate.go |
| +++ b/internal/console/apisystemupdate.go |
| @@ -272,10 +272,11 @@ func (s *Server) systemBoxes(ctx context.Context, strict bool) ([]status.Box, bo |
| } |
| func (s *Server) systemBoxesFiltered(ctx context.Context, strict, skipRetiredGroups bool) ([]status.Box, bool, error) { |
| - _, fleets, fleetsErr := s.systemFleets() |
| + reg, fleets, fleetsErr := s.systemFleets() |
| // The platform's heartbeats and each fleet's are read at once, then |
| // taken in the order they were always taken: the platform's, then the |
| - // fleets in registry order. |
| + // fleets in registry order. Each fleet's reports drop the boxes its |
| + // registry rows hold detached (withoutDetached). |
| type read struct { |
| boxes []status.Box |
| err error |
| @@ -289,6 +290,14 @@ func (s *Server) systemBoxesFiltered(ctx context.Context, strict, skipRetiredGro |
| reads := make([]read, len(prefixes)) |
| forEachLimit(len(prefixes), readConcurrency, func(i int) { |
| b, _, err := s.loadBoxes(ctx, s.cfg.Releases, prefixes[i]) |
| + if err == nil && i > 0 { |
| + fleet := fleets[i-1].Name |
| + detached, derr := detachedFleetBoxes(ctx, reg, fleet) |
| + if derr != nil { |
| + log.Printf("console: api system: fleet %s registry boxes: %v; showing every report", fleet, derr) |
| + } |
| + b = withoutDetached(b, detached) |
| + } |
| reads[i] = read{b, err} |
| }) |
| boxes, err := reads[0].boxes, reads[0].err |
| @@ -344,6 +353,50 @@ func (s *Server) systemFleets() (consoleRegistry, []registry.Fleet, error) { |
| return reg, fleets, nil |
| } |
| +// detachedFleetBoxes is the labels of fleet's boxes whose registry rows are |
| +// detached (carlos fleets detach-box). Nothing deletes a detached box's |
| +// report object, and a box detached while its machine still runs may go on |
| +// rewriting it, so without this a detached box is listed as unreachable, or |
| +// as a live box, for as long as the object is listed. A label added again |
| +// (add-box reactivates the row) is active and is no longer in the set. The |
| +// read is the console's own registry, no bucket request. |
| +func detachedFleetBoxes(ctx context.Context, reg consoleRegistry, fleet string) (map[string]bool, error) { |
| + if reg == nil { |
| + return nil, nil |
| + } |
| + rows, err := reg.ListFleetBoxesContext(ctx, fleet) |
| + if err != nil { |
| + return nil, err |
| + } |
| + var detached map[string]bool |
| + for _, r := range rows { |
| + if r.Status == "detached" { |
| + if detached == nil { |
| + detached = map[string]bool{} |
| + } |
| + detached[r.Label] = true |
| + } |
| + } |
| + return detached, nil |
| +} |
| + |
| +// withoutDetached is boxes without the ones whose Label is in detached, in |
| +// a new slice: boxes may be a request memo's (loadBoxes), shared with other |
| +// readers. A report under a label the registry has no row for is kept, as |
| +// before; only a row the registry holds detached hides its box. |
| +func withoutDetached(boxes []status.Box, detached map[string]bool) []status.Box { |
| + if len(detached) == 0 { |
| + return boxes |
| + } |
| + kept := make([]status.Box, 0, len(boxes)) |
| + for _, b := range boxes { |
| + if !detached[b.Label] { |
| + kept = append(kept, b) |
| + } |
| + } |
| + return kept |
| +} |
| + |
| // systemFleetsUnreadable is the shared answer to "this console cannot say |
| // what fleets exist": a refusal under strict, a logged degradation otherwise. |
| func (s *Server) systemFleetsUnreadable(boxes []status.Box, strict bool, err error) ([]status.Box, bool, error) { |
diff --git a/internal/console/apisystemupdate_test.go b/internal/console/apisystemupdate_test.go| index af66eb371..8d1c07181 100644 |
| --- a/internal/console/apisystemupdate_test.go |
| +++ b/internal/console/apisystemupdate_test.go |
| @@ -21,6 +21,7 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/publicrelease" |
| + "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/status" |
| ) |
| @@ -700,3 +701,71 @@ func TestSystemDoorsRefuseBelowTheRegionalFloor(t *testing.T) { |
| t.Fatalf("the override's rollback pointer = %+v", p) |
| } |
| } |
| + |
| +// TestSystemStatusHidesDetachedFleetBoxes: carlos fleets detach-box leaves |
| +// the box's report under fleets/<name>/status/boxes/, so system status |
| +// listed a detached box as UNREACHABLE for as long as the object stayed. |
| +// A fleet box whose registry row is detached is not listed; an active box, |
| +// fresh or stale, still is, and a stale one still reads stale; a label added |
| +// again shows again; and a platform box under the same label as a detached |
| +// fleet box is untouched. |
| +func TestSystemStatusHidesDetachedFleetBoxes(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + owner := cliToken(t, fx, "paul@keymail.dev", "code-sys-detached") |
| + now := time.Now() |
| + seedBox(t, fx, "gone", "linux-arm64", "stable", "v0.25.0") // a platform box |
| + |
| + reg := openTestReg(t, fx) |
| + t.Cleanup(func() { reg.Close() }) |
| + if err := reg.UpsertFleet(registry.Fleet{Name: "pi", Account: fx.sqid, Bucket: "pi-bucket"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + fleetStore := release.NewPrefixStore(fx.rel, "fleets/pi/") |
| + for _, c := range []struct { |
| + label string |
| + at time.Time |
| + }{{"live", now}, {"dark", now.Add(-20 * time.Minute)}, {"gone", now.Add(-20 * time.Minute)}} { |
| + if err := reg.AddFleetBox(registry.FleetBox{Fleet: "pi", Label: c.label, TokenHash: strings.Repeat("a", 64)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + b := status.CollectBox(nil, status.Probes{Release: "v0.25.0", Platform: "linux-amd64"}, c.at) |
| + b.Label = c.label |
| + if err := status.WriteBox(context.Background(), fleetStore, b); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := reg.SetFleetBoxStatus("pi", "gone", "detached"); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + rows := func() map[string][]bool { |
| + t.Helper() |
| + st, out := getJSON(t, fx.ts.URL+"/api/cli/system/status", owner) |
| + if st != http.StatusOK { |
| + t.Fatalf("status = %d %v", st, out) |
| + } |
| + if out["partial"] != false { |
| + t.Fatalf("partial = %v, want false", out["partial"]) |
| + } |
| + got := map[string][]bool{} |
| + for _, r := range out["boxes"].([]any) { |
| + row := r.(map[string]any) |
| + got[row["label"].(string)] = append(got[row["label"].(string)], row["stale"].(bool)) |
| + } |
| + return got |
| + } |
| + got := rows() |
| + want := map[string][]bool{"gone": {false}, "live": {false}, "dark": {true}} |
| + if !reflect.DeepEqual(got, want) { |
| + t.Fatalf("rows (label: stale) = %v, want %v: the platform's gone, the fleet's live and dark, never the detached fleet box", got, want) |
| + } |
| + |
| + // add-box reactivates the detached label: its report shows again. |
| + if err := reg.AddFleetBox(registry.FleetBox{Fleet: "pi", Label: "gone", TokenHash: strings.Repeat("b", 64)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + got = rows() |
| + if len(got["gone"]) != 2 { |
| + t.Fatalf("rows = %v, want the platform's gone and the reactivated fleet box gone", got) |
| + } |
| +} |