mastra-ai/mastra · error

Cannot update working memory: ${scope} ID is required

Error message

Cannot update working memory: ${scope} ID is required

What it means

updateWorkingMemory resolves which identifier to use based on workingMemoryConfig.scope: 'resource' uses resourceId, 'thread' uses threadId. If the relevant ID is missing, the update cannot proceed and this error is thrown. It is the public-API guard equivalent of errors 1440/1441, raised inside updateWorkingMemory itself.

Source

Thrown at packages/core/src/memory/mock.ts:403

    memoryConfig,
  }: {
    threadId: string;
    resourceId?: string;
    workingMemory: string;
    memoryConfig?: MemoryConfigInternal;
  }) {
    const mergedConfig = this.getMergedThreadConfig(memoryConfig);
    const workingMemoryConfig = mergedConfig.workingMemory;

    if (!workingMemoryConfig?.enabled) {
      return;
    }

    const scope = workingMemoryConfig.scope || 'resource';
    const id = scope === 'resource' ? resourceId : threadId;

    if (!id) {
      throw new Error(`Cannot update working memory: ${scope} ID is required`);
    }

    const memoryStorage = await this.getMemoryStore();
    await memoryStorage.updateResource({
      resourceId: id,
      workingMemory,
    });
  }

  async __experimental_updateWorkingMemoryVNext({
    threadId,
    resourceId,
    workingMemory,
    searchString: _searchString,
    memoryConfig,
  }: {
    threadId: string;
    resourceId?: string;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. For thread-scoped memory, pass threadId: memory.updateWorkingMemory({ threadId, workingMemory })
  2. For resource-scoped (default) memory, pass resourceId: memory.updateWorkingMemory({ resourceId, workingMemory })
  3. Check your Memory configuration: if options.workingMemory.scope is 'thread', ensure every call site supplies a threadId
  4. Derive the missing ID before the call (e.g. resolve resourceId from the current user/session)

Example fix

// before (thread-scoped config)
await memory.updateWorkingMemory({ workingMemory: 'Prefers dark mode' });
// after
await memory.updateWorkingMemory({ threadId, workingMemory: 'Prefers dark mode' });
Defensive patterns

Strategy: validation

Validate before calling

function assertWorkingMemoryArgs(
  cfg: { scope?: 'thread' | 'resource' } | undefined,
  ids: { threadId?: string; resourceId?: string },
) {
  const scope = cfg?.scope ?? 'resource';
  const id = scope === 'resource' ? ids.resourceId : ids.threadId;
  if (!id) throw new Error(`Cannot update working memory: ${scope} ID is required`);
}

Type guard

const hasScopeId = (
  scope: 'thread' | 'resource',
  ids: { threadId?: string; resourceId?: string },
): boolean => typeof (scope === 'resource' ? ids.resourceId : ids.threadId) === 'string';

Try / catch

try {
  await memory.updateWorkingMemory(args);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Cannot update working memory')) {
    const scope = workingMemoryConfig?.scope ?? 'resource';
    const id = scope === 'resource' ? await resolveResourceId() : await resolveThreadId();
    await memory.updateWorkingMemory({ ...args, ...(scope === 'resource' ? { resourceId: id } : { threadId: id }) });
  } else throw err;
}

Prevention

When it happens

Trigger: Calling memory.updateWorkingMemory with scope 'resource' (default) and no resourceId, or with workingMemory explicitly configured as { scope: 'thread' } and no threadId.

Common situations: Upgrading Mastra versions where the default scope changed from thread to resource; forgetting to pass threadId in thread-scoped setups; tests calling updateWorkingMemory directly with only workingMemory content.

Related errors


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