Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(titles): derive H1 title for empty-frontmatter pages (#599)
AkitaOnRails6a7c77d52943
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 135484b6..3185b142 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Fixed+- Pages with an empty frontmatter `title` no longer read back titleless+ or trip a bogus duplicate-title lint (#599). Auto-improve could store+ `title: ""` while the page had a proper `# H1`; now `memory_read_page`+ derives the title from the H1 (then the path stem) when the stored one+ is blank, the duplicate-title lint ignores blank titles instead of+ collapsing them into one `Multiple pages share title ""` finding, and+ the consolidator derives a title from the body H1 at write time so new+ pages never store an empty one. The DB `title` column (search) was+ already correct; this aligns the frontmatter-title readers with it+ without rewriting stored pages.- Local-embedding model download (`fetch_model`) now bounds a stalledconnection instead of hanging startup forever (#602): a 30s connecttimeout and a generous 600s overall ceiling (large model files, sodiff --git a/crates/ai-memory-consolidate/src/consolidator.rs b/crates/ai-memory-consolidate/src/consolidator.rsindex 8c82def6..b47f94d2 100644--- a/crates/ai-memory-consolidate/src/consolidator.rs+++ b/crates/ai-memory-consolidate/src/consolidator.rs@@ -605,8 +605,22 @@ fn build_update(actor: &ai_memory_core::ActorContext,author_id: Option<ai_memory_core::UserId>,) -> ConsolidatorResult<(WritePageRequest, ConsolidationOutcome)> {+ // Never store an empty title: when a proposal omits one, fall back to+ // the body's H1 (then the path stem), the same derivation the wiki+ // write path uses — otherwise the page lands with `title: ""` in+ // frontmatter, reads back titleless, and trips the duplicate-title+ // lint (#599). `derive_title` returns the frontmatter title when+ // present, so passing a null frontmatter here means "derive from the+ // body/path".+ let effective_title = if upd.title.trim().is_empty() {+ let probe_path = PagePath::new(upd.path.clone())+ .unwrap_or_else(|_| PagePath::new("notes/untitled.md").expect("static path is valid"));+ ai_memory_wiki::derive_title(&serde_json::Value::Null, &upd.body_markdown, &probe_path)+ } else {+ upd.title.clone()+ };let final_path = if upd.kind == crate::types::PageKind::Rule {- let slug = slugify_for_rule(&upd.title);+ let slug = slugify_for_rule(&effective_title);format!("_rules/{slug}.md")} else {upd.path.clone()@@ -615,7 +629,10 @@ fn build_update(let tier = upd.tier;let mut fm = serde_json::Map::new();- fm.insert("title".into(), serde_json::Value::String(upd.title.clone()));+ fm.insert(+ "title".into(),+ serde_json::Value::String(effective_title.clone()),+ );fm.insert("tier".into(),serde_json::Value::String(tier_as_str(tier).into()),@@ -627,7 +644,7 @@ fn build_update("kind".into(),serde_json::Value::String(upd.kind.as_str().into()),);- if let Some(summary) = usable_summary(upd.summary.as_deref(), &upd.title) {+ if let Some(summary) = usable_summary(upd.summary.as_deref(), &effective_title) {fm.insert("summary".into(), serde_json::Value::String(summary));}if !upd.tags.is_empty() {@@ -671,7 +688,7 @@ fn build_update(body: upd.body_markdown.clone(),tier,pinned: false,- title: Some(upd.title.clone()),+ title: Some(effective_title.clone()),admission_ctx: Some(AdmissionContext {op: AdmissionOp::Consolidate,actor: actor.clone(),@@ -683,7 +700,7 @@ fn build_update(let outcome = ConsolidationOutcome {path,dry_run,- new_title: upd.title.clone(),+ new_title: effective_title.clone(),new_body_markdown: upd.body_markdown.clone(),page_id: None,tags: upd.tags.clone(),@@ -1901,6 +1918,38 @@ mod tests {);}+ #[test]+ fn build_update_derives_title_from_h1_when_proposal_title_is_empty() {+ // A proposal with no title must not store `title: ""` — it derives+ // from the body's H1, matching the wiki write path (#599).+ let update = crate::types::ConsolidatedPageUpdate {+ path: "concepts/thing.md".into(),+ tier: Tier::Semantic,+ kind: crate::types::PageKind::Fact,+ title: " ".into(), // blank+ body_markdown: "# The Real Title\n\nbody text".into(),+ summary: None,+ tags: Vec::new(),+ slot_kind: SlotKind::State,+ entities: Vec::new(),+ };+ let (req, outcome) = build_update(+ WorkspaceId::new(),+ ProjectId::new(),+ &update,+ false,+ &ai_memory_core::ActorContext::default(),+ None,+ )+ .unwrap();+ assert_eq!(+ req.frontmatter["title"], "The Real Title",+ "empty proposal title must derive from the body H1, not persist as \"\""+ );+ assert_eq!(req.title.as_deref(), Some("The Real Title"));+ assert_eq!(outcome.new_title, "The Real Title");+ }+#[test]fn build_update_persists_only_normalized_bounded_entities() {let update = crate::types::ConsolidatedPageUpdate {diff --git a/crates/ai-memory-consolidate/src/lint.rs b/crates/ai-memory-consolidate/src/lint.rsindex ffcadf3e..079bcf7d 100644--- a/crates/ai-memory-consolidate/src/lint.rs+++ b/crates/ai-memory-consolidate/src/lint.rs@@ -357,8 +357,14 @@ fn rule_based_findings(candidates: &[DecayCandidate], stale_days: f64) -> Vec<Li});}// Duplicate-title tracking: peek the frontmatter for a `title` field.+ // An empty/blank title is not a meaningful shared title — several+ // pages carrying `title: ""` (e.g. auto-improve pages whose stored+ // frontmatter title was never filled) must not all collapse into one+ // bogus `Multiple pages share title ""` finding (#599). Real titles+ // still dedupe as before.if let Some(fm) = frontmatter.as_ref()&& let Some(t) = fm.get("title").and_then(serde_json::Value::as_str)+ && !t.trim().is_empty(){titles.entry(t.to_lowercase())@@ -730,6 +736,34 @@ mod tests {assert_eq!(dupes[0].pages.len(), 2);}+ #[test]+ fn rule_pass_ignores_empty_titles_as_duplicates() {+ // Several pages carrying `title: ""` (or blank) must NOT collapse+ // into one bogus `Multiple pages share title ""` finding (#599).+ let base = DecayCandidate {+ id: ai_memory_core::PageId::new(),+ path: ai_memory_core::PagePath::new("concepts/a.md").unwrap(),+ tier: Tier::Semantic,+ pinned: false,+ updated_at_us: Timestamp::now().as_microsecond(),+ access_count: 0,+ last_accessed_at_us: None,+ frontmatter_json: r#"{"title": ""}"#.into(),+ expires_at_us: None,+ salience: None,+ };+ let blank = DecayCandidate {+ path: ai_memory_core::PagePath::new("concepts/b.md").unwrap(),+ frontmatter_json: r#"{"title": " "}"#.into(),+ ..base.clone()+ };+ let findings = rule_based_findings(&[base, blank], STALE_DAYS);+ assert!(+ !findings.iter().any(|f| f.kind == "duplicate"),+ "empty/blank titles must not be reported as a shared title"+ );+ }+/// M20: a page tagged `kind: rule` in its frontmatter triggers/// a rule_suggestion finding pointing the user at CLAUDE.md.#[test]diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex d1ee6d97..d2f71bf5 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -3107,11 +3107,12 @@ impl AiMemoryServer {// so operators can fix the disk source of truth.match wiki.read_page(ws, proj, &page_path) {Ok(md) => {- let title = md- .frontmatter- .get("title")- .and_then(|v| v.as_str())- .map(str::to_string);+ // Derive the title so an empty/absent frontmatter `title`+ // falls back to the body's H1 (then the path stem), instead+ // of surfacing "" — auto-improve pages whose stored+ // frontmatter title was never filled otherwise read back+ // titleless despite a proper `# Heading` (#599).+ let title = ai_memory_wiki::derive_title(&md.frontmatter, &md.body, &page_path);ok_json(&serde_json::json!({"path": page_path.to_string(),"title": title,