Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

test(fake-claude): match hooks by the CLI's documented rules; note which versions enforce an async hook's timeout

Absterrg05f31707ed4a0
diff --git a/server/drivers/claude-hooks.ts b/server/drivers/claude-hooks.ts
index 68bf372c37..af29bbc0f5 100644
--- a/server/drivers/claude-hooks.ts
+++ b/server/drivers/claude-hooks.ts
@@ -8,8 +8,8 @@ export function claudeHookSettings(helperPath: string, platform: NodeJS.Platform
? { type: "command", command: process.execPath, args: [helperPath], timeout: 5 }
: { type: "command", command: [process.execPath, helperPath].map(path => `'${path.replace(/'/g, "'\\''")}'`).join(" "), timeout: 5 };
// Claude Code holds a tool's result until its synchronous hooks exit, and every hook starts a process (on Windows the
- // whole Electron binary). An async hook (2.1.325) holds nothing but is still killed at its timeout, so it gets room for
- // a slow start. The matchers keep a process from starting for events whose reply the harness never sends.
+ // whole Electron binary). An async hook holds nothing; 2.1.308 still kills one at its timeout (2.1.325 does not), so it
+ // gets room for a slow start. The matchers keep a process from starting for events whose reply the harness never sends.
return {
PostToolUse: [{ matcher: "", hooks: [{ ...hook, async: true, timeout: 60 }] }],
PostToolUseFailure: [{ matcher: "Bash", hooks: [hook] }],
diff --git a/server/testing/claude-hook-matcher.test.ts b/server/testing/claude-hook-matcher.test.ts
new file mode 100644
index 0000000000..7952227534
--- /dev/null
+++ b/server/testing/claude-hook-matcher.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from "vitest";
+
+import { hookMatches } from "./claude-hook-matcher.ts";
+
+describe("hookMatches", () => {
+ it("matches every event with no matcher, an empty one or a star", () => {
+ for (const matcher of [undefined, "", "*"]) expect(hookMatches(matcher, "Read")).toBe(true);
+ });
+
+ it("compares plain names exactly, alone or in a list", () => {
+ expect(hookMatches("Bash", "Bash")).toBe(true);
+ expect(hookMatches("Bash", "BashOutput")).toBe(false);
+ expect(hookMatches("compact", "startup")).toBe(false);
+ expect(hookMatches("Edit, Write", "Write")).toBe(true);
+ expect(hookMatches("Edit|Write", "Edit")).toBe(true);
+ expect(hookMatches("Edit, Write", "NotebookEdit")).toBe(false);
+ });
+
+ it("tests any other matcher as an unanchored regular expression", () => {
+ expect(hookMatches("Edit.*", "NotebookEdit")).toBe(true);
+ expect(hookMatches("^Notebook", "NotebookEdit")).toBe(true);
+ expect(hookMatches("^Edit$", "NotebookEdit")).toBe(false);
+ });
+});
diff --git a/server/testing/claude-hook-matcher.ts b/server/testing/claude-hook-matcher.ts
new file mode 100644
index 0000000000..d07e75dba2
--- /dev/null
+++ b/server/testing/claude-hook-matcher.ts
@@ -0,0 +1,9 @@
+/** Whether a settings hook's `matcher` selects an event, as the Claude Code hooks reference describes it: "", "*" or none
+ * match all; a matcher of only letters, digits, `_`, `-`, spaces, `,` and `|` is an exact name or a list of them; any other
+ * is an unanchored JavaScript regular expression. */
+export function hookMatches(matcher: string | undefined, value: unknown): boolean {
+ if (!matcher || matcher === "*") return true;
+ const text = String(value ?? "");
+ if (/^[\w\s,|-]+$/.test(matcher)) return matcher.split(/[|,]/).some((name) => name.trim() === text);
+ return new RegExp(matcher).test(text);
+}
diff --git a/server/testing/fake-claude-cli.ts b/server/testing/fake-claude-cli.ts
index b2036daaa5..d1667ef456 100755
--- a/server/testing/fake-claude-cli.ts
+++ b/server/testing/fake-claude-cli.ts
@@ -96,6 +96,7 @@ import { spawn, spawnSync, type ChildProcess } from "node:child_process";
import { appendFileSync, closeSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
+import { hookMatches } from "./claude-hook-matcher.ts";
import { runRoomHandoffAgent } from "./room-handoff-agent.ts";
import { playStreamScript, streamScript } from "./stream-script.ts";
@@ -254,10 +255,8 @@ const settingsHooks: Record<string, Array<{ matcher?: string; hooks?: Array<{ ty
return {};
}
})();
-// Like Claude Code: a matcher tests the tool name, a session's source or a compaction's trigger; "" and "*" match all.
+// Like Claude Code: a matcher tests the tool name, a session's source or a compaction's trigger.
const HOOK_MATCHED_FIELD: Record<string, string> = { PostToolUse: "tool_name", PostToolUseFailure: "tool_name", SessionStart: "source", PreCompact: "trigger" };
-const hookMatches = (matcher: string | undefined, value: unknown) =>
- !matcher || matcher === "*" || new RegExp(`^(?:${matcher})$`).test(String(value ?? ""));
function runHooks(event: string, payload: Record<string, unknown>): string {
let stdout = "";
for (const entry of settingsHooks[event] ?? []) {