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
refactor: make login shell environment capture asynchronous to optimize daemon startup and prevent blocking calls
0xken15f7c4af1043f8
diff --git a/cli/src/cli.ts b/cli/src/cli.tsindex 2ac6a0914..919e3f35e 100644--- a/cli/src/cli.ts+++ b/cli/src/cli.ts@@ -761,15 +761,21 @@ async function startCommand(foreground: boolean, repair: boolean = false): Promiconsole.error('\n ✗ Not signed in. Run: harness login\n')process.exit(1)}- // Update-before-connect: pull the newest bundle FIRST so a machine always reconnects on the latest- // build. Staging swaps ~/.harness/cli/cli.js, and the daemon start spawns below (`node cli.js __run`)+ // Update-before-connect: pull the newest bundle so a machine always reconnects on the latest build.+ // Staging swaps ~/.harness/cli/cli.js, and the daemon start spawns below (`node cli.js __run`)// executes those fresh bytes. Skipped in the foreground (THIS process becomes the long-lived daemon// and self-updates on its own) and on a dev/repo build; never blocks start if the check fails.+ // Run alongside resolveComputerMachine() rather than before it — the two touch disjoint resources+ // (bundle files vs. the auth-session file/a machine-resolve POST), so there is nothing to serialize.if (!foreground) {- const v = await stageLatestBundle((m) => console.log(m))+ const [v] = await Promise.all([+ stageLatestBundle((m) => console.log(m)),+ resolveComputerMachine(),+ ])if (v) console.log(` ✓ updated to v${v} — connecting on the new build`)+ } else {+ await resolveComputerMachine()}- await resolveComputerMachine()await launch(foreground, repair)}@@ -960,8 +966,13 @@ async function runForeground(session: AuthSession): Promise<void> {// Before ANY tmux call: a daemon that came up outside a terminal (the usual shape after a reboot)// has a minimal PATH, and every `execFile('tmux', …)` below would ENOENT. Ask the user's own login// shell where tmux is and adopt that directory, the same way the engine launch already consults it.- if (terminalConfig.backends.includes('tmux')) {- const tmuxPath = await ensureTmuxOnPath()+ // Both of these are independent login-shell spawns with nothing dependent on the other's+ // result — started together here so their wall-clock cost overlaps instead of adding up.+ // `loginShellEnvPromise` is awaited later, near the existing `[env]` log line.+ const tmuxPathPromise = terminalConfig.backends.includes('tmux') ? ensureTmuxOnPath() : null+ const loginShellEnvPromise = warmLoginShellEnvironment()+ if (tmuxPathPromise) {+ const tmuxPath = await tmuxPathPromiseif (tmuxPath.state === 'adopted') {console.log(`[tmux] not on the daemon PATH · adopted ${tmuxPath.from} from the user's login shell`)} else if (tmuxPath.state === 'absent') {@@ -1086,14 +1097,16 @@ async function runForeground(session: AuthSession): Promise<void> {}const sqliteWarning = sqlitePreflightMessage()if (sqliteWarning) console.warn(sqliteWarning)- // Capture the user's shell environment NOW, not on the first recap. It is a synchronous spawn, and- // paying it here — where the daemon is already doing blocking startup work — keeps it off the path- // of a live turn, where a slow profile (nvm, conda, …) would stall frame handling instead.- // See lib/loginShellEnv.ts: this is what lets a recap reach a credential the user exports from their- // rc file, which a launchd/systemd-parented daemon never read.+ // Capture the user's shell environment NOW, not on the first recap — paying it here, where the+ // daemon is already doing blocking startup work, keeps it off the path of a live turn, where a+ // slow profile (nvm, conda, …) would stall frame handling instead. Started above, alongside the+ // tmux PATH probe, so this is usually just picking up an already-finished (or nearly so) capture+ // rather than paying for it here — the logged "…ms" is residual wait, not the full capture time.+ // See lib/loginShellEnv.ts: this is what lets a recap reach a credential the user exports from+ // their rc file, which a launchd/systemd-parented daemon never read.{const t0 = Date.now()- const captured = Object.keys(warmLoginShellEnvironment()).length+ const captured = Object.keys(await loginShellEnvPromise).lengthconsole.log(captured? `[env] read ${captured} variables from the login shell in ${Date.now() - t0}ms (engine one-shots only)`: '[env] could not read a login shell environment — engine one-shots use the daemon environment only')diff --git a/cli/src/lib/loginShellEnv.spec.ts b/cli/src/lib/loginShellEnv.spec.tsindex 28ddb6bf4..a918f59a2 100644--- a/cli/src/lib/loginShellEnv.spec.ts+++ b/cli/src/lib/loginShellEnv.spec.ts@@ -20,10 +20,10 @@ describe('loginShellEnvironment', () => {* The property the pi fix rests on: the daemon's own environment always wins, so every variable it* already has keeps its value and nothing that works today can change. The shell only fills gaps.*/- it('never lets the shell override a variable the daemon already has', () => {+ it('never lets the shell override a variable the daemon already has', async () => {process.env.HARNESS_LOGIN_SHELL_PROBE = 'from-daemon'resetLoginShellEnvironmentCache()- warmLoginShellEnvironment()+ await warmLoginShellEnvironment()expect(oneShotParentEnv().HARNESS_LOGIN_SHELL_PROBE).toBe('from-daemon')})@@ -36,30 +36,34 @@ describe('loginShellEnvironment', () => {expect(oneShotParentEnv().PATH).toBe(process.env.PATH)})- it('still carries the daemon environment when no shell can be consulted', () => {+ it('still carries the daemon environment when no shell can be consulted', async () => {delete process.env.SHELLresetLoginShellEnvironmentCache()- expect(warmLoginShellEnvironment()).toEqual({})+ expect(await warmLoginShellEnvironment()).toEqual({})expect(oneShotParentEnv().PATH).toBe(process.env.PATH)})- it('refuses a relative or bogus SHELL rather than spawning it', () => {+ it('refuses a relative or bogus SHELL rather than spawning it', async () => {process.env.SHELL = 'zsh' // not absoluteresetLoginShellEnvironmentCache()- expect(warmLoginShellEnvironment()).toEqual({})+ expect(await warmLoginShellEnvironment()).toEqual({})})- it('caches, so a recap never pays for the probe twice', () => {+ it('caches, so a recap never pays for the probe twice', async () => {resetLoginShellEnvironmentCache()- expect(warmLoginShellEnvironment()).toBe(warmLoginShellEnvironment())+ // Two overlapping callers join the same in-flight capture rather than each spawning their own+ // shell, and a call after it settles reuses the cached, already-resolved value.+ const [a, b] = await Promise.all([warmLoginShellEnvironment(), warmLoginShellEnvironment()])+ expect(a).toBe(b)+ expect(await warmLoginShellEnvironment()).toBe(a)})// Reads ~/.zshrc etc., so it only runs where a real shell exists — the environment this fixes.it.skipIf(process.platform === 'win32' || !realShell?.startsWith('/'))(- 'captures variables an interactive login shell exports', () => {+ 'captures variables an interactive login shell exports', async () => {resetLoginShellEnvironmentCache()- const captured = warmLoginShellEnvironment()+ const captured = await warmLoginShellEnvironment()// A login shell always exports these two; if neither came back, the capture is not working.expect(Object.keys(captured).length).toBeGreaterThan(0)diff --git a/cli/src/lib/loginShellEnv.ts b/cli/src/lib/loginShellEnv.tsindex e32d26e19..f9cd25b6d 100644--- a/cli/src/lib/loginShellEnv.ts+++ b/cli/src/lib/loginShellEnv.ts@@ -16,57 +16,78 @@* This is not a new privilege: an engine started in a pane already runs with exactly this environment.* All it does is let the recap run where the agent it summarises runs.*/-import { spawnSync } from 'node:child_process'+import { spawn } from 'node:child_process'/** Printed before the dump so anything a chatty rc file writes to stdout is skipped, not parsed. */const SENTINEL = '__HARNESS_ENV_BEGIN__'const CAPTURE_TIMEOUT_MS = 5_000let cached: NodeJS.ProcessEnv | null = null+// The in-flight capture, not just its resolved value — two overlapping warm-up calls (there is only+// ever one production caller today, but this keeps it true regardless) must join the same spawn+// rather than each starting their own shell.+let inFlight: Promise<NodeJS.ProcessEnv> | null = null/** What was captured, or `{}` if nothing has been. NEVER spawns — see warmLoginShellEnvironment. */export function loginShellEnvironment(): NodeJS.ProcessEnv {return cached ?? {}}-/**- * Perform the capture. Best-effort and once per process; a shell that is missing, slow, or broken- * yields `{}` and the caller still gets `process.env`, i.e. exactly today's behaviour.- *- * This is a SYNCHRONOUS spawn and it is deliberately not called lazily from the one-shot path. It was,- * and it cost ~1s inside `runDevinOneShot`, which raced that suite's 200ms timeout into a flaky- * failure — a live turn would have paid the same stall. The daemon warms it during startup instead,- * where blocking work already happens, and tests never spawn a shell at all.- */-export function warmLoginShellEnvironment(): NodeJS.ProcessEnv {- if (cached) return cached- cached = {}- if (process.platform === 'win32') return cached- const shell = process.env.SHELL- if (!shell || !shell.startsWith('/')) return cached-- let result- try {- result = spawnSync(shell, ['-lic', `printf %s ${SENTINEL}; env -0`], {- timeout: CAPTURE_TIMEOUT_MS,- encoding: 'utf8',- // An interactive shell with no tty still runs rc files; keep its stdin closed so nothing waits.- stdio: ['ignore', 'pipe', 'ignore'],- })- } catch { return cached }- const stdout = result?.stdout- if (result?.error || typeof stdout !== 'string') return cached+function parse(stdout: string): NodeJS.ProcessEnv {const start = stdout.indexOf(SENTINEL)- if (start < 0) return cached-+ if (start < 0) return {}const parsed: NodeJS.ProcessEnv = {}for (const entry of stdout.slice(start + SENTINEL.length).split('\0')) {const eq = entry.indexOf('=')if (eq <= 0) continueparsed[entry.slice(0, eq)] = entry.slice(eq + 1)}- cached = parsed- return cached+ return parsed+}++async function capture(shell: string): Promise<NodeJS.ProcessEnv> {+ return await new Promise<NodeJS.ProcessEnv>((resolve) => {+ let child+ try {+ child = spawn(shell, ['-lic', `printf %s ${SENTINEL}; env -0`], {+ timeout: CAPTURE_TIMEOUT_MS, // spawn (Node ≥15.14) SIGTERMs the child once this elapses+ // An interactive shell with no tty still runs rc files; keep its stdin closed so nothing waits.+ stdio: ['ignore', 'pipe', 'ignore'],+ })+ } catch {+ resolve({})+ return+ }+ const chunks: Buffer[] = []+ child.stdout?.on('data', (chunk: Buffer) => chunks.push(chunk))+ child.once('error', () => resolve({}))+ child.once('close', (code) => resolve(code === 0 ? parse(Buffer.concat(chunks).toString('utf8')) : {}))+ })+}++/**+ * Perform the capture. Best-effort and once per process; a shell that is missing, slow, or broken+ * yields `{}` and the caller still gets `process.env`, i.e. exactly today's behaviour.+ *+ * Async (not `spawnSync`) so the daemon can run this alongside other independent startup work+ * (e.g. `ensureTmuxOnPath`'s own login-shell probe) instead of paying for both back to back. It is+ * deliberately not called lazily from the one-shot path. It was, and it cost ~1s inside+ * `runDevinOneShot`, which raced that suite's 200ms timeout into a flaky failure — a live turn would+ * have paid the same stall. The daemon warms it during startup instead, where blocking work already+ * happens, and tests never spawn a shell at all.+ */+export function warmLoginShellEnvironment(): Promise<NodeJS.ProcessEnv> {+ if (inFlight) return inFlight+ if (cached) return Promise.resolve(cached)+ inFlight = (async (): Promise<NodeJS.ProcessEnv> => {+ if (process.platform === 'win32') return (cached = {})+ const shell = process.env.SHELL+ if (!shell || !shell.startsWith('/')) return (cached = {})+ return (cached = await capture(shell))+ })().finally(() => {+ inFlight = null+ })+ return inFlight}/**@@ -81,4 +102,7 @@ export function oneShotParentEnv(): NodeJS.ProcessEnv {}/** Test seam. */-export function resetLoginShellEnvironmentCache(): void { cached = null }+export function resetLoginShellEnvironmentCache(): void {+ cached = null+ inFlight = null+}