carlos / platform Public

Ignore cancelled placement forwards in peer reachability

Classify visitor cancellation and queued dial expiry separately so
placementPeers preserves peer suspicion, epochs and deadlines. Keep real
target failures and P1b-2 Holder outcomes unchanged under controller R7.

Add cancellation regression witnesses, refresh census envelopes and
record the branch review's deferred P3s and ledger minors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 3e08575b9782b52649db1d7b3395b9c706316df9 parent ef4ffcb
7 files changed, +166 −12
  • docs/superpowers/notes/census-region-ha-p1b3a-claims.md +2 −0
  • docs/superpowers/notes/region-ha-p1b3a-followups.md +18 −0
  • internal/activator/haplacement_peers.go +3 −0
  • internal/activator/haplacement_test.go +67 −8
  • internal/edge/forwardha.go +8 −2
  • internal/edge/forwardha_test.go +66 −0
  • internal/provision/testdata/plan14-root-reads-region-ha-p1b3a.tsv +2 −2
diff --git a/docs/superpowers/notes/census-region-ha-p1b3a-claims.md b/docs/superpowers/notes/census-region-ha-p1b3a-claims.md
index cf199476b..9c6e3f688 100644
--- a/docs/superpowers/notes/census-region-ha-p1b3a-claims.md
+++ b/docs/superpowers/notes/census-region-ha-p1b3a-claims.md
@@ -5,3 +5,5 @@ internal/host/agent.go:runAgent Arm HA placement exactly once on the existing Se
internal/activator/haforward.go:Service.ForwardTarget Retain immutable lock observations and evaluate the pure placement rule after all existing bypass and positive-holder/suspect checks. Fix round 1: decide and reserve under one peer mutex using only current serving candidates, and log the final Base/Refuse decision if attempt tagging loses a race. Reads in-memory route Host and the Route passed to the pure rule; same bounded lock read, no new store or filesystem operation and no acquire authority.
internal/edge/forwardha.go:Edge.frontForwardHA Pass the existing route to a lazy cached wakeSnapshot closure, handle explicit local denial before all accounting, and refuse a disappeared Placement choice rather than falling through locally; whole Route read only, no new store or filesystem operation and no root authority.
internal/activator/haplacement.go:DecidePlacement Pure applicability rule reads in-memory route Host/App and RouteAccount for capacity preference, self/expired-owner recovery and explicit LocalDenied refusal; no proof, acquire, store, filesystem or root authority, and no guessed free observation.
+# Envelope-only internal/edge/forwardha.go:Edge.forwardHA: Branch P2-1/R7: label Placement/Recovery client cancellation and a timed-out queued dial ForwardCanceled without changing responses or Holder outcomes. No new read, filesystem/store operation, wire field or root authority.
+# Envelope-only internal/activator/haplacement_peers.go:placementPeers.note: Branch P2-1/R7: ignore ForwardCanceled before peer classification; preserve suspicion, epochs and deadlines. All actual target-failure classification remains here; in-memory state only, no new read or root authority.
diff --git a/docs/superpowers/notes/region-ha-p1b3a-followups.md b/docs/superpowers/notes/region-ha-p1b3a-followups.md
new file mode 100644
index 000000000..658415ad4
--- /dev/null
+++ b/docs/superpowers/notes/region-ha-p1b3a-followups.md
@@ -0,0 +1,18 @@
+# Region HA P1b-3a agent follow-ups
+
+- P3-1. The app's own headerless 503 or 502 on a successfully woken host marks the peer suspect
+- P3-2. Ingress Base meeting a held lease answers with Retry-After of up to the lease TTL (about 900 s)
+- P3-3. A Recovery decision that turns NotInView falls through to a local wake on a box whose own file forbids local service
+- P3-4. P1b-2 holder semantics changed independently of the switch
+- P3-5. Switch-off boxes now log one placement line per cold born-HA host per minute
+- P3-6. The over-capacity page can fire on a rollout where every candidate's totals read 0
+- Task 2: minor (deferred): placement fields re-decoded per wakeSnapshot call (decode once per poll)
+- Task 2: minor (deferred): duplicate box across files: wakeSnapshot keeps last sorted, boxInView keeps last map-iteration
+- Task 2: minor (deferred): negative headroom.awake unclamped; headroom:null excludes candidate while absent keeps it ineligible
+- Task 2: minor (deferred): carry-forward test cannot tell kept from dropped file; assert Usable at exactly wakeNow
+- Task 2: minor (deferred): non-empty own placement required (stricter than brief, fails toward Base); comment already present per branch review, wakeplacement.go:114 (Done)
+- Task 3: minor (deferred): failure not recorded against a ref when this box is awake/waking the host or a holder is now known; ref stays eligible for other hosts until their own attempt fails (Task 4 model must not assume every failure is recorded)
+- Task 3: minor (deferred): placementPeers lock held while DecidePlacement runs; cold decisions on a box serialized
+- Task 4: minor (deferred): budget 10s re-read P3-3/P3-4/P3-7 not taken (see task-4-review-r1.md); root payload agent composition still hand-assembled (P3-2 partial)
+- Task 4: minor (deferred): coldRootAuthorize opens leo's live SQLite registry as root per request; read gate hash once at setup
+- Task 4: minor (deferred): control relay makes sophie's controller calls appear from carlos-edge main pid; witnesses say nothing about the controller peer check for a second box
diff --git a/internal/activator/haplacement_peers.go b/internal/activator/haplacement_peers.go
index 7b39b83e7..aef8a233e 100644
--- a/internal/activator/haplacement_peers.go
+++ b/internal/activator/haplacement_peers.go
@@ -87,6 +87,9 @@ func (p *placementPeers) beginLocked(ref string, now time.Time) (uint64, bool) {
// note is the sole classifier for reference-level wake failures (N-HA12).
func (p *placementPeers) note(res edge.ForwardResult, now time.Time) {
+ if res.Outcome == edge.ForwardCanceled {
+ return
+ }
p.mu.Lock()
defer p.mu.Unlock()
e := p.refs[res.Ref]
diff --git a/internal/activator/haplacement_test.go b/internal/activator/haplacement_test.go
index 8e04efba1..6e0f78544 100644
--- a/internal/activator/haplacement_test.go
+++ b/internal/activator/haplacement_test.go
@@ -99,12 +99,14 @@ func TestPlacementApplicability(t *testing.T) {
}
func TestPlacementRefusalMarksSuspect(t *testing.T) {
for _, tc := range []struct {
- name string
- outcome edge.ForwardOutcome
- status int
- holder string
+ name string
+ outcome edge.ForwardOutcome
+ status int
+ holder string
+ keepPeer bool
}{
- {"none", edge.ForwardDone, 503, "none"}, {"503", edge.ForwardDone, 503, ""}, {"502", edge.ForwardDone, 502, ""}, {"zero", edge.ForwardDone, 0, ""}, {"out of view", edge.ForwardNotInView, 0, ""}, {"dial", edge.ForwardUnreachable, 0, ""},
+ {"none", edge.ForwardDone, 503, "none", false}, {"503", edge.ForwardDone, 503, "", false}, {"502", edge.ForwardDone, 502, "", false}, {"zero", edge.ForwardDone, 0, "", false}, {"out of view", edge.ForwardNotInView, 0, "", false}, {"dial", edge.ForwardUnreachable, 0, "", false},
+ {"client canceled", edge.ForwardCanceled, 0, "", true},
} {
for _, clean := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/clean=%v", tc.name, clean), func(t *testing.T) {
@@ -124,12 +126,16 @@ func TestPlacementRefusalMarksSuspect(t *testing.T) {
r := placementResult(d, tc.status, tc.holder)
r.Outcome = tc.outcome
s.a.NoteForward(s.rt.Host, r)
- if d = s.a.ForwardTarget(context.Background(), s.rt, project); d.Ref != "" || d.Refuse {
- t.Fatalf("suspect %+v", d)
+ wantRef := ""
+ if tc.keepPeer {
+ wantRef = "b"
+ }
+ if d = s.a.ForwardTarget(context.Background(), s.rt, project); d.Ref != wantRef || d.Refuse {
+ t.Fatalf("next %+v, want ref %q", d, wantRef)
}
other := s.rt
other.Host = "other.example.com"
- if d = s.a.ForwardTarget(context.Background(), other, project); d.Ref != "" {
+ if d = s.a.ForwardTarget(context.Background(), other, project); d.Ref != wantRef {
t.Fatalf("cross host %+v", d)
}
if lockGets(s.b, s.rt) != 1 {
@@ -725,3 +731,56 @@ func TestPlacementStaleResultCannotPoisonHolder(t *testing.T) {
t.Fatalf("superseded hint replaced holder: %q positive=%v", ref, positive)
}
}
+
+func TestPlacementCanceledPreservesPeerState(t *testing.T) {
+ for _, kind := range []edge.ForwardKind{edge.Placement, edge.Recovery, edge.Holder} {
+ for _, suspect := range []bool{false, true} {
+ t.Run(fmt.Sprintf("kind=%d/suspect=%v", kind, suspect), func(t *testing.T) {
+ now := time.Now()
+ var p placementPeers
+ token, _ := p.begin("b", now)
+ if suspect {
+ p.note(edge.ForwardResult{Ref: "b", Kind: edge.Placement, PeerGen: token, Outcome: edge.ForwardUnreachable}, now)
+ }
+ before, seq := *p.refs["b"], p.seq
+ // Cancellation is not evidence, even with response fields present.
+ p.note(edge.ForwardResult{Ref: "b", Kind: kind, PeerGen: before.epoch, Outcome: edge.ForwardCanceled, Status: 200, Holder: "b"}, now.Add(time.Second))
+ if after := *p.refs["b"]; after != before || p.seq != seq {
+ t.Fatalf("cancellation changed peer: before=%+v after=%+v seq=%d/%d", before, after, seq, p.seq)
+ }
+ })
+ }
+ }
+}
+
+func TestPlacementCanceledRetry(t *testing.T) {
+ for _, kind := range []edge.ForwardKind{edge.Placement, edge.Recovery, edge.Holder} {
+ t.Run(fmt.Sprintf("kind=%d", kind), func(t *testing.T) {
+ s := haService(t, "shop.example.com")
+ s.rt.HA = true
+ clk := &fakeClock{t: time.Now()}
+ s.a.holders.now = clk.now
+ if kind != edge.Placement {
+ expiry := clk.now().Add(-time.Second)
+ if kind == edge.Holder {
+ expiry = clk.now().Add(time.Minute)
+ }
+ putLockFor(t, s.b, s.rt, leaseBody(t, heldByB, expiry))
+ }
+ project := func() boxchoice.WakeSnapshot { return placementSnapshot(clk.now()) }
+ first := s.a.ForwardTarget(context.Background(), s.rt, project)
+ if first.Ref != "b" || first.Kind != kind {
+ t.Fatalf("initial %+v", first)
+ }
+ r := placementResult(first, 0, "")
+ r.Outcome = edge.ForwardCanceled
+ s.a.NoteForward(s.rt.Host, r)
+ if next := s.a.ForwardTarget(context.Background(), s.rt, project); next != first {
+ t.Fatalf("retry %+v, want unchanged decision %+v", next, first)
+ }
+ if n := lockGets(s.b, s.rt); n != 1 {
+ t.Fatalf("retry used %d GETs, want 1 total", n)
+ }
+ })
+ }
+}
diff --git a/internal/edge/forwardha.go b/internal/edge/forwardha.go
index 92e717e4d..6414a13b6 100644
--- a/internal/edge/forwardha.go
+++ b/internal/edge/forwardha.go
@@ -81,9 +81,12 @@ const (
ForwardUnreachable
// ForwardDone: the request went to the named box and its answer, whatever
// it was, was relayed, or this box refused the hop itself (Status 0), or
- // the request waited out its deadline behind another dial (Status 0,
- // errForwardDialQueued: it says nothing about the holder).
+ // a Holder request was cancelled or waited out its dial queue (Status 0).
ForwardDone
+ // ForwardCanceled: a Placement or Recovery request's client left, or its
+ // dial timed out in the queue without reaching the target. Neither is
+ // evidence of target reachability. Holder keeps P1b-2's ForwardDone.
+ ForwardCanceled
)
// ForwardResult is what NoteForward is told. Ref and Gen are the decision's:
@@ -225,6 +228,9 @@ func (e *Edge) forwardHA(w http.ResponseWriter, r *http.Request, host string, d
}
res.Outcome = ForwardDone
onError := func(w http.ResponseWriter, r *http.Request, err error) {
+ if res.Kind != Holder && (r.Context().Err() != nil || errors.Is(err, errForwardDialQueued)) {
+ res.Outcome = ForwardCanceled
+ }
var connect *forwardConnectError
if errors.Is(err, errForwardDialQueued) && r.Context().Err() == nil {
e.logThrottled("forward-queued "+ref, fmt.Sprintf("forward %s → %s (%s): %v; answering 503 + Retry-After: 10", host, ref, dial, err))
diff --git a/internal/edge/forwardha_test.go b/internal/edge/forwardha_test.go
index 059b2282a..c33896b86 100644
--- a/internal/edge/forwardha_test.go
+++ b/internal/edge/forwardha_test.go
@@ -1307,3 +1307,69 @@ func TestForwardHAUpgradeSwitchesProtocols(t *testing.T) {
laWaitFor(t, "the forward's note", func() bool { return len(x.aAct.noted()) == 1 })
x.wantNote(t, fwNote{fwHost, ForwardResult{Outcome: ForwardDone, Ref: "betty", Status: http.StatusSwitchingProtocols}})
}
+
+// fwRoundTripFunc keeps error timing deterministic at the signed hop boundary.
+type fwRoundTripFunc func(*http.Request) (*http.Response, error)
+
+func (f fwRoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
+
+func TestForwardHAClientCanceledOutcome(t *testing.T) {
+ x := newFwScene(t, fwOpts{})
+ dial, ok := x.a.homes.boxInView("betty", time.Now())
+ if !ok {
+ t.Fatal("betty out of view")
+ }
+ for _, kind := range []ForwardKind{Placement, Recovery, Holder} {
+ for _, mode := range []string{"cancel", "deadline", "queued", "queued client deadline", "connect failure", "target hang"} {
+ t.Run(fmt.Sprintf("kind=%d/%s", kind, mode), func(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ var deadlineCancel context.CancelFunc
+ if strings.Contains(mode, "deadline") {
+ ctx, deadlineCancel = context.WithTimeout(ctx, 10*time.Millisecond)
+ defer deadlineCancel()
+ }
+ tr := &http.Transport{}
+ tr.RegisterProtocol("https", fwRoundTripFunc(func(*http.Request) (*http.Response, error) {
+ switch mode {
+ case "cancel":
+ cancel()
+ return nil, context.Canceled
+ case "deadline", "queued client deadline":
+ <-ctx.Done()
+ if mode == "queued client deadline" {
+ return nil, &forwardConnectError{fmt.Errorf("%w: %w", errForwardDialQueued, ctx.Err())}
+ }
+ return nil, ctx.Err()
+ case "queued":
+ return nil, &forwardConnectError{fmt.Errorf("%w: %w", errForwardDialQueued, context.DeadlineExceeded)}
+ case "connect failure":
+ return nil, &forwardConnectError{context.DeadlineExceeded}
+ default:
+ return nil, errors.New("target hung after connecting")
+ }
+ }))
+ x.a.edge.forwardTransports.Store(dial, tr)
+ r := httptest.NewRequest(http.MethodGet, "https://"+fwHost+"/", nil).WithContext(ctx)
+ rec := httptest.NewRecorder()
+ d := ForwardDecision{Ref: "betty", Kind: kind, Gen: 7, PeerGen: 11}
+ got := x.a.edge.forwardHA(rec, r, fwHost, d)
+ want := ForwardDone
+ wantStatus := http.StatusBadGateway
+ if mode == "connect failure" {
+ want, wantStatus = ForwardUnreachable, http.StatusServiceUnavailable
+ } else if mode != "target hang" {
+ if kind != Holder {
+ want = ForwardCanceled
+ }
+ if mode == "queued" {
+ wantStatus = http.StatusServiceUnavailable
+ }
+ }
+ if got != (ForwardResult{Outcome: want, Ref: d.Ref, Kind: kind, Gen: d.Gen, PeerGen: d.PeerGen}) || rec.Code != wantStatus {
+ t.Fatalf("result=%+v status=%d, want outcome=%d status=%d", got, rec.Code, want, wantStatus)
+ }
+ })
+ }
+ }
+}
diff --git a/internal/provision/testdata/plan14-root-reads-region-ha-p1b3a.tsv b/internal/provision/testdata/plan14-root-reads-region-ha-p1b3a.tsv
index 87b6edcef..4fa4949bf 100644
--- a/internal/provision/testdata/plan14-root-reads-region-ha-p1b3a.tsv
+++ b/internal/provision/testdata/plan14-root-reads-region-ha-p1b3a.tsv
@@ -13,7 +13,7 @@ internal/activator/haplacement.go DecidePlacement legacy accepted-legacy accepte
internal/edge/wakeplacement.go homeMap.wakeSnapshot legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b3a;basis=Project cold-wake candidates from one immutable cached edges fold using the local route's canonical host, release.RouteAccount and app. Reads only in-memory route fields, cached publication additions and mirrored signing keys; no filesystem, registry, store or root authority added. Reject malformed placement evidence independently of the unchanged holder decoder; preserve explicit local safety denials even when stale. registry:App@2068,registry:Host@1173,whole:Route@1204 738a92d33ed1ff738f503f33aa33e29550f0ffb4ce66eeda9551ea83ae1b3bdc
internal/activator/haforward.go Service.ForwardTarget legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's activator answers the edge with a holder box ref for an HA route not awake here, from its in-memory holder cache or one bounded (2 s) lock.json read through readHolderView; reads the route's HA flag and host from its own registry row; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=Returns edge.ForwardDecision (Ref, Refuse, Gen) from holderCache.resolve, in memory only: a suspect holder refuses; a positive entry forwards under its generation; a failed read that revalidates an expired suspect keeps it suspect and refuses. The round-1 cache connection gate is removed. The same single bounded (2 s) lock.json read through readHolderView on a miss; no new read and no new S3 request; root never calls it.;rev3=census-ruling-region-ha-p1b3a;basis=Retain immutable lock observations and evaluate the pure placement rule after all existing bypass and positive-holder/suspect checks. Fix round 1: decide and reserve under one peer mutex using only current serving candidates, and log the final Base/Refuse decision if attempt tagging loses a race. Reads in-memory route Host and the Route passed to the pure rule; same bounded lock read, no new store or filesystem operation and no acquire authority. registry:HA@140,registry:Host@1028,registry:Host@1984,registry:Host@2132,registry:Host@2305,registry:Host@2575,registry:Host@2698,registry:Host@302,registry:Host@415,whole:Route@1747,whole:Route@216,whole:Route@2523,whole:Route@769 3aec5f42ee7a1f943537f884bc3ee4773ede60c4bb475ea4b9806f0a3bbcdd92
internal/edge/forwardha.go Edge.frontForwardHA legacy accepted-legacy accepted-legacy;rev3=census-ruling-region-ha-p1b2;basis=new (region HA P1b-2): the agent's edge decides at the front door whether to forward a visitor request: reads the route's HA flag and host from the box's own registry row and asks the activator; forwards only to a box in view over the existing signed hop; root never calls it;rev3=census-ruling-region-ha-dead-holder-backoff;basis=ForwardTarget returns a ForwardDecision (Ref, Refuse, Gen); a Refuse is answered at the front door with the existing unreachable answer (503, Retry-After: 10, same body, via answerHolderUnreachable), with no forward, dial, note or per-request step. A forward passes the decision to forwardHA and NoteForward carries Ref and Gen back. The round-1 connection callback (NoteConnected) is gone: the dial bound now lives in forwardTransport's forwardDialGate, which coalesces TCP dials per holder address in memory. The same 5 read sites; no new read; root never calls it.;rev3=census-ruling-region-ha-p1b3a;basis=Pass the existing route to a lazy cached wakeSnapshot closure, handle explicit local denial before all accounting, and refuse a disappeared Placement choice rather than falling through locally; whole Route read only, no new store or filesystem operation and no root authority. registry:HA@225,registry:Host@928,whole:Route@228,whole:Route@405,whole:Route@548,whole:Route@931 ae7b0d8715c18d05fdebfb3a65861b8b83cd051b6e0d25a60ea16cc4867a8fbc
-@coverage internal/activator c5b7e26a3821e9f1482f35667c63d5a15de613c4be6d157d697332814885cfc3
+@coverage internal/activator 1a3772e63940ef5a570f82eba533fe8eb80516f4b7a121a74bda8102c53dc139
@coverage internal/boxchoice 2461971eb41338ab53f0c7c77c0e01b1e0b854f657a1cecddfbaf6c142f0feed
-@coverage internal/edge be7b24797e5b0f87f01aafc728c77a8939571a77d8e17394b74b3b6cec29d89d
+@coverage internal/edge 79082861fc954adb49f0501ce398556e5fa51fe291989d7f9b20ee0157b7d9af
@coverage internal/host 99415a544b5dbbe53ee873c118ff7998ccdea57aad30f5e78eaa914146fb9f0c