fix(runner): a pattern or format rejection names the rule in the field's words

The swarm route leaves out of the CLI's copy any pattern RE2 cannot
compile, so the model may send a value only the Runner rejects. The tool
error now adds the field's own schema description ("key: Stable document
identifier, not a filename or path ...") after the validator's detail, so
the model can correct the call. Every provider gets the same guidance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-24 14:37:01 -06:00
1 parent 82c3144dee
commit 9ec2f634b4
2 files changed
+39

No files matched your search

@@ -95,3 +95,22 @@ it("serves only RE2-compatible patterns for every Paperclip tool on the swarm ro
return problem ? [`${entry.path}: ${problem}: ${entry.pattern}`] : [];
})).toEqual([]);
});
it("tells the model in plain words which rule a value broke when the CLI never saw the pattern", async () => {
const bridge = await startRunnerToolBridge({
preserveToolNames: true, schemaPatternDialect: "re2",
tools: [{ name: "read_document", inputSchema: { type: "object", properties: { key: taskDocumentKeySchema }, required: ["key"], additionalProperties: false } }],
handler: async () => ({ ok: true }),
});
bridges.push(bridge);
const response = await fetch(bridge.url, {
method: "POST", headers: { Authorization: `Bearer ${bridge.secret}`, "Content-Type": "application/json" },
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "read_document", arguments: { key: "notes.md" } } }),
});
const result = (await response.json() as { result: { isError?: boolean; content: Array<{ text: string }> } }).result;
expect(result.isError).toBe(true);
const text = result.content[0]!.text;
expect(text).toMatch(/^Invalid tool input: \/key /);
// The rule the served schema no longer carried, as the schema's own words.
expect(text).toContain("key: Stable document identifier, not a filename or path");
});
@@ -3,6 +3,7 @@ import type { ErrorObject, ValidateFunction } from "ajv/dist/2020.js";
type JsonSchemaNode = {
type?: unknown;
properties?: Record<string, JsonSchemaNode>;
items?: JsonSchemaNode;
required?: unknown;
description?: unknown;
};
@@ -32,6 +33,16 @@ function listFields(keys: string[]) {
return keys.length > MAX_LISTED_FIELDS ? `${shown}, and ${keys.length - MAX_LISTED_FIELDS} more` : shown;
}
function schemaAtInstancePath(root: JsonSchemaNode | undefined, instancePath: string): JsonSchemaNode | undefined {
let node = root;
for (const segment of instancePath.split("/").filter(Boolean)) {
if (!node) return undefined;
const key = segment.replaceAll("~1", "/").replaceAll("~0", "~");
node = /^\d+$/.test(key) && node.items && typeof node.items === "object" ? node.items : node.properties?.[key];
}
return node;
}
/** Return schema-owned recovery guidance without changing validation or dispatch. */
export function toolInputError(validator: ValidateFunction | undefined, input?: unknown): string {
const errors = validator?.errors ?? [];
@@ -52,6 +63,15 @@ export function toolInputError(validator: ValidateFunction | undefined, input?:
if (typeof description === "string" && description.trim()) guidance.add(description.slice(0, 1000));
if (guidance.size === 3) break;
}
// A value that broke a pattern or format: the field's own description says the
// rule in words (a client may not have been shown the pattern itself).
for (const error of errors) {
if (guidance.size === 3) break;
if (error.keyword !== "pattern" && error.keyword !== "format") continue;
const node = schemaAtInstancePath(rootSchema, error.instancePath);
const field = error.instancePath.split("/").filter(Boolean).at(-1);
if (field && typeof node?.description === "string" && node.description.trim()) guidance.add(`${field}: ${node.description.slice(0, 1000)}`);
}
const parts: string[] = [];
if (missing.length) {
const received = typeof input === "object" && input !== null && !Array.isArray(input) ? Object.keys(input) : [];