{"record":{"id":"d5a0074b183fa44d","repo":"mastra-ai/mastra","slug":"subconscious-requires-requestcontext-organizationi","errorCode":null,"errorMessage":"Subconscious requires requestContext.organizationId to derive scoped knowledge. Set organizationId on the request context for this conversation.","messagePattern":"Subconscious requires requestContext\\.organizationId to derive scoped knowledge\\. Set organizationId on the request context for this conversation\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/memory/src/processors/observational-memory/subconscious/capture.ts","lineNumber":78,"sourceCode":"Set node scope to the narrowest level where that identity and content should be shared. Omit it to use the configured default scope.\nKnowledge records must be grounded in the conversation, concise, and written as prose. Do not infer unstated information.\nWhen the conversation states a canonical identifier or URL for an entity, preserve it verbatim in the record text.\nWrap every named node mentioned in record text in [[wikilinks]].\nSet 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.\nOmit scope when uncertain; omitted record scopes stay private to the current thread.\nEmit when only when the conversation anchors the referred time. Resolve relative dates against the current date and use ISO 8601.\nCapture 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.`;\n\nconst 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\".`;\n\nfunction clampScope(level: KnowledgeScopeLevel, ceiling?: KnowledgeScopeLevel): KnowledgeScopeLevel {\n  return ceiling && SCOPE_ORDER[level] < SCOPE_ORDER[ceiling] ? ceiling : level;\n}\n\nfunction requireScopeContext(context: ExtractorRuntimeContext): KnowledgeScope {\n  const organizationId = context.requestContext?.get('organizationId');\n  if (typeof organizationId !== 'string' || !organizationId.trim()) {\n    throw new Error(\n      'Subconscious requires requestContext.organizationId to derive scoped knowledge. Set organizationId on the request context for this conversation.',\n    );\n  }\n  const resourceId = resolveKnowledgeResourceId(context.requestContext, context.resourceId);\n  if (!resourceId) {\n    throw new Error('Subconscious requires resourceId to derive scoped knowledge.');\n  }\n  if (!context.threadId) {\n    throw new Error('Subconscious requires threadId to derive scoped knowledge.');\n  }\n  return [`org:${organizationId}`, `resource:${resourceId}`, `thread:${context.threadId}`];\n}\n\nasync function getKnowledgeStore(context: ExtractorRuntimeContext): Promise<KnowledgeStorage> {\n  if (!context.memory) throw new Error('Subconscious capture requires an active Memory instance.');\n  const store = await context.memory.storage.getStore('knowledge');\n  if (!store) {\n    throw new Error(","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/processors/observational-memory/subconscious/capture.ts#L60-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set organizationId in the request context before invoking the agent/memory: requestContext.set('organizationId', 'org_123')","If single-tenant, pass a constant organizationId per deployment","Wrap scope-capture wiring in a helper that guarantees context population","Catch the error to log misconfigured invocations in non-knowledge flows"],"exampleFix":"// before\nawait agent.generate(input);\n// after\nawait agent.generate(input, { requestContext: rc.with('organizationId', 'org_123') });","handlingStrategy":"validation","validationCode":"if (typeof requestContext?.get('organizationId') !== 'string' || !requestContext.get('organizationId')?.trim()) throw new Error('organizationId required');","typeGuard":"const hasOrg = (rc?: RequestContext) => typeof rc?.get('organizationId') === 'string' && !!rc.get('organizationId')!.trim();","tryCatchPattern":"try { await agent.generate(input, { requestContext }) } catch (e) { if (String(e).includes('organizationId')) { fixContextAndRetry(); } else throw e; }","preventionTips":["Always seed requestContext with organizationId in multi-tenant apps","Centralize request-context construction in one helper","Add integration tests asserting context completeness"],"tags":["configuration","request-context","subconscious","multi-tenancy"],"backgroundTag":"missing-request-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}