The Open Framework for Building Agents
fix(eve): workflow tools - unify owner hooks and inline turn execution (#3042)
Andrew Barba62f076cf3029
diff --git a/.changeset/quiet-workflow-inbox.md b/.changeset/quiet-workflow-inbox.mdnew file mode 100644index 000000000..abe426c2a--- /dev/null+++ b/.changeset/quiet-workflow-inbox.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Inline turns now handle workflow tools on the parent through one ordered inbox for progress, input requests, and outcomes. Waiting workflow tools use a fresh cancellation token per dispatch attempt; a retried dispatch can start another run, so side effects need application idempotency.diff --git a/.changeset/tidy-workflow-hook-lifetimes.md b/.changeset/tidy-workflow-hook-lifetimes.mdnew file mode 100644index 000000000..34c067be0--- /dev/null+++ b/.changeset/tidy-workflow-hook-lifetimes.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Activity collectors now finish on expiry even when a hook read is pending. Task and activity workflows rely on workflow completion to clean up their hooks, and subagent calls skip conflict checks for generated reply tokens.diff --git a/docs/tools/workflows.mdx b/docs/tools/workflows.mdxindex cc4f06b29..f15ef782e 100644--- a/docs/tools/workflows.mdx+++ b/docs/tools/workflows.mdx@@ -387,6 +387,11 @@ session's task index, can be cancelled with `task_cancel`, and are cancelled wheErrors follow the SDK. A thrown error in a step retries per the step's policy; `FatalError` doesnot. An error that escapes the body fails the run.+Starting a waiting workflow tool can also be retried. If dispatch is interrupted after starting a+run, its retry starts another run, and both may execute. The parent tracks the run returned by the+successful dispatch attempt. Use an application idempotency key for side effects that must happen+only once.+The workflow id derives from the executor's module path and function name. Inline executors usethe tool module and the name `execute`; imported executors use their declaring module and name.Renaming or moving that function creates a new workflow. Runs in flight finish on the deployment that started them; a run thatdiff --git a/e2e/fixtures/agent-workflow-tools/agent/tools/confirm_deploy.ts b/e2e/fixtures/agent-workflow-tools/agent/tools/confirm_deploy.tsindex ec98d5c31..0499f9234 100644--- a/e2e/fixtures/agent-workflow-tools/agent/tools/confirm_deploy.ts+++ b/e2e/fixtures/agent-workflow-tools/agent/tools/confirm_deploy.ts@@ -10,9 +10,10 @@ import { describePlan } from "../lib/plan.ts";export default defineWorkflowTool({description: "Deploy a service after a human approves the plan.",inputSchema: z.strictObject({ service: z.string() }),- async execute({ service }, ctx) {+ async *execute({ service }, ctx) {"use workflow";+ yield "awaiting approval";const answer = await ctx.ask({display: "confirmation",options: [@@ -21,6 +22,7 @@ export default defineWorkflowTool({],prompt: `Apply ${describePlan(service)}?`,});+ yield "approval received";return { approved: answer.optionId === "approve", service };},});diff --git a/e2e/fixtures/agent-workflow-tools/evals/confirm-deploy.hitl.eval.ts b/e2e/fixtures/agent-workflow-tools/evals/confirm-deploy.hitl.eval.tsindex c942c586e..5bc547114 100644--- a/e2e/fixtures/agent-workflow-tools/evals/confirm-deploy.hitl.eval.ts+++ b/e2e/fixtures/agent-workflow-tools/evals/confirm-deploy.hitl.eval.ts@@ -2,7 +2,7 @@ import { defineEval } from "eve/evals";export default defineEval({description:- "A workflow tool asks the human mid-body; the answer resumes the run and the result settles the call.",+ "A workflow tool reports progress around a human question, then settles the call in order.",async test(t) {const parked = await t.send("WORKFLOW-CONFIRM-START");t.requireInputRequest({@@ -10,6 +10,10 @@ export default defineEval({optionIds: ["approve", "cancel"],toolName: "confirm_deploy",});+ parked.event("action.partial", {+ count: (count) => count >= 1,+ data: { result: { toolName: "confirm_deploy", output: "awaiting approval" } },+ });parked.calledTool("confirm_deploy", { status: "pending", count: 1 });const approved = await t.respondAll("approve");@@ -25,6 +29,19 @@ export default defineEval({status: "completed",},});+ approved.eventsSatisfy("progress arrives before the final workflow result", (events) => {+ const progress = events.findIndex(+ (event) =>+ event.type === "action.partial" && event.data.result.output === "approval received",+ );+ const result = events.findIndex(+ (event) =>+ event.type === "action.result" &&+ event.data.result.kind === "tool-result" &&+ event.data.result.toolName === "confirm_deploy",+ );+ return progress >= 0 && result > progress;+ });approved.messageIncludes("WORKFLOW-CONFIRM-RESULT");t.noFailedActions();},diff --git a/packages/eve/src/execution/activity-collector.test.ts b/packages/eve/src/execution/activity-collector.test.tsindex 0bd40e339..410e0e367 100644--- a/packages/eve/src/execution/activity-collector.test.ts+++ b/packages/eve/src/execution/activity-collector.test.ts@@ -1,7 +1,33 @@-import { describe, expect, it } from "vitest";+import { beforeEach, describe, expect, it, vi } from "vitest";-import { reduceCollectorActivity } from "#execution/activity-collector.js";+import {+ activityCollectorWorkflow,+ reduceCollectorActivity,+} from "#execution/activity-collector.js";import { createActivitySnapshot } from "#execution/session-activity.js";+import type { ActivityBatchV1 } from "#protocol/activity.js";++const mocks = vi.hoisted(() => ({+ createHook: vi.fn(),+ disposeSessionActivityStep: vi.fn(),+ renderSessionActivityStep: vi.fn(),+ sleep: vi.fn(),+}));++vi.mock("#compiled/@workflow/core/index.js", async (importOriginal) => ({+ ...(await importOriginal<typeof import("#compiled/@workflow/core/index.js")>()),+ createHook: mocks.createHook,+ sleep: mocks.sleep,+}));+vi.mock("#execution/session-activity-renderer-step.js", () => ({+ disposeSessionActivityStep: mocks.disposeSessionActivityStep,+ renderSessionActivityStep: mocks.renderSessionActivityStep,+}));++beforeEach(() => {+ vi.resetAllMocks();+ mocks.disposeSessionActivityStep.mockResolvedValue(undefined);+});const work = {id: "root:work",@@ -10,6 +36,48 @@ const work = {rootTurnId: "turn",};+describe("activityCollectorWorkflow", () => {+ it.each([false, true])(+ "expires with a pending hook read (debouncing: %s)",+ async (debouncing) => {+ const expiry = Promise.withResolvers<void>();+ const reading = Promise.withResolvers<void>();+ mocks.sleep.mockImplementation((duration: Date | number) =>+ duration instanceof Date ? expiry.promise : new Promise<void>(() => {}),+ );+ mocks.createHook.mockReturnValue({+ token: "activity",+ getConflict: async () => null,+ async *[Symbol.asyncIterator]() {+ if (debouncing) {+ yield {+ events: [{ eventId: "start", kind: "work.started", startedAt: "1", work }],+ version: 1,+ } satisfies ActivityBatchV1;+ }+ reading.resolve();+ yield await new Promise<ActivityBatchV1>(() => {});+ },+ });++ const result = activityCollectorWorkflow({+ expiresAt: "2026-09-04T00:00:00Z",+ serializedContext: {},+ token: "activity",+ });+ await reading.promise;+ expiry.resolve();++ await expect(result).resolves.toBeUndefined();+ expect(mocks.disposeSessionActivityStep).toHaveBeenCalledExactlyOnceWith({+ rendererStates: {},+ serializedContext: {},+ });+ },+ 1_000,+ );+});+describe("reduceCollectorActivity", () => {it("marks only presentation revision advances as changed", () => {const visible = reduceCollectorActivity(createActivitySnapshot(), {diff --git a/packages/eve/src/execution/activity-collector.ts b/packages/eve/src/execution/activity-collector.tsindex eabc20f3e..1193ba421 100644--- a/packages/eve/src/execution/activity-collector.ts+++ b/packages/eve/src/execution/activity-collector.ts@@ -1,11 +1,6 @@import { createHook, sleep } from "#compiled/@workflow/core/index.js";-import {- claimHookOwnership,- closeHookIterator,- disposeHook,- isHookConflictError,-} from "#execution/hook-ownership.js";+import { claimHookOwnership, isHookConflictError } from "#execution/hook-ownership.js";import { createActivitySnapshot, reduceActivityBatch } from "#execution/session-activity.js";import type { ActivityBatchV1, ActivitySnapshotV1 } from "#protocol/activity.js";import {@@ -27,21 +22,19 @@ export async function activityCollectorWorkflow(input: ActivityCollectorInput):const batches = createHook<ActivityBatchV1>({ token: input.token });const iterator = batches[Symbol.asyncIterator]();- let ownsHook = false;let pendingRead: Promise<IteratorResult<ActivityBatchV1>> | undefined;const expiry = sleep(new Date(input.expiresAt)).then(() => ({ kind: "expired" as const }));let snapshot = createActivitySnapshot();let rendererStates: Readonly<Record<string, unknown>> = {};try {- try {- await claimHookOwnership(batches);- ownsHook = true;- } catch (error) {- if (isHookConflictError(error)) return;- throw error;- }+ await claimHookOwnership(batches);+ } catch (error) {+ if (isHookConflictError(error)) return;+ throw error;+ }+ try {while (true) {pendingRead ??= iterator.next();const next = await Promise.race([@@ -77,14 +70,10 @@ export async function activityCollectorWorkflow(input: ActivityCollectorInput):rendererStates = rendered.rendererStates;}} finally {- if (ownsHook) {- await closeHookIterator(iterator).catch(() => {});- await disposeHook(batches).catch(() => {});- await disposeSessionActivityStep({- rendererStates,- serializedContext: input.serializedContext,- }).catch(() => {});- }+ await disposeSessionActivityStep({+ rendererStates,+ serializedContext: input.serializedContext,+ }).catch(() => {});}}diff --git a/packages/eve/src/execution/coordination-dispatch-shared.ts b/packages/eve/src/execution/coordination-dispatch-shared.tsindex 05be8e471..296f37215 100644--- a/packages/eve/src/execution/coordination-dispatch-shared.ts+++ b/packages/eve/src/execution/coordination-dispatch-shared.ts@@ -48,6 +48,7 @@ import { buildSubagentRunInput } from "#subagents/tool.js";import { readSessionTraceContext } from "#tracing/agent-trace-context-store.js";import { resolveEffectiveAgentRuntime } from "#execution/effective-agent-config.js";import { isTaskControlAction } from "#execution/tasks/parent/dispatch.js";+import type { WorkflowToolRunOwner } from "#execution/tools/workflow/messages.js";export type DispatchPlanEntry =| { readonly kind: "task-control"; readonly action: RuntimeToolCallActionRequest }@@ -56,8 +57,7 @@ export type DispatchPlanEntry =/** Input shared by direct and Workflow-originated owner-side dispatch. */export interface CoordinationDispatchInput {readonly callbackBaseUrl?: string;- /** Internal hook that receives child completion and HITL payloads. */- readonly parentContinuationToken?: string;+ readonly workflowToolRunOwner: WorkflowToolRunOwner;readonly parentWritable: WritableStream<Uint8Array>;readonly serializedContext: Record<string, unknown>;readonly sessionState: DurableSessionState;diff --git a/packages/eve/src/execution/coordination-dispatch-step.ts b/packages/eve/src/execution/coordination-dispatch-step.tsindex 634a9e76c..ae819352e 100644--- a/packages/eve/src/execution/coordination-dispatch-step.ts+++ b/packages/eve/src/execution/coordination-dispatch-step.ts@@ -72,7 +72,7 @@ export async function dispatchCoordinationStep(auth: prepared.auth,batchEvent: batch.event,initiatorAuth: prepared.initiatorAuth,- parentContinuationToken: input.parentContinuationToken ?? session.continuationToken,+ owner: input.workflowToolRunOwner,parentSession: prepared.parentSession,session: nextSession,task: entry.task,diff --git a/packages/eve/src/execution/durable-session-migrations/turn-workflow.ts b/packages/eve/src/execution/durable-session-migrations/turn-workflow.tsindex 562ad914e..a50238d77 100644--- a/packages/eve/src/execution/durable-session-migrations/turn-workflow.ts+++ b/packages/eve/src/execution/durable-session-migrations/turn-workflow.ts@@ -64,7 +64,7 @@ interface TurnWorkflowInputBase {readonly stepInput: TurnStepInput;}-/** A step completed inline before the turn needed its child workflow. */+/** A step completed inline before the turn needed the shared turn runner. */export interface InitialTurnStep {readonly beforeStep: {readonly serializedContext: Record<string, unknown>;diff --git a/packages/eve/src/execution/inline-turn.ts b/packages/eve/src/execution/inline-turn.tsindex ddb7e4621..ecdbd28eb 100644--- a/packages/eve/src/execution/inline-turn.ts+++ b/packages/eve/src/execution/inline-turn.ts@@ -25,11 +25,11 @@ export type InlineTurnOutcome =| {readonly initialCancellation?: TurnCancelPayload;readonly initialStep?: InitialTurnStep;- readonly kind: "child";+ readonly kind: "child" | "continue";}| { readonly action: NextDriverAction; readonly kind: "result" };-/** Runs ordinary same-deployment turn steps inline and hands complex work to the child path. */+/** Runs same-deployment steps directly until they need the shared turn runner. */export async function runInlineTurn(input: {readonly bufferedDeliveries: DeliverHookPayload[];readonly bufferedSessionControls: Array<"clear" | "compact" | "expired" | "reset">;@@ -81,7 +81,7 @@ export async function runInlineTurn(input: {);if (result.requiresChildDispatch === true) {- return childOutcome(control);+ return { initialCancellation: control.initialCancellation, kind: "child" };}const initialStep = { beforeStep, result } satisfies InitialTurnStep;@@ -90,7 +90,7 @@ export async function runInlineTurn(input: {result.action === "cancelled" ||(result.backgroundTasks?.length ?? 0) > 0) {- return childOutcome(control, initialStep);+ return continueOutcome(control, initialStep);}if (result.action === "done") {@@ -112,7 +112,7 @@ export async function runInlineTurn(input: {result.action === "dispatch-workflow-tasks" ||(result.action === "park" && result.pendingCoordinationCallIds !== undefined)) {- return childOutcome(control, initialStep);+ return continueOutcome(control, initialStep);}if (result.action === "park") {@@ -120,7 +120,7 @@ export async function runInlineTurn(input: {result.hasPendingAuthorization ||(result.hasPendingInputBatch && input.capabilities?.requestInput === true) ||input.mode === "conversation";- if (!canPark) return childOutcome(control, initialStep);+ if (!canPark) return continueOutcome(control, initialStep);return {action: {authorizationAttemptIds: result.authorizationAttemptIds,@@ -146,14 +146,14 @@ export async function runInlineTurn(input: {}}-function childOutcome(+function continueOutcome(control: InlineTurnControl,- initialStep?: InitialTurnStep,-): Extract<InlineTurnOutcome, { readonly kind: "child" }> {+ initialStep: InitialTurnStep,+): Exclude<InlineTurnOutcome, { readonly kind: "result" }> {return {initialCancellation: control.initialCancellation,initialStep,- kind: "child",+ kind: "continue",};}diff --git a/packages/eve/src/execution/tasks/child/workflow.test.ts b/packages/eve/src/execution/tasks/child/workflow.test.tsindex 32951aca6..b7a7ccd98 100644--- a/packages/eve/src/execution/tasks/child/workflow.test.ts+++ b/packages/eve/src/execution/tasks/child/workflow.test.ts@@ -1,6 +1,6 @@import { beforeEach, describe, expect, it, vi } from "vitest";-import type { WorkflowToolRunRequestMessage } from "#execution/tools/workflow/messages.js";+import type { WorkflowToolRunMessage } from "#execution/tools/workflow/messages.js";import { taskRunWorkflow } from "#execution/tasks/child/workflow.js";import type { TaskView } from "#tasks/types.js";@@ -12,8 +12,10 @@ const mocks = vi.hoisted(() => ({createChannelReader: vi.fn((channel: string) => ({ channel, iterator: [][Symbol.iterator]() })),createHook: vi.fn(() => ({ token: "task-token" })),deliverTaskInputResponsesStep: vi.fn(),- disposeHook: vi.fn(),- openWorkflowToolRunOwnerChannels: vi.fn(() => ({ dispose: vi.fn(), readers: [] })),+ openWorkflowToolRunOwnerInbox: vi.fn(() => ({+ owner: { inbox: "generated-owner-token" },+ reader: { channel: "workflow" },+ })),raceChannelReads: vi.fn(),resumeHookStep: vi.fn(),wakeTaskAgentRequestParentStep: vi.fn(),@@ -40,7 +42,6 @@ vi.mock("#compiled/@workflow/core/index.js", async (importOriginal) => ({}));vi.mock("#execution/hook-ownership.js", () => ({claimHookOwnership: mocks.claimHookOwnership,- disposeHook: mocks.disposeHook,isHookConflictError: () => false,}));vi.mock("#execution/tasks/child/steps.js", () => ({@@ -62,7 +63,7 @@ vi.mock("#execution/tools/workflow/owner-channels.js", () => ({raceChannelReads: mocks.raceChannelReads,}));vi.mock("#execution/tools/workflow/owner.js", () => ({- openWorkflowToolRunOwnerChannels: mocks.openWorkflowToolRunOwnerChannels,+ openWorkflowToolRunOwnerInbox: mocks.openWorkflowToolRunOwnerInbox,}));vi.mock("#execution/tools/workflow/resume-hook-step.js", () => ({resumeHookStep: mocks.resumeHookStep,@@ -79,6 +80,7 @@ const initialView = {} satisfies TaskView;const bufferedAgentRequest = {+ kind: "request",from: {callId: "tool-call-1",execution: "background",@@ -95,7 +97,7 @@ const bufferedAgentRequest = {invocationId: "tool-call-1:approver",kind: "agent-invoke",},-} satisfies WorkflowToolRunRequestMessage;+} satisfies WorkflowToolRunMessage;const workflowAgentRequest = {...bufferedAgentRequest,@@ -104,13 +106,16 @@ const workflowAgentRequest = {invocationId: "tool-call-1:approver:2",kind: "agent-invoke",},-} satisfies WorkflowToolRunRequestMessage;+} satisfies WorkflowToolRunMessage;describe("taskRunWorkflow", () => {beforeEach(() => {vi.resetAllMocks();mocks.createHook.mockReturnValue({ token: "task-token" });- mocks.openWorkflowToolRunOwnerChannels.mockReturnValue({ dispose: vi.fn(), readers: [] });+ mocks.openWorkflowToolRunOwnerInbox.mockReturnValue({+ owner: { inbox: "generated-owner-token" },+ reader: { channel: "workflow" },+ });mocks.executeWorkflowBody.mockResolvedValue({outcome: { output: "done", status: "completed" },reportCount: 0,@@ -153,7 +158,7 @@ describe("taskRunWorkflow", () => {it("buffers agent requests until task dispatch is acknowledged", async () => {mocks.raceChannelReads.mockResolvedValueOnce({- channel: "request",+ channel: "workflow",next: { done: false, value: bufferedAgentRequest },}).mockResolvedValueOnce({@@ -185,7 +190,7 @@ describe("taskRunWorkflow", () => {next: { done: false, value: { command: { kind: "ready" }, kind: "task-command" } },}).mockResolvedValueOnce({- channel: "request",+ channel: "workflow",next: { done: false, value: workflowAgentRequest },}).mockResolvedValueOnce({ channel: "commands", next: { done: true, value: undefined } });@@ -257,6 +262,10 @@ describe("taskRunWorkflow", () => {});expect(mocks.executeWorkflowBody).toHaveBeenCalledOnce();+ expect(mocks.executeWorkflowBody).toHaveBeenCalledWith(+ expect.objectContaining({ owner: { inbox: "generated-owner-token" } }),+ expect.any(AbortSignal),+ );});it.each(["tool", "subagent"])(@@ -384,10 +393,11 @@ describe("taskRunWorkflow", () => {},}).mockResolvedValueOnce({- channel: "report",+ channel: "workflow",next: {done: false,value: {+ kind: "report",from: bufferedAgentRequest.from,update: { kind: "eve:task-message", message: "Review the export" },},diff --git a/packages/eve/src/execution/tasks/child/workflow.ts b/packages/eve/src/execution/tasks/child/workflow.tsindex fb151d361..5e1034fa7 100644--- a/packages/eve/src/execution/tasks/child/workflow.ts+++ b/packages/eve/src/execution/tasks/child/workflow.ts@@ -1,7 +1,7 @@import { createHook } from "#compiled/@workflow/core/index.js";import type { ActivityObserverConfig } from "#channel/types.js";-import { claimHookOwnership, disposeHook, isHookConflictError } from "#execution/hook-ownership.js";+import { claimHookOwnership, isHookConflictError } from "#execution/hook-ownership.js";import {appendTaskProgressStep,appendTaskViewStep,@@ -19,12 +19,9 @@ import {type WorkflowBodyDefinition,type WorkflowBodyResult,} from "#execution/tools/workflow/body.js";-import {- deriveWorkflowToolRunOwner,- type WorkflowToolRunRequestMessage,-} from "#execution/tools/workflow/messages.js";+import type { WorkflowToolRunRequestMessage } from "#execution/tools/workflow/messages.js";import { createChannelReader, raceChannelReads } from "#execution/tools/workflow/owner-channels.js";-import { openWorkflowToolRunOwnerChannels } from "#execution/tools/workflow/owner.js";+import { openWorkflowToolRunOwnerInbox } from "#execution/tools/workflow/owner.js";import {workflowToolRunOutcomeToTaskCommand,workflowToolRunReportToTaskPayload,@@ -78,12 +75,8 @@ export async function taskRunWorkflow(input: TaskRunWorkflowInput): Promise<void"use workflow";const commands = createHook<TaskRunInboundPayload>({ token: input.taskInboxToken });- const workflowToolRunChannels = openWorkflowToolRunOwnerChannels(input.taskInboxToken);- const readers = [- ...workflowToolRunChannels.readers,- createChannelReader("commands", commands),- ] as const;- let ownsHook = false;+ const workflowToolRunInbox = openWorkflowToolRunOwnerInbox();+ const readers = [workflowToolRunInbox.reader, createChannelReader("commands", commands)] as const;let view = input.initialView;let dispatchAcknowledged = false;let dispatchRejected = false;@@ -103,81 +96,68 @@ export async function taskRunWorkflow(input: TaskRunWorkflowInput): Promise<voidlet pendingBodyResult: WorkflowBodyResult | undefined;try {- try {- await claimHookOwnership(commands);- ownsHook = true;- } catch (error) {- if (isHookConflictError(error)) return;- throw error;- }+ await claimHookOwnership(commands);+ } catch (error) {+ if (isHookConflictError(error)) return;+ throw error;+ }- await appendTaskViewStep({ activityObserver: input.activityObserver, view });- while (true) {- // Hook persistence does not mean the owner has consumed every report yet.- if (pendingBodyResult !== undefined && updateIndex >= pendingBodyResult.reportCount) {- executorSettled = true;- await applyPayload({- command: workflowToolRunOutcomeToTaskCommand({- from: createWorkflowBodyRef({ ...input.workflow!, execution: "background" }),- result: pendingBodyResult.outcome,- }),- kind: "task-command",- });- pendingBodyResult = undefined;- if (view.status === "cancelled" && dispatchAcknowledged && !dispatchRejected) {- await wakeTaskParentStep({ token: input.parentContinuationToken, view });- }- }- if (isFinished()) break;- const read = await raceChannelReads(- bodyReader === undefined ? readers : [...readers, bodyReader],- );- if (read.channel === "body") {- bodyReader = undefined;- if (read.next.done) continue;- pendingBodyResult = read.next.value;- continue;+ await appendTaskViewStep({ activityObserver: input.activityObserver, view });+ while (true) {+ // Hook persistence does not mean the owner has consumed every report yet.+ if (pendingBodyResult !== undefined && updateIndex >= pendingBodyResult.reportCount) {+ executorSettled = true;+ await applyPayload({+ command: workflowToolRunOutcomeToTaskCommand({+ from: createWorkflowBodyRef({ ...input.workflow!, execution: "background" }),+ result: pendingBodyResult.outcome,+ }),+ kind: "task-command",+ });+ pendingBodyResult = undefined;+ if (view.status === "cancelled" && dispatchAcknowledged && !dispatchRejected) {+ await wakeTaskParentStep({ token: input.parentContinuationToken, view });}- if (read.next.done) return;+ }+ if (isFinished()) break;+ const read = await raceChannelReads(+ bodyReader === undefined ? readers : [...readers, bodyReader],+ );+ if (read.channel === "body") {+ bodyReader = undefined;+ if (read.next.done) continue;+ pendingBodyResult = read.next.value;+ continue;+ }+ if (read.next.done) return;- if (read.channel === "report") {- await applyPayload(- workflowToolRunReportToTaskPayload(read.next.value, view.taskId, updateIndex++),- );+ if (read.channel === "workflow") {+ const message = read.next.value;+ if (message.kind === "report") {+ await applyPayload(workflowToolRunReportToTaskPayload(message, view.taskId, updateIndex++));continue;}- if (read.channel === "outcome") {+ if (message.kind === "outcome") {await applyPayload({- command: workflowToolRunOutcomeToTaskCommand(read.next.value),+ command: workflowToolRunOutcomeToTaskCommand(message),kind: "task-command",});continue;}- if (read.channel === "request") {- const request = read.next.value;- const kind = request.request.kind;- if (- kind === "agent-invoke" ||- kind === "agent-settled" ||- kind === "authorization-request"- ) {- await handleOwnerRequest(request);- continue;- }- if (request.requestCoordinates === undefined) {- answerHooks.set(request.replyTo, { runId: request.from.runId });- }- await applyPayload(workflowToolRunRequestToTaskInputRequest(request));+ const request = message;+ const kind = request.request.kind;+ if (kind === "agent-invoke" || kind === "agent-settled" || kind === "authorization-request") {+ await handleOwnerRequest(request);continue;}-- await applyPayload(read.next.value);- }- } finally {- if (ownsHook) {- await workflowToolRunChannels.dispose();- await disposeHook(commands);+ if (request.requestCoordinates === undefined) {+ answerHooks.set(request.replyTo, { runId: request.from.runId });+ }+ await applyPayload(workflowToolRunRequestToTaskInputRequest(request));+ continue;}++ await applyPayload(read.next.value);}function isFinished(): boolean {@@ -242,7 +222,7 @@ export async function taskRunWorkflow(input: TaskRunWorkflowInput): Promise<void{...input.workflow,execution: "background",- owner: deriveWorkflowToolRunOwner(input.taskInboxToken),+ owner: workflowToolRunInbox.owner,},bodyController.signal,),diff --git a/packages/eve/src/execution/tools/subagent/invoke-agent.test.ts b/packages/eve/src/execution/tools/subagent/invoke-agent.test.tsindex 6d2d94e94..f34ab7fa4 100644--- a/packages/eve/src/execution/tools/subagent/invoke-agent.test.ts+++ b/packages/eve/src/execution/tools/subagent/invoke-agent.test.ts@@ -6,7 +6,6 @@ import { agent, type AgentInvocationReply } from "#execution/tools/subagent/invoimport type { ToolContext } from "#tools/definition.js";const mocks = vi.hoisted(() => ({- claimHookOwnership: vi.fn(),createHook: vi.fn(),disposeHook: vi.fn(),resumeHook: vi.fn(),@@ -17,7 +16,6 @@ vi.mock("#compiled/@workflow/core/index.js", async (importOriginal) => ({createHook: mocks.createHook,}));vi.mock("#execution/hook-ownership.js", () => ({- claimHookOwnership: mocks.claimHookOwnership,disposeHook: mocks.disposeHook,}));vi.mock("#execution/tools/workflow/resume-hook-step.js", () => ({@@ -66,10 +64,7 @@ describe("background agent invocation routing", () => {turnId: "turn-1",},owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -125,10 +120,7 @@ describe("background agent invocation routing", () => {admission: Promise.resolve({ status: "accepted" }),from,owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -136,13 +128,8 @@ describe("background agent invocation routing", () => {agent(ctx, { key: "research", message: "Find it", target: "research" }),).resolves.toBe("available");- expect(mocks.claimHookOwnership).toHaveBeenCalledWith(- expect.objectContaining({ token: "agent-reply" }),- );- expect(mocks.claimHookOwnership.mock.invocationCallOrder[0]).toBeLessThan(- mocks.resumeHook.mock.invocationCallOrder[0]!,- );- expect(mocks.resumeHook).toHaveBeenCalledWith("owner-request", {+ expect(mocks.resumeHook).toHaveBeenCalledWith("owner-inbox", {+ kind: "request",from,replyTo: "agent-reply",request: {@@ -192,15 +179,13 @@ describe("background agent invocation routing", () => {turnId: "turn-1",},owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});const result = agent(ctx, { key: "research", message: "Find it", target: "research" });await Promise.resolve();+ expect(mocks.createHook).not.toHaveBeenCalled();expect(mocks.resumeHook).not.toHaveBeenCalled();admission.resolve({ status: "accepted" });@@ -251,10 +236,7 @@ describe("background agent invocation routing", () => {turnId: "turn-1",},owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -262,7 +244,8 @@ describe("background agent invocation routing", () => {agent(ctx, { key: "research", message: "Find it", target: "research" }),).resolves.toBe("inline");expect(mocks.resumeHook).toHaveBeenCalledTimes(2);- expect(mocks.resumeHook).toHaveBeenNthCalledWith(1, "owner-request", {+ expect(mocks.resumeHook).toHaveBeenNthCalledWith(1, "owner-inbox", {+ kind: "request",from: expect.objectContaining({ execution: "blocking", runId: "run-1" }),replyTo: "agent-reply",request: {@@ -310,10 +293,7 @@ describe("background agent invocation routing", () => {turnId: "turn-1",},owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -322,7 +302,7 @@ describe("background agent invocation routing", () => {).rejects.toEqual(failure.output);expect(mocks.resumeHook).toHaveBeenCalledTimes(1);expect(mocks.resumeHook).not.toHaveBeenCalledWith(- "owner-request",+ "owner-inbox",expect.objectContaining({ request: expect.objectContaining({ kind: "agent-settled" }) }),);});@@ -397,10 +377,7 @@ describe("background agent invocation routing", () => {admission: Promise.resolve({ status: "accepted" }),from,owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -408,7 +385,8 @@ describe("background agent invocation routing", () => {agent(ctx, { key: "research", message: "Find it", target: "research" }),).resolves.toBe("done");- expect(mocks.resumeHook).toHaveBeenNthCalledWith(2, "owner-request", {+ expect(mocks.resumeHook).toHaveBeenNthCalledWith(2, "owner-inbox", {+ kind: "request",from,replyTo: "child-continuation",request: {@@ -417,7 +395,8 @@ describe("background agent invocation routing", () => {},requestCoordinates: { sequence: 3, stepIndex: 1, turnId: "turn-child" },});- expect(mocks.resumeHook).toHaveBeenNthCalledWith(3, "owner-request", {+ expect(mocks.resumeHook).toHaveBeenNthCalledWith(3, "owner-inbox", {+ kind: "request",from,replyTo: "child-continuation",request: {@@ -484,10 +463,7 @@ describe("background agent invocation routing", () => {admission: Promise.resolve({ status: "accepted" }),from,owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -495,7 +471,8 @@ describe("background agent invocation routing", () => {agent(ctx, { key: "research", message: "Find it", target: "research" }),).resolves.toBe("done");- expect(mocks.resumeHook).toHaveBeenCalledWith("owner-request", {+ expect(mocks.resumeHook).toHaveBeenCalledWith("owner-inbox", {+ kind: "request",from,replyTo: "agent-reply",request: {@@ -503,7 +480,10 @@ describe("background agent invocation routing", () => {kind: "authorization-request",},});- expect(mocks.resumeHook).not.toHaveBeenCalledWith("owner-report", expect.anything());+ expect(mocks.resumeHook).not.toHaveBeenCalledWith(+ "owner-inbox",+ expect.objectContaining({ kind: "report" }),+ );});it("forwards task-owned child updates to the workflow tool report channel", async () => {@@ -553,10 +533,7 @@ describe("background agent invocation routing", () => {admission: Promise.resolve({ status: "accepted" }),from,owner: {- admission: "owner-admission",- outcome: "owner-outcome",- report: "owner-report",- request: "owner-request",+ inbox: "owner-inbox",},});@@ -564,7 +541,8 @@ describe("background agent invocation routing", () => {agent(ctx, { key: "research", message: "Find it", target: "research" }),).resolves.toBe("done");- expect(mocks.resumeHook).toHaveBeenCalledWith("owner-report", {+ expect(mocks.resumeHook).toHaveBeenCalledWith("owner-inbox", {+ kind: "report",from,update: "Still working",});diff --git a/packages/eve/src/execution/tools/subagent/invoke-agent.ts b/packages/eve/src/execution/tools/subagent/invoke-agent.tsindex 1800dd717..83396e016 100644--- a/packages/eve/src/execution/tools/subagent/invoke-agent.ts+++ b/packages/eve/src/execution/tools/subagent/invoke-agent.ts@@ -14,7 +14,7 @@ import { resumeHookStep } from "#execution/tools/workflow/resume-hook-step.js";import type { RuntimeSubagentChildResult, RuntimeSubagentResult } from "#shared/action-types.js";import type { JsonValue } from "#shared/json.js";import type { JsonObject } from "#shared/json.js";-import { claimHookOwnership, disposeHook } from "#execution/hook-ownership.js";+import { disposeHook } from "#execution/hook-ownership.js";import type { AgentInput } from "#tools/workflow-definition.js";import type { ToolContext } from "#tools/definition.js";import type { TaskInboundUpdate } from "#tasks/types.js";@@ -94,16 +94,14 @@ export async function invokeAgent(const owner = readWorkflowToolRunOwner(ctx);const admission = readWorkflowToolRunAdmission(ctx);claimInvocationId(ctx, options.invocationId);+ if (admission !== undefined) {+ const admitted = await admission;+ if (admitted.status === "rejected") throw new Error(admitted.reason);+ }const replies = createHook<AgentInvocationReply>();- let ownsReplies = false;try {- await claimHookOwnership(replies);- ownsReplies = true;- if (admission !== undefined) {- const admitted = await admission;- if (admitted.status === "rejected") throw new Error(admitted.reason);- }- await resumeHookStep(owner.request, {+ await resumeHookStep(owner.inbox, {+ kind: "request",from: run,replyTo: replies.token,request: { input, invocationId: options.invocationId, kind: "agent-invoke" },@@ -121,7 +119,8 @@ export async function invokeAgent();if (result !== undefined) {if (result.origin === "child") {- await resumeHookStep(owner.request, {+ await resumeHookStep(owner.inbox, {+ kind: "request",from: run,replyTo: replies.token,request: { kind: "agent-settled", result },@@ -134,7 +133,8 @@ export async function invokeAgent(continue;}if (reply.kind === "subagent-input-request") {- await resumeHookStep(owner.request, {+ await resumeHookStep(owner.inbox, {+ kind: "request",from: run,replyTo: reply.childContinuationToken,request: {@@ -150,25 +150,25 @@ export async function invokeAgent(continue;}if (reply.kind === "task-update") {- await resumeHookStep(owner.report, {+ await resumeHookStep(owner.inbox, {+ kind: "report",from: run,update: reply.message,});continue;}- await resumeHookStep(owner.request, {+ await resumeHookStep(owner.inbox, {+ kind: "request",from: run,replyTo: replies.token,request: { event: reply, kind: "authorization-request" },});}} finally {- if (ownsReplies) {- try {- await disposeHook(replies);- } catch {- // A result or invocation error is authoritative; reply-hook cleanup is best effort.- }+ try {+ await disposeHook(replies);+ } catch {+ // A result or invocation error is authoritative; reply-hook cleanup is best effort.}}throw new Error(`Agent "${input.target}" closed without a result.`);diff --git a/packages/eve/src/execution/tools/workflow/ask.ts b/packages/eve/src/execution/tools/workflow/ask.tsindex 40bb2e777..c9ed85df3 100644--- a/packages/eve/src/execution/tools/workflow/ask.ts+++ b/packages/eve/src/execution/tools/workflow/ask.ts@@ -5,7 +5,6 @@ import type {WorkflowToolRunRef,} from "#execution/tools/workflow/messages.js";import { resumeHookStep } from "#execution/tools/workflow/resume-hook-step.js";-import { workflowToolRunAnswerToken } from "#harness/workflow-tool-runs.js";import type { ToolContext, ToolInputRequest, ToolInputResponse } from "#tools/definition.js";import { workflowToolContextErrorMessage } from "#shared/workflow-tool-context.js";@@ -18,7 +17,6 @@ interface WorkflowToolRunContext {readonly admission?: Promise<{ readonly status: "accepted" } | { readonly status: "rejected"; readonly reason: string }>;- answerSeq: number;readonly from: WorkflowToolRunRef;readonly owner: WorkflowToolRunOwner;}@@ -29,11 +27,11 @@ type WorkflowToolRunContextCarrier = {export function attachWorkflowToolRunContext(ctx: ToolContext,- context: Omit<WorkflowToolRunContext, "answerSeq">,+ context: WorkflowToolRunContext,): void {Object.defineProperty(ctx, WORKFLOW_TOOL_RUN_CONTEXT, {enumerable: false,- value: { ...context, answerSeq: 0 },+ value: context,});}@@ -65,10 +63,9 @@ export function readWorkflowToolRunAdmission(/** Returns an answer hook which may be awaited or raced with another workflow operation. */export function ask(ctx: ToolContext, request: ToolInputRequest): Hook<ToolInputResponse> {const context = readWorkflowToolRunContext(ctx, "ask");- const answer = createHook<ToolInputResponse>({- token: workflowToolRunAnswerToken(context.from.runId, context.answerSeq++),- });- void resumeHookStep(context.owner.request, {+ const answer = createHook<ToolInputResponse>();+ void resumeHookStep(context.owner.inbox, {+ kind: "request",from: context.from,replyTo: answer.token,request: { kind: "ask", request },diff --git a/packages/eve/src/execution/tools/workflow/body.test.ts b/packages/eve/src/execution/tools/workflow/body.test.tsindex c00cc704e..881006d6b 100644--- a/packages/eve/src/execution/tools/workflow/body.test.ts+++ b/packages/eve/src/execution/tools/workflow/body.test.ts@@ -21,7 +21,7 @@ it("binds workflow-only methods to the run context", async () => {stepIndex: 0,toolName: "deploy",workflowId: "workflow//test//execute",- owner: { report: "report" },+ owner: { inbox: "inbox" },execution: "blocking",runId: "run",} as WorkflowBodyInput & { execution: "blocking"; runId: string };diff --git a/packages/eve/src/execution/tools/workflow/body.ts b/packages/eve/src/execution/tools/workflow/body.tsindex 8cdf666fd..7bfbd7386 100644--- a/packages/eve/src/execution/tools/workflow/body.ts+++ b/packages/eve/src/execution/tools/workflow/body.ts@@ -71,7 +71,7 @@ export async function executeWorkflowBody(while (next.done !== true) {last = next.value;const report: WorkflowToolRunReport = { from, update: next.value };- await resumeHookStep(input.owner.report, report);+ await resumeHookStep(input.owner.inbox, { kind: "report", ...report });reportCount += 1;next = await iterator.next();}diff --git a/packages/eve/src/execution/tools/workflow/cancel.test.ts b/packages/eve/src/execution/tools/workflow/cancel.test.tsnew file mode 100644index 000000000..95657b873--- /dev/null+++ b/packages/eve/src/execution/tools/workflow/cancel.test.ts@@ -0,0 +1,64 @@+import { beforeEach, describe, expect, it, vi } from "vitest";++import { EntityConflictError, HookNotFoundError } from "#compiled/@workflow/errors/index.js";+import { cancelRun, getWorld, resumeHook } from "#internal/workflow/runtime.js";+import { logError } from "#internal/logging.js";+import { cancelWorkflowToolRun } from "./cancel.js";++vi.mock("#compiled/@workflow/core/runtime.js", () => ({+ cancelRun: vi.fn(),+ getWorld: vi.fn(),+ resumeHook: vi.fn(),+}));++vi.mock("#internal/logging.js", () => ({+ createLogger: vi.fn(() => ({})),+ logError: vi.fn(),+}));++const world = {} as Awaited<ReturnType<typeof getWorld>>;+const address = { hookToken: "generated-control-token", runId: "tool-run" };+const reason = "The calling turn was cancelled.";++describe("cancelWorkflowToolRun", () => {+ beforeEach(() => {+ vi.resetAllMocks();+ vi.mocked(getWorld).mockResolvedValue(world);+ });++ it("lets a registered workflow cancel cooperatively", async () => {+ await cancelWorkflowToolRun(address, reason);++ expect(resumeHook).toHaveBeenCalledWith(address.hookToken, { kind: "cancel", reason });+ expect(cancelRun).not.toHaveBeenCalled();+ });++ it("cancels by run ID before the control hook is registered", async () => {+ vi.mocked(resumeHook).mockRejectedValue(new HookNotFoundError(address.hookToken));++ await cancelWorkflowToolRun(address, reason);++ expect(cancelRun).toHaveBeenCalledWith(world, address.runId, { cancelReason: reason });+ expect(logError).not.toHaveBeenCalled();+ });++ it("ignores a run that finishes before the cancellation fallback", async () => {+ vi.mocked(resumeHook).mockRejectedValue(new HookNotFoundError(address.hookToken));+ vi.mocked(cancelRun).mockRejectedValue(new EntityConflictError("Run already completed"));++ await expect(cancelWorkflowToolRun(address, reason)).resolves.toBeUndefined();++ expect(cancelRun).toHaveBeenCalledOnce();+ expect(logError).not.toHaveBeenCalled();+ });++ it("falls back on delivery errors and logs an unsuccessful cancellation", async () => {+ vi.mocked(resumeHook).mockRejectedValue(new Error("hook delivery unavailable"));+ vi.mocked(cancelRun).mockRejectedValue(new Error("run cancellation unavailable"));++ await expect(cancelWorkflowToolRun(address, reason)).resolves.toBeUndefined();++ expect(cancelRun).toHaveBeenCalledWith(world, address.runId, { cancelReason: reason });+ expect(logError).toHaveBeenCalledTimes(2);+ });+});diff --git a/packages/eve/src/execution/tools/workflow/cancel.ts b/packages/eve/src/execution/tools/workflow/cancel.tsindex 058f45f42..82b50424f 100644--- a/packages/eve/src/execution/tools/workflow/cancel.ts+++ b/packages/eve/src/execution/tools/workflow/cancel.ts@@ -20,10 +20,17 @@ export async function cancelWorkflowToolRun(await resumeHook(run.hookToken, cancel);return;} catch (error) {- if (isTaskWorkflowTargetGone(error)) return;- logError(log, "failed to signal a workflow tool run to cancel; cancelling it outright", error, {- runId: run.runId,- });+ // A fresh run may not have registered its control hook yet.+ if (!isTaskWorkflowTargetGone(error)) {+ logError(+ log,+ "failed to signal a workflow tool run to cancel; cancelling it outright",+ error,+ {+ runId: run.runId,+ },+ );+ }}try {diff --git a/packages/eve/src/execution/tools/workflow/messages.ts b/packages/eve/src/execution/tools/workflow/messages.tsindex b2657fdac..d6b7b5ec1 100644--- a/packages/eve/src/execution/tools/workflow/messages.ts+++ b/packages/eve/src/execution/tools/workflow/messages.ts@@ -10,10 +10,7 @@ import type { JsonObject, JsonValue } from "#shared/json.js";import type { ToolInputRequest } from "#tools/definition.js";export interface WorkflowToolRunOwner {- readonly admission: string;- readonly outcome: string;- readonly report: string;- readonly request: string;+ readonly inbox: string;}/**@@ -93,18 +90,12 @@ export interface WorkflowToolRunOutcomeMessage {readonly result: WorkflowToolRunOutcome;}-export const workflowToolRunReportHook = defineHook<WorkflowToolRunReport>();-export const workflowToolRunRequestHook = defineHook<WorkflowToolRunRequestMessage>();-export const workflowToolRunOutcomeHook = defineHook<WorkflowToolRunOutcomeMessage>();+export type WorkflowToolRunMessage =+ | ({ readonly kind: "report" } & WorkflowToolRunReport)+ | ({ readonly kind: "request" } & WorkflowToolRunRequestMessage)+ | ({ readonly kind: "outcome" } & WorkflowToolRunOutcomeMessage);-export function deriveWorkflowToolRunOwner(inboxToken: string): WorkflowToolRunOwner {- return {- admission: `${inboxToken}:admission`,- outcome: `${inboxToken}:outcome`,- report: `${inboxToken}:report`,- request: `${inboxToken}:request`,- };-}+export const workflowToolRunHook = defineHook<WorkflowToolRunMessage>();export type WorkflowToolRunControlMessage = { readonly kind: "cancel"; readonly reason: string };diff --git a/packages/eve/src/execution/tools/workflow/owner.ts b/packages/eve/src/execution/tools/workflow/owner.tsindex 93f7833fe..54bb3fc36 100644--- a/packages/eve/src/execution/tools/workflow/owner.ts+++ b/packages/eve/src/execution/tools/workflow/owner.ts@@ -1,45 +1,22 @@-import { disposeHook } from "#execution/hook-ownership.js";import {- deriveWorkflowToolRunOwner,- workflowToolRunOutcomeHook,- workflowToolRunReportHook,- workflowToolRunRequestHook,- type WorkflowToolRunOutcomeMessage,- type WorkflowToolRunReport,- type WorkflowToolRunRequestMessage,+ workflowToolRunHook,+ type WorkflowToolRunMessage,+ type WorkflowToolRunOwner,} from "#execution/tools/workflow/messages.js";import {type ChannelReader,createChannelReader,} from "#execution/tools/workflow/owner-channels.js";-/** In read priority: what a workflow tool run said before its outcome lands first. */-export type WorkflowToolRunOwnerReaders = readonly [- ChannelReader<"report", WorkflowToolRunReport>,- ChannelReader<"request", WorkflowToolRunRequestMessage>,- ChannelReader<"outcome", WorkflowToolRunOutcomeMessage>,-];--export interface WorkflowToolRunOwnerChannels {- readonly readers: WorkflowToolRunOwnerReaders;- dispose(): Promise<void>;+export interface WorkflowToolRunOwnerInbox {+ readonly owner: WorkflowToolRunOwner;+ readonly reader: ChannelReader<"workflow", WorkflowToolRunMessage>;}-export function openWorkflowToolRunOwnerChannels(inboxToken: string): WorkflowToolRunOwnerChannels {- const owner = deriveWorkflowToolRunOwner(inboxToken);- const report = workflowToolRunReportHook.create({ token: owner.report });- const request = workflowToolRunRequestHook.create({ token: owner.request });- const outcome = workflowToolRunOutcomeHook.create({ token: owner.outcome });+export function openWorkflowToolRunOwnerInbox(): WorkflowToolRunOwnerInbox {+ const hook = workflowToolRunHook.create();return {- readers: [- createChannelReader("report", report),- createChannelReader("request", request),- createChannelReader("outcome", outcome),- ],- async dispose() {- await disposeHook(report);- await disposeHook(request);- await disposeHook(outcome);- },+ owner: { inbox: hook.token },+ reader: createChannelReader("workflow", hook),};}diff --git a/packages/eve/src/execution/tools/workflow/run-control.ts b/packages/eve/src/execution/tools/workflow/run-control.tsindex a60b45d80..6d846ed10 100644--- a/packages/eve/src/execution/tools/workflow/run-control.ts+++ b/packages/eve/src/execution/tools/workflow/run-control.ts@@ -1,4 +1,4 @@-import { createHook, type Hook } from "#compiled/@workflow/core/index.js";+import { createHook } from "#compiled/@workflow/core/index.js";import {isWorkflowToolRunControlMessage,@@ -10,18 +10,14 @@ import {* replay, so `cancelled` must be raced for a cancel to be observed at all.*/export interface WorkflowToolRunControlInbox {- readonly hook: Hook<WorkflowToolRunControlMessage>;readonly signal: AbortSignal;/** Rejects with {@link WorkflowToolRunCancelledError} when a cancel message arrives; never resolves. */readonly cancelled: Promise<never>;reason(): string | undefined;}-/** Opens without claiming, so a losing duplicate start never trips the signal. */export function openWorkflowToolRunControlInbox(hookToken: string): WorkflowToolRunControlInbox {const hook = createHook<WorkflowToolRunControlMessage>({ token: hookToken });- // Hook promises and iterators share one durable cursor. Create the iterator- // before the caller claims so conflict replay is consumed by getConflict().const iterator = hook[Symbol.asyncIterator]();const controller = new AbortController();let cancelReason: string | undefined;@@ -35,7 +31,6 @@ export function openWorkflowToolRunControlInbox(hookToken: string): WorkflowToolreturn {cancelled,- hook,reason: () => cancelReason,signal: controller.signal,};@@ -47,12 +42,7 @@ async function consumeCancel(onCancel: (reason: string) => void,): Promise<never> {while (true) {- let next: IteratorResult<WorkflowToolRunControlMessage>;- try {- next = await iterator.next();- } catch {- return await new Promise<never>(() => {});- }+ const next = await iterator.next();if (next.done === true) return await new Promise<never>(() => {});if (!isWorkflowToolRunControlMessage(next.value)) continue;onCancel(next.value.reason);diff --git a/packages/eve/src/execution/tools/workflow/start.test.ts b/packages/eve/src/execution/tools/workflow/start.test.tsnew file mode 100644index 000000000..65c017599--- /dev/null+++ b/packages/eve/src/execution/tools/workflow/start.test.ts@@ -0,0 +1,64 @@+import { afterEach, describe, expect, it, vi } from "vitest";++import { startWorkflowToolRun } from "./start.js";+import type { WorkflowToolRunInput } from "./types.js";+import {+ startWorkflowOnCurrentDeployment,+ workflowToolRunWorkflowReference,+} from "#execution/workflow-runtime.js";++vi.mock("#execution/workflow-runtime.js", () => ({+ startWorkflowOnCurrentDeployment: vi.fn(),+ workflowToolRunWorkflowReference: { workflowId: "workflow//eve//workflowToolRunWorkflow" },+}));++const input: Omit<WorkflowToolRunInput, "hookToken"> = {+ callId: "call-1",+ input: { service: "api" },+ owner: { inbox: "owner-inbox" },+ session: {+ auth: { current: null, initiator: null },+ id: "session-1",+ turn: { id: "turn-1", sequence: 0 },+ },+ stepIndex: 0,+ toolName: "deploy",+ workflowId: "workflow//test//deploy",+};++describe("startWorkflowToolRun", () => {+ afterEach(() => vi.resetAllMocks());++ it("starts repeated calls independently and returns each run's cancellation address", async () => {+ const start = vi.mocked(startWorkflowOnCurrentDeployment);+ start+ .mockResolvedValueOnce({ runId: "run-1" } as never)+ .mockResolvedValueOnce({ runId: "run-2" } as never);++ const first = await startWorkflowToolRun(input);+ const second = await startWorkflowToolRun(input);++ expect(first.runId).toBe("run-1");+ expect(second.runId).toBe("run-2");+ expect(first.hookToken).toBeTruthy();+ expect(second.hookToken).not.toBe(first.hookToken);+ for (const [index, address] of [first, second].entries()) {+ expect(start).toHaveBeenNthCalledWith(index + 1, workflowToolRunWorkflowReference, [+ { ...input, hookToken: address.hookToken },+ ]);+ }+ });++ it("uses a new token after an ambiguous start failure", async () => {+ const start = vi.mocked(startWorkflowOnCurrentDeployment);+ const failure = new Error("start response lost");+ start.mockRejectedValueOnce(failure).mockResolvedValueOnce({ runId: "retry-run" } as never);++ await expect(startWorkflowToolRun(input)).rejects.toBe(failure);+ const retried = await startWorkflowToolRun(input);+ const firstInput = start.mock.calls[0]![1][0] as WorkflowToolRunInput;++ expect(retried.runId).toBe("retry-run");+ expect(retried.hookToken).not.toBe(firstInput.hookToken);+ });+});diff --git a/packages/eve/src/execution/tools/workflow/start.ts b/packages/eve/src/execution/tools/workflow/start.tsindex d6d5f83af..d4ca99998 100644--- a/packages/eve/src/execution/tools/workflow/start.ts+++ b/packages/eve/src/execution/tools/workflow/start.ts@@ -8,42 +8,27 @@ import type {RuntimeWorkflowTaskRequest,} from "#shared/action-types.js";import { toError } from "#shared/errors.js";-import type { WorkflowToolRunInput } from "#execution/tools/workflow/types.js";-import { deriveWorkflowToolRunOwner } from "#execution/tools/workflow/messages.js";+import type {+ WorkflowToolRunAddress,+ WorkflowToolRunInput,+} from "#execution/tools/workflow/types.js";+import type { WorkflowToolRunOwner } from "#execution/tools/workflow/messages.js";import {startWorkflowOnCurrentDeployment,workflowToolRunWorkflowReference,- waitForCommandHookOwner,} from "#execution/workflow-runtime.js";-import { deriveAgentOperationId } from "#subagents/handles/operation-id.js";const log = createLogger("execution.workflow-tool-run");-// Derived from the call alone so a replayed dispatch starts a duplicate that-// loses the claim and still resolves to the workflow tool run that owns the call.-export function deriveWorkflowToolRunHookToken(input: {- readonly callId: string;- readonly parentSessionId: string;- readonly parentTurnId: string;-}): string {- return `eve:workflow-tool-run:${deriveAgentOperationId(input)}`;-}--/** Resolves once the workflow tool run owns its hook. Call from a `"use step"` body. */+/** Starts a new run for each dispatch attempt. Call from a `"use step"` body. */export async function startWorkflowToolRun(- input: Omit<WorkflowToolRunInput, "hookToken"> & { readonly hookToken?: string },-): Promise<{ readonly hookToken: string; readonly runId: string }> {- const hookToken =- input.hookToken ??- deriveWorkflowToolRunHookToken({- callId: input.callId,- parentSessionId: input.session.id,- parentTurnId: input.session.turn.id,- });- const workflowToolRunInput = { ...input, hookToken } as WorkflowToolRunInput;- await startWorkflowOnCurrentDeployment(workflowToolRunWorkflowReference, [workflowToolRunInput]);- const owner = await waitForCommandHookOwner(hookToken);- return { hookToken, runId: owner.runId };+ input: Omit<WorkflowToolRunInput, "hookToken">,+): Promise<WorkflowToolRunAddress> {+ const hookToken = crypto.randomUUID();+ const run = await startWorkflowOnCurrentDeployment(workflowToolRunWorkflowReference, [+ { ...input, hookToken },+ ]);+ return { hookToken, runId: run.runId };}/** Starts one durable workflow task and records it on the owning session. */@@ -55,7 +40,7 @@ export async function startWorkflowTask(input: {readonly turnId: string;};readonly initiatorAuth: SessionAuth["initiator"];- readonly parentContinuationToken: string;+ readonly owner: WorkflowToolRunOwner;readonly parentSession: SessionParent | undefined;readonly session: RuntimeSession;readonly task: RuntimeWorkflowTaskRequest;@@ -66,7 +51,7 @@ export async function startWorkflowTask(input: {callId: task.callId,executeInput: task.executeInput,input: task.input,- owner: deriveWorkflowToolRunOwner(input.parentContinuationToken),+ owner: input.owner,resultKind: task.resultKind,session: {auth: { current: input.auth, initiator: input.initiatorAuth },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.tsindex 757575970..16f765506 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@@ -1,4 +1,4 @@-import { describe, expect, it } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";import { sessionCommandHookToken } from "#execution/session-command-token.js";import { executeSleepTool, SLEEP_INPUT_SCHEMA } from "#execution/tools/sleep.js";@@ -36,6 +36,7 @@ const DEPLOY_INPUT_SCHEMA = toInputSchema({});function buildSerializedContext(input: {+ readonly acceptedDeploymentId?: string;readonly continuationToken: string;readonly mode: "conversation" | "task";readonly requestInput?: boolean;@@ -45,6 +46,16 @@ function buildSerializedContext(input: {"eve.bundle": { source: createBundledRuntimeCompiledArtifactsSource() },"eve.capabilities": { requestInput: input.requestInput ?? false },"eve.channel": { kind: "http", state: {} },+ ...(input.acceptedDeploymentId === undefined+ ? {}+ : {+ "eve.channelDelivery": {+ acceptedDeploymentId: input.acceptedDeploymentId,+ channelKind: "http",+ channelName: "test",+ deliveryId: "delivery-initial",+ },+ }),"eve.continuationToken": input.continuationToken,"eve.mode": input.mode,};@@ -125,6 +136,7 @@ function eventsText(events: readonly { readonly data?: unknown }[]): string {}describe("workflow tools", () => {+ afterEach(() => vi.unstubAllEnvs());it("runs the framework sleep tool through the workflow tool path", async () => {const runtime = await createWorkflowToolRuntime({agentName: "workflow-tool-sleep",@@ -199,58 +211,84 @@ describe("workflow tools", () => {expect(output).toContain("deploy of api exploded");});- it("routes a human answer to the waiting workflow body", async () => {- const runtime = await createWorkflowToolRuntime({- agentName: "workflow-tool-hitl",- execute: confirmDeployWorkflow,- toolName: "confirm_deploy",- });-- await runtime.run(async () => {- const run = await start(workflowEntry, [- {- input: { message: 'Run confirm_deploy with service "api"' },- serializedContext: buildSerializedContext({- continuationToken: "http:workflow-tool-hitl",- mode: "conversation",- requestInput: true,- }),- },- ]);- const stream = captureTurnEvents(run);-- try {- const asked = await stream.nextTurn();- const requested = filterEventsByType(asked, "input.requested");- expect(requested).toHaveLength(1);- const request = (requested[0] as InputRequestedStreamEvent).data.requests[0]!;- expect(request).toMatchObject({- action: { input: { service: "api" }, kind: "tool-call", toolName: "confirm_deploy" },- display: "confirmation",- kind: "question",- prompt: "Apply plan:api?",- });- expect(request.options?.map((option) => option.id)).toEqual(["approve", "cancel"]);-- const commandToken = sessionCommandHookToken(run.runId);- await waitForHook(run, { token: commandToken });- await resumeSessionInbox(commandToken, {- kind: "send",- payload: { inputResponses: [{ optionId: "approve", requestId: request.requestId }] },- });-- const answered = await stream.nextTurn();- const results = filterEventsByType(answered, "action.result");- expect(results.map((event) => JSON.stringify(event.data.result.output))).toContainEqual(- JSON.stringify({ approved: true, service: "api" }),- );- expect(filterEventsByType(answered, "turn.failed")).toHaveLength(0);- } finally {- stream.dispose();- await run.cancel();- }- });- }, 60_000);+ it.each(["inline", "child"] as const)(+ "routes workflow reports, human input, and outcome on the %s owner",+ async (owner) => {+ vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");+ const runtime = await createWorkflowToolRuntime({+ agentName: "workflow-tool-hitl",+ execute: confirmDeployWorkflow,+ toolName: "confirm_deploy",+ });++ await runtime.run(async () => {+ const run = await start(workflowEntry, [+ {+ input: { message: 'Run confirm_deploy with service "api"' },+ serializedContext: buildSerializedContext({+ acceptedDeploymentId: owner === "inline" ? "dpl_inline" : undefined,+ continuationToken: "http:workflow-tool-hitl",+ mode: "conversation",+ requestInput: true,+ }),+ },+ ]);+ const stream = captureTurnEvents(run);++ try {+ const asked = await stream.nextTurn();+ const world = await getWorld();+ const parentHooks = (await world.hooks.list({ runId: run.runId })).data;+ expect(+ parentHooks.some((hook) => hook.token === `${run.runId}:turn-control:0:inbox`),+ ).toBe(owner === "inline");+ expect(+ filterEventsByType(asked, "action.partial").map((event) => event.data.result.output),+ ).toEqual(["awaiting approval"]);+ const requested = filterEventsByType(asked, "input.requested");+ expect(requested).toHaveLength(1);+ const request = (requested[0] as InputRequestedStreamEvent).data.requests[0]!;+ expect(request).toMatchObject({+ action: { input: { service: "api" }, kind: "tool-call", toolName: "confirm_deploy" },+ display: "confirmation",+ kind: "question",+ prompt: "Apply plan:api?",+ });+ expect(request.options?.map((option) => option.id)).toEqual(["approve", "cancel"]);++ const commandToken = sessionCommandHookToken(run.runId);+ await waitForHook(run, { token: commandToken });+ await resumeSessionInbox(commandToken, {+ kind: "send",+ payload: { inputResponses: [{ optionId: "approve", requestId: request.requestId }] },+ });++ const answered = await stream.nextTurn();+ const progress = answered.findIndex(+ (event) =>+ event.type === "action.partial" && event.data.result.output === "approval received",+ );+ const resultIndex = answered.findIndex(+ (event) =>+ event.type === "action.result" &&+ event.data.result.kind === "tool-result" &&+ event.data.result.toolName === "confirm_deploy",+ );+ expect(progress).toBeGreaterThanOrEqual(0);+ expect(resultIndex).toBeGreaterThan(progress);+ const results = filterEventsByType(answered, "action.result");+ expect(results.map((event) => JSON.stringify(event.data.result.output))).toContainEqual(+ JSON.stringify({ approved: true, service: "api" }),+ );+ expect(filterEventsByType(answered, "turn.failed")).toHaveLength(0);+ } finally {+ stream.dispose();+ await run.cancel();+ }+ });+ },+ 60_000,+ );it("times out a hook raced against a sleep when it is not resumed", async () => {const runtime = await createWorkflowToolRuntime({@@ -321,102 +359,115 @@ describe("workflow tools", () => {});}, 30_000);- it("cancels the run when the waiting turn is cancelled and lets the body clean up", async () => {- const runtime = await createWorkflowToolRuntime({- agentName: "workflow-tool-cancel",- execute: holdUntilAbortedWorkflow,- toolName: "deploy_service",- });-- await runtime.run(async () => {- const before = await listWorkflowToolRunIds();- const run = await start(workflowEntry, [- {- input: { message: 'Run deploy_service with service "api"' },- serializedContext: buildSerializedContext({- continuationToken: "http:workflow-tool-cancel",- mode: "conversation",- }),- },- ]);- const stream = captureTurnEvents(run);-- try {- const workflowToolRunId = await waitForNewWorkflowToolRun(before);- const commandToken = sessionCommandHookToken(run.runId);- await waitForHook(run, { token: commandToken });- await resumeSessionInbox(commandToken, { kind: "cancel", turnId: "turn_0" });-- // The body is holding in a step that received ctx.abortSignal, so the- // run ends well inside its grace period once the step rejects and- // `finally` runs; a run that ignored the signal would still be running.- expect(await waitForWorkflowToolRunTerminal(workflowToolRunId)).toBe("completed");-- await resumeSessionInbox(commandToken, {- kind: "send",- payload: { message: "Thanks, no deploy today." },- });- const next = await stream.nextTurn();- expect(filterEventsByType(next, "turn.started")).toHaveLength(1);- expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);- expect(next.at(-1)?.type).toBe("session.waiting");- } finally {- stream.dispose();- await run.cancel();- }- });- }, 60_000);-- it("runs a background workflow tool as its task's executor", async () => {- const runtime = await createWorkflowToolRuntime({- agentName: "workflow-tool-background",- execute: backgroundDeployWorkflow,- toolName: "report_deploy",- });-- await runtime.run(async () => {- enableBackgroundTool(runtime, "report_deploy");-- const run = await start(workflowEntry, [- {- input: { message: 'Run report_deploy with service "api"' },- serializedContext: buildSerializedContext({- continuationToken: "http:workflow-tool-background",- mode: "conversation",- }),- },- ]);- const stream = captureTurnEvents(run);-- try {- const receiptTurn = await stream.nextTurn();- const receipt = filterEventsByType(receiptTurn, "action.result").find(- (event) => event.data.result.kind === "tool-result",- );- expect(receipt?.data.result.output).toMatchObject({ status: "working" });- expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);+ it.each(["inline", "child"] as const)(+ "cancels the run when the waiting turn is cancelled and lets the body clean up (%s)",+ async (owner) => {+ vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");+ const runtime = await createWorkflowToolRuntime({+ agentName: "workflow-tool-cancel",+ execute: holdUntilAbortedWorkflow,+ toolName: "deploy_service",+ });++ await runtime.run(async () => {+ const before = await listWorkflowToolRunIds();+ const run = await start(workflowEntry, [+ {+ input: { message: 'Run deploy_service with service "api"' },+ serializedContext: buildSerializedContext({+ acceptedDeploymentId: owner === "inline" ? "dpl_inline" : undefined,+ continuationToken: "http:workflow-tool-cancel",+ mode: "conversation",+ }),+ },+ ]);+ const stream = captureTurnEvents(run);++ try {+ const workflowToolRunId = await waitForNewWorkflowToolRun(before);+ await waitForHook({ runId: workflowToolRunId });+ const commandToken = sessionCommandHookToken(run.runId);+ await waitForHook(run, { token: commandToken });+ await resumeSessionInbox(commandToken, { kind: "cancel", turnId: "turn_0" });++ // The body is holding in a step that received ctx.abortSignal, so the+ // run ends well inside its grace period once the step rejects and+ // `finally` runs; a run that ignored the signal would still be running.+ expect(await waitForWorkflowToolRunTerminal(workflowToolRunId)).toBe("completed");++ await resumeSessionInbox(commandToken, {+ kind: "send",+ payload: { message: "Thanks, no deploy today." },+ });+ const next = await stream.nextTurn();+ expect(filterEventsByType(next, "turn.started")).toHaveLength(1);+ expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);+ expect(next.at(-1)?.type).toBe("session.waiting");+ } finally {+ stream.dispose();+ await run.cancel();+ }+ });+ },+ 60_000,+ );- const notifications: string[] = [];- for (- let turn = 0;- turn < 3 && !notifications.some((text) => text.includes("is completed"));- turn += 1- ) {- const woken = await stream.nextTurn();- expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);- notifications.push(eventsText(filterEventsByType(woken, "message.received")));+ it.each(["inline", "child"] as const)(+ "runs a background workflow tool as its task's executor (%s)",+ async (owner) => {+ vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");+ const runtime = await createWorkflowToolRuntime({+ agentName: "workflow-tool-background",+ execute: backgroundDeployWorkflow,+ toolName: "report_deploy",+ });++ await runtime.run(async () => {+ enableBackgroundTool(runtime, "report_deploy");++ const run = await start(workflowEntry, [+ {+ input: { message: 'Run report_deploy with service "api"' },+ serializedContext: buildSerializedContext({+ acceptedDeploymentId: owner === "inline" ? "dpl_inline" : undefined,+ continuationToken: "http:workflow-tool-background",+ mode: "conversation",+ }),+ },+ ]);+ const stream = captureTurnEvents(run);++ try {+ const receiptTurn = await stream.nextTurn();+ const receipt = filterEventsByType(receiptTurn, "action.result").find(+ (event) => event.data.result.kind === "tool-result",+ );+ expect(receipt?.data.result.output).toMatchObject({ status: "working" });+ expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);++ const notifications: string[] = [];+ for (+ let turn = 0;+ turn < 3 && !notifications.some((text) => text.includes("is completed"));+ turn += 1+ ) {+ const woken = await stream.nextTurn();+ expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);+ notifications.push(eventsText(filterEventsByType(woken, "message.received")));+ }+ const text = notifications.join("\n");+ expect(text).toContain("Review plan:api");+ expect(text).not.toContain("update: planned api");+ expect(text).toContain("is completed");+ expect(text).toContain("plan:api");+ } finally {+ stream.dispose();+ await run.cancel();}- const text = notifications.join("\n");- expect(text).toContain("Review plan:api");- expect(text).not.toContain("update: planned api");- expect(text).toContain("is completed");- expect(text).toContain("plan:api");- } finally {- stream.dispose();- await run.cancel();- }- });- }, 90_000);+ });+ },+ 90_000,+ );it("streams a waiting tool's yields as action.partial and settles with its return", async () => {const runtime = await createWorkflowToolRuntime({diff --git a/packages/eve/src/execution/tools/workflow/workflow.ts b/packages/eve/src/execution/tools/workflow/workflow.tsindex f322b895a..e0bfa6cac 100644--- a/packages/eve/src/execution/tools/workflow/workflow.ts+++ b/packages/eve/src/execution/tools/workflow/workflow.ts@@ -1,6 +1,5 @@import { sleep as workflowSleep } from "#compiled/@workflow/core/index.js";-import { claimHookOwnership, disposeHook, isHookConflictError } from "#execution/hook-ownership.js";import type {WorkflowToolRunOutcome,WorkflowToolRunOutcomeMessage,@@ -13,46 +12,37 @@ import { normalizeSerializableError } from "#execution/workflow-errors.js";const CANCEL_GRACE = "30s";-/** Runs one authored workflow tool call and reports to its declared owner hooks. */+/** Runs one authored workflow tool call and reports to its declared owner hook. */export async function workflowToolRunWorkflow(input: WorkflowToolRunInput): Promise<void> {"use workflow";const control = openWorkflowToolRunControlInbox(input.hookToken);- let ownsInbox = false;+ const bodyInput = { ...input, execution: input.execution ?? "blocking" } as const;+ const from = createWorkflowBodyRef(bodyInput);+ const body = executeWorkflowBody(bodyInput, control.signal).then(({ outcome }) => {+ if (outcome.status === "completed") return outcome.output;+ if (outcome.status === "failed") throw outcome.error;+ throw control.signal.reason ?? new Error(outcome.reason ?? "Workflow tool run cancelled.");+ });+ const settled = body.catch(() => {});+ let outcome: WorkflowToolRunOutcome;try {- try {- await claimHookOwnership(control.hook);- ownsInbox = true;- } catch (error) {- if (isHookConflictError(error)) return;- throw error;- }-- const bodyInput = { ...input, execution: input.execution ?? "blocking" } as const;- const from = createWorkflowBodyRef(bodyInput);- const body = executeWorkflowBody(bodyInput, control.signal).then(({ outcome }) => {- if (outcome.status === "completed") return outcome.output;- if (outcome.status === "failed") throw outcome.error;- throw control.signal.reason ?? new Error(outcome.reason ?? "Workflow tool run cancelled.");- });- const settled = body.catch(() => {});- let outcome: WorkflowToolRunOutcome;- try {- outcome = { output: await Promise.race([body, control.cancelled]), status: "completed" };- } catch (error) {- if (!control.signal.aborted) {- outcome = { error: normalizeSerializableError(error), status: "failed" };- } else {- await Promise.race([settled, workflowSleep(CANCEL_GRACE)]);- outcome = { reason: control.reason(), status: "cancelled" };- }+ outcome = { output: await Promise.race([body, control.cancelled]), status: "completed" };+ } catch (error) {+ if (!control.signal.aborted) {+ outcome = { error: normalizeSerializableError(error), status: "failed" };+ } else {+ await Promise.race([settled, workflowSleep(CANCEL_GRACE)]);+ outcome = { reason: control.reason(), status: "cancelled" };}+ }- const message: WorkflowToolRunOutcomeMessage = { from, result: outcome };- await resumeHookStep(input.owner.outcome, message, {+ const message: WorkflowToolRunOutcomeMessage = { from, result: outcome };+ await resumeHookStep(+ input.owner.inbox,+ { kind: "outcome", ...message },+ {ifPresent: outcome.status === "cancelled",- });- } finally {- if (ownsInbox) await disposeHook(control.hook);- }+ },+ );}diff --git a/packages/eve/src/execution/turn-dispatch.test.ts b/packages/eve/src/execution/turn-dispatch.test.tsindex b4ded7fde..fd0f0216c 100644--- a/packages/eve/src/execution/turn-dispatch.test.ts+++ b/packages/eve/src/execution/turn-dispatch.test.ts@@ -4,7 +4,9 @@ import type { DeliverHookPayload } from "#channel/types.js";import type { DurableSessionState } from "#execution/durable-session-store.js";import { cancelAllIndexedSessionTasksStep } from "#execution/cancel-indexed-session-tasks-step.js";import { dispatchTurnStep } from "#execution/dispatch-turn-step.js";+import { forwardTurnCancellationStep } from "#execution/forward-turn-cancellation-step.js";import { forwardTurnDeliveryStep } from "#execution/forward-turn-delivery-step.js";+import { runTurnOwnedWorkflow } from "#execution/turn-workflow.js";import { dispatchAndAwaitTurn } from "#execution/turn-dispatch.js";import type { SessionCommandInbox, SessionInboxPayload } from "#execution/session-command-inbox.js";import type { TurnControlPayload } from "#execution/turn-control-protocol.js";@@ -28,6 +30,14 @@ vi.mock("./forward-turn-delivery-step.js", () => ({forwardTurnDeliveryStep: vi.fn(),}));+vi.mock("./forward-turn-cancellation-step.js", () => ({+ forwardTurnCancellationStep: vi.fn(),+}));++vi.mock("./turn-workflow.js", () => ({+ runTurnOwnedWorkflow: vi.fn(),+}));+vi.mock("./workflow-steps.js", () => ({turnStep: vi.fn(),}));@@ -36,6 +46,7 @@ describe("dispatchAndAwaitTurn", () => {beforeEach(() => {vi.clearAllMocks();createHookMock.mockReset();+ vi.mocked(runTurnOwnedWorkflow).mockImplementation(async (_input, onReady) => onReady?.());});it("rekeys the public hook when the active turn changes its continuation token", async () => {@@ -312,8 +323,10 @@ describe("dispatchAndAwaitTurn", () => {serializedContext: { state: "start" },sessionState: state,});- expect(dispatchTurnStep).toHaveBeenCalledWith(+ expect(dispatchTurnStep).not.toHaveBeenCalled();+ expect(runTurnOwnedWorkflow).toHaveBeenCalledWith(expect.objectContaining({ initialCancellation: { tasks: true } }),+ expect.any(Function),);});@@ -352,7 +365,7 @@ describe("dispatchAndAwaitTurn", () => {expect(dispatchTurnStep).not.toHaveBeenCalled();});- it("hands an already-completed complex step to the child workflow", async () => {+ it("runs coordination on the parent from an already-completed inline step", async () => {const state = createState("http:test");const result = {action: "park" as const,@@ -382,16 +395,78 @@ describe("dispatchAndAwaitTurn", () => {sessionState: state,});- expect(dispatchTurnStep).toHaveBeenCalledWith(+ expect(dispatchTurnStep).not.toHaveBeenCalled();+ expect(runTurnOwnedWorkflow).toHaveBeenCalledWith(expect.objectContaining({initialStep: {beforeStep: expect.objectContaining({ serializedContext: { state: "start" } }),result,},}),+ expect.any(Function),);});+ it("waits for inline hook initialization before forwarding a queued cancel", async () => {+ const state = createState("http:test");+ const command = Promise.withResolvers<IteratorResult<SessionInboxPayload>>();+ const forwarded = Promise.withResolvers<void>();+ vi.mocked(turnStep).mockResolvedValueOnce({+ action: "park",+ hasPendingAuthorization: false,+ hasPendingInputBatch: false,+ pendingCoordinationCallIds: ["call-1"],+ serializedContext: {},+ sessionState: state,+ });+ vi.mocked(forwardTurnCancellationStep).mockImplementationOnce(async () => {+ forwarded.resolve();+ return true;+ });+ createHookMock.mockReturnValue(+ createMockHook(async () => {+ await forwarded.promise;+ return {+ done: false,+ value: {+ action: { cancelled: true, kind: "park", serializedContext: {}, sessionState: state },+ kind: "turn-result",+ },+ };+ }),+ );+ vi.mocked(runTurnOwnedWorkflow).mockImplementationOnce(async (_input, onReady) => {+ command.resolve({ done: false, value: { kind: "cancel" } });+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(forwardTurnCancellationStep).not.toHaveBeenCalled();+ onReady?.();+ });+ let consumed = false;++ const turn = await dispatchAndAwaitTurn({+ bufferedDeliveries: [],+ bufferedSessionControls: [],+ commandInbox: createCommandInbox({+ next: () => (consumed ? new Promise(() => {}) : command.promise),+ consumeNext: () => {+ consumed = true;+ },+ }),+ controlToken: "turn-control",+ delivery: createAcceptedDelivery(),+ mode: "conversation",+ parentWritable: new WritableStream<Uint8Array>(),+ serializedContext: {},+ sessionState: state,+ });++ expect(turn.action).toMatchObject({ cancelled: true });+ expect(forwardTurnCancellationStep).toHaveBeenCalledExactlyOnceWith({+ payload: { tasks: undefined },+ token: "turn-control:cancel",+ });+ });+it("defers a guarded deployment mismatch without consuming the delivery", async () => {const state = createState("http:test");vi.mocked(turnStep).mockResolvedValueOnce({@@ -424,7 +499,7 @@ describe("dispatchAndAwaitTurn", () => {);});- it("carries inline cancellation into the continued child turn", async () => {+ it("carries inline cancellation into the shared turn runner on the parent", async () => {const state = createState("http:test");vi.mocked(turnStep).mockImplementationOnce(async (input) => {await vi.waitFor(() => expect(input.abortSignal?.aborted).toBe(true));@@ -463,13 +538,15 @@ describe("dispatchAndAwaitTurn", () => {sessionState: state,});- expect(dispatchTurnStep).toHaveBeenCalledWith(+ expect(dispatchTurnStep).not.toHaveBeenCalled();+ expect(runTurnOwnedWorkflow).toHaveBeenCalledWith(expect.objectContaining({initialCancellation: {},initialStep: expect.objectContaining({result: expect.objectContaining({ action: "cancelled" }),}),}),+ expect.any(Function),);expect(commandInbox.consumeNext).toHaveBeenCalledOnce();});diff --git a/packages/eve/src/execution/turn-dispatch.ts b/packages/eve/src/execution/turn-dispatch.tsindex 2c93f436d..e757e56f1 100644--- a/packages/eve/src/execution/turn-dispatch.ts+++ b/packages/eve/src/execution/turn-dispatch.ts@@ -2,9 +2,10 @@ import type { DeliverHookPayload, HookPayload, SessionCapabilities } from "#chanimport { dispatchTurnStep } from "#execution/dispatch-turn-step.js";import { TurnControlReceiver } from "#execution/turn-control-receiver.js";import type { DurableSessionState } from "#execution/durable-session-store.js";-import type {- InitialTurnStep,- TurnWorkflowDispatchInput,+import {+ createTurnWorkflowInput,+ type InitialTurnStep,+ type TurnWorkflowDispatchInput,} from "#execution/durable-session-migrations/turn-workflow.js";import { runInlineTurn } from "#execution/inline-turn.js";import type { SessionCommandInbox } from "#execution/session-command-inbox.js";@@ -13,6 +14,7 @@ import type { TurnCancelPayload } from "#execution/turn-cancellation-token.js";import type { TurnDriverAction } from "#execution/turn-control-receiver.js";import type { RunMode } from "#shared/run-mode.js";import { activeTurnId } from "#harness/active-turn-id.js";+import { runTurnOwnedWorkflow } from "#execution/turn-workflow.js";/** One settled turn: its terminal driver action plus deferred hook cleanup. */export interface DispatchedTurn {@@ -56,16 +58,18 @@ export async function dispatchAndAwaitTurn(input: TurnDispatchInput): Promise<Diif (inline.kind === "result") {return { action: inline.action, async dispose() {} };}- return await dispatchAndAwaitChildTurn({+ return await runAndAwaitTurn({...input,+ inline: inline.kind === "continue",initialCancellation: inline.initialCancellation,stateCursor,initialStep: inline.initialStep,});}-async function dispatchAndAwaitChildTurn(+async function runAndAwaitTurn(input: TurnDispatchInput & {+ readonly inline: boolean;readonly initialCancellation?: TurnCancelPayload;readonly initialStep?: InitialTurnStep;},@@ -82,7 +86,7 @@ async function dispatchAndAwaitChildTurn(});try {- await dispatchTurnStep({+ const turnInput = {capabilities: input.capabilities,completionToken: control.token,delivery: input.delivery,@@ -92,8 +96,21 @@ async function dispatchAndAwaitChildTurn(parentWritable: input.parentWritable,serializedContext: input.serializedContext,sessionState: input.sessionState,- } satisfies TurnWorkflowDispatchInput);- const action = await control.waitForAction();+ } satisfies TurnWorkflowDispatchInput;+ let action: TurnDriverAction;+ if (input.inline) {+ // Hook claims can suspend. Do not let a queued command allocate steps+ // midway through initialization: warm execution and cold replay can order+ // those allocations differently.+ const ready = Promise.withResolvers<void>();+ [action] = await Promise.all([+ ready.promise.then(() => control.waitForAction()),+ runTurnOwnedWorkflow(createTurnWorkflowInput(turnInput), ready.resolve),+ ]);+ } else {+ await dispatchTurnStep(turnInput);+ action = await control.waitForAction();+ }return { action, dispose: () => control.dispose() };} catch (error) {await control.dispose();diff --git a/packages/eve/src/execution/turn-workflow-tool-run.ts b/packages/eve/src/execution/turn-workflow-tool-run.tsindex b62b18a38..a0f1c27d0 100644--- a/packages/eve/src/execution/turn-workflow-tool-run.ts+++ b/packages/eve/src/execution/turn-workflow-tool-run.ts@@ -1,4 +1,6 @@+import { emitWorkflowToolRunReportStep } from "#execution/tools/workflow/emit-workflow-tool-run-report-step.js";import type {+ WorkflowToolRunMessage,WorkflowToolRunOutcomeMessage,WorkflowToolRunRequestMessage,} from "#execution/tools/workflow/messages.js";@@ -31,12 +33,32 @@ interface HandlerInput<T> {readonly message: T;}+export async function handleWorkflowToolRunMessage(+ input: HandlerInput<WorkflowToolRunMessage>,+): Promise<RuntimeActionResult | undefined> {+ const { message } = input;+ switch (message.kind) {+ case "outcome":+ return await handleWorkflowToolRunOutcome({ ...input, message });+ case "request":+ await handleWorkflowToolRunRequest({ ...input, message });+ return undefined;+ case "report":+ await emitWorkflowToolRunReportStep({+ from: message.from,+ parentWritable: input.cursor.parentWritable,+ update: message.update,+ });+ return undefined;+ }+}+/*** Settles a workflow tool run outcome against the turn's recorded runs and* returns the runtime action result the turn should accept, or `undefined`* when the outcome does not bind to a run this turn owns.*/-export async function handleWorkflowToolRunOutcome(+async function handleWorkflowToolRunOutcome(input: HandlerInput<WorkflowToolRunOutcomeMessage>,): Promise<RuntimeActionResult | undefined> {const { cursor, message } = input;@@ -98,7 +120,7 @@ async function settleSubagentOutcome(return result;}-export async function handleWorkflowToolRunRequest(+async function handleWorkflowToolRunRequest(input: HandlerInput<WorkflowToolRunRequestMessage>,): Promise<void> {const { cursor, message } = input;diff --git a/packages/eve/src/execution/turn-workflow.test.ts b/packages/eve/src/execution/turn-workflow.test.tsindex b93582cbb..827cecc65 100644--- a/packages/eve/src/execution/turn-workflow.test.ts+++ b/packages/eve/src/execution/turn-workflow.test.ts@@ -47,7 +47,7 @@ vi.mock("#compiled/@workflow/core/index.js", () => ({createHook: (...args: unknown[]) => createHookMock(...args),defineHook: () => ({create: (options?: { readonly token?: string }) => {- const token = options?.token ?? "hook";+ const token = options?.token ?? "generated-owner-token";return createDefinedHookMock(token, definedHookPayloads.get(token) ?? []);},resume: async () => null,@@ -765,7 +765,7 @@ describe("turnWorkflow", () => {expect(dispatchCoordinationStep).toHaveBeenCalledWith({action: "dispatch-workflow-tasks",callbackBaseUrl: "https://eve.example.com",- parentContinuationToken: "turn-token:inbox",+ workflowToolRunOwner: { inbox: "generated-owner-token" },parentWritable,serializedContext: { state: "pending" },sessionState: pendingState,@@ -891,6 +891,7 @@ describe("turnWorkflow", () => {const answerToken = "eve:workflow-tool-run-answer:run-1:0";const requestId = "turn-token:inbox:delivery:0";const workflowRequest = {+ kind: "request",from: {callId: "call-1",execution: "blocking" as const,@@ -910,7 +911,7 @@ describe("turnWorkflow", () => {prompt: "Approve deploy?",},};- definedHookPayloads.set("turn-token:inbox:request", [workflowRequest]);+ definedHookPayloads.set("generated-owner-token", [workflowRequest]);installInbox([{delivery: {@@ -1027,8 +1028,9 @@ describe("turnWorkflow", () => {output: "done",subagentName: "research",};- definedHookPayloads.set("turn-token:inbox:request", [+ definedHookPayloads.set("generated-owner-token", [{+ kind: "request",from: {callId: "call-1",execution: "blocking",@@ -1048,6 +1050,7 @@ describe("turnWorkflow", () => {},},{+ kind: "request",from: {callId: "call-1",execution: "blocking",@@ -1062,9 +1065,8 @@ describe("turnWorkflow", () => {replyTo: "agent-reply",request: { kind: "agent-settled", result: childResult },},- ]);- definedHookPayloads.set("turn-token:inbox:outcome", [{+ kind: "outcome",from: {callId: "call-1",execution: "blocking",@@ -1079,7 +1081,7 @@ describe("turnWorkflow", () => {result: { output: childResult, status: "completed" },},]);- installInbox([]);+ installInbox([], { stayOpen: true });vi.mocked(dispatchCoordinationStep).mockResolvedValue({results: [],sessionState: dispatchedState,@@ -1087,7 +1089,7 @@ describe("turnWorkflow", () => {});vi.mocked(applyTaskAgentRequest).mockResolvedValueOnce({ serializedContext: {}, sessionState: invokedState })- .mockResolvedValueOnce({ serializedContext: {}, sessionState: settledState });+ .mockResolvedValue({ serializedContext: {}, sessionState: settledState });vi.mocked(releaseAgentInvocationOwnerStep).mockResolvedValue({ sessionState: settledState });vi.mocked(turnStep).mockResolvedValueOnce({@@ -1119,6 +1121,12 @@ describe("turnWorkflow", () => {}),expect.objectContaining({ownerId: "run-1",+ replyTo: "agent-reply",+ request: { kind: "agent-settled", result: childResult },+ }),+ expect.objectContaining({+ ownerId: "run-1",+ replyTo: "run-1",request: { kind: "agent-settled", result: childResult },}),]);diff --git a/packages/eve/src/execution/turn-workflow.ts b/packages/eve/src/execution/turn-workflow.tsindex bb420f5dc..e4afe0d9e 100644--- a/packages/eve/src/execution/turn-workflow.ts+++ b/packages/eve/src/execution/turn-workflow.ts@@ -24,16 +24,14 @@ import { claimHookOwnership, disposeHook, isHookConflictError } from "#executionimport type { NextDriverAction } from "#execution/next-driver-action.js";import { routeDeliverToChildren } from "#execution/route-child-delivery.js";import { runProxySubagentEventStep } from "#subagents/event-proxy-step.js";-import { emitWorkflowToolRunReportStep } from "#execution/tools/workflow/emit-workflow-tool-run-report-step.js";import {type ChannelReader,createChannelReader,raceChannelReads,} from "#execution/tools/workflow/owner-channels.js";import {- openWorkflowToolRunOwnerChannels,- type WorkflowToolRunOwnerChannels,- type WorkflowToolRunOwnerReaders,+ openWorkflowToolRunOwnerInbox,+ type WorkflowToolRunOwnerInbox,} from "#execution/tools/workflow/owner.js";import {createTurnCancellationControl,@@ -46,10 +44,7 @@ import { turnStep } from "#execution/workflow-steps.js";import { activeTurnId } from "#harness/active-turn-id.js";import { resolveRuntimeActionResultsForCallIds } from "#runtime/actions/results.js";import type { RuntimeActionResult } from "#shared/action-types.js";-import {- handleWorkflowToolRunOutcome,- handleWorkflowToolRunRequest,-} from "#execution/turn-workflow-tool-run.js";+import { handleWorkflowToolRunMessage } from "#execution/turn-workflow-tool-run.js";const TASK_MODE_WAIT_ERROR_MESSAGE = "Task mode cannot wait for follow-up input (`next: null`).";@@ -76,7 +71,10 @@ export async function turnWorkflow(rawInput: unknown): Promise<void> {return runTurnOwnedWorkflow(input);}-async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {+export async function runTurnOwnedWorkflow(+ input: TurnWorkflowInput,+ onReady?: () => void,+): Promise<void> {const inbox = createHook<TurnInboxPayload>({ token: `${input.completionToken}:inbox` });// Hook promises and iterators share one durable cursor. Create the iterator before// claiming so conflict replay is consumed by getConflict(), not a later iterator read.@@ -98,7 +96,6 @@ async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {const bufferedDeliveries: DeliverHookPayload[] = [];let nextStepInput = input.stepInput.input;let ownsInbox = false;- let workflowToolRunChannels: WorkflowToolRunOwnerChannels | undefined;let cancellation: TurnCancellationControl | undefined;try {@@ -110,10 +107,8 @@ async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {throw error;}- // Opened after the inbox claim so a losing duplicate never contends for- // them; the turn starts no run before this point.- workflowToolRunChannels = openWorkflowToolRunOwnerChannels(inbox.token);- const readers: TurnReaders = [...workflowToolRunChannels.readers, inboxReader];+ const workflowToolRunInbox = openWorkflowToolRunOwnerInbox();+ const readers: TurnReaders = [workflowToolRunInbox.reader, inboxReader];// Claimed after the inbox claim so a losing duplicate run never// contends for the session cancel token.@@ -125,6 +120,8 @@ async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {});}+ onReady?.();+while (true) {const beforeStep = initialStep?.beforeStep ?? {serializedContext: cursor.serializedContext,@@ -219,7 +216,7 @@ async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {const dispatchResult = await dispatchCoordinationStep({action: result.action,callbackBaseUrl: resolveWorkflowCallbackBaseUrl(getWorkflowMetadata().url),- parentContinuationToken: inbox.token,+ workflowToolRunOwner: workflowToolRunInbox.owner,parentWritable: cursor.parentWritable,serializedContext: cursor.serializedContext,sessionState: cursor.sessionState,@@ -288,13 +285,12 @@ async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {// terminal result publishes so the next turn's claim never races this// run's teardown; this backstop covers the error path.if (cancellation !== undefined) await cancellation.dispose();- if (workflowToolRunChannels !== undefined) await workflowToolRunChannels.dispose();if (ownsInbox) await disposeHook(inbox);}}type TurnReaders = readonly [- ...WorkflowToolRunOwnerReaders,+ WorkflowToolRunOwnerInbox["reader"],ChannelReader<"inbox", TurnInboxPayload>,];@@ -399,8 +395,8 @@ async function waitForRuntimeActionResults(input: {}if (read.next.done) throw new Error("Turn inbox closed before runtime actions completed.");- if (read.channel === "outcome") {- const result = await handleWorkflowToolRunOutcome({+ if (read.channel === "workflow") {+ const result = await handleWorkflowToolRunMessage({callbackMetadataUrl: getWorkflowMetadata().url,cursor: input.cursor,message: read.next.value,@@ -411,22 +407,6 @@ async function waitForRuntimeActionResults(input: {}continue;}- if (read.channel === "request") {- await handleWorkflowToolRunRequest({- callbackMetadataUrl: getWorkflowMetadata().url,- cursor: input.cursor,- message: read.next.value,- });- continue;- }- if (read.channel === "report") {- await emitWorkflowToolRunReportStep({- from: read.next.value.from,- parentWritable: input.cursor.parentWritable,- update: read.next.value.update,- });- continue;- }const value = read.next.value;if (value.kind === "runtime-action-result") {diff --git a/packages/eve/src/execution/workflow-steps.test.ts b/packages/eve/src/execution/workflow-steps.test.tsindex 386dad153..1cb809b62 100644--- a/packages/eve/src/execution/workflow-steps.test.ts+++ b/packages/eve/src/execution/workflow-steps.test.ts@@ -905,6 +905,7 @@ describe("dispatchCoordinationStep", () => {const result = await dispatchCoordinationStep({action: "park",+ workflowToolRunOwner: { inbox: "generated-owner-token" },parentWritable: createTestWritable(),serializedContext: createSerializedContext(),sessionState,@@ -967,7 +968,7 @@ describe("dispatchCoordinationStep", () => {await expect(dispatchCoordinationStep({action: "park",- parentContinuationToken: "turn-inbox",+ workflowToolRunOwner: { inbox: "generated-owner-token" },parentWritable: createTestWritable(),serializedContext: createSerializedContext(),sessionState,@@ -1031,7 +1032,7 @@ describe("dispatchCoordinationStep", () => {await expect(dispatchCoordinationStep({action: "park",- parentContinuationToken: "turn-inbox",+ workflowToolRunOwner: { inbox: "generated-owner-token" },parentWritable: createTestWritable(),serializedContext: createSerializedContext(),sessionState,@@ -1108,7 +1109,7 @@ describe("dispatchCoordinationStep", () => {await expect(dispatchCoordinationStep({action: "park",- parentContinuationToken: "turn-inbox",+ workflowToolRunOwner: { inbox: "generated-owner-token" },parentWritable: createTestWritable(),serializedContext: createSerializedContext(),sessionState,diff --git a/packages/eve/src/harness/workflow-tool-runs.ts b/packages/eve/src/harness/workflow-tool-runs.tsindex 4ad6d7ac5..d6ec3a4f7 100644--- a/packages/eve/src/harness/workflow-tool-runs.ts+++ b/packages/eve/src/harness/workflow-tool-runs.ts@@ -2,9 +2,6 @@ import type { HarnessSession, SessionStateMap } from "#harness/types.js";import type { RuntimeToolResultActionResult } from "#shared/action-types.js";const WORKFLOW_TOOL_RUNS_STATE_KEY = "eve.runtime.workflowToolRuns";-export function workflowToolRunAnswerToken(runId: string, seq: number): string {- return `eve:workflow-tool-run-answer:${runId}:${seq}`;-}/** A workflow tool run the active turn waits on. Background runs live in the task index. */export interface WorkflowToolRunRecord {diff --git a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts b/packages/eve/src/internal/testing/workflow-tool-fixtures.tsindex 82e39a863..54aff045b 100644--- a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts+++ b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts@@ -25,13 +25,14 @@ export async function deployServiceWorkflow(return { callId: ctx.callId, plan, sessionId: ctx.session.id };}-export async function confirmDeployWorkflow(+export async function* confirmDeployWorkflow(input: DeployInput,ctx: WorkflowToolContext,-): Promise<{ readonly approved: boolean; readonly service: string }> {+): AsyncGenerator<string, { readonly approved: boolean; readonly service: string }> {"use workflow";const plan = await planDeployStep(input.service);+ yield "awaiting approval";const answer = await ctx.ask({display: "confirmation",options: [@@ -40,6 +41,7 @@ export async function confirmDeployWorkflow(],prompt: `Apply ${plan}?`,});+ yield "approval received";return { approved: answer.optionId === "approve", service: input.service };}