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.

Tasks: find gh where it is installed, not only on the daemon's PATH (#1395)

Deec30953fc88df
diff --git a/cli/src/messages/tasks.spec.ts b/cli/src/messages/tasks.spec.ts
index 56444b5fb..ac8d1ab56 100644
--- a/cli/src/messages/tasks.spec.ts
+++ b/cli/src/messages/tasks.spec.ts
@@ -5,7 +5,18 @@ const child = vi.hoisted(() => ({ execFile: vi.fn() }))
vi.mock('node:child_process', () => ({ execFile: child.execFile }))
import { markerLabel, markerName } from './sharedDirectory.js'
-import { createTaskReader, TASKS_FAILED_MS, TASKS_FRESH_MS, type Run } from './tasks.js'
+import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { createTaskReader, findGh, resetGhPath, TASKS_FAILED_MS, TASKS_FRESH_MS, type Run } from './tasks.js'
+
+/** A folder holding an executable named gh, which nothing runs: the tests read where it is, never start it. */
+function ghFolder(): string {
+ const dir = mkdtempSync(join(tmpdir(), 'gh-'))
+ writeFileSync(join(dir, 'gh'), '#!/bin/sh\nexit 1\n')
+ chmodSync(join(dir, 'gh'), 0o755)
+ return dir
+}
const ID = 'a'.repeat(24)
const OTHER = 'b'.repeat(24)
@@ -33,7 +44,7 @@ const failing = (error: unknown): Run => async () => { throw error }
const reason = async (p: Promise<unknown>) => p.then(() => 'resolved', (e: { code?: string; message: string }) => `${e.code}: ${e.message}`)
describe('Tasks search', () => {
- beforeEach(() => { child.execFile.mockReset(); vi.stubEnv('VITEST', '') })
+ beforeEach(() => { child.execFile.mockReset(); vi.stubEnv('VITEST', ''); resetGhPath() })
afterEach(() => { vi.unstubAllEnvs() })
it('runs gh for the tab\'s issues and pull requests with the brief\'s arguments, the tab id one argument, then each linked PR\'s CI', async () => {
@@ -333,16 +344,32 @@ describe('Tasks search', () => {
})
})
- it('the default runner starts gh with a timeout, SIGKILL, a 1 MiB buffer and no prompts, the arguments untouched', async () => {
+ it('the default runner starts gh by its full path with a timeout, SIGKILL, a 1 MiB buffer and no prompts, the arguments untouched', async () => {
+ const dir = ghFolder()
+ vi.stubEnv('PATH', dir)
child.execFile.mockImplementation((...all: unknown[]) => { const done = all.at(-1) as (e: unknown, r: { stdout: string; stderr: string }) => void
done(null, { stdout: '[]', stderr: '' })
})
await createTaskReader()(ID)
const [command, args, options] = child.execFile.mock.calls[0]
- expect(command).toBe('gh')
+ expect(command).toBe(join(dir, 'gh'))
expect(args.slice(0, 3)).toEqual(['search', 'issues', `harness-tab:${ID}`])
expect(options).toMatchObject({ timeout: 8000, killSignal: 'SIGKILL', maxBuffer: 1024 * 1024, env: { GH_PROMPT_DISABLED: '1', GIT_TERMINAL_PROMPT: '0' } })
- expect(child.execFile.mock.calls.every(([c]) => c === 'gh')).toBe(true)
+ expect(child.execFile.mock.calls.every(([c]) => c === join(dir, 'gh'))).toBe(true)
+ rmSync(dir, { recursive: true, force: true })
+ })
+
+ it('finds gh off the daemon\'s PATH: in the usual install folders, then on the login shell\'s PATH, and the shell only when needed', async () => {
+ // A daemon started by launchd has the bare system PATH: Homebrew's gh was "not installed" beside a working one (2026-10-17).
+ const onPath = ghFolder(), installed = ghFolder(), inShell = ghFolder(), empty = mkdtempSync(join(tmpdir(), 'nogh-'))
+ const shell = vi.fn(async () => inShell)
+ expect(await findGh({ PATH: onPath }, [installed], shell)).toBe(join(onPath, 'gh'))
+ expect(await findGh({ PATH: empty }, [empty, installed], shell)).toBe(join(installed, 'gh'))
+ expect(shell).not.toHaveBeenCalled()
+ expect(await findGh({ PATH: empty }, [empty], shell)).toBe(join(inShell, 'gh'))
+ expect(await findGh({}, [], async () => undefined)).toBeNull()
+ expect(await findGh({ PATH: empty }, [empty], async () => empty)).toBeNull()
+ for (const dir of [onPath, installed, inShell, empty]) rmSync(dir, { recursive: true, force: true })
})
it('the default runner refuses to start gh under a test runner, whatever the caller', async () => {
diff --git a/cli/src/messages/tasks.ts b/cli/src/messages/tasks.ts
index d38f7d88b..dce7bba43 100644
--- a/cli/src/messages/tasks.ts
+++ b/cli/src/messages/tasks.ts
@@ -8,7 +8,11 @@
* loosely; the marker in the body is the real filter, so an issue that merely mentions the id is not a task.
*/
import { execFile } from 'node:child_process'
+import { homedir } from 'node:os'
+import { delimiter, join } from 'node:path'
import { promisify } from 'node:util'
+import { resolveBinaryOnPath } from '../lib/binaryOnPath.js'
+import { warmLoginShellEnvironment } from '../lib/loginShellEnv.js'
import { validPullRequestUrl } from '../lib/sessionWorkWire.js'
import { MessageError } from './model.js'
import { cleanText } from './tabDelivery.js'
@@ -43,15 +47,40 @@ const ISSUE_URL = /^https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/issues\/[1-9]\d*$/
const MERGE_STATES = ['BEHIND', 'BLOCKED', 'CLEAN', 'DIRTY', 'DRAFT', 'HAS_HOOKS', 'UNSTABLE']
const exec = promisify(execFile)
+
+/** Where gh is installed when the daemon's PATH does not have it: Homebrew on Apple silicon and on Intel, Linuxbrew, and
+ * a user install. */
+export const GH_DIRS: readonly string[] = ['/opt/homebrew/bin', '/usr/local/bin', '/home/linuxbrew/.linuxbrew/bin', join(homedir(), '.local', 'bin')]
+
+/**
+ * gh's full path, or null. The daemon is started by launchd or the app, not a shell, so its PATH is the bare system one:
+ * a Mac with gh from Homebrew was told "Install the GitHub CLI (gh) on this computer" beside a working `gh` in every
+ * terminal (2026-10-17), as tmux once was (lib/tmuxOnPath.ts). So: the daemon's PATH, then the usual install folders,
+ * then the PATH the person's own login shell gives, which is spawned only when the first two have no gh.
+ */
+export async function findGh(env: NodeJS.ProcessEnv = process.env, dirs: readonly string[] = GH_DIRS,
+ shellPath: () => Promise<string | undefined> = async () => (await warmLoginShellEnvironment()).PATH): Promise<string | null> {
+ return resolveBinaryOnPath('gh', env) ?? resolveBinaryOnPath('gh', { PATH: dirs.join(delimiter) })
+ ?? resolveBinaryOnPath('gh', { PATH: (await shellPath()) ?? '' })
+}
+
+// Found once and kept; a gh still missing is looked for again at the next read, so installing it needs no restart.
+let ghPath: string | null = null
+
/** `gh` with no prompt and no wait for one: a terminal login question would hang the run until its timeout. */
const ghRun: Run = async (args) => {
// Refused under a test runner, whatever the caller: a real `gh` would read the developer's own GitHub login and search
// public GitHub with it. Tests inject a `Run`; production never has VITEST set.
if (process.env.VITEST) throw Object.assign(new Error('The real gh is not run in tests.'), { stderr: 'The real gh is not run in tests.' })
- return { stdout: (await exec('gh', args, { timeout: 8000, killSignal: 'SIGKILL', maxBuffer: 1024 * 1024,
+ ghPath ??= await findGh()
+ if (!ghPath) throw Object.assign(new Error('gh was not found'), { code: 'ENOENT', stderr: '' })
+ return { stdout: (await exec(ghPath, args, { timeout: 8000, killSignal: 'SIGKILL', maxBuffer: 1024 * 1024,
env: { ...process.env, GH_PROMPT_DISABLED: '1', GIT_TERMINAL_PROMPT: '0' } })).stdout }
}
+/** Test seam: forget the gh that was found. */
+export function resetGhPath(): void { ghPath = null }
+
/** What a failed run means to the person, as a `MessageError`; the stderr shown is `gh`'s own, cleaned and cut. */
function ghError(error: unknown): MessageError {
const e = (error ?? {}) as { code?: unknown; killed?: unknown; signal?: unknown; stderr?: unknown }