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 external worktree aliases using Git metadata (#9907)

Adam Eivyd57c141bf65f
diff --git a/server/services/updateRepoReadiness.test.js b/server/services/updateRepoReadiness.test.js
index 3a74acdb29..7bad32cd8f 100644
--- a/server/services/updateRepoReadiness.test.js
+++ b/server/services/updateRepoReadiness.test.js
@@ -2,7 +2,12 @@ import { mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { execFileSync } from 'child_process';
-import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterAll, beforeEach, describe, expect, it as vitestIt, vi } from 'vitest';
+
+import { ownTestBodies } from '../lib/mockPathsDataRoot.js';
+
+const owned = ownTestBodies(vitestIt);
+const it = owned.it;
// The CoS agent registry is the one collaborator these fixtures cannot supply:
// the temp repos are not a PortOS install, so the live registry read fails
@@ -36,7 +41,13 @@ function makeRepo() {
}
beforeEach(() => vi.restoreAllMocks());
-afterAll(() => repos.forEach((dir) => rmSync(dir, { recursive: true, force: true })));
+// Fail-fast cancellation can finish Vitest's wrapper while its Git subprocess
+// still owns the checkout. Drain those bodies before deleting the fixtures.
+afterAll(async () => {
+ try { await owned.drain(); } finally {
+ repos.forEach((dir) => rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }));
+ }
+});
describe('update repo readiness', () => {
it('reports a clean checkout on the default branch as ready', async () => {
diff --git a/server/services/worktreeExternalCleanup.test.js b/server/services/worktreeExternalCleanup.test.js
index f6b2a54d04..581ada5982 100644
--- a/server/services/worktreeExternalCleanup.test.js
+++ b/server/services/worktreeExternalCleanup.test.js
@@ -1,7 +1,7 @@
/** Real separate-repository fixtures for the public scheduled cleanup consumer. */
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { existsSync, realpathSync } from 'fs';
-import { mkdir, readFile, rm, writeFile } from 'fs/promises';
+import { mkdir, readFile, rm, symlink, unlink, writeFile } from 'fs/promises';
import { join, relative } from 'path';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
import { materializeGitRepo, resetGitWorktreeSandbox, SKIP_HEAVY_INTEGRATION } from '../lib/gitTestRepo.js';
@@ -9,10 +9,15 @@ import { PATHS } from '../lib/fileUtils.js';
import { execGit } from '../lib/execGit.js';
import { cleanupOrphanedWorktrees } from './worktreeManager.js';
+vi.mock('fs', async (importOriginal) => {
+ const actual = await importOriginal();
+ return { ...actual, realpathSync: path => fault.pathAlias && (path === fault.aliasTarget || path === fault.pathAlias) ? path : actual.realpathSync(path) };
+});
+
vi.mock('../lib/fileUtils.js', async (importOriginal) => makePathsProxy(await importOriginal(), {
dataRoot: () => lazyTempDataRoot('portos-external-cleanup-'),
}));
-const fault = vi.hoisted(() => ({ command: null, cwd: null, successfulMatches: 0, calls: [] }));
+const fault = vi.hoisted(() => ({ command: null, cwd: null, successfulMatches: 0, pathAlias: null, aliasTarget: null, registrationPath: null, calls: [] }));
vi.mock('../lib/execGit.js', async (importOriginal) => {
const actual = await importOriginal();
return { ...actual, execGit: async (args, cwd, options) => {
@@ -25,7 +30,16 @@ vi.mock('../lib/execGit.js', async (importOriginal) => {
if (options?.ignoreExitCode) return { stdout: '', stderr: 'fixture read failure', exitCode: 1 };
throw new Error('fixture read failure');
}
- return actual.execGit(args, cwd, options);
+ const result = await actual.execGit(args, cwd, options);
+ // Model Git's expanded path spelling where Node retains a filesystem alias
+ // (Windows 8.3 paths). Both names address the same real disposable checkout.
+ if (fault.pathAlias && args[0] === 'worktree' && args[1] === 'list') {
+ result.stdout = result.stdout.replaceAll(fault.registrationPath.replace(/\\/g, '/'), fault.pathAlias.replace(/\\/g, '/'));
+ }
+ if (fault.pathAlias && cwd === fault.aliasTarget && args[0] === 'rev-parse' && args[1] === '--show-toplevel') {
+ result.stdout = fault.pathAlias.replace(/\\/g, '/') + '\n';
+ }
+ return result;
} };
});
@@ -60,6 +74,9 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
fault.command = null;
fault.cwd = null;
fault.successfulMatches = 0;
+ fault.pathAlias = null;
+ fault.aliasTarget = null;
+ fault.registrationPath = null;
await resetGitWorktreeSandbox(external, initialHead);
await rm(PATHS.worktrees, { recursive: true, force: true });
await mkdir(PATHS.worktrees, { recursive: true });
@@ -90,6 +107,21 @@ describe.skipIf(SKIP_HEAVY_INTEGRATION)('external orphan cleanup recovery policy
expect(await branchExists('agent-merged')).toBe(false);
});
+ it('matches Git registration spelling when Node retains another alias for the same checkout', async () => {
+ const path = await addTree('agent-path-alias');
+ const aliasRoot = join(root(), 'git-spelling');
+ await mkdir(aliasRoot, { recursive: true });
+ const alias = join(aliasRoot, 'agent-path-alias');
+ await symlink(path, alias, 'junction');
+ 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(existsSync(path)).toBe(false);
+ expect(await branchExists('agent-path-alias')).toBe(false);
+ await unlink(alias);
+ });
+
it('resolves a relative .git pointer through Git rather than assuming an absolute parent path', async () => {
const path = await addTree('agent-relative');
const pointer = (await readFile(join(path, '.git'), 'utf8')).replace(/^gitdir: /, '').trim();
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 36e3c7b191..0bf5c6765a 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -1520,6 +1520,8 @@ async function cleanupExternalRepoWorktrees(activeAgentIds, alreadyHandled) {
requireKnownLiveness: true,
};
if (worktreeOwnershipReason(ownership)) continue;
+ const gitFile = await lstat(join(worktreePath, '.git')).catch(() => null);
+ if (!gitFile?.isFile()) continue;
// Ask Git for its common directory and actual registration, rather than
// inferring a parent from the spelling of a .git file. Relative pointers,
@@ -1527,11 +1529,16 @@ async function cleanupExternalRepoWorktrees(activeAgentIds, alreadyHandled) {
const commonDir = await execGit(['rev-parse', '--git-common-dir'], worktreePath)
.then(r => r.stdout.trim() ? resolve(worktreePath, r.stdout.trim()) : null).catch(() => null);
if (!commonDir) continue;
+ // Git expands Windows short (8.3) aliases even where Node's realpath
+ // keeps them. Compare Git's own spelling with its registration output.
+ const gitWorktreePath = await execGit(['rev-parse', '--show-toplevel'], worktreePath)
+ .then(r => r.stdout.trim()).catch(() => null);
+ if (!gitWorktreePath) continue;
const registrations = await listWorktrees(worktreePath).catch(() => []);
- const wt = registrations.find(candidate => pathsEqual(candidate.path, worktreePath));
+ const wt = registrations.find(candidate => pathsEqual(candidate.path, gitWorktreePath));
const primary = registrations[0];
if (!wt || wt.bare || wt.detached || wt.prunable || !wt.branch) continue;
- if (!primary || primary.bare || pathsEqual(primary.path, worktreePath)) continue;
+ if (!primary || primary.bare || pathsEqual(primary.path, gitWorktreePath)) continue;
if (worktreeOwnershipReason({ ...ownership, locked: wt.locked })) continue;
const parentRepo = primary.path;