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: orphan worktree sweep keeps detached-HEAD agent trees that may hold commits (#11868)

Adam Eivy86b2b5252afc
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 4a5d9a64a5..b0584bc089 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -1565,8 +1565,10 @@ export async function cleanupOrphanedWorktrees(sourceWorkspace, activeAgentIds)
const agentId = worktreeAgentId(wt.path);
handledAgentIds.add(agentId);
if (ownershipReason) 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/', '') || '';
+ 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 })
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 9a45ca5019..0525b44818 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -1602,7 +1602,7 @@ describe('cleanupOrphanedWorktrees ownership and removal', () => {
lstat.mockReset().mockResolvedValue({});
});
- it('removes only the clean inactive agent and preserves active, human, locked and dirty trees', async () => {
+ it('removes only the clean inactive agent and preserves active, human, locked, dirty and detached trees', async () => {
const entries = [
{ path: '/repo', branch: 'main' },
{ path: join(PATHS.worktrees + '-old', 'agent-outside'), branch: 'outside' },
@@ -1611,12 +1611,15 @@ describe('cleanupOrphanedWorktrees ownership and removal', () => {
{ path: tree('agent-locked'), branch: 'cos/task/agent-locked', locked: true },
{ path: tree('agent-dead'), branch: 'cos/task/agent-dead' },
{ path: tree('agent-dirty'), branch: 'cos/task/agent-dirty' },
+ // Detached HEAD (e.g. interrupted rebase) may carry commits no ref holds; removal would orphan them.
+ { path: tree('agent-detached'), detached: true },
];
// `worktree list` reflects removals made so far, as real git does — removal is
// verified against the registration afterwards.
const removedPaths = new Set();
const listing = () => entries.filter(entry => !removedPaths.has(entry.path)).flatMap(entry => [
- 'worktree ' + entry.path, 'HEAD abc123', 'branch refs/heads/' + entry.branch,
+ 'worktree ' + entry.path, 'HEAD abc123',
+ entry.detached ? 'detached' : 'branch refs/heads/' + entry.branch,
...(entry.locked ? ['locked protected'] : []), '',
]).join('\n');
// Include the same on-disk entries in the external-repo scan: ownership