Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(autopilot): forward process.env to execSync('which gbrain') under Bun (#2747) (#2976)

Masa1d0b5ed816d5
diff --git a/src/commands/autopilot.ts b/src/commands/autopilot.ts
index 417064cf18..8fd7531237 100644
--- a/src/commands/autopilot.ts
+++ b/src/commands/autopilot.ts
@@ -109,7 +109,21 @@ function logError(phase: string, e: unknown) {
*/
export function resolveGbrainCliPath(): string {
try {
- const which = execSync('which gbrain', { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
+ // #2747: `env: process.env` is required under Bun. Bun's execSync
+ // snapshots process.env at Bun's OWN startup, not at call time — a
+ // runtime PATH mutation (dotenv/config loading, shell-profile sourcing
+ // in a wrapper, etc.) happening between Bun boot and this call is
+ // invisible to `which` without explicitly forwarding the current env.
+ // This is why "which gbrain" succeeds when run standalone (fresh Bun
+ // process, no prior mutation) but can fail from inside autopilot's own
+ // process at this exact call site. Same fix already applied to
+ // detectTini() in spawn-helpers.ts (see its comment) — this call site
+ // was missed.
+ const which = execSync('which gbrain', {
+ encoding: 'utf-8',
+ stdio: ['ignore', 'pipe', 'ignore'],
+ env: process.env,
+ }).trim();
if (which) return which;
} catch { /* not on $PATH — fall through */ }
@@ -123,7 +137,14 @@ export function resolveGbrainCliPath(): string {
return arg1;
}
- throw new Error('Could not resolve the gbrain CLI path. Install gbrain so it is on $PATH (e.g. /usr/local/bin/gbrain), or run autopilot from the compiled binary directly.');
+ // #2747: include what we actually saw so an operator (or a future bug
+ // report) doesn't have to guess whether PATH/execPath/argv[1] looked
+ // sane at the moment of failure.
+ throw new Error(
+ 'Could not resolve the gbrain CLI path. Install gbrain so it is on $PATH ' +
+ '(e.g. /usr/local/bin/gbrain), or run autopilot from the compiled binary directly. ' +
+ `Debug: PATH=${JSON.stringify(process.env.PATH ?? '')} execPath=${JSON.stringify(exec)} argv1=${JSON.stringify(arg1)}`,
+ );
}
export function shouldSpawnAutopilotWorker(args: string[]): boolean {
diff --git a/src/core/brain-repo-durability.ts b/src/core/brain-repo-durability.ts
index 7fa1685aa5..8ac06df218 100644
--- a/src/core/brain-repo-durability.ts
+++ b/src/core/brain-repo-durability.ts
@@ -100,7 +100,15 @@ function gbrainHome(): string {
* core→commands import). which gbrain → process.execPath → argv[1] → "gbrain". */
function resolveGbrainCliPath(): string {
try {
- const which = execSync('which gbrain', { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
+ // #2747: `env: process.env` required under Bun — see the sibling copy
+ // of this function in commands/autopilot.ts for the full explanation
+ // (Bun snapshots process.env at its own startup; execSync without an
+ // explicit env is blind to any PATH mutation since then).
+ const which = execSync('which gbrain', {
+ encoding: 'utf-8',
+ stdio: ['ignore', 'pipe', 'ignore'],
+ env: process.env,
+ }).trim();
if (which) return which;
} catch { /* not on PATH */ }
const exec = process.execPath ?? '';