carlos / platform Public

console: the db door marks a capable cross-region instance (DBF-1, DBF-2, DBF-14 wired, pending copy) (plan 2, Task 5)

The three sentences are the copy record's pending text, wired behind
CARLOS_DB_FORWARD (off unless set). The copy gate's new
dbForwardCopyWired list pins them to the record until the copy review
moves them to dbForwardCopyBuilt.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 23ae5c83210ff16662f9d7750f4759eb6c8ba189 parent 673d795
5 files changed, +237 −1
  • approved_db_forward_copy_test.go +79 −0
  • cmd/console/dbforward_test.go +18 −0
  • internal/console/apidb.go +3 −1
  • internal/console/apidbforward.go +48 −0
  • internal/console/apidbforward_test.go +89 −0
diff --git a/approved_db_forward_copy_test.go b/approved_db_forward_copy_test.go
index f62c5f188..3d0a3b0b9 100644
--- a/approved_db_forward_copy_test.go
+++ b/approved_db_forward_copy_test.go
@@ -2,6 +2,7 @@
package main
import (
+ "fmt"
"os"
"regexp"
"strconv"
@@ -38,6 +39,18 @@ var dbForwardCopyIDs = func() []string {
// approved the row.
var dbForwardCopyBuilt = []string{}
+// dbForwardCopyWired is every id whose PENDING text the code already
+// carries, behind the console's CARLOS_DB_FORWARD switch, which is off
+// unless a deployment sets it (plan 2's build, ruling R1: Paul asked for the
+// feature to work first, and the copy review comes separately). This is the
+// moves record's D13 rule, a pending sentence only behind a switch that is
+// off by default, in list form: every DBF sentence in code is reached only
+// from a console that forwards, so none reaches a person on a deployment
+// that has not turned forwarding on. The copy review moves each id from here
+// to dbForwardCopyBuilt, applying Paul's edits in the same commit. Docs rows
+// are public the moment the site syncs, so they are never wired.
+var dbForwardCopyWired = []string{"DBF-1", "DBF-2", "DBF-14"}
+
func mustParseDBForwardRecord(t *testing.T) []copyRow {
t.Helper()
raw, err := os.ReadFile(dbForwardCopyRecordPath)
@@ -70,3 +83,69 @@ func TestDBForwardBuiltCopyIsApplied(t *testing.T) {
}
reportAll(t, checkRunApplied(rows, "."))
}
+
+// checkDBForwardWired: a wired id is a pending row the record has, named
+// once, not built as well, not a docs row, and its text is in the file it
+// names (so the review's edits must reach the code).
+func checkDBForwardWired(rows []copyRow, wired, built []string) []string {
+ byID := map[string]copyRow{}
+ for _, r := range rows {
+ byID[r.ID] = r
+ }
+ isBuilt := map[string]bool{}
+ for _, id := range built {
+ isBuilt[id] = true
+ }
+ var out []string
+ var apply []copyRow
+ seen := map[string]bool{}
+ for _, id := range wired {
+ if seen[id] {
+ out = append(out, fmt.Sprintf("dbForwardCopyWired names %s twice", id))
+ }
+ seen[id] = true
+ r, ok := byID[id]
+ switch {
+ case !ok:
+ out = append(out, fmt.Sprintf("dbForwardCopyWired names %s, which the record has no row for", id))
+ continue
+ case isBuilt[id]:
+ out = append(out, fmt.Sprintf("%s is both wired and built: an approved row moves to dbForwardCopyBuilt", id))
+ case r.Status != "pending":
+ out = append(out, fmt.Sprintf("%s is %s: an approved row moves from dbForwardCopyWired to dbForwardCopyBuilt", id, r.Status))
+ case strings.HasPrefix(r.Path(), "docs/"):
+ out = append(out, fmt.Sprintf("%s is a docs row: pending docs text is never wired", id))
+ }
+ r.Status = "approved" // checkMovesApplied reads approved rows only
+ apply = append(apply, r)
+ }
+ return append(out, checkMovesApplied(apply, ".")...)
+}
+
+func TestDBForwardWiredCopyIsPendingAndApplied(t *testing.T) {
+ rows := mustParseDBForwardRecord(t)
+ for i := range rows {
+ rows[i].Text = strings.ReplaceAll(rows[i].Text, `\n`, "\n")
+ }
+ reportAll(t, checkDBForwardWired(rows, dbForwardCopyWired, dbForwardCopyBuilt))
+}
+
+// The gate bites: a wired row whose text is not in its file, an approved
+// row left wired, and a docs row wired are each refused.
+func TestDBForwardWiredGateRefuses(t *testing.T) {
+ rows := []copyRow{
+ {"DBF-1", "internal/console/apidbforward.go", "nothing like this sentence is in the file.", "pending"},
+ {"DBF-2", "internal/console/apidbforward.go", "x", "approved"},
+ {"DBF-30", "docs/site/cli.md", "x", "pending"},
+ }
+ got := checkDBForwardWired(rows, []string{"DBF-1", "DBF-2", "DBF-30"}, nil)
+ for _, want := range []string{"does not contain the approved text", "DBF-2 is approved", "DBF-30 is a docs row"} {
+ found := false
+ for _, g := range got {
+ found = found || strings.Contains(g, want)
+ }
+ if !found {
+ t.Errorf("no problem saying %q in %q", want, got)
+ }
+ }
+}
diff --git a/cmd/console/dbforward_test.go b/cmd/console/dbforward_test.go
new file mode 100644
index 000000000..15dfa6c10
--- /dev/null
+++ b/cmd/console/dbforward_test.go
@@ -0,0 +1,18 @@
+package main
+
+import "testing"
+
+// Forwarding is off unless CARLOS_DB_FORWARD is exactly 1: the switch the
+// pending DBF copy waits behind (approved_db_forward_copy_test.go,
+// dbForwardCopyWired) and the condition for answering byte-identically to
+// main (db region forward spec §9.2).
+func TestDBForwardIsOffUnlessSet(t *testing.T) {
+ t.Setenv("CARLOS_DB_FORWARD", "")
+ if dbForward() {
+ t.Fatal("forwarding is on with CARLOS_DB_FORWARD unset")
+ }
+ t.Setenv("CARLOS_DB_FORWARD", "1")
+ if !dbForward() {
+ t.Fatal("forwarding is off with CARLOS_DB_FORWARD=1")
+ }
+}
diff --git a/internal/console/apidb.go b/internal/console/apidb.go
index b4c7eef47..e9c02278c 100644
--- a/internal/console/apidb.go
+++ b/internal/console/apidb.go
@@ -142,7 +142,9 @@ func (s *Server) dbGate(w http.ResponseWriter, r *http.Request) (dbRequest, bool
"%s runs in the pool %s, so its data is on the pool's own server. carlos db cannot read it.", host, src.rec.Pool))
return dbRequest{}, false
case errors.Is(err, ErrResidency):
- jsonError(w, http.StatusConflict, s.dbResidencyRefusal(src.rec))
+ // Main's sentence unless this console forwards (db region forward
+ // spec §5.5 step 1): then DBF-1, DBF-2 or the forward marker.
+ s.dbResidency(w, r, src.rec)
return dbRequest{}, false
case errors.Is(err, errSourceClaim):
g := dbRequest{acct: acct, actor: actor, app: app, host: host}
diff --git a/internal/console/apidbforward.go b/internal/console/apidbforward.go
index ecfa7ff31..c2b0094ab 100644
--- a/internal/console/apidbforward.go
+++ b/internal/console/apidbforward.go
@@ -7,10 +7,15 @@ package console
import (
"crypto/ed25519"
+ "fmt"
+ "net/http"
"slices"
"time"
+ "github.com/carlosframework/platform/internal/dbdoor"
"github.com/carlosframework/platform/internal/dbreads"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
)
@@ -88,3 +93,46 @@ func dbForwardEvidence(snap edgesSnapshot, err error, region string) string {
}
return ""
}
+
+// The console's forwarded-read sentences (copy record DBF-1, DBF-2, DBF-14).
+// PENDING copy review: wired behind CARLOS_DB_FORWARD, which is off unless a
+// deployment sets it (approved_db_forward_copy_test.go, dbForwardCopyWired).
+const (
+ dbfNoCapable = "%s keeps its data in region %s. no server in that region can read databases yet, so carlos db cannot read it for now."
+ dbfNoneLive = "%s keeps its data in region %s. no server in that region is answering right now. try again in a few minutes."
+ dbfUpdateCarlos = "%s keeps its data in region %s. this version of carlos cannot read it from there. run carlos update, then try again."
+)
+
+// forwardable is whether this console forwards reads of rec: it can sign,
+// and rec is in a named platform region (a fleet placement keeps its own
+// sentence, and a home-region record seen from a regional console has no
+// region to name).
+func (s *Server) forwardable(rec provision.Record) bool {
+ _, fleet := registry.FleetPlacement(rec.Region)
+ _, ok := s.dbForwardSigner()
+ return ok && !fleet && dbreads.ValidRegion(rec.Region)
+}
+
+// dbResidency answers a record homed outside this console's region (spec
+// §5.5 step 1, §9.2). Not forwardable: main's refusal, byte for byte.
+// Forwardable: DBF-1 or DBF-2 on complete evidence, else the forward marker,
+// whose sentence an old CLI prints (DBF-14).
+func (s *Server) dbResidency(w http.ResponseWriter, r *http.Request, rec provision.Record) {
+ if !s.forwardable(rec) {
+ jsonError(w, http.StatusConflict, s.dbResidencyRefusal(rec))
+ return
+ }
+ snap, err := s.liveEdges(r.Context())
+ switch dbForwardEvidence(snap, err, rec.Region) {
+ case dbForwardNone:
+ jsonError(w, http.StatusConflict, fmt.Sprintf(dbfNoCapable, rec.Host, rec.Region))
+ return
+ case dbForwardDead:
+ jsonError(w, http.StatusConflict, fmt.Sprintf(dbfNoneLive, rec.Host, rec.Region))
+ return
+ }
+ writeJSON(w, http.StatusConflict, dbdoor.ForwardBody{
+ Error: fmt.Sprintf(dbfUpdateCarlos, rec.Host, rec.Region), Forward: 1,
+ Region: rec.Region, ForwardOps: []string{dbreads.OpQuery},
+ })
+}
diff --git a/internal/console/apidbforward_test.go b/internal/console/apidbforward_test.go
index dbedf07fb..c375948a0 100644
--- a/internal/console/apidbforward_test.go
+++ b/internal/console/apidbforward_test.go
@@ -5,10 +5,16 @@ import (
"crypto/ed25519"
"crypto/rand"
"encoding/json"
+ "fmt"
+ "net/http"
+ "slices"
+ "strings"
"testing"
"time"
+ "github.com/carlosframework/platform/internal/dbdoor"
"github.com/carlosframework/platform/internal/dbreads"
+ "github.com/carlosframework/platform/internal/provision"
"github.com/carlosframework/platform/internal/release"
)
@@ -133,3 +139,86 @@ func TestDBForwardEvidenceRefusesOnlyOnCompleteEvidence(t *testing.T) {
})
}
}
+
+const dbfHost = "shop.example.com"
+
+// dbfFixture is a console in ie with forwarding on or off, a member, and
+// shop.example.com's record in uk with a capable, live uk box published.
+type dbfFixture struct {
+ *consoleFixture
+ member string
+}
+
+func newDBFFixture(t *testing.T, forward bool) *dbfFixture {
+ t.Helper()
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.DBForward = forward
+ fx.srv.cfg.Region = "ie"
+ fx.srv.cfg.HibernateBucket = "data-ie"
+ if _, err := fx.st.CreateAppIn(OpsAccountID, "shop", "paul@keymail.dev", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ if err := fx.st.AddMember(OpsAccountID, "member@example.com", RoleMember, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ member := cliToken(t, fx, "member@example.com", "code-dbf")
+ putShopRecord(t, fx, provision.Record{Host: dbfHost, Region: "uk", CreatedAt: time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC)})
+ putEdgesFile(t, fx.rel, "uk-1", "uk", []string{dbreads.Capability}, false)
+ putBoxStatus(t, fx.rel, "uk-1", time.Now())
+ return &dbfFixture{consoleFixture: fx, member: member}
+}
+
+func (fx *dbfFixture) listURL() string {
+ return fx.ts.URL + "/api/cli/apps/" + release.OpsAccount + "/shop/db/" + dbfHost + "/ltx/9"
+}
+
+// T8, the console's half: forwarding off is main's answer byte for byte.
+// The want is main's dbResidencyRefusal for this fixture, confirmed against
+// main's code before this branch changed dbGate.
+func TestListDoorWithForwardingOffIsMains(t *testing.T) {
+ fx := newDBFFixture(t, false)
+ status, _, body := rawReq(t, http.MethodGet, fx.listURL(), fx.member, "")
+ want := `{"error":"shop.example.com keeps its data in region uk. This console can only read data kept in region ie."}`
+ if status != http.StatusConflict || strings.TrimSpace(body) != want {
+ t.Fatalf("%d %s", status, body)
+ }
+}
+
+func TestListDoorMarksACapableRegion(t *testing.T) {
+ fx := newDBFFixture(t, true)
+ status, _, body := rawReq(t, http.MethodGet, fx.listURL(), fx.member, "")
+ var got dbdoor.ForwardBody
+ json.Unmarshal([]byte(body), &got)
+ if status != http.StatusConflict || got.Forward != 1 || got.Region != "uk" ||
+ !slices.Equal(got.ForwardOps, []string{dbreads.OpQuery}) ||
+ got.Error != fmt.Sprintf(dbfUpdateCarlos, dbfHost, "uk") {
+ t.Fatalf("%d %s", status, body)
+ }
+}
+
+func TestListDoorRefusesOnCompleteEvidence(t *testing.T) {
+ fx := newDBFFixture(t, true)
+ putEdgesFile(t, fx.rel, "uk-1", "uk", nil, false)
+ status, _, body := rawReq(t, http.MethodGet, fx.listURL(), fx.member, "")
+ if status != http.StatusConflict || !strings.Contains(body, fmt.Sprintf(dbfNoCapable, dbfHost, "uk")) || strings.Contains(body, `"forward"`) {
+ t.Fatalf("no capable box: %d %s", status, body)
+ }
+ putEdgesFile(t, fx.rel, "uk-2", "uk", []string{dbreads.Capability}, false)
+ putBoxStatus(t, fx.rel, "uk-2", time.Now().Add(-time.Hour))
+ status, _, body = rawReq(t, http.MethodGet, fx.listURL(), fx.member, "")
+ if status != http.StatusConflict || !strings.Contains(body, fmt.Sprintf(dbfNoneLive, dbfHost, "uk")) {
+ t.Fatalf("none live: %d %s", status, body)
+ }
+}
+
+// Review Focus 5: a home-region record seen from a regional console keeps
+// main's sentence; there is no region to forward to by name.
+func TestListDoorKeepsMainsSentenceForTheHomeRegion(t *testing.T) {
+ fx := newDBFFixture(t, true)
+ putShopRecord(t, fx.consoleFixture, provision.Record{Host: "home.example.com", CreatedAt: time.Now().UTC()})
+ u := fx.ts.URL + "/api/cli/apps/" + release.OpsAccount + "/shop/db/home.example.com/ltx/9"
+ status, _, body := rawReq(t, http.MethodGet, u, fx.member, "")
+ if status != http.StatusConflict || strings.Contains(body, `"forward"`) || !strings.Contains(body, "This console can only read data kept in") {
+ t.Fatalf("%d %s", status, body)
+ }
+}