feat(v2): unify workspace delete through host-service

Merged
KI
7mo ago
+1458-108

Summary

  • Adds workspaceCleanup.destroy on 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.mutate call. 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 runTeardown util that silently runs .superset/teardown.sh with 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.destroy router (src/trpc/router/workspace-cleanup/) with typed errors: CONFLICT (dirty tree; prompt force: true) and INTERNAL_SERVER_ERROR with cause.kind === "TEARDOWN_FAILED" surfaced to clients via a new error formatter.
  • New runTeardown util (src/runtime/teardown/) — silent spawn of .superset/teardown.sh, detached process group, 60s timeout → SIGKILL, 4KB output-tail ring buffer.
  • New disposeSessionsByWorkspaceId in src/terminal/terminal.ts kills all active PTYs for a workspace before touching disk.
  • force semantics: upgrades git worktree remove to --force, upgrades git branch -d to -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 useDestroyWorkspace hook resolves the owning host-service URL and translates TRPCClientError into a typed discriminated union (conflict / teardown-failed / unknown).
  • DashboardSidebarDeleteDialog becomes self-contained: owns the mutation, renders the branch checkbox, and flips into a force-retry state on CONFLICT or TEARDOWN_FAILED (showing outputTail in a scrollable <pre>).
  • useDashboardSidebarWorkspaceItemActions drops the direct cloud call; post-success navigation moves to an onDeleted callback.

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.hostId 1:1), and open questions.

Not included / follow-ups

  • Other v2 entry points: EmptyTabView + CLOSE_WORKSPACE hotkey 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.sh that exit 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 add a 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.destroy runs 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 surfaces CONFLICT and TEARDOWN_FAILED.
    • apps/desktop: useDestroyWorkspace hook 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 for CONFLICT/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, disposeSession also marks zombie rows disposed, and TEARDOWN_TIMEOUT_MS lives in @superset/shared. The tRPC error formatter preserves teardownFailure fields, the dialog strips ANSI, and titles include signal‑terminated cases.
    • Cloud v2Workspace.delete now uses jwtProcedure with org membership checks and returns { success, alreadyGone } idempotently; workspaceCleanup.destroy tolerates missing host‑sqlite rows so cloud cleanup still works. The dialog guards against double‑submit, branchDeleted reflects actual git results, and @superset/ui AlertDialog content is selectable for copying error details.

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.