mastra-ai/mastra · error

Connect a repository before deleting a workspace

Error message

Connect a repository before deleting a workspace

What it means

In useDeleteWorkspaceMutation, after the factoryId guard, mutationFn throws 'Connect a repository before deleting a workspace' when projectRepositoryId is undefined. Deletion removes the server session and strips work-item refs from the cache keyed by the repository, so a known repository scope is required before the delete proceeds.

Source

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

  });
}

export function useDeleteWorkspaceMutation(
  factoryId: string | undefined,
  projectRepositoryId: string | undefined,
  scope?: AgentControllerThreadsScope,
) {
  const { baseUrl } = useApiConfig();
  const queryClient = useQueryClient();
  const navigate = useNavigate();
  // user-session routes carry the session id as :threadId (user/threads/:threadId)
  const { sessionId, threadId } = useParams<{ sessionId?: string; threadId?: string }>();
  const viewedSessionId = sessionId ?? threadId;

  return useMutation({
    mutationFn: async (workspace: FactoryUserSession) => {
      if (!factoryId) throw new Error('No Factory selected');
      if (!projectRepositoryId) throw new Error('Connect a repository before deleting a workspace');
      await deleteUserSession(baseUrl, workspace.sessionId);
      return workspace;
    },
    onSuccess: workspace => {
      removeCachedSession(queryClient, projectRepositoryId, workspace.sessionId);
      // The server strips the work-item refs with the row; mirror it in the cache
      // so the board's cards drop their session links before the next poll.
      if (factoryId) stripCachedSessionRefs(queryClient, factoryId, workspace.sessionId);
      invalidateSessionQueries(queryClient, projectRepositoryId, scope, workspace.sessionId);
      void queryClient.invalidateQueries({ queryKey: queryKeys.userSession(workspace.sessionId) });
      void queryClient.invalidateQueries({
        queryKey: queryKeys.agentControllerThreads(scope?.agentControllerId, scope?.resourceId, workspace.sessionId),
      });
      if (workspace.sessionId === viewedSessionId) void navigate(`/factories/${factoryId}/new`);
      toast('Workspace deleted');
    },
    onError: error => toast.error(error instanceof Error ? error.message : 'Failed to delete workspace'),
  });

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Reconnect a repository to the Factory project before deleting orphaned workspaces, or clear stale sessions server-side.
  2. Hide delete actions for workspaces whose project no longer has a repository, with an explanatory tooltip.
  3. Verify the projectRepositoryId query succeeded; refetch repository settings before allowing deletes.
  4. If the repository was intentionally removed, purge the affected workspaces via the server/admin path instead of the UI.

Example fix

// before
await deleteWorkspace(workspace); // throws when projectRepositoryId undefined

// after
if (!projectRepositoryId) {
  toast.error('Reconnect a repository to manage this workspace');
  return;
}
await deleteWorkspace(workspace);
Defensive patterns

Strategy: validation

Validate before calling

if (!projectRepositoryId) {
  toast.error('Reconnect a repository before managing workspaces');
  return;
}

Type guard

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

Try / catch

try {
  await deleteWorkspace.mutateAsync(workspace);
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Connect a repository')) {
    toast.error(e.message);
  } else throw e;
}

Prevention

When it happens

Trigger: Invoking deleteWorkspace on a project whose projectRepositoryId is undefined — repository never connected, was disconnected, or the repository lookup query failed/cleared the id.

Common situations: Stale workspace UI listing sessions from before a repository was disconnected; switching to a factory project without repos while cached workspaces are still visible; repository settings query error leaves projectRepositoryId null.

Related errors


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