{"record":{"id":"bffcb3e062c237a5","repo":"mastra-ai/mastra","slug":"memory-instance-is-required-for-working-memory-upd-bffcb3","errorCode":null,"errorMessage":"Memory instance is required for working memory updates","messagePattern":"Memory instance is required for working memory updates","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/memory/src/tools/working-memory.ts","lineNumber":205,"sourceCode":"  return createTool({\n    id: 'update-working-memory',\n    description,\n    inputSchema,\n    // Merge semantics depend on the model being able to omit fields it is not updating.\n    // Strict structured outputs would force every field into `required`, so the model has to\n    // emit placeholder values for untouched sections, which then overwrite stored data.\n    ...(usesMergeSemantics ? { strict: false as const } : {}),\n    execute: async (inputData, context) => {\n      const workingMemoryInput = inputData as { memory: any };\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 = memoryConfig?.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,","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/tools/working-memory.ts#L187-L223","documentation":"The update-working-memory tool reads the Memory instance from the tool execution context. If context.memory is missing, it cannot load or persist working memory, so it throws immediately. Memory is injected automatically when the agent runs inside a Mastra instance or is passed directly to a standalone agent.","triggerScenarios":"Executing updateWorkingMemoryTool directly (or via a custom runner) with a context object lacking `memory`; agent created without a Mastra instance registration and without memory passed in its config.","commonSituations":"Unit-testing the tool with a bare context; standalone agent built without the `memory` option; custom orchestration invoking the tool's execute() manually without injecting memory.","solutions":["Pass `memory` when creating the Agent (new Agent({ name, instructions, model, memory })) or register the agent with a Mastra instance so memory is injected.","When calling the tool's execute() manually, include memory in the context object.","In tests, construct a real Memory instance (with storage) and supply it in the context."],"exampleFix":"// before\nconst tool = createUpdateWorkingMemoryTool();\nawait tool.execute({ context: { taskId: 't1' } }); // throws\n// after\nawait tool.execute({ context: { taskId: 't1', memory: myMemoryInstance } });","handlingStrategy":"type-guard","validationCode":"if (!context?.memory) {\n  throw new Error('Working memory tool requires context.memory');\n}","typeGuard":"function hasMemory(ctx: unknown): ctx is { memory: Memory } {\n  return (ctx as any)?.memory != null && typeof (ctx as any).memory.getWorkingMemory === 'function';\n}","tryCatchPattern":"try {\n  await updateWorkingMemoryTool.execute({ context: { taskId, memory } });\n} catch (err) {\n  if (err instanceof Error && err.message === 'Memory instance is required for working memory updates') {\n    throw new Error('Agent misconfigured: attach a Memory instance to the agent or Mastra instance');\n  }\n  throw err;\n}","preventionTips":["Always pass memory to new Agent(...) or register the agent in a Mastra instance.","When invoking tools manually in tests, inject a real Memory instance into context.","Assert agent.memory is defined at app startup for memory-dependent agents."],"tags":["memory","configuration","missing-argument"],"backgroundTag":"missing-required-parameter","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}