A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: refuse a record_work call with no title instead of writing an undefined note (#296)
Brenno Ferrari6396616f92c0
diff --git a/.claude/scripts/lib/mcp-capture.ts b/.claude/scripts/lib/mcp-capture.tsindex acc3737..276a11c 100644--- a/.claude/scripts/lib/mcp-capture.ts+++ b/.claude/scripts/lib/mcp-capture.ts@@ -61,7 +61,10 @@ export interface CaptureResult {/** Filesystem-safe stem from a title. Separators are stripped, not escaped. */export function slugifyTitle(title: unknown): string {- return String(title)+ // Not `String(title)`: `String(undefined)` is "undefined", a non-empty+ // stem that slipped past captureNote's empty-filename guard.+ if (typeof title !== "string") return "";+ return title.normalize("NFKD").replace(/[^\w\s-]/g, "").trim()diff --git a/.claude/scripts/lib/mcp-server.ts b/.claude/scripts/lib/mcp-server.tsindex d5bfaa7..4c95656 100644--- a/.claude/scripts/lib/mcp-server.ts+++ b/.claude/scripts/lib/mcp-server.ts@@ -554,6 +554,16 @@ export function createHandlers(deps: ServerDeps): Handlers {}function callRecordWork(args: Record<string, unknown>): string {+ // `required` in the tool schema is advisory: a client that does not+ // validate against it sends the call without the field, and a missing+ // title used to become a `<date>-undefined.md` note with an empty H1.+ for (const field of ["title", "summary"] as const) {+ const v = args[field];+ if (typeof v !== "string" || v.trim() === "") {+ audit("refused", { tool: "record_work", reason: `missing ${field}` });+ return `Not recorded: \`${field}\` is required and must be a non-empty string.`;+ }+ }// A field carrying tool-call framing means the call's serialization broke,// not that the author wrote something odd. Refuse: writing it produces a// corrupted note whose damage is invisible until a human reads the rendereddiff --git a/.claude/scripts/tests/mcp-capture.test.ts b/.claude/scripts/tests/mcp-capture.test.tsindex c0025ad..7c20e4d 100644--- a/.claude/scripts/tests/mcp-capture.test.ts+++ b/.claude/scripts/tests/mcp-capture.test.ts@@ -77,6 +77,22 @@ describe("slugifying a title", () => {test("is bounded, so a pathological title cannot make an unusable filename", () => {assert.ok(slugifyTitle("x".repeat(500)).length <= 60);});++ test("a non-string title is an empty stem, never the word 'undefined'", () => {+ assert.equal(slugifyTitle(undefined), "");+ assert.equal(slugifyTitle(null), "");+ assert.equal(slugifyTitle(42), "");+ });++ test("captureNote with no title throws the empty-filename refusal and writes nothing", () => {+ withVault((dir) => {+ const { title: _omit, ...noTitle } = BASIC;+ assert.throws(+ () => captureNote(dir, POLICY, {}, null, noTitle, new Set(), { now: NOW }),+ /empty filename/,+ );+ });+ });});// ---------------------------------------------------------------------------diff --git a/.claude/scripts/tests/record-work-required.test.ts b/.claude/scripts/tests/record-work-required.test.tsnew file mode 100644index 0000000..0bf5742--- /dev/null+++ b/.claude/scripts/tests/record-work-required.test.ts@@ -0,0 +1,99 @@+/**+ * `record_work` must refuse a call without `title` or `summary`.+ *+ * Both are `required` in the tool schema, but that is advisory: a client that+ * does not validate against it sends the call anyway. A missing title used to+ * become a `<date>-undefined.md` note with an empty H1 and no alias, returned+ * as "Recorded:". Driven through the real `tools/call` dispatch.+ */++import { test, describe } from "node:test";+import assert from "node:assert/strict";+import { mkdirSync, mkdtempSync, readdirSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";++import { createHandlers } from "../lib/mcp-server.ts";++import { rmTemp } from "./_helpers.ts";++interface Logged {+ readonly action: string;+ readonly detail: Record<string, unknown>;+}++function harness(vaultRoot: string) {+ const logged: Logged[] = [];+ const session = {+ roots: [{ uri: "file:///elsewhere/some-project", name: "some-project" }],+ identityReady: async () => {},+ ok: (_id: unknown, r: unknown) => r,+ error: (_id: unknown, e: unknown) => e,+ };+ const handlers = createHandlers({+ ctx: { vaultRoot, memoryRoot: "memories", qmdIndex: null },+ policy: { roots: ["brain"], neverExpose: new Set(), source: "manifest", memoryRoot: "memories" },+ session,+ qmd: () => ({}),+ audit: (action: string, detail: Record<string, unknown> = {}) => logged.push({ action, detail }),+ now: () => new Date(2026, 6, 26),+ reindex: () => true,+ } as never);+ const call = async (args: Record<string, unknown>): Promise<string> => {+ const r = (await (handlers as never as Record<string, (id: number, p: unknown) => Promise<unknown>>)[+ "tools/call"+ ]!(1, { name: "record_work", arguments: args })) as { content?: { text?: string }[] };+ return r?.content?.[0]?.text ?? JSON.stringify(r);+ };+ return { call, logged };+}++/** Every .md file under the vault, relative — what a call left behind. */+function written(dir: string): string[] {+ return readdirSync(dir, { recursive: true, encoding: "utf-8" }).filter((f) => f.endsWith(".md"));+}++const GOOD = { title: "Add the archive command", summary: "What changed and why.", folder: "brain" };++describe("record_work refuses a call missing a required field", () => {+ const cases: [string, Record<string, unknown>, string][] = [+ ["no title", { summary: GOOD.summary, folder: "brain" }, "title"],+ ["empty title", { ...GOOD, title: "" }, "title"],+ ["whitespace title", { ...GOOD, title: " " }, "title"],+ ["non-string title", { ...GOOD, title: 42 }, "title"],+ ["no summary", { title: GOOD.title, folder: "brain" }, "summary"],+ ];++ for (const [label, args, field] of cases) {+ test(`${label} → Not recorded, nothing written, refusal audited`, async () => {+ const dir = mkdtempSync(join(tmpdir(), "rw-required-"));+ try {+ mkdirSync(join(dir, "brain"));+ const { call, logged } = harness(dir);+ const text = await call(args);+ assert.match(text, new RegExp(`^Not recorded: \`${field}\` is required`));+ assert.deepEqual(written(dir), []);+ const refusals = logged.filter((l) => l.action === "refused");+ assert.equal(refusals.length, 1);+ assert.equal(refusals[0]!.detail["reason"], `missing ${field}`);+ } finally {+ rmTemp(dir);+ }+ });+ }++ test("a complete call still records", async () => {+ const dir = mkdtempSync(join(tmpdir(), "rw-required-"));+ try {+ mkdirSync(join(dir, "brain"));+ const { call } = harness(dir);+ const text = await call(GOOD);+ assert.match(text, /^Recorded/, text);+ const files = written(dir);+ assert.equal(files.length, 1, files.join(","));+ assert.ok(!files[0]!.includes("undefined"));+ } finally {+ rmTemp(dir);+ }+ });+});