{"record":{"id":"98071cce47c6d118","repo":"mastra-ai/mastra","slug":"resource-id-is-required-for-resource-scoped-workin","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":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/memory/mock.ts","lineNumber":266,"sourceCode":"        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,\n              });\n            }\n\n            if (thread.resourceId && resourceId && thread.resourceId !== resourceId) {\n              throw new Error(\n                `Thread with id ${threadId} resourceId does not match the current resourceId ${resourceId}`,\n              );\n            }","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/memory/mock.ts#L248-L284","documentation":"In Mastra memory, working memory can be scoped either to a thread or to a resource. When the configured scope is 'resource' (the default), a resourceId must be supplied to identify which resource's working memory to read or update. This error is thrown by the memory implementation when a resource-scoped working memory operation is attempted without a resourceId.","triggerScenarios":"Calling memory.updateWorkingMemory (or the working-memory tool path invoked via listTools) with workingMemory scope configured as 'resource' (or left at default) while passing no resourceId — e.g. calling updateWorkingMemory({ threadId, workingMemory }) with only a threadId.","commonSituations":"Developers switching from thread-scoped to default (resource-scoped) working memory after upgrading Mastra, or assuming threadId alone is enough. Also happens when resourceId is lost somewhere between an agent invocation and a manual memory.updateWorkingMemory call, or when workingMemoryConfig is built dynamically and the resourceId is undefined at call time.","solutions":["Pass a resourceId in the call: memory.updateWorkingMemory({ resourceId, threadId?, workingMemory })","Alternatively configure workingMemory scope to 'thread' in Memory options if per-thread memory is what you want: new Memory({ options: { workingMemory: { scope: 'thread' } } })","Ensure resourceId is propagated from the agent run (memoryResource / resourceid in request context) into the working-memory update call","Log/inspect mergedConfig.workingMemory?.scope and the resourceId argument before the call to confirm which side is missing"],"exampleFix":"// before\nawait memory.updateWorkingMemory({ threadId, workingMemory: 'User likes jazz' });\n// after\nawait memory.updateWorkingMemory({ threadId, resourceId, workingMemory: 'User likes jazz' });","handlingStrategy":"validation","validationCode":"function assertResourceScopeArgs(opts: { resourceId?: string; threadId?: string }, scope: 'thread' | 'resource' = 'resource') {\n  if (scope === 'resource' && !opts.resourceId) {\n    throw new Error('resourceId is required for resource-scoped working memory updates');\n  }\n  if (scope === 'thread' && !opts.threadId) {\n    throw new Error('threadId is required for thread-scoped working memory updates');\n  }\n}","typeGuard":"const hasResourceId = (o: { resourceId?: string | null }): o is { resourceId: string } =>\n  typeof o.resourceId === 'string' && o.resourceId.length > 0;","tryCatchPattern":"try {\n  await memory.updateWorkingMemory({ resourceId, workingMemory });\n} catch (err) {\n  if (err instanceof Error && err.message.includes('Resource ID is required')) {\n    resourceId ??= await resolveResourceIdFromSession();\n    await memory.updateWorkingMemory({ resourceId, workingMemory });\n  } else throw err;\n}","preventionTips":["Always pass resourceId alongside threadId when scope is the default ('resource')","Set workingMemory scope explicitly in Memory options instead of relying on defaults","Centralize working-memory updates in one helper that resolves IDs before calling the API","Add an assertion/log of scope + ids in your memory service layer","When upgrading Mastra, check the changelog for workingMemory default scope changes"],"tags":["memory","working-memory","validation","missing-argument"],"backgroundTag":"missing-required-identifier","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}