carlos / platform Public

provision: the kind check decodes as record readers do, on the bytes written (Astra Task 2 r1 P1)

Round 1 findings committed unedited. checkKindUnchanged now json.Unmarshals
the kind as DecodeRecord does (case-insensitive, later match wins) and runs
on the final bytes after any sequence re-encoding, so alias spellings or
their reordering cannot carry a kind change past it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev da9f765ea72ba538e454183ce9470d4778d560a2 parent 0a7bad1
5 files changed, +123 −36
  • docs/superpowers/notes/reviews/ASTRA-FINDINGS-nearest-region-a-task2-r1.md +16 −0
  • internal/provision/hostsused.go +35 −29
  • internal/provision/hostsused_test.go +60 −0
  • internal/provision/nearest_region_a_census_test.go +1 −1
  • internal/provision/record_sequence.go +11 −6

CI cancelled — superseded by a newer push — run details

diff --git a/docs/superpowers/notes/reviews/ASTRA-FINDINGS-nearest-region-a-task2-r1.md b/docs/superpowers/notes/reviews/ASTRA-FINDINGS-nearest-region-a-task2-r1.md
new file mode 100644
index 000000000..4929ca825
--- /dev/null
+++ b/docs/superpowers/notes/reviews/ASTRA-FINDINGS-nearest-region-a-task2-r1.md
@@ -0,0 +1,16 @@
+- **P1 — Task 2 / check 4: kind immutability uses different decoding rules from record readers.**
+ **Fault family:** an exempt record must never become a live data-kind record in place.
+
+ [`kindOfFields`](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/nearest-region-a-review/internal/provision/hostsused.go:213) reads only lowercase `"kind"`. [`DecodeRecord`](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/nearest-region-a-review/internal/provision/maintenance.go:73) uses Go’s case-insensitive field matching, where later matching fields overwrite earlier ones.
+
+ **Failure scenario:** a stored record containing `"kind":"instance","Kind":"static"` is read as **static**. Lifetime migration calls [`MarkMigrating`](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/nearest-region-a-review/internal/provision/lifetime_migration.go:87), which marshals the fields in sorted order: `"Kind":"static"` now precedes `"kind":"instance"`. The resulting record reads as **instance**. The new guard sees lowercase `"kind":"instance"` on both sides and permits the change through [`migrationMarkLocked`](/home/paulca/amadan.net/carlos/platform/.claude/worktrees/nearest-region-a-review/internal/console/lifetimes_migrate.go:273). An exempt record therefore becomes a data-kind record without passing through creation and marking.
+
+ The same mismatch also rejects legitimate same-kind updates when an existing record uses only `"Kind":"static"` and a typed updater writes canonical `"kind":"static"`.
+
+ **Fix:** decode the effective kind from the original JSON using the same rules as `DecodeRecord`. Check the final bytes after sequence re-encoding, which can also reorder aliases. Add regression coverage for mixed spellings, migration, sequence advancement, and same-kind typed updates.
+
+No separate P2 or P3 findings.
+
+Targeted tests could not start because the read-only sandbox refused Go’s temporary build directory. This finding is source-verified. `GOCACHE` remained unset; no files were edited.
+
+**NOT READY**
\ No newline at end of file
diff --git a/internal/provision/hostsused.go b/internal/provision/hostsused.go
index 43d8cd01d..14469660f 100644
--- a/internal/provision/hostsused.go
+++ b/internal/provision/hostsused.go
@@ -192,44 +192,50 @@ func WriteHostsFloor(ctx context.Context, s release.Store, f HostsFloor, ifMatch
return s.Put(ctx, HostsFloorKey, body, ifMatch)
}
-// checkKindUnchanged is WriteRecord's kind immutability. before and body are
-// the fields RecordSequence parsed. A create ({} before) may be any kind. A
+// checkKindUnchanged is WriteRecord's kind immutability for an in-place
+// write (a create from {} never reaches it). before is the If-Match read and
+// body the exact bytes about to be written. Both are decoded as
+// DecodeRecord decodes a record (json.Unmarshal into the field: names match
+// case-insensitively and a later match wins), so the check sees the kind
+// every reader will see, whatever spellings or order the bytes carry. A
// tombstone body keeps no kind (TombstoneBody) and is no live record, so
-// retiring a record of any kind still lands. Absent and "" read as instance.
-func checkKindUnchanged(before, body map[string]json.RawMessage) error {
- if len(before) == 0 || isTombstoneFields(body) {
+// retiring a record of any kind still lands. Absent and "" read as
+// instance. A body that does not decode is refused, except an exact rewrite
+// of before's own bytes (exact), which changes nothing.
+func checkKindUnchanged(before, body []byte, exact bool) error {
+ a, errA := decodeKindFields(body)
+ if errA == nil && a.Retired {
return nil
}
- b, okB := kindOfFields(before)
- a, okA := kindOfFields(body)
- if !okB || !okA || a != b {
+ b, errB := decodeKindFields(before)
+ if errA != nil || errB != nil {
+ if exact {
+ return nil
+ }
+ return ErrKindChange
+ }
+ if kindOrInstance(a.Kind) != kindOrInstance(b.Kind) {
return ErrKindChange
}
return nil
}
-// kindOfFields is the kind a record body's fields name; false when the
-// field is present and not a string.
-func kindOfFields(f map[string]json.RawMessage) (string, bool) {
- raw, ok := f["kind"]
- if !ok {
- return "instance", true
- }
- var k string
- if err := json.Unmarshal(raw, &k); err != nil {
- return "", false
- }
- if k == "" {
- return "instance", true
- }
- return k, true
+// kindFields is the part of a record body the kind check reads, with the
+// same tags Record and the tombstone use.
+type kindFields struct {
+ Kind string `json:"kind"`
+ Retired bool `json:"retired"`
}
-func isTombstoneFields(f map[string]json.RawMessage) bool {
- raw, ok := f["retired"]
- if !ok {
- return false
+func decodeKindFields(raw []byte) (kindFields, error) {
+ var f kindFields
+ err := json.Unmarshal(raw, &f)
+ return f, err
+}
+
+func kindOrInstance(k string) string {
+ if k == "" {
+ return "instance"
}
- var retired bool
- return json.Unmarshal(raw, &retired) == nil && retired
+ return k
}
diff --git a/internal/provision/hostsused_test.go b/internal/provision/hostsused_test.go
index 9140f7737..0a67f51ca 100644
--- a/internal/provision/hostsused_test.go
+++ b/internal/provision/hostsused_test.go
@@ -216,3 +216,63 @@ func TestWriteRecordRefusesKindChange(t *testing.T) {
t.Fatalf("tombstoning a static record: %v", err)
}
}
+
+// TestWriteRecordKindReadsAsReadersDo (Astra Task 2 round 1, P1): the kind
+// check decodes as DecodeRecord does (case-insensitive names, the later
+// match wins) and checks the bytes actually written, after the sequence
+// re-encoding, so alias spellings cannot carry a kind change past it.
+func TestWriteRecordKindReadsAsReadersDo(t *testing.T) {
+ ctx := context.Background()
+ const ident = `"schema":1,"host":"shop.example.com","slug":"shop","app":"shop","account":"bab","channel":"stable","created_at":"2026-10-26T09:00:00Z"`
+ key := RecordKey("bab", "shop", "shop.example.com")
+ put := func(t *testing.T, s release.Store, raw string) string {
+ t.Helper()
+ tag, err := s.Put(ctx, key, []byte(raw), "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ return tag
+ }
+ t.Run("aliases reordered by hand", func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ before := `{` + ident + `,"kind":"instance","Kind":"static"}` // reads static
+ after := `{` + ident + `,"Kind":"static","kind":"instance"}` // reads instance
+ if got := mustDecodeKind(t, before); got != "static" {
+ t.Fatalf("fixture: before reads %q", got)
+ }
+ tag := put(t, s, before)
+ if _, _, err := WriteRecord(ctx, s, key, []byte(before), []byte(after), tag); !errors.Is(err, ErrKindChange) {
+ t.Fatalf("static to instance by alias order = %v, want ErrKindChange", err)
+ }
+ })
+ t.Run("aliases reordered by the sequence advance", func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ raw := `{` + ident + `,"record_seq":1,"kind":"instance","Kind":"static"}` // reads static
+ tag := put(t, s, raw)
+ // The same bytes, re-encoded by AdvanceRecordSequence in sorted key
+ // order ("Kind" before "kind"), would read as instance.
+ if _, _, err := WriteRecord(WithRecordSequence(ctx), s, key, []byte(raw), []byte(raw), tag); !errors.Is(err, ErrKindChange) {
+ t.Fatalf("a sequence advance that flips the read kind = %v, want ErrKindChange", err)
+ }
+ if got, _, _ := s.Get(ctx, key); string(got) != raw {
+ t.Fatalf("the refused write landed: %s", got)
+ }
+ })
+ t.Run("a same-kind typed update over another spelling", func(t *testing.T) {
+ s := release.NewDirStore(t.TempDir())
+ before := `{` + ident + `,"Kind":"static"}`
+ tag := put(t, s, before)
+ if _, _, err := WriteRecord(ctx, s, key, []byte(before), []byte(`{`+ident+`,"kind":"static"}`), tag); err != nil {
+ t.Fatalf("static to static, respelled: %v", err)
+ }
+ })
+}
+
+func mustDecodeKind(t *testing.T, raw string) string {
+ t.Helper()
+ rec, err := DecodeRecord([]byte(raw))
+ if err != nil {
+ t.Fatal(err)
+ }
+ return rec.Kind
+}
diff --git a/internal/provision/nearest_region_a_census_test.go b/internal/provision/nearest_region_a_census_test.go
index 458490cf9..e31cf42ba 100644
--- a/internal/provision/nearest_region_a_census_test.go
+++ b/internal/provision/nearest_region_a_census_test.go
@@ -18,7 +18,7 @@ func nearestRegionAWriteCensusInventory() []censusEntry {
{"internal/provision/hostsused.go", "MarkHostUsed", "nonrecord", "sel:Put", "439f9ca1443f2c815f2ddb18b4732285d236d745fb660de66dc3a175b4532fd3"},
{"internal/provision/hostsused.go", "WriteHostsFloor", "nonrecord", "sel:Put", "c10e2e2fd57470c81785df3dddd9bb33013b416ce5381e74a9657811ec6b5c79"},
{"internal/provision/hostsused.go", "WriteHostsMode", "nonrecord", "sel:Put", "21147fa65d21d7a074589ca4ea6cf2ec9852a7ab42ca3acaac429562d8f22146"},
- {"internal/provision/record_sequence.go", "WriteRecord", "record-primitive", "key:IsRecordKey,key:WriteRecord,key:preparedRecordKey,sel:Put", "15806daf27c0266a673aa8c291052b5bc094a8d747ea21254a69e66bd3de585b"},
+ {"internal/provision/record_sequence.go", "WriteRecord", "record-primitive", "key:IsRecordKey,key:WriteRecord,key:preparedRecordKey,sel:Put", "d58d11066b4e4445e4295170de0f4aec1d9c8c046f1a9fc4cd79fc6e6353d527"},
}
for _, r := range replacements {
found := false
diff --git a/internal/provision/record_sequence.go b/internal/provision/record_sequence.go
index d417c0077..43c099ab5 100644
--- a/internal/provision/record_sequence.go
+++ b/internal/provision/record_sequence.go
@@ -130,15 +130,11 @@ func WriteRecord(ctx context.Context, s release.Store, key string, before, body
if err := checkRecordMarker(ctx, before); err != nil {
return "", 0, err
}
- bodyFields, candidate, err := RecordSequence(body)
+ _, candidate, err := RecordSequence(body)
if err != nil {
return "", 0, err
}
- // A record's kind is fixed at create (nearest-region never-reuse,
- // hostsused.go): only a create ({} before) or a tombstone may differ.
- if err := checkKindUnchanged(fields, bodyFields); err != nil {
- return "", 0, err
- }
+ exact := bytes.Equal(before, body)
enabled, _ := ctx.Value(recordSequenceKey{}).(bool)
_, present := fields["record_seq"]
p, nested := ctx.Value(preparedRecordKey{}).(*preparedRecord)
@@ -160,6 +156,15 @@ func WriteRecord(ctx context.Context, s release.Store, key string, before, body
}
p.body, p.seq, p.sequenced = append([]byte(nil), body...), base+1, true
}
+ // A record's kind is fixed at create (nearest-region never-reuse,
+ // hostsused.go): only a create ({} before) or a tombstone may differ.
+ // Checked on the exact bytes about to be written, after any sequence
+ // re-encoding, as the record's readers will decode them.
+ if len(fields) != 0 {
+ if err := checkKindUnchanged(before, body, exact); err != nil {
+ return "", 0, err
+ }
+ }
tag, err := s.Put(ctx, key, body, etag)
return tag, p.seq, err
}