The Open Framework for Building Agents
fix(eve): preserve parent turn identity for resumed subagent dispatches (#1937)
vercel-gh-bot-4[bot]e402672d7336
diff --git a/.changeset/quiet-turns-recover.md b/.changeset/quiet-turns-recover.mdnew file mode 100644index 000000000..3ddd6f831--- /dev/null+++ b/.changeset/quiet-turns-recover.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Keep follow-up subagent dispatches in the active parent turn when a resumed runtime-action batch has lost its turn ID, preventing the session from failing while persisting the child handle.diff --git a/packages/eve/src/execution/dispatch-runtime-actions-step.integration.test.ts b/packages/eve/src/execution/dispatch-runtime-actions-step.integration.test.tsindex caa342cba..550fcc2c8 100644--- a/packages/eve/src/execution/dispatch-runtime-actions-step.integration.test.ts+++ b/packages/eve/src/execution/dispatch-runtime-actions-step.integration.test.ts@@ -5,6 +5,7 @@ import { RemoteAgentContinueRequestError } from "#execution/remote-agent-dispatcimport type { DurableSessionState } from "#execution/durable-session-store.js";import { dispatchRuntimeActionsStep } from "#execution/dispatch-runtime-actions-step.js";import {+ getPendingRuntimeActionBatch,resolvePendingRuntimeActions,setPendingRuntimeActionBatch,} from "#harness/runtime-actions.js";@@ -189,6 +190,65 @@ describe("dispatchRuntimeActionsStep child starts", () => {expect(writes).toHaveLength(1);});+ it("uses the active session turn when pending batch metadata has an empty turn id", async () => {+ const session = createStartSession({+ event: { sequence: 3, stepIndex: 2, turnId: "" },+ kind: "local",+ });+ installContext(session, {+ definition: { description: "Research", kind: "subagent" },+ nodeId: "subagents/research",+ });++ const result = await dispatchRuntimeActionsStep({+ parentContinuationToken: "turn-inbox",+ parentWritable: createWritable(),+ serializedContext: {},+ sessionState: {+ ...BASE_STATE,+ emissionState: { sequence: 3, sessionStarted: true, stepIndex: 2, turnId: "" },+ },+ });++ const resultState = readResultSessionState(result, session);+ expect(getPendingRuntimeActionBatch(resultState)?.event.turnId).toBe("turn_3");+ expect(getAgentHandleStore(resultState)).toMatchObject({+ handles: [+ {+ operation: {+ parentTurnId: "turn_3",+ },+ },+ ],+ });+ });++ it("persists the active turn when dispatch leaves agent handles unchanged", async () => {+ const session = createStartSession({+ event: { sequence: 3, stepIndex: 2, turnId: "" },+ kind: "remote",+ });+ installContext(session, {+ definition: REMOTE_REGISTRY_DEFINITION,+ nodeId: "remote/research",+ });++ const result = await dispatchRuntimeActionsStep({+ parentContinuationToken: "turn-inbox",+ parentWritable: createWritable(),+ serializedContext: {},+ sessionState: {+ ...BASE_STATE,+ emissionState: { sequence: 3, sessionStarted: true, stepIndex: 2, turnId: "" },+ },+ });++ const resultState = readResultSessionState(result, session);+ expect(getAgentHandleStore(resultState)).toBeUndefined();+ expect(getPendingRuntimeActionBatch(resultState)?.event.turnId).toBe("turn_3");+ expect(mocks.createDurableSessionState).toHaveBeenCalledTimes(1);+ });+it("rejects the prepared handle when the local start fails", async () => {const session = createStartSession({ kind: "local" });installContext(session, {@@ -687,7 +747,14 @@ function createBaseSession(handle?: AgentHandle): HarnessSession {: { ...base, state: { [AGENT_HANDLES_STATE_KEY]: { handles: [handle] } } };}-function createStartSession(input: { readonly kind: "local" | "remote" }): HarnessSession {+function createStartSession(input: {+ readonly event?: {+ readonly sequence: number;+ readonly stepIndex: number;+ readonly turnId: string;+ };+ readonly kind: "local" | "remote";+}): HarnessSession {return setPendingRuntimeActionBatch({actions: [input.kind === "local"@@ -710,7 +777,7 @@ function createStartSession(input: { readonly kind: "local" | "remote" }): HarneremoteAgentName: "research",},],- event: { sequence: 1, stepIndex: 2, turnId: "turn-1" },+ event: input.event ?? { sequence: 1, stepIndex: 2, turnId: "turn-1" },responseMessages: [],session: createBaseSession(),});diff --git a/packages/eve/src/execution/dispatch-runtime-actions-step.ts b/packages/eve/src/execution/dispatch-runtime-actions-step.tsindex c7c7e1c15..1d61e38f4 100644--- a/packages/eve/src/execution/dispatch-runtime-actions-step.ts+++ b/packages/eve/src/execution/dispatch-runtime-actions-step.ts@@ -1,13 +1,7 @@/**- * Starts or continues every pending runtime action for the parked parent- * session.- *- * The batch is classified into a dispatch plan first (reject / resume /- * start), then each entry dispatches and emits one- * parent `subagent.called` control-plane event through a single tail.- * Every start commits an agent handle (`starting`) before its side effect- * and confirms it (`running`) once the child reports coordinates, so the- * returned snapshot-bearing state owns every child it may have created.+ * Dispatches every pending runtime action for a parked parent session.+ * Starts commit ownership before their side effect, so the returned state+ * owns every child it may have created.*/import { buildAdapterContext } from "#channel/adapter-context.js";@@ -39,7 +33,10 @@ import {prepareAgentStart,rejectAgentEffect,} from "#harness/handles/transitions.js";-import { getPendingRuntimeActionBatch } from "#harness/runtime-actions.js";+import {+ getPendingRuntimeActionBatch,+ setPendingRuntimeActionBatch,+} from "#harness/runtime-actions.js";import {createSubagentCalledEvent,encodeMessageStreamEvent,@@ -77,6 +74,7 @@ import { readSessionTraceContext } from "#tracing/agent-trace-context-store.js";import { resolveSubagentDepth } from "#harness/subagent-depth.js";import { getDynamicSubagentSelection } from "#context/dynamic-subagent-lifecycle.js";import { resolveEffectiveAgentRuntime } from "#execution/effective-agent-config.js";+import { activeTurnId } from "#harness/active-turn-id.js";const log = createLogger("execution.dispatch-runtime-actions");@@ -137,15 +135,20 @@ export async function dispatchRuntimeActionsStep(input: {return { results: [], sessionState: input.sessionState };}+ const parentTurnId = batch.event.turnId || activeTurnId(input.sessionState.emissionState);+ const batchEvent = { ...batch.event, turnId: parentTurnId };+const ctx = await deserializeContext(input.serializedContext);const bundle = ctx.require(BundleKey);const effectiveAgent = resolveEffectiveAgentRuntime(bundle, ctx);- const session = hydrateDurableSession({- compactionOverrides: {- thresholdPercent: effectiveAgent.thresholdPercent,- },- durable: durableSession,- turnAgent: effectiveAgent.turnAgent,+ const session = setPendingRuntimeActionBatch({+ ...batch,+ event: batchEvent,+ session: hydrateDurableSession({+ compactionOverrides: { thresholdPercent: effectiveAgent.thresholdPercent },+ durable: durableSession,+ turnAgent: effectiveAgent.turnAgent,+ }),});const adapter = ctx.require(ChannelKey);const auth = ctx.get(AuthKey) ?? null;@@ -199,13 +202,13 @@ export async function dispatchRuntimeActionsStep(input: {}),currentSession: nextSession,parentToken: input.parentContinuationToken ?? session.continuationToken,- parentTurnId: batch.event.turnId,+ parentTurnId,});break;case "start":outcome = await startSubagent({auth,- batchEvent: batch.event,+ batchEvent,bundle,callbackBaseUrl: input.callbackBaseUrl,capabilities,@@ -240,10 +243,10 @@ export async function dispatchRuntimeActionsStep(input: {name: outcome.name,remote:outcome.address.kind === "agent/remote" ? { url: outcome.address.url } : undefined,- sequence: batch.event.sequence,+ sequence: batchEvent.sequence,sessionId: session.sessionId,toolName: outcome.toolName,- turnId: batch.event.turnId,+ turnId: parentTurnId,workflowId: workflowEntryReference.workflowId,}),adapterCtx,@@ -262,7 +265,7 @@ export async function dispatchRuntimeActionsStep(input: {}const nextState =- nextSession === session+ nextSession === session && batch.event.turnId === parentTurnId? input.sessionState: createDurableSessionState({ session: nextSession });