| index 761a14ce0..57ae437e4 100644 |
| --- a/docs/superpowers/plans/2026-11-19-named-units-from-parameters.md |
| +++ b/docs/superpowers/plans/2026-11-19-named-units-from-parameters.md |
| @@ -145,13 +145,30 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| func installNamedUnitsLocked(dst string, raw []byte) error |
| ``` |
| `InstallNamedUnits(envDir, dst, raw)` keeps its signature: |
| - `validateForInstall`, then the lock, then `installNamedUnitsLocked`. |
| + `validateForInstall`, then the box guard rule, then the lock, then |
| + `installNamedUnitsLocked`. |
| + ```go |
| + // consoleGuardHost: the host the armed console guard names, read as |
| + // proofnamed.go:126-133 does (trusted read of |
| + // <opt>/system/guard/console-ready.json, matched against |
| + // release.LiveLink). Extract that match into this one helper and have |
| + // proofnamed.go call it too. |
| + func consoleGuardHost(optDir string) (host string, ok bool) |
| + // checkGuardRule: if guarded, the guarded host's entry, when present, |
| + // must be console.service / app console / empty account (proofnamed.go:142). |
| + func checkGuardRule(entries []namedunits.Entry, guardHost string, guarded bool) error |
| + ``` |
| + The operator's off-box `put` (Task 7) runs `validateForInstall` only, |
| + never the guard rule. (Astra plan R1 #4.) |
| |
| -- [ ] **Step 1: Failing test** `TestInstallNamedUnitsRefusesWhatTheReleasePathRefuses`: a file with an unknown field, or an entry without `addr`, returns an error, and `dst` is not created. |
| +- [ ] **Step 1: Failing tests** |
| + - `TestInstallNamedUnitsRefusesWhatTheReleasePathRefuses`: a file with an unknown field, or an entry without `addr`, returns an error, and `dst` is not created. |
| + - `TestCheckGuardRuleRefusesNonConsoleForGuardedHost`: guarded `a.example.com` mapped to `shop.service` → error; the same host as console → nil; other hosts unaffected. |
| + - The existing `TestPlan14NamedUnits*` tests still pass with `proofnamed.go` calling `consoleGuardHost`. |
| - [ ] **Step 2:** `go test ./internal/host -run 'TestInstallNamedUnits'`. Expect the new test to FAIL and `TestInstallNamedUnitsWaitsForTheConfigSyncLock` to PASS. That test must keep passing; if its fixture lacks addr/health, give it both. |
| - [ ] **Step 3:** Implement the split. |
| - [ ] **Step 4:** Run step 2 again: PASS. |
| -- [ ] **Step 5:** Commit: `host: named-units install takes the strict gate; split the locked write`. |
| +- [ ] **Step 5:** Commit: `host: named-units install takes the strict gate and guard rule`. |
| |
| ### Task 3: The parameter adapter (`internal/hostenv`) |
| |
| @@ -195,10 +212,10 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| |
| **Files:** |
| - Create: `internal/host/namedunitsfetch.go`, `internal/host/namedunitsfetch_test.go` |
| -- Modify: `internal/status/snapshot.go` (the record types only; Task 6 wires them into the heartbeat), `internal/host/rootrollback.go` (one helper), `internal/host/agent.go` (`runAdoptUnderRootRelease`, after the `-restart-only` return at `:6157`, before `ParsePublicKey` at `:6165`) |
| +- Modify: `internal/status/snapshot.go` (the record types only; Task 6 wires them into the heartbeat), `internal/host/rootrollback.go` (`declarationBindingInUse`), `internal/host/agent.go` (`runAdoptUnderRootRelease`, after the `-restart-only` return at `:6157`, before `ParsePublicKey` at `:6165`) |
| |
| **Interfaces:** |
| -- Consumes: Tasks 1-3; `readIdentity`, `haIMDSClient`, `imdsBase`; `lockConfigSync`; `rootstate.Open`, `rootstate.OpenTrustedFile`; `readRollbackAttempt`, `rollbackMargin`, `rootRestartState`. |
| +- Consumes: Tasks 1-3 (including `consoleGuardHost`, `checkGuardRule` from Task 2); `readIdentity`, `haIMDSClient`, `imdsBase`; `lockConfigSync`; `rootstate.Open`, `rootstate.OpenTrustedFile`; `readRollbackAttempt`, `rollbackMargin`, `rootRestartState`; `release.RootHistory` as `roothistory.go:23-35` opens it. |
| - Produces: |
| ```go |
| // internal/status |
| @@ -222,17 +239,24 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| |
| // internal/host |
| const namedUnitsStatusDir = "/var/lib/carlos-named-units" |
| - // rollbackInFlight: an attempt exists, !Settled, and now is before |
| - // Deadline+rollbackMargin. No side effects (unlike loadRollback). |
| - func rollbackInFlight(s rootstate.Store, host string, now time.Time) (bool, error) |
| + // declarationBindingInUse: the release path may still compare the |
| + // declaration file's identity for host. True when the host's attempt |
| + // journal (readRollbackAttempt) exists and EITHER it is !Settled and |
| + // now is before Deadline+rollbackMargin, OR a.History != nil and the |
| + // root history still has it pending (release.RootHistory.Pending( |
| + // a.HistoryBinding) returns an attempt equal to *a.History; this is |
| + // the work resumeHistoryRestart/settleHistory would still do, |
| + // roothistory_recovery.go:104-143), whatever Settled says. |
| + // No side effects. (Astra plan R1 #1.) |
| + func declarationBindingInUse(s rootstate.Store, history *release.RootHistory, host string, now time.Time) (bool, error) |
| type namedUnitsFetch struct { |
| skip func() string // non-empty = skip reason |
| identity func(ctx context.Context) (iid, region string, err error) |
| open func(ctx context.Context, region string) (hostenv.NamedUnitsStore, error) |
| lock func() (func(), error) |
| dst string |
| - inFlight func(host string) (bool, error) |
| - guardHost func() (host string, ok bool) // console guard's host, if armed |
| + inUse func(host string) (bool, error) // declarationBindingInUse, bound |
| + guardHost func() (host string, ok bool) // consoleGuardHost(release.OptDir()) |
| now func() time.Time |
| } |
| func (f namedUnitsFetch) run(ctx context.Context) status.NamedUnitsStatus |
| @@ -244,19 +268,24 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| 2. Open and Get, under a 10 s context: |
| - `ErrNoParameter` → `not-found` |
| - any other error → `unreadable` |
| - 3. Gate the value with `validateForInstall`, plus the guard rule (if |
| - `guardHost` returns a host, that host's entry must be |
| - `console.service`, app `console`, empty account). Failure → |
| - `invalid`. |
| + 3. Gate the value with `validateForInstall` and `checkGuardRule`. |
| + Failure → `invalid`. |
| 4. Then, under `lock`: |
| - Read `dst` with `OpenTrustedFile`. Equal bytes → `unchanged`. |
| - - Any host in the *current* file with `inFlight` true (or an error) |
| - → `held`, with the host named. |
| + - The affected hosts are the hosts in the *current* file plus the |
| + guarded console host when the guard is armed, even if the file |
| + is absent or empty: the release path synthesises that host's |
| + entry and binds the file's identity (`proofnamed.go:82-100,139`). |
| + Any affected host with `inUse` true (or an error) → `held`, with |
| + the host named. (Astra plan R1 #2.) |
| - Otherwise `installNamedUnitsLocked` → `installed`. |
| 5. `Digest` is the sha256 of the file as it stands after the step. |
| - Wiring: a package var `adoptNamedUnitsStep = func(ctx context.Context) status.NamedUnitsStatus { return productionNamedUnitsFetch().run(ctx) }`. |
| `runAdoptUnderRootRelease` calls it, logs one line for the outcome, |
| - and keeps the result for Task 5 to complete and write. |
| + and calls `writeNamedUnitsStatus(st)` immediately, before |
| + `release.ParsePublicKey` (which can be fatal, `agent.go:6165`), so an |
| + install is never left unreported. Task 5 writes again after |
| + `SyncUnits` with the per-unit outcomes. (Astra plan R1 #5.) |
| `guardHost` reads `<opt>/system/guard/console-ready.json` with the |
| same trusted read and the same `release.LiveLink` match as |
| `proofnamed.go:126-133`. Extract that match into one helper both |
| @@ -269,12 +298,13 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| - `TestNamedUnitsFetchEqualBytesNoRewrite`: same bytes → `unchanged`, and the inode is the same before and after. |
| - `TestNamedUnitsFetchInstallsNewValue`: → `installed`, the file equals the value, mode 0644, `Version` and `Digest` set. |
| - `TestNamedUnitsFetchInstallsClearingValue`: `{"schema":1,"units":[]}` → `installed`. |
| - - `TestNamedUnitsFetchHeldWhileRollbackInFlight`: `inFlight` is true for a host in the current file → `held`, the reason names the host, the file unchanged. Flip it to false and run again → `installed`. |
| + - `TestNamedUnitsFetchHeldWhileBindingInUse`: `inUse` is true for a host in the current file → `held`, the reason names the host, the file unchanged. Flip it to false and run again → `installed`. |
| + - `TestNamedUnitsFetchHoldsForGuardedConsoleHost`: the current file is absent, the guard names `c.example.com`, and `inUse("c.example.com")` is true → `held`. |
| - `TestNamedUnitsFetchSkipsGroupAndFleetWithoutNetwork`: `skip` returns a reason → `skipped`, and the `identity` and `open` fakes are never called. |
| - `TestNamedUnitsFetchGuardHostMustBeConsole`: the guard names `a.example.com`, and the value maps it to `shop.service` → `invalid`. |
| - `TestNamedUnitsFetchWaitsForConfigSyncLock`: the shape of `TestInstallNamedUnitsWaitsForTheConfigSyncLock`, using the real `lockConfigSync`. |
| - - `TestRollbackInFlight` (in `rootrollback_test.go` or alongside it): no attempt → false; settled → false; past the deadline plus margin → false; open → true. Nothing is written to the store. |
| - - `TestAdoptRunsNamedUnitsStepBeforeConfigSync`: a source-order check over `runAdoptUnderRootRelease`, in the style of `TestRunAdoptOpensTheRegistryThroughTheHelper` (`registryowner_test.go:301`). `adoptNamedUnitsStep(` appears after the `if *restartOnly` block and before `appconfig.SyncUnits(`, and outside the `CARLOS_SECRETS_KEY` branch. |
| + - `TestDeclarationBindingInUse`: no attempt → false; open within the deadline → true; past the deadline plus margin with no history → false; settled and verified with its history completion still pending → true; settled with history completed → false. Nothing is written to either store. |
| + - `TestAdoptRunsNamedUnitsStepBeforeConfigSync`: a source-order check over `runAdoptUnderRootRelease`, in the style of `TestRunAdoptOpensTheRegistryThroughTheHelper` (`registryowner_test.go:301`). `adoptNamedUnitsStep(` appears after the `if *restartOnly` block, then `writeNamedUnitsStatus(` before `release.ParsePublicKey(`, then `appconfig.SyncUnits(`; the step is outside the `CARLOS_SECRETS_KEY` branch. |
| - `TestProductionNamedUnitsFetchIsWired`: every field of `productionNamedUnitsFetch()` is non-nil, and `dst == appconfig.DefaultNamedUnitsPath`. |
| - [ ] **Step 2:** `go test ./internal/host -run 'TestNamedUnitsFetch|TestRollbackInFlight|TestAdoptRunsNamedUnitsStep|TestProductionNamedUnitsFetch'`. Expect FAIL. |
| - [ ] **Step 3:** Implement and wire. |
| @@ -290,23 +320,32 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| **Interfaces:** |
| - Produces, on `appconfig.UnitOps`: |
| ```go |
| - // Outcome, when set, is called exactly once per declared host per pass |
| - // that reaches planning: state "delivered" (config current, restarted |
| - // or not) or "refused" with the same reason the pass prints. |
| + // Outcome, when set, is called exactly once per declared host per pass, |
| + // after planning AND application finish, with the host's FINAL state: |
| + // "delivered" (config current, whether or not anything changed or |
| + // restarted), "refused" with the same reason the pass prints (the last |
| + // refusal wins over an earlier success), or "no-route" (declared, but |
| + // no stamped registry row selects it this pass). An unresolvable |
| + // config is "refused". Implementation: accumulate a map keyed by host |
| + // during the pass, emit once at the end. (Astra plan R1 #3.) |
| Outcome func(host, unit, state, reason string) |
| ``` |
| - In `runAdoptUnderRootRelease`, `productionUnitOps` gets an `Outcome` |
| - that appends to `st.Units`. After `SyncUnits` returns (or is skipped |
| - for want of a key), call `writeNamedUnitsStatus(st)` once. On a |
| - `-restart-only` pass, write nothing. |
| + that appends to `st.Units`. After `SyncUnits` returns, call |
| + `writeNamedUnitsStatus(st)` again (Task 4 already wrote the fetch |
| + outcome). When `SyncUnits` is skipped for want of a key, no second |
| + write. On a `-restart-only` pass, write nothing. |
| |
| - [ ] **Step 1: Failing tests:** |
| - `TestSyncUnitsReportsOneOutcomePerDeclaredHost`. Use `newFakeUnits` with two declared hosts: one delivers, and one has an unmanaged `EnvironmentFile`. That gives exactly two calls: `delivered`, and `refused` with the same reason text the pass prints. |
| + - `TestSyncUnitsOutcomeUnchangedIsDelivered`: a second pass with nothing changed → one `delivered`. |
| + - `TestSyncUnitsOutcomeLaterRefusalWins`: a host whose write succeeds but whose post-reload verification refuses → one `refused`. |
| + - `TestSyncUnitsOutcomeNoRouteAndUnresolved`: a declared host with no registry row → `no-route`; a host whose config cannot be resolved → `refused`. |
| - `TestSyncUnitsOutcomeNilIsSafe`: the existing behaviour with no hook. |
| - - `TestAdoptWritesNamedUnitsStatusOnce` (host, with fakes for the step and the writer): one write per full or config-only pass, none on restart-only. |
| -- [ ] **Step 2:** `go test ./internal/appconfig -run 'TestSyncUnitsReportsOneOutcome|TestSyncUnitsOutcomeNil'` and `go test ./internal/host -run TestAdoptWritesNamedUnitsStatus`. Expect FAIL. |
| -- [ ] **Step 3:** Implement. Call the hook next to the existing |
| - `fmt.Printf` at each site; don't change any printed text. |
| + - `TestAdoptWritesNamedUnitsStatusAfterSync` (host, with fakes for the step and the writer): a full or config-only pass with a key writes twice (fetch outcome, then with units); without a key, once; restart-only, never. |
| +- [ ] **Step 2:** `go test ./internal/appconfig -run 'TestSyncUnitsReportsOneOutcome|TestSyncUnitsOutcome'` and `go test ./internal/host -run TestAdoptWritesNamedUnitsStatus`. Expect FAIL. |
| +- [ ] **Step 3:** Implement. Record state at each existing print site |
| + into the per-pass map; emit at the end; don't change any printed text. |
| - [ ] **Step 4:** Run step 2 again, then the existing declaration tests (`go test ./internal/appconfig -run 'TestSyncUnits.*Declar'`): PASS. |
| - [ ] **Step 5:** Commit: `appconfig: SyncUnits reports each declared unit's outcome`. |
| |
| @@ -333,7 +372,7 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| ### Task 7: Operator CLI `put` / `show`, and the console status rows |
| |
| **Files:** |
| -- Modify: `internal/host/unitops.go` (`RunSystemNamedUnits` dispatch), `internal/console/apisystemupdate.go` (`handleAPISystemStatus` rows, `:1057`), `system.go` (`systemBoxRow` `:326`; usage line `:220`), `docs/site/cli.md` (the `carlos system named-units` section, ~`:3343`) |
| +- Modify: `system.go:96` (replace the single `raw("named-units", box, ...)` leaf with a `named-units` group holding three `clireg` nodes: `install` (audience box-local, root), `put` and `show` (operator audience, as `host-env seed` is registered); follow AGENTS.md:526-545), `parity/parity.tsv` (rows for the new nodes), `parity/cli.tsv` and docs projection (regenerated with `make parity-doc`), `internal/host/unitops.go` (`RunSystemNamedUnits` becomes the install handler only), `internal/console/apisystemupdate.go` (`handleAPISystemStatus` rows, `:1057`), `system.go` (`systemBoxRow` `:326`; usage line `:220`), `docs/site/cli.md` (the `carlos system named-units` section, ~`:3343`). (Astra plan R1 #6.) |
| - Create: `internal/host/namedunitscli.go`, with its test |
| |
| **Interfaces:** |
| @@ -362,6 +401,7 @@ per `awsssm.go:21-25`); `rootstate` public stores; systemd root units. |
| - `TestNamedUnitsShowNamesBothRegions`: the parameter was read in `eu-west-1`, and the box record says region `eu-west-2` with `not-found`. Both regions are printed. |
| - `TestNamedUnitsShowBoxNotReported`: no row matches → the output says the box hasn't reported for that instance. |
| - `TestAPISystemStatusRowsCarryInstanceAndNamedUnits` (console). |
| + - The repo's command-tree checks pass after `make parity-doc`: `go test . -run 'TestParity'` and `go test ./internal/docsite -run TestParityPageIsCurrent`. |
| - [ ] **Step 2:** Run the new tests with targeted `-run` in `./internal/host`, `.` and `./internal/console`. Expect FAIL. |
| - [ ] **Step 3:** Implement. Write the CLI copy (the pickup line from the |
| spec, outcomes, the no-report line) and the docs/site/cli.md |