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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: detect broken worktrees resolving to parent repo instead of app repo
Adam Eivy91de6ff0b92b
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex bde72000d6..8244be13f8 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -26,3 +26,4 @@- CoS agents API returned full output arrays (600KB+) for all agents in listing — output now stripped from listing response and loaded on demand- **Resume Agent modal not dismissing** — clicking "Queue Resume Task" successfully queued the task but left the modal open; modal now closes on successful submit- **Pending task edit form unresponsive** — save/cancel buttons on the edit form for pending tasks did not respond to taps; dnd-kit's sortable wrapper interfered with pointer events inside sortable items. Sorting is now disabled while editing, pointer events are stopped from propagating through the edit form, and button touch targets are enlarged to 40px minimum height+- **Broken worktree detection** — worktree cleanup now verifies `rev-parse --show-toplevel` matches the expected worktree path before trusting `git status`; prevents broken worktrees (missing `.git` file) from resolving to a parent repo and incorrectly blocking removaldiff --git a/server/services/cos.js b/server/services/cos.jsindex ffceab931a..b64514a8f6 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -1289,6 +1289,7 @@ async function generateSelfImprovementTaskForType(taskType, state, taskDescripti// Use configured model/provider if specified, otherwise use defaultif (interval.providerId) {+ metadata.provider = interval.providerId;metadata.providerId = interval.providerId;}if (interval.model) {diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.jsindex 5778c5ba87..d15c52198a 100644--- a/server/services/worktreeManager.js+++ b/server/services/worktreeManager.js@@ -116,6 +116,23 @@ export async function removeWorktree(agentId, sourceWorkspace, branchName, optioreturn { merged: false, removed: true, uncommittedSaved: false, warnings };}+ // Verify the worktree still points to the correct repo before trusting git status.+ // If the .git file is missing (e.g., worktree was partially cleaned up), git walks up+ // the directory tree and may find a parent repo (e.g., PortOS) instead of the app repo.+ // In that case, git status would report the parent repo's dirty files, causing us to+ // incorrectly preserve the worktree.+ const detectedToplevel = await execGit(['rev-parse', '--show-toplevel'], worktreePath)+ .then(s => s.trim())+ .catch(() => null);+ if (detectedToplevel && detectedToplevel !== worktreePath) {+ console.log(`🌳 Worktree ${agentId} resolves to ${detectedToplevel} instead of ${worktreePath} — broken worktree, removing`);+ await rm(worktreePath, { recursive: true, force: true }).catch(rmErr => {+ console.log(`⚠️ Failed to remove broken worktree ${agentId}: ${rmErr.message}`);+ });+ await execGit(['branch', '-D', branchName], sourceWorkspace).catch(() => {});+ return { merged: false, removed: true, uncommittedSaved: false, warnings };+ }+// Safety check: abort removal when uncommitted changes are detected.// Also fail closed if git status itself fails — treat unknown state as dirty.let dirtyFiles;diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.jsindex d606daa7db..44ba3c0dbf 100644--- a/server/services/worktreeManager.test.js+++ b/server/services/worktreeManager.test.js@@ -231,6 +231,32 @@ describe('Auto-generated Lockfile Detection', () => {});});+describe('Broken Worktree Detection', () => {+ // Mirrors the rev-parse validation logic in removeWorktree that prevents+ // git status from resolving to a parent repo (e.g., PortOS) when the+ // worktree's .git file is missing+ function isBrokenWorktree(detectedToplevel, expectedWorktreePath) {+ return detectedToplevel && detectedToplevel !== expectedWorktreePath;+ }++ it('should detect worktree resolving to parent repo as broken', () => {+ const worktreePath = '/data/cos/worktrees/agent-abc';+ const detectedToplevel = '/Users/user/PortOS'; // parent repo+ expect(isBrokenWorktree(detectedToplevel, worktreePath)).toBe(true);+ });++ it('should not flag valid worktree as broken', () => {+ const worktreePath = '/data/cos/worktrees/agent-abc';+ const detectedToplevel = '/data/cos/worktrees/agent-abc';+ expect(isBrokenWorktree(detectedToplevel, worktreePath)).toBe(false);+ });++ it('should not flag as broken when rev-parse fails (null)', () => {+ const worktreePath = '/data/cos/worktrees/agent-abc';+ expect(isBrokenWorktree(null, worktreePath)).toBeFalsy();+ });+});+describe('Orphaned Worktree Detection', () => {function findOrphanedWorktrees(worktrees, worktreesDir, activeAgentIds) {return worktrees.filter(wt => {