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: resolve Windows worktree aliases during cleanup

Adam Eivy2e6c7b95c44b
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 4edb8c1fab..7bc98b5072 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -288,7 +288,7 @@ function errorExcerpt(err) {
* to the form git registered it under (macOS /var → /private/var).
*/
function canonicalPath(p) {
- try { return realpathSync(p); } catch { /* fall through to the ancestor walk */ }
+ try { return realpathSync.native(p); } catch { /* fall through to the ancestor walk */ }
const parent = dirname(p);
return parent === p ? p : join(canonicalPath(parent), p.slice(parent.length).replace(/^[\\/]+/, ''));
}
@@ -467,12 +467,13 @@ export function classifyWorktreeDirt(porcelain, { ignoredPaths = [] } = {}) {
/**
* Compare two filesystem paths for equality, resolving symlinks (e.g. macOS
- * /var → /private/var) so normalization differences don't false-negative.
+ * /var → /private/var) and Windows short-name aliases. Native realpath resolves
+ * the latter where the JavaScript implementation can retain the input spelling.
*/
function pathsEqual(a, b) {
if (!a || !b) return false;
if (a === b) return true;
- const resolved = (p) => { try { return realpathSync(p); } catch { return p; } };
+ const resolved = (p) => { try { return realpathSync.native(p); } catch { return p; } };
return resolved(a) === resolved(b);
}
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 4bb8f0387c..2fe726f12a 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { join } from 'path';
+import { dirname, join } from 'path';
// Stub every git operation; lifecycle tests exercise the real service without
// creating, changing or removing a live repository.
@@ -7,10 +7,11 @@ const execGitMock = vi.fn();
vi.mock('../lib/execGit.js', () => ({ execGit: (...args) => execGitMock(...args) }));
// Stub filesystem effects as well as git so destructive cleanup stays in fixtures.
-vi.mock('fs', () => ({
- existsSync: vi.fn().mockReturnValue(true),
- realpathSync: vi.fn((p) => p),
-}));
+vi.mock('fs', () => {
+ const realpathSync = vi.fn(p => p);
+ realpathSync.native = realpathSync;
+ return { existsSync: vi.fn().mockReturnValue(true), realpathSync };
+});
vi.mock('fs/promises', () => ({
lstat: vi.fn().mockResolvedValue({}),
readdir: vi.fn().mockResolvedValue([]),
@@ -710,6 +711,7 @@ describe('removeWorktree identity, dirt and branch preservation', () => {
hasBranchMergeEvidenceMock.mockResolvedValue(false);
existsSync.mockReset().mockReturnValue(true);
realpathSync.mockReset().mockImplementation(p => p);
+ realpathSync.native = realpathSync;
// Removal is verified against the real directory afterwards; scripted removals
// succeed, so the worktree directory reads as gone (and `worktree list` as empty).
lstat.mockReset().mockRejectedValue(Object.assign(new Error('gone'), { code: 'ENOENT' }));
@@ -778,6 +780,7 @@ describe('removeWorktree identity, dirt and branch preservation', () => {
afterEach(() => {
realpathSync.mockReset().mockImplementation(p => p);
+ realpathSync.native = realpathSync;
lstat.mockReset().mockResolvedValue({});
});
@@ -897,6 +900,50 @@ describe('removeWorktree identity, dirt and branch preservation', () => {
expect(calledWith(['branch', '-D', 'cos/task-1/agent-x'])).toBe(false);
});
+ it('preserves authored work when only native realpath unifies the Git and Node directory spellings', async () => {
+ const worktreePath = join(PATHS.worktrees, 'agent-x');
+ const alias = join('/git-alias', 'agent-x');
+ // Windows short names can survive JS realpath unchanged while Git reports
+ // their long spelling. Native realpath resolves the filesystem identity.
+ realpathSync.native = vi.fn(p => p === alias ? worktreePath : p);
+ scriptGit({ detectedToplevel: alias, porcelain: ' M src/recoverable.js' });
+
+ const result = await removeWorktree('agent-x', '/repo', 'cos/task-1/agent-x');
+
+ expect(result.removed).toBe(false);
+ expect(result.warnings.join(' ')).toContain('src/recoverable.js');
+ expect(rm).not.toHaveBeenCalled();
+ expect(calledWith(['branch', '-D', 'cos/task-1/agent-x'])).toBe(false);
+ });
+
+ it('prunes a missing checkout registered through an alias of its surviving parent', async () => {
+ const worktreePath = join(PATHS.worktrees, 'agent-x');
+ const alias = join('/git-alias', 'agent-x');
+ existsSync.mockReturnValue(false);
+ realpathSync.native = vi.fn(p => {
+ if ([worktreePath, alias].includes(p)) throw Object.assign(new Error('gone'), { code: 'ENOENT' });
+ return p === dirname(alias) ? PATHS.worktrees : p;
+ });
+ let pruned = false;
+ const scripted = execGitMock.getMockImplementation();
+ execGitMock.mockImplementation((args, ...rest) => {
+ if (args[0] === 'worktree' && args[1] === 'list') {
+ return Promise.resolve({ stdout: pruned ? '' : `worktree ${alias}\nbranch refs/heads/cos/task-1/agent-x\nprunable\n`, exitCode: 0 });
+ }
+ if (args[0] === 'worktree' && args[1] === 'remove') return Promise.reject(new Error('missing checkout'));
+ if (args[0] === 'worktree' && args[1] === 'prune') pruned = true;
+ return scripted(args, ...rest);
+ });
+
+ const result = await removeWorktree('agent-x', '/repo', 'cos/task-1/agent-x', {
+ merge: false, preserveBranchWithCommits: true,
+ });
+
+ expect(result.removed).toBe(true);
+ expect(pruned).toBe(true);
+ expect(calledWith(['branch', '-D', 'cos/task-1/agent-x'])).toBe(false);
+ });
+
it('preserves work when identity lookup and status both fail', async () => {
const scripted = execGitMock.getMockImplementation();
execGitMock.mockImplementation((args, ...rest) =>