fix(host-service,cli): release a workspace's git branch on delete so its name is reusable

Closed
AN
1mo ago
+29-3

Fixes #6380

Problem

workspace.delete — the legacy surface used by the CLI, SDK, and MCP — hardcoded deleteBranch: false. Deleting a workspace therefore removed the worktree and the row but left the local git branch in the parent repo.

Because the branch survived, creating a new workspace with the same name silently picked a suffixed branch (<name>-2, then -3, ...) while the workspace name stayed the same, so the name and branch no longer agreed. In the desktop app the flow was worse: delete + recreate with the same name landed on a stale "Workspace not found" screen showing the old workspace ID.

Root cause

packages/host-service/src/trpc/router/workspace/workspace.ts — the delete mutation always passed deleteBranch: false:

return destroyWorkspace(ctx, {
    workspaceId: input.id,
    deleteBranch: false,
    force: true,
    teardownMode: "best-effort",
});

The v2 destroy path (used by the renderer) already lets the caller choose; the legacy delete surface never did, and the cleanup saga only deletes the branch when input.deleteBranch is set (Step 5, workspace-cleanup.ts:520).

Fix

  • workspace.delete now accepts an optional deleteBranch (default true), so a deleted workspace releases its branch by default and the name is genuinely free to reuse. Callers that need to keep the branch (e.g. unmerged work they intend to salvage) pass false explicitly.
  • superset ws delete keeps its existing non-interactive contract (still defaults to deleting the branch) and adds a --keep-branch flag for the salvage case.

The v2 destroy path is untouched.

Verification

  • tsc --noEmit clean on host-service, cli, sdk, and mcp (the SDK/MCP/CLI all consume workspace.delete and compile against the new optional field).
  • biome check clean on both edited files.
  • The existing destroy/deleteBranch behavior in the v2 path and its integration tests are unchanged.

Summary by cubic

Fixes #6380 by aligning workspace deletion with the desktop UI: deleting a workspace now keeps its git branch by default, and an opt-in flag releases the name for reuse.

  • workspace.delete accepts optional deleteBranch, defaulting to false so silent callers don't lose unpushed commits.
  • superset ws delete adds --delete-branch to opt into branch removal; the flag is rejected with --cloud because cloud sandboxes have no workspace git branch to release.

Summary by CodeRabbit

  • New Features

    • Added the --delete-branch option to workspace deletion.
    • Workspace branches are kept by default when deleting a workspace.
    • Branches are deleted only when --delete-branch is explicitly specified.
  • Bug Fixes

    • Workspace cleanup now consistently follows the selected branch deletion behavior.
    • Deleted workspaces that retain their branches no longer block creating a new workspace with the same branch.