mastra-ai/mastra · error

Factory session ${session.sessionId} is not available to the

Error message

Factory session ${session.sessionId} is not available to the current user

What it means

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.

Source

Thrown at mastracode/factory/src/workspace.ts:276

    if (!session) {
      // No factory session, no workspace. Chat still works; workspace tools
      // are simply not registered. Host-cwd behavior is opt-in via a
      // LocalSandbox callback rooted wherever the deployer wants — the
      // resolver never hands out the server host's own filesystem.
      return undefined;
    }

    const user = getFactoryAuthUserFromContext(requestContext);
    const userId = getFactoryAuthUserId(user);
    // No identity at all is a server-side caller that forgot to seed one
    // (webhook, cron), not someone reaching for another user's session.
    if (!user?.organizationId || !userId) {
      throw new Error(`Factory session ${session.sessionId} was resolved without a caller identity`);
    }
    // Org-visible sessions open to any member of the owning organization;
    // only private sessions stay owner-only. Cross-org access never passes.
    if (user.organizationId !== session.orgId || (session.visibility === 'private' && userId !== session.userId)) {
      throw new Error(`Factory session ${session.sessionId} is not available to the current user`);
    }
    if (!sandboxConfig || !github) {
      throw new Error('GitHub and a sandbox callback are required to create a Factory session workspace');
    }
    const createSessionSandboxInstance = sandboxConfig;

    const storage = github.sourceControlStorage;
    const projectRepository = await storage.projectRepositories.get({
      orgId: session.orgId,
      id: session.projectRepositoryId,
    });
    if (!projectRepository) throw new Error(`Repository link ${session.projectRepositoryId} was not found`);
    // The remaining reads only depend on the repository link — issue them in
    // parallel instead of paying four sequential storage round-trips.
    const [connection, repository] = await Promise.all([
      storage.connections.get({ orgId: session.orgId, id: projectRepository.connectionId }),
      storage.repositories.get({ orgId: session.orgId, id: projectRepository.repositoryId }),
    ]);

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure the caller's user context has organizationId matching the session's org
  2. Have the session owner change visibility from 'private' to org-visible so teammates can access it
  3. Verify you are authenticated as the correct user/org before resolving the session
  4. Create a new session owned by the intended user instead of reusing another user's session

Example fix

// before
const factory = await createWorkspaceFactory({ session: otherUsersPrivateSession, user: me });
// after
const session = await storage.sessions.get({ id: sessionId });
if (session.visibility === 'private' && session.userId !== me.userId) {
  throw new Error('Request the owner to make this session org-visible');
}
const factory = await createWorkspaceFactory({ session, user: me });
Defensive patterns

Strategy: try-catch

Validate before calling

const session = await storage.sessions.get({ id: sessionId });
const accessible = session &&
  user.organizationId === session.orgId &&
  (session.visibility !== 'private' || session.userId === user.userId);
if (!accessible) throw new Error('Session not accessible to current user');

Type guard

null

Try / catch

try {
  factory = await createWorkspaceFactory({ session, user });
} catch (e) {
  if (e.message.includes('is not available to the current user')) {
    // prompt: request access or make session org-visible
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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