Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/exec-input-object.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion docs/09_tool_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
32 changes: 32 additions & 0 deletions packages/computer/src/tools/common/exec.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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" });
Comment on lines +106 to +114

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Provider compatibility beyond reference removal

inputObjectSchema removes references but still emits propertyNames and additionalProperties: {}. The reported workaround omitted both, so the schema test does not establish compatibility with that provider.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

});

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 }> = [];
Expand Down
35 changes: 16 additions & 19 deletions packages/computer/src/tools/common/exec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<WorkspaceRuntimeValue> = 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);
Comment on lines +21 to +23

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Undefined fields now pass validation

inputObjectSchema accepts and retains explicitly undefined object fields that the old schema rejected. assertRuntimeValue treats those fields as absent during JSON validation.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

} 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,
Expand Down Expand Up @@ -307,16 +305,15 @@ export function defineExec(options: ExecToolOptions): ExecDefinition {
);
}
if (callableBackendIds.size > 0) {
shape.input = z
.record(z.string(), jsonValueSchema)
shape.input = inputObjectSchema
.optional()
.describe(
single
? "Input object handed to the module."
: "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<ExecInput>;

// Settles when the run should detach, as `detach` asks, and never
Expand Down
Loading