diff --git a/packages/tools/src/mastra/processor.ts b/packages/tools/src/mastra/processor.ts index 3ad37e4c..638dcc76 100644 --- a/packages/tools/src/mastra/processor.ts +++ b/packages/tools/src/mastra/processor.ts @@ -16,7 +16,6 @@ import { createLogger, normalizeBaseUrl, validateApiKey, - MemoryCache, buildMemoriesText, extractQueryText, wrapMemoryContext, @@ -51,7 +50,6 @@ interface ProcessorContext { addMemory: "always" | "never" logger: Logger promptTemplate?: PromptTemplate - memoryCache: MemoryCache } /** @@ -73,7 +71,6 @@ function createProcessorContext( addMemory: options.addMemory ?? "always", logger, promptTemplate: options.promptTemplate, - memoryCache: new MemoryCache(), } } @@ -140,8 +137,8 @@ export class SupermemoryInputProcessor implements Processor { const { messages, messageList, requestContext } = args // Mastra owns tagged system messages by tag. Clear the previous value on - // every invocation so empty, skipped, cached, fresh, and error paths cannot - // leave stale Supermemory context behind. + // every invocation so empty, skipped, fresh, and error paths cannot leave + // stale Supermemory context behind. messageList.clearSystemMessages("supermemory") try { @@ -159,19 +156,6 @@ export class SupermemoryInputProcessor implements Processor { } const effectiveThreadId = getEffectiveId(this.ctx, requestContext) - const turnKey = MemoryCache.makeTurnKey( - this.ctx.namespace, - effectiveThreadId, - this.ctx.mode, - queryText || "", - ) - - const cachedMemories = this.ctx.memoryCache.get(turnKey) - if (cachedMemories) { - this.ctx.logger.debug("Using cached memories", { turnKey }) - messageList.addSystem(wrapMemoryContext(cachedMemories), "supermemory") - return messageList - } this.ctx.logger.info("Starting memory search", { namespace: this.ctx.namespace, @@ -190,7 +174,6 @@ export class SupermemoryInputProcessor implements Processor { }) if (memories) { - this.ctx.memoryCache.set(turnKey, memories) messageList.addSystem(wrapMemoryContext(memories), "supermemory") this.ctx.logger.debug("Injected memories into system prompt", { length: memories.length, diff --git a/packages/tools/test/mastra/unit.test.ts b/packages/tools/test/mastra/unit.test.ts index 61eb69dd..a65693b6 100644 --- a/packages/tools/test/mastra/unit.test.ts +++ b/packages/tools/test/mastra/unit.test.ts @@ -11,6 +11,7 @@ import { profileBody, requestJson, requestPath, + searchBody, } from "../v5-fetch" import { RequestContext, @@ -194,10 +195,14 @@ describe("SupermemoryInputProcessor", () => { expect(systemCall?.args[1]).toBe("supermemory") }) - it("should use cached memories on second call with same message", async () => { - fetchMock.mockImplementation(async () => - jsonResponse(createMockProfileResponse(["Cached memory"])), - ) + it("should fetch fresh profile memories for each new message", async () => { + let callCount = 0 + fetchMock.mockImplementation(async () => { + callCount++ + return jsonResponse( + createMockProfileResponse([`Fact from call ${callCount}`]), + ) + }) const processor = new SupermemoryInputProcessor({ namespace: TEST_CONFIG.namespace, @@ -206,29 +211,67 @@ describe("SupermemoryInputProcessor", () => { mode: "profile", }) + const firstList = createMockMessageList() + await processor.processInput({ + messages: [createMessage("user", "Hello")], + systemMessages: [], + messageList: firstList, + abort: vi.fn() as never, + retryCount: 0, + }) + + const secondList = createMockMessageList() + await processor.processInput({ + messages: [createMessage("user", "What do you know about me?")], + systemMessages: [], + messageList: secondList, + abort: vi.fn() as never, + retryCount: 0, + }) + + expect(fetchMock).toHaveBeenCalledTimes(2) + const injected = secondList.calls.find((c) => c.method === "addSystem") + expect(injected?.args[0]).toContain("Fact from call 2") + expect(injected?.args[0]).not.toContain("Fact from call 1") + }) + + it("should fetch fresh search memories when the same message is repeated", async () => { + let callCount = 0 + fetchMock.mockImplementation(async () => { + callCount++ + return jsonResponse(searchBody([`Memory from call ${callCount}`])) + }) + + const processor = new SupermemoryInputProcessor({ + namespace: TEST_CONFIG.namespace, + id: TEST_CONFIG.id, + apiKey: TEST_CONFIG.apiKey, + mode: "query", + }) + const messages: MastraDBMessage[] = [createMessage("user", "Hello")] - const args1: ProcessInputArgs = { + await processor.processInput({ messages, systemMessages: [], messageList: createMockMessageList(), abort: vi.fn() as never, retryCount: 0, - } + }) - await processor.processInput(args1) - expect(fetchMock).toHaveBeenCalledTimes(1) - - const args2: ProcessInputArgs = { + const secondList = createMockMessageList() + await processor.processInput({ messages, systemMessages: [], - messageList: createMockMessageList(), + messageList: secondList, abort: vi.fn() as never, retryCount: 0, - } + }) - await processor.processInput(args2) - expect(fetchMock).toHaveBeenCalledTimes(1) + expect(fetchMock).toHaveBeenCalledTimes(2) + const injected = secondList.calls.find((c) => c.method === "addSystem") + expect(injected?.args[0]).toContain("Memory from call 2") + expect(injected?.args[0]).not.toContain("Memory from call 1") }) it("should refetch memories for different user message", async () => {