Skip to content

fix(workspace-cleanup): clean up a folder workspace's worktrees as one group - #44745

Merged
AmoabaKelvin merged 4 commits into
stablyai:mainfrom
erdtsieck:fix/folder-workspace-cleanup-group
Nov 20, 2026
Merged

AmoabaKelvin merged 4 commits into
stablyai:mainfrom
erdtsieck:fix/folder-workspace-cleanup-group

Conversation

@erdtsieck

@erdtsieck erdtsieck commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

A folder workspace can hold checkouts of several repos. Automatic cleanup looked at each checkout on its own, so it could delete repo A's checkout because its branch was merged while you were still working in repo B in the same folder workspace. Now the checkouts of one folder workspace are cleaned up together or not at all.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​204 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​204
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​226 $\color{#cf222e}{\Huge{\mathbf{−}}}$​22 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​204

What Changed

Before: with the merged rule on, orca workspace cleanup preview (and a run) would remove a merged worktree attached to a folder workspace even when another repo's worktree in that folder workspace had uncommitted changes or unpushed commits. The reverse happened too.

After: the worktrees a folder workspace lists (its attached worktrees and their lineage children, as Source Control shows them) form one cleanup group. A member is removed only if every member is removable by the rules and protections. Otherwise each removable member is kept with a new keep cause, folder-workspace-sibling ("another repository in this workspace is still in use"), shown in the preview, the run report and the settings activity list.

Mechanism:

  • workspace-cleanup-folder-membership.ts (new): each workspace's folder siblings, by host identity. It walks a listed row up through parentWorktreeId to the row whose workspaceLineage.parentWorkspaceKey is the folder. When the listing lacks a worktree because its repo's scan failed, it reads the stored workspace/worktree lineage instead, so that worktree still joins its folder and holds the group. Archived subtrees, archived checkouts and rows retired by removal are left out. A bot's home folder is not one piece of work, so its workspaces stay independent.
  • workspace-cleanup-evaluation.ts: keepFolderWorkspaceSiblings turns a would-remove verdict into a keep when any sibling is not going. A sibling with no verdict here counts as not going: another host's row (including a same-id checkout on another machine), an unowned repo's, or one the listing lacks. A sibling already being removed counts as going.
  • workspace-cleanup-removal-step.ts: decideWorkspaceCleanups is now the only exported decision, so the preview, the run and the re-check all decide per group. The re-check right before removing a folder member reads one full listing and evaluates the target with its folder as it is now. A sibling added since the scan holds the group, and one removed earlier in the run is gone. A workspace that is in no folder is still re-checked on its own narrowed listing.

Not covered: cross-repo lineage children outside folder workspaces (as noted in the issue), and folders whose members live in another Orca store (a paired runtime's cross-store lineage), where neither side can see the other's members; those are cleaned one by one as before.

Why

Folder workspaces themselves were already never cleanup candidates, but nothing in cleanup knew which worktrees belong to one. Membership is already on every worktree.ps row, so grouping there and applying it after the per-worktree decision keeps the evaluator's rules and protections unchanged and puts the group rule in one place. Checking the group only in the scan would not be enough: the re-check before removal exists because removals take time, so it has to see the siblings too.

Linked Issue

Fixes #44715

Linear: STA-16213

Visual Proof

No screenshots: there is no new UI, only a new keep reason in existing lists (preview output, run report, settings activity list). I did not run the app for this PR; the behaviour is covered by the tests below.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below
  • Every added test fails without this change and avoids the test anti-patterns

Added tests (src/main/workspaces/cleanup/workspace-cleanup-folder-group.test.ts, through the runner's preview and the real re-check; only the Git scan and the host listing are faked):

  • one dirty member (here a lineage child of an attached worktree) keeps every member of its folder with folder-workspace-sibling, while an all-removable folder and an ungrouped worktree are still deleted (fails on main: everything is deleted);
  • a sibling on another host under the same id, and a lineage child missing from a failed listing, both hold the group;
  • a worktree created in the folder after the scan stops the next removal at the re-check (keptOnRecheck), and nothing is removed;
  • a bot's workspaces are cleaned one by one.

Each one fails with its part of the change reverted.

Maintainer run on macOS: the new test file (4 passed), pnpm tc:node, and pnpm check:changed (all 42 gates after a fixed timestamp replaced Date.now() in the test).

Original run on Linux with Node 24 / pnpm 12.14.0:

  • pnpm test src/main/workspaces/cleanup src/shared/workspace-cleanup src/main/ipc/workspace-cleanup src/renderer/src/components/settings/workspace-cleanup src/cli/workspaces --maxWorkers=2: 15 files, 55 tests passed
  • pnpm tc: passed
  • pnpm check:changed --base=upstream/main --maxWorkers=8: 41 of 42 gates pass (format, oxlint, typecheck, changed-code quality, ratchets, localization). The one failure is src/main/orcad/orcad-push-startup.test.ts (7 tests), which fails the same way with this PR's files reverted to main. It looks like my environment: no Electron libraries, so the native rebuild fails.

Risk

Level: Low

Cleanup now keeps more workspaces, never fewer: the only new outcome is a keep. If membership is resolved wrong, a worktree is either kept when it could have gone (safe) or judged alone as before.

  • The re-check before removing a folder member reads one full worktree.ps listing instead of a narrowed one, then scans the group's Git evidence in one batched call. Narrowed listings already resolve every worktree and refresh terminal liveness host-wide, so this costs about the same as one narrowed read per sibling would. Workspaces outside a folder keep the narrowed re-check. Membership is one in-memory pass over the listing and the stored lineage (O(rows + lineage records)). config/main-blocking-helper-baseline.txt goes from 3 to 4 for workspace-cleanup-removal-step.ts: this counts references, not runtime sync calls, and decideWorkspaceCleanups adds one reference to the existing orca.yaml read.
  • Run history written by this build can contain keepCause: 'folder-workspace-sibling'. The CLI shows causes it does not know raw, so older clients reading a newer host's report still show something.
  • Back out with a plain revert.

AI Disclosure

Written with Claude Code (Claude Opus 5.5) from the issue I filed.

Review

  • Cross-platform: no paths, processes or shortcuts touched; host identities use composeWorktreeHostIdentity.
  • SSH/remote: cleanup still only acts on this host's worktrees. A sibling on another host has no verdict here, so its group is kept.
  • Folder workspaces: this is the folder-workspace case. Plain git worktrees with no folder are unaffected (empty sibling list), and so are bot homes.
  • Agents/integrations: no change; live agents and terminals are still per-worktree protections, now applied group-wide.
  • Performance: one in-memory membership pass per listing; one full listing per folder-member re-check, as above. No change for the hourly run when no folder workspaces have attached worktrees.
  • UI: one new keep-cause label (en.json, shared verdict labels).
  • Security: none.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…e group

Cleanup judged each worktree attached to a folder workspace on its own
repo's evidence, so with "merged" on it removed repo A's merged worktree
while repo B's worktree in the same folder workspace still had work.

The worktrees a folder workspace lists (attached ones and their lineage
children) now form one group: a member is removed only when every member
is removable, otherwise it is kept as "another repository in this
workspace is still in use". The re-check right before removal re-reads
the siblings too, so work picked up after the scan still holds the group.

Fixes stablyai#44715
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

Cleanup now evaluates worktrees attached to the same folder workspace as a group. If a sibling is not eligible for removal, cleanup keeps the other group members with a folder-sibling keep cause. Before removal, cleanup recollects the target and its current siblings and rechecks them. The change also adds the keep-cause label and tests for group evaluation and removal-time rechecks.

Severity of issue fixed: Medium

Merge Risk: Moderate · up to b3f47

Folder-workspace cleanup now removes grouped worktrees together and rechecks the current group before deleting. Remaining issues are small: the blocking-call baseline was raised instead of being kept at its previous count, an archived ancestor can make cleanup hold a group longer than it should, and the removal-time test does not actually exercise a dirty sibling. These should be addressed before merge, but none risks deleting data.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: folder workspace worktrees are cleaned up as one group. It is specific and follows the conventional-commit style the change set matches. It is not vague and does not d…
Linked Issues check Passed [#44715] requires that worktrees attached to one folder workspace be removed only when all group members are removable, and that otherwise removable members be kept with an explicit reason. At the rev…
Out of Scope Changes check Passed The changed files serve the folder-workspace grouping rule. These are the membership module, evaluation keep logic, removal-step re-check, run-evidence sibling collection, runner wiring, the new keep-…
Description check Passed The description follows the repository template closely. It has the ELI5, What Changed, Why, Linked Issue (Fixes #44715), Visual Proof (explained as N/A because there is no new UI), Testing, Risk (Lev…
Full details: Title check

Explanation

The title names the main change: folder workspace worktrees are cleaned up as one group. It is specific and follows the conventional-commit style the change set matches. It is not vague and does not describe unrelated work.

Full details: Linked Issues check

Explanation

[#44715] requires that worktrees attached to one folder workspace be removed only when all group members are removable, and that otherwise removable members be kept with an explicit reason. At the reviewed head, the code implements this. (1) workspace-cleanup-folder-membership.ts builds folder groups from listed rows (parentWorktreeId and workspaceLineage.parentWorkspaceKey). It also reads stored lineage for recorded worktrees that the listing lacks, excludes archived subtrees, excludes worktrees retired by removal, and excludes bot-home folders. (2) workspace-cleanup-evaluation.ts keepFolderWorkspaceSiblings converts a non-keep verdict to keep with cause folder-workspace-sibling when any sibling is not going. A sibling with no verdict, or one that is not removing and not removable, counts as not going. (3) workspace-cleanup-removal-step.ts decideWorkspaceCleanups is the shared decision. removeWorkspaceForCleanup re-reads the target and its current folder siblings before deletion, so a sibling that became dirty after the scan holds the group. Cross-repo lineage children outside folder workspaces are not covered, as the issue allows. The reviewed diff does not show a gap for the stated reproduction. I did not re-run the tests. The verdict relies on source inspection of the membership, evaluation, and removal-step files at head, plus the author's reported results.

Full details: Out of Scope Changes check

Explanation

The changed files serve the folder-workspace grouping rule. These are the membership module, evaluation keep logic, removal-step re-check, run-evidence sibling collection, runner wiring, the new keep-cause label (en.json and shared verdict labels), and the blocking-helper baseline count. The baseline increase reflects the extra sibling read in the re-check, as the PR states. The bot-home exclusion and the stored-lineage handling of unlisted worktrees are deliberate scope choices that the PR and objectives describe. They narrow or extend the group rule but do not add unrelated behavior. The orcad-push-startup test failure is reported as environmental and is not a changed file. Residual note: the bot-home exclusion means bot home folder workspaces with several repos keep per-worktree cleanup. The issue does not mention bot homes, so this is not treated as an unrelated change.

Full details: Description check

Explanation

The description follows the repository template closely. It has the ELI5, What Changed, Why, Linked Issue (Fixes #44715), Visual Proof (explained as N/A because there is no new UI), Testing, Risk (Level: Low, with a back-out plan), AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist sections. The What Changed section gives a clear before and after and names the mechanism. The Why section explains the choice of grouping after the per-worktree decision. The Testing section lists the added tests, the macOS maintainer run, and the Linux run with one unrelated failure. The Risk section names the riskiest part (the full listing re-check and the baseline count change) and how to revert. Minor gaps: the manual testing box is unchecked, the final pnpm lint/typecheck/test/build checklist item is unchecked, and the Linux test run did not pass the full gate set. The Visual Proof reason for N/A is given. These gaps are not critical.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Minor comments: 2

src/main/workspaces/cleanup/workspace-cleanup-removal-step.ts: 1

108-108: Minor · Functional Correctness · Poor tradeoff

Recheck the folder group against current members, not only the scan-time sibling IDs.

The removal recheck evaluates only the sibling IDs saved at scan time. A sibling that joins the folder after the scan is never evaluated. The target can then be removed while that sibling still has uncommitted changes or unpushed commits. Git's dirty-checkout refusal protects only the target worktree, not the new sibling.

Do not take the sibling list from the target's own recollected row. The target-scoped getWorktreePs call returns only the target, so that row has no siblings. Use a listing that includes the folder's current members. Evaluate the group with that list, and keep the target if any current member is not going.

config/main-blocking-helper-baseline.txt: 1

1543-1543: Minor · Maintainability & Code Quality · Poor tradeoff

Keep the blocking-helper budget for workspace-cleanup-removal-step.ts at 3.

The ratchet allows its counts to go only down. The one growth exception covers a one-shot startup or shutdown call with reviewer sign-off. The folder-workspace sibling re-check runs during a cleanup pass, so this exception does not apply. The trailing note does not record that sign-off.

The new sibling re-check reaches the synchronous orca.yaml read through decideWorkspaceCleanup. That adds a counted reference, which raises this file's count from 3 to 4. Keep sibling re-checks in the cleanup path. The file must then meet the baseline of 3 again, or it needs a qualifying startup or shutdown justification and reviewer sign-off before its row can rise.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3cd39180-ff2b-423c-aefc-9581468b473d
Commits

Reviewing files that changed from the base of the PR and between 44803c8 and afd2ae4.

Files selected for processing: 10

  • config/main-blocking-helper-baseline.txt
  • src/main/workspaces/cleanup/workspace-cleanup-removal-step.test.ts
  • src/main/workspaces/cleanup/workspace-cleanup-removal-step.ts
  • src/main/workspaces/cleanup/workspace-cleanup-run-evidence.ts
  • src/main/workspaces/cleanup/workspace-cleanup-runner.ts
  • src/renderer/src/components/settings/workspace-cleanup/workspace-cleanup-run-labels.ts
  • src/renderer/src/i18n/locales/en.json
  • src/shared/workspace-cleanup/workspace-cleanup-evaluation.test.ts
  • src/shared/workspace-cleanup/workspace-cleanup-evaluation.ts
  • src/shared/workspace-cleanup/workspace-cleanup-verdict-labels.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

A same-id worktree on another host is a different sibling: it has no
verdict on this host, so it holds the group instead of borrowing the
local copy's. The pre-removal re-check re-reads only this host's
siblings. The group test now runs through the runner's preview.
…s listed now

The pre-removal re-check reads one full listing and evaluates the target
with its current folder members, so a worktree added to the folder since
the scan holds it too, and siblings cost one listing instead of one each.
A recorded folder child the listing lacks (its repo's scan failed) holds
the group. A bot's home folder is not one piece of work, so its
workspaces are still cleaned one by one. Folder membership moves to its
own module.
A worktree the listing lacks (its repo's scan failed) is placed in its
folder through its stored lineage, including a lineage child of an
attached worktree, so it holds the group. A stored record counts as
listed only when a row with the same id and instance stands for it.
An archived checkout holds nothing.
@AmoabaKelvin

Copy link
Copy Markdown
Collaborator

Thanks for filing this and for the fix. The grouping, and having the preview, the run and the re-check share one decision, were the right shape, so I've kept that and pushed three commits on top:

  • Siblings are matched by host identity. A same-id checkout on another machine now holds the group instead of borrowing the local copy's verdict (CodeRabbit's finding).
  • The re-check before removing a folder member reads one full listing and evaluates the folder as it is now. A worktree added after the scan holds the removal, and siblings cost one listing instead of one each.
  • If a worktree is missing from the listing because its repo's scan failed, it is placed in its folder through stored lineage, so it still holds the group. Bot home folders are not grouped, since every bot task is attached to its home.

The tests are now one runner-level file in src/main/workspaces/cleanup/workspace-cleanup-folder-group.test.ts, and the description is updated to match.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Nitpick comments: 1

src/main/workspaces/cleanup/workspace-cleanup-folder-group.test.ts: 1

186-187: Trivial · Maintainability & Code Quality · Quick win

Use an existing repo for the new sibling so its dirty state is evaluated.

makeHost captures its repo list before the new row is pushed. Because repo-c is absent from that list, the collector does not treat repo-c::/n1 as owned. The sibling has no verdict, so it keeps the target through folder-workspace-sibling even without dirty.add(...). The test does not check that the removal recheck re-evaluates fresh sibling evidence.

Use a repo that the test already lists, such as repo-b:

Suggested fix

-    rows.push(row('repo-c::/n1', { folder: 'F' }))
-    dirty.add('repo-c::/n1')
+    rows.push(row('repo-b::/n1', { folder: 'F' }))
+    dirty.add('repo-b::/n1')

With repo-b::/n1, the sibling is owned and its dirty state decides the outcome. Without dirty.add, the sibling is clean and the target is removed, so the test fails.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a58dfecf-e00d-4e26-9c0c-724888320f44
Commits

Reviewing files that changed from the base of the PR and between afd2ae4 and b3f478b.

Files selected for processing: 5

  • src/main/workspaces/cleanup/workspace-cleanup-folder-group.test.ts
  • src/main/workspaces/cleanup/workspace-cleanup-folder-membership.ts
  • src/main/workspaces/cleanup/workspace-cleanup-removal-step.ts
  • src/main/workspaces/cleanup/workspace-cleanup-run-evidence.ts
  • src/shared/workspace-cleanup/workspace-cleanup-evaluation.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

)
// An archived checkout is gone from disk on purpose, so it has nothing to hold.
const archived = store.getWorktreeMeta(worktreeId)?.archivedCheckout
const folderKey = listed || archived ? null : folderOf(undefined, worktreeId)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Archived ancestors must not pass their folder to unlisted descendants.

When a missing child C has stored parent B, and B is a listed, archived local row, the stored-ancestry walk in folderOf cannot match B's row. The lookup for B uses an empty host (|B), but listed local rows are indexed as local|B. The walk then reads B's stored folder edge and returns F, so C joins F's group. C keeps A from removal while C stays unlisted, even though B's archived subtree should hide its folder.

The walk must find B's listed archived row and stop there, so the archived subtree does not hold F. Missing children under non-archived ancestors must remain blockers. The host used for the ancestor lookup is a policy decision: unqualified rows have an unproven host, so defaulting to local needs an explicit choice.

@AmoabaKelvin
AmoabaKelvin merged commit a372846 into stablyai:main Nov 20, 2026
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Automatic cleanup removes one repo's worktree from a folder workspace while another repo in it is still in use

2 participants