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 capture whose arguments did not serialize (#175)
Brenno Ferraric958ecee98dd
diff --git a/.claude/scripts/lib/mcp-capture.ts b/.claude/scripts/lib/mcp-capture.tsindex 455a481..43bb028 100644--- a/.claude/scripts/lib/mcp-capture.ts+++ b/.claude/scripts/lib/mcp-capture.ts@@ -161,6 +161,34 @@ export function resolveDestination(return { dir: join(vaultRoot, fallback), rel: fallback, project: null, routed: "fallback" };}+/**+ * Tool-call framing that a serialization failure can spill into a field value.+ *+ * Observed for real: a `record_work` call arrived whose `summary` ended with a+ * closing tag followed by the entire `changes` array as literal text. The server+ * accepted it verbatim, so the note carried raw markup, the changes section never+ * rendered at all, and the corruption was only noticed because a human saw the+ * tags rendering in Obsidian days later. Every automated signal said success: the+ * call returned, the file was written, every field was a non-empty string.+ *+ * Deliberately narrow. `</summary>` alone is NOT matched, because+ * `<details><summary>` is legitimate prose and notes routinely fold superseded+ * plans with it. The three forms below never occur in real writing, so a match is+ * a structural failure rather than a judgement call. That is why the caller is+ * refused rather than warned: a field that has swallowed the next field cannot be+ * repaired by guessing where it ended.+ */+const TOOL_MARKUP = /<parameter\s+name=|<\/?(?:antml:)?invoke\b|<\/?(?:antml:)?function_calls\b/i;++/** The fields a malformed call can corrupt, named so the error can say which. */+export function findToolMarkup(input: Record<string, unknown>): string | null {+ for (const [key, value] of Object.entries(input)) {+ const texts = Array.isArray(value) ? value.map((v) => String(v)) : [String(value ?? "")];+ if (texts.some((t) => TOOL_MARKUP.test(t))) return key;+ }+ return null;+}+/** Render the note body. Pure, so the shape can be asserted without writing. */export function renderCapture(input: CaptureInput,diff --git a/.claude/scripts/lib/mcp-server.ts b/.claude/scripts/lib/mcp-server.tsindex 82c64ac..24aa3f1 100644--- a/.claude/scripts/lib/mcp-server.ts+++ b/.claude/scripts/lib/mcp-server.ts@@ -30,7 +30,7 @@ import { expandNote } from "./mcp-graph.ts";import { qmdSearch, type QmdClient } from "./mcp-qmd-client.ts";import { callerProject, callerProjectSource, isVaultItself, PROJECT_MARKER, sumAuditField, sanitize } from "./mcp-caller.ts";import { callerPlatforms, digestsFrom, resolvableNames } from "./mcp-memory-bridge.ts";-import { captureNote } from "./mcp-capture.ts";+import { captureNote, findToolMarkup } from "./mcp-capture.ts";import { semanticMemoryOrder } from "./mcp-memory-bridge.ts";import { TOOLS } from "./mcp-tools.ts";import { recallFrom, readMemories, type MemoryEntry } from "./memory-recall.ts";@@ -306,6 +306,21 @@ export function createHandlers(deps: ServerDeps): Handlers {].join("\n");}+ // Same refusal as `record_work`, and it matters MORE here. A corrupted work+ // note damages one project's record; a corrupted memory is served to every+ // other repo through `recall`, so the damage travels. Checked before+ // wikilink neutralisation, because a field that has swallowed the following+ // field is not text worth cleaning up.+ const corruptedMemory = findToolMarkup(args);+ if (corruptedMemory) {+ return [+ `Refused: the "${corruptedMemory}" field contains tool-call markup, so this call's arguments did not serialize correctly.`,+ "",+ "Nothing was written. Re-send with each field as plain prose, and check that",+ "a long list field was not folded into the preceding string.",+ ].join("\n");+ }+const who = caller();const resolvable = resolvableNames(visibleFiles(ctx.vaultRoot, policy));@@ -391,6 +406,19 @@ export function createHandlers(deps: ServerDeps): Handlers {}function callRecordWork(args: Record<string, unknown>): 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 rendered+ // markup, which is exactly how one shipped unnoticed.+ const corrupted = findToolMarkup(args);+ if (corrupted) {+ return [+ `Refused: the "${corrupted}" field contains tool-call markup, so this call's arguments did not serialize correctly.`,+ "",+ "Nothing was written. Re-send with each field as plain prose, and check that",+ "a long list field was not folded into the preceding string.",+ ].join("\n");+ }const who = callerProject(session.roots);const resolvable = resolvableNames(visibleFiles(ctx.vaultRoot, policy));let r;diff --git a/.claude/scripts/tests/capture-markup-guard.test.ts b/.claude/scripts/tests/capture-markup-guard.test.tsnew file mode 100644index 0000000..4fcb48a--- /dev/null+++ b/.claude/scripts/tests/capture-markup-guard.test.ts@@ -0,0 +1,78 @@+/**+ * The guard against a malformed `record_work` call corrupting a note.+ *+ * Observed for real: a call arrived whose `summary` ended with a closing tag and+ * then carried the whole `changes` array as literal text. It was written+ * verbatim, so the changes section never rendered and nobody noticed until the+ * raw tags showed up in Obsidian days later. Silent vault corruption is the+ * failure being closed here, and every automated signal reported success while+ * it happened.+ */++import { describe, test } from "node:test";+import assert from "node:assert/strict";++import { findToolMarkup } from "../lib/mcp-capture.ts";++describe("detecting a serialization failure in a capture field", () => {+ test("the exact shape that corrupted a note is caught, and names its field", () => {+ const summary = [+ "Landed the parser rewrite behind a feature flag.</summary>",+ '<parameter name="changes">["src/parser.ts - rewritten."]',+ ].join("\n");+ assert.equal(findToolMarkup({ title: "parser", summary }), "summary");+ });++ test("markup inside a LIST field is caught too, since lists are what get folded", () => {+ assert.equal(findToolMarkup({ changes: ["fine", '<parameter name="decisions">[]'] }), "changes");+ });++ test("invoke and function_calls framing is caught, namespaced or not", () => {+ for (const bad of ['<invoke name="x">', "</invoke>", "<function_calls>", "</function_calls>"]) {+ assert.equal(findToolMarkup({ summary: `text ${bad} more` }), "summary", bad);+ }+ });++ // The false positive that would make this guard unusable: notes legitimately+ // use <details><summary> to fold superseded plans, and refusing those would+ // train everyone to work around the guard instead of fixing the call.+ test("a legitimate details/summary block is NOT flagged", () => {+ const summary = "Before:\n<details><summary>Original plan</summary>\n\nold text\n</details>";+ assert.equal(findToolMarkup({ summary }), null);+ });++ test("ordinary prose with angle brackets and comparisons is not flagged", () => {+ const cases = [+ "first paint < 100ms on a large document",+ "use `impl Iterator<Item = Entry>` rather than a Vec",+ "see <https://example.com/spec> for the contract",+ "the parameter name is `path`, not `file`",+ ];+ for (const s of cases) assert.equal(findToolMarkup({ summary: s }), null, s);+ });++ test("empty, absent and non-string fields are safe rather than throwing", () => {+ assert.equal(findToolMarkup({}), null);+ assert.equal(findToolMarkup({ summary: undefined, changes: null, kind: 7 }), null);+ });++ test("the first corrupted field is the one reported, so the message is specific", () => {+ const found = findToolMarkup({+ title: "ok",+ summary: '<parameter name="a">',+ changes: ['<parameter name="b">'],+ });+ assert.equal(found, "summary");+ });++ // `remember` shares the write path and the failure mode, and its blast radius+ // is larger: a corrupted work note damages one project's record, while a+ // corrupted memory is served to every other repo through `recall`.+ test("a memory's fields are checked the same way, body included", () => {+ const body = [+ "The retry budget is per-attempt, not per-call.</summary>",+ '<parameter name="verification">["ran the suite"]',+ ].join("\n");+ assert.equal(findToolMarkup({ title: "retries", body }), "body");+ });+});