Files
moltbot/src/infra/gateway-process-argv.ts
Derek YU 5f6e3499f3 fix: detect PID recycling in gateway lock on Windows/macOS + startup progress (#59843)
Fix stale lock files from crashed gateway processes blocking new invocations on Windows/macOS. Detect PID recycling to avoid false positive lock conflicts, and add startup progress indicator.

Thanks @TonyDerek-dot
2026-04-02 22:07:35 -05:00

63 lines
1.5 KiB
TypeScript

function normalizeProcArg(arg: string): string {
return arg.replaceAll("\\", "/").toLowerCase();
}
export function parseProcCmdline(raw: string): string[] {
return raw
.split("\0")
.map((entry) => entry.trim())
.filter(Boolean);
}
/**
* Parse a Windows command line string into argv-style tokens,
* handling double-quoted paths (e.g. `"C:\Program Files\node.exe" gateway run`).
*/
export function parseWindowsCmdline(raw: string): string[] {
const args: string[] = [];
let current = "";
let inQuote = false;
for (const char of raw) {
if (char === '"') {
inQuote = !inQuote;
} else if (char === " " && !inQuote) {
if (current) {
args.push(current);
current = "";
}
} else {
current += char;
}
}
if (current) {
args.push(current);
}
return args;
}
export function isGatewayArgv(args: string[], opts?: { allowGatewayBinary?: boolean }): boolean {
const normalized = args.map(normalizeProcArg);
if (!normalized.includes("gateway")) {
return false;
}
const entryCandidates = [
"dist/index.js",
"dist/entry.js",
"openclaw.mjs",
"scripts/run-node.mjs",
"src/entry.ts",
"src/index.ts",
];
if (normalized.some((arg) => entryCandidates.some((entry) => arg.endsWith(entry)))) {
return true;
}
const exe = (normalized[0] ?? "").replace(/\.(bat|cmd|exe)$/i, "");
return (
exe.endsWith("/openclaw") ||
exe === "openclaw" ||
(opts?.allowGatewayBinary === true && exe.endsWith("/openclaw-gateway"))
);
}