ha: bound departure refusal caches and unresolved paging
Cache internal call timeouts while excluding caller cancellation. Persist one receiver-wide identity page claim per hour and emit the stop notice only after Started.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/hadeparture/consumer.go+6 −2internal/hadeparture/consumer_test.go+19 −0internal/registry/ha_journal.go+13 −1internal/registry/ha_journal_test.go+32 −0internal/termfence/confirm.go+3 −3internal/termfence/confirm_test.go+11 −1internal/termfence/termination_proof.go+2 −2internal/termfence/termination_proof_test.go+21 −0
Intent
<task-notification> <task-id>b8005sfrl</task-id> <tool-use-id>toolu_01UkWngmwT1h9f5wjGbaavsX</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/22c9817b-87ae-432b-b6d2-3ebbc209af38/tasks/b8005sfrl.output</output-file> <status>completed</status> <summary>Background command "Wait for B3 fix round 1" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>adb6e5118b69bc932</task-id> <tool-use-id>toolu_015cGtWxiSrBpg4tsetxBZvb</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/22c9817b-87ae-432b-b6d2-3ebbc209af38/tasks/adb6e5118b69bc932.output</output-file> <status>completed</status> <summary>Agent "Opus review Task B3" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>I re-reviewed fix round 1 (`7a8fadf6..baea196b`): all ten items are addressed, and the fix diff adds no new Critical or Important issues. I read the diff and the new tests and ran no tests; the green results below are from the fix report. - **CI 1 (`AWS_REGION` in TestOfflineMoveE2E): ADDRESSED.** `childEnv` now strips only credential-related variables and passes `AWS_REGION`, `AWS_DEFAULT_REGION` and `AWS_CA_BUNDLE` through (`backend_exec.go`, diff lines 191-198). A new test covers it; the fix report says the TestOfflineMoveE2E group passes. - **CI 2 (`ha_route_update` trigger on a registry without the `ha` column): ADDRESSED.** The fix is in the test fixture, which now drops the trigger before dropping the column (`egress_source_test.go`, diff 686-688). A real registry with that trigger always has the column, so production code didn't need to change. - **CI 3 (unclassified `NewS3StoreIn`): ADDRESSED.** It is now classified as the regional data bucket for proofs (`openers_test.go`, diff 2507). - **Important 1 (takeover gated on inventory completeness): ADDRESSED.** The lifetime check now uses the same route and lifetime lookup the acquire uses (`hadepartures_wiring.go` diff 1452, `hawake.go` diff 226-245). Inventory snapshots are published only at the end of a pass. A new test runs the real check with an incomplete inventory: a wrong lifetime is refused and the matching lifetime takes over. - **Important 2 (inbox liveness): ADDRESSED.** - New events are stored as small rows keyed by account, region and instance id, with a retry time and at most one page per row per hour. This avoids the 1,024-row inbox, and old inbox rows are migrated in pages of 32. - One bad identity record now blocks only the instance ids it could match. - The worker that drains and replays starts as soon as the journal opens, so it no longer waits for the queue. - **Important 3 (one-shot startup tied to the queue): ADDRESSED.** The proof adapter, adoption identity and suspicion callback are installed before the queue starts (`hadepartures_wiring.go` diff 1438-1460). Stores open lazily, and startup failures retry with the 1/2/4/8/16/30 s backoff while Started stays open (`consumer.go` diff 461-509). The startup-failure notice (C5) is limited to once an hour. A bad queue URL returns a typed error and leaves the adapters in place. - **Minor 4 (proof read before Describe): ADDRESSED.** The suspicion and age paths ask EC2 first (`hadepartures.go` diff 1236 and 1252). - **Minor 5 (caller cancellation cached as a refusal): ADDRESSED, but the fix is too broad.** Cancellation and deadline errors are no longer cached in either proof cache or the confirmer. That also skips the code's own 5-second timeouts, so during an S3 outage proof reads can run about once every 5 seconds instead of the budgeted once every 10 seconds (new Minor A below). - **Minor 7 (queue-error notice for journal failures): ADDRESSED.** C6 now fires only for receive failures, at most once an hour; record and journal failures log rate-limited internal codes instead. - **Minor 10 (all `AWS_*` variables stripped): ADDRESSED** by the same change as CI 1. The fix diff adds 4 Minors: - **A.** The over-broad cache exclusion described under Minor 5. The fix is to skip caching only when the caller's own context has ended. - **B.** Pages are limited per row, not in total. A deployment with an unreadable identity record, or events from another placement, can produce one page per parked row per hour. A single hourly summary page would fix it. - **C.** The "recovery has stopped" notice (C10) can be logged even though recovery never started. - **D.** A note for B4/B5: the new lifetime lookup needs a local route row. The report is at `.superpowers/sdd/2026-11-03-region-ha-p1b3b-duties/task-B3-review-r2.md`.</result> <usage><subagent_tokens>281722</subagent_tokens><tool_uses>13</tool_uses><duration_ms>187634</duration_ms></usage> </task-notification>
diff --git a/internal/hadeparture/consumer.go b/internal/hadeparture/consumer.go| index 71aab3bc2..d31100482 100644 |
| --- a/internal/hadeparture/consumer.go |
| +++ b/internal/hadeparture/consumer.go |
| @@ -140,8 +140,12 @@ func (c *Consumer) Start(parent context.Context, makeQueue func(context.Context) |
| } |
| c.cancel() |
| workers.Wait() |
| - if c.OnStopped != nil { |
| - c.OnStopped() |
| + select { |
| + case <-c.started: |
| + if c.OnStopped != nil { |
| + c.OnStopped() |
| + } |
| + default: |
| } |
| }() |
| return c, nil |
diff --git a/internal/hadeparture/consumer_test.go b/internal/hadeparture/consumer_test.go| index 484d0fa00..2dfcda3e3 100644 |
| --- a/internal/hadeparture/consumer_test.go |
| +++ b/internal/hadeparture/consumer_test.go |
| @@ -322,3 +322,22 @@ func TestTerminationConsumerRecordErrorSignal(t *testing.T) { |
| t.Fatal(queueErrors.Load(), recordErrors.Load()) |
| } |
| } |
| + |
| +func TestDepartureNoStoppedBeforeStarted(t *testing.T) { |
| + c := testConsumer() |
| + var stopped atomic.Int32 |
| + c.OnStopped = func() { stopped.Add(1) } |
| + entered := make(chan struct{}) |
| + c.initialize = func(ctx context.Context) error { close(entered); <-ctx.Done(); return ctx.Err() } |
| + l, err := c.Start(t.Context(), func(context.Context) (Queue, error) { return nil, errors.New("offline") }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + waitClosed(t, entered) |
| + if err = l.Stop(context.Background()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if stopped.Load() != 0 { |
| + t.Fatal("stopped notice without Started") |
| + } |
| +} |
diff --git a/internal/registry/ha_journal.go b/internal/registry/ha_journal.go| index a246809b3..afaf24639 100644 |
| --- a/internal/registry/ha_journal.go |
| +++ b/internal/registry/ha_journal.go |
| @@ -71,6 +71,7 @@ func (r *Registry) migrateHAJournal() error { |
| CREATE TABLE IF NOT EXISTS ha_departures(id TEXT PRIMARY KEY,body BLOB NOT NULL,phase TEXT NOT NULL DEFAULT 'pending',next_attempt INTEGER NOT NULL,last_page INTEGER NOT NULL DEFAULT 0,last_error TEXT NOT NULL DEFAULT ''); |
| CREATE TABLE IF NOT EXISTS ha_box_identities(ref TEXT PRIMARY KEY,iid TEXT NOT NULL UNIQUE,body BLOB NOT NULL); |
| CREATE TABLE IF NOT EXISTS ha_confirmation_retries(ref TEXT PRIMARY KEY,next_attempt INTEGER NOT NULL,attempts INTEGER NOT NULL); |
| + CREATE TABLE IF NOT EXISTS ha_identity_pages(id INTEGER PRIMARY KEY CHECK(id=1),last_page INTEGER NOT NULL); |
| CREATE TABLE IF NOT EXISTS ha_identity_walk(id INTEGER PRIMARY KEY CHECK(id=1),body BLOB NOT NULL); |
| `) |
| return err |
| @@ -221,7 +222,18 @@ func (r *Registry) haJournal(ctx context.Context, q haJournalRequest) (out haJou |
| if e != nil { |
| return out, e |
| } |
| - out.Found = n == 1 |
| + out.Found = false |
| + if n == 1 { |
| + res, e = tx.ExecContext(ctx, `INSERT INTO ha_identity_pages(id,last_page) VALUES(1,?) ON CONFLICT(id) DO UPDATE SET last_page=excluded.last_page WHERE last_page<=?`, journalTime(q.At), journalTime(q.At.Add(-time.Hour))) |
| + if e != nil { |
| + return out, e |
| + } |
| + n, e = res.RowsAffected() |
| + if e != nil { |
| + return out, e |
| + } |
| + out.Found = n == 1 |
| + } |
| if _, e = tx.ExecContext(ctx, `UPDATE ha_pending_terminations SET next_attempt=?,last_error=? WHERE account=? AND region=? AND iid=?`, journalTime(q.At.Add(30*time.Second)), q.Code, h.Account, h.Region, h.IID); e != nil { |
| return out, e |
| } |
diff --git a/internal/registry/ha_journal_test.go b/internal/registry/ha_journal_test.go| index 92381cfab..45ee15e64 100644 |
| --- a/internal/registry/ha_journal_test.go |
| +++ b/internal/registry/ha_journal_test.go |
| @@ -228,3 +228,35 @@ func TestTerminationPendingDurability(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +func TestDeparturePendingPagesGloballyBounded(t *testing.T) { |
| + r, path := adoptRegistry(t) |
| + at := time.Now() |
| + pages := 0 |
| + for i := 0; i < 10; i++ { |
| + h := HAPendingTermination{Account: "111122223333", Region: "eu-west-1", IID: fmt.Sprintf("i-%017d", i), ReceivedAt: at} |
| + if err := r.ParkTermination(t.Context(), h, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + page, err := r.RetryPendingTermination(t.Context(), h, at, "identity_unbound") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if page { |
| + pages++ |
| + } |
| + } |
| + if pages != 1 { |
| + t.Fatalf("pages=%d, want 1", pages) |
| + } |
| + r.Close() |
| + r, err := Open(path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer r.Close() |
| + h := HAPendingTermination{Account: "111122223333", Region: "eu-west-1", IID: "i-00000000000000000", ReceivedAt: at} |
| + if page, err := r.RetryPendingTermination(t.Context(), h, at.Add(time.Minute), "identity_unbound"); err != nil || page { |
| + t.Fatal(page, err) |
| + } |
| +} |
diff --git a/internal/termfence/confirm.go b/internal/termfence/confirm.go| index cce1a02e9..ddeeaf2c7 100644 |
| --- a/internal/termfence/confirm.go |
| +++ b/internal/termfence/confirm.go |
| @@ -155,7 +155,7 @@ func (c *Confirmer) terminated(ctx context.Context, iid string) (bool, error) { |
| // Publish and release even if ask panics. |
| f.res = confirmed{err: errors.New("termfence: the describe did not finish")} |
| defer func() { |
| - c.publish(iid, f) |
| + c.publish(ctx, iid, f) |
| close(f.done) |
| }() |
| f.res = c.ask(ctx, self, iid) |
| @@ -165,7 +165,7 @@ func (c *Confirmer) terminated(ctx context.Context, iid string) (bool, error) { |
| // publish caches f's answer and ends the flight, in one step under the |
| // mutex. An unexpired positive answer is never replaced by a negative or an |
| // error. |
| -func (c *Confirmer) publish(iid string, f *flight) { |
| +func (c *Confirmer) publish(ctx context.Context, iid string, f *flight) { |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| delete(c.inflight, iid) |
| @@ -174,7 +174,7 @@ func (c *Confirmer) publish(iid string, f *flight) { |
| f.res = old |
| return |
| } |
| - if errors.Is(f.res.err, context.Canceled) || errors.Is(f.res.err, context.DeadlineExceeded) { |
| + if ctx.Err() != nil { |
| return |
| } |
| ttl := ConfirmNegativeTTL |
diff --git a/internal/termfence/confirm_test.go b/internal/termfence/confirm_test.go| index b97cdd501..217e83908 100644 |
| --- a/internal/termfence/confirm_test.go |
| +++ b/internal/termfence/confirm_test.go |
| @@ -391,7 +391,7 @@ func TestConfirmPositiveIsNeverOverwrittenByANegative(t *testing.T) { |
| r.c.mu.Lock() |
| r.c.inflight["i-b"] = late |
| r.c.mu.Unlock() |
| - r.c.publish("i-b", late) |
| + r.c.publish(context.Background(), "i-b", late) |
| if !late.res.yes { |
| t.Fatalf("late negative answered %+v", late.res) |
| } |
| @@ -461,3 +461,13 @@ func TestConfirmClientConstructionFailureIsRetried(t *testing.T) { |
| t.Fatalf("API called %d times", calls) |
| } |
| } |
| + |
| +func TestDepartureConfirmInternalTimeoutCached(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{err: context.DeadlineExceeded}) |
| + for range 20 { |
| + _, _ = r.c.Terminated(context.Background(), "i-b") |
| + } |
| + if len(r.f.ids) != 1 { |
| + t.Fatalf("describe calls = %d, want 1", len(r.f.ids)) |
| + } |
| +} |
diff --git a/internal/termfence/termination_proof.go b/internal/termfence/termination_proof.go| index 6024d76ff..7f5588a3b 100644 |
| --- a/internal/termfence/termination_proof.go |
| +++ b/internal/termfence/termination_proof.go |
| @@ -160,7 +160,7 @@ func (c *ProofCache) Load(ctx context.Context, api ProofStore, b ProofIdentity) |
| if c.entries == nil { |
| c.entries = map[string]proofResult{} |
| } |
| - if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { |
| + if ctx.Err() == nil { |
| c.entries[key] = r |
| } |
| f.result = r |
| @@ -296,7 +296,7 @@ func (c *Confirmer) PersistTermination(ctx context.Context, api ProofStore, b Pr |
| } |
| cache.entries[key] = r |
| delete(cache.writeErrors, key) |
| - } else if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) { |
| + } else if ctx.Err() == nil { |
| if cache.writeErrors == nil { |
| cache.writeErrors = map[string]proofResult{} |
| } |
diff --git a/internal/termfence/termination_proof_test.go b/internal/termfence/termination_proof_test.go| index 8e5729783..054d4f500 100644 |
| --- a/internal/termfence/termination_proof_test.go |
| +++ b/internal/termfence/termination_proof_test.go |
| @@ -349,3 +349,24 @@ func TestTerminationProofDescribeCallerCancellation(t *testing.T) { |
| t.Fatalf("Describe cancellation poisoned proof retry: %v", err) |
| } |
| } |
| + |
| +type departureTimeoutProof struct { |
| + ProofStore |
| + calls int |
| +} |
| + |
| +func (p *departureTimeoutProof) GetObject(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) (*s3.GetObjectOutput, error) { |
| + p.calls++ |
| + return nil, context.DeadlineExceeded |
| +} |
| +func TestDepartureProofInternalTimeoutCached(t *testing.T) { |
| + b := proofIdentity(t) |
| + api := &departureTimeoutProof{} |
| + cache := &ProofCache{} |
| + for range 20 { |
| + _, _, _ = cache.Load(context.Background(), api, b) |
| + } |
| + if api.calls != 1 { |
| + t.Fatalf("proof calls = %d, want 1", api.calls) |
| + } |
| +} |