Repository navigation
fix(cleanup): keep a folder workspace's checkouts together when one is in use #44767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import { | |
| type WorkspaceCleanupWorkspace | ||
| } from './workspace-cleanup-run-evidence' | ||
| import { decideWorkspaceCleanup, removeWorkspaceForCleanup } from './workspace-cleanup-removal-step' | ||
| import { applyFolderWorkspaceCleanupGroupVeto } from '../../../shared/workspace-cleanup/workspace-cleanup-folder-group' | ||
| import { | ||
| keptRunOutcome, | ||
| summarizeWorkspaceCleanupRun, | ||
|
|
@@ -261,7 +262,14 @@ export class WorkspaceCleanupRunner { | |
| evaluation: await decideWorkspaceCleanup(this.deps.runtime, workspace, now) | ||
| }) | ||
| } | ||
| return decided | ||
| // A folder workspace cleans as one unit: any member that keeps vetoes its siblings (#44715). | ||
| const evaluations = applyFolderWorkspaceCleanupGroupVeto( | ||
| decided.map(({ workspace, evaluation }) => ({ | ||
| ...(workspace.folderParentKey ? { folderParentKey: workspace.folderParentKey } : {}), | ||
| evaluation | ||
| })) | ||
| ) | ||
| return decided.map((row, index) => ({ ...row, evaluation: evaluations[index]! })) | ||
| } | ||
|
|
||
| private async readSizes(): Promise<Map<string, number>> { | ||
|
|
@@ -293,16 +301,34 @@ export class WorkspaceCleanupRunner { | |
| } | ||
| // Nested checkouts first, so a parent's removal never sweeps a child it did not re-check. | ||
| removals.sort((a, b) => b.ref.path.length - a.ref.path.length) | ||
| // Folder siblings must stay removable at this moment too, not just at scan time (#44715). | ||
| const folderMembers = new Map<string, string[]>() | ||
| for (const { workspace } of decided) { | ||
| const parent = workspace.folderParentKey | ||
| if (parent !== undefined) { | ||
| const ids = folderMembers.get(parent) ?? [] | ||
| ids.push(workspace.ref.worktreeId) | ||
| folderMembers.set(parent, ids) | ||
| } | ||
| } | ||
| for (const { workspace, ref, reason } of removals) { | ||
| if (this.abort.signal.aborted) { | ||
| break | ||
| } | ||
| const parent = workspace.folderParentKey | ||
| const outcome = await removeWorkspaceForCleanup({ | ||
| runtime: this.deps.runtime, | ||
| store: this.deps.store, | ||
| globalRules: () => this.globalRules(), | ||
| workspace, | ||
| now: () => Date.now(), | ||
| ...(parent !== undefined | ||
| ? { | ||
| folderMemberWorktreeIds: (folderMembers.get(parent) ?? []).filter( | ||
| (id) => id !== workspace.ref.worktreeId | ||
|
Comment on lines
+327
to
+328
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exclude successfully removed folder members from later sibling checks. After a folder member is removed, its ID stays in Track the IDs of successful removals during the run and exclude them from the sibling list. Failed and kept members stay in the list. Suggested fix
|
||
| ) | ||
| } | ||
| : {}), | ||
| signal: this.abort.signal | ||
| }) | ||
| rows.push({ ...ref, reason, ...outcome }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // @vitest-environment node | ||
| import { describe, expect, it } from 'vitest' | ||
| import type { WorkspaceCleanupEvaluation } from './workspace-cleanup-evaluation' | ||
| import { applyFolderWorkspaceCleanupGroupVeto } from './workspace-cleanup-folder-group' | ||
|
|
||
| const remove: WorkspaceCleanupEvaluation = { action: 'delete', reason: 'merged' } | ||
| const keepDirty: WorkspaceCleanupEvaluation = { | ||
| action: 'keep', | ||
| cause: 'uncommitted-changes' | ||
| } | ||
|
|
||
| describe('applyFolderWorkspaceCleanupGroupVeto', () => { | ||
| it('keeps every sibling when one folder member must keep', () => { | ||
| const evaluations = applyFolderWorkspaceCleanupGroupVeto([ | ||
| { folderParentKey: 'folder-1', evaluation: remove }, | ||
| { folderParentKey: 'folder-1', evaluation: keepDirty } | ||
| ]) | ||
| expect(evaluations).toEqual([ | ||
| { action: 'keep', cause: 'folder-mate-in-use' }, | ||
| // The member that raised the veto keeps its own, more specific cause. | ||
| keepDirty | ||
| ]) | ||
| }) | ||
|
|
||
| it('removes every member when all of them are removable', () => { | ||
| const evaluations = applyFolderWorkspaceCleanupGroupVeto([ | ||
| { folderParentKey: 'folder-1', evaluation: remove }, | ||
| { folderParentKey: 'folder-1', evaluation: { action: 'delete', reason: 'inactive' } } | ||
| ]) | ||
| expect(evaluations).toEqual([remove, { action: 'delete', reason: 'inactive' }]) | ||
| }) | ||
|
|
||
| it('leaves workspaces outside any folder workspace untouched', () => { | ||
| const evaluations = applyFolderWorkspaceCleanupGroupVeto([ | ||
| { evaluation: remove }, | ||
| { folderParentKey: 'folder-1', evaluation: keepDirty } | ||
| ]) | ||
| expect(evaluations).toEqual([remove, keepDirty]) | ||
| }) | ||
|
|
||
| it('does not cross groups', () => { | ||
| const evaluations = applyFolderWorkspaceCleanupGroupVeto([ | ||
| { folderParentKey: 'folder-1', evaluation: remove }, | ||
| { folderParentKey: 'folder-2', evaluation: keepDirty } | ||
| ]) | ||
| expect(evaluations).toEqual([remove, keepDirty]) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { WorkspaceCleanupEvaluation } from './workspace-cleanup-evaluation' | ||
|
|
||
| /** | ||
| * A folder workspace is one unit of work: its attached checkouts (one per repo) clean up only when | ||
| * every member is removable. Any member that must keep vetoes its siblings, so cleanup never pulls | ||
| * one repo's checkout out from under another repo still being worked on (#44715). | ||
| */ | ||
| export function applyFolderWorkspaceCleanupGroupVeto( | ||
| rows: readonly { | ||
| readonly folderParentKey?: string | ||
| readonly evaluation: WorkspaceCleanupEvaluation | ||
| }[] | ||
| ): WorkspaceCleanupEvaluation[] { | ||
| const vetoedGroups = new Set<string>() | ||
| for (const row of rows) { | ||
| if (row.folderParentKey !== undefined && row.evaluation.action === 'keep') { | ||
| vetoedGroups.add(row.folderParentKey) | ||
| } | ||
| } | ||
| if (vetoedGroups.size === 0) { | ||
| return rows.map((row) => row.evaluation) | ||
| } | ||
| return rows.map((row) => | ||
| row.folderParentKey !== undefined && | ||
| vetoedGroups.has(row.folderParentKey) && | ||
| row.evaluation.action !== 'keep' | ||
| ? { action: 'keep', cause: 'folder-mate-in-use' } | ||
| : row.evaluation | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-check the selected workspace's unsaved-editor protection right before the delete.
The selected workspace is decided before the sibling loop starts, and the loop awaits evidence collection for each sibling. An unsaved editor buffer that appears during that wait is not seen. The removal then runs on the earlier decision. The inspected removal path does not check unsaved editor state, and Git's non-forced refusal does not cover editor buffers. The gap existed before this change, but the sibling loop widens it.
The delete must not rely on a protection decision that is older than the delete itself. Re-decide the selected workspace after the sibling loop and before queueing the removal, or enforce the unsaved-editor protection in the removal path. A re-check alone narrows the window but does not close it.