From 5a64f67620fab06a5f98eca9912d73e09a6b814c Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 02:08:15 -0400 Subject: [PATCH] fix(mutation): bound journal prune clock to the journal's own time Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- changelog.d/fixed-journal-prune-clock.md | 1 + src/pkg/convergence/mutation/journal.go | 14 +++++++++++++- .../mutation/journal_prune_test.go | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 changelog.d/fixed-journal-prune-clock.md diff --git a/changelog.d/fixed-journal-prune-clock.md b/changelog.d/fixed-journal-prune-clock.md new file mode 100644 index 0000000000..ba02ca17e5 --- /dev/null +++ b/changelog.d/fixed-journal-prune-clock.md @@ -0,0 +1 @@ +- The mutation journal now clamps the timestamp used for retention pruning to its own clock, so a skewed or forged future timestamp on a commit can no longer age out other journal entries early (#14830). diff --git a/src/pkg/convergence/mutation/journal.go b/src/pkg/convergence/mutation/journal.go index e82f57e83e..f7adbcd18f 100644 --- a/src/pkg/convergence/mutation/journal.go +++ b/src/pkg/convergence/mutation/journal.go @@ -231,6 +231,7 @@ type Journal struct { lockTimeout time.Duration retention time.Duration maxEntries int + now func() time.Time mu sync.Mutex ops map[string]*Operation } @@ -247,6 +248,7 @@ func OpenJournal(path string) (*Journal, error) { lockTimeout: DefaultJournalLockTimeout, retention: DefaultJournalRetention, maxEntries: DefaultJournalMaxEntries, + now: time.Now, ops: make(map[string]*Operation), } if err := j.reloadLocked(); err != nil { @@ -526,7 +528,7 @@ func (j *Journal) commit(id string, work Operation) (Operation, error) { prior, hadPrior := j.ops[id] cp := work.clone() j.ops[id] = &cp - j.pruneLocked(work.UpdatedAt) + j.pruneLocked(j.pruneClock(work.UpdatedAt)) if err := j.persistLocked(); err != nil { if hadPrior { j.ops[id] = prior @@ -538,6 +540,16 @@ func (j *Journal) commit(id string, work Operation) (Operation, error) { return work.clone(), nil } +// pruneClock picks the instant retention is measured from. The caller-supplied +// instant is clamped to the journal's own clock so a skewed or forged future +// timestamp cannot age out other entries early. +func (j *Journal) pruneClock(supplied time.Time) time.Time { + if own := j.now(); supplied.After(own) { + return own + } + return supplied +} + // isTerminal reports whether an operation can never transition again on its // own: Applied is terminal truth and NotApplied only moves through a fresh // Begin, which re-derives the same logical ID from the effect. diff --git a/src/pkg/convergence/mutation/journal_prune_test.go b/src/pkg/convergence/mutation/journal_prune_test.go index b6975c0a7f..d8de5ebfb5 100644 --- a/src/pkg/convergence/mutation/journal_prune_test.go +++ b/src/pkg/convergence/mutation/journal_prune_test.go @@ -126,3 +126,22 @@ func TestJournal_ZeroBoundsDisablePruning(t *testing.T) { t.Fatalf("unbounded journal must keep every entry: %d", got) } } + +// Retention is measured against the journal's own clock: a commit carrying a +// forged future timestamp must not age out other terminal entries. +func TestJournal_PruneIgnoresForgedFutureTimestamp(t *testing.T) { + j, err := OpenJournal(filepath.Join(t.TempDir(), "journal.json")) + if err != nil { + t.Fatalf("OpenJournal: %v", err) + } + j.retention = time.Hour + base := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) + j.now = func() time.Time { return base } + + kept := applyN(t, j, 1, base.Add(-30*time.Minute)) + applyN(t, j, 2, base.Add(100*time.Hour)) + + if _, ok := j.Get(kept.LogicalID); !ok { + t.Fatal("a forged future timestamp must not prune entries inside the retention window") + } +}