carlos / platform Public

spec §25 r3: invariants H1-H5, narrowed guarantee (Astra R2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 61e0b7486646956476de3e7133eb8e80417dc7de parent b1eebdf
1 file changed, +174 −109
  • docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md +174 −109

Intent

Human

<task-notification> <task-id>bs4o30fpe</task-id> <tool-use-id>toolu_01LeLHcdCLzqrzaDKbtenGAC</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/283be278-5c6a-4a71-a8d6-b3d32c668985/tasks/bs4o30fpe.output</output-file> <status>completed</status> <summary>Background command "Wait for Astra round 2 to finish" completed (exit code 0)</summary> </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 8e6873eeb..faa442da6 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
@@ -1037,6 +1037,8 @@ be the protection against publishing disposable member addresses.
Line citations in this section are to `origin/main` at 9622c65a.
+**Revision note (r3 of this section).** Astra round 2 (`~/.cache/region-ha-held/ASTRA-R2.md`, 8 P1) found four families for the second time: scale-in capacity, the coverage of the hold decision, the forward-attempt budget and body ownership. Under stopping rule 2 each is now an invariant enforced in one place (§25.3), or a refusal made up front with a narrower stated guarantee (§25.8). §25.12 maps every finding of both rounds.
+
**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
@@ -1050,171 +1052,234 @@ T1/DR-4 (§9.9) writes a clean release marker about 3 s after `TerminateInstance
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 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.
+A planned replacement of member A is one operator transaction, in this order:
+1. **Out of the load balancer.** A enters Standby, so the NLB sends it no new client flows (§25.6).
+2. **Withdraw and quiesce.** A stops being a placement choice, every live box acknowledges that, and A settles every acquire already under way (§25.4 steps 1-2).
+3. **Move the leases.** A moves each awake host to a successor, and holds the requests that reach it for that host while it moves (§25.4 step 3, §25.5).
+4. **Drain the connections.** A finishes the requests it is still forwarding, and closes keep-alive connections and long streams in an orderly way (§25.4 step 4).
+5. **The provider action.** Restart the edge (roll), or terminate (replace, scale-in). A holds no lease and carries no traffic, so T1's shutdown has nothing to release.
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).
+- **I1.** Only a release marker lets another box acquire (§9.4). The successor acquires through `hafresh.DecideAcquire` (`internal/hafresh/acquire.go:77-115`) like any box. Nothing in this section is an input to it.
+- **Takeover after proof and expiry** stands for every lease without a marker.
+- **F-A.** A host whose lease is lost answers 503 `Retry-After: 10` with holder `none` at once, in every phase.
+- **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). This section adds no exception (§25.3 H3).
-### 25.3 Phase 1: draining member A
+### 25.3 Invariants
-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:
+Each is enforced in one place, checked by the simulation (§25.10) and guarded by a mutation that must fail the sweep. This is stopping rule 2's third option for the families round 2 drew a second time.
-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.
+| # | Invariant | Enforced only in |
+|---|---|---|
+| H1 | **One handover decision.** Every request whose route is a host that is moving on this box is classified by one memory-only activator call, `MoveState(route)`. It runs at the front door, right after route lookup, before `frontForwardHA`, admission, the gate, rate limits, recording and the held-stream branch (`internal/edge/edge.go:1953-2197`). It covers direct requests, hops and marked streams. It checks the lease-loss latch first, so F-A always wins. (R2.2, R2.3) | `MoveState`, called once from `ServeHTTP`. |
+| H2 | **Replay only from a whole copy.** A first box may send a request a second time only if, before its first send, it read the entire body into memory (at most 64 KiB) and marked the hop `X-Carlos-Replayable: 1`. Bodyless requests count as whole. Nothing else is ever sent twice, except under §6.5's existing rule. (R2.5) | The HA forward's body preparation, one function. |
+| H3 | **The attempt budget.** An `X-Carlos-Handover` answer is followed only when it answered the request's **first** attempt. On a second attempt it gets today's answer. So I4's limit of two attempts holds with no exception. (R2.4) | The forward and backhaul retry paths' shared attempt counter. |
+| H4 | **Desired capacity is lowered only by targeting A.** The only call that lowers the group's desired capacity is `EnterStandby` of A with decrement (`scripts/region-group-roll.py:760-765`). Minimum is lowered first, as today. Desired is raised again only by the transaction's final restore. (R2.1) | The roll tool's provider adapter. |
+| H5 | **Quiescence before the provider action.** The provider action runs only against a drain report from the agent's **current incarnation** that certifies: no acquire, wake or recovery in flight; no pending stop or marker obligation; no forwarded request in flight; and no lease naming A. The tool reads the report again immediately before the action. An agent restart voids the drain. (R2.7) | The drain barrier in the agent, and the tool's pre-action check. |
-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 Draining member A
-**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.
+The roll tool runs a new root verb, `carlos system group-maintenance drain`, over SSM, as it runs `apply`. The verb asks the agent to drain, waits, and prints the agent's report. A is already in Standby (§25.6). The agent's steps:
-**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).
+1. **Withdraw, acknowledged.** A publishes a box-level drain state in its edges file, and from then on places nothing on itself. The field withdraws placement only. Name authority, admission and holder reachability are unchanged, so A keeps answering as a holder and a forwarder (R2.9). Placement reads it as local-denied (`internal/edge/wakeplacement.go:36-42`), and that includes §6.3 rule 1, so a clean marker naming A no longer sends a wake to A. The builder fixes the field with a reader table under §24's rule.
-### 25.4 Holds on A
+ A timer does not prove that peers folded the change (R2.9). So A sends a signed `/.carlos/ha-drain` hop to every live box of the deployment that views its region, fixed forwarders and other regions included. Each receiver refreshes its fold of A's edges file at once, which is one normal fold read, and answers whether it now sees A draining. A asks again every 5 s. The drain goes on only once every such box has acknowledged. A box that cannot be reached, or does not acknowledge within 2 minutes, makes the drain fail (failures, below).
+2. **Quiesce (H5).** A closes its local start gate for tenant leases. It then settles everything that crossed the gate before it closed: wakes that are restoring, acquires waiting on their conditional PUT, and lost-lease recovery workers. Each either ends awake, and is then moved in step 3, or fails. Pending stops and marker obligations are kept and settled (§5.2). Nothing is cancelled in a way that would drop a writer proof.
+3. **Move each awake host H,** up to 4 at a time, busiest first:
+ 1. *Hold.* From now `MoveState(H)` is `holding` (§25.5).
+ 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, which ends 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). A sends B a signed take request (§25.7). B acquires and wakes H, then answers with the owner it now sees in `lock.json`. That owner is the **confirmed holder**: B, or a third box that won the marker first (§6.7).
+ 5. *Release the holds* (§25.5). `MoveState(H)` becomes `taken(holder, lock generation)` for 30 s, and then the entry is dropped. After that, a stale hop for H is answered as by any non-holder (§6.4).
+4. **Drain the connections.** When no lease names A, it publishes `drained` and asks for the same acknowledgement as in step 1. Peers drop holder-cache entries naming A (§6.6). A then sends `Connection: close` on every response, closes idle keep-alive connections, and waits up to 60 s for the requests it is still forwarding to finish. Edge-held SSE streams still open through A are closed at an event boundary, and the browser reconnects through the NLB to another box, with `Last-Event-ID`. WebSockets and responses still running at 60 s are closed, and the report counts them.
+5. **Report.** The report lists hosts moved, each one's confirmed holder, the longest hold, holds that got today's answer, unclean releases, connections closed at the bound, and the agent's incarnation. The agent keeps it until it exits, for the tool's pre-action check (H5).
-**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.
+**Failures.** A drain fails, and the tool refuses the provider action, when:
+- a host is left without a marker (S3 down, failed stop proof);
+- a take fails or names no holder;
+- a release was **unclean** (§5.2). Another box may hold the host, but writes or files were lost, so it is not a successful planned move (R2.11);
+- an acknowledgement is missing;
+- the drain deadline passes: 15 minutes plus 1 minute per 20 awake hosts.
-**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.
+On a failure every hold ends with today's answer. Release work already running continues under its own budgets and obligations, owned by the activator as today (R2.10). The drain state stays, so A places nothing, until the operator reruns `drain` or ends it with `drain --cancel`. No override lets the provider action run after a failed drain. An operator who chooses to go on anyway uses today's DR-4 procedure by hand, outside the tool.
-**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.
+### 25.5 Holds on A
-**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 `MoveState(H)` does** (H1), in order:
+1. A lost lease: F-A's answer.
+2. `holding` or `releasing`: the request is held, and A reads none of its body.
+3. `taken(holder, gen)`: the request is released at once, as below.
+4. Anything else: nothing changes, and the request goes on as today.
-**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`).
+A marked held-stream request is classified the same way. While H is held its marked requests are held too, and they never start a held task (R2.3).
+
+**Release.**
+- **A request A received from a client** goes to the confirmed holder through the ordinary front forward. That is A's first attempt, and the holder runs the tenant pipeline once: gate, accounting and recording (R2.2). The unread body streams as today. If the confirmed holder were A the request would get today's answer, but that cannot happen, because A acquires nothing while draining.
+- **A hop from a first box C** cannot be forwarded on (§6.4). If the hop is marked `X-Carlos-Replayable: 1` and it is C's first attempt (H2, H3), A answers 503 with `X-Carlos-Handover: holder=<ref>; gen=<n>`. The answer means "not delivered, send it there". A writes it with full duplex enabled (`http.NewResponseController(w).EnableFullDuplex()`, as `internal/edge/demo_api.go:132` does), so the answer never waits for a body to drain (R2.5). C validates the ref against its view and name authority, then sends its copy there as its second and last attempt. If the ref names C, C serves the request locally, which spends no attempt. C caches the holder.
+- **Any other hop for a moving host** (not replayable, or a second attempt) is held, then gets today's answer at release. This is the narrowed guarantee (§25.8).
**Bounds.** The constants are the builder's to tune; the shapes are not.
| Bound | Value | When exceeded |
|---|---|---|
-| Time held on A, a client's request | 30 s, the wake wait bound (`wakeWaitBound`, `activator.go:1208`) | today's answer |
-| Time held on A, a hop | 25 s, inside the hop's 30 s header timeout (`backhaulHeaderTimeout`, `internal/edge/edge.go:4180`) | today's answer |
+| Time held, a client's request | 30 s (`wakeWaitBound`, `internal/activator/activator.go:1208`) | today's answer |
+| Time held, a hop | 25 s, inside the hop's 30 s header timeout (`internal/edge/edge.go:4180`) | today's answer |
| Held requests per host | 64 | the new request gets today's answer |
| Held requests per box | 1,024 | the same |
-| Kept body bytes per forward on C | 1 MiB | not replayable: today's answer |
+| Whole-body copy on C | 64 KiB | not replayable |
-**Silence is not suspicion.** Today a connected holder that sends no headers within 3 s is made suspect (`holderHeaderSuspectAfter`, `forwardha.go:332`), and a suspect holder's next requests are refused (`forwardha.go:222`). A held hop is silent by design. So C never makes a box that publishes the drain state suspect for silence. A connection failure to it still counts, as today.
+**Silence is not suspicion.** Today a connected holder that sends no headers within 3 s is made suspect (`internal/edge/forwardha.go:332`), and a suspect holder's next requests are refused (`forwardha.go:222`). A held hop is silent by design, so no box makes a box in the drain state suspect for silence. A connection failure still counts, as today.
-**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.
+**The headers.** `X-Carlos-Handover` is honoured only on an answer to a hop sent over the verified forward or backhaul transport. It is stripped from app responses at the tenant boundary, beside `X-Carlos-Holder` (`stripAppHolder`, `forwardha.go:118-128`). Only the platform sets `X-Carlos-Replayable`, and it is removed from inbound client requests.
-**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.
+**Cross-region first boxes** follow the same rules (H2, H3). In the common case, a cross-region request's first attempt goes to a box of the region other than A, which names A as the holder, so its second attempt reaches A. That second attempt cannot be redirected, so it gets today's answer (§25.8).
-### 25.5 The take request
+### 25.6 The provider action and the three paths
-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 roll tool gains `replace` and `remove` transactions beside `roll`. All three run under the existing maintenance lock and journal, one member at a time, with no concurrent transaction or region apply. They share one shape:
-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.
+1. Preflight: the roll's shape checks (`scripts/region-group-roll.py:917-942`) and the floor (§25.9).
+2. Lower minimum by one, then `EnterStandby` of A with decrement, then verify A is deregistered from both target groups, as the roll does today (H4).
+3. `drain` (§25.4). A failed drain stops the transaction: the tool exits Standby and restores the shape, and A stays draining until `drain --cancel`.
+4. Re-read every lock in the region's data bucket that names A. Each must be a marker or have another owner. Read A's drain report again and check its incarnation (H5).
+5. The action:
+ - **roll:** `apply`, which restarts the edge with nothing awake, then ExitStandby and restore minimum, as today. The restarted agent is not draining. Its markers name it, but the hosts are held elsewhere;
+ - **replace:** terminate A in the group without decrement. Desired is already N - 1, so nothing launches yet. Then restore desired, then minimum, to N, and the group launches one replacement;
+ - **remove (scale-in):** terminate A without decrement, leave desired at N - 1, and set minimum to N - 1. Preflight refuses if the survivors would leave a pool A served with no server, or if fewer than 2 members would remain. The operator then sets `box_count` in tofu to match, which plans no instance change.
+6. Watch: the departure finishing within §7.5's bound and, for replace, the replacement admitted and healthy in both target groups. Report.
-### 25.6 Phase 2: the provider action
+Terminating an instance in Standby without decrement must leave desired unchanged. The builder confirms this against the AWS documentation, and tests it with a fake provider that terminates a different instance whenever any other call lowers desired (H4). One attended live call on dev, with the operator's real transport, comes before any window.
-**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.
+**Capacity** is N - 1 from Standby until the replacement is admitted, 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).
-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).
+**Resize.** The operator sets `instance_type` in the region's group module and applies, which makes a new launch template version. They re-pin `group_launch_template_version` to it, then run `replace` once per member. 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 got today's answer (target 0), connections closed at the bound, and the departure time.
-**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.7 The take request
-**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.
+The take is a new hop endpoint on every HA box, `/.carlos/ha-take`, signed with the mesh key like any hop. It carries the host, its lifetime and the generation of A's marker. The receiver:
+- refuses unless it serves H's pool, is admitted and not draining, and holds a valid record for the host with that lifetime;
+- runs the HA acquire for H, and never forwards. A lock in any state other than that marker gets the normal acquire decision;
+- wakes H, and answers once H is awake or the wake has failed, within the wake wait bound;
+- answers with the owner it read or wrote in `lock.json`: itself, another box (held), or none.
-**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.
+A take is a forwarded wake without a visitor, and it authorizes nothing the acquire table does not already allow. A box advertises it with an additive edges field, `ha_take: 1`.
-### 25.7 The floor (R1.11)
+### 25.8 What clients see
-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.
+- **Zero errors** for every request whose first attempt reaches A. That covers requests through the NLB before Standby, from a peer whose cache names A (the normal in-region case), or through a fixed forwarder in the region. The body must be empty or at most 64 KiB, unless the request reached A directly. 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.
+- **Today's 503 with `Retry-After: 10`,** only in the seconds that host is moving, for:
+ - a hop whose body is over 64 KiB;
+ - a request whose first attempt went to another box that named A, as most cross-region requests do (H3);
+ - any bound hit or failed move.
+- **Connections:** keep-alive connections are closed between requests. SSE streams reconnect with `Last-Event-ID` after the browser's retry delay. WebSockets still open through A 60 s after its leases have moved are closed, and their clients reconnect.
+- **An unplanned death:** today's behaviour, unchanged.
-**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.9 The floor
-### 25.8 What clients see
+A drain is only safe when every box that may send A a request understands the new headers, and successors understand a take. So:
+- `drain` refuses unless every live box of the deployment that views the region publishes `ha_take: 1`. The same release adds every behaviour in this section. Fixed forwarders such as adi count.
+- `replace` also checks that the release the pinned launch template boots (`group_config.release`, `infra/modules/region-host/group.tf:50`) is at or above the floor. After the launch, it checks that the replacement publishes `ha_take: 1`.
+- **First install.** Rolling a group onto the first release with §25 cannot drain. That roll uses today's DR-4 rule, replacing only lock-free members, and the tool says which rule it applied.
+- A rollback below the floor brings DR-4 back. The tool checks the floor on every transaction.
-- **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.
+**DR-4's lock-free rule** becomes obsolete for a region once all its boxes are at the floor and the dev witnesses (§25.10) have passed. Until then it stays in every card.
-### 25.9 Request budget (§18)
+### 25.10 Request budget, tests and witnesses
+**Request budget (§18).**
- **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 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.
+- **Per drain:**
+ - two edges mutations (the drain state, then `drained`), each with its change-index update and §18's reader amplification;
+ - two acknowledgement rounds, each costing one fold read per live box (one LIST plus one GET per edges file, as at any fold);
+ - the tool's lock check: one LIST of the region's locks plus one GET per HA host.
+- **Per host moved:** the marker PUT, as at any sleep, plus the successor's acquire (1 GET + 1 PUT) and restore, which the host's next wake would have cost anyway.
+- **Example:** replacing all 4 members of a region with 200 awake hosts, some moved twice, costs about 300 markers and acquires, 8 edges mutations, 8 acknowledgement rounds over 6 boxes, and 4 lock sweeps of 500 hosts. That is about $0.01 in S3 requests, plus the restore GETs.
+
+**Simulation (§21.1).** It adds:
+- Standby and drains;
+- acknowledgements that are slow or missing;
+- takes that win or lose the marker race;
+- direct requests, hops and marked streams during a move;
+- whole-copy replay, and connection failures before and after a send;
+- lease loss mid-move, wakes in flight when the drain starts, and stale caches.
+
+New invariants:
+- **I9, no unsafe replay.** No request reaches two apps' handlers.
- **I10, bounded hold.** Every held request is answered within its bound plus one forward.
-- 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.
-
-**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.
-
-**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.
+- **I11, lock-free action.** No provider action runs while a lease names A or an acquire on A is in flight.
+- I1 and I4 still hold.
+
+Each of these mutations must fail the sweep:
+- a take or a handover answer authorizes an acquire (I1);
+- `MoveState` is checked after the gate, or not checked for marked requests (H1);
+- a request is held after a lost lease (F-A);
+- a replay without a whole copy (H2);
+- a handover answer followed on a second attempt (H3);
+- desired lowered by any call but A's Standby (H4);
+- a provider action on a stale incarnation's report, or before quiescence (H5);
+- placement on A before every acknowledgement.
+
+**Integration tests** run in-process against MinIO, with two real activators and edges. They cover:
+- the front-door position of `MoveState` for every route kind, gated routes included, with once-only accounting;
+- F-A at every phase;
+- the take racing a third box, and a successor that is the first box;
+- replay of a hop POST up to 64 KiB, and refusal over it;
+- no replay after a reset;
+- full-duplex answers under a slow chunked upload;
+- an SSE stream closed and resumed with `Last-Event-ID`;
+- a wake still restoring when the drain starts, and an agent restart between the report and the action;
+- an unclean release failing the drain;
+- `errShuttingDown` getting a real answer;
+- store-call counts per drain.
+
+**Dev witnesses** run on the dev IE group. Each has a probe from outside the VPC sending about 3 requests a second to two HA hosts awake on the member: GETs, POSTs of up to 64 KiB with unique ids that write, and an SSE stream from an app that emits a numbered event every second. A witness passes with: 0 non-2xx, 0 transport errors and 0 timeouts on the probe's in-region path; every acknowledged POST id present afterwards; no SSE event number missing after the reconnect; and the longest pause under 5 s.
- **W1 replace** a lease-holding member.
-- **W2 roll** a lease-holding member through the Standby transaction.
+- **W2 roll** a lease-holding member.
- **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.
+- **W4 scale-in** from 3 to 2 with `remove`, plus a `remove` that is refused because it 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.
+- **W5 limits** (recorded, not gating): a cross-region request and a POST over 64 KiB during W1. They get today's answer within the move window.
-W1 to W4 passing on a release at the floor is what retires the DR-4 rule (§25.7).
+W1 to W4 passing on a release at the floor is what retires the DR-4 rule.
### 25.11 What a self-hosted fleet needs (Tito)
- **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 in that pool (N-HA7). Existing instances need the operator migration slice, which is not built. Until then they resize the old way.
+- **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 the 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 in that pool (N-HA7). Existing instances need the operator migration slice, which is not built. Until it is, they resize the old way.
-### 25.12 Round 1 findings and where they went
+### 25.12 Findings and where they went
| 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). |
+| R1.1 tenants stop before SIGTERM | The move runs before any systemd action (§25.2). |
+| R1.2, R2.2, R2.3: coverage of the decision, F-A, gate | Invariant H1: one front-door `MoveState` for every request kind, after the loss latch. |
+| R1.3 closed admission | A draining box keeps admission open. Placement is withdrawn, not admission (§25.4 step 1). |
+| R1.4, R1.12, R1.13, R1.14: notices, fallback reads, deadlines | Gone with the notices. A is alive and answers itself. |
+| R1.5, R2.9: placement and fold | Withdrawal is acknowledged before any move (§25.4 step 1). |
+| R1.6 successor hint | The take names the confirmed holder from `lock.json` (§25.7). |
+| R1.7, R2.4: attempt budget, cross-region | Invariant H3: a handover answer is followed only on a first attempt. The guarantee is narrowed (§25.8). |
+| R1.8, R2.5: body ownership | Invariant H2: replay only from a whole copy of at most 64 KiB, taken before the first send. Handover answers are full duplex. |
+| R1.9 SSE | Marked requests are held. Streams through A close at the connection drain (§25.4 step 4). |
+| R1.10, R2.1: scale-in, capacity | Invariant H4: desired is lowered only by A's Standby (§25.6). |
+| R1.11 floor | The floor covers forwarders and the pinned template. First install uses DR-4 (§25.9). |
+| R1.15 witnesses | Integration tests against MinIO. Dev witnesses track ids, transport errors and SSE numbers, and gate DR-4 (§25.10). |
+| R2.6 forwarding cut at the action | Standby comes first, and the connection drain runs before the action (§25.2, §25.4 step 4). |
+| R2.7 quiescence | Invariant H5 (§25.4 step 2). |
+| R2.8 remembered holder | Kept 30 s, keyed by lock generation, then the normal non-holder answer (§25.4 step 3.5). |
+| R2.10 deadline and `--accept-left` | No override. A failed drain stops the transaction, and release work keeps its owner (§25.4 failures). |
+| R2.11 unclean release | Fails the drain (§25.4 failures). |
### 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.
+- **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 for now.
+- **Q2. The narrowed guarantee (§25.8).** Zero errors covers in-region traffic and bodies up to 64 KiB. Cross-region requests that first reach another box, and hops with larger bodies, keep today's 503 for the seconds a host moves. Closing those gaps needs either a third, handover-only attempt (an exception to I4) or a bounded body spool on every forwarding box. Both drew P1s twice in review. Recommendation: accept the narrower guarantee now and measure W5.
+- **Q3. Drain time.** Two acknowledgement rounds, about 5 s per host at 4 at a time, and up to 60 s of connection drain make about 3 to 4 minutes for 40 awake hosts. Recommendation: accept.