feat(v2): unify workspace delete through host-service
Merged
KI
7mo ago+1458+1458+0123456789012345678901234567890123456789-108-108-012345678901234567890123456789
Summary
- Adds
workspaceCleanup.destroyon host-service as the single canonical path for v2 workspace delete: terminals → teardown → worktree → branch → cloud → host sqlite. Ordered so each step is reversible (or a no-op) if the next fails; cloud failure becomes a warning, not an orphan. - Renderer drops its direct
apiTrpcClient.v2Workspace.delete.mutatecall. The v2 sidebar's delete dialog now owns the mutation, shows the "Also delete local branch" opt-in (off by default), and exposes typed force-retry for dirty worktrees (CONFLICT) and failed teardown scripts (with output tail preview). - Ships a host-service
runTeardownutil that silently runs.superset/teardown.shwith a 60s timeout, SIGKILLs the process group on timeout, and surfaces exit code + last 4KB of combined stdio via a tRPC error formatter.
Design doc: plans/workspace-delete-unification.md. Audit of the prior three disjoint delete paths: plans/v1-v2-delete-patterns-audit.md.
Why
Before this PR, v2 had three disjoint delete implementations:
- Electron main (
workspaces.delete) — teardown + PTYs + worktree + local-db. Only reachable from v1 UIs and v1-style hotkey/EmptyTabView paths. Never touched cloud. - Renderer → cloud (
v2Workspace.delete) — cloud row only; called by the v2 sidebar. Orphaned worktree, PTYs, and host sqlite. - Host-service (
workspace.delete) — partial composition; unreachable from UI.
Net effect: v2 sidebar delete left worktrees + terminals behind; other paths left cloud rows orphaned.
What changed
Host-service (packages/host-service)
- New
workspaceCleanup.destroyrouter (src/trpc/router/workspace-cleanup/) with typed errors:CONFLICT(dirty tree; promptforce: true) andINTERNAL_SERVER_ERRORwithcause.kind === "TEARDOWN_FAILED"surfaced to clients via a new error formatter. - New
runTeardownutil (src/runtime/teardown/) — silent spawn of.superset/teardown.sh, detached process group, 60s timeout → SIGKILL, 4KB output-tail ring buffer. - New
disposeSessionsByWorkspaceIdinsrc/terminal/terminal.tskills all active PTYs for a workspace before touching disk. forcesemantics: upgradesgit worktree removeto--force, upgradesgit branch -dto-D, and skips teardown entirely (don't re-run a known-broken script).- Cloud delete failure is swallowed to a warning; disk is already clean, cloud self-heals on next sync.
Desktop renderer (apps/desktop)
- New
useDestroyWorkspacehook resolves the owning host-service URL and translatesTRPCClientErrorinto a typed discriminated union (conflict/teardown-failed/unknown). DashboardSidebarDeleteDialogbecomes self-contained: owns the mutation, renders the branch checkbox, and flips into a force-retry state onCONFLICTorTEARDOWN_FAILED(showingoutputTailin a scrollable<pre>).useDashboardSidebarWorkspaceItemActionsdrops the direct cloud call; post-success navigation moves to anonDeletedcallback.
Docs
plans/v1-v2-delete-patterns-audit.md— audit of the three pre-existing delete paths and the gaps they produced.plans/workspace-delete-unification.md— design doc for this unification, including failure semantics, teardown contract, host-ownership rule (v2Workspaces.hostId1:1), and open questions.
Not included / follow-ups
- Other v2 entry points:
EmptyTabView+CLOSE_WORKSPACEhotkey are v1-only today; no v2-workspaces list row delete affordance yet. Separate PR. - Cloud lock-down of
v2Workspace.delete: no remaining renderer callers after this PR, but a device-key / service-token validator doesn't exist on cloud yet, so the procedure remains session-authed. Follow-up once the auth primitive lands. - Abandoned-host cleanup: retired machines leave zombie v2Hosts + workspaces in the cloud. That's a separate "Remove this device" settings flow, not this PR.
Test plan
- Clean fast path: delete a v2 workspace with no uncommitted changes, no teardown script, branch checkbox off → single click, worktree + host row + cloud row all gone, toast success.
- Branch opt-in: same flow with the checkbox on → local branch also deleted (verify with
git branch). - Dirty worktree (CONFLICT): leave uncommitted changes → first click shows "Uncommitted changes — delete anyway?" → "Delete anyway" succeeds, changes discarded.
- Teardown script: add
.superset/teardown.shthatexit 1s → first click shows exit-code + outputTail preview → "Delete anyway" skips teardown and completes. - Teardown timeout: script sleeps 120s → force-kill after 60s, outputTail shows timeout marker.
- Running terminals: open PTYs in the workspace; delete → PTYs close before worktree removal; no zombie processes in Activity Monitor.
- Active workspace: delete the currently-viewed workspace → sidebar focuses next sibling or navigates home.
- Cloud unreachable: disconnect from cloud mid-delete → disk cleanup still completes, toast warning "cloud delete pending", retry next time cleans up cloud row.
- Untracked worktree safety: manually
git worktree adda path host-service doesn't track, point a workspace at it, attempt delete → worktree preserved (logged) and host row removed. - Typecheck:
bun run typecheck --filter=@superset/host-service @superset/desktop— passes on this branch.
Summary by cubic
Unifies v2 workspace delete through @superset/host-service with cloud as the commit point; the sidebar dialog closes immediately and runs under toasts, reopening only when a force decision is needed.
-
New Features
packages/host-service:workspaceCleanup.destroyruns preflight (dirty check) → teardown via the v2 PTY → cloud delete (commit point) → local cleanup (kill terminals → remove worktree → optional branch → host sqlite). Returns warnings and surfacesCONFLICTandTEARDOWN_FAILED.apps/desktop:useDestroyWorkspacehook and a self-contained delete dialog with panes (confirm, conflict, teardown‑failed, unknown). “Also delete local branch” is off by default. Optimistic close with toast feedback; dialog reopens only forCONFLICT/TEARDOWN_FAILED.
-
Bug Fixes
- All v2 deletes route through
@superset/host-service; the old cloud‑only path is removed. Cloud errors stop the delete before any local changes; post‑cloud local issues surface as warnings. - Teardown runs via the same PTY primitive as v2 terminals; timeout uses
pty.kill()with a grace fallback so it can’t hang,disposeSessionalso marks zombie rows disposed, andTEARDOWN_TIMEOUT_MSlives in@superset/shared. The tRPC error formatter preservesteardownFailurefields, the dialog strips ANSI, and titles include signal‑terminated cases. - Cloud
v2Workspace.deletenow usesjwtProcedurewith org membership checks and returns{ success, alreadyGone }idempotently;workspaceCleanup.destroytolerates missing host‑sqlite rows so cloud cleanup still works. The dialog guards against double‑submit,branchDeletedreflects actual git results, and@superset/uiAlertDialog content is selectable for copying error details.
- All v2 deletes route through
Summary by CodeRabbit
-
New Features
- New workspace deletion flow and hook powering confirm dialog variants: normal confirm, conflict (force-delete), teardown-failed (shows reason/output), and retryable unknown-error panes.
-
Improvements
- Deletion unified via host cleanup: terminates terminals, runs teardown with timeout and output capture, removes worktree, optionally deletes branch, attempts cloud deletion, and surfaces toasts/warnings.
-
Documentation
- Added audit and design docs describing unified deletion behavior, error contracts, and UX mappings.