; persist/compaction — once a snapshot subsumes a log prefix, those events are
; dead weight: replay starts from the snapshot, so events with seq <= the
; snapshot's seq are never folded again. Compaction checkpoints then truncates
; that prefix. The seq counter is monotonic (backend high-water mark) so future
; appends keep climbing — the surviving tail keeps its original seqs and replay
; from the snapshot still equals a full replay of the pre-compaction log.
; Policy is explicit: compact when the uncompacted tail reaches `every` events.
; Requires: lib/persist/snapshot.sx, lib/persist/log.sx.

; events accumulated since the last snapshot for name
(define
  persist/uncompacted
  (fn
    (b stream name seed)
    (-
      (persist/last-seq b stream)
      (persist/project-seq (persist/snapshot-load b name seed)))))

; policy: should we compact yet? tail since snapshot >= every
(define
  persist/should-compact?
  (fn
    (b stream name every seed)
    (>= (persist/uncompacted b stream name seed) every)))

; checkpoint then drop the snapshotted prefix; returns the new snapshot state
(define
  persist/compact
  (fn
    (b stream name step seed)
    (let
      ((state (persist/checkpoint b stream name step seed)))
      (begin (persist/truncate b stream (persist/project-seq state)) state))))

; compact only if the policy fires; always returns the current snapshot state
(define
  persist/maybe-compact
  (fn
    (b stream name step seed every)
    (if
      (persist/should-compact? b stream name every seed)
      (persist/compact b stream name step seed)
      (persist/snapshot-load b name seed))))
