plan: region HA graceful terminate (T1/DR-4)
A terminated group member writes no release marker; its HA instances wait out the lease. Keep instance-control and D-Bus up until the edge stops, bound the release to one shutdown deadline, and witness it with a real EC2 terminate.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+212
−0
docs/superpowers/plans/2026-11-08-region-ha-graceful-terminate.md+212 −0
diff --git a/docs/superpowers/plans/2026-11-08-region-ha-graceful-terminate.md b/docs/superpowers/plans/2026-11-08-region-ha-graceful-terminate.md| new file mode 100644 |
| index 000000000..0c67099f7 |
| --- /dev/null |
| +++ b/docs/superpowers/plans/2026-11-08-region-ha-graceful-terminate.md |
| @@ -0,0 +1,212 @@ |
| +# Region HA T1/DR-4: a planned member termination hands its leases off in seconds |
| + |
| +Branch `region-ha-graceful-terminate` from origin/main 326d4677. |
| +Spec: `docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md` |
| +(§5.2 marker at release, §9 termination and takeover). This plan adds §9.9. |
| +Root-cause read (Astra, 2026-11-08): `.superpowers/sdd/2026-11-08-region-ha-graceful-terminate/rca-report.md` |
| +(git-ignored; the facts it rests on are restated below). |
| + |
| +## Problem |
| + |
| +A group member holding HA leases is terminated (ASG terminate, EC2 ACPI |
| +power-off, systemd shutdown). No release marker is written, so every HA |
| +instance it held answers 503 until the lease expires (~13-15 min; dev |
| +2026-11-08 02:31-02:44Z, G2 drills 1-2). |
| + |
| +Proven in code (rca-report §A, §B, §D): |
| + |
| +1. With generations on, a stopped entry reaches `finishStop` and |
| + `releaseHAClean` only after `releasable` proves the generation gone through |
| + `StopGen`, a root RPC to `carlos-instance-control.service` |
| + (`activator.go:5001-5014`, `opset.go:257-339`, `containment.go:460-469`). |
| + At system shutdown nothing orders that service (or its socket) after the |
| + edge, so systemd stops it in parallel, and a socket with a pending stop job |
| + refuses activation (systemd `socket.c` 2138-2146). The proof fails, the stop |
| + stays pending, no marker. |
| +2. The agent's unprivileged `systemctl show` (exit observation) uses the system |
| + D-Bus; `dbus.service` is not ordered after the edge either. |
| +3. The release budgets (files 2 min, sync 30 s, marker 10 s, each |
| + `WithoutCancel`) do not fit the agent's 60 s Shutdown wait, which itself |
| + starts only after the edge drain and joins; systemd's default 90 s |
| + `TimeoutStopSec` then SIGKILLs. A release still running is abandoned. |
| + |
| +Instance templates keep `BindsTo=` + `After=carlos-edge.service` (they stop |
| +before the edge at shutdown). Reversing that deadlocks the agent's blocking |
| +`systemctl stop` behind the edge's own stop job (rca-report §E). Not changed. |
| + |
| +## Global constraints (binding on every task) |
| + |
| +- Single writer. A marker (clean or unclean) is written only after every |
| + writer of the host is proven stopped AND the files publisher and the replica |
| + finalizer have RETURNED. A timeout never stands in for that proof. If the |
| + finalizer or publisher has not returned by the deadline: no marker, lease |
| + left to expiry (today's crash path). Never wrap Finish/Final in a goroutine |
| + and write a marker while it may still run. |
| +- "No takeover before proof AND expiry" is unchanged for the unclean path |
| + (`hafresh/acquire.go`); a marker lets the next holder take over at once |
| + (existing path, unchanged). |
| +- Hard-crash behaviour unchanged (no marker, wait for expiry). |
| +- Edge restarts (`systemctl restart carlos-edge`, rolls) behave as today: |
| + instance units still stop before the edge via BindsTo/After. |
| +- Exact values: agent shutdown deadline `agentShutdownBudget = 90 * time.Second` |
| + measured from receipt of SIGTERM; `carlos-edge.service` `TimeoutStopSec=120`; |
| + marker reserve `haShutdownMarkerReserve = 10 * time.Second`; final-sync |
| + reserve inside the shutdown deadline `haShutdownSyncReserve = 30 * time.Second` |
| + (files Final gets until deadline - 40 s, the sync until deadline - 10 s, the |
| + marker until the deadline; each also keeps its existing cap of 2 min / 30 s / |
| + 10 s; a budget never drops below 1 s). |
| +- Go: `TMPDIR=/tmp/g` for socket-binding packages (activator, edge, host); |
| + run only touched packages with `-run`; never `go test ./...`. Default |
| + GOCACHE. Scratch in `$TMPDIR` or the SDD workspace. |
| +- Commits: subject < 80 chars, body after a blank line, end with |
| + `Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>`. |
| +- NEVER run `amadan branch merge` (in any form) — the controller merges. |
| + |
| +## Task 1: keep instance-control and D-Bus up until the edge has stopped |
| + |
| +Files: `internal/host/units/carlos-edge.service` (embedded; find every test |
| +that pins its content or parses it, e.g. grep for `carlos-edge.service` in |
| +`internal/host/*_test.go` and the unit installer `ensureSystemUnits`), |
| +`internal/activator/cmd.go` and `internal/activator/activator.go` comments |
| +that say "systemd's 90 s TimeoutStopSec". |
| + |
| +Change the edge unit's `[Unit]`: |
| + |
| +``` |
| +Wants=network-online.target carlos-instance-control.service |
| +After=network-online.target data-instances.mount run-carlos-instances.mount run-carlos-logs.mount carlos-instance-control.socket carlos-instance-control.service dbus.service |
| +``` |
| + |
| +and `[Service]` `TimeoutStopSec=120`, each with a comment stating why: |
| +systemd stops units in reverse start order, so the edge (holding HA leases) |
| +stops before the root control service, its socket and D-Bus, whose RPCs and |
| +reads are the stop proofs a release needs (region HA T1/DR-4); `Wants=` makes |
| +the control service run from boot, because a socket with a pending stop job |
| +refuses activation, so it cannot be socket-started during shutdown. Do not |
| +touch `BindsTo`/`After` on any instance template. |
| + |
| +Check the change does not create an ordering cycle with the control service: |
| +it is `After=` the three mounts only, and `Requires=` its socket; the group |
| +drop-in `carlos-edge-group.conf` adds nftables. Write a Go test that parses |
| +the embedded edge unit and asserts: `Wants` contains |
| +`carlos-instance-control.service`; `After` contains |
| +`carlos-instance-control.service`, `carlos-instance-control.socket`, |
| +`dbus.service`; `TimeoutStopSec=120`; and that no embedded |
| +`carlos-instance*` template or `carlos-instance-control.service` has |
| +`Before=carlos-edge.service` or orders itself after anything that is ordered |
| +after the edge (a direct cycle check over the embedded units is enough). |
| +If `systemd-analyze verify` is available locally, run it on a scratch copy of |
| +the units and report the output (not a gate). |
| + |
| +Update the two comments that name a 90 s systemd stop budget. |
| + |
| +## Task 2: one shutdown deadline from SIGTERM; HA release fits inside it |
| + |
| +Files: `internal/host/agent.go` (SIGTERM handling, `svc.Shutdown` call site, |
| +lines ~1290-1348), `internal/host/claimfilesseed_shutdown.go` |
| +(`joinAgentShutdown`), `internal/activator/activator.go` (`Shutdown`, |
| +`shutdownCtx`), `internal/activator/harelease.go` (`releaseHAClean`). |
| + |
| +1. The agent records the instant it receives SIGTERM/SIGINT (where the edge's |
| + `<-stop` fires, or the agent's own signal context; find the single earliest |
| + point) and derives one deadline `sigterm + agentShutdownBudget`. The |
| + context `joinAgentShutdown` returns (today a fresh 60 s) must not end |
| + later than that deadline; existing shorter inner budgets stay. Do not |
| + lengthen any existing step. If wiring the instant through the edge is |
| + invasive, take it as the time `e.Run`'s stop channel fires via an exported |
| + hook or a package-level `atomic` set by the agent's signal handler; pick the |
| + smallest correct seam and say which in the report. |
| +2. `releaseHAClean`: when the service is shutting down and `a.shutdownCtx` |
| + has a deadline D, each step's budget is the minimum of its existing cap and |
| + the time left before its own cut-off: files Final until |
| + D - (haShutdownSyncReserve + haShutdownMarkerReserve), Finish until |
| + D - haShutdownMarkerReserve, the marker PUT until D; floor 1 s. Outside |
| + shutdown nothing changes. Keep `WithoutCancel` (a cancelled shutdown ctx must |
| + not kill the marker), only the deadlines move. The marker is still written |
| + only after Final and Finish have returned (constraint 1). |
| +3. `Shutdown` already waits on each stopping entry's `stopped` channel. Confirm |
| + (and say in the report, with file:line) that `stopped` closes only after |
| + `finishStop`'s release returned, so a marker in flight is joined until D. |
| + At exit, log one line per HA entry still not released: |
| + `activator: shutdown: <host>: HA lease not released (<state>); it frees at expiry` |
| + (extend `logUnresolvedAtExit` or add beside it), and one line per HA host |
| + released during shutdown is already logged by releaseHAClean. |
| + |
| +Tests (activator package, fakes as the existing harelease tests use; find |
| +them with grep `releaseHAClean\|ReleaseMarker` in `internal/activator/*_test.go`): |
| +- shutdown with D = now+20 s and a files publisher whose Final blocks until its |
| + ctx ends: Final's ctx deadline is <= D-40s floor-clamped (1 s), Finish and |
| + the marker still run, the marker is written UNCLEAN before D. |
| +- shutdown with a Finish that never returns: Shutdown returns by D (+ small |
| + slack) and NO marker is written (the fake records no ReleaseMarker call). |
| +- shutdown, everything healthy: CLEAN marker; budgets were capped (the fakes |
| + record their ctx deadlines). |
| +- not shutting down: budgets are the existing 2 min / 30 s / 10 s. |
| +Mutation check (in a scratch copy, never in place): remove the cap in (2) and |
| +show the first test fails; report it. |
| + |
| +## Task 3: production-wired witness — a real EC2 terminate of a runner holding a lease |
| + |
| +Goal: measure, on a real arm64 EC2 (root runner, dev account kass, |
| +eu-west-1, see `~/.cache/tmp/claude-1001/own/after236/root-runner.py` and |
| +`root-runner-contract.md` beside it), the time from the EC2 |
| +TerminateInstances call to the HA release marker, through production units |
| +and the real agent, and show main fails where the branch passes. |
| + |
| +Build on the cold root harness (`newColdRootHarness` in |
| +`internal/host/haplacement_root_linux_test.go`; it installs the real |
| +embedded units, the real agent as `/opt/carlos/carlos`, generations, HA, and |
| +an in-process S3 fixture `leasetest.NewS3Server`). New file |
| +`internal/host/haterminate_rootrunner_linux_test.go`, build tags |
| +`linux && rootrunner`, test `TestRootHATerminateRelease`: |
| + |
| +1. Re-exec itself as a transient systemd unit (`systemd-run --unit |
| + ha-terminate-witness --property Before=carlos-edge.service |
| + --property DefaultDependencies=no --property Conflicts= |
| + ...` or the minimal property set that makes systemd stop it only AFTER |
| + carlos-edge.service at poweroff and not kill it early); the outer test |
| + (running under SSM) waits for a `ready` file then exits 0, leaving the |
| + unit running. Document the exact properties chosen and why. |
| +2. Inside the unit: start the harness, wake one HA host so its lease is held |
| + (generations on, nested layout, as the harness's HA tests do), check the |
| + lease is held by this box, then write `ready`. |
| +3. The S3 fixture mirrors every write to the host's lock object, and every |
| + 10 s a heartbeat, synchronously to REAL S3 before replying: |
| + `s3://carlos-dev-640012953448/root-runner/<run>/events/<unixnano>-<kind>.json` |
| + (kind = lock-write | heartbeat | unit-stop), body = the lock JSON (marker |
| + fields: release present, clean flag) or heartbeat time. The runner role |
| + may write only `root-runner/*`. `<run>` comes from an env var the driver |
| + sets. On its own SIGTERM (after the edge has stopped), it uploads |
| + `journalctl -u carlos-edge.service -u carlos-instance-control.service |
| + -u 'carlos-instance*' -b --no-pager -o short-iso-precise` to |
| + `.../<run>/journal.txt` and a final `unit-stop` event. |
| +4. No other network beyond IMDS, the regional S3 endpoint and SSM. |
| + |
| +Driver (outside the repo, `~/.cache/region-ha-graceful/witness.py`, copied |
| +from root-runner.py): build the tagged test binary for linux/arm64, launch a |
| +runner, run the outer test over SSM, wait for its success, record t_term, call |
| +`ec2 terminate-instances`, then poll `root-runner/<run>/events/` for up to |
| +20 min; print: t_term, first lock-write after t_term (marker? clean?), the |
| +delta, last heartbeat, and save the journal. It must ALWAYS terminate the |
| +instance (it does by design here) and print LEAKED on failure. |
| + |
| +Runs (controller executes; sandbox off; AWS profile `kass`): |
| +- baseline: the witness test file applied on origin/main 326d4677 (a scratch |
| + worktree), expected: no marker before the box dies. |
| +- branch: expected: marker within 60 s of t_term (target: well under). |
| + |
| +Report the two timelines. The witness is the acceptance gate for the fix. |
| + |
| +## Task 4: spec, docs, census, CI |
| + |
| +- Spec §9.9 "Planned termination" (new subsection in the spec named above, |
| + plus a revision line at the top): what a graceful stop guarantees (marker |
| + within the shutdown deadline when the proofs and final durability finish; |
| + else no marker and expiry), the unit ordering facts, the deadline values, and |
| + that an ASG termination lifecycle hook is NOT used (ruling, see ledger) and |
| + when it would be needed. |
| +- AGENTS.md: only if it states the edge's stop budget or unit ordering. |
| +- Census (#280): if CI's `TestPlan14RootReadCensus` fails, run |
| + `make census-card` / `make census-propose NAME=region-ha-graceful-terminate |
| + CLAIMS=<file>` per AGENTS.md; the controller gets the cross-model approval. |
| +- CI green on the branch tip. |