Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(windows): add linkSharedEntry for sharing assets without the symlink privilege
reviewa0cff223f416
diff --git a/src/core/shared-entry-link.test.ts b/src/core/shared-entry-link.test.tsnew file mode 100644index 000000000..00ac89a8f--- /dev/null+++ b/src/core/shared-entry-link.test.ts@@ -0,0 +1,135 @@+// linkSharedEntry: the POSIX leg is a plain symlink exactly as before; the Windows leg (deps+// injected, so it runs here on Linux) never lets one unprivileged link block an account add.+import { describe, expect, it, vi } from 'vitest'+import { lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'+import { constants } from 'fs'+import os from 'os'+import path from 'path'+import { linkSharedEntry, type SharedEntryLinkDeps } from './shared-entry-link'++const err = (code: string): NodeJS.ErrnoException => Object.assign(new Error(code), { code })+const stats = (dir: boolean) => ({ isDirectory: () => dir, isSymbolicLink: () => false })++function fakeDeps(over: Partial<SharedEntryLinkDeps> & { dir?: boolean } = {}): SharedEntryLinkDeps {+ return {+ platform: 'win32',+ lstat: vi.fn(async () => stats(!!over.dir)),+ stat: vi.fn(async () => stats(!!over.dir)),+ symlink: vi.fn(async () => {}),+ link: vi.fn(async () => {}),+ copyFile: vi.fn(async () => {}),+ warn: vi.fn(),+ ...over+ }+}++describe('linkSharedEntry — win32', () => {+ it('a directory is a junction (no privilege needed)', async () => {+ const deps = fakeDeps({ dir: true })+ expect(await linkSharedEntry('C:\\s\\skills', 'C:\\t\\skills', deps)).toBe('junction')+ expect(deps.symlink).toHaveBeenCalledWith('C:\\s\\skills', 'C:\\t\\skills', 'junction')+ })++ it('a file whose symlink is refused (EPERM) becomes a hardlink', async () => {+ const deps = fakeDeps({ symlink: vi.fn(async () => Promise.reject(err('EPERM'))) })+ expect(await linkSharedEntry('s', 't', deps)).toBe('hardlink')+ expect(deps.symlink).toHaveBeenCalledWith('s', 't', 'file')+ expect(deps.link).toHaveBeenCalledWith('s', 't')+ expect(deps.copyFile).not.toHaveBeenCalled()+ expect(deps.warn).not.toHaveBeenCalled()+ })++ it('EPERM symlink + EXDEV hardlink ⇒ an exclusive copy, with a drift warning', async () => {+ const deps = fakeDeps({+ symlink: vi.fn(async () => Promise.reject(err('EPERM'))),+ link: vi.fn(async () => Promise.reject(err('EXDEV')))+ })+ expect(await linkSharedEntry('s', 't', deps)).toBe('copy')+ expect(deps.copyFile).toHaveBeenCalledWith('s', 't', constants.COPYFILE_EXCL)+ expect(deps.warn).toHaveBeenCalledWith(expect.stringContaining('COPY'))+ })++ it('an unexpected error is logged and skipped, never thrown', async () => {+ const deps = fakeDeps({ symlink: vi.fn(async () => Promise.reject(err('EIO'))) })+ await expect(linkSharedEntry('s', 't', deps)).resolves.toBe('skipped')+ expect(deps.warn).toHaveBeenCalledTimes(1)+ const copyFails = fakeDeps({+ symlink: vi.fn(async () => Promise.reject(err('EPERM'))),+ link: vi.fn(async () => Promise.reject(err('EXDEV'))),+ copyFile: vi.fn(async () => Promise.reject(err('EBUSY')))+ })+ await expect(linkSharedEntry('s', 't', copyFails)).resolves.toBe('skipped')+ })++ it('a missing source makes no link call at all', async () => {+ const deps = fakeDeps({ lstat: vi.fn(async () => Promise.reject(err('ENOENT'))) })+ expect(await linkSharedEntry('s', 't', deps)).toBe('skipped')+ expect(deps.symlink).not.toHaveBeenCalled()+ expect(deps.warn).not.toHaveBeenCalled()+ })++ it('an existing target is a silent skip (no hardlink/copy over it)', async () => {+ const deps = fakeDeps({ symlink: vi.fn(async () => Promise.reject(err('EEXIST'))) })+ expect(await linkSharedEntry('s', 't', deps)).toBe('skipped')+ expect(deps.link).not.toHaveBeenCalled()+ expect(deps.warn).not.toHaveBeenCalled()+ const viaLink = fakeDeps({+ symlink: vi.fn(async () => Promise.reject(err('EPERM'))),+ link: vi.fn(async () => Promise.reject(err('EEXIST')))+ })+ expect(await linkSharedEntry('s', 't', viaLink)).toBe('skipped')+ expect(viaLink.copyFile).not.toHaveBeenCalled()+ })+})++describe('linkSharedEntry — POSIX (unchanged behavior)', () => {+ it('is an untyped symlink and still throws an unexpected error', async () => {+ for (const p of ['linux', 'darwin'] as const) {+ const deps = fakeDeps({ platform: p })+ expect(await linkSharedEntry('/s/config.toml', '/t/config.toml', deps)).toBe('symlink')+ expect(deps.symlink).toHaveBeenCalledWith('/s/config.toml', '/t/config.toml')+ expect(deps.stat).not.toHaveBeenCalled()+ const failing = fakeDeps({ platform: p, symlink: vi.fn(async () => Promise.reject(err('EPERM'))) })+ await expect(linkSharedEntry('/s', '/t', failing)).rejects.toThrow('EPERM')+ expect(failing.link).not.toHaveBeenCalled()+ }+ })++ it.skipIf(process.platform === 'win32')('real fs: files and directories resolve through the link', async () => {+ const root = mkdtempSync(path.join(os.tmpdir(), 'nt-shared-entry-'))+ try {+ mkdirSync(path.join(root, 'src', 'skills'), { recursive: true })+ writeFileSync(path.join(root, 'src', 'config.toml'), 'model = "x"\n')+ mkdirSync(path.join(root, 'dst'))+ await linkSharedEntry(path.join(root, 'src', 'config.toml'), path.join(root, 'dst', 'config.toml'))+ await linkSharedEntry(path.join(root, 'src', 'skills'), path.join(root, 'dst', 'skills'))+ expect(await linkSharedEntry(path.join(root, 'src', 'absent'), path.join(root, 'dst', 'absent'))).toBe(+ 'skipped'+ )+ expect(lstatSync(path.join(root, 'dst', 'skills')).isSymbolicLink()).toBe(true)+ expect(readFileSync(path.join(root, 'dst', 'config.toml'), 'utf8')).toBe('model = "x"\n')+ } finally {+ rmSync(root, { recursive: true, force: true })+ }+ })+})++describe('linkSharedEntry — real Windows', () => {+ it.skipIf(process.platform !== 'win32')('links a dir and a file without the symlink privilege', async () => {+ const root = mkdtempSync(path.join(os.tmpdir(), 'nt-shared-entry-'))+ try {+ mkdirSync(path.join(root, 'src', 'skills'), { recursive: true })+ writeFileSync(path.join(root, 'src', 'config.toml'), 'a')+ mkdirSync(path.join(root, 'dst'))+ expect(await linkSharedEntry(path.join(root, 'src', 'skills'), path.join(root, 'dst', 'skills'))).toBe(+ 'junction'+ )+ expect(+ await linkSharedEntry(path.join(root, 'src', 'config.toml'), path.join(root, 'dst', 'config.toml'))+ ).not.toBe('skipped')+ expect(readFileSync(path.join(root, 'dst', 'config.toml'), 'utf8')).toBe('a')+ } finally {+ rmSync(root, { recursive: true, force: true })+ }+ })+})diff --git a/src/core/shared-entry-link.ts b/src/core/shared-entry-link.tsnew file mode 100644index 000000000..8771fe20b--- /dev/null+++ b/src/core/shared-entry-link.ts@@ -0,0 +1,94 @@+// Link one shared, NON-secret runtime asset (config.toml, skills/, …) from a system agent home into+// a managed account home. Electron-free, deps injected so the Windows leg is testable on Linux.+//+// POSIX: a plain `symlink(source, target)` — byte-identical to the inline loop this replaced,+// including that an unexpected error still throws.+//+// Windows: an untyped / file `symlink` needs SeCreateSymbolicLinkPrivilege, which a stock account+// lacks (EPERM) — that used to reject the whole account add. So:+// - a directory is a JUNCTION (no privilege; same precedent as claude-skill-share.ts);+// - a file tries a symlink, then an NTFS hardlink (no privilege; EXDEV across volumes), then a+// copy — which is a SNAPSHOT that will not follow later edits of the original, so it warns. A+// hardlink can drift too: an editor's atomic-rename save of config.toml replaces the original+// inode and leaves the managed home holding the old one;+// - any other failure for one entry is logged and SKIPPED, never thrown — these are convenience+// assets, and a missing one must not block creating the account.+// In every dialect a missing source (ENOENT) and an existing target (EEXIST) are a silent skip.+import { constants, promises as fsp } from 'fs'++export type SharedEntryLinkDeps = {+ platform: NodeJS.Platform+ lstat: (p: string) => Promise<{ isDirectory(): boolean; isSymbolicLink(): boolean }>+ stat: (p: string) => Promise<{ isDirectory(): boolean }>+ symlink: (source: string, target: string, type?: 'dir' | 'file' | 'junction') => Promise<void>+ link: (source: string, target: string) => Promise<void>+ copyFile: (source: string, target: string, mode: number) => Promise<void>+ warn: (message: string) => void+}++export const defaultSharedEntryLinkDeps = (): SharedEntryLinkDeps => ({+ platform: process.platform,+ lstat: (p) => fsp.lstat(p),+ stat: (p) => fsp.stat(p),+ symlink: (source, target, type) => fsp.symlink(source, target, type),+ link: (source, target) => fsp.link(source, target),+ copyFile: (source, target, mode) => fsp.copyFile(source, target, mode),+ warn: (message) => console.warn(message)+})++const codeOf = (error: unknown): string | undefined => (error as NodeJS.ErrnoException)?.code++/** How the entry ended up in the managed home (`skipped` = absent source, existing target, or a+ * logged Windows failure). Returned for tests and logs; callers need not inspect it. */+export type SharedEntryLinkResult = 'symlink' | 'junction' | 'hardlink' | 'copy' | 'skipped'++export async function linkSharedEntry(+ source: string,+ target: string,+ deps: SharedEntryLinkDeps = defaultSharedEntryLinkDeps()+): Promise<SharedEntryLinkResult> {+ if (deps.platform !== 'win32') {+ try {+ await deps.lstat(source)+ await deps.symlink(source, target)+ return 'symlink'+ } catch (error) {+ const code = codeOf(error)+ if (code !== 'ENOENT' && code !== 'EEXIST') throw error+ return 'skipped'+ }+ }+ try {+ await deps.lstat(source)+ // Follow a source that is itself a link: a junction to a symlinked directory is still a dir.+ const isDir = (await deps.stat(source)).isDirectory()+ if (isDir) {+ await deps.symlink(source, target, 'junction')+ return 'junction'+ }+ try {+ await deps.symlink(source, target, 'file')+ return 'symlink'+ } catch (error) {+ const code = codeOf(error)+ if (code !== 'EPERM' && code !== 'EACCES') throw error+ }+ try {+ await deps.link(source, target)+ return 'hardlink'+ } catch (error) {+ if (codeOf(error) === 'EEXIST') throw error+ }+ await deps.copyFile(source, target, constants.COPYFILE_EXCL)+ deps.warn(+ `[accounts] ${target} is a COPY of ${source} (no symlink privilege, hardlink impossible): ` ++ 'later edits of the original will not reach this account'+ )+ return 'copy'+ } catch (error) {+ const code = codeOf(error)+ if (code === 'ENOENT' || code === 'EEXIST') return 'skipped'+ deps.warn(`[accounts] could not share ${source} into ${target}: ${String(error)}`)+ return 'skipped'+ }+}