edgesfile: ReadSnapshot reads a body only under its own box's key (H3-M4)
main's console door (#396) elects a host's home over ReadSnapshot and must show the edge's answer or unknown. The edge refuses a body whose box field names another box than its key (hunt H3-M4), but ReadSnapshot still took it, so the door could show a home no edge acts on (TestStatusElectsTheSameHomeAsTheEdge failed after the merge). ReadSnapshot now skips such a body and marks the snapshot incomplete, as it does a body that will not parse; every console reader already treats an incomplete snapshot as unknown or a refusal. - the election test's key-order row uses matching boxes; a new row pins that a misfiled body is no file to the edge or the door. - TestReadSnapshotKeepsEachFilesKey uses matching boxes; new TestReadSnapshotSkipsABodyNamingAnotherBox. - hotreads: the dry pass answers each edges key with a body naming that key's box, so the snapshot's dry read stays silent. - hosts used: a second file naming a proven group member is still refused, now as the 503 an incomplete edges read gets, not S4's 409.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+91
−23
internal/console/hostsused_groupmember_test.go+7 −2internal/console/hotreads.go+29 −4internal/edge/homemap_status_election_test.go+19 −8internal/edgesfile/homeelect_test.go+25 −6internal/edgesfile/snapshot.go+11 −3
diff --git a/internal/console/hostsused_groupmember_test.go b/internal/console/hostsused_groupmember_test.go| index 440d9298f..41e1c0ba1 100644 |
| --- a/internal/console/hostsused_groupmember_test.go |
| +++ b/internal/console/hostsused_groupmember_test.go |
| @@ -171,7 +171,10 @@ func TestUnprovenGroupMemberRefused(t *testing.T) { |
| }) |
| t.Run("another file names the member", func(t *testing.T) { |
| // An edges file under another key whose body names the proven |
| - // member must not hide behind its proof. |
| + // member must not hide behind its proof. Such a body is no file at |
| + // all since hunt H3-M4 (the edges snapshot skips a body naming a |
| + // box its key does not), so the snapshot is incomplete and the |
| + // door refuses before it gets to the proof: 503, not S4's 409. |
| c := newGroupMemberCase(t) |
| c.observe(t, groupTestInstance, time.Now().UTC(), nil) |
| c.proven(t) |
| @@ -179,7 +182,9 @@ func TestUnprovenGroupMemberRefused(t *testing.T) { |
| if _, err := c.fx.rel.Put(context.Background(), edgesFilePrefix+"old.json", []byte(body), ""); err != nil { |
| t.Fatal(err) |
| } |
| - wantS4(t, "a second file naming the member", c.coverage(t)) |
| + if ref := c.coverage(t); ref == nil || ref.Status != http.StatusServiceUnavailable || ref.Msg != msgHostsInvalidated { |
| + t.Fatalf("a second file naming the member: coverage = %+v, want the 503 an incomplete edges read gets", ref) |
| + } |
| }) |
| t.Run("pinned as a fixed box", func(t *testing.T) { |
| c := newGroupMemberCase(t) |
diff --git a/internal/console/hotreads.go b/internal/console/hotreads.go| index 3293de797..343969935 100644 |
| --- a/internal/console/hotreads.go |
| +++ b/internal/console/hotreads.go |
| @@ -2,6 +2,7 @@ package console |
| import ( |
| "context" |
| + "encoding/json" |
| "errors" |
| "io" |
| "slices" |
| @@ -9,6 +10,7 @@ import ( |
| "sync" |
| "sync/atomic" |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/status" |
| ) |
| @@ -98,6 +100,9 @@ var errNoListMod = errors.New("console: the store cannot list modification times |
| type prefetchStore struct { |
| release.Store |
| dryBody []byte |
| + // dryFor, when set, answers the dry pass per key instead of dryBody: |
| + // for a reader that accepts a body silently only under its own key. |
| + dryFor func(key string) []byte |
| // limit is the most bytes the reader accepts for key, 0 for no limit. |
| limit func(key string) int64 |
| @@ -200,6 +205,9 @@ func (p *prefetchStore) Get(ctx context.Context, key string) ([]byte, string, er |
| if p.dry { |
| p.asked = append(p.asked, key) |
| p.mu.Unlock() |
| + if p.dryFor != nil { |
| + return p.dryFor(key), "", nil |
| + } |
| return slices.Clone(p.dryBody), "", nil |
| } |
| pg, ok := p.pending[key] |
| @@ -316,7 +324,13 @@ func readConcurrently[T any](ctx context.Context, st release.Store, dryBody []by |
| // readConcurrentlyStats is readConcurrently, and what it held. |
| func readConcurrentlyStats[T any](ctx context.Context, st release.Store, dryBody []byte, limit func(key string) int64, read func(release.Store) T) (T, prefetchStats) { |
| - p := &prefetchStore{Store: st, dryBody: dryBody, limit: limit, dry: true, |
| + return readConcurrentlyStatsFor(ctx, st, dryBody, nil, limit, read) |
| +} |
| + |
| +// readConcurrentlyStatsFor is readConcurrentlyStats with the dry pass's |
| +// body chosen per key by dryFor when it is set. |
| +func readConcurrentlyStatsFor[T any](ctx context.Context, st release.Store, dryBody []byte, dryFor func(key string) []byte, limit func(key string) int64, read func(release.Store) T) (T, prefetchStats) { |
| + p := &prefetchStore{Store: st, dryBody: dryBody, dryFor: dryFor, limit: limit, dry: true, |
| lists: map[string]listed{}, dirs: map[string]listed{}, mods: map[string]listedMod{}, pending: map[string]*pendingGet{}, |
| window: make(chan struct{}, readConcurrency)} |
| read(p) |
| @@ -388,16 +402,27 @@ func loadBoxesConcurrently(ctx context.Context, st release.Store) ([]status.Box, |
| // readEdgesSnapshotConcurrently is readEdgesSnapshot with the edges files |
| // read concurrently: the same listing, files and skips. The dry body is |
| -// the smallest versioned edges file, which the snapshot keeps without |
| -// logging. |
| +// the smallest versioned edges file naming the box its key names, which |
| +// the snapshot keeps without logging (it skips, and logs, a body naming |
| +// another box: hunt H3-M4). |
| func readEdgesSnapshotConcurrently(ctx context.Context, st release.Store) (edgesSnapshot, error) { |
| type out struct { |
| snap edgesSnapshot |
| err error |
| } |
| - o := readConcurrently(ctx, st, []byte(`{"v":1,"box":"-"}`), nil, func(s release.Store) out { |
| + o, _ := readConcurrentlyStatsFor(ctx, st, nil, edgesDryBody, nil, func(s release.Store) out { |
| snap, err := readEdgesSnapshot(ctx, s) |
| return out{snap, err} |
| }) |
| return o.snap, o.err |
| } |
| + |
| +// edgesDryBody is the smallest versioned edges file for key's box. |
| +func edgesDryBody(key string) []byte { |
| + box, ok := edgesfile.BoxFromKey(key) |
| + if !ok { |
| + box = "-" |
| + } |
| + raw, _ := json.Marshal(map[string]any{"v": 1, "box": box}) |
| + return raw |
| +} |
diff --git a/internal/edge/homemap_status_election_test.go b/internal/edge/homemap_status_election_test.go| index 8efc74ec2..c3866d8ab 100644 |
| --- a/internal/edge/homemap_status_election_test.go |
| +++ b/internal/edge/homemap_status_election_test.go |
| @@ -89,11 +89,18 @@ func TestStatusElectsTheSameHomeAsTheEdge(t *testing.T) { |
| "box-a": public("box-a", "192.0.2.1", `"role":"home","stamp":3,`+owner+`,`+block("/assets/", "aaaaaaaaaaaaaaaa", 1761381000)), |
| "box-b": public("box-b", "192.0.2.2", `"role":"home","stamp":3,`+owner+`,`+block("/img/", "bbbbbbbbbbbbbbbb", 1761382800)), |
| }, "box-a", "box-a", "box-a"}, |
| - {"key order is the bucket key's, not the box field's", |
| + {"key order is the bucket key's", |
| + map[string]string{ |
| + "aaa": public("aaa", "192.0.2.1", `"role":"home",`+owner+`,`+block("/assets/", "aaaaaaaaaaaaaaaa", 1761381000)), |
| + "zzz": public("zzz", "192.0.2.2", `"role":"home",`+owner+`,`+block("/img/", "bbbbbbbbbbbbbbbb", 1761382800)), |
| + }, "aaa", "aaa", "aaa"}, |
| + // A body whose box is not its key's is read by neither (hunt |
| + // H3-M4): the edge carries no parse of it, and the door's snapshot |
| + // is incomplete, which the door shows as unknown. |
| + {"a body naming another box than its key is no file to either", |
| map[string]string{ |
| "aaa": public("zed", "192.0.2.1", `"role":"home",`+owner+`,`+block("/assets/", "aaaaaaaaaaaaaaaa", 1761381000)), |
| - "zzz": public("amy", "192.0.2.2", `"role":"home",`+owner+`,`+block("/img/", "bbbbbbbbbbbbbbbb", 1761382800)), |
| - }, "zed", "zed", "zed"}, |
| + }, "", "", unknown}, |
| {"a role this build does not know takes no part", |
| map[string]string{ |
| "box-a": public("box-a", "192.0.2.1", `"role":"home","stamp":1,`+owner+`,`+block("/assets/", "aaaaaaaaaaaaaaaa", 1761381000)), |
| @@ -150,14 +157,18 @@ func TestStatusElectsTheSameHomeAsTheEdge(t *testing.T) { |
| } |
| snap, err := edgesfile.ReadSnapshot(context.Background(), s) |
| - if err != nil || !snap.Complete { |
| - t.Fatalf("ReadSnapshot = complete %v, %v", snap.Complete, err) |
| + if err != nil { |
| + t.Fatalf("ReadSnapshot: %v", err) |
| } |
| + // An incomplete snapshot is unknown at the door |
| + // (console homeAssets). |
| door := unknown |
| - won, split, err := edgesfile.ElectHomesForEveryReader(snap.Files) |
| var doorAssets *edgesfile.HostAssets |
| - if err == nil && !split[host] { |
| - door, doorAssets = won[host].Box, won[host].Assets |
| + if snap.Complete { |
| + won, split, err := edgesfile.ElectHomesForEveryReader(snap.Files) |
| + if err == nil && !split[host] { |
| + door, doorAssets = won[host].Box, won[host].Assets |
| + } |
| } |
| if door == unknown { |
| if row.door != unknown { |
diff --git a/internal/edgesfile/homeelect_test.go b/internal/edgesfile/homeelect_test.go| index 2feae9e22..a45ce7f6e 100644 |
| --- a/internal/edgesfile/homeelect_test.go |
| +++ b/internal/edgesfile/homeelect_test.go |
| @@ -134,14 +134,14 @@ func TestElectionRefusesAHostEntryThatDoesNotDecode(t *testing.T) { |
| } |
| // TestReadSnapshotKeepsEachFilesKey: the election orders files by the key |
| -// they were read from, which need not match the box field. |
| +// they were read from. |
| func TestReadSnapshotKeepsEachFilesKey(t *testing.T) { |
| s := release.NewDirStore(t.TempDir()) |
| ctx := context.Background() |
| - if _, err := s.Put(ctx, Prefix+"aaa.json", []byte(`{"v":1,"box":"zed","eip":"192.0.2.1","hosts":{"shop.example.com":{"role":"home"}}}`), ""); err != nil { |
| + if _, err := s.Put(ctx, Prefix+"zzz.json", []byte(`{"v":1,"box":"zzz","eip":"192.0.2.1","hosts":{"shop.example.com":{"role":"home"}}}`), ""); err != nil { |
| t.Fatal(err) |
| } |
| - if _, err := s.Put(ctx, Prefix+"zzz.json", []byte(`{"v":1,"box":"amy","eip":"192.0.2.2","hosts":{"shop.example.com":{"role":"home"}}}`), ""); err != nil { |
| + if _, err := s.Put(ctx, Prefix+"aaa.json", []byte(`{"v":1,"box":"aaa","eip":"192.0.2.2","hosts":{"shop.example.com":{"role":"home"}}}`), ""); err != nil { |
| t.Fatal(err) |
| } |
| snap, err := ReadSnapshot(ctx, s) |
| @@ -149,7 +149,7 @@ func TestReadSnapshotKeepsEachFilesKey(t *testing.T) { |
| t.Fatalf("ReadSnapshot = %+v, %v", snap, err) |
| } |
| for _, f := range snap.Files { |
| - if want := map[string]string{"zed": Prefix + "aaa.json", "amy": Prefix + "zzz.json"}[f.Box]; f.Key != want { |
| + if want := Key(f.Box); f.Key != want { |
| t.Errorf("%s: Key = %q, want %q", f.Box, f.Key, want) |
| } |
| } |
| @@ -157,7 +157,26 @@ func TestReadSnapshotKeepsEachFilesKey(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - if got := won["shop.example.com"].Box; got != "zed" { |
| - t.Errorf("unstamped duplicates elect %q, want zed, the first file in key order", got) |
| + if got := won["shop.example.com"].Box; got != "aaa" { |
| + t.Errorf("unstamped duplicates elect %q, want aaa, the first file in key order", got) |
| + } |
| +} |
| + |
| +// TestReadSnapshotSkipsABodyNamingAnotherBox: a body is read only under its |
| +// own box's key (hunt H3-M4), as the edge reads it. One whose box field |
| +// names another box is skipped and leaves the snapshot incomplete, like a |
| +// body that will not parse, so nothing is concluded from it. |
| +func TestReadSnapshotSkipsABodyNamingAnotherBox(t *testing.T) { |
| + s := release.NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + if _, err := s.Put(ctx, Prefix+"aaa.json", []byte(`{"v":1,"box":"aaa","eip":"192.0.2.1","hosts":{}}`), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := s.Put(ctx, Prefix+"zzz.json", []byte(`{"v":1,"box":"amy","eip":"192.0.2.2","hosts":{"shop.example.com":{"role":"home"}}}`), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + snap, err := ReadSnapshot(ctx, s) |
| + if err != nil || snap.Complete || len(snap.Files) != 1 || snap.Files[0].Box != "aaa" { |
| + t.Fatalf("ReadSnapshot = %+v, %v; want only aaa's file, incomplete", snap, err) |
| } |
| } |
diff --git a/internal/edgesfile/snapshot.go b/internal/edgesfile/snapshot.go| index 56885908f..7668872b9 100644 |
| --- a/internal/edgesfile/snapshot.go |
| +++ b/internal/edgesfile/snapshot.go |
| @@ -248,7 +248,10 @@ func (s Snapshot) EvidenceComplete() bool { return s.Complete && s.BoxesComplete |
| // ReadSnapshot REPORTS a failed listing instead of answering empty: |
| // the door must tell "could not read" (503, C28) from "no box publishes |
| // this code" (codex R2-2). A single unreadable or malformed file is |
| -// skipped, as before, and marks the snapshot incomplete. |
| +// skipped, as before, and marks the snapshot incomplete. So is a body whose |
| +// box field names a box other than the one its key names (BoxFromKey, hunt |
| +// H3-M4): the edge refuses that body, and a reader that took it would show |
| +// an answer no edge acts on. |
| func ReadSnapshot(ctx context.Context, s release.Store) (Snapshot, error) { |
| keys, err := s.List(ctx, Prefix) |
| if err != nil { |
| @@ -256,8 +259,8 @@ func ReadSnapshot(ctx context.Context, s release.Store) (Snapshot, error) { |
| } |
| snap := Snapshot{Complete: true} |
| for _, key := range keys { |
| - name, ok := strings.CutPrefix(key, Prefix) |
| - if !ok || !strings.HasSuffix(name, ".json") || strings.Contains(name, "/") { |
| + box, ok := BoxFromKey(key) |
| + if !ok { |
| continue |
| } |
| body, _, err := s.Get(ctx, key) |
| @@ -272,6 +275,11 @@ func ReadSnapshot(ctx context.Context, s release.Store) (Snapshot, error) { |
| snap.Complete = false |
| continue |
| } |
| + if f.Box != box { |
| + log.Printf("edges: skipping %s in the placement join: its body names box %q, not the box its key names", key, f.Box) |
| + snap.Complete = false |
| + continue |
| + } |
| f.Key = key |
| snap.Files = append(snap.Files, f) |
| } |