The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
dsh/install: never delete a checkout by linking it; run bare script names; say how git ended
deecb8ce461a9db
diff --git a/cli/src/dsh/install.spec.ts b/cli/src/dsh/install.spec.tsindex 593909ef9..7a07c85d0 100644--- a/cli/src/dsh/install.spec.ts+++ b/cli/src/dsh/install.spec.ts@@ -1,14 +1,14 @@// `harness dsh install` on real (local) git repositories: a harness that uses a viewer package// installs the viewer too, and narrates the viewer's phases under its own id.-import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { execFileSync } from 'node:child_process'-import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'+import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'import { env } from '../config/env.js'-import { installDsh, type DshInstallProgress } from './install.js'-import { installedDsh, invalidateInstalledDsh, listInstalledDsh, readInstalledIndex } from './installed.js'-import type { DshRegistryEntry } from './registry.js'+import { DOCTOR_TIMEOUT_MS, installDsh, removeDsh, resolveInstallSource, runDshDoctor, type DshInstallProgress } from './install.js'+import { dshInstallDir, installedDsh, invalidateInstalledDsh, listInstalledDsh, readInstalledIndex, type InstalledDsh } from './installed.js'+import { HARNESS_MONOREPO, type DshRegistryEntry } from './registry.js'function gitRepo(dir: string, files: Record<string, string>): string {mkdirSync(dir, { recursive: true })@@ -159,3 +159,338 @@ describe('installDsh from one folder of a repo (the built-in shelf)', () => {}})})++describe('installDsh, every way it can go', () => {+ let root: string+ let savedDshDir: string+ let savedPath: string | undefined+ beforeEach(() => {+ root = mkdtempSync(join(tmpdir(), 'dsh-install-each-'))+ savedDshDir = env.DSH_DIR+ savedPath = process.env.PATH+ env.DSH_DIR = join(root, 'installed')+ invalidateInstalledDsh()+ })+ afterEach(() => {+ vi.useRealTimers()+ process.env.PATH = savedPath+ env.DSH_DIR = savedDshDir+ invalidateInstalledDsh()+ rmSync(root, { recursive: true, force: true })+ })++ const REAL_GIT = execFileSync('/bin/sh', ['-c', 'command -v git'], { encoding: 'utf8' }).trim()+ /** Put a `git` first on PATH that runs `script` (sh), which may hand anything on to the real git. */+ const stubGit = (script: string): void => {+ const bin = join(root, 'bin')+ mkdirSync(bin, { recursive: true })+ writeFileSync(join(bin, 'git'), `#!/bin/sh\nREAL_GIT='${REAL_GIT}'\n${script}\n`, { mode: 0o755 })+ process.env.PATH = `${bin}:${savedPath}`+ }+ const leftovers = (): string[] => (existsSync(env.DSH_DIR) ? readdirSync(env.DSH_DIR).filter((name) => name.startsWith('.tmp-')) : [])+ const thing = (extra: Record<string, unknown> = {}, files: Record<string, string> = {}): string => gitRepo(join(root, 'src', 'thing'), {+ 'harness.json': JSON.stringify({ spec: 1, id: 'acme/thing', name: 'Thing', engine: 'claude', ...extra }),+ ...files,+ })++ describe('resolveInstallSource', () => {+ it('a registry id is its repo, ref and folder; a URL or path is itself; an empty or control-character source is nothing', () => {+ const entries: Record<string, DshRegistryEntry> = {+ 'acme/whole': { id: 'acme/whole', name: 'Whole', repo: 'https://example.com/whole.git', ref: 'v1', engine: 'claude' },+ 'acme/folder': { id: 'acme/folder', name: 'Folder', repo: HARNESS_MONOREPO, ref: 'main', path: 'store/agents/folder', engine: 'claude' },+ }+ const registry = (id: string): DshRegistryEntry | undefined => entries[id]+ expect(resolveInstallSource('acme/whole', registry)).toEqual({ source: 'https://example.com/whole.git', ref: 'v1', id: 'acme/whole' })+ expect(resolveInstallSource('acme/folder', registry)).toEqual({ source: HARNESS_MONOREPO, ref: 'main', path: 'store/agents/folder', id: 'acme/folder' })+ expect(resolveInstallSource('/Users/example/code/thing', registry)).toEqual({ source: '/Users/example/code/thing' })+ expect(resolveInstallSource('', registry)).toBeNull()+ expect(resolveInstallSource('https://example.com/x', registry)).toBeNull()+ })+ })++ describe('--link', () => {+ it('links a checkout in place, records it as linked with its commit, and a second link replaces the first', async () => {+ const repo = thing()+ const frames: DshInstallProgress[] = []+ const result = await installDsh({ source: repo, link: true, onProgress: (p) => frames.push(p) })+ expect(result.ok, JSON.stringify(result)).toBe(true)+ expect(frames[0]).toEqual({ id: null, phase: 'clone', detail: `linking ${repo}` })+ const dir = dshInstallDir('acme/thing')+ expect(lstatSync(dir).isSymbolicLink()).toBe(true)+ const row = readInstalledIndex()[0]+ expect(row).toMatchObject({ id: 'acme/thing', dir, source: repo, ref: null, linked: true })+ expect(row).not.toHaveProperty('path')+ expect(row.commit).toMatch(/^[0-9a-f]{40}$/)+ expect(result.ok && result.installed.realDir).toBe(realpathSync(repo))+ const again = await installDsh({ source: repo, link: true, path: 'ignored/for/a/link' })+ expect(again.ok).toBe(true)+ expect(readInstalledIndex()).toHaveLength(1)+ })++ it('a folder that is not a git checkout links with no commit', async () => {+ const plain = join(root, 'plain')+ mkdirSync(plain)+ writeFileSync(join(plain, 'harness.json'), JSON.stringify({ spec: 1, id: 'acme/plain', name: 'Plain', engine: 'codex' }))+ const result = await installDsh({ source: plain, link: true })+ expect(result.ok).toBe(true)+ expect(readInstalledIndex()[0]?.commit).toBeNull()+ })++ it('a git that answers rev-parse with nothing is no commit, not an empty one', async () => {+ const repo = thing()+ stubGit('if [ "$3" = "rev-parse" ]; then exit 0; fi\nexec "$REAL_GIT" "$@"')+ expect((await installDsh({ source: repo, link: true })).ok).toBe(true)+ expect(readInstalledIndex()[0]?.commit).toBeNull()+ })++ it('refuses a relative path, a path that does not exist, and a folder with no manifest, before touching anything', async () => {+ const frames: DshInstallProgress[] = []+ expect(await installDsh({ source: 'relative/thing', link: true, onProgress: (p) => frames.push(p) }))+ .toEqual({ ok: false, error: 'INVALID_SOURCE', detail: '--link needs an absolute path to a checkout' })+ expect(frames.at(-1)).toEqual({ id: null, phase: 'failed', detail: '--link needs an absolute path to a checkout' })+ expect(await installDsh({ source: join(root, 'nope'), link: true })).toEqual({ ok: false, error: 'SOURCE_NOT_FOUND', detail: `${join(root, 'nope')} does not exist` })+ mkdirSync(join(root, 'empty'))+ expect(await installDsh({ source: join(root, 'empty'), link: true })).toMatchObject({ ok: false, error: 'INVALID_MANIFEST' })+ expect(existsSync(env.DSH_DIR)).toBe(false)+ })++ it('refuses to link the installed copy onto itself, which would delete it first', async () => {+ // Found by this spec: placing the link clears the install directory, so linking a clone that is+ // already installed (or a folder inside it) deleted the only copy and left a link to nothing.+ const repo = thing({}, { 'AGENTS.md': '# mine\n' })+ expect((await installDsh({ source: repo })).ok).toBe(true)+ const installed = dshInstallDir('acme/thing')+ writeFileSync(join(installed, 'notes.md'), 'work in progress\n')+ expect(await installDsh({ source: installed, link: true })).toMatchObject({ ok: false, error: 'INVALID_SOURCE' })+ mkdirSync(join(installed, 'nested'))+ writeFileSync(join(installed, 'nested', 'harness.json'), readFileSync(join(installed, 'harness.json')))+ expect(await installDsh({ source: join(installed, 'nested'), link: true })).toMatchObject({ ok: false, error: 'INVALID_SOURCE' })+ expect(lstatSync(installed).isDirectory()).toBe(true)+ expect(readFileSync(join(installed, 'notes.md'), 'utf8')).toBe('work in progress\n')+ expect(readInstalledIndex()[0]).toMatchObject({ id: 'acme/thing', linked: false })+ })++ it('linking a checkout elsewhere replaces an installed clone of the same id', async () => {+ const repo = thing()+ expect((await installDsh({ source: repo })).ok).toBe(true)+ expect(lstatSync(dshInstallDir('acme/thing')).isDirectory()).toBe(true)+ expect((await installDsh({ source: repo, link: true })).ok).toBe(true)+ expect(lstatSync(dshInstallDir('acme/thing')).isSymbolicLink()).toBe(true)+ expect(readInstalledIndex()[0]).toMatchObject({ id: 'acme/thing', linked: true })+ })+ })++ describe('cloning', () => {+ it('clones a whole repo at a ref, replacing an earlier install of the same id', async () => {+ const repo = thing()+ execFileSync('git', ['-C', repo, 'branch', 'v2'])+ expect((await installDsh({ source: repo })).ok).toBe(true)+ writeFileSync(join(dshInstallDir('acme/thing'), 'stale.txt'), 'from the first install')+ const result = await installDsh({ source: repo, ref: 'v2' })+ expect(result.ok, JSON.stringify(result)).toBe(true)+ expect(existsSync(join(dshInstallDir('acme/thing'), 'stale.txt'))).toBe(false)+ expect(readInstalledIndex()[0]).toMatchObject({ ref: 'v2', linked: false })+ expect(leftovers()).toEqual([])+ })++ it('a repo that cannot be cloned, or has no manifest, fails with what git or the manifest said', async () => {+ const missing = await installDsh({ source: join(root, 'no-such-repo') })+ expect(missing).toMatchObject({ ok: false, error: 'CLONE_FAILED' })+ expect(!missing.ok && missing.detail).toMatch(/^git clone exited 128: .*no-such-repo/)+ const bare = gitRepo(join(root, 'src', 'bare'), { 'README.md': '# no manifest\n' })+ const noManifest = await installDsh({ source: bare })+ expect(noManifest).toMatchObject({ ok: false, error: 'INVALID_MANIFEST' })+ expect(!noManifest.ok && noManifest.detail).toMatch(/^no harness\.json in /)+ const sparse = await installDsh({ source: join(root, 'no-such-repo'), ref: 'main', path: 'store/agents/x' })+ expect(sparse).toMatchObject({ ok: false, error: 'CLONE_FAILED' })+ expect(leftovers()).toEqual([])+ })++ it('a folder the repo lacks at a ref is said with the ref; a path that is a file fails at the sparse checkout', async () => {+ const repo = gitRepo(join(root, 'src', 'mono'), { 'store/agents/file': 'not a folder\n' })+ expect(await installDsh({ source: repo, ref: 'main', path: 'store/agents/nope' }))+ .toEqual({ ok: false, error: 'CLONE_FAILED', detail: `${repo} at main has no folder store/agents/nope` })+ const file = await installDsh({ source: repo, path: 'store/agents/file' })+ expect(file).toMatchObject({ ok: false, error: 'CLONE_FAILED' })+ expect(!file.ok && file.detail).toMatch(/^git sparse-checkout exited 128: .*not a directory/)+ expect(leftovers()).toEqual([])+ })++ it('streams git\'s lines as they land, and reports the last three that are not progress', async () => {+ stubGit([+ `printf 'Cloning into x...\\n\\n' >&2`,+ `i=0; while [ $i -lt 25 ]; do printf 'remote: Counting objects: %s\\r' $i >&2; i=$((i+1)); done`,+ `printf '\\nReceiving objects: 100%%\\r\\n' >&2`,+ `printf 'fatal: first\\nfatal: second\\nfatal: third\\nfatal: no newline' >&2`,+ 'exit 128',+ ].join('\n'))+ const lines: string[] = []+ const result = await installDsh({ source: 'https://example.com/thing.git', onLine: (line) => lines.push(line) })+ expect(result).toEqual({ ok: false, error: 'CLONE_FAILED', detail: 'git clone exited 128: fatal: second · fatal: third · fatal: no newline' })+ expect(lines[0]).toBe('Cloning into x...')+ expect(lines.filter((line) => line.startsWith('remote: Counting objects'))).toHaveLength(25)+ expect(lines.slice(-4)).toEqual(['fatal: first', 'fatal: second', 'fatal: third', 'fatal: no newline'])+ expect(lines).not.toContain('')+ })++ it('when git said nothing but progress, the last progress line is the detail', async () => {+ stubGit(`printf 'Receiving objects: 10%%\\rReceiving objects: 20%%\\r' >&2\nexit 1`)+ expect(await installDsh({ source: 'https://example.com/thing.git' })).toEqual({ ok: false, error: 'CLONE_FAILED', detail: 'git clone exited 1: Receiving objects: 20%' })+ })++ it('no git at all is a failed clone that says so', async () => {+ const empty = join(root, 'empty-bin')+ mkdirSync(empty)+ process.env.PATH = empty+ const result = await installDsh({ source: 'https://example.com/thing.git' })+ expect(result).toMatchObject({ ok: false, error: 'CLONE_FAILED' })+ expect(!result.ok && result.detail).toContain('ENOENT')+ })++ it('a clone still running after ten minutes is stopped, and said to have been', async () => {+ stubGit('echo "Cloning into x..." >&2\nexec sleep 30')+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })+ let said!: () => void+ const saidSomething = new Promise<void>((resolve) => { said = resolve })+ const pending = installDsh({ source: 'https://example.com/thing.git', onLine: () => said() })+ await saidSomething+ vi.advanceTimersByTime(10 * 60_000)+ vi.useRealTimers()+ expect(await pending).toEqual({ ok: false, error: 'CLONE_FAILED', detail: 'git clone was still running after 10 min: Cloning into x...' })+ })++ it('a git that fails without a word is reported by how it ended alone, a code or a signal', async () => {+ stubGit('exit 2')+ expect(await installDsh({ source: 'https://example.com/thing.git' })).toEqual({ ok: false, error: 'CLONE_FAILED', detail: 'git clone exited 2' })+ stubGit('kill -KILL $$')+ expect(await installDsh({ source: 'https://example.com/thing.git' })).toEqual({ ok: false, error: 'CLONE_FAILED', detail: 'git clone exited SIGKILL' })+ })+ })++ describe('setup, the used viewer, and the doctor', () => {+ it('a bare script name is the harness\'s own script, as `harness dsh check` reads it, not a PATH lookup', async () => {+ // Found by this spec: `"setup": "setup.sh"` (the shape spec/README.md shows for a viewer package)+ // passed the check, which looks for the file, and then failed install with "command not found".+ const repo = thing({ toolchain: { setup: 'setup.sh', doctor: 'doctor.sh' } }, {+ 'setup.sh': '#!/bin/sh\ntouch set-up\n',+ 'doctor.sh': '#!/bin/sh\necho "ok bare doctor ran"\n',+ })+ const result = await installDsh({ source: repo })+ expect(result.ok, JSON.stringify(result)).toBe(true)+ expect(result.ok && result.doctor.lines).toContain('ok bare doctor ran')+ expect(existsSync(join(dshInstallDir('acme/thing'), 'set-up'))).toBe(true)+ })++ it('a setup that fails says how, with its last five lines', async () => {+ const repo = thing({ toolchain: { setup: 'setup.sh' } }, { 'setup.sh': '#!/bin/sh\nfor l in l1 l2 l3 l4 l5 l6; do echo $l; done\nexit 2\n' })+ const frames: DshInstallProgress[] = []+ const result = await installDsh({ source: repo, onProgress: (p) => frames.push(p) })+ expect(result).toMatchObject({ ok: false, error: 'SETUP_FAILED' })+ expect(!result.ok && result.detail).toMatch(/^setup exited 2 · (.* · )?l2 · l3 · l4 · l5 · l6$/)+ expect(frames.map((f) => f.phase)).toEqual(['clone', 'setup', 'failed'])+ expect(readInstalledIndex()).toEqual([])+ })++ it('a setup killed by a signal, or past its timeout, says that instead', async () => {+ const killed = await installDsh({ source: thing({ toolchain: { setup: 'kill -KILL $$' } }) })+ expect(!killed.ok && killed.detail).toMatch(/^setup exited SIGKILL · /)+ rmSync(join(root, 'src'), { recursive: true, force: true })+ const slow = await installDsh({ source: thing({ toolchain: { setup: 'sleep 30' } }), setupTimeoutMs: 300 })+ expect(slow).toEqual({ ok: false, error: 'SETUP_FAILED', detail: 'setup timed out' })+ })++ it('a used viewer that fails to install fails the harness, said once and under the harness', async () => {+ const repo = thing({ viewer: { use: 'acme/viewer' } })+ const registry = (id: string): DshRegistryEntry | undefined => (id === 'acme/viewer' ? { id, kind: 'viewer', name: 'Viewer', repo: join(root, 'no-viewer-repo') } : undefined)+ const frames: DshInstallProgress[] = []+ const lines: string[] = []+ const result = await installDsh({ source: repo, registry, onProgress: (p) => frames.push(p), onLine: (line) => lines.push(line) })+ expect(result).toMatchObject({ ok: false, error: 'CLONE_FAILED' })+ expect(!result.ok && result.detail).toMatch(/^viewer acme\/viewer · git clone exited 128: /)+ expect(frames.filter((f) => f.phase === 'failed')).toEqual([{ id: 'acme/thing', phase: 'failed', detail: !result.ok && result.detail }])+ expect(frames.every((f) => f.id === null || f.id === 'acme/thing')).toBe(true)+ expect(lines).toContain('viewer acme/viewer · installing')+ expect(readInstalledIndex()).toEqual([])+ })++ it('a used viewer the bundled registry does not know is said, and the harness still installs', async () => {+ const lines: string[] = []+ const result = await installDsh({ source: thing({ viewer: { use: 'acme/nowhere-to-be-found' } }), onLine: (line) => lines.push(line) })+ expect(result.ok).toBe(true)+ expect(lines).toContain('miss viewer acme/nowhere-to-be-found is not installed and not in the registry · install it first')+ })++ it('a doctor that fails still records the install, and names what is missing', async () => {+ const repo = thing({ toolchain: { doctor: 'doctor.sh' } }, { 'doctor.sh': '#!/bin/sh\necho "ok git"\necho "miss typst on PATH"\necho "miss fonts"\nexit 1\n' })+ const frames: DshInstallProgress[] = []+ const result = await installDsh({ source: repo, onProgress: (p) => frames.push(p) })+ expect(result).toEqual({ ok: false, error: 'DOCTOR_FAILED', detail: 'doctor failed · miss typst on PATH · miss fonts' })+ expect(frames.at(-1)).toEqual({ id: 'acme/thing', phase: 'failed', detail: 'doctor failed · miss typst on PATH · miss fonts' })+ expect(readInstalledIndex().map((row) => row.id)).toEqual(['acme/thing'])+ })++ it('a doctor that fails without a miss line is reported by its last three lines', async () => {+ const repo = thing({ toolchain: { doctor: 'doctor.sh' } }, { 'doctor.sh': '#!/bin/sh\nfor l in a b c d; do echo "$l"; done\nexit 3\n' })+ const result = await installDsh({ source: repo })+ expect(!result.ok && result.detail).toMatch(/^doctor failed · .*b · c · d$/)+ })++ it('runDshDoctor: no doctor is ready; one still running after five minutes is stopped and said to be', async () => {+ const base = { id: 'acme/thing', dir: root, realDir: root, source: root, ref: null, commit: null, linked: true, installedAt: 0 }+ const none: InstalledDsh = { ...base, manifest: { spec: 1, id: 'acme/thing', name: 'Thing', engine: 'claude' } }+ expect(await runDshDoctor(none)).toEqual({ ok: true, lines: [] })+ const slow: InstalledDsh = { ...base, manifest: { ...none.manifest, toolchain: { doctor: 'echo "ok started"; sleep 30' } } }+ const said = 'miss doctor still running after 5 min — stopped; run `harness dsh doctor acme/thing` again'+ const seen: string[] = []+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })+ const pending = runDshDoctor(slow, (line) => seen.push(line))+ vi.advanceTimersByTime(DOCTOR_TIMEOUT_MS)+ vi.useRealTimers()+ const result = await pending+ expect(result.ok).toBe(false)+ expect(result.lines.at(-1)).toBe(said)+ expect(seen.at(-1)).toBe(said)+ // and with no one listening for lines+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })+ const quiet = runDshDoctor(slow)+ vi.advanceTimersByTime(DOCTOR_TIMEOUT_MS)+ vi.useRealTimers()+ expect((await quiet).lines.at(-1)).toBe(said)+ })+ })++ describe('removeDsh', () => {+ it('removes a clone and its row; a link, never the checkout it points at; a row whose folder is already gone', async () => {+ expect(removeDsh('acme/thing')).toEqual({ ok: false, error: 'NOT_INSTALLED', detail: 'acme/thing is not installed' })+ const repo = thing()+ await installDsh({ source: repo })+ expect(removeDsh('acme/thing')).toEqual({ ok: true })+ expect(existsSync(dshInstallDir('acme/thing'))).toBe(false)+ expect(readInstalledIndex()).toEqual([])++ await installDsh({ source: repo, link: true })+ expect(removeDsh('acme/thing')).toEqual({ ok: true })+ expect(existsSync(dshInstallDir('acme/thing'))).toBe(false)+ expect(existsSync(join(repo, 'harness.json'))).toBe(true)++ await installDsh({ source: repo })+ rmSync(dshInstallDir('acme/thing'), { recursive: true })+ expect(removeDsh('acme/thing')).toEqual({ ok: true })+ expect(readInstalledIndex()).toEqual([])+ })++ it('a folder that cannot be removed is a failure that keeps the row', async () => {+ await installDsh({ source: thing() })+ const owner = join(env.DSH_DIR, 'acme')+ chmodSync(owner, 0o500)+ try {+ const result = removeDsh('acme/thing')+ expect(result).toMatchObject({ ok: false, error: 'REMOVE_FAILED' })+ expect(!result.ok && result.detail).toContain('EACCES')+ expect(readInstalledIndex().map((row) => row.id)).toEqual(['acme/thing'])+ } finally {+ chmodSync(owner, 0o700)+ }+ })+ })+})diff --git a/cli/src/dsh/install.ts b/cli/src/dsh/install.tsindex 1b0e105e1..43573908c 100644--- a/cli/src/dsh/install.ts+++ b/cli/src/dsh/install.ts@@ -11,14 +11,15 @@import { execFile, spawn } from 'node:child_process'import { existsSync, lstatSync, mkdirSync, realpathSync, renameSync, rmSync, symlinkSync } from 'node:fs'import { randomUUID } from 'node:crypto'-import { dirname, isAbsolute, join, resolve } from 'node:path'+import { dirname, isAbsolute, join, resolve, sep } from 'node:path'import { promisify } from 'node:util'import {- dshInstallDir, dshRootDir, installedDsh, readInstalledIndex, removeInstalledRecord, resolveInstalled,- upsertInstalledRecord, isBrokenDsh, type InstalledDsh, type InstalledDshRecord,+ dshInstallDir, dshRootDir, installedDsh, readInstalledIndex, removeInstalledRecord, upsertInstalledRecord,+ type InstalledDsh, type InstalledDshRecord,} from './installed.js'import { readDshManifest, viewerUse, type DshManifest } from './manifest.js'import { PACKAGE_PATH_RE, registryEntry, type DshRegistryEntry } from './registry.js'+import { resolveDshCommand } from './materialize.js'import { runDshCommand } from './shell.js'const execFileAsync = promisify(execFile)@@ -67,8 +68,11 @@ export type DshInstallResult =| { ok: false; error: string; detail: string }/** A registry id (`autonomous/typst`) resolves to its repo, ref and folder; anything else is a source. */-export function resolveInstallSource(idOrSource: string): { source: string; ref?: string; path?: string; id?: string } | null {- const entry = registryEntry(idOrSource)+export function resolveInstallSource(+ idOrSource: string,+ registry: (id: string) => DshRegistryEntry | undefined = registryEntry,+): { source: string; ref?: string; path?: string; id?: string } | null {+ const entry = registry(idOrSource)if (entry) return { source: entry.repo, ref: entry.ref, ...(entry.path ? { path: entry.path } : {}), id: entry.id }if (!idOrSource || /[\x00-\x1f\x7f]/.test(idOrSource)) return nullreturn { source: idOrSource }@@ -93,6 +97,17 @@ function linkInstall(source: string): { ok: true; realDir: string; manifest: Dsh}const manifest = readDshManifest(realDir)if (!manifest.ok) return { ok: false, error: 'INVALID_MANIFEST', detail: manifest.error }+ // Placing the link first clears the install directory. When the checkout IS that directory (a clone+ // installed earlier), or lives inside it, that would delete the only copy before linking to it.+ const installDir = dshInstallDir(manifest.manifest.id)+ let placed: ReturnType<typeof lstatSync> | null = null+ try { placed = lstatSync(installDir) } catch { placed = null }+ if (placed && !placed.isSymbolicLink()) {+ const installed = realpathSync(installDir)+ if (realDir === installed || realDir.startsWith(installed + sep)) {+ return { ok: false, error: 'INVALID_SOURCE', detail: `${source} is inside the installed copy of ${manifest.manifest.id}; link a checkout that lives elsewhere` }+ }+ }return { ok: true, realDir, manifest: manifest.manifest }}@@ -145,13 +160,17 @@ async function cloneInstall(return { ok: true, tmpDir, manifest: manifest.manifest, commit }}+/** How long one git command may take: a clone of a large repository over a slow link, not a hang. */+const GIT_TIMEOUT_MS = 10 * 60_000+/** Run git, handing each stderr line (and each carriage-return progress segment) to `onLine` as it lands. */function streamGit(args: string[], onLine: ((line: string) => void) | undefined): Promise<{ ok: true } | { ok: false; detail: string }> {return new Promise((resolve) => {const child = spawn('git', args, { stdio: ['ignore', 'ignore', 'pipe'] })const tail: string[] = []let rest = ''- const timer = setTimeout(() => child.kill('SIGTERM'), 10 * 60_000)+ let timedOut = false+ const timer = setTimeout(() => { timedOut = true; child.kill('SIGTERM') }, GIT_TIMEOUT_MS)child.stderr?.on('data', (chunk: Buffer) => {rest += chunk.toString('utf8')let at: number@@ -165,11 +184,14 @@ function streamGit(args: string[], onLine: ((line: string) => void) | undefined)}})child.on('error', (error) => { clearTimeout(timer); resolve({ ok: false, detail: error.message }) })- child.on('exit', (code) => {+ child.on('exit', (code, signal) => {clearTimeout(timer)if (rest.trim()) { onLine?.(rest.trim()); tail.push(rest.trim()) }- if (code === 0) resolve({ ok: true })- else resolve({ ok: false, detail: `git ${args[0] === '-C' ? args[2] : args[0]} exited ${code}: ${tail.filter((l) => !/^(Receiving|Resolving|Updating|remote:)/.test(l)).slice(-3).join(' · ') || tail.slice(-1).join('')}` })+ if (code === 0) { resolve({ ok: true }); return }+ const what = `git ${args[0] === '-C' ? args[2] : args[0]}`+ const said = tail.filter((l) => !/^(Receiving|Resolving|Updating|remote:)/.test(l)).slice(-3).join(' · ') || tail.slice(-1).join('')+ const how = timedOut ? `${what} was still running after ${GIT_TIMEOUT_MS / 60_000} min` : `${what} exited ${code ?? signal}`+ resolve({ ok: false, detail: said ? `${how}: ${said}` : how })})})}@@ -198,7 +220,8 @@ export const DOCTOR_TIMEOUT_MS = 5 * 60_000export async function runDshDoctor(installed: InstalledDsh, onLine?: (line: string) => void): Promise<DshDoctorResult> {const doctor = installed.manifest.toolchain?.doctorif (!doctor) return { ok: true, lines: [] }- const result = await runDshCommand(doctor, {+ // Resolved inside the harness, as check.ts reads it: a bare `doctor.sh` is that file, not a PATH lookup.+ const result = await runDshCommand(resolveDshCommand(installed, doctor), {cwd: installed.realDir,env: { HARNESS_DSH: installed.id, HARNESS_DSH_DIR: installed.realDir },onLine,@@ -248,16 +271,13 @@ export async function installDsh(opts: DshInstallOptions): Promise<DshInstallReslinked: opts.link === true,installedAt: Date.now(),}- const resolved = resolveInstalled(record)- if (isBrokenDsh(resolved)) {- progress({ id: manifest.id, phase: 'failed', detail: resolved.error })- return { ok: false, error: 'INVALID_MANIFEST', detail: resolved.error }- }+ // Exactly what was just read and placed; resolving the record from disk again would read it twice.+ const resolved: InstalledDsh = { ...record, manifest, realDir }const setupLines: string[] = []if (manifest.toolchain?.setup) {progress({ id: manifest.id, phase: 'setup', detail: manifest.toolchain.setup })- const setup = await runDshCommand(manifest.toolchain.setup, {+ const setup = await runDshCommand(resolveDshCommand({ realDir }, manifest.toolchain.setup), {cwd: realDir,env: { HARNESS_DSH: manifest.id, HARNESS_DSH_DIR: realDir },onLine: (line) => { setupLines.push(line); opts.onLine?.(line) },@@ -327,7 +347,8 @@ export function removeDsh(id: string): { ok: true } | { ok: false; error: stringif (isLink) rmSync(record.dir, { force: true })else if (existsSync(record.dir)) rmSync(record.dir, { recursive: true, force: true })} catch (error) {- return { ok: false, error: 'REMOVE_FAILED', detail: error instanceof Error ? error.message : String(error) }+ // rmSync throws only system errors (EACCES, EBUSY).+ return { ok: false, error: 'REMOVE_FAILED', detail: (error as Error).message }}removeInstalledRecord(id)return { ok: true }diff --git a/cli/src/dsh/viewer.spec.ts b/cli/src/dsh/viewer.spec.tsindex caab01890..f70a163f3 100644--- a/cli/src/dsh/viewer.spec.ts+++ b/cli/src/dsh/viewer.spec.ts@@ -312,6 +312,16 @@ describe('DshViewerManager, the rest of a viewer\'s life', () => {return { spawned, urls, logs, manager }}+ it('runs a bare script name as the package\'s own file, the way `harness dsh check` reads it', async () => {+ writeFileSync(join(root, 'viewer.sh'), '#!/bin/sh\n', { mode: 0o755 })+ const scripts: string[] = []+ const { manager } = setup({ spawn: ((script: string) => { scripts.push(script); return fakeChild() }) as unknown as typeof import('./shell.js').spawnDshCommand })+ const own = (command: string): InstalledDsh => ({ ...dsh({ command, url: 'http://127.0.0.1:${port}/' }), dir: root, realDir: root })+ await manager.start('a1', own('viewer.sh'), '/ws')+ await manager.start('a2', own('node viewer.mjs --port $HARNESS_VIEWER_PORT'), '/ws')+ expect(scripts).toEqual([`'${join(root, 'viewer.sh')}'`, 'node viewer.mjs --port $HARNESS_VIEWER_PORT'])+ })+it('does nothing for a harness with no viewer', async () => {const { spawned, logs, manager } = setup()await manager.start('a1', { ...dsh({ command: 'v', url: 'u' }), manifest: { spec: 1, id: 'acme/thing', name: 'Thing', engine: 'claude' } }, '/ws')diff --git a/cli/src/dsh/viewer.ts b/cli/src/dsh/viewer.tsindex 3773b6b84..0dd28f1a5 100644--- a/cli/src/dsh/viewer.ts+++ b/cli/src/dsh/viewer.ts@@ -18,6 +18,7 @@ import { relative } from 'node:path'import { isCandidateArtifact, isArtifactDirIgnored, newestArtifact } from './artifacts.js'import { installedDsh, type InstalledDsh } from './installed.js'import { isViewerPackage } from './manifest.js'+import { resolveDshCommand } from './materialize.js'import { isShellNoise, killProcessGroup, spawnDshCommand } from './shell.js'/** The viewer that will actually run for a harness: its own, or the package it points at. */@@ -244,7 +245,8 @@ export class DshViewerManager {// A used viewer runs in ITS directory with ITS files, and is told which harness it draws for:// HARNESS_DSH/_DIR stay the harness's (the contract every DSH script relies on), HARNESS_VIEWER// and HARNESS_VIEWER_DIR name the package. A harness's own viewer sees both pairs agree.- const child = (this.deps.spawn ?? spawnDshCommand)(viewer.command, {+ // Resolved inside the viewer's directory, as check.ts reads it: a bare `viewer.sh` is that file.+ const child = (this.deps.spawn ?? spawnDshCommand)(resolveDshCommand({ realDir: viewer.dir }, viewer.command), {cwd: viewer.dir,env: {HARNESS_DSH: state.dsh.id,