{"record":{"id":"aa5bbefcc7013919","repo":"mastra-ai/mastra","slug":"factory-session-not-found","errorCode":null,"errorMessage":"Factory session not found","messagePattern":"Factory session not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/rules/start-coordinator.ts","lineNumber":89,"sourceCode":"\n  const skills = session.getWorkspace()?.skills;\n  await skills?.maybeRefresh();\n  const skill = await skills?.get(invocation.skillName);\n  if (!skill || skill['user-invocable'] === false) {\n    throw new Error(`Skill not found: ${invocation.skillName}.`);\n  }\n  const args = invocation.arguments.trim();\n  const content = `${formatSkillActivation(skill)}${args ? `\\n\\nARGUMENTS: ${args}` : ''}`.trim();\n  return `<skill name=\"${skill.name}\">\\n${escapeSkillBoundary(content)}\\n</skill>`;\n}\n\nasync function resolveSourceSession(\n  storage: SourceControlStorageHandle,\n  request: FactoryStartRequest,\n): Promise<SourceControlSession> {\n  const session = await storage.sessions.getBySessionId(request.sessionId);\n  if (!session || session.orgId !== request.orgId || session.userId !== request.userId) {\n    throw new Error('Factory session not found');\n  }\n  const projectRepository = await storage.projectRepositories.get({\n    orgId: request.orgId,\n    id: session.projectRepositoryId,\n  });\n  if (!projectRepository) throw new Error('Factory session repository not found');\n  const connection = await storage.connections.get({ orgId: request.orgId, id: projectRepository.connectionId });\n  if (!connection || connection.factoryProjectId !== request.factoryProjectId) {\n    throw new Error('Factory session does not belong to this project');\n  }\n  return session;\n}\n\nasync function configureThread(session: FactorySession, request: FactoryStartRequest): Promise<string> {\n  const threadId = session.thread.requireId();\n  await session.thread.rename({ title: request.threadTitle });\n  const settings = { ...(request.threadTags ?? {}), factorySessionId: request.sessionId };\n  await Promise.all(Object.entries(settings).map(([key, value]) => session.thread.setSetting({ key, value })));","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/rules/start-coordinator.ts#L71-L107","documentation":"Thrown by resolveSourceSession when the source-control storage has no session row for request.sessionId, or the stored session's orgId/userId do not match the request's orgId/userId. This is a tenant-scoped lookup: the session must exist AND belong to the exact org and acting user in the request, otherwise the start is rejected to avoid operating on another tenant's session.","triggerScenarios":"Calling factory start (prepare and its wrappers) with a sessionId that (a) does not exist in source-control storage (wrong/typo'd ID, session purged, wrong storage backend), (b) exists but was created under a different orgId, or (c) exists under the right org but a different userId than request.userId.","commonSituations":"Copying a sessionId between environments (staging vs prod) or between orgs; replaying a session from a different user account than the one that created it; storage not migrated/seeded so the session row is missing; using a thread ID instead of the factory session ID.","solutions":["Confirm request.sessionId is a real factory session ID in the same storage backend (query storage.sessions.getBySessionId directly to check).","Ensure request.orgId and request.userId exactly match the orgId/userId recorded on the session row; use the original creator's credentials or pass the matching user context.","If the session was deleted or belongs to another environment, create a new factory session instead of replaying the old ID.","Check you are pointed at the correct storage/database (dev vs prod) for this org."],"exampleFix":"// before\nawait factory.prepare({ sessionId: 'sess_123', orgId: currentOrgId, userId: someOtherUserId });\n\n// after: use the owning org and user recorded on the session\nconst stored = await storage.sessions.getBySessionId('sess_123');\nawait factory.prepare({ sessionId: 'sess_123', orgId: stored.orgId, userId: stored.userId });","handlingStrategy":"try-catch","validationCode":"const session = await storage.sessions.getBySessionId(request.sessionId);\nif (!session) throw new Error(`Session ${request.sessionId} does not exist in this storage backend.`);\nif (session.orgId !== request.orgId || session.userId !== request.userId) {\n  throw new Error(`Session ${request.sessionId} belongs to org=${session.orgId}/user=${session.userId}.`);\n}","typeGuard":"function sessionBelongsToRequest(session: FactorySession | undefined, req: FactoryStartRequest): session is FactorySession {\n  return !!session && session.orgId === req.orgId && session.userId === req.userId;\n}","tryCatchPattern":"try {\n  const prepared = await coordinator.prepare(request);\n} catch (err) {\n  if (err instanceof Error && err.message === 'Factory session not found') {\n    console.error(`No session ${request.sessionId} for org=${request.orgId}/user=${request.userId}; verify the ID and credentials.`);\n  }\n  throw err;\n}","preventionTips":["Persist and reuse the full session record (ID + orgId + userId) together, never just the ID.","Check you are connected to the same environment/storage where the session was created.","Use the original creator's user context when replaying sessions."],"tags":["session","storage","not-found","multi-tenancy"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}