plan: held requests r2 (drain before the provider action)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+60
−53
docs/superpowers/plans/2026-11-08-region-ha-held-requests.md+60 −53
CI passed — run details
Intent
Attributed to this commit: 27.3 g CO₂e (2,613,893 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-region-ha-held-requests, 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/docs/superpowers/plans/2026-11-08-region-ha-held-requests.md b/docs/superpowers/plans/2026-11-08-region-ha-held-requests.md| index f099fd141..9b7044329 100644 |
| --- a/docs/superpowers/plans/2026-11-08-region-ha-held-requests.md |
| +++ b/docs/superpowers/plans/2026-11-08-region-ha-held-requests.md |
| @@ -1,87 +1,94 @@ |
| # Region HA G5: held requests during a planned handover |
| Branch `design-region-ha-held-requests` from origin/main 9622c65a. |
| -Spec: `docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md` §25 (rev 6.6). |
| -**Not to be built before Paul approves §25.** Review budget: three Astra rounds for spec and plan together. Stop at the first round with no P1. A family that draws a P1 twice gets cut or becomes an invariant (stopping rule 2). |
| +Spec: `docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md` §25 (rev 6.6, section r2 after Astra round 1). |
| +**Not to be built before Paul approves §25.** Review budget: three Astra rounds for spec and plan together; round 1 is spent (NOT READY, 11 P1, all dispositioned in §25.12). Stop at the first round with no P1. A family that draws a P1 twice gets cut or becomes an invariant. |
| ## Problem |
| -A planned member replacement answers 503 for every awake HA host it held, from SIGTERM until a peer re-reads the lock after the marker and restores the host (§25.1). The edge closes its listeners before any release (`internal/edge/edge.go:4855-4858`). Peers mark the box suspect and answer `Retry-After: 10` (`forwardha.go:376-380`, `hacache.go:27-41`). `errShuttingDown` falls through to a 503 with no `Retry-After` (`activator.go:6603-6611`). |
| +A planned member replacement answers 503 for every awake HA host it held. systemd stops the tenants before the edge's SIGTERM, then the edge closes its listeners before any marker, and peers then refuse with `Retry-After: 10` until a re-read finds the marker (§25.1). The fix drains the member while it is alive and holds requests on it, then takes the provider action on a lock-free box (§25.2). |
| ## Global constraints (binding on every task) |
| -- **I1 unchanged.** Only `hafresh.DecideAcquire` (`internal/hafresh/acquire.go:77-115`) authorizes an acquire. No notice, header or hold reaches it as an input. |
| -- **Unplanned paths unchanged.** A lost lease (`haleaseloss.go`), a suspect holder that is not marked departing, and every refusal that exists today keep their answers byte for byte. A test pins each one. |
| -- **No replay after possible delivery** (spec I9): hold only on the three triggers in §25.5, and buffer only within the bounds. |
| -- **No new polling.** The only S3 reads added are the fallback lock reads in §25.5, which are per handover. Tests count store calls. |
| -- **Census.** Any task that touches a census-pinned site (edge shutdown order, the hop answer path, units) carries a census card and cross-family approval on #280 before merge. |
| -- **Local verification:** run only the changed packages with `-run` locally. CI runs the full suite. |
| +- **I1 unchanged.** Only `hafresh.DecideAcquire` authorizes an acquire. A take request and an `X-Carlos-Handover` answer are never inputs to it. |
| +- **Unplanned paths unchanged.** A lost lease, a suspect holder, a hard kill and every refusal that exists today keep their answers byte for byte. A table test pins each one. |
| +- **No unsafe replay (I9).** Replay only after an `X-Carlos-Handover` answer, or under §6.5's existing rule. |
| +- **No new polling.** The S3 work added is per drain (§25.9). Tests count store calls. |
| +- **Census.** Tasks that touch census-pinned sites (the hop answer path, `begin`, the roll tool, root verbs) carry a census card and cross-family approval on #280 before merge. |
| +- **Local verification:** changed packages only, with `-run`. CI runs the full suite. |
| ## Task 0: `errShuttingDown` gets a real answer (independent; can merge first) |
| -- In `serveWakeError` (`activator.go:~6534-6611`), map `errShuttingDown` to the admission "unavailable" 503 with `Retry-After: 10`, and on a forwarded HA hop to `X-Carlos-Holder: none`. Stop logging it as a wake failure. |
| -- Test: a request waiting on a stopping entry during `Shutdown` gets that answer, not "instance failed to wake". |
| +In `serveWakeError` (`activator.go:~6534-6611`), map `errShuttingDown` to the admission "unavailable" 503 with `Retry-After: 10`, plus `X-Carlos-Holder: none` on a forwarded HA hop. Stop logging it as a wake failure. Test: a request waiting on a stopping entry during `Shutdown` gets that answer. |
| -## Task 1: handover mode on the departing box (§25.3) |
| +## Task 1: drain state and placement exclusion (§25.3 step 1, step 3) |
| -Files: `internal/edge/edge.go` (shutdown block), `internal/edge/shutdown.go`, `internal/activator/activator.go` (`Shutdown`, `begin`), new `internal/activator/handover.go`, `internal/host/agent.go` (shutdown order). |
| +Files: `internal/host/edgesfile.go`, `internal/host/group_admission.go`, `internal/edge/wakeplacement.go`, `internal/activator/haplacement.go`, `internal/activator/hacache.go`. |
| -1. Move `http.Server.Shutdown` after the activator's handover. The edge keeps accepting until `Service.Handover(ctx)` returns, or until D - 10 s. Then it runs `Shutdown` with what remains of the deadline. |
| -2. Handover state per host: `inflight-drain` (up to 10 s), then `releasing`, then `released(marker gen | none, successor)`. |
| -3. The edge answers a verified hop for a host in any handover state with 503 and `X-Carlos-Handover: pending`, before `act.ServeHTTP` and before admission (`admissionRefuses`, `edge.go:2068`). That ordering is the "not delivered" proof, and a test pins it. |
| -4. Direct requests for such a host are held through Task 3's queue, with this box as first box. |
| -5. Successor choice: `boxchoice.ChooseRoomiest` over the cached eligible serving set without self, or HRW for always-on workers. |
| -6. Strip `X-Carlos-Handover` from app responses beside `stripAppHolder`. |
| -7. Tests: listeners still accept during the release; the pending answer comes before the activator for every route kind; the in-flight bound is 10 s; a host with no marker gets `marker: none`; a deadline that runs out still closes. |
| +1. A box-level drain state and `drained`, published in the edges file. Pick the field (existing `Draining`/`Drained` or a new one) after checking every reader of `control/edges` (spec §24's rule for a new reader). The existing per-entry `draining` used by db reads keeps its meaning. |
| +2. While draining, the box never acquires an HA lease, keeps tenant admission open, and routes as a first box. |
| +3. Placement on every box treats a draining box as local-denied, including §6.3 rule 1 (releaser reuse). Test: a clean marker naming a draining box sends the wake elsewhere. |
| +4. `drained` drops holder-cache entries naming the box. |
| +5. A draining box is never made suspect by `ForwardSilent`. |
| +6. Tests: an asleep host, a remote-held host, a waking host and an alias or metadata-only route on a draining box; the same on a fixed forwarder. |
| -## Task 2: HA notices (§25.4) |
| +## Task 2: holds on the draining box (§25.4) |
| -Files: new `internal/edge/ha_notice.go` (modelled on `burst_notice.go:28-60`), `internal/host/edgesfile.go` (`ha_notices` field), `internal/activator/hacache.go` (departing state, notice-fed entries). |
| +Files: `internal/activator/activator.go` (`begin`), new `internal/activator/handover.go`, `internal/edge/forwardha.go`. |
| -1. `/.carlos/ha-notice`, sealed and signed with the mesh key, answered ahead of routing. Its own rate bound. Batches of up to 200 hosts, at most every 200 ms per peer. |
| -2. Kinds: departing, released, wake. Validate every ref against view(region) and drop anything else. |
| -3. Holder cache: a `departing` box state until its deadline. A connect failure to a departing box reports `ForwardHandover`, not `ForwardUnreachable`, and does not raise `OnDepartureSuspect`. |
| -4. A released notice sets a positive entry for the successor and signals Task 3's queue. |
| -5. A wake notice on the successor runs the forwarded-wake path (HA acquire, wake, never forward). A test proves it cannot acquire without a marker. |
| -6. Tests: seal and replay refusal; a notice from a box out of view is dropped; a box without `ha_notices` gets none; a mutation where a notice feeds `DecideAcquire` fails a test. |
| +1. Per-host move state: `holding` → `releasing` → `taken(holder)` | `left(reason)`. It sits under the activator lock and is checked after the loss latch. F-A wins in every state and answers held requests at once. |
| +2. Hold queue: 64 per host, 1,024 per box; 30 s for client requests, 25 s for hops. The body is unread. |
| +3. Release: a client request is forwarded once to the confirmed holder, streaming its body. A hop gets 503 `X-Carlos-Handover: holder=<ref>`. After the move, an `H → holder` memory answers stale hops the same way. |
| +4. Strip `X-Carlos-Handover` from app responses beside `stripAppHolder`. |
| +5. Held SSE streams on H close at an event boundary once the take is confirmed. |
| +6. Tests: the hold comes before app contact for every route kind; F-A at each phase; every bound; a stale hop after the move. |
| -## Task 3: the first-box hold (§25.5) |
| +## Task 3: replay on the first box (§25.4 replay, cross-region) |
| -Files: new `internal/edge/hahold.go`, `internal/edge/forwardha.go` (tee body, new outcomes), `internal/edge/backhaul_holder.go` (cross-region hold). |
| +Files: `internal/edge/forwardha.go`, `internal/edge/backhaul_holder.go`, new `internal/edge/replaybody.go`. |
| -1. Body tee for HA forwards: a bounded replay buffer (1 MiB per request, 64 MiB per box). The request is replayable only if the whole body fitted. |
| -2. Triggers: a pending answer; a connect failure to a departing box; a direct request on the departing box. Nothing else holds. A table test lists every existing 503 and asserts it is not held. |
| -3. Per-host queue: 64 per host, 1,024 per box, 15 s from the first refusal. Over a bound, today's answer. |
| -4. Release on a released notice, on the box's own release (departing box), or on the fallback lock read at 2, 4 and 8 s, coalesced through `holderCache.resolve`. A release goes through `forwardHAObserved` once, at most two attempts in all. |
| -5. Methods: any method with a definite non-delivery and a full buffer; after a post-connect failure, only bodyless GET, HEAD, OPTIONS or an un-switched upgrade (`backhaul_holder.go`'s existing rule). |
| -6. Held-stream requests (#432): held like a GET, released from memory, never wake. |
| -7. Cross-region first box: honour pending, one release hop at 4 s to another in-view box of the region. |
| -8. Metrics and log lines: `ha_hold_started`, `ha_hold_released{by=notice|own|read}`, `ha_hold_expired`, the longest hold per host. |
| -9. Tests: every bound; a POST with a body replays exactly; a POST after a mid-body reset is not replayed; reads are coalesced; the store call count per handover. |
| +1. A body wrapper for HA forwards and HA backhaul: it keeps the sent bytes up to 1 MiB and stops the transport from closing the client's body. The replay is the kept bytes plus the rest, with the same framing and trailers. |
| +2. On `X-Carlos-Handover`, validate the ref against view(region) and name authority, then make the second and last attempt. Serve locally if the ref is this box. |
| +3. The held-stream path (#432) follows the answer from memory, with no lock read. |
| +4. Tests: a hop POST, chunked with trailers, `Expect: 100-continue`; over 1 MiB sent is not replayable; no replay after a reset or any other answer; a cross-region POST through backhaul; an alias request. |
| -## Task 4: simulation (spec §21.1, §25.10) |
| +## Task 4: the take endpoint (§25.5) |
| -Extend `internal/sim/regionha` with planned terminations, notices (lost, delayed, reordered), held requests and the connect-phase failures. Add I9 and I10, plus the five mutations in §25.10. Run one 3,000-seed sweep per revision, serially, at nice 19. |
| +Files: new `internal/edge/hatake.go`, `internal/activator/hatake.go`, `internal/host/edgesfile.go` (`ha_take`). |
| -## Task 5: root-runner witness `TestRootHAHandoverHold` |
| +1. `/.carlos/ha-take`, hop-signed, with host and marker generation. Refusals as §25.5 lists them. It runs the HA acquire, never forwards, wakes within the wake bound, and answers the owner from `lock.json`. |
| +2. Tests: no marker, so the acquire table decides; another generation; a third box wins the race; the refusals; a mutation where take bypasses `DecideAcquire` fails a test. |
| -Extends `internal/host/haterminate_rootrunner_linux_test.go`'s apparatus (`~/.cache/region-ha-graceful/witness.py`): two real agents, real `TerminateInstances`, and a client driving GET, POST with body and SSE through both boxes. Pass: 0 non-2xx; holds within bound; acknowledged writes present after; the departing box's listener open until its handover lines. Budget: about 10 live runs. |
| +## Task 5: the drain driver and verb (§25.3) |
| -## Task 6: `region-group-roll.py replace` and roll changes (§25.8) |
| +Files: `internal/host/group_maintenance_verbs.go` (`drain`), `internal/host/hadrain.go` (new), activator hooks. |
| -1. New `replace` transaction: preflight (shape, health, per-member version floor), `TerminateInstanceInAutoScalingGroup` without decrement, watch (handover lines, departure done within §7.5's bound, replacement admitted, locks naming the old ref resolved), journal and report. |
| -2. Roll: add the floor check to preflight and the handover lines to verify. |
| -3. Fakes and argv tests in the existing style, plus one live attended call before any window, with the operator's real transport (P3b-2 lesson). |
| -4. Runbook text in `docs/region-ha-runbook.md` for resize: `instance_type`, apply, re-pin `group_launch_template_version`, then `replace` per member. The text goes through Paul's copy review on its own docs branch. |
| +1. The verb asks the agent to drain and waits. It prints the result as JSON: hosts moved, holder per host, longest hold, holds that got today's answer, hosts `left` with reasons. |
| +2. The driver: publish the drain state; wait one fold period; refuse if no eligible `ha_take` box exists; move hosts up to 4 at a time, busiest first; drain in-flight work for 10 s; stop with a drain reason; take; release the holds; publish `drained`; keep forwarding for one fold period; report. It has a deadline (§25.3). |
| +3. Tests: the full driver against two in-process boxes and MinIO; the deadline; a take that fails; S3 down during the release. |
| -## Task 7: spec, census and CI |
| +## Task 6: roll tool transactions (§25.6, §25.7) |
| -Update §25 with anything the build changes. Census cards for the edge shutdown order and the hop answer path. Run CI on the branch, then the branch review (Opus, cross-family against Astra implementers). |
| +Files: `scripts/region-group-roll.py` and its tests. |
| -## Dev witnesses (a dated card for 8a3d302 after release; not part of the build) |
| +1. `replace` and `remove`, under the existing lock and journal. Steps: preflight, floor (§25.7), `drain`, lock re-read, the provider call, watch, report. `remove` checks pools and the 2-member minimum. |
| +2. Roll: `drain` before `apply`, and the floor check. |
| +3. First install: with the floor absent, refuse a drain and say that DR-4's lock-free rule applies. |
| +4. Fakes and argv tests in the existing style, plus one attended live call on dev with the operator's real transport before any window (P3b-2 lesson). |
| +5. Runbook text for resize and scale-in in `docs/region-ha-runbook.md`, on its own docs branch with Paul's copy review. |
| -W0 hard kill control, W1 planned terminate, W2 roll, W3 resize (t4g.small to t4g.medium), each as §25.10 states, on the dev IE group with two HA hosts awake on the member and the probe at about 3 requests a second. DR-4's lock-free rule stays in every card until W1 to W3 pass on a release with §25 on every member. |
| +## Task 7: simulation (§21.1, §25.10) |
| + |
| +Add drains, take races, holds, replay, connect-phase failures, lease loss mid-move and stale caches. Add I9 and I10 and the mutations in §25.10. Run one 3,000-seed sweep per revision, serially, at nice 19. |
| + |
| +## Task 8: integration witness, spec, census and CI |
| + |
| +Write the in-process integration test of §25.10 against MinIO with two real activators and edges. Update §25 with anything the build changes, write the census cards and run CI. Then the branch review: Opus, cross-family when Astra implements. |
| + |
| +## Dev witnesses (a dated card for 8a3d302 after a release; not part of the build) |
| + |
| +W0 to W4 and the cross-region request, as §25.10 states, on the dev IE group. DR-4's lock-free rule stays in every card until W1 to W4 pass on a release at the floor. |
| ## Order |
| -Task 0 first (independent). Then 1 and 2, which share the activator handover state. Then 3, then 4 and 5. Task 6 can run beside 3 to 5. Then 7. Release, then the card. |
| +Task 0 first (independent). Then 1, then 2, 3 and 4 in parallel (2 and 4 share activator state; one implementer for both), then 5, then 6 and 7, then 8. Release, then the card. |