The Open Framework for Building Agents
refactor(eve): run workflow programs on run directly (#5187)
Andrew Barba07dbdef3eaf3
diff --git a/.changeset/workflow-programs-on-run.md b/.changeset/workflow-programs-on-run.mdnew file mode 100644index 000000000..7f0d2c331--- /dev/null+++ b/.changeset/workflow-programs-on-run.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Each start or resume of a `workflow` tool program may now run up to 30 seconds of JavaScript and take up to 5 minutes in total, and time spent waiting for child agents counts toward neither limit.diff --git a/NOTICE b/NOTICEindex d4068e02f..793686614 100644--- a/NOTICE+++ b/NOTICE@@ -4,7 +4,7 @@ Copyright 2026 Vercel, Inc. and contributorsThis product includes software developed at Vercel, Inc.(https://vercel.com/).-The bundled code-mode runtime includes run, Copyright 2023 Vercel, Inc.,+The bundled workflow program runtime is run, Copyright 2023 Vercel, Inc.,licensed under the Apache License, Version 2.0.run embeds devalue 5.8.2, quickjs-wasi 3.6.0, and QuickJS under the MITdiff --git a/docs/tools/workflows.mdx b/docs/tools/workflows.mdxindex 4550949e3..ecb955d94 100644--- a/docs/tools/workflows.mdx+++ b/docs/tools/workflows.mdx@@ -146,9 +146,21 @@ continue a child from an earlier call. Agent authorization, questions, approvalsbehave as they do for sessions an authored workflow tool opens with `ctx.agent`.The sandbox resumes after every pending call in the current batch settles. `Promise.all` supports-fan-out followed by fan-in, but `Promise.race` does not resume after only the first child settles.-A failed child turn throws at the corresponding `ctx.agent` call, so generated code can catch it.-The program must return a JSON-serializable value.+fan-out followed by fan-in. The program must await every `ctx.agent` call before it returns. A call+that is still pending at that point, such as one that is never awaited or one that loses a+`Promise.race` or `Promise.any`, fails the workflow.++A failed child turn, an invalid call, or a call over the `maxSubagents` budget throws a+`WorkflowProgramAgentError` at that `ctx.agent` call, which the program can catch; the message+says which.++The sandbox converts the program's return value and each `ctx.agent` input with `JSON.stringify`,+so a `Date` becomes a string and functions and `undefined` members drop. A return value that+converts to nothing, such as `undefined`, becomes `null`. Each converted value may be up to 1 MiB of+JSON, each child reply up to 4 MiB of JSON, and the program source up to 256 KiB.++Each start or resume may spend up to 30 seconds running JavaScript and up to 5 minutes in total.+Time spent waiting for child agents does not count toward either limit.### Migrate from `experimental_workflow`diff --git a/packages/eve/package.json b/packages/eve/package.jsonindex 4da343d3a..30bc9e3f3 100644--- a/packages/eve/package.json+++ b/packages/eve/package.json@@ -587,7 +587,6 @@"devDependencies": {"@agentclientprotocol/sdk": "1.3.0","@ai-sdk/anthropic": "catalog:",- "@ai-sdk/code-mode": "catalog:","@ai-sdk/google": "catalog:","@ai-sdk/mcp": "catalog:","@ai-sdk/openai": "catalog:",@@ -652,7 +651,7 @@"playwright-core": "1.63.0","react": "catalog:","react-test-renderer": "19.2.6",- "run": "2.1.2",+ "run": "2.1.8","semver": "7.8.4","svelte": "^5.0.0","turndown": "7.2.4",diff --git a/packages/eve/scripts/vendor-compiled/@ai-sdk/code-mode.mjs b/packages/eve/scripts/vendor-compiled/@ai-sdk/code-mode.mjsdeleted file mode 100644index 992b5629d..000000000--- a/packages/eve/scripts/vendor-compiled/@ai-sdk/code-mode.mjs+++ /dev/null@@ -1,37 +0,0 @@-import { relative, sep } from "node:path";--import { collectFilesRecursively, createDeclarationCopier } from "../_shared.mjs";--function toPosixPath(path) {- return path.split(sep).join("/");-}--function buildRunStub(names, moduleName) {- const unsupported = [...names].filter((name) => name !== "setMaxWorkers");- if (unsupported.length > 0) {- throw new Error(- `Vendor: unsupported ${moduleName} declaration imports: ${unsupported.join(", ")}`,- );- }- return "export declare function setMaxWorkers(maxWorkers?: number): void;\n";-}--export default {- packageName: "@ai-sdk/code-mode",- compiledPath: "@ai-sdk/code-mode",- bundling: "standalone",- copyDeclarations: createDeclarationCopier({- rewrites: {- ai: { kind: "external" },- run: { kind: "stub", stubBaseName: "_run", build: buildRunStub },- },- files: async ({ distDir }) =>- (await collectFilesRecursively(distDir, [".d.ts"]))- .map((file) => toPosixPath(relative(distDir, file)))- .sort()- .map((file) => ({ source: file, output: file })),- }),- external(source) {- return source === "ai";- },-};diff --git a/packages/eve/scripts/vendor-compiled/index.mjs b/packages/eve/scripts/vendor-compiled/index.mjsindex a7e759ce3..53d1bc256 100644--- a/packages/eve/scripts/vendor-compiled/index.mjs+++ b/packages/eve/scripts/vendor-compiled/index.mjs@@ -5,7 +5,6 @@*/import acpSdk from "./@agentclientprotocol/sdk.mjs";import anthropic from "./@ai-sdk/anthropic.mjs";-import codeMode from "./@ai-sdk/code-mode.mjs";import google from "./@ai-sdk/google.mjs";import mcp from "./@ai-sdk/mcp.mjs";import openai from "./@ai-sdk/openai.mjs";@@ -55,6 +54,7 @@ import jsonSchema from "./json-schema.mjs";import justSecrets from "./just-secrets.mjs";import marked from "./marked.mjs";import picocolors from "./picocolors.mjs";+import run from "./run.mjs";import semver from "./semver.mjs";import shadcnRegistry from "./shadcn-registry.mjs";import turndown from "./turndown.mjs";@@ -65,7 +65,6 @@ export const MODULES = [acpSdk,anthropic,cfworkerJsonSchema,- codeMode,chat,chatAdapterSlack,chatAdapterStateMemory,@@ -94,6 +93,7 @@ export const MODULES = [picocolors,provider,providerUtils,+ run,semver,shadcnRegistry,standardSchemaSpec,diff --git a/packages/eve/scripts/vendor-compiled/run.mjs b/packages/eve/scripts/vendor-compiled/run.mjsnew file mode 100644index 000000000..7b53f6203--- /dev/null+++ b/packages/eve/scripts/vendor-compiled/run.mjs@@ -0,0 +1,10 @@+import { createDeclarationCopier } from "./_shared.mjs";++// run embeds its QuickJS WebAssembly and worker source as strings, so the bundle+// needs no assets. Standalone keeps that payload out of eve's shared chunks.+export default {+ packageName: "run",+ compiledPath: "run",+ bundling: "standalone",+ copyDeclarations: createDeclarationCopier(),+};diff --git a/packages/eve/src/execution/dynamic-workflow/program-step.scenario.test.ts b/packages/eve/src/execution/dynamic-workflow/program-step.scenario.test.tsindex 67b372b95..1a06e8231 100644--- a/packages/eve/src/execution/dynamic-workflow/program-step.scenario.test.ts+++ b/packages/eve/src/execution/dynamic-workflow/program-step.scenario.test.ts@@ -1,10 +1,9 @@import { describe, expect, it, vi } from "vitest";import { runWorkflowProgramStep } from "#execution/dynamic-workflow/program-step.js";-import {- readWorkflowProgramAgentCall,- readWorkflowProgramCallInterrupt,- type WorkflowProgramInput,+import type {+ WorkflowProgramCallResult,+ WorkflowProgramInput,} from "#execution/dynamic-workflow/schema.js";import { runJsProgram } from "#execution/dynamic-workflow/workflow.js";@@ -21,24 +20,123 @@ function program(js: string, maxSubagents = 10): WorkflowProgramInput {};}+/** Resumes a parked program with one result per pending call, in pending order. */+function resume(+ outcome: Awaited<ReturnType<typeof runWorkflowProgramStep>>,+ values: readonly WorkflowProgramCallResult[],+) {+ if (outcome.status !== "interrupted") throw new Error("Expected pending agent calls.");+ return {+ continuation: outcome.continuation,+ resolutions: outcome.pending.map((pending, index) => ({+ interruptionId: pending.id,+ value: values[index]!,+ })),+ };+}+describe("workflow program sandbox", () => {- it("returns pure JSON and reports syntax failures", async () => {+ it("converts the result as JSON.stringify does and reports syntax failures", async () => {await expect(runWorkflowProgramStep({callId: "pure",- program: program('return { answer: 42, nested: ["ok"] };'),+ program: program("return { answer: 42, at: new Date(0), skipped: undefined };"),}),).resolves.toEqual({- output: { answer: 42, nested: ["ok"] },+ output: { answer: 42, at: "1970-01-01T00:00:00.000Z" },status: "completed",});+ await expect(+ runWorkflowProgramStep({ callId: "no-return", program: program("const unused = 1;") }),+ ).resolves.toEqual({ output: null, status: "completed" });await expect(runWorkflowProgramStep({ callId: "syntax", program: program("return (") }),- ).rejects.toThrow();+ ).rejects.toThrow("unexpected token in expression: '}'");+ });++ it("measures values whose encoding run escapes six-fold by their JSON", async () => {+ // run encodes each `<` as `\u003C`; the quotes bring the JSON text to the limit.+ const atLimit = await runWorkflowProgramStep({+ callId: "escaped-result",+ program: program('return "<".repeat(1_048_574);'),+ });+ expect(atLimit.status === "completed" && (atLimit.output as string).length).toBe(1_048_574);+ await expect(+ runWorkflowProgramStep({+ callId: "escaped-result-over",+ program: program('return "<".repeat(1_048_575);'),+ }),+ ).rejects.toThrow(+ "Workflow program result is 1048577 bytes of JSON, over the 1048576 byte limit.",+ );++ // `{"target":"researcher","input":{"message":""}}` adds 46 bytes to the message.+ const parked = await runWorkflowProgramStep({+ callId: "escaped-input",+ program: program('return ctx.agent("researcher", { message: "<".repeat(1_048_530) });'),+ });+ expect(parked.status === "interrupted" && parked.pending[0]!.call.input.message.length).toBe(+ 1_048_530,+ );+ await expect(+ runWorkflowProgramStep({+ callId: "escaped-input-over",+ program: program(`+try {+ return await ctx.agent("researcher", { message: "<".repeat(1_048_531) });+} catch (error) {+ return { code: error.code, message: error.message };+}`),+ }),+ ).resolves.toEqual({+ output: {+ code: "WORKFLOW_PROGRAM_AGENT_ERROR",+ message:+ "Workflow program ctx.agent() input is 1048577 bytes of JSON, over the 1048576 byte limit.",+ },+ status: "completed",+ });+ });++ it("resumes with a child reply of up to 4 MiB of JSON", async () => {+ // run encodes this 4 MiB of JSON as about 24 MiB.+ const reply = "<".repeat(4 * 1024 * 1024 - 2);+ const input = program(+ 'const reply = await ctx.agent("researcher", { message: "m" }); return reply.length;',+ );+ const first = await runWorkflowProgramStep({ callId: "large-reply", program: input });+ await expect(+ runWorkflowProgramStep({+ callId: "large-reply",+ program: input,+ resume: resume(first, [{ status: "completed", output: reply }]),+ }),+ ).resolves.toEqual({ output: reply.length, status: "completed" });+ });++ it("converts a shared-reference result to JSON inside the sandbox", async () => {+ // The sandbox holds 18 objects; the host only receives their 1.7 MB of JSON text.+ await expect(+ runWorkflowProgramStep({+ callId: "dag-result",+ program: program("let o = {}; for (let i = 0; i < 17; i++) o = { a: o, b: o }; return o;"),+ }),+ ).rejects.toThrow(+ "Workflow program result is 1703925 bytes of JSON, over the 1048576 byte limit.",+ );+ });++ it("rejects values that bypass the program prelude", async () => {+ await expect(+ runWorkflowProgramStep({+ callId: "bypass-result",+ program: program(`})) || { raw: true }; ((async () => {`),+ }),+ ).rejects.toThrow("Workflow program result must be JSON text.");});- it("continues parallel calls in request order and then runs a dependent call", async () => {+ it("resumes a parallel batch together and then runs a dependent call", async () => {const input = program(`const pair = await Promise.all([ctx.agent("researcher", { message: "alpha" }),@@ -48,11 +146,7 @@ return ctx.agent("researcher", { message: pair.join("|") });`);const first = await runWorkflowProgramStep({ callId: "dependent", program: input });expect(first.status).toBe("interrupted");if (first.status !== "interrupted") throw new Error("Expected initial calls.");- expect(- first.pending.map((entry) =>- readWorkflowProgramAgentCall(readWorkflowProgramCallInterrupt(entry).toolInput),- ),- ).toEqual([+ expect(first.pending.map((entry) => entry.call)).toEqual([{ input: { message: "alpha" }, target: "researcher" },{ input: { message: "beta" }, target: "reviewer" },]);@@ -60,53 +154,94 @@ return ctx.agent("researcher", { message: pair.join("|") });`);const second = await runWorkflowProgramStep({callId: "dependent",program: input,- resume: {- interrupt: first.interrupt,- resolutions: [- { status: "completed", output: "A" },- { status: "completed", output: "B" },- ],- },+ resume: resume(first, [+ { status: "completed", output: "A" },+ { status: "completed", output: "B" },+ ]),});expect(second.status).toBe("interrupted");if (second.status !== "interrupted") throw new Error("Expected dependent call.");- expect(- readWorkflowProgramAgentCall(readWorkflowProgramCallInterrupt(second.pending[0]!).toolInput),- ).toEqual({ input: { message: "A|B" }, target: "researcher" });+ expect(second.pending.map((entry) => entry.call)).toEqual([+ { input: { message: "A|B" }, target: "researcher" },+ ]);await expect(runWorkflowProgramStep({callId: "dependent",program: input,- resume: {- interrupt: second.interrupt,- resolutions: [{ status: "completed", output: { done: true } }],- },+ resume: resume(second, [{ status: "completed", output: { done: true } }]),}),).resolves.toEqual({ output: { done: true }, status: "completed" });});+ it("fails a program that returns before every ctx.agent call settles", async () => {+ const input = program(`+return Promise.race([+ ctx.agent("researcher", { message: "a" }),+ ctx.agent("reviewer", { message: "b" }),+]);`);+ const first = await runWorkflowProgramStep({ callId: "race", program: input });+ // The losing call is still pending when the race settles on resume.+ await expect(+ runWorkflowProgramStep({+ callId: "race",+ program: input,+ resume: resume(first, [+ { status: "completed", output: "A" },+ { status: "completed", output: "B" },+ ]),+ }),+ ).rejects.toThrow(+ "Workflow program returned while a ctx.agent() call was still pending. Await every ctx.agent() call before returning, including calls passed to Promise.race or Promise.any.",+ );+ });+it("lets generated code catch a failed child", async () => {const input = program(`try {await ctx.agent("researcher", { message: "fail" });return "unexpected";} catch (error) {- return { caught: error.message };+ return { code: error.code, message: error.message, name: error.name };}`);const first = await runWorkflowProgramStep({ callId: "failure", program: input });- if (first.status !== "interrupted") throw new Error("Expected agent call.");await expect(runWorkflowProgramStep({callId: "failure",program: input,- resume: {- interrupt: first.interrupt,- resolutions: [{ status: "failed", error: "child failed" }],- },+ resume: resume(first, [{ status: "failed", error: "child failed" }]),}),- ).resolves.toEqual({ output: { caught: "child failed" }, status: "completed" });+ ).resolves.toEqual({+ output: {+ code: "WORKFLOW_PROGRAM_AGENT_ERROR",+ message: "child failed",+ name: "WorkflowProgramAgentError",+ },+ status: "completed",+ });+ });++ it("lets generated code catch a malformed agent call without parking it", async () => {+ await expect(+ runWorkflowProgramStep({+ callId: "malformed",+ program: program(`+try {+ await ctx.agent("researcher", { prompt: "no message" });+ return "unexpected";+} catch (error) {+ return { code: error.code, message: error.message, name: error.name };+}`),+ }),+ ).resolves.toEqual({+ output: {+ code: "WORKFLOW_PROGRAM_AGENT_ERROR",+ message: 'Workflow program ctx.agent() requires a "message" string.',+ name: "WorkflowProgramAgentError",+ },+ status: "completed",+ });});it("enforces the total call budget through the real sandbox", async () => {@@ -115,26 +250,22 @@ try {result: async () => ({ data: undefined, message: "ok", status: "waiting" }),}),}));- const ctx = {- abortSignal: new AbortController().signal,- agent,- callId: "policy",- } as never;-- await expect(- runJsProgram(- `+ const ctx = { abortSignal: new AbortController().signal, agent, callId: "policy" } as never;+ const js = `+await ctx.agent("researcher", { message: "one" });try {- await ctx.agent("researcher", { message: "one" });await ctx.agent("researcher", { message: "two" });return "unexpected";} catch (error) {- return error.message;-}`,- ctx,- { maxSubagents: 1 },- ),- ).resolves.toContain("WORKFLOW_PROGRAM_SUBAGENT_LIMIT_REACHED");+ return { code: error.code, message: error.message, name: error.name };+}`;++ await expect(runJsProgram(js, ctx, { maxSubagents: 1 })).resolves.toEqual({+ code: "WORKFLOW_PROGRAM_AGENT_ERROR",+ message:+ 'WORKFLOW_PROGRAM_SUBAGENT_LIMIT_REACHED: workflow may invoke at most 1 agents; "researcher" was not called.',+ name: "WorkflowProgramAgentError",+ });expect(agent).toHaveBeenCalledTimes(1);});@@ -144,23 +275,42 @@ try {callId: "large",program: program(`return ${JSON.stringify("x".repeat(300 * 1024))};`),}),- ).rejects.toThrow(/source|large|bytes/i);+ ).rejects.toThrow("Workflow program source is 307210 bytes, over the 262144 byte limit.");const input = program('return ctx.agent("researcher", { message: "x" });');const first = await runWorkflowProgramStep({ callId: "tamper", program: input });- if (first.status !== "interrupted") throw new Error("Expected agent call.");- const tampered = structuredClone(first.interrupt);- tampered.continuation.auth.signature = `${tampered.continuation.auth.signature}x`;+ const valid = resume(first, [{ status: "completed", output: "no" }]);+ const middle = Math.floor(valid.continuation.length / 2);+ const flipped = valid.continuation[middle] === "A" ? "B" : "A";+ const tampered = `${valid.continuation.slice(0, middle)}${flipped}${valid.continuation.slice(middle + 1)}`;await expect(runWorkflowProgramStep({callId: "tamper",program: input,- resume: {- interrupt: tampered,- resolutions: [{ status: "completed", output: "no" }],+ resume: { ...valid, continuation: tampered },+ }),+ ).rejects.toThrow("Continuation signature is invalid.");+ await expect(+ runWorkflowProgramStep({+ callId: "tamper",+ program: {+ ...input,+ continuationSecurity: {+ ...continuationSecurity,+ signingKey: "another-workflow-program-signing-key",+ },},+ resume: valid,}),- ).rejects.toThrow();+ ).rejects.toThrow("Continuation signature is invalid.");+ await expect(+ runWorkflowProgramStep({ callId: "another-call", program: input, resume: valid }),+ ).rejects.toThrow(+ "Continuation does not match the supplied source, scope, or runtime version.",+ );+ await expect(+ runWorkflowProgramStep({ callId: "tamper", program: input, resume: valid }),+ ).resolves.toEqual({ output: "no", status: "completed" });});});diff --git a/packages/eve/src/execution/dynamic-workflow/program-step.test.ts b/packages/eve/src/execution/dynamic-workflow/program-step.test.tsindex 68e877654..f355dd5e7 100644--- a/packages/eve/src/execution/dynamic-workflow/program-step.test.ts+++ b/packages/eve/src/execution/dynamic-workflow/program-step.test.ts@@ -1,93 +1,47 @@import { beforeEach, describe, expect, it, vi } from "vitest";-const mocks = vi.hoisted(() => ({- continueWorkflowSandboxInterrupt: vi.fn(),- createParkingHostTool: vi.fn((input) => input),- createWorkflowSandboxTool: vi.fn(),- getWorkflowSandboxPendingInterrupts: vi.fn(),- unwrapWorkflowSandboxResult: vi.fn(),+import { FatalError, RetryableError } from "#compiled/@workflow/core/index.js";+import { RunConcurrencyError, RunCpuTimeoutError, RunError } from "#compiled/run/index.js";++const run = vi.fn();+vi.mock("#shared/workflow-sandbox.js", () => ({+ loadWorkflowSandboxModule: async () => ({+ RunError,+ createRunner: () => ({ run }),+ createSignedContinuationCodec: () => ({}),+ getHostFunctionContext: () => ({}),+ }),}));-vi.mock("#shared/workflow-sandbox.js", () => mocks);-import { runWorkflowProgramStep } from "#execution/dynamic-workflow/program-step.js";-import {- WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,- WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,- readWorkflowProgramCallInterrupt,- type WorkflowProgramInput,-} from "#execution/dynamic-workflow/schema.js";--const program: WorkflowProgramInput = {- continuationSecurity: { signingKey: "key" },- js: "return ctx.agent('researcher', { message: 'x' });",- maxSubagents: 3,-};-const callPayload = {- kind: WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,- task: undefined,- toolInput: { input: { message: "x" }, target: "researcher" },- toolName: "agent" as const,-};-const interrupt = { payload: callPayload } as never;+const step = () =>+ runWorkflowProgramStep({+ callId: "call",+ program: { continuationSecurity: { signingKey: "key" }, js: "return 1;", maxSubagents: 1 },+ });-describe("runWorkflowProgramStep", () => {+describe("runWorkflowProgramStep failures", () => {beforeEach(() => vi.resetAllMocks());- it("exposes one restricted agent bridge and returns pending calls", async () => {- const execute = vi.fn().mockResolvedValue("parked");- mocks.createWorkflowSandboxTool.mockResolvedValue({ execute });- mocks.unwrapWorkflowSandboxResult.mockResolvedValue({ interrupt, status: "interrupted" });- mocks.getWorkflowSandboxPendingInterrupts.mockReturnValue([interrupt]);-- await expect(runWorkflowProgramStep({ callId: "call", program })).resolves.toEqual({- interrupt,- pending: [interrupt],- status: "interrupted",- });- expect(mocks.createWorkflowSandboxTool).toHaveBeenCalledWith(- expect.objectContaining({- bridgeRequestLimit: WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,- continuationSecurity: program.continuationSecurity,- hostTools: { agent: expect.anything() },- }),- );- expect(mocks.createParkingHostTool).toHaveBeenCalledTimes(1);- expect(execute).toHaveBeenCalledWith(- {- js: expect.stringContaining("agent: (target, input) => tools.agent({ target, input })"),- },- { toolCallId: "call" },- );- });-- it("returns JSON output after ordered continuation resolutions", async () => {- mocks.continueWorkflowSandboxInterrupt.mockResolvedValue("complete");- mocks.unwrapWorkflowSandboxResult.mockResolvedValue({- output: { ok: true },- status: "completed",- });+ it("retries when run has no free worker", async () => {+ run.mockRejectedValue(new RunConcurrencyError(4));- await expect(- runWorkflowProgramStep({- callId: "call",- program,- resume: {- interrupt,- resolutions: [{ status: "completed", output: { answer: 1 } }],- },- }),- ).resolves.toEqual({ output: { ok: true }, status: "completed" });- expect(mocks.continueWorkflowSandboxInterrupt).toHaveBeenCalledWith(- expect.objectContaining({ bridgeRequestLimit: WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT }),+ const error = await step().catch((caught: unknown) => caught);+ expect(RetryableError.is(error)).toBe(true);+ expect(FatalError.is(error)).toBe(false);+ expect((error as RetryableError).message).toBe(+ "JavaScript runtime maxWorkers limit reached (4).",);+ expect((error as RetryableError).retryAfter.getTime()).toBeGreaterThan(Date.now() + 4_000);});- it("validates interrupt payloads", () => {- expect(readWorkflowProgramCallInterrupt(interrupt)).toEqual(callPayload);- expect(() => readWorkflowProgramCallInterrupt({ payload: { kind: "other" } } as never)).toThrow(- 'Unsupported workflow program interrupt kind "other"',+ it("keeps other run failures fatal", async () => {+ run.mockRejectedValue(new RunCpuTimeoutError(30_000));+ const cpu = await step().catch((caught: unknown) => caught);+ expect(FatalError.is(cpu)).toBe(true);+ expect((cpu as FatalError).message).toBe(+ "JavaScript runtime exceeded its CPU time limit of 30000ms.",);});});diff --git a/packages/eve/src/execution/dynamic-workflow/program-step.ts b/packages/eve/src/execution/dynamic-workflow/program-step.tsindex 00dbf949f..51b4ac404 100644--- a/packages/eve/src/execution/dynamic-workflow/program-step.ts+++ b/packages/eve/src/execution/dynamic-workflow/program-step.ts@@ -1,30 +1,24 @@-import { jsonSchema, type ToolSet } from "ai";--import { FatalError } from "#compiled/@workflow/core/index.js";+import { FatalError, RetryableError } from "#compiled/@workflow/core/index.js";+import type { RunLimits } from "#compiled/run/index.js";import {WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,- WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,- parseWorkflowProgramOutput,- readWorkflowProgramCallInterrupt,- type WorkflowProgramCallInterrupt,+ WORKFLOW_PROGRAM_MAX_AGENT_OUTPUT_BYTES,+ readWorkflowProgramAgentCall,+ type WorkflowProgramAgentCall,+ type WorkflowProgramCallResult,type WorkflowProgramInput,+ type WorkflowProgramPendingCall,+ type WorkflowProgramResolution,} from "#execution/dynamic-workflow/schema.js";import { toErrorMessage } from "#shared/errors.js";-import {- continueWorkflowSandboxInterrupt,- createParkingHostTool,- createWorkflowSandboxTool,- getWorkflowSandboxPendingInterrupts,- unwrapWorkflowSandboxResult,- type WorkflowSandboxContinuationSecurity,- type WorkflowSandboxInterrupt,-} from "#shared/workflow-sandbox.js";+import { parseJsonValue, type JsonValue } from "#shared/json.js";+import { loadWorkflowSandboxModule } from "#shared/workflow-sandbox.js";export type WorkflowProgramStepOutcome =- | { readonly output: ReturnType<typeof parseWorkflowProgramOutput>; readonly status: "completed" }+ | { readonly output: JsonValue; readonly status: "completed" }| {- readonly interrupt: WorkflowSandboxInterrupt;- readonly pending: readonly WorkflowSandboxInterrupt[];+ readonly continuation: string;+ readonly pending: readonly WorkflowProgramPendingCall[];readonly status: "interrupted";};@@ -32,28 +26,55 @@ interface WorkflowProgramStepInput {readonly callId: string;readonly program: WorkflowProgramInput;readonly resume?: {- readonly interrupt: WorkflowSandboxInterrupt;- readonly resolutions: readonly unknown[];+ readonly continuation: string;+ readonly resolutions: readonly WorkflowProgramResolution[];};}-const agentBridgeSchema = jsonSchema({- additionalProperties: false,- properties: {- input: {- additionalProperties: false,- properties: {- message: { type: "string" },- outputSchema: { type: "object" },- },- required: ["message"],- type: "object",- },- target: { type: "string" },- },- required: ["target", "input"],- type: "object",-});+const WORKFLOW_PROGRAM_CONTINUATION_AUDIENCE = "eve.workflow-program/v1";+const WORKFLOW_PROGRAM_HOST_NAMESPACE = "__eveWorkflowProgram";+/** Size limit for the JSON text of a program's result and of each `ctx.agent()` call. */+const WORKFLOW_PROGRAM_MAX_JSON_BYTES = 1024 * 1024;+const WORKFLOW_PROGRAM_MAX_SOURCE_BYTES = 256 * 1024;+/** Guest JavaScript time per run or resume; parked agent calls do not count. */+const WORKFLOW_PROGRAM_CPU_TIMEOUT_MS = 30_000;+/** Wall-clock time per run or resume; parked agent calls do not count. */+const WORKFLOW_PROGRAM_TIMEOUT_MS = 300_000;++/**+ * Converts the program's values to JSON inside the sandbox, where run's limits+ * bound the work. run transfers values with shared references intact, so a+ * small value can expand without limit if the host converts it. The prelude+ * shares the program's first line, so stack line numbers match its source.+ */+function wrapWorkflowProgram(js: string): string {+ return `const ctx = Object.freeze({ agent: (target, input) => ${WORKFLOW_PROGRAM_HOST_NAMESPACE}.agent(JSON.stringify({ target, input })) }); return JSON.stringify(await (async () => {${js}\n})());`;+}++/**+ * run measures values in its own encoding, which can turn one byte of JSON+ * into six (`<` becomes `\u003C`), so run gets the worst-case budget and eve's+ * JSON size checks always fail first, with eve's message.+ */+const runEncodingLimit = (jsonBytes: number) => jsonBytes * 6 + 1024;++/** Gives programs holding the busy workers time to finish before the step retries. */+const WORKFLOW_PROGRAM_BUSY_RETRY_MS = 5_000;++const WORKFLOW_PROGRAM_LIMITS: RunLimits = {+ cpuTimeoutMs: WORKFLOW_PROGRAM_CPU_TIMEOUT_MS,+ maxBridgeRequests: WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,+ maxConsoleOutputBytes: 64 * 1024,+ maxHostFunctionArgumentsBytes: runEncodingLimit(WORKFLOW_PROGRAM_MAX_JSON_BYTES),+ maxHostFunctionOutputBytes: runEncodingLimit(WORKFLOW_PROGRAM_MAX_AGENT_OUTPUT_BYTES),+ maxInFlightBridgeRequests: WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,+ maxResultBytes: runEncodingLimit(WORKFLOW_PROGRAM_MAX_JSON_BYTES),+ // eve checks the program's own size; the ASCII wrapper's length is its byte count.+ maxSourceBytes: WORKFLOW_PROGRAM_MAX_SOURCE_BYTES + wrapWorkflowProgram("").length,+ maxStackSizeBytes: 2 * 1024 * 1024,+ memoryLimitBytes: 64 * 1024 * 1024,+ timeoutMs: WORKFLOW_PROGRAM_TIMEOUT_MS,+};/** Runs or resumes the side-effect-free JavaScript sandbox for one durable workflow. */export async function runWorkflowProgramStep(@@ -64,7 +85,9 @@ export async function runWorkflowProgramStep(try {return await runWorkflowProgram(input);} catch (error) {- // The sandbox is deterministic, so a failed program fails the same way on every retry.+ // The step has no side effects, so retrying a busy runtime is safe.+ if (RetryableError.is(error)) throw error;+ // The sandbox is deterministic, so any other failure repeats on every retry.throw new FatalError(toErrorMessage(error));}}@@ -72,69 +95,96 @@ export async function runWorkflowProgramStep(async function runWorkflowProgram(input: WorkflowProgramStepInput,): Promise<WorkflowProgramStepOutcome> {- const tools = buildWorkflowProgramTools();- const security = input.program.continuationSecurity as WorkflowSandboxContinuationSecurity;- let raw: unknown;- if (input.resume === undefined) {- const tool = await createWorkflowSandboxTool({- bridgeRequestLimit: WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,- continuationSecurity: security,- hostTools: tools,- });- if (tool.execute === undefined) throw new Error("Workflow program has no sandbox executor.");- raw = await tool.execute(- { js: wrapWorkflowProgram(input.program.js) } as never,- { toolCallId: input.callId } as never,+ const sourceBytes = Buffer.byteLength(input.program.js);+ if (sourceBytes > WORKFLOW_PROGRAM_MAX_SOURCE_BYTES) {+ throw new RangeError(+ `Workflow program source is ${String(sourceBytes)} bytes, over the ${String(WORKFLOW_PROGRAM_MAX_SOURCE_BYTES)} byte limit.`,);- } else {- let current = input.resume.interrupt;- raw = current;- for (const resolution of input.resume.resolutions) {- raw = await continueWorkflowSandboxInterrupt({- bridgeRequestLimit: WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT,- continuationSecurity: security,- interrupt: current,- resolution,- tools,- });- const unwrapped = await unwrapWorkflowSandboxResult(raw, security);- if (unwrapped.status === "completed") break;- const next = getWorkflowSandboxPendingInterrupts(unwrapped.interrupt)[0];- if (next === undefined) {- throw new Error("Workflow program continuation contains no pending agent call.");- }- current = next;- }}+ const { createRunner, createSignedContinuationCodec, getHostFunctionContext, RunError } =+ await loadWorkflowSandboxModule();+ const { maxAgeMs, signingKey } = input.program.continuationSecurity;+ const runner = createRunner({+ continuationAudience: WORKFLOW_PROGRAM_CONTINUATION_AUDIENCE,+ continuationCodec: createSignedContinuationCodec({ maxAgeMs, secret: signingKey }),+ });- const unwrapped = await unwrapWorkflowSandboxResult(raw, security);- if (unwrapped.status === "completed") {- return { output: parseWorkflowProgramOutput(unwrapped.output), status: "completed" };- }- const pending = getWorkflowSandboxPendingInterrupts(unwrapped.interrupt);- if (pending.length === 0) {- throw new Error("Workflow program continuation contains no pending agent call.");- }- for (const interrupt of pending) readWorkflowProgramCallInterrupt(interrupt);- return { interrupt: pending[0]!, pending, status: "interrupted" };-}+ // run shows the program only RunError messages; other host errors read "Host function failed."+ const guestError = (message: string) => {+ const error = new RunError(message, "WORKFLOW_PROGRAM_AGENT_ERROR");+ // RunError takes its name from its class, which the minified vendored bundle renames.+ error.name = "WorkflowProgramAgentError";+ return error;+ };++ const running = runner.run({+ source: wrapWorkflowProgram(input.program.js),+ hostFunctions: {+ [WORKFLOW_PROGRAM_HOST_NAMESPACE]: {+ agent: async (callJson: unknown) => {+ let call: WorkflowProgramAgentCall;+ try {+ call = readWorkflowProgramAgentCall(+ parseWorkflowProgramJson(callJson, "ctx.agent() input"),+ );+ } catch (error) {+ throw guestError(toErrorMessage(error));+ }+ const { interrupt, resume } = getHostFunctionContext();+ if (resume === undefined) return interrupt(call);+ const outcome = resume.resolution as WorkflowProgramCallResult;+ if (outcome.status === "failed") throw guestError(outcome.error);+ return outcome.output;+ },+ },+ },+ limits: WORKFLOW_PROGRAM_LIMITS,+ continuationContext: { callId: input.callId },+ continuation: input.resume?.continuation,+ resolutions: input.resume?.resolutions.slice(),+ });+ const result = await running.catch((error: unknown) => {+ if (!RunError.isInstance(error)) throw error;+ // run has no free worker or worker memory in this process; another attempt can succeed.+ if (error.code === "RUN_CONCURRENCY_LIMIT") {+ throw new RetryableError(error.message, { retryAfter: WORKFLOW_PROGRAM_BUSY_RETRY_MS });+ }+ if (error.code === "RUN_DETACHED_BRIDGE_REQUEST") {+ throw new Error(+ "Workflow program returned while a ctx.agent() call was still pending. Await every ctx.agent() call before returning, including calls passed to Promise.race or Promise.any.",+ );+ }+ throw error;+ });-function buildWorkflowProgramTools(): ToolSet {+ if (result.status === "completed") {+ // `JSON.stringify` returns undefined for values it drops, such as `undefined` itself.+ const output =+ result.value === undefined+ ? null+ : parseJsonValue(parseWorkflowProgramJson(result.value, "result"));+ return { output, status: "completed" };+ }return {- agent: createParkingHostTool({- description: "Invoke one allowlisted child agent.",- inputSchema: agentBridgeSchema,- interrupt: (toolInput) =>- ({- kind: WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,- task: undefined,- toolInput,- toolName: "agent",- }) satisfies WorkflowProgramCallInterrupt,- }),- } as ToolSet;+ continuation: result.continuation,+ pending: result.interruptions.map((interruption) => ({+ call: readWorkflowProgramAgentCall(interruption.payload),+ id: interruption.id,+ })),+ status: "interrupted",+ };}-function wrapWorkflowProgram(source: string): string {- return `return await (async (ctx) => {\n${source}\n})(Object.freeze({\n agent: (target, input) => tools.agent({ target, input }),\n}));`;+/** Parses JSON text from the prelude; code that bypasses the prelude can send any value. */+function parseWorkflowProgramJson(value: unknown, label: string): unknown {+ if (typeof value !== "string") {+ throw new TypeError(`Workflow program ${label} must be JSON text.`);+ }+ const bytes = Buffer.byteLength(value);+ if (bytes > WORKFLOW_PROGRAM_MAX_JSON_BYTES) {+ throw new RangeError(+ `Workflow program ${label} is ${String(bytes)} bytes of JSON, over the ${String(WORKFLOW_PROGRAM_MAX_JSON_BYTES)} byte limit.`,+ );+ }+ return JSON.parse(value);}diff --git a/packages/eve/src/execution/dynamic-workflow/schema.ts b/packages/eve/src/execution/dynamic-workflow/schema.tsindex 123c7ac96..0040ddae7 100644--- a/packages/eve/src/execution/dynamic-workflow/schema.ts+++ b/packages/eve/src/execution/dynamic-workflow/schema.ts@@ -1,16 +1,10 @@-import { parseJsonObject, parseJsonValue, type JsonObject, type JsonValue } from "#shared/json.js";+import { parseJsonObject, type JsonObject, type JsonValue } from "#shared/json.js";export const DEFAULT_WORKFLOW_PROGRAM_MAX_SUBAGENTS = 100;export const MAX_WORKFLOW_PROGRAM_MAX_SUBAGENTS = 128;export const WORKFLOW_PROGRAM_BRIDGE_REQUEST_LIMIT = 256;-export const WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND = "eve.workflow-program-agent-call";--export interface WorkflowProgramCallInterrupt {- readonly kind: typeof WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND;- readonly task: undefined;- readonly toolInput: unknown;- readonly toolName: "agent";-}+/** Size limit for the JSON text of each child reply a `ctx.agent()` call resolves to. */+export const WORKFLOW_PROGRAM_MAX_AGENT_OUTPUT_BYTES = 4 * 1024 * 1024;export interface WorkflowProgramContinuationSecurity {readonly maxAgeMs?: number;@@ -34,6 +28,21 @@ export interface WorkflowProgramAgentCall {readonly target: string;}+/** One parked `ctx.agent()` call, keyed by its sandbox interruption id. */+export interface WorkflowProgramPendingCall {+ readonly call: WorkflowProgramAgentCall;+ readonly id: string;+}++export type WorkflowProgramCallResult =+ | { readonly output: JsonValue; readonly status: "completed" }+ | { readonly error: string; readonly status: "failed" };++export interface WorkflowProgramResolution {+ readonly interruptionId: string;+ readonly value: WorkflowProgramCallResult;+}+export function parseWorkflowProgramOptions(value: unknown): WorkflowProgramOptions {const input = parseJsonObject(value);if (@@ -47,21 +56,6 @@ export function parseWorkflowProgramOptions(value: unknown): WorkflowProgramOptireturn { maxSubagents: input.maxSubagents };}-export function readWorkflowProgramCallInterrupt(input: {- readonly payload: unknown;-}): WorkflowProgramCallInterrupt {- const payload = input.payload as Partial<WorkflowProgramCallInterrupt>;- if (payload.kind !== WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND || payload.toolName !== "agent") {- throw new Error(`Unsupported workflow program interrupt kind "${String(payload.kind)}".`);- }- return {- kind: WORKFLOW_PROGRAM_CALL_INTERRUPT_KIND,- task: undefined,- toolInput: payload.toolInput,- toolName: "agent",- };-}-export function readWorkflowProgramAgentCall(value: unknown): WorkflowProgramAgentCall {const call = parseJsonObject(value);if (typeof call.target !== "string" || call.target.trim() === "") {@@ -81,10 +75,6 @@ export function readWorkflowProgramAgentCall(value: unknown): WorkflowProgramAgereturn { input, target: call.target };}-export function parseWorkflowProgramOutput(value: unknown): JsonValue {- return parseJsonValue(value ?? null);-}-function isPositiveInteger(value: unknown): value is number {return typeof value === "number" && Number.isSafeInteger(value) && value > 0;}diff --git a/packages/eve/src/execution/dynamic-workflow/workflow.test.ts b/packages/eve/src/execution/dynamic-workflow/workflow.test.tsindex 069cebdb8..9b44f58ad 100644--- a/packages/eve/src/execution/dynamic-workflow/workflow.test.ts+++ b/packages/eve/src/execution/dynamic-workflow/workflow.test.ts@@ -13,13 +13,12 @@ import {runJsProgram,} from "#execution/dynamic-workflow/workflow.js";-const pending = {- payload: {- kind: "eve.workflow-program-agent-call",- toolInput: { input: { message: "one" }, target: "researcher" },- toolName: "agent",- },-} as never;+const pending = { call: { input: { message: "one" }, target: "researcher" }, id: "interrupt-1" };+const secondPending = {+ call: { input: { message: "two" }, target: "researcher" },+ id: "interrupt-2",+};+const continuation = "signed-continuation";/** Each child's reply to the one message a program sends it. */const reply = vi.fn();const agent = (target: string) => ({@@ -43,7 +42,7 @@ describe("runJsProgram", () => {it("delegates agent interrupts and resumes the sandbox", async () => {mocks.runWorkflowProgramStep- .mockResolvedValueOnce({ interrupt: pending, pending: [pending], status: "interrupted" })+ .mockResolvedValueOnce({ continuation, pending: [pending], status: "interrupted" }).mockResolvedValueOnce({ output: { result: "done" }, status: "completed" });reply.mockResolvedValue({ result: "child" });@@ -52,26 +51,24 @@ describe("runJsProgram", () => {expect(mocks.runWorkflowProgramStep).toHaveBeenLastCalledWith(expect.objectContaining({resume: {- interrupt: pending,- resolutions: [{ status: "completed", output: { result: "child" } }],+ continuation,+ resolutions: [+ {+ interruptionId: "interrupt-1",+ value: { status: "completed", output: { result: "child" } },+ },+ ],},}),);});it("keeps a pending batch in flight until every child settles", async () => {- const secondPending = {- payload: {- kind: "eve.workflow-program-agent-call",- toolInput: { input: { message: "two" }, target: "researcher" },- toolName: "agent",- },- } as never;const first = Promise.withResolvers<unknown>();const second = Promise.withResolvers<unknown>();mocks.runWorkflowProgramStep.mockResolvedValueOnce({- interrupt: pending,+ continuation,pending: [pending, secondPending],status: "interrupted",})@@ -89,11 +86,22 @@ describe("runJsProgram", () => {await expect(result).resolves.toEqual(["one", "two"]);expect(mocks.runWorkflowProgramStep).toHaveBeenCalledTimes(2);+ expect(mocks.runWorkflowProgramStep).toHaveBeenLastCalledWith(+ expect.objectContaining({+ resume: {+ continuation,+ resolutions: [+ { interruptionId: "interrupt-1", value: { status: "completed", output: "one" } },+ { interruptionId: "interrupt-2", value: { status: "completed", output: "two" } },+ ],+ },+ }),+ );});it("returns catchable failures for failed children", async () => {mocks.runWorkflowProgramStep- .mockResolvedValueOnce({ interrupt: pending, pending: [pending], status: "interrupted" })+ .mockResolvedValueOnce({ continuation, pending: [pending], status: "interrupted" }).mockResolvedValueOnce({ output: "caught", status: "completed" });reply.mockRejectedValue(new Error("child failed"));@@ -101,8 +109,10 @@ describe("runJsProgram", () => {expect(mocks.runWorkflowProgramStep).toHaveBeenLastCalledWith(expect.objectContaining({resume: {- interrupt: pending,- resolutions: [{ status: "failed", error: "child failed" }],+ continuation,+ resolutions: [+ { interruptionId: "interrupt-1", value: { status: "failed", error: "child failed" } },+ ],},}),);@@ -110,10 +120,10 @@ describe("runJsProgram", () => {it("bounds total calls across resumed batches", async () => {mocks.runWorkflowProgramStep- .mockResolvedValueOnce({ interrupt: pending, pending: [pending], status: "interrupted" })+ .mockResolvedValueOnce({ continuation, pending: [pending], status: "interrupted" }).mockResolvedValueOnce({- interrupt: pending,- pending: [pending, pending],+ continuation,+ pending: [pending, secondPending],status: "interrupted",}).mockResolvedValueOnce({ output: "limited", status: "completed" });@@ -125,17 +135,50 @@ describe("runJsProgram", () => {expect.objectContaining({resume: expect.objectContaining({resolutions: [- { status: "completed", output: "two" },- expect.objectContaining({- status: "failed",- error: expect.stringContaining("WORKFLOW_PROGRAM_SUBAGENT_LIMIT_REACHED"),- }),+ { interruptionId: "interrupt-1", value: { status: "completed", output: "two" } },+ {+ interruptionId: "interrupt-2",+ value: {+ status: "failed",+ error: expect.stringContaining("WORKFLOW_PROGRAM_SUBAGENT_LIMIT_REACHED"),+ },+ },],}),}),);});+ it("returns a catchable failure for a child reply over 4 MiB of JSON", async () => {+ // Quotes make each JSON text 4 MiB and 4 MiB + 1 byte.+ const atLimit = "<".repeat(4 * 1024 * 1024 - 2);+ mocks.runWorkflowProgramStep+ .mockResolvedValueOnce({+ continuation,+ pending: [pending, secondPending],+ status: "interrupted",+ })+ .mockResolvedValueOnce({ output: "caught", status: "completed" });+ reply.mockImplementation((_target, message: string) =>+ message === "one" ? atLimit : `${atLimit}<`,+ );++ await expect(runJsProgram("return 1", ctx, options)).resolves.toBe("caught");+ const { resolutions } = mocks.runWorkflowProgramStep.mock.lastCall![0].resume;+ expect(resolutions[0]).toEqual({+ interruptionId: "interrupt-1",+ value: { status: "completed", output: atLimit },+ });+ expect(resolutions[1]).toEqual({+ interruptionId: "interrupt-2",+ value: {+ status: "failed",+ error:+ 'Agent "researcher" replied with 4194305 bytes of JSON, over the 4194304 byte limit for a ctx.agent() result.',+ },+ });+ });+it("validates JavaScript and the trusted call budget before starting a program", async () => {await expect(runJsProgram(42 as never, ctx, options)).rejects.toThrow('requires a "js" string');await expect(@@ -154,7 +197,7 @@ describe("runJsProgram", () => {callId: "workflow-call",} as never;mocks.runWorkflowProgramStep.mockResolvedValueOnce({- interrupt: pending,+ continuation,pending: [pending],status: "interrupted",});diff --git a/packages/eve/src/execution/dynamic-workflow/workflow.ts b/packages/eve/src/execution/dynamic-workflow/workflow.tsindex 896865970..ff84caa65 100644--- a/packages/eve/src/execution/dynamic-workflow/workflow.ts+++ b/packages/eve/src/execution/dynamic-workflow/workflow.ts@@ -1,11 +1,11 @@import {DEFAULT_WORKFLOW_PROGRAM_MAX_SUBAGENTS,MAX_WORKFLOW_PROGRAM_MAX_SUBAGENTS,+ WORKFLOW_PROGRAM_MAX_AGENT_OUTPUT_BYTES,parseWorkflowProgramOptions,- readWorkflowProgramAgentCall,- readWorkflowProgramCallInterrupt,type WorkflowProgramAgentCall,type WorkflowProgramInput,+ type WorkflowProgramResolution,} from "#execution/dynamic-workflow/schema.js";import {runWorkflowProgramStep,@@ -42,28 +42,29 @@ export async function runJsProgram(while (outcome.status === "interrupted") {ctx.abortSignal.throwIfAborted();const resolutions = await Promise.all(- outcome.pending.map(async (pending) => {- const interrupt = readWorkflowProgramCallInterrupt(pending);- const call = readWorkflowProgramAgentCall(interrupt.toolInput);+ outcome.pending.map(async ({ call, id }): Promise<WorkflowProgramResolution> => {const invocationIndex = calls++;if (invocationIndex >= program.maxSubagents) {return {- status: "failed" as const,- error: `WORKFLOW_PROGRAM_SUBAGENT_LIMIT_REACHED: workflow may invoke at most ${String(program.maxSubagents)} agents; "${call.target}" was not called.`,+ interruptionId: id,+ value: {+ status: "failed",+ error: `WORKFLOW_PROGRAM_SUBAGENT_LIMIT_REACHED: workflow may invoke at most ${String(program.maxSubagents)} agents; "${call.target}" was not called.`,+ },};}try {const output = await callProgramAgent(ctx, call);- return { status: "completed" as const, output };+ return { interruptionId: id, value: { status: "completed", output } };} catch (error) {ctx.abortSignal.throwIfAborted();- return { status: "failed" as const, error: toErrorMessage(error) };+ return { interruptionId: id, value: { status: "failed", error: toErrorMessage(error) } };}}),);outcome = await runWorkflowProgramStep({...base,- resume: { interrupt: outcome.interrupt, resolutions },+ resume: { continuation: outcome.continuation, resolutions },});}return outcome.output;@@ -84,8 +85,15 @@ async function callProgramAgent(`Agent "${call.target}" failed to handle the message: ${result.error?.message ?? "no reason given"}`,);}- if (outputSchema !== undefined) return result.data ?? null;- return result.message ?? null;+ const output = outputSchema !== undefined ? (result.data ?? null) : (result.message ?? null);+ // Checked here so the program can catch an oversized reply; run would fail the whole workflow.+ const bytes = new TextEncoder().encode(JSON.stringify(output)).byteLength;+ if (bytes > WORKFLOW_PROGRAM_MAX_AGENT_OUTPUT_BYTES) {+ throw new Error(+ `Agent "${call.target}" replied with ${String(bytes)} bytes of JSON, over the ${String(WORKFLOW_PROGRAM_MAX_AGENT_OUTPUT_BYTES)} byte limit for a ctx.agent() result.`,+ );+ }+ return output;}export { MAX_WORKFLOW_PROGRAM_MAX_SUBAGENTS };diff --git a/packages/eve/src/internal/nitro/host/workflow-sandbox-runtime-plugin.ts b/packages/eve/src/internal/nitro/host/workflow-sandbox-runtime-plugin.tsindex 1b4b5c42c..2c76cf800 100644--- a/packages/eve/src/internal/nitro/host/workflow-sandbox-runtime-plugin.ts+++ b/packages/eve/src/internal/nitro/host/workflow-sandbox-runtime-plugin.ts@@ -1,18 +1,16 @@import {- CodeModeToolError,- experimental_continueCodeModeInterrupt as continueCodeModeInterrupt,- experimental_createCodeModeTool as createCodeModeTool,- experimental_requestCodeModeInterrupt as requestCodeModeInterrupt,- experimental_unwrapCodeModeResult as unwrapCodeModeResult,-} from "#compiled/@ai-sdk/code-mode/index.js";+ createRunner,+ createSignedContinuationCodec,+ getHostFunctionContext,+ RunError,+} from "#compiled/run/index.js";import { installWorkflowSandboxModule } from "#shared/workflow-sandbox.js";installWorkflowSandboxModule({- CodeModeToolError,- continueCodeModeInterrupt,- createCodeModeTool,- requestCodeModeInterrupt,- unwrapCodeModeResult,+ createRunner,+ createSignedContinuationCodec,+ getHostFunctionContext,+ RunError,});export default function installWorkflowSandboxRuntimePlugin(): void {}diff --git a/packages/eve/src/shared/workflow-sandbox.ts b/packages/eve/src/shared/workflow-sandbox.tsindex cdd68ca94..37e7db8b5 100644--- a/packages/eve/src/shared/workflow-sandbox.ts+++ b/packages/eve/src/shared/workflow-sandbox.ts@@ -1,166 +1,30 @@-import type { FlexibleSchema, ToolSet } from "ai";-import type * as CodeModeModule from "#compiled/@ai-sdk/code-mode/index.js";+import type * as RunModule from "#compiled/run/index.js";const WORKFLOW_SANDBOX_MODULE_KEY = Symbol.for("eve.workflowSandbox.module");-const WORKFLOW_SANDBOX_MODULE_SPECIFIER = ["#compiled", "@ai-sdk", "code-mode", "index.js"].join(- "/",-);+const WORKFLOW_SANDBOX_MODULE_SPECIFIER = ["#compiled", "run", "index.js"].join("/");-type WorkflowSandboxModule = {- readonly CodeModeToolError: typeof CodeModeModule.CodeModeToolError;- readonly continueCodeModeInterrupt: typeof CodeModeModule.experimental_continueCodeModeInterrupt;- readonly createCodeModeTool: typeof CodeModeModule.experimental_createCodeModeTool;- readonly requestCodeModeInterrupt: typeof CodeModeModule.experimental_requestCodeModeInterrupt;- readonly unwrapCodeModeResult: typeof CodeModeModule.experimental_unwrapCodeModeResult;-};+type WorkflowSandboxModule = Pick<+ typeof RunModule,+ "createRunner" | "createSignedContinuationCodec" | "getHostFunctionContext" | "RunError"+>;type WorkflowSandboxGlobal = typeof globalThis & {[WORKFLOW_SANDBOX_MODULE_KEY]?: WorkflowSandboxModule;};-export type WorkflowSandboxInterrupt = CodeModeModule.CodeModeInterrupt;-export type WorkflowSandboxContinuationSecurity =- CodeModeModule.CodeModeContinuationSecurityOptions;-let workflowSandboxModulePromise: Promise<WorkflowSandboxModule> | undefined;export function installWorkflowSandboxModule(module: WorkflowSandboxModule): void {(globalThis as WorkflowSandboxGlobal)[WORKFLOW_SANDBOX_MODULE_KEY] = module;}-export async function createWorkflowSandboxTool(input: {- readonly bridgeRequestLimit: number;- readonly continuationSecurity: WorkflowSandboxContinuationSecurity;- readonly hostTools: ToolSet;-}): Promise<ToolSet[string]> {- const { createCodeModeTool } = await loadWorkflowSandboxModule();- return createCodeModeTool(- input.hostTools,- createWorkflowSandboxOptions(input.bridgeRequestLimit, input.continuationSecurity),- ) as ToolSet[string];-}--export async function continueWorkflowSandboxInterrupt(input: {- readonly bridgeRequestLimit: number;- readonly continuationSecurity: WorkflowSandboxContinuationSecurity;- readonly interrupt: WorkflowSandboxInterrupt;- readonly resolution: unknown;- readonly tools: ToolSet;-}): Promise<unknown> {- const { continueCodeModeInterrupt } = await loadWorkflowSandboxModule();- return continueCodeModeInterrupt({- interrupt: input.interrupt,- options: createWorkflowSandboxOptions(input.bridgeRequestLimit, input.continuationSecurity),- resolution: input.resolution,- tools: input.tools,- } as never);-}--export async function unwrapWorkflowSandboxResult(- value: unknown,- continuationSecurity: WorkflowSandboxContinuationSecurity,-): Promise<- | { readonly output: unknown; readonly status: "completed" }- | { readonly interrupt: WorkflowSandboxInterrupt; readonly status: "interrupted" }-> {- const { unwrapCodeModeResult } = await loadWorkflowSandboxModule();- return unwrapCodeModeResult(value, continuationSecurity) as- | { readonly output: unknown; readonly status: "completed" }- | { readonly interrupt: WorkflowSandboxInterrupt; readonly status: "interrupted" };-}--type WorkflowSandboxResolution =- | { readonly status: "completed"; readonly output: unknown }- | { readonly status: "failed"; readonly error: string };--/** A host tool that parks on first call and consumes its resolution on replay. */-export function createParkingHostTool(input: {- readonly description: string;- readonly inputSchema: FlexibleSchema;- readonly outputSchema?: FlexibleSchema | null;- readonly interrupt: (toolInput: unknown) => { readonly kind: string } & Record<string, unknown>;-}): ToolSet[string] {- return {- description: input.description,- inputSchema: input.inputSchema,- outputSchema: input.outputSchema ?? undefined,- execute: async (toolInput: unknown, options: unknown) => {- const module = await loadWorkflowSandboxModule();- const resolution = readWorkflowProgramResolution(options);- if (resolution?.status === "failed") throw new module.CodeModeToolError(resolution.error);- if (resolution?.status === "completed") return resolution.output;- return module.requestCodeModeInterrupt(input.interrupt(toolInput));- },- } as ToolSet[string];-}--function readWorkflowProgramResolution(options: unknown): WorkflowSandboxResolution | undefined {- if (typeof options !== "object" || options === null) return undefined;- const interrupt = (options as Record<string, unknown>).codeModeInterrupt;- if (typeof interrupt !== "object" || interrupt === null) return undefined;- const resolution = (interrupt as Record<string, unknown>).resolution;- if (typeof resolution !== "object" || resolution === null) return undefined;- const status = (resolution as Record<string, unknown>).status;- return status === "completed" || status === "failed"- ? (resolution as WorkflowSandboxResolution)- : undefined;-}--/** Reconstructs every unresolved interruption from the authenticated continuation. */-export function getWorkflowSandboxPendingInterrupts(- interrupt: WorkflowSandboxInterrupt,-): WorkflowSandboxInterrupt[] {- const continuation = interrupt.continuation;- const resolved = new Set(- continuation.resolutions.map((resolution) => resolution.runInterruptionId),- );-- return continuation.pendingInterruptions.flatMap((pending) =>- resolved.has(pending.runInterruptionId)- ? []- : [- {- continuation,- input: pending.input,- interruptId: pending.interruptId,- outerToolCallId: continuation.outerToolCallId,- payload: pending.payload,- toolCallId: pending.toolCallId,- toolName: pending.toolName,- type: "code-mode-interrupt" as const,- },- ],- );-}--function createWorkflowSandboxOptions(- bridgeRequestLimit: number,- continuationSecurity: WorkflowSandboxContinuationSecurity,-): CodeModeModule.CodeModeOptions {- return {- continuationSecurity,- executionPolicy: {- maxBridgeRequests: bridgeRequestLimit,- maxInFlightBridgeRequests: bridgeRequestLimit,- },- };-}--async function loadWorkflowSandboxModule(): Promise<WorkflowSandboxModule> {+/** Returns the vendored `run` runtime that executes generated workflow programs. */+export async function loadWorkflowSandboxModule(): Promise<WorkflowSandboxModule> {const installed = (globalThis as WorkflowSandboxGlobal)[WORKFLOW_SANDBOX_MODULE_KEY];if (installed !== undefined) return installed;- workflowSandboxModulePromise ??= importWorkflowSandboxModule(WORKFLOW_SANDBOX_MODULE_SPECIFIER);+ workflowSandboxModulePromise ??= import(WORKFLOW_SANDBOX_MODULE_SPECIFIER) as Promise<+ typeof RunModule+ >;return await workflowSandboxModulePromise;}--async function importWorkflowSandboxModule(specifier: string): Promise<WorkflowSandboxModule> {- const module = (await import(specifier)) as typeof CodeModeModule;- return {- CodeModeToolError: module.CodeModeToolError,- continueCodeModeInterrupt: module.experimental_continueCodeModeInterrupt,- createCodeModeTool: module.experimental_createCodeModeTool,- requestCodeModeInterrupt: module.experimental_requestCodeModeInterrupt,- unwrapCodeModeResult: module.experimental_unwrapCodeModeResult,- };-}diff --git a/packages/eve/src/tools/provided/workflow.test.ts b/packages/eve/src/tools/provided/workflow.test.tsindex 4c4ae0a01..f073048de 100644--- a/packages/eve/src/tools/provided/workflow.test.ts+++ b/packages/eve/src/tools/provided/workflow.test.ts@@ -30,6 +30,7 @@ describe("workflow", () => {expect(definition.description).toContain('return await ctx.agent("researcher", { message: "Describe the task" })',);+ expect(definition.description).toContain("Await every ctx.agent call before returning.");expect(definition.description).not.toContain("Available agents");expect(definition.description).toContain("at most 7 agents");expect(definition.task).toBe(runWorkflowProgramTask);diff --git a/packages/eve/src/tools/provided/workflow.ts b/packages/eve/src/tools/provided/workflow.tsindex e29c82dfb..979e5bd41 100644--- a/packages/eve/src/tools/provided/workflow.ts+++ b/packages/eve/src/tools/provided/workflow.ts@@ -41,6 +41,7 @@ export function workflow(options: WorkflowToolOptions = {}): WorkflowTool {"Run an async JavaScript function body that invokes agents and returns one JSON-serializable value.",workflowProgramAgentContract,'Supply the body directly, for example: return await ctx.agent("researcher", { message: "Describe the task" });',+ "Await every ctx.agent call before returning.",`The program may invoke at most ${String(normalized.maxSubagents)} agents.`,].join(" ");return attachWorkflowProgramOptions(diff --git a/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts b/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.tsindex 81b21b493..695a4ad19 100644--- a/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts+++ b/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts@@ -612,8 +612,6 @@ describe("app runtime dependency tracing", () => {// The generated JavaScript sandbox runtime ships bundled inline, never traced — and// these apps do not declare the optional just-bash engine, so its// quickjs dependency must not sneak into the trace either.- expect(sleepTracedPackageJson.dependencies).not.toHaveProperty("@ai-sdk/code-mode");- expect(workflowTracedPackageJson.dependencies).not.toHaveProperty("@ai-sdk/code-mode");expect(sleepTracedPackageJson.dependencies).not.toHaveProperty("run");expect(workflowTracedPackageJson.dependencies).not.toHaveProperty("run");expect(sleepTracedPackageJson.dependencies).not.toHaveProperty("quickjs-emscripten");diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex f2a63c2b6..06141c1dc 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -192,9 +192,6 @@ catalogs:'@ai-sdk/anthropic':specifier: ^4.0.78version: 4.0.78- '@ai-sdk/code-mode':- specifier: ^1.0.94- version: 1.0.94'@ai-sdk/google':specifier: ^4.0.93version: 4.0.93@@ -1793,9 +1790,6 @@ importers:'@ai-sdk/anthropic':specifier: 'catalog:'version: 4.0.78([email protected])- '@ai-sdk/code-mode':- specifier: 'catalog:''@ai-sdk/google':specifier: 'catalog:'version: 4.0.93([email protected])@@ -1989,8 +1983,8 @@ importers:specifier: 19.2.6version: 19.2.6([email protected])run:- specifier: 2.1.2- version: 2.1.2([email protected])+ specifier: 2.1.8+ version: 2.1.8([email protected])semver:specifier: 7.8.4version: 7.8.4@@ -2191,12 +2185,6 @@ packages:peerDependencies:zod: ^3.25.76 || ^4.1.8- '@ai-sdk/[email protected]':- resolution: {integrity: sha512-4H/DVcceS0o8iMmLF+uVIW8t8PsvFB7ygMETqUsBqS6C1wEPemM8IepDFQX+tRwgsS7bK4L3N9wrew3e82A5aw==}- engines: {node: '>=22.13.0'}- peerDependencies:- ai: 7.0.137-'@ai-sdk/[email protected]':resolution: {integrity: sha512-sF+t6MnK1PTNN1oqmJpnWCm/4reVOpKryR1EcIl17O5Bu+AFstD/DpFZueRtzFk1dEZ0+uPX6gZXndjrNPAxLQ==}engines: {node: '>=18'}@@ -13759,8 +13747,8 @@ packages:resolution: {integrity: sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==}- resolution: {integrity: sha512-8c5Zs3WazGnBisfv0XWItQJxVHp04N6YnNgjsdprplvvFh4Kltfwh6duVkpro4hQ5uIUmUKTAIVfTTIa0uDpuw==}+ resolution: {integrity: sha512-B+N1a8grWUEmQwS8QL+o0yoB0sjPitphQ/PaION9j0m/msDxA9j1fxgX6VC4m604/OzT3IAMAZIw7kn8zn1R0A==}engines: {node: '>=20.19.0'}peerDependencies:typescript: '>=5.1.6 <7'@@ -15517,13 +15505,6 @@ snapshots:zod: 4.4.3optional: true- dependencies:- ai: 7.0.137([email protected])- run: 2.1.2([email protected])- transitivePeerDependencies:- - typescript-'@ai-sdk/[email protected]([email protected])':dependencies:'@ai-sdk/provider': 2.0.4@@ -29681,7 +29662,7 @@ snapshots:dependencies:queue-microtask: 1.2.3optionalDependencies:typescript: 7.0.2diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex c0fecf684..3d3bc4c12 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -30,7 +30,6 @@ ignoredOptionalDependencies:catalog:"@ai-sdk/anthropic": "^4.0.78"- "@ai-sdk/code-mode": "^1.0.94""@ai-sdk/google": "^4.0.93""@ai-sdk/mcp": "^2.0.73""@ai-sdk/openai": "^4.0.91"