Skip to content

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

Rust

fix(wiki): skip fenced code when deriving a page title from its H1

Rudy Celekli7478e9811498
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fee7fa0b..e9ac1913 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Stopped deriving a page title from a `# comment` line inside a fenced code
+ block. A page without a frontmatter title that opened with a shell or Python
+ block was indexed under that comment instead of its real `#` heading, or
+ instead of its filename when it had none.
- Fixed `memory_lint` reporting findings for TTL-expired pages. The lint pass
now omits expired pages before evaluating episodic staleness, rule suggestions,
duplicate titles, and vector or LLM contradiction candidates, preventing
diff --git a/crates/ai-memory-wiki/src/markdown.rs b/crates/ai-memory-wiki/src/markdown.rs
index e7b3f4cf..8acc7df8 100644
--- a/crates/ai-memory-wiki/src/markdown.rs
+++ b/crates/ai-memory-wiki/src/markdown.rs
@@ -116,8 +116,8 @@ pub fn emit(md: &Markdown) -> WikiResult<String> {
/// Derive a page title.
///
-/// Priority: frontmatter.title (string) → first `# ` heading in body →
-/// path stem with the `.md` suffix stripped.
+/// Priority: frontmatter.title (string) → first `# ` heading in body
+/// outside fenced code blocks → path stem with the `.md` suffix stripped.
#[must_use]
pub fn derive_title(frontmatter: &serde_json::Value, body: &str, path: &PagePath) -> String {
if let Some(t) = frontmatter.get("title").and_then(serde_json::Value::as_str) {
@@ -126,7 +126,15 @@ pub fn derive_title(frontmatter: &serde_json::Value, body: &str, path: &PagePath
return trimmed.to_string();
}
}
+ let mut fence = None;
for line in body.lines() {
+ // A `# ` line inside a fenced block is a shell or Python comment,
+ // not a heading.
+ let (next, in_code) = CodeFence::step(fence, line);
+ fence = next;
+ if in_code {
+ continue;
+ }
if let Some(rest) = line.strip_prefix("# ") {
let trimmed = rest.trim();
if !trimmed.is_empty() {
@@ -1636,6 +1644,35 @@ mod tests {
);
}
+ /// Agent-written pages often open with a fenced command block. A
+ /// `# comment` inside it is code, not the page's H1, so it must not
+ /// become the indexed title.
+ #[test]
+ fn title_skips_comment_lines_inside_code_fences() {
+ let path = PagePath::new("notes/setup.md").unwrap();
+ let null = serde_json::Value::Null;
+ assert_eq!(
+ derive_title(
+ &null,
+ "```bash\n# install deps\nnpm i\n```\n# Real Title\n",
+ &path
+ ),
+ "Real Title"
+ );
+ assert_eq!(
+ derive_title(&null, "~~~python\n# compute\nx = 1\n~~~\nbody\n", &path),
+ "setup",
+ "a fenced comment must not replace the stem fallback"
+ );
+ // An unclosed fence runs to the end of the body.
+ assert_eq!(derive_title(&null, "```sh\n# only code\n", &path), "setup");
+ // A heading after an inline triple-backtick span is still a heading.
+ assert_eq!(
+ derive_title(&null, "```inline``` text\n# Heading\n", &path),
+ "Heading"
+ );
+ }
+
#[test]
fn extracts_internal_wiki_and_markdown_links() {
let path = PagePath::new("concepts/current.md").unwrap();