carlos / platform Public

db forward task 3: dbreads ids, db-reads/ keys and the spec's limits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 2e30753b2ba87531e93ea4ba83617446fbada425 parent 87c2d4d
3 files changed, +267 −0
  • internal/dbreads/ids.go +114 −0
  • internal/dbreads/ids_test.go +91 −0
  • internal/dbreads/limits.go +62 −0
diff --git a/internal/dbreads/ids.go b/internal/dbreads/ids.go
new file mode 100644
index 000000000..283d4a34d
--- /dev/null
+++ b/internal/dbreads/ids.go
@@ -0,0 +1,114 @@
+package dbreads
+
+import (
+ "crypto/rand"
+ "encoding/base32"
+ "fmt"
+ "io"
+ "regexp"
+ "strconv"
+ "strings"
+ "time"
+)
+
+var idEnc = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
+
+var (
+ idShape = regexp.MustCompile(`^dbr_[a-z2-7]{26}$`)
+ regionShape = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`)
+ hourShape = regexp.MustCompile(`^[0-9]{10}$`)
+)
+
+// NewID mints an id from r: "dbr_" and 26 lowercase base32 characters, 128
+// random bits, the way runs.NewID mints run ids. Production mints with MintID.
+func NewID(r io.Reader) (string, error) {
+ var b [16]byte
+ if _, err := io.ReadFull(r, b[:]); err != nil {
+ return "", err
+ }
+ return "dbr_" + idEnc.EncodeToString(b[:]), nil
+}
+
+func MintID() (string, error) { return NewID(rand.Reader) }
+
+// ValidID reports a db read id: 30 bytes of [a-z2-7_], one clean path
+// element, and never "queue".
+func ValidID(id string) bool { return idShape.MatchString(id) }
+
+// ValidRegion reports a placement code that can be one key segment. A fleet
+// placement ("fleet/x") is not one: fleet instances never forward.
+func ValidRegion(r string) bool { return regionShape.MatchString(r) }
+
+func RequestKey(id string) string { return Prefix + id + "/request.json" }
+func PickedKey(id string) string { return Prefix + id + "/picked" }
+func OutPrefix(id string) string { return Prefix + id + "/out/" }
+func ResultKey(id string) string { return Prefix + id + "/result.json" }
+func CancelKey(id string) string { return Prefix + id + "/cancel" }
+
+// ChunkKey is out/<seq>, zero-padded to 8 digits so keys sort in order.
+func ChunkKey(id string, seq int64) string { return OutPrefix(id) + fmt.Sprintf("%08d", seq) }
+
+// ParseChunkKey reads a key ChunkKey made under outPrefix.
+func ParseChunkKey(key, outPrefix string) (int64, bool) {
+ name, ok := strings.CutPrefix(key, outPrefix)
+ if !ok || len(name) != 8 {
+ return 0, false
+ }
+ for i := 0; i < 8; i++ {
+ if name[i] < '0' || name[i] > '9' {
+ return 0, false
+ }
+ }
+ seq, err := strconv.ParseInt(name, 10, 64)
+ if err != nil || seq < 1 {
+ return 0, false
+ }
+ return seq, true
+}
+
+const hourLayout = "2006010215"
+
+func QueueRegionPrefix(region string) string { return Prefix + "queue/" + region + "/" }
+
+func QueueHourPrefix(region string, t time.Time) string {
+ return QueueRegionPrefix(region) + t.UTC().Format(hourLayout) + "/"
+}
+
+// QueueKey is the empty pointer the console writes after the request:
+// db-reads/queue/<region>/<YYYYMMDDHH>/<unix>-<id>. A box can skip an
+// out-of-window entry from its key alone (spec §5.4).
+func QueueKey(region string, issued time.Time, id string) string {
+ return QueueHourPrefix(region, issued) + strconv.FormatInt(issued.Unix(), 10) + "-" + id
+}
+
+// QueueEntry is a parsed queue key.
+type QueueEntry struct {
+ Region, Hour, ID string
+ Issued time.Time
+}
+
+func ParseQueueKey(key string) (QueueEntry, bool) {
+ rest, ok := strings.CutPrefix(key, Prefix+"queue/")
+ if !ok {
+ return QueueEntry{}, false
+ }
+ parts := strings.Split(rest, "/")
+ if len(parts) != 3 || !ValidRegion(parts[0]) || !hourShape.MatchString(parts[1]) {
+ return QueueEntry{}, false
+ }
+ unixRaw, id, ok := strings.Cut(parts[2], "-")
+ if !ok || !ValidID(id) {
+ return QueueEntry{}, false
+ }
+ unix, err := strconv.ParseInt(unixRaw, 10, 64)
+ if err != nil || unix <= 0 || strconv.FormatInt(unix, 10) != unixRaw {
+ return QueueEntry{}, false
+ }
+ return QueueEntry{Region: parts[0], Hour: parts[1], ID: id, Issued: time.Unix(unix, 0).UTC()}, true
+}
+
+// InWindow is the box's pickup window: no more than PickupWindow plus Skew
+// old, and no more than Skew in the future (spec §5.5, §5.7).
+func InWindow(issued, now time.Time) bool {
+ return !issued.Before(now.Add(-PickupWindow-Skew)) && !issued.After(now.Add(Skew))
+}
diff --git a/internal/dbreads/ids_test.go b/internal/dbreads/ids_test.go
new file mode 100644
index 000000000..4810c0edc
--- /dev/null
+++ b/internal/dbreads/ids_test.go
@@ -0,0 +1,91 @@
+package dbreads
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestIDsAreDbrAnd26Base32(t *testing.T) {
+ t.Parallel()
+ id, err := NewID(bytes.NewReader(make([]byte, 16)))
+ if err != nil || id != "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa" || !ValidID(id) {
+ t.Fatalf("NewID = %q, %v", id, err)
+ }
+ minted, err := MintID()
+ if err != nil || !ValidID(minted) {
+ t.Fatalf("MintID = %q, %v", minted, err)
+ }
+ for _, bad := range []string{"", "run_aaaaaaaaaaaaaaaaaaaaaaaaaa", "dbr_AAAAAAAAAAAAAAAAAAAAAAAAAA", "dbr_aaaa", "queue", "dbr_aaaaaaaaaaaaaaaaaaaaaaaaa/"} {
+ if ValidID(bad) {
+ t.Errorf("ValidID(%q) = true", bad)
+ }
+ }
+}
+
+func TestKeysLiveUnderOnePrefix(t *testing.T) {
+ t.Parallel()
+ id := "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa"
+ for _, k := range []string{RequestKey(id), PickedKey(id), ChunkKey(id, 1), ResultKey(id), CancelKey(id),
+ QueueKey("uk", time.Date(2026, 10, 22, 9, 0, 5, 0, time.UTC), id)} {
+ if !strings.HasPrefix(k, "db-reads/") {
+ t.Errorf("%s is outside db-reads/", k)
+ }
+ }
+ if got := ChunkKey(id, 12); got != "db-reads/"+id+"/out/00000012" {
+ t.Fatalf("ChunkKey = %s", got)
+ }
+ if seq, ok := ParseChunkKey(ChunkKey(id, 12), OutPrefix(id)); !ok || seq != 12 {
+ t.Fatalf("ParseChunkKey = %d %v", seq, ok)
+ }
+ for _, bad := range []string{OutPrefix(id) + "0000001", OutPrefix(id) + "-0000001", OutPrefix(id) + "00000000", OutPrefix(id) + "0000001x"} {
+ if _, ok := ParseChunkKey(bad, OutPrefix(id)); ok {
+ t.Errorf("ParseChunkKey(%q) accepted", bad)
+ }
+ }
+}
+
+func TestQueueKeyRoundTrips(t *testing.T) {
+ t.Parallel()
+ id := "dbr_bbbbbbbbbbbbbbbbbbbbbbbbbb"
+ issued := time.Date(2026, 10, 22, 10, 59, 50, 0, time.UTC)
+ key := QueueKey("uk", issued, id)
+ if key != "db-reads/queue/uk/2026102210/1792666790-"+id {
+ t.Fatalf("QueueKey = %s", key)
+ }
+ e, ok := ParseQueueKey(key)
+ if !ok || e.Region != "uk" || e.Hour != "2026102210" || !e.Issued.Equal(issued) || e.ID != id {
+ t.Fatalf("ParseQueueKey = %+v %v", e, ok)
+ }
+ for _, bad := range []string{
+ "db-reads/queue/uk/2026102210/x-" + id,
+ "db-reads/queue/uk/20261022/1792666790-" + id,
+ "db-reads/queue/../2026102210/1792666790-" + id,
+ "db-reads/queue/uk/2026102210/01792666790-" + id,
+ "db-reads/queue/uk/2026102210/1792666790-run_bbbbbbbbbbbbbbbbbbbbbbbbbb",
+ } {
+ if _, ok := ParseQueueKey(bad); ok {
+ t.Errorf("ParseQueueKey(%q) accepted", bad)
+ }
+ }
+}
+
+func TestInWindowIs60sPlus60sOfSkew(t *testing.T) {
+ t.Parallel()
+ now := time.Date(2026, 10, 22, 9, 0, 0, 0, time.UTC)
+ for _, c := range []struct {
+ issued time.Time
+ want bool
+ }{
+ {now, true},
+ {now.Add(-120 * time.Second), true},
+ {now.Add(-121 * time.Second), false},
+ {now.Add(60 * time.Second), true},
+ {now.Add(61 * time.Second), false},
+ } {
+ if got := InWindow(c.issued, now); got != c.want {
+ t.Errorf("InWindow(%s) = %v", c.issued.Sub(now), got)
+ }
+ }
+}
diff --git a/internal/dbreads/limits.go b/internal/dbreads/limits.go
new file mode 100644
index 000000000..7a19ca557
--- /dev/null
+++ b/internal/dbreads/limits.go
@@ -0,0 +1,62 @@
+// Package dbreads is a forwarded `carlos db` read (spec
+// docs/superpowers/specs/2026-10-22-db-region-forward-design.md): the wire
+// the console, the box and the CLI share, and the box's half of the work.
+// Every object lives under db-reads/ in the deployment bucket and is a
+// conditional create; the store's lifecycle rule is the only deletion (J2,
+// J4).
+package dbreads
+
+import (
+ "time"
+
+ "github.com/carlosframework/platform/internal/runs"
+)
+
+// Capability is what a box publishes in its edges file while its armed
+// db-reads loop's last readiness probe passed (spec §7.1).
+const Capability = "db-read-1"
+
+// Purpose is the envelope's fixed purpose and the sealing's HKDF info: one
+// string, so nothing else signed by the root or sealed by this package can be
+// mistaken for a db read.
+const Purpose = "carlos-db-read-v1"
+
+// Prefix is the one prefix every db read object lives under.
+const Prefix = "db-reads/"
+
+const (
+ OpQuery = "query"
+ OpPull = "pull" // plan 3
+
+ PickupWindow = 60 * time.Second // from issued_at (§5.7)
+ Skew = 60 * time.Second
+ ConsoleWaitsPicked = 2 * time.Minute
+ PlanBudget = 10 * time.Minute
+ QueryTimeout = 5 * time.Minute
+ OwnerDeadline = 20 * time.Minute
+ OwnerGrace = time.Minute
+ RequestBound = 22 * time.Minute
+ CLIBound = 23 * time.Minute
+ CancelEvery = 5 * time.Second
+ ListEvery = 30 * time.Second
+ ProbeEvery = 5 * time.Minute
+ WorkerExitGrace = 5 * time.Second
+ MaxJitter = 500 * time.Millisecond
+
+ DefaultMaxRestored int64 = 2 << 30 // CARLOS_DB_READ_MAX_BYTES
+ MaxOutputBytes = runs.MaxOutputBytes
+ ChunkBytes = runs.ChunkBytes
+ ChunkEvery = runs.ChunkEvery
+
+ ReadsPerBox = 2
+ MaxConnections = 3 // two reads and one probe (carlos-dbread.socket)
+ OpenPerPerson = 2
+ OpenPerConsole = 16
+
+ DiskFloor int64 = 5 << 30
+ DiskFreePercent = 10
+
+ // MaxSQLBytes keeps a request well inside the root envelope's payload
+ // bound (release.MaxRootEnvelopeBytes/2).
+ MaxSQLBytes = 256 << 10
+)