carlos / platform Public

console: the forwarded read's evidence rule (T14) (plan 2, Task 4)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 673d795cb0a84bba9a60aa7a188e386ba77fc1a7 parent 1e1f08c
2 files changed, +130 −0
  • internal/console/apidbforward.go +34 −0
  • internal/console/apidbforward_test.go +96 −0
diff --git a/internal/console/apidbforward.go b/internal/console/apidbforward.go
index 6046e8849..ecfa7ff31 100644
--- a/internal/console/apidbforward.go
+++ b/internal/console/apidbforward.go
@@ -7,6 +7,7 @@ package console
import (
"crypto/ed25519"
+ "slices"
"time"
"github.com/carlosframework/platform/internal/dbreads"
@@ -54,3 +55,36 @@ func (s *Server) dbForwardSigner() (release.Signer, bool) {
}
return release.RootSigner(s.cfg.Key), true
}
+
+const (
+ dbForwardNone = "none" // DBF-1: no capable, non-draining box publishes in the region
+ dbForwardDead = "dead" // DBF-2: capable boxes, none live
+)
+
+// dbForwardEvidence is spec §7.1's rule. It reads Complete and
+// BoxesComplete, never the error alone: ReadLiveSnapshot answers a nil
+// error when status snapshots cannot be loaded and reports the gap in those
+// flags (codex 10). Incomplete evidence comes first and wins over both
+// refusals: the request is written and the pickup window decides.
+func dbForwardEvidence(snap edgesSnapshot, err error, region string) string {
+ if err != nil || !snap.Complete || !snap.BoxesComplete {
+ return ""
+ }
+ capable, live := 0, 0
+ for _, f := range snap.Files {
+ if f.Placement != region || f.Draining || !slices.Contains(f.Capabilities, dbreads.Capability) {
+ continue
+ }
+ capable++
+ if f.Fresh {
+ live++
+ }
+ }
+ switch {
+ case capable == 0:
+ return dbForwardNone
+ case live == 0:
+ return dbForwardDead
+ }
+ return ""
+}
diff --git a/internal/console/apidbforward_test.go b/internal/console/apidbforward_test.go
index 285b7acfe..dbedf07fb 100644
--- a/internal/console/apidbforward_test.go
+++ b/internal/console/apidbforward_test.go
@@ -1,10 +1,15 @@
package console
import (
+ "context"
"crypto/ed25519"
"crypto/rand"
+ "encoding/json"
"testing"
"time"
+
+ "github.com/carlosframework/platform/internal/dbreads"
+ "github.com/carlosframework/platform/internal/release"
)
// The console forwards only with the switch on, a root key whose public
@@ -37,3 +42,94 @@ func TestNewHTTPServerKeepsTheConsolesTimeouts(t *testing.T) {
t.Fatalf("%+v", srv)
}
}
+
+// putFixtureObject creates key, or replaces it under its current etag: a
+// fixture may republish a box's files, as a box does every pass. Protocol
+// writes stay create-only; this helper is for fixtures only (plan review
+// finding 6: DirStore.Put refuses a create over an existing key).
+func putFixtureObject(t *testing.T, s release.Store, key string, body []byte) {
+ t.Helper()
+ etag := ""
+ if _, cur, err := s.Get(context.Background(), key); err == nil {
+ etag = cur
+ }
+ if _, err := s.Put(context.Background(), key, body, etag); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// putEdgesFile writes control/edges/<box>.json as a box publishes it.
+func putEdgesFile(t *testing.T, s release.Store, box, placement string, caps []string, draining bool) {
+ t.Helper()
+ body, _ := json.Marshal(map[string]any{"v": 1, "box": box, "eip": "203.0.113.10", "placement": placement,
+ "capabilities": caps, "draining": draining, "hosts": map[string]any{}})
+ putFixtureObject(t, s, "control/edges/"+box+".json", body)
+}
+
+// putBoxStatus writes status/boxes/<box>.json generated at at.
+func putBoxStatus(t *testing.T, s release.Store, box string, at time.Time) {
+ t.Helper()
+ body, _ := json.Marshal(map[string]any{"generated_at": at.UTC(), "label": box})
+ putFixtureObject(t, s, "status/boxes/"+box+".json", body)
+}
+
+// T14 (codex 10): only complete evidence, both flags, refuses up front.
+// Incomplete evidence of any kind admits the read and lets the pickup
+// window decide. Mutation: check Complete and not BoxesComplete (red: the
+// missing, unmatched and unreadable status cases refuse).
+func TestDBForwardEvidenceRefusesOnlyOnCompleteEvidence(t *testing.T) {
+ t.Parallel()
+ now := time.Now()
+ capable := []string{dbreads.Capability}
+ for name, c := range map[string]struct {
+ setup func(t *testing.T, s release.Store)
+ want string
+ }{
+ "a capable live box": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", capable, false)
+ putBoxStatus(t, s, "uk-1", now)
+ }, ""},
+ "no capable box": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", nil, false)
+ putBoxStatus(t, s, "uk-1", now)
+ }, dbForwardNone},
+ "a capable box that is draining": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", capable, true)
+ putBoxStatus(t, s, "uk-1", now)
+ }, dbForwardNone},
+ "capable but stale": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", capable, false)
+ putBoxStatus(t, s, "uk-1", now.Add(-time.Hour))
+ }, dbForwardDead},
+ "capable in another region only": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "ie-1", "ie", capable, false)
+ putBoxStatus(t, s, "ie-1", now)
+ }, dbForwardNone},
+ "a status snapshot missing": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", nil, false)
+ }, ""},
+ "an edges file unreadable": {func(t *testing.T, s release.Store) {
+ s.Put(context.Background(), "control/edges/uk-1.json", []byte("{not json"), "")
+ putBoxStatus(t, s, "uk-1", now)
+ }, ""},
+ "a fresh snapshot with no edges file": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", nil, false)
+ putBoxStatus(t, s, "uk-1", now)
+ putBoxStatus(t, s, "uk-2", now)
+ }, ""},
+ "status snapshots unreadable": {func(t *testing.T, s release.Store) {
+ putEdgesFile(t, s, "uk-1", "uk", nil, false)
+ s.Put(context.Background(), "status/boxes/uk-1.json", []byte("{not json"), "")
+ }, ""},
+ } {
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+ s := release.NewDirStore(t.TempDir())
+ c.setup(t, s)
+ snap, err := readLiveEdgesSnapshot(context.Background(), s, now)
+ if got := dbForwardEvidence(snap, err, "uk"); got != c.want {
+ t.Fatalf("evidence %q, want %q (complete %v, boxes %v)", got, c.want, snap.Complete, snap.BoxesComplete)
+ }
+ })
+ }
+}