diff --git a/packages/core/src/tool/runtime.ts b/packages/core/src/tool/runtime.ts index fa510787ad7..de92537652b 100644 --- a/packages/core/src/tool/runtime.ts +++ b/packages/core/src/tool/runtime.ts @@ -104,9 +104,17 @@ const jsonSchema = (schema: JsonSchema.JsonSchema) => { (typeof schema.$schema === "string" && schema.$schema.includes("draft-07")) || "definitions" in schema ? JsonSchema.fromSchemaDraft07(schema) : JsonSchema.fromSchemaDraft2020_12(schema) - // Tool schemas are untrusted, so skip regexes (ReDoS) but keep validating everything else. + // Tool schemas are untrusted, so skip regexes (ReDoS) but keep validating everything else. With regexes gone, + // `oneOf` branches that only differed by pattern would all match and reject valid input, so treat it as `anyOf`. return Schema.make>( - SchemaRepresentation.fromJsonSchemaDocument(draft, { patterns: "ignore" }).ast, + SchemaRepresentation.fromJsonSchemaDocument(draft, { + patterns: "ignore", + onEnter: (node) => { + if (!("oneOf" in node) || "anyOf" in node) return node + const { oneOf, ...rest } = node + return { ...rest, anyOf: oneOf } + }, + }).ast, ) } diff --git a/packages/core/test/tool-schema.test.ts b/packages/core/test/tool-schema.test.ts index a045820ee62..0f4fe3b6335 100644 --- a/packages/core/test/tool-schema.test.ts +++ b/packages/core/test/tool-schema.test.ts @@ -530,6 +530,26 @@ test("raw JSON schemas with regex patterns still validate input", async () => { expect(await Effect.runPromise(execute(tool, { id: "not-hex", count: 1 }, context))).toMatchObject({ content: [{ type: "text", text: '{"id":"not-hex","count":1}' }], }) + const oneOf: Info = { + ...tool, + name: "pattern-one-of", + input: { + type: "object", + properties: { + v: { + oneOf: [ + { type: "string", pattern: "^a" }, + { type: "integer", minimum: 0 }, + { type: "string", pattern: "^b" }, + ], + }, + }, + }, + } + expect(await Effect.runPromise(execute(oneOf, { v: "abc" }, context))).toMatchObject({ + content: [{ type: "text", text: '{"v":"abc"}' }], + }) + expect(await Effect.runPromise(Effect.flip(execute(oneOf, { v: true }, context)))).toBeInstanceOf(Tool.Error) expect(await Effect.runPromise(Effect.flip(execute(tool, { id: "abc" }, context)))).toEqual( new Tool.Error({ message: