carlos / platform Public

region labels: the regionlabel package, names and constructors (plan 1 task 2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d8183e2341d45397aac7fd6767ab9594883f2dc7 parent 8ee5406
2 files changed, +310 −0
  • internal/regionlabel/regionlabel.go +165 −0
  • internal/regionlabel/regionlabel_test.go +145 −0
diff --git a/internal/regionlabel/regionlabel.go b/internal/regionlabel/regionlabel.go
new file mode 100644
index 000000000..d10388363
--- /dev/null
+++ b/internal/regionlabel/regionlabel.go
@@ -0,0 +1,165 @@
+// Package regionlabel names where an app or an instance runs, as a place
+// and a code: "London (uk)" (region labels spec
+// docs/superpowers/specs/2026-10-25-region-labels-design.md §3; Paul's
+// ruling of 2026-10-25). It is pure: it reads nothing. The console builds a
+// Names from one edges read and keeps it (internal/console/regionnames.go).
+package regionlabel
+
+import (
+ "strings"
+
+ "github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/geo"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// The label words (copy rows RC-1, RC-1b, RC-2, RC-4b).
+const (
+ fleetWord = "fleet "
+ poolWord = "pool "
+ homeText = "the home region"
+ everywhereText = "every region"
+ placeSep = " · "
+)
+
+// Names is what the edges files say about places: each display code's AWS
+// region (a majority of the boxes publishing it), and each box label's
+// placement code in the record vocabulary, where present-and-"" is the home
+// (labels 4). The zero Names knows nothing, so every label is its code.
+// A Names is never changed once built, so one may be shared freely.
+type Names struct {
+ aws map[string]string // display code -> AWS region
+ box map[string]string // box label -> placement code ("" the home)
+}
+
+// NamesFrom folds every edges file, fresh or stale. A placement of "" or
+// the home's name is the home; a fleet ("fleet/<name>") or a malformed
+// code is skipped. Each file votes its AWS region for its display code;
+// the majority wins and a tie goes to the lexically smaller region.
+func NamesFrom(snap edgesfile.Snapshot, home string) Names {
+ n := Names{aws: map[string]string{}, box: map[string]string{}}
+ votes := map[string]map[string]int{}
+ for _, f := range snap.Files {
+ code := f.Placement
+ if home != "" && code == home {
+ code = ""
+ }
+ if code != "" && !provision.ValidRegion(code) {
+ continue
+ }
+ if f.Box != "" {
+ n.box[f.Box] = code
+ }
+ display := code
+ if display == "" {
+ display = home
+ }
+ if display == "" || f.Region == "" {
+ continue
+ }
+ if votes[display] == nil {
+ votes[display] = map[string]int{}
+ }
+ votes[display][f.Region]++
+ }
+ for display, regions := range votes {
+ best, most := "", 0
+ for r, c := range regions {
+ if c > most || (c == most && r < best) {
+ best, most = r, c
+ }
+ }
+ n.aws[display] = best
+ }
+ return n
+}
+
+// BoxPlacement is a box's placement code in the record vocabulary. known
+// is false for a box no edges file names; ("", true) is the home.
+func (n Names) BoxPlacement(label string) (code string, known bool) {
+ code, known = n.box[label]
+ return code, known
+}
+
+// Label is one place as the surfaces show it. Text is what they print.
+type Label struct {
+ Code string // the display code: the home's name for "", the fleet/<name> value for a fleet
+ Place string // the full C23 place, "" when unknown
+ Fleet string // the customer fleet's name
+ Pool string // the record's or row's pool
+ Everywhere bool // served by every platform box (the everywhere class)
+ Text string
+}
+
+// City is a C23 place after its last " · ": "UK · London" is "London",
+// "Bahrain" is "Bahrain".
+func City(place string) string {
+ if i := strings.LastIndex(place, placeSep); i >= 0 {
+ return place[i+len(placeSep):]
+ }
+ return place
+}
+
+// ForCode labels a bare code: an app's stored region, a box's placement, a
+// move's endpoint. "" is the home.
+func ForCode(code, home string, n Names) Label {
+ if name, ok := registry.FleetPlacement(code); ok {
+ return Label{Code: code, Fleet: name, Text: fleetWord + name}
+ }
+ display := code
+ if display == "" {
+ display = home
+ }
+ if display == "" {
+ return Label{Text: homeText}
+ }
+ l := Label{Code: display, Text: display}
+ if r, ok := geo.LookupAWSRegion(n.aws[display]); ok {
+ l.Place = r.Place
+ l.Text = City(r.Place) + " (" + display + ")"
+ }
+ return l
+}
+
+// ForRecord labels an instance record: fleet first, then a pool of the
+// everywhere class, then the everywhere class, then its region.
+func ForRecord(rec provision.Record, home string, n Names) Label {
+ if _, ok := registry.FleetPlacement(rec.Region); ok {
+ return ForCode(rec.Region, home, n)
+ }
+ if provision.ServedEverywhere(rec) {
+ return everywhere(rec.Region, rec.Pool, home, n)
+ }
+ l := ForCode(rec.Region, home, n)
+ l.Pool = rec.Pool
+ return l
+}
+
+// ForRoute labels a registry row that has no record in hand, by the same
+// table, with the row's half of the class rule.
+func ForRoute(rt registry.Route, home string, n Names) Label {
+ if _, ok := registry.FleetPlacement(rt.Region); ok {
+ return ForCode(rt.Region, home, n)
+ }
+ if provision.RouteServedEverywhere(rt) {
+ return everywhere(rt.Region, rt.Pool, home, n)
+ }
+ l := ForCode(rt.Region, home, n)
+ l.Pool = rt.Pool
+ return l
+}
+
+// everywhere is the everywhere class's label: "pool <name>" for a named
+// pool, else "every region". Code and Place still say where the record
+// lives.
+func everywhere(code, pool, home string, n Names) Label {
+ l := ForCode(code, home, n)
+ l.Everywhere, l.Pool = true, pool
+ if pool != "" {
+ l.Text = poolWord + pool
+ } else {
+ l.Text = everywhereText
+ }
+ return l
+}
diff --git a/internal/regionlabel/regionlabel_test.go b/internal/regionlabel/regionlabel_test.go
new file mode 100644
index 000000000..8d8839e7a
--- /dev/null
+++ b/internal/regionlabel/regionlabel_test.go
@@ -0,0 +1,145 @@
+package regionlabel
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/edgesfile"
+ "github.com/carlosframework/platform/internal/geo"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// names is a deployment with its home in ie, a box in uk and a box in au.
+func names() Names {
+ return NamesFrom(edgesfile.Snapshot{Complete: true, Files: []edgesfile.PlacementView{
+ {V: 1, Box: "box-ie", Placement: "", Region: "eu-west-1"},
+ {V: 1, Box: "box-uk", Placement: "uk", Region: "eu-west-2"},
+ {V: 1, Box: "box-au", Placement: "au", Region: "ap-southeast-2"},
+ }}, "ie")
+}
+
+func rec(region, pool, kind string) provision.Record {
+ return provision.Record{Schema: 1, Host: "a.shop.example.com", App: "shop", Account: "bab",
+ Region: region, Pool: pool, Kind: kind, Channel: "stable"}
+}
+
+// TestLabelTableInOrder is §3.1's table, first match wins, row by row.
+func TestLabelTableInOrder(t *testing.T) {
+ t.Parallel()
+ n := names()
+ cases := []struct {
+ name string
+ got Label
+ want string
+ }{
+ {"a static record on a fleet: fleet first", ForRecord(rec("fleet/private", "shop", "static"), "ie", n), "fleet private"},
+ {"a pooled static record: pool", ForRecord(rec("uk", "shop", "static"), "ie", n), "pool shop"},
+ {"a static record with no pool: every region", ForRecord(rec("uk", "", "static"), "ie", n), "every region"},
+ {"a pooled socket-backed record keeps its region", ForRecord(rec("uk", "shop", ""), "ie", n), "London (uk)"},
+ {"the home's record", ForRecord(rec("", "", ""), "ie", n), "Dublin (ie)"},
+ {"no home named", ForCode("", "", n), "the home region"},
+ {"a code the table knows", ForCode("au", "ie", n), "Sydney (au)"},
+ {"the home's name as a code", ForCode("ie", "ie", n), "Dublin (ie)"},
+ {"a code no box publishes", ForCode("nz", "ie", n), "nz"},
+ {"a registry-only everywhere row", ForRoute(registry.Route{Host: "s.shop.example.com", Kind: "static", Region: "uk"}, "ie", n), "every region"},
+ {"a registry-only pooled everywhere row", ForRoute(registry.Route{Host: "s.shop.example.com", Kind: "static", Region: "uk", Pool: "shop"}, "ie", n), "pool shop"},
+ {"a registry-only socket row", ForRoute(registry.Route{Host: "e.shop.example.com", Kind: "instance", Region: "uk"}, "ie", n), "London (uk)"},
+ {"a registry-only fleet row", ForRoute(registry.Route{Host: "e.shop.example.com", Kind: "static", Region: "fleet/private"}, "ie", n), "fleet private"},
+ }
+ for _, c := range cases {
+ if c.got.Text != c.want {
+ t.Errorf("%s: Text = %q, want %q (%+v)", c.name, c.got.Text, c.want, c.got)
+ }
+ }
+ if l := ForCode("uk", "ie", n); l.Code != "uk" || l.Place != "UK · London" || l.Fleet != "" || l.Everywhere {
+ t.Fatalf("ForCode(uk) = %+v", l)
+ }
+ if l := ForRecord(rec("uk", "shop", "static"), "ie", n); !l.Everywhere || l.Pool != "shop" || l.Code != "uk" {
+ t.Fatalf("a pooled static record = %+v: the code and place still say where its record lives", l)
+ }
+ if l := ForCode("fleet/private", "ie", n); l.Fleet != "private" || l.Code != "fleet/private" {
+ t.Fatalf("ForCode(fleet/private) = %+v", l)
+ }
+}
+
+// TestForCodeWithoutAPlaceIsTheCode: Review Focus 1. Before the map is
+// read, or for a code no box publishes, the label is the code, never empty.
+func TestForCodeWithoutAPlaceIsTheCode(t *testing.T) {
+ t.Parallel()
+ for _, code := range []string{"uk", "au", "nz", "ie"} {
+ if l := ForCode(code, "ie", Names{}); l.Text != code || l.Place != "" {
+ t.Errorf("ForCode(%q) with no names = %+v, want the bare code", code, l)
+ }
+ }
+ if l := ForCode("", "ie", Names{}); l.Text != "ie" {
+ t.Errorf("the home with no names = %+v, want its name", l)
+ }
+}
+
+// TestCityOverEveryPlace is §10's city rule over all of C23.
+func TestCityOverEveryPlace(t *testing.T) {
+ t.Parallel()
+ for _, code := range geo.AWSRegionCodes() {
+ r, _ := geo.LookupAWSRegion(code)
+ city := City(r.Place)
+ if city == "" || strings.Contains(city, "·") || !strings.HasSuffix(r.Place, city) {
+ t.Errorf("%s: City(%q) = %q", code, r.Place, city)
+ }
+ }
+ for place, want := range map[string]string{
+ "UK · London": "London", "US East · N. Virginia": "N. Virginia", "Bahrain": "Bahrain", "UAE": "UAE",
+ } {
+ if got := City(place); got != want {
+ t.Errorf("City(%q) = %q, want %q", place, got, want)
+ }
+ }
+}
+
+// TestNamesFromFolds: home folding, fleets and malformed codes skipped,
+// stale files counted, majority, ties to the lexically smaller region.
+func TestNamesFromFolds(t *testing.T) {
+ t.Parallel()
+ n := NamesFrom(edgesfile.Snapshot{Files: []edgesfile.PlacementView{
+ {V: 1, Box: "home-1", Placement: "", Region: "eu-west-1"},
+ {V: 1, Box: "home-2", Placement: "ie", Region: "eu-west-1", Fresh: false}, // the home's name folds to ""; stale still counts
+ {V: 1, Box: "uk-1", Placement: "uk", Region: "eu-west-2"},
+ {V: 1, Box: "uk-2", Placement: "uk", Region: "eu-west-2"},
+ {V: 1, Box: "uk-3", Placement: "uk", Region: "eu-west-3"}, // outvoted
+ {V: 1, Box: "tie-1", Placement: "tie", Region: "eu-west-3"},
+ {V: 1, Box: "tie-2", Placement: "tie", Region: "eu-central-1"}, // tie: eu-central-1 < eu-west-3
+ {V: 1, Box: "pi-1", Placement: "fleet/pi", Region: "eu-west-1"},
+ {V: 1, Box: "bad-1", Placement: "UK!", Region: "eu-west-2"},
+ }}, "ie")
+ for code, want := range map[string]string{"ie": "Dublin (ie)", "uk": "London (uk)", "tie": "Frankfurt (tie)"} {
+ if got := ForCode(code, "ie", n).Text; got != want {
+ t.Errorf("%s: %q, want %q", code, got, want)
+ }
+ }
+ for label, want := range map[string]string{"home-1": "", "home-2": "", "uk-3": "uk"} {
+ if code, known := n.BoxPlacement(label); !known || code != want {
+ t.Errorf("BoxPlacement(%s) = %q %v, want %q true", label, code, known, want)
+ }
+ }
+ for _, label := range []string{"pi-1", "bad-1"} {
+ if _, known := n.BoxPlacement(label); known {
+ t.Errorf("%s: a fleet or malformed placement must not be in the box map", label)
+ }
+ }
+}
+
+// TestBoxPlacementKnownHomeVersusUnknown (labels 4): a home box is known
+// with code "", which is not the same answer as a box with no entry.
+func TestBoxPlacementKnownHomeVersusUnknown(t *testing.T) {
+ t.Parallel()
+ n := names()
+ if code, known := n.BoxPlacement("box-ie"); !known || code != "" {
+ t.Fatalf("home box = %q %v, want \"\" true", code, known)
+ }
+ if code, known := n.BoxPlacement("box-gone"); known || code != "" {
+ t.Fatalf("unknown box = %q %v, want \"\" false", code, known)
+ }
+ if code, known := (Names{}).BoxPlacement("box-ie"); known || code != "" {
+ t.Fatal("the zero Names knows no box")
+ }
+}