Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-12682]): keep agent cleanup off a checkout another retirement is archiving

[._.]/ Adam Eivyc2509c7e7d77
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 44379118b9..e21c6cf2cd 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -1385,11 +1385,13 @@ async function cleanupWorktreeBranch(sourceWorkspace, branchName, {
* run, forever. ONLY the discard path may pass this.
*/
export async function removeWorktree(agentId, sourceWorkspace, branchName, options = {}) {
- const { withClaimOwnershipLock, readClaimOwnershipAgents } = await import('./cosClaimOwnershipState.js');
- const ownershipHold = async () => claimCheckoutOwnerReason({
- branchName, holderPath: join(WORKTREES_DIR, agentId), sourceWorkspace,
- agents: await readClaimOwnershipAgents().catch(() => null), ignoreIds: [agentId],
- });
+ const { withClaimOwnershipLock, readClaimOwnershipAgents, checkoutRetirementFenceReason } = await import('./cosClaimOwnershipState.js');
+ // A retirement archiving this checkout with the lock released owns it until removal.
+ const ownershipHold = async () => checkoutRetirementFenceReason({ repoPath: sourceWorkspace, path: join(WORKTREES_DIR, agentId), branch: branchName })
+ || claimCheckoutOwnerReason({
+ branchName, holderPath: join(WORKTREES_DIR, agentId), sourceWorkspace,
+ agents: await readClaimOwnershipAgents().catch(() => null), ignoreIds: [agentId],
+ });
const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');
return withClaimOwnershipLock(() => withCheckoutPublication(sourceWorkspace,
() => removeWorktreeNow(agentId, sourceWorkspace, branchName, options, ownershipHold)));
@@ -1844,9 +1846,9 @@ function activeIdsReader(activeIds) {
* can be removed out from under it. Resolves `remove()`'s result, or null when held.
*/
async function removeIfStillOrphaned({ isOwned, branchName, holderPath, sourceWorkspace, readOwners }, remove) {
- const { withClaimOwnershipLock } = await import('./cosClaimOwnershipState.js');
+ const { withClaimOwnershipLock, checkoutRetirementFenceReason } = await import('./cosClaimOwnershipState.js');
return withClaimOwnershipLock(async () => {
- if (isOwned()) return null;
+ if (isOwned() || checkoutRetirementFenceReason({ repoPath: sourceWorkspace, path: holderPath, branch: branchName })) return null;
const agents = await readOwners();
if (claimCheckoutOwnerReason({ branchName, holderPath, sourceWorkspace, agents })) return null;
const { withCheckoutPublication } = await import('../lib/checkoutRetirementAdmission.js');
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index f2ad8447f3..cd19dd861b 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -1161,6 +1161,17 @@ describe('removeWorktree identity, dirt and branch preservation', () => {
|| (args[0] === 'worktree' && args[1] === 'remove'))).toBe(false);
});
+ it('leaves a checkout another retirement is archiving to that retirement (#12682)', async () => {
+ const { reserveCheckoutRetirementFence } = await import('./cosClaimOwnershipState.js');
+ const release = reserveCheckoutRetirementFence({ repoPath: '/repo', path: join(PATHS.worktrees, 'agent-x'), branch: 'cos/task-1/agent-x' });
+ try {
+ const result = await removeWorktree('agent-x', '/repo', 'cos/task-1/agent-x');
+ expect(result.removed).toBe(false);
+ expect(result.warnings.join(' ')).toContain('checkout-retirement-reserved');
+ expect(preserveCheckoutMock).not.toHaveBeenCalled();
+ } finally { release(); }
+ });
+
it.each(['present', 'missing', 'lockfile'])('retains %s checkout evidence when archival fails', async state => {
if (state === 'missing') existsSync.mockReturnValue(false);
if (state === 'lockfile') scriptGit({ porcelain: ' M package-lock.json' });