mastra-ai/mastra · error

Subconscious requires resourceId to derive scoped knowledge.

Error message

Subconscious requires resourceId to derive scoped knowledge.

What it means

After organizationId, requireScopeContext resolves a resourceId (via resolveKnowledgeResourceId or context.resourceId) to scope knowledge per resource. Missing/blank resourceId means knowledge can't be attributed, so capture throws.

Source

Thrown at packages/memory/src/processors/observational-memory/subconscious/capture.ts:84

Emit when only when the conversation anchors the referred time. Resolve relative dates against the current date and use ISO 8601.
Capture what was learned through the work, not what the session was told: skip records that merely restate standing instructions, configured rules, or the text of the task or issue the session was handed. The exception is an explicit request from the user to remember something, which is always captured even when it duplicates an existing instruction.`;

const CAPTURE_REASON_INSTRUCTIONS = `Every record requires a reason: the concrete why behind capturing it, in one short sentence - what it cost to learn or when it will matter again (and for pinned records, why it must stay in context). Never write generic filler such as "seemed relevant" or "useful context".`;

function clampScope(level: KnowledgeScopeLevel, ceiling?: KnowledgeScopeLevel): KnowledgeScopeLevel {
  return ceiling && SCOPE_ORDER[level] < SCOPE_ORDER[ceiling] ? ceiling : level;
}

function requireScopeContext(context: ExtractorRuntimeContext): KnowledgeScope {
  const organizationId = context.requestContext?.get('organizationId');
  if (typeof organizationId !== 'string' || !organizationId.trim()) {
    throw new Error(
      'Subconscious requires requestContext.organizationId to derive scoped knowledge. Set organizationId on the request context for this conversation.',
    );
  }
  const resourceId = resolveKnowledgeResourceId(context.requestContext, context.resourceId);
  if (!resourceId) {
    throw new Error('Subconscious requires resourceId to derive scoped knowledge.');
  }
  if (!context.threadId) {
    throw new Error('Subconscious requires threadId to derive scoped knowledge.');
  }
  return [`org:${organizationId}`, `resource:${resourceId}`, `thread:${context.threadId}`];
}

async function getKnowledgeStore(context: ExtractorRuntimeContext): Promise<KnowledgeStorage> {
  if (!context.memory) throw new Error('Subconscious capture requires an active Memory instance.');
  const store = await context.memory.storage.getStore('knowledge');
  if (!store) {
    throw new Error(
      'Subconscious requires a knowledge storage domain. Configure a storage adapter that provides stores.knowledge.',
    );
  }
  return store;
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Pass resourceId to the memory/agent call (e.g. { resourceId: 'user_42' })
  2. Set requestContext.set('resourceId', 'user_42') if deriving it from the context
  3. Audit call sites so every Subconscious-enabled invocation supplies both organizationId and resourceId

Example fix

// before
await memory.recall({ threadId });
// after
await memory.recall({ threadId, resourceId: 'user_42' });
Defensive patterns

Strategy: validation

Validate before calling

if (!resourceId || !resourceId.trim()) throw new Error('resourceId required for Subconscious');

Type guard

const hasResource = (rc?: RequestContext, fallback?: string) => !!(typeof rc?.get('resourceId') === 'string' && rc.get('resourceId')!.trim()) || !!fallback?.trim();

Try / catch

try { await memory.recall(params) } catch (e) { if (String(e).includes('resourceId')) { logConfigError(e); } else throw e; }

Prevention

When it happens

Trigger: Subconscious capture invoked with requestContext.organizationId set but no resourceId in context and resolveKnowledgeResourceId returning falsy (e.g. no 'resourceId' entry in requestContext and context.resourceId empty).

Common situations: Calling memory APIs without passing resourceId; agent runs where resourceId was dropped between layers; tests that set organizationId but not resource identity.

Related errors


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