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: link managed app package dependencies into agent worktrees (#8801)
[._.]/ Adam Eivya09432d94842
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.jsindex fa88a5b3fe..2e2f674f3a 100644--- a/server/services/worktreeManager.js+++ b/server/services/worktreeManager.js@@ -45,7 +45,20 @@ const DIRT_PATHS_IN_WARNING = 5;// Dependencies are linked from the source checkout for ordinary ephemeral// worktrees. Dependency-update tasks opt out so package managers cannot mutate// the source checkout's installed tree through these links.-const WORKTREE_DEPENDENCY_PATHS = ['node_modules', 'client/node_modules', 'server/node_modules'];+async function worktreeDependencyPaths(sourceWorkspace, worktreePath) {+ const entries = await readdir(sourceWorkspace, { withFileTypes: true });+ const children = await Promise.all(entries+ .filter(entry => entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules')+ .map(async ({ name }) => {+ const targetDirectory = await lstat(join(worktreePath, name)).catch(() => null);+ if (!targetDirectory?.isDirectory()) return null;+ if (existsSync(join(sourceWorkspace, name, '.git')) || existsSync(join(worktreePath, name, '.git'))) return null;+ const manifest = await lstat(join(worktreePath, name, 'package.json')).catch(() => null);+ if (!manifest?.isFile()) return null;+ return join(name, 'node_modules');+ }));+ return ['node_modules', ...children.filter(Boolean)];+}/*** Make installed dependencies available to a fresh worktree without copying@@ -54,10 +67,11 @@ const WORKTREE_DEPENDENCY_PATHS = ['node_modules', 'client/node_modules', 'serve* layout. Existing paths are preserved, including real directories and links.*/export async function linkWorktreeDependencies(sourceWorkspace, worktreePath) {- await Promise.all(WORKTREE_DEPENDENCY_PATHS.map(async (relativePath) => {+ const dependencyPaths = await worktreeDependencyPaths(sourceWorkspace, worktreePath);+ await Promise.all(dependencyPaths.map(async (relativePath) => {const sourcePath = join(sourceWorkspace, relativePath);const targetPath = join(worktreePath, relativePath);- const sourceExists = await lstat(sourcePath).then(() => true).catch(() => false);+ const sourceExists = await stat(sourcePath).then(info => info.isDirectory()).catch(() => false);if (!sourceExists) return;const targetExists = await lstat(targetPath).then(() => true).catch(() => false);if (targetExists) return;@@ -70,7 +84,8 @@ export async function linkWorktreeDependencies(sourceWorkspace, worktreePath) {* Real dependency directories and links managed by another tool stay intact.*/export async function unlinkWorktreeDependencies(sourceWorkspace, worktreePath) {- await Promise.all(WORKTREE_DEPENDENCY_PATHS.map(async (relativePath) => {+ const dependencyPaths = await worktreeDependencyPaths(sourceWorkspace, worktreePath);+ await Promise.all(dependencyPaths.map(async (relativePath) => {const sourcePath = join(sourceWorkspace, relativePath);const targetPath = join(worktreePath, relativePath);const targetStat = await lstat(targetPath).catch((err) => {diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.jsindex 9e090cbd80..82085561d0 100644--- a/server/services/worktreeManager.test.js+++ b/server/services/worktreeManager.test.js@@ -60,7 +60,7 @@ const { isPathInsideDir } = await import('../lib/fileUtils.js');const { worktreeOwnershipReason } = await import('../lib/worktreeOwnership.js');const { win32 } = await import('path');const { existsSync } = await import('fs');-const { lstat, readlink, symlink, unlink } = await import('fs/promises');+const { lstat, readdir, stat, readlink, symlink, unlink } = await import('fs/promises');const { PATHS } = await import('../lib/fileUtils.js');/**@@ -118,63 +118,80 @@ describe('Worktree Path Construction', () => {});describe('Worktree dependency preparation', () => {+ const directories = ['client', 'server', 'admin', 'uninstalled', 'assets', '.hidden', 'node_modules', 'nested'];+ const missing = () => Promise.reject(Object.assign(new Error('missing'), { code: 'ENOENT' }));+ const normalize = path => path.replaceAll('\\', '/');+beforeEach(() => {lstat.mockReset();readlink.mockReset();symlink.mockClear();unlink.mockClear();- });-- it('links installed root, client, and server dependencies when targets are absent', async () => {- lstat.mockImplementation((path) => path.replaceAll('\\', '/').startsWith('/repo/')- ? Promise.resolve({})- : Promise.reject(new Error('missing')));-- await linkWorktreeDependencies('/repo', '/worktree');-- expect(symlink).toHaveBeenCalledTimes(3);- const linkedPaths = symlink.mock.calls.map(([source, target]) => [- source.replaceAll('\\', '/'), target.replaceAll('\\', '/'),+ existsSync.mockImplementation(path => normalize(path) === '/repo/nested/.git');+ readdir.mockResolvedValue([+ ...directories.map(name => ({ name, isDirectory: () => true })),+ { name: 'linked-package', isDirectory: () => false },]);- expect(linkedPaths).toEqual(expect.arrayContaining([- ['/repo/node_modules', '/worktree/node_modules'],- ['/repo/client/node_modules', '/worktree/client/node_modules'],- ['/repo/server/node_modules', '/worktree/server/node_modules'],- ]));+ stat.mockImplementation(path => normalize(path) === '/repo/uninstalled/node_modules'+ ? missing() : Promise.resolve({ isDirectory: () => true }));+ lstat.mockImplementation(path => {+ const normalized = normalize(path);+ if (directories.some(name => normalized === `/worktree/${name}`)) {+ return Promise.resolve({ isDirectory: () => true });+ }+ if (normalized.endsWith('/package.json') && normalized !== '/worktree/assets/package.json') {+ return Promise.resolve({ isFile: () => true });+ }+ return missing();+ });});- it('skips missing sources and preserves existing targets', async () => {- lstat.mockImplementation((path) => ['/repo/node_modules', '/worktree/node_modules'].includes(path.replaceAll('\\', '/'))- ? Promise.resolve({})- : Promise.reject(new Error('missing')));+ afterEach(() => {+ lstat.mockReset().mockResolvedValue({});+ readdir.mockReset().mockResolvedValue([]);+ stat.mockReset().mockResolvedValue({ isDirectory: () => true });+ existsSync.mockReset().mockReturnValue(true);+ });+ it('links installed immediate packages including admin and skips non-packages, hidden and nested repos', async () => {await linkWorktreeDependencies('/repo', '/worktree');- expect(symlink).not.toHaveBeenCalled();+ expect(symlink.mock.calls.map(([source, target]) => [normalize(source), normalize(target)]))+ .toEqual(expect.arrayContaining(['', '/client', '/server', '/admin'].map(part => [+ `/repo${part}/node_modules`, `/worktree${part}/node_modules`,+ ])));+ expect(symlink).toHaveBeenCalledTimes(4);});- it('does not require the PortOS layout for generic managed repositories', async () => {- lstat.mockRejectedValue(new Error('missing'));+ it('preserves existing real directories and foreign links', async () => {+ const original = lstat.getMockImplementation();+ lstat.mockImplementation(path => normalize(path).endsWith('/node_modules')+ ? Promise.resolve({ isSymbolicLink: () => normalize(path).includes('/admin/') })+ : original(path));- await expect(linkWorktreeDependencies('/generic-repo', '/generic-worktree')).resolves.toBeUndefined();+ await linkWorktreeDependencies('/repo', '/worktree');expect(symlink).not.toHaveBeenCalled();});- it('removes only symlinks that point to the source checkout', async () => {- const sourceDependencyPath = join('/repo', 'node_modules');- const worktreeDependencyPath = join('/worktree', 'node_modules');- lstat.mockImplementation((path) => {- const normalized = path.replaceAll('\\', '/');- if (normalized === '/worktree/node_modules') return Promise.resolve({ isSymbolicLink: () => true });- if (normalized === '/worktree/client/node_modules') return Promise.resolve({ isSymbolicLink: () => false });- return Promise.reject(Object.assign(new Error('missing'), { code: 'ENOENT' }));+ it('removes only owned links including admin, even if source dependencies were removed', async () => {+ const original = lstat.getMockImplementation();+ lstat.mockImplementation(path => {+ const normalized = normalize(path);+ if (normalized.endsWith('/node_modules')) {+ return Promise.resolve({ isSymbolicLink: () => normalized !== '/worktree/client/node_modules' });+ }+ return original(path);});- readlink.mockResolvedValue(sourceDependencyPath);+ stat.mockImplementation(missing);+ readlink.mockImplementation(path => Promise.resolve(normalize(path).includes('/server/')+ ? join('/foreign', 'node_modules')+ : normalize(path).replace('/worktree/', '/repo/')));await unlinkWorktreeDependencies('/repo', '/worktree');- expect(unlink).toHaveBeenCalledTimes(1);- expect(unlink).toHaveBeenCalledWith(worktreeDependencyPath);+ expect(unlink.mock.calls.map(([path]) => normalize(path)).sort()).toEqual([+ '/worktree/admin/node_modules', '/worktree/node_modules', '/worktree/uninstalled/node_modules',+ ]);});});