fix(antigravity): leave out patterns RE2 cannot compile and guard every served tool

A guard test now runs every Paperclip tool schema the swarm route serves
through an RE2 compatibility check. It found a second pattern the
Antigravity CLI would refuse: create_project's repository URL uses a
lookahead. A pattern with no RE2 form (lookaround, backreference, lone
surrogate) is left out of the copy served to the CLI; the Runner still
validates calls against the original schema.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-24 14:34:05 -06:00
1 parent 0f2d40e464
commit 82c3144dee
2 files changed
+69 -2

No files matched your search

@@ -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<string, { pattern?: string }>; patternProperties: Record<string, unknown> };
// 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<string, unknown>)) {
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<string, unknown> }> = [];
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<string, unknown>)) {
if (key === "inputSchema" && child && typeof child === "object") tools.push({ name: `${name}_${tools.length}`, inputSchema: child as Record<string, unknown> });
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([]);
});
@@ -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 (/\(\?<?[=!]/.test(pattern)) return "lookaround";
return null;
}
/**
* The RE2 form of an ECMAScript pattern, or null when it has none (a lone
* surrogate, lookaround, a backreference). A schema served without such a
* pattern is looser for the client only; the Runner validates the original.
*/
export function re2PortablePattern(pattern: string): string | null {
const portable = escapesToRe2(pattern);
return portable !== null && re2Incompatibility(portable) === null ? portable : null;
}
function escapesToRe2(pattern: string): string | null {
let output = "";
for (let index = 0; index < pattern.length; index++) {
const character = pattern[index]!;