Compare commits

...

3 Commits

Author SHA1 Message Date
Peter Steinberger
f2795754e9 fix: normalize Windows argv handling (#1564) (thanks @Takhoffman) 2026-01-24 07:10:23 +00:00
Peter Steinberger
4ab6263762 fix: normalize Windows argv (#1564) (thanks @Takhoffman) 2026-01-24 07:05:56 +00:00
Tak hoffman
d3df691684 CLI: fix Windows node argv stripping 2026-01-24 07:05:12 +00:00
7 changed files with 125 additions and 110 deletions

View File

@ -20,6 +20,7 @@ Docs: https://docs.clawd.bot
### Fixes ### Fixes
- Agents: ignore IDENTITY.md template placeholders when parsing identity to avoid placeholder replies. (#1556) - Agents: ignore IDENTITY.md template placeholders when parsing identity to avoid placeholder replies. (#1556)
- CLI: normalize Windows argv to drop duplicate node.exe entries before commands. (#1564) Thanks @Takhoffman.
- Docker: update gateway command in docker-compose and Hetzner guide. (#1514) - Docker: update gateway command in docker-compose and Hetzner guide. (#1514)
- Sessions: reject array-backed session stores to prevent silent wipes. (#1469) - Sessions: reject array-backed session stores to prevent silent wipes. (#1469)
- Voice wake: auto-save wake words on blur/submit across iOS/Android and align limits with macOS. - Voice wake: auto-save wake words on blur/submit across iOS/Android and align limits with macOS.

View File

@ -90,6 +90,12 @@ function safeJsonStringify(value: unknown): string | null {
} }
} }
function formatError(error: unknown): string {
if (typeof error === "string") return error;
if (error instanceof Error) return error.stack ?? error.message;
return safeJsonStringify(error) ?? "[unknown error]";
}
function digest(value: unknown): string | undefined { function digest(value: unknown): string | undefined {
const serialized = safeJsonStringify(value); const serialized = safeJsonStringify(value);
if (!serialized) return undefined; if (!serialized) return undefined;
@ -163,7 +169,7 @@ export function createAnthropicPayloadLogger(params: {
options?.onPayload?.(payload); options?.onPayload?.(payload);
}; };
return streamFn(model, context, { return streamFn(model, context, {
...(options ?? {}), ...options,
onPayload: nextOnPayload, onPayload: nextOnPayload,
}); });
}; };
@ -178,7 +184,7 @@ export function createAnthropicPayloadLogger(params: {
...base, ...base,
ts: new Date().toISOString(), ts: new Date().toISOString(),
stage: "usage", stage: "usage",
error: String(error), error: formatError(error),
}); });
} }
return; return;
@ -188,7 +194,7 @@ export function createAnthropicPayloadLogger(params: {
ts: new Date().toISOString(), ts: new Date().toISOString(),
stage: "usage", stage: "usage",
usage, usage,
error: error ? String(error) : undefined, error: error ? formatError(error) : undefined,
}); });
log.info("anthropic usage", { log.info("anthropic usage", {
runId: params.runId, runId: params.runId,

View File

@ -1,4 +1,3 @@
import path from "node:path";
import process from "node:process"; import process from "node:process";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
@ -12,6 +11,7 @@ import { installUnhandledRejectionHandler } from "../infra/unhandled-rejections.
import { enableConsoleCapture } from "../logging.js"; import { enableConsoleCapture } from "../logging.js";
import { getPrimaryCommand, hasHelpOrVersion } from "./argv.js"; import { getPrimaryCommand, hasHelpOrVersion } from "./argv.js";
import { tryRouteCli } from "./route.js"; import { tryRouteCli } from "./route.js";
import { normalizeWindowsArgv } from "./windows-argv.js";
export function rewriteUpdateFlagArgv(argv: string[]): string[] { export function rewriteUpdateFlagArgv(argv: string[]): string[] {
const index = argv.indexOf("--update"); const index = argv.indexOf("--update");
@ -23,7 +23,7 @@ export function rewriteUpdateFlagArgv(argv: string[]): string[] {
} }
export async function runCli(argv: string[] = process.argv) { export async function runCli(argv: string[] = process.argv) {
const normalizedArgv = stripWindowsNodeExec(argv); const normalizedArgv = normalizeWindowsArgv(argv);
loadDotEnv({ quiet: true }); loadDotEnv({ quiet: true });
normalizeEnv(); normalizeEnv();
ensureClawdbotCliOnPath(); ensureClawdbotCliOnPath();
@ -59,50 +59,6 @@ export async function runCli(argv: string[] = process.argv) {
await program.parseAsync(parseArgv); await program.parseAsync(parseArgv);
} }
function stripWindowsNodeExec(argv: string[]): string[] {
if (process.platform !== "win32") return argv;
const stripControlChars = (value: string): string => {
let out = "";
for (let i = 0; i < value.length; i += 1) {
const code = value.charCodeAt(i);
if (code >= 32 && code !== 127) {
out += value[i];
}
}
return out;
};
const normalizeArg = (value: string): string =>
stripControlChars(value)
.replace(/^['"]+|['"]+$/g, "")
.trim();
const normalizeCandidate = (value: string): string =>
normalizeArg(value).replace(/^\\\\\\?\\/, "");
const execPath = normalizeCandidate(process.execPath);
const execPathLower = execPath.toLowerCase();
const execBase = path.basename(execPath).toLowerCase();
const isExecPath = (value: string | undefined): boolean => {
if (!value) return false;
const lower = normalizeCandidate(value).toLowerCase();
return (
lower === execPathLower ||
path.basename(lower) === execBase ||
lower.endsWith("\\node.exe") ||
lower.endsWith("/node.exe") ||
lower.includes("node.exe")
);
};
const filtered = argv.filter((arg, index) => index === 0 || !isExecPath(arg));
if (filtered.length < 3) return filtered;
const cleaned = [...filtered];
if (isExecPath(cleaned[1])) {
cleaned.splice(1, 1);
}
if (isExecPath(cleaned[2])) {
cleaned.splice(2, 1);
}
return cleaned;
}
export function isCliMainModule(): boolean { export function isCliMainModule(): boolean {
return isMainModule({ currentFile: fileURLToPath(import.meta.url) }); return isMainModule({ currentFile: fileURLToPath(import.meta.url) });
} }

View File

@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { normalizeWindowsArgv } from "./windows-argv.js";
describe("normalizeWindowsArgv", () => {
const execPath = "C:\\Program Files\\nodejs\\node.exe";
const scriptPath = "C:\\clawdbot\\dist\\entry.js";
it("returns argv unchanged on non-windows platforms", () => {
const argv = [execPath, scriptPath, "status"];
expect(normalizeWindowsArgv(argv, { platform: "darwin", execPath })).toBe(argv);
});
it("removes duplicate node exec at argv[1]", () => {
const argv = [execPath, execPath, scriptPath, "status"];
expect(normalizeWindowsArgv(argv, { platform: "win32", execPath })).toEqual([
execPath,
scriptPath,
"status",
]);
});
it("removes duplicate node exec at argv[2]", () => {
const argv = [execPath, scriptPath, execPath, "gateway", "run"];
expect(normalizeWindowsArgv(argv, { platform: "win32", execPath })).toEqual([
execPath,
scriptPath,
"gateway",
"run",
]);
});
it("keeps url arguments that contain node.exe", () => {
const argv = [execPath, scriptPath, "send", "https://example.com/node.exe"];
expect(normalizeWindowsArgv(argv, { platform: "win32", execPath })).toEqual(argv);
});
it("keeps node.exe paths after the command", () => {
const argv = [execPath, scriptPath, "send", "C:\\Program Files\\nodejs\\node.exe"];
expect(normalizeWindowsArgv(argv, { platform: "win32", execPath })).toEqual(argv);
});
});

55
src/cli/windows-argv.ts Normal file
View File

@ -0,0 +1,55 @@
import path from "node:path";
import process from "node:process";
type WindowsArgvOptions = {
platform?: NodeJS.Platform;
execPath?: string;
};
export function normalizeWindowsArgv(
argv: string[],
{ platform = process.platform, execPath = process.execPath }: WindowsArgvOptions = {},
): string[] {
if (platform !== "win32") return argv;
if (argv.length < 2) return argv;
const stripControlChars = (value: string): string => {
let out = "";
for (let i = 0; i < value.length; i += 1) {
const code = value.charCodeAt(i);
if (code >= 32 && code !== 127) {
out += value[i];
}
}
return out;
};
const normalizeArg = (value: string): string =>
stripControlChars(value)
.replace(/^['"]+|['"]+$/g, "")
.trim();
const normalizeCandidate = (value: string): string =>
normalizeArg(value).replace(/^\\\\\\?\\/, "");
const execPathNormalized = normalizeCandidate(execPath);
const execPathLower = execPathNormalized.toLowerCase();
const execBaseLower = path.basename(execPathLower);
const isNodeExecPath = (value: string | undefined): boolean => {
if (!value) return false;
const normalized = normalizeCandidate(value);
if (!normalized) return false;
if (normalized.includes("://")) return false;
const lower = normalized.toLowerCase();
if (lower === execPathLower || lower === execBaseLower) return true;
if (!lower.endsWith("node.exe")) return false;
return path.isAbsolute(normalized) || normalized.includes("\\") || normalized.includes("/");
};
const next = [...argv];
for (let i = 1; i <= 2 && i < next.length; ) {
if (isNodeExecPath(next[i])) {
next.splice(i, 1);
continue;
}
i += 1;
}
return next;
}

View File

@ -1,9 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
import { spawn } from "node:child_process"; import { spawn } from "node:child_process";
import path from "node:path";
import process from "node:process"; import process from "node:process";
import { applyCliProfileEnv, parseCliProfileArgs } from "./cli/profile.js"; import { applyCliProfileEnv, parseCliProfileArgs } from "./cli/profile.js";
import { normalizeWindowsArgv } from "./cli/windows-argv.js";
import { isTruthyEnvValue } from "./infra/env.js"; import { isTruthyEnvValue } from "./infra/env.js";
import { attachChildProcessBridge } from "./process/child-process-bridge.js"; import { attachChildProcessBridge } from "./process/child-process-bridge.js";
@ -57,65 +57,6 @@ function ensureExperimentalWarningSuppressed(): boolean {
return true; return true;
} }
function normalizeWindowsArgv(argv: string[]): string[] {
if (process.platform !== "win32") return argv;
if (argv.length < 2) return argv;
const stripControlChars = (value: string): string => {
let out = "";
for (let i = 0; i < value.length; i += 1) {
const code = value.charCodeAt(i);
if (code >= 32 && code !== 127) {
out += value[i];
}
}
return out;
};
const normalizeArg = (value: string): string =>
stripControlChars(value)
.replace(/^['"]+|['"]+$/g, "")
.trim();
const normalizeCandidate = (value: string): string =>
normalizeArg(value).replace(/^\\\\\\?\\/, "");
const execPath = normalizeCandidate(process.execPath);
const execPathLower = execPath.toLowerCase();
const execBase = path.basename(execPath).toLowerCase();
const isExecPath = (value: string | undefined): boolean => {
if (!value) return false;
const lower = normalizeCandidate(value).toLowerCase();
return (
lower === execPathLower ||
path.basename(lower) === execBase ||
lower.endsWith("\\node.exe") ||
lower.endsWith("/node.exe") ||
lower.includes("node.exe")
);
};
const next = [...argv];
for (let i = 1; i <= 3 && i < next.length; ) {
if (isExecPath(next[i])) {
next.splice(i, 1);
continue;
}
i += 1;
}
const filtered = next.filter((arg, index) => index === 0 || !isExecPath(arg));
if (filtered.length < 3) return filtered;
const cleaned = [...filtered];
for (let i = 2; i < cleaned.length; ) {
const arg = cleaned[i];
if (!arg || arg.startsWith("-")) {
i += 1;
continue;
}
if (isExecPath(arg)) {
cleaned.splice(i, 1);
continue;
}
break;
}
return cleaned;
}
process.argv = normalizeWindowsArgv(process.argv); process.argv = normalizeWindowsArgv(process.argv);
if (!ensureExperimentalWarningSuppressed()) { if (!ensureExperimentalWarningSuppressed()) {

View File

@ -194,8 +194,22 @@ function sanitizeArgs(args: string | undefined): string | undefined {
return args.slice(0, MAX_ARGS_LENGTH); return args.slice(0, MAX_ARGS_LENGTH);
} }
const stripControlChars = (value: string): string => {
let out = "";
for (let i = 0; i < value.length; i += 1) {
const code = value.charCodeAt(i);
if (code === 9 || code === 10) {
out += value[i];
continue;
}
if (code < 32 || code === 127) continue;
out += value[i];
}
return out;
};
// Remove control characters (except newlines and tabs which may be intentional) // Remove control characters (except newlines and tabs which may be intentional)
return args.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, ""); return stripControlChars(args);
} }
/** /**