Skip to content

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

fix: a wikilink terminator cannot survive into a memory filename (#237) (#241)

Brenno Ferrari54e9bbb84257
diff --git a/.claude/scripts/lib/memory-write.ts b/.claude/scripts/lib/memory-write.ts
index 073be88..f89f753 100644
--- a/.claude/scripts/lib/memory-write.ts
+++ b/.claude/scripts/lib/memory-write.ts
@@ -127,7 +127,25 @@ export function slugify(title: unknown): string {
// Square brackets go too. A title carrying `[[Something]]` otherwise
// produces a file literally named `... [[Something]].md`, which reads as a
// wikilink embedded in a filename and confuses every surface showing it.
- .replace(/[[\]]/g, " ")
+ //
+ // `#` goes with them, and is the worse case of the two. It is the heading
+ // separator, so `[[A note #7 landed]]` resolves to the note `A note ` and
+ // the heading `7 landed`: a link that points somewhere else without ever
+ // erroring, to a note that looks perfectly normal in every listing. The
+ // alias written for a truncated title does not rescue it, because the
+ // split happens before any alias lookup.
+ //
+ // **The class is the set of characters that terminate a wikilink target**,
+ // which is `#`, `|`, `[` and `]`. `|` is absent here only because the
+ // Windows-illegal class above already removes it, and that is coverage by
+ // coincidence rather than by rule: if that class were ever narrowed, this
+ // one would silently stop being complete. Stated here so the next reader
+ // checks all four rather than the two that are visible.
+ //
+ // Replaced with a space rather than deleted, so `Flag #7 set` becomes
+ // `Flag 7 set` and not `Flag7 set`. The whitespace collapse below tidies
+ // the double space that leaves.
+ .replace(/[[\]#]/g, " ")
.replace(/\.{2,}/g, " ")
.replace(/\s+/g, " ")
.trim();
diff --git a/.claude/scripts/tests/memory-write.test.ts b/.claude/scripts/tests/memory-write.test.ts
index 0e5f409..27e4013 100644
--- a/.claude/scripts/tests/memory-write.test.ts
+++ b/.claude/scripts/tests/memory-write.test.ts
@@ -378,6 +378,55 @@ describe("slug and path safety", () => {
assert.ok(!s.includes("/"));
});
+ test("a wikilink terminator cannot survive a title, and `#` is the one that used to", () => {
+ // The defect: `#` is not a path hazard, so none of the filesystem classes
+ // removed it, and it reached the filename. The note then looked entirely
+ // normal and could not be addressed by any link, because a wikilink splits
+ // on `#` into a note target and a heading reference.
+ const s = slugify("Config flag #7 changes the default");
+ assert.ok(!s.includes("#"), `slug kept the hash: ${s}`);
+ // A space, not nothing: `Flag #7 set` must not collapse to `Flag7 set`.
+ assert.match(s, /flag 7 changes/i, `word boundary lost: ${s}`);
+ assert.ok(!/\s{2,}/.test(s), `double space left behind: ${s}`);
+ });
+
+ test("widening the class does not start eating ordinary titles", () => {
+ // The risk of a wider removal class is that it reaches titles it was never
+ // aimed at. A title whose words survive must keep all of them.
+ for (const [title, expect] of [
+ ["Never revert a probe with git checkout", "Never revert a probe with git checkout"],
+ ["Issue #142 and issue #7 disagree", "Issue 142 and issue 7 disagree"],
+ ["C# is a language, not a heading", "C is a language, not a heading"],
+ ] as const) {
+ assert.equal(slugify(title), expect);
+ }
+ });
+
+ test("a title of nothing but terminators is rejected rather than padded", () => {
+ // It slugifies to empty, which is the state the existing guard already
+ // catches — the same answer `...` and `***` get. Asserted here because the
+ // wider class creates new ways to reach that state, and the alternative
+ // (inventing a stem) would write a note nobody asked for.
+ for (const bad of ["#", "###", "# # #", "[[#]]"]) {
+ assert.equal(slugify(bad), "", `expected an empty slug for ${JSON.stringify(bad)}`);
+ const r = validateMemory({ ...OK, title: bad }, { now: DAY, origin: "p" });
+ assert.equal(r.ok, false, `should reject title ${JSON.stringify(bad)}`);
+ }
+ });
+
+ test("the link built from a written path contains no wikilink terminator", () => {
+ // The round trip, and the assertion that generalises: whatever the class
+ // grows to hold, the target built from the stem must stay addressable.
+ // This is the one that would have failed before the fix.
+ const stem = memoryPath(new Date("2026-01-05T00:00:00Z"), "Config flag #7 changes the default")
+ .replace(/\.md$/, "")
+ .split(sep)
+ .pop() as string;
+ for (const terminator of ["#", "[", "]", "|"]) {
+ assert.ok(!stem.includes(terminator), `[[${stem}]] is not addressable: contains ${terminator}`);
+ }
+ });
+
test("windows reserved stems are escaped", () => {
for (const name of ["CON", "con", "PRN", "AUX", "NUL", "COM1", "LPT9"]) {
assert.ok(slugify(name).startsWith("_"), `${name} must not be a bare reserved name`);