carlos / platform Public

region-ha p1b-1: the acquire table takes a terminated owner's expired lock

DecideAcquire gains one row: an expired lock of another box's non-empty
instance id is taken when Proofs.OwnerTerminated confirms it (N-HA9: never
before expiry). Self grows Account and Region with Complete(). The spec
records N-HA8 and N-HA9.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 03accb0e881b6be970bce2caf716367f768c8256 parent 42f9f09
5 files changed, +139 −10
  • docs/superpowers/specs/2026-10-26-region-ha-without-claims-design.md +6 −2
  • internal/hafresh/acquire.go +35 −5
  • internal/hafresh/acquire_test.go +72 −1
  • internal/hafresh/owner.go +9 −2
  • internal/hafresh/owner_test.go +17 −0
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 b6cd4fc56..295884901 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
@@ -80,6 +80,8 @@ These stand from spec 1 and spec 2.
| N-HA5 | App files on the continuous (top-plan) class sync every 10 s. `instance.key`, `report.token` and `<host>.env` sync at about 1 s. |
| N-HA6 | Confirms A16 (2026-10-26): the cheaper replication classes stay available in HA regions and lose up to their interval on a failover, stated plainly. H3's ~1 s applies to the stream class. |
| N-HA7 | (2026-10-26, after P1a plan round 2) **P1a gives HA only to hosts born HA**: created after their region's HA activation, which the console stamps on the record (`ha: true`, from `CONSOLE_HA_REGIONS`). P1a migrates no existing host; existing hosts behave exactly as today. Migrating existing hosts is its own later slice: an operator-run, held, offline step (the host stopped and held on every box, verified, published, then released). |
+| N-HA8 | (2026-10-27) **P1b is cut into slices.** The first, P1b-1, is takeover on confirmed termination, request-driven: no termination queue consumer, no scan, no duties. A born-HA host on a dead box otherwise never comes back, which is worse than a legacy host. |
+| N-HA9 | (2026-10-27, after Astra rounds 1 and 2) **A survivor takes a terminated box's lock only once that lock has also expired.** A same-box re-take stays at expiry, as in P1a. An upload the dead process already sent can still land: `CompleteMultipartUpload` can run for minutes after S3 receives it, and replica and manifest keys carry no ownership generation. A time margin shorter than the lease is not defensible, and the 15-minute lease is the margin every legacy takeover and P1a's same-box recovery already rely on. Taking over sooner needs storage fencing, which is its own design. |
### 2.2 Author's calls
@@ -401,7 +403,7 @@ On an HA box, the lease package's acquire reads first: one GET of `lock.json` wi
| A release marker, clean or not | Conditional PUT, with `If-Match`. |
| Held and unexpired | A held answer naming the owner (§6.2). |
| Expired, owner is this box's instance id | Conditional PUT, only with the emptiness proof of §9.6. Otherwise refused. |
-| Expired or unexpired, owner confirmed terminated (§9.2) | Conditional PUT: the takeover. |
+| Expired, owner confirmed terminated (§9.2, N-HA9) | Conditional PUT: the takeover. A terminated owner's unexpired lock is held until it expires, because a write the dead process already sent can still land. |
| A lifted tombstone, left by a move before the region went HA | Conditional PUT; never keep the local copy. |
| Anything else | Refused: another box's expired lease, an old-format owner, or a live tombstone. |
@@ -427,7 +429,7 @@ Rev 3's automatic replacement (A22 of rev 3) is cut (astra-2.2): a peer's recent
After an agent crash, a box may re-take its own expired lease only once it has proven the old writer gone. Its agent stops the host's units, and proves the host's cgroups empty.
- **No proof, no recovery.** Layouts with no such proof are refused: nested and rootfs instances, `Backing=unit`, and boxes without cgroup delegation. `wakeHostProven` accepts all of those today (`internal/activator/opset.go:437-453`), and startup cleanup is best-effort (`internal/host/agent.go:592-598`).
- **What a refusal does.** It raises the suspect-holder alarm. The runbook step terminates the box, which then makes the takeover path apply.
-- **Same-box takeover before expiry** stays a later improvement.
+- **Same-box takeover before expiry** stays a later improvement. Storage fencing is its precondition: until stores refuse writes from an earlier owner, no box may take a lock early, even its own.
### 9.7 Clock skew
@@ -447,6 +449,7 @@ These are estimates; dev measures each one (§21).
| EC2 reports `terminated`; the event arrives | 30 s to 2 min | 30 s to 2 min after the group's terminate |
| First request: acquire, describe, takeover, restore | about a second, plus the restore | the same |
| **Total, request-driven instances** | **about 1 to 3 min** | **about 2 to 5 min** |
+| Until storage fencing (N-HA9) | takeover also waits for the lease to expire, so the totals above are up to about 15 minutes after the last renewal | the same |
| Always-on workers, ticks, due wakes | one sweep (30 s or less) after confirmation; backoffs cancelled | the same |
| Worst case, event lost | the next request's dial failure confirms at once; with no requests, view ageing (15 min) | the same |
@@ -672,6 +675,7 @@ S3 prices: LIST and PUT $0.005 per 1,000; GET $0.0004 per 1,000; DELETE free.
|---|---|
| P0 | The removals this series owns (§16.3); rides in P1a. |
| P1a | **First slice** (N-HA7). Born-HA hosts only: the record flag and `CONSOLE_HA_REGIONS`; owner format, HA acquire, release marker, keep rule, retry queue (§5); the first-wake leftover refusal and empty baseline; files and wake-state publication and the H6 trio (§8); key producers (§7.3); same-box proof (§9.6); the move and import refusals for HA hosts (§14). No migration. |
+| P1b-1 | **Slice of P1b** (N-HA8). Takeover on confirmed termination, request-driven: the acquire table takes a terminated owner's expired lock (§9.4, N-HA9). No termination queue consumer, no scan, no duties. |
| P1b | Forwarding (§6), duties and the scan (§7), takeover and same-box proof (§9), admission, latch and lifecycle hook (§10, §4.3), hairpin (§12.4), roles. |
| P2 | Console: role and readiness doors, capacity metric, create refusals (§11), the HA move and import refusals (§14), ingress list, the S2 §8 rewrites (§15). |
| P3 | Infrastructure and boot (§12): group mode, NLB security group, lifecycle hook, boot script, IAM, alarms. |
diff --git a/internal/hafresh/acquire.go b/internal/hafresh/acquire.go
index 2c59ab1cd..b3382f9e2 100644
--- a/internal/hafresh/acquire.go
+++ b/internal/hafresh/acquire.go
@@ -36,7 +36,13 @@ type Decision struct {
// Proofs are the facts DecideAcquire cannot read from the lock.
// SameBoxEmpty proves that nothing of the host still runs on this box
// (Task 12); nil, false or an error refuses the same-box takeover.
-type Proofs struct{ SameBoxEmpty func() (bool, error) }
+// OwnerTerminated asks the cloud whether the instance id is confirmed
+// terminated (a stopped or unknown instance is not); nil, false or an
+// error refuses the takeover of another box's expired lock.
+type Proofs struct {
+ SameBoxEmpty func() (bool, error)
+ OwnerTerminated func(iid string) (bool, error)
+}
// Refusal reasons, matched by callers and tests.
const (
@@ -46,6 +52,8 @@ const (
ReasonBadMarker = "malformed release marker"
ReasonSameBoxUnsure = "same box without emptiness proof"
ReasonOtherExpired = "another box's expired lease"
+ ReasonSameBox = "same box, proven empty"
+ ReasonTerminated = "holder terminated"
)
// DecideAcquire is the HA acquire's one table (I1), for HA hosts only:
@@ -56,8 +64,11 @@ const (
// - an unexpired HA owner: held, by that owner;
// - expired, owned by this box's non-empty instance id: take only with
// the same-box emptiness proof, else refuse;
-// - expired, any other box: refuse. Expiry alone never hands an HA lock
-// to another box.
+// - expired, owned by another box's non-empty instance id: take only
+// when the cloud confirms that instance terminated, else refuse.
+// Expiry alone never hands an HA lock to another box, and a
+// terminated owner's lock is not taken before it expires (N-HA9);
+// - expired, owned by a box with no instance id: refuse.
func DecideAcquire(v View, self Self, now time.Time, p Proofs) Decision {
if v.Absent {
return Decision{ActCreate, ReasonAbsent}
@@ -76,9 +87,12 @@ func DecideAcquire(v View, self Self, now time.Time, p Proofs) Decision {
if !now.After(v.Lock.ExpiresAt) {
return Decision{ActHeld, v.Lock.Owner}
}
- if self.IID == "" || o.IID != self.IID {
+ if o.IID == "" {
return Decision{ActRefuse, ReasonOtherExpired}
}
+ if o.IID != self.IID {
+ return decideOtherExpired(o, p)
+ }
if p.SameBoxEmpty == nil {
return Decision{ActRefuse, ReasonSameBoxUnsure}
}
@@ -89,5 +103,21 @@ func DecideAcquire(v View, self Self, now time.Time, p Proofs) Decision {
if !empty {
return Decision{ActRefuse, ReasonSameBoxUnsure}
}
- return Decision{ActTake, "same box, proven empty"}
+ return Decision{ActTake, ReasonSameBox}
+}
+
+// decideOtherExpired is the row for an expired lock of another box's
+// non-empty instance id.
+func decideOtherExpired(o Owner, p Proofs) Decision {
+ if p.OwnerTerminated == nil {
+ return Decision{ActRefuse, ReasonOtherExpired}
+ }
+ gone, err := p.OwnerTerminated(o.IID)
+ if err != nil {
+ return Decision{ActRefuse, ReasonOtherExpired + ": " + err.Error()}
+ }
+ if !gone {
+ return Decision{ActRefuse, ReasonOtherExpired}
+ }
+ return Decision{ActTake, ReasonTerminated}
}
diff --git a/internal/hafresh/acquire_test.go b/internal/hafresh/acquire_test.go
index dc9e8a076..8a5e51356 100644
--- a/internal/hafresh/acquire_test.go
+++ b/internal/hafresh/acquire_test.go
@@ -12,10 +12,13 @@ func TestDecideAcquireTable(t *testing.T) {
now := time.Unix(1_800_000_000, 0)
self := Self{Ref: "a", IID: "i-a"}
exp := now.Add(-time.Minute)
- other, mine := OwnerFor(Self{"b", "i-b"}, 9, true), OwnerFor(self, 9, true)
+ other, mine := OwnerFor(Self{Ref: "b", IID: "i-b"}, 9, true), OwnerFor(self, 9, true)
yes := Proofs{SameBoxEmpty: func() (bool, error) { return true, nil }}
no := Proofs{SameBoxEmpty: func() (bool, error) { return false, nil }}
broken := Proofs{SameBoxEmpty: func() (bool, error) { return true, errors.New("cgroup unreadable") }}
+ terminated := Proofs{OwnerTerminated: func(string) (bool, error) { return true, nil }}
+ terminatedErr := Proofs{OwnerTerminated: func(string) (bool, error) { return true, errors.New("ec2 unreachable") }}
+ noIIDOther := OwnerFor(Self{Ref: "c"}, 9, true)
for i, c := range []struct {
v View
p Proofs
@@ -38,6 +41,12 @@ func TestDecideAcquireTable(t *testing.T) {
{View{Lock: Lock{ExpiresAt: exp, Owner: OwnerFor(self, 9, false)}}, yes, ActRefuse},
{View{Lock: Lock{ExpiresAt: exp, Owner: "released/v1;ref=b"}}, yes, ActRefuse},
{View{Lock: Lock{ExpiresAt: exp, Owner: other}}, Proofs{}, ActRefuse},
+ // Another box's lock, EC2 confirming it terminated: taken only
+ // once expired; a failed confirmation or an iid-less owner refuses.
+ {View{Lock: Lock{ExpiresAt: exp, Owner: other}}, terminated, ActTake},
+ {View{Lock: Lock{ExpiresAt: now.Add(time.Minute), Owner: other}}, terminated, ActHeld},
+ {View{Lock: Lock{ExpiresAt: exp, Owner: other}}, terminatedErr, ActRefuse},
+ {View{Lock: Lock{ExpiresAt: exp, Owner: noIIDOther}}, terminated, ActRefuse},
} {
if got := DecideAcquire(c.v, self, now, c.p).Action; got != c.want {
t.Errorf("case %d: %v, want %v", i, got, c.want)
@@ -53,11 +62,16 @@ func TestDecideAcquireTable(t *testing.T) {
{View{Lock: Lock{ExpiresAt: exp, Owner: other}}, yes, ReasonOtherExpired},
{View{Lock: Lock{ExpiresAt: exp, Owner: mine}}, Proofs{}, ReasonSameBoxUnsure},
{View{Lock: Lock{ExpiresAt: now.Add(time.Minute), Owner: other}}, Proofs{}, other},
+ {View{Lock: Lock{ExpiresAt: exp, Owner: other}}, terminated, ReasonTerminated},
+ {View{Lock: Lock{ExpiresAt: exp, Owner: mine}}, yes, ReasonSameBox},
} {
if got := DecideAcquire(c.v, self, now, c.p).Reason; got != c.want {
t.Errorf("reason %q, want %q", got, c.want)
}
}
+ if got := DecideAcquire(View{Lock: Lock{ExpiresAt: exp, Owner: other}}, self, now, terminatedErr).Reason; !strings.HasPrefix(got, ReasonOtherExpired+": ") {
+ t.Fatalf("terminated-proof error reason %q", got)
+ }
// A box whose instance id is still unknown never proves itself the
// owner of an expired lock, even one with no iid either.
noIID := OwnerFor(Self{Ref: "a"}, 9, true)
@@ -66,6 +80,63 @@ func TestDecideAcquireTable(t *testing.T) {
}
}
+func TestDecideAcquireReasons(t *testing.T) {
+ now := time.Unix(1_800_000_000, 0)
+ self := Self{Ref: "a", IID: "i-a"}
+ v := View{Lock: Lock{ExpiresAt: now.Add(-time.Minute), Owner: OwnerFor(Self{Ref: "b", IID: "i-b"}, 9, true)}}
+ d := DecideAcquire(v, self, now, Proofs{OwnerTerminated: func(string) (bool, error) { return true, nil }})
+ if d.Action != ActTake || d.Reason != ReasonTerminated {
+ t.Fatalf("terminated take: %+v", d)
+ }
+ d = DecideAcquire(v, self, now, Proofs{OwnerTerminated: func(string) (bool, error) { return false, errors.New("throttled") }})
+ if d.Action != ActRefuse || d.Reason != ReasonOtherExpired+": throttled" {
+ t.Fatalf("proof error: %+v", d)
+ }
+}
+
+// Each proof is asked only about the lock it can speak to.
+func TestDecideAcquireProofScope(t *testing.T) {
+ now := time.Unix(1_800_000_000, 0)
+ self := Self{Ref: "a", IID: "i-a"}
+ exp, live := now.Add(-time.Minute), now.Add(time.Minute)
+ b, mine := OwnerFor(Self{Ref: "b", IID: "i-b"}, 9, true), OwnerFor(self, 9, true)
+ noIID := OwnerFor(Self{Ref: "c"}, 9, true)
+ for _, c := range []struct {
+ name string
+ lock Lock
+ wantTerm []string
+ wantSameBox int
+ }{
+ {"expired other", Lock{ExpiresAt: exp, Owner: b}, []string{"i-b"}, 0},
+ {"unexpired other", Lock{ExpiresAt: live, Owner: b}, nil, 0},
+ {"unexpired mine", Lock{ExpiresAt: live, Owner: mine}, nil, 0},
+ {"expired mine", Lock{ExpiresAt: exp, Owner: mine}, nil, 1},
+ {"expired iid-less owner", Lock{ExpiresAt: exp, Owner: noIID}, nil, 0},
+ } {
+ var term []string
+ same := 0
+ p := Proofs{
+ SameBoxEmpty: func() (bool, error) { same++; return true, nil },
+ OwnerTerminated: func(iid string) (bool, error) { term = append(term, iid); return true, nil },
+ }
+ DecideAcquire(View{Lock: c.lock}, self, now, p)
+ if strings.Join(term, ",") != strings.Join(c.wantTerm, ",") || same != c.wantSameBox {
+ t.Errorf("%s: OwnerTerminated calls %v, SameBoxEmpty calls %d; want %v, %d", c.name, term, same, c.wantTerm, c.wantSameBox)
+ }
+ }
+}
+
+// A stopped box is not a terminated one: EC2 answering false leaves the
+// lock to its owner.
+func TestDecideAcquireStoppedOwnerIsNotTaken(t *testing.T) {
+ now := time.Unix(1_800_000_000, 0)
+ v := View{Lock: Lock{ExpiresAt: now.Add(-time.Minute), Owner: OwnerFor(Self{Ref: "b", IID: "i-b"}, 9, true)}}
+ d := DecideAcquire(v, Self{Ref: "a", IID: "i-a"}, now, Proofs{OwnerTerminated: func(string) (bool, error) { return false, nil }})
+ if d.Action != ActRefuse || d.Reason != ReasonOtherExpired {
+ t.Fatalf("%+v", d)
+ }
+}
+
func TestMarkerBodyFormat(t *testing.T) {
at := time.Date(2026, 10, 26, 12, 0, 0, 0, time.UTC)
body, err := MarkerBody(7, Marker{V: 1, Box: "a", IID: "i-a", Lifetime: "L1", TXID: 12, FilesGen: "3:ab", At: at, Clean: true})
diff --git a/internal/hafresh/owner.go b/internal/hafresh/owner.go
index 516fe1ab1..22a7c657b 100644
--- a/internal/hafresh/owner.go
+++ b/internal/hafresh/owner.go
@@ -9,8 +9,15 @@ import (
)
// Self is this box's identity as a lease owner: its reference (the label
-// it publishes metrics under) and its cloud instance id, "" until known.
-type Self struct{ Ref, IID string }
+// it publishes metrics under), its cloud instance id ("" until known),
+// and the cloud account and region it runs in. OwnerFor writes only the
+// reference and the instance id.
+type Self struct{ Ref, IID, Account, Region string }
+
+// Complete reports whether all four fields are known.
+func (s Self) Complete() bool {
+ return s.Ref != "" && s.IID != "" && s.Account != "" && s.Region != ""
+}
// Owner is a parsed owner string.
type Owner struct {
diff --git a/internal/hafresh/owner_test.go b/internal/hafresh/owner_test.go
index 49750575b..c5fc0a50f 100644
--- a/internal/hafresh/owner_test.go
+++ b/internal/hafresh/owner_test.go
@@ -54,3 +54,20 @@ func TestParseOwnerIsStrict(t *testing.T) {
}
}
}
+
+func TestSelfComplete(t *testing.T) {
+ full := hafresh.Self{Ref: "a", IID: "i-a", Account: "123456789012", Region: "eu-west-2"}
+ if !full.Complete() {
+ t.Fatal("all four fields set is incomplete")
+ }
+ for name, s := range map[string]hafresh.Self{
+ "ref": {IID: "i-a", Account: "123456789012", Region: "eu-west-2"},
+ "iid": {Ref: "a", Account: "123456789012", Region: "eu-west-2"},
+ "account": {Ref: "a", IID: "i-a", Region: "eu-west-2"},
+ "region": {Ref: "a", IID: "i-a", Account: "123456789012"},
+ } {
+ if s.Complete() {
+ t.Errorf("without %s: complete", name)
+ }
+ }
+}