mastra-ai/mastra · error · Error

Resource ID is required for resource-scoped working memory u

Error message

Resource ID is required for resource-scoped working memory updates

What it means

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.

Source

Thrown at packages/memory/src/tools/working-memory.ts:213

    execute: async (inputData, context) => {
      const workingMemoryInput = inputData as { memory: any };
      const threadId = context?.agent?.threadId;
      const resourceId = context?.agent?.resourceId;

      // Memory can be accessed via context.memory (when agent is part of Mastra instance)
      // or context.memory (when agent is standalone with memory passed directly)
      const memory = (context as any)?.memory;

      if (!memory) {
        throw new Error('Memory instance is required for working memory updates');
      }

      const scope = memoryConfig?.workingMemory?.scope || 'resource';
      if (scope === 'thread' && !threadId) {
        throw new Error('Thread ID is required for thread-scoped working memory updates');
      }
      if (scope === 'resource' && !resourceId) {
        throw new Error('Resource ID is required for resource-scoped working memory updates');
      }

      if (threadId) {
        let thread = await memory.getThreadById({ threadId });

        if (!thread) {
          thread = await memory.createThread({
            threadId,
            resourceId,
            memoryConfig,
          });
        }

        if (thread.resourceId && resourceId && thread.resourceId !== resourceId) {
          throw new Error(`Thread with id ${threadId} resourceId does not match the current resourceId ${resourceId}`);
        }
      }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Pass resourceId alongside threadId: agent.generate(input, { memory: { thread, resource } }).
  2. Explicitly set workingMemory scope to 'thread' in the Memory config if resource scoping is not desired (then threadId is required instead).
  3. If invoking the tool directly in tests, supply context.agent.resourceId in the execution context.
  4. Validate resourceId exists before running the agent in your own code.

Example fix

// before
await agent.stream(msg, { memory: { thread: 't-1' } });
// after
await agent.stream(msg, { memory: { thread: 't-1', resource: 'user-42' } });
Defensive patterns

Strategy: validation

Validate before calling

if (!resourceId) throw new Error('resourceId is required for resource-scoped working memory');

Type guard

const hasResourceId = (v: unknown): v is { resourceId: string } =>
  typeof v === 'object' && v !== null && typeof (v as any).resourceId === 'string' && (v as any).resourceId.length > 0;

Try / catch

try {
  await agent.generate(input, { memory: { thread, resource } });
} catch (e) {
  if (e instanceof Error && e.message.includes('Resource ID is required for resource-scoped')) {
    // supply resourceId and retry once
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/044ab1d36f182f01. Report an issue: GitHub.