Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(minions): detach a compiled supervisor without its embedded /$bunfs entry (#7532)
garrytan5b17c6592b2c
diff --git a/src/core/minions/detached-stderr.ts b/src/core/minions/detached-stderr.tsindex 898915140e..557bee1ef1 100644--- a/src/core/minions/detached-stderr.ts+++ b/src/core/minions/detached-stderr.ts@@ -21,7 +21,7 @@import { spawn } from '../spawn.ts';import { closeSync, mkdirSync, openSync } from 'node:fs';-import { join } from 'node:path';+import { basename, join } from 'node:path';import { resolveAuditDir } from '../audit/audit-writer.ts';export interface DetachedStderrSink {@@ -64,6 +64,16 @@ export function closeDetachedStderrSink(sink: DetachedStderrSink): void {}}+/**+ * The argv after execPath that re-runs this CLI (#7532). A bun or node+ * runtime needs the CLI script first. A compiled binary IS the CLI: its+ * script is the embedded `/$bunfs/root/...` entry, which Bun inserts again on+ * its own, so passing it would become the command and fail as unknown.+ */+export function supervisorArgv(execPath: string, cliScript: string, childArgs: string[]): string[] {+ return ['bun', 'bun.exe', 'node', 'node.exe'].includes(basename(execPath)) ? [cliScript, ...childArgs] : childArgs;+}+/*** Re-exec the CLI as a fully detached supervisor: stdin/stdout ignored,* stderr on the durable sink (NEVER 'inherit' — the #4418 regression).@@ -74,7 +84,7 @@ export function spawnDetachedSupervisor(childArgs: string[],): { pid: number | undefined; stderrPath: string | null } {const sink = openDetachedStderrSink();- const child = spawn(execPath, [cliScript, ...childArgs], {+ const child = spawn(execPath, supervisorArgv(execPath, cliScript, childArgs), {detached: true,stdio: ['ignore', 'ignore', sink.fd],env: process.env,diff --git a/test/detached-stderr.test.ts b/test/detached-stderr.test.tsindex 1de8c1a7c8..74e6ed1719 100644--- a/test/detached-stderr.test.ts+++ b/test/detached-stderr.test.ts@@ -16,7 +16,8 @@* no longer passes stderr 'inherit' (source pin — the branch needs a* live Postgres supervisor to exercise end-to-end).*/-import { describe, test, expect } from 'bun:test';+import { afterEach, describe, test, expect, mock, spyOn } from 'bun:test';+import * as childProcess from 'node:child_process';import { mkdtempSync, writeFileSync, readFileSync, rmSync, writeSync, closeSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';@@ -24,6 +25,7 @@ import { withEnv } from './helpers/with-env.ts';import {openDetachedStderrSink,closeDetachedStderrSink,+ spawnDetachedSupervisor,} from '../src/core/minions/detached-stderr.ts';import { surfaceSource } from './helpers/source-surface.ts';@@ -75,6 +77,38 @@ describe('openDetachedStderrSink (#4418)', () => {});});+describe('spawnDetachedSupervisor argv (#7532)', () => {+ afterEach(() => { mock.restore(); });++ async function spawnedArgs(execPath: string, cliScript: string): Promise<unknown[]> {+ const dir = mkdtempSync(join(tmpdir(), 'gb-detached-argv-'));+ const spawn = spyOn(childProcess, 'spawn').mockImplementation((() => ({ pid: 4242, unref: () => {} })) as never);+ try {+ await withEnv({ GBRAIN_AUDIT_DIR: dir }, async () => {+ expect(spawnDetachedSupervisor(execPath, cliScript, ['jobs', 'supervisor', 'start']).pid).toBe(4242);+ });+ expect(spawn.mock.calls.length).toBe(1);+ expect(spawn.mock.calls[0]?.[0]).toBe(execPath);+ return spawn.mock.calls[0]?.[1] as unknown[];+ } finally {+ mock.restore();+ rmSync(dir, { recursive: true, force: true });+ }+ }++ test('a compiled binary gets only its CLI args, not its embedded /$bunfs entry', async () => {+ expect(await spawnedArgs('/opt/acme-example/bin/gbrain-linux-x64', '/$bunfs/root/gbrain-linux-x64'))+ .toEqual(['jobs', 'supervisor', 'start']);+ });++ test('a bun or node runtime keeps the CLI script as its first arg', async () => {+ expect(await spawnedArgs('/usr/local/bin/bun', '/opt/acme-example/gbrain/src/cli.ts'))+ .toEqual(['/opt/acme-example/gbrain/src/cli.ts', 'jobs', 'supervisor', 'start']);+ expect(await spawnedArgs('/usr/local/bin/node', '/opt/acme-example/gbrain/dist/cli.js'))+ .toEqual(['/opt/acme-example/gbrain/dist/cli.js', 'jobs', 'supervisor', 'start']);+ });+});+describe('jobs.ts detach branch (source pin, #4418)', () => {const jobsSource = surfaceSource('jobs');