This commit is contained in:
Oleg Maslov
2026-09-02 10:14:22 +02:00
parent 0c3e2ead3b
commit b20b138fe4
771 changed files with 161561 additions and 9027 deletions

View File

@@ -41,7 +41,7 @@
* therefore route through HOOKS_COHORT, not the launchable registry.
*/
import { spawn, execFile } from 'node:child_process';
import { spawn, execFile, type ChildProcess } from 'node:child_process';
import { existsSync } from 'node:fs';
import { delimiter, dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -52,6 +52,11 @@ import {
resolveToolCommandInvocation,
type ToolCommandInvocation,
} from './tool-command.js';
import { buildExternalProcessEnv } from './external-process-env.js';
import {
isSidecarOwnedProcessExitMessage,
spawnSidecarOwnedProcess,
} from './sidecar-owned-process.js';
const execFileAsync = promisify(execFile);
@@ -79,6 +84,8 @@ export interface ObservedHandle {
export interface ToolLauncherDeps {
/** Override platform (defaults to process.platform). */
platform?: NodeJS.Platform;
/** Test seam for the ambient process environment. */
baseEnv?: NodeJS.ProcessEnv;
/**
* Detached-spawn implementation. Production uses `child_process.spawn`
* with `detached: true`. Returns { pid } on success or { error }.
@@ -124,7 +131,7 @@ function defaultSpawnDetached(
const invocation = resolveSpawnInvocation(binary, args);
const child = spawn(invocation.binary, invocation.args, {
cwd: options.cwd,
env: { ...process.env, ...(options.env ?? {}) },
env: options.env,
detached: true,
stdio: 'ignore',
windowsVerbatimArguments: invocation.windowsVerbatimArguments === true,
@@ -145,19 +152,20 @@ function defaultSpawnDetached(
}
}
function defaultSpawnObserved(
export function defaultSpawnObserved(
binary: string,
args: string[],
options: { cwd?: string; env?: NodeJS.ProcessEnv },
): { pid: number | null; error?: string; handle?: ObservedHandle } {
try {
const invocation = resolveSpawnInvocation(binary, args);
const child = spawn(invocation.binary, invocation.args, {
const child = spawnSidecarOwnedProcess(invocation.binary, invocation.args, {
cwd: options.cwd,
env: { ...process.env, ...(options.env ?? {}) },
// NOT detached, NOT unref'd: observation requires holding the pipes,
// so the child is tethered to the sidecar lifecycle.
env: options.env,
// Observation keeps the pipes; IPC supervision makes that ownership
// survive an abrupt Windows sidecar termination.
stdio: ['ignore', 'pipe', 'pipe'],
windowsHide: true,
windowsVerbatimArguments: invocation.windowsVerbatimArguments === true,
});
child.once('error', () => {
@@ -166,15 +174,7 @@ function defaultSpawnObserved(
if (child.pid == null) {
return { pid: null, error: 'spawn returned no pid' };
}
const handle: ObservedHandle = {
onData(cb) {
child.stdout?.on('data', (d: Buffer) => cb(d.toString('utf8')));
child.stderr?.on('data', (d: Buffer) => cb(d.toString('utf8')));
},
onExit(cb) {
child.on('exit', (code) => cb(code));
},
};
const handle = createObservedHandle(child);
return { pid: child.pid, handle };
} catch (err) {
return {
@@ -184,6 +184,22 @@ function defaultSpawnObserved(
}
}
export function createObservedHandle(child: ChildProcess): ObservedHandle {
let targetExitCode: number | null | undefined;
child.on('message', (message) => {
if (isSidecarOwnedProcessExitMessage(message)) targetExitCode = message.code;
});
return {
onData(cb) {
child.stdout?.on('data', (d: Buffer) => cb(d.toString('utf8')));
child.stderr?.on('data', (d: Buffer) => cb(d.toString('utf8')));
},
onExit(cb) {
child.on('exit', (code) => cb(targetExitCode === undefined ? code : targetExitCode));
},
};
}
export function resolveSpawnInvocation(
binary: string,
args: string[],
@@ -201,7 +217,7 @@ async function defaultExecCapture(
const invocation = resolveToolCommandInvocation(binary, args);
const { stdout, stderr } = await execFileAsync(invocation.binary, invocation.args, {
timeout: options?.timeoutMs ?? 30000,
env: { ...process.env, ...(options?.env ?? {}) },
env: options?.env,
shell: false,
windowsVerbatimArguments: invocation.windowsVerbatimArguments === true,
maxBuffer: 4 * 1024 * 1024,
@@ -224,6 +240,7 @@ async function defaultExecCapture(
interface ResolvedDeps {
platform: NodeJS.Platform;
baseEnv: NodeJS.ProcessEnv;
spawnDetached: NonNullable<ToolLauncherDeps['spawnDetached']>;
spawnObserved: NonNullable<ToolLauncherDeps['spawnObserved']>;
execCapture: NonNullable<ToolLauncherDeps['execCapture']>;
@@ -232,6 +249,7 @@ interface ResolvedDeps {
function resolveDeps(opts: ToolLauncherDeps): ResolvedDeps {
return {
platform: opts.platform ?? process.platform,
baseEnv: opts.baseEnv ?? process.env,
spawnDetached: opts.spawnDetached ?? defaultSpawnDetached,
spawnObserved: opts.spawnObserved ?? defaultSpawnObserved,
execCapture: opts.execCapture ?? defaultExecCapture,
@@ -350,28 +368,29 @@ export function launchTool(opts: LaunchOptions): LaunchResult {
}
const deps = resolveDeps(opts.deps ?? {});
const args = opts.args ?? [];
const env: NodeJS.ProcessEnv = {};
const waggleEnv: NodeJS.ProcessEnv = {};
if (opts.workspaceId) {
env.WAGGLE_WORKSPACE_ID = opts.workspaceId;
waggleEnv.WAGGLE_WORKSPACE_ID = opts.workspaceId;
}
// Self-enabling: light the SignalBus this pipeline was built to feed.
// Opt out with signalEmit:false for a silent launch.
if (opts.signalEmit !== false) {
env.WAGGLE_SIGNAL_EMIT = '1';
waggleEnv.WAGGLE_SIGNAL_EMIT = '1';
}
if (opts.sidecarUrl) {
env.WAGGLE_SIDECAR_URL = opts.sidecarUrl;
waggleEnv.WAGGLE_SIDECAR_URL = opts.sidecarUrl;
}
if (opts.dataDir) {
env.HIVE_MIND_DATA_DIR = opts.dataDir;
waggleEnv.HIVE_MIND_DATA_DIR = opts.dataDir;
}
if (opts.runId && opts.roomId && opts.runToken) {
env.WAGGLE_RUN_ID = opts.runId;
env.WAGGLE_ROOM_ID = opts.roomId;
env.WAGGLE_DANCE_TEAM_ID = `room::${opts.roomId}`;
env.WAGGLE_SENDER_ID = `run::${opts.runId}`;
env.WAGGLE_RUN_TOKEN = opts.runToken;
waggleEnv.WAGGLE_RUN_ID = opts.runId;
waggleEnv.WAGGLE_ROOM_ID = opts.roomId;
waggleEnv.WAGGLE_DANCE_TEAM_ID = `room::${opts.roomId}`;
waggleEnv.WAGGLE_SENDER_ID = `run::${opts.runId}`;
waggleEnv.WAGGLE_RUN_TOKEN = opts.runToken;
}
const env = buildExternalProcessEnv(deps.baseEnv, waggleEnv, deps.platform);
// Observed mode: piped-stdio spawn that surfaces a live output handle.
// Tethered to the sidecar (not unref'd) and tracked in-memory only.
if (opts.observe) {
@@ -539,13 +558,15 @@ export async function runHookCommand(
};
}
const args = [runtime.hookEntry, opts.action];
if (opts.action === 'install') args.push('--cli-path', runtime.cliEntry);
if (opts.action === 'install' || (opts.action === 'verify' && opts.id === 'openclaw')) {
args.push('--cli-path', runtime.cliEntry);
}
const result = await deps.execCapture(runtime.nodePath, args, {
timeoutMs: 60000,
env: {
env: buildExternalProcessEnv(deps.baseEnv, {
WAGGLE_HOOK_NODE_PATH: runtime.nodePath,
...(opts.dataDir ? { HIVE_MIND_DATA_DIR: opts.dataDir } : {}),
},
}, deps.platform),
});
if (!result) {
return {