mastra-ai/mastra · error

Connect a repository before creating a workspace

Error message

Connect a repository before creating a workspace

What it means

In useCreateWorkspaceMutation, after the factoryId check passes, mutationFn throws 'Connect a repository before creating a workspace' when projectRepositoryId is undefined. A workspace is a user session bound to a specific repository, so creating one without a connected repository is rejected client-side.

Source

Thrown at mastracode/factory-ui/src/hooks/useWorkspaces.ts:192

    queryKey: queryKeys.userSession(sessionId),
    queryFn: sessionId ? () => getUserSession(baseUrl, sessionId) : skipToken,
  });
}

export function useCreateWorkspaceMutation(
  factoryId: string | undefined,
  projectRepositoryId: string | undefined,
  scope?: AgentControllerThreadsScope,
) {
  const { baseUrl } = useApiConfig();
  const queryClient = useQueryClient();
  const navigate = useNavigate();

  return useMutation({
    mutationFn: async (branch: string) => {
      const trimmedBranch = branch.trim();
      if (!factoryId) throw new Error('No Factory selected');
      if (!projectRepositoryId) throw new Error('Connect a repository before creating a workspace');
      return createUserSession(baseUrl, projectRepositoryId, { branch: trimmedBranch });
    },
    onSuccess: session => {
      invalidateSessionQueries(queryClient, projectRepositoryId, scope, session.sessionId);
      void queryClient.invalidateQueries({ queryKey: queryKeys.userSession(session.sessionId) });
      void navigate(`/factories/${factoryId}/workspaces/${session.sessionId}`);
    },
    onError: error => toast.error(error instanceof Error ? error.message : 'Failed to create workspace'),
  });
}

export function useDeleteWorkspaceMutation(
  factoryId: string | undefined,
  projectRepositoryId: string | undefined,
  scope?: AgentControllerThreadsScope,
) {
  const { baseUrl } = useApiConfig();
  const queryClient = useQueryClient();

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Connect a repository in the Factory project settings, then retry workspace creation.
  2. Disable the create-workspace action until projectRepositoryId is present and link to the repository-connection UI from the disabled state.
  3. If the repo should be connected, verify the projectRepositoryId query succeeded and is not stuck loading/error.
  4. After switching factories, confirm the repository id was re-resolved for the new factory.

Example fix

// before
await createWorkspace.mutate(branch); // throws if no repo connected

// after
if (!projectRepositoryId) {
  navigate(`/factories/${factoryId}/settings/repositories`);
  return;
}
await createWorkspace.mutate(branch);
Defensive patterns

Strategy: validation

Validate before calling

if (!projectRepositoryId) {
  navigate(`/factories/${factoryId}/settings/repositories`);
  return;
}

Type guard

function hasRepository(id: string | undefined | null): id is string {
  return typeof id === 'string' && id.length > 0;
}

Try / catch

try {
  await createWorkspace.mutateAsync(branch);
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Connect a repository')) {
    toast.error(e.message);
    navigate('/settings/repositories');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the create-workspace mutation on a Factory project that has no repository linked yet (projectRepositoryId undefined), e.g. a freshly created project or one where repository connection was revoked/failed.

Common situations: New Factory project onboarding where the user skips the 'connect repo' step; repository disconnected by an admin; projectRepositoryId failed to load because the repository settings query errored; user switched factories and the repo id reset to null.

Related errors


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