A self-organizing Obsidian vault that gives AI coding agents persistent memory.
TypeScript ai-agentsclaude-codecodex-cligemini-cliknowledge-managementobsidianobsidian-vaultpersistent-memorysecond-braintemplate
fix: the markup refusal names the fields the server received (#233)
Brenno Ferrari1452f9c97a7a
diff --git a/.claude/scripts/lib/mcp-capture.ts b/.claude/scripts/lib/mcp-capture.tsindex c8cf73d..2a7e520 100644--- a/.claude/scripts/lib/mcp-capture.ts+++ b/.claude/scripts/lib/mcp-capture.ts@@ -305,14 +305,37 @@ export function describeToolMarkup(input: Record<string, unknown>): ToolMarkupSi* again. The evidence is quoted so the caller can go to the exact offset rather* than hunt for what fired.*/-export function toolMarkupRefusal(site: ToolMarkupSite): string {+export function toolMarkupRefusal(site: ToolMarkupSite, args?: Record<string, unknown>): string {const where = site.item === null ? `"${site.field}"` : `item ${site.item} of "${site.field}"`;+ // A caller knows what it SENT; only the server knows what ARRIVED. When a+ // field has swallowed the ones after it, the gap between those two lists is+ // the entire finding, and nothing else in this message can show it. Without+ // it a caller can comply with the refusal completely, re-send, and fail+ // identically: a loop in which no attempt carries new information.+ //+ // The key set only. Echoing the corrupt value back makes the message longer+ // rather than more diagnosable, and what is needed is which fields exist.+ const received = args ? Object.keys(args) : [];+ const fieldLen = args ? String(args[site.field] ?? "").length : null;+const diagnosis = site.trailing? ["It sits at the END of the field with none of your content after it, so the",- "CALL is malformed rather than the prose. Extra closing tags after your last",- "parameter were folded into the preceding string. Re-send the same text with",- "exactly one closing tag per parameter — do not rewrite the prose, it is fine.",+ "CALL is likely malformed rather than the prose.",+ "",+ // Trailing does NOT prove nothing was lost. A field can absorb the+ // VALUES of the fields after it while their opening tags are consumed+ // by the parser, leaving only a closing tag at the very end, which+ // looks exactly like harmless trailing noise. The received list is the+ // only thing that tells those two apart, so it is checked FIRST.+ "CHECK THE FIELD LIST ABOVE FIRST. If a field you sent is missing from it,",+ "its text was folded into this one and the data is gone rather than merely",+ "mispunctuated; the offset then tracks your whole payload rather than a",+ "position inside your own prose. Re-send every field in that case.",+ "",+ "If every field you sent is listed, then only the closing tags are wrong:",+ "re-send the same text with exactly one closing tag per parameter, and",+ "do not rewrite the prose.",]: ["Your content CONTINUES past it, so this field swallowed the field after it.",@@ -320,15 +343,17 @@ export function toolMarkupRefusal(site: ToolMarkupSite): string {"call with every field as a separate argument in plain prose, and check that a","long list field was not folded into the preceding string.",];- return [++ const lines = [`Refused: "${site.field}" contains tool-call markup, so this call's arguments did not serialize correctly.`,"",- `Found "${site.match}" at offset ${site.offset} of ${where}.`,- "",- ...diagnosis,+ `Found "${site.match}" at offset ${site.offset} of ${where}` ++ (fieldLen === null ? "." : `, which is ${fieldLen} characters long.`),"",- "Nothing was written.",- ].join("\n");+ ];+ if (received.length) lines.push(`Fields received: ${received.join(", ")}.`, "");+ lines.push(...diagnosis, "", "Nothing was written.");+ return lines.join("\n");}/** The fields a malformed call can corrupt, named so the error can say which. */diff --git a/.claude/scripts/lib/mcp-server.ts b/.claude/scripts/lib/mcp-server.tsindex 48ad0f9..88101d4 100644--- a/.claude/scripts/lib/mcp-server.ts+++ b/.claude/scripts/lib/mcp-server.ts@@ -449,7 +449,7 @@ export function createHandlers(deps: ServerDeps): Handlers {offset: corruptedMemory.offset,trailing: corruptedMemory.trailing,});- return toolMarkupRefusal(corruptedMemory);+ return toolMarkupRefusal(corruptedMemory, args);}const who = caller();@@ -553,7 +553,7 @@ export function createHandlers(deps: ServerDeps): Handlers {offset: corrupted.offset,trailing: corrupted.trailing,});- return toolMarkupRefusal(corrupted);+ return toolMarkupRefusal(corrupted, args);}const who = callerProject(session.roots);const resolvable = resolvableNames(visibleFiles(ctx.vaultRoot, policy));diff --git a/.claude/scripts/tests/capture-markup-guard.test.ts b/.claude/scripts/tests/capture-markup-guard.test.tsindex a48178f..d2d41c1 100644--- a/.claude/scripts/tests/capture-markup-guard.test.ts+++ b/.claude/scripts/tests/capture-markup-guard.test.ts@@ -125,3 +125,54 @@ describe("describing a serialization failure well enough to repair it", () => {assert.equal(summary.slice(site.offset, site.offset + site.match.length), site.match);});});++/**+ * The refusal has to say what ARRIVED, not only what is wrong.+ *+ * A caller knows what it sent; only the server knows what arrived. When a field+ * has swallowed the ones after it, the gap between those two lists is the whole+ * finding. Without it a caller can comply with the refusal completely, re-send,+ * and fail identically, in a loop where no attempt carries new information.+ */+describe("the refusal names the fields the server received", () => {+ const PROSE = "The gate was measured at the cheapest position, so it passed against the defect.";++ test("a swallowed field is visibly absent from the received list", () => {+ // `changes` was folded into `summary`, so it never arrived as its own key.+ const args = { summary: `${PROSE}</summary>\n<parameter name="changes">["src/a.ts"]` };+ const site = describeToolMarkup(args);+ const msg = toolMarkupRefusal(site!, args);+ assert.match(msg, /Fields received: summary\./);+ assert.ok(!/Fields received:.*changes/.test(msg), "the swallowed field must not appear as received");+ });++ test("the corrupt field's length is reported, but never its value", () => {+ const args = { summary: `${PROSE}</invoke></function_calls>` };+ const site = describeToolMarkup(args);+ const msg = toolMarkupRefusal(site!, args);+ assert.match(msg, /which is \d+ characters long/);+ // The evidence is the key set and a length. Echoing the prose back would+ // make the message longer without making it more diagnosable.+ assert.ok(!msg.includes(PROSE), "the refusal must not echo the caller's prose");+ });++ test("the trailing branch tells the caller to check the list before trusting it", () => {+ const args = { summary: `${PROSE}</invoke></function_calls>`, folder: "brain" };+ const site = describeToolMarkup(args);+ const msg = toolMarkupRefusal(site!, args);+ assert.equal(site?.trailing, true);+ assert.match(msg, /Fields received: summary, folder\./);+ // Trailing does not prove nothing was lost, so the message must not say+ // the prose is fine before the caller has checked which fields arrived.+ assert.match(msg, /CHECK THE FIELD LIST ABOVE FIRST/);+ });++ // The optional contract: existing callers pass one argument and still get a+ // usable message, it simply omits the list.+ test("called without args the message is still produced, minus the list", () => {+ const site = describeToolMarkup({ summary: `${PROSE}</invoke>` });+ const msg = toolMarkupRefusal(site!);+ assert.ok(!msg.includes("Fields received"));+ assert.match(msg, /^Refused: "summary" contains tool-call markup/);+ });+});