Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): settle abandoned tool calls before model retries (#3309)

roboeve[bot]f528669ac5a2
diff --git a/.changeset/settle-retried-tool-calls.md b/.changeset/settle-retried-tool-calls.md
new file mode 100644
index 000000000..48fbb2094
--- /dev/null
+++ b/.changeset/settle-retried-tool-calls.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Settle tool calls when a model call is retried and remove unresolved tool parts when a turn ends.
diff --git a/packages/eve/src/client/message-reducer.test.ts b/packages/eve/src/client/message-reducer.test.ts
index 942cf9c6c..c59126fa8 100644
--- a/packages/eve/src/client/message-reducer.test.ts
+++ b/packages/eve/src/client/message-reducer.test.ts
@@ -379,6 +379,135 @@ describe("defaultMessageReducer", () => {
]);
});
+ it("removes unresolved tool parts on every turn end and preserves terminal and approval states", () => {
+ const terminalEvents = [
+ createTurnCompletedEvent({
+ sequence: 1,
+ turnId: "turn_1",
+ }),
+ createTurnCancelledEvent({ sequence: 1, turnId: "turn_1" }),
+ createTurnFailedEvent({
+ code: "MODEL_FAILED",
+ message: "model failed",
+ sequence: 1,
+ turnId: "turn_1",
+ }),
+ ] as const;
+
+ for (const terminalEvent of terminalEvents) {
+ const reducer = defaultMessageReducer();
+ const data = reduceServerEvents(reducer, reducer.initial(), [
+ createActionInputAppendedEvent({
+ callId: "call_streaming",
+ inputTextDelta: "{",
+ sequence: 1,
+ stepIndex: 0,
+ toolName: "render",
+ turnId: "turn_1",
+ }),
+ createActionsRequestedEvent({
+ actions: [
+ {
+ callId: "call_available",
+ input: { title: "Pending" },
+ kind: "tool-call",
+ toolName: "render",
+ },
+ {
+ callId: "call_approval",
+ input: { title: "Approve" },
+ kind: "tool-call",
+ toolName: "render",
+ },
+ {
+ callId: "call_completed",
+ input: { title: "Done" },
+ kind: "tool-call",
+ toolName: "render",
+ },
+ ],
+ sequence: 1,
+ stepIndex: 0,
+ turnId: "turn_1",
+ }),
+ createInputRequestedEvent({
+ requests: [
+ {
+ action: {
+ callId: "call_approval",
+ input: { title: "Approve" },
+ kind: "tool-call",
+ toolName: "render",
+ },
+ kind: "tool-approval",
+ prompt: "Approve?",
+ requestId: "approval_1",
+ },
+ ],
+ sequence: 1,
+ stepIndex: 0,
+ turnId: "turn_1",
+ }),
+ createActionResultEvent({
+ result: {
+ callId: "call_completed",
+ kind: "tool-result",
+ output: { saved: true },
+ toolName: "render",
+ },
+ sequence: 1,
+ stepIndex: 0,
+ turnId: "turn_1",
+ }),
+ terminalEvent,
+ ]);
+
+ expect(findToolPart(data, "call_streaming")).toBeUndefined();
+ expect(findToolPart(data, "call_available")).toBeUndefined();
+ expect(findToolPart(data, "call_approval")).toMatchObject({ state: "approval-requested" });
+ expect(findToolPart(data, "call_completed")).toMatchObject({
+ output: { saved: true },
+ state: "output-available",
+ });
+ }
+ });
+
+ it("removes tool parts settled by a retried model-call attempt", () => {
+ const reducer = defaultMessageReducer();
+ const data = reduceServerEvents(reducer, reducer.initial(), [
+ createActionsRequestedEvent({
+ actions: [
+ {
+ callId: "call_abandoned",
+ input: { title: "Pending" },
+ kind: "tool-call",
+ toolName: "render",
+ },
+ ],
+ sequence: 1,
+ stepIndex: 0,
+ turnId: "turn_1",
+ }),
+ createActionResultEvent({
+ result: {
+ callId: "call_abandoned",
+ isError: true,
+ kind: "tool-result",
+ output: {
+ code: "MODEL_CALL_ATTEMPT_RETRIED",
+ message: "The model call attempt was retried.",
+ },
+ toolName: "render",
+ },
+ sequence: 1,
+ stepIndex: 0,
+ turnId: "turn_1",
+ }),
+ ]);
+
+ expect(findToolPart(data, "call_abandoned")).toBeUndefined();
+ });
+
it("does not create an assistant message when a turn fails before streaming", () => {
const reducer = defaultMessageReducer();
const data = reduceServerEvents(reducer, reducer.initial(), [
diff --git a/packages/eve/src/client/message-reducer.ts b/packages/eve/src/client/message-reducer.ts
index 2dd242c1b..f14904eca 100644
--- a/packages/eve/src/client/message-reducer.ts
+++ b/packages/eve/src/client/message-reducer.ts
@@ -30,7 +30,12 @@ import {
} from "#client/message-reducer-primitives.js";
import { messageRun } from "#client/message-run-parts.js";
import type { AuthorizationCompletedStreamEvent } from "#protocol/message.js";
-import { foldSession, initialSessionProjection } from "#protocol/session-projection.js";
+import {
+ foldSession,
+ initialSessionProjection,
+ reportedCallStatus,
+ type SessionProjection,
+} from "#protocol/session-projection.js";
import { toolPartState } from "#client/tool-part-state.js";
export type {
@@ -232,6 +237,15 @@ function reduceContent(data: EveMessageData, event: EveAgentReducerEvent): EveMe
// A task call's result is only its start receipt, which can arrive after the task settled.
if (conversationProjection(data).calls[event.data.result.callId]?.taskId !== undefined)
return data;
+ // A retried model-call attempt's calls never ran; the replacement attempt re-requests them.
+ if (event.data.error?.code === "MODEL_CALL_ATTEMPT_RETRIED") {
+ return updateAssistantMessage(data, event.data.turnId, (message) => ({
+ ...message,
+ parts: message.parts.filter(
+ (part) => part.type !== "dynamic-tool" || part.toolCallId !== event.data.result.callId,
+ ),
+ }));
+ }
const existing = findToolPart(data, event.data.result.callId);
const descriptor = normalizeActionResult(event.data.result);
const succeeded =
@@ -363,7 +377,10 @@ function reduceContent(data: EveMessageData, event: EveAgentReducerEvent): EveMe
return updateAssistantMessage(data, event.data.turnId, (message) => ({
...message,
metadata: { ...message.metadata, status: "complete" },
- parts: removeStreamingToolParts(closeStreamingRuns(message.parts)),
+ parts: removeUnsettledToolParts(
+ closeStreamingRuns(message.parts),
+ conversationProjection(data),
+ ),
}));
}
@@ -425,8 +442,26 @@ function closeStreamingRuns(
);
}
-function removeStreamingToolParts(parts: readonly EveMessagePart[]): readonly EveMessagePart[] {
- return parts.filter((part) => part.type !== "dynamic-tool" || part.state !== "input-streaming");
+/**
+ * Drops tool parts a finished turn left without a result: input that never finished streaming,
+ * and validated requests whose call never ran. A task call outlives its turn, a call awaiting
+ * input still has a pending request, and a call the projection doesn't know keeps its state.
+ */
+function removeUnsettledToolParts(
+ parts: readonly EveMessagePart[],
+ projection: SessionProjection,
+): readonly EveMessagePart[] {
+ return parts.filter((part) => {
+ if (part.type !== "dynamic-tool") return true;
+ if (part.state === "input-streaming") return false;
+ if (part.state !== "input-available") return true;
+ const call = projection.calls[part.toolCallId];
+ return (
+ call === undefined ||
+ call.taskId !== undefined ||
+ reportedCallStatus(projection, call) !== "running"
+ );
+ });
}
function updateAssistantMessage(
diff --git a/packages/eve/src/harness/emission.ts b/packages/eve/src/harness/emission.ts
index 9184ac9b8..232f5682e 100644
--- a/packages/eve/src/harness/emission.ts
+++ b/packages/eve/src/harness/emission.ts
@@ -72,6 +72,7 @@ interface StreamActionEmissionOptions {
*/
readonly hidesHeldText?: boolean;
readonly tools: HarnessToolMap;
+ readonly unsettledActionToolNames?: Map<string, string>;
}
/**
@@ -91,6 +92,11 @@ export async function emitStreamContent(
const orderedEmitter = createOrderedStreamEmitter(emitFn);
const providerActionBatch = createProviderStreamActionBatch({
emitFn: orderedEmitter.emit,
+ onActionsEmitted: (actions) => {
+ for (const { request, toolName } of actions) {
+ options?.unsettledActionToolNames?.set(request.action.callId, toolName);
+ }
+ },
state,
});
try {
@@ -161,8 +167,8 @@ async function consumeStreamContent(
callId: string,
toolName: string,
inputTextDelta: string,
- ): Promise<void> =>
- emitFn(
+ ): Promise<void> => {
+ await emitFn(
createActionInputAppendedEvent({
callId,
inputTextDelta,
@@ -172,8 +178,13 @@ async function consumeStreamContent(
turnId: state.turnId,
}),
);
+ options?.unsettledActionToolNames?.set(callId, toolName);
+ };
- const emitActionRequest = async (projection: RuntimeActionRequestProjection): Promise<void> => {
+ const emitActionRequest = async (
+ projection: RuntimeActionRequestProjection,
+ toolName: string,
+ ): Promise<void> => {
const { action } = projection;
if (emittedActionCallIds.has(action.callId)) {
return;
@@ -194,6 +205,7 @@ async function consumeStreamContent(
turnId: state.turnId,
}),
);
+ options?.unsettledActionToolNames?.set(action.callId, toolName);
};
const collectProviderToolCall = async (toolCall: {
@@ -226,7 +238,7 @@ async function consumeStreamContent(
}
actionInputs.set(resolved.request.action.callId, resolved.request.action.input);
- providerActionBatch.observe(resolved.request);
+ providerActionBatch.observe(resolved.request, toolCall.toolName);
};
const emitActionResult = async (result: RuntimeToolResultActionResult): Promise<void> => {
@@ -253,6 +265,7 @@ async function consumeStreamContent(
turnId: state.turnId,
}),
);
+ options?.unsettledActionToolNames?.delete(result.callId);
};
const emitActionPartial = async (result: RuntimeToolResultActionResult): Promise<void> => {
@@ -288,6 +301,7 @@ async function consumeStreamContent(
toolCall,
tools: options.tools,
}),
+ toolCall.toolName,
);
} catch (error) {
if (error instanceof TypeError) {
diff --git a/packages/eve/src/harness/model-call/call.ts b/packages/eve/src/harness/model-call/call.ts
index 6f3e0818a..3bc9f3197 100644
--- a/packages/eve/src/harness/model-call/call.ts
+++ b/packages/eve/src/harness/model-call/call.ts
@@ -32,6 +32,7 @@ import {
} from "#harness/prompt-cache.js";
import { estimateRequestEnvelope } from "#harness/request-envelope.js";
import { summarizeKnownError } from "#harness/semantic-errors/index.js";
+import { discardAttempt } from "#harness/session-machine/transitions.js";
import { activeTurnId } from "#harness/session-machine/view.js";
import type { Step } from "#harness/step/context.js";
import {
@@ -149,12 +150,22 @@ export class ModelCaller {
/** Calls the model, retrying transient failures. */
async call(options: ModelCallOptions): Promise<HarnessStepResult> {
+ // Calls the current attempt announced that haven't received a result yet, by tool name.
+ let unsettledActionToolNames = new Map<string, string>();
return await runModelCallWithRetries(
- (attempt) =>
- this.attempt({
- ...options,
- suppressStepStartedEmission: attempt === 1 ? options.suppressStepStartedEmission : true,
- }),
+ async (attempt) => {
+ if (attempt > 1) {
+ await this.settleRetriedActions(unsettledActionToolNames);
+ unsettledActionToolNames = new Map<string, string>();
+ }
+ return await this.attempt(
+ {
+ ...options,
+ suppressStepStartedEmission: attempt === 1 ? options.suppressStepStartedEmission : true,
+ },
+ unsettledActionToolNames,
+ );
+ },
{
canRetry: () => this.compactionFailure === undefined,
sessionId: this.step.session.sessionId,
@@ -164,6 +175,19 @@ export class ModelCaller {
);
}
+ /**
+ * Answers the calls a discarded attempt announced, so none stays open once the replacement
+ * attempt starts. The replacement re-requests whatever the model still wants to run.
+ */
+ private async settleRetriedActions(unsettled: Map<string, string>): Promise<void> {
+ // One call per transition: publishing isn't atomic, so a publish that fails partway leaves
+ // only the calls it didn't reach for the next attempt to settle.
+ for (const [callId, toolName] of unsettled) {
+ await this.step.apply(discardAttempt(this.step.view(), { calls: [{ callId, toolName }] }));
+ unsettled.delete(callId);
+ }
+ }
+
/** A failed compaction fails the step, whatever recovery the call attempted. */
throwIfCompactionFailed(): void {
if (this.compactionFailure !== undefined) throw this.compactionFailure.error;
@@ -283,7 +307,10 @@ export class ModelCaller {
return await this.prepare(options);
}
- private async attempt(options: ModelCallOptions): Promise<HarnessStepResult> {
+ private async attempt(
+ options: ModelCallOptions,
+ unsettledActionToolNames: Map<string, string>,
+ ): Promise<HarnessStepResult> {
const { step } = this;
const { generation, model } = this.input;
const tools = await this.compact(options, await this.prepare(options));
@@ -371,7 +398,13 @@ export class ModelCaller {
const result =
step.emit === undefined
? await this.generate(agent, callMessages, hooks.stepResult)
- : await this.stream(agent, callMessages, hooks.stepResult, tools);
+ : await this.stream(
+ agent,
+ callMessages,
+ hooks.stepResult,
+ tools,
+ unsettledActionToolNames,
+ );
await attempt?.complete();
return result;
} catch (error) {
@@ -388,6 +421,7 @@ export class ModelCaller {
messages: ModelMessage[],
stepResultPromise: Promise<HarnessStepResult>,
tools: ModelTools,
+ unsettledActionToolNames: Map<string, string>,
): Promise<HarnessStepResult> {
const { step } = this;
const { generation } = this.input;
@@ -407,6 +441,7 @@ export class ModelCaller {
excludedActionToolNames,
hidesHeldText: this.input.hidesHeldText && workingTaskIds(step.session).length > 0,
tools: tools.presentationTools,
+ unsettledActionToolNames,
},
);
throwIfTurnAborted(step.config.abortSignal);
diff --git a/packages/eve/src/harness/session-machine/transitions.ts b/packages/eve/src/harness/session-machine/transitions.ts
index 8f4f73e0f..d4a59b388 100644
--- a/packages/eve/src/harness/session-machine/transitions.ts
+++ b/packages/eve/src/harness/session-machine/transitions.ts
@@ -284,6 +284,45 @@ export function settle(view: SessionView, input: { readonly results: readonly Se
} satisfies Transition;
}
+/** A call a discarded model-call attempt announced without a result. */
+export interface DiscardedCall {
+ readonly callId: string;
+ readonly toolName: string;
+}
+
+/** What a discarded attempt's calls report: the replacement attempt re-requests what it needs. */
+const RETRIED_CALL_RESULT = {
+ code: "MODEL_CALL_ATTEMPT_RETRIED",
+ message: "The model call attempt was retried before this tool could run.",
+} as const;
+
+/**
+ * A model-call attempt failed and the step retries it. The calls the attempt announced never
+ * ran, so each settles as failed before the replacement attempt streams. Nothing reaches
+ * history: the discarded response was never committed.
+ */
+export function discardAttempt(
+ view: SessionView,
+ input: { readonly calls: readonly DiscardedCall[] },
+): Transition {
+ const coordinates = at(view.projection);
+ return unchanged(
+ view,
+ input.calls.map(({ callId, toolName }) =>
+ createActionResultEvent({
+ ...coordinates,
+ result: {
+ callId,
+ isError: true,
+ kind: "tool-result",
+ output: { ...RETRIED_CALL_RESULT },
+ toolName,
+ },
+ }),
+ ),
+ );
+}
+
/**
* A model response made calls the runtime runs. The response waits in a suspended step until
* every call it made has a result, so a call never reaches the model without its result.
diff --git a/packages/eve/src/harness/stream-actions.ts b/packages/eve/src/harness/stream-actions.ts
index 81fa5ce19..544c50ff2 100644
--- a/packages/eve/src/harness/stream-actions.ts
+++ b/packages/eve/src/harness/stream-actions.ts
@@ -11,18 +11,24 @@ interface ActionEventCoordinates {
readonly turnId: string;
}
+interface EmittedProviderAction {
+ readonly request: RuntimeActionRequestProjection;
+ readonly toolName: string;
+}
+
interface ProviderStreamActionBatch {
cancel(): Promise<void>;
flush(): Promise<void>;
- observe(action: RuntimeActionRequestProjection): void;
+ observe(action: RuntimeActionRequestProjection, toolName: string): void;
}
/** Batches provider-managed calls that arrive in one streamed model response. */
export function createProviderStreamActionBatch(input: {
readonly emitFn: HarnessEmitFn;
+ readonly onActionsEmitted?: (actions: readonly EmittedProviderAction[]) => void;
readonly state: ActionEventCoordinates;
}): ProviderStreamActionBatch {
- const pendingActions = new Map<string, RuntimeActionRequestProjection>();
+ const pendingActions = new Map<string, EmittedProviderAction>();
let actionFlush: Promise<void> = Promise.resolve();
let actionFlushError: unknown;
let actionFlushTimer: ReturnType<typeof setTimeout> | undefined;
@@ -36,7 +42,8 @@ export function createProviderStreamActionBatch(input: {
}
if (pendingActions.size === 0) return;
- const projections = [...pendingActions.values()];
+ const actions = [...pendingActions.values()];
+ const projections = actions.map(({ request }) => request);
pendingActions.clear();
await input.emitFn(
createActionsRequestedEvent({
@@ -47,6 +54,7 @@ export function createProviderStreamActionBatch(input: {
turnId: input.state.turnId,
}),
);
+ input.onActionsEmitted?.(actions);
};
const scheduleFlush = (): void => {
@@ -88,9 +96,9 @@ export function createProviderStreamActionBatch(input: {
releaseFlushTimer();
await actionFlush;
},
- observe(action) {
+ observe(action, toolName) {
if (cancelled) return;
- pendingActions.set(action.action.callId, action);
+ pendingActions.set(action.action.callId, { request: action, toolName });
scheduleFlush();
},
async flush() {
diff --git a/packages/eve/src/harness/tool-loop-stream-retry.integration.test.ts b/packages/eve/src/harness/tool-loop-stream-retry.integration.test.ts
index 00783dc70..916e3cd8d 100644
--- a/packages/eve/src/harness/tool-loop-stream-retry.integration.test.ts
+++ b/packages/eve/src/harness/tool-loop-stream-retry.integration.test.ts
@@ -1,9 +1,11 @@
-import { type LanguageModel } from "ai";
+import { jsonSchema, type LanguageModel } from "ai";
import { MockLanguageModelV3 } from "ai/test";
import { afterEach, describe, expect, it, vi } from "vitest";
+import { defaultMessageReducer } from "#client/message-reducer.js";
import { createToolLoopHarness } from "#harness/tool-loop.js";
import type { HarnessEmitFn, HarnessSession, ToolLoopHarnessConfig } from "#harness/types.js";
+import { stampTestEvents } from "#internal/testing/events.js";
import type { UnstampedMessageStreamEvent } from "#protocol/message.js";
type StreamResult = Awaited<ReturnType<MockLanguageModelV3["doStream"]>>;
@@ -44,11 +46,15 @@ function createEventCollector(): {
};
}
-function createConfig(model: LanguageModel, emit: HarnessEmitFn): ToolLoopHarnessConfig {
+function createConfig(
+ model: LanguageModel,
+ emit: HarnessEmitFn,
+ tools: ToolLoopHarnessConfig["tools"] = new Map(),
+): ToolLoopHarnessConfig {
return {
handleEvent: emit,
resolveModel: vi.fn().mockResolvedValue(model),
- tools: new Map(),
+ tools,
};
}
@@ -85,11 +91,246 @@ function enqueueTextSuccess(
controller.close();
}
+function enqueueToolCall(
+ controller: ReadableStreamDefaultController<StreamPart>,
+ callId: string,
+ note: string,
+): void {
+ const input = JSON.stringify({ note });
+ controller.enqueue({ id: callId, toolName: "save_note", type: "tool-input-start" });
+ controller.enqueue({ delta: input, id: callId, type: "tool-input-delta" });
+ controller.enqueue({ id: callId, type: "tool-input-end" });
+ controller.enqueue({ input, toolCallId: callId, toolName: "save_note", type: "tool-call" });
+}
+
afterEach(() => {
vi.restoreAllMocks();
});
describe("tool loop streamed provider retries", () => {
+ it("settles tool calls emitted by an abandoned retry attempt", async () => {
+ vi.spyOn(Math, "random").mockReturnValue(0);
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+
+ let attempt = 0;
+ const doStream = vi.fn(async () => ({
+ stream: new ReadableStream<StreamPart>({
+ start(controller) {
+ attempt += 1;
+ controller.enqueue({ type: "stream-start", warnings: [] });
+ if (attempt === 1) {
+ enqueueToolCall(controller, "call_a1", "first note");
+ controller.enqueue({ id: "call_a2", toolName: "save_note", type: "tool-input-start" });
+ controller.enqueue({
+ delta: '{"note":"second',
+ id: "call_a2",
+ type: "tool-input-delta",
+ });
+ controller.enqueue({
+ error: { message: "Overloaded", type: "overloaded_error" },
+ type: "error",
+ });
+ controller.close();
+ return;
+ }
+ if (attempt === 2) {
+ enqueueToolCall(controller, "call_b1", "first note");
+ enqueueToolCall(controller, "call_b2", "second note");
+ controller.enqueue({
+ finishReason: { raw: undefined, unified: "tool-calls" },
+ type: "finish",
+ usage,
+ });
+ controller.close();
+ return;
+ }
+ enqueueTextSuccess(controller, "Notes saved.");
+ },
+ }),
+ }));
+ const model = new MockLanguageModelV3({
+ doStream,
+ modelId: "retry-integration-model",
+ provider: "eve-integration-mock",
+ });
+ const execute = vi.fn(async ({ note }: { readonly note: string }) => ({ saved: note }));
+ const tools: ToolLoopHarnessConfig["tools"] = new Map([
+ [
+ "save_note",
+ {
+ description: "Save a note.",
+ execute,
+ inputSchema: jsonSchema({
+ additionalProperties: false,
+ properties: { note: { type: "string" } },
+ required: ["note"],
+ type: "object",
+ }),
+ name: "save_note",
+ },
+ ],
+ ]);
+ const { emit, events } = createEventCollector();
+
+ const toolStep = await createToolLoopHarness(createConfig(model, emit, tools))(
+ createSession(),
+ {
+ message: "Save both notes.",
+ },
+ );
+ if (typeof toolStep.next !== "function") {
+ throw new TypeError("Expected tool calls to continue the tool loop.");
+ }
+ await toolStep.next(toolStep.session);
+
+ expect(doStream).toHaveBeenCalledTimes(3);
+ expect(execute).toHaveBeenCalledTimes(2);
+ const retryStart = events.findIndex(
+ (event) =>
+ (event.type === "action.input.appended" && event.data.callId === "call_b1") ||
+ (event.type === "actions.requested" &&
+ event.data.actions.some((action) => action.callId === "call_b1")),
+ );
+ const abandonedResults = events.filter(
+ (event) =>
+ event.type === "action.result" &&
+ (event.data.result.callId === "call_a1" || event.data.result.callId === "call_a2"),
+ );
+ expect(abandonedResults).toHaveLength(2);
+ expect(abandonedResults).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ data: expect.objectContaining({
+ error: expect.objectContaining({ message: expect.stringMatching(/retried/i) }),
+ result: expect.objectContaining({ callId: "call_a1", isError: true }),
+ status: "failed",
+ }),
+ }),
+ expect.objectContaining({
+ data: expect.objectContaining({
+ error: expect.objectContaining({ message: expect.stringMatching(/retried/i) }),
+ result: expect.objectContaining({ callId: "call_a2", isError: true }),
+ status: "failed",
+ }),
+ }),
+ ]),
+ );
+ expect(events.indexOf(abandonedResults[1]!)).toBeLessThan(retryStart);
+
+ const reducer = defaultMessageReducer();
+ const projection = stampTestEvents(events).reduce(reducer.reduce, reducer.initial());
+ expect(
+ projection.messages.flatMap((message) =>
+ message.parts.flatMap((part) =>
+ part.type === "dynamic-tool" ? [[part.toolCallId, part.state]] : [],
+ ),
+ ),
+ ).toEqual([
+ ["call_b1", "output-available"],
+ ["call_b2", "output-available"],
+ ]);
+ });
+
+ it("settles each abandoned call once when a settlement publish fails and retries", async () => {
+ vi.spyOn(Math, "random").mockReturnValue(0);
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+
+ let attempt = 0;
+ const doStream = vi.fn(async () => ({
+ stream: new ReadableStream<StreamPart>({
+ start(controller) {
+ attempt += 1;
+ controller.enqueue({ type: "stream-start", warnings: [] });
+ if (attempt === 1) {
+ enqueueToolCall(controller, "call_a1", "first note");
+ enqueueToolCall(controller, "call_a2", "second note");
+ controller.enqueue({
+ error: { message: "Overloaded", type: "overloaded_error" },
+ type: "error",
+ });
+ controller.close();
+ return;
+ }
+ if (attempt === 2) {
+ enqueueToolCall(controller, "call_b1", "first note");
+ enqueueToolCall(controller, "call_b2", "second note");
+ controller.enqueue({
+ finishReason: { raw: undefined, unified: "tool-calls" },
+ type: "finish",
+ usage,
+ });
+ controller.close();
+ return;
+ }
+ enqueueTextSuccess(controller, "Notes saved.");
+ },
+ }),
+ }));
+ const model = new MockLanguageModelV3({
+ doStream,
+ modelId: "retry-integration-model",
+ provider: "eve-integration-mock",
+ });
+ const execute = vi.fn(async ({ note }: { readonly note: string }) => ({ saved: note }));
+ const tools: ToolLoopHarnessConfig["tools"] = new Map([
+ [
+ "save_note",
+ {
+ description: "Save a note.",
+ execute,
+ inputSchema: jsonSchema({
+ additionalProperties: false,
+ properties: { note: { type: "string" } },
+ required: ["note"],
+ type: "object",
+ }),
+ name: "save_note",
+ },
+ ],
+ ]);
+ const events: UnstampedMessageStreamEvent[] = [];
+ let sinkFailed = false;
+ const emit: HarnessEmitFn = async (event) => {
+ if (!sinkFailed && event.type === "action.result" && event.data.result.callId === "call_a2") {
+ sinkFailed = true;
+ // A transient sink failure the model-call retry classifies as retryable.
+ throw Object.assign(new Error("Event sink unavailable"), { statusCode: 503 });
+ }
+ events.push(event);
+ };
+
+ const toolStep = await createToolLoopHarness(createConfig(model, emit, tools))(
+ createSession(),
+ { message: "Save both notes." },
+ );
+ if (typeof toolStep.next !== "function") {
+ throw new TypeError("Expected tool calls to continue the tool loop.");
+ }
+ await toolStep.next(toolStep.session);
+
+ expect(sinkFailed).toBe(true);
+ // The failed settlement consumed the second attempt before it streamed.
+ expect(doStream).toHaveBeenCalledTimes(3);
+ const abandonedResultIds = events.flatMap((event) =>
+ event.type === "action.result" &&
+ (event.data.result.callId === "call_a1" || event.data.result.callId === "call_a2")
+ ? [event.data.result.callId]
+ : [],
+ );
+ expect(abandonedResultIds).toEqual(["call_a1", "call_a2"]);
+ const lastAbandoned = events.findLastIndex(
+ (event) => event.type === "action.result" && event.data.result.callId === "call_a2",
+ );
+ const replacementStart = events.findIndex(
+ (event) => event.type === "action.input.appended" && event.data.callId === "call_b1",
+ );
+ expect(replacementStart).toBeGreaterThan(-1);
+ expect(lastAbandoned).toBeLessThan(replacementStart);
+ expect(execute).toHaveBeenCalledTimes(2);
+ });
+
it("retries an overloaded stream and preserves prior work", async () => {
vi.spyOn(Math, "random").mockReturnValue(0);
vi.spyOn(console, "error").mockImplementation(() => {});
@@ -150,10 +391,27 @@ describe("tool loop streamed provider retries", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
vi.spyOn(console, "warn").mockImplementation(() => {});
+ let attempt = 0;
const doStream = vi.fn(async () => ({
stream: new ReadableStream<StreamPart>({
start(controller) {
- enqueueOverload(controller);
+ attempt += 1;
+ controller.enqueue({ type: "stream-start", warnings: [] });
+ controller.enqueue({
+ id: `call_terminal_${attempt}`,
+ toolName: "save_note",
+ type: "tool-input-start",
+ });
+ controller.enqueue({
+ delta: '{"note":"partial',
+ id: `call_terminal_${attempt}`,
+ type: "tool-input-delta",
+ });
+ controller.enqueue({
+ error: { message: "Overloaded", type: "overloaded_error" },
+ type: "error",
+ });
+ controller.close();
},
}),
}));
@@ -183,5 +441,37 @@ describe("tool loop streamed provider retries", () => {
expect(events.filter((event) => event.type === "turn.failed")).toHaveLength(1);
expect(events.filter((event) => event.type === "session.failed")).toHaveLength(0);
expect(events.at(-1)?.type).toBe("session.waiting");
+ const abandonedResults = events.filter(
+ (event) =>
+ event.type === "action.result" && event.data.result.callId.startsWith("call_terminal_"),
+ );
+ expect(abandonedResults).toHaveLength(2);
+ expect(
+ abandonedResults.map((event) =>
+ event.type === "action.result" ? event.data.result.callId : undefined,
+ ),
+ ).toEqual(["call_terminal_1", "call_terminal_2"]);
+ expect(
+ abandonedResults.map((event) =>
+ event.type === "action.result" ? event.data.error?.message : undefined,
+ ),
+ ).toEqual([
+ "The model call attempt was retried before this tool could run.",
+ "The model call attempt was retried before this tool could run.",
+ ]);
+ expect(events.lastIndexOf(abandonedResults[1]!)).toBeLessThan(
+ events.findIndex(
+ (event) =>
+ event.type === "action.input.appended" && event.data.callId === "call_terminal_3",
+ ),
+ );
+
+ const reducer = defaultMessageReducer();
+ const projection = stampTestEvents(events).reduce(reducer.reduce, reducer.initial());
+ expect(
+ projection.messages.flatMap((message) =>
+ message.parts.filter((part) => part.type === "dynamic-tool"),
+ ),
+ ).toEqual([]);
});
});