carlos / platform Public

dbreads: identity bracket as CaptureIdentity/ConfirmIdentity (#898)

Slice 2a task 1. The record check that brackets every forwarded read
moves out of Agent.check into package-level CaptureIdentity (fresh
record, identity computed from it, record-changed or moving codes) and
ConfirmIdentity (capture again, record-changed unless the identity is
the one wanted). Agent.check now calls ConfirmIdentity with no change
in behaviour; the restore drill will share the same code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8a0dd368e9dcecffabf19eeb9b1b4ca401e29c58 parent b358d51
3 files changed, +246 −20
  • internal/dbreads/agent.go +3 −20
  • internal/dbreads/identity.go +65 −0
  • internal/dbreads/identity_test.go +178 −0

CI failed — run details

Intent

Attributed to this commit: 12.6 g CO₂e (1,594,280 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on member-backups-2a, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/dbreads/agent.go b/internal/dbreads/agent.go
index 6d7abe273..cb2345f8c 100644
--- a/internal/dbreads/agent.go
+++ b/internal/dbreads/agent.go
@@ -18,7 +18,6 @@ import (
"github.com/carlosframework/platform/internal/dbdoor"
"github.com/carlosframework/platform/internal/provision"
- "github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
)
@@ -205,26 +204,10 @@ func (a *Agent) dial(ctx context.Context) (net.Conn, error) {
// fresh, and refused unless it is the instance the envelope named, still a
// replicated instance, still in this box's region and not moving. A class,
// pool or fleet change after the console admitted the read is a record
-// change.
+// change. ConfirmIdentity is the check; the restore drill shares it.
func (a *Agent) check(ctx context.Context, req Request) (provision.Record, string, error) {
- rec, err := provision.GetRecord(ctx, a.cfg.Store, req.Account, req.App, req.Host)
- if errors.Is(err, os.ErrNotExist) {
- return provision.Record{}, CodeRecordChanged, nil
- }
- if err != nil {
- return provision.Record{}, "", err
- }
- inc, ierr := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt)
- _, fleet := registry.FleetPlacement(rec.Region)
- switch {
- case ierr != nil, inc != req.Incarnation, rec.Account != req.Account, rec.App != req.App,
- rec.Host != req.Host, !provision.ValidHostShape(rec.Host), ClassRefusal(*rec) != "",
- rec.Pool != "", fleet, rec.Region != a.cfg.Region:
- return *rec, CodeRecordChanged, nil
- case rec.Move != nil:
- return *rec, CodeMoving, nil
- }
- return *rec, "", nil
+ return ConfirmIdentity(ctx, a.cfg.Store, Identity{Account: req.Account, App: req.App, Host: req.Host,
+ Region: a.cfg.Region, Incarnation: req.Incarnation})
}
func nonce() string {
diff --git a/internal/dbreads/identity.go b/internal/dbreads/identity.go
new file mode 100644
index 000000000..77e3dbfba
--- /dev/null
+++ b/internal/dbreads/identity.go
@@ -0,0 +1,65 @@
+package dbreads
+
+import (
+ "context"
+ "errors"
+ "os"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// Identity is the instance a read (or a restore drill) is bound to: its
+// name, the region it was found in, and the incarnation its record's
+// CreatedAt gives it. Two identities are the same instance only when every
+// field agrees; a host recreated under the same name has a new incarnation.
+type Identity struct {
+ Account, App, Host, Region, Incarnation string
+}
+
+// CaptureIdentity is the opening half of the identity bracket (spec §8.2):
+// the record read fresh, and its identity, or a code. CodeRecordChanged
+// when the record is missing or retired, names another instance than the
+// one asked for, has a bad shape, is in another region, is pooled or placed
+// on a fleet, or is a class the platform keeps no database copy for
+// (ClassRefusal). CodeMoving when it is mid-move. The identity is zero
+// whenever the code is CodeRecordChanged; the record is zero when there was
+// none to read.
+func CaptureIdentity(ctx context.Context, s release.Store, region, account, app, host string) (Identity, provision.Record, string, error) {
+ rec, err := provision.GetRecord(ctx, s, account, app, host)
+ if errors.Is(err, os.ErrNotExist) {
+ return Identity{}, provision.Record{}, CodeRecordChanged, nil
+ }
+ if err != nil {
+ return Identity{}, provision.Record{}, "", err
+ }
+ inc, ierr := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt)
+ _, fleet := registry.FleetPlacement(rec.Region)
+ switch {
+ case ierr != nil, rec.Account != account, rec.App != app, rec.Host != host,
+ !provision.ValidHostShape(rec.Host), ClassRefusal(*rec) != "",
+ rec.Pool != "", fleet, rec.Region != region:
+ return Identity{}, *rec, CodeRecordChanged, nil
+ }
+ id := Identity{Account: rec.Account, App: rec.App, Host: rec.Host, Region: region, Incarnation: inc}
+ if rec.Move != nil {
+ return id, *rec, CodeMoving, nil
+ }
+ return id, *rec, "", nil
+}
+
+// ConfirmIdentity is the closing half: CaptureIdentity again for want's
+// instance, and CodeRecordChanged unless the identity it finds is want. A
+// changed record outranks a move, so an instance recreated while moving is
+// CodeRecordChanged, not CodeMoving.
+func ConfirmIdentity(ctx context.Context, s release.Store, want Identity) (provision.Record, string, error) {
+ got, rec, code, err := CaptureIdentity(ctx, s, want.Region, want.Account, want.App, want.Host)
+ if err != nil || code == CodeRecordChanged {
+ return rec, code, err
+ }
+ if got != want {
+ return rec, CodeRecordChanged, nil
+ }
+ return rec, code, nil
+}
diff --git a/internal/dbreads/identity_test.go b/internal/dbreads/identity_test.go
new file mode 100644
index 000000000..32de6631c
--- /dev/null
+++ b/internal/dbreads/identity_test.go
@@ -0,0 +1,178 @@
+package dbreads
+
+import (
+ "context"
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/provision"
+)
+
+// identity is the identity of the fixture's record as it stands.
+func (fx *fixture) identity() Identity {
+ fx.t.Helper()
+ rec := fx.record()
+ inc, err := provision.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt)
+ if err != nil {
+ fx.t.Fatal(err)
+ }
+ return Identity{Account: fxAccount, App: fxApp, Host: fxHost, Region: "uk", Incarnation: inc}
+}
+
+// CaptureIdentity reads the record fresh and refuses anything the platform
+// keeps no database copy for, or that is not where it was asked for.
+func TestCaptureIdentityRefusals(t *testing.T) {
+ t.Parallel()
+ const badHost = "bad_host.example.com"
+ if provision.ValidHostShape(badHost) {
+ t.Fatalf("%s has a valid shape; the bad-shape row tests nothing", badHost)
+ }
+ no := false
+ type ask struct{ region, account, app, host string }
+ asked := ask{"uk", fxAccount, fxApp, fxHost}
+ for name, c := range map[string]struct {
+ change func(fx *fixture)
+ ask ask
+ want string
+ }{
+ "unchanged": {func(*fixture) {}, asked, ""},
+ "missing": {func(*fixture) {}, ask{"uk", fxAccount, fxApp, "other.example.com"}, CodeRecordChanged},
+ "retired": {func(fx *fixture) { r := fx.record(); r.Retired = true; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "another region": {func(fx *fixture) { r := fx.record(); r.Region = "ie"; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "asked another": {func(*fixture) {}, ask{"ie", fxAccount, fxApp, fxHost}, CodeRecordChanged},
+ "pooled": {func(fx *fixture) { r := fx.record(); r.Pool = "shared"; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "placed on a fleet": {func(fx *fixture) { r := fx.record(); r.Region = "fleet/acme"; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "does not sleep": {func(fx *fixture) { r := fx.record(); r.Hibernate = &no; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "not an instance": {func(fx *fixture) { r := fx.record(); r.Kind = "static"; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "own address": {func(fx *fixture) { r := fx.record(); r.Addr = "10.0.0.1:8080"; fx.putRecord(r) }, asked, CodeRecordChanged},
+ "bad shape": {func(fx *fixture) {
+ r := fx.record()
+ r.Host = badHost
+ body, err := json.Marshal(r)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := fx.store.Put(context.Background(), provision.RecordKey(fxAccount, fxApp, badHost), body, ""); err != nil {
+ t.Fatal(err)
+ }
+ }, ask{"uk", fxAccount, fxApp, badHost}, CodeRecordChanged},
+ "moving": {func(fx *fixture) {
+ r := fx.record()
+ r.Move = &provision.MoveState{ID: "mv_x", To: "ie", State: "requested"}
+ fx.putRecord(r)
+ }, asked, CodeMoving},
+ } {
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+ fx := newFixture(t, 0)
+ wantID := fx.identity()
+ c.change(fx)
+ id, rec, code, err := CaptureIdentity(context.Background(), fx.store, c.ask.region, c.ask.account, c.ask.app, c.ask.host)
+ if err != nil || code != c.want {
+ t.Fatalf("CaptureIdentity = %q, %v; want %q", code, err, c.want)
+ }
+ switch code {
+ case CodeRecordChanged:
+ if id != (Identity{}) {
+ t.Fatalf("a refused capture named %+v", id)
+ }
+ default:
+ if id != wantID {
+ t.Fatalf("identity %+v, want %+v", id, wantID)
+ }
+ if rec.Host != fxHost || rec.Account != fxAccount {
+ t.Fatalf("record %s/%s, want the fixture's", rec.Account, rec.Host)
+ }
+ }
+ })
+ }
+}
+
+// ConfirmIdentity refuses an instance recreated under the same name, in the
+// same account or in another one: a new CreatedAt is a new incarnation.
+func TestConfirmIdentityDetectsRecreate(t *testing.T) {
+ t.Parallel()
+ for name, c := range map[string]struct {
+ change func(fx *fixture)
+ want string
+ }{
+ "unchanged": {func(*fixture) {}, ""},
+ "recreated in the account": {func(fx *fixture) {
+ r := fx.record()
+ r.CreatedAt = r.CreatedAt.Add(time.Hour)
+ fx.putRecord(r)
+ }, CodeRecordChanged},
+ "recreated in another account": {func(fx *fixture) {
+ created := fx.record().CreatedAt
+ fx.store.Delete(context.Background(), provision.RecordKey(fxAccount, fxApp, fxHost))
+ fx.putRecordUnder("bac", provision.Record{Region: "uk", CreatedAt: created.Add(time.Hour)})
+ }, CodeRecordChanged},
+ "recreated in another account, same moment": {func(fx *fixture) {
+ created := fx.record().CreatedAt
+ fx.store.Delete(context.Background(), provision.RecordKey(fxAccount, fxApp, fxHost))
+ fx.putRecordUnder("bac", provision.Record{Region: "uk", CreatedAt: created})
+ }, CodeRecordChanged},
+ } {
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+ fx := newFixture(t, 0)
+ want := fx.identity()
+ c.change(fx)
+ _, code, err := ConfirmIdentity(context.Background(), fx.store, want)
+ if err != nil || code != c.want {
+ t.Fatalf("ConfirmIdentity = %q, %v; want %q", code, err, c.want)
+ }
+ })
+ }
+ // An identity naming the other account is refused against the
+ // original, even though the host and the moment agree.
+ t.Run("another account's identity", func(t *testing.T) {
+ t.Parallel()
+ fx := newFixture(t, 0)
+ rec := fx.record()
+ inc, err := provision.Incarnation("bac", fxApp, fxHost, rec.CreatedAt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := Identity{Account: "bac", App: fxApp, Host: fxHost, Region: "uk", Incarnation: inc}
+ fx.putRecordUnder("bac", provision.Record{Region: "uk", CreatedAt: rec.CreatedAt.Add(time.Hour)})
+ if _, code, err := ConfirmIdentity(context.Background(), fx.store, want); err != nil || code != CodeRecordChanged {
+ t.Fatalf("ConfirmIdentity = %q, %v; want %q", code, err, CodeRecordChanged)
+ }
+ })
+}
+
+// ConfirmIdentity answers moving for the same instance mid-move, and
+// record-changed for a recreated one that is also moving: a changed record
+// outranks a move, as the agent's check always had it.
+func TestConfirmIdentityDetectsMove(t *testing.T) {
+ t.Parallel()
+ move := &provision.MoveState{ID: "mv_x", To: "ie", State: "requested"}
+ for name, c := range map[string]struct {
+ change func(fx *fixture)
+ want string
+ }{
+ "moving": {func(fx *fixture) { r := fx.record(); r.Move = move; fx.putRecord(r) }, CodeMoving},
+ "recreated and moving": {func(fx *fixture) {
+ r := fx.record()
+ r.Move, r.CreatedAt = move, r.CreatedAt.Add(time.Hour)
+ fx.putRecord(r)
+ }, CodeRecordChanged},
+ "moved": {func(fx *fixture) { r := fx.record(); r.Region = "ie"; fx.putRecord(r) }, CodeRecordChanged},
+ } {
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+ fx := newFixture(t, 0)
+ want := fx.identity()
+ c.change(fx)
+ rec, code, err := ConfirmIdentity(context.Background(), fx.store, want)
+ if err != nil || code != c.want {
+ t.Fatalf("ConfirmIdentity = %q, %v; want %q", code, err, c.want)
+ }
+ if c.want == CodeMoving && rec.Move == nil {
+ t.Fatal("a moving answer without the record's move")
+ }
+ })
+ }
+}