Skip to content

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

Rust

fix(store): ignore links from superseded or expired pages in health orphans

Rudy Mizrahi Celekli313a0a57770f
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index 775b29d7..a5320a02 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -9511,6 +9511,13 @@ impl ReaderPool {
let ttl = not_expired("pages", "?");
let ttl_p = not_expired("p", "?");
+ // Incoming links are only evidence of a reference when their
+ // source page is itself a live page. `replace_links_in_tx` deletes
+ // rows only for the new version's id, so a superseded version's
+ // links survive, and `refresh_incoming_links_for_path` repoints
+ // them at the new latest id without filtering on `is_latest` — so
+ // a link removed by an edit still answers for the target.
+ let ttl_fp = not_expired("fp", "?");
let stale: i64 = conn
.query_row(
@@ -9555,11 +9562,16 @@ impl ReaderPool {
"SELECT COUNT(*) FROM pages p \
WHERE p.workspace_id = ? AND p.is_latest = 1{clause_p}{ttl_p} \
AND NOT EXISTS (SELECT 1 FROM links l WHERE l.from_page_id = p.id) \
- AND NOT EXISTS (SELECT 1 FROM links l WHERE l.to_page_id = p.id)"
+ AND NOT EXISTS ( \
+ SELECT 1 FROM links l \
+ JOIN pages fp ON fp.id = l.from_page_id \
+ AND fp.is_latest = 1{ttl_fp} \
+ WHERE l.to_page_id = p.id)"
),
params_from_iter(
std::iter::once(ws())
.chain(proj.clone())
+ .chain(std::iter::once(Value::Integer(now_us)))
.chain(std::iter::once(Value::Integer(now_us))),
),
|row| row.get(0),
@@ -9654,6 +9666,7 @@ impl ReaderPool {
);
let ttl_pg = not_expired("pg", "?");
+ let ttl_fp = not_expired("fp", "?");
let ttl_inner = not_expired("pages", "?");
// Params follow placeholder order: ws, cutoff, [proj], now_us, limit.
@@ -9702,11 +9715,15 @@ impl ReaderPool {
)?
.collect::<Result<Vec<_>, _>>()?;
- // Params: ws, [proj], now_us, limit.
+ // Params: ws, [proj], now_us, now_us, limit.
let orphan_sql = format!(
"{select} WHERE pg.workspace_id = ? AND pg.is_latest = 1{clause}{ttl_pg} \
AND NOT EXISTS (SELECT 1 FROM links l WHERE l.from_page_id = pg.id) \
- AND NOT EXISTS (SELECT 1 FROM links l WHERE l.to_page_id = pg.id) \
+ AND NOT EXISTS ( \
+ SELECT 1 FROM links l \
+ JOIN pages fp ON fp.id = l.from_page_id \
+ AND fp.is_latest = 1{ttl_fp} \
+ WHERE l.to_page_id = pg.id) \
ORDER BY pg.updated_at DESC LIMIT ?"
);
let mut orphan_stmt = conn.prepare(&orphan_sql)?;
@@ -9716,6 +9733,7 @@ impl ReaderPool {
std::iter::once(ws())
.chain(proj.clone())
.chain(std::iter::once(Value::Integer(now_us)))
+ .chain(std::iter::once(Value::Integer(now_us)))
.chain(std::iter::once(Value::Integer(limit))),
),
health_page_from_row,
diff --git a/crates/ai-memory-store/tests/suite/multi_session.rs b/crates/ai-memory-store/tests/suite/multi_session.rs
index 8e0a6c33..ba97ea3b 100644
--- a/crates/ai-memory-store/tests/suite/multi_session.rs
+++ b/crates/ai-memory-store/tests/suite/multi_session.rs
@@ -4313,6 +4313,73 @@ async fn open_feedback_findings_ignores_expired_pages() {
assert_eq!(findings[0].reason.as_deref(), Some("live page is stale"));
}
+/// A link authored by a superseded page version is not a reference.
+/// `replace_links_in_tx` deletes rows only for the new version's id, so v1's
+/// row survives an edit, and `refresh_incoming_links_for_path` repoints it at
+/// the new latest id without filtering on `is_latest` — so without a join to a
+/// live source page, a link the author removed still answers for its target.
+#[tokio::test]
+async fn memory_health_orphans_ignore_links_from_a_superseded_version() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let (ws, proj) = scope(&store).await;
+
+ store
+ .writer
+ .upsert_page(page(ws, proj, "notes/b.md", "B", "target"))
+ .await
+ .unwrap();
+
+ // a.md v1 links b.md.
+ let mut a_v1 = page(ws, proj, "notes/a.md", "A", "see b");
+ a_v1.links = vec![ai_memory_core::LinkTarget {
+ workspace: None,
+ project: None,
+ path: PagePath::new("notes/b.md").unwrap(),
+ relation: None,
+ }];
+ store.writer.upsert_page(a_v1).await.unwrap();
+
+ // Control: while the link is live, b.md is not an orphan.
+ let (_, _, linked_orphans) = store
+ .reader
+ .memory_health_for_project(ws, proj)
+ .await
+ .unwrap();
+ assert_eq!(
+ linked_orphans, 0,
+ "with a live inbound link neither page is an orphan"
+ );
+
+ // a.md v2 drops the link; v1's row survives, repointed at b.md's latest id.
+ store
+ .writer
+ .upsert_page(page(ws, proj, "notes/a.md", "A", "no link now"))
+ .await
+ .unwrap();
+
+ let (_, _, orphans) = store
+ .reader
+ .memory_health_for_project(ws, proj)
+ .await
+ .unwrap();
+ assert_eq!(
+ orphans, 2,
+ "both pages are orphans once the only link is gone from the latest version"
+ );
+
+ let detail = store
+ .reader
+ .health_detail_for_project(ws, proj, 10)
+ .await
+ .unwrap();
+ let orphan_paths: Vec<&str> = detail.orphans.iter().map(|p| p.path.as_str()).collect();
+ assert!(
+ orphan_paths.contains(&"notes/b.md"),
+ "b.md lost its only inbound link and must be listed: {orphan_paths:?}"
+ );
+}
+
/// Memory health counts and detail lists ignore TTL-expired pages.
#[tokio::test]
async fn memory_health_ignores_expired_pages() {