{"record":{"id":"044ab1d36f182f01","repo":"mastra-ai/mastra","slug":"resource-id-is-required-for-resource-scoped-workin-044ab1","errorCode":null,"errorMessage":"Resource ID is required for resource-scoped working memory updates","messagePattern":"Resource ID is required for resource-scoped working memory updates","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/memory/src/tools/working-memory.ts","lineNumber":213,"sourceCode":"    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,\n          });\n        }\n\n        if (thread.resourceId && resourceId && thread.resourceId !== resourceId) {\n          throw new Error(`Thread with id ${threadId} resourceId does not match the current resourceId ${resourceId}`);\n        }\n      }\n","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/tools/working-memory.ts#L195-L231","documentation":"The update-working-memory tool is configured with the default scope 'resource' (or explicitly resource-scoped), but no resourceId was available in the tool execution context. Mastra refuses to write resource-scoped memory without knowing which resource (user) it belongs to. This is a validation guard in the tool's execute function.","triggerScenarios":"Invoking the agent (which then calls the update-working-memory tool) without memory: { resource } in the memory options while workingMemory.scope is 'resource'; context.agent.resourceId is undefined at execute time.","commonSituations":"Passing only threadId but forgetting resourceId in generate/stream memory options; tests or scripts that construct agent context manually; older call sites using thread-only memory from before resource-scoped memory became the default.","solutions":["Pass resourceId alongside threadId: agent.generate(input, { memory: { thread, resource } }).","Explicitly set workingMemory scope to 'thread' in the Memory config if resource scoping is not desired (then threadId is required instead).","If invoking the tool directly in tests, supply context.agent.resourceId in the execution context.","Validate resourceId exists before running the agent in your own code."],"exampleFix":"// before\nawait agent.stream(msg, { memory: { thread: 't-1' } });\n// after\nawait agent.stream(msg, { memory: { thread: 't-1', resource: 'user-42' } });","handlingStrategy":"validation","validationCode":"if (!resourceId) throw new Error('resourceId is required for resource-scoped working memory');","typeGuard":"const hasResourceId = (v: unknown): v is { resourceId: string } =>\n  typeof v === 'object' && v !== null && typeof (v as any).resourceId === 'string' && (v as any).resourceId.length > 0;","tryCatchPattern":"try {\n  await agent.generate(input, { memory: { thread, resource } });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Resource ID is required for resource-scoped')) {\n    // supply resourceId and retry once\n  }\n}","preventionTips":["Always derive resourceId server-side from authenticated user identity and pass it explicitly.","Never rely on defaults for memory options in agent helper wrappers.","Run a smoke test invoking a memory-writing tool before deploying.","When scope is the default 'resource', treat resourceId as mandatory in your call-site types."],"tags":["memory","working-memory","configuration","missing-argument"],"backgroundTag":"missing-required-identifier","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}