Skip to content

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

Rust

fix(importer): skip fenced code when deriving a page title

Rudy Celekli0aae83eb1e45
diff --git a/companions/ai-memory-importer/src/agentmemory/vault.rs b/companions/ai-memory-importer/src/agentmemory/vault.rs
index 76a2bf8c..af061bb3 100644
--- a/companions/ai-memory-importer/src/agentmemory/vault.rs
+++ b/companions/ai-memory-importer/src/agentmemory/vault.rs
@@ -22,7 +22,7 @@ use super::{
validate_id, validate_planned_path,
};
use crate::{
- DEFAULT_SERVER_URL, ImportClient, YamlMap, sha256_hex, sha256_parts, slugify,
+ DEFAULT_SERVER_URL, ImportClient, YamlMap, first_h1_line, sha256_hex, sha256_parts, slugify,
split_frontmatter, validate_label, validate_source_rel, yaml_string,
};
@@ -283,9 +283,7 @@ pub(crate) fn plan(dir: &Path, destination: &Destination, prefix: &str) -> Resul
links.get(target.trim_end_matches(".md")).cloned()
});
let body = ai_memory_client::sanitize_external_text(&linked);
- let title = body
- .lines()
- .find_map(|line| line.strip_prefix("# "))
+ let title = first_h1_line(&body)
.map(|title| one_line(title, MAX_EXPLICIT_TITLE_CHARS))
.filter(|title| !title.is_empty())
.unwrap_or_else(|| one_line(&page.id, MAX_EXPLICIT_TITLE_CHARS));
diff --git a/companions/ai-memory-importer/src/main.rs b/companions/ai-memory-importer/src/main.rs
index 24573696..335f74b6 100644
--- a/companions/ai-memory-importer/src/main.rs
+++ b/companions/ai-memory-importer/src/main.rs
@@ -1155,11 +1155,53 @@ fn yaml_tags(map: &YamlMap) -> Vec<String> {
}
}
fn first_h1(body: &str) -> Option<String> {
- body.lines()
- .find_map(|l| l.strip_prefix("# ").map(str::trim).map(str::to_owned))
+ first_h1_line(body)
+ .map(str::trim)
+ .map(str::to_owned)
.filter(|s| !s.is_empty())
}
+/// The text after `# ` on the first H1 line outside fenced code. Inside a
+/// fence a `# ` line is a shell or Python comment; fences follow CommonMark
+/// as the server's `derive_title` reads them (a run of 3+ backticks or
+/// tildes indented at most 3 spaces, closed by a bare run of the same glyph
+/// at least as long).
+fn first_h1_line(body: &str) -> Option<&str> {
+ let mut fence: Option<(char, usize)> = None;
+ for line in body.lines() {
+ let indent = line.len() - line.trim_start_matches(' ').len();
+ let marker = &line[indent..];
+ if let Some(glyph) = marker.chars().next().filter(|c| matches!(c, '`' | '~'))
+ && indent <= 3
+ {
+ let run = marker.chars().take_while(|&c| c == glyph).count();
+ let info = &marker[run..];
+ if run >= 3 {
+ match fence {
+ // A backtick info string cannot hold a backtick: this is
+ // an inline code span, not a fence.
+ None if glyph == '`' && info.contains('`') => {}
+ None => {
+ fence = Some((glyph, run));
+ continue;
+ }
+ Some((open, len)) if open == glyph && run >= len && info.trim().is_empty() => {
+ fence = None;
+ continue;
+ }
+ Some(_) => continue,
+ }
+ }
+ }
+ if fence.is_none()
+ && let Some(rest) = line.strip_prefix("# ")
+ {
+ return Some(rest);
+ }
+ }
+ None
+}
+
fn validate_source_rel(rel: &str) -> Result<()> {
let p = Path::new(rel);
if p.is_absolute() || p.components().any(|c| matches!(c, Component::ParentDir)) {
@@ -1448,6 +1490,20 @@ mod tests {
/// there by an editor, has CRLF line endings; one saved as "UTF-8 with
/// BOM" opens with U+FEFF. Either used to miss the fence, so kind, tier,
/// tags and pin were dropped and the YAML block was imported as body.
+ /// A `# ` line inside fenced code is a shell or Python comment, not the
+ /// page's H1; the server's `derive_title` skips fences the same way.
+ #[test]
+ fn title_skips_comment_lines_inside_code_fences() {
+ let parsed = parse_markdown("```bash\n# install deps\nnpm i\n```\n# Real Title\n").unwrap();
+ assert_eq!(parsed.title.as_deref(), Some("Real Title"));
+ let tilde = parse_markdown("~~~~\n# not it\n~~~\n# still code\n~~~~\n# Title\n").unwrap();
+ assert_eq!(tilde.title.as_deref(), Some("Title"));
+ let none = parse_markdown("```sh\n# just a comment\n```\nbody\n").unwrap();
+ assert_eq!(none.title, None);
+ let inline = parse_markdown("```code``` text\n# After\n").unwrap();
+ assert_eq!(inline.title.as_deref(), Some("After"));
+ }
+
#[test]
fn parses_omc_frontmatter_with_crlf_or_a_bom() {
let crlf = "---\r\ntitle: T\r\nkind: rule\r\ntier: procedural\r\ntags: [a, b]\r\npinned: true\r\n---\r\n# Body\r\ntext";