Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): guard workflow step context capabilities (#3522)

Casey Gowried58065e2e78e
diff --git a/.changeset/guard-workflow-step-context.md b/.changeset/guard-workflow-step-context.md
new file mode 100644
index 000000000..8e3bbfa95
--- /dev/null
+++ b/.changeset/guard-workflow-step-context.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Workflow steps now fail immediately with actionable guidance when they access workflow-body-only `ctx.agents`, `ctx.agent()`, or `ctx.ask()` capabilities. Use the new `WorkflowStepToolContext` type for step helpers and pass serializable agent metadata from the workflow body.
diff --git a/docs/reference/typescript-api.md b/docs/reference/typescript-api.md
index 025f8e0cf..5b4c7f4a9 100644
--- a/docs/reference/typescript-api.md
+++ b/docs/reference/typescript-api.md
@@ -108,7 +108,7 @@ import template from "../../prompts/template.txt?raw";
| `ctx.getToken(provider)` | Resolve a bearer token for an inline auth provider such as `connect("...")` |
| `ctx.requireAuth(provider)` | Evict and re-authorize an inline provider, commonly after a downstream `401` |
-Tool definitions accept `availableInSubagents: false` to restrict the tool to top-level root sessions. Authored workflow tools also receive `ctx.agents`, a replay-stable map of callable-agent metadata, and `ctx.agent(name, input)` for invocation. The root-copy `agent` entry has an empty description when the root omits `description`. See [Workflows as tools](../tools/workflows#delegate-work-ctxagent) for the workflow-only context.
+Tool definitions accept `availableInSubagents: false` to restrict the tool to top-level root sessions. Authored workflow bodies also receive `ctx.agents`, a replay-stable map of callable-agent metadata, and `ctx.agent(name, input)` for invocation. A `"use step"` helper that receives the context directly should type it as `WorkflowStepToolContext`, which excludes `agents`, `agent`, and `ask`. The root-copy `agent` entry has an empty description when the root omits `description`. See [Workflows as tools](../tools/workflows#delegate-work-ctxagent) for the workflow-only context.
## Imports at a glance
diff --git a/docs/tools/workflows.mdx b/docs/tools/workflows.mdx
index b0f265bf5..81a22e4ef 100644
--- a/docs/tools/workflows.mdx
+++ b/docs/tools/workflows.mdx
@@ -79,14 +79,16 @@ or days later, the run resumes, deploys, and returns. The model sees one tool re
`start`, `getRun`, and `resumeHook` from `workflow/api` belong in steps. Your app does not install
the SDK; for types, new projects list `eve/workflow-modules` in the tsconfig `types`.
- In the body, `ctx` has `session`, `callId`, `toolName`, `abortSignal`, `agent`, `agents`, and `ask`.
- `getToken` and `requireAuth` work inside a `"use step"` helper that receives `ctx` as a direct argument;
- they throw in the workflow body. `getSandbox` and `getSkill` remain unavailable.
+ A `"use step"` helper that receives `ctx` directly gets a restricted `WorkflowStepToolContext` with
+ `session`, `callId`, `toolName`, `abortSignal`, `getToken`, and `requireAuth`. Read `ctx.agents`, call
+ `ctx.agent()`, and call `ctx.ask()` in the workflow body; pass only the serializable values the step needs.
+ `getSandbox` and `getSkill` remain unavailable.
- The tool's input must be a JSON object. Workflow bodies are for static tools under `agent/tools/`,
not tools returned from `defineDynamic` resolvers.
-`ctx.agent`, `ctx.agents`, and `ctx.ask` are available only on `WorkflowToolContext`. Ordinary tools, channel
-handlers, and schedule handlers do not receive these methods. A shared helper may accept
-`WorkflowToolContext` from `eve/tools` and use the context supplied by a workflow tool body.
+`ctx.agent`, `ctx.agents`, and `ctx.ask` are available only on `WorkflowToolContext` in the workflow body.
+Ordinary tools, channel handlers, schedule handlers, and workflow steps do not receive these methods. Type
+step helpers with `WorkflowStepToolContext` from `eve/tools` when they need step-safe context capabilities.
Workflow executors require `defineWorkflowTool`. Adding `"use workflow"` to `defineTool`, a bare
tool object, a channel handler, or a schedule handler fails the build. To start a session from a
@@ -217,9 +219,9 @@ The provider declaration and the API request both stay inside the step:
```ts
import { connect } from "@vercel/connect/eve";
-import type { WorkflowToolContext } from "eve/tools";
+import type { WorkflowStepToolContext } from "eve/tools";
-async function readRepository(ctx: WorkflowToolContext, repository: string) {
+async function readRepository(ctx: WorkflowStepToolContext, repository: string) {
"use step";
const provider = connect("github/my-agent");
const { token } = await ctx.getToken(provider);
@@ -360,23 +362,24 @@ Define the model-visible router under `agent/tools/`. Build the JEV criteria fro
```ts title="agent/tools/agent-router.ts"
import { evaluate } from "eve/ai";
-import { defineWorkflowTool, type WorkflowToolContext } from "eve/tools";
+import { defineWorkflowTool } from "eve/tools";
import { z } from "zod";
-async function chooseTarget(task: string, ctx: WorkflowToolContext) {
+async function chooseTarget(
+ task: string,
+ criteria: Record<"researcher" | "operator", string>,
+ abortSignal: AbortSignal,
+) {
"use step";
const result = await evaluate({
- abortSignal: ctx.abortSignal,
+ abortSignal,
state: { task },
questions: {
route: {
type: "choice",
instructions: "Which specialist should handle this task?",
- criteria: {
- researcher: ctx.agents.researcher.description,
- operator: ctx.agents.operator.description,
- },
+ criteria,
},
},
});
@@ -390,7 +393,14 @@ export default defineWorkflowTool({
async execute({ task }, ctx) {
"use workflow";
- const target = await chooseTarget(task, ctx);
+ const target = await chooseTarget(
+ task,
+ {
+ researcher: ctx.agents.researcher.description,
+ operator: ctx.agents.operator.description,
+ },
+ ctx.abortSignal,
+ );
return ctx.agent(target, { message: task });
},
diff --git a/packages/eve/extension-contracts/compatibility/tool/v51.ts b/packages/eve/extension-contracts/compatibility/tool/v51.ts
new file mode 100644
index 000000000..04d94730e
--- /dev/null
+++ b/packages/eve/extension-contracts/compatibility/tool/v51.ts
@@ -0,0 +1,8 @@
+import { defineTool } from "#public/tools/index.js";
+
+export default defineTool({
+ availableInSubagents: false,
+ description: "Coordinate work only from a top-level root session.",
+ inputSchema: { type: "object", properties: {}, additionalProperties: false },
+ execute: () => ({ status: "ready" }),
+});
diff --git a/packages/eve/extension-contracts/entrypoints/tool.ts b/packages/eve/extension-contracts/entrypoints/tool.ts
index a9f50687e..22c97d9f8 100644
--- a/packages/eve/extension-contracts/entrypoints/tool.ts
+++ b/packages/eve/extension-contracts/entrypoints/tool.ts
@@ -6,6 +6,7 @@ export {
toolOutput,
toolOutputPart,
toolResultFrom,
+ type WorkflowStepToolContext,
} from "../../src/public/tools/index.ts";
export {
agentRouter,
diff --git a/packages/eve/extension-contracts/reports/tool/v52.json b/packages/eve/extension-contracts/reports/tool/v52.json
new file mode 100644
index 000000000..7bf4ef9b7
--- /dev/null
+++ b/packages/eve/extension-contracts/reports/tool/v52.json
@@ -0,0 +1,27 @@
+{
+ "kind": "eve-extension-capability-contract",
+ "capability": "tool",
+ "epoch": 52,
+ "sha256": "016f8b915b37331f6f98321ab88f5cdf905ec6226ba596a1e7bf4b2b25a131ff",
+ "exports": [
+ "AgentRouterInput",
+ "AgentRouterTool",
+ "WorkflowStepToolContext",
+ "WorkflowTool",
+ "WorkflowToolInput",
+ "WorkflowToolOptions",
+ "agentRouter",
+ "defaultWebSearch",
+ "defineTool",
+ "defineWorkflowTool",
+ "disableTool",
+ "evaluate",
+ "isDisabledToolSentinel",
+ "isWebSearchToolDefinition",
+ "toolOutput",
+ "toolOutputPart",
+ "toolResultFrom",
+ "webSearch",
+ "workflow"
+ ]
+}
diff --git a/packages/eve/src/compiler/extension-compatibility.ts b/packages/eve/src/compiler/extension-compatibility.ts
index 97f1a5617..8aca75dbb 100644
--- a/packages/eve/src/compiler/extension-compatibility.ts
+++ b/packages/eve/src/compiler/extension-compatibility.ts
@@ -22,9 +22,10 @@ interface ExtensionCapabilityContract {
const EXTENSION_CAPABILITY_CONTRACTS = {
extension: { current: 1, supported: [1], dropped: {} },
tool: {
- current: 51,
+ current: 52,
supported: [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 28, 29, 30, 31, 32, 34, 35, 44, 45, 46, 49, 50, 51,
+ 52,
],
dropped: {
14: "TaskExec.delegated was removed; migrate to workflow-backed background tools",
diff --git a/packages/eve/src/execution/tools/workflow/step-execution.test.ts b/packages/eve/src/execution/tools/workflow/step-execution.test.ts
index f2d31cc57..474cfd145 100644
--- a/packages/eve/src/execution/tools/workflow/step-execution.test.ts
+++ b/packages/eve/src/execution/tools/workflow/step-execution.test.ts
@@ -11,6 +11,7 @@ import type {
WorkflowStepResult,
} from "#execution/tools/workflow/step-context.js";
import type { ToolContext } from "#tools/definition.js";
+import type { WorkflowToolContext } from "#tools/workflow-definition.js";
import type { AuthorizationDefinition } from "#shared/connection-types.js";
const durable = vi.hoisted(() => ({
@@ -61,8 +62,8 @@ function context(user = "user-1"): WorkflowStepContext {
};
}
-async function runStep(
- execute: (ctx: ToolContext) => unknown,
+async function runStep<TContext = ToolContext>(
+ execute: (ctx: TContext) => unknown,
input = context(),
): Promise<WorkflowStepResult> {
return (await withWorkflowStepAuthorization(execute)({
@@ -79,6 +80,33 @@ describe("workflow step authorization", () => {
durable.entries.clear();
});
afterEach(() => vi.unstubAllEnvs());
+ it.each([
+ {
+ access: (ctx: WorkflowToolContext) => ctx.agents,
+ capability: "ctx.agents",
+ guidance:
+ "Read ctx.agents in the workflow body and pass the required serializable metadata into the step.",
+ },
+ {
+ access: (ctx: WorkflowToolContext) => ctx.agent("researcher", { message: "Investigate" }),
+ capability: "ctx.agent()",
+ guidance: "Call ctx.agent() in the workflow body.",
+ },
+ {
+ access: (ctx: WorkflowToolContext) => ctx.ask({ prompt: "Continue?" }),
+ capability: "ctx.ask()",
+ guidance: "Call ctx.ask() in the workflow body.",
+ },
+ ])(
+ "fails $capability misuse without retrying the step",
+ async ({ access, capability, guidance }) => {
+ await expect(runStep<WorkflowToolContext>((ctx) => access(ctx))).rejects.toMatchObject({
+ fatal: true,
+ message: `${capability} is unavailable inside a "use step" function. ${guidance}`,
+ });
+ },
+ );
+
it("does not exchange a consumed code again when the rest of the step retries", async () => {
let exchanged = false;
const complete = vi.fn(async () => {
diff --git a/packages/eve/src/execution/tools/workflow/step-execution.ts b/packages/eve/src/execution/tools/workflow/step-execution.ts
index 79a8fbc4b..7d0759e22 100644
--- a/packages/eve/src/execution/tools/workflow/step-execution.ts
+++ b/packages/eve/src/execution/tools/workflow/step-execution.ts
@@ -44,9 +44,18 @@ export function withWorkflowStepAuthorization(execute: (...args: never[]) => unk
toolName: input.toolName,
options: { abortSignal: input.abortSignal, toolCallId: input.callId },
}),
+ agent: () => unavailableInStep("ctx.agent()", "Call ctx.agent() in the workflow body."),
+ ask: () => unavailableInStep("ctx.ask()", "Call ctx.ask() in the workflow body."),
getToken: auth.getToken,
requireAuth: auth.requireAuth,
};
+ Object.defineProperty(ctx, "agents", {
+ get: () =>
+ unavailableInStep(
+ "ctx.agents",
+ "Read ctx.agents in the workflow body and pass the required serializable metadata into the step.",
+ ),
+ });
let output: unknown;
try {
output = await auth.run(() =>
@@ -76,3 +85,10 @@ export function withWorkflowStepAuthorization(execute: (...args: never[]) => unk
Object.defineProperty(wrapped, "maxRetries", { get: () => Reflect.get(execute, "maxRetries") });
return wrapped;
}
+
+function unavailableInStep(capability: string, guidance: string): never {
+ throw Object.assign(
+ new Error(`${capability} is unavailable inside a "use step" function. ${guidance}`),
+ { fatal: true },
+ );
+}
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
index 3160ebc18..ec11e4b60 100644
--- a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
@@ -19,6 +19,7 @@ import {
reportingDeployWorkflow,
stepThenRaceWorkflow,
stepReferenceWorkflow,
+ workflowContextMisuseWorkflow,
} from "#internal/testing/workflow-tool-fixtures.js";
import { waitForHook } from "#internal/testing/workflow-test-helpers.js";
import { getRun, getWorld, start } from "#internal/workflow/runtime.js";
@@ -487,6 +488,36 @@ describe("workflow tools", () => {
expect(output).toContain('"callId":"call_deploy_service');
});
+ it("fails workflow-context misuse in a step with actionable guidance", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-step-context-misuse",
+ execute: workflowContextMisuseWorkflow,
+ toolName: "deploy_service",
+ });
+
+ const output = await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: buildSerializedContext({
+ continuationToken: "schedule:workflow-step-context-misuse",
+ mode: "task",
+ }),
+ },
+ ]);
+ const result = await run.returnValue;
+ return String(result.output);
+ });
+
+ expect(output).toContain('ctx.agents is unavailable inside a "use step" function.');
+ expect(output).toContain(
+ "Read ctx.agents in the workflow body and pass the required serializable metadata into the step.",
+ );
+ expect(output).toContain("Attempt 1.");
+ });
+
it("settles the call with an error when the workflow body throws", async () => {
const runtime = await createWorkflowToolRuntime({
agentName: "workflow-tool-fail",
diff --git a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
index 274351cf5..7af04f022 100644
--- a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
+++ b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
@@ -41,6 +41,23 @@ export async function authorizedDeployWorkflow(input: DeployInput, ctx: Workflow
return { plan, authenticatedAs };
}
+export async function workflowContextMisuseWorkflow(_input: DeployInput, ctx: WorkflowToolContext) {
+ "use workflow";
+ return await readAgentsStep(ctx);
+}
+
+async function readAgentsStep(ctx: WorkflowToolContext) {
+ "use step";
+ try {
+ return ctx.agents;
+ } catch (error) {
+ if (error instanceof Error) {
+ error.message += ` Attempt ${getStepMetadata().attempt}.`;
+ }
+ throw error;
+ }
+}
+
export async function stepReferenceWorkflow(input: DeployInput) {
"use workflow";
const byArgument = await returnStepReference(planDeployStep);
diff --git a/packages/eve/src/public/tools/index.ts b/packages/eve/src/public/tools/index.ts
index 400c6cc8b..1da7215e6 100644
--- a/packages/eve/src/public/tools/index.ts
+++ b/packages/eve/src/public/tools/index.ts
@@ -39,6 +39,7 @@ export {
export {
defineWorkflowTool,
+ type WorkflowStepToolContext,
type WorkflowToolContext,
type WorkflowToolDefinition,
type AgentInput,
diff --git a/packages/eve/src/tools/workflow-definition.test.ts b/packages/eve/src/tools/workflow-definition.test.ts
index 5afc2e27b..0f6f0bfca 100644
--- a/packages/eve/src/tools/workflow-definition.test.ts
+++ b/packages/eve/src/tools/workflow-definition.test.ts
@@ -6,6 +6,7 @@ import {
defineWorkflowTool,
isWorkflowToolDefinition,
type WorkflowAgentMetadata,
+ type WorkflowStepToolContext,
type WorkflowToolContext,
} from "#tools/workflow-definition.js";
import { normalizeToolDefinition } from "#internal/authored-definition/schema-backed.js";
@@ -52,6 +53,22 @@ describe("defineWorkflowTool", () => {
expectTypeOf(definition.execute).parameter(0).toEqualTypeOf<{ service: string }>();
});
+ it("exposes only step-safe capabilities on WorkflowStepToolContext", () => {
+ const useStepContext = (ctx: WorkflowStepToolContext) => {
+ void ctx.getToken;
+ void ctx.requireAuth;
+ void ctx.abortSignal;
+ // @ts-expect-error Agent metadata is available only in the workflow body.
+ void ctx.agents;
+ // @ts-expect-error Agent invocation is available only in the workflow body.
+ void ctx.agent;
+ // @ts-expect-error Human input is available only in the workflow body.
+ void ctx.ask;
+ };
+
+ expectTypeOf(useStepContext).parameter(0).toEqualTypeOf<WorkflowStepToolContext>();
+ });
+
it("provides task messages and receipt projections for background workflows", () => {
const definition = defineWorkflowTool({
description: "Report a deployment",
diff --git a/packages/eve/src/tools/workflow-definition.ts b/packages/eve/src/tools/workflow-definition.ts
index a8d588f72..3fd9f6a2d 100644
--- a/packages/eve/src/tools/workflow-definition.ts
+++ b/packages/eve/src/tools/workflow-definition.ts
@@ -68,6 +68,12 @@ export interface WorkflowAgentMetadata {
readonly description: string;
}
+/** Context capabilities available inside an authored `"use step"` helper. */
+export type WorkflowStepToolContext = Pick<
+ ToolContext,
+ "abortSignal" | "callId" | "session" | "toolName" | "getToken" | "requireAuth"
+>;
+
interface WorkflowAgent {
<const TOutputSchema extends JsonObject>(
target: string,
@@ -77,8 +83,8 @@ interface WorkflowAgent {
}
/**
- * Context supplied to a workflow tool. Pass it directly to a step helper for
- * getToken/requireAuth; those capabilities throw in the workflow body itself.
+ * Context supplied to a workflow tool body. When passed directly to a step,
+ * eve replaces it with {@link WorkflowStepToolContext}.
*/
export type WorkflowToolContext = Pick<
ToolContext,