The Open Framework for Building Agents
fix(eve): restore turn connections before approval authorization (#3763)
OwenKephart83c5fe2dc9d2
diff --git a/.changeset/turn-connection-approval-restoration.md b/.changeset/turn-connection-approval-restoration.mdnew file mode 100644index 000000000..b4bd83a79--- /dev/null+++ b/.changeset/turn-connection-approval-restoration.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Restore turn-scoped dynamic connections before authorizing approval responses, including after a cold start. Connection response policies now run instead of failing because their callbacks are unavailable; missing tools or changed connection identities fail explicitly rather than replaying against a replacement connection.diff --git a/docs/connections/overview.mdx b/docs/connections/overview.mdxindex 6f22a42d4..4793cbc5c 100644--- a/docs/connections/overview.mdx+++ b/docs/connections/overview.mdx@@ -58,6 +58,28 @@ must also set `instanceKey` to a stable, non-secret account or tenant identifierSee [Dynamic capabilities](/docs/guides/dynamic-capabilities#dynamic-connections)for event scope, naming, conflicts, and durable recovery behavior.+Tools from a `turn.started` connection can require approval, including an+`approval.response` policy. When an approval resumes between turns, eve rebuilds+the connection callbacks before checking the response policy, even in a fresh+process. This restoration uses the pending request's turn coordinates and the+current authenticated session context; it does not emit another public+`turn.started` event. The response policy still receives the authenticated+responder separately. When one delivery answers requests from different turns,+eve restores each candidate's originating connection scope before its policy+check, then prepares the complete batch selected for tool replay. Keep connection+resolvers idempotent because restoration can run them again.++Restoration uses current resolver results, not a historical connection snapshot.+If the tool is absent from the persisted tool metadata, or its required response+policy is unavailable, eve refuses the response and leaves the request pending.+eve does not reconstruct missing tool metadata from earlier turns. Before replay, eve checks that the+approved tool is still available and that its connection identity has not+changed since the call requested approval. A missing or changed connection+fails the replay rather than sending the call to a replacement destination.+Request a new tool call and approval after correcting the connection. Identity+includes the endpoint and `instanceKey`; use a distinct `instanceKey` for each+account or tenant even when they share an endpoint.+## Static-token auth`getToken` returns a `TokenResult` (`{ token, expiresAt? }`), and eve sends it as `Authorization: Bearer <token>` on every request. Because it runs on each connection attempt, you can mint a fresh token from wherever you keep secrets, including an env var, a secrets manager, an internal vault, or your own OAuth exchange:diff --git a/packages/eve/src/execution/dynamic-connections-approval.test.ts b/packages/eve/src/execution/dynamic-connections-approval.test.tsnew file mode 100644index 000000000..010b45713--- /dev/null+++ b/packages/eve/src/execution/dynamic-connections-approval.test.ts@@ -0,0 +1,83 @@+import { describe, expect, it, vi } from "vitest";+import { ContextContainer } from "#context/container.js";+import { AuthKey, InitiatorAuthKey, SessionIdKey } from "#context/keys.js";+import { ConnectionRegistryKey } from "#context/providers/connection-key.js";+import { bindDynamicConnections } from "#execution/dynamic-connections.js";+import { defineMcpClientConnection } from "#public/definitions/connections/mcp.js";+import { ConnectionRegistryImpl } from "#runtime/connections/registry.js";++const alice = {+ attributes: {},+ authenticator: "test",+ principalId: "alice",+ principalType: "user" as const,+};+const bob = { ...alice, principalId: "bob" };+const state = { sequence: 8, sessionStarted: true, stepIndex: 0, turnId: "" };+const runtime = { agentId: "test", eveVersion: "test" };+const origin = { sequence: 2, stepIndex: 1, turnId: "turn_2" };++function setup() {+ const ctx = new ContextContainer();+ ctx.set(AuthKey, bob);+ ctx.set(InitiatorAuthKey, alice);+ ctx.set(SessionIdKey, "test");+ const registry = new ConnectionRegistryImpl([]);+ ctx.set(ConnectionRegistryKey, registry);+ const turn = vi.fn(() => ({+ notes: defineMcpClientConnection({ description: "Notes", url: "https://notes.example/mcp" }),+ }));+ const session = vi.fn(() => null);+ const lifecycle = bindDynamicConnections(ctx, {+ dynamicConnectionResolvers: [+ {+ slug: "notes",+ sourceId: "notes",+ sourceKind: "module",+ logicalPath: "connections/notes.ts",+ eventNames: ["session.started", "turn.started"],+ events: { "session.started": session, "turn.started": turn },+ },+ ],+ });+ return { ctx, lifecycle, registry, session, turn };+}++describe("approval-only connection restoration", () => {+ it("uses the matching parked turn, not the upcoming turn, without changing auth or emission state", async () => {+ const fixture = setup();+ const before = structuredClone(state);+ await fixture.lifecycle.rehydrate(state, runtime, origin);+ expect(fixture.turn).toHaveBeenCalledWith(+ expect.objectContaining({+ type: "turn.started",+ data: expect.objectContaining({ sequence: 2, turnId: "turn_2" }),+ }),+ expect.objectContaining({+ session: { id: "test", auth: { current: bob, initiator: alice } },+ }),+ );+ expect(fixture.registry.getConnectionNames()).toEqual(["notes"]);+ expect(fixture.ctx.get(AuthKey)).toEqual(bob);+ expect(state).toEqual(before);+ });++ it("keeps ordinary active-turn restoration unchanged", async () => {+ const fixture = setup();+ await fixture.lifecycle.rehydrate({ ...state, turnId: "turn_8" }, runtime, {+ sequence: 8,+ turnId: "turn_8",+ });+ expect(fixture.turn).toHaveBeenCalledWith(+ expect.objectContaining({ data: expect.objectContaining({ sequence: 8, turnId: "turn_8" }) }),+ expect.anything(),+ );+ });++ it("does not resolve any connections before session start", async () => {+ const fixture = setup();+ await fixture.lifecycle.rehydrate({ ...state, sessionStarted: false }, runtime);+ expect(fixture.session).not.toHaveBeenCalled();+ expect(fixture.turn).not.toHaveBeenCalled();+ });+});diff --git a/packages/eve/src/execution/dynamic-connections.test.ts b/packages/eve/src/execution/dynamic-connections.test.tsindex e6c772167..e9c2c9275 100644--- a/packages/eve/src/execution/dynamic-connections.test.ts+++ b/packages/eve/src/execution/dynamic-connections.test.ts@@ -28,7 +28,7 @@ describe("bindDynamicConnections", () => {await lifecycle.rehydrate({ sequence: 3, sessionStarted: true, stepIndex: 1, turnId: "turn_3" },{ agentId: "agent", eveVersion: "test" },- false,+ { sequence: 3, turnId: "turn_3" },);expect(seen).toEqual(["session.started", "turn.started"]);@@ -54,7 +54,7 @@ describe("bindDynamicConnections", () => {await lifecycle.rehydrate({ sequence: 4, sessionStarted: true, stepIndex: 0, turnId: "" },{ agentId: "agent", eveVersion: "test" },- true,+ undefined,);expect(seen).toEqual(["session.started"]);diff --git a/packages/eve/src/execution/dynamic-connections.ts b/packages/eve/src/execution/dynamic-connections.tsindex c0873a241..8f6f99657 100644--- a/packages/eve/src/execution/dynamic-connections.ts+++ b/packages/eve/src/execution/dynamic-connections.ts@@ -1,6 +1,5 @@import type { ContextContainer } from "#context/container.js";import { dispatchDynamicConnectionEvent } from "#context/dynamic-connection-lifecycle.js";-import { activeTurnId } from "#harness/active-turn-id.js";import type { HarnessEmissionState } from "#harness/emission.js";import {createSessionStartedEvent,@@ -24,14 +23,11 @@ export function bindDynamicConnections(async rehydrate(state: HarnessEmissionState,runtime: RuntimeIdentity,- betweenTurns: boolean,+ turn?: { readonly sequence: number; readonly turnId: string },): Promise<void> {if (!state.sessionStarted) return;await dispatch(createSessionStartedEvent({ runtime }));- if (betweenTurns) return;- await dispatch(- createTurnStartedEvent({ sequence: state.sequence, turnId: activeTurnId(state) }),- );+ if (turn !== undefined) await dispatch(createTurnStartedEvent(turn));},};}diff --git a/packages/eve/src/execution/node-step.ts b/packages/eve/src/execution/node-step.tsindex ee2f32de2..ce27cfad7 100644--- a/packages/eve/src/execution/node-step.ts+++ b/packages/eve/src/execution/node-step.ts@@ -25,6 +25,7 @@ import type { PreparedRuntimeTool, PreparedRuntimeWorkflowTask } from "#runtime/import { findRegisteredRuntimeTool } from "#runtime/tools/registry.js";import type { ResolvedToolDefinition } from "#runtime/types.js";import { createToolExecuteWithAuth } from "#execution/tool-auth.js";+import { connectionToolReplayIdentity } from "#execution/tools/connection-search.js";import {createPreparedWorkflowToolHarnessDefinition,createWorkflowToolHarnessDefinition,@@ -69,6 +70,10 @@ export interface CreateExecutionNodeStepInput {*/readonly createRuntime: CreateRuntime;readonly handleEvent?: HandleEventFn;+ readonly prepareApprovalTurn?: (event: {+ readonly sequence: number;+ readonly turnId: string;+ }) => Promise<void>;readonly historyProjector?: HistoryViewProjector;readonly historyView?: PreparedHistoryView;readonly instrumentation: ExecutionInstrumentation | undefined;@@ -105,6 +110,8 @@ export function createExecutionNodeStep(input: CreateExecutionNodeStepInput): SthistoryView: input.historyView,instrumentation: sessionInstrumentation,mode: input.mode,+ prepareApprovalTurn: input.prepareApprovalTurn,+ toolReplayIdentity: connectionToolReplayIdentity,resolveStepDynamicTools: (resolveInput) =>preparePersistedStepDynamicToolMetadata({...resolveInput,diff --git a/packages/eve/src/execution/session/turn-connection-approval.integration.test.ts b/packages/eve/src/execution/session/turn-connection-approval.integration.test.tsnew file mode 100644index 000000000..08af73e0b--- /dev/null+++ b/packages/eve/src/execution/session/turn-connection-approval.integration.test.ts@@ -0,0 +1,664 @@+import { simulateReadableStream } from "ai";+import { MockLanguageModelV4 } from "ai/test";+import { afterEach, describe, expect, it, vi } from "vitest";+import { ContextContainer } from "#context/container.js";+import { AuthKey, InitiatorAuthKey, ModeKey, SessionIdKey } from "#context/keys.js";+import { serializeContext } from "#context/serialize.js";+import { createDurableSessionState, readDurableSession } from "#execution/durable-session-store.js";+import { turnStep } from "#execution/session/turn-step.js";+import type { DurableStepResult, TurnStepPayload } from "#execution/session/turn-step-types.js";+import {+ getApprovalAuditState,+ markApprovalCandidateAuthorizationRequired,+} from "#harness/approval-candidates.js";+import { setPendingAuthorization } from "#harness/authorization.js";+import { getPendingInputBatches } from "#harness/pending-input-batches.js";+import type { HarnessSession } from "#harness/types.js";+import { defineOpenAPIConnection } from "#public/definitions/connections/openapi.js";+import { getCompiledRuntimeAgentBundle } from "#runtime/sessions/compiled-agent-cache.js";+import {+ BundleKey,+ ChannelKey,+ type CompiledBundle,+} from "#runtime/sessions/runtime-context-keys.js";+import { createEmptyHookRegistry } from "#runtime/hooks/registry.js";+import { resolveRuntimeModelReference } from "#runtime/agent/resolve-model.js";+import type {+ ResolvedDynamicConnectionResolver,+ ResolvedDynamicToolResolver,+} from "#runtime/types.js";+import connectionSearch from "#tools/framework/connection-search.js";+import { clearDurableDynamicCallbacks } from "#tools/durable-callbacks.js";+import type { ApprovalResponseContext } from "#approval/definition.js";++vi.mock("#runtime/sessions/compiled-agent-cache.js", () => ({+ getCompiledRuntimeAgentBundle: vi.fn(),+}));+vi.mock("#runtime/agent/resolve-model.js", async (importOriginal) => ({+ ...(await importOriginal<typeof import("#runtime/agent/resolve-model.js")>()),+ resolveRuntimeModelReference: vi.fn(),+}));++const alice = {+ attributes: {},+ authenticator: "test",+ principalId: "alice",+ principalType: "user" as const,+};+const bob = { ...alice, principalId: "bob" };+const usage = {+ inputTokens: { cacheRead: undefined, cacheWrite: undefined, noCache: 1, total: 1 },+ outputTokens: { reasoning: undefined, text: 1, total: 1 },+};+const sessionId = "turn-connection-approval";++function modelResponse(toolName?: string, callId?: string, connection = "notes") {+ return {+ stream: simulateReadableStream({+ chunks: [+ { type: "stream-start" as const, warnings: [] },+ ...(toolName+ ? [+ {+ type: "tool-call" as const,+ toolCallId: callId ?? (toolName === "connection_search" ? "search" : "save"),+ toolName,+ input: JSON.stringify(+ toolName === "connection_search"+ ? { connection, keywords: "save" }+ : { body: { note: "hello" } },+ ),+ },+ ]+ : [+ { type: "text-start" as const, id: "reply" },+ { type: "text-delta" as const, id: "reply", delta: "Saved." },+ { type: "text-end" as const, id: "reply" },+ ]),+ {+ type: "finish" as const,+ finishReason: {+ raw: undefined,+ unified: toolName ? ("tool-calls" as const) : ("stop" as const),+ },+ usage,+ },+ ],+ }),+ };+}++function setup(+ scope: "turn.started" | "session.started" = "turn.started",+ reject = false,+ variation?: "destination" | "name" | "request-only",+) {+ const response = vi.fn((context: ApprovalResponseContext) => {+ expect(context.responder.principalId).toBe("bob");+ expect(context.session.initiator?.principalId).toBe("alice");+ return reject+ ? { status: "rejected" as const, reason: "Only the notes owner can approve." }+ : { status: "allowed" as const };+ });+ const policyTurns: string[] = [];+ let removed = false;+ const resolver = vi.fn((event: unknown) => {+ if (removed) return {};+ const sequence = (event as { data: { sequence?: number } }).data.sequence ?? 0;+ return {+ [variation === "name" && sequence > 0 ? "second-notes" : "notes"]: defineOpenAPIConnection({+ baseUrl:+ variation === "destination"+ ? `https://notes-${sequence}.example.com`+ : "https://notes.example.com",+ instanceKey: variation === "destination" ? `turn-${sequence}` : undefined,+ description: "Save notes",+ spec: {+ openapi: "3.0.0",+ info: { title: "Notes", version: "1.0.0" },+ paths: {+ "/notes": {+ post: {+ operationId: "saveNote",+ summary: "Save a note",+ requestBody: {+ required: true,+ content: {+ "application/json": {+ schema: {+ type: "object",+ properties: { note: { type: "string" } },+ required: ["note"],+ },+ },+ },+ },+ responses: { 200: { description: "Saved" } },+ },+ },+ },+ },+ approval: {+ request: () => "user-approval",+ response:+ variation === "request-only"+ ? undefined+ : (context) => {+ policyTurns.push(+ (event as { data: { turnId?: string } }).data.turnId ?? "session",+ );+ return response(context);+ },+ },+ }),+ };+ });+ const dynamicConnectionResolvers: ResolvedDynamicConnectionResolver[] = [+ {+ eventNames: [scope],+ events: { [scope]: resolver },+ logicalPath: "connections/notes.ts",+ slug: "notes",+ sourceId: "notes",+ sourceKind: "module",+ },+ ];+ const dynamicToolResolvers: ResolvedDynamicToolResolver[] = [+ {+ eventNames: ["step.started"],+ events: connectionSearch.events as ResolvedDynamicToolResolver["events"],+ logicalPath: "tools/connection-search.ts",+ slug: "connection-search",+ sourceId: "eve:connection-search",+ sourceKind: "module",+ },+ ];+ const adapter = { kind: "test" };+ const turnAgent = {+ id: "notes-agent",+ instructions: ["Save notes."],+ model: { id: "test" },+ skills: [],+ tools: [],+ workspaceSpec: { rootEntries: [] },+ };+ const resolvedAgent: Partial<CompiledBundle["resolvedAgent"]> = {+ connections: [],+ dynamicConnectionResolvers,+ dynamicToolResolvers,+ };+ const sandboxRegistry: {+ sandbox: CompiledBundle["graph"]["root"]["sandboxRegistry"]["sandbox"] | null;+ } = { sandbox: null };+ const bundle = {+ adapterRegistry: { adaptersByKind: new Map([[adapter.kind, adapter]]) },+ compiledArtifactsSource: { kind: "bundled" },+ graph: {+ nodesByNodeId: new Map(),+ root: {+ agent: resolvedAgent as CompiledBundle["resolvedAgent"],+ sandboxRegistry: sandboxRegistry as CompiledBundle["graph"]["root"]["sandboxRegistry"],+ turnAgent,+ channels: [],+ hookRegistry: createEmptyHookRegistry(),+ nodeId: "__root__",+ subagentRegistry: {+ dynamicNodeIds: new Set(),+ dynamicResolvers: [],+ preparedTools: [],+ subagentsByName: new Map(),+ subagentsByNodeId: new Map(),+ },+ toolRegistry: { preparedTools: [], toolsByName: new Map() },+ },+ },+ hookRegistry: createEmptyHookRegistry(),+ moduleMap: { nodes: {} },+ resolvedAgent: resolvedAgent as CompiledBundle["resolvedAgent"],+ subagentRegistry: {+ dynamicNodeIds: new Set(),+ dynamicResolvers: [],+ preparedTools: [],+ subagentsByName: new Map(),+ subagentsByNodeId: new Map(),+ },+ toolRegistry: { preparedTools: [], toolsByName: new Map() },+ turnAgent,+ } as CompiledBundle;+ vi.mocked(getCompiledRuntimeAgentBundle).mockResolvedValue(bundle);+ const doStream = vi+ .fn()+ .mockImplementationOnce(() => modelResponse("connection_search"))+ .mockImplementationOnce(() => modelResponse("notes__saveNote"))+ .mockImplementation(() => modelResponse());+ vi.mocked(resolveRuntimeModelReference).mockResolvedValue(new MockLanguageModelV4({ doStream }));+ const fetch = vi.fn(+ async () =>+ new Response(JSON.stringify({ saved: true }), {+ headers: { "content-type": "application/json" },+ }),+ );+ vi.stubGlobal("fetch", fetch);+ const ctx = new ContextContainer();+ ctx.set(AuthKey, alice);+ ctx.set(InitiatorAuthKey, alice);+ ctx.set(BundleKey, bundle);+ ctx.set(ChannelKey, adapter);+ ctx.set(ModeKey, "conversation");+ ctx.set(SessionIdKey, sessionId);+ const session: HarnessSession = {+ agent: { modelReference: { id: "test" }, system: "Save notes.", tools: [] },+ compaction: { recentWindowSize: 10, threshold: 100_000 },+ continuationToken: "notes-session",+ history: [],+ sessionId,+ };+ let snapshot = {+ serializedContext: serializeContext(ctx),+ sessionState: createDurableSessionState({ session }),+ };+ const events: Array<{ type: string; data: Record<string, unknown> }> = [];+ async function step(input?: TurnStepPayload): Promise<DurableStepResult> {+ const result = await turnStep({+ ...snapshot,+ input,+ sessionWritable: new WritableStream<Uint8Array>({+ write(chunk) {+ const text = new TextDecoder().decode(chunk);+ for (const line of text.split("\n")) {+ if (line.trim()) events.push(JSON.parse(line));+ }+ },+ }),+ });+ snapshot = result;+ return result;+ }+ return {+ doStream,+ events,+ fetch,+ resolver,+ response,+ step,+ policyTurns,+ removeConnection() {+ removed = true;+ },+ updateSession(update: (session: HarnessSession) => HarnessSession) {+ snapshot = {+ ...snapshot,+ sessionState: createDurableSessionState({+ session: update({+ ...session,+ ...readDurableSession(snapshot.sessionState),+ agent: session.agent,+ compaction: session.compaction,+ }),+ }),+ };+ },+ };+}++afterEach(() => {+ clearDurableDynamicCallbacks(sessionId);+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+});++describe("turn connection approval restoration", () => {+ it("authorizes requests from two originating turns independently in one cold delivery", async () => {+ const fixture = setup();+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's first note." }] },+ });+ const first = await fixture.step();+ const firstBatch = getPendingInputBatches(readDurableSession(first.sessionState).state)[0]!;+ fixture.doStream+ .mockImplementationOnce(() => modelResponse("connection_search", "search-2"))+ .mockImplementationOnce(() => modelResponse("notes__saveNote", "save-2"));+ await fixture.step({+ delivery: {+ kind: "deliver",+ payloads: [{ message: "Prepare Alice's second independent note." }],+ },+ });+ const second = await fixture.step();+ const batches = getPendingInputBatches(readDurableSession(second.sessionState).state);+ expect(batches).toHaveLength(2);+ expect(batches[1]!.event!.turnId).not.toBe(firstBatch.event!.turnId);+ clearDurableDynamicCallbacks(sessionId);+ await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [+ {+ inputResponses: batches.flatMap((batch) =>+ batch.requests.map((request) => ({+ requestId: request.requestId,+ optionId: "approve",+ })),+ ),+ },+ ],+ },+ });+ clearDurableDynamicCallbacks(sessionId);+ const resumed = await fixture.step();+ expect(fixture.policyTurns).toEqual(batches.map((batch) => batch.event!.turnId));+ expect(fixture.response).toHaveBeenCalledTimes(2);+ expect(+ getApprovalAuditState(readDurableSession(resumed.sessionState).state).settlements,+ ).toHaveLength(2);+ expect(fixture.fetch).toHaveBeenCalled();+ expect(fixture.events.filter((event) => event.type === "session.failed")).toEqual([]);+ });+ it.each([false, true])(+ "fails before execution when the resumed connection changes (cold: %s)",+ async (cold) => {+ const fixture = setup("turn.started", false, "destination");+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's note." }] },+ });+ const parked = await fixture.step();+ const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .requests[0]!;+ if (cold) clearDurableDynamicCallbacks(sessionId);+ await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ });+ if (cold) clearDurableDynamicCallbacks(sessionId);+ await expect(fixture.step()).rejects.toThrow(+ "connection for this tool call changed or is unavailable",+ );+ expect(fixture.response).toHaveBeenCalledOnce();+ expect(fixture.fetch).not.toHaveBeenCalled();+ },+ );++ it.each([false, true])(+ "refuses an older tool missing from persisted metadata while approving an available tool (cold: %s)",+ async (cold) => {+ const fixture = setup("turn.started", false, "name");+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's first note." }] },+ });+ await fixture.step();+ fixture.doStream+ .mockImplementationOnce(() =>+ modelResponse("connection_search", "search-2", "second-notes"),+ )+ .mockImplementationOnce(() => modelResponse("second-notes__saveNote", "save-2"));+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's second note." }] },+ });+ const second = await fixture.step();+ const batches = getPendingInputBatches(readDurableSession(second.sessionState).state);+ expect(batches).toHaveLength(2);+ expect(batches.map((batch) => batch.requests[0]!.action.toolName)).toEqual([+ "notes__saveNote",+ "second-notes__saveNote",+ ]);+ if (cold) clearDurableDynamicCallbacks(sessionId);+ await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [+ {+ inputResponses: batches.flatMap((batch) =>+ batch.requests.map((request) => ({+ requestId: request.requestId,+ optionId: "approve",+ })),+ ),+ },+ ],+ },+ });+ if (cold) clearDurableDynamicCallbacks(sessionId);+ const resumed = await fixture.step();+ const state = readDurableSession(resumed.sessionState).state;+ const older = batches[0]!.requests[0]!;+ const newer = batches[1]!.requests[0]!;+ expect(fixture.policyTurns).toEqual([batches[1]!.event!.turnId]);+ expect(getApprovalAuditState(state).candidateHistory).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ requestId: older.requestId,+ status: "failed",+ reason: "Approval authorization is temporarily unavailable. Please try again.",+ }),+ ]),+ );+ expect(getApprovalAuditState(state).settlements).toEqual([+ expect.objectContaining({ requestId: newer.requestId, outcome: "allowed" }),+ ]);+ expect(getPendingInputBatches(state).flatMap((batch) => batch.requests)).toEqual([older]);+ expect(getPendingInputBatches(state)[0]!.toolReplayIdentities).toEqual(+ batches[0]!.toolReplayIdentities,+ );+ expect(fixture.fetch).toHaveBeenCalledOnce();+ },+ );++ it("rejects a missing connection before replay even without a response policy", async () => {+ const fixture = setup("turn.started", false, "request-only");+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's note." }] },+ });+ const parked = await fixture.step();+ const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .requests[0]!;+ fixture.removeConnection();+ await expect(+ fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ }),+ ).rejects.toThrow("connection for this tool call changed or is unavailable");+ expect(fixture.fetch).not.toHaveBeenCalled();+ });++ it("leaves the request pending when its connection can no longer be reconstructed", async () => {+ const fixture = setup();+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's note." }] },+ });+ const parked = await fixture.step();+ const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .requests[0]!;+ fixture.removeConnection();+ await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ });+ clearDurableDynamicCallbacks(sessionId);+ const refused = await fixture.step();+ expect(fixture.response).not.toHaveBeenCalled();+ expect(fixture.fetch).not.toHaveBeenCalled();+ const state = readDurableSession(refused.sessionState).state;+ expect(getPendingInputBatches(state)[0]!.requests[0]!.requestId).toBe(request.requestId);+ expect(getApprovalAuditState(state).candidateHistory).toEqual([+ expect.objectContaining({+ status: "failed",+ reason: "Approval authorization is temporarily unavailable. Please try again.",+ }),+ ]);+ });++ it("restores the originating connection for a sign-in callback without a premature turn", async () => {+ const fixture = setup();+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Prepare Alice's note for Bob." }] },+ });+ const parked = await fixture.step();+ const batch = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!;+ const request = batch.requests[0]!;+ const ingested = await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ });+ const candidate = getApprovalAuditState(readDurableSession(ingested.sessionState).state)+ .activeCandidates[0]!;+ const challenges = [+ {+ attemptId: "sign-in-1",+ candidateId: candidate.candidateId,+ name: "notes",+ principal: { type: "user" as const, id: "bob" },+ hookUrl: "https://example.com/callback",+ challenge: { url: "https://example.com/sign-in" },+ },+ ];+ fixture.updateSession((session) => ({+ ...session,+ state: setPendingAuthorization(+ markApprovalCandidateAuthorizationRequired({+ state: session.state,+ candidateId: candidate.candidateId,+ authorizationChallenges: challenges,+ }),+ { challenges },+ ),+ }));+ clearDurableDynamicCallbacks(sessionId);+ const start = fixture.events.length;+ await fixture.step({+ delivery: {+ kind: "deliver",+ payloads: [+ {+ authorizationCallback: {+ connectionName: "notes",+ attemptId: "sign-in-1",+ callback: { params: { code: "approved" } },+ },+ },+ ],+ },+ });+ expect(fixture.policyTurns).toEqual([batch.event!.turnId]);+ expect(fixture.response).toHaveBeenCalledOnce();+ expect(fixture.fetch).toHaveBeenCalledOnce();+ const events = fixture.events.slice(start);+ expect(events.findIndex((event) => event.type === "authorization.completed")).toBeLessThan(+ events.findIndex((event) => event.type === "turn.started"),+ );+ expect(events.filter((event) => event.type === "turn.started")).toHaveLength(1);+ });++ it.each([false, true])(+ "approves and executes a discovered tool with a cold callback cache: %s",+ async (cold) => {+ const fixture = setup();+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Save hello in notes." }] },+ });+ const parked = await fixture.step();+ const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .requests[0]!;+ expect(request.action.toolName).toBe("notes__saveNote");+ expect(+ getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .toolReplayIdentities,+ ).toEqual({+ [request.requestId]: expect.stringMatching(/^connection:/),+ });+ expect(fixture.fetch).not.toHaveBeenCalled();+ if (cold) clearDurableDynamicCallbacks(sessionId);+ const candidate = await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ });+ expect(+ getApprovalAuditState(readDurableSession(candidate.sessionState).state).activeCandidates,+ ).toHaveLength(1);+ if (cold) clearDurableDynamicCallbacks(sessionId);+ const resumed = await fixture.step();+ expect(fixture.response).toHaveBeenCalledOnce();+ expect(+ getApprovalAuditState(readDurableSession(resumed.sessionState).state).settlements,+ ).toEqual([expect.objectContaining({ outcome: "allowed", requestId: request.requestId })]);+ expect(fixture.fetch).toHaveBeenCalledOnce();+ expect(getPendingInputBatches(readDurableSession(resumed.sessionState).state)).toEqual([]);+ expect(resumed.serializedContext).not.toHaveProperty("eve.pendingConnectionCalls");+ expect(fixture.events.filter((event) => event.type === "turn.started")).toHaveLength(2);+ expect(fixture.events).toContainEqual(+ expect.objectContaining({+ type: "action.result",+ data: expect.objectContaining({+ status: "completed",+ result: expect.objectContaining({ toolName: "notes__saveNote" }),+ }),+ }),+ );+ },+ );++ it("preserves the author's rejection reason after a cold continuation", async () => {+ const fixture = setup("turn.started", true);+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Save hello in notes." }] },+ });+ const parked = await fixture.step();+ const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .requests[0]!;+ await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ });+ clearDurableDynamicCallbacks(sessionId);+ const rejected = await fixture.step();+ expect(fixture.response).toHaveBeenCalledOnce();+ expect(+ getApprovalAuditState(readDurableSession(rejected.sessionState).state).candidateHistory,+ ).toEqual([+ expect.objectContaining({ status: "rejected", reason: "Only the notes owner can approve." }),+ ]);+ expect(fixture.fetch).not.toHaveBeenCalled();+ expect(fixture.events.filter((event) => event.type === "turn.started")).toHaveLength(1);+ });++ it("preserves session-scoped connection approval", async () => {+ const fixture = setup("session.started");+ await fixture.step({+ delivery: { kind: "deliver", payloads: [{ message: "Save hello in notes." }] },+ });+ const parked = await fixture.step();+ const request = getPendingInputBatches(readDurableSession(parked.sessionState).state)[0]!+ .requests[0]!;+ await fixture.step({+ delivery: {+ kind: "deliver",+ auth: bob,+ payloads: [{ inputResponses: [{ requestId: request.requestId, optionId: "approve" }] }],+ },+ });+ clearDurableDynamicCallbacks(sessionId);+ await fixture.step();+ expect(fixture.response).toHaveBeenCalledOnce();+ expect(fixture.fetch).toHaveBeenCalledOnce();+ });+});diff --git a/packages/eve/src/execution/session/turn-step.ts b/packages/eve/src/execution/session/turn-step.tsindex ea2ff56fa..27b7f812e 100644--- a/packages/eve/src/execution/session/turn-step.ts+++ b/packages/eve/src/execution/session/turn-step.ts@@ -476,6 +476,7 @@ async function runSessionStep(input: TurnStepInput): Promise<DurableStepResult>compactOnly: input.input?.control === "compact",createRuntime: createWorkflowRuntime,handleEvent,+ prepareApprovalTurn: (event) => dynamicConnections.dispatch(createTurnStartedEvent(event)),historyProjector: history.projector,historyView: history.prepare(modelSession),instrumentation,@@ -513,14 +514,20 @@ async function runSessionStep(input: TurnStepInput): Promise<DurableStepResult>session: enrichedSession,}): enrichedSession;+ const connectionState = getHarnessEmissionState(schemaSession.state);await dynamicConnections.rehydrate(- getHarnessEmissionState(schemaSession.state),+ connectionState,runtimeIdentity,- isHarnessBetweenTurns(schemaSession),+ isHarnessBetweenTurns(schemaSession)+ ? undefined+ : { sequence: connectionState.sequence, turnId: activeTurnId(connectionState) },);if (firstCall && completedAuths) {let emissionState = getHarnessEmissionState(schemaSession.state);- if (isHarnessBetweenTurns(schemaSession)) {+ const startsTurn = completedAuths.some(+ ({ candidateId }) => candidateId === undefined,+ );+ if (startsTurn && isHarnessBetweenTurns(schemaSession)) {const turnInput = createTurnInputMessages(consumeDeferredStepInput({ session: schemaSession, input: stepInput }).input,);@@ -562,10 +569,7 @@ async function runSessionStep(input: TurnStepInput): Promise<DurableStepResult>}schemaSession = setHarnessEmissionState(schemaSession, emissionState);}- for (const { authorization, result } of completedAuths) {- const candidateId = pendingAuth?.challenges.find(- (challenge) => challenge.attemptId === result.attemptId,- )?.candidateId;+ for (const { authorization, result, candidateId } of completedAuths) {await handleEvent(createAuthorizationCompletedEvent({attemptId: result.attemptId,diff --git a/packages/eve/src/execution/tools/connection-search.test.ts b/packages/eve/src/execution/tools/connection-search.test.tsindex 7b57e6bf1..dc3f348cb 100644--- a/packages/eve/src/execution/tools/connection-search.test.ts+++ b/packages/eve/src/execution/tools/connection-search.test.ts@@ -199,6 +199,47 @@ describe("connection dynamic tools", () => {expect(Object.values(tools).every(isBrandedToolEntry)).toBe(true);});+ it("keeps the connection identity in durable callback closures without separate call state", async () => {+ let instanceId = "account-a";+ const executeTool = vi.fn(async () => ({ ok: true }));+ const base = registry({+ connections: [{ ...connection("linear"), instanceId }],+ loadTools: {+ linear: async () => [+ { name: "list_issues", description: "List issues", inputSchema: { type: "object" } },+ ],+ },+ });+ const ctx = new ContextContainer();+ ctx.set(ConnectionRegistryKey, {+ ...base,+ getConnections: () => [{ ...connection("linear"), instanceId }],+ getClient: (name) => ({ ...base.getClient(name), executeTool }),+ });+ await contextStorage.run(ctx, async () => {+ const resolve = getConnectionSearchResolver().events["step.started"]!;+ const context = {+ channel: {},+ model: null,+ messages: [],+ session: { auth: { current: null, initiator: null }, id: "identity-test" },+ } satisfies DynamicResolveContext;+ const initial = (await resolve({}, context)) as DynamicToolSet;+ await initial["connection_search"]!.execute({ keywords: "list issues" }, {} as ToolContext);+ const tools = (await resolve({}, context)) as DynamicToolSet;+ const callback = readDurableDynamicToolCallbacks(tools["linear__list_issues"]!)!.execute!;+ expect(callback.closure).toMatchObject({ connectionName: "linear", instanceId: "account-a" });+ instanceId = "account-b";+ await expect(+ callback.callback(callback.closure, {} as never, { callId: "call-1" } as never),+ ).rejects.toThrow("connection for this tool call changed or is unavailable");+ expect(executeTool).not.toHaveBeenCalled();+ expect([...ctx.entries()].some(([key]) => key.name === "eve.pendingConnectionCalls")).toBe(+ false,+ );+ });+ });+it("forwards the authored tool call ID to connection execution", async () => {const linear = connection("linear");const executeTool = vi.fn(diff --git a/packages/eve/src/execution/tools/connection-search.ts b/packages/eve/src/execution/tools/connection-search.tsindex ba410a584..7dcbd84e6 100644--- a/packages/eve/src/execution/tools/connection-search.ts+++ b/packages/eve/src/execution/tools/connection-search.ts@@ -318,6 +318,32 @@ async function executeConnectionSearch(return summaries;}+export function connectionToolReplayIdentity(toolName: string): string | undefined {+ const ctx = loadContext();+ const discovered = ctx+ .get(ConnectionSearchResultsKey)+ ?.find((entry) => entry.qualifiedName === toolName);+ if (discovered === undefined) return;+ return ctx+ .get(ConnectionRegistryKey)+ ?.getConnections()+ .find((connection) => connection.connectionName === discovered.connection)?.instanceId;+}++function assertConnectionToolInstance(closure: JsonObject): void {+ if (typeof closure.instanceId !== "string") return;+ const { connectionName } = readDiscoveredToolClosure(closure);+ const connection = loadContext()+ .get(ConnectionRegistryKey)+ ?.getConnections()+ .find((entry) => entry.connectionName === connectionName);+ if (connection?.instanceId !== closure.instanceId) {+ throw new Error(+ "The connection for this tool call changed or is unavailable. Request a new tool call and approval.",+ );+ }+}+function readDiscoveredToolClosure(closure: JsonObject): {readonly connectionName: string;readonly toolName: string;@@ -340,6 +366,7 @@ async function executeDiscoveredConnectionTool(if (registry === undefined) {throw new Error("Connection registry is unavailable while replaying a discovered tool.");}+ assertConnectionToolInstance(closure);assertPendingConnectionAuthorizationInstances(registry);const scoped = await resolveInteractiveAuth(registry, connectionName);const auth = createAuthorizationExecution();@@ -373,6 +400,7 @@ async function requestDiscoveredConnectionToolApproval(context: ApprovalContext,) {const { connectionName } = readDiscoveredToolClosure(closure);+ assertConnectionToolInstance(closure);const approval = loadContext().get(ConnectionRegistryKey)?.getConnectionApproval(connectionName);return approval === undefined ? "not-applicable" : await resolveApprovalPolicy(approval)(context);}@@ -382,6 +410,7 @@ async function authorizeDiscoveredConnectionToolApproval(context: ApprovalResponseContext,) {const { connectionName } = readDiscoveredToolClosure(closure);+ assertConnectionToolInstance(closure);const approval = loadContext().get(ConnectionRegistryKey)?.getConnectionApproval(connectionName);const response =approval === undefined || typeof approval === "function" ? undefined : approval.response;@@ -430,7 +459,14 @@ export async function resolveConnectionSearchDynamicTools() {const toolName = result.tool!;const approval = registry.getConnectionApproval(connectionName);- const closure = { connectionName, toolName };+ const instanceId = connections.find(+ (connection) => connection.connectionName === connectionName,+ )?.instanceId;+ const closure: { connectionName: string; toolName: string; instanceId?: string } = {+ connectionName,+ toolName,+ };+ if (instanceId !== undefined) closure.instanceId = instanceId;const discoveredTool = defineTool({description: result.description,inputSchema: (result.inputSchema ?? {diff --git a/packages/eve/src/harness/approval-delivery-coordinator.test.ts b/packages/eve/src/harness/approval-delivery-coordinator.test.tsindex 46e4de063..76cabd570 100644--- a/packages/eve/src/harness/approval-delivery-coordinator.test.ts+++ b/packages/eve/src/harness/approval-delivery-coordinator.test.ts@@ -11,10 +11,8 @@ import type { HarnessToolDefinition } from "#harness/execute-tool.js";import type { SessionAuthContext } from "#channel/types.js";import { settleDirectApprovalResponse } from "#harness/approval-candidates.js";-import {- coordinateApprovalDelivery,- shouldPrepareApprovalReplayTools,-} from "#harness/approval-delivery-coordinator.js";+import { coordinateApprovalDelivery } from "#harness/approval-delivery-coordinator.js";+import { selectApprovalReplayBatch } from "#harness/input-requests.js";import { appendPendingInputBatch, getPendingInputBatches } from "#harness/pending-input-batches.js";import type { HarnessSession } from "#harness/types.js";import type { InputRequest } from "#shared/input.js";@@ -281,6 +279,12 @@ describe("coordinateApprovalDelivery", () => {});describe("text approval replay preparation", () => {+ function shouldPrepareApprovalReplayTools(input: {+ session: HarnessSession;+ stepInput?: import("#harness/types.js").StepInput;+ }) {+ return selectApprovalReplayBatch(input.session, input.stepInput) !== undefined;+ }function sessionWithRequests(requests: InputRequest[] = [request],responseAuthRequiredRequestIds?: string[],diff --git a/packages/eve/src/harness/approval-delivery-coordinator.ts b/packages/eve/src/harness/approval-delivery-coordinator.tsindex 9339623f9..4173e074a 100644--- a/packages/eve/src/harness/approval-delivery-coordinator.ts+++ b/packages/eve/src/harness/approval-delivery-coordinator.ts@@ -1,4 +1,3 @@-import { resolveTextToResponses } from "#channel/resolve-text.js";import type { SessionAuthContext } from "#channel/types.js";import { buildCallbackContext } from "#context/build-callback-context.js";import { contextStorage } from "#context/container.js";@@ -66,89 +65,12 @@ export interface ApprovalDeliveryResult {* creation commit before long-running policy execution. Candidate results also* commit before lifecycle events are projected by the next stack layer.*/-/** Returns whether this invocation should prepare tools for persisted policy work. */-export function shouldPrepareApprovalPolicyTools(input: {- readonly now?: number;- readonly session: HarnessSession;- readonly stepInput?: StepInput;-}): boolean {- const batches = getPendingInputBatches(input.session.state);- const responses = [- ...(input.stepInput?.attributedInputResponses ?? []).map(({ response }) => response),- ...(input.stepInput?.inputResponses ?? []),- ];- if (- batches.some((batch) =>- batch.requests.some(- (request) =>- isApprovalRequest(request) &&- responses.some((response) => response.requestId === request.requestId),- ),- )- ) {- return false;- }-- const now = input.now ?? Date.now();- return getApprovalAuditState(input.session.state).activeCandidates.some(- (candidate) =>- candidate.expiresAt > now &&- (candidate.status === "pending" ||- (candidate.authorizationChallenges?.some(- (challenge) => getAuthorizationResult(challenge.name) !== undefined,- ) ??- false)),- );-}--/** Returns whether this invocation can replay a previously approved tool call. */-export function shouldPrepareApprovalReplayTools(input: {- readonly now?: number;- readonly session: HarnessSession;- readonly stepInput?: StepInput;-}): boolean {- if (shouldPrepareApprovalPolicyTools(input)) return true;-- const batches = getPendingInputBatches(input.session.state);- const responses = [- ...(input.stepInput?.attributedInputResponses ?? []).map(({ response }) => response),- ...(input.stepInput?.inputResponses ?? []),- ];- const batch = batches.length === 1 ? batches[0] : undefined;- if (- batch !== undefined &&- typeof input.stepInput?.message === "string" &&- !responses.some((response) =>- batch.requests.some((request) => request.requestId === response.requestId),- )- ) {- // Match the text-only approvals resolvePendingInput will consume after preparation.- responses.push(- ...resolveTextToResponses(- input.stepInput.message,- batch.requests.filter(- (request) => !batch.responseAuthRequiredRequestIds?.includes(request.requestId),- ),- ),- );- }- const approvedRequestIds = new Set(- responses- .filter((response) => response.optionId === "approve")- .map((response) => response.requestId),- );- return batches.some((batch) =>- batch.requests.some(- (request) => isApprovalRequest(request) && approvedRequestIds.has(request.requestId),- ),- );-}-export async function coordinateApprovalDelivery(input: {readonly now?: number;readonly session: HarnessSession;readonly stepInput?: StepInput;readonly tools: HarnessToolMap;+ readonly prepareTools?: (request: InputRequest) => Promise<HarnessToolMap>;}): Promise<ApprovalDeliveryResult> {const now = input.now ?? Date.now();const expiredCandidates = getApprovalAuditState(input.session.state).activeCandidates.filter(@@ -328,7 +250,7 @@ export async function coordinateApprovalDelivery(input: {request,responder: candidate.responder,session,- tools: input.tools,+ tools: (await input.prepareTools?.(request)) ?? input.tools,});session = processed.session;didCommit ||= processed.didCommit;diff --git a/packages/eve/src/harness/input-request-resolution.ts b/packages/eve/src/harness/input-request-resolution.tsindex 64559380e..02eddee9a 100644--- a/packages/eve/src/harness/input-request-resolution.ts+++ b/packages/eve/src/harness/input-request-resolution.ts@@ -14,6 +14,7 @@ export interface ResolvedInputBatch {readonly outcome: "answered" | ApprovalTerminalStatus;readonly request: InputRequest;readonly response?: InputResponse;+ readonly toolReplayIdentity?: string;}[];}@@ -21,6 +22,7 @@ export function buildResolvedInputBatch(batch: {readonly event?: PendingInputBatchEvent;readonly requests: readonly InputRequest[];+ readonly toolReplayIdentities?: Readonly<Record<string, string>>;},responses: readonly InputResponse[],): ResolvedInputBatch | undefined {@@ -38,6 +40,11 @@ export function buildResolvedInputBatch(: "answered",request,response,+ ...(batch.toolReplayIdentities?.[request.requestId] === undefined+ ? {}+ : {+ toolReplayIdentity: batch.toolReplayIdentities[request.requestId],+ }),};}),};diff --git a/packages/eve/src/harness/input-requests.ts b/packages/eve/src/harness/input-requests.tsindex 6db69db47..c2cf87b7e 100644--- a/packages/eve/src/harness/input-requests.ts+++ b/packages/eve/src/harness/input-requests.ts@@ -85,6 +85,28 @@ export function hasPendingApprovalBatch(session: HarnessSession): boolean {);}+/** Selects the complete approval batch that pending-input resolution will resume. */+export function selectApprovalReplayBatch(+ session: HarnessSession,+ stepInput?: StepInput,+): PendingInputBatch | undefined {+ const batches = getPendingInputBatches(session.state);+ if (batches.some(isSessionLimitInputBatch)) return;+ const resolved =+ batches.length === 1 ? resolveTextMessageInput(batches[0]!, stepInput) : stepInput;+ const responses = canonicalizeInputResponses(resolved?.inputResponses ?? []);+ const batch = findAnsweredApprovalBatches(batches, responses)[0];+ return batch?.requests.some(+ (request) =>+ isApprovalRequest(request) &&+ responses.some(+ (response) => response.requestId === request.requestId && response.optionId === "approve",+ ),+ )+ ? batch+ : undefined;+}+/*** Resolves pending input at the start of a harness step.*diff --git a/packages/eve/src/harness/issue-3494-adversarial.integration.test.ts b/packages/eve/src/harness/issue-3494-adversarial.integration.test.tsindex f1486beb6..ec1dbfe0b 100644--- a/packages/eve/src/harness/issue-3494-adversarial.integration.test.ts+++ b/packages/eve/src/harness/issue-3494-adversarial.integration.test.ts@@ -20,6 +20,10 @@ import type { HarnessToolDefinition } from "#harness/execute-tool.js";import type { HarnessSession, StepInput, StepResult } from "#harness/types.js";import type { UnstampedMessageStreamEvent } from "#protocol/message.js";import { always } from "#tools/approval/policies.js";+import { bindDynamicConnections } from "#execution/dynamic-connections.js";+import { ConnectionRegistryKey } from "#context/providers/connection-key.js";+import { ConnectionRegistryImpl } from "#runtime/connections/registry.js";+import { createTurnStartedEvent } from "#protocol/message.js";// Diagnostic probes assert desired outcomes. Pending state is created by the// real harness and SDK; only provider output and runtime result delivery are scripted.@@ -155,7 +159,31 @@ function fixture(throw new Error("Must dispatch through executor");},});+ const restoredTurns: string[] = [];const harness = createToolLoopHarness({+ prepareApprovalTurn: async (event) => {+ const ctx = new ContextContainer();+ ctx.set(ConnectionRegistryKey, new ConnectionRegistryImpl([]));+ await bindDynamicConnections(ctx, {+ dynamicConnectionResolvers: [+ {+ slug: "notes",+ sourceId: "notes",+ sourceKind: "module",+ logicalPath: "connections/notes.ts",+ eventNames: ["turn.started"],+ events: {+ "turn.started": (event) => {+ restoredTurns.push(+ (event as ReturnType<typeof createTurnStartedEvent>).data.turnId,+ );+ return null;+ },+ },+ },+ ],+ }).dispatch(createTurnStartedEvent(event));+ },mode: "conversation",capabilities: { requestInput: true },tools,@@ -216,6 +244,7 @@ function fixture(reports.push(report);return {script,+ restoredTurns,executions,events,step,@@ -758,6 +787,7 @@ it("resumes a complete batch while another batch has only a partial approval [cof.script.push(calls("gateB"));await f.drive({ message: "Prepare another independent B." });const independentB = f.pending().at(-1)!;+ const independentTurn = getPendingInputBatches(f.session.state)[1]!.event!.turnId;// When one delivery answers A and the independent B.f.script.push("Independent B approved.");@@ -770,6 +800,7 @@ it("resumes a complete batch while another batch has only a partial approval [co// Then only the complete batch executes and replies, without another model call.expect(result.settledTurn?.output).toBe("Independent B approved.");+ expect(f.restoredTurns).toEqual([independentTurn]);expect(f.executions).toEqual(["gateB"]);expect(f.pending()).toHaveLength(2);f.script.push("Both approved.");diff --git a/packages/eve/src/harness/pending-input-batches.ts b/packages/eve/src/harness/pending-input-batches.tsindex 1686c7fc8..038c5a6a2 100644--- a/packages/eve/src/harness/pending-input-batches.ts+++ b/packages/eve/src/harness/pending-input-batches.ts@@ -28,6 +28,7 @@ export interface PendingInputBatchEvent {* assistant turn's requests plus its withheld model output.*/export interface PendingInputBatch {+ readonly toolReplayIdentities?: Readonly<Record<string, string>>;readonly event?: PendingInputBatchEvent;readonly activityRootTurnId?: string;readonly requests: readonly InputRequest[];@@ -138,6 +139,7 @@ function setPendingInputBatches(event: batch.event,activityRootTurnId: batch.activityRootTurnId,responseAuthRequiredRequestIds: batch.responseAuthRequiredRequestIds,+ toolReplayIdentities: batch.toolReplayIdentities,requests: [...batch.requests],responseMessages: [...batch.responseMessages],}));@@ -151,6 +153,7 @@ function setPendingInputBatches(* batches stay open and independently answerable.*/export function appendPendingInputBatch(input: {+ readonly toolReplayIdentities?: Readonly<Record<string, string>>;readonly event?: PendingInputBatchEvent;readonly activityRootTurnId?: string;readonly requests: readonly InputRequest[];@@ -165,6 +168,7 @@ export function appendPendingInputBatch(input: {activityRootTurnId:input.activityRootTurnId ?? contextStorage.getStore()?.get(ActivityRootTurnIdKey),responseAuthRequiredRequestIds: input.responseAuthRequiredRequestIds,+ toolReplayIdentities: input.toolReplayIdentities,requests: input.requests,responseMessages: input.responseMessages,},diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.tsindex bc6d10c13..6ab5c7b2a 100644--- a/packages/eve/src/harness/tool-loop.ts+++ b/packages/eve/src/harness/tool-loop.ts@@ -129,10 +129,7 @@ import {markApprovalCandidatePendingEventEmitted,markApprovalSettlementEventEmitted,} from "#harness/approval-candidates.js";-import {- coordinateApprovalDelivery,- shouldPrepareApprovalReplayTools,-} from "#harness/approval-delivery-coordinator.js";+import { coordinateApprovalDelivery } from "#harness/approval-delivery-coordinator.js";import type { InstrumentationAttempt, InstrumentationStepScope } from "#instrumentation/runtime.js";import {consumeDeferredStepInput,@@ -142,6 +139,7 @@ import {hasPendingApprovalBatch,hasStepInput,resolvePendingInput,+ selectApprovalReplayBatch,appendPendingInputBatch,} from "#harness/input-requests.js";import { getPendingInputBatches, queueDeferredStepInput } from "#harness/pending-input-batches.js";@@ -693,36 +691,46 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {: effectiveStepInput;const approvalContext = contextStorage.getStore();- if (- approvalContext !== undefined &&- config.resolveStepDynamicTools !== undefined &&- shouldPrepareApprovalReplayTools({ session, stepInput: effectiveStepInput })- ) {- await config.resolveStepDynamicTools({- ctx: approvalContext,- event: createStepStartedEvent({- modelId: session.agent.modelReference?.id ?? "dynamic",- sequence: emissionState.sequence,- stepIndex: emissionState.stepIndex,- turnId: emissionState.turnId,- }),- messages: projectHistory(resolvedCoordination.messages, session.state),+ const prepareApprovalTools = async (+ batch: ReturnType<typeof getPendingInputBatches>[number] | undefined,+ ) => {+ if (batch?.event !== undefined) await config.prepareApprovalTurn?.(batch.event);+ if (approvalContext !== undefined) {+ await config.resolveStepDynamicTools?.({+ ctx: approvalContext,+ event: createStepStartedEvent({+ modelId: session.agent.modelReference?.id ?? "dynamic",+ ...(batch?.event ?? emissionState),+ }),+ messages: projectHistory(resolvedCoordination.messages, session.state),+ });+ }+ return buildResponseAuthorizationTools({+ authoredTools: config.tools,+ context: approvalContext,});- }- const responseAuthorizationTools = shouldPrepareApprovalReplayTools({- session,- stepInput: effectiveStepInput,- })- ? buildResponseAuthorizationTools({- authoredTools: config.tools,- context: approvalContext,- })- : config.tools;+ };const pendingApprovalChallenges = getPendingAuthorization(session.state)?.challenges ?? [];const coordinated = await coordinateApprovalDelivery({session,stepInput: effectiveStepInput,- tools: responseAuthorizationTools,+ tools: config.tools,+ prepareTools: async (request) => {+ const batch = getPendingInputBatches(session.state).find((batch) =>+ batch.requests.some((entry) => entry.requestId === request.requestId),+ );+ const tools = await prepareApprovalTools(batch);+ const expected = batch?.toolReplayIdentities?.[request.requestId];+ if (+ expected !== undefined &&+ config.toolReplayIdentity?.(request.action.toolName) !== expected+ ) {+ const available = new Map(tools);+ available.delete(request.action.toolName);+ return available;+ }+ return tools;+ },});session = coordinated.session;if (emit) {@@ -870,6 +878,9 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {};}+ const replayBatch = selectApprovalReplayBatch(session, coordinated.stepInput);+ const responseAuthorizationTools =+ replayBatch === undefined ? config.tools : await prepareApprovalTools(replayBatch);const pending = resolvePendingInput({activeTurnId: pendingCoordination?.event.turnId ?? activeTurnId(emissionState),deferMessagesWhileApprovalsPending: config.mode !== "conversation",@@ -1258,6 +1269,30 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {return failBoundaryEvent(error, emissionState);}}+ const replayRequests = (pending.resolvedInputs ?? []).flatMap((batch) =>+ batch.inputs.filter((input) => input.outcome === "approved"),+ );+ if (replayRequests.length > 0) {+ const replayTools = buildResponseAuthorizationTools({+ authoredTools: config.tools,+ context: ctx,+ });+ for (const { request, toolReplayIdentity } of replayRequests) {+ if (+ toolReplayIdentity !== undefined &&+ config.toolReplayIdentity?.(request.action.toolName) !== toolReplayIdentity+ ) {+ throw new Error(+ "The connection for this tool call changed or is unavailable. Request a new tool call and approval.",+ );+ }+ if (!replayTools.has(request.action.toolName)) {+ throw new Error(+ "The approved tool is no longer available. Request a new tool call and approval.",+ );+ }+ }+ }const approvedTools = getApprovedTools(session,resolveApprovalKeyFromTools(@@ -2761,6 +2796,7 @@ async function handleStepResult(input: {turnId: emissionState.turnId,},requests: inputRequests,+ toolReplayIdentities: captureToolReplayIdentities(config, approvalRequests),responseMessages: [],session: parkedSession,});@@ -2796,6 +2832,7 @@ async function handleStepResult(input: {turnId: emissionState.turnId,},requests: inputRequests,+ toolReplayIdentities: captureToolReplayIdentities(config, approvalRequests),responseAuthRequiredRequestIds: approvalRequests.filter((request) => {const approval = responseAuthorizationTools.get(request.action.toolName)?.approval;@@ -3363,6 +3400,17 @@ async function maybeCompact(input: {* compound keys at recording time instead of pre-computing and persisting* them on the pending batch.*/+function captureToolReplayIdentities(+ config: ToolLoopHarnessConfig,+ requests: readonly InputRequest[],+): Readonly<Record<string, string>> | undefined {+ const identities = requests.flatMap((request) => {+ const identity = config.toolReplayIdentity?.(request.action.toolName);+ return identity === undefined ? [] : [[request.requestId, identity]];+ });+ return identities.length === 0 ? undefined : Object.fromEntries(identities);+}+function resolveApprovalKeyFromTools(tools: HarnessToolMap,): (request: InputRequest) => string | undefined {diff --git a/packages/eve/src/harness/types.ts b/packages/eve/src/harness/types.tsindex 9b29934ca..8b4f0aa32 100644--- a/packages/eve/src/harness/types.ts+++ b/packages/eve/src/harness/types.ts@@ -307,6 +307,13 @@ export interface ToolLoopHarnessConfig {readonly mode: RunMode;/** Whether this node enables framework background-task behavior. */readonly tasksEnabled?: boolean;+ /** Restores runtime resources for the originating turn before approval work. */+ readonly prepareApprovalTurn?: (event: {+ readonly sequence: number;+ readonly turnId: string;+ }) => Promise<void>;+ /** Opaque runtime identity retained by a pending approval batch. */+ readonly toolReplayIdentity?: (toolName: string) => string | undefined;/** Resolves persisted step-scoped tools before an approval policy reads them. */readonly resolveStepDynamicTools?: (input: {readonly ctx: AlsContext;