carlos / platform Public

activator: deterministic backoff test (seam for the start-loop wait)

TestStartAdmittedLoopStopsAfterFiveStartsWithDoublingBackoff measured
real time with +200 ms per step and failed on a loaded CI worker
(discussion 662). The start loop's backoff wait now goes through a
seam; production durations, cancellation, timer cleanup and ordering
are unchanged, and the test records the exact 1/2/4/8 x base waits.
Census approved by Astra on #280.

Squashed from fix-activator-backoff-flake into main.
claude agent, operated by paul@keymail.dev 3d0729b9129be2387347a0adb805c0aed19f3bf1 parent adef0d6
6 files changed, +116 −23
  • docs/superpowers/notes/census-proposal-fix-activator-backoff-flake.md +19 −0
  • docs/superpowers/reviews/CODEX-REVIEW-BRIEF-fix-activator-backoff-flake.md +26 −0
  • internal/activator/claimloop_linux_test.go +54 −20
  • internal/activator/claimwake.go +11 −3
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −0
  • internal/provision/testdata/plan14-root-reads-fix-activator-backoff-flake.tsv +5 −0

CI passed — run details

diff --git a/docs/superpowers/notes/census-proposal-fix-activator-backoff-flake.md b/docs/superpowers/notes/census-proposal-fix-activator-backoff-flake.md
new file mode 100644
index 000000000..f2929b625
--- /dev/null
+++ b/docs/superpowers/notes/census-proposal-fix-activator-backoff-flake.md
@@ -0,0 +1,19 @@
+# Census approval: fix-activator-backoff-flake
+
+Ruling `census-ruling-fix-activator-backoff-flake`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26).
+
+| unit | change | read sites | what changed |
+|---|---|---|---|
+| `internal/activator/claimwake.go` `Service.startAdmitted` | body | unchanged (10) | value-identical: the backoff wait between starts gets its timer from package var claimBackoffTimer (production: time.NewTimer(d), returning timer.C and timer.Stop) instead of calling time.NewTimer inline; same claimStartBackoff << attempt duration (1, 2, 4, 8 s), same select on waitCtx.Done() (stop timer, cancelWait, return) or the timer firing (cancelWait); var replaced only by tests; no read, path or root decision added |
+
+Envelopes re-pinned: `internal/activator`.
+
+Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
+
+Recorded dispositions, which each change must still satisfy:
+
+- `internal/activator/claimwake.go` `Service.startAdmitted`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J1,J4;basis=route-and-input;rev3=census-ruling-ci-speed-activator;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=value-identical: the start backoff time.Second << attempt reads package var claimStartBackoff (1 s, changed only by tests) << attempt; the same 1, 2, 4, 8 s waits; read sites and offsets unchanged; no root decision;rev3=census-ruling-edge-held-streams;basis=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos): the one call p.bindProducer(producer) becomes a.bindClaimProducer(e, p, producer), which adds a pre-stop step (refuseHeldLegsOn: under a.mu mark the process as taking no held leg and cancel its entry's legs) before the producer cleanup's first Stop. Cancel funcs and an atomic flag only; no read added.
+
+Seal: `e820ef22ac677cd49aa9ca3f70d6bca9dea061bce3184ca86f0a3fc98f8de0eb` → `feba66806ddfcbdfaf369d72413bb18e75162263b6f82f117fccf1b75497986a`.
+
+Overlay: `internal/provision/testdata/plan14-root-reads-fix-activator-backoff-flake.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-fix-activator-backoff-flake.md b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-fix-activator-backoff-flake.md
new file mode 100644
index 000000000..29058f32a
--- /dev/null
+++ b/docs/superpowers/reviews/CODEX-REVIEW-BRIEF-fix-activator-backoff-flake.md
@@ -0,0 +1,26 @@
+# Review brief: branch fix-activator-backoff-flake, round 1 of 3
+
+Repo carlos/platform, branch `fix-activator-backoff-flake`, commit: the commit
+that adds this file. Under review: the diff `c0a65b0e..HEAD`.
+
+Context: CI flake (amadan discussion 662) in
+internal/activator/claimloop_linux_test.go
+TestStartAdmittedLoopStopsAfterFiveStartsWithDoublingBackoff, which measured
+real time with +200 ms tolerance. The fix adds a seam for the start loop's
+backoff wait in internal/activator/claimwake.go (startAdmitted) so the test
+records the requested durations exactly.
+
+Check:
+1. Production behaviour is unchanged: same durations (claimStartBackoff <<
+ attempt), and every select branch the old inline timer had (deadline,
+ ctx cancel, stop, timer fire) behaves exactly as before, including timer
+ cleanup (no leaked timers, no changed ordering).
+2. The tests now prove the doubling deterministically and the deadline-cuts-
+ the-wait test still proves a real cut.
+3. CENSUS: docs/superpowers/notes/census-proposal-fix-activator-backoff-flake.md
+ (unit Service.startAdmitted body change, internal/activator envelope
+ re-pinned). Give the verdict the card asks for, per unit and envelope.
+
+Severity P1/P2/P3 as usual. End with exactly one verdict: READY, READY AFTER
+FIXES, or NOT READY. Do not edit files. No subagents. Targeted go test -run
+only.
diff --git a/internal/activator/claimloop_linux_test.go b/internal/activator/claimloop_linux_test.go
index f51a84446..88bdbebce 100644
--- a/internal/activator/claimloop_linux_test.go
+++ b/internal/activator/claimloop_linux_test.go
@@ -6,6 +6,7 @@ import (
"context"
"errors"
"os/exec"
+ "slices"
"sync"
"testing"
"time"
@@ -34,6 +35,30 @@ type loopRun struct {
mu sync.Mutex
reserved []time.Time
failed int
+ // events is the loop's order of starts and backoff waits, as "start"
+ // and "wait <d>"; waits are logged only under recordClaimBackoff.
+ events []string
+}
+
+func (r *loopRun) log(event string) {
+ r.mu.Lock()
+ r.events = append(r.events, event)
+ r.mu.Unlock()
+}
+
+// recordClaimBackoff replaces startAdmitted's backoff timer for one test:
+// each wait asked for is logged on run and fires at once, so the schedule
+// is read exactly, with no real time in it.
+func recordClaimBackoff(t *testing.T, run *loopRun) {
+ t.Helper()
+ old := claimBackoffTimer
+ claimBackoffTimer = func(d time.Duration) (<-chan time.Time, func() bool) {
+ run.log("wait " + d.String())
+ fired := make(chan time.Time, 1)
+ fired <- time.Now()
+ return fired, func() bool { return false }
+ }
+ t.Cleanup(func() { claimBackoffTimer = old })
}
func (r *loopRun) starts() []time.Time {
@@ -44,7 +69,11 @@ func (r *loopRun) starts() []time.Time {
func driveLoop(t *testing.T, policy startPolicy) (*loopRun, error) {
t.Helper()
- run := &loopRun{}
+ return driveLoopWith(t, &loopRun{}, policy)
+}
+
+func driveLoopWith(t *testing.T, run *loopRun, policy startPolicy) (*loopRun, error) {
+ t.Helper()
obs := hostclaim.Observed{Host: "x.example", ETag: "one", Claim: &hostclaim.Claim{Holder: &hostclaim.Holder{Location: "instances/x.example/inc", Session: 1, Generation: "one"}, Op: hostclaim.Op{Kind: "landing", ID: "op", Phase: "probing", Exec: "box"}}}
work, _ := (hostwrite.Config{}).Select(t.Context(), obs.Host, "spawn")
a := &Service{cfg: Config{HostWrites: hostwrite.Config{Enabled: true}, Claims: &ClaimRuntime{Box: "box", Read: func(context.Context, string) (hostclaim.Observed, error) { return obs, nil }, Prepare: func(context.Context, registry.Route, hostclaim.Observed, startPolicy) (ClaimStart, error) {
@@ -63,6 +92,7 @@ func driveLoop(t *testing.T, policy startPolicy) (*loopRun, error) {
Reserve: func(context.Context, int) error {
run.mu.Lock()
run.reserved = append(run.reserved, time.Now())
+ run.events = append(run.events, "start")
run.mu.Unlock()
return nil
},
@@ -76,37 +106,41 @@ func driveLoop(t *testing.T, policy startPolicy) (*loopRun, error) {
return run, a.startAdmitted(t.Context(), &actEntry{host: obs.Host}, obs, policy)
}
-// Five starts, then the loop gives up, with the delay between starts
-// doubling: 1, 2, 4 and 8 seconds. The loop's backoff is time.NewTimer
-// inline (claimwake.go), with no clock or sleeper to substitute, so this
-// reads real time. It runs on a quarter of the production base
-// (claimStartBackoff), so 0.25, 0.5, 1 and 2 s, each delay checked to
-// within 200 ms, which keeps every step apart from its neighbours: the
-// doubling is what is proven, on 3.75 s of waiting rather than 15.
+// Five starts, then the loop gives up, with the wait between starts
+// doubling: 1, 2, 4 and 8 seconds, the production schedule. The backoff
+// timer is recorded rather than slept (recordClaimBackoff), so each wait
+// is checked exactly, in its place between two starts, with no real time
+// to drift on a loaded machine (discussion 662).
func TestStartAdmittedLoopStopsAfterFiveStartsWithDoublingBackoff(t *testing.T) {
if claimStartBackoff != time.Second {
t.Fatalf("claimStartBackoff = %v: the production schedule is 1, 2, 4, 8 s", claimStartBackoff)
}
- base := 250 * time.Millisecond
- shrinkClaimStartBackoff(t, base)
- run, err := driveLoop(t, startPolicy{MaxStarts: 5, Window: 30 * time.Second, Deadline: time.Now().Add(time.Hour), Probe: true})
+ run := &loopRun{}
+ recordClaimBackoff(t, run)
+ _, err := driveLoopWith(t, run, startPolicy{MaxStarts: 5, Window: 30 * time.Second, Deadline: time.Now().Add(time.Hour), Probe: true})
if !errors.Is(err, ErrClaimIsolationUnavailable) || errors.Is(err, ErrAttemptOver) {
t.Fatalf("the last start's refusal should end the loop: %v", err)
}
- at := run.starts()
- if len(at) != 5 || run.failed != 5 {
- t.Fatalf("starts=%d failed=%d, want 5 and 5", len(at), run.failed)
+ if n := len(run.starts()); n != 5 || run.failed != 5 {
+ t.Fatalf("starts=%d failed=%d, want 5 and 5", n, run.failed)
}
- for i, want := range []time.Duration{base, 2 * base, 4 * base, 8 * base} {
- if gap := at[i+1].Sub(at[i]); gap < want || gap > want+200*time.Millisecond {
- t.Errorf("delay before start %d = %v, want %v", i+2, gap, want)
- }
+ want := []string{"start", "wait 1s", "start", "wait 2s", "start", "wait 4s", "start", "wait 8s", "start"}
+ run.mu.Lock()
+ got := append([]string(nil), run.events...)
+ run.mu.Unlock()
+ if !slices.Equal(got, want) {
+ t.Fatalf("loop = %q, want %q", got, want)
}
}
// The cut deadline ends the attempt while it waits between starts: one
-// start, then ErrAttemptOver at the deadline, not after the 1 s backoff.
+// start, then ErrAttemptOver at the deadline, not when the backoff fires.
+// This one runs the real timer, since a real wait being cut is what it
+// proves. The backoff is stretched to 30 s so the bound can be generous:
+// ending within 10 s of a deadline 300 ms out is only possible if the
+// deadline, not the timer, ended the wait.
func TestStartAdmittedLoopCutDeadlineEndsTheBackoffWait(t *testing.T) {
+ shrinkClaimStartBackoff(t, 30*time.Second)
deadline := time.Now().Add(300 * time.Millisecond)
run, err := driveLoop(t, startPolicy{MaxStarts: 5, Window: 30 * time.Second, Deadline: deadline, Probe: true})
if !errors.Is(err, ErrAttemptOver) {
@@ -115,7 +149,7 @@ func TestStartAdmittedLoopCutDeadlineEndsTheBackoffWait(t *testing.T) {
if n := len(run.starts()); n != 1 {
t.Fatalf("starts=%d, want 1", n)
}
- if late := time.Since(deadline); late > 500*time.Millisecond {
+ if late := time.Since(deadline); late > 10*time.Second {
t.Fatalf("the attempt ended %v after the deadline", late)
}
}
diff --git a/internal/activator/claimwake.go b/internal/activator/claimwake.go
index 99c4a4354..ba886852a 100644
--- a/internal/activator/claimwake.go
+++ b/internal/activator/claimwake.go
@@ -399,6 +399,14 @@ func (a *Service) doClaimWake(ctx context.Context, e *actEntry) (*wokenInstance,
// can shrink it.
var claimStartBackoff = time.Second
+// claimBackoffTimer starts one of startAdmitted's waits between starts and
+// returns its fire channel and its stop. Production is a real time.Timer; a
+// var so a test can record the waits asked for instead of sleeping them.
+var claimBackoffTimer = func(d time.Duration) (<-chan time.Time, func() bool) {
+ timer := time.NewTimer(d)
+ return timer.C, timer.Stop
+}
+
func (a *Service) startAdmitted(ctx context.Context, e *actEntry, admitted hostclaim.Observed, policy startPolicy) (result error) {
r := a.cfg.Claims
if !a.cfg.HostWrites.Enabled || r == nil || r.Read == nil || r.Prepare == nil || e == nil || admitted.Host != e.host || policy.MaxStarts < 1 || policy.MaxStarts > 5 || policy.Window <= 0 || policy.Window > 30*time.Second {
@@ -706,13 +714,13 @@ func (a *Service) startAdmitted(ctx context.Context, e *actEntry, admitted hostc
if policy.Probe && !budget.opened.IsZero() {
waitCtx, cancelWait = context.WithDeadline(ctx, budget.opened.Add(policy.Window))
}
- timer := time.NewTimer(claimStartBackoff << attempt)
+ fired, stopTimer := claimBackoffTimer(claimStartBackoff << attempt)
select {
case <-waitCtx.Done():
- timer.Stop()
+ stopTimer()
cancelWait()
return budget.result(waitCtx.Err())
- case <-timer.C:
+ case <-fired:
cancelWait()
}
}
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 2f64c1076..4e9d2dbde 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -124,3 +124,4 @@ plan14-root-reads-region-ha-graceful-terminate-t1.tsv 3273ecae03559a8d450027f4cd
plan14-root-reads-region-ha-graceful-terminate-t2.tsv 687cf7744aecdf8a238dcdb80562b0ef191b3e247c28851de44c01866dbeca13
plan14-root-reads-static-sweep-periodic.tsv 3dc3f6657527133a251c028772315bdd333b04c318fbbc6d90ef5047f15408ed
plan14-root-reads-agent-login-link.tsv e820ef22ac677cd49aa9ca3f70d6bca9dea061bce3184ca86f0a3fc98f8de0eb
+plan14-root-reads-fix-activator-backoff-flake.tsv feba66806ddfcbdfaf369d72413bb18e75162263b6f82f117fccf1b75497986a
diff --git a/internal/provision/testdata/plan14-root-reads-fix-activator-backoff-flake.tsv b/internal/provision/testdata/plan14-root-reads-fix-activator-backoff-flake.tsv
new file mode 100644
index 000000000..a8a7d739d
--- /dev/null
+++ b/internal/provision/testdata/plan14-root-reads-fix-activator-backoff-flake.tsv
@@ -0,0 +1,5 @@
+# Census overlay fix-activator-backoff-flake, written by census-propose (#369). census-ruling-fix-activator-backoff-flake, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-fix-activator-backoff-flake.md.
+# 1 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+@legacy a97f41f8da2097b6a870755497faaa3bd33bdd123b2cd16d1b18e86c02ce6551 internal/activator/claimwake.go Service.startAdmitted legacy accepted-legacy accepted-legacy;rev3=J1,J4;basis=route-and-input;rev3=census-ruling-ci-speed-activator;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=value-identical: the start backoff time.Second << attempt reads package var claimStartBackoff (1 s, changed only by tests) << attempt; the same 1, 2, 4, 8 s waits; read sites and offsets unchanged; no root decision;rev3=census-ruling-edge-held-streams;basis=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos): the one call p.bindProducer(producer) becomes a.bindClaimProducer(e, p, producer), which adds a pre-stop step (refuseHeldLegsOn: under a.mu mark the process as taking no held leg and cancel its entry's legs) before the producer cleanup's first Stop. Cancel funcs and an atomic flag only; no read added.;rev3=census-ruling-fix-activator-backoff-flake;basis=value-identical: the backoff wait between starts gets its timer from package var claimBackoffTimer (production: time.NewTimer(d), returning timer.C and timer.Stop) instead of calling time.NewTimer inline; same claimStartBackoff << attempt duration (1, 2, 4, 8 s), same select on waitCtx.Done() (stop timer, cancelWait, return) or the timer firing (cancelWait); var replaced only by tests; no read, path or root decision added file:Prepare@1038,file:Prepare@213,file:Read@196,file:Read@812,file:Read@8383,whole:Route@1055,whole:Route@5087,whole:Route@5163,whole:Route@5795,whole:Route@5858 0e641d4fb1f08ca8a0599dc91ade481b4a6efdebe0cb31787de1819332ac75d4
+internal/activator/claimwake.go Service.startAdmitted legacy accepted-legacy accepted-legacy;rev3=J1,J4;basis=route-and-input;rev3=census-ruling-ci-speed-activator;identity=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos);basis=value-identical: the start backoff time.Second << attempt reads package var claimStartBackoff (1 s, changed only by tests) << attempt; the same 1, 2, 4, 8 s waits; read sites and offsets unchanged; no root decision;rev3=census-ruling-edge-held-streams;basis=the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos): the one call p.bindProducer(producer) becomes a.bindClaimProducer(e, p, producer), which adds a pre-stop step (refuseHeldLegsOn: under a.mu mark the process as taking no held leg and cancel its entry's legs) before the producer cleanup's first Stop. Cancel funcs and an atomic flag only; no read added.;rev3=census-ruling-fix-activator-backoff-flake;basis=value-identical: the backoff wait between starts gets its timer from package var claimBackoffTimer (production: time.NewTimer(d), returning timer.C and timer.Stop) instead of calling time.NewTimer inline; same claimStartBackoff << attempt duration (1, 2, 4, 8 s), same select on waitCtx.Done() (stop timer, cancelWait, return) or the timer firing (cancelWait); var replaced only by tests; no read, path or root decision added file:Prepare@1038,file:Prepare@213,file:Read@196,file:Read@812,file:Read@8383,whole:Route@1055,whole:Route@5087,whole:Route@5163,whole:Route@5795,whole:Route@5858 0e641d4fb1f08ca8a0599dc91ade481b4a6efdebe0cb31787de1819332ac75d4
+@coverage internal/activator 3c863eefff9ae8a8af6fe499c47d6c712bba2b8a3a1732eecae0707b12911c64