| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- import { describe, expect, it } from "vitest";
- import { createErrorResult } from "../src/tool/executor/errors.js";
- import { validateInitialModelToolInput, validateInput } from "../src/tool/executor/validation.js";
- import { askUserQuestionToolEntry } from "../src/tool/handlers/ask-user-question.js";
- import { bashToolEntry } from "../src/tool/handlers/bash.js";
- import { jsToolEntry } from "../src/tool/handlers/node-repl.js";
- import { exitPlanModeToolEntry } from "../src/tool/handlers/plan-mode.js";
- import { readToolEntry } from "../src/tool/handlers/read.js";
- import { skillToolEntry } from "../src/tool/handlers/skill.js";
- import { todoWriteToolEntry } from "../src/tool/handlers/todo.js";
- import { formatToolInputValidationError } from "../src/tool/input-validation-model-content.js";
- import { prepareInitialToolExecutionInput } from "../src/tool/input-normalization.js";
- import { validateJsonSchemaValue } from "../src/tool/json-schema.js";
- import type { ToolEntry } from "../src/tool/types.js";
- describe("tool input validation provider content", () => {
- it("formats missing, unexpected, and wrong-type issues in the required order", () => {
- const validation = validateJsonSchemaValue(
- {
- extraOne: true,
- extraTwo: false,
- items: [{ count: "bad", nestedExtra: true }],
- },
- {
- additionalProperties: false,
- properties: {
- missingValue: { type: "string" },
- otherMissing: { type: "boolean" },
- items: {
- items: {
- additionalProperties: false,
- properties: {
- count: { type: "integer" },
- },
- required: ["count"],
- type: "object",
- },
- type: "array",
- },
- },
- required: ["missingValue", "otherMissing", "items"],
- type: "object",
- },
- );
- expect(validation.valid).toBe(false);
- expect(formatToolInputValidationError("ValidationTool", validation.issues)).toBe(
- [
- "ValidationTool failed due to the following issues:",
- "The required parameter `missingValue` is missing",
- "The required parameter `otherMissing` is missing",
- "An unexpected parameter `nestedExtra` was provided",
- "An unexpected parameter `extraOne` was provided",
- "An unexpected parameter `extraTwo` was provided",
- "The parameter `items[0].count` type is expected as `number` but provided as `string`",
- ].join("\n"),
- );
- });
- it("uses empty paths and unknown when the received type cannot be extracted", () => {
- expect(
- formatToolInputValidationError("RootTool", [
- {
- expected: "string",
- code: "invalid_type",
- path: [],
- message: "custom type mismatch",
- },
- ]),
- ).toBe(
- [
- "RootTool failed due to the following issue:",
- "The parameter `` type is expected as `string` but provided as `unknown`",
- ].join("\n"),
- );
- });
- it("omits unrecognized issue kinds when a recognized issue exists", () => {
- expect(
- formatToolInputValidationError("MixedTool", [
- {
- code: "invalid_value",
- values: ["fast", "safe"],
- path: ["mode"],
- message: 'Invalid option: expected one of "fast"|"safe"',
- },
- {
- expected: "string",
- code: "invalid_type",
- path: ["name"],
- message: "Invalid input: expected string, received undefined",
- },
- ]),
- ).toBe(
- [
- "MixedTool failed due to the following issue:",
- "The required parameter `name` is missing",
- ].join("\n"),
- );
- });
- it("serializes every non-parameter issue with source-compatible messages and layout", () => {
- const validation = validateJsonSchemaValue(
- {
- choice: true,
- count: 0,
- label: "",
- list: [],
- literal: "wrong",
- mode: "other",
- },
- {
- additionalProperties: false,
- properties: {
- mode: { enum: ["fast", "safe"] },
- label: { type: "string", minLength: 3 },
- count: { type: "number", minimum: 1 },
- list: { type: "array", minItems: 1 },
- literal: { const: "fixed" },
- choice: {
- oneOf: [{ type: "string" }, { type: "number" }],
- },
- },
- required: ["mode", "label", "count", "list", "literal", "choice"],
- type: "object",
- },
- );
- expect(validation.valid).toBe(false);
- expect(formatToolInputValidationError("ConstraintTool", validation.issues)).toBe(`[
- {
- "code": "invalid_value",
- "values": [
- "fast",
- "safe"
- ],
- "path": [
- "mode"
- ],
- "message": "Invalid option: expected one of \\"fast\\"|\\"safe\\""
- },
- {
- "origin": "string",
- "code": "too_small",
- "minimum": 3,
- "inclusive": true,
- "path": [
- "label"
- ],
- "message": "Too small: expected string to have >=3 characters"
- },
- {
- "origin": "number",
- "code": "too_small",
- "minimum": 1,
- "inclusive": true,
- "path": [
- "count"
- ],
- "message": "Too small: expected number to be >=1"
- },
- {
- "origin": "array",
- "code": "too_small",
- "minimum": 1,
- "inclusive": true,
- "path": [
- "list"
- ],
- "message": "Too small: expected array to have >=1 items"
- },
- {
- "code": "invalid_value",
- "values": [
- "fixed"
- ],
- "path": [
- "literal"
- ],
- "message": "Invalid input: expected \\"fixed\\""
- },
- {
- "code": "invalid_union",
- "errors": [
- [
- {
- "expected": "string",
- "code": "invalid_type",
- "path": [],
- "message": "Invalid input: expected string, received boolean"
- }
- ],
- [
- {
- "expected": "number",
- "code": "invalid_type",
- "path": [],
- "message": "Invalid input: expected number, received boolean"
- }
- ]
- ],
- "path": [
- "choice"
- ],
- "message": "Invalid input"
- }
- ]`);
- });
- it("keeps normalized enum type mismatches on the invalid-value JSON fallback", () => {
- const validation = validateJsonSchemaValue(
- {
- mode: 7,
- },
- {
- additionalProperties: false,
- properties: {
- mode: { type: "string", enum: ["fast", "safe"] },
- },
- required: ["mode"],
- type: "object",
- },
- );
- expect(formatToolInputValidationError("EnumTool", validation.issues)).toBe(`[
- {
- "code": "invalid_value",
- "values": [
- "fast",
- "safe"
- ],
- "path": [
- "mode"
- ],
- "message": "Invalid option: expected one of \\"fast\\"|\\"safe\\""
- }
- ]`);
- });
- it("preserves schema property order across present constraints and missing enums", () => {
- const validation = validateJsonSchemaValue(
- {
- todos: [
- {
- content: "",
- priority: "high",
- },
- ],
- },
- {
- additionalProperties: false,
- properties: {
- todos: {
- items: {
- additionalProperties: false,
- properties: {
- content: { type: "string", minLength: 1 },
- status: {
- type: "string",
- enum: ["pending", "in_progress", "completed"],
- },
- priority: {
- type: "string",
- enum: ["high", "medium", "low"],
- },
- },
- required: ["content", "status", "priority"],
- type: "object",
- },
- type: "array",
- },
- },
- required: ["todos"],
- type: "object",
- },
- );
- expect(formatToolInputValidationError("TodoWrite", validation.issues)).toBe(`[
- {
- "origin": "string",
- "code": "too_small",
- "minimum": 1,
- "inclusive": true,
- "path": [
- "todos",
- 0,
- "content"
- ],
- "message": "Too small: expected string to have >=1 characters"
- },
- {
- "code": "invalid_value",
- "values": [
- "pending",
- "in_progress",
- "completed"
- ],
- "path": [
- "todos",
- 0,
- "status"
- ],
- "message": "Invalid option: expected one of \\"pending\\"|\\"in_progress\\"|\\"completed\\""
- }
- ]`);
- });
- it("serializes bigint issue values as strings", () => {
- expect(
- formatToolInputValidationError("BigIntConstraintTool", [
- {
- code: "invalid_value",
- values: [1n],
- path: ["value"],
- message: "Invalid input: expected 1n",
- },
- ]),
- ).toBe(`[
- {
- "code": "invalid_value",
- "values": [
- "1"
- ],
- "path": [
- "value"
- ],
- "message": "Invalid input: expected 1n"
- }
- ]`);
- });
- it("attaches exact model content only to initial model input validation", () => {
- const entry = createValidationEntry("ValidationTool");
- const toolCall = {
- id: "call_initial_validation",
- input: {},
- name: "ValidationTool",
- };
- const initialError = validateInitialModelToolInput({}, entry);
- const initialResult = createErrorResult(toolCall, initialError!);
- expect(initialResult.error?.message).toBe("Tool input failed inputSchema validation");
- expect(initialResult.modelContent).toBe(
- [
- "<tool_use_error>InputValidationError: ValidationTool failed due to the following issue:",
- "The required parameter `value` is missing</tool_use_error>",
- ].join("\n"),
- );
- const laterError = validateInput({}, entry);
- expect(createErrorResult(toolCall, laterError!).modelContent).toBeUndefined();
- });
- it("keeps every provider issue when internal diagnostics exceed their limit", () => {
- const required = Array.from({ length: 25 }, (_, index) => `value${index.toString()}`);
- const entry = {
- inputSchema: {
- additionalProperties: false,
- properties: Object.fromEntries(required.map((name) => [name, { type: "string" }])),
- required,
- type: "object",
- },
- metadata: {
- name: "CompleteValidationTool",
- },
- } as ToolEntry;
- const error = validateInitialModelToolInput({}, entry);
- const result = createErrorResult(
- {
- id: "call_complete_validation",
- input: {},
- name: "CompleteValidationTool",
- },
- error!,
- );
- expect(result.modelContent?.match(/The required parameter/g)).toHaveLength(25);
- expect(result.modelContent).toContain("The required parameter `value24` is missing");
- });
- it("lets initial validation content take precedence over the Edit execution wrapper", () => {
- const entry = createValidationEntry("Edit");
- const error = validateInitialModelToolInput({}, entry);
- const result = createErrorResult(
- {
- id: "call_edit_validation",
- input: {},
- name: "Edit",
- },
- error!,
- );
- expect(result.modelContent).toBe(
- [
- "<tool_use_error>InputValidationError: Edit failed due to the following issue:",
- "The required parameter `value` is missing</tool_use_error>",
- ].join("\n"),
- );
- });
- });
- describe("production runtime schema validation issues", () => {
- it("does not report a defaulted field when another AskUserQuestion constraint fails", () => {
- const content = initialValidationModelContent(askUserQuestionToolEntry, {
- questions: [
- {
- header: "Choice",
- options: [],
- question: "Which option?",
- },
- ],
- });
- expect(content).toContain('"code": "too_small"');
- expect(content).toContain('"path": [\n "questions",\n 0,\n "options"\n ]');
- expect(content).not.toContain("multiSelect");
- });
- it("places nested item constraints before outer array constraints", () => {
- const questions = Array.from({ length: 5 }, (_, index) => validQuestion(index));
- questions[0] = { ...questions[0], options: [] };
- const content = initialValidationModelContent(askUserQuestionToolEntry, { questions });
- const nestedIssue = content.indexOf(
- '"path": [\n "questions",\n 0,\n "options"\n ]',
- );
- const outerIssue = content.indexOf('"path": [\n "questions"\n ]');
- expect(nestedIssue).toBeGreaterThanOrEqual(0);
- expect(outerIssue).toBeGreaterThan(nestedIssue);
- });
- it("keeps runtime custom issues instead of reconstructing a default-field error", () => {
- const question = validQuestion(0);
- const content = initialValidationModelContent(askUserQuestionToolEntry, {
- questions: [
- { ...question, multiSelect: undefined },
- { ...question, multiSelect: undefined },
- ],
- });
- expect(content).toBe(`<tool_use_error>InputValidationError: [
- {
- "code": "custom",
- "path": [
- "questions"
- ],
- "message": "Question texts must be unique"
- }
- ]</tool_use_error>`);
- });
- it("does not report fields accepted by runtime preprocessing when a sibling fails", () => {
- expect(
- initialValidationModelContent(bashToolEntry, {
- command: 1,
- run_in_background: "true",
- timeout: "10",
- }),
- ).toBe(
- [
- "<tool_use_error>InputValidationError: Bash failed due to the following issue:",
- "The parameter `command` type is expected as `string` but provided as `number`</tool_use_error>",
- ].join("\n"),
- );
- });
- it.each([
- {
- entry: jsToolEntry,
- input: { code: 1 },
- expected: [
- "The required parameter `title` is missing",
- "The parameter `code` type is expected as `string` but provided as `number`",
- ],
- },
- {
- entry: skillToolEntry,
- input: { args: 1, name: "existing-skill" },
- expected: [
- "The required parameter `skill` is missing",
- "An unexpected parameter `name` was provided",
- "The parameter `args` type is expected as `string` but provided as `number`",
- ],
- },
- {
- entry: readToolEntry,
- input: { file_path: 1, pages: "1" },
- expected: [
- "An unexpected parameter `pages` was provided",
- "The parameter `file_path` type is expected as `string` but provided as `number`",
- ],
- },
- ])(
- "keeps provider-only structural issues for $entry.metadata.name",
- ({ entry, input, expected }) => {
- const content = initialValidationModelContent(entry, input);
- for (const line of expected) {
- expect(content).toContain(line);
- }
- },
- );
- it("keeps a single invalid-value issue for production enum schemas", () => {
- const content = initialValidationModelContent(todoWriteToolEntry, {
- todos: [
- {
- content: "Check result",
- priority: "urgent",
- status: "pending",
- },
- ],
- });
- expect(content.match(/"code": "invalid_value"/gu)).toHaveLength(1);
- expect(content).not.toContain('"code": "invalid_type"');
- });
- it("keeps a field refinement before a later enum issue", () => {
- expect(
- initialValidationModelContent(exitPlanModeToolEntry, {
- allowedPrompts: [{ prompt: "", tool: "bad" }],
- plan: " ",
- }),
- ).toBe(`<tool_use_error>InputValidationError: [
- {
- "code": "custom",
- "path": [
- "plan"
- ],
- "message": "String must contain at least 1 character(s)"
- },
- {
- "code": "invalid_value",
- "values": [
- "Bash"
- ],
- "path": [
- "allowedPrompts",
- 0,
- "tool"
- ],
- "message": "Invalid input: expected \\"Bash\\""
- }
- ]</tool_use_error>`);
- });
- });
- function createValidationEntry(name: string): ToolEntry {
- return {
- inputSchema: {
- additionalProperties: false,
- properties: {
- value: { type: "string" },
- },
- required: ["value"],
- type: "object",
- },
- metadata: {
- name,
- },
- } as ToolEntry;
- }
- function initialValidationModelContent(entry: ToolEntry, input: unknown): string {
- const prepared = prepareInitialToolExecutionInput({ entry, input });
- const error = validateInitialModelToolInput(
- prepared.input,
- entry,
- prepared.runtimeValidationIssues,
- );
- const result = createErrorResult(
- {
- id: `call_${entry.metadata.name}`,
- input,
- name: entry.metadata.name,
- },
- error!,
- );
- expect(typeof result.modelContent).toBe("string");
- return result.modelContent as string;
- }
- function validQuestion(index: number): {
- header: string;
- multiSelect: boolean;
- options: Array<{ description: string; label: string }>;
- question: string;
- } {
- return {
- header: `Q${index.toString()}`,
- multiSelect: false,
- options: [
- { description: "First choice", label: "First" },
- { description: "Second choice", label: "Second" },
- ],
- question: `Question ${index.toString()}?`,
- };
- }
|