carlos / platform Public

edge: the far edge records every matched answer it writes itself (asset cache plan 3 fix wave 5)

Astra's re-review 3: a backhaul refusal (saturation's 503, the fair
share's 429) is written by the far edge without reaching the home, but
was not billed, so a matched request was recorded by neither edge.

serveAssetFar now records a matched request when it billed it or when no
hop for it reached the home. Arrival is seen by an httptrace WroteHeaders
hook on the request's context, which the fill and the forward both
inherit, so this also covers a forward refused inside backhaulTo, the
no-key 503 and a failed dial's 502. The fill's backhaul refusal and every
forward after the checks write through the counting writer, so the
recorded status is the client's. A request a hop delivered stays the
home's to judge and record.

TestFarLocalAnswersAreRecorded: saturation 503, fair-share 429 and a
waiter's hit each give exactly one far record with the client's status.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3e5f89d9c615fd9dd8f98d1b61cc6360013edab9 parent 6103e6d
2 files changed, +115 −7
  • internal/edge/asset_far_test.go +89 −0
  • internal/edge/assetcatch.go +26 −7
diff --git a/internal/edge/asset_far_test.go b/internal/edge/asset_far_test.go
index 9cff2b9b3..db6c79d78 100644
--- a/internal/edge/asset_far_test.go
+++ b/internal/edge/asset_far_test.go
@@ -1286,3 +1286,92 @@ func TestUncaughtLocalInformationalVocabularyIsScrubbed(t *testing.T) {
})
}
}
+
+// ---- answers the far edge writes itself are recorded (fix wave 5) ---------
+
+// With protection watching, a far request a rule matches (/assets/.env) is
+// recorded by the far edge exactly once, with the status the client got,
+// whenever the far edge answers it itself: a backhaul refusal written before
+// any hop reaches the home (saturation's 503, the fair share's 429), and a
+// waiter's hit from the leader's fill. A fill that reached the home is the
+// home's to record (ordinary-backhaul parity), so the leader adds nothing.
+func TestFarLocalAnswersAreRecorded(t *testing.T) {
+ const p = "/assets/.env"
+ watched := func(t *testing.T, s *farScene) *memRecorder {
+ t.Helper()
+ rec := &memRecorder{}
+ s.e.EnableRequestRecords(rec)
+ s.e.guard = guardWatch
+ return rec
+ }
+ wantOne := func(t *testing.T, rec *memRecorder, status int) {
+ t.Helper()
+ if len(rec.resp) != 0 || len(rec.match) != 1 {
+ t.Fatalf("recorded %d responses and %d matches, want one match", len(rec.resp), len(rec.match))
+ }
+ if m := rec.match[0]; m.Kind != reqrec.KindWatch || m.RuleID != "hard.secrets.env" || m.Status != status || m.Path != p || m.Acct != "abc" {
+ t.Fatalf("recorded %+v, want a watch match with status %d", m, status)
+ }
+ }
+ t.Run("saturated backhaul", func(t *testing.T) {
+ s := newFarScene(t, assetOK(laBody))
+ rec := watched(t, s)
+ old := backhaulMaxInflight.Load()
+ backhaulMaxInflight.Store(0)
+ t.Cleanup(func() { backhaulMaxInflight.Store(old) })
+ got := s.get(t, p)
+ if got.Code != http.StatusServiceUnavailable {
+ t.Fatalf("saturated fill = %d, want 503", got.Code)
+ }
+ if n := s.rec.get().n; n != 0 {
+ t.Fatalf("the home saw %d requests, want none", n)
+ }
+ wantOne(t, rec, http.StatusServiceUnavailable)
+ })
+ t.Run("fair share", func(t *testing.T) {
+ s := newFarScene(t, assetOK(laBody))
+ rec := watched(t, s)
+ s.e.rateMode = RateEnforce
+ req := s.req(http.MethodGet, p)
+ key := s.e.backhaulClientKey(req)
+ for i := 0; i < 32; i++ {
+ release, ok, shed := s.e.backhaulAcquire("betty", key)
+ if !ok || shed {
+ t.Fatalf("holding slot %d: ok=%v shed=%v", i+1, ok, shed)
+ }
+ t.Cleanup(release)
+ }
+ if got := s.serve(req); got.Code != http.StatusTooManyRequests {
+ t.Fatalf("shed fill = %d, want 429", got.Code)
+ }
+ if n := s.rec.get().n; n != 0 {
+ t.Fatalf("the home saw %d requests, want none", n)
+ }
+ wantOne(t, rec, http.StatusTooManyRequests)
+ })
+ t.Run("waiter hit", func(t *testing.T) {
+ first, started, hold, release := fillGate()
+ s := newFarScene(t, func(w http.ResponseWriter, r *http.Request) {
+ if first() {
+ close(started)
+ hold()
+ }
+ assetOK(laBody)(w, r)
+ })
+ t.Cleanup(release)
+ rec := &lockedRecorder{}
+ s.e.EnableRequestRecords(rec)
+ s.e.guard = guardWatch
+ leader := s.async(s.req(http.MethodGet, p))
+ <-started
+ waiter := s.async(s.req(http.MethodGet, p))
+ laWaitFor(t, "the waiter", func() bool { return flightWaitersOf(s.e, s.key(p).ID()) == 1 })
+ release()
+ wantCache(t, <-leader, "miss")
+ wantCache(t, <-waiter, "hit")
+ ms := rec.matches()
+ if len(ms) != 1 || ms[0].Kind != reqrec.KindWatch || ms[0].Status != http.StatusOK || ms[0].Path != p {
+ t.Fatalf("recorded %+v, want the waiter's hit only", ms)
+ }
+ })
+}
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go
index ec998dcd7..9716a2273 100644
--- a/internal/edge/assetcatch.go
+++ b/internal/edge/assetcatch.go
@@ -8,12 +8,14 @@ import (
"fmt"
"hash"
"net/http"
+ "net/http/httptrace"
"net/http/httputil"
"net/url"
"path"
"strconv"
"strings"
"sync"
+ "sync/atomic"
"time"
"github.com/carlosframework/platform/internal/assetcache"
@@ -1057,17 +1059,25 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string
cw := &countingWriter{ResponseWriter: w, n: &n}
var billed, acked, reforwarded bool
var out *outcome
+ // reached is set once a hop for this request (a fill, a forward) has
+ // written its headers to the home: from then on the home has the request
+ // to judge and record. Set from the transport (httptrace), which may run
+ // on another goroutine.
+ var reached atomic.Bool
defer func() {
if billed || (acked && !reforwarded) {
if m := e.usageMeter.Load(); m != nil {
m.Record(rt, 1, n, false)
}
}
- // What this edge answered itself (a hit, the block page, a throttle
- // refusal) is recorded here when a rule matched it, as ServeHTTP's
- // observer defer records a matched request it serves. A fill or a
- // forward reached the home as a hop, and the home records it there.
- if billed && out != nil && out.guard != nil && out.guard.verdict != nil {
+ // Every answer this edge wrote itself is recorded here when a rule
+ // matched it, as ServeHTTP's observer defer records a matched
+ // request it serves: a hit, the block page, a throttle refusal, and
+ // any answer written without reaching the home (a backhaul refusal,
+ // the no-key 503, a failed dial's 502). A request a hop delivered
+ // to the home is the home's to judge and record, as for any
+ // backhauled request (ordinary-backhaul parity).
+ if out != nil && out.guard != nil && out.guard.verdict != nil && (billed || !reached.Load()) {
e.observeRecords(rt, false, r, cw, out)
}
}()
@@ -1083,7 +1093,16 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string
billed = true
return true
}
- }
+ // Every hop below inherits r's context (a fill's request keeps its
+ // values), so this sees the first one that reaches the home.
+ r = r.WithContext(httptrace.WithClientTrace(r.Context(), &httptrace.ClientTrace{
+ WroteHeaders: func() { reached.Store(true) },
+ }))
+ }
+ // From here on a forward writes through cw, so a refusal it writes
+ // itself is recorded with its status; it bills nothing (billed is not
+ // set on a forward).
+ forward = func() { e.farForward(cw, r, host, eip, box) }
var judged, admitted bool
throttle := func() bool {
if !judged {
@@ -1127,7 +1146,7 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string
forward()
return true
}
- releaseSlot, ok := e.backhaulAdmit(w, r, eip, box)
+ releaseSlot, ok := e.backhaulAdmit(cw, r, eip, box)
if !ok {
done(nil)
return true