{"record":{"id":"f46502d3a2724e40","repo":"mastra-ai/mastra","slug":"subconscious-requires-resourceid-to-derive-scoped","errorCode":null,"errorMessage":"Subconscious requires resourceId to derive scoped knowledge.","messagePattern":"Subconscious requires resourceId to derive scoped knowledge\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/memory/src/processors/observational-memory/subconscious/capture.ts","lineNumber":84,"sourceCode":"Emit 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(\n      'Subconscious requires a knowledge storage domain. Configure a storage adapter that provides stores.knowledge.',\n    );\n  }\n  return store;\n}\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/processors/observational-memory/subconscious/capture.ts#L66-L102","documentation":"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.","triggerScenarios":"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).","commonSituations":"Calling memory APIs without passing resourceId; agent runs where resourceId was dropped between layers; tests that set organizationId but not resource identity.","solutions":["Pass resourceId to the memory/agent call (e.g. { resourceId: 'user_42' })","Set requestContext.set('resourceId', 'user_42') if deriving it from the context","Audit call sites so every Subconscious-enabled invocation supplies both organizationId and resourceId"],"exampleFix":"// before\nawait memory.recall({ threadId });\n// after\nawait memory.recall({ threadId, resourceId: 'user_42' });","handlingStrategy":"validation","validationCode":"if (!resourceId || !resourceId.trim()) throw new Error('resourceId required for Subconscious');","typeGuard":"const hasResource = (rc?: RequestContext, fallback?: string) => !!(typeof rc?.get('resourceId') === 'string' && rc.get('resourceId')!.trim()) || !!fallback?.trim();","tryCatchPattern":"try { await memory.recall(params) } catch (e) { if (String(e).includes('resourceId')) { logConfigError(e); } else throw e; }","preventionTips":["Pass resourceId on every memory/agent call","Derive resourceId from authenticated user in middleware","Write a test asserting resourceId presence in memory calls"],"tags":["configuration","request-context","subconscious","resourceid"],"backgroundTag":"missing-request-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}