carlos / platform Public

appconfig: one classifier for where a config write lands

ClassifyWrite sorts every route a config write reaches into the console
secrets spec's buckets (updates itself, restarted by Carloku, restart it
yourself, picks it up when it starts, can't see from here) plus the
blocked, couldn't-check and doesn't-get-these exceptions, from the same
predicates the delivery passes use. AwaitingRestart is now its Pending()
projection; the existing AwaitingRestart and BlockedStamps tests pass
unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 20d3367bb95d88795996295ba321a35ed6b85c20 parent 86ebd76
3 files changed, +946 −170
  • internal/appconfig/reach.go +394 −0
  • internal/appconfig/reach_test.go +548 −0
  • internal/appconfig/syncexec.go +4 −170

Intent

Human

<task-notification> <task-id>a30f707594acd3b8b</task-id> <tool-use-id>toolu_015TqFUh1rBUDr1z2dyynHF9</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/abe35d56-e4c2-40df-9005-6e66b76b1f50/tasks/a30f707594acd3b8b.output</output-file> <status>completed</status> <summary>Agent "Implement Task 6 browser sealing" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE. The browser sealing script, the plaintext check and all tests are built and committed. Commit: 86ebd76b `console: seal secrets in the browser with the app's key` (only my six files under internal/console). Tests: the six configseal tests and the TestOnlySealKeyForBuildsSealers guard all pass; gofmt and vet are clean. Red was shown afterwards by five mutations, each caught. Concerns: 1. I wrote the script before its tests, so there was no failing run first. Red came from mutations: v2 info string, AAD byte order, a dropped backslash rule, plaintext not cleared, empty secret allowed. 2. The brief left the DOM attribute names open, so I designed them; Task 7 must render to that contract (written down in the report). It covers `data-secret-for`/`data-name-from` on the Secret box, inputs keyed by id, `data-seal-skip` on buttons that save nothing (Remove, Discard), and `data-msg-*` overrides for the error texts. 3. The script's three default error texts are person-facing copy nobody has reviewed yet; the Task 9 copy pass should include them. 4. The DOM wiring has only been run in node against a fake DOM, not real Chromium; the page task should add a headless-browser check. 5. The committed vectors file holds a test-only private key, labelled as such, but secret scanners may still flag it. 6. Size limits are left to the server door; the browser does not check them. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/design-console-secrets/.superpowers/sdd/2026-11-08-console-secrets/task-6-report.md</result> <usage><subagent_tokens>97846</subagent_tokens><tool_uses>21</tool_uses><duration_ms>739841</duration_ms></usage> </task-notification>

Attributed to this commit: 13.6 g CO₂e (1,671,634 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-console-secrets, 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/appconfig/reach.go b/internal/appconfig/reach.go
new file mode 100644
index 000000000..aae1a6af8
--- /dev/null
+++ b/internal/appconfig/reach.go
@@ -0,0 +1,394 @@
+package appconfig
+
+import (
+ "context"
+ "sort"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// The shared delivery classifier (spec 2026-11-08-console-secrets-design.md,
+// "How Save applies"): for one config write, where does each route it
+// reaches stand? The CLI's restart notice (AwaitingRestart, a projection of
+// this) and the console's Save outcome are the same computation, so the two
+// cannot disagree about a route.
+
+// ReachShape is the delivery shape of a route, one per row of the spec's
+// table. Closed: "unit", "stamped", "socket", "sidecar", "addr".
+type ReachShape string
+
+const (
+ ReachShapeUnit ReachShape = "unit" // carlos-app@ unit (Backing == BackingUnit)
+ ReachShapeStamped ReachShape = "stamped" // stampedForDelivery
+ ReachShapeSocket ReachShape = "socket" // execSyncable
+ ReachShapeSidecar ReachShape = "sidecar" // execSidecarCandidate
+ ReachShapeAddr ReachShape = "addr" // execAddrCandidate
+)
+
+// ReachBucket names the spec's five buckets.
+type ReachBucket int
+
+const (
+ ReachSelf ReachBucket = 1 // delivery restarts it
+ ReachCarloku ReachBucket = 2 // always empty in this slice
+ ReachYours ReachBucket = 3 // the file lands, nothing here restarts it
+ ReachStarting ReachBucket = 4 // picks it up when it starts
+ ReachUnseen ReachBucket = 5 // a record with no route on this box
+)
+
+// ReachHow says how a ReachYours route gets restarted.
+type ReachHow string
+
+const (
+ ReachHowAddr ReachHow = "addr" // restart it the way you started it
+ ReachHowSidecar ReachHow = "sidecar" // check Running
+)
+
+// ReachWhy says why a ReachStarting route is waiting.
+type ReachWhy string
+
+const (
+ ReachWhyAsleep ReachWhy = "asleep"
+ ReachWhyNotRunning ReachWhy = "not-running"
+)
+
+// ReachHost is one route the write reaches. Unit is carried for the
+// AwaitingRestart projection (RestartHost.Unit decides the remedy).
+type ReachHost struct {
+ Host string
+ Unit string
+ Shape ReachShape
+ How ReachHow
+ Why ReachWhy
+}
+
+// ReachRecord is one provision record of the app: fleet-wide, no shape.
+type ReachRecord struct{ Host, Environment string }
+
+// HostFact holds only what a qualifying box report established for a host.
+// The zero value is "unknown", never "awake and unmanaged" by assertion:
+// unknown management sends a sidecar to ReachYours, unknown sleep leaves a
+// route in its shape's bucket.
+type HostFact struct{ Managed, Asleep bool }
+
+// ReachFacts are the sources beyond this box's registry.
+type ReachFacts struct {
+ Records []ReachRecord // every provision record of the app
+ RecordsComplete bool // false: the listing failed or was truncated
+ Hosts map[string]HostFact
+}
+
+// WriteReach is ClassifyWrite's answer. Every reached route is in exactly
+// one of Self, Yours, Starting, Blocked, Unresolved or Undelivered; Unseen
+// holds record hosts with no route in the registry it was shown. A route
+// whose touched keys are all shadowed above the written bundle is in none.
+type WriteReach struct {
+ Self, Carloku, Yours, Starting []ReachHost // Carloku is always empty in this slice
+ Unseen []string
+ Blocked []BlockedHost
+ Unresolved []ReachHost // Shape kept so the projection can filter
+ Undelivered []string
+ RecordsComplete bool
+}
+
+// reachShape is the one place a route's delivery shape is decided, in the
+// order the passes themselves decide it: stamped before the carlos-app@
+// unit (stampedForDelivery excludes a unit-backed stamp, which stays on the
+// carlos-app@ path), and SyncExec's inScope order for the exec shapes
+// (socket, then sidecar, then addr). ok is false for a route no pass
+// delivers config to at all.
+func reachShape(rt registry.Route) (ReachShape, bool) {
+ switch {
+ case stampedForDelivery(rt):
+ return ReachShapeStamped, true
+ case unitDelivered(rt) && rt.Backing == registry.BackingUnit:
+ return ReachShapeUnit, true
+ case execSyncable(rt):
+ return ReachShapeSocket, true
+ case execSidecarCandidate(rt):
+ return ReachShapeSidecar, true
+ case execAddrCandidate(rt):
+ return ReachShapeAddr, true
+ }
+ return "", false
+}
+
+// ClassifyWrite sorts every route a config write to app's envName bundle
+// ("" = the default) reaches into the spec's buckets. keys are the var
+// names the write touched (set and unset together); an empty slice disables
+// the shadowing test, as in AwaitingRestart.
+//
+// Sources, and their limits:
+// - routes: the registry of THIS box only. A route on another box is not
+// here, which is why Unseen exists and why callers say what they looked
+// at.
+// - facts.Records: the app's provision records, fleet-wide, with no route
+// shape. A record host with no route here goes to Unseen: unknown,
+// never unused. RecordsComplete false means the listing itself was
+// short, and is passed through for the caller to say so.
+// - facts.Hosts: box status reports, possibly incomplete or stale.
+// Incomplete means unknown: no fact is never "asleep" and never
+// "managed".
+//
+// Per route, in order: the (acct, app) and environment narrowing; an owned
+// environment's standalone boundary; the door-side rails (Blocked, stamped
+// only); the bound bundle's read (Unresolved when it fails or is missing);
+// then per shape, the addr opt-in (Undelivered when off) or the layers the
+// self-delivering passes need (Unresolved when one will not read); the
+// shadowing test; the bucket.
+func ClassifyWrite(ctx context.Context, s release.Store, routes []registry.Route, facts ReachFacts, acct, app, envName string, keys []string) WriteReach {
+ out := WriteReach{RecordsComplete: facts.RecordsComplete}
+ // The door-side rails over the full box list; a blocked host is
+ // reported only once the loop below has established that this write
+ // reaches it (environment narrowing, and an owned environment's
+ // standalone boundary).
+ blocked := map[string]string{}
+ for _, b := range BlockedStamps(routes, acct, app, envName) {
+ blocked[b.Host] = b.Reason
+ }
+ // The instance layer, through the same loader the sweeps use, so this
+ // classification and the passes that write the files agree about what
+ // sits above a bundle. Lives for one call, like the caches below.
+ overlayFor := instanceOverlayLoader(ctx, s)
+ // One read per bundle per call, however many routes share it.
+ var (
+ defM *Manifest
+ defFound bool
+ defErr error
+ defDone bool
+ )
+ loadDefault := func() (*Manifest, bool, error) {
+ if !defDone {
+ defM, defFound, defErr = getIfExists(ctx, s, acct, app)
+ defDone = true
+ }
+ return defM, defFound, defErr
+ }
+ type envRes struct {
+ m *Manifest
+ found bool
+ err error
+ }
+ envBundles := map[string]envRes{}
+ loadEnv := func(env string) envRes {
+ if e, done := envBundles[env]; done {
+ return e
+ }
+ m, found, err := getIfExistsAt(ctx, s, EnvironmentKey(acct, app, env))
+ e := envRes{m: m, found: found, err: err}
+ envBundles[env] = e
+ return e
+ }
+ // above lists the layers over the written bundle for a route bound to
+ // env: a default-bundle write sits under the named bundle and the
+ // instance overlay, a named-bundle write under the overlay alone.
+ above := func(env string, eb envRes, inst *Manifest) []*Manifest {
+ var l []*Manifest
+ if envName == "" && env != "" {
+ l = append(l, eb.m)
+ }
+ return append(l, inst)
+ }
+
+ for _, rt := range routes {
+ shape, ok := reachShape(rt)
+ if !ok || rt.App != app || release.RouteAccount(rt) != acct {
+ continue
+ }
+ // Which routes a write REACHES. A named bundle is read by the
+ // routes bound to it and by nothing else. The default bundle is
+ // read by every route of the app, bound or not, because a named
+ // bundle is an overlay ON it (environments.go, LoadMerged).
+ if envName != "" && rt.Environment != envName {
+ continue
+ }
+ h := ReachHost{Host: rt.Host, Unit: rt.Unit, Shape: shape}
+ eb := envRes{}
+ def := loadDefault
+ standalone := false
+ if rt.Environment != "" {
+ eb = loadEnv(rt.Environment)
+ // An OWNED environment is standalone (team-scoped credentials
+ // §3): a default-bundle write reaches none of its routes, and
+ // its opt-in never inherits the default bundle's. A bundle that
+ // cannot be read decides nothing here.
+ if eb.err == nil && eb.found && eb.m.Owned() {
+ if envName == "" {
+ continue
+ }
+ def = standaloneDefault
+ standalone = true
+ }
+ }
+ // Blocked is decided before bundle resolution (spec §3.4 of the
+ // named-unit design): a route the root pass will refuse is reported
+ // as blocked, not as healthy and not as unresolved.
+ // Keyed by host, which a registry holds once.
+ if reason, bad := blocked[rt.Host]; bad {
+ out.Blocked = append(out.Blocked, BlockedHost{Host: rt.Host, Reason: reason})
+ continue
+ }
+ if rt.Environment != "" && (eb.err != nil || !eb.found) {
+ // A bound bundle that will not read, or does not exist: LoadMerged
+ // refuses the route and every pass leaves its file as it was.
+ // Nothing was delivered, and the route is not fine either.
+ out.Unresolved = append(out.Unresolved, h)
+ continue
+ }
+
+ if shape == ReachShapeAddr {
+ v, err := resolveExecDelivery(eb.m, eb.found, def)
+ if err != nil {
+ out.Unresolved = append(out.Unresolved, h)
+ continue
+ }
+ // Not opted in, including the Unknown case, which decides
+ // "off": no pass delivers a file to this route.
+ if !v.On {
+ out.Undelivered = append(out.Undelivered, rt.Host)
+ continue
+ }
+ // The overlay is consulted only when the shadowing test could
+ // change the verdict (as AwaitingRestart always has): an addr
+ // route's delivery is a file, and an empty keys asks nothing.
+ if len(keys) > 0 {
+ inst, _, instErr := overlayFor(acct, app, rt.Host)
+ if instErr != nil {
+ out.Unresolved = append(out.Unresolved, h)
+ continue
+ }
+ if allShadowed(keys, above(rt.Environment, eb, inst)...) {
+ continue
+ }
+ }
+ // An addr route's process is the app's own supervisor's; a
+ // sleep fact does not change who restarts it.
+ h.How = ReachHowAddr
+ out.Yours = append(out.Yours, h)
+ continue
+ }
+
+ // The self-delivering shapes (stamped, unit, socket, sidecar). Each
+ // pass renders from the default bundle (unless an owned environment
+ // stands alone) and the instance overlay, and delivers nothing when
+ // either will not read; so neither can it restart anything.
+ if !standalone {
+ if _, _, err := loadDefault(); err != nil {
+ out.Unresolved = append(out.Unresolved, h)
+ continue
+ }
+ }
+ inst, _, instErr := overlayFor(acct, app, rt.Host)
+ if instErr != nil {
+ out.Unresolved = append(out.Unresolved, h)
+ continue
+ }
+ // Shadowed: the merged file is byte-identical, the pass writes
+ // nothing and restarts nothing. Not reported at all.
+ if len(keys) > 0 && allShadowed(keys, above(rt.Environment, eb, inst)...) {
+ continue
+ }
+ fact := facts.Hosts[rt.Host] // a nil map reads as no fact
+ bucket := ReachSelf
+ if shape == ReachShapeSidecar && !fact.Managed {
+ // Only a box whose rollout manager manages the sidecar restarts
+ // it on delivery; unknown is not managed.
+ bucket = ReachYours
+ h.How = ReachHowSidecar
+ }
+ // Starting overrides Self and a sidecar's Yours, never stamped (its
+ // unit is restarted by the root pass whatever the activator thinks)
+ // and never addr (above). The registry status test cannot fire for
+ // today's predicates, which all require a ready row; it is here so
+ // a predicate that ever admits one classifies it honestly.
+ if shape != ReachShapeStamped {
+ switch {
+ case fact.Asleep:
+ bucket, h.How, h.Why = ReachStarting, "", ReachWhyAsleep
+ case rt.Status != "" && rt.Status != "ready":
+ bucket, h.How, h.Why = ReachStarting, "", ReachWhyNotRunning
+ }
+ }
+ switch bucket {
+ case ReachSelf:
+ out.Self = append(out.Self, h)
+ case ReachYours:
+ out.Yours = append(out.Yours, h)
+ case ReachStarting:
+ out.Starting = append(out.Starting, h)
+ }
+ }
+
+ // Unseen: record hosts with no route in what this call was shown, under
+ // the same environment narrowing, owned-environment rule and shadowing
+ // test as a route. A layer that will not read leaves the host in Unseen:
+ // unknown, never dropped.
+ local := map[string]bool{}
+ for _, rt := range routes {
+ local[rt.Host] = true
+ }
+ seen := map[string]bool{}
+ for _, rec := range facts.Records {
+ if rec.Host == "" || local[rec.Host] || seen[rec.Host] {
+ continue
+ }
+ if envName != "" && rec.Environment != envName {
+ continue
+ }
+ eb := envRes{}
+ if rec.Environment != "" {
+ eb = loadEnv(rec.Environment)
+ if eb.err == nil && eb.found && eb.m.Owned() && envName == "" {
+ continue
+ }
+ }
+ if len(keys) > 0 && eb.err == nil {
+ inst, _, instErr := overlayFor(acct, app, rec.Host)
+ if instErr == nil && allShadowed(keys, above(rec.Environment, eb, inst)...) {
+ continue
+ }
+ }
+ seen[rec.Host] = true
+ out.Unseen = append(out.Unseen, rec.Host)
+ }
+
+ // Sorted so a caller's output, and any test pinning it, is stable
+ // across passes, where registry order is not.
+ for _, l := range [][]ReachHost{out.Self, out.Yours, out.Starting, out.Unresolved} {
+ sortReachHosts(l)
+ }
+ sort.Slice(out.Blocked, func(i, j int) bool { return out.Blocked[i].Host < out.Blocked[j].Host })
+ sort.Strings(out.Unseen)
+ sort.Strings(out.Undelivered)
+ return out
+}
+
+func sortReachHosts(l []ReachHost) {
+ sort.Slice(l, func(i, j int) bool { return l[i].Host < l[j].Host })
+}
+
+// Pending is the AwaitingRestart projection: what the CLI's restart notice
+// has always said, read off the classification. Hosts are the opted-in addr
+// routes (the files nothing here restarts); Unresolved is narrowed to the
+// addr and stamped shapes, the ones the notice has always covered.
+func (w WriteReach) Pending() RestartPending {
+ var p RestartPending
+ for _, h := range w.Yours {
+ if h.Shape == ReachShapeAddr {
+ p.Hosts = append(p.Hosts, RestartHost{Host: h.Host, Unit: h.Unit})
+ }
+ }
+ for _, h := range w.Unresolved {
+ if h.Shape == ReachShapeAddr || h.Shape == ReachShapeStamped {
+ p.Unresolved = append(p.Unresolved, h.Host)
+ }
+ }
+ p.Blocked = append(p.Blocked, w.Blocked...)
+ p.Undelivered = append(p.Undelivered, w.Undelivered...)
+ sort.Slice(p.Hosts, func(i, j int) bool { return p.Hosts[i].Host < p.Hosts[j].Host })
+ sort.Slice(p.Blocked, func(i, j int) bool { return p.Blocked[i].Host < p.Blocked[j].Host })
+ sort.Strings(p.Unresolved)
+ sort.Strings(p.Undelivered)
+ return p
+}
diff --git a/internal/appconfig/reach_test.go b/internal/appconfig/reach_test.go
new file mode 100644
index 000000000..7ed10b7f6
--- /dev/null
+++ b/internal/appconfig/reach_test.go
@@ -0,0 +1,548 @@
+package appconfig
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "math/rand/v2"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// reachWorld is one app (bab/relay) with every layer the classifier reads:
+// a default bundle stating SHARED (opted in to exec_delivery or not), an
+// unowned "prod" bundle stating PROD_KEY, an owned "team" bundle stating
+// TEAM_ONLY, and an instance overlay stating INST_KEY for each host in
+// overlays. "ghost" is an environment with no bundle.
+func reachWorld(t *testing.T, optIn bool, overlays ...string) (context.Context, release.Store) {
+ t.Helper()
+ ctx := context.Background()
+ s := release.NewDirStore(t.TempDir())
+ if err := SetEnv(ctx, s, "bab", "relay", map[string]string{"SHARED": "d"}); err != nil {
+ t.Fatal(err)
+ }
+ if optIn {
+ if err := SetExecDelivery(ctx, s, "bab", "relay", ExecDeliveryFile); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if _, err := ApplyEnvironment(ctx, s, "bab", "relay", "prod", map[string]Var{"PROD_KEY": {Kind: "env", Value: "p"}}, nil); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := ApplyEnvironmentOwned(ctx, s, "bab", "relay", "team", "alta", map[string]Var{"TEAM_ONLY": {Kind: "env", Value: "y"}}, nil); err != nil {
+ t.Fatal(err)
+ }
+ for _, h := range overlays {
+ if _, _, err := ApplyInstance(ctx, s, "bab", "relay", h, map[string]Var{"INST_KEY": {Kind: "env", Value: "i"}}, nil); err != nil {
+ t.Fatal(err)
+ }
+ }
+ return ctx, s
+}
+
+// reachRoute builds a ready bab/relay route of one shape. "blocked" is a
+// stamped route the shape rail refuses (a socket upstream).
+func reachRoute(shape, host, env string) registry.Route {
+ rt := registry.Route{Host: host, Kind: "instance", App: "relay", Account: "bab", Status: "ready", Environment: env}
+ switch shape {
+ case "unit":
+ rt.Addr, rt.Backing = "127.0.0.1:9001", registry.BackingUnit
+ case "stamped":
+ rt.Addr, rt.Unit = "127.0.0.1:9002", "u-"+strings.SplitN(host, ".", 2)[0]+".service"
+ case "blocked":
+ rt.Socket, rt.Unit = "/run/"+host+".sock", "b-"+strings.SplitN(host, ".", 2)[0]+".service"
+ case "socket":
+ rt.Socket = "/run/" + host + ".sock"
+ case "sidecar":
+ rt.Kind = "sidecar"
+ case "addr":
+ rt.Addr = "127.0.0.1:9003"
+ default:
+ panic(shape)
+ }
+ return rt
+}
+
+// reachWhere names the one list host is in ("" for none) and fails the test
+// when it is in more than one.
+func reachWhere(t *testing.T, w WriteReach, host string) (string, ReachHost) {
+ t.Helper()
+ var at []string
+ var got ReachHost
+ for name, l := range map[string][]ReachHost{"self": w.Self, "carloku": w.Carloku, "yours": w.Yours, "starting": w.Starting, "unresolved": w.Unresolved} {
+ for _, h := range l {
+ if h.Host == host {
+ at, got = append(at, name), h
+ }
+ }
+ }
+ for _, b := range w.Blocked {
+ if b.Host == host {
+ at = append(at, "blocked")
+ }
+ }
+ for name, l := range map[string][]string{"unseen": w.Unseen, "undelivered": w.Undelivered} {
+ for _, h := range l {
+ if h == host {
+ at = append(at, name)
+ }
+ }
+ }
+ if len(at) > 1 {
+ t.Fatalf("%s is in %v, want at most one list: %+v", host, at, w)
+ }
+ if len(at) == 0 {
+ return "", got
+ }
+ return at[0], got
+}
+
+func TestClassifyWriteTable(t *testing.T) {
+ const h = "aa.example.com"
+ asleep := map[string]HostFact{h: {Asleep: true}}
+ awake := map[string]HostFact{h: {}}
+ managed := map[string]HostFact{h: {Managed: true}}
+ notReady := func(rt registry.Route) registry.Route { rt.Status = "starting"; return rt }
+ cases := []struct {
+ name string
+ optIn bool
+ overlay bool
+ failKey string // a key the store will not read
+ route *registry.Route
+ record *ReachRecord
+ hosts map[string]HostFact
+ envName string
+ keys []string
+ want string
+ how ReachHow
+ why ReachWhy
+ wantUnit string
+ }{
+ {name: "unit route updates itself", route: ptr(reachRoute("unit", h, "")), keys: []string{"SHARED"}, want: "self"},
+ {name: "unit route asleep", route: ptr(reachRoute("unit", h, "")), hosts: asleep, keys: []string{"SHARED"}, want: "starting", why: ReachWhyAsleep},
+ {name: "socket unreported", route: ptr(reachRoute("socket", h, "")), keys: []string{"SHARED"}, want: "self"},
+ {name: "socket reported awake", route: ptr(reachRoute("socket", h, "")), hosts: awake, keys: []string{"SHARED"}, want: "self"},
+ {name: "socket asleep", route: ptr(reachRoute("socket", h, "")), hosts: asleep, keys: []string{"SHARED"}, want: "starting", why: ReachWhyAsleep},
+ // Every delivery predicate requires a ready row: a not-ready route
+ // is reached by no pass, so the classifier follows the predicates.
+ {name: "socket not ready is no pass's", route: ptr(notReady(reachRoute("socket", h, ""))), keys: []string{"SHARED"}, want: ""},
+ {name: "stamped not ready is no pass's", route: ptr(notReady(reachRoute("stamped", h, ""))), keys: []string{"SHARED"}, want: ""},
+ {name: "sidecar managed", route: ptr(reachRoute("sidecar", h, "")), hosts: managed, keys: []string{"SHARED"}, want: "self"},
+ {name: "sidecar unreported", route: ptr(reachRoute("sidecar", h, "")), keys: []string{"SHARED"}, want: "yours", how: ReachHowSidecar},
+ {name: "sidecar unmanaged asleep", route: ptr(reachRoute("sidecar", h, "")), hosts: asleep, keys: []string{"SHARED"}, want: "starting", why: ReachWhyAsleep},
+ {name: "addr opted in", optIn: true, route: ptr(reachRoute("addr", h, "")), keys: []string{"SHARED"}, want: "yours", how: ReachHowAddr},
+ {name: "addr opted in and asleep stays yours", optIn: true, route: ptr(reachRoute("addr", h, "")), hosts: asleep, keys: []string{"SHARED"}, want: "yours", how: ReachHowAddr},
+ {name: "addr not opted in", route: ptr(reachRoute("addr", h, "")), keys: []string{"SHARED"}, want: "undelivered"},
+ {name: "stamped updates itself", route: ptr(reachRoute("stamped", h, "")), keys: []string{"SHARED"}, want: "self", wantUnit: "u-aa.service"},
+ {name: "stamped asleep is never starting", route: ptr(reachRoute("stamped", h, "")), hosts: asleep, keys: []string{"SHARED"}, want: "self", wantUnit: "u-aa.service"},
+ {name: "stamped blocked", route: ptr(reachRoute("blocked", h, "")), keys: []string{"SHARED"}, want: "blocked"},
+ {name: "bound route, default write", route: ptr(reachRoute("socket", h, "prod")), keys: []string{"SHARED"}, want: "self"},
+ {name: "bound route, shadowed default write", route: ptr(reachRoute("socket", h, "prod")), keys: []string{"PROD_KEY"}, want: ""},
+ {name: "bound addr, shadowed default write", optIn: true, route: ptr(reachRoute("addr", h, "prod")), keys: []string{"PROD_KEY"}, want: ""},
+ {name: "bound route, its own write", route: ptr(reachRoute("socket", h, "prod")), envName: "prod", keys: []string{"PROD_KEY"}, want: "self"},
+ {name: "unbound route, named write", route: ptr(reachRoute("socket", h, "")), envName: "prod", keys: []string{"PROD_KEY"}, want: ""},
+ {name: "owned env, default write", route: ptr(reachRoute("socket", h, "team")), keys: []string{"SHARED"}, want: ""},
+ {name: "owned env, its own write", route: ptr(reachRoute("socket", h, "team")), envName: "team", keys: []string{"TEAM_ONLY"}, want: "self"},
+ {name: "owned env addr is standalone", optIn: true, route: ptr(reachRoute("addr", h, "team")), envName: "team", keys: []string{"TEAM_ONLY"}, want: "undelivered"},
+ {name: "missing bound bundle", route: ptr(reachRoute("socket", h, "ghost")), keys: []string{"SHARED"}, want: "unresolved"},
+ {name: "unreadable bound bundle", route: ptr(reachRoute("unit", h, "prod")), failKey: EnvironmentKey("bab", "relay", "prod"), keys: []string{"SHARED"}, want: "unresolved"},
+ {name: "unreadable default bundle", route: ptr(reachRoute("socket", h, "")), failKey: Key("bab", "relay"), keys: []string{"SHARED"}, want: "unresolved"},
+ {name: "overlay shadows", overlay: true, route: ptr(reachRoute("socket", h, "")), keys: []string{"INST_KEY"}, want: ""},
+ {name: "overlay unreadable", overlay: true, route: ptr(reachRoute("sidecar", h, "")), failKey: InstanceConfigKey("bab", "relay", h), keys: []string{"INST_KEY"}, want: "unresolved"},
+ {name: "addr, empty keys never reads the overlay", optIn: true, overlay: true, route: ptr(reachRoute("addr", h, "")), failKey: InstanceConfigKey("bab", "relay", h), want: "yours", how: ReachHowAddr},
+ {name: "other app", route: ptr(func() registry.Route { r := reachRoute("socket", h, ""); r.App = "other"; return r }()), keys: []string{"SHARED"}, want: ""},
+ {name: "other account", route: ptr(func() registry.Route { r := reachRoute("socket", h, ""); r.Account = "zed"; return r }()), keys: []string{"SHARED"}, want: ""},
+ {name: "record only", record: &ReachRecord{Host: h}, keys: []string{"SHARED"}, want: "unseen"},
+ {name: "record only, bound, shadowed", record: &ReachRecord{Host: h, Environment: "prod"}, keys: []string{"PROD_KEY"}, want: ""},
+ {name: "record only, owned env, default write", record: &ReachRecord{Host: h, Environment: "team"}, keys: []string{"SHARED"}, want: ""},
+ {name: "record only, other environment", record: &ReachRecord{Host: h}, envName: "prod", keys: []string{"PROD_KEY"}, want: ""},
+ {name: "record only, overlay shadows", overlay: true, record: &ReachRecord{Host: h}, keys: []string{"INST_KEY"}, want: ""},
+ {name: "record only, unreadable overlay is unseen", overlay: true, record: &ReachRecord{Host: h}, failKey: InstanceConfigKey("bab", "relay", h), keys: []string{"INST_KEY"}, want: "unseen"},
+ {name: "record only, unreadable env is unseen", record: &ReachRecord{Host: h, Environment: "team"}, failKey: EnvironmentKey("bab", "relay", "team"), keys: []string{"SHARED"}, want: "unseen"},
+ {name: "record only, unreadable env under a shadowing overlay is unseen", overlay: true, record: &ReachRecord{Host: h, Environment: "prod"}, failKey: EnvironmentKey("bab", "relay", "prod"), keys: []string{"INST_KEY"}, want: "unseen"},
+ {name: "record of a local route is the route's", route: ptr(reachRoute("socket", h, "")), record: &ReachRecord{Host: h}, keys: []string{"SHARED"}, want: "self"},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ var overlays []string
+ if tc.overlay {
+ overlays = []string{h}
+ }
+ ctx, s := reachWorld(t, tc.optIn, overlays...)
+ if tc.failKey != "" {
+ s = &failingKeyStore{Store: s, failKey: tc.failKey, err: errors.New("503 slow down")}
+ }
+ var routes []registry.Route
+ if tc.route != nil {
+ routes = append(routes, *tc.route)
+ }
+ facts := ReachFacts{Hosts: tc.hosts, RecordsComplete: true}
+ if tc.record != nil {
+ facts.Records = append(facts.Records, *tc.record)
+ }
+ w := ClassifyWrite(ctx, s, routes, facts, "bab", "relay", tc.envName, tc.keys)
+ got, rh := reachWhere(t, w, h)
+ if got != tc.want || rh.How != tc.how || rh.Why != tc.why {
+ t.Fatalf("%s in %q how=%q why=%q, want %q how=%q why=%q: %+v", h, got, rh.How, rh.Why, tc.want, tc.how, tc.why, w)
+ }
+ if tc.wantUnit != "" && rh.Unit != tc.wantUnit {
+ t.Fatalf("Unit = %q, want %q", rh.Unit, tc.wantUnit)
+ }
+ if len(w.Carloku) != 0 || !w.RecordsComplete {
+ t.Fatalf("Carloku = %v, RecordsComplete = %v", w.Carloku, w.RecordsComplete)
+ }
+ })
+ }
+}
+
+func ptr[T any](v T) *T { return &v }
+
+// reachCase is one generated combination over the table's axes.
+type reachCase struct {
+ optIn bool
+ failKey string
+ routes []registry.Route
+ facts ReachFacts
+ envName string
+ keys []string
+}
+
+var (
+ reachPool = func() []string {
+ var p []string
+ for i := range 24 {
+ p = append(p, fmt.Sprintf("h%02d.example.com", i))
+ }
+ return p
+ }()
+ reachOverlays = reachPool[:8] // these hosts have an INST_KEY overlay
+ reachShapes = []string{"unit", "stamped", "blocked", "socket", "sidecar", "addr"}
+ reachEnvs = []string{"", "", "prod", "team", "ghost"}
+ reachKeySet = []string{"SHARED", "PROD_KEY", "INST_KEY", "TEAM_ONLY"}
+)
+
+func genReachCase(r *rand.Rand) reachCase {
+ c := reachCase{optIn: r.IntN(2) == 0, facts: ReachFacts{RecordsComplete: r.IntN(2) == 0}}
+ switch r.IntN(5) {
+ case 0:
+ c.failKey = Key("bab", "relay")
+ case 1:
+ c.failKey = EnvironmentKey("bab", "relay", []string{"prod", "team"}[r.IntN(2)])
+ case 2:
+ c.failKey = InstanceConfigKey("bab", "relay", reachOverlays[r.IntN(len(reachOverlays))])
+ }
+ c.envName = []string{"", "", "prod", "team"}[r.IntN(4)]
+ for _, k := range reachKeySet {
+ if r.IntN(3) == 0 {
+ c.keys = append(c.keys, k)
+ }
+ }
+ hosts := slices.Clone(reachPool)
+ r.Shuffle(len(hosts), func(i, j int) { hosts[i], hosts[j] = hosts[j], hosts[i] })
+ n := r.IntN(10)
+ for _, h := range hosts[:n] {
+ rt := reachRoute(reachShapes[r.IntN(len(reachShapes))], h, reachEnvs[r.IntN(len(reachEnvs))])
+ switch r.IntN(8) {
+ case 0:
+ rt.Status = "starting"
+ case 1:
+ rt.App = "other"
+ case 2:
+ rt.Account = "zed"
+ case 3:
+ rt.Platform = true
+ }
+ c.routes = append(c.routes, rt)
+ }
+ for _, h := range hosts[n : n+r.IntN(6)] {
+ c.facts.Records = append(c.facts.Records, ReachRecord{Host: h, Environment: reachEnvs[r.IntN(len(reachEnvs))]})
+ }
+ // Some records name local hosts too: a record and a route of one host.
+ for _, rt := range c.routes {
+ if r.IntN(4) == 0 {
+ c.facts.Records = append(c.facts.Records, ReachRecord{Host: rt.Host, Environment: rt.Environment})
+ }
+ }
+ c.facts.Hosts = map[string]HostFact{}
+ for _, h := range hosts[:n+3] {
+ if f := r.IntN(4); f > 0 {
+ c.facts.Hosts[h] = HostFact{Managed: f&1 == 1, Asleep: f&2 == 2}
+ }
+ }
+ if r.IntN(6) == 0 {
+ c.facts.Hosts = nil
+ }
+ return c
+}
+
+// reachOracleShadowed is the shadowing test from the world's known content:
+// which of keys some layer above the written bundle states for a host bound
+// to env.
+func reachOracleShadowed(keys []string, envName, env, host string) bool {
+ if len(keys) == 0 {
+ return false
+ }
+ stated := map[string]bool{}
+ if slices.Contains(reachOverlays, host) {
+ stated["INST_KEY"] = true
+ }
+ if envName == "" && env == "prod" {
+ stated["PROD_KEY"] = true
+ }
+ if envName == "" && env == "team" {
+ stated["TEAM_ONLY"] = true
+ }
+ for _, k := range keys {
+ if !stated[k] {
+ return false
+ }
+ }
+ return true
+}
+
+// reachInScope: the write reaches a host bound to env (narrowing and the
+// owned environment's boundary, decided only when the bundle reads).
+func reachInScope(c reachCase, env string) bool {
+ if c.envName != "" && env != c.envName {
+ return false
+ }
+ ownedReadable := c.failKey != EnvironmentKey("bab", "relay", "team")
+ return !(c.envName == "" && env == "team" && ownedReadable)
+}
+
+func reachCandidate(rt registry.Route) bool {
+ return unitDelivered(rt) || execSyncable(rt) || execSidecarCandidate(rt) || execAddrCandidate(rt)
+}
+
+func TestClassifyWriteProperty(t *testing.T) {
+ ctx := context.Background()
+ worlds := map[bool]release.Store{}
+ for _, on := range []bool{true, false} {
+ _, worlds[on] = reachWorld(t, on, reachOverlays...)
+ }
+ r := rand.New(rand.NewPCG(20261108, 3))
+ for i := range 2000 {
+ c := genReachCase(r)
+ s := worlds[c.optIn]
+ if c.failKey != "" {
+ s = &failingKeyStore{Store: s, failKey: c.failKey, err: errors.New("503 slow down")}
+ }
+ w := ClassifyWrite(ctx, s, c.routes, c.facts, "bab", "relay", c.envName, c.keys)
+ if len(w.Carloku) != 0 {
+ t.Fatalf("case %d: Carloku = %v, want always empty", i, w.Carloku)
+ }
+ if w.RecordsComplete != c.facts.RecordsComplete {
+ t.Fatalf("case %d: RecordsComplete not carried", i)
+ }
+ local := map[string]bool{}
+ for _, rt := range c.routes {
+ local[rt.Host] = true
+ at, _ := reachWhere(t, w, rt.Host) // fails on two lists
+ ours := rt.App == "relay" && release.RouteAccount(rt) == "bab"
+ reached := ours && reachCandidate(rt) && reachInScope(c, rt.Environment)
+ switch {
+ case !reached && at != "":
+ t.Fatalf("case %d: %+v is not reached by this write, but is in %q", i, rt, at)
+ case reached && at == "unseen":
+ t.Fatalf("case %d: local route %s in Unseen", i, rt.Host)
+ case reached && at == "" && !reachOracleShadowed(c.keys, c.envName, rt.Environment, rt.Host):
+ t.Fatalf("case %d: reached, unshadowed %+v is in no list: %+v", i, rt, w)
+ }
+ }
+ seenRec := map[string]bool{}
+ for _, rec := range c.facts.Records {
+ if local[rec.Host] || seenRec[rec.Host] {
+ continue
+ }
+ seenRec[rec.Host] = true
+ at, _ := reachWhere(t, w, rec.Host)
+ reached := reachInScope(c, rec.Environment)
+ switch {
+ case at != "" && at != "unseen":
+ t.Fatalf("case %d: record-only %s in %q", i, rec.Host, at)
+ case !reached && at != "":
+ t.Fatalf("case %d: record %+v not reached, but Unseen", i, rec)
+ case reached && at == "" && !reachOracleShadowed(c.keys, c.envName, rec.Environment, rec.Host):
+ t.Fatalf("case %d: reached, unshadowed record %+v dropped: %+v", i, rec, w)
+ }
+ }
+ for _, h := range w.Unseen {
+ if local[h] || !seenRec[h] {
+ t.Fatalf("case %d: Unseen %s is local or no record's", i, h)
+ }
+ }
+ }
+}
+
+// TestClassifySelfMatchesDelivery: bucket 1 holds iff delivery restarts the
+// route. First as the predicate identity over generated routes whose layers
+// all read and whose write shadows nothing; then by running the real Sync and
+// SyncExec passes over one route per locally runnable shape, recording
+// restarts.
+func TestClassifySelfMatchesDelivery(t *testing.T) {
+ ctx := context.Background()
+ worlds := map[bool]release.Store{}
+ for _, on := range []bool{true, false} {
+ _, worlds[on] = reachWorld(t, on, reachOverlays...)
+ }
+ r := rand.New(rand.NewPCG(20261108, 7))
+ for i := range 2000 {
+ c := genReachCase(r)
+ c.failKey, c.keys = "", nil
+ w := ClassifyWrite(ctx, worlds[c.optIn], c.routes, c.facts, "bab", "relay", c.envName, c.keys)
+ blocked := map[string]bool{}
+ for _, b := range BlockedStamps(c.routes, "bab", "relay", c.envName) {
+ blocked[b.Host] = true
+ }
+ for _, rt := range c.routes {
+ if rt.App != "relay" || release.RouteAccount(rt) != "bab" || !reachInScope(c, rt.Environment) || rt.Environment == "ghost" {
+ continue
+ }
+ at, _ := reachWhere(t, w, rt.Host)
+ fact := c.facts.Hosts[rt.Host]
+ if execSidecarCandidate(rt) && !fact.Managed && fact.Asleep {
+ // The one Starting that is not a Self overridden: an
+ // unmanaged sidecar nobody restarts, asleep, which reads the
+ // file when it is next started.
+ if at != "starting" {
+ t.Fatalf("case %d: asleep unmanaged sidecar %s in %q, want starting", i, rt.Host, at)
+ }
+ continue
+ }
+ got := at == "self" || at == "starting"
+ want := (unitDelivered(rt) && !blocked[rt.Host]) || execSyncable(rt) || (execSidecarCandidate(rt) && fact.Managed)
+ if got != want {
+ t.Fatalf("case %d: %+v in %q; delivery restarts it = %v", i, rt, at, want)
+ }
+ }
+ }
+
+ // The passes themselves. Converge once, change SHARED, classify the
+ // change, run the passes again and compare who they restarted.
+ ctx, s, reg, dd, priv, _ := execKit(t)
+ pk, err := ParsePrivateKey(priv, "test")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := SetEnv(ctx, s, "bab", "relay", map[string]string{"SHARED": "one"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := SetExecDelivery(ctx, s, "bab", "relay", ExecDeliveryFile); err != nil {
+ t.Fatal(err)
+ }
+ routes := []registry.Route{
+ reachRoute("unit", "un.example.com", ""),
+ reachRoute("socket", "so.example.com", ""),
+ reachRoute("sidecar", "si.example.com", ""),
+ reachRoute("addr", "ad.example.com", ""),
+ }
+ for _, rt := range routes {
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ }
+ envDir := t.TempDir()
+ var restarted []string
+ record := func(h string) error { restarted = append(restarted, h); return nil }
+ pass := func() {
+ t.Helper()
+ if err := Sync(ctx, reg, s, pk, envDir, record, nil); err != nil {
+ t.Fatalf("Sync: %v", err)
+ }
+ if err := SyncExec(ctx, reg, s, pk, dd, record, "", nil); err != nil {
+ t.Fatalf("SyncExec: %v", err)
+ }
+ }
+ pass()
+ restarted = nil
+ if err := SetEnv(ctx, s, "bab", "relay", map[string]string{"SHARED": "two"}); err != nil {
+ t.Fatal(err)
+ }
+ w := ClassifyWrite(ctx, s, routes, ReachFacts{}, "bab", "relay", "", []string{"SHARED"})
+ pass()
+ var self, yours []string
+ for _, h := range w.Self {
+ self = append(self, h.Host)
+ }
+ for _, h := range w.Yours {
+ yours = append(yours, h.Host)
+ }
+ slices.Sort(restarted)
+ if !slices.Equal(self, restarted) {
+ t.Fatalf("Self = %v, the passes restarted %v", self, restarted)
+ }
+ if want := []string{"ad.example.com", "si.example.com"}; !slices.Equal(yours, want) {
+ t.Fatalf("Yours = %v, want %v (written, restarted by nobody here)", yours, want)
+ }
+}
+
+func TestClassifySidecarByMode(t *testing.T) {
+ const h = "bot.example.com"
+ ctx, s := reachWorld(t, false)
+ routes := []registry.Route{reachRoute("sidecar", h, "")}
+ for _, tc := range []struct {
+ name string
+ hosts map[string]HostFact
+ want string
+ }{
+ {"managed", map[string]HostFact{h: {Managed: true}}, "self"},
+ {"unmanaged fact", map[string]HostFact{h: {}}, "yours"},
+ {"no fact for the host", map[string]HostFact{"other.example.com": {Managed: true}}, "yours"},
+ {"no reports at all", nil, "yours"},
+ } {
+ w := ClassifyWrite(ctx, s, routes, ReachFacts{Hosts: tc.hosts}, "bab", "relay", "", []string{"SHARED"})
+ at, rh := reachWhere(t, w, h)
+ if at != tc.want {
+ t.Fatalf("%s: in %q, want %q", tc.name, at, tc.want)
+ }
+ if at == "yours" && rh.How != ReachHowSidecar {
+ t.Fatalf("%s: How = %q, want %q", tc.name, rh.How, ReachHowSidecar)
+ }
+ }
+}
+
+func TestClassifyNoAbsenceClaims(t *testing.T) {
+ ctx, s := reachWorld(t, true)
+ recs := []ReachRecord{{Host: "b.example.com"}, {Host: "a.example.com"}}
+ w := ClassifyWrite(ctx, s, nil, ReachFacts{Records: recs}, "bab", "relay", "", []string{"SHARED"})
+ if !slices.Equal(w.Unseen, []string{"a.example.com", "b.example.com"}) {
+ t.Fatalf("Unseen = %v, want both records, sorted", w.Unseen)
+ }
+ for _, complete := range []bool{true, false} {
+ w = ClassifyWrite(ctx, s, nil, ReachFacts{RecordsComplete: complete}, "bab", "relay", "", []string{"SHARED"})
+ if len(w.Self)+len(w.Carloku)+len(w.Yours)+len(w.Starting)+len(w.Unseen)+len(w.Blocked)+len(w.Unresolved)+len(w.Undelivered) != 0 {
+ t.Fatalf("empty routes and records listed something: %+v", w)
+ }
+ if w.RecordsComplete != complete {
+ t.Fatalf("RecordsComplete = %v, want %v", w.RecordsComplete, complete)
+ }
+ }
+}
+
+// TestClassifyPendingProjection: the projection keeps only what the notice
+// has always said, and orders every list.
+func TestClassifyPendingProjection(t *testing.T) {
+ w := WriteReach{
+ Self: []ReachHost{{Host: "s.example.com", Shape: ReachShapeSocket}},
+ Yours: []ReachHost{{Host: "z.example.com", Shape: ReachShapeAddr, Unit: "z.service"}, {Host: "c.example.com", Shape: ReachShapeSidecar}, {Host: "a.example.com", Shape: ReachShapeAddr}},
+ Starting: []ReachHost{{Host: "t.example.com", Shape: ReachShapeUnit}},
+ Unresolved: []ReachHost{{Host: "u2.example.com", Shape: ReachShapeStamped}, {Host: "u3.example.com", Shape: ReachShapeSocket}, {Host: "u1.example.com", Shape: ReachShapeAddr}},
+ Blocked: []BlockedHost{{Host: "b.example.com", Reason: "r"}},
+ Undelivered: []string{"d2.example.com", "d1.example.com"},
+ Unseen: []string{"x.example.com"},
+ }
+ p := w.Pending()
+ if !slices.Equal(p.Hosts, []RestartHost{{Host: "a.example.com"}, {Host: "z.example.com", Unit: "z.service"}}) ||
+ !slices.Equal(p.Unresolved, []string{"u1.example.com", "u2.example.com"}) ||
+ !slices.Equal(p.Undelivered, []string{"d1.example.com", "d2.example.com"}) ||
+ len(p.Blocked) != 1 {
+ t.Fatalf("Pending = %+v", p)
+ }
+ if !(WriteReach{Self: w.Self, Starting: w.Starting, Unseen: w.Unseen}).Pending().Empty() {
+ t.Fatal("Self, Starting and Unseen must project to nothing")
+ }
+}
diff --git a/internal/appconfig/syncexec.go b/internal/appconfig/syncexec.go
index edc85b0fa..8f4b5653d 100644
--- a/internal/appconfig/syncexec.go
+++ b/internal/appconfig/syncexec.go
@@ -1161,176 +1161,10 @@ func BlockedStamps(routes []registry.Route, acct, app, envName string) []Blocked
// shown, so its callers say what they looked at rather than letting a
// short list read as a complete one.
func AwaitingRestart(ctx context.Context, s release.Store, routes []registry.Route, acct, app, envName string, keys []string) RestartPending {
- var out RestartPending
- // The door-side rails over the full box list; a blocked host is
- // reported only once the loop below has established that this write
- // reaches it (environment narrowing, and an owned environment's
- // standalone boundary; review round 1 P2-5).
- blocked := map[string]string{}
- for _, b := range BlockedStamps(routes, acct, app, envName) {
- blocked[b.Host] = b.Reason
- }
- // The instance layer, through the same loader the sweeps use, so this
- // notice and the pass that writes the files agree about what sits above
- // a bundle. One listing for the app, then a Get per route that has an
- // overlay; both live for one call, like the caches below.
- overlayFor := instanceOverlayLoader(ctx, s)
- // Both caches exist for the same reason SyncExec's do: a box with many
- // TCP-addr routes on one bundle must not cost more than one read for
- // it. Unlike SyncExec's, they live for one call.
- var (
- defM *Manifest
- defFound bool
- defErr error
- defDone bool
- )
- loadDefault := func() (*Manifest, bool, error) {
- if !defDone {
- defM, defFound, defErr = getIfExists(ctx, s, acct, app)
- defDone = true
- }
- return defM, defFound, defErr
- }
- type envRes struct {
- m *Manifest
- found bool
- err error
- }
- envBundles := map[string]envRes{}
- loadEnv := func(env string) envRes {
- if e, done := envBundles[env]; done {
- return e
- }
- m, found, err := getIfExistsAt(ctx, s, EnvironmentKey(acct, app, env))
- e := envRes{m: m, found: found, err: err}
- envBundles[env] = e
- return e
- }
- for _, rt := range routes {
- stamped := stampedForDelivery(rt)
- if (!execAddrCandidate(rt) && !stamped) || rt.App != app || release.RouteAccount(rt) != acct {
- continue
- }
- // Which routes a write REACHES. A named bundle is read by the
- // routes bound to it and by nothing else. The default bundle is
- // read by every route of the app, bound or not, because a named
- // bundle is an overlay ON it (environments.go, LoadMerged) rather
- // than a replacement for it.
- if envName != "" && rt.Environment != envName {
- continue
- }
- eb := envRes{}
- def := loadDefault
- if rt.Environment != "" {
- eb = loadEnv(rt.Environment)
- // An OWNED environment is standalone (team-scoped credentials
- // §3): a default-bundle write reaches none of its routes, and
- // its opt-in never inherits the default bundle's. Decided
- // before the blocked notice, which is about routes the write
- // reaches; a bundle that cannot be read decides nothing here.
- if eb.err == nil && eb.found && eb.m.Owned() {
- if envName == "" {
- continue
- }
- def = standaloneDefault
- }
- }
- // Blocked is decided before bundle resolution (spec §3.4): a route
- // the root pass will refuse is reported as blocked, not as healthy
- // and not as unresolved.
- if reason, bad := blocked[rt.Host]; bad {
- out.Blocked = append(out.Blocked, BlockedHost{Host: rt.Host, Reason: reason})
- continue
- }
- if rt.Environment != "" {
- if eb.err != nil {
- out.Unresolved = append(out.Unresolved, rt.Host)
- continue
- }
- // A route bound to a bundle that does not exist resolves to an
- // ERROR, not to the app default: LoadMerged refuses it, and
- // SyncExec leaves the route's file exactly as it was. Nothing
- // was delivered, so "delivered" would be a lie — but neither
- // is this route fine, so it is not a skip either.
- if !eb.found {
- out.Unresolved = append(out.Unresolved, rt.Host)
- continue
- }
- }
- v, err := resolveExecDelivery(eb.m, eb.found, def)
- if err != nil {
- out.Unresolved = append(out.Unresolved, rt.Host)
- continue
- }
- // A stamped route is delivered and restarted by the root pass within
- // seconds of this write; it has nothing to be told about unless a
- // layer it is rendered from would not resolve. The bundles were
- // checked above; the instance overlay is checked here, because the
- // root pass loads it for a stamped route and refuses delivery when
- // it will not read (sync.go; review round 1 P2-6).
- if stamped {
- // The default bundle is required by delivery whatever the
- // exec-delivery opt-in says (round 2 P2-5: resolveExecDelivery
- // returns before reading it when the environment sets
- // exec_delivery explicitly); only an owned environment stands
- // alone without it.
- if def != nil && !(eb.found && eb.m != nil && eb.m.Owned()) {
- if _, _, defErr := loadDefault(); defErr != nil {
- out.Unresolved = append(out.Unresolved, rt.Host)
- continue
- }
- }
- if _, _, instErr := overlayFor(acct, app, rt.Host); instErr != nil {
- out.Unresolved = append(out.Unresolved, rt.Host)
- }
- continue
- }
- // Not opted in — including the Unknown case, which decides "off" —
- // means no pass delivers a file to this route: this write reached
- // nothing here. Say so (spec §3.4, codex R1-9: "this write", not
- // "no file" — an opted-out route may hold a file from before).
- if !v.On {
- out.Undelivered = append(out.Undelivered, rt.Host)
- continue
- }
- // The shadowing test, and the reason keys is a parameter: where every
- // touched key is shadowed, the merged manifest is byte-identical,
- // syncExecHost's bytes.Equal writes nothing, and naming the route
- // would send someone to restart a process for a value it was never
- // going to see. An empty keys disables this whole test (nothing to
- // shadow — see keys' own doc comment), so the instance layer is
- // consulted only when it could actually change the verdict: a route
- // whose overlay is unreadable must not be downgraded to Unresolved
- // over a call that was never going to ask it anything.
- if len(keys) > 0 {
- // Which layers sit ABOVE the bundle that was written, for this
- // route. A write cannot change what a route resolves to for a
- // key some higher layer states, and the third layer sits above
- // both of the other two.
- inst, _, instErr := overlayFor(acct, app, rt.Host)
- if instErr != nil {
- out.Unresolved = append(out.Unresolved, rt.Host)
- continue
- }
- var above []*Manifest
- if envName == "" && rt.Environment != "" {
- above = append(above, eb.m)
- }
- above = append(above, inst)
- if allShadowed(keys, above...) {
- continue
- }
- }
- out.Hosts = append(out.Hosts, RestartHost{Host: rt.Host, Unit: rt.Unit})
- }
- // Sorted so a caller's output — and any test pinning it — is stable
- // across passes, where registry order is not. Same discipline as
- // ExecConfigApps'.
- sort.Slice(out.Hosts, func(i, j int) bool { return out.Hosts[i].Host < out.Hosts[j].Host })
- sort.Slice(out.Blocked, func(i, j int) bool { return out.Blocked[i].Host < out.Blocked[j].Host })
- sort.Strings(out.Unresolved)
- sort.Strings(out.Undelivered)
- return out
+ // A projection of the shared classifier (reach.go), so the CLI's notice
+ // and the console's Save outcome are one computation. No box facts: the
+ // notice has never claimed sleep or management.
+ return ClassifyWrite(ctx, s, routes, ReachFacts{}, acct, app, envName, keys).Pending()
}
// allShadowed reports whether every one of keys is STATED by at least one