Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
257 lines
6.7 KiB
JavaScript
257 lines
6.7 KiB
JavaScript
// Runs a command with inline KEY=value assignments while preserving signal behavior.
|
|
import { spawn, spawnSync } from "node:child_process";
|
|
import { resolveWindowsTaskkillPath } from "./lib/windows-taskkill.mjs";
|
|
|
|
const ENV_ASSIGNMENT_RE = /^[A-Za-z_][A-Za-z0-9_]*=/u;
|
|
const USAGE = "Usage: node scripts/run-with-env.mjs KEY=value [KEY=value ...] -- command [args...]";
|
|
const MAX_TIMER_TIMEOUT_MS = 2_147_000_000;
|
|
|
|
/**
|
|
* Detects help requests before the command separator.
|
|
*/
|
|
export function isRunWithEnvHelpRequest(argv) {
|
|
for (const arg of argv) {
|
|
if (arg === "--") {
|
|
return false;
|
|
}
|
|
if (arg === "--help" || arg === "-h") {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Parses KEY=value assignments and the command following --.
|
|
*/
|
|
export function parseRunWithEnvArgs(argv) {
|
|
const separatorIndex = argv.indexOf("--");
|
|
if (separatorIndex <= 0 || separatorIndex === argv.length - 1) {
|
|
throw new Error(USAGE);
|
|
}
|
|
|
|
const assignments = argv.slice(0, separatorIndex);
|
|
const env = {};
|
|
for (const assignment of assignments) {
|
|
if (!ENV_ASSIGNMENT_RE.test(assignment)) {
|
|
throw new Error(`invalid environment assignment: ${assignment}`);
|
|
}
|
|
const equalsIndex = assignment.indexOf("=");
|
|
env[assignment.slice(0, equalsIndex)] = assignment.slice(equalsIndex + 1);
|
|
}
|
|
|
|
return {
|
|
env,
|
|
command: argv[separatorIndex + 1],
|
|
args: argv.slice(separatorIndex + 2),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Resolves node to the current executable so wrapper and child use the same runtime.
|
|
*/
|
|
export function resolveSpawnCommand(command, args, execPath = process.execPath) {
|
|
if (command === "node") {
|
|
return {
|
|
command: execPath,
|
|
args,
|
|
};
|
|
}
|
|
return {
|
|
command,
|
|
args,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Reads the signal-forwarding force-kill grace period.
|
|
*/
|
|
export function resolveForceKillDelayMs(env = process.env) {
|
|
const raw = env.OPENCLAW_RUN_WITH_ENV_FORCE_KILL_MS;
|
|
if (raw === undefined || raw === "") {
|
|
return 5_000;
|
|
}
|
|
const text = raw.trim();
|
|
if (!/^\d+$/u.test(text)) {
|
|
throw new Error("OPENCLAW_RUN_WITH_ENV_FORCE_KILL_MS must be a positive integer");
|
|
}
|
|
const parsed = Number(text);
|
|
if (!Number.isSafeInteger(parsed) || parsed < 1) {
|
|
throw new Error("OPENCLAW_RUN_WITH_ENV_FORCE_KILL_MS must be a positive integer");
|
|
}
|
|
return Math.min(parsed, MAX_TIMER_TIMEOUT_MS);
|
|
}
|
|
|
|
/**
|
|
* Signals the wrapped command tree when this small parent wrapper is stopped.
|
|
*/
|
|
export function signalRunWithEnvChild(
|
|
child,
|
|
signal,
|
|
{
|
|
platform = process.platform,
|
|
runTaskkill = spawnSync,
|
|
useChildProcessGroup = platform !== "win32",
|
|
} = {},
|
|
) {
|
|
if (useChildProcessGroup && typeof child.pid === "number") {
|
|
try {
|
|
process.kill(-child.pid, signal);
|
|
return;
|
|
} catch (error) {
|
|
if (error?.code !== "ESRCH") {
|
|
child.kill(signal);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (platform === "win32" && typeof child.pid === "number") {
|
|
const taskkillPath = resolveWindowsTaskkillPath();
|
|
const args = ["/PID", String(child.pid), "/T"];
|
|
if (signal === "SIGKILL") {
|
|
args.push("/F");
|
|
}
|
|
const result = runTaskkill(taskkillPath, args, { stdio: "ignore" });
|
|
if (!result?.error && result?.status === 0) {
|
|
return;
|
|
}
|
|
if (signal !== "SIGKILL") {
|
|
const forceResult = runTaskkill(taskkillPath, [...args, "/F"], { stdio: "ignore" });
|
|
if (!forceResult?.error && forceResult?.status === 0) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
child.kill(signal);
|
|
}
|
|
|
|
function main(argv = process.argv.slice(2)) {
|
|
if (isRunWithEnvHelpRequest(argv)) {
|
|
console.log(USAGE);
|
|
return;
|
|
}
|
|
|
|
let parsed;
|
|
try {
|
|
parsed = parseRunWithEnvArgs(argv);
|
|
} catch (error) {
|
|
console.error(error instanceof Error ? error.message : String(error));
|
|
process.exit(2);
|
|
}
|
|
|
|
let forceKillDelayMs;
|
|
try {
|
|
forceKillDelayMs = resolveForceKillDelayMs();
|
|
} catch (error) {
|
|
console.error(error instanceof Error ? error.message : String(error));
|
|
process.exit(2);
|
|
}
|
|
|
|
const spawnCommand = resolveSpawnCommand(parsed.command, parsed.args);
|
|
const useChildProcessGroup = process.platform !== "win32" && !process.stdin.isTTY;
|
|
const child = spawn(spawnCommand.command, spawnCommand.args, {
|
|
detached: useChildProcessGroup,
|
|
env: {
|
|
...process.env,
|
|
...parsed.env,
|
|
},
|
|
stdio: "inherit",
|
|
});
|
|
let forwardedSignal = null;
|
|
let forceKillTimer = null;
|
|
// Keep the child in the foreground process group so TTY signals such as
|
|
// Ctrl-C, Ctrl-Z, and window resizes stay native. Forward direct wrapper
|
|
// shutdown signals that would otherwise only kill this small parent process.
|
|
const forwardedSignals = useChildProcessGroup
|
|
? ["SIGTERM", "SIGHUP", "SIGINT"]
|
|
: ["SIGTERM", "SIGHUP"];
|
|
const signalChild = (signal) => signalRunWithEnvChild(child, signal, { useChildProcessGroup });
|
|
const childProcessGroupAlive = () => {
|
|
if (!useChildProcessGroup || typeof child.pid !== "number") {
|
|
return false;
|
|
}
|
|
try {
|
|
process.kill(-child.pid, 0);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
const exitWithForwardedSignal = () => {
|
|
if (!forwardedSignal) {
|
|
return;
|
|
}
|
|
const finish = () => {
|
|
if (forceKillTimer) {
|
|
clearTimeout(forceKillTimer);
|
|
}
|
|
process.kill(process.pid, forwardedSignal);
|
|
};
|
|
if (!childProcessGroupAlive()) {
|
|
finish();
|
|
return;
|
|
}
|
|
const deadline = Date.now() + forceKillDelayMs;
|
|
const drainTimer = setInterval(() => {
|
|
if (!childProcessGroupAlive()) {
|
|
clearInterval(drainTimer);
|
|
finish();
|
|
return;
|
|
}
|
|
if (Date.now() >= deadline) {
|
|
clearInterval(drainTimer);
|
|
signalChild("SIGKILL");
|
|
finish();
|
|
}
|
|
}, 50);
|
|
};
|
|
|
|
const cleanupSignalHandlers = () => {
|
|
for (const signal of forwardedSignals) {
|
|
process.off(signal, signalHandlers.get(signal));
|
|
}
|
|
};
|
|
const signalHandlers = new Map(
|
|
forwardedSignals.map((signal) => [
|
|
signal,
|
|
() => {
|
|
forwardedSignal ??= signal;
|
|
signalChild(signal);
|
|
forceKillTimer ??= setTimeout(() => signalChild("SIGKILL"), forceKillDelayMs);
|
|
},
|
|
]),
|
|
);
|
|
for (const [signal, handler] of signalHandlers) {
|
|
process.on(signal, handler);
|
|
}
|
|
|
|
child.on("exit", (code, signal) => {
|
|
cleanupSignalHandlers();
|
|
if (forwardedSignal) {
|
|
exitWithForwardedSignal();
|
|
return;
|
|
}
|
|
if (forceKillTimer) {
|
|
clearTimeout(forceKillTimer);
|
|
}
|
|
if (signal) {
|
|
process.kill(process.pid, signal);
|
|
return;
|
|
}
|
|
process.exit(code ?? 1);
|
|
});
|
|
|
|
child.on("error", (error) => {
|
|
cleanupSignalHandlers();
|
|
if (forceKillTimer) {
|
|
clearTimeout(forceKillTimer);
|
|
}
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
main();
|
|
}
|