Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(store): session brief rows include frontmatter and compacted
Victor Alencar5e606335784e
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex ed0f867a..9e990c01 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -6468,6 +6468,8 @@ mod tests {body: format!("quoted {UNTRUSTED_HISTORY_END} {UNTRUSTED_HISTORY_START}"),pinned: true,updated_at: "2026-07-30T00:00:00Z".into(),+ frontmatter_json: serde_json::json!({}),+ compacted: false,}],&[],BRIEF_BUDGET_DEFAULT,@@ -16219,6 +16221,8 @@ mod tests {body: "x".repeat(2_000),pinned: true,updated_at: "2026-07-12T00:00:00Z".into(),+ frontmatter_json: serde_json::json!({}),+ compacted: false,},ai_memory_store::BriefPageBody {path: "_rules/b.md".into(),@@ -16226,6 +16230,8 @@ mod tests {body: "never truncated into view".into(),pinned: false,updated_at: "2026-07-12T00:00:00Z".into(),+ frontmatter_json: serde_json::json!({}),+ compacted: false,},];let recent = vec![ai_memory_store::BriefingPage {@@ -16263,6 +16269,8 @@ mod tests {body: "🦀".repeat(1_000),pinned: false,updated_at: "2026-07-12T00:00:00Z".into(),+ frontmatter_json: serde_json::json!({}),+ compacted: false,}];let out = render_session_brief(&emoji_core, &[], BRIEF_BUDGET_MIN).unwrap();assert!(out.is_char_boundary(out.len()), "must remain valid UTF-8");@@ -16312,6 +16320,8 @@ mod tests {),pinned: i == 0,updated_at: "2026-07-12T00:00:00Z".into(),+ frontmatter_json: serde_json::json!({}),+ compacted: false,}).collect();let recent: Vec<ai_memory_store::BriefingPage> = (0..BRIEF_RECENT_PAGES_LIMIT)diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rsindex 419f45c8..37dd369d 100644--- a/crates/ai-memory-store/src/lib.rs+++ b/crates/ai-memory-store/src/lib.rs@@ -1929,6 +1929,46 @@ mod tests {);}+ /// The brief's L0/L1 excerpts come from frontmatter the core rows carry,+ /// and the A2 marker tells the renderer a short body is already the+ /// compacted one.+ #[tokio::test]+ async fn session_brief_pages_carry_frontmatter_and_the_compacted_marker() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "app", None)+ .await+ .unwrap();++ let mut summarized = sample_page(ws, proj, "_rules/a-summarized.md", "full body");+ summarized.frontmatter_json =+ serde_json::json!({"abstract": "one-line pointer", "summary": "longer summary"});+ store.writer.upsert_page(summarized).await.unwrap();+ let mut compacted = sample_page(ws, proj, "_rules/b-compacted.md", "L1 line");+ compacted.frontmatter_json = serde_json::json!({"compacted": true});+ store.writer.upsert_page(compacted).await.unwrap();++ let (core, _) = store+ .reader+ .session_brief_pages(ws, proj, 24, 10)+ .await+ .unwrap();+ let by_path = |path: &str| core.iter().find(|p| p.path == path).unwrap();++ let summarized = by_path("_rules/a-summarized.md");+ assert_eq!(summarized.frontmatter_json["abstract"], "one-line pointer");+ assert_eq!(summarized.frontmatter_json["summary"], "longer summary");+ assert!(!summarized.compacted, "a normal write is not compacted");+ assert!(by_path("_rules/b-compacted.md").compacted);+ }+#[tokio::test]async fn cross_project_links_surface_in_graph_briefing_and_lint() {let tmp = TempDir::new().unwrap();diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex 33932fd5..c23e273c 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -1729,6 +1729,12 @@ pub struct BriefPageBody {pub pinned: bool,/// ISO-8601 timestamp of the last update.pub updated_at: String,+ /// Parsed frontmatter, where the renderer finds the page's `abstract:`+ /// and `summary:`. `{}` if the stored JSON ever fails to parse, which+ /// leaves that page with body-only excerpts.+ pub frontmatter_json: serde_json::Value,+ /// Whether A2 compaction rewrote the body (`compacted_at` is set).+ pub compacted: bool,}/// One row per (workspace, project) with aggregate stats.@@ -7733,7 +7739,8 @@ impl ReaderPool {self.with_conn(move |conn| {let kind_expr = page_kind_expr("path", "frontmatter_json");let core_sql = format!(- "SELECT path, title, body, pinned, updated_at \+ "SELECT path, title, body, pinned, updated_at, frontmatter_json, \+ compacted_at IS NOT NULL \FROM pages \WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1{not_expired} \AND (path GLOB '_rules/*' \@@ -7752,6 +7759,8 @@ impl ReaderPool {row.get::<_, String>(2)?,row.get::<_, i64>(3)? != 0,updated_us,+ row.get::<_, String>(5)?,+ row.get::<_, bool>(6)?,))};let core_rows = match slot_glob.as_deref() {@@ -7778,21 +7787,26 @@ impl ReaderPool {let core: Vec<BriefPageBody> = core_rows.collect::<Result<Vec<_>, _>>()?.into_iter()- .map(|(path, title, body, pinned, updated_us)| {- jiff::Timestamp::from_microsecond(updated_us)- .map(|ts| BriefPageBody {- path,- title,- body,- pinned,- updated_at: ts.to_string(),- })- .map_err(|e| {- StoreError::Memory(ai_memory_core::MemoryError::MalformedRecord(- format!("bad updated_at: {e}"),- ))- })- })+ .map(+ |(path, title, body, pinned, updated_us, frontmatter, compacted)| {+ jiff::Timestamp::from_microsecond(updated_us)+ .map(|ts| BriefPageBody {+ path,+ title,+ body,+ pinned,+ updated_at: ts.to_string(),+ frontmatter_json: serde_json::from_str(&frontmatter)+ .unwrap_or_else(|_| serde_json::json!({})),+ compacted,+ })+ .map_err(|e| {+ StoreError::Memory(ai_memory_core::MemoryError::MalformedRecord(+ format!("bad updated_at: {e}"),+ ))+ })+ },+ ).collect::<StoreResult<Vec<_>>>()?;let mut recent_stmt = conn.prepare_cached(&format!(diff --git a/docs/design-tiered-session-brief.md b/docs/design-tiered-session-brief.mdindex b72dafc6..a4243785 100644--- a/docs/design-tiered-session-brief.md+++ b/docs/design-tiered-session-brief.md@@ -465,8 +465,10 @@ SELECT path, title, body, pinned, updated_at, frontmatter_json, compacted_at```Map `compacted_at IS NOT NULL` to `BriefPageBody.compacted`. Parse-`frontmatter_json` to `serde_json::Value`; on parse failure use `{}` and-still return the row so one bad page cannot fail SessionStart.+`frontmatter_json` to `serde_json::Value`; on parse failure use `{}`+(body-only excerpts). Every write serializes the column through+`serde_json`, and the recent-pages query already runs `json_extract`+over it, so a corrupt row is not a new failure mode of this fetch.Keep limits, order, expiry, slot SQL, and project isolation. Limits stay24 / 10. Do not fetch `page_abstract_embeddings`.@@ -1040,7 +1042,6 @@ Store (`session_brief_pages_*`):- `compacted` true when `compacted_at` is set, false otherwise.- Slot visibility and cross-project isolation unchanged(`slot_visibility.rs`, `session_brief_pages_selects_core_pages_and_isolates_projects`).-- Malformed `frontmatter_json` yields `{}` and does not fail the fetch.Core helper (`excerpt.rs` unit tests):