spec §25 r2: drain before the provider action (Astra R1)
Handover moves before any systemd action; holds live only on the draining member; no notices or fallback reads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+124
−108
docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md+124 −108
Intent
Human
<task-notification> <task-id>bcb4ivkjc</task-id> <tool-use-id>toolu_01LmGGtCHFJSSmuJXZC3dn2N</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/283be278-5c6a-4a71-a8d6-b3d32c668985/tasks/bcb4ivkjc.output</output-file> <status>killed</status> <summary>Background command "Wait for Astra round 1 findings" was stopped after reaching its background time limit</summary> <note>If the work in progress still needs it, start it again with `run_in_background` and a longer `timeout`. If it already had the longest `timeout` allowed, do not restart it. Either way, report that it was stopped.</note> </task-notification>
diff --git a/docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md b/docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md| index 4affa1908..a73255490 100644 |
| --- a/docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md |
| +++ b/docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md |
| @@ -1030,172 +1030,188 @@ exercise unknown evidence so old writers retain their behavior. DNS |
| readers exercise missing configured ingress; configuration alone must not |
| be the protection against publishing disposable member addresses. |
| + |
| ## 25. Held requests during a planned handover (G5, rev 6.6, 2026-11-08) |
| **Ruling being designed.** Paul, 2026-11-08, via agent manager 8a3d302: a planned member replacement gives tenants of HA-pool hosts a pause of a few seconds and no errors, instead of today's 503 with `Retry-After: 10`. "Planned" covers a roll, a re-pin, an instance-size change through the launch template, and scale-in. This is also what a self-hosted fleet such as Tito's needs to resize without downtime. Unplanned deaths keep today's behaviour: 503 with `Retry-After` until lease expiry plus termination proof (§9.4, N-HA9). |
| Line citations in this section are to `origin/main` at 9622c65a. |
| +**Revision note (r2 of this section).** Astra round 1 (`~/.cache/region-ha-held/ASTRA-R1.md`, 11 P1) found that a handover started at SIGTERM is too late. systemd stops the tenant units before the edge receives its signal (`internal/host/units/carlos-instance@.service:14-16`, `carlos-instance-gen@.service:15-17`, both bound to the edge and ordered after it), so in-flight requests lose their app first. This revision moves the handover **before** any systemd action: the operator's tool drains the member while it is fully alive, and terminates or restarts it only once it holds no lease. That one move also removed the SIGTERM deadline arithmetic, the peer notices, the holds on every first box and the fallback lock reads, which drew R1.4, R1.5, R1.6, R1.7, R1.12, R1.13 and R1.14. §25.12 maps every finding. |
| + |
| ### 25.1 What happens today |
| -T1/DR-4 (§9.9) writes a clean release marker about 3 s after `TerminateInstances`. The requests around it still fail, for three reasons in the code: |
| +T1/DR-4 (§9.9) writes a clean release marker about 3 s after `TerminateInstances`. Requests still fail around it: |
| -1. **The listeners close first.** On SIGTERM the edge closes admission (`beginShutdown`, `internal/edge/shutdown.go:9-14`), then runs `http.Server.Shutdown` on both listeners with one 10 s context (`internal/edge/edge.go:4855-4858`). Only after that, and after the agent's joins, does `Service.Shutdown` (`internal/activator/activator.go:6127`) stop instances and write markers. A new connection to the dying box is refused for the whole release. |
| -2. **Peers treat that as a dead holder.** A refused dial makes the holder suspect (`internal/activator/hacache.go:27-41`, `holderResult` 438-511). Each forward answers 503 with `Retry-After: 10` (`answerHolderUnreachable`, `forwardha.go:376-380`). One lock re-read per 10 s suspect window finds the marker (`runRead`, `hacache.go:358-363`), so a host is down for up to about 10 s after its marker, plus its restore. |
| -3. **The dying box refuses its own clients.** A request already inside `begin` sees admission closed and gets 503 `Retry-After: 10` (`activator.go:1786`). A request for a host that is stopping waits on `e.stopped`, then tries to wake the host again here and gets `errShuttingDown`. That error has no case in `serveWakeError` and answers 503 "instance failed to wake" with no `Retry-After` (`activator.go:6603-6611`). This is a bug today, whatever this section decides. |
| +1. **Tenants stop first.** Instance units are bound to the edge and ordered after it, so at shutdown or an edge restart they stop before the edge gets SIGTERM (R1.1). §9.9's witness shows it: the generation stop at 04:11:04.770, edge SIGTERM at 04:11:05.292. |
| +2. **The listeners close next.** On SIGTERM the edge closes admission (`internal/edge/shutdown.go:9-14`), then shuts both listeners with one 10 s context (`internal/edge/edge.go:4855-4858`). Only after that does `Service.Shutdown` (`internal/activator/activator.go:6127`) write markers. |
| +3. **Peers see a dead holder.** A refused dial makes the holder suspect (`internal/activator/hacache.go:27-41`). Each forward answers 503 with `Retry-After: 10` (`internal/edge/forwardha.go:376-380`) until a suspect-window re-read finds the marker (`hacache.go:358-363`). |
| +4. **A bug.** A request waiting on a stopping host that then meets `errShuttingDown` falls through `serveWakeError` to 503 "instance failed to wake" with no `Retry-After` (`activator.go:6603-6611`). |
| -The flagship A5 drill (2026-11-08, before T1 reached the boxes) saw the unplanned shape: 503 from the next second until lease expiry +8 s. With T1 the planned shape is shorter, but it is still a run of errors per awake host. |
| +Nothing on main drains a member: role `drain` is a value that group boxes treat as invalid (`internal/host/group_admission.go:101`), and the roll tool never looks at leases (`scripts/region-group-roll.py`). |
| ### 25.2 The model |
| -A planned handover moves each awake host from the departing box A to another serving box, and every request that arrives meanwhile **waits at the box whose edge accepted the client's connection** (the *first box*). The first box can be a group member, a fixed forwarder such as adi, or A itself for clients already connected to it. The first box keeps the request, its body and the client's connection until the host's new holder is known, then forwards it once, as §6.4 allows. Nothing is replayed that may have reached A's app. |
| - |
| -Three rules carry over unchanged: |
| -- **I1.** Only the release marker lets another box acquire (§9.4). Nothing in this section authorizes an acquire. A notice (§25.4) is advice, so a box that acts on one still reads the lock and makes the conditional write. |
| -- **"Takeover only after proof and expiry"** stands for every lease that has no marker. A planned handover that fails to write a marker falls back to it, and so to today's 503s. |
| -- **F-A.** A box that has lost its lease answers 503 `Retry-After: 10` with holder `none` at once, as today. A lost lease is never a handover, and a first box never holds on that answer. |
| - |
| -### 25.3 The departing box |
| +A planned replacement of member A runs in two phases: |
| +1. **Drain A while it is alive.** A stops being a placement choice. It then moves each awake host, one host at a time, to a successor box, and **holds every request for that host on A** while the host moves. A is the only place anything is held. |
| +2. **Then the provider action.** Once A holds no lease, the tool terminates it, restarts its edge (roll), or removes it (scale-in). At that point T1's shutdown has nothing to release, so its 503s cannot happen. |
| -When SIGTERM arrives on a box with `CARLOS_REGION_HA`, the agent enters **handover mode** for at most the shutdown deadline D (SIGTERM + 90 s, §9.9). The order in shutdown changes as follows: |
| +Rules that carry over unchanged: |
| +- **I1.** Only the release marker lets another box acquire (§9.4). The successor acquires through `hafresh.DecideAcquire` (`internal/hafresh/acquire.go:77-115`) as any box does. Nothing here is an input to it. |
| +- **Takeover after proof and expiry** stands for every lease without a marker. A drain that fails to write a marker leaves the host on today's path. |
| +- **F-A.** A host whose lease is lost answers 503 `Retry-After: 10` with holder `none` at once, in every phase below (§25.4). |
| +- **I4.** A request makes at most two forward attempts, and a box never forwards a request it received as a hop (§6.4, §6.5). |
| -1. **Announce.** The box closes tenant admission for new wakes and clears the serving latch, as today, so lb-health turns 503 and the box stops taking leases. It publishes no edges change. It sends a **departing notice** to every box in its view of its region (§25.4). |
| -2. **Keep the listeners open.** `http.Server.Shutdown` moves to the end of handover mode. Until then the edge accepts connections and answers: |
| - - a **forwarded hop** for a host awake or stopping here: 503 with `X-Carlos-Handover: pending`, written by the edge before the request reaches the activator or the app (§25.6). The header means the request was not delivered; |
| - - a **direct request** for such a host: held here, as on any first box (§25.5); |
| - - a request for any other host: routed as from any first box. This box is no longer a placement choice for anything, because its admission is closed. |
| -3. **Finish in-flight work, bounded.** For each awake host, requests already admitted to the app may run for up to 10 s, the same bound as today's HTTP drain. Edge-held stream legs are cancelled first (#432 G5). Long requests and hijacked WebSockets still running at 10 s are cut, as they are today; their clients reconnect and the reconnect is held like any request. |
| -4. **Release.** Every awake host is stopped and released in parallel, as `Service.Shutdown` does today, with the §9.9 budgets. Nothing about the marker changes. |
| -5. **Hand over.** As each host's release returns, the box picks a **successor**: `ChooseRoomiest` over its cached eligible serving set without itself (§6.3), or the HRW box for an always-on worker (A10). It then: |
| - - sends a **released notice** to every box in its view of the region, naming the host, the marker's generation and the successor; |
| - - forwards the requests it holds for that host to the successor, which acquires from the marker and wakes (§25.5); |
| - - when it holds no request for the host, sends the successor a **wake notice** if the host had a request in the last 60 s or is an always-on worker. The successor treats a wake notice as a forwarded wake with no body (§6.4): HA acquire, then wake, never forward. |
| +### 25.3 Phase 1: draining member A |
| - A host whose release ends without a marker (S3 down, stop proof failed, deadline) gets a released notice with `marker: none`. Its held requests get today's answer, 503 `Retry-After: 10`. |
| -6. **Close.** When every host is handed over and every held request is answered, or at D - 10 s, the edge runs `http.Server.Shutdown` with what remains of the deadline. Then the process exits as today. |
| +The roll tool starts it with a new root verb, `carlos system group-maintenance drain`, run over SSM as `apply` is. The verb asks the agent to drain, waits, and prints the agent's result. The agent's steps: |
| -The successor is a hint. If two boxes race, for example a first box that read the marker itself, the conditional PUT picks one winner and the loser forwards to it (§6.7). |
| +1. **Withdraw from placement.** A publishes the box-level drain state in its edges file. Placement already treats it as local-denied (`internal/edge/wakeplacement.go:36-42`). That covers §6.3 rule 1 too: a clean marker naming A no longer sends a wake back to A, because A is not eligible (R1.5). A keeps its admission open and keeps serving and forwarding as a first box (R1.3). Only its own new acquires stop: a request for a host A does not hold goes to the placement choice, exactly as on any box that is not the choice. A then waits one fold period (60 s: the 30 s edges poll plus a margin) so every peer has excluded it before any host moves. |
| +2. **Move each awake host H**, up to 4 at a time, busiest first: |
| + 1. *Hold.* New requests for H that reach A are held (§25.4), whether they came straight from a client or as a hop from a peer. |
| + 2. *Finish in-flight work.* Requests already admitted to H's app get up to 10 s, today's HTTP drain bound. Then the stop proceeds as a shutdown stop does. Stopping the app ends what is left: long requests, WebSockets and edge-held stream legs (#432 G5). |
| + 3. *Release.* `finishStop` writes the marker (§5.2), with the drain as its stop reason and the existing budgets. |
| + 4. *Take.* A chooses a successor B: `ChooseRoomiest` over its eligible serving set (§6.3), or the HRW box for an always-on worker (A10). It sends B a signed **take** request (§25.5). B runs the HA acquire and wakes H, then answers with the lease owner it now sees in `lock.json`. That answer is the **confirmed holder**: B, or a third box that won the marker first (§6.7). |
| + 5. *Release the holds.* A sends each held request to the confirmed holder (§25.4) and remembers `H → holder` for the rest of the drain. |
| + 6. *Failures.* No marker (S3 down, failed stop proof), a take that fails, or no confirmed holder: every held request for H gets today's answer, 503 `Retry-After: 10`, and the drain records the host as `left` with its reason. A held lease left this way is on today's expiry path. |
| +3. **Report drained.** When no lease names A, it publishes `drained` in its edges file. Peers then drop holder-cache entries naming A (a holder that left the serving set, §6.6). A keeps serving as a forwarder for one more fold period, answering any stale forward from its `H → holder` memory, then reports to the tool. |
| -**Restart on the same box.** In a roll (§25.8) the box restarts its edge in Standby. Its markers name it, so placement rule 1 (§6.3) sends a later cold wake back to it once it serves again. Hosts that were handed over stay on their successor until they sleep. |
| +A request for a host that is asleep, or held elsewhere, is never held on A; it is routed as today (R1.3). |
| -### 25.4 Notices |
| - |
| -Notices reuse the probe-burst notice transport (`internal/edge/burst_notice.go:28-60`): a POST over the backhaul port, sealed and signed with the mesh key, from one goroutine per peer with a bounded queue, and answered ahead of routing. A notice gets a new path, `/.carlos/ha-notice`, and its own rate bound. It is opt-in through a new additive edges field, `ha_notices: 1`, so a box sends notices only to peers that publish it. |
| - |
| -| Notice | Fields | The receiver | |
| -|---|---|---| |
| -| departing | box ref, instance id, deadline | marks the box *departing* in its holder cache until the deadline. A failed dial to a departing box means "handover", not "suspect". | |
| -| released | host, marker generation or `none`, successor ref | sets its holder entry for the host to the successor (positive, as an `X-Carlos-Holder` answer would) and releases its held requests for the host (§25.5). With `marker: none` it answers them as today. | |
| -| wake | host, marker generation | runs the HA acquire for the host and wakes it, as for a forwarded wake. It is only sent to the successor. | |
| +**The bound.** The drain verb has a deadline: 15 minutes plus 1 minute per 20 awake hosts. At the deadline the agent stops moving hosts, releases every hold with today's answer, and reports which hosts are left. The tool then refuses to go on unless the operator passes `--accept-left`, which takes the old DR-4 behaviour for those hosts. |
| -A receiver validates every ref against view(region) (§4.3) and drops a notice from a box not in view. **A notice never authorizes anything:** an acquire still reads `lock.json`, and a holder named by a notice is a cache entry like any other (§6.6). |
| +**Lost lease (F-A, R1.2).** The hold decision is made in the activator, under its lock, after the loss latch is checked (`activator.go:1760-1764`, `haforward.go:128-147`). A host whose lease is lost before or during its move is never held. If the loss latches while requests are held, they get the F-A answer at once, and the host is recorded `left` (lease lost). |
| -Notices are best effort. A lost released notice costs time, not correctness, because the first box falls back to the lock (§25.5). A released notice is sent once per host. Notices travel in batches of up to 200 hosts per POST per peer, sent at most every 200 ms. |
| +### 25.4 Holds on A |
| -### 25.5 The first box: holding and releasing |
| +**Where.** In memory on A, in a per-host queue, inside the activator's `begin` path after the loss check and before any app contact. That position is what proves a held request was not delivered. |
| -A request is **held** when the first box would otherwise answer it 503 for one of these reasons, and only these: |
| -- a forward got `X-Carlos-Handover: pending` (§25.3 step 2); |
| -- a forward to a box marked departing failed to connect (§25.4); |
| -- on the departing box itself, a direct request for a host awake or stopping there. |
| +**What is held.** Any request for a host that is moving, any method, from a client or a hop. A does not read the body of a held request: the client, or the peer's transport, stays blocked on sending it. |
| -Everything else answers as today: a lost lease (F-A), `HAUnavailableResponse`, a held lease of a box not departing, admission refusals, and every refusal for an unplanned death. |
| +**Release.** |
| +- **A request A received from a client** is sent to the confirmed holder as A's one forward, streaming its unread body as today. If A itself is named (it cannot be: A refuses acquires while draining), the request gets today's answer. |
| +- **A request A received as a hop from a first box C** cannot be forwarded on (§6.4). A answers it 503 with `X-Carlos-Handover: holder=<ref>`, which means "not delivered, send it there". C validates the ref against its view (§4.3) and makes its second and last attempt to that box, replaying the request (below). If the named box is C itself, C serves it locally, which spends no forward. C also caches the holder, as for `X-Carlos-Holder`. |
| +- **A stale forward after the move** (C's cache still names A) gets the same answer from A's `H → holder` memory, so C does not have to read the lock. |
| -**Where it waits.** In memory on the first box, in a per-host queue. A held request keeps its client connection and its buffered body (below). The box answers nothing until the host is released. |
| +**Replay on C (R1.8).** C's HA forwards wrap the request body so that the bytes the transport sent are kept, up to 1 MiB, and the transport cannot close the client's body. A replay is the kept bytes followed by the rest of the client's body, with the same framing and trailers. Because A never reads a held body, what C's transport has sent by the time A answers is at most what fits in socket buffers. A body whose sent part outgrew 1 MiB is not replayable, and that request gets today's answer. Only the `X-Carlos-Handover` answer allows a replay. A connection failure or any other answer after the request was sent replays nothing, except under §6.5's existing rule for bodyless GET and HEAD. |
| -**What releases it,** in order of preference: |
| -1. a released notice for the host, which names the successor: forward every held request for the host to it; |
| -2. on the departing box itself, its own release of the host (§25.3 step 5); |
| -3. a **fallback lock read.** If no notice has come 2 s after the first hold for the host, the box reads `lock.json` once for the host, coalesced (one read in flight per host, as `resolve` does today, `hacache.go:270-341`). It retries at 4 s and 8 s from the first hold. A release marker sends the held requests through the normal §6.2 decision: the box acquires and wakes the host itself, or forwards to the §6.3 choice. An unexpired lease of a box in view sends them there. A held lease of the departing box keeps them waiting. Any other answer gives today's response. |
| - |
| -A released request is forwarded at most once, to one box, and the receiver never forwards it again (§6.4). If that forward fails, the request gets today's answer for that failure. A held request is never held twice. With the refused first send, a held request makes at most two forward attempts, §6.5's limit; §6.5's own retry is not added on top (I4). |
| +**The header.** `X-Carlos-Handover` is honoured only on an answer to a hop C sent over its verified forward or backhaul transport. It is stripped from app responses at the tenant boundary, beside `X-Carlos-Holder` (`stripAppHolder`, `forwardha.go:118-128`). |
| **Bounds.** The constants are the builder's to tune; the shapes are not. |
| | Bound | Value | When exceeded | |
| |---|---|---| |
| -| Time held, from the first refusal | 15 s | today's answer: 503, `Retry-After: 10` | |
| +| Time held on A | 30 s, the wake wait bound (`wakeWaitBound`, `activator.go:1208`) | today's answer | |
| | Held requests per host | 64 | the new request gets today's answer | |
| | Held requests per box | 1,024 | the same | |
| -| Buffered body per request | 1 MiB | the request is not holdable (below) | |
| -| Buffered bodies per box | 64 MiB | the same | |
| +| Kept body bytes per forward on C | 1 MiB | not replayable: today's answer | |
| + |
| +**Streams and upgrades (R1.9).** |
| +- An established WebSocket or long request to H is ended by H's stop (step 2.2), and its client's reconnect is held like any request. |
| +- An **edge-held SSE stream (#432)** whose outer connection is on A, or proxied through A, is closed at an event boundary once H's take is confirmed. The browser reconnects with `Last-Event-ID`. The reconnect reaches C or A and goes to the holder from the handover answer or the cache, never through a lock read. A marked request is never a reason to read a lock, and it never wakes a host (#432 G2). |
| +- An upgrade that has not switched protocols is held like any request. |
| + |
| +**Cross-region first boxes (R1.7).** A box in another region that reaches A over backhaul is a first box like C. It honours `X-Carlos-Handover`, validates the named box against that region's view and name authority (`internal/edge/backhaul_holder.go:167-192`), and spends its second attempt there. It needs no notice and reads no foreign bucket. |
| -The departing box applies the same bounds and stops holding at D - 10 s. |
| +### 25.5 The take request |
| -**Which requests may be held.** A request may be held only when the first box can prove it was not delivered and can send it again exactly as received: |
| -- **Not delivered.** Either the first box has not sent it anywhere, or the answer it got was `X-Carlos-Handover: pending`, or the connection failed before it was established (TCP or TLS). A failure after the connection was made proves nothing. Then only a GET, HEAD or OPTIONS with no body, or an upgrade that has not switched protocols, may be held. That is A9's rule (§6.5). Everything else gets 502 as today. |
| -- **Replayable.** A request with no body qualifies. So does one whose whole body the first box has buffered, up to 1 MiB (Content-Length, or chunked ending within 1 MiB). Only HA forwards buffer: the edge tees the body into a bounded buffer as it sends it, so the first send streams as it does today. A body over the limit, or a client that has not finished sending, makes the request unholdable, and it gets today's answer. |
| -- **Methods.** With both conditions met, every method may be held, POST and PUT included. The pending answer is written before the request reaches A's activator, so the app never saw it. |
| -- **WebSockets and upgrades.** An upgrade that has not switched protocols may be held, since it has no body. An established WebSocket on A is cut at the step 3 bound, as today. The client's reconnect is held like any request. |
| -- **Edge-held SSE streams (#432).** A marked held-stream request is held like a GET, and it never wakes a host. On release it is forwarded to the successor from memory. If the successor is not running the host, the successor holds the stream as #432 already does for a sleeping host. A's own held streams close when A's edge exits, and the browsers reconnect. |
| -- **T6-b.** Today a marked request on a forwarding box with a cold holder cache gets `retry: 30000` (`forwardha.go:194-213`). During a handover, the released notice puts the successor in every peer's cache, so reconnecting streams find it from memory and do not get the retry. The general cold-cache case, a box that learned nothing, stays the open #432 follow-up. This section does not add a lock read on the held-stream path. |
| -- **Cross-region first boxes.** A box in another region that forwards to A (backhaul) is a first box too. It honours `X-Carlos-Handover: pending` and holds. It receives no notices (it is outside view(region)), and a foreign region's lock is never read (§6.1). So it releases once, 4 s after the first hold: one hop through `backhaulTo` to a box of the region in its view other than A. That box answers as §6.4 says: it serves, or acquires from the marker and wakes, or refuses. A refusal is relayed as today. A's release takes about 3 s (§9.9), so 4 s usually finds the marker. |
| +A new hop endpoint on every HA box, `/.carlos/ha-take`, signed with the mesh key like any hop. It carries the host and the generation of A's marker. The receiver: |
| +- refuses unless it serves H's pool, is admitted, is not draining, and the host's record is valid here; |
| +- runs the HA acquire for H. It never forwards. A marker of a different generation, or any state other than A's marker, gets the normal acquire decision; |
| +- wakes H, and answers when H is awake or the wake failed, within the wake wait bound; |
| +- answers with the owner it read or wrote in `lock.json`: itself, another box (held), or none. |
| -**The header.** `X-Carlos-Handover` is honoured only on an answer to a hop this box sent over its verified transport (`forwardTransport`, TLS to the box's name). It is stripped from app responses at the tenant boundary, as `X-Carlos-Holder` is (`stripAppHolder`, `forwardha.go:118-128`). |
| +A take is a forwarded wake without a visitor. It authorizes nothing the acquire table does not already allow. A box advertises the endpoint with an additive edges field, `ha_take: 1`. A drain refuses to start if no eligible serving box in the region advertises it. |
| -### 25.6 What clients see |
| +### 25.6 Phase 2: the provider action |
| -- **Within the bounds:** the request completes after a pause. The pause is the rest of A's in-flight drain for that host (up to 10 s, usually milliseconds), plus A's release (about 3 s, §9.9), plus the successor's acquire and restore. The target is under 5 s for a small host. Responses carry no new header. |
| -- **A bound hit, or a failed handover:** today's 503 with `Retry-After: 10`, and the C1 copy. |
| -- **An unplanned death:** today's behaviour, unchanged. |
| +**Replace and scale-in.** The roll tool gains `replace` and `remove` transactions. Today it only rolls in place and never terminates (docstring, lines 1-54). Both run under the existing one-at-a-time maintenance lock and journal, with no concurrent roll, replace or region apply (R1.10): |
| +1. Preflight: the shape checks of a roll (`region-group-roll.py:917-942`), and the floor of §25.7. |
| +2. `drain` on the member (§25.3). Stop on any `left` host unless `--accept-left`. |
| +3. Re-read every lock in the region's data bucket that names the member. Each must be a marker or another owner. This is the DR-4 check, now automatic. |
| +4. `replace`: `TerminateInstanceInAutoScalingGroup` without decrementing desired, so the group launches a replacement. `remove` (scale-in): lower minimum and desired by one under N-HA14's maintenance exception, then terminate with decrement. It refuses if the survivors would leave any pool the member served with no server, or fewer than 2 members. The operator then sets `box_count` in tofu to match, a plan with no instance change. |
| +5. Watch: the departure finishing (§7.5's bound), and for `replace` the new member admitted and healthy in both target groups. Report. |
| -### 25.7 Interactions |
| +Capacity is N - 1 between the drain and the replacement's admission, a few minutes. With N = 2 the survivor carries every host meanwhile; wakes are never refused for headroom (A20). Launching the replacement first needs a second N-HA14 exception (Q1). |
| -- **Takeover after proof and expiry** (§9.4, N-HA9) is untouched. Holds only wait for a marker. A host left without a marker is down until expiry plus proof, as today. |
| -- **F-A** is untouched. A lost-lease answer is never held, and a box that has lost a lease sends no released notice for it. |
| -- **DR-4's replacement rule** ("replace only lock-free members") becomes obsolete for a group whose members all run the release with §25, through every path in §25.8. Until then it stays in every card. The roll and replace tools check the floor per member (§25.8). |
| -- **The NLB deregistration delay** (30 s, `infra/modules/region-host/ingress/main.tf:105`) works in our favour. When the group terminates a member, it deregisters it from the target groups and waits for the delay before EC2 terminates it. The member then gets no new flows, still holds its leases and serves forwards normally. SIGTERM comes after. In Standby the member is already deregistered when the roll restarts its edge. |
| -- **Drain** (§13 step 2): handover mode is the same work as draining a box, but compressed into the shutdown deadline. The role `drain` is not needed for these paths. §13's retirement drain, which sleeps instances one at a time without a deadline, can reuse §25.3 steps 3 to 5 per host later. It is not part of this slice. |
| -- **Litestream #284 and restore time.** The pause includes a restore on the successor. A large database makes the pause longer than 15 s, and then the client gets today's 503. The witnesses measure restore time per host size (§25.10). |
| -- **Box-local caches.** The successor's edge asset cache is cold for the host. That is a speed cost, not an error. |
| +**Roll.** The existing Standby transaction runs `drain` before `apply`. `apply` restarts the edge with nothing awake. After the restart the member serves again. The markers it wrote name it, but the moved hosts are held elsewhere. Its next cold wakes come back through placement. |
| -### 25.8 The three paths, end to end |
| +**Resize.** The operator sets `instance_type` in the region's group module and applies, which makes a new launch template version. Then they re-pin `group_launch_template_version` to it, and run `replace` once per member, one at a time. The operator sees a tofu plan with a launch template change and no instance change, then one report per member: hosts moved, the longest hold, holds that ended with today's answer (target 0), hosts left, and departure time. |
| -**Planned terminate, scale-in and member replacement.** `scripts/region-group-roll.py` gains a `replace` transaction. Today the script only does in-place rolls and never terminates (docstring 1-54). The transaction: |
| -1. preflight as for a roll: min = desired = max = N ≥ 2, every member InService and healthy in both target groups, and every member's published version at or above the §25 floor (otherwise it refuses and names the member); |
| -2. `TerminateInstanceInAutoScalingGroup` on the member, without decrementing desired, so the group launches its replacement at once. For scale-in, the operator first lowers the module's `box_count` through tofu as today; the tool is not used; |
| -3. watch: the departing member's journal lines for handover mode (hosts handed over, held requests, holds that hit a bound, hosts without a marker), the departure finishing (§7.5's bound), the replacement admitted and healthy in both target groups, and every lock that named the departed ref now a marker or another owner; |
| -4. report, then allow the next member. |
| +**The NLB deregistration delay** (30 s, `infra/modules/region-host/ingress/main.tf:105`) no longer matters for correctness: by the time the group deregisters A, it holds no lease and only forwards. |
| -Capacity dips to N - 1 for the 2 to 3 minutes the replacement takes. With N = 2 the survivor carries every host for that time; wakes are never refused for headroom (A20). A surge replacement (launch first, then terminate) needs maximum raised by one, which N-HA14 does not allow. That is open question Q1 (§25.12). |
| +### 25.7 The floor (R1.11) |
| -**Roll.** The existing Standby transaction. Its `apply` step restarts the edge (`systemapply.go:697-703`), and that SIGTERM runs handover mode. The member is already deregistered, so its direct clients are only the keep-alive connections the delay did not end. The tool adds the floor check to its preflight and the handover lines to its verify step. |
| +A drain is only safe when every box that might send A a request understands the handover answer, and the successor understands a take. So: |
| +- `drain` refuses unless every box in the deployment's live edges files that serves or forwards the region publishes `ha_take: 1` (the same release adds both behaviours). Fixed forwarders such as adi count. |
| +- `replace` also checks the release the pinned launch template boots (`group_config.release`, `infra/modules/region-host/group.tf:50`) is at or above the floor, and after the launch that the replacement publishes `ha_take: 1`. |
| +- **First install.** Rolling a group onto the first release with §25 cannot use a drain. That roll uses today's DR-4 rule: replace only lock-free members. The tool says which rule it is applying. |
| +- A rollback below the floor brings DR-4 back. The tool checks the floor on every transaction, not once. |
| -**Resize.** The operator sets `instance_type` in the region's group module and applies, which creates a new launch template version. The re-pin then sets `group_launch_template_version` to it, as for a release re-pin. Then `replace` runs once per member, one at a time. Every replacement launches on the new type. The operator sees a tofu plan with one launch template version and no instance change, then one `replace` report per member. Each report gives hosts handed over, the longest hold, holds that hit a bound (target 0) and the departure time. |
| +**DR-4's lock-free rule** becomes obsolete for a region once its boxes are all on the floor and the dev witnesses (§25.10) have passed. Until then it stays in every card. |
| + |
| +### 25.8 What clients see |
| + |
| +- **Within the bounds:** the request completes after a pause. The pause is the rest of H's in-flight work (up to 10 s, usually milliseconds), plus the release (about 1 to 3 s), plus the successor's acquire and restore. The target is under 5 s for a small host. No new header reaches clients. |
| +- **A bound hit or a failed move:** today's 503 with `Retry-After: 10`. |
| +- **Uploads:** a request whose sent body outgrew 1 MiB before A's answer gets today's answer. Requests A received directly are never limited, because A streams their unread body to the successor. |
| +- **An unplanned death:** today's behaviour. |
| +- **SSE:** a gap of the browser's reconnect delay plus the move. |
| ### 25.9 Request budget (§18) |
| -- **Steady state:** unchanged. Nothing here polls. The `ha_notices` field rides the existing edges publication. |
| -- **Per planned departure:** one departing notice per peer. No S3. |
| -- **Per host handed over:** the marker PUT, as today; one released notice per peer, batched; and on the successor the acquire (1 GET + 1 PUT) and restore that the host's next wake would have cost anyway. A wake notice brings that wake forward. |
| -- **Fallback lock reads,** only when a released notice is lost: at most 3 GETs per host per first box per handover. |
| -- **Example:** a 4-member region replacing all members, 200 awake hosts in all: about 200 marker PUTs and 200 acquires (as any sleep and wake), 2,400 notices in about 50 POSTs, and no fallback reads if no notice is lost. That is about $0.002 in S3 requests. |
| +- **Steady state:** unchanged. Nothing polls. `ha_take` rides the existing edges publication. |
| +- **Per drain:** two edges mutations (drain state, then `drained`), each with its change-index update and the reader amplification of §18. The tool's lock check reads each lock naming the member: one LIST of the region's locks plus one GET per HA host, about $0.0002 for 500 hosts. |
| +- **Per host moved:** the marker PUT, as at any sleep; the successor's acquire (1 GET + 1 PUT) and restore, which its next wake would have cost anyway. No other reads. |
| +- **Example:** replacing all 4 members of a region with 200 awake hosts in all, and some hosts moved twice: about 300 markers and 300 acquires and restores, 8 edges mutations and 4 lock sweeps. That is about $0.01 in S3 requests, plus restore GETs. |
| ### 25.10 Tests and witnesses |
| -**Simulation (§21.1).** It adds planned terminations, notices that are lost, delayed and reordered, held requests with and without bodies, and connection failures before and after a request was sent. New invariants: |
| -- **I9, no unsafe replay.** No request reaches two apps' handlers. A request whose first send may have been delivered is never sent again unless it is a bodyless GET, HEAD, OPTIONS or upgrade. |
| +**Simulation (§21.1).** It adds drains, takes that win or lose the marker race, hops and direct requests during a move, body replay, connection failures before and after a send, lease loss during a move, and a stale cache after a move. New invariants: |
| +- **I9, no unsafe replay.** No request reaches two apps. A request is replayed only after an `X-Carlos-Handover` answer, or under §6.5. |
| - **I10, bounded hold.** Every held request is answered within its bound plus one forward. |
| -- I1 still holds with notices. Mutations that must fail the sweep: a notice authorizes an acquire; hold after a post-connect failure for a POST; hold on a lost-lease answer; a held request forwarded twice; the departing box closes its listeners before its releases. |
| +- I1 and I4 still hold. Mutations that must fail the sweep: a take or a handover answer authorizes an acquire; hold after a lost lease; replay after a connection failure for a POST; A forwards a hop; the drain moves a host before placement excludes A; terminate with a lease still naming the member. |
| -**Unit and integration tests.** The pending answer comes before the activator for every route kind; the header is stripped from app responses; the body tee and its bounds; the notice seal and its validation against view(region); the fallback read is coalesced; and `errShuttingDown` gets a real answer. |
| +**Integration** (in-process, against MinIO, two real activators and edges). Hold before app contact for every route kind; F-A wins at each phase; the take race against a third box; successor equals the first box; replay of a direct POST, a hop POST, chunked with trailers and `Expect: 100-continue`; no replay after a mid-body reset; SSE closed and resumed with `Last-Event-ID`; `errShuttingDown` gets a real answer; store-call counts per drain. |
| -**Root runner.** `TestRootHAHandoverHold` extends `TestRootHATerminateRelease`: two real agents, real `TerminateInstances`, and a client driving GET, POST with a body, and SSE through the survivor and through the departing box during the terminate. It asserts 0 non-2xx, every hold within its bound, and every acknowledged write present after. |
| +**Dev witnesses** on the dev IE group. Each runs a probe from outside the VPC at about 3 requests a second against two HA hosts awake on the member: GETs, POSTs with unique ids that write, and an SSE stream from an app that emits a numbered event every second. Pass for every witness: 0 non-2xx, 0 transport errors and 0 timeouts; every acknowledged POST id present after; no SSE event number missing after the reconnect; the longest pause under 5 s. |
| +- **W1 replace** a lease-holding member. |
| +- **W2 roll** a lease-holding member through the Standby transaction. |
| +- **W3 resize** both members, t4g.small to t4g.medium, with `replace`. |
| +- **W4 scale-in** from 3 to 2 with `remove`, and a refused `remove` that would leave a pool unserved. |
| +- **W0 control.** A hard kill still gives today's 503 with `Retry-After: 10` until expiry plus proof. |
| +- **Plus one cross-region request** through a fixed forwarder during W1. |
| -**Dev witnesses,** one per path, each with a probe at about 3 requests a second (GETs, POSTs that write and an SSE stream) on two HA hosts awake on the member being replaced: |
| -- **W1 planned terminate** of a lease-holding member: 0 non-2xx, the longest hold under 5 s, 0 acknowledged writes lost, an SSE gap under 5 s, and the departure finished within §7.5's bound. |
| -- **W2 roll** of a lease-holding member through the Standby transaction: the same. |
| -- **W3 resize** (t4g.small to t4g.medium) of both members with `replace`, one at a time: the same per member, and both members on the new type. |
| -- **W0 control:** a hard kill (`SIGKILL` of the agent, then terminate) still gives today's 503 with `Retry-After: 10` until expiry plus proof. |
| +W1 to W4 passing on a release at the floor is what retires the DR-4 rule (§25.7). |
| ### 25.11 What a self-hosted fleet needs (Tito) |
| -- **Version floor:** a release with T1/DR-4 (v0.34.27), DR-5 (v0.34.28) and §25, on every box of the region, the console included. The console must be current main; a forked console is not supported. |
| -- **Shape:** the nested layout; the region in group mode (`infra/modules/region-host`) with its NLB, launch hook, termination queue and rule; `CARLOS_REGION_HA` and `CARLOS_GROUP_ADMISSION` on the members; chrony. |
| +- **Version floor:** a release with §25 on every box of the region and on every box that forwards into it, the console included. The console must be current main. |
| +- **Shape:** the nested layout, the region in group mode (`infra/modules/region-host`) with its NLB, launch hook, terminations queue and rule, `CARLOS_REGION_HA` and `CARLOS_GROUP_ADMISSION` on the members, chrony, and SSM on members for the roll tool. |
| - **Pool:** a named HA pool in `CONSOLE_HA_CAPACITY_CONFIG` (`ha_pools`) and `CONSOLE_HA_REGIONS` on the console (§23). |
| -- **Which instances benefit:** only born-HA instances, created in that pool after switch-on (N-HA7). Instances that exist today need the operator migration slice, which is not built. Until then a resize is still the old way for them. |
| +- **Which instances benefit:** only born-HA instances in that pool (N-HA7). Existing instances need the operator migration slice, which is not built. Until then they resize the old way. |
| -### 25.12 Open questions for Paul |
| +### 25.12 Round 1 findings and where they went |
| -- **Q1. Surge replacement.** `replace` terminates first and lets the group launch the replacement, so capacity dips to N - 1 for 2 to 3 minutes. Launching first needs a second N-HA14 exception: raise maximum and desired by one for the transaction. Recommendation: terminate first now; surge later if a 2-box region's survivor runs short. |
| -- **Q2. Wake notices.** §25.3 step 5 re-wakes a host on its successor if it had a request in the last 60 s, so the next visitor does not pay the restore. It costs extra awake time for hosts that were about to sleep. Recommendation: keep it. |
| +| Finding | Disposition | |
| +|---|---| |
| +| R1.1 tenants stop before SIGTERM | Accepted: the handover runs before any systemd action (§25.2, §25.3). | |
| +| R1.2 pending answer bypasses F-A | Accepted: the hold is decided in the activator after the loss latch (§25.3, §25.4). | |
| +| R1.3 closed admission refuses forwarding | Accepted: a draining box keeps admission open and only stops its own acquires (§25.3 step 1). | |
| +| R1.4 fallback reads | Gone: no fallback reads; A is alive and answers (§25.4). | |
| +| R1.5 placement sends traffic back to A | Accepted: placement excludes A before any host moves, including releaser reuse (§25.3 step 1). | |
| +| R1.6 successor hint is not the holder | Accepted: the take answer names the confirmed holder from `lock.json`; successor equal to the first box serves locally (§25.4, §25.5). | |
| +| R1.7 cross-region timing | Accepted: cross-region first boxes act on A's answer, not on a timer (§25.4). | |
| +| R1.8 body ownership | Accepted: A never reads a held body; C keeps what it sent and replays it with the rest (§25.4). | |
| +| R1.9 SSE stays attached to A | Accepted: streams on H close once the take is confirmed; reconnects find the holder without a lock read (§25.4). | |
| +| R1.10 scale-in bypasses the tool | Accepted: `remove` (§25.6), witness W4. | |
| +| R1.11 floor blocks its own install | Accepted: first install uses DR-4; the floor covers forwarders and the pinned template (§25.7). | |
| +| R1.12 notice freshness | Gone: no notices. | |
| +| R1.13 deadline arithmetic | Gone: no shutdown deadline in the move; one hold bound (§25.4). | |
| +| R1.14 fallback cost | Gone; budget restated (§25.9). | |
| +| R1.15 witnesses | Accepted: integration against MinIO; dev witnesses with ids, transport errors and SSE event numbers, gating DR-4 (§25.10). | |
| + |
| +### 25.13 Open questions for Paul |
| + |
| +- **Q1. Surge replacement.** `replace` drains and terminates first, so capacity is N - 1 for a few minutes. Launching first needs a second N-HA14 exception (maximum and desired up by one for the transaction). Recommendation: drain first now. Revisit if a 2-box region's survivor runs short. |
| +- **Q2. Drain time.** One fold period before and after, plus about 5 s per host at 4 at a time: about 3 minutes for 40 awake hosts. A resize of a 4-member region is then about 30 minutes, mostly replacement boot. Recommendation: accept. |