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 47a6323809a..7d02be3a3e8 100644 --- a/packages/paperclip-runner/src/drivers/re2-schema-patterns.test.ts +++ b/packages/paperclip-runner/src/drivers/re2-schema-patterns.test.ts @@ -1,6 +1,6 @@ import { afterEach, expect, it } from "vitest"; import { taskDocumentKeySchema } from "../protocol-actions/task-document-key-schema.js"; -import { re2PortableSchema } from "./re2-schema-patterns.js"; +import { re2Incompatibility, re2PortableSchema } from "./re2-schema-patterns.js"; import { startRunnerToolBridge, type RunnerToolBridge } from "./runner-tool-bridge.js"; const bridges: RunnerToolBridge[] = []; @@ -22,6 +22,7 @@ it("rewrites ECMAScript \\u escapes in patterns to the RE2 form the Antigravity emoji: { type: "string", pattern: "^\\ud83d\\ude00$" }, lone: { type: "string", pattern: "^\\ud83d$" }, braced: { type: "string", pattern: "^\\u{1F600}$" }, + lookahead: { type: "string", pattern: "^(?!\\.)[a-z]+$" }, }, patternProperties: { ["^x\\u0041$"]: { type: "string" } } }; const portable = re2PortableSchema(schema) as { properties: Record; patternProperties: Record }; // The live failure: the CLI refused the whole toolbox for this one pattern. @@ -34,6 +35,7 @@ it("rewrites ECMAScript \\u escapes in patterns to the RE2 form the Antigravity expect(portable.properties.braced.pattern).toBe(String.raw`^\x{1f600}$`); // A lone surrogate has no RE2 form: the pattern is left out (the bridge still validates it). expect(portable.properties.lone).toEqual({ type: "string" }); + expect(portable.properties.lookahead).toEqual({ type: "string" }); expect(Object.keys(portable.patternProperties)).toEqual([String.raw`^x\x{0041}$`]); // The source schema is unchanged. expect(schema.properties.key.pattern).toBe(taskDocumentKeySchema.pattern); @@ -55,3 +57,41 @@ it("serves RE2 patterns to the CLI while validating calls with the original sche expect((await rpc(bridge, { id: 3, method: "tools/call", params: { name: "read_document", arguments: { key: "x{212a}" } } })).result.isError).toBe(true); expect(calls).toEqual([{ key: "plan" }]); }); + +function servedPatterns(value: unknown, path: string, out: Array<{ path: string; pattern: string }>): void { + if (Array.isArray(value)) { value.forEach((item, index) => servedPatterns(item, `${path}[${index}]`, out)); return; } + if (value === null || typeof value !== "object") return; + for (const [key, child] of Object.entries(value as Record)) { + if (key === "pattern" && typeof child === "string") out.push({ path, pattern: child }); + if (key === "patternProperties" && child && typeof child === "object") { + for (const pattern of Object.keys(child)) out.push({ path: `${path}.patternProperties`, pattern }); + } + servedPatterns(child, `${path}.${key}`, out); + } +} + +it("serves only RE2-compatible patterns for every Paperclip tool on the swarm route", async () => { + const { PAPERCLIP_PROTOCOL_ACTIONS } = await import("../protocol-actions/index.js"); + const tools: Array<{ name: string; inputSchema: Record }> = []; + const collect = (value: unknown, name: string): void => { + if (Array.isArray(value)) { value.forEach(item => collect(item, name)); return; } + if (value === null || typeof value !== "object") return; + for (const [key, child] of Object.entries(value as Record)) { + if (key === "inputSchema" && child && typeof child === "object") tools.push({ name: `${name}_${tools.length}`, inputSchema: child as Record }); + else collect(child, name); + } + }; + for (const action of PAPERCLIP_PROTOCOL_ACTIONS) collect(action, String((action as { name?: unknown }).name ?? "action")); + expect(tools.length).toBeGreaterThan(40); + const bridge = await startRunnerToolBridge({ preserveToolNames: true, schemaPatternDialect: "re2", tools, handler: async () => ({ ok: true }) }); + bridges.push(bridge); + const listed = (await rpc(bridge, { id: 1, method: "tools/list" })).result.tools!; + const patterns: Array<{ path: string; pattern: string }> = []; + for (const tool of listed) servedPatterns(tool.inputSchema, tool.name, patterns); + // The live failure came from one such pattern, so the guard must see some. + expect(patterns.some(entry => entry.pattern.includes(String.raw`\x{212a}`))).toBe(true); + expect(patterns.flatMap(entry => { + const problem = re2Incompatibility(entry.pattern); + return problem ? [`${entry.path}: ${problem}: ${entry.pattern}`] : []; + })).toEqual([]); +}); diff --git a/packages/paperclip-runner/src/drivers/re2-schema-patterns.ts b/packages/paperclip-runner/src/drivers/re2-schema-patterns.ts index 4b71e6ba885..23e2c2a971a 100644 --- a/packages/paperclip-runner/src/drivers/re2-schema-patterns.ts +++ b/packages/paperclip-runner/src/drivers/re2-schema-patterns.ts @@ -10,8 +10,35 @@ type Json = unknown; const HEX4 = /^[0-9a-fA-F]{4}$/; -/** The RE2 form of an ECMAScript pattern, or null when it has none (a lone surrogate). */ +/** + * The first construct in `pattern` that RE2 refuses (lookaround, backreference, + * a \u or \c escape), or null. Conservative: it does not prove a pattern valid. + */ +export function re2Incompatibility(pattern: string): string | null { + for (let index = 0; index < pattern.length; index++) { + if (pattern[index] !== "\\") continue; + const next = pattern[index + 1] ?? ""; + if (next === "u") return "\\u escape"; + if (/[1-9]/.test(next)) return "backreference"; + if (next === "k") return "named backreference"; + if (next === "c") return "control escape"; + index++; + } + if (/\(\?