Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): complete delegated tasks after nested background work settles (#3623)

Ruid6fc658b9f76
diff --git a/.changeset/tidy-nested-completion.md b/.changeset/tidy-nested-completion.md
new file mode 100644
index 000000000..dc86631ef
--- /dev/null
+++ b/.changeset/tidy-nested-completion.md
@@ -0,0 +1,9 @@
+---
+"eve": patch
+---
+
+Keep a delegated agent task open when its model turn yields while background work started in that turn is still running. Deliver the final result after that work finishes, including usage accumulated across the yielded turns, instead of reporting the interim reply as completion.
+
+Propagate task cancellation to a yielded child's nested background work for self, local, and remote delegation, without waking the cancelled child on its nested task notifications. Preserve cancellation across replay and session handoff.
+
+Fail an unanswered delegated task when its yielded child session ends, and preserve the unreported usage in that terminal result.
diff --git a/.changeset/warm-pandas-report.md b/.changeset/warm-pandas-report.md
new file mode 100644
index 000000000..d6190e36c
--- /dev/null
+++ b/.changeset/warm-pandas-report.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Keep durable MCP invocations open for pending background subagents so clients receive the delegated result.
diff --git a/docs/subagents/index.mdx b/docs/subagents/index.mdx
index 17b743a84..de897fa30 100644
--- a/docs/subagents/index.mdx
+++ b/docs/subagents/index.mdx
@@ -9,6 +9,12 @@ Use a subagent to run independent work in parallel, narrow the available tools,
- Use a [declared local subagent](#declared-subagents) for a specialist with its own directory and authored capabilities, rather than inheriting the root's.
- Use a [remote agent](./guides/remote-agents) to delegate to a separately deployed eve agent, with its own configuration and authentication.
+A subagent can finish a model turn while background tasks started in that turn are still running.
+That turn yields; its interim reply does not complete the caller's delegated task. Background work started by an earlier turn does not hold the reply, and a turn that ends with an error always answers the caller. If the child session expires, resets, or closes before replying, the delegated task fails and reports the usage accumulated since its previous settlement.
+After the nested tasks finish, the subagent resumes and returns its final result to
+the caller. This applies to root-agent copies, declared local subagents, and remote
+subagents. The subagent session remains available for later calls.
+
## Completion batching
Overlapping background work owned by a session forms a cohort. Tasks launched in
@@ -99,7 +105,7 @@ The parent can alternatively export `disableTool()` from a same-named tool path,
Every declared local or remote subagent runs as a durable background task: the call returns `{ status: "working", taskId, agentId }` immediately, then task notifications wake the parent with completion, failure, or cancellation. Human input requests surface separately on the parent session.
-`task_cancel` is available to sessions that own background tasks, including a child that starts nested background work.
+`task_cancel` is available to sessions that own background tasks, including a child that starts nested background work. Cancelling a delegated agent task also requests cancellation of its child's nested background tasks, even after that child's model turn has yielded. The child does not start another model turn for notifications from that cancelled work. This applies to self, local, and remote delegation. The calling model receives the cancellation receipt in the `task_cancel` result; a later report of that same outcome does not start another model turn.
A mounted extension can also contribute declared subagents from `extension/subagents/`. The mount namespace prefixes the subagent visible to the consuming agent node: mounting an extension as `crm` exposes its `reviewer` subagent as `crm__reviewer`. The contributed subagent keeps its own isolated tools, connections, skills, hooks, instructions, sandbox, and nested subagents, and its modules can read configuration from the extension handle. See [Extensions](./extensions#add-a-subagent) for the authoring and override behavior.
diff --git a/e2e/fixtures/agent-subagents/agent/channels/verification.ts b/e2e/fixtures/agent-subagents/agent/channels/verification.ts
new file mode 100644
index 000000000..00d095c7c
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/channels/verification.ts
@@ -0,0 +1,54 @@
+import { defineChannel, POST } from "eve/channels";
+import { getHookByToken, getRun, resumeHook } from "workflow/api";
+import { z } from "zod";
+import { verificationNamespace, type VerificationGate } from "../lib/verification-gate.js";
+
+// The random per-eval key is a capability for only that worker's fixture gate.
+export default defineChannel({
+ routes: [
+ POST("/test/verification/:sessionId/:key/:action", async (request, { params }) => {
+ const { sessionId, key, action } = z
+ .object({
+ sessionId: z.string().min(1).max(128),
+ key: z.string().uuid(),
+ action: z.enum(["ready", "release", "status"]),
+ })
+ .parse(params);
+ const signal = AbortSignal.any([request.signal, AbortSignal.timeout(60_000)]);
+ const reader = getRun(sessionId)
+ .getReadable<VerificationGate>({ namespace: verificationNamespace(key), startIndex: 0 })
+ .getReader();
+ const abort = () => {
+ void reader.cancel();
+ };
+ signal.addEventListener("abort", abort, { once: true });
+ let gate: VerificationGate;
+ try {
+ signal.throwIfAborted();
+ const next = await reader.read();
+ if (next.done) throw new Error("Verification gate was not acknowledged.");
+ gate = next.value;
+ } finally {
+ signal.removeEventListener("abort", abort);
+ await reader.cancel();
+ reader.releaseLock();
+ }
+ // Cancellation disposes the hook; the published run identity remains readable.
+ if (action === "status") return Response.json({ status: await getRun(gate.runId).status });
+ const hook = await getHookByToken(gate.token);
+ const metadata = await hook.metadata;
+ if (
+ metadata === null ||
+ typeof metadata !== "object" ||
+ Reflect.get(metadata, "sessionId") !== sessionId ||
+ Reflect.get(metadata, "key") !== key
+ ) {
+ return new Response(null, { status: 403 });
+ }
+ if (action === "ready") return Response.json({ status: await getRun(hook.runId).status });
+ const result = crypto.randomUUID();
+ await resumeHook(gate.token, result);
+ return Response.json({ released: true, result });
+ }),
+ ],
+});
diff --git a/e2e/fixtures/agent-subagents/agent/lib/verification-gate.ts b/e2e/fixtures/agent-subagents/agent/lib/verification-gate.ts
new file mode 100644
index 000000000..93e3fc1e3
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/lib/verification-gate.ts
@@ -0,0 +1,26 @@
+import { getRun } from "workflow/api";
+
+export const verificationNamespace = (key: string) => `nested-verification.${key}`;
+
+export interface VerificationGate {
+ readonly token: string;
+ readonly runId: string;
+}
+
+export async function publishVerificationGate(
+ sessionId: string,
+ key: string,
+ gate: VerificationGate,
+) {
+ "use step";
+ const ops: Promise<void>[] = [];
+ const writer = getRun(sessionId)
+ .getWritable<VerificationGate>({ namespace: verificationNamespace(key), ops })
+ .getWriter();
+ try {
+ await writer.write(gate);
+ } finally {
+ writer.releaseLock();
+ }
+ await Promise.all(ops);
+}
diff --git a/e2e/fixtures/agent-subagents/agent/subagents/local-detector/agent.ts b/e2e/fixtures/agent-subagents/agent/subagents/local-detector/agent.ts
new file mode 100644
index 000000000..96d6e3e25
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/subagents/local-detector/agent.ts
@@ -0,0 +1,8 @@
+import { e2eSubagentConfig } from "@eve-e2e/config";
+import { defineAgent } from "eve";
+
+export default defineAgent({
+ description:
+ "Coordinates Alice's release checklist sign-off by handing it to verification-worker.",
+ ...e2eSubagentConfig(),
+});
diff --git a/e2e/fixtures/agent-subagents/agent/subagents/local-detector/subagents/verification-worker/agent.ts b/e2e/fixtures/agent-subagents/agent/subagents/local-detector/subagents/verification-worker/agent.ts
new file mode 100644
index 000000000..19f7438d7
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/subagents/local-detector/subagents/verification-worker/agent.ts
@@ -0,0 +1 @@
+export { default } from "../../../verification-worker/agent.js";
diff --git a/e2e/fixtures/agent-subagents/agent/subagents/local-detector/subagents/verification-worker/tools/verification_gate.ts b/e2e/fixtures/agent-subagents/agent/subagents/local-detector/subagents/verification-worker/tools/verification_gate.ts
new file mode 100644
index 000000000..d36043da8
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/subagents/local-detector/subagents/verification-worker/tools/verification_gate.ts
@@ -0,0 +1 @@
+export { default } from "../../../../verification-worker/tools/verification_gate.js";
diff --git a/e2e/fixtures/agent-subagents/agent/subagents/verification-worker/agent.ts b/e2e/fixtures/agent-subagents/agent/subagents/verification-worker/agent.ts
new file mode 100644
index 000000000..c3d5cd473
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/subagents/verification-worker/agent.ts
@@ -0,0 +1,8 @@
+import { e2eSubagentConfig } from "@eve-e2e/config";
+import { defineAgent } from "eve";
+
+export default defineAgent({
+ description:
+ "Collects Alice's release checklist sign-off with verification_gate and reports its sign-off code.",
+ ...e2eSubagentConfig(),
+});
diff --git a/e2e/fixtures/agent-subagents/agent/subagents/verification-worker/tools/verification_gate.ts b/e2e/fixtures/agent-subagents/agent/subagents/verification-worker/tools/verification_gate.ts
new file mode 100644
index 000000000..576662a69
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/agent/subagents/verification-worker/tools/verification_gate.ts
@@ -0,0 +1,35 @@
+import {
+ defineWorkflowTool,
+ type WorkflowToolContext,
+ type WorkflowToolDefinition,
+} from "eve/tools";
+import { createHook, FatalError, getWorkflowMetadata, sleep } from "workflow";
+import { z } from "zod";
+import { publishVerificationGate } from "../../../lib/verification-gate.js";
+
+async function execute({ key }: { key: string }, ctx: WorkflowToolContext) {
+ "use workflow";
+ const gate = createHook<string>({ metadata: { key, sessionId: ctx.session.id } });
+ try {
+ await publishVerificationGate(ctx.session.id, key, {
+ token: gate.token,
+ runId: getWorkflowMetadata().workflowRunId,
+ });
+ return await Promise.race([
+ gate,
+ sleep("2m").then(() => {
+ throw new FatalError("Verification was not released within two minutes.");
+ }),
+ ]);
+ } finally {
+ await gate.dispose();
+ }
+}
+
+const tool: WorkflowToolDefinition<{ key: string }, string> = defineWorkflowTool({
+ description: "Wait for Alice's release checklist sign-off and return its sign-off code.",
+ inputSchema: z.object({ key: z.string().uuid() }),
+ execute,
+});
+
+export default tool;
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/cancel-local.eval.ts b/e2e/fixtures/agent-subagents/evals/nested-background/cancel-local.eval.ts
new file mode 100644
index 000000000..042216907
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/cancel-local.eval.ts
@@ -0,0 +1,85 @@
+import assert from "node:assert/strict";
+import { defineEval } from "eve/evals";
+import { z } from "zod";
+import {
+ cancellationRequest,
+ resetSessions,
+ signOffRequest,
+ waitForVerification,
+ waitForVerificationStop,
+} from "./fixture.js";
+
+export default defineEval({
+ tags: ["real-model"],
+ timeoutMs: 240_000,
+ description: "Cancelling a local subagent after it yields also cancels its nested worker.",
+ async test(t) {
+ const key = crypto.randomUUID();
+ const sessions: string[] = [];
+ try {
+ // The caller delegates to local-detector and yields.
+ const caller = await t.session();
+ sessions.push(caller.sessionId);
+ const callerTurn = (
+ await caller.send(
+ signOffRequest(
+ "Hand this off with the local-detector tool, using the note below as its message.",
+ key,
+ ),
+ )
+ ).expectOk();
+ const delegation = callerTurn.requireToolCall("local-detector", {
+ output: { status: "working" },
+ });
+ const { taskId } = z.object({ taskId: z.string() }).parse(delegation.output);
+ callerTurn.notEvent("subagent.completed");
+ const callerCompletion = t.target.watchTurn(caller.sessionId, {
+ startIndex: caller.state.streamIndex,
+ });
+ const detectorCall =
+ callerTurn.events.find((event) => event.type === "subagent.called") ??
+ (await callerCompletion.waitForEvent("subagent.called"));
+ assert.equal(detectorCall.data.name, "local-detector");
+ sessions.push(detectorCall.data.childSessionId);
+
+ // The detector delegates to its worker and also yields.
+ const detectorTurn = (
+ await t.target.watchTurn(detectorCall.data.childSessionId).result()
+ ).expectOk();
+ detectorTurn.requireToolCall("verification-worker", { output: { status: "working" } });
+ detectorTurn.notEvent("subagent.completed");
+ const detectorCompletion = t.target.watchTurn(detectorTurn.sessionId, {
+ startIndex: detectorTurn.session.state.streamIndex,
+ });
+ const workerCall =
+ detectorTurn.events.find((event) => event.type === "subagent.called") ??
+ (await detectorCompletion.waitForEvent("subagent.called"));
+ assert.equal(workerCall.data.name, "verification-worker");
+ sessions.push(workerCall.data.childSessionId);
+ const workerCompletion = t.target.watchTurn(workerCall.data.childSessionId);
+
+ // Both delegation turns have ended, but the worker is still gated.
+ await waitForVerification(t, workerCall.data.childSessionId, key);
+ const cancellation = (await caller.send(cancellationRequest(taskId))).expectOk();
+ cancellation.requireToolCall("task_cancel", {
+ input: { taskIds: [taskId] },
+ output: { tasks: [{ taskId, status: "cancelled" }] },
+ });
+
+ // A cancelled outer receipt is insufficient: its worker must actually stop.
+ await waitForVerificationStop(t, workerCall.data.childSessionId, key);
+ const workerResult = (await workerCompletion.result()).expectOk();
+ workerResult.event("turn.cancelled", { count: 1 });
+ workerResult.notEvent("turn.failed");
+ workerResult.notEvent("session.failed");
+ assert.equal(
+ detectorCompletion.events.filter((event) => event.type === "turn.started").length,
+ 0,
+ "cancelling nested work must not wake the cancelled detector",
+ );
+ t.succeeded();
+ } finally {
+ await resetSessions(t, sessions);
+ }
+ },
+});
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/cancel-remote.eval.ts b/e2e/fixtures/agent-subagents/evals/nested-background/cancel-remote.eval.ts
new file mode 100644
index 000000000..8328844ae
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/cancel-remote.eval.ts
@@ -0,0 +1,86 @@
+import assert from "node:assert/strict";
+import { defineEval } from "eve/evals";
+import { z } from "zod";
+import {
+ cancellationRequest,
+ resetSessions,
+ signOffRequest,
+ waitForVerification,
+ waitForVerificationStop,
+} from "./fixture.js";
+
+export default defineEval({
+ tags: ["real-model"],
+ timeoutMs: 240_000,
+ description: "Cancelling a remote subagent after it yields also cancels its nested worker.",
+ async test(t) {
+ const key = crypto.randomUUID();
+ const sessions: string[] = [];
+ try {
+ // The caller delegates to remote-loopback and yields.
+ const caller = await t.session();
+ sessions.push(caller.sessionId);
+ const callerTurn = (
+ await caller.send(
+ signOffRequest(
+ "Hand this off with the remote-loopback tool, using the note below as its message.",
+ key,
+ ),
+ )
+ ).expectOk();
+ const delegation = callerTurn.requireToolCall("remote-loopback", {
+ output: { status: "working" },
+ });
+ const { taskId } = z.object({ taskId: z.string() }).parse(delegation.output);
+ callerTurn.notEvent("subagent.completed");
+ const callerCompletion = t.target.watchTurn(caller.sessionId, {
+ startIndex: caller.state.streamIndex,
+ });
+ const detectorCall =
+ callerTurn.events.find((event) => event.type === "subagent.called") ??
+ (await callerCompletion.waitForEvent("subagent.called"));
+ assert.equal(detectorCall.data.name, "remote-loopback");
+ assert.ok(detectorCall.data.remote, "detector must use the remote HTTP transport");
+ sessions.push(detectorCall.data.childSessionId);
+
+ // The detector delegates to its worker and also yields.
+ const detectorTurn = (
+ await t.target.watchTurn(detectorCall.data.childSessionId).result()
+ ).expectOk();
+ detectorTurn.requireToolCall("verification-worker", { output: { status: "working" } });
+ detectorTurn.notEvent("subagent.completed");
+ const detectorCompletion = t.target.watchTurn(detectorTurn.sessionId, {
+ startIndex: detectorTurn.session.state.streamIndex,
+ });
+ const workerCall =
+ detectorTurn.events.find((event) => event.type === "subagent.called") ??
+ (await detectorCompletion.waitForEvent("subagent.called"));
+ assert.equal(workerCall.data.name, "verification-worker");
+ sessions.push(workerCall.data.childSessionId);
+ const workerCompletion = t.target.watchTurn(workerCall.data.childSessionId);
+
+ // Both delegation turns have ended, but the worker is still gated.
+ await waitForVerification(t, workerCall.data.childSessionId, key);
+ const cancellation = (await caller.send(cancellationRequest(taskId))).expectOk();
+ cancellation.requireToolCall("task_cancel", {
+ input: { taskIds: [taskId] },
+ output: { tasks: [{ taskId, status: "cancelled" }] },
+ });
+
+ // A cancelled outer receipt is insufficient: its worker must actually stop.
+ await waitForVerificationStop(t, workerCall.data.childSessionId, key);
+ const workerResult = (await workerCompletion.result()).expectOk();
+ workerResult.event("turn.cancelled", { count: 1 });
+ workerResult.notEvent("turn.failed");
+ workerResult.notEvent("session.failed");
+ assert.equal(
+ detectorCompletion.events.filter((event) => event.type === "turn.started").length,
+ 0,
+ "cancelling nested work must not wake the cancelled detector",
+ );
+ t.succeeded();
+ } finally {
+ await resetSessions(t, sessions);
+ }
+ },
+});
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/cancel-self.eval.ts b/e2e/fixtures/agent-subagents/evals/nested-background/cancel-self.eval.ts
new file mode 100644
index 000000000..7aa346fad
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/cancel-self.eval.ts
@@ -0,0 +1,84 @@
+import assert from "node:assert/strict";
+import { defineEval } from "eve/evals";
+import { z } from "zod";
+import {
+ cancellationRequest,
+ resetSessions,
+ signOffRequest,
+ waitForVerification,
+ waitForVerificationStop,
+} from "./fixture.js";
+
+export default defineEval({
+ tags: ["real-model"],
+ timeoutMs: 240_000,
+ description: "Cancelling the built-in agent after it yields also cancels its nested worker.",
+ async test(t) {
+ const key = crypto.randomUUID();
+ const sessions: string[] = [];
+ try {
+ // The caller delegates to agent and yields.
+ const caller = await t.session();
+ sessions.push(caller.sessionId);
+ const callerTurn = (
+ await caller.send(
+ signOffRequest(
+ `Call the tool named "agent" exactly once, with the note below as its message argument.
+Your handoff tool for this request is "agent"; that copy of you will contact verification-worker.`,
+ key,
+ ),
+ )
+ ).expectOk();
+ const delegation = callerTurn.requireToolCall("agent", { output: { status: "working" } });
+ const { taskId } = z.object({ taskId: z.string() }).parse(delegation.output);
+ callerTurn.notEvent("subagent.completed");
+ const callerCompletion = t.target.watchTurn(caller.sessionId, {
+ startIndex: caller.state.streamIndex,
+ });
+ const detectorCall =
+ callerTurn.events.find((event) => event.type === "subagent.called") ??
+ (await callerCompletion.waitForEvent("subagent.called"));
+ assert.equal(detectorCall.data.name, "agent");
+ sessions.push(detectorCall.data.childSessionId);
+
+ // The detector delegates to its worker and also yields.
+ const detectorTurn = (
+ await t.target.watchTurn(detectorCall.data.childSessionId).result()
+ ).expectOk();
+ detectorTurn.requireToolCall("verification-worker", { output: { status: "working" } });
+ detectorTurn.notEvent("subagent.completed");
+ const detectorCompletion = t.target.watchTurn(detectorTurn.sessionId, {
+ startIndex: detectorTurn.session.state.streamIndex,
+ });
+ const workerCall =
+ detectorTurn.events.find((event) => event.type === "subagent.called") ??
+ (await detectorCompletion.waitForEvent("subagent.called"));
+ assert.equal(workerCall.data.name, "verification-worker");
+ sessions.push(workerCall.data.childSessionId);
+ const workerCompletion = t.target.watchTurn(workerCall.data.childSessionId);
+
+ // Both delegation turns have ended, but the worker is still gated.
+ await waitForVerification(t, workerCall.data.childSessionId, key);
+ const cancellation = (await caller.send(cancellationRequest(taskId))).expectOk();
+ cancellation.requireToolCall("task_cancel", {
+ input: { taskIds: [taskId] },
+ output: { tasks: [{ taskId, status: "cancelled" }] },
+ });
+
+ // A cancelled outer receipt is insufficient: its worker must actually stop.
+ await waitForVerificationStop(t, workerCall.data.childSessionId, key);
+ const workerResult = (await workerCompletion.result()).expectOk();
+ workerResult.event("turn.cancelled", { count: 1 });
+ workerResult.notEvent("turn.failed");
+ workerResult.notEvent("session.failed");
+ assert.equal(
+ detectorCompletion.events.filter((event) => event.type === "turn.started").length,
+ 0,
+ "cancelling nested work must not wake the cancelled detector",
+ );
+ t.succeeded();
+ } finally {
+ await resetSessions(t, sessions);
+ }
+ },
+});
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/fixture.ts b/e2e/fixtures/agent-subagents/evals/nested-background/fixture.ts
new file mode 100644
index 000000000..2402f6b20
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/fixture.ts
@@ -0,0 +1,81 @@
+import assert from "node:assert/strict";
+import { setTimeout } from "node:timers/promises";
+import type { EveEvalContext } from "eve/evals";
+import { z } from "zod";
+
+/**
+ * Builds the caller prompt for a two-level delegation. `handoff` names the
+ * caller's delegation tool; the note is forwarded unchanged to the delegate,
+ * which hands it to verification-worker.
+ */
+export function signOffRequest(handoff: string, key: string) {
+ return `Alice is putting together her weekly project status update and needs the release checklist sign-off for it.
+${handoff}
+Once the handoff is accepted, let Alice know the sign-off is in progress and end your turn.
+When the handoff finishes, give Alice the sign-off code it returns.
+
+Note for the handoff:
+Please ask verification-worker to collect the release checklist sign-off by calling verification_gate with key ${key}.
+Once verification-worker has started, reply that the sign-off is in progress and end your turn.
+When verification-worker finishes, reply with the sign-off code it returned.`;
+}
+
+export function cancellationRequest(taskId: string) {
+ return `Alice collected the checklist sign-off herself, so the handoff is no longer needed. Call task_cancel with taskIds ["${taskId}"] to stop it, then let Alice know it was cancelled.`;
+}
+
+export async function waitForVerification(t: EveEvalContext, workerId: string, key: string) {
+ const response = await t.target.fetch(`/test/verification/${workerId}/${key}/ready`, {
+ method: "POST",
+ signal: t.signal,
+ });
+ assert.equal(response.status, 200);
+ assert.deepEqual(await response.json(), { status: "running" });
+}
+
+export async function releaseVerification(t: EveEvalContext, workerId: string, key: string) {
+ const response = await t.target.fetch(`/test/verification/${workerId}/${key}/release`, {
+ method: "POST",
+ signal: t.signal,
+ });
+ assert.equal(response.status, 200);
+ const { released, result } = await response.json();
+ assert.equal(released, true);
+ return z.uuid().parse(result);
+}
+
+export async function waitForVerificationStop(t: EveEvalContext, workerId: string, key: string) {
+ const deadline = Date.now() + 45_000;
+ let status: unknown;
+ do {
+ const response = await t.target.fetch(`/test/verification/${workerId}/${key}/status`, {
+ method: "POST",
+ signal: AbortSignal.any([t.signal, AbortSignal.timeout(5_000)]),
+ });
+ assert.equal(response.status, 200);
+ ({ status } = await response.json());
+ if (status !== "running" && status !== "pending") break;
+ await setTimeout(250, undefined, { signal: t.signal });
+ } while (Date.now() < deadline);
+ // Cooperative cancellation returns normally from the workflow wrapper. The
+ // worker's turn.cancelled event distinguishes cancellation from normal success.
+ assert.ok(
+ status === "completed" || status === "cancelled",
+ `the nested verification workflow must stop before cleanup; got ${String(status)}`,
+ );
+}
+
+export async function resetSessions(t: EveEvalContext, sessionIds: readonly string[]) {
+ await Promise.allSettled(
+ sessionIds.map(async (sessionId) => {
+ const response = await t.target.fetch(`/eve/v1/session/${sessionId}/reset`, {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ reason: "Nested verification eval cleanup" }),
+ // Cleanup must still run when the eval's own signal has expired.
+ signal: AbortSignal.timeout(5_000),
+ });
+ await response.body?.cancel();
+ }),
+ );
+}
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/local.eval.ts b/e2e/fixtures/agent-subagents/evals/nested-background/local.eval.ts
new file mode 100644
index 000000000..ea388fb86
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/local.eval.ts
@@ -0,0 +1,94 @@
+import assert from "node:assert/strict";
+import { defineEval } from "eve/evals";
+import { equals } from "eve/evals/expect";
+import {
+ releaseVerification,
+ resetSessions,
+ signOffRequest,
+ waitForVerification,
+} from "./fixture.js";
+
+export default defineEval({
+ tags: ["real-model"],
+ timeoutMs: 240_000,
+ description:
+ "A local subagent yields with nested work pending and delivers the final result once.",
+ async test(t) {
+ const key = crypto.randomUUID();
+ const sessions: string[] = [];
+ try {
+ // The caller delegates to local-detector and yields.
+ const caller = await t.session();
+ sessions.push(caller.sessionId);
+ const callerTurn = (
+ await caller.send(
+ signOffRequest(
+ "Hand this off with the local-detector tool, using the note below as its message.",
+ key,
+ ),
+ )
+ ).expectOk();
+ callerTurn.requireToolCall("local-detector", { output: { status: "working" } });
+ callerTurn.notEvent("subagent.completed");
+ const callerCompletion = t.target.watchTurn(caller.sessionId, {
+ startIndex: caller.state.streamIndex,
+ });
+ const detectorCall =
+ callerTurn.events.find((event) => event.type === "subagent.called") ??
+ (await callerCompletion.waitForEvent("subagent.called"));
+ assert.equal(detectorCall.data.name, "local-detector");
+ sessions.push(detectorCall.data.childSessionId);
+
+ // The detector delegates to its worker and also yields.
+ const detectorTurn = (
+ await t.target.watchTurn(detectorCall.data.childSessionId).result()
+ ).expectOk();
+ detectorTurn.requireToolCall("verification-worker", { output: { status: "working" } });
+ detectorTurn.notEvent("subagent.completed");
+ const detectorCompletion = t.target.watchTurn(detectorTurn.sessionId, {
+ startIndex: detectorTurn.session.state.streamIndex,
+ });
+ const workerCall =
+ detectorTurn.events.find((event) => event.type === "subagent.called") ??
+ (await detectorCompletion.waitForEvent("subagent.called"));
+ assert.equal(workerCall.data.name, "verification-worker");
+ sessions.push(workerCall.data.childSessionId);
+ const workerCompletion = t.target.watchTurn(workerCall.data.childSessionId);
+
+ // Neither yielded turn may complete the caller's task while the worker is gated.
+ await waitForVerification(t, workerCall.data.childSessionId, key);
+ t.check(
+ callerCompletion.events.filter((event) => event.type === "subagent.completed").length,
+ equals(0),
+ ).label("no completion before verification finishes");
+
+ // The receipt is created on release, so no model can obtain it from the prompt.
+ const receipt = await releaseVerification(t, workerCall.data.childSessionId, key);
+ const workerResult = (await workerCompletion.result()).expectOk();
+ workerResult.calledTool("verification_gate", {
+ input: { key },
+ output: receipt,
+ status: "completed",
+ count: 1,
+ });
+ workerResult.messageIncludes(receipt);
+ const detectorResult = (await detectorCompletion.result()).expectOk();
+ detectorResult.event("subagent.completed", {
+ data: { subagentName: "verification-worker", output: (output) => output.includes(receipt) },
+ count: 1,
+ });
+ detectorResult.messageIncludes(receipt);
+ const callerResult = (await callerCompletion.result()).expectOk();
+ callerResult.event("subagent.completed", { count: 1 });
+ callerResult.event("subagent.completed", {
+ data: { subagentName: "local-detector", output: (output) => output.includes(receipt) },
+ count: 1,
+ });
+ callerResult.messageIncludes(receipt);
+ t.noFailedActions();
+ t.succeeded();
+ } finally {
+ await resetSessions(t, sessions);
+ }
+ },
+});
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/remote.eval.ts b/e2e/fixtures/agent-subagents/evals/nested-background/remote.eval.ts
new file mode 100644
index 000000000..d6baade3f
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/remote.eval.ts
@@ -0,0 +1,95 @@
+import assert from "node:assert/strict";
+import { defineEval } from "eve/evals";
+import { equals } from "eve/evals/expect";
+import {
+ releaseVerification,
+ resetSessions,
+ signOffRequest,
+ waitForVerification,
+} from "./fixture.js";
+
+export default defineEval({
+ tags: ["real-model"],
+ timeoutMs: 240_000,
+ description:
+ "A remote subagent yields with nested work pending and delivers the final result once.",
+ async test(t) {
+ const key = crypto.randomUUID();
+ const sessions: string[] = [];
+ try {
+ // The caller delegates to remote-loopback and yields.
+ const caller = await t.session();
+ sessions.push(caller.sessionId);
+ const callerTurn = (
+ await caller.send(
+ signOffRequest(
+ "Hand this off with the remote-loopback tool, using the note below as its message.",
+ key,
+ ),
+ )
+ ).expectOk();
+ callerTurn.requireToolCall("remote-loopback", { output: { status: "working" } });
+ callerTurn.notEvent("subagent.completed");
+ const callerCompletion = t.target.watchTurn(caller.sessionId, {
+ startIndex: caller.state.streamIndex,
+ });
+ const detectorCall =
+ callerTurn.events.find((event) => event.type === "subagent.called") ??
+ (await callerCompletion.waitForEvent("subagent.called"));
+ assert.equal(detectorCall.data.name, "remote-loopback");
+ assert.ok(detectorCall.data.remote, "detector must use the remote HTTP transport");
+ sessions.push(detectorCall.data.childSessionId);
+
+ // The detector delegates to its worker and also yields.
+ const detectorTurn = (
+ await t.target.watchTurn(detectorCall.data.childSessionId).result()
+ ).expectOk();
+ detectorTurn.requireToolCall("verification-worker", { output: { status: "working" } });
+ detectorTurn.notEvent("subagent.completed");
+ const detectorCompletion = t.target.watchTurn(detectorTurn.sessionId, {
+ startIndex: detectorTurn.session.state.streamIndex,
+ });
+ const workerCall =
+ detectorTurn.events.find((event) => event.type === "subagent.called") ??
+ (await detectorCompletion.waitForEvent("subagent.called"));
+ assert.equal(workerCall.data.name, "verification-worker");
+ sessions.push(workerCall.data.childSessionId);
+ const workerCompletion = t.target.watchTurn(workerCall.data.childSessionId);
+
+ // Neither yielded turn may complete the caller's task while the worker is gated.
+ await waitForVerification(t, workerCall.data.childSessionId, key);
+ t.check(
+ callerCompletion.events.filter((event) => event.type === "subagent.completed").length,
+ equals(0),
+ ).label("no completion before verification finishes");
+
+ // The receipt is created on release, so no model can obtain it from the prompt.
+ const receipt = await releaseVerification(t, workerCall.data.childSessionId, key);
+ const workerResult = (await workerCompletion.result()).expectOk();
+ workerResult.calledTool("verification_gate", {
+ input: { key },
+ output: receipt,
+ status: "completed",
+ count: 1,
+ });
+ workerResult.messageIncludes(receipt);
+ const detectorResult = (await detectorCompletion.result()).expectOk();
+ detectorResult.event("subagent.completed", {
+ data: { subagentName: "verification-worker", output: (output) => output.includes(receipt) },
+ count: 1,
+ });
+ detectorResult.messageIncludes(receipt);
+ const callerResult = (await callerCompletion.result()).expectOk();
+ callerResult.event("subagent.completed", { count: 1 });
+ callerResult.event("subagent.completed", {
+ data: { subagentName: "remote-loopback", output: (output) => output.includes(receipt) },
+ count: 1,
+ });
+ callerResult.messageIncludes(receipt);
+ t.noFailedActions();
+ t.succeeded();
+ } finally {
+ await resetSessions(t, sessions);
+ }
+ },
+});
diff --git a/e2e/fixtures/agent-subagents/evals/nested-background/self.eval.ts b/e2e/fixtures/agent-subagents/evals/nested-background/self.eval.ts
new file mode 100644
index 000000000..902b38de9
--- /dev/null
+++ b/e2e/fixtures/agent-subagents/evals/nested-background/self.eval.ts
@@ -0,0 +1,95 @@
+import assert from "node:assert/strict";
+import { defineEval } from "eve/evals";
+import { equals } from "eve/evals/expect";
+import {
+ releaseVerification,
+ resetSessions,
+ signOffRequest,
+ waitForVerification,
+} from "./fixture.js";
+
+export default defineEval({
+ tags: ["real-model"],
+ timeoutMs: 240_000,
+ description:
+ "The built-in agent yields with nested work pending and delivers the final result once.",
+ async test(t) {
+ const key = crypto.randomUUID();
+ const sessions: string[] = [];
+ try {
+ // The caller delegates to agent and yields.
+ const caller = await t.session();
+ sessions.push(caller.sessionId);
+ const callerTurn = (
+ await caller.send(
+ signOffRequest(
+ `Call the tool named "agent" exactly once, with the note below as its message argument.
+Your handoff tool for this request is "agent"; that copy of you will contact verification-worker.`,
+ key,
+ ),
+ )
+ ).expectOk();
+ callerTurn.requireToolCall("agent", { output: { status: "working" } });
+ callerTurn.notEvent("subagent.completed");
+ const callerCompletion = t.target.watchTurn(caller.sessionId, {
+ startIndex: caller.state.streamIndex,
+ });
+ const detectorCall =
+ callerTurn.events.find((event) => event.type === "subagent.called") ??
+ (await callerCompletion.waitForEvent("subagent.called"));
+ assert.equal(detectorCall.data.name, "agent");
+ sessions.push(detectorCall.data.childSessionId);
+
+ // The detector delegates to its worker and also yields.
+ const detectorTurn = (
+ await t.target.watchTurn(detectorCall.data.childSessionId).result()
+ ).expectOk();
+ detectorTurn.requireToolCall("verification-worker", { output: { status: "working" } });
+ detectorTurn.notEvent("subagent.completed");
+ const detectorCompletion = t.target.watchTurn(detectorTurn.sessionId, {
+ startIndex: detectorTurn.session.state.streamIndex,
+ });
+ const workerCall =
+ detectorTurn.events.find((event) => event.type === "subagent.called") ??
+ (await detectorCompletion.waitForEvent("subagent.called"));
+ assert.equal(workerCall.data.name, "verification-worker");
+ sessions.push(workerCall.data.childSessionId);
+ const workerCompletion = t.target.watchTurn(workerCall.data.childSessionId);
+
+ // Neither yielded turn may complete the caller's task while the worker is gated.
+ await waitForVerification(t, workerCall.data.childSessionId, key);
+ t.check(
+ callerCompletion.events.filter((event) => event.type === "subagent.completed").length,
+ equals(0),
+ ).label("no completion before verification finishes");
+
+ // The receipt is created on release, so no model can obtain it from the prompt.
+ const receipt = await releaseVerification(t, workerCall.data.childSessionId, key);
+ const workerResult = (await workerCompletion.result()).expectOk();
+ workerResult.calledTool("verification_gate", {
+ input: { key },
+ output: receipt,
+ status: "completed",
+ count: 1,
+ });
+ workerResult.messageIncludes(receipt);
+ const detectorResult = (await detectorCompletion.result()).expectOk();
+ detectorResult.event("subagent.completed", {
+ data: { subagentName: "verification-worker", output: (output) => output.includes(receipt) },
+ count: 1,
+ });
+ detectorResult.messageIncludes(receipt);
+ const callerResult = (await callerCompletion.result()).expectOk();
+ callerResult.event("subagent.completed", { count: 1 });
+ callerResult.event("subagent.completed", {
+ data: { subagentName: "agent", output: (output) => output.includes(receipt) },
+ count: 1,
+ });
+ callerResult.messageIncludes(receipt);
+ t.noFailedActions();
+ t.succeeded();
+ } finally {
+ await resetSessions(t, sessions);
+ }
+ },
+});
diff --git a/e2e/fixtures/agent-subagents/tsconfig.json b/e2e/fixtures/agent-subagents/tsconfig.json
index 85e3eceb8..032878901 100644
--- a/e2e/fixtures/agent-subagents/tsconfig.json
+++ b/e2e/fixtures/agent-subagents/tsconfig.json
@@ -11,7 +11,7 @@
"forceConsistentCasingInFileNames": true,
"declaration": true,
"noEmit": true,
- "types": ["node"]
+ "types": ["node", "eve/workflow-modules"]
},
"include": ["agent/**/*.ts", "evals/**/*.ts"]
}
diff --git a/e2e/fixtures/fixture-tasks/agent/agent.ts b/e2e/fixtures/fixture-tasks/agent/agent.ts
index 2ed707984..93e5a7d60 100644
--- a/e2e/fixtures/fixture-tasks/agent/agent.ts
+++ b/e2e/fixtures/fixture-tasks/agent/agent.ts
@@ -133,14 +133,12 @@ function respond(request: MockModelRequest): MockModelResponse | string {
if (message === "TASK-FAN-IN") return fanInTasks(request);
if (message === "TASK-FAN-IN-STATUS") return fanInNotification(request);
if (message === "TASK-CANCEL-SETUP") return setupCancelWorker(request);
+ if (message.startsWith("TASK-CANCEL-INSPECT ")) {
+ // The eval proves the approval route was retired before inspecting the receipt.
+ return inspectTerminalTask(request, "task-cancel-inspect", "TASK-CANCEL-STATUS", message);
+ }
if (message.startsWith("TASK-CANCEL-VERIFY ")) {
- return inspectTerminalTask(
- request,
- "task-cancel-verify",
- "TASK-CANCEL-STATUS",
- message,
- "cancelled",
- );
+ return inspectTerminalTask(request, "task-cancel-verify", "TASK-CANCEL-STATUS", message);
}
if (message.startsWith("TASK-HITL-VERIFY ")) {
diff --git a/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.noop-already-cancelled.eval.ts b/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.noop-already-cancelled.eval.ts
index f8d65534c..944d24d8b 100644
--- a/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.noop-already-cancelled.eval.ts
+++ b/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.noop-already-cancelled.eval.ts
@@ -5,7 +5,6 @@ import {
requireTaskView,
sendAndFollowQueuedTurn,
waitForTaskInput,
- waitForTaskStatus,
} from "./shared.js";
import { defineTaskEval } from "./task-transition.js";
@@ -53,12 +52,10 @@ export default defineTaskEval({
),
);
- const still = await waitForTaskStatus(
+ const { turn: still } = await sendAndFollowQueuedTurn(
t,
+ `TASK-CANCEL-VERIFY ${taskId}`,
repeated.session,
- "TASK-CANCEL-VERIFY",
- taskId,
- "cancelled",
);
still.expectOk();
await t.require(
diff --git a/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.owned-session-accepted-nonterminal.eval.ts b/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.owned-session-accepted-nonterminal.eval.ts
index d1130cd62..db6a37849 100644
--- a/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.owned-session-accepted-nonterminal.eval.ts
+++ b/e2e/fixtures/fixture-tasks/evals/task.lifecycle.cancel.owned-session-accepted-nonterminal.eval.ts
@@ -4,7 +4,7 @@ import {
requireBackgroundTaskId,
requireTaskView,
waitForTaskInput,
- waitForTaskStatus,
+ sendAndFollowQueuedTurn,
} from "./shared.js";
import { defineTaskEval } from "./task-transition.js";
@@ -42,13 +42,27 @@ export default defineTaskEval({
),
);
- const verified = await waitForTaskStatus(
+ // Before task_cancel can affect the outcome, prove session cancellation
+ // retired the approval route: its old answer must reach the parent model.
+ // If the task were still live, this answer would instead release the child.
+ const staleAnswer = await blocked.session.respond([
+ { optionId: "approve", requestId: blocked.request.requestId },
+ ]);
+ staleAnswer.expectOk();
+ staleAnswer.event("step.started", { count: 1 });
+ staleAnswer.notCalledTool("task_cancel");
+ staleAnswer.notEvent("subagent.completed");
+ const inspected = await sendAndFollowQueuedTurn(
t,
- blocked.session,
- "TASK-CANCEL-VERIFY",
- taskId,
- "cancelled",
+ `TASK-CANCEL-INSPECT ${taskId}`,
+ staleAnswer.session,
);
+ const verified = inspected.turn;
+ for (const turn of [staleAnswer, ...inspected.observedTurns]) {
+ turn.notEvent("message.received", {
+ data: { message: (message) => message.startsWith("Background task ") },
+ });
+ }
verified.expectOk();
verified.messageIncludes("TASK-CANCEL-STATUS");
await t.require(
diff --git a/packages/eve/src/execution/cancel-indexed-session-tasks-step.test.ts b/packages/eve/src/execution/cancel-indexed-session-tasks-step.test.ts
index 0e7cf13cc..34ee9b9df 100644
--- a/packages/eve/src/execution/cancel-indexed-session-tasks-step.test.ts
+++ b/packages/eve/src/execution/cancel-indexed-session-tasks-step.test.ts
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type { DurableSessionState } from "#execution/durable-session-store.js";
import { cancelAllIndexedSessionTasksStep } from "#execution/cancel-indexed-session-tasks-step.js";
import {
- getBackgroundWorkflowToolRuns,
+ getBackgroundTasks,
readWorkflowTaskView,
type BackgroundWorkflowToolRun,
} from "#harness/workflow-tool-runs.js";
@@ -44,9 +44,9 @@ describe("cancelAllIndexedSessionTasksStep", () => {
expect(result.sessionState).toBeDefined();
expect(
- getBackgroundWorkflowToolRuns(result.sessionState?.snapshot.session.state).map((entry) =>
- readWorkflowTaskView(entry.task),
- ),
+ getBackgroundTasks(result.sessionState?.snapshot.session.state)
+ .query()
+ .map((task) => readWorkflowTaskView(task.run.task)),
).toEqual([cancelledView(task1), cancelledView(task2)]);
expect(cancelOwnedTaskMock).toHaveBeenCalledTimes(2);
expect(cancelOwnedTaskMock).toHaveBeenNthCalledWith(1, {
@@ -63,6 +63,19 @@ describe("cancelAllIndexedSessionTasksStep", () => {
});
});
+ it("leaves a task working when cancellation fails", async () => {
+ cancelOwnedTaskMock.mockRejectedValueOnce(new Error("Cancellation unavailable"));
+ const result = await cancelAllIndexedSessionTasksStep({
+ serializedContext: {},
+ sessionState: makeSessionState([indexedTask("failed-cancel"), indexedTask("cancelled")]),
+ });
+ expect(
+ getBackgroundTasks(result.sessionState?.snapshot.session.state)
+ .query()
+ .map((task) => task.status),
+ ).toEqual(["working", "cancelled"]);
+ });
+
it("does not require runtime context when no tasks are indexed", async () => {
await expect(
cancelAllIndexedSessionTasksStep({ sessionState: makeSessionState([]) }),
diff --git a/packages/eve/src/execution/cancel-indexed-session-tasks-step.ts b/packages/eve/src/execution/cancel-indexed-session-tasks-step.ts
index ea4a7a7bc..ce55b3ced 100644
--- a/packages/eve/src/execution/cancel-indexed-session-tasks-step.ts
+++ b/packages/eve/src/execution/cancel-indexed-session-tasks-step.ts
@@ -9,7 +9,7 @@ import { cancelOwnedTask } from "#execution/tasks/parent/dispatch.js";
import { cancelBackgroundAgentTask } from "#execution/tools/subagent/task-cancel.js";
import { createLogger, logError } from "#internal/logging.js";
import { BundleKey } from "#runtime/sessions/runtime-context-keys.js";
-import { getBackgroundWorkflowToolRuns } from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
const log = createLogger("execution.cancel-indexed-session-tasks");
@@ -30,15 +30,28 @@ export async function cancelAllIndexedSessionTasksStep(input: {
return { sessionState: input.sessionState };
}
- let entries;
+ let tasks;
+ let working;
try {
- entries = getBackgroundWorkflowToolRuns(durable.state);
+ tasks = getBackgroundTasks(durable.state);
+ working = tasks.query({ state: "working" });
} catch (error) {
logError(log, "failed to read the task index", error, {
parentSessionId: durable.sessionId,
});
return { sessionState: input.sessionState };
}
+ // A cancelled task is retried: its earlier cancellation may have committed before its child
+ // stopped. Reading retained outcomes can fail without blocking cancellation of live work.
+ let retries: typeof working = [];
+ try {
+ retries = tasks.query({ state: "cancelled" });
+ } catch (error) {
+ logError(log, "failed to read cancelled task outcomes", error, {
+ parentSessionId: durable.sessionId,
+ });
+ }
+ const entries = [...working, ...retries].map((task) => task.run);
if (entries.length === 0) return { sessionState: input.sessionState };
if (input.serializedContext === undefined) {
throw new Error("Indexed task cancellation requires serialized runtime context.");
diff --git a/packages/eve/src/execution/proxied-deliver-step.ts b/packages/eve/src/execution/proxied-deliver-step.ts
index 68ae53c16..e608ef57c 100644
--- a/packages/eve/src/execution/proxied-deliver-step.ts
+++ b/packages/eve/src/execution/proxied-deliver-step.ts
@@ -22,7 +22,7 @@ import {
stampMessageStreamEvent,
} from "#protocol/message.js";
import type { InputResponse } from "#shared/input.js";
-import { findBackgroundWorkflowToolRun } from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import {
createTaskInputRequestId,
retireProxyInputRequests,
@@ -80,7 +80,7 @@ export async function routeProxiedDeliverStep(input: {
allowRoute: (requestId, route) =>
!resolvedQuestions.has(requestId) &&
(route.taskId === undefined ||
- findBackgroundWorkflowToolRun(durableSession.state, route.taskId) !== undefined),
+ getBackgroundTasks(durableSession.state).get(route.taskId) !== undefined),
payload,
resolveMessage,
state: durableSession.state,
@@ -133,7 +133,7 @@ export async function routeProxiedDeliverStep(input: {
// durable decision, so its view cannot claim the child resumed first.
const taskId = child.taskId;
if (taskId !== undefined) {
- const entry = findBackgroundWorkflowToolRun(durableSession.state, taskId);
+ const entry = getBackgroundTasks(durableSession.state).get(taskId)?.run;
if (entry === undefined) {
mergeStrandedResponses(parentPayloads, child, taskId);
continue;
diff --git a/packages/eve/src/execution/route-child-delivery.test.ts b/packages/eve/src/execution/route-child-delivery.test.ts
index 9b87ffcc7..41c49e8b2 100644
--- a/packages/eve/src/execution/route-child-delivery.test.ts
+++ b/packages/eve/src/execution/route-child-delivery.test.ts
@@ -1,5 +1,8 @@
+import { SessionInputQueue } from "#execution/session/input-queue.js";
+import type { DeliverHookPayload } from "#channel/types.js";
+import { registerWorkflowToolRun, recordWorkflowTaskView } from "#harness/workflow-tool-runs.js";
import { createTestSessionState } from "#internal/testing/session-state.js";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { assert, beforeEach, describe, expect, it, vi } from "vitest";
import { routeDeliverToChildren } from "#execution/route-child-delivery.js";
import {
@@ -13,7 +16,10 @@ import {
import { acceptTaskAuthorizationEventStep } from "#execution/tools/subagent/accept-event-step.js";
import { routeProxiedDeliverStep } from "#execution/proxied-deliver-step.js";
import type { DurableSessionState } from "#execution/durable-session-store.js";
-import { dispatchTaskAgentInvocationStep } from "#execution/tools/subagent/invoke-step.js";
+import {
+ dispatchTaskAgentInvocationStep,
+ settleTaskAgentInvocationStep,
+} from "#execution/tools/subagent/invoke-step.js";
import { resumeHookStep } from "#execution/tools/workflow/resume-hook-step.js";
vi.mock("#subagents/event-proxy-step.js", () => ({
@@ -32,6 +38,7 @@ vi.mock("#execution/proxied-deliver-step.js", () => ({
}));
vi.mock("#execution/tools/subagent/invoke-step.js", () => ({
dispatchTaskAgentInvocationStep: vi.fn(),
+ settleTaskAgentInvocationStep: vi.fn(),
}));
vi.mock("#execution/tools/workflow/resume-hook-step.js", () => ({
resumeHookStep: vi.fn(),
@@ -62,6 +69,138 @@ const taskRequest = {
describe("task HITL delivery routing", () => {
beforeEach(() => vi.resetAllMocks());
+ it("acknowledges late settlement after restoring only the recorded task outcome", async () => {
+ const session = registerWorkflowToolRun(state(false).snapshot.session, {
+ callId: "call-1",
+ toolName: "worker",
+ lifetime: "session",
+ origin: { turnId: "turn-1", stepIndex: 0 },
+ address: { runId: "task-run", hookToken: "task-inbox" },
+ task: {
+ taskId: "task-1",
+ metadata: { kind: "subagent", name: "worker" },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ });
+ const cancelled = {
+ taskId: "task-1",
+ metadata: { kind: "subagent", name: "worker" },
+ status: "cancelled" as const,
+ };
+ const sessionState = createTestSessionState({
+ snapshot: {
+ session: {
+ ...session,
+ state: JSON.parse(JSON.stringify(recordWorkflowTaskView(session.state, cancelled).state)),
+ },
+ },
+ });
+ vi.mocked(recordTerminalTaskViewsStep).mockResolvedValue({
+ views: [],
+ subagentCompletions: [],
+ sessionState,
+ serializedContext: {},
+ });
+ const context = {
+ sessionState,
+ serializedContext: {},
+ sessionWritable: new WritableStream<Uint8Array>(),
+ };
+ const queue = new SessionInputQueue();
+ async function routeQueued(delivery: DeliverHookPayload) {
+ const admitted = queue.enqueueDelivery(delivery, sessionState.snapshot.session.state);
+ assert(admitted);
+ const routed = await routeDeliverToChildren({ ...context, delivery });
+ assert(routed.kind === "continue");
+ queue.replaceDelivery(admitted.sequence, routed.remainder);
+ return queue.delivery(admitted.sequence);
+ }
+ const outcome = await routeQueued({
+ kind: "deliver",
+ taskDeliveryId: "task-1:ready:completed",
+ payloads: [
+ {
+ message: "Late completion",
+ task: {
+ views: [
+ {
+ ...cancelled,
+ status: "completed",
+ lastOutput: { type: "result", data: "Late result" },
+ },
+ ],
+ },
+ },
+ ],
+ });
+ expect(recordTerminalTaskViewsStep).toHaveBeenCalledOnce();
+ expect(outcome).toBeUndefined();
+ vi.mocked(settleTaskAgentInvocationStep).mockResolvedValue({
+ settled: false,
+ sessionState,
+ serializedContext: {},
+ });
+ const settlement = await routeQueued({
+ kind: "deliver",
+ taskDeliveryId: "task-1:agent:run-1:settled",
+ payloads: [
+ {
+ task: {
+ agentRequests: [
+ {
+ taskId: "task-1",
+ replyTo: "settlement-reply",
+ request: {
+ kind: "agent-settled",
+ result: {
+ callId: "child-call",
+ kind: "subagent-result",
+ origin: "child",
+ subagentName: "worker",
+ output: "",
+ outcome: {
+ kind: "parked",
+ result: { kind: "cancelled" },
+ usageDelta: {
+ inputTokens: 0,
+ outputTokens: 0,
+ cacheReadTokens: 0,
+ cacheWriteTokens: 0,
+ },
+ },
+ },
+ },
+ },
+ ],
+ },
+ },
+ ],
+ });
+ expect(settlement).toBeUndefined();
+ expect(resumeHookStep).toHaveBeenCalledWith(
+ "settlement-reply",
+ { kind: "agent-settled", callId: "child-call" },
+ { ifPresent: true },
+ );
+ expect(
+ queue.enqueueDelivery(
+ {
+ kind: "deliver",
+ taskDeliveryId: "task-1:update:1",
+ payloads: [{ message: "Still working" }],
+ },
+ sessionState.snapshot.session.state,
+ ),
+ ).toBeUndefined();
+ const user = await routeQueued({
+ kind: "deliver",
+ payloads: [{ message: "New request" }],
+ });
+ expect(user).toMatchObject({
+ payloads: [{ message: "New request" }],
+ });
+ });
+
it("commits the task route before emitting and consumes the framework-only delivery", async () => {
const recordedState = state(true);
vi.mocked(recordTaskInputRequestStep).mockResolvedValue({
@@ -150,7 +289,7 @@ describe("task HITL delivery routing", () => {
expect(recordTerminalTaskViewsStep).toHaveBeenCalledOnce();
});
- it("uses the parent's cancelled outcome when a late child reports success", async () => {
+ it("consumes a late outcome without notifying the model again", async () => {
const cancelled = {
taskId: "task-1",
metadata: { kind: "tool", name: "export" },
@@ -158,7 +297,7 @@ describe("task HITL delivery routing", () => {
};
vi.mocked(recordTerminalTaskViewsStep).mockResolvedValue({
subagentCompletions: [],
- views: [cancelled],
+ views: [],
serializedContext: {},
sessionState: state(false),
});
@@ -183,7 +322,7 @@ describe("task HITL delivery routing", () => {
});
expect(result).toMatchObject({
kind: "continue",
- remainder: { payloads: [{ message: "Background task task-1 (export) is cancelled." }] },
+ remainder: undefined,
});
});
diff --git a/packages/eve/src/execution/route-child-delivery.ts b/packages/eve/src/execution/route-child-delivery.ts
index 9fb145772..a33940a70 100644
--- a/packages/eve/src/execution/route-child-delivery.ts
+++ b/packages/eve/src/execution/route-child-delivery.ts
@@ -1,5 +1,5 @@
import { emitSubagentEventStep } from "#execution/tools/subagent/emit-event-step.js";
-import { formatTaskNotification } from "#tasks/notification.js";
+import { formatTaskNotification, hasRecordedTaskOutcome } from "#tasks/notification.js";
import type { TaskView } from "#tasks/types.js";
import type { DeliverHookPayload, DeliverPayload } from "#channel/types.js";
import { coalesceDeliverPayloads } from "#execution/deliver-payloads.js";
@@ -120,6 +120,13 @@ export async function routeDeliverToChildren(input: {
}
}
+ // The delivery that records a task's first outcome still notifies the model. A later report
+ // of that outcome only applies its envelopes above.
+ const stateBeforeDelivery = input.sessionState.snapshot.session.state;
+ if (payload.task !== undefined && hasRecordedTaskOutcome(input.delivery, stateBeforeDelivery)) {
+ return { kind: "continue", remainder: undefined, serializedContext, sessionState };
+ }
+
const ordinaryPayloads: DeliverPayload[] = [];
const ordinaryMetadata: NonNullable<DeliverHookPayload["deliveryMetadata"]>[number][] = [];
for (const [sourcePayloadIndex, sourcePayload] of input.delivery.payloads.entries()) {
diff --git a/packages/eve/src/execution/session/admission.ts b/packages/eve/src/execution/session/admission.ts
index d9f048dd7..cdb5333f6 100644
--- a/packages/eve/src/execution/session/admission.ts
+++ b/packages/eve/src/execution/session/admission.ts
@@ -74,7 +74,10 @@ export async function admitSessionInboxPayload(
switch (command.kind) {
case "deliver": {
- const admission = input.queue.enqueueDelivery(command);
+ const admission = input.queue.enqueueDelivery(
+ command,
+ input.cursor.sessionState.snapshot.session.state,
+ );
return admission === undefined ? { kind: "consumed" } : { admission, kind: "delivery" };
}
case "clear":
@@ -109,6 +112,7 @@ export async function applySessionCancellation(
sessionState: input.cursor.sessionState,
});
await input.cursor.apply(cancelled);
+ input.queue.discardStaleNotifications(input.cursor.sessionState.snapshot.session.state);
}
if (command.taskId !== undefined) input.queue.cancelTask(command.taskId);
}
diff --git a/packages/eve/src/execution/session/finalization.test.ts b/packages/eve/src/execution/session/finalization.test.ts
new file mode 100644
index 000000000..31e8badad
--- /dev/null
+++ b/packages/eve/src/execution/session/finalization.test.ts
@@ -0,0 +1,124 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { finalizeSession } from "#execution/session/finalization.js";
+import { resolveSessionStepResult } from "#execution/session/turn-step-result.js";
+import { setTurnUsageState, takeSessionUsageDelta } from "#harness/turn-tag-state.js";
+import type { HarnessSession } from "#harness/types.js";
+import { registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
+import { notifyTurnCallerStep } from "#subagents/parent-notification.js";
+
+vi.mock("#execution/terminate-child-sessions-step.js", () => ({
+ terminateChildSessionsStep: vi.fn(),
+}));
+vi.mock("#execution/terminal-session-completion-step.js", () => ({
+ emitTerminalSessionCompletionStep: vi.fn(),
+}));
+vi.mock("#execution/terminal-session-failure-step.js", () => ({
+ emitTerminalSessionFailureStep: vi.fn(),
+}));
+vi.mock("#subagents/callback-step.js", () => ({ fireSessionCallbackStep: vi.fn() }));
+vi.mock("#subagents/parent-notification.js", () => ({
+ notifyDelegatedParentStep: vi.fn(),
+ notifyTurnCallerStep: vi.fn(),
+}));
+
+function withUsage(session: HarnessSession, inputTokens: number): HarnessSession {
+ const totals = {
+ inputTokens,
+ outputTokens: 0,
+ cacheReadTokens: 0,
+ cacheWriteTokens: 0,
+ costUsd: 0,
+ sawCost: false,
+ };
+ return setTurnUsageState(session, { ...totals, session: totals, turnId: "current" });
+}
+
+function sessionAwaitingCallerNotification() {
+ const session: HarnessSession = {
+ agent: { modelReference: { id: "unused" }, system: "", tools: [] },
+ compaction: { recentWindowSize: 10, threshold: 100_000 },
+ continuationToken: "detector",
+ history: [],
+ sessionId: "detector",
+ };
+ const previouslySettled = takeSessionUsageDelta(withUsage(session, 100)).session;
+ const pending = registerWorkflowToolRun(withUsage(previouslySettled, 250), {
+ callId: "worker",
+ toolName: "worker",
+ lifetime: "session",
+ origin: { turnId: "current", stepIndex: 0 },
+ address: { runId: "worker", hookToken: "worker" },
+ task: {
+ taskId: "worker",
+ metadata: { kind: "subagent", name: "worker" },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ });
+ const parked = resolveSessionStepResult(
+ {
+ next: null,
+ session: pending,
+ settledTurn: { output: "Verification is running." },
+ },
+ {},
+ "conversation",
+ {},
+ "current",
+ );
+ expect(parked).toMatchObject({ action: "park", settled: { notifyCaller: false } });
+ return parked.sessionState;
+}
+
+beforeEach(() => vi.clearAllMocks());
+
+describe("session finalization with an unsettled caller", () => {
+ it.each([
+ { kind: "expired" as const },
+ { kind: "failed" as const, error: new Error("Owner failed") },
+ ])(
+ "reports terminal failure and only unreported usage when the owner is $kind",
+ async (outcome) => {
+ const caller = {
+ callId: "delegate",
+ subagentName: "detector",
+ replyTo: { kind: "hook" as const, token: "parent" },
+ };
+ const result = await finalizeSession(outcome, {
+ caller,
+ cursor: { serializedContext: {}, sessionState: sessionAwaitingCallerNotification() },
+ mode: "conversation",
+ sessionWritable: new WritableStream(),
+ });
+ expect(result).toMatchObject({
+ isError: true,
+ usage: { inputTokens: 250 },
+ usageDelta: { inputTokens: 150 },
+ });
+ expect(result.output).not.toBe("");
+ expect(notifyTurnCallerStep).toHaveBeenCalledExactlyOnceWith({
+ caller,
+ lifecycle: "terminal",
+ sessionId: "detector",
+ settled: {
+ isError: true,
+ output: result.output,
+ usage: expect.objectContaining({ inputTokens: 150 }),
+ },
+ });
+ },
+ );
+
+ it("does not notify again when an already settled conversation expires", async () => {
+ const result = await finalizeSession(
+ { kind: "expired" },
+ {
+ caller: undefined,
+ cursor: { serializedContext: {}, sessionState: sessionAwaitingCallerNotification() },
+ mode: "conversation",
+ sessionWritable: new WritableStream(),
+ },
+ );
+ expect(result).toMatchObject({ isError: false, output: "" });
+ expect(notifyTurnCallerStep).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/eve/src/execution/session/finalization.ts b/packages/eve/src/execution/session/finalization.ts
index 03c60fd9c..7a330731b 100644
--- a/packages/eve/src/execution/session/finalization.ts
+++ b/packages/eve/src/execution/session/finalization.ts
@@ -8,6 +8,7 @@ import { normalizeSerializableError } from "#execution/workflow-errors.js";
import type { WorkflowEntryResult } from "#execution/session/entry-input.js";
import type { RunMode } from "#shared/run-mode.js";
import type { TokenUsage } from "#shared/token-usage.js";
+import { getSessionTokenUsage, takeSessionUsageDelta, toUsage } from "#harness/turn-tag-state.js";
import { fireSessionCallbackStep } from "#subagents/callback-step.js";
import { notifyDelegatedParentStep, notifyTurnCallerStep } from "#subagents/parent-notification.js";
import {
@@ -58,7 +59,7 @@ export async function finalizeSession(
});
}
- const settled = settledResult(outcome);
+ const settled = settledResult(outcome, context);
if (context.mode === "task") {
await fireSessionCallbackStep({
error: settled.isError ? settled.output : undefined,
@@ -94,15 +95,31 @@ export async function finalizeSession(
usage: outcome.action.usage,
usageDelta: outcome.action.usageDelta,
}
- : { isError: settled.isError, output: settled.output };
+ : {
+ isError: settled.isError,
+ output: settled.output,
+ usage: settled.sessionUsage,
+ usageDelta: settled.turnUsage,
+ };
}
-function settledResult(outcome: SessionTerminalOutcome): {
+function settledResult(
+ outcome: SessionTerminalOutcome,
+ context: SessionFinalizationContext,
+): {
readonly isError: boolean;
readonly output: unknown;
readonly sessionUsage?: TokenUsage;
readonly turnUsage?: TokenUsage;
} {
+ const session = context.cursor.sessionState?.snapshot.session;
+ const usage =
+ session === undefined || context.caller === undefined
+ ? {}
+ : {
+ sessionUsage: toUsage(getSessionTokenUsage(session)),
+ turnUsage: takeSessionUsageDelta(session).delta,
+ };
switch (outcome.kind) {
case "done":
return {
@@ -112,8 +129,14 @@ function settledResult(outcome: SessionTerminalOutcome): {
turnUsage: outcome.action.usageDelta,
};
case "expired":
- return { isError: false, output: "" };
+ return context.caller === undefined
+ ? { isError: false, output: "" }
+ : {
+ isError: true,
+ output: "The session ended before the delegated task completed.",
+ ...usage,
+ };
case "failed":
- return { isError: true, output: normalizeSerializableError(outcome.error) };
+ return { isError: true, output: normalizeSerializableError(outcome.error), ...usage };
}
}
diff --git a/packages/eve/src/execution/session/handoff-steps.ts b/packages/eve/src/execution/session/handoff-steps.ts
index 2ca5161d0..1868f8a99 100644
--- a/packages/eve/src/execution/session/handoff-steps.ts
+++ b/packages/eve/src/execution/session/handoff-steps.ts
@@ -1,4 +1,4 @@
-import { getWorkflowToolRuns, readWorkflowTaskView } from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks, getBlockingWorkflowToolRuns } from "#harness/workflow-tool-runs.js";
import { deserializeContext } from "#context/serialize.js";
import { readDurableSession, type DurableSessionState } from "#execution/durable-session-store.js";
import {
@@ -16,11 +16,9 @@ import { getAgentHandleStore } from "#subagents/handles/store.js";
/** Parses retained work with this deployment's code before deciding whether it can move. */
export function isSessionStateIdleForHandoff(sessionState: DurableSessionState): boolean {
const { state } = readDurableSession(sessionState);
- // Parse all entries, including terminal tasks, before any busy-work shortcut.
- const invocations = getWorkflowToolRuns(state);
- for (const entry of invocations) {
- if (entry.lifetime === "session") readWorkflowTaskView(entry.task);
- }
+ // Decoding every background task, terminal ones included, rejects corrupt outcomes before any
+ // busy-work shortcut.
+ const backgroundTasks = getBackgroundTasks(state).query();
const handles = getAgentHandleStore(state);
// These registries are deleted when work settles. Their ordinary readers
@@ -46,7 +44,8 @@ export function isSessionStateIdleForHandoff(sessionState: DurableSessionState):
handles.handles.every(
(handle) => handle.phase === "parked" || handle.phase === "available",
)) &&
- invocations.every((entry) => entry.lifetime === "session" && entry.task.outcome !== undefined)
+ getBlockingWorkflowToolRuns(state).length === 0 &&
+ backgroundTasks.every((task) => task.status !== "working")
);
}
diff --git a/packages/eve/src/execution/session/input-queue.test.ts b/packages/eve/src/execution/session/input-queue.test.ts
new file mode 100644
index 000000000..a8d965d0a
--- /dev/null
+++ b/packages/eve/src/execution/session/input-queue.test.ts
@@ -0,0 +1,121 @@
+import { describe, expect, it } from "vitest";
+import type { DeliverHookPayload } from "#channel/types.js";
+import { SessionInputQueue } from "#execution/session/input-queue.js";
+import { recordWorkflowTaskView, registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
+import { createTestSessionState } from "#internal/testing/session-state.js";
+import type { SessionStateMap } from "#harness/types.js";
+
+function tasks() {
+ let session = createTestSessionState({}).snapshot.session;
+ for (const taskId of ["first", "second"]) {
+ session = registerWorkflowToolRun(session, {
+ callId: taskId,
+ toolName: "worker",
+ lifetime: "session",
+ origin: { turnId: "turn-1", stepIndex: 0 },
+ address: { runId: `${taskId}-run`, hookToken: `${taskId}-inbox` },
+ task: {
+ taskId,
+ cohortId: "siblings",
+ metadata: { kind: "subagent", name: "worker" },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ });
+ }
+ return session.state;
+}
+
+function settle(
+ state: SessionStateMap | undefined,
+ taskId: string,
+ status: "completed" | "cancelled",
+) {
+ return recordWorkflowTaskView(state, {
+ taskId,
+ metadata: { kind: "subagent", name: "worker" },
+ ...(status === "cancelled"
+ ? { status }
+ : { status, lastOutput: { type: "result", data: "done" } }),
+ }).state;
+}
+
+const first: DeliverHookPayload = {
+ kind: "deliver",
+ taskDeliveryId: "first:ready:completed",
+ payloads: [{ message: "First finished" }],
+};
+const second: DeliverHookPayload = {
+ kind: "deliver",
+ taskDeliveryId: "second:ready:completed",
+ payloads: [{ message: "Second finished" }],
+};
+const user: DeliverHookPayload = { kind: "deliver", payloads: [{ message: "New request" }] };
+
+describe.each(["auto", "cohort"] as const)(
+ "task notifications with %s delivery",
+ (taskDeliveryPolicy) => {
+ it("discards an already queued completion during cancellation without changing its outcome", () => {
+ const queue = new SessionInputQueue();
+ let state = settle(tasks(), "first", "completed");
+ queue.enqueueDelivery(first);
+ state = settle(state, "second", "cancelled");
+ queue.discardStaleNotifications(state);
+ expect(queue.takeNext(state, { taskDeliveryPolicy })).toBeUndefined();
+ expect(queue.pendingCount).toBe(0);
+ });
+
+ it("uses restored outcomes to reject late model notifications while retaining settlement", () => {
+ const state = JSON.parse(JSON.stringify(settle(tasks(), "first", "cancelled")));
+ const queue = new SessionInputQueue();
+ expect(queue.enqueueDelivery(first, state)).toBeUndefined();
+ expect(
+ queue.enqueueDelivery({ ...first, taskDeliveryId: "first:update:1" }, state),
+ ).toBeUndefined();
+ const settlement: DeliverHookPayload = {
+ ...first,
+ payloads: [
+ {
+ task: {
+ views: [
+ {
+ taskId: "first",
+ status: "cancelled",
+ metadata: { kind: "subagent", name: "worker" },
+ },
+ ],
+ },
+ },
+ ],
+ };
+ const admitted = queue.enqueueDelivery(settlement, state);
+ expect(admitted).toBeDefined();
+ queue.discardStaleNotifications(state);
+ expect(queue.taskDeliveries()).toMatchObject([admitted]);
+ queue.replaceDelivery(admitted!.sequence, undefined);
+ queue.enqueueDelivery(user, state);
+ expect(queue.takeNext(state, { taskDeliveryPolicy })).toMatchObject({ delivery: user });
+ });
+
+ it("preserves updates and results for a worker whose cancellation failed", () => {
+ const queue = new SessionInputQueue();
+ const state = settle(tasks(), "first", "cancelled");
+ const update = { ...second, taskDeliveryId: "second:update:1" };
+ queue.enqueueDelivery(update, state);
+ queue.discardStaleNotifications(state);
+ expect(queue.takeNext(state, { taskDeliveryPolicy })).toMatchObject({ delivery: update });
+ queue.enqueueDelivery(second, state);
+ expect(
+ queue.takeNext(settle(state, "second", "completed"), { taskDeliveryPolicy }),
+ ).toMatchObject({ delivery: second });
+ });
+
+ it("releases a completed sibling without waiting for the cancelled task's duplicate receipt", () => {
+ const queue = new SessionInputQueue();
+ const state = settle(tasks(), "first", "cancelled");
+ queue.enqueueDelivery(second, state);
+ expect(
+ queue.takeNext(settle(state, "second", "completed"), { taskDeliveryPolicy }),
+ ).toMatchObject({ delivery: second });
+ });
+ },
+);
diff --git a/packages/eve/src/execution/session/input-queue.ts b/packages/eve/src/execution/session/input-queue.ts
index 6cc9a1e88..ddc820ccc 100644
--- a/packages/eve/src/execution/session/input-queue.ts
+++ b/packages/eve/src/execution/session/input-queue.ts
@@ -1,12 +1,12 @@
+import type { SessionStateMap } from "#harness/types.js";
+import { getBackgroundTasks, type BackgroundTasks } from "#harness/workflow-tool-runs.js";
+import { hasRecordedTaskOutcome, taskIdOfDelivery } from "#tasks/notification.js";
import type { TaskDeliveryPolicy, DeliverHookPayload, DeliverPayload } from "#channel/types.js";
import { coalesceDeliveries } from "#harness/messages.js";
import { jsonValuesEqual } from "#shared/json.js";
-import type { getSessionTaskCohorts } from "#tasks/session-task-cohorts.js";
export type SessionControl = "clear" | "compact" | "expired" | "reset";
-type TaskCohorts = ReturnType<typeof getSessionTaskCohorts>;
-
export interface DeliveryAdmission {
readonly delivery: DeliverHookPayload;
readonly sequence: number;
@@ -53,8 +53,7 @@ export type SessionInputSelection =
* Ordered, admitted session input, plus the idempotency and cancellation
* facts for task deliveries this owner has seen. Entries are private; callers
* receive typed admission and selection values. Everything here is rebuilt
- * deterministically on replay, and a successor never inherits it because
- * handoff requires every indexed task to be terminal.
+ * deterministically on replay. Indexed task outcomes live in session state.
*/
export class SessionInputQueue {
private readonly entries: QueuedSessionInput[] = [];
@@ -66,17 +65,18 @@ export class SessionInputQueue {
return this.entries.length;
}
- /** Admits a delivery unless it repeats or belongs to a cancelled task. */
- enqueueDelivery(delivery: DeliverHookPayload): DeliveryAdmission | undefined {
+ /** Drops notifications the model must not see; lifecycle envelopes are always admitted. */
+ enqueueDelivery(
+ delivery: DeliverHookPayload,
+ state?: SessionStateMap,
+ ): DeliveryAdmission | undefined {
+ if (isStaleNotification(delivery, state)) return undefined;
const deliveryId = taskDeliveryId(delivery);
if (deliveryId !== undefined) {
const terminalId = terminalTaskId(delivery);
// Competing outcomes for one task must not produce separate cohort reports.
const deduplicationId = terminalId === undefined ? deliveryId : `${terminalId}:ready`;
- if (
- this.seenTaskDeliveryIds.has(deduplicationId) ||
- this.isCancelledTaskDelivery(deliveryId)
- ) {
+ if (this.seenTaskDeliveryIds.has(deduplicationId) || this.isCancelledNotification(delivery)) {
return undefined;
}
this.seenTaskDeliveryIds.add(deduplicationId);
@@ -131,8 +131,7 @@ export class SessionInputQueue {
taskDeliveries(): readonly DeliveryAdmission[] {
return this.entries.filter(
(entry): entry is QueuedDelivery =>
- entry.kind === "delivery" &&
- entry.delivery.payloads.some((payload) => payload.task !== undefined),
+ entry.kind === "delivery" && !isNotificationOnly(entry.delivery),
);
}
@@ -141,21 +140,22 @@ export class SessionInputQueue {
(entry) => entry.kind === "delivery" && entry.sequence === sequence,
);
if (index < 0) return;
- if (delivery === undefined) {
+ if (delivery === undefined || this.isCancelledNotification(delivery)) {
this.entries.splice(index, 1);
return;
}
this.entries[index] = { delivery, kind: "delivery", sequence };
}
- /** Drops queued notifications from a cancelled task and refuses later ones. */
+ /** Revokes model wakeups for a cancelled task; its lifecycle envelopes still reach routing. */
cancelTask(taskId: string): void {
this.cancelledTaskIds.add(taskId);
- this.retain(
- (entry) =>
- entry.kind !== "delivery" ||
- !isTaskDelivery(entry.delivery, (id) => id === taskId || id.startsWith(`${taskId}:`)),
- );
+ this.discardDeliveries((delivery) => this.isCancelledNotification(delivery));
+ }
+
+ /** After cancellation records outcomes, queued notifications for those tasks are stale. */
+ discardStaleNotifications(state: SessionStateMap | undefined): void {
+ this.discardDeliveries((delivery) => isStaleNotification(delivery, state));
}
takeSteering(
@@ -179,7 +179,7 @@ export class SessionInputQueue {
}
takeNext(
- cohorts: TaskCohorts,
+ state: SessionStateMap | undefined,
options?: {
readonly deferDeliveries?: boolean;
readonly taskDeliveryPolicy?: TaskDeliveryPolicy;
@@ -210,39 +210,45 @@ export class SessionInputQueue {
};
}
}
+ const tasks = getBackgroundTasks(state);
+ // A cohort reports once every task is ready; a cancelled task's notification may never arrive.
+ const pendingCohorts = new Set(
+ tasks
+ .query({ state: "working" })
+ .filter(
+ ({ taskId }) =>
+ !this.seenTaskDeliveryIds.has(`${taskId}:ready`) && !this.cancelledTaskIds.has(taskId),
+ )
+ .map((task) => task.cohortId),
+ );
const index = this.nextActionableIndex(
- cohorts,
+ tasks,
+ pendingCohorts,
options?.deferDeliveries === true,
options?.taskDeliveryPolicy ?? "cohort",
);
if (index < 0) return undefined;
- return this.takeSelectionAt(index, cohorts, options?.freshSequence);
+ return this.takeSelectionAt(index, tasks, options?.freshSequence);
}
private nextActionableIndex(
- cohorts: TaskCohorts,
+ tasks: BackgroundTasks,
+ pendingCohorts: ReadonlySet<string>,
deferDeliveries: boolean,
taskDeliveryPolicy: TaskDeliveryPolicy,
): number {
- const pendingCohorts = new Set<string>();
- for (const [taskId, cohortId] of cohorts) {
- // A control step can record cancellation before its notification is admitted.
- // Wait for that notification too, so it cannot trigger a second cohort report.
- if (!this.seenTaskDeliveryIds.has(`${taskId}:ready`) && !this.cancelledTaskIds.has(taskId)) {
- pendingCohorts.add(cohortId);
- }
- }
return this.entries.findIndex((entry) => {
if (entry.kind === "control") return true;
if (entry.kind === "authorization" || deferDeliveries) return false;
- const cohort = terminalCohort(entry.delivery, cohorts);
+ if (this.isCancelledDelivery(entry.delivery)) return false;
+ const cohort = terminalCohort(entry.delivery, tasks);
return taskDeliveryPolicy === "auto" || cohort === undefined || !pendingCohorts.has(cohort);
});
}
private takeSelectionAt(
index: number,
- cohorts: TaskCohorts,
+ tasks: BackgroundTasks,
freshSequence: number | undefined,
): SessionInputSelection {
const selected = this.entries[index]!;
@@ -251,34 +257,38 @@ export class SessionInputQueue {
if (selected.kind === "control") return { control: selected.control, kind: "control" };
return { kind: "authorization-resume", payloads: [selected.payload] };
}
- const readyCohort = terminalCohort(selected.delivery, cohorts);
+ const readyCohort = terminalCohort(selected.delivery, tasks);
if (readyCohort !== undefined) {
const lastSibling = this.entries.findLastIndex(
(entry) =>
- entry.kind === "delivery" && terminalCohort(entry.delivery, cohorts) === readyCohort,
+ entry.kind === "delivery" &&
+ !this.isCancelledDelivery(entry.delivery) &&
+ terminalCohort(entry.delivery, tasks) === readyCohort,
);
const boundary = this.entries.findIndex(
(entry, position) =>
position > index &&
position < lastSibling &&
- (entry.kind !== "delivery" || terminalCohort(entry.delivery, cohorts) === undefined),
+ (entry.kind !== "delivery" ||
+ (!this.isCancelledDelivery(entry.delivery) &&
+ terminalCohort(entry.delivery, tasks) === undefined)),
);
- if (boundary >= 0) return this.takeSelectionAt(boundary, cohorts, freshSequence);
+ if (boundary >= 0) return this.takeSelectionAt(boundary, tasks, freshSequence);
}
const first = this.entries.splice(index, 1)[0]!;
if (first.kind !== "delivery") throw new Error("Selected a non-delivery entry as a turn.");
const turnEntries = [first];
- const cohort = terminalCohort(first.delivery, cohorts);
+ const cohort = terminalCohort(first.delivery, tasks);
if (cohort !== undefined) {
const siblings = this.entries.filter(
(entry): entry is QueuedDelivery =>
- entry.kind === "delivery" && terminalCohort(entry.delivery, cohorts) === cohort,
+ entry.kind === "delivery" &&
+ !this.isCancelledDelivery(entry.delivery) &&
+ terminalCohort(entry.delivery, tasks) === cohort,
);
turnEntries.push(...siblings);
- this.retain(
- (entry) => entry.kind !== "delivery" || terminalCohort(entry.delivery, cohorts) !== cohort,
- );
+ this.retain((entry) => entry.kind !== "delivery" || !siblings.includes(entry));
} else {
const authenticated =
first.delivery.auth != null && first.delivery.auth.principalType !== "anonymous";
@@ -315,11 +325,18 @@ export class SessionInputQueue {
};
}
- private isCancelledTaskDelivery(deliveryId: string): boolean {
- for (const taskId of this.cancelledTaskIds) {
- if (deliveryId === taskId || deliveryId.startsWith(`${taskId}:`)) return true;
- }
- return false;
+ private isCancelledDelivery(delivery: DeliverHookPayload): boolean {
+ const deliveryId = taskDeliveryId(delivery);
+ return deliveryId !== undefined && this.cancelledTaskIds.has(taskIdOfDelivery(deliveryId));
+ }
+
+ /** A cancelled task's notification never reaches the model. */
+ private isCancelledNotification(delivery: DeliverHookPayload): boolean {
+ return isNotificationOnly(delivery) && this.isCancelledDelivery(delivery);
+ }
+
+ private discardDeliveries(predicate: (delivery: DeliverHookPayload) => boolean): void {
+ this.retain((entry) => entry.kind !== "delivery" || !predicate(entry.delivery));
}
private retain(predicate: (entry: QueuedSessionInput) => boolean): void {
@@ -328,6 +345,19 @@ export class SessionInputQueue {
}
}
+/** Carries only model-facing text, without `payload.task` envelopes that routing must apply. */
+function isNotificationOnly(delivery: DeliverHookPayload): boolean {
+ return delivery.payloads.every((payload) => payload.task === undefined);
+}
+
+/** A notification for a task whose outcome the parent already recorded and reported. */
+function isStaleNotification(
+ delivery: DeliverHookPayload,
+ state: SessionStateMap | undefined,
+): boolean {
+ return isNotificationOnly(delivery) && hasRecordedTaskOutcome(delivery, state);
+}
+
export function isSteeringDelivery(
delivery: DeliverHookPayload,
callerCallId: string | undefined,
@@ -362,17 +392,9 @@ function taskDeliveryId(delivery: DeliverHookPayload): string | undefined {
return delivery.taskDeliveryId ?? delivery.caller?.taskId;
}
-function isTaskDelivery(
- delivery: DeliverHookPayload,
- predicate: (deliveryId: string) => boolean,
-): boolean {
- const deliveryId = taskDeliveryId(delivery);
- return deliveryId !== undefined && predicate(deliveryId);
-}
-
-function terminalCohort(delivery: DeliverHookPayload, cohorts: TaskCohorts): string | undefined {
+function terminalCohort(delivery: DeliverHookPayload, tasks: BackgroundTasks): string | undefined {
const taskId = terminalTaskId(delivery);
- return taskId === undefined ? undefined : cohorts.get(taskId);
+ return taskId === undefined ? undefined : tasks.get(taskId)?.cohortId;
}
function terminalTaskId(delivery: DeliverHookPayload): string | undefined {
diff --git a/packages/eve/src/execution/session/next-input.test.ts b/packages/eve/src/execution/session/next-input.test.ts
index c03f6d2b1..41c05b429 100644
--- a/packages/eve/src/execution/session/next-input.test.ts
+++ b/packages/eve/src/execution/session/next-input.test.ts
@@ -1,3 +1,5 @@
+import { applySessionCancellation } from "#execution/session/admission.js";
+import { recordWorkflowTaskView } from "#harness/workflow-tool-runs.js";
import { createTestSessionState } from "#internal/testing/session-state.js";
import { assert, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -271,6 +273,50 @@ describe("nextTurnDelivery", () => {
});
});
+ it("discards a held completion when session cancellation records the remaining task", async () => {
+ const input = batchingInput(2);
+ const session = input.cursor.sessionState.snapshot.session;
+ const completed = recordWorkflowTaskView(session.state, {
+ taskId: "task_0",
+ metadata: { kind: "subagent", name: "worker" },
+ status: "completed",
+ lastOutput: { type: "result", data: "done" },
+ }).state;
+ await input.cursor.apply({
+ sessionState: {
+ ...input.cursor.sessionState,
+ snapshot: { session: { ...session, state: completed } },
+ },
+ });
+ input.queue.enqueueDelivery(report(completion("task_0")));
+ vi.mocked(cancelAllIndexedSessionTasksStep).mockImplementation(async ({ sessionState }) => ({
+ sessionState: {
+ ...sessionState,
+ snapshot: {
+ session: {
+ ...sessionState.snapshot.session,
+ state: recordWorkflowTaskView(sessionState.snapshot.session.state, {
+ taskId: "task_1",
+ metadata: { kind: "subagent", name: "worker" },
+ status: "cancelled",
+ }).state,
+ },
+ },
+ },
+ }));
+
+ await applySessionCancellation({ kind: "cancel", tasks: true }, input);
+
+ expect(input.queue.pendingCount).toBe(0);
+ expect(input.queue.isTaskCancelled("task_0")).toBe(false);
+ expect(input.queue.isTaskCancelled("task_1")).toBe(false);
+ input.inbox = createMockInbox([messageRead("New request")]);
+ await expect(nextTurnDelivery(input)).resolves.toMatchObject({
+ kind: "turn",
+ delivery: { payloads: [{ message: "New request" }] },
+ });
+ });
+
it("resumes authorization after a consumed no-op cancel", async () => {
// A cancel with no active turn is consumed without producing a parent
// turn; the wait continues and the callback must still resume the challenge.
@@ -632,7 +678,7 @@ describe("buffered task completion batching", () => {
delivery: question,
});
expect(
- input.queue.takeNext(new Map(), { taskDeliveryPolicy: "auto", deferDeliveries: true }),
+ input.queue.takeNext(undefined, { taskDeliveryPolicy: "auto", deferDeliveries: true }),
).toBeUndefined();
expect(input.queue.pendingCount).toBe(2);
await expect(nextTurnDelivery(input)).resolves.toMatchObject({
@@ -852,18 +898,16 @@ describe("buffered task completion batching", () => {
});
});
- it("waits for a recorded cancellation's notification before reporting its cohort", () => {
- const queue = new SessionInputQueue();
- const cohorts = new Map([
- ["task_0", "cohort"],
- ["task_1", "cohort"],
- ]);
- queue.enqueueDelivery(completion("task_0"));
- expect(queue.takeNext(cohorts)).toBeUndefined();
- queue.enqueueDelivery(terminalDelivery("task_1", "cancelled"));
- expect(queue.takeNext(cohorts)).toMatchObject({ kind: "turn" });
- expect(queue.pendingCount).toBe(0);
- expect(queue.enqueueDelivery(completion("task_1"))).toBeUndefined();
+ it("releases a cohort when cancellation is already recorded without waiting for its echo", () => {
+ const input = batchingInput(2);
+ const state = recordWorkflowTaskView(input.cursor.sessionState.snapshot.session.state, {
+ taskId: "task_1",
+ metadata: { kind: "subagent", name: "worker" },
+ status: "cancelled",
+ }).state;
+ input.queue.enqueueDelivery(completion("task_0"));
+ expect(input.queue.takeNext(state)).toMatchObject({ kind: "turn" });
+ expect(input.queue.pendingCount).toBe(0);
});
it.each(["failed", "cancelled"] as const)(
@@ -879,7 +923,7 @@ describe("buffered task completion batching", () => {
expect(queue.enqueueDelivery(first)).toBeDefined();
expect(queue.enqueueDelivery(late)).toBeUndefined();
expect(queue.pendingCount).toBe(1);
- expect(queue.takeNext(new Map())).toBeDefined();
+ expect(queue.takeNext(undefined)).toBeDefined();
expect(queue.enqueueDelivery(late)).toBeUndefined();
expect(queue.pendingCount).toBe(0);
}
diff --git a/packages/eve/src/execution/session/next-input.ts b/packages/eve/src/execution/session/next-input.ts
index 19acf0727..e23b20c02 100644
--- a/packages/eve/src/execution/session/next-input.ts
+++ b/packages/eve/src/execution/session/next-input.ts
@@ -14,7 +14,6 @@ import {
} from "#execution/session/admission.js";
import type { SessionStateCursor } from "#execution/session/state-cursor.js";
import type { WorkflowToolRunMessage } from "#execution/tools/workflow/messages.js";
-import { getSessionTaskCohorts } from "#tasks/session-task-cohorts.js";
export type NextTurnInstruction =
| { readonly kind: "workflow"; readonly message: WorkflowToolRunMessage }
@@ -54,18 +53,15 @@ export async function nextTurnDelivery(input: {
queue.replaceDelivery(sequence, routed.kind === "cancel-turn" ? undefined : routed.remainder);
if (routed.kind === "cancel-turn") return routed;
}
- const selected = queue.takeNext(
- getSessionTaskCohorts(cursor.sessionState.snapshot.session.state),
- {
- deferDeliveries: input.deferDeliveries,
- taskDeliveryPolicy:
- cursor.serializedContext[TASK_DELIVERY_POLICY_CONTEXT_KEY_NAME] === "auto"
- ? "auto"
- : "cohort",
- expectedAttemptIds: input.expectedAttemptIds,
- freshSequence: inbox.hasPending() ? undefined : freshSequence,
- },
- );
+ const selected = queue.takeNext(cursor.sessionState.snapshot.session.state, {
+ deferDeliveries: input.deferDeliveries,
+ taskDeliveryPolicy:
+ cursor.serializedContext[TASK_DELIVERY_POLICY_CONTEXT_KEY_NAME] === "auto"
+ ? "auto"
+ : "cohort",
+ expectedAttemptIds: input.expectedAttemptIds,
+ freshSequence: inbox.hasPending() ? undefined : freshSequence,
+ });
if (selected?.kind === "control") return { kind: selected.control };
if (selected?.kind === "authorization-resume") return selected;
if (selected?.kind === "turn") {
diff --git a/packages/eve/src/execution/session/program.ts b/packages/eve/src/execution/session/program.ts
index 797fe08de..adc0b6f7d 100644
--- a/packages/eve/src/execution/session/program.ts
+++ b/packages/eve/src/execution/session/program.ts
@@ -316,7 +316,7 @@ async function runSessionLoop(
? cancelledCaller
: { ...cancelledCaller, usage: settled.usage },
);
- } else if (action.settled !== undefined) {
+ } else if (action.settled?.notifyCaller === true) {
if (progress.caller !== undefined) {
await notifyTurnCallerStep({
caller: progress.caller,
@@ -353,8 +353,7 @@ async function runSessionLoop(
sessionState: cursor.sessionState,
});
await settleCancelledTurn();
- // Re-enter with `settled` cleared: the parked answer was already
- // delivered to its caller before this wait.
+ // Cancellation consumes any outstanding caller; do not report the prior turn.
action = { ...action, settled: undefined };
continue;
case "turn": {
diff --git a/packages/eve/src/execution/session/state-compatibility.test.ts b/packages/eve/src/execution/session/state-compatibility.test.ts
index b7a77901b..b09e95ce7 100644
--- a/packages/eve/src/execution/session/state-compatibility.test.ts
+++ b/packages/eve/src/execution/session/state-compatibility.test.ts
@@ -4,7 +4,7 @@ import { isSessionStateIdleForHandoff } from "#execution/session/handoff-steps.j
import {
readWorkflowTaskView,
recordWorkflowTaskView,
- getBackgroundWorkflowToolRuns,
+ getBackgroundTasks,
registerWorkflowToolRun,
} from "#harness/workflow-tool-runs.js";
import { parseActivityWorkIdentityV1 } from "#protocol/activity.js";
@@ -103,7 +103,11 @@ describe("additive durable state", () => {
authored: { opaque: true },
"eve.workflowTool": { version: 3, runs: [task], futureIndex: true },
};
- expect(getBackgroundWorkflowToolRuns(restored(state))).toEqual([task]);
+ expect(
+ getBackgroundTasks(restored(state))
+ .query()
+ .map((entry) => entry.run),
+ ).toEqual([task]);
expect(parseActivityWorkIdentityV1(activity)).toEqual(activity);
const updated = registerWorkflowToolRun(session(restored(state)), {
callId: "task",
@@ -129,7 +133,7 @@ describe("additive durable state", () => {
status: "completed",
lastOutput: { type: "result", data: "updated" },
usage: { inputTokens: 3, outputTokens: 4, cacheReadTokens: 0, cacheWriteTokens: 0 },
- });
+ }).state;
expect(restored(saved)).toMatchObject({
authored: { opaque: true },
"eve.workflowTool": {
@@ -159,15 +163,15 @@ describe("additive durable state", () => {
taskId: "task",
metadata,
status: "cancelled",
- });
- expect(getBackgroundWorkflowToolRuns(restored(cancelled))[0]?.task.outcome).toMatchObject({
+ }).state;
+ expect(getBackgroundTasks(restored(cancelled)).get("task")?.run.task.outcome).toMatchObject({
futureView: true,
status: "completed",
});
expect(cancelled).toBe(saved);
- const [retained] = getBackgroundWorkflowToolRuns(restored(cancelled));
+ const retained = getBackgroundTasks(restored(cancelled)).get("task");
assert(retained !== undefined);
- expect(readWorkflowTaskView(retained.task)?.lastOutput?.data).toBe("done");
+ expect(readWorkflowTaskView(retained.run.task)?.lastOutput?.data).toBe("done");
});
});
diff --git a/packages/eve/src/execution/session/task-settlement.integration.test.ts b/packages/eve/src/execution/session/task-settlement.integration.test.ts
index 616d59762..6ba063457 100644
--- a/packages/eve/src/execution/session/task-settlement.integration.test.ts
+++ b/packages/eve/src/execution/session/task-settlement.integration.test.ts
@@ -16,10 +16,7 @@ import {
getProxyInputRequests,
upsertProxyInputRequestState,
} from "#harness/proxy-input-requests.js";
-import {
- findBackgroundWorkflowToolRun,
- registerWorkflowToolRun,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks, registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
import { createTestSessionState } from "#internal/testing/session-state.js";
import type { TaskView } from "#tasks/types.js";
@@ -103,7 +100,7 @@ it.each(["completed", "failed", "cancelled"] as const)(
// The owner is about to wait for B; A must already be settled without a model turn.
reads++;
const current = cursor.sessionState.snapshot.session;
- expect(findBackgroundWorkflowToolRun(current.state, "A")?.task.outcome).toEqual({
+ expect(getBackgroundTasks(current.state).get("A")?.run.task.outcome).toEqual({
status: a.status,
lastOutput: a.lastOutput,
usage: a.usage,
@@ -167,8 +164,7 @@ it.each(["completed", "failed", "cancelled"] as const)(
};
vi.mocked(emitSubagentEventStep).mockImplementation(async (input) => {
expect(
- findBackgroundWorkflowToolRun(input.sessionState.snapshot.session.state, "task")?.task
- .outcome,
+ getBackgroundTasks(input.sessionState.snapshot.session.state).get("task")?.run.task.outcome,
).toEqual({ status: view.status, lastOutput: view.lastOutput, usage: view.usage });
return {
serializedContext: input.serializedContext,
@@ -205,7 +201,7 @@ it.each(["completed", "failed", "cancelled"] as const)(
);
}
expect(
- findBackgroundWorkflowToolRun(sessionState.snapshot.session.state, "task")?.task.outcome,
+ getBackgroundTasks(sessionState.snapshot.session.state).get("task")?.run.task.outcome,
).toEqual({ status: view.status, lastOutput: view.lastOutput, usage: view.usage });
},
);
diff --git a/packages/eve/src/execution/session/turn-step-result.test.ts b/packages/eve/src/execution/session/turn-step-result.test.ts
new file mode 100644
index 000000000..68b5fe35a
--- /dev/null
+++ b/packages/eve/src/execution/session/turn-step-result.test.ts
@@ -0,0 +1,151 @@
+import { describe, expect, it } from "vitest";
+import { resolveSessionStepResult } from "#execution/session/turn-step-result.js";
+import { setTurnUsageState, takeSessionUsageDelta } from "#harness/turn-tag-state.js";
+import type { HarnessSession, SettledTurn } from "#harness/types.js";
+import { recordWorkflowTaskView, registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
+
+function session(): HarnessSession {
+ return {
+ agent: { modelReference: { id: "unused" }, system: "", tools: [] },
+ compaction: { recentWindowSize: 10, threshold: 100_000 },
+ continuationToken: "detector",
+ history: [],
+ sessionId: "detector",
+ };
+}
+
+function startWorker(
+ session: HarnessSession,
+ taskId: string,
+ turnId = "current-turn",
+): HarnessSession {
+ return registerWorkflowToolRun(session, {
+ callId: taskId,
+ toolName: "worker",
+ lifetime: "session",
+ origin: { turnId, stepIndex: 0 },
+ address: { runId: taskId, hookToken: taskId },
+ task: {
+ taskId,
+ metadata: { kind: "subagent", name: "worker" },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ });
+}
+
+function endTurn(session: HarnessSession, settledTurn: SettledTurn, turnId = "current-turn") {
+ return resolveSessionStepResult(
+ { next: null, session, settledTurn },
+ {},
+ "conversation",
+ {},
+ turnId,
+ );
+}
+
+function withUsage(session: HarnessSession, inputTokens: number): HarnessSession {
+ const totals = {
+ inputTokens,
+ outputTokens: 0,
+ cacheReadTokens: 0,
+ cacheWriteTokens: 0,
+ costUsd: 0,
+ sawCost: false,
+ };
+ return setTurnUsageState(session, { ...totals, session: totals, turnId: "current-turn" });
+}
+
+describe("delegated turn completion", () => {
+ it("defers caller notification while a task is working and reports accumulated usage with the final result", () => {
+ const pending = startWorker(withUsage(session(), 100), "worker-1");
+ const parked = endTurn(pending, { output: "Verification is running." });
+ expect(parked).toMatchObject({
+ action: "park",
+ settled: { output: "Verification is running.", notifyCaller: false },
+ });
+ const checkpoint = { ...pending, state: parked.sessionState.snapshot.session.state };
+ expect(takeSessionUsageDelta(checkpoint).delta.inputTokens).toBe(100);
+
+ const completed = withUsage(
+ {
+ ...checkpoint,
+ state: recordWorkflowTaskView(checkpoint.state, {
+ taskId: "worker-1",
+ metadata: { kind: "subagent", name: "worker" },
+ status: "completed",
+ lastOutput: { type: "result", data: "VERIFIED" },
+ }).state,
+ },
+ 150,
+ );
+ // The worker's notification wakes a later turn, which gives the final answer.
+ const settled = endTurn(completed, { output: "VERIFIED" }, "woken-turn");
+ expect(settled).toMatchObject({
+ action: "park",
+ settled: {
+ notifyCaller: true,
+ output: "VERIFIED",
+ usage: { inputTokens: 150 },
+ },
+ });
+ expect(
+ takeSessionUsageDelta({ ...completed, state: settled.sessionState.snapshot.session.state })
+ .delta.inputTokens,
+ ).toBe(0);
+ });
+
+ it("does not notify the caller after one worker finishes while another is working", () => {
+ const pending = startWorker(startWorker(session(), "worker-1"), "worker-2");
+ const partiallyCompleted = {
+ ...pending,
+ state: recordWorkflowTaskView(pending.state, {
+ taskId: "worker-1",
+ metadata: { kind: "subagent", name: "worker" },
+ status: "completed",
+ lastOutput: { type: "result", data: "First result" },
+ }).state,
+ };
+ expect(endTurn(partiallyCompleted, { output: "Still working." })).toMatchObject({
+ action: "park",
+ settled: { notifyCaller: false },
+ });
+ });
+
+ it("answers the caller when only work started by an earlier turn is still working", () => {
+ const pending = startWorker(session(), "monitor", "earlier-turn");
+ expect(endTurn(pending, { output: "Here is the report." })).toMatchObject({
+ action: "park",
+ settled: { notifyCaller: true, output: "Here is the report." },
+ });
+ });
+
+ it.each(["failed", "cancelled"] as const)(
+ "allows a final response after a worker is %s",
+ (status) => {
+ const pending = startWorker(session(), "worker-1");
+ const finished = {
+ ...pending,
+ state: recordWorkflowTaskView(pending.state, {
+ taskId: "worker-1",
+ metadata: { kind: "subagent", name: "worker" },
+ ...(status === "failed"
+ ? { status, lastOutput: { type: "error" as const, data: "Failed" } }
+ : { status }),
+ }).state,
+ };
+ expect(endTurn(finished, { output: "Unable to verify." })).toMatchObject({
+ action: "park",
+ settled: { notifyCaller: true, output: "Unable to verify." },
+ });
+ },
+ );
+
+ it("answers the caller with a final error even when its worker is still working", () => {
+ expect(
+ endTurn(startWorker(session(), "worker-1"), { isError: true, output: "Model failed" }),
+ ).toMatchObject({
+ action: "park",
+ settled: { notifyCaller: true, isError: true, output: "Model failed" },
+ });
+ });
+});
diff --git a/packages/eve/src/execution/session/turn-step-result.ts b/packages/eve/src/execution/session/turn-step-result.ts
index 8e728d4e3..bfb234f98 100644
--- a/packages/eve/src/execution/session/turn-step-result.ts
+++ b/packages/eve/src/execution/session/turn-step-result.ts
@@ -1,6 +1,7 @@
import { createDurableSessionState } from "#execution/durable-session-store.js";
import { derivePendingState } from "#execution/session/pending-turn-state.js";
import type { DurableStepResult } from "#execution/session/turn-step-types.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import { hasPendingInputBatch } from "#harness/input-requests.js";
import { getTurnUsageState, takeSessionUsageDelta, toUsage } from "#harness/turn-tag-state.js";
import type { StepResult } from "#harness/types.js";
@@ -12,6 +13,8 @@ export function resolveSessionStepResult(
nextSerializedContext: Record<string, unknown>,
mode: RunMode,
beforeStepContext: Record<string, unknown>,
+ /** The turn this step belongs to. */
+ turnId: string,
): DurableStepResult {
const nextState = createDurableSessionState({ session: stepResult.session });
if (stepResult.steered)
@@ -59,11 +62,23 @@ export function resolveSessionStepResult(
if (stepResult.next === null) {
const pending = derivePendingState(stepResult.session);
- // `settledTurn` is the harness's explicit settlement verdict. Pending
- // state may predate this turn, while newly created parks omit the verdict.
- // `usage` carries only this turn's delta: the take marks the totals
- // reported, so a persistent child never re-reports earlier spend.
+ // A turn that ends while its own background tasks are working yields: its answer is interim.
+ // Usage stays unreported so the caller's final result includes every yielded turn. An error
+ // is final and always answers the caller.
if (stepResult.settledTurn !== undefined) {
+ const yielded =
+ stepResult.settledTurn.isError !== true &&
+ getBackgroundTasks(stepResult.session.state).query({ state: "working", turnId }).length > 0;
+ if (yielded) {
+ return {
+ action: "park",
+ ...backgroundTransition,
+ ...pending,
+ settled: { ...stepResult.settledTurn, notifyCaller: false },
+ serializedContext: nextSerializedContext,
+ sessionState: nextState,
+ };
+ }
const { delta, session: reportedSession } = takeSessionUsageDelta(stepResult.session);
return {
action: "park",
@@ -72,6 +87,7 @@ export function resolveSessionStepResult(
serializedContext: nextSerializedContext,
sessionState: createDurableSessionState({ session: reportedSession }),
settled: {
+ notifyCaller: true,
output: stepResult.settledTurn.output,
isError: stepResult.settledTurn.isError,
usage: delta,
diff --git a/packages/eve/src/execution/session/turn-step-types.ts b/packages/eve/src/execution/session/turn-step-types.ts
index 8b43e8276..19a54028c 100644
--- a/packages/eve/src/execution/session/turn-step-types.ts
+++ b/packages/eve/src/execution/session/turn-step-types.ts
@@ -4,6 +4,12 @@ import type { SettledTurn, StepResult } from "#harness/types.js";
import type { RuntimeActionResult } from "#shared/action-types.js";
import type { TokenUsage } from "#shared/token-usage.js";
+/** A settled turn as the session parks it. The session, not the harness, decides caller delivery. */
+export interface ParkedSettledTurn extends SettledTurn {
+ /** False while background tasks started by this turn are still working. */
+ readonly notifyCaller: boolean;
+}
+
/** Trusted runtime-action results collected by the session owner. */
export interface RuntimeActionResultStepInput {
readonly acceptedAtMsByCallId?: Readonly<Record<string, number>>;
@@ -56,7 +62,7 @@ export type DurableStepResult = (
readonly hasPendingAuthorization: boolean;
readonly hasPendingInputBatch: boolean;
readonly pendingCoordinationCallIds?: readonly string[];
- readonly settled?: SettledTurn;
+ readonly settled?: ParkedSettledTurn;
}
) &
DurableStepResultFields;
@@ -74,5 +80,5 @@ export type TurnOutcome =
readonly authorizationAttemptIds?: readonly string[];
readonly cancelled?: true;
readonly kind: "park";
- readonly settled?: SettledTurn;
+ readonly settled?: ParkedSettledTurn;
};
diff --git a/packages/eve/src/execution/session/turn-step.test.ts b/packages/eve/src/execution/session/turn-step.test.ts
index f7e982810..69e7578e8 100644
--- a/packages/eve/src/execution/session/turn-step.test.ts
+++ b/packages/eve/src/execution/session/turn-step.test.ts
@@ -1220,7 +1220,11 @@ describe("turnStep", () => {
session.history,
);
return modelTurn === 1
- ? { next: null, session, settledTurn: { output: { report: message } } }
+ ? {
+ next: null,
+ session,
+ settledTurn: { output: { report: message } },
+ }
: { next: { done: true, output: { report: message } }, session };
};
});
@@ -2393,7 +2397,7 @@ describe("turnStep", () => {
expect(result).toMatchObject({
action: "park",
- settled: { output: "settled answer" },
+ settled: { notifyCaller: true, output: "settled answer" },
});
});
@@ -2425,7 +2429,7 @@ describe("turnStep", () => {
expect(result).toMatchObject({
action: "park",
- settled: { output: "settled answer" },
+ settled: { notifyCaller: true, output: "settled answer" },
});
});
@@ -2477,6 +2481,7 @@ describe("turnStep", () => {
expect(first).toMatchObject({
action: "park",
settled: {
+ notifyCaller: true,
output: "first answer",
usage: { cacheReadTokens: 0, cacheWriteTokens: 0, inputTokens: 100, outputTokens: 40 },
},
@@ -2510,6 +2515,7 @@ describe("turnStep", () => {
expect(second).toMatchObject({
action: "park",
settled: {
+ notifyCaller: true,
output: "second answer",
usage: { cacheReadTokens: 0, cacheWriteTokens: 0, inputTokens: 50, outputTokens: 20 },
},
@@ -2556,7 +2562,7 @@ describe("turnStep", () => {
expect(result).toMatchObject({
action: "park",
hasPendingInputBatch: true,
- settled: { output: "settled while approval remains open" },
+ settled: { notifyCaller: true, output: "settled while approval remains open" },
});
});
diff --git a/packages/eve/src/execution/session/turn-step.ts b/packages/eve/src/execution/session/turn-step.ts
index 1529e9567..ea2ff56fa 100644
--- a/packages/eve/src/execution/session/turn-step.ts
+++ b/packages/eve/src/execution/session/turn-step.ts
@@ -618,6 +618,7 @@ async function runSessionStep(input: TurnStepInput): Promise<DurableStepResult>
nextSerializedContext,
mode,
input.serializedContext,
+ activeTurnId(initialEmissionState),
);
if (durableResult.action === "done") await sink.close();
return durableResult;
diff --git a/packages/eve/src/execution/session/turn.test.ts b/packages/eve/src/execution/session/turn.test.ts
index 253443a08..d62b6ce25 100644
--- a/packages/eve/src/execution/session/turn.test.ts
+++ b/packages/eve/src/execution/session/turn.test.ts
@@ -11,6 +11,7 @@ import { turnStep } from "#execution/session/turn-step.js";
import type { DeliverHookPayload } from "#channel/types.js";
import { dispatchCoordinationStep } from "#execution/coordination-dispatch-step.js";
import { routeDeliverToChildren } from "#execution/route-child-delivery.js";
+import type { RunMode } from "#shared/run-mode.js";
vi.mock("#compiled/@workflow/core/index.js", async (importOriginal) => ({
...(await importOriginal()),
@@ -44,6 +45,38 @@ beforeEach(() => {
afterEach(() => vi.restoreAllMocks());
describe("SessionExecution background task checkpoints", () => {
+ it("parks a yielded task-mode turn for background delivery", async () => {
+ const inbox: SessionInbox = {
+ claimedTokens: [],
+ claimSessionHook: vi.fn(),
+ claimSessionHooks: vi.fn(),
+ drain: () => [],
+ hasPending: () => false,
+ next: vi.fn(),
+ restore: vi.fn(),
+ onDelivery: () => () => {},
+ onInterrupt: () => () => {},
+ };
+ const sessionState = state("");
+ const settled = { notifyCaller: false, output: "Verification is running." };
+ vi.mocked(turnStep).mockResolvedValue({
+ action: "park",
+ hasPendingAuthorization: false,
+ hasPendingInputBatch: false,
+ serializedContext: {},
+ sessionState,
+ settled,
+ });
+
+ await expect(
+ createExecution({ inbox, mode: "task", sessionState }).runTurn(undefined),
+ ).resolves.toEqual({
+ authorizationAttemptIds: undefined,
+ kind: "park",
+ settled,
+ });
+ });
+
it("retains the durable steering signal across steps until a correction uses it", async () => {
const inbox: SessionInbox = {
claimedTokens: [],
@@ -371,7 +404,7 @@ describe("SessionExecution background task checkpoints", () => {
).resolves.toEqual({ cancelled: true, kind: "park" });
expect(inbox.restore).not.toHaveBeenCalled();
- expect(queue.takeNext(new Map())).toMatchObject({ delivery: followUp, kind: "turn" });
+ expect(queue.takeNext(undefined)).toMatchObject({ delivery: followUp, kind: "turn" });
});
it("steers a continuing turn with user input while retaining background notifications for cohort routing", async () => {
@@ -509,22 +542,30 @@ describe("SessionExecution background task checkpoints", () => {
hasPendingInputBatch: false,
serializedContext: input.serializedContext,
sessionState: input.sessionState,
- settled: { output: "Done." },
+ settled: { notifyCaller: true, output: "Done." },
}));
await expect(
execution.runTurn({
delivery: { kind: "deliver", payloads: [{ message: "Start the work." }] },
}),
- ).resolves.toMatchObject({ kind: "park", settled: { output: "Done." } });
+ ).resolves.toMatchObject({
+ kind: "park",
+ settled: { notifyCaller: true, output: "Done." },
+ });
expect(turnStep).toHaveBeenCalledTimes(1);
expect(queue.pendingCount).toBe(1);
});
- it.each([false, true])(
- "keeps a settled turn when a late cancellation races its checkpoint (background tasks: %s)",
- async (backgroundTasks) => {
+ it.each([
+ { backgroundTasks: false, settled: { notifyCaller: true, output: "Done." } },
+ { backgroundTasks: true, settled: { notifyCaller: true, output: "Done." } },
+ { backgroundTasks: false, settled: { notifyCaller: false, output: "Still working." } },
+ { backgroundTasks: true, settled: { notifyCaller: false, output: "Still working." } },
+ ] as const)(
+ "preserves the completed turn when cancellation races its checkpoint (notify caller: $settled.notifyCaller, background tasks: $backgroundTasks)",
+ async ({ backgroundTasks, settled }) => {
const followUp: DeliverHookPayload = {
kind: "deliver",
payloads: [{ message: "Follow up after completion." }],
@@ -561,7 +602,7 @@ describe("SessionExecution background task checkpoints", () => {
hasPendingInputBatch: false,
serializedContext: input.serializedContext,
sessionState: completedState,
- settled: { output: "Done." },
+ settled,
...(backgroundTasks
? {
backgroundTaskState: state("http:background"),
@@ -579,7 +620,7 @@ describe("SessionExecution background task checkpoints", () => {
});
await expect(execution.runTurn(undefined)).resolves.toMatchObject({
kind: "park",
- settled: { output: "Done." },
+ settled,
});
expect(execution.cursor.sessionState).toBe(completedState);
expect(cancelDescendantTurnsStep).not.toHaveBeenCalled();
@@ -721,6 +762,7 @@ describe("SessionExecution background task checkpoints", () => {
function createExecution(input: {
readonly inbox: SessionInbox;
+ readonly mode?: RunMode;
readonly queue?: SessionInputQueue;
readonly serializedContext?: Record<string, unknown>;
readonly sessionState: DurableSessionState;
@@ -734,7 +776,7 @@ function createExecution(input: {
return new SessionExecution({
cursor,
inbox: input.inbox,
- mode: "conversation",
+ mode: input.mode ?? "conversation",
queue: input.queue ?? new SessionInputQueue(),
sessionId: input.sessionState.sessionId,
});
diff --git a/packages/eve/src/execution/session/turn.ts b/packages/eve/src/execution/session/turn.ts
index 8df490651..ddb58508e 100644
--- a/packages/eve/src/execution/session/turn.ts
+++ b/packages/eve/src/execution/session/turn.ts
@@ -91,7 +91,7 @@ export class SessionExecution {
const pendingCallIds =
result.action === "park" ? result.pendingCoordinationCallIds : undefined;
const hasBackgroundTasks = (result.backgroundTasks?.length ?? 0) > 0;
- const settled = result.action === "park" && result.settled !== undefined;
+ const turnCompleted = result.action === "park" && result.settled !== undefined;
if (hasBackgroundTasks) {
if (result.backgroundTaskState === undefined) {
@@ -107,7 +107,7 @@ export class SessionExecution {
await cursor.apply({
serializedContext: result.serializedContext,
sessionState:
- result.action === "cancelled" || (!settled && turn.signal.aborted)
+ result.action === "cancelled" || (!turnCompleted && turn.signal.aborted)
? (result.backgroundTaskState ?? result.sessionState)
: result.sessionState,
});
@@ -115,7 +115,11 @@ export class SessionExecution {
turn.resetSteering();
if (result.action === "cancelled") return await this.finishCancelledTurn();
- if (!settled && turn.signal.aborted && (pendingCallIds === undefined || hasBackgroundTasks)) {
+ if (
+ !turnCompleted &&
+ turn.signal.aborted &&
+ (pendingCallIds === undefined || hasBackgroundTasks)
+ ) {
return await this.finishCancelledTurn();
}
@@ -160,6 +164,7 @@ export class SessionExecution {
const canPark =
result.hasPendingAuthorization ||
(result.hasPendingInputBatch && this.input.capabilities?.requestInput === true) ||
+ result.settled?.notifyCaller === false ||
this.input.mode === "conversation";
if (!canPark) throw new Error(TASK_MODE_WAIT_ERROR_MESSAGE);
return {
diff --git a/packages/eve/src/execution/tasks/parent/control-shared.ts b/packages/eve/src/execution/tasks/parent/control-shared.ts
index 23bee24da..57a4a9565 100644
--- a/packages/eve/src/execution/tasks/parent/control-shared.ts
+++ b/packages/eve/src/execution/tasks/parent/control-shared.ts
@@ -1,10 +1,7 @@
import type { HarnessSession as RuntimeSession } from "#harness/types.js";
import type { RuntimeActionResult, RuntimeToolCallActionRequest } from "#shared/action-types.js";
import { taskViewsToJson } from "#tasks/json.js";
-import {
- findBackgroundWorkflowToolRun,
- type BackgroundWorkflowToolRun,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks, type BackgroundWorkflowToolRun } from "#harness/workflow-tool-runs.js";
import type { TaskView } from "#tasks/types.js";
/**
@@ -22,8 +19,9 @@ export function lookupTaskEntries(
| { readonly kind: "unknown"; readonly unknown: string[] } {
const entries: BackgroundWorkflowToolRun[] = [];
const unknown: string[] = [];
+ const tasks = getBackgroundTasks(session.state);
for (const taskId of taskIds) {
- const entry = findBackgroundWorkflowToolRun(session.state, taskId);
+ const entry = tasks.get(taskId)?.run;
if (entry === undefined) {
unknown.push(taskId);
} else {
diff --git a/packages/eve/src/execution/tasks/parent/dispatch.test.ts b/packages/eve/src/execution/tasks/parent/dispatch.test.ts
index 38865401f..d21450919 100644
--- a/packages/eve/src/execution/tasks/parent/dispatch.test.ts
+++ b/packages/eve/src/execution/tasks/parent/dispatch.test.ts
@@ -1,7 +1,4 @@
-import {
- findBackgroundWorkflowToolRun,
- readWorkflowTaskView,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import type { HarnessSession } from "#harness/types.js";
import { WORKFLOW_CANCELLATION_SETTLE_MS } from "#execution/tools/workflow/cancellation-policy.js";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -84,9 +81,7 @@ describe("task cancellation", () => {
},
session,
});
- const recorded = findBackgroundWorkflowToolRun(cancelled.session.state, entry.task.taskId);
- expect(recorded).toBeDefined();
- expect(recorded === undefined ? undefined : readWorkflowTaskView(recorded.task)).toMatchObject({
+ expect(getBackgroundTasks(cancelled.session.state).get(entry.task.taskId)).toMatchObject({
status: "cancelled",
});
expect(sendTaskCommand).toHaveBeenCalledExactlyOnceWith({
diff --git a/packages/eve/src/execution/tasks/parent/dispatch.ts b/packages/eve/src/execution/tasks/parent/dispatch.ts
index 2e50b7143..224cb6f3e 100644
--- a/packages/eve/src/execution/tasks/parent/dispatch.ts
+++ b/packages/eve/src/execution/tasks/parent/dispatch.ts
@@ -62,7 +62,7 @@ export async function executeTaskControlAction(input: {
serializedContext: input.serializedContext,
session,
});
- session = { ...session, state: recordWorkflowTaskView(session.state, view) };
+ session = { ...session, state: recordWorkflowTaskView(session.state, view).state };
views.push(view);
}
return { result: createTaskViewsResult(action, views), session };
diff --git a/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.test.ts b/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.test.ts
index b70c063e5..f0f1c1e66 100644
--- a/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.test.ts
+++ b/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.test.ts
@@ -1,7 +1,4 @@
-import {
- recordWorkflowTaskView,
- getBackgroundWorkflowToolRuns,
-} from "#harness/workflow-tool-runs.js";
+import { recordWorkflowTaskView, getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import { createTestSessionState } from "#internal/testing/session-state.js";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ContextContainer } from "#context/container.js";
@@ -125,7 +122,7 @@ describe("recordTaskInputRequestStep", () => {
metadata: { kind: "tool", name: "export" },
status: "completed",
taskId: "task-1",
- }),
+ }).state,
});
await expect(recordTaskInputRequestStep({ request, sessionState })).resolves.toEqual({
@@ -281,7 +278,7 @@ describe("recordTerminalTaskViewsStep", () => {
});
const state = result.sessionState.snapshot.session.state;
- expect(getBackgroundWorkflowToolRuns(state)[0]?.task.outcome).toEqual({
+ expect(getBackgroundTasks(state).query()[0]?.run.task.outcome).toEqual({
status: view.status,
lastOutput: view.lastOutput,
});
@@ -291,6 +288,16 @@ describe("recordTerminalTaskViewsStep", () => {
expect.objectContaining({ phase: "available" }),
]);
expect(result.serializedContext).toEqual({});
+ expect(result.subagentCompletions).toHaveLength(1);
+
+ vi.mocked(readDurableSession).mockReturnValue(result.sessionState.snapshot.session);
+ const repeated = await recordTerminalTaskViewsStep({
+ serializedContext: {},
+ sessionState: result.sessionState,
+ views: [view],
+ });
+ expect(repeated.subagentCompletions).toEqual([]);
+ expect(repeated.views).toEqual([]);
});
it("settles instrumentation from an accepted terminal task view", async () => {
diff --git a/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.ts b/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.ts
index d0a186efe..70fb51e3f 100644
--- a/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.ts
+++ b/packages/eve/src/execution/tasks/parent/hitl-proxy-steps.ts
@@ -24,11 +24,7 @@ import { isInputRequest } from "#shared/input.js";
import { getAgentHandleStore } from "#subagents/handles/store.js";
import { applyTaskAgentHandleCommand } from "#subagents/handles/transitions.js";
import { createEveTaskInputRoutePath } from "#protocol/routes.js";
-import {
- recordWorkflowTaskView,
- readWorkflowTaskView,
- findBackgroundWorkflowToolRun,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks, recordWorkflowTaskView } from "#harness/workflow-tool-runs.js";
import type { TaskInputRequestDelivery, TaskView } from "#tasks/types.js";
const log = createLogger("execution.tasks.parent");
@@ -48,12 +44,9 @@ export async function recordTaskInputRequestStep(input: {
"use step";
const durableSession = readDurableSession(input.sessionState);
- const entry = findBackgroundWorkflowToolRun(durableSession.state, input.request.taskId);
+ const task = getBackgroundTasks(durableSession.state).get(input.request.taskId);
const requests = input.request.requests ?? [input.request.request];
- if (entry === undefined || requests.length === 0 || !requests.every(isInputRequest)) {
- return { accepted: false, sessionState: input.sessionState };
- }
- if (readWorkflowTaskView(entry.task) !== undefined) {
+ if (task?.status !== "working" || requests.length === 0 || !requests.every(isInputRequest)) {
return { accepted: false, sessionState: input.sessionState };
}
@@ -109,6 +102,7 @@ export async function recordTerminalTaskViewsStep(input: {
}): Promise<{
readonly serializedContext: Record<string, unknown>;
readonly sessionState: DurableSessionState;
+ /** Newly recorded terminal outcomes eligible for model notification. */
readonly views: readonly TaskView[];
readonly subagentCompletions: readonly SubagentCompletedStreamEvent[];
}> {
@@ -116,28 +110,32 @@ export async function recordTerminalTaskViewsStep(input: {
const durableSession = readDurableSession(input.sessionState);
let session = durableSession;
const acceptedViews: TaskView[] = [];
+ const firstOutcomeViews: TaskView[] = [];
const subagentCompletions: SubagentCompletedStreamEvent[] = [];
for (const view of input.views) {
- const entry = findBackgroundWorkflowToolRun(session.state, view.taskId);
+ const entry = getBackgroundTasks(session.state).get(view.taskId)?.run;
if (entry === undefined) continue;
- const state = recordWorkflowTaskView(session.state, view);
- if (state !== session.state) {
- session = { ...session, state };
- if (entry.task.metadata.kind === "subagent" && view.status === "completed") {
- subagentCompletions.push({
- type: "subagent.completed",
- data: {
- callId: entry.callId,
- subagentName: entry.toolName,
- output:
- typeof view.lastOutput.data === "string"
- ? view.lastOutput.data
- : JSON.stringify(view.lastOutput.data),
- },
- });
- }
+ const recorded = recordWorkflowTaskView(session.state, view);
+ session = { ...session, state: recorded.state };
+ if (
+ recorded.firstOutcome &&
+ recorded.view.metadata.kind === "subagent" &&
+ recorded.view.status === "completed"
+ ) {
+ subagentCompletions.push({
+ type: "subagent.completed",
+ data: {
+ callId: entry.callId,
+ subagentName: entry.toolName,
+ output:
+ typeof recorded.view.lastOutput.data === "string"
+ ? recorded.view.lastOutput.data
+ : JSON.stringify(recorded.view.lastOutput.data),
+ },
+ });
}
- acceptedViews.push(readWorkflowTaskView(entry.task) ?? view);
+ acceptedViews.push(recorded.view);
+ if (recorded.firstOutcome) firstOutcomeViews.push(recorded.view);
session = clearProxyInputRequestsForTask(session, view.taskId);
session = applyTaskAgentHandleCommand(session, {
kind: "release-owner",
@@ -153,7 +151,7 @@ export async function recordTerminalTaskViewsStep(input: {
session === durableSession
? input.sessionState
: replaceDurableSessionSnapshot({ session, state: input.sessionState });
- return { serializedContext, sessionState, views: acceptedViews, subagentCompletions };
+ return { serializedContext, sessionState, views: firstOutcomeViews, subagentCompletions };
}
async function settleBackgroundTaskActions(input: {
@@ -166,8 +164,9 @@ async function settleBackgroundTaskActions(input: {
const ctx = await deserializeContext(input.serializedContext);
const observer = ctx.get(ActivityObserverKey);
const settledAt = new Date().toISOString();
+ const tasks = getBackgroundTasks(input.session.state);
const events = input.views.flatMap((view) => {
- const entry = findBackgroundWorkflowToolRun(input.session.state, view.taskId);
+ const entry = tasks.get(view.taskId)?.run;
return projectTaskActivity({
activityObserver:
observer === undefined
diff --git a/packages/eve/src/execution/tasks/parent/tool-execution.integration.test.ts b/packages/eve/src/execution/tasks/parent/tool-execution.integration.test.ts
index cac73fcd1..0b154c4c5 100644
--- a/packages/eve/src/execution/tasks/parent/tool-execution.integration.test.ts
+++ b/packages/eve/src/execution/tasks/parent/tool-execution.integration.test.ts
@@ -21,10 +21,7 @@ import { setHarnessEmissionState } from "#harness/emission-state.js";
import { TurnCancelledError } from "#harness/turn-cancellation.js";
import type { HarnessSession } from "#harness/types.js";
import { getAgentHandleStore, setAgentHandleStore } from "#subagents/handles/store.js";
-import {
- getBackgroundWorkflowToolRuns,
- registerWorkflowToolRun,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks, registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
vi.mock("#execution/tools/subagent/steer.js", () => ({ steerBackgroundAgent: vi.fn() }));
vi.mock("#execution/tasks/parent/run-parent.js", () => ({
sendTaskCommand: vi.fn(async () => "delivered"),
@@ -148,7 +145,11 @@ describe("background subagent steering", () => {
expect(receipt).toEqual({ agentId: identity.id, status: "working", taskId: entry.task.taskId });
const session = await scope.commit();
expect(getAgentHandleStore(session.state)?.handles).toEqual([handle]);
- expect(getBackgroundWorkflowToolRuns(session.state)).toEqual([entry]);
+ expect(
+ getBackgroundTasks(session.state)
+ .query()
+ .map(({ run }) => run),
+ ).toEqual([entry]);
expect(startTaskRun).not.toHaveBeenCalled();
expect(waitForTaskCommandOwner).not.toHaveBeenCalled();
expect(sendTaskCommand).not.toHaveBeenCalled();
@@ -178,9 +179,10 @@ describe("background subagent steering", () => {
await scope.execute("new-call", "");
const committed = await scope.commit();
- const task = getBackgroundWorkflowToolRuns(committed.state).find(
- (candidate) => candidate.task.taskId !== entry.task.taskId,
- );
+ const task = getBackgroundTasks(committed.state)
+ .query()
+ .map(({ run }) => run)
+ .find((candidate) => candidate.task.taskId !== entry.task.taskId);
expect(task?.task.dispatchContext).toEqual({
auth: { current: creatorCurrent, initiator: creatorInitiator },
@@ -199,9 +201,10 @@ describe("background subagent steering", () => {
},
});
expect(
- getBackgroundWorkflowToolRuns(replayed.state).find(
- (candidate) => candidate.task.taskId === task.task.taskId,
- )?.task.dispatchContext,
+ getBackgroundTasks(replayed.state)
+ .query()
+ .map(({ run }) => run)
+ .find((candidate) => candidate.task.taskId === task.task.taskId)?.task.dispatchContext,
).toEqual({ auth: { current: creatorCurrent, initiator: creatorInitiator } });
});
@@ -220,9 +223,10 @@ describe("background subagent steering", () => {
await scope.execute("new-call", "");
const committed = await scope.commit();
- const task = getBackgroundWorkflowToolRuns(committed.state).find(
- (candidate) => candidate.task.taskId !== entry.task.taskId,
- );
+ const task = getBackgroundTasks(committed.state)
+ .query()
+ .map(({ run }) => run)
+ .find((candidate) => candidate.task.taskId !== entry.task.taskId);
expect(task?.task.dispatchContext).toEqual({
auth: { current: null, initiator: sessionInitiator },
@@ -245,9 +249,10 @@ describe("background subagent steering", () => {
await scope.execute("new-call", "", identity.name, kind);
const committed = await scope.commit();
- const task = getBackgroundWorkflowToolRuns(committed.state).find(
- (candidate) => candidate.task.taskId !== entry.task.taskId,
- );
+ const task = getBackgroundTasks(committed.state)
+ .query()
+ .map(({ run }) => run)
+ .find((candidate) => candidate.task.taskId !== entry.task.taskId);
expect(task).toBeDefined();
expect(task?.task.activityWorkIdentity).toMatchObject({
@@ -285,9 +290,10 @@ describe("background subagent steering", () => {
}),
);
const committed = await scope.commit();
- const task = getBackgroundWorkflowToolRuns(committed.state).find(
- (candidate) => candidate.task.taskId !== entry.task.taskId,
- );
+ const task = getBackgroundTasks(committed.state)
+ .query()
+ .map(({ run }) => run)
+ .find((candidate) => candidate.task.taskId !== entry.task.taskId);
expect(task).toMatchObject({
task: {
diff --git a/packages/eve/src/execution/tasks/parent/tool-execution.ts b/packages/eve/src/execution/tasks/parent/tool-execution.ts
index 349301c24..01cfed3ee 100644
--- a/packages/eve/src/execution/tasks/parent/tool-execution.ts
+++ b/packages/eve/src/execution/tasks/parent/tool-execution.ts
@@ -1,7 +1,6 @@
import {
type BackgroundWorkflowToolRun,
- findBackgroundWorkflowToolRun,
- getBackgroundWorkflowToolRuns,
+ getBackgroundTasks,
registerWorkflowToolRun,
} from "#harness/workflow-tool-runs.js";
import type { ContextContainer } from "#context/container.js";
@@ -189,9 +188,8 @@ class BackgroundToolExecutionScope implements BackgroundToolExecutor {
hasPendingTasks(): boolean {
return (
- getBackgroundWorkflowToolRuns(this.initialSession.state).some(
- (entry) => entry.task.outcome === undefined,
- ) || this.records.some((record) => record.settled && record.task !== undefined)
+ getBackgroundTasks(this.initialSession.state).query({ state: "working" }).length > 0 ||
+ this.records.some((record) => record.settled && record.task !== undefined)
);
}
@@ -426,12 +424,12 @@ class BackgroundToolExecutionScope implements BackgroundToolExecutor {
});
if (claim.kind === "busy" && claim.handle.phase === "claimed") {
const handle = claim.handle;
- const entry = findBackgroundWorkflowToolRun(this.agentHandleSession.state, handle.ownerId);
+ const owner = getBackgroundTasks(this.agentHandleSession.state).get(handle.ownerId);
if (
- entry?.task.metadata.kind === "subagent" &&
- entry.task.metadata.agentId === handle.identity.id &&
- entry.task.metadata.name === handle.identity.name &&
- entry.task.outcome === undefined
+ owner?.metadata.kind === "subagent" &&
+ owner.metadata.agentId === handle.identity.id &&
+ owner.metadata.name === handle.identity.name &&
+ owner.status === "working"
) {
await steerBackgroundAgent({
ctx: input.ctx,
@@ -442,7 +440,7 @@ class BackgroundToolExecutionScope implements BackgroundToolExecutor {
});
return {
kind: "steered",
- receipt: { agentId: handle.identity.id, taskId: entry.task.taskId, status: "working" },
+ receipt: { agentId: handle.identity.id, taskId: owner.taskId, status: "working" },
};
}
}
diff --git a/packages/eve/src/execution/tools/subagent/accept-event-step.test.ts b/packages/eve/src/execution/tools/subagent/accept-event-step.test.ts
index 6e72febc3..8e8af5007 100644
--- a/packages/eve/src/execution/tools/subagent/accept-event-step.test.ts
+++ b/packages/eve/src/execution/tools/subagent/accept-event-step.test.ts
@@ -153,7 +153,7 @@ describe("acceptTaskAuthorizationEventStep", () => {
metadata: { kind: "tool", name: "export" },
status: "completed",
taskId: "task-1",
- }),
+ }).state,
});
await expect(
diff --git a/packages/eve/src/execution/tools/subagent/accept-event-step.ts b/packages/eve/src/execution/tools/subagent/accept-event-step.ts
index af813a3e6..5bf94703f 100644
--- a/packages/eve/src/execution/tools/subagent/accept-event-step.ts
+++ b/packages/eve/src/execution/tools/subagent/accept-event-step.ts
@@ -1,9 +1,6 @@
import { type DurableSessionState, readDurableSession } from "#execution/durable-session-store.js";
import { getAgentHandleStore } from "#subagents/handles/store.js";
-import {
- readWorkflowTaskView,
- findBackgroundWorkflowToolRun,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import { type TaskAuthorizationEventDelivery } from "#tasks/types.js";
/** Accepts authorization events from the workflow task itself or an agent it owns. */
@@ -15,21 +12,20 @@ export async function acceptTaskAuthorizationEventStep(input: {
const { hookPayload, taskId } = input.delivery;
const durableSession = readDurableSession(input.sessionState);
- const entry = findBackgroundWorkflowToolRun(durableSession.state, taskId);
- if (entry === undefined) return false;
+ const task = getBackgroundTasks(durableSession.state).get(taskId);
+ if (task === undefined) return false;
// A workflow tool can request authorization without invoking a child agent.
// It has no agent handle, so bind its sender to the recorded task run, tool,
// and launching turn before accepting the event.
if (
- entry.task.metadata.kind === "tool" &&
- entry.address.runId === hookPayload.childSessionId &&
- entry.task.metadata.name === hookPayload.subagentName &&
- entry.origin.turnId === hookPayload.event.data.turnId
+ task.metadata.kind === "tool" &&
+ task.run.address.runId === hookPayload.childSessionId &&
+ task.metadata.name === hookPayload.subagentName &&
+ task.turnId === hookPayload.event.data.turnId
) {
- const view = readWorkflowTaskView(entry.task);
// Completion can arrive after the body has returned; its sign-in UI must still close.
- return hookPayload.event.type === "authorization.completed" || view === undefined;
+ return hookPayload.event.type === "authorization.completed" || task.status === "working";
}
const handles = getAgentHandleStore(durableSession.state)?.handles ?? [];
@@ -51,6 +47,5 @@ export async function acceptTaskAuthorizationEventStep(input: {
);
if (claimed === undefined && reserved.length !== 1) return false;
- const view = readWorkflowTaskView(entry.task);
- return view === undefined;
+ return task.status === "working";
}
diff --git a/packages/eve/src/execution/tools/subagent/invoke-preparation.ts b/packages/eve/src/execution/tools/subagent/invoke-preparation.ts
index 552a85743..17ae4175f 100644
--- a/packages/eve/src/execution/tools/subagent/invoke-preparation.ts
+++ b/packages/eve/src/execution/tools/subagent/invoke-preparation.ts
@@ -38,7 +38,7 @@ import {
import type { SubagentStartTarget } from "#execution/tools/subagent/start.js";
import type { SubagentInputSource } from "#subagents/tool.js";
import { createLogger } from "#internal/logging.js";
-import { findBackgroundWorkflowToolRun } from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
const log = createLogger("execution.agent-invocation");
@@ -67,7 +67,7 @@ export async function prepareOwnerAgentInvocation(input: {
const task =
input.taskId === undefined
? undefined
- : findBackgroundWorkflowToolRun(durableSession.state, input.taskId);
+ : getBackgroundTasks(durableSession.state).get(input.taskId)?.run;
const action = resolveAgentInvocationAction({
ctx,
input: input.invocation,
diff --git a/packages/eve/src/execution/tools/subagent/invoke-step.ts b/packages/eve/src/execution/tools/subagent/invoke-step.ts
index 97de3979b..12a1c36d9 100644
--- a/packages/eve/src/execution/tools/subagent/invoke-step.ts
+++ b/packages/eve/src/execution/tools/subagent/invoke-step.ts
@@ -42,11 +42,7 @@ import {
AGENT_UNREACHABLE,
formatAgentBusyMessage,
} from "#subagents/agent-handle-errors.js";
-import {
- readWorkflowTaskView,
- findBackgroundWorkflowToolRun,
- type TaskAgentDispatchContext,
-} from "#harness/workflow-tool-runs.js";
+import { getBackgroundTasks, type TaskAgentDispatchContext } from "#harness/workflow-tool-runs.js";
import type { RuntimeSubagentChildResult } from "#shared/action-types.js";
import {
clearProxyInputRequestsForChild,
@@ -354,15 +350,13 @@ export async function dispatchTaskAgentInvocationStep(
let taskDispatchContext: TaskAgentDispatchContext | undefined;
if (input.taskId !== undefined) {
const session = readDurableSession(input.sessionState);
- const entry = findBackgroundWorkflowToolRun(session.state, input.taskId);
- if (entry === undefined) return { kind: "not-admitted", sessionState: input.sessionState };
- const view = readWorkflowTaskView(entry.task);
- if (view !== undefined) {
+ const task = getBackgroundTasks(session.state).get(input.taskId);
+ if (task?.status !== "working") {
return { kind: "not-admitted", sessionState: input.sessionState };
}
- taskDispatchContext = entry.task.dispatchContext;
- if (entry.task.metadata.kind === "subagent") {
- activityWorkIdentity = entry.task.activityWorkIdentity;
+ taskDispatchContext = task.run.task.dispatchContext;
+ if (task.metadata.kind === "subagent") {
+ activityWorkIdentity = task.run.task.activityWorkIdentity;
}
}
const dispatched = await dispatchAgentInvocation({
@@ -474,13 +468,10 @@ export async function settleTaskAgentInvocationStep(input: {
}),
);
const task =
- input.taskId === undefined
- ? undefined
- : findBackgroundWorkflowToolRun(session.state, input.taskId);
+ input.taskId === undefined ? undefined : getBackgroundTasks(session.state).get(input.taskId);
// A generated subagent task completes when the parent records its task outcome.
// Nested agents inside authored background workflows settle independently of that task.
- const isTaskAgent =
- task?.task.metadata.kind === "subagent" && task.callId === input.result.callId;
+ const isTaskAgent = task?.metadata.kind === "subagent" && task.run.callId === input.result.callId;
const completion: SubagentCompletedStreamEvent | undefined =
input.result.outcome.result.kind !== "succeeded" || isTaskAgent
? undefined
diff --git a/packages/eve/src/execution/tools/subagent/task-cancel.test.ts b/packages/eve/src/execution/tools/subagent/task-cancel.test.ts
index 1376ef339..4acad47d9 100644
--- a/packages/eve/src/execution/tools/subagent/task-cancel.test.ts
+++ b/packages/eve/src/execution/tools/subagent/task-cancel.test.ts
@@ -70,8 +70,14 @@ it.each(["background", "waiting"])(
});
}
expect(requestWorkflowTurnCancellation).toHaveBeenCalledTimes(2);
- expect(requestWorkflowTurnCancellation).toHaveBeenCalledWith({ sessionId: "child-a" });
- expect(requestWorkflowTurnCancellation).toHaveBeenCalledWith({ sessionId: "child-b" });
+ expect(requestWorkflowTurnCancellation).toHaveBeenCalledWith({
+ sessionId: "child-a",
+ tasks: true,
+ });
+ expect(requestWorkflowTurnCancellation).toHaveBeenCalledWith({
+ sessionId: "child-b",
+ tasks: true,
+ });
expect(deserializeContext).not.toHaveBeenCalled();
},
);
@@ -139,5 +145,6 @@ it("cancels remote children at their recorded address", async () => {
expect(cancelRemoteAgentTurn).toHaveBeenCalledWith({
remote: expect.objectContaining({ url: "https://original.example" }),
sessionId: "remote",
+ tasks: true,
});
});
diff --git a/packages/eve/src/execution/tools/subagent/task-cancel.ts b/packages/eve/src/execution/tools/subagent/task-cancel.ts
index b1e3bdcc0..497778edd 100644
--- a/packages/eve/src/execution/tools/subagent/task-cancel.ts
+++ b/packages/eve/src/execution/tools/subagent/task-cancel.ts
@@ -11,7 +11,7 @@ import { createLogger, logError } from "#internal/logging.js";
const log = createLogger("execution.agent-invocation-cancel");
-/** Cancels all child turns still owned by a background task. */
+/** Cancels child turns and their nested tasks still owned by a background task. */
export const cancelBackgroundAgentTask: TaskExecutorCancel = async (input) => {
if (input.session === undefined || input.serializedContext === undefined) return;
await cancelAgentInvocationOwner({
@@ -54,7 +54,7 @@ async function cancelAgentInvocationOwner(input: {
const results = await Promise.allSettled(
handles.map(async (handle) => {
if (handle.address.kind !== "agent/remote") {
- await requestWorkflowTurnCancellation({ sessionId: handle.address.sessionId });
+ await requestWorkflowTurnCancellation({ sessionId: handle.address.sessionId, tasks: true });
return;
}
remoteContext ??= deserializeContext(input.serializedContext);
@@ -70,6 +70,7 @@ async function cancelAgentInvocationOwner(input: {
await cancelRemoteAgentTurn({
remote: { ...remote, url: handle.address.url },
sessionId: handle.address.sessionId,
+ tasks: true,
});
}),
);
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
index 096ea9741..68f33620e 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
@@ -5,6 +5,8 @@ import { handleConnectionCallbackRequest } from "#execution/connections/callback
import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
import { executeSleepTool, SLEEP_INPUT_SCHEMA } from "#execution/tools/sleep.js";
import { resumeSessionInbox } from "#execution/session-inbox/resume.js";
+import { cancelBackgroundAgentTask } from "#execution/tools/subagent/task-cancel.js";
+import { setAgentHandleStore } from "#subagents/handles/store.js";
import { workflowEntry } from "#execution/session/entry.js";
import { createTestRuntime, type TestRuntime } from "#internal/testing/app-harness.js";
import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
@@ -17,6 +19,7 @@ import {
failingDeployWorkflow,
holdUntilAbortedWorkflow,
reportingDeployWorkflow,
+ receiveDelegatedResultWorkflow,
stepThenRaceWorkflow,
stepReferenceWorkflow,
workflowContextMisuseWorkflow,
@@ -695,6 +698,182 @@ describe("workflow tools", () => {
});
}, 30_000);
+ it.each(["session-timeout", "reset"] as const)(
+ "fails the retained caller when a yielded child receives %s",
+ async (kind) => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: `yielded-child-${kind}`,
+ background: true,
+ execute: holdUntilAbortedWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const parentToken = `parent-result-${kind}`;
+ const parent = await start(receiveDelegatedResultWorkflow, [parentToken]);
+ let child: ReturnType<typeof getRun> | undefined;
+ let stream: ReturnType<typeof captureTurnEvents> | undefined;
+ let nestedRunId: string | undefined;
+ try {
+ await waitForHook(parent, { token: parentToken });
+ const before = await listWorkflowToolRunIds();
+ child = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: {
+ ...buildSerializedContext({
+ continuationToken: `child-${kind}`,
+ mode: "conversation",
+ }),
+ "eve.channel": {
+ kind: "subagent",
+ state: {
+ callId: "delegate",
+ subagentName: "detector",
+ parentContinuationToken: parentToken,
+ parentSessionId: parent.runId,
+ },
+ },
+ },
+ },
+ ]);
+ stream = captureTurnEvents(child);
+ const yielded = await stream.nextTurn();
+ expect(filterEventsByType(yielded, "turn.completed")).toHaveLength(1);
+ expect(yielded.at(-1)?.type).toBe("session.waiting");
+ nestedRunId = await waitForNewWorkflowToolRun(before);
+ await waitForHook({ runId: nestedRunId });
+ expect(await parent.status).toBe("running");
+ await resumeSessionInbox(
+ sessionCommandHookToken(child.runId),
+ kind === "session-timeout" ? { kind, ownerRunId: child.runId } : { kind },
+ );
+ await expect(parent.returnValue).resolves.toMatchObject({
+ kind: "runtime-action-result",
+ results: [
+ {
+ callId: "delegate",
+ isError: true,
+ outcome: {
+ kind: "terminal",
+ result: { kind: "failed" },
+ },
+ },
+ ],
+ });
+ await expect(child.returnValue).resolves.toEqual({
+ output: "The session ended before the delegated task completed.",
+ });
+ expect(await waitForWorkflowToolRunTerminal(nestedRunId)).toBe("completed");
+ } finally {
+ stream?.dispose();
+ if (nestedRunId !== undefined) {
+ const nested = getRun(nestedRunId);
+ const status = await nested.status;
+ if (status === "pending" || status === "running") await nested.cancel();
+ }
+ for (const run of [child, parent]) {
+ if (run === undefined) continue;
+ const status = await run.status;
+ if (status === "pending" || status === "running") await run.cancel();
+ }
+ }
+ });
+ },
+ 30_000,
+ );
+
+ it("cancels nested work after its owning child has yielded", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "yielded-child-cancel",
+ background: true,
+ execute: holdUntilAbortedWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const before = await listWorkflowToolRunIds();
+ const child = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: buildSerializedContext({
+ continuationToken: "http:yielded-child-cancel",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(child);
+ let nestedRunId: string | undefined;
+ try {
+ const yielded = await stream.nextTurn();
+ expect(filterEventsByType(yielded, "turn.completed")).toHaveLength(1);
+ expect(yielded.at(-1)?.type).toBe("session.waiting");
+ nestedRunId = await waitForNewWorkflowToolRun(before);
+ await waitForHook({ runId: nestedRunId });
+ expect(await getRun(nestedRunId).status).toBe("running");
+
+ await cancelBackgroundAgentTask({
+ entry: {
+ callId: "delegate",
+ toolName: "child",
+ lifetime: "session",
+ origin: { turnId: "parent-turn", stepIndex: 0 },
+ address: { runId: child.runId, hookToken: "unused" },
+ task: {
+ taskId: "outer-task",
+ metadata: { kind: "subagent", name: "child" },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ },
+ session: {
+ state: setAgentHandleStore(undefined, {
+ handles: [
+ {
+ phase: "claimed",
+ ownerId: "outer-task",
+ operationId: "delegate",
+ identity: { id: "child", name: "child", nodeId: "subagents/child" },
+ address: {
+ kind: "agent/local",
+ sessionId: child.runId,
+ continuationToken: "child",
+ },
+ },
+ ],
+ }),
+ },
+ serializedContext: {},
+ });
+
+ // The held step can finish only after its abort signal fires or its 60s timer expires.
+ // Finishing within 15s proves cancellation reached the yielded child's work.
+ expect(await waitForWorkflowToolRunTerminal(nestedRunId)).toBe("completed");
+
+ // This input follows the cancellation notification in the child's inbox.
+ // It must be the next turn, without a task notification waking the child first.
+ await resumeSessionInbox(sessionCommandHookToken(child.runId), {
+ kind: "send",
+ payload: { message: "Hello again" },
+ });
+ const next = await stream.nextTurn();
+ expect(
+ filterEventsByType(next, "message.received").map((event) => event.data.message),
+ ).toEqual(["Hello again"]);
+ expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);
+ } finally {
+ stream.dispose();
+ if (nestedRunId !== undefined) {
+ const nested = getRun(nestedRunId);
+ const status = await nested.status;
+ if (status === "pending" || status === "running") await nested.cancel();
+ }
+ await child.cancel();
+ }
+ });
+ }, 30_000);
+
it("cancels the run when the waiting turn is cancelled and lets the body clean up", async () => {
vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");
const runtime = await createWorkflowToolRuntime({
diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.ts
index cf00e0517..49944cb27 100644
--- a/packages/eve/src/harness/tool-loop.test.ts
+++ b/packages/eve/src/harness/tool-loop.test.ts
@@ -80,7 +80,7 @@ import {
appendPendingInputBatch,
} from "#harness/input-requests.js";
import { activeTurnId } from "#harness/active-turn-id.js";
-import { registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
+import { recordWorkflowTaskView, registerWorkflowToolRun } from "#harness/workflow-tool-runs.js";
import { getPendingCoordinationBatch } from "#harness/coordination.js";
import { AGENT_HANDLES_STATE_KEY } from "#subagents/handles/store.js";
import { BackgroundToolExecutorKey } from "#harness/background-tools.js";
@@ -858,8 +858,7 @@ function createGatewayModelCallError(input: {
describe("createToolLoopHarness", () => {
it("keeps a scheduled task session alive across individual results until all tasks settle", async () => {
const { SessionInputQueue } = await import("#execution/session/input-queue.js");
- const { getSessionTaskCohorts } = await import("#tasks/session-task-cohorts.js");
- const { recordWorkflowTaskView, findBackgroundWorkflowToolRun } =
+ const { recordWorkflowTaskView, getBackgroundTasks } =
await import("#harness/workflow-tool-runs.js");
const { resolveTaskDeliveryContext } = await import("#tasks/delivery-context.js");
const { backgroundToolExecutionProvider } =
@@ -879,12 +878,8 @@ describe("createToolLoopHarness", () => {
taskDeliveryId: "A:ready:completed",
payloads: [{ message: "A completed" }],
});
- expect(
- queue.takeNext(getSessionTaskCohorts(session.state), { taskDeliveryPolicy: "cohort" }),
- ).toBeUndefined();
- expect(
- queue.takeNext(getSessionTaskCohorts(session.state), { taskDeliveryPolicy: "auto" })?.kind,
- ).toBe("turn");
+ expect(queue.takeNext(session.state, { taskDeliveryPolicy: "cohort" })).toBeUndefined();
+ expect(queue.takeNext(session.state, { taskDeliveryPolicy: "auto" })?.kind).toBe("turn");
session = {
...session,
state: recordWorkflowTaskView(session.state, {
@@ -892,7 +887,7 @@ describe("createToolLoopHarness", () => {
metadata: { kind: "report-probe", name: "A" },
status: "completed",
lastOutput: { type: "result", data: "Report A" },
- }),
+ }).state,
};
const report = resolveTaskDeliveryContext({
state: session.state,
@@ -911,7 +906,7 @@ describe("createToolLoopHarness", () => {
const result = await contextStorage.run(ctx, () =>
runStep(session, { message: "A completed", context: [report.context] }),
);
- expect(findBackgroundWorkflowToolRun(result.session.state, "B")?.task.outcome).toBeUndefined();
+ expect(getBackgroundTasks(result.session.state).get("B")?.status).toBe("working");
expect(result.next).toBeNull();
session = {
@@ -921,7 +916,7 @@ describe("createToolLoopHarness", () => {
metadata: { kind: "report-probe", name: "B" },
status: "completed",
lastOutput: { type: "result", data: "Report B" },
- }),
+ }).state,
};
const finalReport = resolveTaskDeliveryContext({
state: session.state,
@@ -2941,6 +2936,46 @@ describe("createToolLoopHarness", () => {
expect(events.at(-1)?.type).toBe("session.waiting");
});
+ it("parks a task turn without completing the session while durable background work is pending", async () => {
+ setupMockAgent({
+ finishReason: "stop",
+ response: { messages: [{ content: "Still working.", role: "assistant" }] },
+ text: "Still working.",
+ toolCalls: [],
+ toolResults: [],
+ });
+ const { emit, events } = createEventCollector();
+ const runStep = createToolLoopHarness(createTestConfig("task", emit));
+
+ const result = await contextStorage.run(new ContextContainer(), () =>
+ runStep(recordBackgroundTask(createTestSession()), { message: "Continue" }),
+ );
+
+ expect(result.next).toBeNull();
+ expect(result.settledTurn).toEqual({ output: "Still working." });
+ expect(events.some((event) => event.type === "session.completed")).toBe(false);
+ expect(events.at(-1)?.type).toBe("session.waiting");
+
+ const completed = await contextStorage.run(new ContextContainer(), () =>
+ runStep(
+ {
+ ...result.session,
+ state: recordWorkflowTaskView(result.session.state, {
+ lastOutput: { data: "MCP-CHILD-ANSWER", type: "result" },
+ metadata: { kind: "report-probe", name: "analysis" },
+ status: "completed",
+ taskId: "analysis",
+ }),
+ },
+ { message: "Background work finished." },
+ ),
+ );
+
+ expect(completed.next).toEqual({ done: true, output: "Still working." });
+ expect(events.filter((event) => event.type === "session.completed")).toHaveLength(1);
+ expect(events.at(-1)?.type).toBe("session.completed");
+ });
+
it("fails a task turn as an error when structured output is not produced", async () => {
setupMockAgent({
finishReason: "stop",
@@ -3265,7 +3300,9 @@ describe("createToolLoopHarness", () => {
const result = await runStep(session, { message: "What's the weather in NY?" });
expect(result.next).toBeNull();
- expect(result.settledTurn).toEqual({ output: "It is 41 F in New York right now." });
+ expect(result.settledTurn).toEqual({
+ output: "It is 41 F in New York right now.",
+ });
expect(result.session.history).toEqual([
{ content: "What's the weather in NY?", kind: "user" as const, role: "user" },
{
@@ -4825,7 +4862,10 @@ describe("createToolLoopHarness", () => {
// session parks (`next: null`) so the user can follow up in the
// same thread rather than the whole run being torn down.
expect(result.next).toBeNull();
- expect(result.settledTurn).toEqual({ isError: true, output: "Model blew up" });
+ expect(result.settledTurn).toEqual({
+ isError: true,
+ output: "Model blew up",
+ });
expect(result.session.outputSchema).toBeUndefined();
const types = events.map((e) => e.type);
@@ -12963,7 +13003,10 @@ describe("boundary event failures", () => {
message: "Denied request",
});
expect(result.next).toBeNull();
- expect(result.settledTurn).toEqual({ isError: true, output: "admission denied" });
+ expect(result.settledTurn).toEqual({
+ isError: true,
+ output: "admission denied",
+ });
expect(result.session.outputSchema).toBeUndefined();
expect(ToolLoopAgent).not.toHaveBeenCalled();
expect(events.filter((event) => event.type === "turn.failed")).toMatchObject([
diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.ts
index 02ddd92c7..4dd8652cf 100644
--- a/packages/eve/src/harness/tool-loop.ts
+++ b/packages/eve/src/harness/tool-loop.ts
@@ -86,6 +86,7 @@ import {
shouldCompact,
} from "#harness/compaction.js";
import { createCurrentMessages } from "#harness/current-messages.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import { estimateTokens } from "#harness/token-estimate.js";
import {
accumulateTurnUsage,
@@ -2895,8 +2896,8 @@ async function handleStepResult(input: {
if (
config.mode === "task" &&
- contextStorage.getStore()?.get(ScheduleIdKey) !== undefined &&
- contextStorage.getStore()?.get(BackgroundToolExecutorKey)?.hasPendingTasks?.() === true
+ (getBackgroundTasks(nextSession.state).query({ state: "working" }).length > 0 ||
+ contextStorage.getStore()?.get(BackgroundToolExecutorKey)?.hasPendingTasks?.() === true)
) {
return deferTaskTurn({
emissionState,
@@ -2932,7 +2933,7 @@ async function handleStepResult(input: {
});
}
-/** Keeps a scheduled task session open until its background work settles. */
+/** Keeps a task session open until its background work settles. */
async function deferTaskTurn(input: {
readonly emissionState: ReturnType<typeof getHarnessEmissionState>;
readonly emit?: ToolLoopHarnessConfig["handleEvent"];
diff --git a/packages/eve/src/harness/turn-tag-state.ts b/packages/eve/src/harness/turn-tag-state.ts
index 18f8a75eb..3545ac916 100644
--- a/packages/eve/src/harness/turn-tag-state.ts
+++ b/packages/eve/src/harness/turn-tag-state.ts
@@ -178,9 +178,11 @@ function configuredSessionUsageLimits(
* measures everything since the last report, so the deltas of a
* multi-turn persistent child sum exactly to its session totals.
*/
-export function takeSessionUsageDelta(session: HarnessSession): {
+export function takeSessionUsageDelta<T extends Pick<HarnessSession, "state">>(
+ session: T,
+): {
readonly delta: TokenUsage;
- readonly session: HarnessSession;
+ readonly session: T;
} {
const totals = getSessionTokenUsage(session);
const reported =
diff --git a/packages/eve/src/harness/workflow-tool-run-registry.test.ts b/packages/eve/src/harness/workflow-tool-run-registry.test.ts
index ed9b0767e..c31da9711 100644
--- a/packages/eve/src/harness/workflow-tool-run-registry.test.ts
+++ b/packages/eve/src/harness/workflow-tool-run-registry.test.ts
@@ -2,6 +2,7 @@ import { runInNewContext } from "node:vm";
import { describe, expect, it } from "vitest";
import type { SessionStateMap } from "#harness/types.js";
import {
+ getBackgroundTasks,
readWorkflowTaskView,
recordWorkflowTaskView,
findBlockingWorkflowToolRun,
@@ -11,7 +12,6 @@ import {
type BackgroundWorkflowToolRun,
type BlockingWorkflowToolRun,
} from "./workflow-tool-runs.js";
-import { getSessionTaskCohorts } from "#tasks/session-task-cohorts.js";
import { resolveTaskDeliveryContext } from "#tasks/delivery-context.js";
const waiting = (turnId: string): BlockingWorkflowToolRun => ({
@@ -56,7 +56,7 @@ describe("shared workflow invocation ownership", () => {
metadata: task("task-a").task.metadata,
status: "completed",
lastOutput: { type: "result", data: "first" },
- }),
+ }).state,
};
session = removeBlockingWorkflowToolRuns(session, "turn-a");
const entries = getWorkflowToolRuns(session.state);
@@ -83,7 +83,7 @@ describe("shared workflow invocation ownership", () => {
metadata: task("task-b").task.metadata,
status: "failed",
lastOutput: { type: "error", data: "second" },
- }),
+ }).state,
};
const restored = JSON.parse(
JSON.stringify(removeBlockingWorkflowToolRuns(session, "turn-b").state),
@@ -97,7 +97,11 @@ describe("shared workflow invocation ownership", () => {
expect(report?.phase).toBe("settled");
expect(report?.context).toContain("first");
expect(report?.context).toContain("second");
- expect([...getSessionTaskCohorts(restored).values()]).toEqual(["task-a", "task-a"]);
+ expect(
+ getBackgroundTasks(restored)
+ .query()
+ .map((task) => task.cohortId),
+ ).toEqual(["task-a", "task-a"]);
expect(
JSON.stringify(removeBlockingWorkflowToolRuns({ state: restored }, "turn-a").state),
).toBe(beforeReport);
@@ -117,7 +121,7 @@ describe("shared workflow invocation ownership", () => {
taskId: "live",
metadata: old.task.metadata,
status: "cancelled",
- }),
+ }).state,
};
session = removeBlockingWorkflowToolRuns(session, "turn-b");
const entries = getWorkflowToolRuns(session.state);
diff --git a/packages/eve/src/harness/workflow-tool-run-tasks.test.ts b/packages/eve/src/harness/workflow-tool-run-tasks.test.ts
index 1f346b45c..bccf05e8d 100644
--- a/packages/eve/src/harness/workflow-tool-run-tasks.test.ts
+++ b/packages/eve/src/harness/workflow-tool-run-tasks.test.ts
@@ -1,15 +1,21 @@
import { assert, describe, expect, it } from "vitest";
import type { HarnessSession } from "#harness/types.js";
import {
- readWorkflowTaskView,
+ getBackgroundTasks,
recordWorkflowTaskView,
- findBackgroundWorkflowToolRun,
- getBackgroundWorkflowToolRuns,
registerWorkflowToolRun,
} from "#harness/workflow-tool-runs.js";
-import { getTaskCohortId, getSessionTaskCohorts } from "#tasks/session-task-cohorts.js";
import { deriveTaskId } from "#tasks/task-id.js";
import type { TaskView } from "#tasks/types.js";
+import type { BackgroundTask } from "#harness/workflow-tool-runs.js";
+
+function views(tasks: readonly BackgroundTask[]): TaskView[] {
+ return tasks.map(({ cohortId: _cohortId, turnId: _turnId, run: _run, ...view }) => view);
+}
+
+function ids(tasks: readonly BackgroundTask[]): string[] {
+ return tasks.map((task) => task.taskId);
+}
function createSession(state?: HarnessSession["state"]): HarnessSession {
return {
@@ -33,8 +39,108 @@ describe("session task index", () => {
};
const dispatchContext = { auth: { current: null, initiator: null } } as const;
it("returns an empty index when the key is absent", () => {
- expect(getBackgroundWorkflowToolRuns({})).toEqual([]);
- expect(getBackgroundWorkflowToolRuns(undefined)).toEqual([]);
+ expect(getBackgroundTasks({}).query()).toEqual([]);
+ expect(getBackgroundTasks(undefined).query()).toEqual([]);
+ });
+
+ it("queries restored background tasks by state across turns, excluding blocking runs", () => {
+ let session = registerWorkflowToolRun(createSession(), task("working", "turn-1"));
+ for (const status of ["completed", "failed", "cancelled"] as const) {
+ session = registerWorkflowToolRun(session, task(status, "turn-2"));
+ session = {
+ ...session,
+ state: recordWorkflowTaskView(session.state, terminal(status, status)).state,
+ };
+ }
+ session = registerWorkflowToolRun(session, {
+ callId: "blocking",
+ toolName: "research",
+ lifetime: "turn",
+ origin: { turnId: "turn-2", stepIndex: 0 },
+ address: { runId: "blocking", hookToken: "blocking" },
+ });
+
+ const backgroundTasks = getBackgroundTasks(JSON.parse(JSON.stringify(session.state)));
+ expect(views(backgroundTasks.query({ state: "working" }))).toEqual([
+ { taskId: "working", metadata, status: "working" },
+ ]);
+ expect(views(backgroundTasks.query({ state: "completed" }))).toEqual([
+ terminal("completed", "completed"),
+ ]);
+ expect(views(backgroundTasks.query({ state: "failed" }))).toEqual([
+ terminal("failed", "failed"),
+ ]);
+ expect(views(backgroundTasks.query({ state: "cancelled" }))).toEqual([
+ terminal("cancelled", "cancelled"),
+ ]);
+ expect(getBackgroundTasks(undefined).query({ state: "working" })).toEqual([]);
+ });
+
+ it("distinguishes a first terminal settlement from repeated and late reports", () => {
+ const session = registerWorkflowToolRun(createSession(), task("task_a", "turn-1"));
+ const completed = terminal("task_a", "completed");
+ const first = recordWorkflowTaskView(session.state, completed);
+ expect(first.firstOutcome).toBe(true);
+ expect(first.view).toEqual(completed);
+
+ const repeated = recordWorkflowTaskView(first.state, completed);
+ expect(repeated.state).toBe(first.state);
+ expect(repeated.firstOutcome).toBe(false);
+ expect(repeated.view).toEqual(completed);
+
+ const late = recordWorkflowTaskView(repeated.state, terminal("task_a", "cancelled"));
+ expect(late.state).toBe(repeated.state);
+ expect(late.firstOutcome).toBe(false);
+ expect(late.view).toEqual(completed);
+ });
+
+ it("retains cancellation across restore and late outcomes without removing cohort membership", () => {
+ const session = registerWorkflowToolRun(createSession(), task("task_a", "turn-1"));
+ const state = recordWorkflowTaskView(session.state, terminal("task_a", "cancelled")).state;
+ const replay = registerWorkflowToolRun(
+ createSession(JSON.parse(JSON.stringify(state))),
+ task("task_a", "turn-1"),
+ );
+ const late = recordWorkflowTaskView(replay.state, terminal("task_a", "completed")).state;
+ expect(views(getBackgroundTasks(late).query({ state: "cancelled" }))).toEqual([
+ terminal("task_a", "cancelled"),
+ ]);
+ expect(getBackgroundTasks(late).get("task_a")?.cohortId).toBe("task_a");
+ });
+
+ it("decodes only the tasks a read returns", () => {
+ const retained = task("task_old", "turn-1");
+ const tasks = getBackgroundTasks({
+ "eve.workflowTool": {
+ version: 3,
+ runs: [
+ { ...retained, task: { ...retained.task, outcome: { status: "completed" } } },
+ task("task_live", "turn-2"),
+ ],
+ },
+ });
+ // A corrupt retained outcome must not block reads of unrelated work.
+ expect(ids(tasks.query({ state: "working" }))).toEqual(["task_live"]);
+ expect(tasks.get("task_live")?.status).toBe("working");
+ expect(() => tasks.get("task_old")).toThrow("Corrupt workflow task result");
+ expect(() => tasks.query({ state: "completed" })).toThrow("Corrupt workflow task result");
+ expect(() => tasks.query()).toThrow("Corrupt workflow task result");
+ });
+
+ it("filters by starting turn, cohort, and a set of states", () => {
+ let session = registerWorkflowToolRun(createSession(), task("task_a", "turn-1"));
+ // Work started while task_a is open joins its cohort.
+ session = registerWorkflowToolRun(session, task("task_b", "turn-2"));
+ session = {
+ ...session,
+ state: recordWorkflowTaskView(session.state, terminal("task_a", "cancelled")).state,
+ };
+ const tasks = getBackgroundTasks(session.state);
+ expect(ids(tasks.query({ turnId: "turn-2" }))).toEqual(["task_b"]);
+ expect(ids(tasks.query({ cohortId: "task_a" }))).toEqual(["task_a", "task_b"]);
+ expect(ids(tasks.query({ state: ["working", "cancelled"] }))).toEqual(["task_a", "task_b"]);
+ expect(ids(tasks.query({ state: "working", turnId: "turn-1" }))).toEqual([]);
+ expect(tasks.get("task_b")).toMatchObject({ cohortId: "task_a", turnId: "turn-2" });
});
it("records a task and finds it by id", () => {
@@ -47,7 +153,7 @@ describe("session task index", () => {
task: { dispatchContext, metadata, taskId: "task_a" },
});
- expect(findBackgroundWorkflowToolRun(session.state, "task_a")).toEqual({
+ expect(getBackgroundTasks(session.state).get("task_a")?.run).toEqual({
callId: "task_a",
toolName: metadata.name,
lifetime: "session" as const,
@@ -55,7 +161,7 @@ describe("session task index", () => {
address: { runId: "run-1", hookToken: "task:token-1" },
task: { dispatchContext, metadata, taskId: "task_a" },
});
- expect(findBackgroundWorkflowToolRun(session.state, "task_other")).toBeUndefined();
+ expect(getBackgroundTasks(session.state).get("task_other")?.run).toBeUndefined();
});
it("keeps activity identity in the persisted task index", () => {
@@ -78,9 +184,9 @@ describe("session task index", () => {
});
const restoredState = JSON.parse(JSON.stringify(session.state));
- expect(
- findBackgroundWorkflowToolRun(restoredState, "task_a")?.task.activityWorkIdentity,
- ).toEqual(activityWorkIdentity);
+ expect(getBackgroundTasks(restoredState).get("task_a")?.run.task.activityWorkIdentity).toEqual(
+ activityWorkIdentity,
+ );
});
it("keeps subagent metadata in the persisted task index", () => {
@@ -100,7 +206,7 @@ describe("session task index", () => {
task: { dispatchContext, metadata: subagentMetadata, taskId: "task_a" },
});
- expect(findBackgroundWorkflowToolRun(session.state, "task_a")?.task.metadata).toEqual(
+ expect(getBackgroundTasks(session.state).get("task_a")?.run.task.metadata).toEqual(
subagentMetadata,
);
});
@@ -130,7 +236,7 @@ describe("session task index", () => {
});
session = {
...session,
- state: recordWorkflowTaskView(session.state, terminal("task_a", "completed")),
+ state: recordWorkflowTaskView(session.state, terminal("task_a", "completed")).state,
};
session = registerWorkflowToolRun(session, {
callId: "task_a",
@@ -155,7 +261,7 @@ describe("session task index", () => {
},
});
- expect(findBackgroundWorkflowToolRun(session.state, "task_a")).toMatchObject({
+ expect(getBackgroundTasks(session.state).get("task_a")?.run).toMatchObject({
task: {
activityWorkIdentity: { label: "Second label" },
outcome: { status: "completed", lastOutput: { type: "result", data: "done" } },
@@ -181,7 +287,9 @@ describe("session task index", () => {
task: { dispatchContext, metadata, taskId: "task_a" },
});
- const entries = getBackgroundWorkflowToolRuns(session.state);
+ const entries = getBackgroundTasks(session.state)
+ .query()
+ .map((task) => task.run);
expect(entries).toHaveLength(1);
expect(entries[0]?.address.runId).toBe("run-2");
});
@@ -209,14 +317,15 @@ describe("session task index", () => {
const initial = registerWorkflowToolRun(createSession(), first);
const second = task("task_b", "turn-2");
const session = registerWorkflowToolRun(initial, second);
- const entries = getBackgroundWorkflowToolRuns(session.state);
- expect(entries.map((entry) => getTaskCohortId(entry.task))).toEqual(["task_a", "task_a"]);
- expect(entries.map((entry) => entry.origin.turnId)).toEqual(["turn-1", "turn-2"]);
- expect(entries[0]?.task.cohortId).toBeUndefined();
- expect(entries[1]?.task.cohortId).toBe("task_a");
+ const tasks = getBackgroundTasks(session.state).query();
+ expect(tasks.map((task) => task.cohortId)).toEqual(["task_a", "task_a"]);
+ expect(tasks.map((task) => task.turnId)).toEqual(["turn-1", "turn-2"]);
+ // A cohort's first task has no join target; later members store it.
+ expect(tasks[0]?.run.task.cohortId).toBeUndefined();
+ expect(tasks[1]?.run.task.cohortId).toBe("task_a");
const restored = createSession(JSON.parse(JSON.stringify(initial.state)));
expect(registerWorkflowToolRun(restored, second).state).toEqual(session.state);
- expect(getBackgroundWorkflowToolRuns(initial.state)).toHaveLength(1);
+ expect(getBackgroundTasks(initial.state).query()).toHaveLength(1);
});
it.each(["completed", "failed", "cancelled"] as const)(
@@ -226,27 +335,26 @@ describe("session task index", () => {
session = registerWorkflowToolRun(session, task("task_b", "turn-1"));
session = {
...session,
- state: recordWorkflowTaskView(session.state, terminal("task_a", status)),
+ state: recordWorkflowTaskView(session.state, terminal("task_a", status)).state,
};
session = registerWorkflowToolRun(session, task("task_c", "turn-2"));
expect(
- getBackgroundWorkflowToolRuns(session.state).map((entry) => getTaskCohortId(entry.task)),
+ getBackgroundTasks(session.state)
+ .query()
+ .map((task) => task.cohortId),
).toEqual(["task_a", "task_a", "task_a"]);
- expect([...getSessionTaskCohorts(session.state).values()]).toEqual([
- "task_a",
- "task_a",
- "task_a",
- ]);
for (const taskId of ["task_b", "task_c"]) {
session = {
...session,
- state: recordWorkflowTaskView(session.state, terminal(taskId, status)),
+ state: recordWorkflowTaskView(session.state, terminal(taskId, status)).state,
};
}
// Even another creation in the same turn must not reopen a settled cohort.
session = registerWorkflowToolRun(session, task("task_d", "turn-2"));
expect(
- getBackgroundWorkflowToolRuns(session.state).map((entry) => getTaskCohortId(entry.task)),
+ getBackgroundTasks(session.state)
+ .query()
+ .map((task) => task.cohortId),
).toEqual(["task_a", "task_a", "task_a", "task_d"]);
},
);
@@ -257,7 +365,7 @@ describe("session task index", () => {
for (const taskId of ["task_a", "task_b"]) {
session = {
...session,
- state: recordWorkflowTaskView(session.state, terminal(taskId, "completed")),
+ state: recordWorkflowTaskView(session.state, terminal(taskId, "completed")).state,
};
}
session = registerWorkflowToolRun(session, task("task_c", "turn-3"));
@@ -271,28 +379,28 @@ describe("session task index", () => {
origin: { ...task("task_b", "turn-2").origin, stepIndex: 9 },
});
expect(
- getBackgroundWorkflowToolRuns(session.state).map((entry) => ({
- taskId: entry.task.taskId,
- cohortId: getTaskCohortId(entry.task),
- turnId: entry.origin.turnId,
- stepIndex: entry.origin.stepIndex,
- settled: entry.task.outcome !== undefined,
- })),
+ getBackgroundTasks(session.state)
+ .query()
+ .map((task) => ({
+ taskId: task.taskId,
+ cohortId: task.cohortId,
+ turnId: task.turnId,
+ stepIndex: task.run.origin.stepIndex,
+ settled: task.status !== "working",
+ })),
).toEqual([
{ taskId: "task_a", cohortId: "task_a", turnId: "turn-1", stepIndex: 0, settled: true },
{ taskId: "task_b", cohortId: "task_a", turnId: "turn-2", stepIndex: 0, settled: true },
{ taskId: "task_c", cohortId: "task_c", turnId: "turn-3", stepIndex: 0, settled: false },
]);
- expect(findBackgroundWorkflowToolRun(session.state, "task_a")?.address.runId).toBe(
- "run-replayed",
- );
+ expect(getBackgroundTasks(session.state).get("task_a")?.run.address.runId).toBe("run-replayed");
session = registerWorkflowToolRun(session, task("task_d", "turn-4"));
- expect(findBackgroundWorkflowToolRun(session.state, "task_d")?.task.cohortId).toBe("task_c");
+ expect(getBackgroundTasks(session.state).get("task_d")?.run.task.cohortId).toBe("task_c");
});
it.each(["", null, 42])("rejects an invalid additive cohort identity: %j", (cohortId) => {
expect(() =>
- getBackgroundWorkflowToolRuns({
+ getBackgroundTasks({
"eve.workflowTool": {
version: 3,
runs: [
@@ -324,7 +432,7 @@ describe("session task index", () => {
...base,
task: { ...base.task, outcome },
});
- expect(findBackgroundWorkflowToolRun(session.state, "task_a")?.task.outcome).toEqual(outcome);
+ expect(getBackgroundTasks(session.state).get("task_a")?.run.task.outcome).toEqual(outcome);
for (const invalidOutcome of [
{ status: "working" },
{ status: "completed" },
@@ -342,15 +450,15 @@ describe("session task index", () => {
status: "completed",
},
]) {
- const [entry] = getBackgroundWorkflowToolRuns({
+ const tasks = getBackgroundTasks({
"eve.workflowTool": {
version: 3,
runs: [{ ...base, task: { ...base.task, outcome: invalidOutcome } }],
},
});
- expect(entry?.address).toEqual(base.address);
- assert(entry !== undefined);
- expect(() => readWorkflowTaskView(entry.task)).toThrow("Corrupt workflow task result");
+ // The registry keeps the entry; only reading its view rejects the outcome.
+ expect(tasks.query({ state: "working" })).toEqual([]);
+ expect(() => tasks.get(base.task.taskId)).toThrow("Corrupt workflow task result");
expect(() =>
registerWorkflowToolRun(createSession(), {
...base,
@@ -366,34 +474,35 @@ describe("session task index", () => {
const session = registerWorkflowToolRun(createSession(), task("task_a", "turn-1"));
const usage = { inputTokens: 3, outputTokens: 5, cacheReadTokens: 0, cacheWriteTokens: 0 };
const first = { ...terminal("task_a", status), usage };
- const state = recordWorkflowTaskView(session.state, first);
+ const state = recordWorkflowTaskView(session.state, first).state;
for (const late of ["completed", "failed", "cancelled"] as const) {
- expect(recordWorkflowTaskView(state, terminal("task_a", late))).toBe(state);
+ expect(recordWorkflowTaskView(state, terminal("task_a", late)).state).toBe(state);
}
- const entry = findBackgroundWorkflowToolRun(state, "task_a");
- assert(entry !== undefined);
- expect(entry.task.outcome).toEqual(
+ const recorded = getBackgroundTasks(state).get("task_a");
+ assert(recorded !== undefined);
+ expect(recorded.run.task.outcome).toEqual(
status === "cancelled"
? { status, usage }
: { status, usage, lastOutput: first.lastOutput },
);
- expect(readWorkflowTaskView(entry.task)).toEqual(first);
+ expect(views([recorded])).toEqual([first]);
},
);
it("rejects an incoming result with metadata belonging to a different task", () => {
const session = registerWorkflowToolRun(createSession(), task("task_a", "turn-1"));
- expect(() =>
- recordWorkflowTaskView(session.state, {
- ...terminal("task_a", "cancelled"),
- metadata: { kind: "tool", name: "other" },
- }),
+ expect(
+ () =>
+ recordWorkflowTaskView(session.state, {
+ ...terminal("task_a", "cancelled"),
+ metadata: { kind: "tool", name: "other" },
+ }).state,
).toThrow("Task view metadata does not match");
});
it("throws on a corrupt index instead of treating it as absent", () => {
expect(() =>
- getBackgroundWorkflowToolRuns({
+ getBackgroundTasks({
"eve.workflowTool": { version: 3, runs: [{ taskId: 42 }] },
}),
).toThrow("Corrupt workflow tool run registry");
@@ -402,7 +511,7 @@ describe("session task index", () => {
it("rejects missing creator context", () => {
const entry = task("task_a", "turn-1");
expect(() =>
- getBackgroundWorkflowToolRuns({
+ getBackgroundTasks({
"eve.workflowTool": {
version: 3,
runs: [{ ...entry, task: { ...entry.task, dispatchContext: undefined } }],
@@ -420,7 +529,7 @@ describe("session task index", () => {
it("rejects unrecognized task dispatch context fields", () => {
expect(() =>
- getBackgroundWorkflowToolRuns({
+ getBackgroundTasks({
"eve.workflowTool": {
version: 3,
runs: [
@@ -447,7 +556,7 @@ describe("session task index", () => {
it("rejects an unsupported registry version", () => {
expect(() =>
- getBackgroundWorkflowToolRuns({
+ getBackgroundTasks({
"eve.workflowTool": { version: 99, runs: [] },
}),
).toThrow("Corrupt workflow tool run registry");
diff --git a/packages/eve/src/harness/workflow-tool-runs.ts b/packages/eve/src/harness/workflow-tool-runs.ts
index 8e883aec8..3dc9f8e6d 100644
--- a/packages/eve/src/harness/workflow-tool-runs.ts
+++ b/packages/eve/src/harness/workflow-tool-runs.ts
@@ -7,6 +7,7 @@ import {
sameTaskMetadata,
type TaskMetadata,
type TaskOutput,
+ type TaskStatus,
type TaskUsage,
type TaskView,
} from "#tasks/types.js";
@@ -275,12 +276,69 @@ export function getWorkflowToolRuns(
return readRegistry(state).runs;
}
-export function getBackgroundWorkflowToolRuns(
- state: SessionStateMap | undefined,
-): readonly BackgroundWorkflowToolRun[] {
- return getWorkflowToolRuns(state).filter(
- (entry): entry is BackgroundWorkflowToolRun => entry.lifetime === "session",
+/** Parent-observed lifecycle state. Input requests are tracked on the parent session instead. */
+export type BackgroundTaskState = Exclude<TaskStatus, "input_required">;
+
+/** A background run with its decoded lifecycle view. */
+export type BackgroundTask = TaskView & {
+ /** Overlapping work reported together; a task without a join target starts its own cohort. */
+ readonly cohortId: string;
+ /** The parent turn that started the task. */
+ readonly turnId: string;
+ readonly run: BackgroundWorkflowToolRun;
+};
+
+export interface BackgroundTaskFilter {
+ readonly state?: BackgroundTaskState | readonly BackgroundTaskState[];
+ readonly cohortId?: string;
+ readonly turnId?: string;
+}
+
+export interface BackgroundTasks {
+ get(taskId: string): BackgroundTask | undefined;
+ query(filter?: BackgroundTaskFilter): readonly BackgroundTask[];
+}
+
+/**
+ * Read-only parent view: a task without a recorded terminal outcome is working.
+ * Only returned tasks are decoded, so one corrupt retained outcome cannot block
+ * reads of unrelated tasks, and working-only reads never decode.
+ */
+export function getBackgroundTasks(state: SessionStateMap | undefined): BackgroundTasks {
+ const runs = getWorkflowToolRuns(state).filter(
+ (run): run is BackgroundWorkflowToolRun => run.lifetime === "session",
);
+ return {
+ get(taskId) {
+ const run = runs.find((candidate) => candidate.task.taskId === taskId);
+ return run === undefined ? undefined : toBackgroundTask(run);
+ },
+ query(filter = {}) {
+ const states: readonly TaskStatus[] | undefined =
+ typeof filter.state === "string" ? [filter.state] : filter.state;
+ const admitsOutcomes = states === undefined || states.some((status) => status !== "working");
+ return runs.flatMap((run) => {
+ if (filter.cohortId !== undefined && cohortIdOf(run) !== filter.cohortId) return [];
+ if (filter.turnId !== undefined && run.origin.turnId !== filter.turnId) return [];
+ if (run.task.outcome !== undefined && !admitsOutcomes) return [];
+ const task = toBackgroundTask(run);
+ return states === undefined || states.includes(task.status) ? [task] : [];
+ });
+ },
+ };
+}
+
+function cohortIdOf(run: BackgroundWorkflowToolRun): string {
+ return run.task.cohortId ?? run.task.taskId;
+}
+
+function toBackgroundTask(run: BackgroundWorkflowToolRun): BackgroundTask {
+ const view = readWorkflowTaskView(run.task) ?? {
+ taskId: run.task.taskId,
+ metadata: run.task.metadata,
+ status: "working" as const,
+ };
+ return { ...view, cohortId: cohortIdOf(run), turnId: run.origin.turnId, run };
}
export function getBlockingWorkflowToolRuns(
@@ -293,13 +351,6 @@ export function getBlockingWorkflowToolRuns(
);
}
-export function findBackgroundWorkflowToolRun(
- state: SessionStateMap | undefined,
- taskId: string,
-): BackgroundWorkflowToolRun | undefined {
- return getBackgroundWorkflowToolRuns(state).find((entry) => entry.task.taskId === taskId);
-}
-
function writeRegistry(
state: SessionStateMap | undefined,
registry: WorkflowToolRunRegistry,
@@ -379,11 +430,16 @@ export function registerWorkflowToolRun<T extends { readonly state?: SessionStat
return { ...session, state: writeRegistry(session.state, { ...registry, runs }) };
}
-/** Task payloads remain available for the session lifetime, including after report delivery. */
+/** Records the first terminal outcome and distinguishes it from repeated or late reports. */
export function recordWorkflowTaskView(
state: SessionStateMap | undefined,
view: TaskView,
-): SessionStateMap | undefined {
+): {
+ readonly state: SessionStateMap | undefined;
+ readonly view: TaskView;
+ /** True only when this call recorded the task's first outcome. */
+ readonly firstOutcome: boolean;
+} {
const { taskId, metadata, ...result } = view;
const outcome = parseTaskOutcome(result);
if (!isNonEmptyString(taskId) || !isTaskMetadata(metadata) || outcome === undefined)
@@ -394,12 +450,15 @@ export function recordWorkflowTaskView(
(entry) => entry.lifetime === "session" && entry.task.taskId === taskId,
);
const entry = runs[index];
- if (entry === undefined || entry.lifetime !== "session") return state;
+ if (entry === undefined || entry.lifetime !== "session")
+ return { state, view, firstOutcome: false };
if (!sameTaskMetadata(entry.task.metadata, metadata))
throw new Error(`Task view metadata does not match invocation "${view.taskId}".`);
const previous = readWorkflowTaskView(entry.task);
// Parent delivery order decides settlement. Replays and late outcomes cannot replace it.
- if (previous !== undefined) return state;
+ if (previous !== undefined) {
+ return { state, view: previous, firstOutcome: false };
+ }
runs[index] = {
...entry,
task: {
@@ -407,7 +466,11 @@ export function recordWorkflowTaskView(
outcome,
},
};
- return writeRegistry(state, { ...registry, runs });
+ return {
+ state: writeRegistry(state, { ...registry, runs }),
+ view,
+ firstOutcome: true,
+ };
}
/** Removes only this turn's waiting calls. Session-owned task payloads are never pruned here. */
diff --git a/packages/eve/src/internal/invocation/mcp-agent-channel.scenario.test.ts b/packages/eve/src/internal/invocation/mcp-agent-channel.scenario.test.ts
index 485b82ec6..f73fc388c 100644
--- a/packages/eve/src/internal/invocation/mcp-agent-channel.scenario.test.ts
+++ b/packages/eve/src/internal/invocation/mcp-agent-channel.scenario.test.ts
@@ -63,11 +63,99 @@ export default mcpChannel({
export default askQuestion();
`,
},
+ dependencies: { "just-bash": "3.4.2" },
installDependencies: true,
name: "mcp-agent-channel",
};
+const MCP_BACKGROUND_SUBAGENT_DESCRIPTOR: ScenarioAppDescriptor = {
+ files: {
+ "agent/agent.ts": `import { defineAgent } from "eve";
+import { mockModel } from "eve/evals";
+
+const model = mockModel((request) => {
+ const messages = JSON.stringify(request.messages);
+ if (messages.includes("MCP-CHILD-ANSWER")) {
+ return "MCP-CHILD-ANSWER";
+ }
+ if (request.toolResults.some((result) => result.name === "researcher")) {
+ return "MCP-FIRST-TURN-STATUS";
+ }
+ return {
+ toolCalls: [{
+ id: "delegate-1",
+ input: { message: "Return the durable child answer." },
+ name: "researcher",
+ }],
+ };
+});
+
+export default defineAgent({
+ description: "Delegates every MCP request to the researcher subagent.",
+ model,
+ modelContextWindowTokens: 32_000,
+});
+`,
+ "agent/channels/mcp.ts": `import { mcpChannel } from "eve/channels/mcp";
+
+export default mcpChannel({
+ auth: (request) => {
+ const principalId = request.headers.get("x-test-principal");
+ return principalId === null
+ ? null
+ : { attributes: {}, authenticator: "scenario", principalId, principalType: "user" };
+ },
+});
+`,
+ "agent/instructions.md": "Delegate the request to the declared researcher subagent.\n",
+ "agent/subagents/researcher/agent.ts": `import { defineAgent } from "eve";
+import { mockModel } from "eve/evals";
+
+export default defineAgent({
+ description: "Returns the durable child answer.",
+ model: mockModel(() => "MCP-CHILD-ANSWER"),
+ modelContextWindowTokens: 32_000,
+});
+`,
+ "agent/subagents/researcher/instructions.md": "Reply with the requested answer.\n",
+ },
+ dependencies: { "just-bash": "3.4.2" },
+ installDependencies: true,
+ name: "mcp-background-subagent",
+};
+
describe("MCP agent channel", () => {
+ it(
+ "returns a background subagent result from a durable MCP invocation",
+ async () => {
+ const app = await scenarioApp(MCP_BACKGROUND_SUBAGENT_DESCRIPTOR);
+ const server = await startEveDev(app.appRoot);
+
+ try {
+ const started = await callTool(server.url, "alice", "modern", "agent_start", {
+ message: "Delegate this request and return the researcher result.",
+ });
+ const invocationId = requiredString(started.invocationId, "invocationId");
+ const completed = await pollInvocation(server.url, "alice", "modern", invocationId, [
+ "completed",
+ ]);
+
+ expect(completed).toMatchObject({
+ result: "MCP-CHILD-ANSWER",
+ status: "completed",
+ });
+ } catch (error) {
+ throw new Error(
+ [`stdout:\n${server.stdout()}`, `stderr:\n${server.stderr()}`].join("\n\n"),
+ { cause: error },
+ );
+ } finally {
+ await server.stop();
+ }
+ },
+ SCENARIO_TIMEOUT_MS,
+ );
+
it(
"runs a durable input lifecycle through modern and legacy Nitro requests",
async () => {
diff --git a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
index 0dc97cd38..05eedf718 100644
--- a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
+++ b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
@@ -264,3 +264,10 @@ export async function askThenRaceWorkflow(
const answer = await Promise.race([pending, workflowSleep("50ms")]);
return { decided: answer === undefined ? "timed out" : "answered", service: input.service };
}
+
+/** Receives the actual delegated result over the durable parent hook. */
+export async function receiveDelegatedResultWorkflow(token: string): Promise<unknown> {
+ "use workflow";
+ using result = createHook<unknown>({ token });
+ return await result;
+}
diff --git a/packages/eve/src/subagents/parent-notification.ts b/packages/eve/src/subagents/parent-notification.ts
index a185bc9ef..6299f4aab 100644
--- a/packages/eve/src/subagents/parent-notification.ts
+++ b/packages/eve/src/subagents/parent-notification.ts
@@ -80,7 +80,7 @@ export async function notifyDelegatedParentStep(input: {
export interface SettledTurnNotification {
readonly output: unknown;
readonly isError?: boolean;
- /** Usage this turn added; omitted (zero) on crash and expiry paths. */
+ /** Usage accumulated since the previous caller settlement, including yielded turns. */
readonly usage?: TokenUsage;
}
diff --git a/packages/eve/src/subagents/remote-dispatch.test.ts b/packages/eve/src/subagents/remote-dispatch.test.ts
index 8d9dad179..bcdd5e9c9 100644
--- a/packages/eve/src/subagents/remote-dispatch.test.ts
+++ b/packages/eve/src/subagents/remote-dispatch.test.ts
@@ -1174,6 +1174,22 @@ describe("cancelRemoteAgentTurn", () => {
);
});
+ it("forwards cancellation of a yielded child's background tasks", async () => {
+ const fetchMock = vi
+ .fn()
+ .mockResolvedValue(
+ Response.json({ ok: true, sessionId: "child-1", status: "accepted" }, { status: 202 }),
+ );
+ vi.stubGlobal("fetch", fetchMock);
+
+ await cancelRemoteAgentTurn({ remote: createRemoteAgent(), sessionId: "child-1", tasks: true });
+
+ expect(fetchMock).toHaveBeenCalledWith(
+ "https://remote.example.com/eve/v1/session/child-1/cancel",
+ expect.objectContaining({ body: JSON.stringify({ tasks: true }), method: "POST" }),
+ );
+ });
+
it("preserves a prefixed remote base path on cancel-turn requests", async () => {
const fetchMock = vi
.fn()
diff --git a/packages/eve/src/subagents/remote-dispatch.ts b/packages/eve/src/subagents/remote-dispatch.ts
index a55e111f6..18615769c 100644
--- a/packages/eve/src/subagents/remote-dispatch.ts
+++ b/packages/eve/src/subagents/remote-dispatch.ts
@@ -360,14 +360,15 @@ export async function cancelRemoteAgentTurn(input: {
readonly remote: Pick<ResolvedRuntimeRemoteAgentNode, "auth" | "headers" | "name" | "url">;
readonly sessionId: string;
readonly taskId?: string;
+ readonly tasks?: boolean;
readonly turnId?: string;
}): Promise<CancelTurnResult> {
const headers = input.headers ?? (await resolveRemoteAgentRequestHeaders(input.remote));
const response = await fetch(createRemoteAgentCancelTurnUrl(input.remote, input.sessionId), {
body:
- input.turnId === undefined && input.taskId === undefined
+ input.turnId === undefined && input.taskId === undefined && input.tasks === undefined
? undefined
- : JSON.stringify({ taskId: input.taskId, turnId: input.turnId }),
+ : JSON.stringify({ taskId: input.taskId, tasks: input.tasks, turnId: input.turnId }),
headers,
method: "POST",
});
diff --git a/packages/eve/src/tasks/delivery-context.ts b/packages/eve/src/tasks/delivery-context.ts
index a994d4c4a..65b2284c2 100644
--- a/packages/eve/src/tasks/delivery-context.ts
+++ b/packages/eve/src/tasks/delivery-context.ts
@@ -2,12 +2,8 @@ import type { TaskDeliveryPolicy, DeliverHookPayload } from "#channel/types.js";
import { markFrameworkStepInput } from "#harness/messages.js";
import type { SessionStateMap, StepInput } from "#harness/types.js";
import { EMPTY_DELIVERY_SENTINEL } from "#shared/empty-delivery.js";
-import {
- readWorkflowTaskView,
- getBackgroundWorkflowToolRuns,
- type BackgroundWorkflowToolRun,
-} from "#harness/workflow-tool-runs.js";
-import { getTaskCohortId } from "#tasks/session-task-cohorts.js";
+import { getBackgroundTasks, type BackgroundTask } from "#harness/workflow-tool-runs.js";
+import { taskIdOfDelivery } from "#tasks/notification.js";
export const TASK_DELIVERY_CONTEXT_LABEL = "[Task state]";
@@ -54,16 +50,15 @@ export function resolveTaskDeliveryContext(input: {
| undefined {
const firstDeliveryId = input.taskDeliveryIds[0];
if (firstDeliveryId === undefined) return undefined;
- const entries = getBackgroundWorkflowToolRuns(input.state);
- const delivered = entries.find((entry) => firstDeliveryId.startsWith(`${entry.task.taskId}:`));
+ const tasks = getBackgroundTasks(input.state);
+ const delivered = tasks.get(taskIdOfDelivery(firstDeliveryId));
if (delivered === undefined) return undefined;
-
- const cohort = entries.filter(
- (entry) => getTaskCohortId(entry.task) === getTaskCohortId(delivered.task),
- );
return {
- ...projectTaskCohort(cohort, input.taskDeliveryPolicy === "auto"),
- rootTurnId: delivered.origin.turnId,
+ ...projectTaskCohort(
+ tasks.query({ cohortId: delivered.cohortId }),
+ input.taskDeliveryPolicy === "auto",
+ ),
+ rootTurnId: delivered.turnId,
};
}
@@ -72,32 +67,29 @@ export function resolveInitiatingTaskContext(input: {
readonly state: SessionStateMap | undefined;
readonly turnId: string;
}): { readonly context: string; readonly phase: "initiating" } | undefined {
- const cohort = getBackgroundWorkflowToolRuns(input.state).filter(
- (entry) => entry.origin.turnId === input.turnId,
- );
- if (!cohort.some((entry) => entry.task.outcome === undefined)) {
- return undefined;
- }
- return { ...projectTaskCohort(cohort, false), phase: "initiating" };
+ const tasks = getBackgroundTasks(input.state);
+ if (tasks.query({ state: "working", turnId: input.turnId }).length === 0) return undefined;
+ return {
+ ...projectTaskCohort(tasks.query({ turnId: input.turnId }), false),
+ phase: "initiating",
+ };
}
function projectTaskCohort(
- cohort: readonly BackgroundWorkflowToolRun[],
+ cohort: readonly BackgroundTask[],
includePartialOutputs: boolean,
): {
readonly context: string;
readonly phase: "pending" | "settled";
} {
- const settled = cohort.every((entry) => entry.task.outcome !== undefined);
- const tasks = cohort.map((entry) => {
- const view = readWorkflowTaskView(entry.task);
- return {
- name: entry.task.metadata.name,
- output: settled || includePartialOutputs ? view?.lastOutput : undefined,
- status: view?.status ?? "pending",
- taskId: entry.task.taskId,
- };
- });
+ const settled = cohort.every((task) => task.status !== "working");
+ const tasks = cohort.map((task) => ({
+ name: task.metadata.name,
+ output: settled || includePartialOutputs ? task.lastOutput : undefined,
+ // The model-facing projection names unsettled work "pending".
+ status: task.status === "working" ? "pending" : task.status,
+ taskId: task.taskId,
+ }));
return {
context: `${TASK_DELIVERY_CONTEXT_LABEL}\n${JSON.stringify({ tasks })}`,
diff --git a/packages/eve/src/tasks/notification.ts b/packages/eve/src/tasks/notification.ts
index e3f0976a1..86bfb821d 100644
--- a/packages/eve/src/tasks/notification.ts
+++ b/packages/eve/src/tasks/notification.ts
@@ -1,3 +1,6 @@
+import type { DeliverHookPayload } from "#channel/types.js";
+import type { SessionStateMap } from "#harness/types.js";
+import { getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import type { JsonValue } from "#shared/json.js";
import type { TaskView } from "#tasks/types.js";
@@ -18,3 +21,20 @@ export function formatTaskNotification(view: TaskView): string {
export function formatTaskOutput(output: JsonValue): string {
return typeof output === "string" ? output : (JSON.stringify(output) ?? "null");
}
+
+/** Task delivery ids are `<taskId>` or `<taskId>:<event>`; derived task ids never contain `:`. */
+export function taskIdOfDelivery(deliveryId: string): string {
+ const separator = deliveryId.indexOf(":");
+ return separator < 0 ? deliveryId : deliveryId.slice(0, separator);
+}
+
+/** The parent already recorded this task's outcome, so the delivery cannot notify the model again. */
+export function hasRecordedTaskOutcome(
+ delivery: DeliverHookPayload,
+ state: SessionStateMap | undefined,
+): boolean {
+ const deliveryId = delivery.taskDeliveryId;
+ if (deliveryId === undefined) return false;
+ const task = getBackgroundTasks(state).get(taskIdOfDelivery(deliveryId));
+ return task !== undefined && task.status !== "working";
+}
diff --git a/packages/eve/src/tasks/session-task-cohorts.test.ts b/packages/eve/src/tasks/session-task-cohorts.test.ts
deleted file mode 100644
index 59e5ab43b..000000000
--- a/packages/eve/src/tasks/session-task-cohorts.test.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-import { describe, expect, it } from "vitest";
-
-import { getBackgroundWorkflowToolRuns } from "#harness/workflow-tool-runs.js";
-import { getTaskCohortId, getSessionTaskCohorts } from "#tasks/session-task-cohorts.js";
-
-describe("workflow task cohort lookup", () => {
- it("projects the same identities as the full task index", () => {
- const state = {
- "eve.workflowTool": {
- version: 3,
- runs: ["turn-1", "turn-2", "turn-2"].map((createdByTurnId, index) => ({
- callId: `task_${index}`,
- toolName: "worker",
- lifetime: "session" as const,
- origin: { turnId: createdByTurnId, stepIndex: 0 },
- address: { runId: `run-${index}`, hookToken: `inbox-${index}` },
- task: {
- cohortId: index === 1 ? "task_0" : undefined,
- taskId: `task_${index}`,
- dispatchContext: { auth: { current: null, initiator: null } },
- metadata: { kind: "subagent", name: "worker" },
- },
- })),
- },
- };
- expect([...getSessionTaskCohorts(state)]).toEqual(
- getBackgroundWorkflowToolRuns(state).map((task) => [
- task.task.taskId,
- getTaskCohortId(task.task),
- ]),
- );
- });
-
- it("returns no cohorts when the task index is absent", () => {
- expect(getSessionTaskCohorts(undefined).size).toBe(0);
- expect(getSessionTaskCohorts({}).size).toBe(0);
- });
-
- it.each([
- null,
- { version: 99, runs: [] },
- { version: 1, runs: null },
- { version: 1, runs: [null] },
- ])("rejects invalid registry state: %j", (raw) => {
- expect(() => getSessionTaskCohorts({ "eve.workflowTool": raw })).toThrow(
- "Corrupt workflow tool run registry",
- );
- });
-});
diff --git a/packages/eve/src/tasks/session-task-cohorts.ts b/packages/eve/src/tasks/session-task-cohorts.ts
deleted file mode 100644
index 5fce49af3..000000000
--- a/packages/eve/src/tasks/session-task-cohorts.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import type { SessionStateMap } from "#harness/types.js";
-import { getBackgroundWorkflowToolRuns } from "#harness/workflow-tool-runs.js";
-
-/** An entry without a join target starts its own cohort. */
-export function getTaskCohortId(task: {
- readonly taskId: string;
- readonly cohortId?: string;
-}): string {
- return task.cohortId ?? task.taskId;
-}
-
-export function getSessionTaskCohorts(
- state: SessionStateMap | undefined,
-): ReadonlyMap<string, string> {
- return new Map(
- getBackgroundWorkflowToolRuns(state).map(({ task }) => [task.taskId, getTaskCohortId(task)]),
- );
-}
diff --git a/packages/eve/src/tracing/agent-trace-context-store.ts b/packages/eve/src/tracing/agent-trace-context-store.ts
index c37924bea..d974fe435 100644
--- a/packages/eve/src/tracing/agent-trace-context-store.ts
+++ b/packages/eve/src/tracing/agent-trace-context-store.ts
@@ -15,10 +15,7 @@ import type {
import { actionIdempotencyKey } from "#instrumentation/lifecycle.js";
import { deriveTaskId } from "#tasks/task-id.js";
import type { SessionStateMap } from "#harness/types.js";
-import {
- getBlockingWorkflowToolRuns,
- getBackgroundWorkflowToolRuns,
-} from "#harness/workflow-tool-runs.js";
+import { getBlockingWorkflowToolRuns, getBackgroundTasks } from "#harness/workflow-tool-runs.js";
import { createLogger } from "#internal/logging.js";
import type { InstrumentationDecision } from "#shared/instrumentation-decision.js";
@@ -66,9 +63,9 @@ function pruneTraceOwnership(
if (state === undefined) return;
const calls = new Set(getBlockingWorkflowToolRuns(sessionState).map((run) => run.callId));
const tasks = new Set(
- getBackgroundWorkflowToolRuns(sessionState)
- .filter((task) => task.task.outcome === undefined)
- .map((task) => task.task.taskId),
+ getBackgroundTasks(sessionState)
+ .query({ state: "working" })
+ .map((task) => task.taskId),
);
const actionAnchors = Object.fromEntries(
Object.entries(state.actionAnchors).filter(