Skip to content
Source
Sign inSign up
pingdotgg/t3code

Add safe worktree inventory, pruning, and revival #4742

Closedjuliusmarmingewants to merge 1 commit intomain fromt3code/plan-worktree-managementopened View on GitHub

juliusmarmingecommented

What Changed

  • Added server-side worktree inventory and safety classification.
  • Added safe manual and bulk pruning with dirty and active-worktree safeguards.
  • Added periodic auto-pruning with configurable inactivity and orphan policies.
  • Added worktree revival before starting turns when a worktree is missing.
  • Added thread-deletion orphan cleanup and the Worktrees settings page with filtering, status badges, policy controls, and destructive-delete confirmations.
  • Added contracts, RPC support, integration coverage, and real Git-backed service tests.

Why

Worktrees accumulated indefinitely, especially for web users and settled threads, because cleanup was limited to a desktop-only deletion flow. This change provides visibility and controlled cleanup while preserving branches and checkpoint references so pruned worktrees can be recreated safely when needed.

UI Changes

Added a Worktrees settings page showing managed worktree status, linked threads, pruning eligibility, attention blockers, filters, bulk pruning, row-level deletion, and global auto-prune policies.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Agent Transcript

Expand

Assistant

Here's what the codebase actually has today, then a proposed feature set. The short version of the current state: there is almost no worktree management — which matches your 186-worktree situation.

Current state (from a codebase sweep):

  • There's no worktree table or registry. A worktree exists only as a denormalized worktree_path string on projection_threads (ProjectionThreads.ts:41). Nothing records creation time, last use, or dirty state.
  • The only cleanup path is the interactive "delete worktree too?" dialog when you delete the last thread using it (useThreadActions.ts:253-397) — and it's desktop-only (needs localApi), so web users always leak worktrees. Server-side thread.delete never touches the worktree.
  • git worktree prune is never called anywhere; git worktree list is only used to annotate the branch list.
  • Settling is purely a projection-level soft-hide (settled_override/settled_at, decider at decider.ts:448-507) — so as you said, settling means worktrees now live forever.
  • Checkpoint refs (refs/t3/checkpoints/...) live in the shared ref store, not per-worktree, so deleting a worktree does not lose checkpoints — which is what makes revival viable.

Proposed features

1. Worktree inventory (the foundation). A server-side service that scans git worktree list --porcelain per project and joins against thread rows to produce, per worktree: linked threads + their settled state, dirty/clean, ahead/unpushed counts, PR state (merged?), last activity (last thread activity or dir mtime), and orphaned (no thread references it at all — your existing 186 will be full of these from deleted threads). I'd derive this on demand with a short cache rather than adding a persisted table — worktrees are filesystem truth and a table will drift. The status pieces already exist (localStatus/remoteStatus in GitManager.ts).

2. Worktrees settings page (your observability ask). Follows the appearance-settings pattern exactly (route file + panel in SettingsPanels.tsx + nav entry in SettingsSidebarNav.tsx). List with badges (dirty, unpushed, merged, orphaned, settled-only), per-row delete, and a "prune all safe" bulk action.

3. A "safe to prune" classification — this is the crux. A worktree is auto-prunable when: no active (unsettled) thread references it, the working tree is clean, and the branch is merged or fully pushed. Everything else needs explicit user action — with one big enhancement below.

4. Safety snapshot before deletion. Before removing a worktree (even a dirty one, if the user forces it), commit the dirty state to a backup ref like refs/t3/worktree-backup/<branch>. This makes every prune lossless and makes auto-pruning much less scary — you can loosen the policy because deletion is reversible.

5. Auto-prune sweep + policy. A periodic server loop on the ProviderSessionReaper pattern (Effect.forkScoped + Schedule.spaced, registered in serverRuntimeStartup.ts) that applies a configurable policy — Zortos's "delete after N days of inactivity" (say default 14, options like 7/14/30/never), evaluated only against safe-to-prune worktrees whose threads are all settled or deleted. Settling doesn't prune immediately; it just starts the clock. Also runs git worktree prune for stale admin entries.

6. Revival on unsettle. The thread keeps its branch + worktreePath metadata even after prune. When a thread unsettles (explicitly, or via the existing auto-unsettle-on-activity in the decider) and its path is gone, bootstrap.prepareWorktree recreates the worktree at the same path from the still-existing branch, and restores the backup ref if one exists. Since branches and checkpoint refs survive pruning, this should be genuinely lossless.

7. Move orphan cleanup server-side. Replace the desktop-only dialog mechanism with a server-side removal path so web gets it too (keep the dialog as UX, but the server does the work).

8. t3 worktrees list|prune CLI. Net-new (nothing exists in apps/server/src/cli/), but cheap once the inventory service exists — and it's the practical tool for digging out of the current 186-worktree hole headlessly. Worth peeking at worktrunk per Max's suggestion for list-view/status ideas; the safety model above is roughly what it does.

Open decisions for you

  1. Branches too, or worktrees only? Pruning 186 worktrees still leaves 186 t3code/* branches (plus their checkpoint refs). I'd prune worktrees only in v1 — branches are cheap, and keeping them is what makes revival trivial. Merged-branch deletion could be an opt-in second tier later.
  2. Dirty worktrees under auto-prune: never touch them (conservative), or snapshot-then-prune once they've been inactive long past the threshold (my lean, given feature 4 makes it lossless)?
  3. Policy granularity: one global "prune after N days" setting, or per-project? Global feels right for v1.
  4. Does deleting a settled thread now silently prune its orphaned worktree (no dialog, since we have safety snapshots), or keep asking?

My suggested build order: inventory service → settings page (immediate observability win + manual bulk-prune for your 186) → safety snapshot + server-side removal → auto-prune sweep → revival → CLI. The first two alone solve the acute pain.

User

  1. We don't need this. If we only auto-prune safe ones and allow the user to force, it's on them that we cannot restore it later. Should just be clear that it is an unsafe action if they choose to delete it in a dirty state?

  2. If we do this it should be super minimal in v1


  1. Sure, lets start with worktrees v1 and then we can add more advanced options later

  2. never touch dirty worktrees. We can have some UI to emphasize that they have worktrees that "needs action" in the worktrees setting to show like "This worktree has not had activity for N days but is dirty so we wont delete it" kinda thing

  3. Global for now. Per-project settings override is a larger thing we need to support application-wide

  4. Should be configurable I think. User can choose (in settings) to delete "orphan" worktrees immediately or just keep it on the normal schedule

Assistant

Plan is written up at .plans/worktree-management-v1.md with all your decisions folded in. The v1 shape:

  • Inventory service (server, derived not persisted) — scans git worktree list --porcelain per project, joins thread rows, computes dirty/unpushed/merged/orphaned/lastActivity and a safeToPrune flag. Exposed as vcsListWorktrees + vcsPruneWorktrees next to the existing worktree RPCs, with the server re-validating safety before every removal rather than trusting the client's classification.
  • Worktrees settings page — appearance-pattern route/panel/nav. Needs-attention group first ("no activity for N days, but has uncommitted changes so we won't delete it"), badges, per-row delete, bulk "prune N safe worktrees". Dirty/unpushed deletes get a destructive-styled confirm that spells out the state is permanently lost — no snapshot machinery, per your call.
  • Auto-prune sweep — ProviderSessionReaper-pattern loop; only ever removes safe worktrees past the inactivity window (default 14d, options 7/14/30/off), never dirty ones. Uses non-forced git worktree remove so git itself is a last-line safety net, and finishes with git worktree prune. The "delete orphans immediately" toggle is handled here too: when on, thread.delete orphaning a worktree removes it server-side with no dialog — which also fixes the current gap where that cleanup is desktop-only.
  • Revival — threads keep branch/worktreePath after prune; bootstrap.prepareWorktree recreates the worktree from the still-existing branch on unsettle/turn-start when the path is missing. Branches and checkpoint refs both survive v1 pruning, so this is lossless.
  • CLI — t3 worktrees list / prune --dry-run as a stretch, cut first if needed; the settings page's bulk prune already covers digging out of the 186.

One open implementation question I flagged in the doc: the sweep runs server-side, but our existing policy-ish settings (like sidebarAutoSettleAfterDays) are client-side only — so the prune policy needs a server-readable home. I'd resolve that at implementation time (reuse a server-persisted settings mechanism if one exists, otherwise a small settings store + RPC).

Build order: inventory → settings page → sweep/policy → revival → CLI. Steps 1–2 alone give you observability and manual cleanup. Say the word and I'll start on the inventory service.

User

the page probably also needs some filter functionality

Assistant

Added to the plan doc. The filter spec for the settings page is now:

  • Free-text search over branch name, directory name, and linked thread titles — thread title is the important one, since t3code-ab12cd34 means nothing to a human and "which worktree belongs to that auth-refactor thread" is the actual question.
  • Project filter, since the page is global and multiple projects' worktrees would interleave.
  • Status filter chips (multi-select): needs attention, safe to prune, dirty, unpushed, orphaned, settled-only.
  • All client-side over the already-loaded inventory — no new RPC surface needed.
  • One interaction rule worth calling out: when filters are active, the bulk prune operates on the filtered set and the button says so ("Prune 12 safe worktrees (filtered)"), so it never silently deletes rows the user isn't looking at.
  • Filter state is ephemeral in v1, no persistence.

Anything else you want to shape before I start on the inventory service?


Note

Medium Risk Touches turn-start paths and git worktree removal with policy-driven auto-prune; mistakes could remove worktrees or fail sessions, but server-side safety checks and non-force prune limit blast radius.

Overview Introduces managed worktree lifecycle so thread worktrees no longer accumulate without visibility or server-side cleanup.

Server: New WorktreeService builds inventory from git worktree list plus thread projections, classifies safe-to-prune (no active thread, clean tree, no unpushed commits), and implements pruneWorktrees (re-validates on the server, non-force remove, git worktree prune) and reviveWorktree (recreate directory from surviving branch). WorktreeReaper runs periodic sweeps from worktrees server settings (autoPruneAfterDays, deleteOrphanedImmediately). ProviderCommandReactor calls revival before turn start when the path is missing. ThreadDeletionReactor can auto-prune orphaned safe worktrees when the immediate-orphan setting is on. WS exposes vcsListWorktrees / vcsPruneWorktrees; contracts add worktree types and ServerSettings.worktrees.

Web: New Settings → Worktrees page lists worktrees with filters, needs-attention grouping, bulk safe prune, row delete (safe vs force), and policy controls. Thread delete skips the desktop orphan dialog when the server handles immediate orphan pruning on the primary environment.

Also adds a v1 design plan doc and Git-backed WorktreeService tests; integration tests mock WorktreeService for revival.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de7f96038b30be29c4d550b131af9a4197e182dd. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Add worktree inventory, pruning, and revival with a settings UI and background reaper

  • Adds WorktreeService exposing listWorktrees, pruneWorktrees, and reviveWorktree with safety checks (dirty files, unpushed commits, orphaned detection) across all managed projects.
  • Adds WorktreeReaper as a background service that periodically prunes safe/orphaned worktrees according to configurable autoPruneAfterDays and deleteOrphanedImmediately server settings.
  • Exposes two new WebSocket RPC methods (vcs.listWorktrees, vcs.pruneWorktrees) with authorization scopes; pruning triggers git status refreshes for affected workspaces.
  • Adds a WorktreesSettingsPanel at /settings/worktrees with a filterable table, bulk safe-prune, per-row delete/force-delete, and an auto-prune configuration section.
  • On thread.turn.start, ProviderCommandReactor now attempts to revive a pruned worktree before starting the provider session; on thread deletion, ThreadDeletionReactor may immediately prune the orphaned worktree when settings permit.
📊 Macroscope summarized de7f960. 17 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted

🗂️ Filtered Issues

No issues evaluated.

    1. Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 5c02353a-5db8-4289-8a06-09d0b5c74a96

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `t3code/plan-worktree-management`

      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    3. @@ -495,3 +497,6 @@
      495497 thread,
      496498 projects: project ? [project] : [],
      497499 });
      500+ // A pruned (or manually deleted) worktree is recreated from the thread's
      501+ // branch before the provider session starts in it.
      502+ if (thread.worktreePath !== null && thread.branch !== null && project) {
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High Layers/ProviderCommandReactor.ts:502

      When a thread's worktree directory is manually deleted while a provider session is still active, ensureSessionForThread revives the worktree but then reuses the existing provider session because the path string is unchanged. The provider process keeps its original cwd handle pointing at the deleted directory, so the turn runs against a stale/invalid cwd instead of the revived worktree. The cwdChanged check on line 568 only compares path strings (effectiveCwd === activeSession?.cwd), so it stays false even though the underlying directory was recreated. Track the revived result from worktreeService.reviveWorktree and force a session restart when it is true, so the provider process is respawned with the fresh directory handle.

      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @apps/server/src/orchestration/Layers/ProviderCommandReactor.ts around line 502:
      
      When a thread's worktree directory is manually deleted while a provider session is still active, `ensureSessionForThread` revives the worktree but then reuses the existing provider session because the path string is unchanged. The provider process keeps its original cwd handle pointing at the deleted directory, so the turn runs against a stale/invalid cwd instead of the revived worktree. The `cwdChanged` check on line 568 only compares path strings (`effectiveCwd === activeSession?.cwd`), so it stays false even though the underlying directory was recreated. Track the `revived` result from `worktreeService.reviveWorktree` and force a session restart when it is true, so the provider process is respawned with the fresh directory handle.
      @@ -0,0 +511,21 @@
      511+ "worktree",
      512+ "prune",
      513+ ]).pipe(Effect.ignore({ log: true })),
      514+ { concurrency: 1 },
      515+ );
      516+
      517+ if (removed.length > 0) {
      518+ yield* Effect.logInfo("worktrees.pruned", {
      519+ removedCount: removed.length,
      520+ skippedCount: skipped.length,
      521+ removedPaths: removed.map((entry) => entry.path),
      522+ });
      523+ }
      524+
      525+ return { removed, skipped };
      526+ });
      527+
      528+ const reviveWorktree: WorktreeService["Service"]["reviveWorktree"] = Effect.fn(
      529+ "WorktreeService.reviveWorktree",
      530+ )(function* (input) {
      531+ const exists = yield* fs.exists(input.worktreePath).pipe(Effect.orElseSucceed(() => false));
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium vcs/WorktreeService.ts:531

      reviveWorktree returns { revived: false } whenever input.worktreePath exists on the filesystem, even when that path is an empty directory, a stale worktree registration, or an unrelated directory left behind by a pruned or manually deleted worktree. The caller treats { revived: false } as "the worktree is ready to use," so it continues with a path that has no valid Git worktree, causing the provider session to fail or run in the wrong directory instead of recreating the worktree.

      The fs.exists check at line 531 only confirms the path exists on disk — it does not verify the path is a registered Git worktree pointing at the expected branch. Consider checking the Git worktree registration (e.g. via git worktree list) or verifying the .git worktree metadata before treating existence as "already revived."

      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @apps/server/src/vcs/WorktreeService.ts around line 531:
      
      `reviveWorktree` returns `{ revived: false }` whenever `input.worktreePath` exists on the filesystem, even when that path is an empty directory, a stale worktree registration, or an unrelated directory left behind by a pruned or manually deleted worktree. The caller treats `{ revived: false }` as "the worktree is ready to use," so it continues with a path that has no valid Git worktree, causing the provider session to fail or run in the wrong directory instead of recreating the worktree.
      
      The `fs.exists` check at line 531 only confirms the path exists on disk — it does not verify the path is a registered Git worktree pointing at the expected branch. Consider checking the Git worktree registration (e.g. via `git worktree list`) or verifying the `.git` worktree metadata before treating existence as "already revived."
      @@ -0,0 +366,21 @@
      366+ const lastActivityAt = anyThreadActivity ?? (yield* worktreeDirMtime(entry.path));
      367+
      368+ const blockers: WorktreePruneBlocker[] = [];
      369+ if (threadRefs.some((thread) => thread.status === "active")) {
      370+ blockers.push("active_thread");
      371+ }
      372+ if (dirty === true) {
      373+ blockers.push("dirty");
      374+ } else if (dirty === null) {
      375+ blockers.push("status_unavailable");
      376+ }
      377+ if (branchName === null) {
      378+ // Detached HEAD: no branch retains this worktree's commits.
      379+ blockers.push("status_unavailable");
      380+ } else if (sync !== undefined && sync.upstream !== null && !sync.upstreamGone) {
      381+ if (aheadOfUpstreamCount === null) {
      382+ blockers.push("status_unavailable");
      383+ } else if (aheadOfUpstreamCount > 0) {
      384+ blockers.push("unpushed");
      385+ }
      386+ } else if (sync !== undefined && sync.upstreamGone) {
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium vcs/WorktreeService.ts:386

      When sync.upstreamGone is true, the prune-safety check treats the branch as fully pushed and skips all commit-counting, so any local commits made after the remote ref was deleted are never detected. A clean, inactive worktree with unpushed local-only commits is marked safeToPrune and gets auto-removed, bypassing the advertised no-unpushed-commits safeguard.

      The else if (sync !== undefined && sync.upstreamGone) branch returns no blocker without comparing the branch against defaultRef. Consider falling through to the aheadOfDefaultCount check (or another rev-list comparison) so post-deletion local commits still trigger unpushed.

      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @apps/server/src/vcs/WorktreeService.ts around line 386:
      
      When `sync.upstreamGone` is `true`, the prune-safety check treats the branch as fully pushed and skips **all** commit-counting, so any local commits made *after* the remote ref was deleted are never detected. A clean, inactive worktree with unpushed local-only commits is marked `safeToPrune` and gets auto-removed, bypassing the advertised no-unpushed-commits safeguard.
      
      The `else if (sync !== undefined && sync.upstreamGone)` branch returns no blocker without comparing the branch against `defaultRef`. Consider falling through to the `aheadOfDefaultCount` check (or another rev-list comparison) so post-deletion local commits still trigger `unpushed`.
      @@ -200,3 +204,17 @@
      200204 scheduler: vcsCommandScheduler,
      201205 concurrency: vcsCommandConcurrency,
      202206 }),
      207+ listWorktrees: createEnvironmentRpcQueryAtomFamily(runtime, {
      208+ label: "environment-data:vcs:list-worktrees",
      209+ tag: WS_METHODS.vcsListWorktrees,
      210+ staleTimeMs: 10_000,
      211+ }),
      212+ pruneWorktrees: createEnvironmentRpcCommand(runtime, {
      213+ label: "environment-data:vcs:prune-worktrees",
      214+ tag: WS_METHODS.vcsPruneWorktrees,
      215+ scheduler: vcsCommandScheduler,
      216+ // Prune spans repositories, so serialize per environment instead of
      217+ // per-cwd like the other vcs commands.
      218+ concurrency: {
      219+ mode: "serial",
      220+ key: ({ environmentId }) => JSON.stringify([environmentId, "worktrees.prune"]),
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium state/vcs.ts:218

      pruneWorktrees uses the concurrency key [environmentId, "worktrees.prune"], so the shared vcsCommandScheduler only serializes prune requests against other prune requests. Other VCS commands use [environmentId, input.cwd], so a prune can still run concurrently with createWorktree, removeWorktree, switchRef, etc. in the same environment. The comment says prune should serialize per environment, but the key namespace is disjoint from every other command's key, so there is no actual mutual exclusion. To serialize prune against all other VCS commands in the same environment, the key must overlap with theirs — for example [environmentId].

      -        key: ({ environmentId }) => JSON.stringify([environmentId, "worktrees.prune"]),
      +        key: ({ environmentId }) => JSON.stringify([environmentId]),
      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @packages/client-runtime/src/state/vcs.ts around lines 218-220:
      
      `pruneWorktrees` uses the concurrency key `[environmentId, "worktrees.prune"]`, so the shared `vcsCommandScheduler` only serializes prune requests against other prune requests. Other VCS commands use `[environmentId, input.cwd]`, so a prune can still run concurrently with `createWorktree`, `removeWorktree`, `switchRef`, etc. in the same environment. The comment says prune should serialize per environment, but the key namespace is disjoint from every other command's key, so there is no actual mutual exclusion. To serialize prune against all other VCS commands in the same environment, the key must overlap with theirs — for example `[environmentId]`.
      @@ -0,0 +435,21 @@
      435+ const selectedProjects = projects.filter(
      436+ (project) =>
      437+ project.deletedAt === null &&
      438+ (input.projectId === undefined || project.projectId === input.projectId),
      439+ );
      440+
      441+ const perProject = yield* Effect.forEach(
      442+ selectedProjects,
      443+ (project) => listProjectWorktrees(project),
      444+ { concurrency: PROJECT_SCAN_CONCURRENCY },
      445+ );
      446+
      447+ // Two projects can point at the same repository; keep the first record
      448+ // per worktree path.
      449+ const seen = new Set<string>();
      450+ const worktrees: WorktreeInfo[] = [];
      451+ for (const record of perProject.flat()) {
      452+ if (seen.has(record.path)) continue;
      453+ seen.add(record.path);
      454+ worktrees.push(record);
      455+ }
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High vcs/WorktreeService.ts:449

      The deduplication loop in listWorktrees keeps the first WorktreeInfo per path and silently discards all later records for the same path. When two projects share a repository and the second project has an active thread on that worktree, the retained first record can lack the active_thread blocker and be marked safeToPrune: true. pruneWorktrees then removes a worktree that an active thread is still using. The fix is to merge thread references and blockers from later records into the kept record instead of dropping them.

      Suggested change
      449- const seen = new Set<string>();
      450- const worktrees: WorktreeInfo[] = [];
      451- for (const record of perProject.flat()) {
      452- if (seen.has(record.path)) continue;
      453- seen.add(record.path);
      454- worktrees.push(record);
      455- }
      449+ const seen = new Map<string, WorktreeInfo>();
      450+ const worktrees: WorktreeInfo[] = [];
      451+ for (const record of perProject.flat()) {
      452+ const existing = seen.get(record.path);
      453+ if (existing === undefined) {
      454+ seen.set(record.path, record);
      455+ worktrees.push(record);
      456+ } else {
      457+ existing.threads = [...existing.threads, ...record.threads];
      458+ existing.orphaned = existing.orphaned && record.orphaned;
      459+ existing.pruneBlockers = [
      460+ ...new Set([...existing.pruneBlockers, ...record.pruneBlockers]),
      461+ ] as WorktreePruneBlocker[];
      462+ existing.safeToPrune = existing.pruneBlockers.length === 0;
      463+ }
      464+ }
      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @apps/server/src/vcs/WorktreeService.ts around lines 449-455:
      
      The deduplication loop in `listWorktrees` keeps the first `WorktreeInfo` per path and silently discards all later records for the same path. When two projects share a repository and the second project has an active thread on that worktree, the retained first record can lack the `active_thread` blocker and be marked `safeToPrune: true`. `pruneWorktrees` then removes a worktree that an active thread is still using. The fix is to merge thread references and blockers from later records into the kept record instead of dropping them.
      @@ -55,3 +64,15 @@
      5564 threadId,
      5665 });
      5766
      67+ const pruneOrphanedWorktree = (threadId: ThreadDeletedEvent["payload"]["threadId"]) =>
      68+ logCleanupCauseUnlessInterrupted({
      69+ effect: Effect.gen(function* () {
      70+ const settings = yield* serverSettings.getSettings;
      71+ if (!settings.worktrees.deleteOrphanedImmediately) {
      72+ return;
      73+ }
      74+ const thread = Option.getOrNull(yield* threadRepository.getById({ threadId }));
      75+ if (thread === null || thread.worktreePath === null) {
      76+ return;
      77+ }
      78+ const worktreePath = pathService.resolve(thread.worktreePath);
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium Layers/ThreadDeletionReactor.ts:78

      pruneOrphanedWorktree resolves the thread's stored path with pathService.resolve, but WorktreeService.listWorktrees returns paths canonicalized via fs.realPath. When the stored path uses a non-canonical spelling (e.g. /var/... on macOS where /private/var is the real path), worktrees.find((worktree) => worktree.path === worktreePath) never matches, so the immediate orphan cleanup is silently skipped and the worktree is left behind. Consider canonicalizing worktreePath with fs.realPath (or the equivalent the service uses) before the comparison.

      🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
      In file @apps/server/src/orchestration/Layers/ThreadDeletionReactor.ts around line 78:
      
      `pruneOrphanedWorktree` resolves the thread's stored path with `pathService.resolve`, but `WorktreeService.listWorktrees` returns paths canonicalized via `fs.realPath`. When the stored path uses a non-canonical spelling (e.g. `/var/...` on macOS where `/private/var` is the real path), `worktrees.find((worktree) => worktree.path === worktreePath)` never matches, so the immediate orphan cleanup is silently skipped and the worktree is left behind. Consider canonicalizing `worktreePath` with `fs.realPath` (or the equivalent the service uses) before the comparison.
    4. @@ -55,3 +64,19 @@
      5564 threadId,
      5665 });
      5766
      67+ const pruneOrphanedWorktree = (threadId: ThreadDeletedEvent["payload"]["threadId"]) =>
      68+ logCleanupCauseUnlessInterrupted({
      69+ effect: Effect.gen(function* () {
      70+ const settings = yield* serverSettings.getSettings;
      71+ if (!settings.worktrees.deleteOrphanedImmediately) {
      72+ return;
      73+ }
      74+ const thread = Option.getOrNull(yield* threadRepository.getById({ threadId }));
      75+ if (thread === null || thread.worktreePath === null) {
      76+ return;
      77+ }
      78+ const worktreePath = pathService.resolve(thread.worktreePath);
      79+ const { worktrees } = yield* worktreeService.listWorktrees({
      80+ projectId: thread.projectId,
      81+ });
      82+ const record = worktrees.find((worktree) => worktree.path === worktreePath);

      Orphan prune path mismatch

      High Severity

      Immediate orphan worktree cleanup in ThreadDeletionReactor fails to prune eligible worktrees. The path comparison between the stored worktreePath (resolved) and inventory paths (canonicalized with realPath) can mismatch, especially with symlinks or macOS /var paths, preventing removal.

      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de7f96038b30be29c4d550b131af9a4197e182dd. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -0,0 +374,21 @@
      374+ } else if (dirty === null) {
      375+ blockers.push("status_unavailable");
      376+ }
      377+ if (branchName === null) {
      378+ // Detached HEAD: no branch retains this worktree's commits.
      379+ blockers.push("status_unavailable");
      380+ } else if (sync !== undefined && sync.upstream !== null && !sync.upstreamGone) {
      381+ if (aheadOfUpstreamCount === null) {
      382+ blockers.push("status_unavailable");
      383+ } else if (aheadOfUpstreamCount > 0) {
      384+ blockers.push("unpushed");
      385+ }
      386+ } else if (sync !== undefined && sync.upstreamGone) {
      387+ // The branch was pushed and its remote ref was deleted — the usual
      388+ // post-merge state. The local branch keeps the commits, so this is
      389+ // not "unpushed".
      390+ } else if (aheadOfDefaultCount === null) {
      391+ blockers.push("status_unavailable");
      392+ } else if (aheadOfDefaultCount > 0) {
      393+ blockers.push("unpushed");
      394+ }

      Upstream gone skips unpushed check

      High Severity

      When a branch’s upstream is marked gone, prune safety treats it as fully synced and does not apply the already-computed aheadOfDefaultCount. Worktrees whose branches still have commits not on the default ref can be marked safeToPrune and removed by auto-prune, bulk prune, or orphan cleanup.

      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de7f96038b30be29c4d550b131af9a4197e182dd. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -258,6 +263,11 @@
      258263 ? formatWorktreePathForDisplay(orphanedWorktreePath)
      259264 : null;
      260265 const canDeleteWorktree = orphanedWorktreePath !== null && threadProject !== null;
      266+ // When the server is configured to remove orphaned worktrees on its
      267+ // own, skip the dialog and the client-side removal — the server's
      268+ // thread-deletion reactor prunes the worktree (safely, never forced).
      269+ const serverPrunesOrphan =
      270+ deleteOrphanedWorktreesImmediately && threadRef.environmentId === primaryEnvironmentId;
      261271 const localApi = readLocalApi();
      262272 let shouldDeleteWorktree = false;
      263- if (canDeleteWorktree && localApi) {
      273+ if (canDeleteWorktree && localApi && !serverPrunesOrphan) {

      Orphan setting skips force cleanup

      Medium Severity

      When deleteOrphanedImmediately is on for the primary environment, the desktop worktree confirmation and client removeWorktree path are skipped entirely. The server only removes orphans that are safeToPrune, so dirty or unpushed orphan worktrees are neither auto-pruned nor offered for force deletion after thread delete.

      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de7f96038b30be29c4d550b131af9a4197e182dd. Configure [here](https://www.cursor.com/dashboard/bugbot).
    5. Approvability

      Verdict: Needs human review

      6 blocking correctness issues found. This PR introduces a substantial new worktree management feature with new services, UI, contracts, and integration into core orchestration. Multiple unresolved high-severity review comments identify bugs that could lead to data loss (worktrees with unpushed commits incorrectly marked safe to prune) and require attention before merge.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    6. Effect service conventions review. One clear error-handling violation (wrapper drops cause and rebuilds its message from cause.message), plus a curried error-constructor helper that the conventions call out. Details inline.

      Posted via Macroscope — Effect Service Conventions
      @@ -495,3 +497,20 @@
      495497 thread,
      496498 projects: project ? [project] : [],
      497499 });
      500+ // A pruned (or manually deleted) worktree is recreated from the thread's
      501+ // branch before the provider session starts in it.
      502+ if (thread.worktreePath !== null && thread.branch !== null && project) {
      503+ yield* worktreeService
      504+ .reviveWorktree({
      505+ workspaceRoot: project.workspaceRoot,
      506+ worktreePath: thread.worktreePath,
      507+ branch: thread.branch,
      508+ })
      509+ .pipe(
      510+ Effect.mapError(
      511+ (error) =>
      512+ new ProviderAdapterRequestError({
      513+ provider: providerErrorLabel(preferredProvider),
      514+ method: "thread.turn.start",
      515+ detail: `Could not restore the thread's worktree: ${error.message}`,
      516+ }),
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      This wrapper drops the underlying error and rebuilds detail from error.message. Per the error conventions, preserve the immediate failure as cause and derive the wrapper's message/detail from its own structural attributes rather than copying cause.message. ProviderAdapterRequestError already accepts an optional cause.

      Suggested change
      512- new ProviderAdapterRequestError({
      513- provider: providerErrorLabel(preferredProvider),
      514- method: "thread.turn.start",
      515- detail: `Could not restore the thread's worktree: ${error.message}`,
      516- }),
      512+ new ProviderAdapterRequestError({
      513+ provider: providerErrorLabel(preferredProvider),
      514+ method: "thread.turn.start",
      515+ detail: `Could not restore the thread's worktree at ${thread.worktreePath}.`,
      516+ cause: error,
      Posted via Macroscope — Effect Service Conventions
      @@ -0,0 +175,21 @@
      175+ const fs = yield* FileSystem.FileSystem;
      176+ const pathService = yield* Path.Path;
      177+ const git = yield* GitVcsDriver.GitVcsDriver;
      178+ const gitWorkflow = yield* GitWorkflowService;
      179+ const projectRepository = yield* ProjectionProjectRepository;
      180+ const threadRepository = yield* ProjectionThreadRepository;
      181+
      182+ // Canonicalize through symlinks (macOS /var → /private/var) so paths from
      183+ // `git worktree list` compare equal to configured and stored paths.
      184+ const canonicalizePath = (value: string) =>
      185+ fs.realPath(value).pipe(Effect.orElseSucceed(() => pathService.resolve(value)));
      186+
      187+ const managedWorktreesRoot = yield* canonicalizePath(worktreesDir);
      188+
      189+ const isManagedWorktreePath = (worktreePath: string): boolean => {
      190+ const relative = pathService.relative(managedWorktreesRoot, worktreePath);
      191+ return relative.length > 0 && !relative.startsWith("..") && !pathService.isAbsolute(relative);
      192+ };
      193+
      194+ const toGitManagerError = (operation: string, cwd: string, detail: string) => (cause: unknown) =>
      195+ new GitManagerError({ operation, cwd, detail, cause });
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      toGitManagerError is a curried helper whose only behavior is (...args) => new GitManagerError({ ...args }), used with mapError. The conventions ask that the error be constructed at the failure boundary so its attributes and cause stay visible, rather than through a generic constructor alias. Consider inlining new GitManagerError({ operation, cwd, detail, cause }) at each mapError site.

      Posted via Macroscope — Effect Service Conventions
    7. Closing in favor of #2829 (orchestration V2).

      #2829 deletes the V1 orchestration layer this PR builds on — apps/server/src/orchestration/**, provider/Layers/*Adapter.ts and provider/Services/** are removed and replaced by apps/server/src/orchestration-v2/**, with the IPC surface renamed to ORCHESTRATION_V2_WS_METHODS. The files this PR touches either no longer exist or are rewritten, so it can't be rebased — it would need reimplementing against the V2 adapters.

      This is not a judgement on the change itself. Several of these are real gaps we still want fixed; the base just moved out from under them.

      Once #2829 merges, please rebase onto main, port the change to the V2 equivalent, and reopen (or open a fresh PR). Ping me and I'll prioritise the review.

    8. juliusmarminge closed this

    9. juliusmarminge reopened this

    10. Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

      There are 4 total unresolved issues (including 3 from previous reviews).

      Fix All in Cursor Bugbot Autofix is [ON](https://www.cursor.com/dashboard/bugbot), but a cloud agent failed to start. Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de7f96038b30be29c4d550b131af9a4197e182dd. Configure [here](https://www.cursor.com/dashboard/bugbot).
      @@ -126,3 +127,7 @@
      126127 });
      127128 const sidebarThreadSortOrder = useClientSettings((settings) => settings.sidebarThreadSortOrder);
      128129 const confirmThreadDelete = useClientSettings((settings) => settings.confirmThreadDelete);
      130+ const primaryEnvironmentId = usePrimaryEnvironmentId();
      131+ const deleteOrphanedWorktreesImmediately = usePrimarySettings(
      132+ (settings) => settings.worktrees.deleteOrphanedImmediately,
      133+ );

      Wrong environment orphan policy

      Medium Severity

      Thread delete skips the desktop “delete worktree too?” prompt only when deleteOrphanedImmediately comes from primary server settings and the thread’s environment is the primary one. Deletes on other connected servers ignore that server’s worktree policy and may show the dialog even when that server auto-prunes orphans.

      Additional Locations (1)
      Fix in Cursor Fix in Web
      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit de7f96038b30be29c4d550b131af9a4197e182dd. Configure [here](https://www.cursor.com/dashboard/bugbot).
    11. Closing as part of the open-PR backlog sweep (wave 1).

      Reason: Julius said close in favor of #2829; V1 worktree inventory does not survive v2. Related: #2829

      Reopen if this is still wanted and you’re willing to rebase onto current main.

    12. juliusmarminge closed this

Sign in to join this conversation.