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
cli: sign in and out on the grid the daemon resolves, and keep the suite off the real runtime dir
Kelvinbcd2c68c9735
diff --git a/cli/src/config/envIsolation.spec.ts b/cli/src/config/envIsolation.spec.tsnew file mode 100644index 000000000..36f4d5b3f--- /dev/null+++ b/cli/src/config/envIsolation.spec.ts@@ -0,0 +1,25 @@+/**+ * The suite must never read or write the developer's real `~/.harness`.+ *+ * `vitest.setup.ts` moved the DATA dir to a throwaway after a spec wrote a fixture into the live+ * registry. The RUNTIME dir is the same hazard in the other direction: a real `current-grid` or+ * `current-node` pointer there outranks the fakes a spec puts on PATH (`gridExec.ts` resolves the+ * managed runtime BEFORE PATH, by design), so the suite would measure this machine's managed+ * binaries instead of the code under test.+ */+import { homedir } from 'node:os'+import { join } from 'node:path'+import { describe, expect, it } from 'vitest'+import { env } from './env.js'++const productRoot = join(homedir(), '.harness')++describe('test environment isolation', () => {+ it('keeps the data dir off the developer\'s ~/.harness', () => {+ expect(env.ADAPTER_DATA_DIR.startsWith(productRoot)).toBe(false)+ })++ it('keeps the runtime dir off the developer\'s ~/.harness, so no real managed grid or node outranks a fake on PATH', () => {+ expect(env.ADAPTER_RUNTIME_DIR.startsWith(productRoot)).toBe(false)+ })+})diff --git a/cli/src/gridCommand.spec.ts b/cli/src/gridCommand.spec.tsindex 69aae8e6b..75fcb9149 100644--- a/cli/src/gridCommand.spec.ts+++ b/cli/src/gridCommand.spec.ts@@ -92,6 +92,19 @@ function fakeGridBin(root: string, state: GridOnPath): string {return dir}+/** The same fake, somewhere PATH does not reach, for the case where the harness is TOLD where+ * `grid` is. Its shebang names this process's Node by absolute path, because that case runs with+ * an empty PATH — where `#!/usr/bin/env node` would fail for a reason that is not the one under+ * test. */+function fakeGridOutsidePath(root: string): string {+ const dir = join(root, 'elsewhere')+ mkdirSync(dir, { recursive: true })+ const script = join(dir, 'grid')+ writeFileSync(script, FAKE_GRID.replace('#!/usr/bin/env node', `#!${process.execPath}`))+ chmodSync(script, 0o755)+ return script+}+function recordFile(root: string): string { return join(root, 'grid-invocation.json') }function readRecord(root: string): { args: string[]; stdin: string; env: Record<string, string> } {@@ -115,12 +128,18 @@ function seedSession(root: string, overrides: Record<string, unknown> = {}): voi}function envFor(root: string, backendUrl?: string, extra: NodeJS.ProcessEnv = {}, grid: GridOnPath = 'runnable'): NodeJS.ProcessEnv {+ // The harness resolves `grid` as HARNESS_GRID_BIN → the managed runtime → PATH (lib/gridExec.ts).+ // The fake below is put on PATH, so the two answers that would outrank it are taken away: a+ // developer's own override never reaches the child, and the runtime dir is one with no+ // `current-grid` in it — not this machine's `~/.harness/runtime`.+ const { HARNESS_GRID_BIN: _developersOwn, ...inherited } = process.envreturn {- ...process.env,+ ...inherited,HOME: root,HARNESS_AUTH_DIR: join(root, 'auth'),ADAPTER_DATA_DIR: join(root, 'data'),ADAPTER_CLI_DIR: join(root, 'cli'),+ ADAPTER_RUNTIME_DIR: join(root, 'runtime'),ADAPTER_COMPUTER_ID_FILE: join(root, 'computer-id'),ADAPTER_UPDATE_DISABLE: 'true',// The fake goes FIRST so it wins over any real `grid` this machine has. Unless it is meant to be@@ -461,6 +480,26 @@ describe('harness grid login — when the hand-off fails', () => {}, 20_000)})+describe('harness grid login — which `grid` it runs', () => {+ /** The hand-off carries the token, so it has to run the binary every other grid call resolves to+ * (`gridExec.ts`: override → managed runtime → PATH), never a PATH lookup of its own. The+ * resolution order itself is pinned in `gridHandoff.spec.ts`; what this drives through the real+ * CLI is that the override is read off THIS process's environment, which is the seam a unit test+ * of the module cannot see. */+ it('runs the grid HARNESS_GRID_BIN names when PATH has none', async () => {+ const root = tempRoot()+ seedSession(root)+ const { base } = await signedInBackend()+ const elsewhere = fakeGridOutsidePath(root)++ const result = await run(root, ['grid', 'login', '--json'], base, { HARNESS_GRID_BIN: elsewhere }, 'absent')++ expect(result.status).toBe(0)+ expect(readRecord(root).args).toEqual(['login', '--harness', '--json'])+ expect(readRecord(root).stdin.trim()).toBe('tok_seeded')+ }, 20_000)+})+describe('harness grid login — when the harness session itself is broken', () => {it('names the harness sign-in, not the grid one, when the refresh token is invalid', async () => {const root = tempRoot()diff --git a/cli/src/lib/gridExec.ts b/cli/src/lib/gridExec.tsindex 0263f31c7..eb5b37f8d 100644--- a/cli/src/lib/gridExec.ts+++ b/cli/src/lib/gridExec.ts@@ -4,7 +4,10 @@* `gridHandoff.ts` and `gridLogout.ts` each grew their own spawn because there were two calls. The* harness-grid flow adds several more — ensure, models, endpoint — and a per-caller spawn is how a* PATH check, a capture bound and an exit-code meaning end up disagreeing three ways. So: one- * resolver, one classification, one bounded capture.+ * resolver, one classification, one bounded capture. The hand-off and the sign-out keep their own+ * spawn (a token on stdin; a passthrough to the terminal where a person is watching), but they+ * resolve the binary HERE, through [gridBinaryPath] — so every grid call on this daemon runs the one+ * `grid`.** **Which binary.** `HARNESS_GRID_BIN` (an explicit override, for a developer testing an unreleased* grid) → the runtime this daemon manages → `grid` on PATH. The managed runtime is PINNED, and it@@ -22,7 +25,8 @@ import { join, sep } from 'node:path'import { env } from '../config/env.js'import { binaryOnPath } from './binaryOnPath.js'-/** The command, as it is named on PATH. Shared with `gridHandoff.ts`, which owns the sign-in seam. */+/** The command, as it is named on PATH — the last resort of [gridBinaryPath], and the word the+ * sign-out's own "nothing to run" sentence uses (`gridLogout.ts`). */export const GRID_BINARY = 'grid'/**diff --git a/cli/src/lib/gridHandoff.spec.ts b/cli/src/lib/gridHandoff.spec.tsnew file mode 100644index 000000000..588ffa7b4--- /dev/null+++ b/cli/src/lib/gridHandoff.spec.ts@@ -0,0 +1,99 @@+/**+ * Which `grid` the sign-in hand-off runs.+ *+ * The hand-off carries the account token, so it has to run the SAME binary every other grid call+ * resolves to (`gridExec.ts`): the developer override, then the managed runtime, then PATH. A+ * hand-off that asked PATH on its own would sign in with one `grid` while models ran on another —+ * two versions writing one `~/.grid`. The token seam itself (stdin, never argv) is covered end to end+ * in `gridCommand.spec.ts`; this file is only about WHICH child gets it.+ */+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++let root = ''+let runtimeDir = ''+const saved = { PATH: process.env.PATH, HARNESS_GRID_BIN: process.env.HARNESS_GRID_BIN, ADAPTER_RUNTIME_DIR: process.env.ADAPTER_RUNTIME_DIR }++/** `gridExec.ts` reads `env.ADAPTER_RUNTIME_DIR` at import time, so the module is loaded fresh per case. */+async function load() {+ vi.resetModules()+ process.env.ADAPTER_RUNTIME_DIR = runtimeDir+ return import('./gridHandoff.js')+}++/** A `grid` that records its argv and (on `login`) its standard input, under `label`. A shell script+ * rather than a Node one, with `/bin/cat` by absolute path: several cases below run with an EMPTY+ * PATH, where `#!/usr/bin/env node` — or a bare `cat` — would fail for a reason that has nothing to+ * do with the resolution under test. */+function fakeGrid(dir: string, label: string): string {+ mkdirSync(dir, { recursive: true })+ const bin = join(dir, 'grid')+ writeFileSync(bin, [+ '#!/bin/sh',+ `printf '%s\\n' "$@" > "${join(root, `${label}.args`)}"`,+ `[ "$1" = login ] && /bin/cat > "${join(root, `${label}.stdin`)}"`,+ 'exit 0',+ '',+ ].join('\n'), { mode: 0o755 })+ return bin+}++function ran(label: string): boolean { return existsSync(join(root, `${label}.args`)) }+function argsOf(label: string): string[] { return readFileSync(join(root, `${label}.args`), 'utf8').trim().split('\n') }++beforeEach(() => {+ root = mkdtempSync(join(tmpdir(), 'grid-handoff-'))+ runtimeDir = join(root, 'runtime')+ mkdirSync(runtimeDir)+ // Nothing on PATH unless a case puts something there — never this machine's own `grid`.+ mkdirSync(join(root, 'empty-bin'))+ process.env.PATH = join(root, 'empty-bin')+ delete process.env.HARNESS_GRID_BIN+})++afterEach(() => {+ for (const [key, value] of Object.entries(saved)) {+ if (value === undefined) delete process.env[key]+ else process.env[key] = value+ }+ rmSync(root, { recursive: true, force: true })+})++describe('handOffToGrid — which grid it runs', () => {+ it('runs the grid HARNESS_GRID_BIN names, with nothing on PATH', async () => {+ const override = fakeGrid(join(root, 'elsewhere'), 'override')+ process.env.HARNESS_GRID_BIN = override+ const { handOffToGrid } = await load()++ const result = await handOffToGrid('tok_1', { json: true })++ expect(result.code).toBe('OK')+ expect(argsOf('override')).toEqual(['login', '--harness', '--json'])+ expect(readFileSync(join(root, 'override.stdin'), 'utf8')).toBe('tok_1\n')+ })++ it('prefers the managed runtime over a grid on PATH', async () => {+ fakeGrid(join(root, 'path-bin'), 'path')+ process.env.PATH = join(root, 'path-bin')+ const managed = fakeGrid(join(runtimeDir, 'grid-0.3.47-darwin-arm64'), 'managed')+ writeFileSync(join(runtimeDir, 'current-grid'), `${managed}\n`)+ const { handOffToGrid } = await load()++ const result = await handOffToGrid('tok_2', { json: true })++ expect(result.code).toBe('OK')+ expect(argsOf('managed')).toEqual(['login', '--harness', '--json'])+ expect(ran('path')).toBe(false)+ })++ it('reports GRID_CLI_MISSING when neither the managed runtime nor PATH has one', async () => {+ const { handOffToGrid } = await load()++ const result = await handOffToGrid('tok_3', { json: true })++ expect(result).toMatchObject({ code: 'GRID_CLI_MISSING', exitCode: 1 })+ expect(result.message).toContain('grid')+ })+})diff --git a/cli/src/lib/gridHandoff.ts b/cli/src/lib/gridHandoff.tsindex df4f72dd2..e552d5c51 100644--- a/cli/src/lib/gridHandoff.ts+++ b/cli/src/lib/gridHandoff.ts@@ -13,11 +13,13 @@*/import { spawn } from 'node:child_process'import { binaryOnPath } from './binaryOnPath.js'+import { gridBinaryPath } from './gridExec.js'-/** The command, and the flag on it that means "read the token off stdin" (autonomous-grid's- * `cli/parser.py`). Located on PATH with no environment override: tests control PATH directly, so an- * override would be a knob with no user. */-export const GRID_BINARY = 'grid'+/** The flag on `grid login` that means "read the token off stdin" (autonomous-grid's `cli/parser.py`).+ * WHICH `grid` is not decided here: `gridBinaryPath()` (lib/gridExec.ts) answers that for every grid+ * call alike — the developer override, then the managed runtime, then PATH — because this call+ * carries the token, and a sign-in on one binary with models on another would be two versions+ * writing one `~/.grid`. */export const GRID_HANDOFF_FLAG = '--harness'/**@@ -52,8 +54,8 @@ function capped(sofar: string, chunk: Buffer): string {}const MISSING_MESSAGE =- 'No `grid` on PATH, so there is nothing to hand this sign-in to. Install the grid CLI, then run '- + '`harness grid login` again.'+ 'No `grid` on PATH and no managed grid runtime, so there is nothing to hand this sign-in to. Install '+ + 'the grid CLI, then run `harness grid login` again.'const OUTDATED_MESSAGE =`Your \`grid\` CLI is too old: it does not understand \`grid login ${GRID_HANDOFF_FLAG}\`. Update `@@ -80,16 +82,17 @@ export async function handOffToGrid(token: string,opts: { json?: boolean } = {},): Promise<GridHandoffResult> {- // Asking PATH rather than spawning to find out: a missing `grid` is a sentence about installing- // one, not a spawn error the caller has to recognise. This process's own environment, inherited —- // there is nothing to override, and a parameter for one would be the knob with no user the ticket- // refuses.- if (!binaryOnPath(GRID_BINARY)) {+ // Asking by reading rather than by spawning to find out: a missing `grid` is a sentence about+ // installing one, not a spawn error the caller has to recognise — and a present-but-unrunnable+ // one (EACCES, never ENOENT) is caught too. Resolved off this process's own environment, which is+ // the one place the override and the runtime dir are read from.+ const binary = gridBinaryPath()+ if (!binaryOnPath(binary)) {return { code: 'GRID_CLI_MISSING', exitCode: 1, message: MISSING_MESSAGE, stdout: '', stderr: '' }}const args = ['login', GRID_HANDOFF_FLAG, ...(opts.json ? ['--json'] : [])]return await new Promise<GridHandoffResult>((resolve) => {- const child = spawn(GRID_BINARY, args, {+ const child = spawn(binary, args, {stdio: ['pipe', opts.json ? 'pipe' : 'inherit', opts.json ? 'pipe' : 'inherit'],})let stdout = ''@@ -109,7 +112,7 @@ export async function handOffToGrid(const { stdin } = childif (!stdin) {child.kill()- settle({ code: 'GRID_LOGIN_FAILED', exitCode: 1, message: `Could not open a pipe to \`${GRID_BINARY}\`.`, stdout, stderr })+ settle({ code: 'GRID_LOGIN_FAILED', exitCode: 1, message: `Could not open a pipe to \`${binary}\`.`, stdout, stderr })return}@@ -117,7 +120,7 @@ export async function handOffToGrid(// before reading breaks the pipe. Both are the child's story to tell, never a crash here.child.once('error', (err: NodeJS.ErrnoException) => settle(err.code === 'ENOENT'? { code: 'GRID_CLI_MISSING', exitCode: 1, message: MISSING_MESSAGE, stdout, stderr }- : { code: 'GRID_LOGIN_FAILED', exitCode: 1, message: `Could not run \`${GRID_BINARY}\`: ${err.message}`, stdout, stderr }))+ : { code: 'GRID_LOGIN_FAILED', exitCode: 1, message: `Could not run \`${binary}\`: ${err.message}`, stdout, stderr }))stdin.on('error', () => { /* EPIPE — see above */ })// A trailing newline as well as the close: `grid` reads one bounded LINE, so the hand-off doesdiff --git a/cli/src/lib/gridLogout.spec.ts b/cli/src/lib/gridLogout.spec.tsnew file mode 100644index 000000000..cef1698b6--- /dev/null+++ b/cli/src/lib/gridLogout.spec.ts@@ -0,0 +1,83 @@+/**+ * Which `grid` the sign-out passthrough runs — the same answer as the hand-off, for the same reason:+ * the sign-in and the sign-out must land on ONE binary, the one `gridExec.ts` resolves for every+ * other grid call (override → managed runtime → PATH). What the passthrough does and does not wrap+ * is covered in `gridCommand.spec.ts`.+ */+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++let root = ''+let runtimeDir = ''+const saved = { PATH: process.env.PATH, HARNESS_GRID_BIN: process.env.HARNESS_GRID_BIN, ADAPTER_RUNTIME_DIR: process.env.ADAPTER_RUNTIME_DIR }++async function load() {+ vi.resetModules()+ process.env.ADAPTER_RUNTIME_DIR = runtimeDir+ return import('./gridLogout.js')+}++/** Records its argv under `label` and exits 0. A shell script, so an empty PATH cannot break it. */+function fakeGrid(dir: string, label: string): string {+ mkdirSync(dir, { recursive: true })+ const bin = join(dir, 'grid')+ writeFileSync(bin, `#!/bin/sh\nprintf '%s\\n' "$@" > "${join(root, `${label}.args`)}"\nexit 0\n`, { mode: 0o755 })+ return bin+}++function ran(label: string): boolean { return existsSync(join(root, `${label}.args`)) }+function argsOf(label: string): string[] { return readFileSync(join(root, `${label}.args`), 'utf8').trim().split('\n') }++beforeEach(() => {+ root = mkdtempSync(join(tmpdir(), 'grid-logout-'))+ runtimeDir = join(root, 'runtime')+ mkdirSync(runtimeDir)+ mkdirSync(join(root, 'empty-bin'))+ process.env.PATH = join(root, 'empty-bin')+ delete process.env.HARNESS_GRID_BIN+})++afterEach(() => {+ for (const [key, value] of Object.entries(saved)) {+ if (value === undefined) delete process.env[key]+ else process.env[key] = value+ }+ rmSync(root, { recursive: true, force: true })+})++describe('passThroughToGridLogout — which grid it runs', () => {+ it('runs the grid HARNESS_GRID_BIN names, with nothing on PATH', async () => {+ process.env.HARNESS_GRID_BIN = fakeGrid(join(root, 'elsewhere'), 'override')+ const { passThroughToGridLogout } = await load()++ const outcome = await passThroughToGridLogout(['--force'])++ expect(outcome).toEqual({ ran: true, exitCode: 0 })+ expect(argsOf('override')).toEqual(['logout', '--force'])+ })++ it('prefers the managed runtime over a grid on PATH', async () => {+ fakeGrid(join(root, 'path-bin'), 'path')+ process.env.PATH = join(root, 'path-bin')+ const managed = fakeGrid(join(runtimeDir, 'grid-0.3.47-darwin-arm64'), 'managed')+ writeFileSync(join(runtimeDir, 'current-grid'), `${managed}\n`)+ const { passThroughToGridLogout } = await load()++ const outcome = await passThroughToGridLogout([])++ expect(outcome).toEqual({ ran: true, exitCode: 0 })+ expect(argsOf('managed')).toEqual(['logout'])+ expect(ran('path')).toBe(false)+ })++ it('says there was no child to run when neither the managed runtime nor PATH has one', async () => {+ const { passThroughToGridLogout } = await load()++ const outcome = await passThroughToGridLogout([])++ expect(outcome).toMatchObject({ ran: false, exitCode: 1 })+ expect(outcome.ran === false && outcome.message).toContain('grid')+ })+})diff --git a/cli/src/lib/gridLogout.ts b/cli/src/lib/gridLogout.tsindex bfae698ba..9a955adb0 100644--- a/cli/src/lib/gridLogout.ts+++ b/cli/src/lib/gridLogout.ts@@ -17,7 +17,7 @@*/import { spawn } from 'node:child_process'import { binaryOnPath } from './binaryOnPath.js'-import { GRID_BINARY } from './gridHandoff.js'+import { GRID_BINARY, gridBinaryPath } from './gridExec.js'/** The verb, on the same binary the hand-off spawns. Deliberately NOT a cross-repo pin: rename it in* autonomous-grid and argparse refuses it loudly, in the child's own words, on the child's own@@ -25,8 +25,8 @@ import { GRID_BINARY } from './gridHandoff.js'const GRID_LOGOUT_VERB = 'logout'const MISSING_MESSAGE =- `No \`${GRID_BINARY}\` on PATH, so there is no grid sign-in here for this to end. If your grid CLI `- + 'is installed somewhere else, run `grid logout` there.'+ `No \`${GRID_BINARY}\` on PATH and no managed grid runtime, so there is no grid sign-in here for this `+ + 'to end. If your grid CLI is installed somewhere else, run `grid logout` there.'/*** A child ran and its exit code is the answer, or none did and this carries the sentence.@@ -48,21 +48,23 @@ const missing = (): GridLogoutOutcome => ({ ran: false, exitCode: 1, message: MI* place that has to know what the sign-out accepts.*/export async function passThroughToGridLogout(args: string[]): Promise<GridLogoutOutcome> {- // Asking PATH rather than spawning to find out, exactly as the hand-off does: an absent `grid` is+ // The same binary the hand-off signed in on — `gridBinaryPath()`: override, managed runtime, PATH —+ // and asked by reading rather than by spawning, exactly as the hand-off does: an absent `grid` is// a sentence, not a spawn error every caller would have to recognise. It also catches the case a// spawn cannot — a `grid` that is present but not executable answers EACCES, never ENOENT.- if (!binaryOnPath(GRID_BINARY)) return missing()+ const binary = gridBinaryPath()+ if (!binaryOnPath(binary)) return missing()return await new Promise<GridLogoutOutcome>((resolve) => {// All three streams inherited: the child talks to the terminal directly, which is what makes// this a passthrough rather than a re-narration of one.- const child = spawn(GRID_BINARY, [GRID_LOGOUT_VERB, ...args], { stdio: 'inherit' })+ const child = spawn(binary, [GRID_LOGOUT_VERB, ...args], { stdio: 'inherit' })let settled = falseconst settle = (outcome: GridLogoutOutcome): void => { if (!settled) { settled = true; resolve(outcome) } }// Between the PATH check and the spawn the binary can still be gone.child.once('error', (err: NodeJS.ErrnoException) => settle(err.code === 'ENOENT'? missing()- : { ran: false, exitCode: 1, message: `Could not run \`${GRID_BINARY}\`: ${err.message}` }))+ : { ran: false, exitCode: 1, message: `Could not run \`${binary}\`: ${err.message}` }))// A child killed by a signal has no status. It reports as an ordinary non-zero exit, because// that is what a shell would see and because the signal reached this process too — the persondiff --git a/cli/vitest.setup.ts b/cli/vitest.setup.tsindex adbaeb556..0e97585ad 100644--- a/cli/vitest.setup.ts+++ b/cli/vitest.setup.ts@@ -1,13 +1,19 @@/**- * Point every test's data dir at a throwaway directory BEFORE any module reads `env`.+ * Point every test's data AND runtime dirs at throwaway directories BEFORE any module reads `env`.** `config/env.ts` resolves `ADAPTER_DATA_DIR` at import time and defaults to the user's real* `~/.harness/cli/data`. Specs that import the hook server (and through it the registry) therefore wrote* their fixtures into the LIVE registry — a stale `late-session` record turned up there, written by* `launcherWs.spec.ts`, which is how this was found. Tests must never touch the user's data.+ *+ * The runtime dir is the same hazard in the other direction. It is only ever READ, but what is read+ * there — `current-grid`, `current-node` — outranks PATH by design (lib/gridExec.ts), so a real pointer+ * under `~/.harness/runtime` would send a spec's calls to this machine's managed binary instead of the+ * fake the spec put on PATH. `src/config/envIsolation.spec.ts` pins both.*/import { mkdtempSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'process.env.ADAPTER_DATA_DIR = mkdtempSync(join(tmpdir(), 'adapter-test-data-'))+process.env.ADAPTER_RUNTIME_DIR = mkdtempSync(join(tmpdir(), 'adapter-test-runtime-'))