Skip to main content

car_sync/
compact.rs

1//! Compaction + oplog GC (slice B4 of `docs/proposals/multi-device-sync.md`,
2//! §"Deep dive: the checkpoint / compaction / GC protocol").
3//!
4//! Compaction = **checkpoint at the stable frontier, then drop the ops below
5//! it**. Three guards make that safe, each straight from the proposal:
6//!
7//! 1. **The frontier is the min acked HLC over every known device**
8//!    ([`AckTable::stable_frontier`]) — "GC of a CRDT log is only safe once
9//!    every replica has folded past the truncation point". Compaction
10//!    refuses to run at all when a device present in the log has no ack
11//!    entry ([`CompactError::UnackedDevice`]) or when nothing is acked
12//!    ([`CompactError::NothingAcked`]): ops above ANY device's acked
13//!    frontier are data another device hasn't seen, and are never dropped.
14//!    (Device eviction — the horizon `H` that unpins a dead laptop — is
15//!    relay policy, B3.)
16//! 2. **Checkpoint durable FIRST, then truncate**
17//!    ([`compact_and_truncate`]). The crash-ordering invariant: a crash
18//!    after the checkpoint fsync but before the journal rename leaves the
19//!    full journal plus a redundant checkpoint (harmless — rerunning
20//!    compaction recomputes the identical content-addressed file); the
21//!    rename itself is atomic, so mid-truncation crashes leave either the
22//!    old complete journal or the new complete tail. **At no point does
23//!    acknowledged data exist only in a file that isn't durably written.**
24//! 3. **Retention is applied to the checkpoint state, never to the tail.**
25//!    The proposal's per-surface retention table
26//!    ([`RetentionPolicy::proposal_default`]) trims what the snapshot
27//!    *keeps*; ops above the frontier are untouched by policy. **Replay**
28//!    surfaces (routing observations — path-dependent, replayed from genesis;
29//!    [`crate::oplog::Surface::is_replay_stream`]) additionally reject any rule
30//!    but keep-all ([`CompactError::EventStreamRetention`]), so an unacked (or
31//!    acked!) observation tail can never be compacted away. (Conversation is
32//!    an event-stream multiset too — B2 — but its turns are independent, so it
33//!    tolerates `LastN`; only replay streams are forbidden.) **Every
34//!    retention-dropped id-bearing entry leaves a minimal tombstone stub**
35//!    (`{"id": …, "tombstone": true}`, original `op_id`/`hlc` kept), so a
36//!    `"supersedes"` reference always resolves against a tombstone, not a
37//!    hole — **including a reference that arrives AFTER compaction**.
38//!    (Preserving only the references visible at compaction time was a
39//!    reproduced divergence: a later tail op superseding an
40//!    already-dropped entry made the global fold retain what the compacted
41//!    device could not. Universal stubs are time-hole-free: both sides
42//!    reduce the same record to the same stub, deterministically. Entries
43//!    with no entity id — e.g. content-hash-keyed conversation turns —
44//!    cannot be referenced by id and drop entirely.) Stubs are carried
45//!    unchanged by later retention passes and never count against a
46//!    surface's retention quota.
47//!
48//! Determinism note (the "same frontier ⇒ same snapshot hash" invariant):
49//! retention is deterministic over a fixed state, and the age-based rules'
50//! reference instant defaults to [`as_of_from_ops`] — the max payload
51//! `"timestamp"` among the ops at/below the frontier, i.e. **pure over the
52//! same inputs the fold already consumes**, so every device compacting the
53//! same frontier derives the same instant with no out-of-band agreement
54//! (this crate never reads a clock; a caller may still pass an explicit
55//! `Some(as_of_ms)`). The derived value is conservative: a stale max
56//! under-drops, and undated entries never age-drop anyway.
57//! Ordering/recency come from the payload's numeric `"timestamp"` field
58//! (ms) and grouping from `"agent_id"`; an entry with no timestamp is
59//! treated as newest / never age-dropped — undated data is never silently
60//! discarded.
61
62use crate::checkpoint::Checkpoint;
63use crate::fold::{FoldedRecord, SyncState};
64use crate::journal::OplogJournal;
65use crate::oplog::{verify_log, ChainError, Hlc, OpRecord};
66use serde::{Deserialize, Serialize};
67use serde_json::{json, Value};
68use std::collections::{BTreeMap, BTreeSet};
69use std::fmt;
70use std::fs::{self, File};
71use std::io::Write;
72use std::path::{Path, PathBuf};
73
74/// Parity constants with today's run GC (`car-server-core::run_store`):
75/// keep the 50 most recent runs per agent…
76pub const RUNS_MAX_PER_AGENT: usize = 50;
77/// …and drop runs older than 30 days (in ms), whichever is more
78/// restrictive — exactly `RunStore::gc`'s rule, made globally coherent.
79pub const RUNS_MAX_AGE_MS: u64 = 30 * 24 * 60 * 60 * 1000;
80
81/// How one surface's checkpoint retention trims — the proposal's table rows.
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83#[serde(rename_all = "snake_case")]
84pub enum RetentionRule {
85    /// Keep everything (knowledge/skills — "the valuable distilled state" —
86    /// and forced for event-stream surfaces).
87    KeepAll,
88    /// Keep the last `n` entries by payload `"timestamp"` (conversations —
89    /// "last N turns by timestamp (= today's `max_turns`)").
90    LastN { n: usize },
91    /// Keep entries no older than `max_age_ms` relative to the compaction's
92    /// `as_of_ms` (trajectories — "last D days").
93    MaxAgeMs { max_age_ms: u64 },
94    /// Keep the most recent `max_per_agent` per payload `"agent_id"` AND
95    /// drop anything older than `max_age_ms` — both restrictive, the
96    /// `RunStore::gc` rule (runs — "50 / agent + 30 days").
97    PerAgentWithAge {
98        max_per_agent: usize,
99        max_age_ms: u64,
100    },
101}
102
103/// Per-surface retention policy: surface tag → rule, with a conservative
104/// keep-all default for unknown surfaces. Applies to the grow-only log tier
105/// only — the LWW registry tier is already one compact record per id, and
106/// the proposal's table assigns it no retention.
107#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108pub struct RetentionPolicy {
109    pub rules: BTreeMap<String, RetentionRule>,
110    pub default_rule: RetentionRule,
111}
112
113impl Default for RetentionPolicy {
114    fn default() -> Self {
115        Self::keep_all()
116    }
117}
118
119impl RetentionPolicy {
120    /// Retain everything — the exact-checkpoint policy (compaction still
121    /// drops ops; the snapshot just keeps their full folded state).
122    pub fn keep_all() -> Self {
123        Self {
124            rules: BTreeMap::new(),
125            default_rule: RetentionRule::KeepAll,
126        }
127    }
128
129    /// The proposal's retention table. Two rows are deployment-configurable
130    /// in the proposal itself and therefore parameters here: conversations'
131    /// `N` ("= today's `max_turns`" — a memgine config value, not a global
132    /// constant) and trajectories' `D` days (given as `max_age_ms`). Runs
133    /// use the shipped `RunStore::gc` constants
134    /// ([`RUNS_MAX_PER_AGENT`]/[`RUNS_MAX_AGE_MS`]); knowledge, skills, and
135    /// routing observations keep all (routing's keep-all is also enforced
136    /// structurally — see [`CompactError::EventStreamRetention`]).
137    pub fn proposal_default(conversation_last_n: usize, trajectory_max_age_ms: u64) -> Self {
138        let mut rules = BTreeMap::new();
139        rules.insert(
140            "conversation".to_string(),
141            RetentionRule::LastN {
142                n: conversation_last_n,
143            },
144        );
145        rules.insert(
146            "run".to_string(),
147            RetentionRule::PerAgentWithAge {
148                max_per_agent: RUNS_MAX_PER_AGENT,
149                max_age_ms: RUNS_MAX_AGE_MS,
150            },
151        );
152        rules.insert(
153            "trajectory".to_string(),
154            RetentionRule::MaxAgeMs {
155                max_age_ms: trajectory_max_age_ms,
156            },
157        );
158        rules.insert("knowledge".to_string(), RetentionRule::KeepAll);
159        rules.insert("skill".to_string(), RetentionRule::KeepAll);
160        rules.insert("routing".to_string(), RetentionRule::KeepAll);
161        Self {
162            rules,
163            default_rule: RetentionRule::KeepAll,
164        }
165    }
166
167    pub fn rule_for(&self, surface_tag: &str) -> &RetentionRule {
168        self.rules.get(surface_tag).unwrap_or(&self.default_rule)
169    }
170}
171
172/// What retention did: per-surface counts of entries dropped entirely
173/// (id-less — nothing can reference them) and the entries reduced to
174/// tombstone stubs, as `(surface_tag, key)`.
175#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
176pub struct RetentionReport {
177    pub dropped: BTreeMap<String, usize>,
178    pub tombstoned: Vec<(String, String)>,
179}
180
181fn value_ts(payload: &Value) -> Option<u64> {
182    payload
183        .get("timestamp")
184        .and_then(|v| v.as_u64().or_else(|| v.as_f64().map(|f| f as u64)))
185}
186
187fn payload_ts(record: &FoldedRecord) -> Option<u64> {
188    value_ts(&record.payload)
189}
190
191/// The deterministic default reference instant for age-based retention:
192/// the max payload `"timestamp"` across `ops` (0 when none carry one).
193/// Pure over the same inputs the fold consumes, so every device compacting
194/// the same frontier derives the identical value — no out-of-band
195/// agreement, no clock read. Conservative by construction: a stale max
196/// under-drops (age rules see everything as newer), and undated entries
197/// never age-drop regardless.
198pub fn as_of_from_ops(ops: &[OpRecord]) -> u64 {
199    ops.iter()
200        .filter_map(|op| value_ts(&op.payload))
201        .max()
202        .unwrap_or(0)
203}
204
205fn within_age(record: &FoldedRecord, as_of_ms: u64, max_age_ms: u64) -> bool {
206    match payload_ts(record) {
207        // Future-stamped entries have age 0 (saturating) — kept.
208        Some(ts) => as_of_ms.saturating_sub(ts) <= max_age_ms,
209        // Undated data is never age-dropped.
210        None => true,
211    }
212}
213
214/// Deterministic recency order: ascending `(timestamp, hlc, op_id)`, with a
215/// missing timestamp sorting as newest (never preferentially dropped).
216fn recency_sorted(entries: &BTreeMap<String, FoldedRecord>) -> Vec<(&String, &FoldedRecord)> {
217    let mut sorted: Vec<(&String, &FoldedRecord)> = entries.iter().collect();
218    sorted.sort_by(|(_, a), (_, b)| {
219        (payload_ts(a).unwrap_or(u64::MAX), &a.hlc, &a.op_id).cmp(&(
220            payload_ts(b).unwrap_or(u64::MAX),
221            &b.hlc,
222            &b.op_id,
223        ))
224    });
225    sorted
226}
227
228fn select_retained(
229    entries: &BTreeMap<String, FoldedRecord>,
230    rule: &RetentionRule,
231    as_of_ms: u64,
232) -> BTreeSet<String> {
233    match rule {
234        RetentionRule::KeepAll => entries.keys().cloned().collect(),
235        RetentionRule::LastN { n } => recency_sorted(entries)
236            .into_iter()
237            .rev()
238            .take(*n)
239            .map(|(k, _)| k.clone())
240            .collect(),
241        RetentionRule::MaxAgeMs { max_age_ms } => entries
242            .iter()
243            .filter(|(_, r)| within_age(r, as_of_ms, *max_age_ms))
244            .map(|(k, _)| k.clone())
245            .collect(),
246        RetentionRule::PerAgentWithAge {
247            max_per_agent,
248            max_age_ms,
249        } => {
250            // Rank per agent over ALL entries (recency), then apply both
251            // caps restrictively — RunStore::gc's exact semantics.
252            let mut per_agent_rank: BTreeMap<&str, usize> = BTreeMap::new();
253            let mut keep = BTreeSet::new();
254            for (key, record) in recency_sorted(entries).into_iter().rev() {
255                let agent = record
256                    .payload
257                    .get("agent_id")
258                    .and_then(Value::as_str)
259                    .unwrap_or("");
260                let rank = per_agent_rank.entry(agent).or_insert(0);
261                let over_count = *rank >= *max_per_agent;
262                *rank += 1;
263                if !over_count && within_age(record, as_of_ms, *max_age_ms) {
264                    keep.insert(key.clone());
265                }
266            }
267            keep
268        }
269    }
270}
271
272/// The entity id a folded log entry answers to for tombstone stubs: the
273/// `id:` key form, else the payload's own `"id"`.
274fn entity_id<'a>(key: &'a str, record: &'a FoldedRecord) -> Option<&'a str> {
275    key.strip_prefix("id:")
276        .or_else(|| record.payload.get("id").and_then(Value::as_str))
277}
278
279/// Is this record a retention tombstone stub? (`"tombstone": true` is the
280/// reserved marker [`apply_retention`] stamps.)
281pub fn is_tombstone(record: &FoldedRecord) -> bool {
282    record
283        .payload
284        .get("tombstone")
285        .and_then(Value::as_bool)
286        .unwrap_or(false)
287}
288
289/// The minimal stub a retention-dropped entry leaves behind: the entity id
290/// plus the tombstone marker, under the record's original `op_id`/`hlc`.
291/// Deterministic from the dropped record, so every device reduces it to the
292/// identical stub. Stubbing is idempotent (a stub of a stub is itself).
293fn tombstone_of(record: &FoldedRecord, id: &str) -> FoldedRecord {
294    FoldedRecord {
295        op_id: record.op_id.clone(),
296        hlc: record.hlc.clone(),
297        payload: json!({"id": id, "tombstone": true}),
298    }
299}
300
301/// Apply per-surface retention to a folded (checkpoint) state — the
302/// proposal's "the snapshot applies each surface's retention", with the
303/// §"Two free properties fall out" referential-integrity rule realized as
304/// **universal tombstone stubs**: every dropped id-bearing entry is reduced
305/// to `{"id", "tombstone": true}` (original `op_id`/`hlc` kept) rather than
306/// erased, so a `"supersedes"` reference — even one that arrives after
307/// compaction — always resolves. Stubs never count against a rule's quota
308/// and are carried unchanged by later passes. Pure over its inputs;
309/// `as_of_ms` is the reference instant for the age rules (no clock reads
310/// here — [`plan_compaction`] defaults it via [`as_of_from_ops`]).
311pub fn apply_retention(
312    state: &SyncState,
313    policy: &RetentionPolicy,
314    as_of_ms: u64,
315) -> Result<(SyncState, RetentionReport), CompactError> {
316    // Path-dependent REPLAY surfaces (routing's EMA) are retention-forbidden:
317    // the folded result is recomputed from the ordered multiset, so any trim
318    // silently corrupts every device's replay. Reject loudly.
319    //
320    // This is NARROWER than "event stream": conversation turns are an
321    // op_id-keyed multiset too (B2 — so two genuine same-content turns never
322    // collapse), but they are INDEPENDENT entries, so `LastN` over them is
323    // well-defined and MUST be allowed (conversations need last-N retention).
324    // Only replay streams are rejected here — see `Surface::is_replay_stream`.
325    // Routing is the only one today; its tag is the guard key.
326    let routing_tag = crate::oplog::Surface::Routing.tag();
327    debug_assert!(crate::oplog::Surface::Routing.is_replay_stream());
328    if state.logs.contains_key(&routing_tag)
329        && policy.rule_for(&routing_tag) != &RetentionRule::KeepAll
330    {
331        return Err(CompactError::EventStreamRetention {
332            surface: routing_tag,
333        });
334    }
335
336    // Leased execution intents (B5) are KEEP-ALL, structurally protected.
337    // `state.intents` (incl. the fence-independent `committed_runs` idempotency
338    // oracle) is carried untouched below — the fold routes `Surface::Intent`
339    // into `state.intents`, never `state.logs`, so retention here cannot reach
340    // it. A retention rule keyed on "intent" would therefore be a silent no-op
341    // that misleads a reader into thinking committed records get trimmed; reject
342    // it loudly so the keep-all invariant is explicit and reviewable, mirroring
343    // the event-stream guard. (The idempotency oracle surviving compaction
344    // depends on this: a committed run's record must NEVER be trimmable.)
345    if policy.rule_for(&crate::oplog::Surface::Intent.tag()) != &RetentionRule::KeepAll {
346        return Err(CompactError::IntentRetention);
347    }
348
349    let mut retained = state.clone();
350    let mut report = RetentionReport::default();
351    for (tag, entries) in &state.logs {
352        let rule = policy.rule_for(tag);
353        if rule == &RetentionRule::KeepAll {
354            continue;
355        }
356        // Selection ranks LIVE entries only: existing stubs are carried
357        // unchanged and never displace a live entry from the quota.
358        let live: BTreeMap<String, FoldedRecord> = entries
359            .iter()
360            .filter(|(_, record)| !is_tombstone(record))
361            .map(|(key, record)| (key.clone(), record.clone()))
362            .collect();
363        let keep = select_retained(&live, rule, as_of_ms);
364        let surface = retained.logs.get_mut(tag).expect("cloned from state");
365        for (key, record) in &live {
366            if keep.contains(key) {
367                continue;
368            }
369            match entity_id(key, record) {
370                Some(id) => {
371                    surface.insert(key.clone(), tombstone_of(record, id));
372                    report.tombstoned.push((tag.clone(), key.clone()));
373                }
374                None => {
375                    surface.remove(key);
376                    *report.dropped.entry(tag.clone()).or_insert(0) += 1;
377                }
378            }
379        }
380    }
381    report.tombstoned.sort();
382    Ok((retained, report))
383}
384
385/// The fold-frontier / ack bookkeeping B3's relay `ack(frontier)` reports
386/// against: per-device max folded HLC, **monotone-only** advance, persisted
387/// alongside the checkpoint (temp + atomic rename, like everything durable
388/// here). This is the proposal's `acked[device]` table, device-local.
389///
390/// **MUST, binding on B3: an ack asserts durably-folded state.** A device
391/// may report `ack(frontier)` only after the ops at/below that frontier are
392/// durably persisted on it (journal-durable, fold applied) — an ack sent
393/// from memory ahead of the fsync lets compaction drop ops the acking
394/// device then loses in a crash, which is exactly the data loss the stable
395/// frontier exists to prevent. The mirror of B1's
396/// journal-durable-before-transmit rule.
397#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
398pub struct AckTable {
399    acked: BTreeMap<String, Hlc>,
400}
401
402impl AckTable {
403    pub fn new() -> Self {
404        Self::default()
405    }
406
407    /// Advance a device's acked frontier. Monotone-only: an ack at or below
408    /// the current frontier is ignored (returns `false`) — a delayed or
409    /// replayed ack can never move GC eligibility backwards.
410    pub fn ack(&mut self, device_id: impl Into<String>, frontier: Hlc) -> bool {
411        let device_id = device_id.into();
412        match self.acked.get(&device_id) {
413            Some(current) if frontier <= *current => false,
414            _ => {
415                self.acked.insert(device_id, frontier);
416                true
417            }
418        }
419    }
420
421    pub fn get(&self, device_id: &str) -> Option<&Hlc> {
422        self.acked.get(device_id)
423    }
424
425    pub fn devices(&self) -> impl Iterator<Item = &str> {
426        self.acked.keys().map(String::as_str)
427    }
428
429    /// The stable frontier — `min(acked[d])` over every known device
430    /// (proposal §"Snapshots"). Ops at or below it are folded by everyone
431    /// and are the only truncation candidates. `None` when no device has
432    /// acked (nothing is ever droppable then).
433    pub fn stable_frontier(&self) -> Option<&Hlc> {
434        self.acked.values().min()
435    }
436
437    /// Durably persist (temp + atomic rename); pairs with
438    /// [`AckTable::load`]. Kept alongside the checkpoint directory by
439    /// convention.
440    pub fn save(&self, path: &Path) -> std::io::Result<()> {
441        if let Some(parent) = path.parent() {
442            if !parent.as_os_str().is_empty() {
443                fs::create_dir_all(parent)?;
444            }
445        }
446        let tmp_path = {
447            let mut s = path.as_os_str().to_owned();
448            s.push(".tmp");
449            PathBuf::from(s)
450        };
451        {
452            let mut tmp = File::create(&tmp_path)?;
453            tmp.write_all(
454                serde_json::to_string(self)
455                    .map_err(std::io::Error::other)?
456                    .as_bytes(),
457            )?;
458            tmp.sync_all()?;
459        }
460        fs::rename(&tmp_path, path)
461    }
462
463    /// Load a persisted table; a missing file is an empty table (a fresh
464    /// device knows of no acks — and an empty table makes compaction
465    /// refuse, the safe default).
466    pub fn load(path: &Path) -> std::io::Result<Self> {
467        match fs::read_to_string(path) {
468            Ok(raw) => serde_json::from_str(&raw).map_err(std::io::Error::other),
469            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
470            Err(e) => Err(e),
471        }
472    }
473}
474
475/// A compaction failure. No `PartialEq` (carries `io::Error`); match on
476/// variants.
477#[derive(Debug)]
478pub enum CompactError {
479    /// The input log doesn't verify — never compact what you can't trust
480    /// (the B1 verify-before-fold contract).
481    Chain(ChainError),
482    /// No device has acked anything: no stable frontier exists, nothing is
483    /// provably folded-by-everyone, nothing may be dropped.
484    NothingAcked,
485    /// A device present in the log has no ack entry — its fold frontier is
486    /// unknown, so every op is data it may not have seen. Refuse.
487    UnackedDevice {
488        device_id: String,
489    },
490    /// The policy tried to trim an event-stream surface (op_id-keyed
491    /// observation multiset). Those replay from genesis; only keep-all is
492    /// sound.
493    EventStreamRetention {
494        surface: String,
495    },
496    /// The policy set a non-keep-all rule for the leased `intent` surface
497    /// (B5). Intents are keep-all — the `committed_runs` idempotency oracle
498    /// must survive compaction, so trimming it is never sound.
499    IntentRetention,
500    /// The journal was already truncated below a checkpoint (its truncation
501    /// marker names it). Re-planning from the tail alone would fold a
502    /// checkpoint that silently misses everything the prior checkpoint
503    /// covers — recompaction over a checkpoint base is a later slice.
504    TruncatedJournal {
505        checkpoint_hash: String,
506    },
507    Io(std::io::Error),
508}
509
510impl fmt::Display for CompactError {
511    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
512        match self {
513            CompactError::Chain(e) => write!(f, "compaction refused: log does not verify: {e}"),
514            CompactError::NothingAcked => {
515                write!(
516                    f,
517                    "compaction refused: no acked frontier exists (empty ack table)"
518                )
519            }
520            CompactError::UnackedDevice { device_id } => write!(
521                f,
522                "compaction refused: device {device_id} appears in the log but has no acked \
523                 frontier — its ops may include state no other replica has folded"
524            ),
525            CompactError::EventStreamRetention { surface } => write!(
526                f,
527                "retention policy for event-stream surface {surface} must be keep_all — \
528                 observation multisets replay from genesis and cannot be trimmed"
529            ),
530            CompactError::IntentRetention => write!(
531                f,
532                "retention policy for the leased `intent` surface must be keep_all — the \
533                 committed-run idempotency oracle must survive compaction and cannot be trimmed"
534            ),
535            CompactError::TruncatedJournal { checkpoint_hash } => write!(
536                f,
537                "compaction refused: journal already truncated below checkpoint \
538                 {checkpoint_hash} — re-planning from the tail alone would drop that \
539                 checkpoint's state (recompaction over a checkpoint base is a later slice)"
540            ),
541            CompactError::Io(e) => write!(f, "compaction io error: {e}"),
542        }
543    }
544}
545
546impl std::error::Error for CompactError {}
547
548/// A computed (not yet executed) compaction: the retained checkpoint, the
549/// ops that stay in the journal, and what happened.
550#[derive(Debug)]
551pub struct CompactionPlan {
552    /// The checkpoint to persist BEFORE truncating — frontier heads +
553    /// retention-applied state, content-addressed over the whole record.
554    pub checkpoint: Checkpoint,
555    /// Ops strictly above the stable frontier — the journal's new content.
556    pub retained_ops: Vec<OpRecord>,
557    /// How many ops fall at/below the frontier (dropped from the journal,
558    /// covered by the checkpoint).
559    pub dropped_ops: usize,
560    /// The stable frontier the plan cut at.
561    pub frontier: Hlc,
562    /// The effective reference instant the age rules ran with (the caller's
563    /// explicit value, or [`as_of_from_ops`] over the below-frontier ops).
564    pub as_of_ms: u64,
565    /// What retention trimmed inside the checkpoint state.
566    pub retention: RetentionReport,
567}
568
569/// Plan a compaction of `ops` at the [`AckTable`]'s stable frontier. Pure —
570/// no file IO; [`compact_and_truncate`] executes a plan durably. See the
571/// module docs for the three safety guards; per-device HLC monotonicity
572/// (enforced by `verify_log`) guarantees "hlc ≤ frontier" is a chain prefix,
573/// so the cut is always anchorable.
574///
575/// `as_of_ms`: the age rules' reference instant. `None` (the default every
576/// caller should want) derives it deterministically via [`as_of_from_ops`]
577/// over the below-frontier ops, so identical frontiers yield identical
578/// retained checkpoints on every device.
579pub fn plan_compaction(
580    ops: &[OpRecord],
581    acks: &AckTable,
582    policy: &RetentionPolicy,
583    as_of_ms: Option<u64>,
584) -> Result<CompactionPlan, CompactError> {
585    verify_log(ops).map_err(CompactError::Chain)?;
586    let frontier = acks
587        .stable_frontier()
588        .cloned()
589        .ok_or(CompactError::NothingAcked)?;
590    for op in ops {
591        if acks.get(&op.device_id).is_none() {
592            return Err(CompactError::UnackedDevice {
593                device_id: op.device_id.clone(),
594            });
595        }
596    }
597
598    let (below, retained_ops): (Vec<OpRecord>, Vec<OpRecord>) =
599        ops.iter().cloned().partition(|op| op.hlc <= frontier);
600    let as_of_ms = as_of_ms.unwrap_or_else(|| as_of_from_ops(&below));
601
602    // Exact fold at the frontier first (from_ops re-verifies the prefix),
603    // then retention on the snapshot only — never on the tail.
604    let exact = Checkpoint::from_ops(&below).map_err(CompactError::Chain)?;
605    let (retained_state, retention) = apply_retention(&exact.state, policy, as_of_ms)?;
606    let checkpoint = Checkpoint::assemble(exact.frontier, exact.scopes, retained_state);
607
608    Ok(CompactionPlan {
609        checkpoint,
610        retained_ops,
611        dropped_ops: below.len(),
612        frontier,
613        as_of_ms,
614        retention,
615    })
616}
617
618/// The outcome of an executed compaction.
619#[derive(Debug)]
620pub struct CompactionOutcome {
621    /// Where the checkpoint landed (content-addressed file in
622    /// `checkpoint_dir`) — `None` when the plan dropped nothing (nothing at
623    /// or below the frontier), in which case neither a checkpoint write nor
624    /// a truncation happened: an empty compaction is a no-op, not an empty
625    /// checkpoint file.
626    pub checkpoint_path: Option<PathBuf>,
627    pub plan: CompactionPlan,
628}
629
630/// Execute a compaction end-to-end on a live journal, enforcing the
631/// crash-ordering invariant by construction:
632///
633/// 1. load + verify the journal (a journal already carrying a truncation
634///    marker is refused — [`CompactError::TruncatedJournal`] — because
635///    re-planning from the tail alone would silently lose the prior
636///    checkpoint's state);
637/// 2. plan at the ack table's stable frontier (a plan that drops nothing
638///    is a **no-op**: no checkpoint written, journal untouched);
639/// 3. **checkpoint durable FIRST** ([`Checkpoint::save`]: temp + fsync +
640///    atomic rename);
641/// 4. only then truncate the journal to the retained tail
642///    ([`OplogJournal::truncate_to`]: temp + atomic rename under the
643///    journal's advisory lock, stamping the truncation marker that fences
644///    `DeviceLog::resume`).
645///
646/// A crash between 3 and 4 leaves the full journal plus a redundant
647/// checkpoint; rerunning is idempotent (same frontier ⇒ same
648/// content-addressed checkpoint file, rename-over-identical). A crash
649/// inside 4 leaves either the old or the new journal, whole. Acknowledged
650/// data is never lost. (Steps are inherently sequential — each depends on
651/// the previous one's durability.)
652pub fn compact_and_truncate(
653    journal: &mut OplogJournal,
654    checkpoint_dir: &Path,
655    acks: &AckTable,
656    policy: &RetentionPolicy,
657    as_of_ms: Option<u64>,
658) -> Result<CompactionOutcome, CompactError> {
659    let (marker, ops) = OplogJournal::load_with_marker(journal.path()).map_err(CompactError::Io)?;
660    if let Some(marker) = marker {
661        return Err(CompactError::TruncatedJournal {
662            checkpoint_hash: marker.checkpoint_hash,
663        });
664    }
665    let plan = plan_compaction(&ops, acks, policy, as_of_ms)?;
666    if plan.dropped_ops == 0 {
667        // Nothing at or below the frontier: writing an empty checkpoint and
668        // rewriting the journal to itself would be pure churn. No-op.
669        return Ok(CompactionOutcome {
670            checkpoint_path: None,
671            plan,
672        });
673    }
674    // INVARIANT: checkpoint durable BEFORE any op leaves the journal.
675    let checkpoint_path = plan
676        .checkpoint
677        .save(checkpoint_dir)
678        .map_err(CompactError::Io)?;
679    journal
680        .truncate_to(&plan.retained_ops, &plan.checkpoint.checkpoint_hash)
681        .map_err(CompactError::Io)?;
682    Ok(CompactionOutcome {
683        checkpoint_path: Some(checkpoint_path),
684        plan,
685    })
686}
687
688#[cfg(test)]
689mod tests {
690    use super::*;
691    use crate::fold::fold;
692    use crate::oplog::{DeviceLog, Scope, Surface};
693    use serde_json::json;
694
695    fn hlc(wall_ms: u64, device: &str) -> Hlc {
696        Hlc {
697            wall_ms,
698            counter: 0,
699            device_id: device.into(),
700        }
701    }
702
703    #[test]
704    fn ack_table_is_monotone_only_and_min_frontier() {
705        let mut acks = AckTable::new();
706        assert_eq!(acks.stable_frontier(), None);
707        assert!(acks.ack("a", hlc(5, "a")));
708        assert!(acks.ack("b", hlc(9, "b")));
709        assert_eq!(
710            acks.stable_frontier(),
711            Some(&hlc(5, "a")),
712            "min over devices"
713        );
714
715        // Regression is ignored — a replayed/late ack can't move GC back.
716        assert!(!acks.ack("b", hlc(3, "b")));
717        assert!(!acks.ack("b", hlc(9, "b")), "equal is not an advance");
718        assert_eq!(acks.get("b"), Some(&hlc(9, "b")));
719        assert!(acks.ack("b", hlc(12, "b")));
720        assert_eq!(acks.get("b"), Some(&hlc(12, "b")));
721    }
722
723    #[test]
724    fn ack_table_persists_atomically_and_loads_missing_as_empty() {
725        let dir = tempfile::tempdir().unwrap();
726        let path = dir.path().join("nested").join("acks.json");
727        assert_eq!(
728            AckTable::load(&path).unwrap(),
729            AckTable::new(),
730            "missing → empty"
731        );
732
733        let mut acks = AckTable::new();
734        acks.ack("a", hlc(5, "a"));
735        acks.ack("b", hlc(9, "b"));
736        acks.save(&path).unwrap();
737        assert_eq!(AckTable::load(&path).unwrap(), acks);
738        // No temp file left behind.
739        assert!(!path.parent().unwrap().join("acks.json.tmp").exists());
740    }
741
742    /// Ops: device a emits 3 knowledge facts, b (having observed) emits 1.
743    fn simple_ops() -> Vec<OpRecord> {
744        let mut a = DeviceLog::new("a");
745        let mut b = DeviceLog::new("b");
746        let mut ops = vec![
747            a.append(Scope::Personal, Surface::Knowledge, json!({"id": "f1"})),
748            a.append(Scope::Personal, Surface::Knowledge, json!({"id": "f2"})),
749            a.append(Scope::Personal, Surface::Knowledge, json!({"id": "f3"})),
750        ];
751        for op in &ops {
752            b.observe(&op.hlc);
753        }
754        ops.push(b.append(Scope::Personal, Surface::Knowledge, json!({"id": "f4"})));
755        ops
756    }
757
758    #[test]
759    fn compaction_refuses_without_acks() {
760        let ops = simple_ops();
761        let policy = RetentionPolicy::keep_all();
762        assert!(matches!(
763            plan_compaction(&ops, &AckTable::new(), &policy, None),
764            Err(CompactError::NothingAcked)
765        ));
766
767        // A device in the log with no ack entry → refuse: its fold frontier
768        // is unknown.
769        let mut acks = AckTable::new();
770        acks.ack("a", ops[2].hlc.clone());
771        assert!(matches!(
772            plan_compaction(&ops, &acks, &policy, None),
773            Err(CompactError::UnackedDevice { .. })
774        ));
775    }
776
777    #[test]
778    fn compaction_never_drops_above_a_lagging_ack() {
779        let ops = simple_ops();
780        let mut acks = AckTable::new();
781        // b has folded everything; a's ack lags at its own second op.
782        acks.ack("b", ops[3].hlc.clone());
783        acks.ack("a", ops[1].hlc.clone());
784
785        let plan = plan_compaction(&ops, &acks, &RetentionPolicy::keep_all(), None).unwrap();
786        assert_eq!(
787            plan.frontier, ops[1].hlc,
788            "stable frontier = the lagging device's ack"
789        );
790        assert_eq!(plan.dropped_ops, 2, "only ops ≤ the lagging frontier drop");
791        assert_eq!(plan.retained_ops.len(), 2);
792        assert!(
793            plan.retained_ops.iter().all(|op| op.hlc > plan.frontier),
794            "everything a device hasn't seen stays in the journal"
795        );
796    }
797
798    #[test]
799    fn retention_conversations_last_n_by_timestamp() {
800        let mut dev = DeviceLog::new("a");
801        let ops: Vec<OpRecord> = (0..5)
802            .map(|i| {
803                dev.append(
804                    Scope::Personal,
805                    Surface::Conversation,
806                    json!({"speaker": "u", "text": format!("t{i}"), "timestamp": 100 + i}),
807                )
808            })
809            .collect();
810        let state = fold(&ops);
811        let policy = RetentionPolicy::proposal_default(2, u64::MAX);
812        let (retained, report) = apply_retention(&state, &policy, 1_000).unwrap();
813        let tag = Surface::Conversation.tag();
814        let texts: Vec<String> = retained
815            .log_entries(&tag)
816            .iter()
817            .map(|r| r.payload["text"].as_str().unwrap().to_string())
818            .collect();
819        assert_eq!(texts, vec!["t3", "t4"], "last 2 turns by timestamp survive");
820        assert_eq!(report.dropped[&tag], 3);
821    }
822
823    #[test]
824    fn retention_runs_per_agent_and_age_matches_run_store_gc() {
825        assert_eq!(
826            RUNS_MAX_PER_AGENT, 50,
827            "parity with run_store DEFAULT_MAX_RUNS_PER_AGENT"
828        );
829        assert_eq!(
830            RUNS_MAX_AGE_MS,
831            30 * 24 * 60 * 60 * 1000,
832            "parity with DEFAULT_MAX_AGE_DAYS"
833        );
834
835        // Behavior with small numbers: keep 2 per agent AND drop older than
836        // age 50 — both restrictive.
837        let mut dev = DeviceLog::new("a");
838        let mut ops = Vec::new();
839        for (id, agent, ts) in [
840            ("r1", "milo", 10u64), // over per-agent cap AND stale
841            ("r2", "milo", 60),    // within both → kept
842            ("r3", "milo", 70),    // within both → kept
843            ("r4", "other", 10),   // within cap, but stale → dropped
844            ("r5", "other", 80),   // kept
845        ] {
846            ops.push(dev.append(
847                Scope::Personal,
848                Surface::Run,
849                json!({"id": id, "agent_id": agent, "timestamp": ts}),
850            ));
851        }
852        let state = fold(&ops);
853        let mut policy = RetentionPolicy::keep_all();
854        policy.rules.insert(
855            "run".to_string(),
856            RetentionRule::PerAgentWithAge {
857                max_per_agent: 2,
858                max_age_ms: 50,
859            },
860        );
861        let (retained, report) = apply_retention(&state, &policy, 100).unwrap();
862        let entries = retained.log_entries(&Surface::Run.tag());
863        let kept: Vec<&str> = entries
864            .iter()
865            .filter(|r| !is_tombstone(r))
866            .map(|r| r.payload["id"].as_str().unwrap())
867            .collect();
868        assert_eq!(kept, vec!["r2", "r3", "r5"]);
869        // Runs carry ids → the trimmed ones leave stubs, not holes.
870        let stubs: Vec<&str> = entries
871            .iter()
872            .filter(|r| is_tombstone(r))
873            .map(|r| r.payload["id"].as_str().unwrap())
874            .collect();
875        assert_eq!(stubs, vec!["r1", "r4"]);
876        assert_eq!(report.tombstoned.len(), 2);
877        assert!(
878            report.dropped.is_empty(),
879            "id-bearing entries are stubbed, never erased"
880        );
881    }
882
883    #[test]
884    fn retention_trajectories_by_age_and_undated_never_dropped() {
885        let mut dev = DeviceLog::new("a");
886        let ops = vec![
887            dev.append(
888                Scope::Personal,
889                Surface::Trajectory,
890                json!({"id": "old", "timestamp": 10}),
891            ),
892            dev.append(
893                Scope::Personal,
894                Surface::Trajectory,
895                json!({"id": "new", "timestamp": 90}),
896            ),
897            dev.append(
898                Scope::Personal,
899                Surface::Trajectory,
900                json!({"id": "undated"}),
901            ),
902        ];
903        let state = fold(&ops);
904        let mut policy = RetentionPolicy::keep_all();
905        policy.rules.insert(
906            "trajectory".to_string(),
907            RetentionRule::MaxAgeMs { max_age_ms: 30 },
908        );
909        let (retained, _) = apply_retention(&state, &policy, 100).unwrap();
910        let entries = retained.log_entries(&Surface::Trajectory.tag());
911        let kept: Vec<&str> = entries
912            .iter()
913            .filter(|r| !is_tombstone(r))
914            .map(|r| r.payload["id"].as_str().unwrap())
915            .collect();
916        assert!(kept.contains(&"new"));
917        assert!(
918            kept.contains(&"undated"),
919            "undated data is never silently age-dropped"
920        );
921        assert!(!kept.contains(&"old"));
922        // The aged-out trajectory left a stub, not a hole.
923        assert!(entries
924            .iter()
925            .any(|r| is_tombstone(r) && r.payload["id"] == json!("old")));
926    }
927
928    #[test]
929    fn knowledge_and_skills_keep_all_under_the_proposal_default() {
930        let mut dev = DeviceLog::new("a");
931        let ops = vec![
932            dev.append(
933                Scope::Personal,
934                Surface::Knowledge,
935                json!({"id": "f1", "timestamp": 1}),
936            ),
937            dev.append(
938                Scope::Personal,
939                Surface::Skill,
940                json!({"id": "s1", "timestamp": 1}),
941            ),
942        ];
943        let state = fold(&ops);
944        // Aggressive everything-else policy; knowledge/skills still keep all.
945        let (retained, report) =
946            apply_retention(&state, &RetentionPolicy::proposal_default(1, 1), u64::MAX).unwrap();
947        assert_eq!(retained.logs[&Surface::Knowledge.tag()].len(), 1);
948        assert_eq!(retained.logs[&Surface::Skill.tag()].len(), 1);
949        assert!(report.dropped.is_empty());
950    }
951
952    #[test]
953    fn event_stream_retention_is_rejected() {
954        let mut dev = DeviceLog::new("a");
955        let ops = vec![
956            dev.append(Scope::Personal, Surface::Routing, json!({"sample": 1.0})),
957            dev.append(Scope::Personal, Surface::Routing, json!({"sample": 0.0})),
958        ];
959        let state = fold(&ops);
960        let mut policy = RetentionPolicy::keep_all();
961        policy
962            .rules
963            .insert("routing".to_string(), RetentionRule::LastN { n: 1 });
964        assert!(matches!(
965            apply_retention(&state, &policy, 0),
966            Err(CompactError::EventStreamRetention { .. })
967        ));
968        // And the proposal default keeps the whole multiset.
969        let (retained, _) =
970            apply_retention(&state, &RetentionPolicy::proposal_default(10, 10), u64::MAX).unwrap();
971        assert_eq!(retained.log_entries(&Surface::Routing.tag()).len(), 2);
972    }
973
974    #[test]
975    fn every_dropped_id_bearing_entry_leaves_a_tombstone_stub() {
976        // f3 supersedes f2 supersedes f1; LastN(1) keeps only f3 live —
977        // and EVERY trimmed id-bearing entry (f0, f1, f2) leaves a stub,
978        // whether or not anything references it *yet* (the time-hole fix:
979        // a supersedes that arrives after compaction still resolves).
980        let mut dev = DeviceLog::new("a");
981        let ops = vec![
982            dev.append(
983                Scope::Personal,
984                Surface::Knowledge,
985                json!({"id": "f1", "timestamp": 1}),
986            ),
987            dev.append(
988                Scope::Personal,
989                Surface::Knowledge,
990                json!({"id": "f2", "timestamp": 2, "supersedes": "f1"}),
991            ),
992            dev.append(
993                Scope::Personal,
994                Surface::Knowledge,
995                json!({"id": "f3", "timestamp": 3, "supersedes": ["f2"]}),
996            ),
997            dev.append(
998                Scope::Personal,
999                Surface::Knowledge,
1000                json!({"id": "f0", "timestamp": 0}),
1001            ),
1002        ];
1003        let state = fold(&ops);
1004        let mut policy = RetentionPolicy::keep_all();
1005        policy
1006            .rules
1007            .insert("knowledge".to_string(), RetentionRule::LastN { n: 1 });
1008        let (retained, report) = apply_retention(&state, &policy, 10).unwrap();
1009        let tag = Surface::Knowledge.tag();
1010        let surface = &retained.logs[&tag];
1011        assert_eq!(
1012            surface["id:f3"].payload["timestamp"],
1013            json!(3),
1014            "newest stays live"
1015        );
1016        for id in ["f0", "f1", "f2"] {
1017            let stub = &surface[&format!("id:{id}")];
1018            assert!(is_tombstone(stub), "{id} left a stub");
1019            assert_eq!(
1020                stub.payload,
1021                json!({"id": id, "tombstone": true}),
1022                "minimal stub shape"
1023            );
1024            assert_eq!(
1025                stub.op_id,
1026                state.logs[&tag][&format!("id:{id}")].op_id,
1027                "stub keeps the original op identity"
1028            );
1029        }
1030        assert_eq!(report.tombstoned.len(), 3);
1031        assert!(report.dropped.is_empty());
1032
1033        // Idempotent + quota-neutral: re-applying the policy changes
1034        // nothing — stubs are carried, and they don't consume f3's slot.
1035        let (again, report2) = apply_retention(&retained, &policy, 10).unwrap();
1036        assert_eq!(again, retained);
1037        assert!(report2.tombstoned.is_empty());
1038    }
1039
1040    #[test]
1041    fn derived_as_of_is_the_max_below_frontier_timestamp() {
1042        let mut dev = DeviceLog::new("a");
1043        let ops = vec![
1044            dev.append(
1045                Scope::Personal,
1046                Surface::Trajectory,
1047                json!({"id": "t1", "timestamp": 40}),
1048            ),
1049            dev.append(
1050                Scope::Personal,
1051                Surface::Trajectory,
1052                json!({"id": "t2", "timestamp": 100}),
1053            ),
1054            dev.append(Scope::Personal, Surface::Skill, json!({"id": "s1"})), // undated
1055        ];
1056        assert_eq!(as_of_from_ops(&ops), 100, "max payload timestamp");
1057        assert_eq!(
1058            as_of_from_ops(&ops[2..]),
1059            0,
1060            "no timestamps → 0 (age rules drop nothing)"
1061        );
1062
1063        // plan_compaction defaults to the derived value — pure over the
1064        // below-frontier ops, so every device agrees with no out-of-band
1065        // coordination.
1066        let mut acks = AckTable::new();
1067        acks.ack("a", ops[2].hlc.clone());
1068        let mut policy = RetentionPolicy::keep_all();
1069        policy.rules.insert(
1070            "trajectory".to_string(),
1071            RetentionRule::MaxAgeMs { max_age_ms: 30 },
1072        );
1073        let plan = plan_compaction(&ops, &acks, &policy, None).unwrap();
1074        assert_eq!(plan.as_of_ms, 100);
1075        // age(t1) = 100 - 40 = 60 > 30 → stubbed; t2 lives.
1076        let surface = &plan.checkpoint.state.logs[&Surface::Trajectory.tag()];
1077        assert!(is_tombstone(&surface["id:t1"]));
1078        assert!(!is_tombstone(&surface["id:t2"]));
1079    }
1080
1081    #[test]
1082    fn empty_below_frontier_compaction_is_a_no_op() {
1083        let dir = tempfile::tempdir().unwrap();
1084        let journal_path = dir.path().join("oplog.jsonl");
1085        let ckpt_dir = dir.path().join("checkpoints");
1086
1087        // All ops sit ABOVE the acked frontier (a device acked long ago and
1088        // never caught up): nothing is droppable.
1089        let ops = simple_ops();
1090        let mut journal = OplogJournal::open(&journal_path).unwrap();
1091        for op in &ops {
1092            journal.append(op).unwrap();
1093        }
1094        let mut acks = AckTable::new();
1095        acks.ack(
1096            "a",
1097            Hlc {
1098                wall_ms: 0,
1099                counter: 0,
1100                device_id: "a".into(),
1101            },
1102        );
1103        acks.ack(
1104            "b",
1105            Hlc {
1106                wall_ms: 0,
1107                counter: 0,
1108                device_id: "b".into(),
1109            },
1110        );
1111
1112        let before = fs::read_to_string(&journal_path).unwrap();
1113        let outcome = compact_and_truncate(
1114            &mut journal,
1115            &ckpt_dir,
1116            &acks,
1117            &RetentionPolicy::keep_all(),
1118            None,
1119        )
1120        .unwrap();
1121        assert_eq!(outcome.plan.dropped_ops, 0);
1122        assert!(
1123            outcome.checkpoint_path.is_none(),
1124            "no empty checkpoint file written"
1125        );
1126        assert!(!ckpt_dir.exists(), "checkpoint dir not even created");
1127        assert_eq!(
1128            fs::read_to_string(&journal_path).unwrap(),
1129            before,
1130            "journal untouched (no marker, no rewrite)"
1131        );
1132        // And it is still loadable the normal way (no truncation happened).
1133        assert_eq!(OplogJournal::load(&journal_path).unwrap(), ops);
1134    }
1135
1136    #[test]
1137    fn recompacting_an_already_truncated_journal_is_refused() {
1138        let dir = tempfile::tempdir().unwrap();
1139        let journal_path = dir.path().join("oplog.jsonl");
1140        let ckpt_dir = dir.path().join("checkpoints");
1141        let ops = simple_ops();
1142        let mut journal = OplogJournal::open(&journal_path).unwrap();
1143        for op in &ops {
1144            journal.append(op).unwrap();
1145        }
1146        let mut acks = AckTable::new();
1147        acks.ack("a", ops[1].hlc.clone());
1148        acks.ack("b", ops[1].hlc.clone());
1149        let outcome = compact_and_truncate(
1150            &mut journal,
1151            &ckpt_dir,
1152            &acks,
1153            &RetentionPolicy::keep_all(),
1154            None,
1155        )
1156        .unwrap();
1157        let expected_hash = outcome.plan.checkpoint.checkpoint_hash.clone();
1158
1159        // A second compaction over the truncated journal would fold a
1160        // checkpoint missing the prior one's state — refused, naming the
1161        // checkpoint to re-anchor on.
1162        acks.ack("a", ops[3].hlc.clone());
1163        acks.ack("b", ops[3].hlc.clone());
1164        match compact_and_truncate(
1165            &mut journal,
1166            &ckpt_dir,
1167            &acks,
1168            &RetentionPolicy::keep_all(),
1169            None,
1170        ) {
1171            Err(CompactError::TruncatedJournal { checkpoint_hash }) => {
1172                assert_eq!(checkpoint_hash, expected_hash)
1173            }
1174            other => panic!("expected TruncatedJournal refusal, got {other:?}"),
1175        }
1176    }
1177
1178    #[test]
1179    fn plan_refuses_an_invalid_log() {
1180        let mut ops = simple_ops();
1181        ops[1].payload = json!({"forged": true});
1182        let mut acks = AckTable::new();
1183        acks.ack("a", ops[2].hlc.clone());
1184        acks.ack("b", ops[3].hlc.clone());
1185        assert!(matches!(
1186            plan_compaction(&ops, &acks, &RetentionPolicy::keep_all(), None),
1187            Err(CompactError::Chain(ChainError::IdMismatch { .. }))
1188        ));
1189    }
1190
1191    #[test]
1192    fn intent_retention_rule_is_rejected() {
1193        // B5 keep-all guard: a non-keep-all rule on the leased `intent` surface
1194        // is rejected loudly (mirroring the event-stream guard), so the
1195        // committed-run idempotency oracle can never be configured away.
1196        use crate::lease::{Intent, IntentStatus};
1197        let mut dev = DeviceLog::new("a");
1198        let ops = vec![dev.append(
1199            Scope::Personal,
1200            Surface::Intent,
1201            Intent::new("milo", "R", 1, IntentStatus::Committed).payload(),
1202        )];
1203        let state = fold(&ops);
1204        let mut policy = RetentionPolicy::keep_all();
1205        policy
1206            .rules
1207            .insert("intent".to_string(), RetentionRule::LastN { n: 1 });
1208        assert!(matches!(
1209            apply_retention(&state, &policy, 0),
1210            Err(CompactError::IntentRetention)
1211        ));
1212        // keep-all is fine and preserves the committed oracle untouched.
1213        let (retained, _) = apply_retention(&state, &RetentionPolicy::keep_all(), 0).unwrap();
1214        assert!(retained.committed_run("milo", "R").is_some());
1215    }
1216
1217    #[test]
1218    fn c2_committed_run_survives_a_checkpoint_compaction() {
1219        // C2 REPRO: a committed run below the stable frontier is truncated from
1220        // the journal by compaction. Its record must NOT be lost — the
1221        // fence-independent committed-run oracle is carried keep-all into the
1222        // checkpoint, so idempotency survives compaction.
1223        use crate::fold::fold_onto;
1224        use crate::lease::{Intent, IntentStatus};
1225
1226        let mut a = DeviceLog::new("a");
1227        let mut b = DeviceLog::new("b");
1228        let mut ops = vec![a.append(
1229            Scope::Personal,
1230            Surface::Intent,
1231            Intent::new("milo", "R", 1, IntentStatus::Committed).payload(),
1232        )];
1233        let split = ops.len();
1234        for op in &ops {
1235            b.observe(&op.hlc);
1236        }
1237        // A later, higher-epoch tail op (above the frontier) raises the fence.
1238        ops.push(b.append(
1239            Scope::Personal,
1240            Surface::Intent,
1241            Intent::new("milo", "S", 2, IntentStatus::Pending).payload(),
1242        ));
1243
1244        // Frontier cut so committed R (epoch 1) is BELOW and dropped.
1245        let frontier = ops[..split].iter().map(|o| o.hlc.clone()).max().unwrap();
1246        let mut acks = AckTable::new();
1247        acks.ack("a", frontier.clone());
1248        acks.ack("b", frontier);
1249        let plan = plan_compaction(&ops, &acks, &RetentionPolicy::keep_all(), None).unwrap();
1250        assert_eq!(
1251            plan.dropped_ops, split,
1252            "committed R is below the frontier, dropped from journal"
1253        );
1254
1255        // The raw R op is gone from the retained tail, but the oracle survives
1256        // in the checkpoint — the idempotency answer is durable.
1257        assert!(
1258            plan.checkpoint.state.committed_run("milo", "R").is_some(),
1259            "committed R survives compaction in the checkpoint oracle (C2 fixed)"
1260        );
1261        let reconstructed = fold_onto(&plan.checkpoint.state, &plan.retained_ops);
1262        assert_eq!(
1263            reconstructed,
1264            fold(&ops),
1265            "fold_onto(checkpoint, tail) == fold(full)"
1266        );
1267        assert!(
1268            reconstructed.committed_run("milo", "R").is_some(),
1269            "oracle intact post-compaction"
1270        );
1271    }
1272}