Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

refactor(spawn): core subsystems launch through src/core/spawn.ts (#4992)

garrytan6885c327a3c0
diff --git a/src/core/agent-install/setup.ts b/src/core/agent-install/setup.ts
index 5fa22215ae..197bdd730d 100644
--- a/src/core/agent-install/setup.ts
+++ b/src/core/agent-install/setup.ts
@@ -15,6 +15,7 @@ import {
readFileConfigState, readInstallReceipt, sha256, writeInstallReceipt,
type AgentHarness, type AgentInstallReceipt, type InstallArtifact,
} from './state.ts';
+import { bunSpawn, bunSpawnSync } from '../spawn.ts';
export interface AgentSetupOptions {
root: string;
@@ -41,7 +42,7 @@ export interface AgentSetupResult {
async function cli(root: string, artifact: InstallArtifact, args: string[]): Promise<void> {
const commandArgs = args.includes('--migrate-only') || args[0] === 'apply-migrations' ? [...args, '--json'] : args;
- const child = Bun.spawn([join(root, artifact.directory, 'bun'), '--no-env-file', join(root, artifact.cli), '--brain', 'host', ...commandArgs], {
+ const child = bunSpawn([join(root, artifact.directory, 'bun'), '--no-env-file', join(root, artifact.cli), '--brain', 'host', ...commandArgs], {
cwd: root, env: { ...isolatedAgentEnv(root), DATABASE_URL: '', GBRAIN_DATABASE_URL: '', ...(args[0] === 'init' ? { GBRAIN_IN_AGENT_SETUP: '1' } : {}) },
stdin: 'ignore', stdout: 'pipe', stderr: 'pipe',
});
@@ -80,7 +81,7 @@ function artifactFromBundle(bundle: string, sourceRef: string): Omit<InstallArti
throw new AgentInstallError('invalid_artifact', 'Setup bundle does not contain the GBrain package and setup helper.');
}
if (!/^[a-f0-9]{40}$/.test(sourceRef)) throw new AgentInstallError('invalid_artifact_ref', 'A resolved 40-character source commit is required.');
- const version = Bun.spawnSync([bun, '--version'], { stdout: 'pipe', stderr: 'pipe' });
+ const version = bunSpawnSync([bun, '--version'], { stdout: 'pipe', stderr: 'pipe' });
if (version.exitCode !== 0) throw new AgentInstallError('runtime_unusable', 'The staged Bun runtime is not executable on this machine.');
return { source_ref: sourceRef, version: pkg.version, bun_version: version.stdout.toString().trim(), bun_sha256: sha256(readFileSync(bun)), package_sha256: sha256(readFileSync(join(packageDir, 'package.json'))) };
}
@@ -109,7 +110,7 @@ function installArtifact(root: string, options: AgentSetupOptions, identity: Ret
function artifactUsable(root: string, artifact: InstallArtifact): boolean {
try {
if (!existsSync(confinedPath(root, artifact.cli)) || sha256(readFileSync(confinedPath(root, `${artifact.directory}/bun`))) !== artifact.bun_sha256) return false;
- const probe = Bun.spawnSync([join(root, artifact.directory, 'bun'), '--no-env-file', join(root, artifact.cli), '--version'], {
+ const probe = bunSpawnSync([join(root, artifact.directory, 'bun'), '--no-env-file', join(root, artifact.cli), '--version'], {
cwd: root, env: { ...isolatedAgentEnv(root), DATABASE_URL: '', GBRAIN_DATABASE_URL: '' }, stdout: 'pipe', stderr: 'pipe', timeout: 15_000,
});
return probe.exitCode === 0;
diff --git a/src/core/ai/recipes/azure-openai.ts b/src/core/ai/recipes/azure-openai.ts
index d4302b7b8e..a832184c92 100644
--- a/src/core/ai/recipes/azure-openai.ts
+++ b/src/core/ai/recipes/azure-openai.ts
@@ -1,6 +1,6 @@
import type { Recipe } from '../types.ts';
import { AIConfigError } from '../errors.ts';
-import { execSync } from 'node:child_process';
+import { execSync } from '../../spawn.ts';
const DEFAULT_API_VERSION = '2024-10-21'; // stable Azure OpenAI version as of 2026-05
diff --git a/src/core/bootstrap/status.ts b/src/core/bootstrap/status.ts
index b8d5400204..ad1af68c9e 100644
--- a/src/core/bootstrap/status.ts
+++ b/src/core/bootstrap/status.ts
@@ -26,7 +26,7 @@
*/
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync } from 'node:fs';
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { join } from 'node:path';
import { VERSION } from '../../version.ts';
diff --git a/src/core/calibration/gstack-coupling.ts b/src/core/calibration/gstack-coupling.ts
index a014ed2221..5aac1e248e 100644
--- a/src/core/calibration/gstack-coupling.ts
+++ b/src/core/calibration/gstack-coupling.ts
@@ -24,7 +24,7 @@
* `gstack-learnings-prune` (Lane D / T17).
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { GBrainError } from '../types.ts';
export interface IncorrectResolutionEvent {
diff --git a/src/core/calibration/undo-wave.ts b/src/core/calibration/undo-wave.ts
index c7412b7bb5..9de115338e 100644
--- a/src/core/calibration/undo-wave.ts
+++ b/src/core/calibration/undo-wave.ts
@@ -39,7 +39,7 @@
* be reverted.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { GSTACK_LEARNING_NAMESPACE } from './gstack-coupling.ts';
import type { BrainEngine } from '../engine.ts';
import { maintenanceTransaction } from '../persistence/attribution.ts';
diff --git a/src/core/claw-test/agent-runner.ts b/src/core/claw-test/agent-runner.ts
index b39dfa125c..4262c741b3 100644
--- a/src/core/claw-test/agent-runner.ts
+++ b/src/core/claw-test/agent-runner.ts
@@ -16,7 +16,7 @@
* └────────────────────┘
*/
-import { execFileSync } from 'child_process';
+import { execFileSync } from '../spawn.ts';
import { statSync } from 'fs';
export interface AgentRunner {
diff --git a/src/core/claw-test/runners/grok.ts b/src/core/claw-test/runners/grok.ts
index a3fec81189..0fedcd19f7 100644
--- a/src/core/claw-test/runners/grok.ts
+++ b/src/core/claw-test/runners/grok.ts
@@ -40,7 +40,7 @@
* the transcript so a mis-bound binary is diagnosable from the transcript.
*/
-import { execFileSync } from 'child_process';
+import { execFileSync } from '../../spawn.ts';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import {
diff --git a/src/core/claw-test/runners/opencode.ts b/src/core/claw-test/runners/opencode.ts
index 24a5bd9766..8555bd13ad 100644
--- a/src/core/claw-test/runners/opencode.ts
+++ b/src/core/claw-test/runners/opencode.ts
@@ -38,7 +38,7 @@
* Binary resolution: $OPENCODE_BIN > `which opencode` > unavailable.
*/
-import { execFileSync } from 'child_process';
+import { execFileSync } from '../../spawn.ts';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import {
diff --git a/src/core/claw-test/transcript-capture.ts b/src/core/claw-test/transcript-capture.ts
index 7d428b4917..1a13f2c585 100644
--- a/src/core/claw-test/transcript-capture.ts
+++ b/src/core/claw-test/transcript-capture.ts
@@ -26,7 +26,7 @@
*/
import { createWriteStream, type WriteStream } from 'fs';
-import { spawn, type ChildProcess } from 'child_process';
+import { spawn, type ChildProcess } from '../spawn.ts';
import { dirname } from 'path';
import { mkdirSync, existsSync } from 'fs';
import type { TranscriptEvent, TranscriptSink } from './agent-runner.ts';
diff --git a/src/core/company-brain/revision.ts b/src/core/company-brain/revision.ts
index bc9891187e..e6e16ad07e 100644
--- a/src/core/company-brain/revision.ts
+++ b/src/core/company-brain/revision.ts
@@ -1,4 +1,4 @@
-import { spawn } from 'node:child_process';
+import { spawn } from '../spawn.ts';
import { createHash } from 'node:crypto';
import { constants, existsSync, lstatSync, openSync, fstatSync, readSync, closeSync, realpathSync } from 'node:fs';
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
diff --git a/src/core/context/compiled-core.ts b/src/core/context/compiled-core.ts
index 21788f13b1..21c71b5f7c 100644
--- a/src/core/context/compiled-core.ts
+++ b/src/core/context/compiled-core.ts
@@ -4,7 +4,7 @@
* of every compiled file that carries core, so `gbrain doctor` can name
* stale or leftover copies and the command that refreshes them.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { appendFileSync, existsSync, readFileSync } from 'node:fs';
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
import { atomicWriteTextFile } from '../bootstrap/atomic-write.ts';
diff --git a/src/core/cycle/derived-write-through.ts b/src/core/cycle/derived-write-through.ts
index 6c9a8b9ee2..dc3a7fafc2 100644
--- a/src/core/cycle/derived-write-through.ts
+++ b/src/core/cycle/derived-write-through.ts
@@ -14,7 +14,7 @@
* generated pages that have no file yet, a bounded batch per run. A page the
* phase retires has its file removed, so sync cannot bring it back.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { relative } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import { isDbOnly, loadStorageConfig } from '../storage-config.ts';
diff --git a/src/core/eval/drift-watch.ts b/src/core/eval/drift-watch.ts
index 27a4fd1a92..a3c661b91c 100644
--- a/src/core/eval/drift-watch.ts
+++ b/src/core/eval/drift-watch.ts
@@ -11,7 +11,7 @@
* so coverage grows deliberately.
*/
-import { execFileSync } from 'child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
diff --git a/src/core/facts/fence-write.ts b/src/core/facts/fence-write.ts
index d4304a00a8..573e1b2004 100644
--- a/src/core/facts/fence-write.ts
+++ b/src/core/facts/fence-write.ts
@@ -34,7 +34,7 @@
* sees the constraint.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync, appendFileSync } from 'node:fs';
import { dirname, isAbsolute, relative } from 'node:path';
diff --git a/src/core/fence-repair/census.ts b/src/core/fence-repair/census.ts
index fa591c40c0..056eb61605 100644
--- a/src/core/fence-repair/census.ts
+++ b/src/core/fence-repair/census.ts
@@ -23,7 +23,7 @@
* is done, the incremental pass caught up and the walk finished; anything
* less is `partial`, and a partial census never reads as healthy.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync, lstatSync, readFileSync, statSync } from 'node:fs';
import { join, relative, resolve } from 'node:path';
import type { BrainEngine } from '../engine.ts';
diff --git a/src/core/fence-repair/repair-io.ts b/src/core/fence-repair/repair-io.ts
index c817cb5edb..07cf38b1f4 100644
--- a/src/core/fence-repair/repair-io.ts
+++ b/src/core/fence-repair/repair-io.ts
@@ -26,7 +26,7 @@
*/
import { copyFileSync, existsSync, lstatSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import type { BrainEngine } from '../engine.ts';
import { OperationError, type OperationContext } from '../ops/contract.ts';
import { gbrainPath } from '../config.ts';
diff --git a/src/core/fence-repair/uncommitted.ts b/src/core/fence-repair/uncommitted.ts
index cf5aed43ae..6e5d5f1568 100644
--- a/src/core/fence-repair/uncommitted.ts
+++ b/src/core/fence-repair/uncommitted.ts
@@ -12,7 +12,7 @@
* incarnation, so the checkpoint purge keeps it while the source lives.
* Location, hashes and the fix classes only, never a cell value.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync } from 'node:fs';
import type { BrainEngine } from '../engine.ts';
import { shellQuote } from '../agent-output.ts';
diff --git a/src/core/google/access.ts b/src/core/google/access.ts
index c166b11aa2..95de035a19 100644
--- a/src/core/google/access.ts
+++ b/src/core/google/access.ts
@@ -25,7 +25,7 @@
* Same trust class as recipe health_check argv entries.
*/
-import { spawnSync } from 'node:child_process';
+import { spawnSync } from '../spawn.ts';
import { CredentialError } from '../creds/errors.ts';
diff --git a/src/core/repair/frontmatter.ts b/src/core/repair/frontmatter.ts
index 7c8e891a4a..fbddf4e110 100644
--- a/src/core/repair/frontmatter.ts
+++ b/src/core/repair/frontmatter.ts
@@ -37,7 +37,7 @@
* step. An apply refuses while an unfinished managed sync cursor still names
* a selected file.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync, lstatSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { maintenanceTransaction } from '../persistence/attribution.ts';
import { join } from 'node:path';
diff --git a/src/core/repair/slug-conflicts.ts b/src/core/repair/slug-conflicts.ts
index baee9a1383..99546b2b5b 100644
--- a/src/core/repair/slug-conflicts.ts
+++ b/src/core/repair/slug-conflicts.ts
@@ -45,7 +45,7 @@
* at apply and again at the write (file-repair.ts). Messages carry paths,
* slugs, codes and amounts only.
*/
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync, lstatSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import type { BrainEngine } from '../engine.ts';
diff --git a/src/core/shared-skills/migration-export.ts b/src/core/shared-skills/migration-export.ts
index 4bd3a79036..521986a4d5 100644
--- a/src/core/shared-skills/migration-export.ts
+++ b/src/core/shared-skills/migration-export.ts
@@ -1,7 +1,7 @@
import { randomUUID } from 'node:crypto';
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync } from 'node:fs';
import { dirname, isAbsolute, join, resolve } from 'node:path';
-import { spawnSync } from 'node:child_process';
+import { spawnSync } from '../spawn.ts';
import { opError, OperationError, type OperationContext } from '../ops/contract.ts';
import { hostOnlyError, readFix } from '../ops/op-fix.ts';
import type { Action } from '../agent-output.ts';
diff --git a/src/core/shared-skills/setup.ts b/src/core/shared-skills/setup.ts
index 80f19c5cbe..6bb673804b 100644
--- a/src/core/shared-skills/setup.ts
+++ b/src/core/shared-skills/setup.ts
@@ -1,6 +1,6 @@
import { existsSync, mkdirSync, readdirSync, statSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
-import { spawnSync } from 'node:child_process';
+import { spawnSync } from '../spawn.ts';
import { gbrainPath, isThinClient } from '../config.ts';
import { opError, type OperationContext } from '../ops/contract.ts';
import { readFix } from '../ops/op-fix.ts';
diff --git a/src/core/skillopt/apply-edits.ts b/src/core/skillopt/apply-edits.ts
index 423a868793..78dce70343 100644
--- a/src/core/skillopt/apply-edits.ts
+++ b/src/core/skillopt/apply-edits.ts
@@ -25,7 +25,7 @@
import { assertLegacySkillFilesystemWrite } from '../skillpack/writer-guard.ts';
import * as fs from 'node:fs';
-import { execFileSync } from 'node:child_process';
+import { execFileSync } from '../spawn.ts';
import { dirname } from 'node:path';
import type { EditOp, EditResult, EditRejectionReason } from './types.ts';
import { gitChildEnv } from '../git-env.ts';
diff --git a/src/core/skillpack/bundle.ts b/src/core/skillpack/bundle.ts
index 7551344220..0c1df965e3 100644
--- a/src/core/skillpack/bundle.ts
+++ b/src/core/skillpack/bundle.ts
@@ -361,7 +361,7 @@ export function changedSlugsSinceVersion(
): string[] | null {
// Synchronously execute git via Bun.spawnSync to avoid the async overhead
// and keep this callable from CLI dispatch without awaiting.
- const { spawnSync } = require('child_process') as typeof import('child_process');
+ const { spawnSync } = require('../spawn.ts') as typeof import('../spawn.ts');
// Probe git availability + repo state. `.git` may be a directory OR a file
// (worktrees). Either is fine for `git log`.
diff --git a/src/core/skillpack/endorse.ts b/src/core/skillpack/endorse.ts
index f6a696dbd8..36913d6fac 100644
--- a/src/core/skillpack/endorse.ts
+++ b/src/core/skillpack/endorse.ts
@@ -15,7 +15,7 @@
*/
import { assertLegacySkillFilesystemWrite } from './writer-guard.ts';
-import { execFileSync } from 'child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync, readFileSync, renameSync, writeFileSync } from 'fs';
import { join } from 'path';
diff --git a/src/core/skillpack/remote-source.ts b/src/core/skillpack/remote-source.ts
index bfc7e49460..a90afd4007 100644
--- a/src/core/skillpack/remote-source.ts
+++ b/src/core/skillpack/remote-source.ts
@@ -22,7 +22,7 @@
*/
import { assertLegacySkillFilesystemWrite } from './writer-guard.ts';
-import { execFileSync } from 'child_process';
+import { execFileSync } from '../spawn.ts';
import { existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync } from 'fs';
import { isAbsolute, join, resolve } from 'path';
diff --git a/src/core/skillpack/tarball.ts b/src/core/skillpack/tarball.ts
index ed352d4891..e07b1076f5 100644
--- a/src/core/skillpack/tarball.ts
+++ b/src/core/skillpack/tarball.ts
@@ -19,7 +19,7 @@
*/
import { assertLegacySkillFilesystemWrite } from './writer-guard.ts';
-import { spawnSync } from 'child_process';
+import { spawnSync } from '../spawn.ts';
import { createHash } from 'crypto';
import {
existsSync,