diff --git a/packages/paperclip-runner/src/drivers/re2-schema-patterns.test.ts b/packages/paperclip-runner/src/drivers/re2-schema-patterns.test.ts index 7d02be3a3e8..0dfbc22e3b1 100644 --- a/packages/paperclip-runner/src/drivers/re2-schema-patterns.test.ts +++ b/packages/paperclip-runner/src/drivers/re2-schema-patterns.test.ts @@ -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"); +}); diff --git a/packages/paperclip-runner/src/drivers/tool-input-error.ts b/packages/paperclip-runner/src/drivers/tool-input-error.ts index 275c43616c2..a8f324c8db4 100644 --- a/packages/paperclip-runner/src/drivers/tool-input-error.ts +++ b/packages/paperclip-runner/src/drivers/tool-input-error.ts @@ -3,6 +3,7 @@ import type { ErrorObject, ValidateFunction } from "ajv/dist/2020.js"; type JsonSchemaNode = { type?: unknown; properties?: Record; + 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) : [];