diff --git a/.changeset/exec-input-object.md b/.changeset/exec-input-object.md new file mode 100644 index 00000000..ce145005 --- /dev/null +++ b/.changeset/exec-input-object.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/computer": patch +--- + +The `exec` tool declares `input` as a plain object, with no `$ref` into a definitions section, and still checks its values are JSON. diff --git a/docs/09_tool_interface.md b/docs/09_tool_interface.md index 6f410707..1c2aac50 100644 --- a/docs/09_tool_interface.md +++ b/docs/09_tool_interface.md @@ -373,7 +373,7 @@ The tool offers only the arguments that can work: A `backend` value the model sends anyway is dropped when only one backend is configured. The output still names the backend that ran. -`input` is an object of JSON values, described to the model as an input object. Offered any JSON value, some models send the object they mean as a string of JSON. A string that parses to an object is replaced by that object before validation, through the definition's `prepareArguments`; `createPiTools` applies it, and a caller of `defineExec` should too. +`input` is an object of JSON values, described to the model as an input object. The declaration says only that it is an object, and the values are checked as JSON when a call is validated: declaring them would take a recursive schema, which serializes to a `$ref` that some providers reject. Offered any JSON value, some models send the object they mean as a string of JSON. A string that parses to an object is replaced by that object before validation, through the definition's `prepareArguments`; `createPiTools` applies it, and a caller of `defineExec` should too. Long output follows pi's bash tool. Each of stdout and stderr shows its last `maxLines` lines (2000) or `maxBytes` (64 KiB), whichever is hit first. Set them with `exec: { output }` on a tool set, or on `createExecTool` directly. The tool passes the same limits to the runtime, which saves the full output to a Workspace file (see [Long output](./05_runtime_interface.md#long-output)), and the reply ends with a note naming it: diff --git a/packages/computer/src/tools/common/exec.test.ts b/packages/computer/src/tools/common/exec.test.ts index 26317c28..724ec00c 100644 --- a/packages/computer/src/tools/common/exec.test.ts +++ b/packages/computer/src/tools/common/exec.test.ts @@ -1,5 +1,6 @@ import { SQLiteTestStorage } from "@cloudflare/dofs/testing"; import { describe, expect, it, vi } from "vitest"; +import { z } from "zod"; import type { WorkspaceModuleBackend } from "../../runtime/types"; import { Workspace } from "../../workspace"; @@ -94,6 +95,37 @@ describe("defineExec metadata", () => { }); }); +describe("defineExec input", () => { + const callable: ExecWorkspaceLike = { + runtime: { + exec: async () => ({ result: async () => ({ exitCode: 0, stdout: "", stderr: "" }) }), + backends: () => [{ id: "js", protocol: "module", callable: true }], + }, + }; + + it("declares input as an object, with no definitions to point into", () => { + const json = z.toJSONSchema(defineExec({ workspace: callable }).inputSchema, { + target: "draft-7", + io: "input", + }); + + expect(JSON.stringify(json)).not.toContain("$ref"); + expect(json).not.toHaveProperty("definitions"); + expect(json.properties?.input).toMatchObject({ type: "object" }); + }); + + it("accepts nested JSON, and refuses values JSON can't carry", () => { + const { inputSchema } = defineExec({ workspace: callable }); + const parse = (input: unknown) => inputSchema.safeParse({ command: "x", input }).success; + + expect(parse({ a: [1, "two", { three: null }], b: true })).toBe(true); + expect(parse({ a: Number.NaN })).toBe(false); + expect(parse({ a: new Date(0) })).toBe(false); + expect(parse({ a: () => 1 })).toBe(false); + expect(parse([1, 2])).toBe(false); + }); +}); + describe("defineExec result bound", () => { it("saves a return value past the output limit to a file", async () => { const written: Array<{ path: string; size: number }> = []; diff --git a/packages/computer/src/tools/common/exec.ts b/packages/computer/src/tools/common/exec.ts index 4888ceab..31cd2400 100644 --- a/packages/computer/src/tools/common/exec.ts +++ b/packages/computer/src/tools/common/exec.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { assertRuntimeValue } from "../../runtime/capability"; import type { TruncatedOutput } from "../../runtime/output-spool"; import { DEFAULT_OUTPUT_MAX_BYTES, @@ -11,22 +12,19 @@ import type { WorkspaceBackendInfo, WorkspaceRuntimeSaveResult } from "../../run import { notCallableMessage } from "../../runtime/runtime"; import type { WorkspaceRuntimeTruncation, WorkspaceRuntimeValue } from "../../runtime/types"; -// A finite JSON value: what a callable backend accepts as `input` and -// returns as `result`. Declared as a concrete recursive schema rather -// than z.unknown() so the tool validates `input` at its own boundary -// and the generated JSON Schema describes a real shape (z.unknown() -// serializes to an empty schema that some providers reject under -// strict function calling). -const jsonValueSchema: z.ZodType = z.lazy(() => - z.union([ - z.string(), - z.number(), - z.boolean(), - z.null(), - z.array(jsonValueSchema), - z.record(z.string(), jsonValueSchema), - ]), -); +// What a callable backend accepts as `input`: an object of finite JSON +// values. The values are checked here, as the backend checks them, +// rather than declared: a recursive schema serializes to a `$ref` into +// a definitions section, which some providers reject. The object itself +// stays declared, since some providers reject an empty schema under +// strict function calling. +const inputObjectSchema = z.record(z.string(), z.unknown()).superRefine((value, context) => { + try { + assertRuntimeValue(value); + } catch (error) { + context.addIssue({ code: "custom", message: errorMessage(error) }); + } +}); // One event drained from a running execution. stdout / stderr carry // output chunks as they arrive; exit carries the process exit code and, @@ -307,8 +305,7 @@ export function defineExec(options: ExecToolOptions): ExecDefinition { ); } if (callableBackendIds.size > 0) { - shape.input = z - .record(z.string(), jsonValueSchema) + shape.input = inputObjectSchema .optional() .describe( single @@ -316,7 +313,7 @@ export function defineExec(options: ExecToolOptions): ExecDefinition { : "Input object handed to a callable backend's module. Other backends reject it.", ); } - // SAFETY: Every field in `shape` has the type ExecInput gives it, and the fields left out are optional there. + // SAFETY: Every field in `shape` has the type ExecInput gives it, `input`'s through its check, and the fields left out are optional there. const inputSchema = z.object(shape) as unknown as z.ZodType; // Settles when the run should detach, as `detach` asks, and never