carlos / platform Public

instances list: fleet-placed health from the fleet box (#366)

`carlos instances list` showed every fleet-placed instance as "Can't
tell right now": instanceHealth reads only the platform's box reports,
and a fleet-placed record with no such report is unknown on purpose.
The list already loads the caller's own fleets' rollout reports
(loadRolloutJoin) for its rollout column. fleetHealth now takes the word
from that join for each fleet-placed record still unknown, through
join.rollout and so its tenant rule, skipping a view that is Unavailable
(which covers a refused view) or Transient, and wording it as the
platform-remote branch does: healthWord with "failed" for a host held
down (AttemptDown), the box's awake as the reading. Existing words only.

The Running tab (remoteRoute) is unchanged: its remote facts come only
from the cached platform box reports (pageBoxReports;
TestRemoteFactsOneAccessor pins that one accessor), and the fleet
reports are an uncached per-request read, so using them there adds
fleet-box reads to every page view.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 7545ef9a3d22112b0bd67d688f5a7150200be7b7 parent a19a0c4
2 files changed, +150 −0
  • internal/console/apiinstances.go +36 −0
  • internal/console/fleethealth_test.go +114 −0
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go
index e4f71c70e..cb0e63b46 100644
--- a/internal/console/apiinstances.go
+++ b/internal/console/apiinstances.go
@@ -476,6 +476,41 @@ func (s *Server) instanceHealth(ctx context.Context, recs []provision.Record) ma
return out
}
+// fleetHealth fills in, from join, the health of each record placed on a
+// customer fleet that instanceHealth left unknown: the platform's boxes do
+// not report fleet-placed hosts, but the fleet's own boxes' rollout reports
+// do, and join (loadRolloutJoin) reads them for the caller's own fleets
+// only. join.rollout keeps its tenant rule: a report is used only when it
+// names this account and app. A view whose box could not arm its scheduler
+// (Unavailable, which also covers a refused view with no tenant to check)
+// or whose host is starting or stopping (Transient) says nothing about the
+// host's state, and the word stays unknown. Otherwise the word is the
+// platform-remote one (instanceHealth): awake is Running, a host held down
+// after a failed hold-mode build is Failed.
+func fleetHealth(health map[string]string, recs []provision.Record, join boxJoin, acct, app string) {
+ for _, rec := range recs {
+ if health[rec.Host] != healthUnknown {
+ continue
+ }
+ if _, fleetPlaced := registry.FleetPlacement(rec.Region); !fleetPlaced {
+ continue
+ }
+ v := join.rollout(rec.Host, acct, app)
+ if v == nil || v.Unavailable != "" || v.Transient {
+ continue
+ }
+ kind := rec.Kind
+ if kind == "" {
+ kind = "instance"
+ }
+ st := "ready"
+ if v.Attempt == status.AttemptDown {
+ st = "failed"
+ }
+ health[rec.Host] = healthWord(kind, st, rec.HibernateOrDefault(), v.Awake, true)
+ }
+}
+
// healthByHost is the metrics half of a health answer, shared by
// instanceHealth (records joined to routes) and handleAPIRoutes (routes
// directly, issue #64) so the two — and the Running tab, which uses the
@@ -1193,6 +1228,7 @@ func (s *Server) handleAPIInstanceList(w http.ResponseWriter, r *http.Request) {
}
}
health := s.instanceHealth(ctx, recs)
+ fleetHealth(health, recs, join, acct.Sqid, app)
counts := map[string]int{}
matched := make([]instanceListRecord, 0, len(recs))
home, names := s.regionHome(), s.regionNames()
diff --git a/internal/console/fleethealth_test.go b/internal/console/fleethealth_test.go
new file mode 100644
index 000000000..5ddff5fcb
--- /dev/null
+++ b/internal/console/fleethealth_test.go
@@ -0,0 +1,114 @@
+package console
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/status"
+)
+
+// writeFleetRolloutBox registers fleet to owner and writes, under the
+// fleet's own prefix, a box named machine reporting ro (and versions as its
+// route versions, which an Unavailable report's hosts come from).
+func writeFleetRolloutBox(t *testing.T, fx *consoleFixture, fleet, owner, machine string, ro *status.RolloutReport, versions map[string]string) {
+ t.Helper()
+ reg, err := registry.Open(fx.regPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer reg.Close()
+ if err := reg.UpsertFleet(registry.Fleet{Name: fleet, Account: owner, Bucket: fleet + "-bucket"}); err != nil {
+ t.Fatal(err)
+ }
+ box := status.CollectBox(nil, status.Probes{MachineID: machine}, time.Now())
+ box.Metrics = &status.BoxMetrics{Rollout: ro, RouteVersions: versions}
+ if err := status.WriteBox(context.Background(), release.NewPrefixStore(fx.rel, "fleets/"+fleet+"/"), box); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// listHealth is the instance list's health word per host.
+func listHealth(t *testing.T, fx *consoleFixture, token, app string) map[string]string {
+ t.Helper()
+ code, _, out := rawReq(t, http.MethodGet, fx.ts.URL+"/api/cli/apps/"+fx.sqid+"/"+app+"/instances", token, "")
+ if code != http.StatusOK {
+ t.Fatalf("list = %d %s", code, out)
+ }
+ var list struct {
+ Instances []struct {
+ Host string `json:"host"`
+ Health string `json:"health"`
+ } `json:"instances"`
+ }
+ if err := json.Unmarshal([]byte(out), &list); err != nil {
+ t.Fatal(err)
+ }
+ got := map[string]string{}
+ for _, r := range list.Instances {
+ got[r.Host] = r.Health
+ }
+ return got
+}
+
+// #366 Task 11 follow-up: a record placed on the caller's own fleet takes
+// its health word from that fleet box's rollout report (the join the
+// list's rollout column reads), as a platform-remote record takes its word
+// from its box: awake is Running, a hibernating instance not awake is
+// Asleep, a host held down is Failed. Everything the join cannot vouch for
+// stays "Can't tell right now": a host starting or stopping, a report
+// naming another app, a box that could not arm its scheduler, a host no
+// box reports, and another account's fleet box naming this app's host.
+func TestFleetPlacedHealthComesFromItsFleetBox(t *testing.T) {
+ fx := newConsoleFixture(t)
+ const app = "kass"
+ host := func(slug string) string { return slug + ".kass.carlosapp.dev" }
+ asleep := true
+ for _, slug := range []string{"run", "nap", "down", "busy", "other", "gone", "stranger", "unavail"} {
+ place := registry.PlacementTarget("pi")
+ if slug == "stranger" {
+ place = registry.PlacementTarget("stranger")
+ }
+ if err := provision.CreateRecord(context.Background(), fx.rel, provision.Record{
+ Schema: 1, Host: host(slug), Slug: slug, App: app, Account: fx.sqid, Region: place,
+ Channel: "stable", Hibernate: &asleep, CreatedAt: time.Now().UTC(), CreatedBy: "paul@keymail.dev",
+ }); err != nil {
+ t.Fatal(err)
+ }
+ }
+ mine := func(hr status.HostRollout) status.HostRollout {
+ hr.Account, hr.App, hr.Installed = fx.sqid, app, "v1"
+ return hr
+ }
+ writeFleetRolloutBox(t, fx, "pi", fx.sqid, "pi-box", &status.RolloutReport{Hosts: map[string]status.HostRollout{
+ host("run"): mine(status.HostRollout{Awake: true}),
+ host("nap"): mine(status.HostRollout{}),
+ host("down"): mine(status.HostRollout{Attempt: status.AttemptDown, AttemptBuild: "v1"}),
+ host("busy"): mine(status.HostRollout{Transient: true}),
+ host("other"): {Account: fx.sqid, App: "other", Installed: "v1", Awake: true},
+ }}, nil)
+ writeFleetRolloutBox(t, fx, "pi", fx.sqid, "pi-box-2", &status.RolloutReport{Unavailable: "flag-off"},
+ map[string]string{host("unavail"): "v1"})
+ writeFleetRolloutBox(t, fx, "stranger", "zzz", "stranger-box", &status.RolloutReport{Hosts: map[string]status.HostRollout{
+ host("stranger"): mine(status.HostRollout{Awake: true}),
+ }}, nil)
+ token := cliToken(t, fx, "paul@keymail.dev", "code-fleet-health")
+
+ got := listHealth(t, fx, token, app)
+ want := map[string]string{
+ host("run"): "Running", host("nap"): "Asleep", host("down"): "Failed",
+ host("busy"): healthUnknown, host("other"): healthUnknown, host("gone"): healthUnknown,
+ host("stranger"): healthUnknown, host("unavail"): healthUnknown,
+ }
+ for h, w := range want {
+ if got[h] != w {
+ t.Errorf("%s health = %q, want %q", strings.SplitN(h, ".", 2)[0], got[h], w)
+ }
+ }
+}