carlos / platform Public

region HA: cross-region backhaul marks bodyless requests only

Task 3 fix round 2 (controller's ruling 2026-11-09: no per-host HA flag
in edges files).

- backhaulAttempt marks a bodyless request X-Carlos-Replayable: 1 on
  attempt 1, whatever the HA metadata says; there is nothing to buffer,
  and a receiver honours the mark only for a host it is moving. A
  request with a body is never copied or marked and streams as before;
  a draining box gives it today's answer (§25.8, Q2).
- backhaulHA is removed: the HA-metadata body copy stays on the
  in-region front forward only.
- A streamed backhaul body is watched for its end, so an upstream that
  answers and closes early also closes the client connection.
- Tests, through the real backhaul with no hook: a bodyless GET is
  followed once after a handover answer, then goes to the cached holder;
  cross-region POSTs (10 bytes, 64 KiB, larger, chunked) are not marked
  and get today's answer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 786d6fc1ee621b232659206b29f524774d848f3d parent 5a71fbf
3 files changed, +52 −50
  • internal/edge/backhaul_holder.go +20 −31
  • internal/edge/edge.go +1 −1
  • internal/edge/replay_test.go +31 −18

Intent

Human

<task-notification> <task-id>aef76dee7dd89daec</task-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/283be278-5c6a-4a71-a8d6-b3d32c668985/tasks/aef76dee7dd89daec.output</output-file> <status>completed</status> <summary>Agent "Implement Task 3: replay + attempts" 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>Status: NEEDS_CONTEXT. All four findings are fixed and committed; the open question is how to turn on cross-region replay. Commits: `9d35af26` (fix), `5a71fbfd` (census). FIX_BASE `bf69eeb0`. Tests: the replay and non-HA streaming tests pass at `-count=50`. The 64 KiB repro and the over-limit, unmarked and cross-region tests pass at `-count=100`. `TestHandoverExitOnUnfinishedBodies` passes at `-count=5`, the edge regression set and census test pass, and all 5 round-1 mutations are caught. - **P2-1:** the handover exit answers first and reads no body beforehand. Afterwards the connection lingers for up to 1 s, so the sender gets the handover answer or the 503 with `Retry-After: 10` instead of a reset. One difference from the ruling: the discard limit is net/http's built-in 256 KiB, not 64 KiB. It is the only place the response is already fully written; the 1 s bound is ours. - **P2-2:** both 502 allowances are gone. The high counts exposed a second race on the forwarding box when the draining box answers before the client's upload is finished. That box now asks the client connection to close, and the 503 always arrives. - **P2-3:** the backhaul copies a body only when the box's HA ingress records say the host is HA. A new test shows a non-HA body still streams. - **P3:** a handover naming this box now gives the client exactly one `100 Continue`. **Decision needed:** cross-region replay never fires in production, because no box can see trusted HA metadata for hosts in other regions: - The HA ingress records are scoped to the box's own region. - A host this box does have a record for is answered before the backhaul is ever reached. The code path is built, and the tests stand in for the missing metadata through a test hook. One way to turn it on is a published per-host HA flag in the edges file; that changes a writer in `internal/host` and needs the §24 reader rule. Should I add it, or leave cross-region replay off? Census: re-proposed (seal `049b7fe9...`) and the census test passes. The cross-family verdict on #280 is still owed. Report (fix round appended): /home/paulca/amadan.net/carlos/platform/.claude/worktrees/region-ha-held-build/.superpowers/sdd/2026-11-08-region-ha-held-requests/task-3-report.md</result> <usage><subagent_tokens>397783</subagent_tokens><tool_uses>57</tool_uses><duration_ms>3531981</duration_ms></usage> </task-notification>

diff --git a/internal/edge/backhaul_holder.go b/internal/edge/backhaul_holder.go
index 7eff89103..cb43518ae 100644
--- a/internal/edge/backhaul_holder.go
+++ b/internal/edge/backhaul_holder.go
@@ -144,18 +144,6 @@ func (e *Edge) backhaulTarget(host, dial, box string, owner backhaulOwner) (stri
var errBackhaulHolder = errors.New("backhaul holder follow")
-// backhaulHA reports whether a backhauled request for host is for a
-// born-HA route by trusted routing metadata: this box's HA ingress
-// projection (built from platform records, memory-only) names an HA route
-// of the checked owner for it. Only such a request has its body copied
-// whole (§25.5 "Cross-region first boxes"); every other backhaul streams
-// as before. A var only so a test can stand in for metadata it cannot
-// arrange through the front door.
-var backhaulHA = func(e *Edge, host string, owner backhaulOwner) bool {
- rt, known := e.ingressRoute(host)
- return known && rt.HA && rt.Host != "" && rt.Account == owner.account && rt.App == owner.app
-}
-
// backhaulHolderTransport consumes a followed refusal before the proxy can
// copy its status, headers or body. The normal proxy still owns every other
// response, including upgrades and the second attempt's refusal.
@@ -179,10 +167,13 @@ func (t backhaulHolderTransport) RoundTrip(r *http.Request) (*http.Response, err
// backhaulAttempt is one attempt of a backhauled request, and spends one
// of r's attempts (nextForwardAttempt, replay.go): a §6.5 holder follow, a
// connect fallback and a handover follow (§25.5) are the second and last.
-// On the first, for an HA host only (ha, backhaulHA), it copies the body
-// whole when it can (copyWholeBody) and marks the hop replayable; whole is
-// that copy on the second. Any other request streams as it always has.
-func (e *Edge) backhaulAttempt(w http.ResponseWriter, r *http.Request, host, dial, box string, key []byte, owner backhaulOwner, ha bool, whole *wholeBody) {
+// On the first, a bodyless request is marked replayable: there is nothing
+// to copy, and a receiver honours the mark only for a host it is moving
+// (ruling 2026-11-09: no per-host HA mark crosses regions). A request
+// with a body is never copied here, never marked, and streams as it
+// always has; it gets today's answer from a draining box (§25.8). whole
+// is the (empty) copy on the second attempt.
+func (e *Edge) backhaulAttempt(w http.ResponseWriter, r *http.Request, host, dial, box string, key []byte, owner backhaulOwner, whole *wholeBody) {
n, ok := nextForwardAttempt(r)
if !ok {
// I4: never a third attempt.
@@ -191,14 +182,9 @@ func (e *Edge) backhaulAttempt(w http.ResponseWriter, r *http.Request, host, dia
return
}
retry := n < maxForwardAttempts
- if ha && n == 1 && whole == nil {
- c, err := copyWholeBody(r)
- if err != nil {
- e.logThrottled("backhaul-error "+box, fmt.Sprintf("backhaul %s to %s (%s): reading the request body: %v", host, box, dial, err))
- w.WriteHeader(http.StatusBadGateway)
- return
- }
- whole = c
+ if n == 1 && whole == nil && r.ContentLength == 0 && len(r.TransferEncoding) == 0 {
+ // HTTP/2 supplies a body object even when the request has none.
+ whole = &wholeBody{}
}
original := r
marked := false
@@ -297,11 +283,14 @@ func (e *Edge) backhaulAttempt(w http.ResponseWriter, r *http.Request, host, dia
e.logThrottled("backhaul-error "+box, fmt.Sprintf("backhaul %s to %s (%s): %v", host, box, dial, err))
w.WriteHeader(http.StatusBadGateway)
}
+ // A streamed body is watched for its end: an upstream that answers
+ // and closes before it has all gone (a draining box's at-once
+ // refusal) also closes the client connection (closeAfterEarlyAnswer).
var onResponse func(*http.Response)
- if ha {
- if whole == nil {
- var streamed *streamedBody
- r, streamed = watchBody(r)
+ if whole == nil {
+ var streamed *streamedBody
+ r, streamed = watchBody(r)
+ if streamed != nil {
onResponse = func(resp *http.Response) { closeAfterEarlyAnswer(w, r, streamed, resp) }
}
}
@@ -313,8 +302,8 @@ func (e *Edge) backhaulAttempt(w http.ResponseWriter, r *http.Request, host, dia
// First attempt plus one retry, at most two attempts total. A connect
// fallback, a §6.5 holder follow and a handover follow spend the
// same budget (nextForwardAttempt). Start with the original context
- // so trace callbacks cannot accumulate; a copied body goes again
- // from the copy, unmarked.
- e.backhaulAttempt(w, original, host, nextDial, nextRef, key, owner, ha, whole)
+ // so trace callbacks cannot accumulate; a marked bodyless request
+ // goes again unmarked.
+ e.backhaulAttempt(w, original, host, nextDial, nextRef, key, owner, whole)
}
}
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 92eff000c..64946d7ed 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -4272,7 +4272,7 @@ func (e *Edge) backhaulTo(w http.ResponseWriter, r *http.Request, host, eip, box
return false
}
eip, box = e.backhaulTarget(host, eip, box, owner)
- e.backhaulAttempt(w, withForwardAttempts(r), host, eip, box, key, owner, backhaulHA(e, host, owner), nil)
+ e.backhaulAttempt(w, withForwardAttempts(r), host, eip, box, key, owner, nil)
return true
}
diff --git a/internal/edge/replay_test.go b/internal/edge/replay_test.go
index de2c46549..b1acc2c0e 100644
--- a/internal/edge/replay_test.go
+++ b/internal/edge/replay_test.go
@@ -550,20 +550,18 @@ func TestReplayMarkedStreamFromMemory(t *testing.T) {
}
}
-// Cross-region: a backhaul first box (dennis, region us) sends a POST to
-// annie (eu), which is moving the host and answers with the handover
-// naming betty (eu); dennis replays its whole copy to betty once.
+// Cross-region: a backhaul first box (dennis, region us) sends a request
+// to annie (eu), which is moving the host and answers a marked hop with
+// the handover naming betty (eu). A bodyless request is marked and sent
+// to betty once; a request with a body is never marked, streams, and gets
+// today's answer (ruling 2026-11-09, inside §25.8's narrowed guarantee).
+// No test hook: the real backhaul, with no HA metadata on dennis.
func newRpBackhaul(t *testing.T) (*BackhaulHolderScene, *rpMover) {
t.Helper()
mv := &rpMover{fwAct: &fwAct{}, mode: "handover", ref: "betty"}
app := fwApp(t, "annie")
rt := registry.Route{Host: HAForwardHost, Kind: "instance", Hibernate: true, Addr: app.srv.Listener.Addr().String(), Status: "ready", Account: "abc", App: "shop"}
s := NewBackhaulHolderScene(t, t.TempDir(), mv, rt)
- // The HA ingress projection is region-scoped, so the sender (us) has
- // no record of this eu host; stand in for trusted HA metadata here.
- old := backhaulHA
- backhaulHA = func(_ *Edge, host string, o backhaulOwner) bool { return host == HAForwardHost && o.app == "shop" }
- t.Cleanup(func() { backhaulHA = old })
if err := s.first.reg.SetHA(HAForwardHost); err != nil {
t.Fatal(err)
}
@@ -573,13 +571,19 @@ func newRpBackhaul(t *testing.T) (*BackhaulHolderScene, *rpMover) {
return s, mv
}
+// send is a POST of body (chunked: unknown length) from dennis, or a GET
+// when body is nil.
func (s *BackhaulHolderScene) send(t *testing.T, body []byte, chunked bool) (int, http.Header, string) {
t.Helper()
+ method := http.MethodPost
var rd io.Reader = bytes.NewReader(body)
if chunked {
rd = io.MultiReader(bytes.NewReader(body))
}
- req, err := http.NewRequest(http.MethodPost, "https://"+HAForwardHost+"/echo-body", rd)
+ if body == nil {
+ method, rd = http.MethodGet, nil
+ }
+ req, err := http.NewRequest(method, "https://"+HAForwardHost+"/echo-body", rd)
if err != nil {
t.Fatal(err)
}
@@ -593,10 +597,10 @@ func (s *BackhaulHolderScene) send(t *testing.T, body []byte, chunked bool) (int
}
func TestReplayCrossRegionThroughBackhaul(t *testing.T) {
- t.Run("whole copy, replayed once", func(t *testing.T) {
+ t.Run("bodyless GET, followed once", func(t *testing.T) {
s, mv := newRpBackhaul(t)
- body := rpBody(t, 64<<10)
- status, h, got := s.send(t, body, false)
+ var body []byte
+ status, h, got := s.send(t, nil, false)
if status != 200 || got != rpEcho("holder", body) || h.Get(HandoverHeader) != "" {
t.Fatalf("%d %v %q, want betty's echo", status, h, got)
}
@@ -607,7 +611,7 @@ func TestReplayCrossRegionThroughBackhaul(t *testing.T) {
t.Fatalf("annie's marks %v, want [true]", m)
}
// The holder is cached: the next request goes straight to betty.
- if status, _, got := s.send(t, body, false); status != 200 || got != rpEcho("holder", body) {
+ if status, _, got := s.send(t, nil, false); status != 200 || got != rpEcho("holder", body) {
t.Fatalf("next: %d %q", status, got)
}
if a, b := s.Counts(); a != 1 || b != 2 {
@@ -620,22 +624,31 @@ func TestReplayCrossRegionThroughBackhaul(t *testing.T) {
chunked bool
ref string
}{
+ {"POST 10 bytes, not replayed", 10, false, "betty"},
+ {"POST 64 KiB, not replayed", 64 << 10, false, "betty"},
{"64 KiB + 1", 64<<10 + 1, false, "betty"},
{"1 MiB", 1 << 20, false, "betty"},
{"10 bytes chunked", 10, true, "betty"},
- {"ref not in view", 10, false, "zed"},
- {"ref naming the first box", 10, false, "dennis"},
+ {"GET, ref not in view", -1, false, "zed"},
+ {"GET, ref naming the first box", -1, false, "dennis"},
} {
t.Run(tc.name, func(t *testing.T) {
s, mv := newRpBackhaul(t)
mv.set("handover", tc.ref)
- status, h, got := s.send(t, rpBody(t, tc.n), tc.chunked)
+ var body []byte
+ if tc.n >= 0 {
+ body = rpBody(t, tc.n)
+ }
+ status, h, got := s.send(t, body, tc.chunked)
if status != 503 || h.Get("Retry-After") != "10" || h.Get(HandoverHeader) != "" {
t.Fatalf("%d %v %q, want today's answer", status, h, got)
}
if a, b := s.Counts(); a != 1 || b != 0 {
t.Fatalf("calls=%d/%d, want one attempt", a, b)
}
+ if m, want := mv.marks(), body == nil; !equalMarks(m, want) {
+ t.Fatalf("annie's marks %v, want [%v]: only a bodyless request is marked", m, want)
+ }
})
}
t.Run("a second-attempt handover gets today's answer", func(t *testing.T) {
@@ -649,7 +662,7 @@ func TestReplayCrossRegionThroughBackhaul(t *testing.T) {
if err := s.holder.edge.Reload(); err != nil {
t.Fatal(err)
}
- status, h, got := s.send(t, rpBody(t, 10), false)
+ status, h, got := s.send(t, nil, false)
if status != 503 || h.Get("Retry-After") != "10" || h.Get(HandoverHeader) != "" {
t.Fatalf("%d %v %q, want today's answer", status, h, got)
}
@@ -793,7 +806,7 @@ func TestReplayCrossRegionNameAuthority(t *testing.T) {
if _, ok := m.handoverAlternative(HAForwardHost, "betty", "annie", o, time.Now()); ok {
t.Fatal("betty no longer claims the name: allowed, want refused")
}
- status, h, got := s.send(t, rpBody(t, 10), false)
+ status, h, got := s.send(t, nil, false) // a GET: marked, so only authority stops the follow
if status != 503 || h.Get("Retry-After") != "10" || h.Get(HandoverHeader) != "" {
t.Fatalf("%d %v %q, want today's answer", status, h, got)
}