Files
9router/tests/unit/codex-tool-normalization.test.js
anhtran-ai 781c18d837 fix(codex): strip Unicode-property tool schema patterns Codex rejects
Codex's /responses validator has no Unicode property escapes, so a tool
`pattern` containing `\p{...}` 400s the whole request with `Invalid schema
for function ... is not a 'regex'` — identically on every account, costing a
full combo failover per turn (#3922).

- Add open-sse/utils/codexToolSchema.js: copy-on-write walk that drops only
  `pattern` values carrying a property escape, returning the original
  reference when nothing changed so the caller's schema stays intact for a
  retry against another provider
- Treat `properties` keys as property names, so a field literally called
  `pattern` is never read as the schema keyword; skip escaped literals via
  backslash-parity counting
- Apply it in normalizeCodexTools for both function and namespace sub-tool
  parameters, and log the strip count via dbg
- Add three cases to tests/unit/codex-tool-normalization.test.js
2026-09-10 22:25:47 +07:00

204 lines
6.0 KiB
JavaScript

import { describe, expect, it } from "vitest";
import { CodexExecutor } from "../../open-sse/executors/codex.js";
function normalizeTools(tools) {
const executor = new CodexExecutor();
const body = {
model: "gpt-5.5",
input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "probe" }] }],
tools,
stream: true,
};
executor.transformRequest("gpt-5.5", body, true, {
connectionId: "test-codex-tools",
providerSpecificData: {},
});
return body.tools;
}
describe("CodexExecutor tool normalization", () => {
it("preserves Responses text.format for structured outputs", () => {
const executor = new CodexExecutor();
const schema = {
type: "object",
additionalProperties: false,
properties: {
title: { type: "string" },
},
required: ["title"],
};
const body = {
model: "gpt-5.4-mini",
input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "test for session title" }] }],
stream: true,
metadata: { unsupported: true },
text: {
format: {
type: "json_schema",
name: "codex_output_schema",
strict: true,
schema,
},
},
};
executor.transformRequest("gpt-5.4-mini", body, true, {
connectionId: "test-codex-structured-output",
providerSpecificData: {},
});
expect(body.text).toEqual({
format: {
type: "json_schema",
name: "codex_output_schema",
strict: true,
schema,
},
});
expect(body.metadata).toBeUndefined();
});
it("preserves Responses-native tool_search tools", () => {
const tools = normalizeTools([
{
type: "tool_search",
execution: "sync",
description: "Discover deferred tools",
parameters: { type: "object", properties: {} },
},
{
type: "namespace",
name: "codex_app",
description: "app tools",
tools: [
{
type: "function",
name: "automation_update",
description: "automation",
parameters: { type: "object", properties: {} },
defer_loading: true,
},
],
},
{
type: "function",
name: "plain_fn",
description: "plain",
parameters: { type: "object", properties: {} },
},
]);
expect(tools.map((tool) => `${tool.type}:${tool.name || ""}`)).toEqual([
"tool_search:",
"namespace:codex_app",
"function:plain_fn",
]);
});
it("preserves hosted Responses tools", () => {
const tools = normalizeTools([
{ type: "web_search", search_context_size: "medium" },
{ type: "image_generation", size: "1024x1024" },
{ type: "mcp", server_label: "docs", server_url: "https://example.com/mcp" },
{ type: "local_shell" },
{ type: "code_interpreter", container: { type: "auto" } },
{ type: "computer", display_width: 1024, display_height: 768, environment: "browser" },
]);
expect(tools.map((tool) => tool.type)).toEqual([
"web_search",
"image_generation",
"mcp",
"local_shell",
"code_interpreter",
"computer",
]);
});
it("strips only Unicode-property patterns rejected by Codex", () => {
const unicodePattern = "^(?!__.*__$)[^\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}]{1,200}$";
const validPattern = "^[a-z][a-z0-9_-]{0,31}$";
const sourceParameters = {
type: "object",
properties: {
artifact: {
type: "object",
properties: {
name: { type: "string", pattern: unicodePattern },
slug: { type: "string", pattern: validPattern },
},
},
// A property named "pattern" is data, not the schema keyword.
pattern: { type: "string", pattern: validPattern },
},
allOf: [{ properties: { title: { type: "string", pattern: unicodePattern } } }],
};
const tools = normalizeTools([{
type: "function",
name: "Artifact",
parameters: sourceParameters,
}]);
expect(tools[0].parameters.properties.artifact.properties.name.pattern).toBeUndefined();
expect(tools[0].parameters.properties.artifact.properties.slug.pattern).toBe(validPattern);
expect(tools[0].parameters.properties.pattern.pattern).toBe(validPattern);
expect(tools[0].parameters.allOf[0].properties.title.pattern).toBeUndefined();
// Copy-on-write: the caller's schema remains available for another provider.
expect(sourceParameters.properties.artifact.properties.name.pattern).toBe(unicodePattern);
});
it("keeps escaped literal property text and schema identity when no strip is needed", () => {
const parameters = {
type: "object",
properties: {
literal: { type: "string", pattern: "^\\\\p{Cc}$" },
simple: { type: "string", pattern: "^[A-Z]+$" },
},
};
const tools = normalizeTools([{ type: "function", name: "probe", parameters }]);
expect(tools[0].parameters).toBe(parameters);
expect(tools[0].parameters.properties.literal.pattern).toBe("^\\\\p{Cc}$");
});
it("sanitizes nested namespace function schemas", () => {
const tools = normalizeTools([{
type: "namespace",
name: "agent",
tools: [{
type: "function",
name: "Artifact",
parameters: {
type: "object",
properties: { name: { type: "string", pattern: "^\\p{Cc}+$" } },
},
}],
}]);
expect(tools[0].tools[0].parameters.properties.name.pattern).toBeUndefined();
});
it("preserves custom freeform tools with format payloads", () => {
const tools = normalizeTools([
{
type: "custom",
name: "apply_patch",
description: "patch",
format: { type: "grammar", syntax: "lark", definition: "start: /.+/" },
},
]);
expect(tools).toEqual([
{
type: "custom",
name: "apply_patch",
description: "patch",
format: { type: "grammar", syntax: "lark", definition: "start: /.+/" },
},
]);
});
});