Skip to content

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.

Resolve a DSH's init script against its install directory

deea48fe3deeb20
diff --git a/cli/src/dsh/materialize.spec.ts b/cli/src/dsh/materialize.spec.ts
index 943f8bce4..ee3afece6 100644
--- a/cli/src/dsh/materialize.spec.ts
+++ b/cli/src/dsh/materialize.spec.ts
@@ -5,7 +5,7 @@ import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { InstalledDsh } from './installed.js'
import { readDshManifest } from './manifest.js'
-import { dshMarkerLine, materializeWorkspace, skillDirsIn } from './materialize.js'
+import { dshMarkerLine, materializeWorkspace, resolveDshCommand, skillDirsIn } from './materialize.js'
const STARTER = realpathSync(fileURLToPath(new URL('../../../dsh/starter-dsh', import.meta.url)))
@@ -44,6 +44,16 @@ describe('materializeWorkspace', () => {
expect(existsSync(join(link, 'SKILL.md'))).toBe(true)
expect(existsSync(join(workspace, '.harness'))).toBe(true)
expect(result.created).toContain('AGENTS.md')
+ // The init ran in the workspace, found by its path inside the harness, with the contract's env.
+ expect(readFileSync(join(workspace, '.harness-initialized'), 'utf8')).toBe('initialized by autonomous/starter\n')
+ expect(result.warnings).toEqual([])
+ })
+
+ it('runs the init only once: a marked workspace is not re-initialized', async () => {
+ await materializeWorkspace(starter(), workspace)
+ rmSync(join(workspace, '.harness-initialized'))
+ await materializeWorkspace(starter(), workspace)
+ expect(existsSync(join(workspace, '.harness-initialized'))).toBe(false)
})
it('is idempotent: a second run keeps everything and appends nothing', async () => {
@@ -94,3 +104,11 @@ describe('skillDirsIn', () => {
expect(skillDirsIn('/nonexistent')).toEqual([])
})
})
+
+describe('resolveDshCommand', () => {
+ it('turns a path inside the harness into a quoted absolute path and leaves shell lines alone', () => {
+ expect(resolveDshCommand({ realDir: STARTER }, 'toolchain/init-workspace.sh')).toBe(`'${join(STARTER, 'toolchain', 'init-workspace.sh')}'`)
+ expect(resolveDshCommand({ realDir: STARTER }, 'toolchain/missing.sh')).toBe('toolchain/missing.sh')
+ expect(resolveDshCommand({ realDir: STARTER }, 'npm run viewer -- --port $HARNESS_VIEWER_PORT')).toBe('npm run viewer -- --port $HARNESS_VIEWER_PORT')
+ })
+})
diff --git a/cli/src/dsh/materialize.ts b/cli/src/dsh/materialize.ts
index 9f87ae387..6127a24b3 100644
--- a/cli/src/dsh/materialize.ts
+++ b/cli/src/dsh/materialize.ts
@@ -137,6 +137,14 @@ function linkSkills(dsh: InstalledDsh, workspace: string, result: MaterializeRes
}
}
+/** A command that is a path inside the harness becomes that absolute path, shell-quoted. */
+export function resolveDshCommand(dsh: Pick<InstalledDsh, 'realDir'>, command: string): string {
+ if (/[\s;&|<>$`'"\\]/.test(command)) return command
+ const inside = join(dsh.realDir, command)
+ if (!existsSync(inside)) return command
+ return `'${inside.replace(/'/g, `'\\''`)}'`
+}
+
export async function materializeWorkspace(dsh: InstalledDsh, workspace: string): Promise<MaterializeResult> {
const result: MaterializeResult = { created: [], kept: [], warnings: [], initLines: [] }
const ws = dsh.manifest.workspace
@@ -144,7 +152,10 @@ export async function materializeWorkspace(dsh: InstalledDsh, workspace: string)
const fresh = marker ? !existsSync(marker) : false
if (fresh && ws?.template) copyTemplate(join(dsh.realDir, ws.template), workspace, result)
if (fresh && ws?.init) {
- const init = await runDshCommand(ws.init, {
+ // The init runs IN the workspace, so a command that names a script by its path inside the
+ // harness (the usual shape: `harness/toolchain/init-workspace.sh`) is resolved against the
+ // install directory first; anything else is a shell line and runs as written.
+ const init = await runDshCommand(resolveDshCommand(dsh, ws.init), {
cwd: workspace,
env: { HARNESS_DSH: dsh.id, HARNESS_DSH_DIR: dsh.realDir, HARNESS_WORKSPACE: workspace },
onLine: (line) => result.initLines.push(line),
diff --git a/cli/src/dsh/viewer.ts b/cli/src/dsh/viewer.ts
index 7eddc30e9..8fdc1604b 100644
--- a/cli/src/dsh/viewer.ts
+++ b/cli/src/dsh/viewer.ts
@@ -16,7 +16,7 @@ import type { ChildProcess } from 'node:child_process'
import { createServer, connect } from 'node:net'
import { isCandidateArtifact, isArtifactDirIgnored, newestArtifact } from './artifacts.js'
import type { InstalledDsh } from './installed.js'
-import { killProcessGroup, spawnDshCommand } from './shell.js'
+import { isShellNoise, killProcessGroup, spawnDshCommand } from './shell.js'
export interface DshViewerDeps {
/** The URL clients should show for this agent's viewer, or null when there is none right now. */
@@ -197,7 +197,7 @@ export class DshViewerManager {
let logged = 0
const onData = (chunk: Buffer): void => {
for (const line of chunk.toString('utf8').split('\n')) {
- if (!line.trim()) continue
+ if (!line.trim() || isShellNoise(line)) continue
if (logged++ < LOG_LINE_LIMIT) this.log(`[dsh] ${state.dsh.id} viewer · ${line}`)
}
}
diff --git a/dsh/starter-dsh/harness.json b/dsh/starter-dsh/harness.json
index 708528a3a..95b7f05b7 100644
--- a/dsh/starter-dsh/harness.json
+++ b/dsh/starter-dsh/harness.json
@@ -6,7 +6,8 @@
"engine": "claude",
"workspace": {
"template": "template",
- "marker": "NOTES.md"
+ "marker": "NOTES.md",
+ "init": "toolchain/init-workspace.sh"
},
"agent": {
"instructions": "AGENTS.md",
diff --git a/dsh/starter-dsh/toolchain/init-workspace.sh b/dsh/starter-dsh/toolchain/init-workspace.sh
new file mode 100755
index 000000000..8d84f1a1c
--- /dev/null
+++ b/dsh/starter-dsh/toolchain/init-workspace.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+# Runs once, in a fresh workspace (cwd = the workspace), after the template was copied. Harness sets
+# HARNESS_WORKSPACE and HARNESS_DSH_DIR. This one records where it ran, so a test can see it did.
+printf 'initialized by %s\n' "${HARNESS_DSH:-?}" > .harness-initialized