|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import ts from "typescript"; |
| 6 | + |
| 7 | +import { moduleInfoParameters } from "../src/agent/module-info-schema.js"; |
| 8 | + |
| 9 | +// ── Background ──────────────────────────────────────────────────────── |
| 10 | +// Reproduces the production failure: |
| 11 | +// |
| 12 | +// 400 Invalid schema for function 'module_info': In context=('properties', |
| 13 | +// 'functionName', 'type', '1'), array schema missing items. |
| 14 | +// |
| 15 | +// The CAPI/OpenAI tool-schema validator rejects any schema node whose `type` |
| 16 | +// resolves to (or includes) "array" unless an `items` schema is also present. |
| 17 | +// Standard JSON Schema treats `items` as optional, so a generic validator like |
| 18 | +// ajv does NOT catch this — these tests encode the CAPI-specific rule directly. |
| 19 | + |
| 20 | +/** JSON Schema keywords whose values are themselves schemas (or schema maps). */ |
| 21 | +const SCHEMA_CHILD_KEYS = [ |
| 22 | + "items", |
| 23 | + "additionalProperties", |
| 24 | + "contains", |
| 25 | + "propertyNames", |
| 26 | + "if", |
| 27 | + "then", |
| 28 | + "else", |
| 29 | + "not", |
| 30 | +] as const; |
| 31 | + |
| 32 | +const SCHEMA_LIST_KEYS = ["anyOf", "oneOf", "allOf", "prefixItems"] as const; |
| 33 | + |
| 34 | +const SCHEMA_MAP_KEYS = [ |
| 35 | + "properties", |
| 36 | + "patternProperties", |
| 37 | + "$defs", |
| 38 | + "definitions", |
| 39 | +] as const; |
| 40 | + |
| 41 | +/** Does a `type` value (string or string[]) declare an array? */ |
| 42 | +function declaresArray(type: unknown): boolean { |
| 43 | + if (type === "array") return true; |
| 44 | + if (Array.isArray(type)) return type.includes("array"); |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Walk a JSON-Schema-shaped object and return the dotted paths of every node |
| 50 | + * that declares an `array` type without an accompanying `items` schema. An |
| 51 | + * empty result means the schema satisfies the CAPI "array needs items" rule. |
| 52 | + */ |
| 53 | +function findArrayTypesMissingItems( |
| 54 | + schema: unknown, |
| 55 | + path = "$", |
| 56 | + found: string[] = [], |
| 57 | +): string[] { |
| 58 | + if (schema === null || typeof schema !== "object") return found; |
| 59 | + |
| 60 | + if (Array.isArray(schema)) { |
| 61 | + schema.forEach((entry, index) => |
| 62 | + findArrayTypesMissingItems(entry, `${path}[${index}]`, found), |
| 63 | + ); |
| 64 | + return found; |
| 65 | + } |
| 66 | + |
| 67 | + const node = schema as Record<string, unknown>; |
| 68 | + |
| 69 | + if (declaresArray(node.type) && node.items === undefined) { |
| 70 | + found.push(path); |
| 71 | + } |
| 72 | + |
| 73 | + for (const key of SCHEMA_CHILD_KEYS) { |
| 74 | + if (node[key] !== undefined) { |
| 75 | + findArrayTypesMissingItems(node[key], `${path}.${key}`, found); |
| 76 | + } |
| 77 | + } |
| 78 | + for (const key of SCHEMA_LIST_KEYS) { |
| 79 | + if (node[key] !== undefined) { |
| 80 | + findArrayTypesMissingItems(node[key], `${path}.${key}`, found); |
| 81 | + } |
| 82 | + } |
| 83 | + for (const key of SCHEMA_MAP_KEYS) { |
| 84 | + const map = node[key]; |
| 85 | + if (map && typeof map === "object" && !Array.isArray(map)) { |
| 86 | + for (const [childName, childSchema] of Object.entries(map)) { |
| 87 | + findArrayTypesMissingItems( |
| 88 | + childSchema, |
| 89 | + `${path}.${key}.${childName}`, |
| 90 | + found, |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return found; |
| 97 | +} |
| 98 | + |
| 99 | +describe("CAPI array-schema rule checker", () => { |
| 100 | + it("flags the original broken module_info shape (regression guard)", () => { |
| 101 | + // This is the exact shape that triggered the 400 in production. |
| 102 | + const broken = { |
| 103 | + type: "object", |
| 104 | + properties: { |
| 105 | + functionName: { type: ["string", "array"] }, |
| 106 | + }, |
| 107 | + }; |
| 108 | + expect(findArrayTypesMissingItems(broken)).toEqual([ |
| 109 | + "$.properties.functionName", |
| 110 | + ]); |
| 111 | + }); |
| 112 | + |
| 113 | + it("accepts an array type once items is supplied", () => { |
| 114 | + const fixed = { |
| 115 | + type: "object", |
| 116 | + properties: { |
| 117 | + functionName: { |
| 118 | + anyOf: [ |
| 119 | + { type: "string" }, |
| 120 | + { type: "array", items: { type: "string" } }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + }, |
| 124 | + }; |
| 125 | + expect(findArrayTypesMissingItems(fixed)).toEqual([]); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe("module_info parameter schema (real shipped object)", () => { |
| 130 | + it("does not declare an array type without items", () => { |
| 131 | + // Validates the ACTUAL object exported and used by the module_info tool — |
| 132 | + // not a re-declaration — so this proves the production schema is valid. |
| 133 | + expect(findArrayTypesMissingItems(moduleInfoParameters)).toEqual([]); |
| 134 | + }); |
| 135 | + |
| 136 | + it("models functionName as a string or an array of strings via anyOf", () => { |
| 137 | + const functionName = ( |
| 138 | + moduleInfoParameters.properties as Record<string, unknown> |
| 139 | + ).functionName; |
| 140 | + expect(functionName).toMatchObject({ |
| 141 | + anyOf: [{ type: "string" }, { type: "array", items: { type: "string" } }], |
| 142 | + }); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +// ── Static safety net across every tool schema ─────────────────────── |
| 147 | +// Parses the real source under src/agent and asserts that no tool schema |
| 148 | +// (inline `defineTool` parameters or extracted schema literal) reintroduces an |
| 149 | +// array type without items. Catches the whole class of bug for all tools, |
| 150 | +// present and future, without booting the agent. |
| 151 | + |
| 152 | +const AGENT_SRC_DIR = fileURLToPath(new URL("../src/agent", import.meta.url)); |
| 153 | + |
| 154 | +function collectTsFiles(dir: string, acc: string[] = []): string[] { |
| 155 | + for (const entry of readdirSync(dir)) { |
| 156 | + const full = join(dir, entry); |
| 157 | + if (statSync(full).isDirectory()) { |
| 158 | + collectTsFiles(full, acc); |
| 159 | + } else if (entry.endsWith(".ts") && !entry.endsWith(".d.ts")) { |
| 160 | + acc.push(full); |
| 161 | + } |
| 162 | + } |
| 163 | + return acc; |
| 164 | +} |
| 165 | + |
| 166 | +/** AST equivalent of `declaresArray` for a `type` property initializer. */ |
| 167 | +function astTypeDeclaresArray(initializer: ts.Expression): boolean { |
| 168 | + if (ts.isStringLiteral(initializer)) { |
| 169 | + return initializer.text === "array"; |
| 170 | + } |
| 171 | + if (ts.isArrayLiteralExpression(initializer)) { |
| 172 | + return initializer.elements.some( |
| 173 | + (el) => ts.isStringLiteral(el) && el.text === "array", |
| 174 | + ); |
| 175 | + } |
| 176 | + return false; |
| 177 | +} |
| 178 | + |
| 179 | +function findSchemaViolationsInSource( |
| 180 | + filePath: string, |
| 181 | + source: string, |
| 182 | +): string[] { |
| 183 | + const sourceFile = ts.createSourceFile( |
| 184 | + filePath, |
| 185 | + source, |
| 186 | + ts.ScriptTarget.Latest, |
| 187 | + /* setParentNodes */ true, |
| 188 | + ); |
| 189 | + const violations: string[] = []; |
| 190 | + |
| 191 | + const visit = (node: ts.Node): void => { |
| 192 | + if (ts.isObjectLiteralExpression(node)) { |
| 193 | + let typeProp: ts.PropertyAssignment | undefined; |
| 194 | + let hasItems = false; |
| 195 | + for (const prop of node.properties) { |
| 196 | + if (!ts.isPropertyAssignment(prop)) continue; |
| 197 | + const name = prop.name.getText(sourceFile); |
| 198 | + if (name === "type") typeProp = prop; |
| 199 | + if (name === "items") hasItems = true; |
| 200 | + } |
| 201 | + if (typeProp && astTypeDeclaresArray(typeProp.initializer) && !hasItems) { |
| 202 | + const { line, character } = sourceFile.getLineAndCharacterOfPosition( |
| 203 | + typeProp.getStart(sourceFile), |
| 204 | + ); |
| 205 | + violations.push(`${filePath}:${line + 1}:${character + 1}`); |
| 206 | + } |
| 207 | + } |
| 208 | + ts.forEachChild(node, visit); |
| 209 | + }; |
| 210 | + |
| 211 | + visit(sourceFile); |
| 212 | + return violations; |
| 213 | +} |
| 214 | + |
| 215 | +describe("all agent tool schemas (static source scan)", () => { |
| 216 | + it("no schema literal declares an array type without items", () => { |
| 217 | + const files = collectTsFiles(AGENT_SRC_DIR); |
| 218 | + // Sanity: ensure the scan actually found source to inspect. |
| 219 | + expect(files.length).toBeGreaterThan(0); |
| 220 | + |
| 221 | + const violations = files.flatMap((file) => |
| 222 | + findSchemaViolationsInSource(file, readFileSync(file, "utf8")), |
| 223 | + ); |
| 224 | + expect(violations).toEqual([]); |
| 225 | + }); |
| 226 | +}); |
0 commit comments