fix(host-service,cli): repair workspaces whose worktree moved (#6791)

Closed
KI
25d ago
+425-7

Problem

A workspace record pins its worktree by absolute path. After git worktree move (a supported git operation) git stays healthy but Superset's copy of the path goes stale: workspaces get reports worktreeExists: false, terminals fail with WORKTREE_GONE, and nothing in workspaces update could re-point it. The only recoveries were moving the worktree back or deleting and recreating the workspace (losing task links and terminal history).

Fix

  • Auto repair on open. workspace.get (what the desktop workspace page and workspaces get hit) now calls repairMovedWorktree: when the stored path no longer exists and the row is a worktree workspace, it lists the project's worktrees via a new off-loop git/listWorktrees worker task (built on the shared parseWorktreeList), finds the one on the workspace's branch, updates the row, and logs a warning.
  • Explicit repair. workspace.update accepts worktreePath; the path must exist, be a worktree of the project's repo, and be on the workspace's branch (main workspaces are rejected). Surfaced as superset workspaces update <id> --worktree-path <path>, in the SDK WorkspaceUpdateParams, and in the CLI reference docs.

Verified

  • packages/host-service/src/workspaces/moved-worktree.test.ts: real temp repo + git worktree add / move / remove; 9 cases covering repair, no-op when present, no-op when removed, main workspaces untouched, and each validation rejection. The repair case fails when the repair is disabled.
  • bun run typecheck in host-service, cli, sdk.
  • no-main-loop-blocking.test.ts passes (git runs in the worker pool, no new ctx.git sites).
  • bun test src/workers, src/commands/workspaces pass.

Not in this PR: the terminal-create path still checks existsSync directly (it repairs once the workspace has been fetched), and the two secondary findings in the issue thread (cleanup skipping git worktree remove on a stale path; recreate hard-deleting the old row) are left for follow-ups.

Fixes #6791

https://claude.ai/code/session_011XQusXtWEwq2nxN4jfMUpv


Summary by cubic

Repairs workspaces whose Git worktree was moved so they open normally and terminals stop failing. Previously, moving a worktree left the workspace pinned to a stale absolute path; now we auto-repair on fetch/open and allow explicit re-pointing.

  • Auto repair on fetch/open: workspace.get in host-service lists project worktrees via the off-loop git/listWorktrees task, finds the matching branch, and updates the stored worktreePath (logs a warning; main workspaces are untouched).
  • Explicit repair: workspace.update accepts worktreePath; surfaced as superset workspaces update <id> --worktree-path <path> in cli and WorkspaceUpdateParams.worktreePath in sdk. Validates that the path exists, is a worktree of the project, and matches the workspace branch.
  • Docs/tests: CLI reference updated; added moved-worktree.test.ts covering repair, no-ops, and all validation rejections. Git runs in the worker pool, avoiding main-loop blocking.

Summary by CodeRabbit

  • New Features

    • Added a --worktree-path option to update a workspace’s project worktree location.
    • Automatically repairs workspace paths after a Git worktree is moved, when the branch can be matched.
    • Validates that the specified path exists, belongs to the project, and matches the workspace branch.
  • Bug Fixes

    • Improved workspace retrieval to recover from relocated worktrees while preserving valid workspace records.