Skip to content

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

fix: a capture could not be linked by the name it calls itself

Brenno Ferrari9695deee57c8
diff --git a/.claude/scripts/lib/mcp-capture.ts b/.claude/scripts/lib/mcp-capture.ts
index 43bb028..6bff00c 100644
--- a/.claude/scripts/lib/mcp-capture.ts
+++ b/.claude/scripts/lib/mcp-capture.ts
@@ -71,6 +71,37 @@ export function slugifyTitle(title: unknown): string {
.toLowerCase();
}
+/**
+ * A description that stops at a word, not mid-syllable.
+ *
+ * The naive `slice(0, MAX)` cuts wherever the budget runs out, and the result is
+ * indistinguishable from a complete sentence that happened to end there. That
+ * field is the single most load-bearing line a note has in a vault whose
+ * `MEMORY.md` is generated from it: it is what a future session reads before
+ * deciding whether to open the note at all. A reader who cannot tell an
+ * amputated description from a whole one has to open every note to find out,
+ * which is the cost the index exists to remove.
+ *
+ * So: break on whitespace and mark it. The ellipsis is the whole point — it
+ * makes truncation *visible*, which is what the hard cut never was.
+ *
+ * The 60% floor covers the degenerate case of a budget containing no whitespace
+ * at all (one long token, or a script that does not space between words), where
+ * breaking at the last space would throw away most of the allowance. There, a
+ * hard cut with an ellipsis is still better than a hard cut without one.
+ */
+export function clampDescription(text: unknown, max: number = DESCRIPTION_MAX): string {
+ const flat = String(text ?? "")
+ .replace(/"/g, "'")
+ .replace(/\s+/g, " ")
+ .trim();
+ if (flat.length <= max) return flat;
+ const cut = flat.slice(0, max - 1);
+ const lastSpace = cut.lastIndexOf(" ");
+ const stem = lastSpace > max * 0.6 ? cut.slice(0, lastSpace) : cut;
+ return `${stem.replace(/[\s,;:.–—-]+$/, "")}…`;
+}
+
/**
* The project folder matching a calling repo, if this vault has one.
*
@@ -228,18 +259,44 @@ export function renderCapture(
const related = [home, informed].filter(Boolean).join("\n");
const summary = String(input.summary ?? "").trim();
+ const title = String(input.title ?? "").trim();
+
+ // The title, carried as an alias, is what makes this note reachable by the
+ // name it calls itself.
+ //
+ // The filename is a slug of the title: lowercased, punctuation stripped,
+ // whitespace hyphenated, cut at SLUG_MAX, and prefixed with the date. So the
+ // basename is NEVER the title — not only when it was truncated. `[[I3 lands:
+ // a soak]]` cannot resolve to `2026-07-31-i3-lands-a-soak.md`, because
+ // `resolvableNames` matches on basenames and aliases and this note had
+ // neither spelling of its own name.
+ //
+ // The consequence was that every cross-reference this renderer emits between
+ // its own notes fell to `_(no note yet)_`, including references to notes
+ // written hours earlier, so a run of captures arrived mutually unlinked —
+ // orphans, in a vault whose stated rule is that a note without links is a
+ // bug. The links were always *intended*; the resolver simply could not see
+ // the target.
+ //
+ // Fixing it here rather than by keeping full-length filenames is deliberate:
+ // filenames are already published and linked, a longer one still would not
+ // round-trip through slugification, and `resolvableNames` has read aliases
+ // since it was written — for exactly this class of problem, where a note's
+ // real name and its file's name differ.
+ const aliases = title ? ["aliases:", ` - "${title.replace(/"/g, "'")}"`] : [];
return [
"---",
`date: ${day}`,
- `description: "${summary.replace(/"/g, "'").replace(/\s+/g, " ").slice(0, DESCRIPTION_MAX)}"`,
+ `description: "${clampDescription(summary)}"`,
+ ...aliases,
"tags:",
input.kind === "decision" ? " - decision" : " - project-note",
...(dest.project ? [`project: ${dest.project}`] : []),
`source_repo: ${caller}`,
"---",
"",
- `# ${String(input.title ?? "").trim()}`,
+ `# ${title}`,
"",
summary,
section("What changed", list(input.changes)),
diff --git a/.claude/scripts/tests/mcp-capture.test.ts b/.claude/scripts/tests/mcp-capture.test.ts
index 568020e..7c6cf0d 100644
--- a/.claude/scripts/tests/mcp-capture.test.ts
+++ b/.claude/scripts/tests/mcp-capture.test.ts
@@ -24,9 +24,11 @@ import {
resolveDestination,
renderCapture,
captureNote,
+ clampDescription,
type Destination,
} from "../lib/mcp-capture.ts";
import type { ExposurePolicy } from "../lib/mcp-exposure.ts";
+import { resolvableNames } from "../lib/mcp-memory-bridge.ts";
const NOW = new Date("2026-07-26T10:00:00Z");
const POLICY: ExposurePolicy = { roots: ["brain", "projects"], neverExpose: new Set(), source: "manifest", memoryRoot: "memories" };
@@ -310,3 +312,108 @@ describe("writing a capture", () => {
});
});
});
+
+// ---------------------------------------------------------------------------
+// A capture reachable by the name it calls itself (#180)
+// ---------------------------------------------------------------------------
+
+describe("a capture is reachable by its own title", () => {
+ const dest: Destination = { dir: "/v/projects/atlas/notes", rel: "projects/atlas/notes", project: "atlas", routed: "caller-identity" };
+
+ 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);
+ 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);
+ assert.match(md, /^ {2}- "The 'obvious' fix"$/m);
+ assert.doesNotMatch(md, /- "The "obvious/);
+ });
+
+ 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);
+ assert.doesNotMatch(md, /^aliases:/m);
+ });
+
+ /**
+ * The regression that motivated #180, asserted end to end rather than on the
+ * alias line alone. Writing the alias is the mechanism; being resolvable is
+ * the property, and only the round trip proves the two agree.
+ *
+ * Before the fix this failed on every capture, not only long-titled ones:
+ * the basename carries a date prefix, is lowercased and has its punctuation
+ * stripped, so it is never the title even when nothing was truncated.
+ */
+ 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 });
+
+ const files = readdirSync(join(dir, "inbox"));
+ assert.equal(files.length, 1, "exactly one capture written");
+
+ const label = files[0]!.replace(/\.md$/, "");
+ // The precondition is that the basename is not the title — NOT that it
+ // is shorter. It carries an 11-character date prefix, so a slug cut at
+ // 60 can be longer than the title it came from and still not be it.
+ assert.notEqual(label.toLowerCase(), title.toLowerCase(), "precondition: the basename is a slug, not the title");
+
+ const resolvable = resolvableNames([{ label, full: join(dir, "inbox", files[0]!), scope: "inbox" }]);
+ assert.ok(resolvable.has(title.toLowerCase()), "the note must resolve by the name it calls itself");
+ });
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Descriptions stop at a word (#180)
+// ---------------------------------------------------------------------------
+
+describe("clamping a description", () => {
+ test("a short description is returned whole and unmarked", () => {
+ assert.equal(clampDescription("Short and complete.", 150), "Short and complete.");
+ });
+
+ /**
+ * The fixture is irregular on purpose, and the first version of this test was
+ * wrong in a way worth keeping the note about: it used `"alpha ".repeat(60)`,
+ * where the token length divides the budget so the *hard cut* lands on a word
+ * boundary by arithmetic accident. Disabling the word-break entirely still
+ * passed. A fixture has to distinguish the implementation from its mutation,
+ * and one built from a single repeated token cannot.
+ *
+ * So: varied word lengths, and the assertion is that every character returned
+ * came from a whole word of the input.
+ */
+ const PROSE = "the settle margin stays at two seconds because granularity is not uniform within one volume";
+
+ for (const max of [30, 41, 57, 60, 73]) {
+ test(`a long one breaks on whitespace and says it was cut (budget ${max})`, () => {
+ const out = clampDescription(PROSE, max);
+ assert.ok(out.endsWith("…"), "truncation must be visible");
+ assert.ok(out.length <= max, `stayed within budget, got ${out.length}`);
+
+ const kept = out.slice(0, -1);
+ const words = PROSE.split(" ");
+ // Every token kept must be a complete word of the source, and the next
+ // source word must not fit — otherwise it broke early rather than late.
+ const keptWords = kept.split(" ");
+ assert.deepEqual(keptWords, words.slice(0, keptWords.length), `broke mid-word: ${JSON.stringify(out)}`);
+ });
+ }
+
+ test("a budget containing no whitespace still marks the cut", () => {
+ const out = clampDescription("x".repeat(400), 50);
+ assert.equal(out.length, 50);
+ assert.ok(out.endsWith("…"));
+ });
+
+ test("trailing punctuation is not left dangling before the ellipsis", () => {
+ assert.doesNotMatch(clampDescription("one two three, four five six seven", 22), /[,;:–—-]…$/);
+ });
+
+ test("whitespace is flattened and quotes made frontmatter-safe", () => {
+ assert.equal(clampDescription('a\n\nb "c"', 150), "a b 'c'");
+ });
+});