{"record":{"id":"955cfbf6f79063a3","repo":"mastra-ai/mastra","slug":"memory-instance-is-required-for-working-memory-upd","errorCode":null,"errorMessage":"Memory instance is required for working memory updates","messagePattern":"Memory instance is required for working memory updates","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/memory/mock.ts","lineNumber":258,"sourceCode":"    const description = usesMergeSemantics\n      ? `Update the working memory with new information. Data is merged with existing memory - only include fields you want to add or update.`\n      : `Update the working memory with new information. Any data not included will be overwritten.`;\n\n    return {\n      updateWorkingMemory: createTool({\n        id: 'update-working-memory',\n        description,\n        inputSchema: z.object({ memory: z.string() }),\n        execute: async (inputData, context) => {\n          const threadId = context?.agent?.threadId;\n          const resourceId = context?.agent?.resourceId;\n\n          // Memory can be accessed via context.memory (when agent is part of Mastra instance)\n          // or context.memory (when agent is standalone with memory passed directly)\n          const memory = (context as any)?.memory;\n\n          if (!memory) {\n            throw new Error('Memory instance is required for working memory updates');\n          }\n\n          const scope = mergedConfig.workingMemory?.scope || 'resource';\n          if (scope === 'thread' && !threadId) {\n            throw new Error('Thread ID is required for thread-scoped working memory updates');\n          }\n          if (scope === 'resource' && !resourceId) {\n            throw new Error('Resource ID is required for resource-scoped working memory updates');\n          }\n\n          if (threadId) {\n            let thread = await memory.getThreadById({ threadId });\n\n            if (!thread) {\n              thread = await memory.createThread({\n                threadId,\n                resourceId,\n                memoryConfig: _config,","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/memory/mock.ts#L240-L276","documentation":"The working-memory update tool (registered by listTools on the mock/standalone memory path) needs a Memory instance to write working memory. It reads it from the tool execution context; if `context.memory` is absent, a plain Error is thrown. This happens when the agent running the tool is neither part of a Mastra instance nor constructed with memory wired into the context.","triggerScenarios":"Executing the update-working-memory tool (via agent generate/stream with tools enabled) where the request context has no `memory` property — e.g. standalone agent whose memory was not registered, or tool invoked outside an agent run without memory in context.","commonSituations":"Instantiating an Agent with a Memory instance but not attaching it to Mastra and not passing memory through the run context; calling the tool directly in tests without a memory in context; custom tool runners dropping the context.","solutions":["Attach the Agent (with its Memory) to a Mastra instance so `context.memory` is populated.","Pass the memory instance in the run/request context when invoking the agent or tool directly.","Verify the agent is constructed with `memory` and that workingMemory is enabled in its config."],"exampleFix":"// before\nconst agent = new Agent({ name: 'a', model, memory }); // never registered anywhere\nconst res = await agent.generate('...'); // tool cannot find context.memory\n// after\nconst mastra = new Mastra({ agents: { a: agent }, storage });\nconst res = await mastra.getAgent('a').generate('...'); // context.memory now available","handlingStrategy":"validation","validationCode":"const memory = (context as any)?.memory;\nif (!memory) throw new Error('Working-memory tool requires context.memory; attach agent to Mastra or pass memory in context');","typeGuard":"function hasMemoryInContext(ctx: unknown): ctx is { memory: MastraMemory } {\n  return Boolean((ctx as any)?.memory);\n}","tryCatchPattern":"try {\n  await updateWorkingMemoryTool.execute({ context, ... });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Memory instance is required')) {\n    // re-run inside an agent run with memory in context\n  } else throw e;\n}","preventionTips":["Invoke working-memory tools through agent runs, not directly, unless you populate context.memory.","Register agents with a Mastra instance so memory is injected automatically.","In tests, seed the context with a real Memory instance."],"tags":["memory","working-memory","tools","context"],"backgroundTag":"missing-memory-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}