{"record":{"id":"65f9802a04d1367d","repo":"mastra-ai/mastra","slug":"factory-session-session-sessionid-is-not-availa","errorCode":null,"errorMessage":"Factory session ${session.sessionId} is not available to the current user","messagePattern":"Factory session (.+?) is not available to the current user","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/workspace.ts","lineNumber":276,"sourceCode":"    if (!session) {\n      // No factory session, no workspace. Chat still works; workspace tools\n      // are simply not registered. Host-cwd behavior is opt-in via a\n      // LocalSandbox callback rooted wherever the deployer wants — the\n      // resolver never hands out the server host's own filesystem.\n      return undefined;\n    }\n\n    const user = getFactoryAuthUserFromContext(requestContext);\n    const userId = getFactoryAuthUserId(user);\n    // No identity at all is a server-side caller that forgot to seed one\n    // (webhook, cron), not someone reaching for another user's session.\n    if (!user?.organizationId || !userId) {\n      throw new Error(`Factory session ${session.sessionId} was resolved without a caller identity`);\n    }\n    // Org-visible sessions open to any member of the owning organization;\n    // only private sessions stay owner-only. Cross-org access never passes.\n    if (user.organizationId !== session.orgId || (session.visibility === 'private' && userId !== session.userId)) {\n      throw new Error(`Factory session ${session.sessionId} is not available to the current user`);\n    }\n    if (!sandboxConfig || !github) {\n      throw new Error('GitHub and a sandbox callback are required to create a Factory session workspace');\n    }\n    const createSessionSandboxInstance = sandboxConfig;\n\n    const storage = github.sourceControlStorage;\n    const projectRepository = await storage.projectRepositories.get({\n      orgId: session.orgId,\n      id: session.projectRepositoryId,\n    });\n    if (!projectRepository) throw new Error(`Repository link ${session.projectRepositoryId} was not found`);\n    // The remaining reads only depend on the repository link — issue them in\n    // parallel instead of paying four sequential storage round-trips.\n    const [connection, repository] = await Promise.all([\n      storage.connections.get({ orgId: session.orgId, id: projectRepository.connectionId }),\n      storage.repositories.get({ orgId: session.orgId, id: projectRepository.repositoryId }),\n    ]);","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/workspace.ts#L258-L294","documentation":"When resolving a Factory session into a workspace, createWorkspaceFactory enforces access rules: the caller must belong to the session's organization, and private sessions are restricted to their owner. If the caller's org differs from session.orgId, or the session is private and the caller isn't the owner, this Error is thrown.","triggerScenarios":"Calling createWorkspaceFactory (via prepared/resolver/createRemoteFactory) with a session the current user has no access to: cross-org session ID, or a private session owned by another user.","commonSituations":"Sharing session IDs between teammates when the session was created as private, using credentials from a different organization, or a stale/rotated user context resolving someone else's session.","solutions":["Ensure the caller's user context has organizationId matching the session's org","Have the session owner change visibility from 'private' to org-visible so teammates can access it","Verify you are authenticated as the correct user/org before resolving the session","Create a new session owned by the intended user instead of reusing another user's session"],"exampleFix":"// before\nconst factory = await createWorkspaceFactory({ session: otherUsersPrivateSession, user: me });\n// after\nconst session = await storage.sessions.get({ id: sessionId });\nif (session.visibility === 'private' && session.userId !== me.userId) {\n  throw new Error('Request the owner to make this session org-visible');\n}\nconst factory = await createWorkspaceFactory({ session, user: me });","handlingStrategy":"try-catch","validationCode":"const session = await storage.sessions.get({ id: sessionId });\nconst accessible = session &&\n  user.organizationId === session.orgId &&\n  (session.visibility !== 'private' || session.userId === user.userId);\nif (!accessible) throw new Error('Session not accessible to current user');","typeGuard":"null","tryCatchPattern":"try {\n  factory = await createWorkspaceFactory({ session, user });\n} catch (e) {\n  if (e.message.includes('is not available to the current user')) {\n    // prompt: request access or make session org-visible\n  } else throw e;\n}","preventionTips":["Check session visibility and ownership before resolving","Use org-visible sessions for team-shared workflows","Keep the caller's auth context (orgId/userId) fresh and correct"],"tags":["authorization","access-control","multi-tenancy"],"backgroundTag":"unauthorized-access","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}