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: re-check agent liveness before the daily cleanup reaps a worktree (#11858)

Adam Eivy7bda1fc7f43e
diff --git a/server/services/autonomousJobs.test.js b/server/services/autonomousJobs.test.js
index 0f47b36c50..7b859c8ff2 100644
--- a/server/services/autonomousJobs.test.js
+++ b/server/services/autonomousJobs.test.js
@@ -1206,7 +1206,7 @@ describe('autonomousJobs', () => {
expect(reapMergedWorktrees).toHaveBeenCalledOnce()
const [, opts] = reapMergedWorktrees.mock.calls[0]
- expect(opts.activeAgentIds.has('paused-agent-1')).toBe(true)
+ expect(opts.activeAgentIds().has('paused-agent-1')).toBe(true)
})
it('paused agent id is in the set passed to cleanupOrphanedWorktrees', async () => {
@@ -1220,8 +1220,23 @@ describe('autonomousJobs', () => {
await agentDataCleanup()
expect(cleanupOrphanedWorktrees).toHaveBeenCalledOnce()
- const [, activeIds] = cleanupOrphanedWorktrees.mock.calls[0]
- expect(activeIds.has('paused-agent-1')).toBe(true)
+ const [, getActiveIds] = cleanupOrphanedWorktrees.mock.calls[0]
+ expect(getActiveIds().has('paused-agent-1')).toBe(true)
+ })
+
+ it('both reap passes see an agent dispatched after the job took its snapshot', async () => {
+ readJSONFile.mockResolvedValueOnce({ agents: { 'paused-agent-1': { status: 'paused' } } })
+ // Empty when the job starts; agent-x registers while the earlier passes run.
+ getActiveAgentIds.mockReturnValueOnce([]).mockReturnValue(['agent-x'])
+
+ await agentDataCleanup()
+
+ const [, opts] = reapMergedWorktrees.mock.calls[0]
+ const [, getActiveIds] = cleanupOrphanedWorktrees.mock.calls[0]
+ for (const ids of [opts.activeAgentIds(), getActiveIds()]) {
+ expect(ids.has('agent-x')).toBe(true)
+ expect(ids.has('paused-agent-1')).toBe(true)
+ }
})
it('non-paused agent is NOT added to the protect set via state.json', async () => {
@@ -1236,7 +1251,7 @@ describe('autonomousJobs', () => {
const [, opts] = reapMergedWorktrees.mock.calls[0]
// running-agent-2 is not paused and not in getActiveAgentIds() → NOT in protect set
- expect(opts.activeAgentIds.has('running-agent-2')).toBe(false)
+ expect(opts.activeAgentIds().has('running-agent-2')).toBe(false)
})
it('in-memory active agents are also protected alongside paused agents', async () => {
@@ -1248,8 +1263,8 @@ describe('autonomousJobs', () => {
await agentDataCleanup()
const [, opts] = reapMergedWorktrees.mock.calls[0]
- expect(opts.activeAgentIds.has('paused-agent-1')).toBe(true)
- expect(opts.activeAgentIds.has('live-agent-99')).toBe(true)
+ expect(opts.activeAgentIds().has('paused-agent-1')).toBe(true)
+ expect(opts.activeAgentIds().has('live-agent-99')).toBe(true)
})
it('handles missing cosState gracefully (null state.json)', async () => {
diff --git a/server/services/autonomousJobs/scriptHandlers.js b/server/services/autonomousJobs/scriptHandlers.js
index 6dae8d59a1..e721dfc759 100644
--- a/server/services/autonomousJobs/scriptHandlers.js
+++ b/server/services/autonomousJobs/scriptHandlers.js
@@ -96,15 +96,21 @@ async function agentDataCleanup() {
// after a restart — the interrupted runs still recorded `running`, whose
// `.agent-done` sentinel is exactly the resume state the abandon path
// preserved on purpose (#3202). Only the sentinel sweep reads this; the
- // worktree reaps below keep using `activeIds`, since an unfinished record left
- // behind by a dead process is precisely what they exist to collect.
+ // worktree reaps below protect only live + paused ids, since an unfinished
+ // record left behind by a dead process is precisely what they exist to collect.
const unfinishedIds = new Set(activeIds)
+ const pausedIds = new Set()
if (cosState?.agents && typeof cosState.agents === 'object') {
for (const [id, agent] of Object.entries(cosState.agents)) {
- if (agent?.status === 'paused') activeIds.add(id)
+ if (agent?.status === 'paused') pausedIds.add(id)
if (agent?.status !== 'completed' && agent?.status !== 'failed') unfinishedIds.add(id)
}
}
+ for (const id of pausedIds) activeIds.add(id)
+ // The worktree reaps run tens of seconds after the snapshot above, after the
+ // sentinel sweep and transcript pruning; an agent CoS dispatches meanwhile is
+ // absent from it. They re-read live liveness per tree through this (#11858).
+ const getLiveProtectedIds = () => new Set([...getActiveAgentIds(), ...pausedIds])
// Worktree-less agents run in a REAL checkout — this repo, or a managed app's
// own repo — so a sentinel their cleanup never reached sits there as untracked
@@ -153,7 +159,7 @@ async function agentDataCleanup() {
// branch is fully merged into the default branch AND whose working tree is clean.
// This is the bulk of the cleanup and cannot lose work (merged+clean ⇒ nothing
// pending). It guards active agents and human `claim-*` worktrees.
- const merged = await reapMergedWorktrees(PATHS.root, { activeAgentIds: activeIds }).catch((err) => {
+ const merged = await reapMergedWorktrees(PATHS.root, { activeAgentIds: getLiveProtectedIds }).catch((err) => {
console.warn(`⚠️ Merged-worktree reap failed: ${err.message}`)
return { reaped: [] }
})
@@ -162,7 +168,7 @@ async function agentDataCleanup() {
// merged (e.g. an agent died after committing but before its PR landed), attempt
// to merge their commits back so the work isn't stranded. The helper guards
// `claim-*` worktrees, so this can't touch a human's in-flight claim.
- const worktreesReaped = await cleanupOrphanedWorktrees(PATHS.root, activeIds).catch((err) => {
+ const worktreesReaped = await cleanupOrphanedWorktrees(PATHS.root, getLiveProtectedIds).catch((err) => {
console.warn(`⚠️ Orphaned worktree cleanup failed: ${err.message}`)
return 0
})
diff --git a/server/services/worktreeExternalCleanup.test.js b/server/services/worktreeExternalCleanup.test.js
index a13bbcbffa..96450a0284 100644
--- a/server/services/worktreeExternalCleanup.test.js
+++ b/server/services/worktreeExternalCleanup.test.js
@@ -17,11 +17,12 @@ vi.mock('fs', async (importOriginal) => {
vi.mock('../lib/fileUtils.js', async (importOriginal) => makePathsProxy(await importOriginal(), {
dataRoot: () => lazyTempDataRoot('portos-external-cleanup-'),
}));
-const fault = vi.hoisted(() => ({ command: null, subcommand: null, cwd: null, successfulMatches: 0, pathAlias: null, aliasTarget: null, registrationPath: null, calls: [] }));
+const fault = vi.hoisted(() => ({ command: null, subcommand: null, cwd: null, successfulMatches: 0, pathAlias: null, aliasTarget: null, registrationPath: null, onGit: null, calls: [] }));
vi.mock('../lib/execGit.js', async (importOriginal) => {
const actual = await importOriginal();
return { ...actual, execGit: async (args, cwd, options) => {
fault.calls.push({ args, cwd });
+ fault.onGit?.(args, cwd);
if (args[0] === fault.command && (!fault.subcommand || args[1] === fault.subcommand) && (!fault.cwd || cwd === fault.cwd)) {
if (fault.successfulMatches > 0) {
fault.successfulMatches--;
@@ -44,6 +45,9 @@ vi.mock('../lib/execGit.js', async (importOriginal) => {
});
const root = () => realpathSync(lazyTempDataRoot('portos-external-cleanup-'));
+// An empty agent registry; omitting it reads this sandbox's missing state.json,
+// which fails closed and holds every tree.
+const NO_OWNERS = { claimOwners: { agents: [] } };
let primary;
let external;
let initialHead;
@@ -90,6 +94,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
fault.pathAlias = null;
fault.aliasTarget = null;
fault.registrationPath = null;
+ fault.onGit = null;
await resetGitWorktreeSandbox(external, initialHead);
await rm(PATHS.worktrees, { recursive: true, force: true });
await mkdir(PATHS.worktrees, { recursive: true });
@@ -111,7 +116,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
const merged = await addTree('agent-merged', { committed: true });
await execGit(['merge', '--no-ff', 'agent-merged', '--no-edit'], external);
- expect(await cleanupOrphanedWorktrees(primary, new Set(['agent-active']))).toBe(1);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(['agent-active']), NO_OWNERS)).toBe(1);
for (const path of [dirty, unmerged, locked, detached, active, human, nonAgent]) expect(existsSync(path)).toBe(true);
expect(await readFile(join(dirty, 'private-recovery.txt'), 'utf8')).toBe('dirty recovery\n');
expect(await readFile(join(locked, 'private-recovery.txt'), 'utf8')).toBe('locked recovery\n');
@@ -129,7 +134,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
fault.registrationPath = (await execGit(['rev-parse', '--show-toplevel'], path)).stdout.trim();
fault.aliasTarget = path;
fault.pathAlias = alias;
- expect(await cleanupOrphanedWorktrees(primary, new Set())).toBe(1);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(1);
expect(existsSync(path)).toBe(false);
expect(await branchExists('agent-path-alias')).toBe(false);
await unlink(alias);
@@ -139,7 +144,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
const path = await addTree('agent-relative');
const pointer = (await readFile(join(path, '.git'), 'utf8')).replace(/^gitdir: /, '').trim();
await rewriteGitPointer(path, `gitdir: ${relative(realpathSync(path), pointer)}\n`);
- expect(await cleanupOrphanedWorktrees(primary, new Set())).toBe(1);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(1);
expect(existsSync(path)).toBe(false);
});
@@ -151,7 +156,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
const unreadable = await addTree('agent-unreadable');
await rewriteGitPointer(unreadable, 'unreadable registration\n');
await rm(missingParent, { recursive: true, force: true });
- expect(await cleanupOrphanedWorktrees(primary, new Set())).toBe(0);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(0);
expect(existsSync(missing)).toBe(true);
expect(existsSync(unreadable)).toBe(true);
});
@@ -160,7 +165,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
const path = await addTree('agent-read-failure');
fault.command = command;
fault.cwd = path;
- expect(await cleanupOrphanedWorktrees(primary, new Set())).toBe(0);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(0);
expect(existsSync(path)).toBe(true);
expect(await branchExists('agent-read-failure')).toBe(true);
});
@@ -169,7 +174,7 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
const path = await addTree('agent-cherry-failure', { committed: true });
fault.command = 'cherry';
fault.successfulMatches = successfulMatches;
- expect(await cleanupOrphanedWorktrees(primary, new Set())).toBe(0);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(0);
expect(existsSync(path)).toBe(true);
expect(await branchExists('agent-cherry-failure')).toBe(true);
});
@@ -180,14 +185,33 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
// itself reports for the parent repo, whose spelling differs on Windows.
fault.command = 'worktree';
fault.subcommand = 'remove';
- expect(await cleanupOrphanedWorktrees(primary, new Set())).toBe(0);
+ expect(await cleanupOrphanedWorktrees(primary, new Set(), NO_OWNERS)).toBe(0);
expect(existsSync(path)).toBe(true);
expect(fault.calls.some(({ args }) => args[0] === 'branch' && args[1] === '-D')).toBe(false);
});
+ it('keeps the worktree and branch of an agent dispatched while the pass is inspecting its tree', async () => {
+ const path = await addTree('agent-dispatched');
+ const live = new Set();
+ // Registration lands after every pre-lock ownership read — while the pass
+ // reads the tree's status (#11858) — so only the in-lock re-read can see it.
+ fault.onGit = (args, cwd) => { if (args[0] === 'status' && cwd === path) live.add('agent-dispatched'); };
+ expect(await cleanupOrphanedWorktrees(primary, () => new Set(live), NO_OWNERS)).toBe(0);
+ expect(existsSync(path)).toBe(true);
+ expect(await branchExists('agent-dispatched')).toBe(true);
+ });
+
+ it('keeps a worktree a registered agent works in even when live liveness omits it', async () => {
+ const path = await addTree('agent-registered');
+ const owners = { claimOwners: { agents: [{ id: 'agent-registered', status: 'running', workspacePath: path, sourceWorkspace: external }] } };
+ expect(await cleanupOrphanedWorktrees(primary, () => new Set(), owners)).toBe(0);
+ expect(existsSync(path)).toBe(true);
+ expect(await branchExists('agent-registered')).toBe(true);
+ });
+
it('holds all external agent work when liveness cannot be established', async () => {
const path = await addTree('agent-unknown-liveness');
- expect(await cleanupOrphanedWorktrees(primary, null)).toBe(0);
+ expect(await cleanupOrphanedWorktrees(primary, null, NO_OWNERS)).toBe(0);
expect(existsSync(path)).toBe(true);
});
});
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 830ca92bf7..e250a9bcd5 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -1538,15 +1538,51 @@ export async function listWorktrees(sourceWorkspace) {
return worktrees;
}
+// Registry the destructive passes re-read under the claim-ownership lock.
+// Omitted reads state.json; `{ agents }` / `{ readAgents }` inject owners.
+function claimOwnerReader(claimOwners, readClaimOwnershipAgents) {
+ return claimOwners === undefined ? readClaimOwnershipAgents
+ : async () => claimOwners?.readAgents ? claimOwners.readAgents().catch(() => null) : claimOwners?.agents;
+}
+
+// A liveness snapshot ages while the caller's earlier passes run, so the orphan
+// passes re-read it per tree (#11858). A bare Set/null is a fixed snapshot.
+function activeIdsReader(activeIds) {
+ return typeof activeIds === 'function' ? activeIds : () => activeIds;
+}
+
+/**
+ * Remove an orphan only if it is STILL unowned: liveness re-read and the agent
+ * registry checked inside the lock a claim bind takes, so neither an agent
+ * dispatched since the caller's last check nor a registered claim/agent owner
+ * 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('./cosClaimOwnership.js');
+ return withClaimOwnershipLock(async () => {
+ if (isOwned()) return null;
+ const agents = await readOwners();
+ if (claimCheckoutOwnerReason({ branchName, holderPath, sourceWorkspace, agents })) return null;
+ return remove();
+ });
+}
+
/**
* Clean up any orphaned worktrees (worktrees whose agent no longer exists)
*
* @param {string} sourceWorkspace - The original git repository path
- * @param {Set<string>} activeAgentIds - Set of currently active agent IDs
+ * @param {(() => Set<string>|null)|Set<string>|null} getActiveIds - live agent ids, re-read
+ * for every worktree. Anything but a Set reads as "liveness unknown" and holds every `agent-*` tree.
+ * @param {object} [options]
+ * @param {{agents?: object[]|null, readAgents?: () => Promise<object[]|null>}} [options.claimOwners] -
+ * agent registry override (tests); omitted reads state.json
*/
-export async function cleanupOrphanedWorktrees(sourceWorkspace, activeAgentIds) {
+export async function cleanupOrphanedWorktrees(sourceWorkspace, getActiveIds, { claimOwners } = {}) {
if (!existsSync(WORKTREES_DIR)) return 0;
+ const readActiveIds = activeIdsReader(getActiveIds);
+ const { readClaimOwnershipAgents } = await import('./cosClaimOwnership.js');
+ const readOwners = claimOwnerReader(claimOwners, readClaimOwnershipAgents);
const worktrees = await listWorktrees(sourceWorkspace).catch(() => []);
let cleaned = 0;
@@ -1554,34 +1590,37 @@ export async function cleanupOrphanedWorktrees(sourceWorkspace, activeAgentIds)
const handledAgentIds = new Set();
for (const wt of worktrees) {
- const ownershipReason = worktreeOwnershipReason({
+ const ownershipReason = () => worktreeOwnershipReason({
path: wt.path,
locked: wt.locked,
- activeAgentIds,
+ activeAgentIds: readActiveIds(),
roots: [{ path: WORKTREES_DIR, requireAgentId: true }],
requireKnownLiveness: true,
});
- if (ownershipReason === 'worktree-unmanaged-location') continue;
+ const initialReason = ownershipReason();
+ if (initialReason === 'worktree-unmanaged-location') continue;
const agentId = worktreeAgentId(wt.path);
handledAgentIds.add(agentId);
- if (ownershipReason) continue;
+ if (initialReason) continue;
// A detached HEAD has no branch to merge or measure; force-removing it would drop commits only its reflog holds.
if (wt.detached || !wt.branch) continue;
const branchName = wt.branch.replace('refs/heads/', '');
// Attempt merge so committed work from preserved worktrees (e.g., PR/push failures) isn't lost.
// If merge fails, the branch is preserved for manual recovery.
- const result = await removeWorktree(agentId, sourceWorkspace, branchName, { merge: true })
- .catch(err => {
- console.log(`⚠️ Failed to clean orphaned worktree ${agentId}: ${err.message}`);
- return { removed: false };
- });
+ const result = await removeIfStillOrphaned(
+ { isOwned: ownershipReason, branchName, holderPath: wt.path, sourceWorkspace, readOwners },
+ () => removeWorktree(agentId, sourceWorkspace, branchName, { merge: true }),
+ ).catch(err => {
+ console.log(`⚠️ Failed to clean orphaned worktree ${agentId}: ${err.message}`);
+ return { removed: false };
+ });
if (result?.removed) cleaned++;
}
// Scan for external-repo worktrees (directories whose .git points to a different repo).
// These are invisible to `git worktree list` run against PortOS.
- cleaned += await cleanupExternalRepoWorktrees(activeAgentIds, handledAgentIds);
+ cleaned += await cleanupExternalRepoWorktrees(readActiveIds, handledAgentIds, readOwners);
if (cleaned > 0) {
console.log(`🌳 Cleaned ${cleaned} orphaned worktree(s)`);
@@ -1633,8 +1672,9 @@ export async function cleanupOrphanedWorktrees(sourceWorkspace, activeAgentIds)
*
* @param {string} sourceWorkspace - repo root
* @param {object} [options]
- * @param {Set<string>} [options.activeAgentIds] - CoS agents currently running (never reaped).
- * Anything but a Set reads as "liveness unknown" and holds every `agent-*` tree.
+ * @param {Set<string>|(() => Set<string>)} [options.activeAgentIds] - CoS agents currently running
+ * (never reaped), or a getter re-read per tree and again before removal. Anything but a
+ * Set reads as "liveness unknown" and holds every `agent-*` tree.
* @param {Set<string>} [options.excludeBranches] - branch names never reaped, whatever their tree
* @param {boolean} [options.includeClaudeTrees=true] - also reap `.claude/worktrees/`
* @param {boolean} [options.includeUnmanagedTrees=false] - consider worktrees outside the managed roots
@@ -1657,8 +1697,8 @@ export async function reapMergedWorktrees(sourceWorkspace, {
const { readBranchCompletionHold } = await import('./branchCompletionPolicy.js');
// Every entry point (scheduled, reconcile, operator cleanup) needs a trusted
// registry. loadState/getAgents may default a corrupt file to empty state.
- const readOwners = claimOwners === undefined ? readClaimOwnershipAgents
- : async () => claimOwners?.readAgents ? claimOwners.readAgents().catch(() => null) : claimOwners?.agents;
+ const readOwners = claimOwnerReader(claimOwners, readClaimOwnershipAgents);
+ const readActiveIds = activeIdsReader(activeAgentIds);
const defaultBranch = knownDefaultBranch || await getDefaultBranch(sourceWorkspace).catch(() => null) || 'main';
const target = await resolveMergeTarget(sourceWorkspace, defaultBranch);
@@ -1698,7 +1738,7 @@ export async function reapMergedWorktrees(sourceWorkspace, {
const ownershipReason = worktreeOwnershipReason({
path: wt.path,
locked: wt.locked,
- activeAgentIds,
+ activeAgentIds: readActiveIds(),
roots: managedRoots,
requireKnownLiveness: true,
});
@@ -1731,7 +1771,7 @@ export async function reapMergedWorktrees(sourceWorkspace, {
if (ownerReason) return ownerReason;
const policyHold = await readBranchCompletionHold({ repoPath: sourceWorkspace, branch: branchName, retirement: true });
if (policyHold) return policyHold;
- const reason = worktreeOwnershipReason({ path: wt.path, locked: holder.locked, activeAgentIds, roots: managedRoots, requireKnownLiveness: true });
+ const reason = worktreeOwnershipReason({ path: wt.path, locked: holder.locked, activeAgentIds: readActiveIds(), roots: managedRoots, requireKnownLiveness: true });
if (reason) return reason;
const freshStatus = await readWorktreeStatus(wt.path);
if (freshStatus === null) return 'worktree-status-unavailable';
@@ -1771,7 +1811,7 @@ export async function reapMergedWorktrees(sourceWorkspace, {
* These are created when agents work on apps outside PortOS but use the shared
* worktrees directory. They're invisible to PortOS's `git worktree list`.
*/
-async function cleanupExternalRepoWorktrees(activeAgentIds, alreadyHandled) {
+async function cleanupExternalRepoWorktrees(readActiveIds, alreadyHandled, readOwners) {
const { getDefaultBranch, hasBranchMergeEvidence } = await import('./git.js');
const entries = await readdir(WORKTREES_DIR, { withFileTypes: true }).catch(() => []);
let cleaned = 0;
@@ -1779,13 +1819,14 @@ async function cleanupExternalRepoWorktrees(activeAgentIds, alreadyHandled) {
for (const entry of entries) {
if (!entry.isDirectory() || alreadyHandled.has(entry.name)) continue;
const worktreePath = join(WORKTREES_DIR, entry.name);
- const ownership = {
+ const ownership = (extra = {}) => worktreeOwnershipReason({
path: worktreePath,
- activeAgentIds,
+ activeAgentIds: readActiveIds(),
roots: [{ path: WORKTREES_DIR, requireAgentId: true }],
requireKnownLiveness: true,
- };
- if (worktreeOwnershipReason(ownership)) continue;
+ ...extra,
+ });
+ if (ownership()) continue;
const gitFile = await lstat(join(worktreePath, '.git')).catch(() => null);
if (!gitFile?.isFile()) continue;
@@ -1805,7 +1846,7 @@ async function cleanupExternalRepoWorktrees(activeAgentIds, alreadyHandled) {
const primary = registrations[0];
if (!wt || wt.bare || wt.detached || wt.prunable || !wt.branch) continue;
if (!primary || primary.bare || pathsEqual(primary.path, gitWorktreePath)) continue;
- if (worktreeOwnershipReason({ ...ownership, locked: wt.locked })) continue;
+ if (ownership({ locked: wt.locked })) continue;
const parentRepo = primary.path;
const parentCommonDir = await execGit(['rev-parse', '--git-common-dir'], parentRepo)
@@ -1823,15 +1864,25 @@ async function cleanupExternalRepoWorktrees(activeAgentIds, alreadyHandled) {
// Non-force removal is a final Git guard against newly dirty or locked
// trees. A refusal must never fall through to recursive filesystem removal.
- const removed = await execGit(['worktree', 'remove', worktreePath], parentRepo).then(() => true).catch(err => {
+ const removed = await removeIfStillOrphaned(
+ { isOwned: () => ownership({ locked: wt.locked }), branchName, holderPath: worktreePath, sourceWorkspace: parentRepo, readOwners },
+ async () => {
+ const ok = await execGit(['worktree', 'remove', worktreePath], parentRepo).then(() => true).catch(err => {
+ console.warn(`⚠️ Preserved external worktree ${entry.name}: ${err.message}`);
+ return false;
+ });
+ if (ok) {
+ await execGit(['branch', '-D', branchName], parentRepo).catch(err => {
+ console.warn(`⚠️ External branch cleanup failed for ${branchName}: ${err.message}`);
+ });
+ }
+ return ok;
+ },
+ ).catch(err => {
console.warn(`⚠️ Preserved external worktree ${entry.name}: ${err.message}`);
return false;
});
- if (!removed) continue;
- await execGit(['branch', '-D', branchName], parentRepo).catch(err => {
- console.warn(`⚠️ External branch cleanup failed for ${branchName}: ${err.message}`);
- });
- cleaned++;
+ if (removed) cleaned++;
}
return cleaned;
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 0525b44818..c45ce5ed1d 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -1636,7 +1636,7 @@ describe('cleanupOrphanedWorktrees ownership and removal', () => {
return Promise.resolve({ stdout: '', stderr: '', exitCode: 0 });
});
- expect(await cleanupOrphanedWorktrees('/repo', new Set(['agent-active']))).toBe(1);
+ expect(await cleanupOrphanedWorktrees('/repo', new Set(['agent-active']), { claimOwners: { agents: [] } })).toBe(1);
expect(execGitMock.mock.calls.filter(([args]) => args[0] === 'worktree' && args[1] === 'remove'))
.toEqual([[['worktree', 'remove', tree('agent-dead'), '--force'], '/repo']]);
expect(execGitMock.mock.calls.filter(([args]) => args[0] === 'branch' && args[1] === '-D'))
@@ -1645,4 +1645,43 @@ describe('cleanupOrphanedWorktrees ownership and removal', () => {
.toEqual([tree('agent-dead'), tree('agent-dirty')]);
expect(rm).not.toHaveBeenCalled();
});
+
+ it('re-reads liveness and the agent registry per tree, so a tree claimed after the caller snapshot survives', async () => {
+ const entries = [
+ { path: '/repo', branch: 'main' },
+ { path: tree('agent-late'), branch: 'cos/task/agent-late' },
+ { path: tree('agent-registered'), branch: 'cos/task/agent-registered' },
+ { path: tree('agent-dead'), branch: 'cos/task/agent-dead' },
+ ];
+ const live = new Set();
+ const removedPaths = new Set();
+ execGitMock.mockImplementation((args, cwd) => {
+ if (args[0] === 'worktree' && args[1] === 'list') {
+ // CoS dispatches agent-late after the job snapshot, before this pass reaches its tree.
+ live.add('agent-late');
+ return Promise.resolve({ stdout: entries.filter(e => !removedPaths.has(e.path))
+ .flatMap(e => ['worktree ' + e.path, 'HEAD abc123', 'branch refs/heads/' + e.branch, '']).join('\n') });
+ }
+ if (args[0] === 'worktree' && args[1] === 'remove') removedPaths.add(args[2]);
+ if (args[0] === 'rev-parse') return Promise.resolve({ stdout: args[1] === '--show-toplevel' ? cwd : 'main' });
+ if (args[0] === 'rev-list') return Promise.resolve({ stdout: '0', stderr: '', exitCode: 0 });
+ return Promise.resolve({ stdout: '', stderr: '', exitCode: 0 });
+ });
+ const claimOwners = { agents: [{ id: 'agent-registered', status: 'running', workspacePath: tree('agent-registered'), sourceWorkspace: '/repo' }] };
+
+ expect(await cleanupOrphanedWorktrees('/repo', () => new Set(live), { claimOwners })).toBe(1);
+ expect(execGitMock.mock.calls.filter(([args]) => args[0] === 'worktree' && args[1] === 'remove'))
+ .toEqual([[['worktree', 'remove', tree('agent-dead'), '--force'], '/repo']]);
+ expect(execGitMock.mock.calls.filter(([args]) => args[0] === 'branch' && args[1] === '-D'))
+ .toEqual([[['branch', '-D', 'cos/task/agent-dead'], '/repo']]);
+ });
+
+ it('holds every tree when the agent registry cannot be read', async () => {
+ execGitMock.mockImplementation((args) => args[0] === 'worktree' && args[1] === 'list'
+ ? Promise.resolve({ stdout: ['worktree ' + tree('agent-dead'), 'HEAD abc123', 'branch refs/heads/cos/task/agent-dead', ''].join('\n') })
+ : Promise.resolve({ stdout: '', stderr: '', exitCode: 0 }));
+
+ expect(await cleanupOrphanedWorktrees('/repo', () => new Set(), { claimOwners: { agents: null } })).toBe(0);
+ expect(execGitMock.mock.calls.some(([args]) => args[0] === 'worktree' && args[1] === 'remove')).toBe(false);
+ });
});