Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Start Claude's hook helper without a shell on Windows
Klaus Singer87cc269bc809
diff --git a/server/drivers/claude.test.ts b/server/drivers/claude.test.tsindex d33d86e712..043017d9a2 100644--- a/server/drivers/claude.test.ts+++ b/server/drivers/claude.test.ts@@ -92,13 +92,14 @@ const CONTROL_PLANE_FIXTURE = {describe("ClaudeDriver.decodeConfig", () => {it("quotes hook paths as shell data rather than JSON strings", () => {- const settings = claudeHookSettings("/tmp/it's $OMB_HOOK_TEST `literal`/helper.ts") as { PostToolUse: Array<{ hooks: Array<{ command: string }> }> };- const command = settings.PostToolUse[0]!.hooks[0]!.command;- if (process.platform === "win32") {- expect(command).toBe('"%OMB_HOOK_NODE%" "%OMB_HOOK_HELPER%"');- } else {- expect(command).toContain("'/tmp/it'\\''s $OMB_HOOK_TEST `literal`/helper.ts'");- }+ const settings = claudeHookSettings("/tmp/it's $OMB_HOOK_TEST `literal`/helper.ts", "linux") as { PostToolUse: Array<{ hooks: Array<{ command: string }> }> };+ expect(settings.PostToolUse[0]!.hooks[0]!.command).toContain("'/tmp/it'\\''s $OMB_HOOK_TEST `literal`/helper.ts'");+ });++ it("starts the hook helper without a shell on Windows, where Git Bash or PowerShell would read the command line", () => {+ const helper = "C:\\Users\\it's $OMB_HOOK_TEST `literal`\\helper.ts";+ const settings = claudeHookSettings(helper, "win32") as { PostToolUse: Array<{ hooks: unknown[] }> };+ expect(settings.PostToolUse[0]!.hooks).toEqual([{ type: "command", command: process.execPath, args: [helper], timeout: 5 }]);});it("hooks failed tool calls too, so a timed-out command can be given a hint", () => {diff --git a/server/drivers/claude.ts b/server/drivers/claude.tsindex 8c58a11eb7..830031e6fd 100644--- a/server/drivers/claude.ts+++ b/server/drivers/claude.ts@@ -418,15 +418,16 @@ export function hookTokenFile(threadId: string, botId?: string): string {return join(DATA_DIR, "hook-tokens", digest, "token");}-/** Hooks for the private --settings file. Claude Code runs each command as a shell line with the- * event JSON on stdin and applies any hookSpecificOutput it prints. */-export function claudeHookSettings(helperPath: string): Record<string, unknown> {+/** Hooks for the private --settings file. Claude Code runs each command with the event JSON on+ * stdin and applies any hookSpecificOutput it prints. */+export function claudeHookSettings(helperPath: string, platform: NodeJS.Platform = process.platform): Record<string, unknown> {+ // Claude Code runs `command` as a shell line: sh on macOS and Linux; on Windows Git Bash, or PowerShell+ // when Git Bash is missing, and the two quote differently. Given `args` (2.1.139), it starts `command` itself.// JSON quoting is not shell quoting: $(), backticks and $names expand inside double quotes.- // Windows paths go through env so their backslashes are not JSON-escaped.- const command = process.platform === "win32"- ? '"%OMB_HOOK_NODE%" "%OMB_HOOK_HELPER%"'- : [process.execPath, helperPath].map(path => `'${path.replace(/'/g, "'\\''")}'`).join(" ");- const entry = [{ matcher: "", hooks: [{ type: "command", command, timeout: 5 }] }];+ const hook = platform === "win32"+ ? { type: "command", command: process.execPath, args: [helperPath], timeout: 5 }+ : { type: "command", command: [process.execPath, helperPath].map(path => `'${path.replace(/'/g, "'\\''")}'`).join(" "), timeout: 5 };+ const entry = [{ matcher: "", hooks: [hook] }];return { PostToolUse: entry, PostToolUseFailure: entry, PreCompact: entry, SessionStart: entry, Stop: entry };}@@ -1161,8 +1162,6 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {writeFileAtomicIfChanged(hookTokenPath, hooks.token, { mode: 0o600, durable: false });env.OMB_HOOK_URL = hooks.url;env.OMB_HOOK_TOKEN_FILE = hookTokenPath;- env.OMB_HOOK_NODE = process.execPath;- env.OMB_HOOK_HELPER = HOOK_HELPER_PATH;// in the packaged app process.execPath is Electron — run the helper as nodeif (process.versions.electron) env.ELECTRON_RUN_AS_NODE = "1";}diff --git a/server/testing/fake-claude-cli.ts b/server/testing/fake-claude-cli.tsindex c22687c97e..e13886ebbb 100755--- a/server/testing/fake-claude-cli.ts+++ b/server/testing/fake-claude-cli.ts@@ -225,7 +225,7 @@ const replyForPrompt = (prompt: JsonValue): string[] | null => {};const argv = process.argv.slice(2);-const settingsHooks: Record<string, Array<{ hooks?: Array<{ type?: string; command?: string; timeout?: number }> }>> = (() => {+const settingsHooks: Record<string, Array<{ hooks?: Array<{ type?: string; command?: string; args?: string[]; timeout?: number }> }>> = (() => {if (process.env.FAKE_CLAUDE_HOOKS !== "1") return {};const i = argv.indexOf("--settings");if (i === -1) return {};@@ -241,8 +241,9 @@ function runHooks(event: string, payload: Record<string, unknown>): string {for (const entry of settingsHooks[event] ?? []) {for (const hook of entry.hooks ?? []) {if (hook.type !== "command" || !hook.command) continue;- const result = spawnSync(hook.command, {- shell: true,+ // Like Claude Code: a hook with `args` starts `command` itself; otherwise `command` is a shell line.+ const result = spawnSync(hook.command, hook.args ?? [], {+ shell: !hook.args,input: JSON.stringify({ hook_event_name: event, session_id: "fake-session", cwd: process.cwd(), ...payload }),env: process.env,timeout: ((hook.timeout ?? 5) + 1) * 1000,