Skip to content

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

Rust

fix(wiki): do not index links written inside inline code

kevin932773909341b19f
diff --git a/crates/ai-memory-wiki/src/markdown.rs b/crates/ai-memory-wiki/src/markdown.rs
index 8367ac9c..1839c093 100644
--- a/crates/ai-memory-wiki/src/markdown.rs
+++ b/crates/ai-memory-wiki/src/markdown.rs
@@ -131,7 +131,9 @@ type LinkKey = (Option<String>, Option<String>, String);
/// Supports `[[wiki links]]`, `[[wiki links|labels]]`, cross-project
/// `[[project:path]]` / `[[workspace/project:path]]` wikilinks, and
/// ordinary markdown links such as `[label](../decisions/foo.md#anchor)`.
-/// External URLs, anchors, images, and non-markdown assets are ignored.
+/// External URLs, anchors, images, and non-markdown assets are ignored, and
+/// so is anything inside a fenced block or an inline code span, which the
+/// page shows as code rather than as a link.
/// Returned values are normalised to wiki-root-relative [`LinkTarget`]s.
#[must_use]
pub fn extract_links(body: &str, page_path: &PagePath) -> Vec<LinkTarget> {
@@ -147,8 +149,9 @@ pub fn extract_links(body: &str, page_path: &PagePath) -> Vec<LinkTarget> {
if in_fence {
continue;
}
- extract_wikilinks(line, page_path, &mut out);
- extract_markdown_links(line, page_path, &mut out);
+ let line = blank_inline_code(line);
+ extract_wikilinks(&line, page_path, &mut out);
+ extract_markdown_links(&line, page_path, &mut out);
}
out.into_iter()
@@ -163,6 +166,58 @@ pub fn extract_links(body: &str, page_path: &PagePath) -> Vec<LinkTarget> {
.collect()
}
+/// `line` with each inline code span, backticks included, replaced by
+/// spaces. The web renderer shows `` `[[notes/x]]` `` as code, so indexing
+/// it minted a backlink the source page never offers and, for a
+/// `[[project:path]]` example, a broken-link lint finding. Blanking rather
+/// than cutting keeps a link whose label is code (`` [`foo`](foo.md) ``)
+/// intact. A run of backticks opens a span only a run of the same length
+/// closes (CommonMark 6.1); with no closer on the line it is literal text.
+/// A span that continues onto the next line is not seen, as the line-based
+/// fence check above does not see an indented code block either.
+fn blank_inline_code(line: &str) -> std::borrow::Cow<'_, str> {
+ if !line.contains('`') {
+ return std::borrow::Cow::Borrowed(line);
+ }
+ let bytes = line.as_bytes();
+ let run_at = |i: usize| bytes[i..].iter().take_while(|&&b| b == b'`').count();
+ let mut out = String::with_capacity(line.len());
+ let mut copied = 0;
+ let mut i = 0;
+ while i < bytes.len() {
+ if bytes[i] != b'`' {
+ i += 1;
+ continue;
+ }
+ let open = run_at(i);
+ let mut j = i + open;
+ let close = loop {
+ if j >= bytes.len() {
+ break None;
+ }
+ if bytes[j] == b'`' {
+ let run = run_at(j);
+ if run == open {
+ break Some(j + run);
+ }
+ j += run;
+ } else {
+ j += 1;
+ }
+ };
+ let Some(end) = close else {
+ i += open;
+ continue;
+ };
+ out.push_str(&line[copied..i]);
+ out.extend(std::iter::repeat_n(' ', line[i..end].chars().count()));
+ copied = end;
+ i = end;
+ }
+ out.push_str(&line[copied..]);
+ std::borrow::Cow::Owned(out)
+}
+
/// Body wikilinks plus typed `relations:` frontmatter edges — the full
/// outgoing link set every page-write path stores. One entry point so a
/// new writer cannot forget the typed half.
@@ -739,4 +794,28 @@ mod tests {
let paths: Vec<&str> = links.iter().map(|l| l.path.as_str()).collect();
assert_eq!(paths, vec!["notes/kept.md"]);
}
+
+ #[test]
+ fn extract_links_ignores_inline_code_spans() {
+ // The web page renders all of these code spans as code, so a link
+ // written inside one is not a link a reader can follow.
+ let path = PagePath::new("notes/a.md").unwrap();
+ let body = "Write `[[notes/syntax]]` or `[[other-project:notes/x]]` to link.\n\
+ A ``[[notes/double]] `nested` `` span, and `[label](md-link.md)`.\n\
+ See [`the flow`](flow.md) and [[notes/kept]].\n\
+ A lone ` backtick hides nothing: [[notes/after-tick]].\n";
+ let links = extract_links(body, &path);
+ let targets: Vec<(Option<&str>, &str)> = links
+ .iter()
+ .map(|l| (l.project.as_deref(), l.path.as_str()))
+ .collect();
+ assert_eq!(
+ targets,
+ vec![
+ (None, "notes/after-tick.md"),
+ (None, "notes/flow.md"),
+ (None, "notes/kept.md"),
+ ]
+ );
+ }
}
diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rs
index fe77491d..32f495e0 100644
--- a/crates/ai-memory-wiki/src/wiki.rs
+++ b/crates/ai-memory-wiki/src/wiki.rs
@@ -3022,6 +3022,41 @@ mod tests {
assert!(ai_memory_core::okf::is_conformant(&parsed.frontmatter));
}
+ /// A `[[project:path]]` written as inline code is an example, not a
+ /// dependency: the page shows it as code. Indexing it made lint report a
+ /// broken cross-project link on a page that has none.
+ #[tokio::test]
+ async fn a_cross_project_link_in_inline_code_is_not_a_dangling_dependency() {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store.writer.get_or_create_workspace("w").await.unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "p", None)
+ .await
+ .unwrap();
+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();
+
+ wiki.write_page(req(
+ ws,
+ proj,
+ "notes/linking.md",
+ "Link across projects with `[[other-project:notes/example]]`.\n\
+ This page depends on [[other-project:notes/real]].\n",
+ serde_json::json!({"title": "Linking"}),
+ ))
+ .await
+ .unwrap();
+
+ let dangling = store
+ .reader
+ .dangling_cross_project_links(ws, proj)
+ .await
+ .unwrap();
+ let paths: Vec<&str> = dangling.iter().map(|d| d.path.as_str()).collect();
+ assert_eq!(paths, vec!["notes/real.md"]);
+ }
+
/// OKF requires every timestamp to carry an explicit UTC offset, while
/// the TTL key also accepts a bare date. The file on disk must name the
/// instant the TTL machinery hides the page, not the date as typed.