Skip to content

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

Rust

Backfill title/description on export when derivable

Maxsuel Einstein8e36ba77cc9a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8114bfd5..64558451 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -165,6 +165,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `export-okf`'s generated `index.md` no longer has a prose sentence outside
its list structure. Some strict OKF v0.2 validators read §11.3 as
rejecting it. (#960)
+- `export-okf` now backfills a `title` (derived, same as `derive_title`) and
+ a `description` (from `summary`, else `abstract`) on an exported page when
+ missing, so a generic OKF consumer sees both §4.1-recommended keys. The
+ backfill only ever changes the bundle's copy, never the on-disk wiki file.
+ (#960)
## [2.4.1] - 2026-09-25
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 0251b6d5..453d1f5e 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -924,8 +924,11 @@ async fn build_okf_bundle_file(
anyhow::bail!("project has no wiki directory yet");
}
- // Validate + collect: every non-reserved .md must be conformant.
- let mut files: Vec<std::path::PathBuf> = Vec::new();
+ // Validate + collect: every non-reserved .md must be conformant. Keep
+ // the raw bytes read here (rather than re-reading from disk below) —
+ // the export augments a page's frontmatter/body only in the tarred
+ // copy, so the file is read exactly once.
+ let mut files: Vec<(std::path::PathBuf, String)> = Vec::new();
let mut families: Vec<String> = Vec::new();
let mut stack = vec![bundle_dir.clone()];
while let Some(dir) = stack.pop() {
@@ -971,7 +974,7 @@ async fn build_okf_bundle_file(
path.strip_prefix(&bundle_dir).unwrap_or(&path).display()
);
}
- files.push(path);
+ files.push((path, raw));
}
}
}
@@ -997,9 +1000,15 @@ async fn build_okf_bundle_file(
header.set_mode(0o644);
header.set_cksum();
tar.append_data(&mut header, "index.md", index.as_bytes())?;
- for path in files {
+ for (path, raw) in files {
let rel = path.strip_prefix(&bundle_dir).unwrap_or(&path);
- tar.append_path_with_name(&path, rel)?;
+ let rel_str = rel.to_string_lossy().replace('\\', "/");
+ let bytes = augment_page_for_export(&raw, &rel_str)?;
+ let mut header = tar::Header::new_gnu();
+ header.set_size(bytes.len() as u64);
+ header.set_mode(0o644);
+ header.set_cksum();
+ tar.append_data(&mut header, &rel_str, bytes.as_slice())?;
}
let encoder = tar.into_inner()?;
encoder.finish()?;
@@ -1009,6 +1018,63 @@ async fn build_okf_bundle_file(
Ok(tokio::fs::File::from_std(tar_file))
}
+/// Export-only augmentation of one wiki page's bytes for the OKF bundle:
+/// backfills `title`/`description` when derivable (issue #960 item 2).
+/// Never touches the on-disk wiki file — the caller tars the returned
+/// bytes instead of the source file.
+fn augment_page_for_export(raw: &str, rel_path: &str) -> anyhow::Result<Vec<u8>> {
+ let page_path = ai_memory_core::PagePath::new(rel_path.to_string())?;
+ let mut md = ai_memory_wiki::parse(raw)?;
+
+ let title_missing = md
+ .frontmatter
+ .get("title")
+ .and_then(serde_json::Value::as_str)
+ .is_none_or(|t| t.trim().is_empty());
+ let title =
+ title_missing.then(|| ai_memory_wiki::derive_title(&md.frontmatter, &md.body, &page_path));
+
+ let description_missing = md
+ .frontmatter
+ .get("description")
+ .and_then(serde_json::Value::as_str)
+ .is_none_or(|d| d.trim().is_empty());
+ let description = description_missing
+ .then(|| {
+ md.frontmatter
+ .get("summary")
+ .and_then(serde_json::Value::as_str)
+ .filter(|s| !s.trim().is_empty())
+ .or_else(|| {
+ md.frontmatter
+ .get("abstract")
+ .and_then(serde_json::Value::as_str)
+ .filter(|s| !s.trim().is_empty())
+ })
+ .map(str::to_string)
+ })
+ .flatten();
+
+ if !md.frontmatter.is_object() {
+ md.frontmatter = serde_json::Value::Object(serde_json::Map::new());
+ }
+ let map = md
+ .frontmatter
+ .as_object_mut()
+ .expect("object ensured above");
+ if let Some(title) = title {
+ map.insert("title".into(), serde_json::Value::String(title));
+ }
+ if let Some(description) = description {
+ map.insert(
+ "description".into(),
+ serde_json::Value::String(description),
+ );
+ }
+
+ Ok(ai_memory_wiki::emit(&md)?.into_bytes())
+}
+
// ---------------------------------------------------------------------
// status
// ---------------------------------------------------------------------
@@ -8591,6 +8657,98 @@ mod tests {
}
}
+ /// Issue #960 item 2: `derive_title` already knows the title (H1 or
+ /// path stem), but until now nothing wrote it into exported frontmatter.
+ /// Export-only: `title`/`description` land in the bundle's copy, never
+ /// on the on-disk wiki file.
+ #[tokio::test]
+ async fn export_okf_backfills_title_and_description() {
+ let (tmp, router) = read_page_test_router();
+ post_write_page(&router, "default", "scratch", "notes/seed.md", "seed").await;
+ let proj_dir = scratch_project_dir(tmp.path());
+ std::fs::create_dir_all(proj_dir.join("concepts")).unwrap();
+
+ // No title, no description/summary/abstract at all: title derives
+ // from the H1 heading; no description key is invented.
+ std::fs::write(
+ proj_dir.join("concepts/no-title.md"),
+ "---\ntype: Concept\n---\n\n# Heading Title\n\nBody.\n",
+ )
+ .unwrap();
+ // `summary` present: description comes from it.
+ std::fs::write(
+ proj_dir.join("concepts/has-summary.md"),
+ "---\ntype: Concept\nsummary: from summary\n---\n\nBody.\n",
+ )
+ .unwrap();
+ // `abstract` but no `summary`: description falls back to it.
+ std::fs::write(
+ proj_dir.join("concepts/has-abstract.md"),
+ "---\ntype: Concept\nabstract: from abstract\n---\n\nBody.\n",
+ )
+ .unwrap();
+ // Explicit title/description already present: untouched.
+ std::fs::write(
+ proj_dir.join("concepts/explicit.md"),
+ "---\ntype: Concept\ntitle: Explicit Title\ndescription: Explicit description\nsummary: ignored\n---\n\nBody.\n",
+ )
+ .unwrap();
+
+ let resp = router
+ .clone()
+ .oneshot(
+ Request::builder()
+ .method("POST")
+ .uri("/admin/export-okf?workspace=default&project=scratch")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+ let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
+ .await
+ .unwrap();
+ let dec = flate2::read::GzDecoder::new(std::io::Cursor::new(bytes.to_vec()));
+ let mut ar = tar::Archive::new(dec);
+ let mut bodies = std::collections::HashMap::new();
+ for entry in ar.entries().unwrap() {
+ let mut entry = entry.unwrap();
+ let name = entry.path().unwrap().display().to_string();
+ use std::io::Read as _;
+ let mut content = String::new();
+ entry.read_to_string(&mut content).unwrap();
+ bodies.insert(name, content);
+ }
+
+ let no_title = ai_memory_wiki::parse(&bodies["concepts/no-title.md"])
+ .unwrap()
+ .frontmatter;
+ assert_eq!(no_title["title"], "Heading Title");
+ assert!(no_title.get("description").is_none(), "{no_title:?}");
+
+ let has_summary = ai_memory_wiki::parse(&bodies["concepts/has-summary.md"])
+ .unwrap()
+ .frontmatter;
+ assert_eq!(has_summary["description"], "from summary");
+
+ let has_abstract = ai_memory_wiki::parse(&bodies["concepts/has-abstract.md"])
+ .unwrap()
+ .frontmatter;
+ assert_eq!(has_abstract["description"], "from abstract");
+
+ let explicit = ai_memory_wiki::parse(&bodies["concepts/explicit.md"])
+ .unwrap()
+ .frontmatter;
+ assert_eq!(explicit["title"], "Explicit Title");
+ assert_eq!(explicit["description"], "Explicit description");
+
+ // Never mutated on disk.
+ let on_disk = std::fs::read_to_string(proj_dir.join("concepts/no-title.md")).unwrap();
+ let on_disk_fm = ai_memory_wiki::parse(&on_disk).unwrap().frontmatter;
+ assert!(on_disk_fm.get("title").is_none(), "{on_disk_fm:?}");
+ }
+
/// Post-audit regression: the things a REAL deployment's tree holds
/// beside concept pages — typed scope manifests and _pending
/// proposal sidecars — must not block the export. Sidecars are