Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

fix: link an informed_by title holding # or | by basename so the link resolves (#299)

Brenno Ferrari527ed8cb6920
diff --git a/.claude/scripts/lib/mcp-capture.ts b/.claude/scripts/lib/mcp-capture.ts
index 276a11c..2af7692 100644
--- a/.claude/scripts/lib/mcp-capture.ts
+++ b/.claude/scripts/lib/mcp-capture.ts
@@ -389,12 +389,33 @@ export function findToolMarkup(input: Record<string, unknown>): string | null {
return describeToolMarkup(input)?.field ?? null;
}
+/** The characters that end a wikilink target: heading, alias, and the brackets. */
+const LINK_TERMINATORS = /[#|[\]]/;
+
+/**
+ * A wikilink to a resolved note, written by `name` when the name survives as
+ * a target and by the note's basename otherwise. Titles routinely carry an
+ * issue number — `Wake on write (#418)` — and resolve as an alias, but
+ * `[[Wake on write (#418)]]` is cut at the `#` into the note `Wake on write (`
+ * and the heading `418)`. The check that admitted the link compared the whole
+ * name; every link reader splits first. So a name holding a terminator links
+ * by basename and keeps the name as the display text, minus the brackets,
+ * which no link can contain at all. A basename that itself holds one cannot
+ * be linked by any form, and is written as plain text rather than broken.
+ */
+export function wikilinkTo(basename: string, name: string): string {
+ if (!LINK_TERMINATORS.test(name)) return `[[${name}]]`;
+ if (LINK_TERMINATORS.test(basename)) return name;
+ const shown = name.replace(/[[\]]/g, "").replace(/\s+/g, " ").trim();
+ return `[[${basename}|${shown}]]`;
+}
+
/** Render the note body. Pure, so the shape can be asserted without writing. */
export function renderCapture(
input: CaptureInput,
dest: Destination,
caller: string,
- resolvable: ReadonlySet<string>,
+ resolvable: ReadonlyMap<string, string>,
now: Date,
): string {
const day = now.toISOString().slice(0, 10);
@@ -410,7 +431,9 @@ export function renderCapture(
// trips the vault's own wikilink gate.
const linkOrPlain = (raw: unknown): string => {
const name = String(raw).replace(/^\[\[|\]\]$/g, "").trim();
- return resolvable.has(name.toLowerCase()) ? `- [[${name}]]` : `- ${name} _(no note yet)_`;
+ const target = resolvable.get(name.toLowerCase());
+ if (target === undefined) return `- ${name} _(no note yet)_`;
+ return `- ${wikilinkTo(target, name)}`;
};
// `informed_by` becomes wikilinks, which is the point of asking for it: the
@@ -500,7 +523,7 @@ export function captureNote(
manifest: Record<string, unknown> | null | undefined,
caller: string | null,
input: CaptureInput,
- resolvable: ReadonlySet<string>,
+ resolvable: ReadonlyMap<string, string>,
opts: { now?: Date; reindex?: () => boolean } = {},
): CaptureResult {
const now = opts.now ?? new Date();
diff --git a/.claude/scripts/lib/mcp-memory-bridge.ts b/.claude/scripts/lib/mcp-memory-bridge.ts
index 5f0ed7d..ef390df 100644
--- a/.claude/scripts/lib/mcp-memory-bridge.ts
+++ b/.claude/scripts/lib/mcp-memory-bridge.ts
@@ -74,23 +74,36 @@ export function unquoteScalar(raw: string): string {
}
export function resolvableNames(files: readonly VisibleFile[]): Set<string> {
- const names = new Set<string>();
+ return new Set(resolvableTargets(files).keys());
+}
+
+/**
+ * Every name a visible note answers to — its basename and its aliases,
+ * lowercased — mapped to the note's BASENAME. A name is not always usable as
+ * a link target: a title like `Wake on write (#418)` resolves as an alias,
+ * but `[[Wake on write (#418)]]` splits at `#`. The basename is what a link
+ * has to point at in that case.
+ */
+export function resolvableTargets(files: readonly VisibleFile[]): Map<string, string> {
+ const names = new Map<string, string>();
+ const add = (name: string, label: string): void => {
+ if (name && !names.has(name)) names.set(name, label);
+ };
for (const f of files) {
- names.add(f.label.toLowerCase());
+ add(f.label.toLowerCase(), f.label);
const head = readHead(f.full, HEAD_CHARS);
if (head === null) continue; // an unreadable file still contributes its basename
const block = head.match(/^aliases:\s*$([\s\S]*?)^(?=\S|---)/m);
if (block?.[1]) {
for (const line of block[1].split("\n")) {
const m = line.match(/^\s*-\s*(.+?)\s*$/);
- if (m?.[1]) names.add(unquoteScalar(m[1]).toLowerCase());
+ if (m?.[1]) add(unquoteScalar(m[1]).toLowerCase(), f.label);
}
}
const inline = head.match(/^aliases:\s*\[(.*)\]\s*$/m);
if (inline?.[1]) {
for (const part of inline[1].split(",")) {
- const name = unquoteScalar(part).toLowerCase();
- if (name) names.add(name);
+ add(unquoteScalar(part).toLowerCase(), f.label);
}
}
}
diff --git a/.claude/scripts/lib/mcp-server.ts b/.claude/scripts/lib/mcp-server.ts
index 4c95656..a5d4595 100644
--- a/.claude/scripts/lib/mcp-server.ts
+++ b/.claude/scripts/lib/mcp-server.ts
@@ -29,7 +29,7 @@ import {
import { expandNote } from "./mcp-graph.ts";
import { qmdProbe, qmdSearch, type QmdClient } from "./mcp-qmd-client.ts";
import { callerProject, callerProjectSource, CALLER_VAR, isVaultItself, PROJECT_MARKER, sumAuditField, sanitize } from "./mcp-caller.ts";
-import { callerPlatforms, digestsFrom, resolvableNames } from "./mcp-memory-bridge.ts";
+import { callerPlatforms, digestsFrom, resolvableNames, resolvableTargets } from "./mcp-memory-bridge.ts";
import { captureNote, describeToolMarkup, toolMarkupRefusal } from "./mcp-capture.ts";
import { semanticMemoryOrder } from "./mcp-memory-bridge.ts";
import { TOOLS } from "./mcp-tools.ts";
@@ -580,7 +580,7 @@ export function createHandlers(deps: ServerDeps): Handlers {
return toolMarkupRefusal(corrupted, args);
}
const who = callerProject(session.roots);
- const resolvable = resolvableNames(visibleFiles(ctx.vaultRoot, policy));
+ const resolvable = resolvableTargets(visibleFiles(ctx.vaultRoot, policy));
let r;
try {
r = captureNote(ctx.vaultRoot, policy, ctx.manifest, who, args, resolvable, {
diff --git a/.claude/scripts/tests/mcp-capture.test.ts b/.claude/scripts/tests/mcp-capture.test.ts
index 7c20e4d..c34a234 100644
--- a/.claude/scripts/tests/mcp-capture.test.ts
+++ b/.claude/scripts/tests/mcp-capture.test.ts
@@ -25,15 +25,20 @@ import {
renderCapture,
captureNote,
clampDescription,
+ wikilinkTo,
yamlQuoted,
type Destination,
} from "../lib/mcp-capture.ts";
import type { ExposurePolicy } from "../lib/mcp-exposure.ts";
-import { resolvableNames } from "../lib/mcp-memory-bridge.ts";
+import { resolvableNames, resolvableTargets } from "../lib/mcp-memory-bridge.ts";
+import { extractWikilinkTargets } from "../lib/wikilinks.ts";
import { rmTemp } from "./_helpers.ts";
const NOW = new Date("2026-07-26T10:00:00Z");
+
+/** Resolvable names → basenames, as resolvableTargets builds them. */
+const names = (...n: string[]): Map<string, string> => new Map(n.map((x) => [x.toLowerCase(), x]));
const POLICY: ExposurePolicy = { roots: ["brain", "projects"], neverExpose: new Set(), source: "manifest", memoryRoot: "memories" };
function withVault(fn: (dir: string) => void): void {
@@ -88,7 +93,7 @@ describe("slugifying a title", () => {
withVault((dir) => {
const { title: _omit, ...noTitle } = BASIC;
assert.throws(
- () => captureNote(dir, POLICY, {}, null, noTitle, new Set(), { now: NOW }),
+ () => captureNote(dir, POLICY, {}, null, noTitle, new Map(), { now: NOW }),
/empty filename/,
);
});
@@ -224,7 +229,7 @@ describe("rendering a capture", () => {
const dest: Destination = { dir: "/v/projects/atlas/notes", rel: "projects/atlas/notes", project: "atlas", routed: "caller-identity" };
test("carries frontmatter the vault's own validator would accept", () => {
- const md = renderCapture(BASIC, dest, "atlas", new Set(["atlas"]), NOW);
+ const md = renderCapture(BASIC, dest, "atlas", names("atlas"), NOW);
assert.match(md, /^---\n/);
assert.match(md, /date: 2026-07-26/);
assert.match(md, /description: 'Shipped the archive command behind a flag\.'/);
@@ -239,7 +244,7 @@ describe("rendering a capture", () => {
{ ...BASIC, informed_by: ["Gotchas", "Note That Does Not Exist"] },
dest,
"atlas",
- new Set(["atlas", "gotchas"]),
+ names("atlas", "Gotchas"),
NOW,
);
assert.match(md, /- \[\[Gotchas\]\]/);
@@ -247,19 +252,55 @@ describe("rendering a capture", () => {
assert.ok(!md.includes("[[Note That Does Not Exist]]"));
});
+ /**
+ * A title carrying `#` resolved as an alias, so it was linked by name —
+ * and every link reader splits at `#` before looking the target up.
+ */
+ test("a name holding # or | links by basename and keeps the name as display text", () => {
+ const base = "2026-09-06-wake-on-write-418";
+ const targets = new Map([
+ ["wake on write (#418)", base],
+ ["a | b choice", "2026-09-07-a-b-choice"],
+ ["plain note", "Plain Note"],
+ ]);
+ const md = renderCapture(
+ { ...BASIC, informed_by: ["Wake on write (#418)", "A | B choice", "Plain note"] },
+ dest,
+ "atlas",
+ targets,
+ NOW,
+ );
+ assert.match(md, /- \[\[2026-09-06-wake-on-write-418\|Wake on write \(#418\)\]\]/);
+ assert.match(md, /- \[\[2026-09-07-a-b-choice\|A \| B choice\]\]/);
+ // No terminator: written exactly as before, by name.
+ assert.match(md, /- \[\[Plain note\]\]/);
+ const links = extractWikilinkTargets(md);
+ assert.ok(links.includes(base), links.join(","));
+ assert.ok(links.includes("2026-09-07-a-b-choice"), links.join(","));
+ assert.ok(!links.some((l) => l.startsWith("Wake on write (")), links.join(","));
+ });
+
+ test("brackets in a linked name are dropped from the display text, which no link can hold", () => {
+ assert.equal(wikilinkTo("2026-01-01-x", "Draft [v2] #3"), "[[2026-01-01-x|Draft v2 #3]]");
+ });
+
+ test("a basename that itself holds a terminator is written as plain text, never a broken link", () => {
+ assert.equal(wikilinkTo("Old #7 note", "Old #7 note"), "Old #7 note");
+ });
+
test("the project home link is emitted only when the project resolves by name", () => {
- assert.match(renderCapture(BASIC, dest, "atlas", new Set(["atlas"]), NOW), /- \[\[atlas\]\]/);
- assert.ok(!renderCapture(BASIC, dest, "atlas", new Set(), NOW).includes("[[atlas]]"));
+ assert.match(renderCapture(BASIC, dest, "atlas", names("atlas"), NOW), /- \[\[atlas\]\]/);
+ assert.ok(!renderCapture(BASIC, dest, "atlas", new Map(), NOW).includes("[[atlas]]"));
});
test("empty sections are omitted rather than left as empty headings", () => {
- const md = renderCapture({ title: "t", summary: "s", kind: "note" }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ title: "t", summary: "s", kind: "note" }, dest, "atlas", new Map(), NOW);
assert.ok(!md.includes("## What changed"));
assert.ok(!md.includes("## Open"));
});
test("a quote in the summary cannot break the frontmatter", () => {
- const md = renderCapture({ ...BASIC, summary: 'He said "hello" today' }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ ...BASIC, summary: 'He said "hello" today' }, dest, "atlas", new Map(), NOW);
const front = md.slice(0, md.indexOf("\n---", 4));
const line = front.split("\n").find((l) => l.startsWith("description:")) ?? "";
// A double quote is now ordinary content, because the scalar is
@@ -269,7 +310,7 @@ describe("rendering a capture", () => {
});
test("the routing decision is recorded in the note itself", () => {
- assert.match(renderCapture(BASIC, dest, "atlas", new Set(), NOW), /routing: caller-identity/);
+ assert.match(renderCapture(BASIC, dest, "atlas", new Map(), NOW), /routing: caller-identity/);
});
});
@@ -280,7 +321,7 @@ describe("rendering a capture", () => {
describe("writing a capture", () => {
test("a dry run writes nothing and previews", () => {
withVault((dir) => {
- const r = captureNote(dir, POLICY, {}, "atlas", { ...BASIC, dry_run: true }, new Set(), { now: NOW });
+ const r = captureNote(dir, POLICY, {}, "atlas", { ...BASIC, dry_run: true }, new Map(), { now: NOW });
assert.equal(r.written, false);
assert.ok(r.preview);
assert.equal(readdirSync(dir).length, 0, "nothing may touch disk");
@@ -289,9 +330,9 @@ describe("writing a capture", () => {
test("a note record takes the date prefix; a decision does not", () => {
withVault((dir) => {
- const note = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW });
+ const note = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW });
assert.match(note.path, /2026-07-26-add-the-archive-command\.md$/);
- const dec = captureNote(dir, POLICY, {}, null, { ...BASIC, kind: "decision" }, new Set(), { now: NOW });
+ const dec = captureNote(dir, POLICY, {}, null, { ...BASIC, kind: "decision" }, new Map(), { now: NOW });
// A decision is a living document; a creation-date prefix would invert
// its recency signal.
assert.match(dec.path, /\/add-the-archive-command\.md$/);
@@ -300,7 +341,7 @@ describe("writing a capture", () => {
test("the file lands where it was routed, with its content", () => {
withVault((dir) => {
- const r = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW });
+ const r = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW });
const md = readFileSync(join(dir, r.path), "utf8");
assert.match(md, /# Add the archive command/);
assert.match(md, /- added cmd/);
@@ -310,8 +351,8 @@ describe("writing a capture", () => {
test("a colliding title takes the next suffix rather than overwriting", () => {
// The obvious check-then-rename loses one capture entirely, with no error.
withVault((dir) => {
- const a = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW });
- const b = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW });
+ const a = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW });
+ const b = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW });
assert.notEqual(a.path, b.path);
assert.match(b.path, /-2\.md$/);
assert.ok(readFileSync(join(dir, a.path), "utf8").length > 0, "the first must survive");
@@ -320,7 +361,7 @@ describe("writing a capture", () => {
test("no temp file is left behind", () => {
withVault((dir) => {
- const r = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW });
+ const r = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW });
const leftovers = readdirSync(join(dir, "inbox")).filter((f) => f.endsWith(".tmp"));
assert.deepEqual(leftovers, [], "a half-written note must never be visible to the indexer");
assert.ok(r.written);
@@ -329,7 +370,7 @@ describe("writing a capture", () => {
test("an empty title is refused before anything is written", () => {
withVault((dir) => {
- assert.throws(() => captureNote(dir, POLICY, {}, null, { ...BASIC, title: "!!!" }, new Set()), /empty filename/);
+ assert.throws(() => captureNote(dir, POLICY, {}, null, { ...BASIC, title: "!!!" }, new Map()), /empty filename/);
assert.equal(readdirSync(dir).length, 0);
});
});
@@ -337,7 +378,7 @@ describe("writing a capture", () => {
test("a refused folder writes nothing at all", () => {
withVault((dir) => {
assert.throws(
- () => captureNote(dir, POLICY, {}, "atlas", { ...BASIC, folder: "work/secret" }, new Set()),
+ () => captureNote(dir, POLICY, {}, "atlas", { ...BASIC, folder: "work/secret" }, new Map()),
/not inside an exposed root/,
);
assert.equal(readdirSync(dir).length, 0, "a refusal must not create the folder either");
@@ -346,9 +387,9 @@ describe("writing a capture", () => {
test("the reindex result is reported, so an unfindable note is not called a success", () => {
withVault((dir) => {
- const ok = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW, reindex: () => true });
+ const ok = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW, reindex: () => true });
assert.equal(ok.indexed, true);
- const bad = captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW, reindex: () => false });
+ const bad = captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW, reindex: () => false });
assert.equal(bad.indexed, false);
});
});
@@ -357,7 +398,7 @@ describe("writing a capture", () => {
withVault((dir) => {
mkdirSync(join(dir, "inbox"), { recursive: true });
writeFileSync(join(dir, "inbox", "existing.md"), "keep me", "utf8");
- captureNote(dir, POLICY, {}, null, BASIC, new Set(), { now: NOW });
+ captureNote(dir, POLICY, {}, null, BASIC, new Map(), { now: NOW });
assert.equal(readFileSync(join(dir, "inbox", "existing.md"), "utf8"), "keep me");
});
});
@@ -372,18 +413,18 @@ describe("a capture is reachable by its own title", () => {
test("the title is carried as an alias, because the basename never is", () => {
const title = "I3 lands: a soak over the whole pipeline";
- const md = renderCapture({ title, summary: "s", kind: "note" }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ title, summary: "s", kind: "note" }, dest, "atlas", new Map(), NOW);
assert.match(md, /^aliases:\n {2}- 'I3 lands: a soak over the whole pipeline'$/m);
});
test("a title with a quote cannot break the frontmatter", () => {
- const md = renderCapture({ title: 'The "obvious" fix', summary: "s", kind: "note" }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ title: 'The "obvious" fix', summary: "s", kind: "note" }, dest, "atlas", new Map(), NOW);
// A double quote is ordinary content inside a single-quoted scalar.
assert.match(md, /^ {2}- 'The "obvious" fix'$/m);
});
test("an empty title emits no alias block rather than an empty one", () => {
- const md = renderCapture({ title: "", summary: "s", kind: "note" }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ title: "", summary: "s", kind: "note" }, dest, "atlas", new Map(), NOW);
assert.doesNotMatch(md, /^aliases:/m);
});
@@ -406,7 +447,7 @@ describe("a capture is reachable by its own title", () => {
for (const [label, title] of HOSTILE) {
test(`${label} in the title cannot break the frontmatter`, () => {
- const md = renderCapture({ title, summary: title, kind: "note" }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ title, summary: title, kind: "note" }, dest, "atlas", new Map(), NOW);
assert.equal((md.match(/^---$/gm) ?? []).length, 2, "exactly one frontmatter block");
@@ -427,7 +468,7 @@ describe("a capture is reachable by its own title", () => {
}
test("a backslash survives literally rather than as an escape", () => {
- const md = renderCapture({ title: "A path C:\\temp\\x", summary: "s", kind: "note" }, dest, "atlas", new Set(), NOW);
+ const md = renderCapture({ title: "A path C:\\temp\\x", summary: "s", kind: "note" }, dest, "atlas", new Map(), NOW);
assert.match(md, /^ {2}- 'A path C:\\temp\\x'$/m);
});
@@ -437,10 +478,32 @@ describe("a capture is reachable by its own title", () => {
assert.equal(yamlQuoted("a\n\nb"), "'a b'");
});
+ test("round trip: citing a captured note whose title holds # writes a link that resolves to it", () => {
+ withVault((dir) => {
+ const title = "The pane wakes on a store write (#418)";
+ captureNote(dir, POLICY, {}, null, { title, summary: "s", kind: "note" }, new Map(), { now: NOW });
+ const [first] = readdirSync(join(dir, "inbox"));
+ const label = first!.replace(/\.md$/, "");
+ const targets = resolvableTargets([{ label, full: join(dir, "inbox", first!), scope: "inbox" }]);
+ captureNote(
+ dir,
+ POLICY,
+ {},
+ null,
+ { title: "Follow-up", summary: "s", kind: "note", informed_by: [title] },
+ targets,
+ { now: NOW },
+ );
+ const second = readdirSync(join(dir, "inbox")).find((f) => f !== first)!;
+ const links = extractWikilinkTargets(readFileSync(join(dir, "inbox", second), "utf-8"));
+ assert.ok(links.includes(label), `expected a link to ${label}, got ${links.join(", ")}`);
+ });
+ });
+
test("round trip: a hostile title still resolves through resolvableNames", () => {
for (const [, title] of HOSTILE) {
withVault((dir) => {
- captureNote(dir, POLICY, {}, null, { title, summary: "s", kind: "note" }, new Set(), { now: NOW });
+ captureNote(dir, POLICY, {}, null, { title, summary: "s", kind: "note" }, new Map(), { now: NOW });
const files = readdirSync(join(dir, "inbox"));
const label = files[0]!.replace(/\.md$/, "");
const resolvable = resolvableNames([{ label, full: join(dir, "inbox", files[0]!), scope: "inbox" }]);
@@ -463,7 +526,7 @@ describe("a capture is reachable by its own title", () => {
test("round trip: a written capture resolves by title through resolvableNames", () => {
withVault((dir) => {
const title = "The harden audit of I2b found a live I9 breach the suite could not see";
- captureNote(dir, POLICY, {}, null, { title, summary: "s", kind: "note" }, new Set(), { now: NOW });
+ captureNote(dir, POLICY, {}, null, { title, summary: "s", kind: "note" }, new Map(), { now: NOW });
const files = readdirSync(join(dir, "inbox"));
assert.equal(files.length, 1, "exactly one capture written");