host: system replay floor is one atomic record (floor.json)
Astra round 2, finding 1: the stamp and identity were two durable files, and a cut between them refused every retry of the pointer just admitted. floor.json now holds both in one replace; seen.json is still written after it for an older release to read. A seen.json stamp with no identity (a box upgraded from before) admits nothing at that stamp (P1-1).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+325
−116
internal/host/hunt_rc_r2_linux_test.go+173 −0internal/host/installchannel.go+6 −13internal/host/installchannel_test.go+3 −4internal/host/systemapply.go+5 −9internal/host/systemapply_test.go+12 −1internal/host/systemleg.go+3 −4internal/host/systemseen_test.go+50 −38internal/host/systemstate.go+73 −47
diff --git a/internal/host/hunt_rc_r2_linux_test.go b/internal/host/hunt_rc_r2_linux_test.go| new file mode 100644 |
| index 000000000..3b553f406 |
| --- /dev/null |
| +++ b/internal/host/hunt_rc_r2_linux_test.go |
| @@ -0,0 +1,173 @@ |
| +package host |
| + |
| +import ( |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + "time" |
| + "unsafe" |
| + |
| + "golang.org/x/sys/unix" |
| +) |
| + |
| +// Fix round 2 of the release chain, finding 1 (Astra round 2): an admission |
| +// that is cut off part way through writing the replay floor must leave the |
| +// box able to admit the same pointer when it is asked again. With the stamp |
| +// and the identity in two files written in turn, a cut between them left the |
| +// new stamp beside the old identity, and every retry of the pointer just |
| +// admitted was refused as a replay, before anything was staged. |
| +// |
| +// The box is on v2 and follows two promotes of it (no-ops, so each run |
| +// writes only the floor and its result). The second admission is watched |
| +// with inotify: every file root renames into its state directory, in order. |
| +// Then, for every cut between those writes, the state directory is put back |
| +// to how the first admission left it with only the writes before the cut |
| +// on disk, and the request is answered again: it must be noted, never |
| +// refused. |
| +func TestApplyFloorWriteCutAnywhereStillAdmitsTheRetry(t *testing.T) { |
| + f := newApplyFixture(t, "stable", "v2") |
| + body := []byte("#!/bin/sh\necho carlos v2\n") |
| + ctx := context.Background() |
| + f.promote("v2", body, true) |
| + if err := f.a.Run(ctx, false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := f.result(); r == nil || r.Outcome != systemOutcomeNoted { |
| + t.Fatalf("first promote: %+v", r) |
| + } |
| + dir := systemRootDir(f.a.optDir) |
| + admitted := snapshotDir(t, dir) |
| + req := f.promote("v2", body, true) |
| + writes := watchRenames(t, dir, func() { |
| + if err := f.a.Run(ctx, false); err != nil { |
| + t.Fatal(err) |
| + } |
| + }) |
| + if r := f.result(); r == nil || r.Outcome != systemOutcomeNoted || !r.PromotedAt.Equal(req.PromotedAt) { |
| + t.Fatalf("second promote: %+v", r) |
| + } |
| + final := snapshotDir(t, dir) |
| + // The floor's writes are every rename before the result's. |
| + var floor []string |
| + for _, name := range writes { |
| + if name == "result.json" { |
| + break |
| + } |
| + floor = append(floor, name) |
| + } |
| + if len(floor) == 0 { |
| + t.Fatalf("control: no floor write seen before the result (%v)", writes) |
| + } |
| + t.Logf("the admission's floor writes, in order: %v", floor) |
| + for cut := 0; cut < len(floor); cut++ { |
| + restoreDir(t, dir, admitted) |
| + for _, name := range floor[:cut] { |
| + if err := os.WriteFile(filepath.Join(dir, name), final[name], 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := writeJSONDurable(systemRequestPath(f.a.dataDir), req, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := f.a.Run(ctx, false); err != nil { |
| + t.Fatal(err) |
| + } |
| + r := f.result() |
| + if r == nil || r.Outcome != systemOutcomeNoted || !r.PromotedAt.Equal(req.PromotedAt) { |
| + t.Fatalf("cut after %v: the retry of the admitted pointer was answered %+v", floor[:cut], r) |
| + } |
| + } |
| +} |
| + |
| +// P1-1 (partly): a box upgraded from a release that kept only seen.json has |
| +// a stamp and no identity. At that stamp it cannot tell the pointer it |
| +// followed from a saved one signed in the same second, so it admits only a |
| +// strictly later stamp there. |
| +func TestApplyEqualStampWithNoRecordedPointerIsRefused(t *testing.T) { |
| + f := newApplyFixture(t, "stable", "v2") |
| + req := f.promote("v2", []byte("x"), true) |
| + if err := writeJSONDurable(filepath.Join(systemRootDir(f.a.optDir), "seen.json"), map[string]time.Time{"stable": req.PromotedAt}, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := f.result(); r == nil || r.Outcome != systemOutcomeRefused || !strings.Contains(r.Reason, "replay") { |
| + t.Fatalf("result = %+v, want a replay refusal", r) |
| + } |
| +} |
| + |
| +func snapshotDir(t *testing.T, dir string) map[string][]byte { |
| + t.Helper() |
| + out := map[string][]byte{} |
| + ents, err := os.ReadDir(dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, e := range ents { |
| + if !e.Type().IsRegular() { |
| + continue |
| + } |
| + b, err := os.ReadFile(filepath.Join(dir, e.Name())) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + out[e.Name()] = b |
| + } |
| + return out |
| +} |
| + |
| +func restoreDir(t *testing.T, dir string, snap map[string][]byte) { |
| + t.Helper() |
| + ents, err := os.ReadDir(dir) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, e := range ents { |
| + if e.Type().IsRegular() { |
| + if err := os.Remove(filepath.Join(dir, e.Name())); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + } |
| + for name, b := range snap { |
| + if err := os.WriteFile(filepath.Join(dir, name), b, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +// watchRenames runs fn and returns the names renamed into dir meanwhile, in |
| +// the order the kernel reported them: the order the durable writes landed. |
| +func watchRenames(t *testing.T, dir string, fn func()) []string { |
| + t.Helper() |
| + fd, err := unix.InotifyInit1(unix.IN_NONBLOCK | unix.IN_CLOEXEC) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer unix.Close(fd) |
| + if _, err := unix.InotifyAddWatch(fd, dir, unix.IN_MOVED_TO); err != nil { |
| + t.Fatal(err) |
| + } |
| + fn() |
| + var names []string |
| + buf := make([]byte, 64<<10) |
| + for { |
| + n, err := unix.Read(fd, buf) |
| + if err == unix.EAGAIN || n <= 0 { |
| + break |
| + } |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for off := 0; off+unix.SizeofInotifyEvent <= n; { |
| + ev := (*unix.InotifyEvent)(unsafe.Pointer(&buf[off])) |
| + name := buf[off+unix.SizeofInotifyEvent : off+unix.SizeofInotifyEvent+int(ev.Len)] |
| + names = append(names, strings.TrimRight(string(name), "\x00")) |
| + off += unix.SizeofInotifyEvent + int(ev.Len) |
| + } |
| + } |
| + return names |
| +} |
diff --git a/internal/host/installchannel.go b/internal/host/installchannel.go| index 7f87c7daf..1d7369ebf 100644 |
| --- a/internal/host/installchannel.go |
| +++ b/internal/host/installchannel.go |
| @@ -306,19 +306,15 @@ func (in channelInstall) install(ctx context.Context) (channelInstalled, error) |
| // a box that has followed a later promote (an update cut off before |
| // applied.json, or a no-op request that was deferred) is not installed |
| // from a saved older pointer put back at the channel key. The install |
| - // raises the floor at its end (systemSeenAdvance), so letting such a |
| + // raises the floor at its end (systemFloorAdvance), so letting such a |
| // pointer through would also move the floor back. |
| - seen, err := readSystemSeen(a.optDir) |
| + floor, err := readSystemFloor(a.optDir) |
| if err != nil { |
| return got, fmt.Errorf("this box's replay floor could not be read, so nothing was installed: %w", err) |
| } |
| - ids, err := readSystemSeenPointers(a.optDir) |
| - if err != nil { |
| - return got, fmt.Errorf("this box's replay floor could not be read, so nothing was installed: %w", err) |
| - } |
| - if !systemSeenAdmits(seen, ids, a.channel, p) { |
| + if !systemSeenAdmits(floor, a.channel, p) { |
| return got, fmt.Errorf("replay: the %s channel names %s promoted %s, not after the pointer this box already followed (promoted %s), so nothing was installed", |
| - a.channel, p.Current.Version, p.PromotedAt.Format(time.RFC3339), seen[a.channel].Format(time.RFC3339)) |
| + a.channel, p.Current.Version, p.PromotedAt.Format(time.RFC3339), floor[a.channel].PromotedAt.Format(time.RFC3339)) |
| } |
| var policy *hostfloor.Policy |
| var policyFloor hostfloor.Verified |
| @@ -474,13 +470,10 @@ func (in channelInstall) install(ctx context.Context) (channelInstalled, error) |
| return got, err |
| } |
| } |
| - if seen, err = readSystemSeen(a.optDir); err != nil { |
| - return got, err |
| - } |
| - if ids, err = readSystemSeenPointers(a.optDir); err != nil { |
| + if floor, err = readSystemFloor(a.optDir); err != nil { |
| return got, err |
| } |
| - if err := systemSeenAdvance(a.optDir, seen, ids, a.channel, p); err != nil { |
| + if err := systemFloorAdvance(a.optDir, floor, a.channel, p); err != nil { |
| return got, err |
| } |
| if err := writeJSONDurable(a.file("applied.json"), systemApplied{Version: p.Current.Version, Channel: a.channel, At: a.ops.now().UTC()}, 0o644); err != nil { |
diff --git a/internal/host/installchannel_test.go b/internal/host/installchannel_test.go| index b8fe12952..6df329e41 100644 |
| --- a/internal/host/installchannel_test.go |
| +++ b/internal/host/installchannel_test.go |
| @@ -585,7 +585,7 @@ func TestGroupInstallFloorObservation(t *testing.T) { |
| } |
| // install-channel holds the channel to the box's replay floor (hunt H6-M11, |
| -// seen.json and seen-pointers.json) as an update does: a box that has |
| +// floor.json and seen.json) as an update does: a box that has |
| // already followed a later promote, with no applied.json (an update cut off |
| // before it, or a forged no-op that was deferred), is not installed from a |
| // saved older pointer a bucket writer put back at the channel key. Nothing |
| @@ -604,9 +604,8 @@ func TestInstallChannelRefusesAPointerOlderThanTheBoxFollowed(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - seen, _ := readSystemSeen(f.a.optDir) |
| - versions, _ := readSystemSeenPointers(f.a.optDir) |
| - if err := systemSeenAdvance(f.a.optDir, seen, versions, "stable", followed); err != nil { |
| + floor, _ := readSystemFloor(f.a.optDir) |
| + if err := systemFloorAdvance(f.a.optDir, floor, "stable", followed); err != nil { |
| t.Fatal(err) |
| } |
| head, etag, err := f.store.Get(ctx, key) |
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go| index 957ab8474..fe547d6cb 100644 |
| --- a/internal/host/systemapply.go |
| +++ b/internal/host/systemapply.go |
| @@ -548,22 +548,18 @@ func (a *systemApplier) runLocked(ctx context.Context, recover bool, operation f |
| return a.record(req, fmt.Errorf("%w: request names %s@%s, pointer is %s@%s", errSystemRefused, |
| req.Version, req.PromotedAt.Format(time.RFC3339), p.Current.Version, p.PromotedAt.Format(time.RFC3339))) |
| } |
| - seen, err := readSystemSeen(a.optDir) |
| + floor, err := readSystemFloor(a.optDir) |
| if err != nil { |
| - return a.record(req, fmt.Errorf("%w: seen.json unreadable: %v", errSystemRefused, err)) |
| - } |
| - ids, err := readSystemSeenPointers(a.optDir) |
| - if err != nil { |
| - return a.record(req, fmt.Errorf("%w: seen-pointers.json unreadable: %v", errSystemRefused, err)) |
| + return a.record(req, fmt.Errorf("%w: replay floor unreadable: %v", errSystemRefused, err)) |
| } |
| // The replay floor (hunt H6-M11): a pointer no newer than the one this |
| // box already followed, unless it is that very pointer read again. |
| // systemSeenAdmits says which. |
| - if !systemSeenAdmits(seen, ids, a.channel, p) { |
| + if !systemSeenAdmits(floor, a.channel, p) { |
| return a.record(req, fmt.Errorf("%w: replay — pointer %s promoted %s is not after the %s watermark %s, and is not the pointer admitted there", errSystemRefused, |
| - p.Current.Version, p.PromotedAt.Format(time.RFC3339), a.channel, seen[a.channel].Format(time.RFC3339))) |
| + p.Current.Version, p.PromotedAt.Format(time.RFC3339), a.channel, floor[a.channel].PromotedAt.Format(time.RFC3339))) |
| } |
| - if err := systemSeenAdvance(a.optDir, seen, ids, a.channel, p); err != nil { |
| + if err := systemFloorAdvance(a.optDir, floor, a.channel, p); err != nil { |
| return err |
| } |
| // Whether there is anything to install is the installed binary's to |
diff --git a/internal/host/systemapply_test.go b/internal/host/systemapply_test.go| index d86aa6c14..0c19d811e 100644 |
| --- a/internal/host/systemapply_test.go |
| +++ b/internal/host/systemapply_test.go |
| @@ -284,14 +284,25 @@ func TestApplyReplayGuardIsPerChannelAndEqualIsAllowed(t *testing.T) { |
| if r := f.result(); r == nil || r.Outcome != systemOutcomeRefused || !strings.Contains(r.Reason, "replay") { |
| t.Fatalf("result = %+v", r) |
| } |
| - // Equal → allowed (a retry of the same promote). |
| + // Equal with no pointer recorded at the stamp (seen.json alone, from a |
| + // release before floor.json) → refused: it cannot tell this pointer |
| + // from a saved one signed in the same second (fix round 2). |
| writeJSONDurable(filepath.Join(systemRootDir(f.a.optDir), "seen.json"), systemSeen{"stable": req.PromotedAt}, 0o644) |
| if err := f.a.Run(context.Background(), false); err != nil { |
| t.Fatal(err) |
| } |
| + if r := f.result(); r == nil || r.Outcome != systemOutcomeRefused || !strings.Contains(r.Reason, "replay") { |
| + t.Fatalf("result = %+v", r) |
| + } |
| + // Equal with this very pointer recorded → allowed (a retry). |
| + writeFloorFixture(t, f.a.optDir, systemFloor{"stable": {PromotedAt: req.PromotedAt, Pointer: pointerID(t, f.a)}}) |
| + if err := f.a.Run(context.Background(), false); err != nil { |
| + t.Fatal(err) |
| + } |
| if r := f.result(); r == nil || r.Outcome != systemOutcomeNoted { |
| t.Fatalf("result = %+v", r) |
| } |
| + os.Remove(filepath.Join(systemRootDir(f.a.optDir), "floor.json")) |
| // Another channel's newer watermark does not block this channel. |
| writeJSONDurable(filepath.Join(systemRootDir(f.a.optDir), "seen.json"), systemSeen{"canary": req.PromotedAt.Add(time.Hour)}, 0o644) |
| if err := f.a.Run(context.Background(), false); err != nil { |
diff --git a/internal/host/systemleg.go b/internal/host/systemleg.go| index cf82c59a7..a4ef3540d 100644 |
| --- a/internal/host/systemleg.go |
| +++ b/internal/host/systemleg.go |
| @@ -131,9 +131,8 @@ func (l *systemLeg) observe(ctx context.Context) { |
| } |
| return |
| } |
| - seen, _ := readSystemSeen(l.optDir) |
| - ids, _ := readSystemSeenPointers(l.optDir) |
| - if !systemSeenAdmits(seen, ids, l.channel, p) { |
| + floor, _ := readSystemFloor(l.optDir) |
| + if !systemSeenAdmits(floor, l.channel, p) { |
| return // root would refuse it as a replay |
| } |
| if res, _ := readSystemResult(l.optDir); res.matches(l.channel, p.Current.Version, p.PromotedAt) && res.terminal() { |
| @@ -142,7 +141,7 @@ func (l *systemLeg) observe(ctx context.Context) { |
| } |
| noop := p.Current.Version == l.build |
| if noop { |
| - if wm, ok := seen[l.channel]; ok && !p.PromotedAt.After(wm) { |
| + if wm, ok := floor[l.channel]; ok && !p.PromotedAt.After(wm.PromotedAt) { |
| return // nothing to do, and root has seen this promote |
| } |
| } |
diff --git a/internal/host/systemseen_test.go b/internal/host/systemseen_test.go| index aba66ed71..a29d413b0 100644 |
| --- a/internal/host/systemseen_test.go |
| +++ b/internal/host/systemseen_test.go |
| @@ -26,31 +26,29 @@ func TestSystemSeenAdmitsOnlyANewerStampOrTheAdmittedPointer(t *testing.T) { |
| return p |
| } |
| admitted := ptr("v2", at, "v1", "v2") |
| - seen := systemSeen{"stable": at} |
| - ids := systemSeenPointers{"stable": release.SystemPointerIdentity(admitted)} |
| + floor := systemFloor{"stable": {PromotedAt: at, Pointer: release.SystemPointerIdentity(admitted)}} |
| fenced := *admitted |
| fenced.CancellationID = "c-1" |
| cases := []struct { |
| - name string |
| - seen systemSeen |
| - ids systemSeenPointers |
| - p *release.SystemPointer |
| - want bool |
| + name string |
| + floor systemFloor |
| + p *release.SystemPointer |
| + want bool |
| }{ |
| - {"no entry for the channel admits the first pointer", systemSeen{}, systemSeenPointers{}, ptr("v2", at, "v1"), true}, |
| - {"strictly later is news", seen, ids, ptr("v4", at.Add(time.Second), "v2"), true}, |
| - {"strictly later is news whatever it lists", seen, ids, ptr("v1", at.Add(time.Second)), true}, |
| - {"older is a replay", seen, ids, ptr("v4", at.Add(-time.Second), "v2"), false}, |
| - {"same second, the admitted pointer again (a retry)", seen, ids, ptr("v2", at, "v1", "v2"), true}, |
| - {"same second, the admitted pointer's cancellation fence", seen, ids, &fenced, true}, |
| - {"same second, a saved v1 listing the admitted v2 (Astra P1-1)", seen, ids, ptr("v1", at, "v2"), false}, |
| - {"same second, the admitted version in another pointer", seen, ids, ptr("v2", at, "v1"), false}, |
| - {"same second, a stranger", seen, ids, ptr("v9", at), false}, |
| - {"same second with no identity recorded admits once, as before", seen, systemSeenPointers{}, ptr("v1", at), true}, |
| - {"another channel's entry is not this channel's", systemSeen{"canary": at.Add(time.Hour)}, systemSeenPointers{"canary": "x"}, ptr("v2", at), true}, |
| + {"no entry for the channel admits the first pointer", systemFloor{}, ptr("v2", at, "v1"), true}, |
| + {"strictly later is news", floor, ptr("v4", at.Add(time.Second), "v2"), true}, |
| + {"strictly later is news whatever it lists", floor, ptr("v1", at.Add(time.Second)), true}, |
| + {"older is a replay", floor, ptr("v4", at.Add(-time.Second), "v2"), false}, |
| + {"same second, the admitted pointer again (a retry)", floor, ptr("v2", at, "v1", "v2"), true}, |
| + {"same second, the admitted pointer marked as a fence is another pointer", floor, &fenced, false}, |
| + {"same second, a saved v1 listing the admitted v2 (Astra P1-1)", floor, ptr("v1", at, "v2"), false}, |
| + {"same second, the admitted version in another pointer", floor, ptr("v2", at, "v1"), false}, |
| + {"same second, a stranger", floor, ptr("v9", at), false}, |
| + {"same second with no identity recorded admits nothing (Astra round 2)", systemFloor{"stable": {PromotedAt: at}}, ptr("v1", at), false}, |
| + {"another channel's entry is not this channel's", systemFloor{"canary": {PromotedAt: at.Add(time.Hour), Pointer: "x"}}, ptr("v2", at), true}, |
| } |
| for _, c := range cases { |
| - if got := systemSeenAdmits(c.seen, c.ids, "stable", c.p); got != c.want { |
| + if got := systemSeenAdmits(c.floor, "stable", c.p); got != c.want { |
| t.Errorf("%s: admits = %v, want %v", c.name, got, c.want) |
| } |
| } |
| @@ -76,15 +74,15 @@ func TestSystemRollbackInTheSameSecondIsFollowedAndNothingReplays(t *testing.T) |
| } |
| return p, raw |
| } |
| - seen, ids := systemSeen{}, systemSeenPointers{} |
| + floor := systemFloor{} |
| var followed []*release.SystemPointer |
| follow := func(what string) { |
| t.Helper() |
| p, _ := read() |
| - if !systemSeenAdmits(seen, ids, "stable", p) { |
| + if !systemSeenAdmits(floor, "stable", p) { |
| t.Fatalf("%s (%s promoted %s) refused by the floor", what, p.Current.Version, p.PromotedAt) |
| } |
| - seen["stable"], ids["stable"] = p.PromotedAt, release.SystemPointerIdentity(p) |
| + floor["stable"] = systemFloorEntry{PromotedAt: p.PromotedAt, Pointer: release.SystemPointerIdentity(p)} |
| followed = append(followed, p) |
| } |
| f.promote("v1", []byte("one"), 0) |
| @@ -100,25 +98,39 @@ func TestSystemRollbackInTheSameSecondIsFollowedAndNothingReplays(t *testing.T) |
| } |
| f.promote("v3", []byte("three"), 0) |
| follow("promote after the rollback") |
| + // A cancelled activation (P2-3): its fence restores v3 under a stamp |
| + // of its own, strictly later, and is followed like any other head. |
| + prepared, err := release.PrepareSystemPromotion(ctx, f.store, release.RootSigner(f.priv), release.SystemComponentRouter, "stable", |
| + release.SystemRelease{Version: "v4", Artifacts: map[string]release.SystemArtifact{ |
| + "linux-arm64": {Key: release.SystemArtifactKey("v4", "linux-arm64"), SHA256: strings.Repeat("ab", 32)}}}, release.SystemPromoteOptions{}, "act-1") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := release.CancelSystemPublication(ctx, f.store, release.RootSigner(f.priv), prepared); err != nil { |
| + t.Fatal(err) |
| + } |
| + follow("cancellation fence") |
| + if p := followed[len(followed)-1]; p.CancellationID != "act-1" || p.Current.Version != "v3" { |
| + t.Fatalf("fence names %s (cancellation %q)", p.Current.Version, p.CancellationID) |
| + } |
| + f.promote("v5", []byte("five"), 0) |
| + follow("promote after the cancellation") |
| for i := 1; i < len(followed); i++ { |
| if !followed[i].PromotedAt.After(followed[i-1].PromotedAt) { |
| t.Fatalf("write %d stamped %s, not after %s", i, followed[i].PromotedAt, followed[i-1].PromotedAt) |
| } |
| } |
| for i, p := range followed[:len(followed)-1] { |
| - if systemSeenAdmits(seen, ids, "stable", p) { |
| + if systemSeenAdmits(floor, "stable", p) { |
| t.Fatalf("write %d (%s promoted %s) replays past the floor", i, p.Current.Version, p.PromotedAt) |
| } |
| } |
| } |
| -func writeSeenFixture(t *testing.T, optDir string, seen systemSeen, ids systemSeenPointers) { |
| +func writeFloorFixture(t *testing.T, optDir string, floor systemFloor) { |
| t.Helper() |
| os.MkdirAll(systemRootDir(optDir), 0o755) |
| - if err := writeJSONDurable(filepath.Join(systemRootDir(optDir), "seen.json"), seen, 0o644); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := writeJSONDurable(filepath.Join(systemRootDir(optDir), "seen-pointers.json"), ids, 0o644); err != nil { |
| + if err := writeJSONDurable(filepath.Join(systemRootDir(optDir), "floor.json"), floor, 0o644); err != nil { |
| t.Fatal(err) |
| } |
| } |
| @@ -137,17 +149,17 @@ func pointerID(t *testing.T, a *systemApplier) string { |
| // passes the floor and is installed; both halves of the watermark advance. |
| func TestApplyStrictlyLaterPromotionPassesTheFloor(t *testing.T) { |
| f, req := applyHappy(t) |
| - writeSeenFixture(t, f.a.optDir, systemSeen{"stable": req.PromotedAt.Add(-time.Second)}, systemSeenPointers{"stable": "an-older-pointer"}) |
| + writeFloorFixture(t, f.a.optDir, systemFloor{"stable": {PromotedAt: req.PromotedAt.Add(-time.Second), Pointer: "an-older-pointer"}}) |
| if err := f.a.Run(context.Background(), false); err != nil { |
| t.Fatal(err) |
| } |
| if r := f.result(); r == nil || r.Outcome != systemOutcomeApplied || f.onDisk() != "v2" { |
| t.Fatalf("result %+v, on disk %q", r, f.onDisk()) |
| } |
| + floor, _ := readSystemFloor(f.a.optDir) |
| seen, _ := readSystemSeen(f.a.optDir) |
| - ids, _ := readSystemSeenPointers(f.a.optDir) |
| - if want := pointerID(t, f.a); !seen["stable"].Equal(req.PromotedAt) || ids["stable"] != want { |
| - t.Fatalf("watermark = %v / %v, want %v / %v", seen["stable"], ids["stable"], req.PromotedAt, want) |
| + if want := pointerID(t, f.a); !floor["stable"].PromotedAt.Equal(req.PromotedAt) || floor["stable"].Pointer != want || !seen["stable"].Equal(req.PromotedAt) { |
| + t.Fatalf("floor = %+v, seen.json %v, want %v / %v", floor["stable"], seen["stable"], req.PromotedAt, want) |
| } |
| } |
| @@ -157,7 +169,7 @@ func TestApplyStrictlyLaterPromotionPassesTheFloor(t *testing.T) { |
| // itself at the same stamp is the retry the spec allows. |
| func TestApplySameSecondSupersededPromotionIsRefusedButARetryProceeds(t *testing.T) { |
| f, req := applyHappy(t) |
| - writeSeenFixture(t, f.a.optDir, systemSeen{"stable": req.PromotedAt}, systemSeenPointers{"stable": "another-pointer"}) |
| + writeFloorFixture(t, f.a.optDir, systemFloor{"stable": {PromotedAt: req.PromotedAt, Pointer: "another-pointer"}}) |
| if err := f.a.Run(context.Background(), false); err != nil { |
| t.Fatal(err) |
| } |
| @@ -165,10 +177,10 @@ func TestApplySameSecondSupersededPromotionIsRefusedButARetryProceeds(t *testing |
| if r == nil || r.Outcome != systemOutcomeRefused || !strings.Contains(r.Reason, "replay") || f.onDisk() != "v1" { |
| t.Fatalf("result %+v, on disk %q", r, f.onDisk()) |
| } |
| - if ids, _ := readSystemSeenPointers(f.a.optDir); ids["stable"] != "another-pointer" { |
| - t.Fatalf("a refused replay moved the watermark: %v", ids) |
| + if floor, _ := readSystemFloor(f.a.optDir); floor["stable"].Pointer != "another-pointer" { |
| + t.Fatalf("a refused replay moved the watermark: %v", floor) |
| } |
| - writeSeenFixture(t, f.a.optDir, systemSeen{"stable": req.PromotedAt}, systemSeenPointers{"stable": pointerID(t, f.a)}) |
| + writeFloorFixture(t, f.a.optDir, systemFloor{"stable": {PromotedAt: req.PromotedAt, Pointer: pointerID(t, f.a)}}) |
| if err := f.a.Run(context.Background(), false); err != nil { |
| t.Fatal(err) |
| } |
| @@ -218,7 +230,7 @@ func TestSystemLegSkipsASameSecondSupersededPromotion(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - writeSeenFixture(t, f.opt, systemSeen{"stable": p.PromotedAt}, systemSeenPointers{"stable": "another-pointer"}) |
| + writeFloorFixture(t, f.opt, systemFloor{"stable": {PromotedAt: p.PromotedAt, Pointer: "another-pointer"}}) |
| f.leg.observe(context.Background()) |
| f.leg.tick(context.Background(), f.now) |
| if f.leg.pending != nil { |
| @@ -227,7 +239,7 @@ func TestSystemLegSkipsASameSecondSupersededPromotion(t *testing.T) { |
| if _, err := f.marker(); err == nil { |
| t.Fatal("requested a pointer root would refuse as a replay") |
| } |
| - writeSeenFixture(t, f.opt, systemSeen{"stable": p.PromotedAt}, systemSeenPointers{"stable": release.SystemPointerIdentity(p)}) |
| + writeFloorFixture(t, f.opt, systemFloor{"stable": {PromotedAt: p.PromotedAt, Pointer: release.SystemPointerIdentity(p)}}) |
| f.leg.observe(context.Background()) |
| f.leg.tick(context.Background(), f.now) |
| if r := f.request(); r == nil || r.Version != "v2" || r.NoOp { |
diff --git a/internal/host/systemstate.go b/internal/host/systemstate.go| index 871af2697..349080da0 100644 |
| --- a/internal/host/systemstate.go |
| +++ b/internal/host/systemstate.go |
| @@ -19,8 +19,8 @@ import ( |
| // request and the artifact cache. Root |
| // reads exactly one file here and trusts |
| // nothing in it beyond a version string. |
| -// <opt>/system/router/ — written by root: journal, seen (replay |
| -// watermarks), applied (the .prev |
| +// <opt>/system/router/ — written by root: journal, floor and |
| +// seen (replay floors), applied (the .prev |
| // authority), result (what the agent |
| // publishes). Root-owned, world-readable. |
| const ( |
| @@ -89,55 +89,93 @@ type systemApplied struct { |
| At time.Time `json:"at"` |
| } |
| -// systemSeen is the replay watermark per channel: the newest PromotedAt root |
| -// has verified. Advanced on every verified pointer, including no-ops. |
| +// systemSeen is seen.json: the newest PromotedAt root has verified, per |
| +// channel. It is the floor every release before hunt H6-M11 kept, and this |
| +// one still writes it, after systemFloor, for a box that rolls back to such |
| +// a release (systemFloorAdvance). It carries no identity, so at its own |
| +// stamp it admits nothing (readSystemFloor). |
| type systemSeen map[string]time.Time |
| -// systemSeenPointers is the other half of the watermark (hunt H6-M11): the |
| -// identity (release.SystemPointerIdentity) of the pointer root admitted at |
| -// seen.json's stamp, per channel, in seen-pointers.json beside it. A |
| -// channel with a stamp but no identity (a box that last admitted a pointer |
| -// before this file existed) admits an equal stamp once, as it did before, |
| -// and records its identity then. |
| -type systemSeenPointers map[string]string |
| +// systemFloorEntry is one channel's replay floor (hunt H6-M11): the stamp |
| +// and the identity (release.SystemPointerIdentity) of the pointer root |
| +// admitted, kept together in one record, floor.json, written in one atomic |
| +// replace. Two files written in turn could be cut off between them, and a |
| +// stamp left beside the previous pointer's identity refused every retry of |
| +// the pointer that had just been admitted (Astra round 2, finding 1). |
| +type systemFloorEntry struct { |
| + PromotedAt time.Time `json:"promoted_at"` |
| + Pointer string `json:"pointer,omitempty"` |
| +} |
| + |
| +// systemFloor is floor.json, per channel. |
| +type systemFloor map[string]systemFloorEntry |
| // systemSeenAdmits is the replay floor both halves of the box apply to a |
| // verified pointer: the agent before it schedules one (systemleg.go), root |
| // before it acts on one (systemapply.go, installchannel.go). It is the |
| // system-pointer twin of release's watermark.admits. A strictly later stamp |
| // is news. Every system signer stamps a new head strictly after the one it |
| -// replaces (PrepareSystemPromotion and PrepareSystemRollback wait past it), |
| -// so at the floor's own stamp only the pointer already admitted passes, read |
| -// again (a retry, or the cancellation fence that re-signs it). Anything |
| -// else at that stamp, or older, is a replay. RollbackSystem pops History, |
| -// so history could never tell a rollback from a replay; the stamp does. |
| -func systemSeenAdmits(seen systemSeen, ids systemSeenPointers, channel string, p *release.SystemPointer) bool { |
| - wm, ok := seen[channel] |
| +// replaces (PrepareSystemPromotion and PrepareSystemRollback wait past it, |
| +// and a cancellation fence takes its publication's later stamp), so at the |
| +// floor's own stamp only the pointer already admitted passes, byte for |
| +// byte, read again (a retry). A floor with no identity at its stamp (seen.json |
| +// alone, from a release before floor.json) admits nothing there: it cannot |
| +// tell that pointer from a saved one signed in the same second. Anything |
| +// older is a replay. RollbackSystem pops History, so history could never |
| +// tell a rollback from a replay; the stamp does. |
| +func systemSeenAdmits(floor systemFloor, channel string, p *release.SystemPointer) bool { |
| + wm, ok := floor[channel] |
| switch { |
| - case !ok || p.PromotedAt.After(wm): |
| + case !ok || p.PromotedAt.After(wm.PromotedAt): |
| return true |
| - case p.PromotedAt.Before(wm): |
| + case p.PromotedAt.Before(wm.PromotedAt): |
| return false |
| } |
| - admitted, known := ids[channel] |
| - if !known { |
| - return true |
| - } |
| - return release.SystemPointerIdentity(p) == admitted |
| + return wm.Pointer != "" && release.SystemPointerIdentity(p) == wm.Pointer |
| } |
| -// systemSeenAdvance records p as the newest pointer admitted on channel, |
| -// seen.json first and then seen-pointers.json, both durable. An identity |
| -// write that fails leaves the stamp in place with the previous identity |
| -// beside it, which refuses the new pointer at that stamp until the next |
| -// admission writes it (fail closed). |
| -func systemSeenAdvance(optDir string, seen systemSeen, ids systemSeenPointers, channel string, p *release.SystemPointer) error { |
| - seen[channel] = p.PromotedAt |
| - ids[channel] = release.SystemPointerIdentity(p) |
| - if err := writeJSONDurable(filepath.Join(systemRootDir(optDir), "seen.json"), seen, 0o644); err != nil { |
| +// systemFloorAdvance records p as the newest pointer admitted on channel: |
| +// floor.json in one durable replace, the stamp and the identity together, |
| +// then seen.json's stamps for an older release to read. A cut anywhere |
| +// leaves floor.json either as it was or with p in it, so the retry of p is |
| +// admitted either way; seen.json is never read past floor.json |
| +// (readSystemFloor), so a stale one changes nothing for this release. |
| +func systemFloorAdvance(optDir string, floor systemFloor, channel string, p *release.SystemPointer) error { |
| + floor[channel] = systemFloorEntry{PromotedAt: p.PromotedAt, Pointer: release.SystemPointerIdentity(p)} |
| + if err := writeJSONDurable(filepath.Join(systemRootDir(optDir), "floor.json"), floor, 0o644); err != nil { |
| return err |
| } |
| - return writeJSONDurable(filepath.Join(systemRootDir(optDir), "seen-pointers.json"), ids, 0o644) |
| + seen := systemSeen{} |
| + for ch, e := range floor { |
| + seen[ch] = e.PromotedAt |
| + } |
| + return writeJSONDurable(filepath.Join(systemRootDir(optDir), "seen.json"), seen, 0o644) |
| +} |
| + |
| +// readSystemFloor is the box's replay floor: floor.json, and seen.json |
| +// beneath it. A seen.json stamp later than floor.json's for its channel |
| +// (an older release advanced it after a rollback, or the box has never |
| +// written floor.json) is the floor there, with no identity. Either file |
| +// unreadable is an error, which every caller treats as a refusal. |
| +func readSystemFloor(optDir string) (systemFloor, error) { |
| + floor := systemFloor{} |
| + err := readJSONFile(filepath.Join(systemRootDir(optDir), "floor.json"), &floor) |
| + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| + return systemFloor{}, err |
| + } |
| + if floor == nil { |
| + floor = systemFloor{} // a literal `null` on disk unmarshals to a nil map |
| + } |
| + seen, err := readSystemSeen(optDir) |
| + if err != nil { |
| + return systemFloor{}, err |
| + } |
| + for ch, at := range seen { |
| + if e, ok := floor[ch]; !ok || at.After(e.PromotedAt) { |
| + floor[ch] = systemFloorEntry{PromotedAt: at} |
| + } |
| + } |
| + return floor, nil |
| } |
| // systemJournal is the open transaction. Written before every box-changing |
| @@ -255,18 +293,6 @@ func readSystemSeen(optDir string) (systemSeen, error) { |
| return s, err |
| } |
| -func readSystemSeenPointers(optDir string) (systemSeenPointers, error) { |
| - v := systemSeenPointers{} |
| - err := readJSONFile(filepath.Join(systemRootDir(optDir), "seen-pointers.json"), &v) |
| - if errors.Is(err, os.ErrNotExist) { |
| - return systemSeenPointers{}, nil |
| - } |
| - if v == nil { |
| - v = systemSeenPointers{} |
| - } |
| - return v, err |
| -} |
| - |
| func readSystemApplied(optDir string) (*systemApplied, error) { |
| var a systemApplied |
| err := readJSONFile(filepath.Join(systemRootDir(optDir), "applied.json"), &a) |