Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): persist HITL approval results in session history (#588)

Rui4f86a219a7fc
diff --git a/.changeset/persist-approval-resume-messages.md b/.changeset/persist-approval-resume-messages.md
new file mode 100644
index 000000000..942c9a84c
--- /dev/null
+++ b/.changeset/persist-approval-resume-messages.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Persist AI SDK approval-resume response messages in session history so approved local tool results survive later provider requests.
diff --git a/e2e/fixtures/agent-tools-hitl-openai/.gitignore b/e2e/fixtures/agent-tools-hitl-openai/.gitignore
new file mode 100644
index 000000000..4301967e4
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/.gitignore
@@ -0,0 +1,11 @@
+node_modules
+.env*
+.eve
+.vercel
+.workflow-data
+.next
+.output
+.nitro
+dist
+.DS_Store
+*.tsbuildinfo
diff --git a/e2e/fixtures/agent-tools-hitl-openai/.vercelignore b/e2e/fixtures/agent-tools-hitl-openai/.vercelignore
new file mode 100644
index 000000000..357664320
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/.vercelignore
@@ -0,0 +1,6 @@
+node_modules
+.eve
+.next
+.output
+.nitro
+dist
diff --git a/e2e/fixtures/agent-tools-hitl-openai/agent/agent.ts b/e2e/fixtures/agent-tools-hitl-openai/agent/agent.ts
new file mode 100644
index 000000000..a780c41c4
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/agent/agent.ts
@@ -0,0 +1,11 @@
+import { defineAgent } from "eve";
+
+/**
+ * HITL fixture pinned to the OpenAI Responses provider path
+ * (https://github.com/vercel/eve/issues/236). Approval-gated executable
+ * tools must complete an approve and execute cycle when the replayed history is
+ * validated by OpenAI's `function_call` / `function_call_output` pairing.
+ */
+export default defineAgent({
+ model: "openai/gpt-5.5",
+});
diff --git a/e2e/fixtures/agent-tools-hitl-openai/agent/instructions.md b/e2e/fixtures/agent-tools-hitl-openai/agent/instructions.md
new file mode 100644
index 000000000..bce87b044
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/agent/instructions.md
@@ -0,0 +1,3 @@
+# Identity
+
+You are a helpful assistant.
diff --git a/e2e/fixtures/agent-tools-hitl-openai/agent/tools/guarded-echo.ts b/e2e/fixtures/agent-tools-hitl-openai/agent/tools/guarded-echo.ts
new file mode 100644
index 000000000..b7931758b
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/agent/tools/guarded-echo.ts
@@ -0,0 +1,25 @@
+import { defineTool } from "eve/tools";
+import { always } from "eve/tools/approval";
+import { z } from "zod";
+
+export const GUARDED_ECHO_OPENAI_TOKEN = "guarded-echo-openai-ok-R2D7";
+
+/**
+ * Deterministic HITL fixture matching #236's repro. It is an authored tool with
+ * both a real `execute` and `always()` approval, so every call parks the
+ * turn for a human decision.
+ */
+export default defineTool({
+ description:
+ "Smoke-test fixture gated by HITL approval. Echoes the note input. Only call when the user explicitly asks for `guarded-echo`.",
+ inputSchema: z.object({
+ note: z.string().optional().describe("Any note string."),
+ }),
+ approval: always(),
+ async execute(input) {
+ return {
+ echoed: input.note ?? null,
+ token: "guarded-echo-openai-ok-R2D7",
+ };
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl-openai/evals/evals.config.ts b/e2e/fixtures/agent-tools-hitl-openai/evals/evals.config.ts
new file mode 100644
index 000000000..6864dd176
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/evals/evals.config.ts
@@ -0,0 +1,6 @@
+import { defineEvalConfig } from "eve/evals";
+
+/** Default judge model for any `t.judge.*` assertion in this fixture. */
+export default defineEvalConfig({
+ judge: { model: "openai/gpt-5.5" },
+});
diff --git a/e2e/fixtures/agent-tools-hitl-openai/evals/hitl/approve-resume.eval.ts b/e2e/fixtures/agent-tools-hitl-openai/evals/hitl/approve-resume.eval.ts
new file mode 100644
index 000000000..350200a2d
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/evals/hitl/approve-resume.eval.ts
@@ -0,0 +1,48 @@
+import { defineEval } from "eve/evals";
+
+const GUARDED_ECHO_OPENAI_TOKEN = "guarded-echo-openai-ok-R2D7";
+
+/**
+ * Regression coverage for https://github.com/vercel/eve/issues/236.
+ *
+ * An `always()`-gated executable tool on the OpenAI Responses provider:
+ * approve-resume must execute the tool and the transcript must replay on a
+ * follow-up turn. For local function tools the `tool-approval-response` part
+ * is not a provider-level closure. OpenAI rejects any replay containing a
+ * `function_call` without a matching `function_call_output` with
+ * `No tool output found for function call call_<id>`.
+ */
+export default defineEval({
+ description: "HITL regression (#236): approve-resume executes and replays on OpenAI Responses.",
+ async test(t) {
+ const parked = await t.send('Call the guarded-echo tool with note "openai-approve".');
+ parked.calledTool("guarded-echo", { status: "pending", count: 1 });
+ const request = t.requireInputRequest({
+ display: "confirmation",
+ toolName: "guarded-echo",
+ });
+
+ const approved = await t.respond({
+ requestId: request.requestId,
+ optionId: "approve",
+ });
+ approved.expectOk();
+ approved.event("action.result", {
+ data: {
+ result: {
+ kind: "tool-result",
+ output: new RegExp(GUARDED_ECHO_OPENAI_TOKEN),
+ toolName: "guarded-echo",
+ },
+ status: "completed",
+ },
+ count: 1,
+ });
+
+ const followup = await t.send("Reply with exactly OPENAI-REPLAY-OK.");
+ followup.expectOk();
+ followup.messageIncludes(/OPENAI-REPLAY-OK/i);
+
+ t.succeeded();
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl-openai/package.json b/e2e/fixtures/agent-tools-hitl-openai/package.json
new file mode 100644
index 000000000..e8c98c2a4
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "agent-tools-hitl-openai",
+ "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": {
+ "@opentelemetry/core": "2.6.1",
+ "@opentelemetry/sdk-trace-base": "2.6.1",
+ "@vercel/otel": "2.1.2",
+ "eve": "workspace:*",
+ "zod": "catalog:"
+ },
+ "devDependencies": {
+ "@types/node": "catalog:",
+ "typescript": "catalog:"
+ }
+}
diff --git a/e2e/fixtures/agent-tools-hitl-openai/tsconfig.json b/e2e/fixtures/agent-tools-hitl-openai/tsconfig.json
new file mode 100644
index 000000000..66f5b1ea5
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl-openai/tsconfig.json
@@ -0,0 +1,17 @@
+{
+ "compilerOptions": {
+ "target": "ES2022",
+ "module": "NodeNext",
+ "moduleResolution": "NodeNext",
+ "outDir": "dist",
+ "rootDir": ".",
+ "strict": true,
+ "esModuleInterop": true,
+ "skipLibCheck": true,
+ "forceConsistentCasingInFileNames": true,
+ "declaration": true,
+ "noEmit": true,
+ "types": ["node"]
+ },
+ "include": ["agent/**/*.ts", "evals/**/*.ts", ".eve/**/*.d.ts"]
+}
diff --git a/e2e/fixtures/agent-tools-hitl/agent/tools/gate.ts b/e2e/fixtures/agent-tools-hitl/agent/tools/gate.ts
new file mode 100644
index 000000000..df5a1dbef
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl/agent/tools/gate.ts
@@ -0,0 +1,12 @@
+import { defineTool } from "eve/tools";
+import { always } from "eve/tools/approval";
+import { z } from "zod";
+
+export default defineTool({
+ description: "PROOF-ONLY: marks a run as executed. Requires approval.",
+ inputSchema: z.object({ marker: z.string() }),
+ approval: always(),
+ async execute({ marker }) {
+ return { executed: true, marker };
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl/agent/tools/guarded-slow-echo.ts b/e2e/fixtures/agent-tools-hitl/agent/tools/guarded-slow-echo.ts
new file mode 100644
index 000000000..74e9803f0
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl/agent/tools/guarded-slow-echo.ts
@@ -0,0 +1,27 @@
+import { defineTool } from "eve/tools";
+import { once } from "eve/tools/approval";
+import { z } from "zod";
+
+export const GUARDED_SLOW_ECHO_TOKEN = "guarded-slow-echo-ok-V7K2";
+
+/**
+ * HITL fixture for https://github.com/vercel/eve/issues/460. It is gated by
+ * `once()` and deliberately slow. The issue's repro singles out a
+ * non-trivial async `execute` as the trigger. The approved call's result
+ * must survive the wait before history is rebuilt and replayed.
+ */
+export default defineTool({
+ description:
+ "Smoke-test fixture gated by HITL approval with a slow side effect. Echoes the note input after a delay. Only call when the user explicitly asks for `guarded-slow-echo`, and call it strictly one call at a time. Never issue a second call before the previous call's result has arrived.",
+ inputSchema: z.object({
+ note: z.string().optional().describe("Any note string."),
+ }),
+ approval: once(),
+ async execute(input) {
+ await new Promise((resolve) => setTimeout(resolve, 2000));
+ return {
+ echoed: input.note ?? null,
+ token: GUARDED_SLOW_ECHO_TOKEN,
+ };
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-approve-resume.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-approve-resume.eval.ts
new file mode 100644
index 000000000..5de7b5f44
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-approve-resume.eval.ts
@@ -0,0 +1,39 @@
+import { defineEval } from "eve/evals";
+
+const MARKER = "authored-always-approve-resume-N4K7";
+const TOOL_NAME = "gate";
+
+/** Regression reproduction for https://github.com/vercel/eve/issues/533. */
+export default defineEval({
+ description:
+ "HITL repro (#533): a separate approval response executes an authored always-gated tool.",
+ async test(t) {
+ const parked = await t.send(`Call the \`${TOOL_NAME}\` tool with marker "${MARKER}".`);
+ parked.calledTool(TOOL_NAME, { status: "pending", count: 1 });
+ const approval = t.requireInputRequest({
+ display: "confirmation",
+ toolName: TOOL_NAME,
+ });
+
+ // This sends only `inputResponses` in a separate turn. No user message or
+ // channel context follows the tool approval response in the model input.
+ const approved = await t.respond({
+ optionId: "approve",
+ requestId: approval.requestId,
+ });
+
+ approved.expectOk();
+ approved.event("action.result", {
+ data: {
+ result: {
+ kind: "tool-result",
+ output: new RegExp(MARKER),
+ toolName: TOOL_NAME,
+ },
+ status: "completed",
+ },
+ count: 1,
+ });
+ t.succeeded();
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.ts
new file mode 100644
index 000000000..4887add5f
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/authored-always-unrelated-input.eval.ts
@@ -0,0 +1,46 @@
+import { defineEval } from "eve/evals";
+
+const MARKER = "authored-always-unrelated-input-P7M2";
+const TOOL_NAME = "gate";
+
+/** Regression reproduction for https://github.com/vercel/eve/issues/533. */
+export default defineEval({
+ description:
+ "HITL repro (#533): unrelated input does not replay an unresolved authored tool call.",
+ async test(t) {
+ const parked = await t.send(`Call the \`${TOOL_NAME}\` tool with marker "${MARKER}".`);
+ parked.calledTool(TOOL_NAME, { status: "pending", count: 1 });
+ const approval = t.requireInputRequest({
+ display: "confirmation",
+ toolName: TOOL_NAME,
+ });
+
+ const unrelated = await t.send("Queue this unrelated note: ORBITAL-PINE-6C3R.");
+
+ unrelated.expectOk();
+ unrelated.notEvent("action.result", {
+ data: { result: { toolName: TOOL_NAME } },
+ });
+ unrelated.notEvent("step.started");
+ unrelated.event("session.waiting", { count: 1 });
+
+ const approved = await t.respond({
+ optionId: "approve",
+ requestId: approval.requestId,
+ });
+
+ approved.expectOk();
+ approved.event("action.result", {
+ data: {
+ result: {
+ kind: "tool-result",
+ output: new RegExp(MARKER),
+ toolName: TOOL_NAME,
+ },
+ status: "completed",
+ },
+ count: 1,
+ });
+ t.succeeded();
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/dynamic-approve-then-followup.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/dynamic-approve-then-followup.eval.ts
new file mode 100644
index 000000000..95ece2901
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/dynamic-approve-then-followup.eval.ts
@@ -0,0 +1,46 @@
+import { defineEval } from "eve/evals";
+
+const DYNAMIC_GUARDED_ECHO_TOKEN = "dynamic-guarded-echo-ok-L8R6";
+const TOOL_NAME = "dynamic_guarded_echo";
+
+/**
+ * Regression coverage for https://github.com/vercel/eve/issues/533.
+ *
+ * An always-gated dynamic tool parks, the user approves, the tool executes,
+ * and then the session must keep working. The follow-up turn replays the
+ * durable transcript containing the approval-parked call's `tool_use`,
+ * approval request/response parts, and result; on Anthropic that replay is
+ * where the reported `tool_use ids were found without tool_result blocks`
+ * 400 lands, turning `session.waiting` into a terminal `session.failed`.
+ */
+export default defineEval({
+ description: "HITL regression (#533): a resolved approval park replays on the next turn.",
+ async test(t) {
+ const parked = await t.send(`Call the \`${TOOL_NAME}\` tool with note "replay-probe".`);
+ parked.calledTool(TOOL_NAME, { status: "pending", count: 1 });
+ t.requireInputRequest({
+ display: "confirmation",
+ toolName: TOOL_NAME,
+ });
+
+ const approved = await t.respondAll("approve");
+ approved.expectOk();
+ approved.event("action.result", {
+ data: {
+ result: {
+ kind: "tool-result",
+ output: new RegExp(DYNAMIC_GUARDED_ECHO_TOKEN),
+ toolName: TOOL_NAME,
+ },
+ status: "completed",
+ },
+ count: 1,
+ });
+
+ const followup = await t.send("Reply with exactly DYNAMIC-REPLAY-OK.");
+ followup.expectOk();
+ followup.messageIncludes(/DYNAMIC-REPLAY-OK/i);
+
+ t.succeeded();
+ },
+});
diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/shared.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/shared.ts
index 652afad1c..85df58ed5 100644
--- a/e2e/fixtures/agent-tools-hitl/evals/hitl/shared.ts
+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/shared.ts
@@ -1 +1,2 @@
export const GUARDED_ECHO_TOKEN = "guarded-echo-ok-T4Q9";
+export const GUARDED_SLOW_ECHO_TOKEN = "guarded-slow-echo-ok-V7K2";
diff --git a/e2e/fixtures/agent-tools-hitl/evals/hitl/slow-once-approve-replay.eval.ts b/e2e/fixtures/agent-tools-hitl/evals/hitl/slow-once-approve-replay.eval.ts
new file mode 100644
index 000000000..189e12afd
--- /dev/null
+++ b/e2e/fixtures/agent-tools-hitl/evals/hitl/slow-once-approve-replay.eval.ts
@@ -0,0 +1,47 @@
+import { defineEval } from "eve/evals";
+
+import { GUARDED_SLOW_ECHO_TOKEN } from "./shared.js";
+
+const TOOL_NAME = "guarded-slow-echo";
+
+/**
+ * Regression coverage for https://github.com/vercel/eve/issues/460.
+ *
+ * A `once()`-gated tool with a slow async `execute` is called twice in one
+ * user turn. The first call is human-approved, and the second auto-approves via
+ * the session's `once()` grant. The approved call's `tool_result` reaches
+ * durable history through the AI SDK's accumulated response messages. If eve
+ * keeps only the final step response, the next turn replays a `tool_use`
+ * without a `tool_result` and the provider rejects every later turn.
+ */
+export default defineEval({
+ description: "HITL regression (#460): approved slow tool result survives replay.",
+ async test(t) {
+ const parked = await t.send(
+ `Call the ${TOOL_NAME} tool with note "alpha". After its result arrives, call it again with note "beta". Use strictly one call at a time, never in parallel. When both results are in, reply with exactly SLOW-DONE.`,
+ );
+ parked.calledTool(TOOL_NAME, { status: "pending", count: 1 });
+ const request = t.requireInputRequest({
+ display: "confirmation",
+ toolName: TOOL_NAME,
+ });
+
+ const approved = await t.respond({
+ requestId: request.requestId,
+ optionId: "approve",
+ });
+ approved.expectOk();
+
+ t.calledTool(TOOL_NAME, {
+ output: new RegExp(GUARDED_SLOW_ECHO_TOKEN),
+ status: "completed",
+ count: 2,
+ });
+
+ const followup = await t.send("Reply with exactly SLOW-REPLAY-OK.");
+ followup.expectOk();
+ followup.messageIncludes(/SLOW-REPLAY-OK/i);
+
+ t.succeeded();
+ },
+});
diff --git a/packages/eve/src/execution/node-step.test.ts b/packages/eve/src/execution/node-step.test.ts
index ececac66a..98dae16a1 100644
--- a/packages/eve/src/execution/node-step.test.ts
+++ b/packages/eve/src/execution/node-step.test.ts
@@ -86,7 +86,7 @@ function setupMockAgentForToolExecution(toolName: string, args: unknown): void {
};
if (onStepFinish) await onStepFinish(result);
- return result;
+ return { ...result, responseMessages: result.response.messages };
});
return this as unknown as ToolLoopAgent;
@@ -160,7 +160,7 @@ function setupMockAgentForToolCall(toolName: string, args: unknown): void {
await onStepFinish(result);
}
- return result;
+ return { ...result, responseMessages: result.response.messages };
});
return this as unknown as ToolLoopAgent;
diff --git a/packages/eve/src/harness/ai-sdk-approval-resume.integration.test.ts b/packages/eve/src/harness/ai-sdk-approval-resume.integration.test.ts
new file mode 100644
index 000000000..4428ae2d6
--- /dev/null
+++ b/packages/eve/src/harness/ai-sdk-approval-resume.integration.test.ts
@@ -0,0 +1,184 @@
+import {
+ isStepCount,
+ simulateReadableStream,
+ tool,
+ ToolLoopAgent,
+ type ModelMessage,
+ type ToolApprovalRequest,
+} from "ai";
+import { MockLanguageModelV4 } from "ai/test";
+import { describe, expect, it, vi } from "vitest";
+import { z } from "zod";
+
+type StreamResult = Awaited<ReturnType<MockLanguageModelV4["doStream"]>>;
+type StreamPart = StreamResult["stream"] extends ReadableStream<infer Part> ? Part : never;
+
+const usage = {
+ inputTokens: {
+ cacheRead: undefined,
+ cacheWrite: undefined,
+ noCache: 1,
+ total: 1,
+ },
+ outputTokens: {
+ reasoning: undefined,
+ text: 1,
+ total: 1,
+ },
+};
+
+function streamResult(chunks: StreamPart[]): StreamResult {
+ return { stream: simulateReadableStream({ chunks }) };
+}
+
+function findApprovalRequest(messages: readonly ModelMessage[]): ToolApprovalRequest | undefined {
+ for (const message of messages) {
+ if (message.role !== "assistant" || typeof message.content === "string") {
+ continue;
+ }
+ for (const part of message.content) {
+ if (part.type === "tool-approval-request") {
+ return part;
+ }
+ }
+ }
+ return undefined;
+}
+
+describe("AI SDK approval-resume streaming contract", () => {
+ it("streams a cross-invocation result and includes it in call-wide response messages", async () => {
+ const execute = vi.fn(async ({ note }: { readonly note: string }) => ({ echoed: note }));
+ const model = new MockLanguageModelV4({
+ doStream: [
+ streamResult([
+ { type: "stream-start", warnings: [] },
+ {
+ input: JSON.stringify({ note: "approved" }),
+ toolCallId: "call-1",
+ toolName: "guardedEcho",
+ type: "tool-call",
+ },
+ {
+ finishReason: { raw: undefined, unified: "tool-calls" },
+ type: "finish",
+ usage,
+ },
+ ]),
+ streamResult([
+ { type: "stream-start", warnings: [] },
+ { id: "answer", type: "text-start" },
+ { delta: "Approved result received.", id: "answer", type: "text-delta" },
+ { id: "answer", type: "text-end" },
+ {
+ finishReason: { raw: undefined, unified: "stop" },
+ type: "finish",
+ usage,
+ },
+ ]),
+ ],
+ modelId: "approval-resume-model",
+ provider: "eve-integration-mock",
+ });
+ const agent = new ToolLoopAgent({
+ model,
+ stopWhen: isStepCount(1),
+ toolApproval: () => "user-approval",
+ tools: {
+ guardedEcho: tool({
+ description: "Echo an approved note.",
+ execute,
+ inputSchema: z.object({ note: z.string() }),
+ toModelOutput: ({ output }) => ({
+ type: "text",
+ value: `canonical:${output.echoed}`,
+ }),
+ }),
+ },
+ });
+ const initialMessages: ModelMessage[] = [
+ { content: "Run guardedEcho with the approved note.", role: "user" },
+ ];
+
+ const approvalRequest = await agent.stream({ messages: initialMessages });
+ const approvalStreamParts = [];
+ for await (const part of approvalRequest.fullStream) {
+ approvalStreamParts.push(part);
+ }
+ const approvalResponseMessages = await approvalRequest.responseMessages;
+ const approvalPart = findApprovalRequest(approvalResponseMessages);
+
+ expect(approvalStreamParts.map((part) => part.type)).toContain("tool-call");
+ expect(approvalStreamParts.map((part) => part.type)).toContain("tool-approval-request");
+ expect(approvalStreamParts.map((part) => part.type)).not.toContain("tool-result");
+ expect(approvalPart).toMatchObject({ toolCallId: "call-1" });
+ expect(execute).not.toHaveBeenCalled();
+ if (approvalPart === undefined) {
+ throw new Error("AI SDK did not produce a tool approval request.");
+ }
+
+ const resumeMessages: ModelMessage[] = [
+ ...initialMessages,
+ ...approvalResponseMessages,
+ {
+ content: [
+ {
+ approvalId: approvalPart.approvalId,
+ approved: true,
+ type: "tool-approval-response",
+ },
+ ],
+ role: "tool",
+ },
+ ];
+ const resumed = await agent.stream({ messages: resumeMessages });
+ const resumedStreamParts = [];
+ for await (const part of resumed.fullStream) {
+ resumedStreamParts.push(part);
+ }
+ const resumedResponseMessages = await resumed.responseMessages;
+ const resumedSteps = await resumed.steps;
+
+ expect(execute).toHaveBeenCalledExactlyOnceWith(
+ { note: "approved" },
+ expect.objectContaining({ toolCallId: "call-1" }),
+ );
+ expect(
+ resumedStreamParts.filter((part) => part.type === "tool-call").map((part) => part.toolCallId),
+ ).toEqual([]);
+ const streamedToolResult = resumedStreamParts.find((part) => part.type === "tool-result");
+ expect(streamedToolResult).toMatchObject({
+ output: { echoed: "approved" },
+ toolCallId: "call-1",
+ });
+ const streamedToolResultIndex = resumedStreamParts.findIndex(
+ (part) => part.type === "tool-result",
+ );
+ const modelStepIndex = resumedStreamParts.findIndex((part) => part.type === "start-step");
+ expect(streamedToolResultIndex).toBeGreaterThanOrEqual(0);
+ expect(modelStepIndex).toBeGreaterThan(streamedToolResultIndex);
+ expect(resumedSteps).toHaveLength(1);
+ expect(resumedSteps[0]?.response.messages.map((message) => message.role)).toEqual([
+ "assistant",
+ ]);
+ expect(resumedResponseMessages.map((message) => message.role)).toEqual(["tool", "assistant"]);
+ expect(resumedResponseMessages[0]).toEqual({
+ content: [
+ {
+ output: { type: "text", value: "canonical:approved" },
+ toolCallId: "call-1",
+ toolName: "guardedEcho",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ });
+
+ const providerPrompt = model.doStreamCalls[1]?.prompt ?? [];
+ const providerToolResults = providerPrompt.flatMap((message) =>
+ message.role === "tool" ? message.content.filter((part) => part.type === "tool-result") : [],
+ );
+ expect(providerToolResults).toEqual([
+ expect.objectContaining({ toolCallId: "call-1", toolName: "guardedEcho" }),
+ ]);
+ });
+});
diff --git a/packages/eve/src/harness/emission.ts b/packages/eve/src/harness/emission.ts
index 7bdd77a0c..7f900ff33 100644
--- a/packages/eve/src/harness/emission.ts
+++ b/packages/eve/src/harness/emission.ts
@@ -9,7 +9,6 @@ import type {
type ToolResponsePart = Extract<ModelMessage, { role: "tool" }>["content"][number];
type InlineToolResultPart = Extract<ToolResponsePart, { type: "tool-result" }>;
-type InlineToolResultJsonValue = Extract<InlineToolResultPart["output"], { type: "json" }>["value"];
import type { AssistantStepFinishReason, RuntimeIdentity } from "#protocol/message.js";
import {
@@ -326,16 +325,14 @@ export function normalizeAssistantStepFinishReason(
/**
* Result of consuming one step's `fullStream`.
*
- * Inline results avoid duplicate post-step events. Approval-resume results
- * also repair persisted history or route authorization back to the park
- * detector.
+ * Inline results avoid duplicate post-step events. Approval-resume
+ * authorization results also route back to the park detector.
*/
interface EmittedStreamContent {
readonly emittedActionCallIds: ReadonlySet<string>;
readonly handledInlineToolResultCallIds: ReadonlySet<string>;
readonly invalidInputToolCallIds: ReadonlySet<string>;
readonly inlineAuthorizationResults: readonly TypedToolResult<ToolSet>[];
- readonly inlineToolResultParts: readonly InlineToolResultPart[];
readonly trailingInlineToolResultParts: readonly InlineToolResultPart[];
}
@@ -370,7 +367,6 @@ export async function emitStreamContent(
const handledInlineToolResultCallIds = new Set<string>();
const invalidInputToolCallIds = new Set<string>();
const inlineAuthorizationResults: TypedToolResult<ToolSet>[] = [];
- const inlineToolResultParts: InlineToolResultPart[] = [];
const trailingInlineToolResultParts: InlineToolResultPart[] = [];
const flushCurrentMessage = async (): Promise<void> => {
@@ -583,17 +579,6 @@ export async function emitStreamContent(
}
await emitActionResult(createRuntimeToolResultFromStepResult(inlineToolResult));
handledInlineToolResultCallIds.add(part.toolCallId);
- // Preserve the SDK's text/json output shape in persisted history.
- const rawOutput: unknown = inlineToolResult.output;
- inlineToolResultParts.push({
- type: "tool-result",
- toolCallId: inlineToolResult.toolCallId,
- toolName: inlineToolResult.toolName,
- output:
- typeof rawOutput === "string"
- ? { type: "text", value: rawOutput }
- : { type: "json", value: (rawOutput ?? null) as InlineToolResultJsonValue },
- });
break;
}
case "tool-error": {
@@ -676,7 +661,6 @@ export async function emitStreamContent(
handledInlineToolResultCallIds,
invalidInputToolCallIds,
inlineAuthorizationResults,
- inlineToolResultParts,
trailingInlineToolResultParts,
};
}
diff --git a/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts b/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts
index 0ff388cfb..ea63d0498 100644
--- a/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts
+++ b/packages/eve/src/harness/tool-loop-compaction-accounting.test.ts
@@ -61,6 +61,19 @@ type MockAgentConstructor =
? (settings: S) => ToolLoopAgent
: never;
+function getMockResponseMessages(result: Record<string, unknown>): unknown[] {
+ const response = result.response;
+ if (
+ typeof response !== "object" ||
+ response === null ||
+ !("messages" in response) ||
+ !Array.isArray(response.messages)
+ ) {
+ throw new Error("Mock ToolLoopAgent result must include response messages.");
+ }
+ return response.messages;
+}
+
function setupMockAgentSequence(results: readonly Record<string, unknown>[]): void {
const queue = [...results];
@@ -91,7 +104,7 @@ function setupMockAgentSequence(results: readonly Record<string, unknown>[]): vo
await onStepFinish(result);
}
- return result;
+ return { ...result, responseMessages: getMockResponseMessages(result) };
});
this.stream = vi.fn();
diff --git a/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.ts b/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.ts
new file mode 100644
index 000000000..bae7a600a
--- /dev/null
+++ b/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.ts
@@ -0,0 +1,177 @@
+import { jsonSchema, type LanguageModel, type ModelMessage } from "ai";
+import { MockLanguageModelV4 } from "ai/test";
+import { describe, expect, it, vi } from "vitest";
+
+import { setPendingInputBatch } from "#harness/input-requests.js";
+import { createToolLoopHarness } from "#harness/tool-loop.js";
+import type { HarnessSession, ToolLoopHarnessConfig } from "#harness/types.js";
+
+const usage = {
+ inputTokens: {
+ cacheRead: undefined,
+ cacheWrite: undefined,
+ noCache: 1,
+ total: 1,
+ },
+ outputTokens: {
+ reasoning: undefined,
+ text: 1,
+ total: 1,
+ },
+};
+
+const toolCall = {
+ input: { command: "pwd" },
+ toolCallId: "call-1",
+ toolName: "bash",
+ type: "tool-call" as const,
+};
+
+const approvalRequest = {
+ approvalId: "approval-1",
+ toolCallId: toolCall.toolCallId,
+ type: "tool-approval-request" as const,
+};
+
+function createPendingApprovalSession(): HarnessSession {
+ const session: HarnessSession = {
+ agent: {
+ modelReference: { id: "generate-approval-resume-model" },
+ system: "You are a test assistant.",
+ tools: [
+ {
+ description: "Run a shell command.",
+ inputSchema: { type: "object" },
+ name: toolCall.toolName,
+ },
+ ],
+ },
+ compaction: { recentWindowSize: 10, threshold: 100_000 },
+ continuationToken: "http:generate-approval-resume-session",
+ history: [{ content: "Run pwd.", role: "user" }],
+ sessionId: "generate-approval-resume-session",
+ };
+
+ return setPendingInputBatch({
+ requests: [
+ {
+ action: {
+ callId: toolCall.toolCallId,
+ input: toolCall.input,
+ kind: "tool-call",
+ toolName: toolCall.toolName,
+ },
+ allowFreeform: false,
+ display: "confirmation",
+ options: [
+ { id: "approve", label: "Yes" },
+ { id: "deny", label: "No" },
+ ],
+ prompt: "Approve tool call: bash",
+ requestId: approvalRequest.approvalId,
+ },
+ ],
+ responseMessages: [
+ {
+ content: [toolCall, approvalRequest],
+ role: "assistant",
+ },
+ ],
+ session,
+ });
+}
+
+function findPart(
+ messages: readonly ModelMessage[],
+ type: "tool-approval-response" | "tool-call" | "tool-result",
+): unknown {
+ for (const message of messages) {
+ if (
+ (message.role !== "assistant" && message.role !== "tool") ||
+ !Array.isArray(message.content)
+ ) {
+ continue;
+ }
+ const part = message.content.find((candidate) => candidate.type === type);
+ if (part !== undefined) return part;
+ }
+ return undefined;
+}
+
+describe("tool loop generate approval resume (real AI SDK)", () => {
+ it("persists the approved pre-model tool result without an event handler", async () => {
+ const execute = vi.fn(async () => "/workspace");
+ const model = new MockLanguageModelV4({
+ doGenerate: {
+ content: [{ text: "The command returned /workspace.", type: "text" }],
+ finishReason: { raw: undefined, unified: "stop" },
+ usage,
+ warnings: [],
+ },
+ modelId: "generate-approval-resume-model",
+ provider: "eve-integration-mock",
+ });
+ const tools: ToolLoopHarnessConfig["tools"] = new Map([
+ [
+ toolCall.toolName,
+ {
+ description: "Run a shell command.",
+ execute,
+ inputSchema: jsonSchema({ type: "object" }),
+ name: toolCall.toolName,
+ toModelOutput: (output) => {
+ if (typeof output !== "string") {
+ throw new TypeError("Expected the bash test tool to return a string.");
+ }
+ return { type: "text", value: `canonical:${output}` };
+ },
+ },
+ ],
+ ]);
+ const config: ToolLoopHarnessConfig = {
+ mode: "conversation",
+ resolveModel: async (): Promise<LanguageModel> => model,
+ tools,
+ };
+
+ const result = await createToolLoopHarness(config)(createPendingApprovalSession(), {
+ inputResponses: [{ optionId: "approve", requestId: approvalRequest.approvalId }],
+ });
+
+ expect(model.doGenerateCalls).toHaveLength(1);
+ expect(model.doStreamCalls).toHaveLength(0);
+ expect(execute).toHaveBeenCalledExactlyOnceWith(
+ toolCall.input,
+ expect.objectContaining({ toolCallId: toolCall.toolCallId }),
+ );
+
+ const providerPrompt = model.doGenerateCalls[0]?.prompt ?? [];
+ expect(findPart(providerPrompt, "tool-result")).toMatchObject({
+ output: { type: "text", value: "canonical:/workspace" },
+ toolCallId: toolCall.toolCallId,
+ toolName: toolCall.toolName,
+ });
+
+ expect(result.session.history.map((message) => message.role)).toEqual([
+ "user",
+ "assistant",
+ "tool",
+ "tool",
+ "assistant",
+ ]);
+ expect(findPart(result.session.history, "tool-call")).toEqual(toolCall);
+ expect(findPart(result.session.history, "tool-approval-response")).toMatchObject({
+ approvalId: approvalRequest.approvalId,
+ approved: true,
+ });
+ expect(findPart(result.session.history, "tool-result")).toMatchObject({
+ output: { type: "text", value: "canonical:/workspace" },
+ toolCallId: toolCall.toolCallId,
+ toolName: toolCall.toolName,
+ });
+ expect(result.session.history.at(-1)).toMatchObject({
+ content: [{ text: "The command returned /workspace.", type: "text" }],
+ role: "assistant",
+ });
+ });
+});
diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.ts
index 8693c547d..9396dbb2f 100644
--- a/packages/eve/src/harness/tool-loop.test.ts
+++ b/packages/eve/src/harness/tool-loop.test.ts
@@ -217,6 +217,7 @@ function getPrepareStep<TMessages, TResult>(value: unknown): PrepareStepProbe<TM
function createMockStreamResult(result: Record<string, unknown>): {
fullStream: AsyncIterable<Record<string, unknown>>;
+ responseMessages: Promise<Record<string, unknown>[]>;
steps: Promise<Record<string, unknown>[]>;
} {
const fullStreamParts = Array.isArray(result.fullStreamParts)
@@ -228,10 +229,26 @@ function createMockStreamResult(result: Record<string, unknown>): {
fullStreamParts === null
? createMockFullStream(result)
: createExplicitMockFullStream(fullStreamParts),
+ responseMessages: Promise.resolve(getMockResponseMessages(result)),
steps: Promise.resolve([result]),
};
}
+function getMockResponseMessages(result: Record<string, unknown>): Record<string, unknown>[] {
+ if (Array.isArray(result.responseMessages)) {
+ return result.responseMessages as Array<Record<string, unknown>>;
+ }
+
+ const response = (result.response ?? {}) as { messages?: unknown };
+ return Array.isArray(response.messages)
+ ? (response.messages as Array<Record<string, unknown>>)
+ : [];
+}
+
+function createMockGenerateResult(result: Record<string, unknown>): Record<string, unknown> {
+ return { ...result, responseMessages: getMockResponseMessages(result) };
+}
+
async function* createExplicitMockFullStream(
parts: readonly Record<string, unknown>[],
): AsyncIterable<Record<string, unknown>> {
@@ -362,7 +379,7 @@ function setupMockAgent(result: Record<string, unknown>): void {
});
}
if (onStepFinish) await onStepFinish(result);
- return result;
+ return createMockGenerateResult(result);
});
this.stream = vi.fn().mockImplementation(async (options: { messages: unknown[] }) => {
@@ -4819,33 +4836,40 @@ describe("createToolLoopHarness", () => {
});
});
- it("persists the inline approval-resume tool-result into session history so the next turn replays a balanced tool_use / tool_result pair", async () => {
+ it("persists the SDK's accumulated approval-resume messages into session history", async () => {
/*
- * When a previously-parked tool call is approved, the AI SDK
- * enqueues its tool-result onto the parent stream before re-
- * entering the LLM call. The result is absent from
- * `stepResult.response.messages` / `toolCalls` / `toolResults`,
- * so the harness must capture it from the stream and splice it
- * into persisted history. Without this, the next turn replays a
- * `tool_use` block with no matching `tool_result` and Anthropic
- * rejects the request with 400.
+ * The real AI SDK contract is covered in
+ * `ai-sdk-approval-resume.integration.test.ts`. This unit isolates Eve's
+ * side of that contract: durable history must use the call-wide
+ * `responseMessages`, even when event projection does not provide a
+ * reconstructable tool-result.
*/
- setupMockAgent({
- content: [],
- finishReason: "stop",
- fullStreamParts: [
+ const resumedToolResultMessage = {
+ content: [
{
- output: { exitCode: 0, stderr: "", stdout: "/workspace\n", truncated: false },
+ output: {
+ type: "json",
+ value: { exitCode: 0, stderr: "", stdout: "/workspace\n", truncated: false },
+ },
toolCallId: "call-1",
toolName: "bash",
type: "tool-result",
},
+ ],
+ role: "tool",
+ };
+ const assistantMessage = { content: "`/workspace`", role: "assistant" };
+ setupMockAgent({
+ content: [],
+ finishReason: "stop",
+ fullStreamParts: [
{ id: "text-1", text: "`/workspace`", type: "text-delta" },
{ finishReason: "stop", type: "finish-step" },
],
response: {
- messages: [{ content: "`/workspace`", role: "assistant" }],
+ messages: [assistantMessage],
},
+ responseMessages: [resumedToolResultMessage, assistantMessage],
text: "`/workspace`",
toolCalls: [],
toolResults: [],
@@ -4890,6 +4914,66 @@ describe("createToolLoopHarness", () => {
expect(result.session.history.at(-1)?.role).toBe("assistant");
});
+ it("persists approved tool results when event handling is disabled", async () => {
+ const resumedToolResultMessage = {
+ content: [
+ {
+ output: { type: "text", value: "/workspace" },
+ toolCallId: "call-1",
+ toolName: "bash",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ };
+ const assistantMessage = { content: "`/workspace`", role: "assistant" };
+
+ // The final step contains only the assistant reply. The call-wide
+ // GenerateTextResult also contains the approved pre-model tool result.
+ setupMockAgent({
+ content: [],
+ finishReason: "stop",
+ response: { messages: [assistantMessage] },
+ responseMessages: [resumedToolResultMessage, assistantMessage],
+ text: "`/workspace`",
+ toolCalls: [],
+ toolResults: [],
+ });
+
+ const harness = createToolLoopHarness(
+ createTestConfig("conversation", undefined, { tools: new Map() }),
+ );
+ const result = await harness(createPendingBashApprovalSession(), {
+ inputResponses: [{ optionId: "approve", requestId: "approval-1" }],
+ });
+
+ const agent = vi.mocked(ToolLoopAgent).mock.results.at(-1)?.value;
+ if (agent === undefined) {
+ throw new Error("ToolLoopAgent mock did not return an instance.");
+ }
+ expect(vi.mocked(agent.generate)).toHaveBeenCalledOnce();
+ expect(vi.mocked(agent.stream)).not.toHaveBeenCalled();
+ const assistantParts = result.session.history.flatMap((message) =>
+ message.role === "assistant" && Array.isArray(message.content) ? message.content : [],
+ );
+ const toolParts = result.session.history.flatMap((message) =>
+ message.role === "tool" ? message.content : [],
+ );
+ const toolCall = assistantParts.find(
+ (part) => part.type === "tool-call" && part.toolCallId === "call-1",
+ );
+ const toolResult = toolParts.find(
+ (part) => part.type === "tool-result" && part.toolCallId === "call-1",
+ );
+ expect([
+ result.session.history[0]?.role,
+ toolCall?.type,
+ toolResult?.type,
+ result.session.history.at(-1)?.role,
+ ]).toEqual(["assistant", "tool-call", "tool-result", "assistant"]);
+ expect(toolResult).toEqual(resumedToolResultMessage.content[0]);
+ });
+
it("does not persist provider-executed deferred tool-results as generic tool messages", async () => {
const toolCallId = "srvtoolu_01HhTt9QAEancMSj7jE8CXN7";
const webSearchOutput = [
@@ -5712,11 +5796,11 @@ describe("createToolLoopHarness", () => {
]);
});
- it("preserves AI-SDK StepResult getter properties when synthesizing the inline tool-result message", async () => {
+ it("preserves AI-SDK StepResult getter properties when storing accumulated messages", async () => {
/*
* AI SDK `StepResult` is a class with prototype getters for
- * `content`, `toolCalls`, `toolResults`, and `text`. The inline
- * approval-resume repair must read those getters explicitly; rebuilding
+ * `content`, `toolCalls`, `toolResults`, and `text`. The accumulated
+ * response adapter must read those getters explicitly; rebuilding
* it through object spread would copy only own enumerable properties,
* silently turning all four getter-backed fields into `undefined`.
*/
@@ -5788,7 +5872,24 @@ describe("createToolLoopHarness", () => {
if (onStepFinish) {
void Promise.resolve().then(() => onStepFinish(stepInstance as unknown));
}
- return { fullStream, steps: Promise.resolve([stepInstance]) };
+ return {
+ fullStream,
+ responseMessages: Promise.resolve([
+ {
+ content: [
+ {
+ output: { type: "json", value: { ok: true } },
+ toolCallId: "call-1",
+ toolName: "bash",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ },
+ ...stepInstance.response.messages,
+ ]),
+ steps: Promise.resolve([stepInstance]),
+ };
});
this.generate = vi.fn().mockResolvedValue(new FakeStepResult());
return this as unknown as ToolLoopAgent;
@@ -6360,6 +6461,9 @@ describe("createToolLoopHarness", () => {
) {
const result = agentResults[instanceIndex];
instanceIndex += 1;
+ if (result === undefined) {
+ throw new Error("ToolLoopAgent mock exhausted its scripted results.");
+ }
const { onStepFinish, prepareStep } = settings;
this.generate = vi.fn().mockImplementation(async (input: { messages: unknown[] }) => {
if (prepareStep) {
@@ -6373,7 +6477,7 @@ describe("createToolLoopHarness", () => {
}
generateCalls.push(input.messages);
if (onStepFinish) await onStepFinish(result);
- return result;
+ return createMockGenerateResult(result);
});
return this;
} as MockAgentConstructor);
@@ -6535,7 +6639,7 @@ describe("createToolLoopHarness", () => {
toolResults: [],
};
if (onStepFinish) await onStepFinish(result);
- return result;
+ return createMockGenerateResult(result);
});
return this as unknown as ToolLoopAgent;
} as unknown as ConstructorParameters<typeof ToolLoopAgent> extends [infer S]
@@ -6673,6 +6777,9 @@ describe("createToolLoopHarness", () => {
) {
const result = agentResults[instanceIndex];
instanceIndex += 1;
+ if (result === undefined) {
+ throw new Error("ToolLoopAgent mock exhausted its scripted results.");
+ }
const { onStepFinish, prepareStep } = settings;
this.generate = vi.fn().mockImplementation(async (input: { messages: unknown[] }) => {
if (prepareStep) {
@@ -6686,7 +6793,7 @@ describe("createToolLoopHarness", () => {
}
generateCalls.push(input.messages as Array<{ role: string; content: unknown }>);
if (onStepFinish) await onStepFinish(result);
- return result;
+ return createMockGenerateResult(result);
});
return this as unknown as ToolLoopAgent;
} as unknown as ConstructorParameters<typeof ToolLoopAgent> extends [infer S]
diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.ts
index 2e5c906be..0b4e30606 100644
--- a/packages/eve/src/harness/tool-loop.ts
+++ b/packages/eve/src/harness/tool-loop.ts
@@ -877,17 +877,19 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {
handledInlineToolResultCallIds,
invalidInputToolCallIds,
inlineAuthorizationResults,
- inlineToolResultParts,
trailingInlineToolResultParts,
} = await emitStreamContent(emit, emissionState, streamResult.fullStream, {
excludedActionToolNames,
tools: config.tools,
});
throwIfTurnAborted(config.abortSignal);
- const stepResult = await hooks.stepResult;
+ const [stepResult, accumulatedResponseMessages] = await Promise.all([
+ hooks.stepResult,
+ streamResult.responseMessages,
+ ]);
if (
isEmptyModelResponse(stepResult) &&
- inlineToolResultParts.length === 0 &&
+ extractToolResultCallIds(accumulatedResponseMessages).size === 0 &&
inlineAuthorizationResults.length === 0 &&
trailingInlineToolResultParts.length === 0
) {
@@ -900,59 +902,39 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {
handledInlineToolResultCallIds,
tools: advertisedHarnessTools,
});
- if (
- inlineToolResultParts.length > 0 ||
- inlineAuthorizationResults.length > 0 ||
- trailingInlineToolResultParts.length > 0
- ) {
- const existingToolResults = stepResult.toolResults as TypedToolResult<ToolSet>[];
- const toolResultsByCallId = new Map(
- existingToolResults.map((toolResult) => [toolResult.toolCallId, toolResult]),
- );
- for (const toolResult of inlineAuthorizationResults) {
- toolResultsByCallId.set(toolResult.toolCallId, toolResult);
- }
- /*
- * AI SDK `StepResult` is a class whose `content`,
- * `toolCalls`, `toolResults`, and `text` are prototype
- * getters. Each field is read explicitly here rather than via
- * spread so the returned plain object carries the values —
- * spread would copy only own enumerable properties and the
- * downstream `extractQuestionInputRequests` would crash on
- * `toolCalls === undefined`.
- */
- return {
- content: stepResult.content,
- finishReason: stepResult.finishReason,
- response: {
- ...stepResult.response,
- ...(inlineToolResultParts.length > 0 || trailingInlineToolResultParts.length > 0
- ? {
- messages: insertInlineToolResultMessages({
- append: trailingInlineToolResultParts,
- prepend: inlineToolResultParts,
- responseMessages: stepResult.response.messages,
- }),
- }
- : {}),
- },
- providerMetadata: stepResult.providerMetadata,
- text: stepResult.text,
- toolCalls: stepResult.toolCalls,
- toolResults: [...toolResultsByCallId.values()],
- invalidInputToolCallIds,
- usage: stepResult.usage,
- };
+ const existingToolResults = stepResult.toolResults as TypedToolResult<ToolSet>[];
+ const toolResultsByCallId = new Map(
+ existingToolResults.map((toolResult) => [toolResult.toolCallId, toolResult]),
+ );
+ for (const toolResult of inlineAuthorizationResults) {
+ toolResultsByCallId.set(toolResult.toolCallId, toolResult);
}
- return stepResult;
+ return withAccumulatedResponseMessages({
+ invalidInputToolCallIds,
+ responseMessages: appendMissingToolResultMessages({
+ append: trailingInlineToolResultParts,
+ responseMessages: accumulatedResponseMessages,
+ }),
+ stepResult,
+ toolResults: [...toolResultsByCallId.values()],
+ });
}
- await agent.generate({ abortSignal: config.abortSignal, messages: callMessages });
+ const generateResult = await agent.generate({
+ abortSignal: config.abortSignal,
+ messages: callMessages,
+ });
throwIfTurnAborted(config.abortSignal);
const stepResult = await hooks.stepResult;
- if (isEmptyModelResponse(stepResult)) {
+ if (
+ isEmptyModelResponse(stepResult) &&
+ extractToolResultCallIds(generateResult.responseMessages).size === 0
+ ) {
throw new EmptyModelResponseError();
}
- return stepResult;
+ return withAccumulatedResponseMessages({
+ responseMessages: generateResult.responseMessages,
+ stepResult,
+ });
};
return executeModelCall().catch(rethrowNoOutputAsEmptyResponse);
@@ -1469,17 +1451,46 @@ type ToolResponsePart = Extract<ModelMessage, { role: "tool" }>["content"][numbe
type ToolResultPart = Extract<ToolResponsePart, { type: "tool-result" }>;
type StepResponseMessage = HarnessStepResult["response"]["messages"][number];
-function insertInlineToolResultMessages(input: {
+function withAccumulatedResponseMessages(input: {
+ readonly invalidInputToolCallIds?: ReadonlySet<string>;
+ readonly responseMessages: readonly StepResponseMessage[];
+ readonly stepResult: HarnessStepResult;
+ readonly toolResults?: readonly TypedToolResult<ToolSet>[];
+}): HarnessStepResult {
+ const { stepResult } = input;
+
+ /*
+ * AI SDK `StepResult` fields are prototype getters, so spreading the
+ * instance drops them. Materialize each field while replacing the final
+ * step's messages with the SDK's accumulated response, which also contains
+ * approval-resume results created before the model step.
+ */
+ return {
+ content: stepResult.content,
+ finishReason: stepResult.finishReason,
+ ...(input.invalidInputToolCallIds === undefined
+ ? {}
+ : { invalidInputToolCallIds: input.invalidInputToolCallIds }),
+ providerMetadata: stepResult.providerMetadata,
+ response: {
+ ...stepResult.response,
+ messages: [...input.responseMessages],
+ },
+ text: stepResult.text,
+ toolCalls: stepResult.toolCalls,
+ toolResults: input.toolResults === undefined ? stepResult.toolResults : [...input.toolResults],
+ usage: stepResult.usage,
+ };
+}
+
+function appendMissingToolResultMessages(input: {
readonly append: readonly ToolResultPart[];
- readonly prepend: readonly ToolResultPart[];
readonly responseMessages: readonly StepResponseMessage[];
}): StepResponseMessage[] {
const existingCallIds = extractToolResultCallIds(input.responseMessages);
- const prepend = input.prepend.filter((part) => !existingCallIds.has(part.toolCallId));
const append = input.append.filter((part) => !existingCallIds.has(part.toolCallId));
return [
- ...(prepend.length > 0 ? [{ role: "tool" as const, content: [...prepend] }] : []),
...input.responseMessages,
...(append.length > 0 ? [{ role: "tool" as const, content: [...append] }] : []),
] satisfies StepResponseMessage[];
@@ -1728,11 +1739,10 @@ async function handleStepResult(input: {
]);
const rawResponseMessages = emptyDelivery
? []
- : insertInlineToolResultMessages({
+ : appendMissingToolResultMessages({
append: invalidInputToolErrors.map((toolError) =>
createToolResultMessagePartFromToolError(toolError),
),
- prepend: [],
responseMessages: result.response.messages,
});
const stepOutput = emptyDelivery ? null : resolvedStepOutput;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f44481cc0..817381070 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -841,6 +841,31 @@ importers:
specifier: 'catalog:'
version: 7.0.2
+ e2e/fixtures/agent-tools-hitl-openai:
+ dependencies:
+ '@opentelemetry/core':
+ specifier: 2.6.1
+ version: 2.6.1(@opentelemetry/[email protected])
+ '@opentelemetry/sdk-trace-base':
+ specifier: 2.6.1
+ version: 2.6.1(@opentelemetry/[email protected])
+ '@vercel/otel':
+ specifier: 2.1.2
+ version: 2.1.2(@opentelemetry/[email protected])(@opentelemetry/[email protected])(@opentelemetry/[email protected](@opentelemetry/[email protected]))(@opentelemetry/[email protected](@opentelemetry/[email protected]))(@opentelemetry/[email protected](@opentelemetry/[email protected]))(@opentelemetry/[email protected](@opentelemetry/[email protected]))(@opentelemetry/[email protected](@opentelemetry/[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-tools-sandbox:
dependencies:
'@opentelemetry/core':