carlos / platform Public

host: a stale settings pass never undoes a newer one

Each telemetry pass takes a generation at start; a publication (sink call
and remembered values) from an older generation than the last published
is dropped, for the analytics and record-only maps alike. Deterministic
overlapping-pass tests. Whole-branch review R1 P1. Built by Astra.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 55ddb6fcb7ff5b99ba7bf79c6ffac6654c07f91f parent 9615821
2 files changed, +169 −0
  • internal/host/telemetry_pass.go +16 −0
  • internal/host/telemetry_pass_test.go +153 −0
diff --git a/internal/host/telemetry_pass.go b/internal/host/telemetry_pass.go
index e5fc1c8d3..0eecc2f20 100644
--- a/internal/host/telemetry_pass.go
+++ b/internal/host/telemetry_pass.go
@@ -48,6 +48,15 @@ func setAssetOwnersSource(fn func() []registry.Route) { assetOwnersSource.Store(
// gate has no good object for yet.
var recordOnlyLast = &recordOnlyMemory{}
+// Generations reject older passes after a newer one publishes, armed or
+// disarmed. The mutex keeps that check, both sink calls and the remembered
+// record-only value together, so a slow read cannot undo newer settings.
+var telemetryPublication struct {
+ next atomic.Uint64
+ mu sync.Mutex
+ published uint64
+}
+
type recordOnlyMemory struct {
mu sync.Mutex
m map[string]bool
@@ -327,6 +336,7 @@ func (r *telemetryReads) read(ctx context.Context, s release.Store, key string,
// line each. A pass that cannot list the routes pushes nothing, so the edge
// keeps the maps it has.
func telemetryPass(ctx context.Context, s release.Store, reg *registry.Registry) {
+ gen := telemetryPublication.next.Add(1)
aSink, pSink := analyticsSink.Load(), protectionSink.Load()
if aSink == nil && pSink == nil {
return
@@ -340,10 +350,16 @@ func telemetryPass(ctx context.Context, s release.Store, reg *registry.Registry)
log.Printf("agent: telemetry pass: %v", err)
return
}
+ telemetryPublication.mu.Lock()
+ defer telemetryPublication.mu.Unlock()
+ if gen < telemetryPublication.published {
+ return
+ }
if aSink != nil {
(*aSink)(set.analytics)
}
if pSink != nil {
(*pSink)(recordOnlyLast.settle(set.recordOnly, set.unread))
}
+ telemetryPublication.published = gen
}
diff --git a/internal/host/telemetry_pass_test.go b/internal/host/telemetry_pass_test.go
index 08543cbfd..ac95a3f01 100644
--- a/internal/host/telemetry_pass_test.go
+++ b/internal/host/telemetry_pass_test.go
@@ -4,16 +4,169 @@ import (
"context"
"errors"
"maps"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
"slices"
"sync"
"testing"
"time"
+ "github.com/carlosframework/platform/internal/analytics"
+ "github.com/carlosframework/platform/internal/edge"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/telemetry"
)
+type telemetryBlockingStore struct {
+ release.Store
+ key string
+ entered chan struct{}
+ resume chan struct{}
+}
+
+func (s *telemetryBlockingStore) Get(ctx context.Context, key string) ([]byte, string, error) {
+ if key == s.key {
+ close(s.entered)
+ select {
+ case <-s.resume:
+ case <-ctx.Done():
+ return nil, "", ctx.Err()
+ }
+ }
+ return s.Store.Get(ctx, key)
+}
+
+type telemetryObservations struct{ obs []analytics.Obs }
+
+func (m *telemetryObservations) Observe(ob analytics.Obs) { m.obs = append(m.obs, ob) }
+
+func TestTelemetryPassRecordOnlyRejectsOlderPublication(t *testing.T) {
+ for _, armed := range []bool{false, true} {
+ name := "disarmed"
+ if armed {
+ name = "rearmed"
+ }
+ t.Run(name, func(t *testing.T) {
+ t.Setenv("CARLOS_EDGE_PROTECT", "block")
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ s := newCICountingStore(t)
+ reg := edgesTestRegistry(t)
+ if err := reg.Upsert(registry.Route{
+ Host: "shop.example.com", Kind: "static", Account: "bab", App: "shop", Status: "ready",
+ }); err != nil {
+ t.Fatal(err)
+ }
+ e, err := edge.New(reg, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ root := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(root, "shop", "current"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ edge.WithLocalStatic(root)(e)
+ if err := e.Reload(); err != nil {
+ t.Fatal(err)
+ }
+ meter := &telemetryObservations{}
+ e.EnableAnalytics(meter)
+ prevA, prevP, prevO := analyticsSink.Load(), protectionSink.Load(), assetOwnersSource.Load()
+ prevW := armedChangeWatch.Load()
+ t.Cleanup(func() {
+ analyticsSink.Store(prevA)
+ protectionSink.Store(prevP)
+ assetOwnersSource.Store(prevO)
+ armedChangeWatch.Store(prevW)
+ recordOnlyLast.reset()
+ })
+ recordOnlyLast.reset()
+ setAnalyticsSink(e.SetAnalyticsOn)
+ setProtectionSink(e.SetProtectionRecordOnly)
+ setAssetOwnersSource(func() []registry.Route {
+ return []registry.Route{{Account: "bdk", App: "cart"}}
+ })
+ armedChangeWatch.Store(nil)
+ if armed {
+ armedChangeWatch.Store(release.NewChangeWatch(s))
+ }
+ setOn := func(on bool) {
+ t.Helper()
+ if err := telemetry.Update(ctx, s, telemetry.AppKey("bab", "shop"), func(st *telemetry.Settings) {
+ st.Analytics = &telemetry.Analytics{Count: telemetry.CountOff}
+ st.Protection = nil
+ if on {
+ st.Analytics.Count = telemetry.CountEdge
+ st.Protection = &telemetry.Protection{Mode: telemetry.ProtectRecordOnly}
+ }
+ }); err != nil {
+ t.Fatal(err)
+ }
+ }
+ setOn(false)
+ // The far asset read follows shop's read, so the older pass holds
+ // shop's off values while the newer pass reads and publishes on.
+ blocked := &telemetryBlockingStore{
+ Store: s, key: telemetry.AppKey("bdk", "cart"),
+ entered: make(chan struct{}), resume: make(chan struct{}),
+ }
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ telemetryPass(ctx, blocked, reg)
+ }()
+ defer func() {
+ cancel()
+ <-done
+ }()
+ <-blocked.entered
+ setOn(true)
+ if armed {
+ // Replacing the watch gives the newer pass its own reader;
+ // publication ordering must outlive either reader's mutex.
+ armedChangeWatch.Store(release.NewChangeWatch(s))
+ }
+ telemetryPass(ctx, s, reg)
+ assertOn := func() {
+ t.Helper()
+ w := httptest.NewRecorder()
+ r := httptest.NewRequest("GET", "http://shop.example.com/.env", nil)
+ n := len(meter.obs)
+ e.ServeHTTP(w, r)
+ if w.Code != http.StatusNotFound || w.Body.String() != "404 page not found\n" || w.Header().Get("X-Carlos-Ref") != "" {
+ t.Errorf("record-only request = %d %q, want static 404 without a protection reference", w.Code, w.Body.String())
+ }
+ if len(meter.obs) != n+1 {
+ t.Errorf("analytics observations = %d, want %d", len(meter.obs), n+1)
+ }
+ recordOnlyLast.mu.Lock()
+ remembered := recordOnlyLast.m["bab/shop"]
+ recordOnlyLast.mu.Unlock()
+ if !remembered {
+ t.Error("remembered record-only value is off, want on")
+ }
+ }
+ assertOn()
+ close(blocked.resume)
+ <-done
+ assertOn()
+ // A failed read must reuse the newer remembered value too.
+ armedChangeWatch.Store(nil)
+ failed := &keysFailingStore{ciCountingStore: s}
+ failed.failing(telemetry.AppKey("bab", "shop"))
+ telemetryPass(ctx, failed, reg)
+ w := httptest.NewRecorder()
+ e.ServeHTTP(w, httptest.NewRequest("GET", "http://shop.example.com/.env", nil))
+ if w.Code != http.StatusNotFound || w.Body.String() != "404 page not found\n" || w.Header().Get("X-Carlos-Ref") != "" {
+ t.Errorf("failed read undid record-only: %d %q", w.Code, w.Body.String())
+ }
+ })
+ }
+}
+
func TestTelemetrySwitchesResolvePerRoute(t *testing.T) {
ctx := context.Background()
s := release.NewDirStore(t.TempDir())