Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(sweep): keep an already-compacted residue terminal when compaction is off

Rudy Celekli06333ec9d49a
diff --git a/crates/ai-memory-consolidate/src/sweep.rs b/crates/ai-memory-consolidate/src/sweep.rs
index 4d07e273..c671b995 100644
--- a/crates/ai-memory-consolidate/src/sweep.rs
+++ b/crates/ai-memory-consolidate/src/sweep.rs
@@ -446,10 +446,11 @@ pub async fn run_sweep_with_options(
/// Run a sweep with the breadth coefficient, the observation prune, and the
/// opt-in A2 extractive tier-down (`compact_cold_episodic`).
///
-/// With `compact_cold_episodic = false` (the default everywhere) this is
-/// byte-for-byte the historical sweep: a cold episodic page is evicted
-/// (tombstoned). With it `true`, a cold episodic page that has NOT already been
-/// compacted is tiered DOWN instead — rewritten through the wiki layer to keep
+/// With `compact_cold_episodic = false` (the default everywhere) this is the
+/// historical sweep for every page that carries no V65 marker: a cold episodic
+/// page is evicted (tombstoned). With it `true`, a cold episodic page that has
+/// NOT already been compacted is tiered DOWN instead — rewritten through the
+/// wiki layer to keep
/// its L0 abstract, an L1 summary and the L2 keep-token set, dropping the prose
/// — and reported under [`SweepReport::compacted`] rather than `evicted`. The
/// original full body stays reachable via supersession + git (reversible,
@@ -669,15 +670,21 @@ pub(crate) async fn run_sweep_at(
if deduped.contains(&entry.id) {
continue;
}
+ // An already-compacted page (its V65 marker set) is terminal for the
+ // decay pass on BOTH arms. The durable residue is what we chose to
+ // keep, so evicting it would destroy exactly what tier-down preserved,
+ // and re-compacting it is a no-op loop. Skip it entirely.
+ //
+ // This guard cannot sit inside the `compact_cold_episodic` arm: the
+ // marker is stamped by passes with their OWN flags — the A3 dedup
+ // merge-note stub and the B2 dream pass both write `compacted: true`,
+ // from which `compacted_at` is derived at the single write choke point
+ // — so residues exist with A2 off, which is the default everywhere.
+ if entry.compacted_at_us.is_some() {
+ continue;
+ }
if compact_cold_episodic {
- // A2 tier-down replaces eviction for cold episodic pages. An
- // already-compacted page (its V65 marker set) is terminal — the
- // durable residue is what we chose to keep, so re-evicting it
- // would destroy exactly what tier-down preserved, and
- // re-compacting it is a no-op loop. Skip it entirely.
- if entry.compacted_at_us.is_some() {
- continue;
- }
+ // A2 tier-down replaces eviction for cold episodic pages.
compacted.push(CompactedPage {
id: entry.id,
path: entry.path.as_str().to_string(),
diff --git a/crates/ai-memory-consolidate/tests/suite/compaction_sweep.rs b/crates/ai-memory-consolidate/tests/suite/compaction_sweep.rs
index ead33c16..1ab250e3 100644
--- a/crates/ai-memory-consolidate/tests/suite/compaction_sweep.rs
+++ b/crates/ai-memory-consolidate/tests/suite/compaction_sweep.rs
@@ -568,3 +568,123 @@ async fn curator_skips_expired_pages() {
report.findings
);
}
+
+/// Test 7: a page that is ALREADY compacted stays terminal with the A2 flag
+/// OFF — the default everywhere.
+///
+/// The V65 marker is not A2's alone: the A3 cold-cluster dedup merge-note stub
+/// and the B2 dream pass both stamp `compacted: true` on their loser stubs
+/// under their OWN flags, and `compacted_at` is derived from that mirror at the
+/// single write choke point. So a durable residue exists in corpora where
+/// `compact_cold_episodic` was never turned on, and the terminality promise
+/// ("neither re-compacted nor evicted") has to hold on the eviction arm too —
+/// otherwise the sweep tombstones and then hard-deletes exactly what the merge
+/// chose to keep.
+///
+/// The control page in the same sweep is cold and NOT compacted: it must still
+/// be evicted, so the guard cannot make the sweep inert.
+#[tokio::test]
+async fn flag_off_keeps_an_already_compacted_residue_terminal() {
+ let fx = seed_fixture().await;
+
+ // The residue: the shape an A3/B2 merge-note stub lands in — a short body
+ // plus the `compacted: true` frontmatter mirror the store derives
+ // `compacted_at` from. A2 is never switched on in this test.
+ fx.wiki
+ .write_page(WritePageRequest {
+ workspace_id: fx.ws,
+ project_id: fx.proj,
+ path: PagePath::new("sessions/residue.md").unwrap(),
+ frontmatter: serde_json::json!({
+ "title": "Residue",
+ "compacted": true,
+ "merged_into": "sessions/survivor.md",
+ }),
+ body: "This page was merged into [[sessions/survivor.md]].\n".to_string(),
+ tier: Tier::Episodic,
+ pinned: false,
+ title: Some("Residue".to_string()),
+ admission_ctx: None,
+ author_id: None,
+ actor: ActorContext::anonymous(),
+ evidence: Vec::new(),
+ })
+ .await
+ .unwrap();
+ // The control: cold, episodic, no marker.
+ write(
+ &fx,
+ "sessions/plain.md",
+ "Plain Session",
+ Tier::Episodic,
+ false,
+ "a long prose body about a debugging session that nobody reopened",
+ )
+ .await;
+ backdate_latest(&fx, "sessions/residue.md", COLD_DAYS);
+ backdate_latest(&fx, "sessions/plain.md", COLD_DAYS);
+
+ // Pre-condition: the marker really is set, so a pass that ignores it would
+ // have something to ignore.
+ let (marker_before, _) = latest_marker_and_body(&fx, "sessions/residue.md");
+ assert!(
+ marker_before.is_some(),
+ "the frontmatter mirror set the V65 compacted_at marker"
+ );
+
+ let report = run_sweep_with_options(
+ &fx.store.reader,
+ &fx.store.writer,
+ Some(&fx.wiki),
+ fx.ws,
+ fx.proj,
+ &DecayParams::default(),
+ 0.0,
+ ObservationRetention::default(),
+ false,
+ )
+ .await
+ .unwrap();
+
+ // Not tombstoned: the residue is absent from `evicted`, and the control is
+ // there — the sweep still does its job.
+ assert_eq!(
+ report
+ .evicted
+ .iter()
+ .map(|e| e.path.as_str())
+ .collect::<Vec<_>>(),
+ vec!["sessions/plain.md"],
+ "only the non-compacted cold page is evicted: {:?}",
+ report.evicted
+ );
+ assert!(report.evicted[0].deleted, "the control eviction landed");
+ assert!(
+ report.compacted.is_empty(),
+ "nothing compacts with the flag off: {:?}",
+ report.compacted
+ );
+
+ // Not hard-deleted: the residue is still the live latest version, with its
+ // body and marker intact. (`DecayParams::default()` purges aged tombstones
+ // in the same pass, which is what would have destroyed it.)
+ let (marker_after, body_after) = latest_marker_and_body(&fx, "sessions/residue.md");
+ assert!(
+ marker_after.is_some(),
+ "the residue keeps its V65 marker after the sweep"
+ );
+ assert!(
+ body_after.contains("sessions/survivor.md"),
+ "the residue body survives: {body_after}"
+ );
+ assert!(
+ fx.store
+ .reader
+ .decay_candidates(fx.ws, fx.proj)
+ .await
+ .unwrap()
+ .iter()
+ .any(|c| c.path.as_str() == "sessions/residue.md"),
+ "the residue is still in the live set"
+ );
+}