mastra-ai/mastra · error

Subconscious requires requestContext.organizationId to deriv

Error message

Subconscious requires requestContext.organizationId to derive scoped knowledge. Set organizationId on the request context for this conversation.

What it means

Subconscious knowledge capture derives knowledge scopes from requestContext.organizationId. When the runtime context lacks a non-empty organizationId string, capture throws, because knowledge cannot be correctly scoped without an organization boundary.

Source

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

Set node scope to the narrowest level where that identity and content should be shared. Omit it to use the configured default scope.
Knowledge records must be grounded in the conversation, concise, and written as prose. Do not infer unstated information.
When the conversation states a canonical identifier or URL for an entity, preserve it verbatim in the record text.
Wrap every named node mentioned in record text in [[wikilinks]].
Set a record scope only when the conversation establishes where it applies. Use org for organization-wide records, resource for records shared across this resource's conversations, and thread for conversation-private records.
Omit scope when uncertain; omitted record scopes stay private to the current thread.
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(

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Set organizationId in the request context before invoking the agent/memory: requestContext.set('organizationId', 'org_123')
  2. If single-tenant, pass a constant organizationId per deployment
  3. Wrap scope-capture wiring in a helper that guarantees context population
  4. Catch the error to log misconfigured invocations in non-knowledge flows

Example fix

// before
await agent.generate(input);
// after
await agent.generate(input, { requestContext: rc.with('organizationId', 'org_123') });
Defensive patterns

Strategy: validation

Validate before calling

if (typeof requestContext?.get('organizationId') !== 'string' || !requestContext.get('organizationId')?.trim()) throw new Error('organizationId required');

Type guard

const hasOrg = (rc?: RequestContext) => typeof rc?.get('organizationId') === 'string' && !!rc.get('organizationId')!.trim();

Try / catch

try { await agent.generate(input, { requestContext }) } catch (e) { if (String(e).includes('organizationId')) { fixContextAndRetry(); } else throw e; }

Prevention

When it happens

Trigger: Calling memory/agent flows that trigger Subconscious capture without setting requestContext.set('organizationId', ...) — e.g. agent.generate/memory.recall with a bare requestContext or none at all.

Common situations: Local scripts and tests omitting request context; upgrading to a version where Subconscious capture became mandatory scope-scoped; multi-tenant apps where one code path forgets to attach organizationId.

Related errors


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