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 project and workspace stats
Vinícius DS Andrade077329f66924
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fee7fa0b..8a8aa1cb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,11 @@ version in [`docs/changelog/`](docs/changelog/):## [Unreleased]### Fixed+- Fixed `list_projects_with_stats` and `list_workspaces_with_stats` counting+ TTL-expired pages and including their timestamps in project and workspace+ aggregates. The queries now apply the `not_expired` guard in their `LEFT JOIN`+ clauses, aligning home page project cards and `/api/v1` project and workspace+ summaries with `list_unexpired_pages`. ([#1425](https://github.com/akitaonrails/ai-memory/issues/1425))- Fixed `memory_lint` reporting findings for TTL-expired pages. The lint passnow omits expired pages before evaluating episodic staleness, rule suggestions,duplicate titles, and vector or LLM contradiction candidates, preventingdiff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rsindex 54b54287..09f9d703 100644--- a/crates/ai-memory-store/src/lib.rs+++ b/crates/ai-memory-store/src/lib.rs@@ -4795,6 +4795,78 @@ mod tests {assert!(s.last_updated.is_some());}+ #[tokio::test]+ async fn list_projects_and_workspaces_with_stats_hide_expired_pages() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let mixed = store+ .writer+ .get_or_create_project(ws, "mixed", None)+ .await+ .unwrap();+ let expired_only = store+ .writer+ .get_or_create_project(ws, "expired-only", None)+ .await+ .unwrap();++ let now = jiff::Timestamp::now().as_second();+ let at = |seconds: i64| Some(jiff::Timestamp::from_second(seconds).unwrap());++ for (path, expires_at) in [+ ("expired.md", at(now - 60)),+ ("future.md", at(now + 3_600)),+ ("live.md", None),+ ] {+ let mut page = sample_page(ws, mixed, path, "body");+ page.expires_at = expires_at;+ store.writer.upsert_page(page).await.unwrap();+ }++ let mut only = sample_page(ws, expired_only, "stale.md", "stale body");+ only.expires_at = at(now - 120);+ store.writer.upsert_page(only).await.unwrap();++ // Project summaries omit the expired pages from page_count and last_updated.+ let summaries = store.reader.list_projects_with_stats(None).await.unwrap();+ assert_eq!(summaries.len(), 2);++ let mixed_summary = summaries+ .iter()+ .find(|s| s.project_name == "mixed")+ .expect("mixed project summary");+ assert_eq!(mixed_summary.page_count, 2);+ assert!(mixed_summary.last_updated.is_some());++ let expired_summary = summaries+ .iter()+ .find(|s| s.project_name == "expired-only")+ .expect("expired-only project summary");+ assert_eq!(expired_summary.page_count, 0);+ assert_eq!(expired_summary.last_updated, None);++ // Filtered by workspace matches.+ let ws_projects = store+ .reader+ .list_projects_with_stats_for_workspace("default".to_string(), None)+ .await+ .unwrap();+ assert_eq!(ws_projects.len(), 2);++ // Workspace aggregates count only unexpired pages across projects.+ let ws_summaries = store.reader.list_workspaces_with_stats(None).await.unwrap();+ assert_eq!(ws_summaries.len(), 1);+ assert_eq!(ws_summaries[0].workspace_name, "default");+ assert_eq!(ws_summaries[0].project_count, 2);+ assert_eq!(ws_summaries[0].page_count, 2);+ assert!(ws_summaries[0].last_updated.is_some());+ }+/// #1208: a project id is an address, so it must be exposed exactly where/// the name is -- no wider. Boundary row 9's rule is that a restricted/// project is hidden from a caller without a grant, and this pins that thediff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex bdc4efff..9c2c016f 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -1874,7 +1874,7 @@ pub struct ProjectSummary {pub project_id: String,/// Name of the project within the workspace.pub project_name: String,- /// Number of `is_latest = 1` pages.+ /// Number of unexpired `is_latest = 1` pages.pub page_count: u64,/// ISO-8601 timestamp of the newest `updated_at`, or `None` when/// the project has no pages yet.@@ -2038,7 +2038,7 @@ pub struct WorkspaceSummary {pub workspace_name: String,/// Number of projects in this workspace.pub project_count: u64,- /// Number of `is_latest = 1` pages across the workspace.+ /// Number of unexpired `is_latest = 1` pages across the workspace.pub page_count: u64,/// ISO-8601 timestamp of the newest `updated_at`, or `None` when/// the workspace has no pages yet.@@ -9278,7 +9278,7 @@ impl ReaderPool {/// Return one row per (workspace, project) with page-count and/// last-updated aggregates. Used by the web UI project-list view.///- /// Only `is_latest = 1` pages are counted. `viewer` lists only the+ /// Only unexpired `is_latest = 1` pages are counted. `viewer` lists only the/// repositories that user may read — see [`readable_repository_filter`];/// `None` lists every one.///@@ -9293,7 +9293,7 @@ impl ReaderPool {/// Return one row per project within one workspace.///- /// Only `is_latest = 1` pages are counted. `viewer` as for+ /// Only unexpired `is_latest = 1` pages are counted. `viewer` as for/// [`Self::list_projects_with_stats`].////// # Errors@@ -9317,6 +9317,7 @@ impl ReaderPool {// three organisations listed by name — so the filter is on the// project row, not only on the pages counted beside it.let visible = readable_repository_predicate("p.id", viewer);+ let ttl = not_expired("pg", "?2");let mut stmt = conn.prepare(&format!("SELECT w.id AS workspace_id, \w.name AS workspace_name, \@@ -9326,12 +9327,13 @@ impl ReaderPool {MAX(pg.updated_at) AS last_updated_us \FROM workspaces w \JOIN projects p ON p.workspace_id = w.id \- LEFT JOIN pages pg ON pg.project_id = p.id AND pg.is_latest = 1 \+ LEFT JOIN pages pg ON pg.project_id = p.id AND pg.is_latest = 1{ttl} \WHERE (?1 IS NULL OR w.name = ?1){visible} \GROUP BY w.id, p.id \ORDER BY last_updated_us DESC NULLS LAST"))?;- let rows = stmt.query_map(params![workspace], |row| {+ let now = now_us();+ let rows = stmt.query_map(params![workspace, now], |row| {let workspace_id: Vec<u8> = row.get(0)?;let workspace_name: String = row.get(1)?;let project_id: Vec<u8> = row.get(2)?;@@ -9583,7 +9585,7 @@ impl ReaderPool {/// last-updated aggregates. Used by custom frontends that need a/// workspace chooser before narrowing into projects.///- /// Only `is_latest = 1` pages are counted.+ /// Only unexpired `is_latest = 1` pages are counted.////// With a `viewer`, a workspace is listed only when it holds at least one/// repository that user may read, and its counts cover only those. A@@ -9608,6 +9610,7 @@ impl ReaderPool {} else {""};+ let ttl = not_expired("pg", "?1");let mut stmt = conn.prepare(&format!("SELECT w.name AS workspace_name, \COUNT(DISTINCT p.id) AS project_count, \@@ -9615,11 +9618,12 @@ impl ReaderPool {MAX(pg.updated_at) AS last_updated_us \FROM workspaces w \LEFT JOIN projects p ON p.workspace_id = w.id{visible} \- LEFT JOIN pages pg ON pg.project_id = p.id AND pg.is_latest = 1 \+ LEFT JOIN pages pg ON pg.project_id = p.id AND pg.is_latest = 1{ttl} \GROUP BY w.id{only_readable} \ORDER BY w.name ASC"))?;- let rows = stmt.query_map([], |row| {+ let now = now_us();+ let rows = stmt.query_map(params![now], |row| {let workspace_name: String = row.get(0)?;let project_count: i64 = row.get(1)?;let page_count: i64 = row.get(2)?;diff --git a/crates/ai-memory-web/tests/suite/routes.rs b/crates/ai-memory-web/tests/suite/routes.rsindex c6b5948e..199e3724 100644--- a/crates/ai-memory-web/tests/suite/routes.rs+++ b/crates/ai-memory-web/tests/suite/routes.rs@@ -5266,6 +5266,12 @@ async fn project_tree_and_namespace_listing_hide_expired_pages() {assert!(!project.contains("Expired Note"), "{project}");assert!(!project.contains("Only Gone"), "{project}");+ // The home page cards count only unexpired pages.+ let (status, home) = get_body(&app, "/").await;+ assert_eq!(status, StatusCode::OK);+ assert!(home.contains("2 pages"), "{home}");+ assert!(!home.contains("4 pages"), "{home}");+let (status, namespace) = get_body(&app, "/w/default/scratch/p/notes/").await;assert_eq!(status, StatusCode::OK);assert!(@@ -5334,6 +5340,17 @@ async fn api_page_lists_hide_expired_pages_like_the_incremental_recent_form() {// The single-page read keeps answering for the expired page.let (status, _) = get_body(&app, &format!("{base}/pages/notes/expired.md")).await;assert_eq!(status, StatusCode::OK);++ // Project and workspace summaries count only unexpired pages.+ let (status, projects) = get_body(&app, "/projects").await;+ assert_eq!(status, StatusCode::OK);+ let projects: Value = serde_json::from_str(&projects).unwrap();+ assert_eq!(projects[0]["page_count"], 2);++ let (status, workspaces) = get_body(&app, "/workspaces").await;+ assert_eq!(status, StatusCode::OK);+ let workspaces: Value = serde_json::from_str(&workspaces).unwrap();+ assert_eq!(workspaces[0]["page_count"], 2);}#[tokio::test]