Skip to content

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

Rust

fix(wiki): pin _global wikilinks to the default workspace

Vinícius Andrade3e200f8660f8
diff --git a/crates/ai-memory-wiki/src/markdown.rs b/crates/ai-memory-wiki/src/markdown.rs
index f977e190..7dbf421b 100644
--- a/crates/ai-memory-wiki/src/markdown.rs
+++ b/crates/ai-memory-wiki/src/markdown.rs
@@ -9,7 +9,7 @@
use std::collections::BTreeSet;
use std::ops::Range;
-use ai_memory_core::{LinkTarget, PagePath};
+use ai_memory_core::{DEFAULT_WORKSPACE_NAME, GLOBAL_SCOPE_PROJECT, LinkTarget, PagePath};
use serde::{Deserialize, Serialize};
use crate::error::WikiResult;
@@ -167,8 +167,10 @@ impl CodeFence {
/// Extract internal wiki links from a markdown body.
///
/// 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)`.
+/// `[[project:path]]` / `[[workspace/project:path]]` wikilinks, the reserved
+/// `[[_global:path]]` form (always the `_global` project in the default
+/// workspace), and ordinary markdown links such as
+/// `[label](../decisions/foo.md#anchor)`.
/// 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.
@@ -338,13 +340,7 @@ pub fn extract_relation_links(frontmatter: &serde_json::Value) -> Vec<LinkTarget
continue;
};
for target in list.iter().filter_map(|v| v.as_str()) {
- let (workspace, project, raw_path) = match target.split_once(':') {
- None => (None, None, target),
- Some((scope, path)) => match scope.split_once('/') {
- None => (None, Some(scope.to_string()), path),
- Some((ws, proj)) => (Some(ws.to_string()), Some(proj.to_string()), path),
- },
- };
+ let (workspace, project, raw_path) = split_scope(target);
// Same terminal normalization as wikilinks: extension-less
// targets gain `.md`; a directory target, a stem-less `.md`, and
// anything with a non-md extension are not pages and are skipped.
@@ -380,6 +376,11 @@ pub fn extract_relation_links(frontmatter: &serde_json::Value) -> Vec<LinkTarget
/// of a wikilink target. Returns `(workspace, project, path_part)`. URL and
/// scheme-prefixed targets carry no scope (the `:` belongs to the scheme);
/// [`normalize_link_target`] rejects those downstream.
+///
+/// `[[_global:path]]` is a project-only qualifier, but the reserved
+/// preferences project lives in [`DEFAULT_WORKSPACE_NAME`], not in the
+/// source page's workspace (issue #1042). An explicit
+/// `[[workspace/_global:path]]` keeps the named workspace.
fn split_scope(target: &str) -> LinkKey {
let lower = target.to_ascii_lowercase();
if target.contains("://")
@@ -397,11 +398,14 @@ fn split_scope(target: &str) -> LinkKey {
.chars()
.all(|c| c.is_alphanumeric() || matches!(c, '-' | '_' | '/' | '.'));
if scope_ok {
- let (workspace, project) = match scope.split_once('/') {
+ let (mut workspace, project) = match scope.split_once('/') {
Some((ws, proj)) => (Some(ws.trim().to_string()), proj.trim()),
None => (None, scope),
};
if !project.is_empty() {
+ if workspace.is_none() && project == GLOBAL_SCOPE_PROJECT {
+ workspace = Some(DEFAULT_WORKSPACE_NAME.to_string());
+ }
return (
workspace,
Some(project.to_string()),
@@ -528,7 +532,13 @@ fn resolve_local_wikilink(
.map_or((raw, None), |(t, l)| (t, Some(l)));
let target_part = target_part.trim();
let (workspace, project, path_part) = split_scope(target_part);
- if workspace.is_some() {
+ // `split_scope` pins `[[_global:path]]` to the default workspace. When
+ // we are exporting that same reserved project, the target is still a
+ // local page even though the workspace is now explicit.
+ let exporting_global_home = own_project == GLOBAL_SCOPE_PROJECT
+ && project.as_deref() == Some(GLOBAL_SCOPE_PROJECT)
+ && workspace.as_deref() == Some(DEFAULT_WORKSPACE_NAME);
+ if workspace.is_some() && !exporting_global_home {
return None; // no cross-workspace Markdown equivalent
}
if let Some(project) = &project
@@ -905,6 +915,30 @@ mod tests {
assert_eq!(causes.project.as_deref(), Some("proj"));
}
+ #[test]
+ fn extract_relation_links_global_target_pins_default_workspace() {
+ let fm = serde_json::json!({
+ "relations": {
+ "fixes": ["_global:python-env"],
+ "contradicts": ["other/_global:notes/x.md"],
+ }
+ });
+ let links = extract_relation_links(&fm);
+ let fixes = links
+ .iter()
+ .find(|l| l.relation == Some(ai_memory_core::Relation::Fixes))
+ .unwrap();
+ assert_eq!(fixes.workspace.as_deref(), Some(DEFAULT_WORKSPACE_NAME));
+ assert_eq!(fixes.project.as_deref(), Some(GLOBAL_SCOPE_PROJECT));
+ assert_eq!(fixes.path.as_str(), "python-env.md");
+ let contra = links
+ .iter()
+ .find(|l| l.relation == Some(ai_memory_core::Relation::Contradicts))
+ .unwrap();
+ assert_eq!(contra.workspace.as_deref(), Some("other"));
+ assert_eq!(contra.project.as_deref(), Some(GLOBAL_SCOPE_PROJECT));
+ }
+
#[test]
fn unknown_relation_keys_and_bad_targets_are_skipped() {
let fm = serde_json::json!({
@@ -983,6 +1017,58 @@ mod tests {
assert_eq!(l.path.as_str(), "decisions/adr-1.md");
}
+ /// `[[_global:path]]` is a project-only qualifier, but the reserved
+ /// preferences project lives in the default workspace (#1042). Pinning
+ /// the workspace here is what lets a page outside `default` resolve.
+ #[test]
+ fn extract_links_global_wikilink_pins_default_workspace() {
+ let links = extract_links(
+ "see [[_global:python-env]] and [[_global:local-machine.md|env]]",
+ &page(),
+ );
+ let pinned: Vec<_> = links
+ .iter()
+ .filter(|l| l.project.as_deref() == Some(GLOBAL_SCOPE_PROJECT))
+ .collect();
+ assert_eq!(pinned.len(), 2, "{links:?}");
+ assert!(
+ pinned
+ .iter()
+ .all(|l| l.workspace.as_deref() == Some(DEFAULT_WORKSPACE_NAME)),
+ "project-only _global must name the default workspace: {links:?}"
+ );
+ assert!(
+ pinned
+ .iter()
+ .any(|l| l.path.as_str() == "python-env.md")
+ );
+ assert!(
+ pinned
+ .iter()
+ .any(|l| l.path.as_str() == "local-machine.md")
+ );
+
+ let sibling = extract_links("[[infra:runbooks/02.md]]", &page());
+ let l = sibling
+ .iter()
+ .find(|l| l.is_cross_project())
+ .expect("infra");
+ assert_eq!(l.workspace, None, "non-global project-only stays source-workspace");
+ assert_eq!(l.project.as_deref(), Some("infra"));
+
+ let explicit = extract_links("[[other/_global:notes/x.md]]", &page());
+ let l = explicit
+ .iter()
+ .find(|l| l.is_cross_project())
+ .expect("explicit");
+ assert_eq!(
+ l.workspace.as_deref(),
+ Some("other"),
+ "an explicit workspace is kept"
+ );
+ assert_eq!(l.project.as_deref(), Some(GLOBAL_SCOPE_PROJECT));
+ }
+
#[test]
fn extract_links_url_wikilink_is_not_a_scope() {
// `https://...` must not be parsed as project "https".
@@ -1216,6 +1302,27 @@ mod tests {
rewrite_local_wikilinks("[[ws/other-project:decisions/b.md]]", &path, "proj"),
"[[ws/other-project:decisions/b.md]]"
);
+ assert_eq!(
+ rewrite_local_wikilinks("[[_global:python-env.md]]", &path, "proj"),
+ "[[_global:python-env.md]]"
+ );
+ }
+
+ #[test]
+ fn rewrite_local_wikilinks_resolves_global_when_exporting_global() {
+ let path = PagePath::new("concepts/a.md").unwrap();
+ assert_eq!(
+ rewrite_local_wikilinks("[[_global:python-env.md]]", &path, GLOBAL_SCOPE_PROJECT),
+ "[_global:python-env.md](../python-env.md)"
+ );
+ assert_eq!(
+ rewrite_local_wikilinks(
+ "[[default/_global:python-env.md]]",
+ &path,
+ GLOBAL_SCOPE_PROJECT,
+ ),
+ "[default/_global:python-env.md](../python-env.md)"
+ );
}
#[test]