Skip to content

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

fix: a comma in a superseding title severs the correction link (#217)

Brenno Ferrari542914959f58
diff --git a/.claude/scripts/lib/memory-recall.ts b/.claude/scripts/lib/memory-recall.ts
index 1e86c8d..11ad9fd 100644
--- a/.claude/scripts/lib/memory-recall.ts
+++ b/.claude/scripts/lib/memory-recall.ts
@@ -116,7 +116,7 @@ export function parseFrontmatter(md: unknown): Record<string, string | string[]>
if (raw === "") continue;
if (raw.startsWith("[") && raw.endsWith("]")) {
const inner = raw.slice(1, -1).trim();
- out[key] = inner ? inner.split(",").map((p) => unquote(p.trim())).filter(Boolean) : [];
+ out[key] = inner ? splitInlineList(inner).map((p) => unquote(p.trim())).filter(Boolean) : [];
} else {
out[key] = unquote(raw);
}
@@ -124,6 +124,53 @@ export function parseFrontmatter(md: unknown): Record<string, string | string[]>
return out;
}
+/**
+ * Split a YAML inline list on its SEPARATORS.
+ *
+ * A blind `split(",")` reads a comma inside a quoted scalar as a separator,
+ * and that severs precisely the entries this layer cannot afford to lose.
+ * `superseded_by` is the one list key whose entries are free prose: the
+ * `remember` contract asks for a lesson "stated as a claim", and claims take
+ * commas at an ordinary rate. Splitting one produces fragments carrying stray
+ * quote characters, so neither half can ever match a real title, and no author
+ * could hand-write a title that survives.
+ *
+ * The other list keys escaped this because `projects` and `platforms` are
+ * slug-validated at write time and `tags` are slugs by convention. It was never
+ * that commas are rare, only that everywhere else they are rejected before
+ * being written.
+ *
+ * Tracking quote state is sufficient here because this parser reads what this
+ * repo writes: `addToFrontmatterList` emits `JSON.stringify(value)`, so a
+ * backslash escape consumed whole inside a double-quoted scalar means neither
+ * `\"` nor `\\` can hide a separator or invent one. The single-quoted branch is
+ * carried for hand-written frontmatter, which `unquote` already accepts.
+ */
+function splitInlineList(inner: string): string[] {
+ const out: string[] = [];
+ let cur = "";
+ let quote: '"' | "'" | null = null;
+ for (let i = 0; i < inner.length; i++) {
+ const ch = inner[i]!;
+ if (quote === null) {
+ if (ch === '"' || ch === "'") quote = ch;
+ else if (ch === ",") {
+ out.push(cur);
+ cur = "";
+ continue;
+ }
+ } else if (ch === "\\" && quote === '"' && i + 1 < inner.length) {
+ cur += ch + inner[++i]!;
+ continue;
+ } else if (ch === quote) {
+ quote = null;
+ }
+ cur += ch;
+ }
+ out.push(cur);
+ return out;
+}
+
function unquote(s: string): string {
const t = String(s).trim();
if (t.length >= 2 && t.startsWith('"') && t.endsWith('"')) {
diff --git a/.claude/scripts/tests/memory-recall.test.ts b/.claude/scripts/tests/memory-recall.test.ts
index 14058cc..8fba228 100644
--- a/.claude/scripts/tests/memory-recall.test.ts
+++ b/.claude/scripts/tests/memory-recall.test.ts
@@ -321,6 +321,30 @@ describe("reader robustness", () => {
assert.equal(fm.description, "one, two");
});
+ // The case the test above reads as though it covers and does not: its comma
+ // sits in a SCALAR, and its list has no comma inside an entry. `superseded_by`
+ // is the one list whose entries are free prose, so it is the one that meets
+ // this shape in the field.
+ test("a comma inside a quoted list entry is content, not a separator", () => {
+ const fm = parseFrontmatter(
+ '---\nsuperseded_by: ["The retry budget is per-attempt, not per-call"]\n---\n',
+ );
+ assert.deepEqual(fm.superseded_by, ["The retry budget is per-attempt, not per-call"]);
+ });
+
+ test("a separator between two quoted entries still separates them", () => {
+ const fm = parseFrontmatter('---\nsuperseded_by: ["one, with comma", "two, also"]\n---\n');
+ assert.deepEqual(fm.superseded_by, ["one, with comma", "two, also"]);
+ });
+
+ // The neighbouring escaping rule, easy to break while fixing the split: the
+ // writer emits JSON, so a quote inside a title arrives backslash-escaped and
+ // must not be read as the end of the scalar.
+ test("an escaped double quote inside an entry does not end it", () => {
+ const fm = parseFrontmatter('---\nsuperseded_by: ["the \\"good\\" parts, revisited"]\n---\n');
+ assert.deepEqual(fm.superseded_by, ['the "good" parts, revisited']);
+ });
+
test("recall on a vault with no memories returns empty, not an error", () => {
const dir = mkdtempSync(join(tmpdir(), "empty-"));
try {
diff --git a/.claude/scripts/tests/memory-supersede.test.ts b/.claude/scripts/tests/memory-supersede.test.ts
index 92e2d3b..065338f 100644
--- a/.claude/scripts/tests/memory-supersede.test.ts
+++ b/.claude/scripts/tests/memory-supersede.test.ts
@@ -73,6 +73,26 @@ describe("markSuperseded", () => {
}
});
+ // The writer and the reader have to agree, and a title is free prose: the
+ // `remember` contract asks for a lesson "stated as a claim", and claims take
+ // commas. Asserted end to end rather than on the parser alone, because the
+ // failure was only ever visible where the two halves meet.
+ test("a comma in the superseding title survives the round trip and still resolves", () => {
+ const title = "The retry budget is per-attempt, not per-call";
+ const { dir, rel } = vaultWith(MD);
+ try {
+ assert.equal(markSuperseded(dir, rel, title).ok, true);
+ const facets = facetsOf(parseFrontmatter(readFileSync(join(dir, rel), "utf8")));
+ assert.deepEqual(facets.superseded_by, [title], "the correction link was torn in half");
+ const r = resolveSupersedes(facets.superseded_by, [{ rel: "new.md", title }]);
+ assert.deepEqual(r.unmatched, []);
+ assert.equal(r.matched.length, 1);
+ assert.equal(r.matched[0]!.rel, "new.md");
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
test("REFUSES to modify a note it did not write, byte-for-byte", () => {
// The safety rule that matters most: a human's note is never edited,
// whatever it happens to be linked to.