The Open Framework for Building Agents
fix(eve): allow fresh reads after empty responses (#3697)
Ben Pankow15ad358c7b1f
diff --git a/.changeset/fresh-empty-response-reads.md b/.changeset/fresh-empty-response-reads.mdnew file mode 100644index 000000000..775b4d29e--- /dev/null+++ b/.changeset/fresh-empty-response-reads.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Allow empty-response recovery to request fresh reads when earlier tool results are stale, while continuing to warn against repeating completed writes and other side effects.diff --git a/packages/eve/src/harness/tool-loop.test.ts b/packages/eve/src/harness/tool-loop.test.tsindex e85910954..ce71ea23f 100644--- a/packages/eve/src/harness/tool-loop.test.ts+++ b/packages/eve/src/harness/tool-loop.test.ts@@ -5783,6 +5783,176 @@ describe("createToolLoopHarness", () => {}});+ it("permits fresh reads in the empty-response retry instruction", async () => {+ const staleReadCall = {+ input: { resource: "report" },+ toolCallId: "stale-read-1",+ toolName: "read_report",+ type: "tool-call" as const,+ };+ const staleReadResult = {+ ...staleReadCall,+ output: { type: "json" as const, value: { rows: 1 } },+ type: "tool-result" as const,+ };+ const tools = new Map([+ [+ "read_report",+ {+ description: "Read the report.",+ execute: vi.fn().mockResolvedValue({ rows: 2 }),+ inputSchema: jsonSchema({ type: "object" }),+ name: "read_report",+ },+ ],+ [+ "write_report",+ {+ description: "Write the report.",+ execute: vi.fn().mockResolvedValue({ saved: true }),+ inputSchema: jsonSchema({ type: "object" }),+ name: "write_report",+ },+ ],+ ]);+ const session = createTestSession({+ agent: {+ modelReference: { id: "test-model" },+ system: "You are a test assistant.",+ tools: [+ {+ description: "Read the report.",+ inputSchema: { type: "object" },+ name: "read_report",+ },+ {+ description: "Write the report.",+ inputSchema: { type: "object" },+ name: "write_report",+ },+ ],+ },+ history: [+ { content: "Read the report.", kind: "user", role: "user" },+ { content: [staleReadCall], role: "assistant" },+ { content: [staleReadResult], role: "tool" },+ ],+ });+ const refreshRequest = "Read the report again and tell me how many rows it has now.";++ setupFirstThenAgent(emptyResult, successResult);+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});+ const { emit } = createEventCollector();+ const runStep = createToolLoopHarness(createTestConfig("conversation", emit, { tools }));++ try {+ await runStep(session, { message: refreshRequest });++ const retryCall = vi.mocked(ToolLoopAgent).mock.calls[1]?.[0];+ const retryAgent = vi.mocked(ToolLoopAgent).mock.results[1]?.value as {+ stream: ReturnType<typeof vi.fn>;+ };+ const retryMessages = retryAgent.stream.mock.calls[0]?.[0]?.messages as Array<{+ content: unknown;+ role: string;+ }>;++ expect(retryCall?.tools).toMatchObject({+ read_report: expect.anything(),+ write_report: expect.anything(),+ });+ expect(retryMessages).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ content: refreshRequest, role: "user" }),+ expect.objectContaining({ content: [staleReadCall], role: "assistant" }),+ expect.objectContaining({ content: [staleReadResult], role: "tool" }),+ ]),+ );+ expect(retryMessages.at(-1)).toMatchObject({+ content: expect.stringContaining("use the appropriate read tools to get fresh results"),+ kind: "execution.retry",+ role: "user",+ });+ expect(retryMessages.at(-1)?.content).not.toMatch(/do not re-run tools/i);+ } finally {+ warnSpy.mockRestore();+ }+ });++ it("warns the model not to repeat a completed write", async () => {+ const writeCall = {+ input: { report: "updated" },+ toolCallId: "write-1",+ toolName: "write_report",+ type: "tool-call" as const,+ };+ const writeResult = {+ ...writeCall,+ output: { type: "json" as const, value: { saved: true } },+ type: "tool-result" as const,+ };+ const completedWrite = {+ finishReason: "tool-calls",+ response: {+ messages: [+ { content: [writeCall], role: "assistant" },+ { content: [writeResult], role: "tool" },+ ],+ },+ text: "",+ toolCalls: [writeCall],+ toolResults: [writeResult],+ };+ const tools = new Map([+ [+ "write_report",+ {+ description: "Write the report.",+ execute: vi.fn().mockResolvedValue({ saved: true }),+ inputSchema: jsonSchema({ type: "object" }),+ name: "write_report",+ },+ ],+ ]);++ setupMockAgentSequence([completedWrite, emptyResult, successResult]);+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});+ const { emit } = createEventCollector();+ const runStep = createToolLoopHarness(createTestConfig("conversation", emit, { tools }));++ try {+ const first = await runStep(createTestSession(), { message: "Update the report." });+ expect(typeof first.next).toBe("function");+ if (typeof first.next !== "function") throw new Error("Expected a tool continuation.");++ await first.next(first.session);++ expect(vi.mocked(ToolLoopAgent)).toHaveBeenCalledTimes(3);+ const retryAgent = vi.mocked(ToolLoopAgent).mock.results[2]?.value as {+ stream: ReturnType<typeof vi.fn>;+ };+ const retryMessages = retryAgent.stream.mock.calls[0]?.[0]?.messages as Array<{+ content: unknown;+ role: string;+ }>;+ expect(retryMessages).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ content: [writeCall], role: "assistant" }),+ expect.objectContaining({ content: [writeResult], role: "tool" }),+ ]),+ );+ expect(retryMessages.at(-1)).toMatchObject({+ content: expect.stringContaining(+ "Do not repeat writes or other side effects that already completed.",+ ),+ kind: "execution.retry",+ role: "user",+ });+ } finally {+ warnSpy.mockRestore();+ }+ });+it("offers conditional delivery on an empty-response retry for a scheduled turn", async () => {setupFirstThenAgent(emptyResult, successResult);const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.tsindex 7c6899d1f..9deae03ab 100644--- a/packages/eve/src/harness/tool-loop.ts+++ b/packages/eve/src/harness/tool-loop.ts@@ -2486,15 +2486,15 @@ function rethrowNoOutputAsEmptyResponse(error: unknown): never {}/**- * Wire-only note the empty-response reissue appends to its retry, so the- * model answers from the tool results already in context instead of- * re-exploring. Each recovery stage declares its own follow-up text: the- * tool recovery prepends {@link buildDisabledToolNote} as a system note- * (its toolset change busts the prompt cache anyway), this one trails as- * a user note to keep the cached prefix valid.+ * Wire-only note the empty-response reissue appends to its retry. Reads may+ * be refreshed when earlier results are stale; completed writes and other+ * side effects must not be repeated. Each recovery stage declares its own+ * follow-up text: tool recovery prepends {@link buildDisabledToolNote} as+ * a system note (its toolset change busts the prompt cache anyway), while+ * this one trails as a user note to keep the cached prefix valid.*/const EMPTY_RESPONSE_NUDGE =- "Your previous reply was empty and was not delivered. Answer now from the tool results above; do not re-run tools or mention this notice.";+ "Your previous reply was empty and was not delivered. Continue the current user request. Reuse completed results when they satisfy the request. If existing results are stale or insufficient, use the appropriate read tools to get fresh results. Do not repeat writes or other side effects that already completed. Do not mention this notice.";function buildEmptyResponseNudge(emptyDeliveryEnabled: boolean): string {if (!emptyDeliveryEnabled) {