Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-journal-prune-clock.md
Original file line number Diff line number Diff line change
@@ -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).
14 changes: 13 additions & 1 deletion src/pkg/convergence/mutation/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions src/pkg/convergence/mutation/journal_prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading