From 712960b30a1364efa17a67cd9ab807f0c9cfc4df Mon Sep 17 00:00:00 2001 From: chengke <404835780@qq.com> Date: Sun, 20 Sep 2026 12:06:10 +0800 Subject: [PATCH] Treat unreachable memory search and send as warnings. Online chat can call Memento before it is deployed; keep the turn going and mark those misses for later. Co-authored-by: Cursor --- src/agent-harness/memory-text.test.ts | 11 +++++++++++ src/agent-harness/memory-text.ts | 27 +++++++++++++++++++-------- src/agent-harness/runtime.test.ts | 20 ++++++++++++++++++++ src/agent-harness/runtime.ts | 9 ++++++++- src/integrations/memento/client.ts | 1 + 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/agent-harness/memory-text.test.ts b/src/agent-harness/memory-text.test.ts index aa6384e..1967ad1 100644 --- a/src/agent-harness/memory-text.test.ts +++ b/src/agent-harness/memory-text.test.ts @@ -33,4 +33,15 @@ describe("memoryToolText", () => { expect(text).toContain('resultCount="0"') expect(text).not.toContain(" { + const text = memoryToolText.formatWarning({ + operation: "search", + message: "MEMENTO_BASE_URL is required.", + }) + + expect(text).toContain('') + expect(text).toContain("MEMENTO_BASE_URL is required.") + expect(text).not.toContain('status="error"') + }) }) diff --git a/src/agent-harness/memory-text.ts b/src/agent-harness/memory-text.ts index 6f170e0..e0c0278 100644 --- a/src/agent-harness/memory-text.ts +++ b/src/agent-harness/memory-text.ts @@ -19,17 +19,28 @@ export const memoryToolText = { }, formatError(input: ErrorTextInput): string { - return [ - formatOpenTag("memory", { - operation: input.operation, - status: "error", - }), - formatTextTag("message", input.message), - "", - ].join("\n") + return formatStatusBlock(input, "error") + }, + + formatWarning(input: ErrorTextInput): string { + return formatStatusBlock(input, "warning") }, } as const +function formatStatusBlock( + input: ErrorTextInput, + status: "error" | "warning", +): string { + return [ + formatOpenTag("memory", { + operation: input.operation, + status, + }), + formatTextTag("message", input.message), + "", + ].join("\n") +} + function formatMemoryItems(response: MemorySearchResponse): string { if (response.items.length === 0) return "" diff --git a/src/agent-harness/runtime.test.ts b/src/agent-harness/runtime.test.ts index 5778e4c..b285a1f 100644 --- a/src/agent-harness/runtime.test.ts +++ b/src/agent-harness/runtime.test.ts @@ -576,6 +576,26 @@ describe("agent harness runtime", () => { }) }) + it("returns a warning when memory_search cannot reach memory", async () => { + const search = vi + .fn() + .mockRejectedValue(new Error("MEMENTO_BASE_URL is required.")) + const tools = createHarnessTools({ + state: {}, + ledger: createEvidenceLedger(), + memoryTools: makeMemoryTools(search), + knowhereTools: makeKnowhereTools(), + recentTurns: [], + }) + + const searchText = await executeTool(tools.memory_search, { + query: "毛利率", + }) + expect(searchText).toContain('') + expect(searchText).toContain("MEMENTO_BASE_URL is required.") + expect(searchText).not.toContain('status="error"') + }) + it("exposes full prior-turn content through policy-approved readPriorTurn", async () => { const state: { contextPolicy?: ContextPolicy diff --git a/src/agent-harness/runtime.ts b/src/agent-harness/runtime.ts index 2b2a748..73dee74 100644 --- a/src/agent-harness/runtime.ts +++ b/src/agent-harness/runtime.ts @@ -7,6 +7,8 @@ import { } from "ai" import { z } from "zod" +import { logger } from "@/lib/logger" + import { composeAnswerContext, type ReadTableHtml } from "./answer-context" import { createEvidenceLedger } from "./ledger" import { knowhereToolText } from "./knowhere-text" @@ -904,7 +906,12 @@ async function executeMemorySearch(input: { accumulateMemoryItems(input.state, response.items) return memoryToolText.formatSearch(response) } catch (error) { - return memoryToolText.formatError({ + // TODO: Memento 未部署时搜索会失败,先只记警告;部署并对上地址后应能搜到。 + logger.warn("chat: failed to search memory", { + query: input.request.query, + error: formatUnknownError(error), + }) + return memoryToolText.formatWarning({ operation: "search", message: formatUnknownError(error), }) diff --git a/src/integrations/memento/client.ts b/src/integrations/memento/client.ts index c7baafe..5ec63d4 100644 --- a/src/integrations/memento/client.ts +++ b/src/integrations/memento/client.ts @@ -36,6 +36,7 @@ export async function captureMemoryTurn( try { await postJson("/turns/capture", input) } catch (error) { + // TODO: Memento 未部署时发送会失败,先只记警告;部署并对上地址后应能发到。 logger.warn("chat: failed to capture memory turn", { workspaceId: input.workspaceId, error: summarizeUnknownError(error),