| index 1806bdb0b..1a86b7ec9 100644 |
| --- a/docs/superpowers/specs/2026-11-12-region-ha-storage-generations-design.md |
| +++ b/docs/superpowers/specs/2026-11-12-region-ha-storage-generations-design.md |
| @@ -1,6 +1,6 @@ |
| # Region HA: per-holder storage generations |
| |
| -Design, 2026-11-12, **rev 4**, **approved 2026-11-15** (§10) (Astra rounds 1 to 3 answered; the review budget is spent, and round 3's findings are applied as cuts without a fourth round: any further review is Paul's call). Branch `design-ha-generations`. Nothing here is built. Paul's ruling (2026-11-12, HA session, #421): contain the unclean-release late write now (branch `ha-unclean-marker-waits`, region HA spec rev 6.7), spec storage generations now, **build after G6**. |
| +Design, 2026-11-12, **rev 4**, **approved 2026-11-15** (§10) (Astra rounds 1 to 3 answered; the review budget is spent, and round 3's findings are applied as cuts without a fourth round: any further review is Paul's call). Branch `design-ha-generations`. Built on branch `ha-generations` (plan `docs/superpowers/plans/2026-11-17-region-ha-storage-generations.md`); **rev 5** records what the build decided, and the sections below are corrected to match. Paul's ruling (2026-11-12, HA session, #421): contain the unclean-release late write now (branch `ha-unclean-marker-waits`, region HA spec rev 6.7), spec storage generations now, **build after G6**. |
| |
| Sources: the wake-race review (`~/.cache/ha-flakes/wake-race/FINDINGS.md`, Astra, 2026-11-11; its options 1 and 2 combined are this design), the containment review (`docs/superpowers/reviews/ASTRA-FINDINGS-ha-unclean-containment-R1.md`), region HA without claims (**RHA**, `2026-10-26-region-ha-without-claims-design.md`, §5, §9.4, N-HA9), and the dormant claim machinery whose shapes this reuses (`internal/genselect`, `internal/hostbox/restore.go`, `internal/moves/source.go`, `internal/lease/claimgeneration.go`). |
| |
| @@ -22,6 +22,18 @@ Sources: the wake-race review (`~/.cache/ha-flakes/wake-race/FINDINGS.md`, Astra |
| - **(gen-3.2)** cut: **moves of a generations host are refused in v1** (§6.3); the current-state move is a later slice; |
| - **(gen-3.3)** a first wake (proven-empty prefix) creates an empty generation and does not go through the start rule (§4 step 0). |
| |
| +**Rev 5 (as built, 2026-11-19)** records the decisions taken during the build (ledger `.superpowers/sdd/2026-11-17-region-ha-storage-generations/progress.md`), one line each, with who decided: |
| +- **Rulings A and B** (8a3d302 for Paul, 2026-11-17): A, the floor is an enforced `generations_floor` in the group roll that refuses outright, never DR-4 (§9); B, an unbound console door read of a host with head.json gets a 409 (§6.1). |
| +- **Slice 0 is the floor:** v0.39.0 (8a3d302, confirmed 2026-11-18) (§9). |
| +- **Point-in-time reads keep S3 LastModified as the CreatedAt rule**, so an earlier holder's late tail is not visible to a T inside its tenure; the §6.2 sentence that said otherwise is corrected (build ruling, reported to 8a3d302). |
| +- **The take bound survives refusals and crashes:** a release marker written before a switch carries `min_txid`, and a held generations lock carries `carlos_min_txid` until its switch (build rulings, Tasks 5 and 11b) (§4, §9). |
| +- **Retirement measures every age in store time, never the box clock** (8a3d302 for Paul, option 1 of the clock family): head.json times come from the store Date of the holder's latest lock write plus monotonic elapsed time; retirement's "now" is the switch PUT's own Date; no Date means no retirement. It replaced a clock-skew guard after the stopping rule (§8). |
| +- **Retirement waits the 7-day window plus a 1-hour grace** before a history entry leaves (build ruling, Task 9) (§8). |
| +- **`carlos ops ha-generations list|verify|point` is the operator recovery** for a refused wake (8a3d302 for Paul, Task 14). Point is a dry run unless `--yes`, targets only head.json's generation or one in its history, and sets the take bound exactly to the chosen TXID (§4). |
| +- **The keep rule applies only when head.json names the marker holder's generation** (build ruling, Task 14 R1) (§4 step 5). |
| +- **Moves of a generations host stay refused**; the follow-up is discussion #1253 (§6.3). |
| +- **The switch value `fast` gates §5.3**; `on` takes generations with today's acquire table (build plan, Task 10) (§5.3). |
| + |
| ## 1. The problem, and what this retires |
| |
| Every holder of an HA instance's lease writes its Litestream replica into one shared prefix, `<loc>/` (`instances/<host>/`), with no ownership condition on the write (`litestream@v0.5.14/s3/replica_client.go:702`). Litestream's close cancels requests without proof that S3 did not receive them. So a request an earlier holder sent can be applied after a successor has restored and started its own history under the same keys. The late object replaces the successor's, and history diverges silently (wake-race P1 B; witness `internal/lease/unclean_marker_test.go`, adapted from 8e8a00a0). |
| @@ -86,9 +98,9 @@ The HA acquire is unchanged up to its conditional take of `lock.json` (RHA §9.4 |
| 3. **List G** with versions (`ListObjectVersions` under G's prefix). A paged listing that fails fails the wake. |
| 4. **Plan at the L0 position, then copy.** Let `T` be the highest MaxTXID among G's current L0 files: the position Litestream reads on startup (`db.go:1414`, `replica.go:210`). Compute a restore plan over G's current versions using only files whose MaxTXID is at most `T`, ending exactly at `T`, and include the L0 file ending at `T`. Then `CopyObject` each planned file, by its exact version id, to the same level and name under `g-N/`. Copies run in parallel and are server-side. |
| - Files above `T` (a snapshot or compaction that ran ahead of the uploaded L0, as an unclean release can leave) are not copied. Their transactions are lost, the same loss class as an unclean release today (RHA §5.2). |
| - - **Refusals, never a guess:** G holds no L0 file while it is not an empty first generation (step 0); no plan over files at most `T` reaches `T`; a clean marker of G records a TXID above `T` (a file it covers is missing). Each refuses the wake with its reason and raises the suspect-holder page (RHA §20); an operator restores by hand. An empty plan on a host with data is also a refusal, never a fresh boot. |
| + - **Refusals, never a guess:** G holds no L0 file while it is not an empty first generation (step 0) (`no_l0`); no plan over files at most `T` reaches `T` (`no_plan_to_l0`); the take bound is above `T` (`clean_marker_above_l0`: a file it covers is missing). The take bound is the highest of a clean marker's TXID, a marker's `min_txid` and the held lock's `carlos_min_txid` (§9), so a take that was refused or crashed before its switch passes its bound to the next one. An empty plan on a host with data is also a refusal, never a fresh boot (`empty_plan_with_data`); so is a planned file over the single-part copy limit (`file_over_5gib`), and a host with no head.json and no root LTX file but `g-*` prefixes under `<loc>/` (`head_missing`). Each refuses the wake with its reason (`ha_generation_refused reason=…`) and raises a page that repeats hourly while wakes are refused. An operator restores the host with `carlos ops ha-generations` (rev 5): `list` and `verify` read; `point --gen g-<M> --accept-loss-after <txid>` is a dry run unless `--yes`, accepts only head.json's generation or one in its `history`, and rewrites head.json to name it and the lock's take bound to exactly `<txid>`, that generation's newest L0 TXID, both by `If-Match`. A `head_missing` host has no such recovery yet (point needs head.json). |
| - Whatever G sent that is not visible now never enters `g-N` (G3). |
| -5. **Switch.** Restore locally from `g-N` (the keep rule still applies: a clean marker naming this box, with `T` equal to its TXID and an unchanged local record, keeps the local copy and skips the download). Then write head.json for `g-N` (`predecessor` G, `history` = G prepended, trimmed by §8), `If-Match: E`, with the lease healthy (`Held.Err()` nil). |
| +5. **Switch.** Restore locally from `g-N` (the keep rule still applies: a clean marker naming this box, with `T` equal to its TXID and an unchanged local record, keeps the local copy and skips the download, and only when head.json names the marker holder's generation, so a host an operator pointed elsewhere always restores). Then write head.json for `g-N` (`predecessor` G, `history` = G prepended, trimmed by §8), `If-Match: E`, with the lease healthy (`Held.Err()` nil). |
| - **412:** head changed since step 1. Only a holder of an older lock generation can have written it, so its own switch was in flight when N took over, and it never served. N abandons: it deletes nothing (its `g-N` objects are unreferenced and §8 removes them), releases through a settled marker (it never replicated) and the wake retries once from the acquire. |
| - Any other failure: a failed wake, released settled. |
| 6. **Replicate** into the current generation (§5.1) and serve. |
| @@ -113,13 +125,13 @@ Releases are unchanged (RHA §5.2, rev 6.7's markers). Whatever a holder sent la |
| |
| ### 5.3 The acquire table |
| |
| -With every take in its own generation, `hafresh.DecideAcquire` drops the rev 6.7 row (an unclean marker is taken at once) and N-HA9's expiry clause (a confirmed-terminated owner is taken at once). Same-box recovery still needs the emptiness proof, and no longer waits for expiry. These table changes ship only with the gate (§9), in slice 4. |
| +With every take in its own generation, `hafresh.DecideAcquire` drops the rev 6.7 row (an unclean marker is taken at once) and N-HA9's expiry clause (a confirmed-terminated owner is taken at once). Same-box recovery still needs the emptiness proof, and no longer waits for expiry. These table changes ship only with the gate (§9), in slice 4, and apply only where the switch is `fast` (rev 5); `on` takes generations with today's table. |
| |
| ## 6. Readers |
| |
| ### 6.1 One resolver |
| |
| -`lease.ResolveReplica(ctx, store, loc) (Replica, error)` reads head.json once (absent: `@root`) and returns a read-only Litestream client **bound to that generation's name**, confined to its prefix. One restore uses one resolution for its listings, downloads and retries. A reader whose flow spans requests (the console DB door lists, then downloads, `apidb.go:307/354`) carries the generation name in its handle and refuses, then restarts the read, if a later step would need a different one. Generations are never rewritten by a later holder, so a bound read is consistent even after head moves on. Every reader below switches to it (G4). Inventory (2026-11-12, at 08aed1c1): |
| +`lease.ResolveReplica(ctx, store, loc) (Replica, error)` reads head.json once (absent: `@root`) and returns a read-only Litestream client **bound to that generation's name**, confined to its prefix. One restore uses one resolution for its listings, downloads and retries. A reader whose flow spans requests (the console DB door lists, then downloads, `apidb.go:307/354`) carries the generation name in its handle and refuses, then restarts the read, if a later step would need a different one. A door read that names no generation, of a host with head.json, gets a 409 carrying the generation to bind (ruling B, rev 5): a current CLI binds it and reads again, an older CLI prints the message. Generations are never rewritten by a later holder, so a bound read is consistent even after head moves on. Every reader below switches to it (G4). Inventory (2026-11-12, at 08aed1c1): |
| |
| | Reader | Today | With generations | |
| |---|---|---| |
| @@ -137,11 +149,11 @@ With every take in its own generation, `hafresh.DecideAcquire` drops the rev 6.7 |
| |
| ### 6.2 Point-in-time reads |
| |
| -`--at T` (#262): if T is at or after the current generation's `created_by.at`, read the current generation. Otherwise walk `history` to the entry whose `created_at` ≤ T < `ended_at` and read that generation alone, with today's CreatedAt rule. Each generation is self-contained (G5), so this needs no other generation. Files a generation's holder wrote after its successor's copy (a late write, or writes lost in an unclean release) are visible to a T inside that holder's tenure, because that is what that holder had at T. Documented as such. A T older than every `history` entry still inside the restore window is refused, as a T before the oldest retained file is today. |
| +`--at T` (#262): if T is at or after the current generation's `created_by.at`, read the current generation. Otherwise walk `history` to the entry whose `created_at` ≤ T < `ended_at` and read that generation alone, with today's CreatedAt rule. Each generation is self-contained (G5), so this needs no other generation. That rule dates a file by its S3 LastModified, and a file a generation's holder wrote after its successor's copy (a late write, or writes lost in an unclean release) is modified after the switch, so a T inside that holder's tenure does not see it (rev 5). Today's root layout excludes a lagging upload the same way, and reading each file's own metadata instead would cost a header GET per file per read. The cost: a point-in-time read in the seconds before an unclean handover misses the old holder's unconfirmed tail, as today. head.json's times are store time (§8). A T older than every `history` entry still inside the restore window is refused, as a T before the oldest retained file is today. |
| |
| ### 6.3 Moves |
| |
| -**v1 refuses every move of a generations host** (legacy, offline, staged promotion), with a reason naming this design, before anything is copied (round 3, gen-3.2). Moves need the instance's whole current state, the files manifest and its objects and root-level build state as well as the database, installed authoritatively on the landing side; that is a later slice (§11). Today's moves are unchanged for hosts without head.json, and the claim moves (`moves/source.go:30`) are unaffected. |
| +**v1 refuses every move of a generations host** (legacy, offline, staged promotion), with a reason naming this design, before anything is copied (round 3, gen-3.2). Moves need the instance's whole current state, the files manifest and its objects and root-level build state as well as the database, installed authoritatively on the landing side; that is a later slice (§11), discussion #1253. Today's moves are unchanged for hosts without head.json, and the claim moves (`moves/source.go:30`) are unaffected. |
| |
| ## 7. Files, wake state and other colocated objects |
| |
| @@ -152,19 +164,20 @@ Files objects are immutable and the manifest moves by compare-and-swap (RHA §8. |
| The data bucket is versioned; noncurrent versions expire after `data_noncurrent_days` (7, `infra/modules/deployment-buckets`). Nothing here depends on a noncurrent version surviving: each take's copy is made at the take, and every later read uses current versions of a generation's own files (G5). |
| |
| - **Inside a generation:** Litestream retention as today (snapshot retention, L0 retention), run by the §5.2 scheduler. |
| -- **History:** a `history` entry stays while its `ended_at` is inside the restore window (7 days, #263). At each switch (§4 step 5) the new head drops the expired entries. |
| -- **Retirement:** after its switch, the new holder deletes the current objects of generations that left `history`, and of abandoned `g-*` prefixes no head names (a §4 step 5 412), older than one day. Their versions then expire by lifecycle. The park sweep already covers the whole prefix. |
| -- **Storage:** every take copies one restore plan (about one database's size, compressed). A host's extra storage is about its database size times its takes inside the 7-day window. A 100 MB host that wakes 50 times a day keeps about 35 GB more (about $0.80 a month at S3 Standard); most hosts wake a few times a day. §10 Q3 and §11 name the later reduction. |
| +- **History:** a `history` entry stays while its `ended_at` is inside the restore window (7 days, #263) plus a one-hour grace. At each switch (§4 step 5) the new head drops the entries past that. The hour covers a point-in-time read that resolved an entry just before the switch that drops it. |
| +- **Retirement:** after its switch, in the background once it replicates, the new holder deletes the current objects of the generations its switch dropped from `history`, of the root layout's LTX files once `@root` has left `history`, and of abandoned `g-*` prefixes no head names (a §4 step 5 412, or a pass an earlier holder did not finish) whose every current object is over a day old. It never touches a generation at or above its own lock generation, head's generation, its predecessor or any `history` entry. Deletes are by bare key, so their versions then expire by lifecycle. It checks the lease before each generation; a failure is logged (`ha_generation_retire_failed`), never fails the wake, and the next take retries. The park sweep already covers the whole prefix. |
| +- **Store time only (rev 5):** every age retirement compares is the storage service's time, never the box's clock. head.json's `created_by.at`, `created_at` and `ended_at`, and the trim, come from the holder's store clock: the Date header of its latest lock write (the acquire's, then each renewal's) plus the monotonic time elapsed since. Retirement's "now" is the Date of the switch's own head.json PUT, and the abandoned cutoff compares objects' LastModified with it. A switch whose PUT carried no Date does no retirement (`reason=no_store_time`); a take with no store time at all fails its switch and writes nothing. This replaced a clock-skew guard (8a3d302 for Paul). |
| +- **Storage:** every take copies one restore plan (about one database's size, compressed). A host's extra storage is about its database size times its takes inside the 7-day window (plus the grace hour, and abandoned prefixes for a day). A 100 MB host that wakes 50 times a day keeps about 35 GB more (about $0.80 a month at S3 Standard); most hosts wake a few times a day. §10 Q3 and §11 name the later reduction. |
| |
| ## 9. Rollout and the old-binary fence |
| |
| A binary that predates this design reads and writes the root. Once a host has head.json, such a binary would restore stale root data and append to it. Three layers: |
| |
| -- **Gate.** A pool turns generations on only when every box serving it publishes capability `replica-generations-1` (as `offline-move-1`). With the gate on, nothing places on a box without it. The release that adds the capability is the **floor**: no HA box goes below it. |
| +- **Gate.** Every box at this design's release publishes capability `replica-generations-1` (as `offline-move-1`), whatever its switch says. A group turns generations on by the box switch `CARLOS_HA_GENERATIONS` in its host.env (`on`, then `fast`, §5.3), only after its `generations_floor` is set. The **floor** is the slice-0 release, **v0.39.0**: the first release that refuses a generations lock (rev 5). The group roll enforces it outright (ruling A): a roll or replacement to a release below it, any present edges file whose capabilities lack `replica-generations-1`, or a `generations_floor` without the `ha_floor`, `data_bucket` and `data_prefix` it is read with, refuses with `floor_generations`, never DR-4. The floor is never lowered or removed while a host in the group has head.json. |
| - **Old binaries still see HA, and refuse.** Today's `HAOn` is true for a lock with `carlos_release`, an owner starting `released/v1;`, or a parsable `;ha=1` owner (`hafresh/lock.go:94`); an HA lock is never taken by the legacy acquire (`legacytake.go:61`). Today's `DecideAcquire` refuses (ReasonBadMarker) an owner starting `released/v1;` with no `carlos_release` record (`acquire.go:89-93`). So a generations host's locks use: |
| - - release markers: owner `released/v1;ref=<box>;iid=<iid>;gen=1`, record field `carlos_release_g` (not `carlos_release`). Old HA acquire: bad marker, refused. Old legacy acquire: HA, refused. |
| - - held locks: owner `box/v1;ref=…;iid=…;pid=…;ha=1`, unchanged, plus record field `carlos_generation` written into the lock body by the take. An old box's HA acquire would treat it as an ordinary HA lock: it waits for expiry and termination proof, then takes, and would then restore the root. That case is closed by the gate and the floor only, and by slice 0 below. |
| -- **Slice 0, ahead of everything (rides an early release):** `DecideAcquire` and the legacy take refuse any lock carrying a field they do not know from a fixed list (`carlos_generation`, `carlos_release_g`, and any future `carlos_*`). Every binary at or above the floor then refuses a generations lock outright, held or released. Verify against the oldest binary on any HA box when the gate turns on. |
| + - release markers: owner `released/v1;ref=<box>;iid=<iid>;gen=1`, record field `carlos_release_g` (not `carlos_release`). A marker released before its holder's switch carries `min_txid`, the take bound that holder was given, so a refused or failed take passes its bound on (rev 5). Old HA acquire: bad marker, refused. Old legacy acquire: HA, refused. |
| + - held locks: owner `box/v1;ref=…;iid=…;pid=…;ha=1`, unchanged, plus record fields written into the lock body by the take: `carlos_generation` (the holder's generation), and `carlos_min_txid`, the take bound (the higher of the marker's bound and the held lock's own). Every CAS round of the take writes `carlos_min_txid` and renewals keep it until the switch, which drops it, so a crash between the take and the switch keeps the bound (rev 5, Task 11b). A present `carlos_min_txid` that is not a non-negative integer makes the lock malformed, never unbounded. An old box's HA acquire would treat such a lock as an ordinary HA lock: it waits for expiry and termination proof, then takes, and would then restore the root. That case is closed by the gate and the floor only, and by slice 0 below. |
| +- **Slice 0, ahead of everything (shipped in v0.39.0, the floor):** `DecideAcquire` and the legacy take refuse any lock carrying a field they do not know from a fixed list (`carlos_generation`, `carlos_release_g`, `carlos_min_txid` and any future `carlos_*`). Every binary at or above the floor then refuses a generations lock outright, held or released. Verify against the oldest binary on any HA box when the gate turns on. |
| - **Migration:** no copy job. A host's first take on the new code starts `g-N` from `@root` (§4) and writes head.json. The root files are then retired with §8 (as a `history` entry named `@root`). |
| |
| ## 10. Questions for Paul |