Skip to content

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

Rust

fix(wiki): preserve escaped and nested Markdown link labels

Rudy Celekli29b98571fe97
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a8c973ca..32cd5f14 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -39,6 +39,7 @@ version in [`docs/changelog/`](docs/changelog/):
of either vendor now receives the traffic. The docs now name the backends that
take no override: the ChatGPT backend behind `openai-oauth` and `codex`, and
`cursor`'s CLI. (#1347)
+- Preserved indexed Markdown links with escaped or nested brackets in their labels, including links exported in OKF bundles and reindexed after import.
- Corrected the `ExtractionState::NotApplicable` documentation and aligned the
generated OpenCode capture policy with the native evaluator: only an active
policy dropping a search/list tool reports `not-applicable`; an invalid
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 0b986268..ead356ea 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -10624,6 +10624,48 @@ mod tests {
assert!(on_disk.contains("[[decisions/b.md]]"));
}
+ #[tokio::test]
+ async fn export_okf_labels_preserve_links_when_the_bundle_is_reindexed() {
+ let (tmp, router) = read_page_test_router();
+ post_write_page(&router, "default", "scratch", "notes/seed.md", "seed").await;
+ let project = scratch_project_dir(tmp.path());
+ std::fs::create_dir_all(project.join("concepts")).unwrap();
+ std::fs::write(
+ project.join("concepts/a.md"),
+ "---\ntype: Concept\n---\n\nSee [[notes/seed.md|array[0] access]].\n",
+ )
+ .unwrap();
+ let response = router
+ .oneshot(
+ Request::builder()
+ .method("POST")
+ .uri("/admin/export-okf?workspace=default&project=scratch")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
+ .await
+ .unwrap();
+ let decoder = flate2::read::GzDecoder::new(std::io::Cursor::new(bytes));
+ let mut archive = tar::Archive::new(decoder);
+ let mut exported = String::new();
+ for entry in archive.entries().unwrap() {
+ let mut entry = entry.unwrap();
+ if entry.path().unwrap().display().to_string() == "concepts/a.md" {
+ use std::io::Read as _;
+ entry.read_to_string(&mut exported).unwrap();
+ }
+ }
+ let document = ai_memory_wiki::parse(&exported).unwrap();
+ let path = ai_memory_core::PagePath::new("concepts/a.md").unwrap();
+ let links = ai_memory_wiki::extract_links(&document.body, &path);
+ assert_eq!(links.len(), 1, "{exported}");
+ assert_eq!(links[0].path.as_str(), "notes/seed.md");
+ }
+
/// 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
diff --git a/crates/ai-memory-wiki/src/markdown.rs b/crates/ai-memory-wiki/src/markdown.rs
index 8567cb11..f9e0eb98 100644
--- a/crates/ai-memory-wiki/src/markdown.rs
+++ b/crates/ai-memory-wiki/src/markdown.rs
@@ -764,31 +764,45 @@ fn extract_wikilinks(line: &str, page_path: &PagePath, out: &mut BTreeSet<LinkKe
}
fn extract_markdown_links(line: &str, page_path: &PagePath, out: &mut BTreeSet<LinkKey>) {
- let mut start_at = 0;
- while let Some(rel_start) = line[start_at..].find('[') {
- let start = start_at + rel_start;
- if start > 0 && line.as_bytes()[start - 1] == b'!' {
- start_at = start + 1;
- continue;
- }
- let after_start = start + 1;
- let Some(rel_close) = line[after_start..].find(']') else {
- break;
- };
- let close = after_start + rel_close;
- if !line[close + 1..].starts_with('(') {
- start_at = close + 1;
- continue;
- }
- let target_start = close + 2;
- let Some((raw, target_end)) = parse_link_destination(&line[target_start..]) else {
- start_at = close + 1;
- continue;
- };
- if let Some(path) = normalize_link_target(raw, page_path, false) {
- out.insert((None, None, path));
+ // Track labels in one pass: rescanning an unmatched outer label would
+ // make repeated `[` quadratic and hide valid inner links.
+ let mut labels = Vec::new();
+ let mut last_escaped = None;
+ let mut pos = 0;
+ while pos < line.len() {
+ match line.as_bytes()[pos] {
+ b'\\' => {
+ pos += 1;
+ if let Some(next) = line[pos..].chars().next() {
+ last_escaped = Some(pos);
+ pos += next.len_utf8();
+ }
+ }
+ b'[' => {
+ labels.push(
+ pos > 0 && line.as_bytes()[pos - 1] == b'!' && last_escaped != Some(pos - 1),
+ );
+ pos += 1;
+ }
+ b']' => {
+ let image = labels.pop();
+ pos += 1;
+ if let Some(image) = image
+ && line.as_bytes().get(pos) == Some(&b'(')
+ && let Some((raw, target_end)) = parse_link_destination(&line[pos + 1..])
+ {
+ if !image && let Some(path) = normalize_link_target(raw, page_path, false) {
+ out.insert((None, None, path));
+ }
+ pos += target_end + 2;
+ // Images may appear in a link's label; links may not.
+ if !image {
+ labels.clear();
+ }
+ }
+ }
+ _ => pos += 1,
}
- start_at = target_start + target_end + 1;
}
}
@@ -1962,6 +1976,59 @@ mod tests {
);
}
+ #[test]
+ fn extract_links_handles_nested_and_escaped_labels() {
+ for label in [
+ "array[0] access",
+ r"array\[0\] access",
+ r"closing \] bracket",
+ r"literal \\ [index]",
+ "日本語 [索引]",
+ "outer [middle [inner]] label",
+ ] {
+ assert_eq!(linked(&format!("[{label}](b.md)")), ["b.md"], "{label}");
+ }
+ assert_eq!(linked("[unclosed [inner](b.md)"), ["b.md"]);
+ assert_eq!(linked("[outer [inner](b.md)](c.md)"), ["b.md"]);
+ assert!(linked(r"\[literal](b.md)").is_empty());
+ assert!(linked("![array[0] access](b.md)").is_empty());
+ assert_eq!(linked("[outer ![icon](image.png)](b.md)"), ["b.md"]);
+ assert_eq!(linked(r"\![label](b.md)"), ["b.md"]);
+ assert!(linked("`[array[0] access](b.md)`").is_empty());
+ assert!(linked("[unclosed [label]").is_empty());
+ }
+
+ #[test]
+ fn export_labels_keep_their_link_when_reindexed() {
+ let path = PagePath::new("concepts/a.md").unwrap();
+ for label in ["array[0] access", r"C:\work\notes", "日本語 [索引]"] {
+ let source = format!("[[notes/array.md|{label}]]");
+ let rewritten = rewrite_local_wikilinks(&source, &path, "proj");
+ assert_eq!(
+ extract_links(&rewritten, &path)
+ .iter()
+ .map(|link| link.path.as_str())
+ .collect::<Vec<_>>(),
+ ["notes/array.md"],
+ "{rewritten}"
+ );
+ }
+ }
+
+ #[test]
+ fn extract_links_stays_linear_on_nested_and_unclosed_labels() {
+ let root = PagePath::new("here.md").unwrap();
+ let started = std::time::Instant::now();
+ for body in [
+ "[".repeat(256 * 1024),
+ format!("{}[kept](b.md)", "[".repeat(256 * 1024)),
+ format!("{}{}(b.md)", "[".repeat(128 * 1024), "]".repeat(128 * 1024)),
+ ] {
+ let _ = extract_links(&body, &root);
+ }
+ assert!(started.elapsed() < std::time::Duration::from_secs(10));
+ }
+
#[test]
fn extract_links_rejects_pointy_destination_followed_by_junk() {
assert!(linked("[a](<b.md>zzz)").is_empty());
diff --git a/docs/okf.md b/docs/okf.md
index 3e0da10d..8321408c 100644
--- a/docs/okf.md
+++ b/docs/okf.md
@@ -18,6 +18,10 @@ standardized. The files are greppable, open in Obsidian, and import into
anything OKF-aware. Nothing is locked in: the export is a validated copy of
the files ai-memory already stores its memory in.
+Local wikilink labels containing brackets or backslashes remain ordinary
+Markdown links in an exported bundle. Reindexing the extracted concept files
+preserves their link targets.
+
The rest of this page is the design: how conformance is enforced and
how existing stores migrate.