carlos / platform Public

db forward round 1, fix 2 (Astra P1): replica-derived detail waits for check B

Every ending that carries anything derived from the replica (a failed
restore's error text, the restored size and cap, the oldest point kept,
nothing-replicated, no-room, a worker's check-failed, changed-twice) now
runs check B first (afterCheckB). When the record changed after check A,
the details are discarded and the read ends refused with the identity code
alone: a requester never learns anything about another instance's replica
read under a reused prefix.

Test first: TestReplicaDetailWaitsForCheckB (T9 extended): a recreated
incarnation plus a failing restore whose error names a schema object, plus
too-large, before-history, nothing-replicated, no-room and changed-twice;
each ended with that detail before the fix.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5e281041517563b9943d6cf61eda7e644c5622af parent 4f8e368
2 files changed, +116 −8
  • internal/dbreads/read.go +32 −8
  • internal/dbreads/read_test.go +84 −0
diff --git a/internal/dbreads/read.go b/internal/dbreads/read.go
index c6b9d1c41..0f3af221b 100644
--- a/internal/dbreads/read.go
+++ b/internal/dbreads/read.go
@@ -93,7 +93,29 @@ func (a *Agent) read(ctx context.Context, req Request) Result {
return res
}
}
- return a.ended(pub, req.ID, StateFailed, CodeChangedTwice, Summary{}, 0)
+ // "Changed twice" is itself the replica's word: it waits for check B
+ // like every other replica-derived ending (Astra round 1, finding 2).
+ res, _ := a.afterCheckB(ctx, req, pub, CodeChangedTwice, Summary{})
+ return res
+}
+
+// afterCheckB is how every ending that carries anything derived from the
+// replica leaves a read: a failed restore's error text, the restored size,
+// the oldest point kept, "nothing replicated", "no room", "changed twice".
+// Check B runs first (spec §8.2), and when it fails the details are
+// discarded: the read ends refused with the identity code alone, so a
+// requester never learns anything about another instance's replica read
+// under a reused prefix (Astra round 1, finding 2).
+func (a *Agent) afterCheckB(ctx context.Context, req Request, pub *ecdh.PublicKey, code string, s Summary) (Result, bool) {
+ _, bcode, err := a.check(ctx, req)
+ switch {
+ case err != nil:
+ a.logf("dbreads: %s: check B before a %s ending: %v", req.ID, code, err)
+ return a.ended(pub, req.ID, StateFailed, CodeInterrupted, Summary{}, 0), false
+ case bcode != "":
+ return a.ended(pub, req.ID, StateRefused, bcode, Summary{}, 0), false
+ }
+ return a.ended(pub, req.ID, StateFailed, code, s, 0), false
}
// settlePoll is how often a retry rereads the unit listing while it waits.
@@ -107,6 +129,8 @@ func (a *Agent) attempt(ctx context.Context, req Request, pub *ecdh.PublicKey, a
fail := func(code string, s Summary) (Result, bool) {
return a.ended(pub, req.ID, StateFailed, code, s, 0), false
}
+ // failB is fail for an ending derived from the replica: check B first.
+ failB := func(code string, s Summary) (Result, bool) { return a.afterCheckB(ctx, req, pub, code, s) }
refuse := func(code string) (Result, bool) {
return a.ended(pub, req.ID, StateRefused, code, Summary{}, 0), false
}
@@ -131,11 +155,11 @@ func (a *Agent) attempt(ctx context.Context, req Request, pub *ecdh.PublicKey, a
var before *dbdoor.BeforeHistoryError
switch {
case errors.Is(err, dbdoor.ErrNothingReplicated):
- return fail(CodeNothingReplicated, Summary{})
+ return failB(CodeNothingReplicated, Summary{})
case errors.As(err, &before):
- return fail(CodeHistoryGone, Summary{Oldest: before.Oldest})
+ return failB(CodeHistoryGone, Summary{Oldest: before.Oldest})
case errors.Is(err, dbdoor.ErrHistoryGone):
- return fail(CodeHistoryGone, Summary{})
+ return failB(CodeHistoryGone, Summary{})
case errors.Is(err, os.ErrNotExist):
return Result{}, true
case err != nil:
@@ -143,10 +167,10 @@ func (a *Agent) attempt(ctx context.Context, req Request, pub *ecdh.PublicKey, a
return fail(CodeInterrupted, Summary{})
}
if plan.RestoredBytes > a.cfg.MaxRestored {
- return fail(CodeTooLarge, Summary{Restored: plan.RestoredBytes, Cap: a.cfg.MaxRestored})
+ return failB(CodeTooLarge, Summary{Restored: plan.RestoredBytes, Cap: a.cfg.MaxRestored})
}
if err := a.adm.grow(req.ID, plan.LTXBytes+plan.RestoredBytes); err != nil {
- return fail(CodeNoRoom, Summary{})
+ return failB(CodeNoRoom, Summary{})
}
conn, err := a.dial(pctx)
if err != nil {
@@ -188,11 +212,11 @@ func (a *Agent) attempt(ctx context.Context, req Request, pub *ecdh.PublicKey, a
if e.Code != CodeTooLarge {
e.Code = CodeCheckFailed
}
- return fail(e.Code, Summary{Error: e.Error, Restored: plan.RestoredBytes, Cap: a.cfg.MaxRestored})
+ return failB(e.Code, Summary{Error: e.Error, Restored: plan.RestoredBytes, Cap: a.cfg.MaxRestored})
}
var ready readyMsg
if typ != frameReady || json.Unmarshal(p, &ready) != nil || ready.TXID != plan.TXID {
- return fail(CodeCheckFailed, Summary{})
+ return failB(CodeCheckFailed, Summary{})
}
if _, code, err := a.check(pctx, req); err != nil || code != "" { // check B
writeFrame(conn, frameStop, nil)
diff --git a/internal/dbreads/read_test.go b/internal/dbreads/read_test.go
index 95d16bb75..b478103d7 100644
--- a/internal/dbreads/read_test.go
+++ b/internal/dbreads/read_test.go
@@ -9,6 +9,7 @@ import (
"errors"
"io"
"os"
+ "reflect"
"strings"
"sync"
"sync/atomic"
@@ -424,3 +425,86 @@ func TestOwnerDeadlineEndsAHungRead(t *testing.T) {
t.Fatalf("result %+v", r)
}
}
+
+// Astra round 1, finding 2 (T9 extended): every replica-derived ending (a
+// failed restore's error text, the restored size, the oldest point kept,
+// "nothing replicated", "no room", "changed twice") waits for check B. When
+// the record changed after check A, the read ends refused record-changed
+// and its sealed summary carries none of that replica's details.
+func TestReplicaDetailWaitsForCheckB(t *testing.T) {
+ t.Parallel()
+ recreate := func(fx *fixture) { r := fx.record(); r.CreatedAt = r.CreatedAt.Add(time.Hour); fx.putRecord(r) }
+ for name, c := range map[string]struct {
+ batches int
+ at string
+ vanish int
+ mut func(*AgentConfig, *inProcWorkers)
+ }{
+ "a failing restore": {batches: 2, mut: func(_ *AgentConfig, w *inProcWorkers) {
+ w.opt.Restore = func(context.Context, litestream.ReplicaClient, string, uint64) error {
+ return errors.New("malformed database schema (secret_customer_ledger)")
+ }
+ }},
+ "too large": {batches: 2, mut: func(c *AgentConfig, _ *inProcWorkers) { c.MaxRestored = 1024 }},
+ "before the history": {batches: 2, at: "2000-01-01T00:00:00Z"},
+ "nothing replicated": {batches: 0},
+ "no room": {batches: 2, mut: func(c *AgentConfig, _ *inProcWorkers) {
+ calls := 0
+ c.Free = func() (uint64, uint64, error) {
+ calls++
+ if calls == 1 {
+ return 11 << 30, 20 << 30, nil
+ }
+ return 5<<30 + 1, 20 << 30, nil
+ }
+ }},
+ "changed twice": {batches: 2, vanish: 2},
+ } {
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+ fx := newFixture(t, c.batches)
+ var rd dbdoor.ReplicaReader = &stageHook{ReplicaReader: lsfile.NewReplicaClient(fx.replica), stage: "planning", change: func() { recreate(fx) }}
+ if c.vanish > 0 {
+ // The record changes during the second attempt's download,
+ // after that attempt's check A: only a check before
+ // "changed twice" can see it.
+ rd = &vanishThenChange{ReplicaReader: &vanishing{ReplicaReader: lsfile.NewReplicaClient(fx.replica), times: c.vanish}, at: 2, change: func() { recreate(fx) }}
+ }
+ a, _ := fx.agent("uk-1", func(cfg *AgentConfig, w *inProcWorkers) {
+ cfg.OpenReplica = func(context.Context, string) (dbdoor.ReplicaReader, error) { return rd, nil }
+ if c.mut != nil {
+ c.mut(cfg, w)
+ }
+ })
+ _, s, r := serveOne(t, a, fx, fx.request("SELECT 1", c.at))
+ if r.State != StateRefused || r.Code != CodeRecordChanged {
+ t.Fatalf("result %+v, want refused %s", r, CodeRecordChanged)
+ }
+ if !reflect.DeepEqual(s, Summary{}) {
+ t.Fatalf("the refused read's summary carries the replica's details: %+v", s)
+ }
+ })
+ }
+}
+
+// vanishThenChange runs change on the at'th whole-file open, before the
+// open answers; over vanishing, that open is one that vanishes.
+type vanishThenChange struct {
+ dbdoor.ReplicaReader
+ mu sync.Mutex
+ n, at int
+ change func()
+}
+
+func (v *vanishThenChange) OpenLTXFile(ctx context.Context, level int, lo, hi ltx.TXID, off, size int64) (io.ReadCloser, error) {
+ if off == 0 && size == 0 {
+ v.mu.Lock()
+ v.n++
+ fire := v.n == v.at
+ v.mu.Unlock()
+ if fire {
+ v.change()
+ }
+ }
+ return v.ReplicaReader.OpenLTXFile(ctx, level, lo, hi, off, size)
+}