The Open Framework for Building Agents
fix(eve): dynamic models - preserve session choice after cancellation (#2077)
Andrew Barba5d123287e6e2
diff --git a/.changeset/curly-pumas-wait.md b/.changeset/curly-pumas-wait.mdnew file mode 100644index 000000000..bb2400ed2--- /dev/null+++ b/.changeset/curly-pumas-wait.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Preserve a session-scoped dynamic model selection when the first turn is cancelled so later turns can reuse it without requiring a `turn.started` resolver.diff --git a/e2e/fixtures/agent-dynamic-model-session/.gitignore b/e2e/fixtures/agent-dynamic-model-session/.gitignorenew file mode 100644index 000000000..eb095f0f8--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/.gitignore@@ -0,0 +1,10 @@+node_modules+.env*+.eve+.vercel+.next+.output+.nitro+dist+.DS_Store+*.tsbuildinfodiff --git a/e2e/fixtures/agent-dynamic-model-session/.vercelignore b/e2e/fixtures/agent-dynamic-model-session/.vercelignorenew file mode 100644index 000000000..357664320--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/.vercelignore@@ -0,0 +1,6 @@+node_modules+.eve+.next+.output+.nitro+distdiff --git a/e2e/fixtures/agent-dynamic-model-session/agent/agent.ts b/e2e/fixtures/agent-dynamic-model-session/agent/agent.tsnew file mode 100644index 000000000..44e5d9807--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/agent/agent.ts@@ -0,0 +1,34 @@+import { e2eAgentConfig, MOCK_MODEL_SENTINEL } from "@eve-e2e/config";+import { defineAgent, defineDynamic } from "eve";++const requestedModel = process.env.EVE_E2E_MODEL;+const selectedModel =+ requestedModel === undefined || requestedModel === MOCK_MODEL_SENTINEL+ ? "openai/gpt-5.6-sol"+ : requestedModel;++if (requestedModel === MOCK_MODEL_SENTINEL) {+ process.env.EVE_MOCK_AUTHORED_MODELS = "1";+}++const { experimental } = e2eAgentConfig();++export default defineAgent({+ experimental,+ model: defineDynamic({+ events: {+ "session.started": (_event, ctx) => {+ if (ctx.messages.length > 0) {+ throw new Error(+ "session.started dynamic model resolver ran after session history existed",+ );+ }++ return {+ model: selectedModel,+ modelContextWindowTokens: 1_000_000,+ };+ },+ },+ }),+});diff --git a/e2e/fixtures/agent-dynamic-model-session/agent/instructions.md b/e2e/fixtures/agent-dynamic-model-session/agent/instructions.mdnew file mode 100644index 000000000..215f5a34e--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/agent/instructions.md@@ -0,0 +1,2 @@+Follow the user's requested response format exactly. When the user asks you to wait until the+turn is cancelled, call the `wait-for-cancellation` tool immediately with no preamble.diff --git a/e2e/fixtures/agent-dynamic-model-session/agent/tools/wait-for-cancellation.ts b/e2e/fixtures/agent-dynamic-model-session/agent/tools/wait-for-cancellation.tsnew file mode 100644index 000000000..4536fe3db--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/agent/tools/wait-for-cancellation.ts@@ -0,0 +1,28 @@+import { defineTool } from "eve/tools";+import { never } from "eve/tools/approval";+import { z } from "zod";++const FALLBACK_TIMEOUT_MS = 90_000;++export default defineTool({+ description: "Waits until the current turn is cancelled.",+ inputSchema: z.object({}),+ approval: never(),+ execute(_input, ctx) {+ return new Promise<string>((resolve, reject) => {+ const timer = setTimeout(() => {+ resolve("wait-for-cancellation: fallback timeout reached without cancellation");+ }, FALLBACK_TIMEOUT_MS);++ const abort = (): void => {+ clearTimeout(timer);+ reject(ctx.abortSignal.reason);+ };+ if (ctx.abortSignal.aborted) {+ abort();+ return;+ }+ ctx.abortSignal.addEventListener("abort", abort, { once: true });+ });+ },+});diff --git a/e2e/fixtures/agent-dynamic-model-session/evals/dynamic-model/cancelled-first-turn.eval.ts b/e2e/fixtures/agent-dynamic-model-session/evals/dynamic-model/cancelled-first-turn.eval.tsnew file mode 100644index 000000000..cc35f7a5e--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/evals/dynamic-model/cancelled-first-turn.eval.ts@@ -0,0 +1,58 @@+import { MOCK_MODEL_SENTINEL } from "@eve-e2e/config";+import { defineEval } from "eve/evals";+import { satisfies } from "eve/evals/expect";++const TOOL_NAME = "wait-for-cancellation";+const requestedModel = process.env.EVE_E2E_MODEL;+const selectedModel =+ requestedModel === undefined || requestedModel === MOCK_MODEL_SENTINEL+ ? "openai/gpt-5.6-sol"+ : requestedModel;++export default defineEval({+ description: "A session-scoped dynamic model remains selected when the first turn is cancelled.",+ timeoutMs: 240_000,++ async test(t) {+ const live = await t.start(+ "Call the wait-for-cancellation tool and wait until this turn is cancelled.",+ );+ await live.waitForEvent("actions.requested", {+ data: {+ actions: (actions) =>+ actions.some((action) => action.kind === "tool-call" && action.toolName === TOOL_NAME),+ },+ });++ const cancelled = await live.cancel();+ await t.require(+ cancelled,+ satisfies(+ (value: typeof cancelled) =>+ value.status === "accepted" && value.sessionId === live.sessionId,+ "the first-turn cancellation is accepted",+ ),+ );++ const cancelledTurn = await live.result();+ cancelledTurn.event("turn.cancelled", { count: 1 });+ cancelledTurn.eventOrder([{ type: "turn.cancelled" }, { type: "session.waiting" }]);+ cancelledTurn.notEvent("turn.failed");+ cancelledTurn.notEvent("session.failed");++ const resumed = await t.send(+ 'Reply with exactly the text "session model after cancellation" and nothing else.',+ );+ resumed.expectOk();+ resumed.messageIncludes("session model after cancellation");+ resumed.eventsSatisfy(+ "the resumed turn reuses the session selection without restarting the session",+ (events) =>+ events.some(+ (event) => event.type === "step.started" && event.data.modelId === selectedModel,+ ) && events.every((event) => event.type !== "session.started"),+ );++ t.succeeded();+ },+});diff --git a/e2e/fixtures/agent-dynamic-model-session/evals/dynamic-model/session-selection.eval.ts b/e2e/fixtures/agent-dynamic-model-session/evals/dynamic-model/session-selection.eval.tsnew file mode 100644index 000000000..b20c77889--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/evals/dynamic-model/session-selection.eval.ts@@ -0,0 +1,38 @@+import { MOCK_MODEL_SENTINEL } from "@eve-e2e/config";+import { defineEval } from "eve/evals";++const requestedModel = process.env.EVE_E2E_MODEL;+const selectedModel =+ requestedModel === undefined || requestedModel === MOCK_MODEL_SENTINEL+ ? "openai/gpt-5.6-sol"+ : requestedModel;++export default defineEval({+ description: "A session-scoped dynamic model resolves once and remains selected across turns.",+ async test(t) {+ const first = await t.send(+ 'Reply with exactly the text "session model turn one" and nothing else.',+ );+ first.expectOk();+ first.messageIncludes("session model turn one");+ first.eventsSatisfy("the session resolver selects the configured model", (events) =>+ events.some((event) => event.type === "step.started" && event.data.modelId === selectedModel),+ );++ const second = await t.send(+ 'Reply with exactly the text "session model turn two" and nothing else.',+ );+ second.expectOk();+ second.messageIncludes("session model turn two");+ second.eventsSatisfy(+ "the second turn reuses the session selection",+ (events) =>+ events.some(+ (event) => event.type === "step.started" && event.data.modelId === selectedModel,+ ) && events.every((event) => event.type !== "session.started"),+ );++ t.succeeded();+ t.usedNoTools();+ },+});diff --git a/e2e/fixtures/agent-dynamic-model-session/evals/evals.config.ts b/e2e/fixtures/agent-dynamic-model-session/evals/evals.config.tsnew file mode 100644index 000000000..7a61a5b55--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/evals/evals.config.ts@@ -0,0 +1,6 @@+import { e2eJudgeModel } from "@eve-e2e/config";+import { defineEvalConfig } from "eve/evals";++export default defineEvalConfig({+ judge: { model: e2eJudgeModel() },+});diff --git a/e2e/fixtures/agent-dynamic-model-session/package.json b/e2e/fixtures/agent-dynamic-model-session/package.jsonnew file mode 100644index 000000000..1345ccc3e--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/package.json@@ -0,0 +1,26 @@+{+ "name": "agent-dynamic-model-session",+ "version": "0.0.0",+ "private": true,+ "type": "module",+ "scripts": {+ "build": "eve build",+ "dev": "eve dev",+ "start": "eve start",+ "typecheck": "eve build && tsc",+ "test:e2e": "eve eval --strict"+ },+ "dependencies": {+ "@eve-e2e/config": "workspace:*",+ "@workflow/world-postgres": "catalog:",+ "eve": "workspace:*",+ "zod": "catalog:"+ },+ "devDependencies": {+ "@types/node": "catalog:",+ "typescript": "catalog:"+ },+ "e2e": {+ "modelMatrix": "full"+ }+}diff --git a/e2e/fixtures/agent-dynamic-model-session/tsconfig.json b/e2e/fixtures/agent-dynamic-model-session/tsconfig.jsonnew file mode 100644index 000000000..85e3eceb8--- /dev/null+++ b/e2e/fixtures/agent-dynamic-model-session/tsconfig.json@@ -0,0 +1,17 @@+{+ "compilerOptions": {+ "target": "ES2022",+ "module": "esnext",+ "moduleResolution": "bundler",+ "outDir": "dist",+ "rootDir": ".",+ "strict": true,+ "esModuleInterop": true,+ "skipLibCheck": true,+ "forceConsistentCasingInFileNames": true,+ "declaration": true,+ "noEmit": true,+ "types": ["node"]+ },+ "include": ["agent/**/*.ts", "evals/**/*.ts"]+}diff --git a/packages/eve/src/context/serialized-dynamic-model-selection.ts b/packages/eve/src/context/serialized-dynamic-model-selection.tsnew file mode 100644index 000000000..dce5e8ef5--- /dev/null+++ b/packages/eve/src/context/serialized-dynamic-model-selection.ts@@ -0,0 +1,12 @@+import { SessionDynamicModelReferenceKey } from "#context/keys.js";++/** Keeps a completed session-scoped model selection when its turn is cancelled. */+export function preserveSerializedSessionDynamicModelSelection(+ original: Record<string, unknown>,+ interrupted: Record<string, unknown>,+): Record<string, unknown> {+ const selection = interrupted[SessionDynamicModelReferenceKey.name];+ return selection === undefined+ ? original+ : { ...original, [SessionDynamicModelReferenceKey.name]: selection };+}diff --git a/packages/eve/src/execution/turn-workflow.test.ts b/packages/eve/src/execution/turn-workflow.test.tsindex 37b666641..08f00e324 100644--- a/packages/eve/src/execution/turn-workflow.test.ts+++ b/packages/eve/src/execution/turn-workflow.test.ts@@ -1,6 +1,7 @@import { afterEach, describe, expect, it, vi } from "vitest";import type { HookPayload } from "#channel/types.js";+import { SessionDynamicModelReferenceKey } from "#context/keys.js";import { cancelDescendantTurnsStep } from "#execution/cancel-descendant-turns-step.js";import { dispatchRuntimeActionsStep } from "#execution/dispatch-runtime-actions-step.js";import { dispatchWorkflowRuntimeActionsStep } from "#execution/dispatch-workflow-runtime-actions-step.js";@@ -334,18 +335,18 @@ describe("turnWorkflow", () => {expect(vi.mocked(turnStep).mock.calls[0]?.[0].abortSignal).toBeInstanceOf(AbortSignal);expect(cancelDescendantTurnsStep).toHaveBeenCalledWith({- serializedContext: { state: "start" },+ serializedContext: { state: "cancelled" },sessionState,});// The command inbox forwards cancellation to this turn-private hook.expect(cancelHookTokens()).toEqual(["turn-token:cancel"]);- // The cancelled result is a pure marker: the control payload carries- // the cursor's last settled state, not the aborted step's echo.+ // The cancelled step has already rolled its context back to the allowed+ // carve-outs, which must reach the driver cancellation epilogue.expect(resumeHookMock).toHaveBeenCalledWith("turn-token", {action: {cancelled: true,kind: "park",- serializedContext: { state: "start" },+ serializedContext: { state: "cancelled" },sessionState,},kind: "turn-result",@@ -355,13 +356,20 @@ describe("turnWorkflow", () => {it("honors cancellation observed while a durable turn step returns", async () => {const sessionState = createSessionState();+ const sessionModel = {+ id: "openai/gpt-5.6-sol",+ contextWindowTokens: 1_000_000,+ };installInbox([], { cancelPayloads: [{}] });vi.mocked(turnStep).mockImplementationOnce(async (stepInput) => {await vi.waitFor(() => expect(stepInput.abortSignal?.aborted).toBe(true));return {action: "done",output: "must not complete",- serializedContext: { state: "done" },+ serializedContext: {+ state: "done",+ [SessionDynamicModelReferenceKey.name]: sessionModel,+ },sessionState,};});@@ -372,15 +380,19 @@ describe("turnWorkflow", () => {});await turnWorkflow(input);+ const cancelledContext = {+ state: "start",+ [SessionDynamicModelReferenceKey.name]: sessionModel,+ };expect(cancelDescendantTurnsStep).toHaveBeenCalledWith({- serializedContext: { state: "start" },+ serializedContext: cancelledContext,sessionState,});expect(resumeHookMock).toHaveBeenCalledWith("turn-token", {action: {cancelled: true,kind: "park",- serializedContext: { state: "start" },+ serializedContext: cancelledContext,sessionState,},kind: "turn-result",@@ -685,7 +697,7 @@ describe("turnWorkflow", () => {expect(vi.mocked(turnStep).mock.calls[1]?.[0].abortSignal?.aborted).toBe(true);expect(cancelDescendantTurnsStep).toHaveBeenCalledWith({- serializedContext: { state: "pending" },+ serializedContext: { state: "cancelled" },sessionState: adoptedState,});expect(vi.mocked(dispatchRuntimeActionsStep).mock.invocationCallOrder[0]).toBeLessThan(diff --git a/packages/eve/src/execution/turn-workflow.ts b/packages/eve/src/execution/turn-workflow.tsindex d548f34d5..04f8fc064 100644--- a/packages/eve/src/execution/turn-workflow.ts+++ b/packages/eve/src/execution/turn-workflow.ts@@ -6,6 +6,7 @@ import {} from "#compiled/@workflow/core/index.js";import type { DeliverHookPayload } from "#channel/types.js";+import { preserveSerializedSessionDynamicModelSelection } from "#context/serialized-dynamic-model-selection.js";import { cancelDescendantTurnsStep } from "#execution/cancel-descendant-turns-step.js";import { sendTurnControlStep, type TurnInboxPayload } from "#execution/turn-control-protocol.js";import { dispatchRuntimeActionsStep } from "#execution/dispatch-runtime-actions-step.js";@@ -107,10 +108,25 @@ async function runTurnOwnedWorkflow(input: TurnWorkflowInput): Promise<void> {// A cancel observed while the step was returning must still win: the// step may have missed the abort and completed normally. Pending// runtime-action batches are exempt — their wait observes the signal.- if (- result.action === "cancelled" ||- (cancellation?.signal.aborted === true && pendingActionKeys === undefined)- ) {+ if (result.action === "cancelled") {+ // The cancelled step returns only the context carve-outs required by+ // the driver epilogue and later turns; adopt those before settling.+ await cursor.adopt(result);+ await finishCancelledTurn({ bufferedDeliveries, cancellation, cursor });+ return;+ }++ if (cancellation?.signal.aborted === true && pendingActionKeys === undefined) {+ // Some worlds cannot interrupt a running step, so it can complete+ // normally after the workflow observes cancellation. Roll that result+ // back except for a session model selected by its one-time preamble.+ await cursor.adopt({+ serializedContext: preserveSerializedSessionDynamicModelSelection(+ cursor.serializedContext,+ result.serializedContext,+ ),+ sessionState: cursor.sessionState,+ });// No `canPark` check here: that gate rejects model-authored waits// (`next: null`) in task mode, whereas every session can resume by// stable ID after a cancelled turn. The epilogue runs in the driverdiff --git a/packages/eve/src/execution/workflow-steps.test.ts b/packages/eve/src/execution/workflow-steps.test.tsindex 6ab61c754..e5d6eef8e 100644--- a/packages/eve/src/execution/workflow-steps.test.ts+++ b/packages/eve/src/execution/workflow-steps.test.ts@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";import type { ChannelAdapter, ChannelAdapterContext } from "#channel/adapter.js";import type { DeliverPayload, SubagentInputRequestHookPayload } from "#channel/types.js";-import { ContextContainer } from "#context/container.js";+import { ContextContainer, loadContext } from "#context/container.js";import { ContextKey } from "#context/key.js";import {AuthKey,@@ -10,6 +10,7 @@ import {DynamicSubagentAgentConfigKey,ModeKey,SessionDynamicSubagentSelectionsKey,+ SessionDynamicModelReferenceKey,SessionDynamicToolMetadataKey,SessionDynamicToolRuntimeRevisionKey,SessionIdKey,@@ -19,6 +20,7 @@ import { serializeContext } from "#context/serialize.js";import { setPendingRuntimeActionBatch } from "#harness/runtime-actions.js";import { getAgentHandleStore } from "#harness/handles/store.js";import { requestTurnSleep } from "#harness/turn-sleep.js";+import { TurnCancelledError } from "#harness/turn-cancellation.js";import { getPendingAuthorization, setPendingAuthorization } from "#harness/authorization.js";import { getProxyInputRequests, upsertProxyInputRequests } from "#harness/proxy-input-requests.js";import { appendPendingInputBatch } from "#harness/input-requests.js";@@ -1140,6 +1142,62 @@ describe("dispatchRuntimeActionsStep", () => {});describe("turnStep", () => {+ it("keeps a session-scoped dynamic model selection when the first turn is cancelled", async () => {+ const session = createStubSession();+ installSessionStoreMocks([session]);+ vi.mocked(getCompiledRuntimeAgentBundle).mockResolvedValue({+ adapterRegistry: {+ adaptersByKind: new Map([[threadContextAdapter.kind, threadContextAdapter]]),+ },+ compiledArtifactsSource: {},+ graph: {+ nodesByNodeId: new Map(),+ root: {+ sandboxRegistry: { sandbox: null },+ turnAgent: TestTurnAgent,+ },+ },+ moduleMap: { nodes: {} },+ hookRegistry: createEmptyHookRegistry(),+ resolvedAgent: { config: {} },+ subagentRegistry: {},+ toolRegistry: {},+ turnAgent: TestTurnAgent,+ } as never);+ vi.mocked(createExecutionNodeStep).mockImplementation(() => {+ return async (): Promise<StepResult> => {+ const ctx = loadContext();+ ctx.set(SessionDynamicModelReferenceKey, {+ id: "anthropic/claude-opus-4.6",+ contextWindowTokens: 1_000_000,+ });+ ctx.set(ThreadKey, "discard this turn-scoped mutation");+ throw new TurnCancelledError();+ };+ });++ const result = await turnStep({+ input: {+ kind: "deliver",+ payloads: [{ message: "cancel this turn" }],+ },+ parentWritable: createTestWritable(),+ serializedContext: createSerializedContext(),+ sessionState: createStubSessionState(),+ });++ expect(result).toMatchObject({+ action: "cancelled",+ serializedContext: {+ [SessionDynamicModelReferenceKey.name]: {+ id: "anthropic/claude-opus-4.6",+ contextWindowTokens: 1_000_000,+ },+ },+ });+ expect(result.serializedContext).not.toHaveProperty(ThreadKey.name);+ });+it("rejects task completion while input requests remain pending", async () => {const session = appendPendingInputBatch({requests: [diff --git a/packages/eve/src/execution/workflow-steps.ts b/packages/eve/src/execution/workflow-steps.tsindex dcdfbf702..31a5be6c3 100644--- a/packages/eve/src/execution/workflow-steps.ts+++ b/packages/eve/src/execution/workflow-steps.ts@@ -9,6 +9,7 @@ import {prepareDynamicInstructionPreamble,} from "#context/dynamic-instruction-lifecycle.js";import { dispatchDynamicModelEvent } from "#context/dynamic-model-lifecycle.js";+import { preserveSerializedSessionDynamicModelSelection } from "#context/serialized-dynamic-model-selection.js";import { dispatchDynamicSkillEvent } from "#context/dynamic-skill-lifecycle.js";import {dispatchDynamicSubagentEvent,@@ -523,14 +524,18 @@ export async function turnStep(rawInput: TurnStepInput): Promise<DurableStepResuthrow error;}writer.releaseLock();- // Both carve-outs exist because the cancellation epilogue still publishes- // `turn.cancelled` downstream, and the open spans and provider state it- // needs to close were written by the step being discarded.+ // Trace and instrumentation state are needed by the cancellation+ // epilogue to close the operation the discarded step opened.+ // The session model is also kept because `session.started` is not emitted+ // again after this cancellation settles.const interrupted = serializeContext(ctx);return {action: "cancelled",serializedContext: preserveSerializedInstrumentationState(- preserveSerializedAgentTraceState(input.serializedContext, interrupted),+ preserveSerializedAgentTraceState(+ preserveSerializedSessionDynamicModelSelection(input.serializedContext, interrupted),+ interrupted,+ ),interrupted,),sessionState: input.sessionState,diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a07e33920..24e125948 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -639,6 +639,28 @@ importers:specifier: 'catalog:'version: 7.0.2+ e2e/fixtures/agent-dynamic-model-session:+ dependencies:+ '@eve-e2e/config':+ specifier: workspace:*+ version: link:../e2e-config+ '@workflow/world-postgres':+ specifier: 'catalog:'+ version: 5.0.0-beta.33(@opentelemetry/[email protected])(@types/[email protected])(@upstash/[email protected])([email protected])([email protected])([email protected])+ eve:+ specifier: workspace:*+ version: link:../../../packages/eve+ zod:+ specifier: 'catalog:'+ version: 4.4.3+ devDependencies:+ '@types/node':+ specifier: 'catalog:'+ version: 25.9.1+ typescript:+ specifier: 'catalog:'+ version: 7.0.2+e2e/fixtures/agent-model:dependencies:'@eve-e2e/config':@@ -7887,6 +7909,7 @@ packages:'@workflow/[email protected]':resolution: {integrity: sha512-yBuMdic/jCm24x+dszmWHiqOnGpD37o/4ySls8yFwCw7DxZU+nq7hUq7I4HJoYwybkBP6BhqN6ZUCWKouE4EFA==}+ hasBin: true'@workflow/[email protected]':resolution: {integrity: sha512-jfbWAZl/9D+bNPrqWCmJ6ohLA3M4+MXR1tagrmEpjGFPFH11f/1lRRARcn6Dwi1+27EQ7HwxeTTpE63BZftt9w==}