Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(store): omit TTL-expired pages from the briefing page count
Rudy Mizrahi Celekli390766de6cdb
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex a5320a02..f194c3f3 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -1669,7 +1669,10 @@ pub struct ObservationPageResult {/// by `ai-memory status`.#[derive(Debug, Clone, Default, Serialize)]pub struct StatusCounts {- /// Pages with `is_latest = 1`.+ /// Pages with `is_latest = 1`. The briefing surfaces+ /// ([`ReaderPool::briefing`] and its scoped variants) additionally exclude+ /// TTL-expired pages, so there the count matches what their own page lists+ /// can contain.pub pages_latest: u64,/// All page versions including superseded ones.pub pages_all: u64,@@ -8621,7 +8624,18 @@ impl ReaderPool {let cutoff_30d = now_us - 30 * day_us;let counts = StatusCounts {- pages_latest: count(conn, "SELECT COUNT(*) FROM pages WHERE is_latest = 1")?,+ // Every page list in this snapshot hides TTL-expired pages, so+ // the headline count has to hide them too or the brief+ // advertises pages the agent cannot see anywhere in it.+ // `pages_all` is deliberately the raw all-versions total.+ pages_latest: count_bound(+ conn,+ &format!(+ "SELECT COUNT(*) FROM pages WHERE is_latest = 1{not_expired}",+ not_expired = not_expired("pages", "?1"),+ ),+ &[&now_us],+ )?,pages_all: count(conn, "SELECT COUNT(*) FROM pages")?,sessions: count(conn, "SELECT COUNT(*) FROM sessions")?,observations: count(conn, "SELECT COUNT(*) FROM observations")?,@@ -8780,11 +8794,15 @@ impl ReaderPool {let cutoff_30d = now_us - 30 * day_us;let counts = StatusCounts {- pages_latest: count_project(+ // See the TTL note in `briefing_with_slot_visibility`.+ pages_latest: count_bound(conn,- "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1",- workspace_id,- project_id,+ &format!(+ "SELECT COUNT(*) FROM pages \+ WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1{not_expired}",+ not_expired = not_expired("pages", "?3"),+ ),+ &[workspace_id.as_bytes(), project_id.as_bytes(), &now_us],)?,pages_all: count_project(conn,@@ -9293,12 +9311,15 @@ impl ReaderPool {let cutoff_30d = now_us - 30 * day_us;let counts = StatusCounts {- pages_latest: count_workspace(+ // See the TTL note in `briefing_with_slot_visibility`.+ pages_latest: count_bound(conn,&format!(- "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND is_latest = 1{visible}"+ "SELECT COUNT(*) FROM pages \+ WHERE workspace_id = ?1 AND is_latest = 1{visible}{not_expired}",+ not_expired = not_expired("pages", "?2"),),- workspace_id,+ &[workspace_id.as_bytes(), &now_us],)?,pages_all: count_workspace(conn,diff --git a/crates/ai-memory-store/tests/suite/multi_session.rs b/crates/ai-memory-store/tests/suite/multi_session.rsindex 4c9ea51a..f0bf8bc1 100644--- a/crates/ai-memory-store/tests/suite/multi_session.rs+++ b/crates/ai-memory-store/tests/suite/multi_session.rs@@ -4536,3 +4536,67 @@ async fn memory_health_ignores_expired_pages() {assert!(p_detail.duplicates.is_empty());assert_eq!(p_detail.orphans.len(), 2);}++/// The briefing's `pages_latest` must agree with the page lists in the same+/// snapshot: an expired page is hidden from every one of them, so counting it+/// in the headline makes the brief advertise a page the agent cannot reach.+#[tokio::test]+async fn briefing_pages_latest_ignores_expired_pages() {+ 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/live.md", "Live", "content"))+ .await+ .unwrap();+ let mut expired = page(ws, proj, "notes/expired.md", "Expired", "content");+ expired.expires_at = Some(jiff::Timestamp::from_second(1).unwrap());+ store.writer.upsert_page(expired).await.unwrap();++ let live_pages = store+ .reader+ .list_unexpired_pages("acme", "shared-app")+ .await+ .unwrap();+ assert_eq!(live_pages.len(), 1, "one of the two pages is expired");++ let project = store+ .reader+ .briefing_for_project(ws, proj, 5, ai_memory_core::OwnerFilter::Any, false)+ .await+ .unwrap();+ assert_eq!(+ project.counts.pages_latest,+ live_pages.len() as u64,+ "project pages_latest counts only the unexpired latest page"+ );+ assert_eq!(+ project.recent_pages.len(),+ 1,+ "control: the list the count is supposed to agree with holds one page"+ );+ // Control: the all-versions total is deliberately TTL-blind, so a fix that+ // merely broke both counts the same way cannot satisfy this test.+ assert_eq!(+ project.counts.pages_all, 2,+ "control: pages_all stays the raw all-versions total"+ );++ let workspace = store+ .reader+ .briefing_for_workspace(ws, 5, ai_memory_core::OwnerFilter::Any, None)+ .await+ .unwrap();+ assert_eq!(workspace.counts.pages_latest, 1);+ assert_eq!(workspace.counts.pages_all, 2);++ let global = store+ .reader+ .briefing(5, ai_memory_core::OwnerFilter::Any)+ .await+ .unwrap();+ assert_eq!(global.counts.pages_latest, 1);+ assert_eq!(global.counts.pages_all, 2);+}