From df7f968581c460b702f95f0d39bc7e6f2ca1a8da Mon Sep 17 00:00:00 2001 From: Frank Yang Date: Mon, 23 Mar 2026 13:20:13 +0800 Subject: [PATCH] fix(memory-core): align prompt with available tools --- extensions/memory-core/index.test.ts | 19 ++++++++++++++++--- extensions/memory-core/index.ts | 23 ++++++++++++++++++----- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/extensions/memory-core/index.test.ts b/extensions/memory-core/index.test.ts index b1855aa7f3f..27e7eacbcc5 100644 --- a/extensions/memory-core/index.test.ts +++ b/extensions/memory-core/index.test.ts @@ -6,18 +6,31 @@ describe("buildPromptSection", () => { expect(buildPromptSection({ availableTools: new Set() })).toEqual([]); }); - it("returns Memory Recall section when memory_search is available", () => { - const result = buildPromptSection({ availableTools: new Set(["memory_search"]) }); + it("describes the two-step flow when both memory tools are available", () => { + const result = buildPromptSection({ + availableTools: new Set(["memory_search", "memory_get"]), + }); expect(result[0]).toBe("## Memory Recall"); + expect(result[1]).toContain("run memory_search"); + expect(result[1]).toContain("then use memory_get"); expect(result).toContain( "Citations: include Source: when it helps the user verify memory snippets.", ); expect(result.at(-1)).toBe(""); }); - it("returns Memory Recall section when memory_get is available", () => { + it("limits the guidance to memory_search when only search is available", () => { + const result = buildPromptSection({ availableTools: new Set(["memory_search"]) }); + expect(result[0]).toBe("## Memory Recall"); + expect(result[1]).toContain("run memory_search"); + expect(result[1]).not.toContain("then use memory_get"); + }); + + it("limits the guidance to memory_get when only get is available", () => { const result = buildPromptSection({ availableTools: new Set(["memory_get"]) }); expect(result[0]).toBe("## Memory Recall"); + expect(result[1]).toContain("run memory_get"); + expect(result[1]).not.toContain("run memory_search"); }); it("includes citations-off instruction when citationsMode is off", () => { diff --git a/extensions/memory-core/index.ts b/extensions/memory-core/index.ts index 3a785201c71..4d215d4a2e2 100644 --- a/extensions/memory-core/index.ts +++ b/extensions/memory-core/index.ts @@ -5,13 +5,26 @@ export const buildPromptSection: MemoryPromptSectionBuilder = ({ availableTools, citationsMode, }) => { - if (!availableTools.has("memory_search") && !availableTools.has("memory_get")) { + const hasMemorySearch = availableTools.has("memory_search"); + const hasMemoryGet = availableTools.has("memory_get"); + + if (!hasMemorySearch && !hasMemoryGet) { return []; } - const lines = [ - "## Memory Recall", - "Before answering anything about prior work, decisions, dates, people, preferences, or todos: run memory_search on MEMORY.md + memory/*.md; then use memory_get to pull only the needed lines. If low confidence after search, say you checked.", - ]; + + let toolGuidance: string; + if (hasMemorySearch && hasMemoryGet) { + toolGuidance = + "Before answering anything about prior work, decisions, dates, people, preferences, or todos: run memory_search on MEMORY.md + memory/*.md; then use memory_get to pull only the needed lines. If low confidence after search, say you checked."; + } else if (hasMemorySearch) { + toolGuidance = + "Before answering anything about prior work, decisions, dates, people, preferences, or todos: run memory_search on MEMORY.md + memory/*.md and answer from the matching results. If low confidence after search, say you checked."; + } else { + toolGuidance = + "Before answering anything about prior work, decisions, dates, people, preferences, or todos that already point to a specific memory file or note: run memory_get to pull only the needed lines. If low confidence after reading them, say you checked."; + } + + const lines = ["## Memory Recall", toolGuidance]; if (citationsMode === "off") { lines.push( "Citations are disabled: do not mention file paths or line numbers in replies unless the user explicitly asks.",