From 2636ba4c7b17657a4639abb00d9ad2fe27c6e365 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 20 Nov 2026 23:57:25 +0800 Subject: [PATCH] fix(hooks): preserve prompt payloads and release evaluation timers --- CHANGELOG.md | 2 ++ README.md | 2 +- src/index.test.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 31 ++++++++++++++++++------------- 4 files changed, 67 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cbe98b..406e78c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ - Use the default Hermes deadline when the configured value exceeds the maximum Node timer. +- Preserve literal event data in Claude prompt-hook templates and clear evaluation timers when the host fails. + - Report tool installation, fresh package installation, lifecycle checks, and cleanup times in the package-consumer log. - Load the MCP SDK when a client or server starts, reducing startup time for everyday CLI commands and plugin registration. diff --git a/README.md b/README.md index 7e18958..dd00a30 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Installing an already installed name requires `--force`; a rejected duplicate le | Agent and MCP tools | Native tools that retain the source JSON schema. MCP inspection follows at most 50 tool pages within one request-timeout budget and rejects repeated cursors. | | Skills and support files | Hermes skills and native tools use the same plugin listing during a rebuild. Native OpenClaw skills with referenced files intact; Markdown frontmatter supports LF and Windows CRLF. Generated command, agent, and output-style descriptions support literal and folded YAML blocks, header comments, one-digit indentation indicators, and clip/strip/keep chomping. Copied skill bodies and assets remain intact. In-root directory names such as `..skills` are supported. | | Prompt and terminal commands | User-invoked skills or top-level CLI commands when the source format supports them. OpenClaw discovers the management and generated Hermes CLI commands before starting the plugin runtime. Hermes MCP command tools accept optional `args`: omitted arguments, `{}`, or an empty `args` string pass `""`; explicit text is preserved. | -| Compatible hooks | Matching lifecycle, prompt, tool, compaction, and subagent hooks. Discovery permits 50 unique hook files and eight nested directories. Command-hook stdin is capped at 1 MiB; oversized decision events fail closed. String commands use a login shell; `args` or a `command` array spawn without a shell. Single-quoted names inside `$(...)` and backticks are inserted. Windows copies those inserted values without a POSIX apostrophe escape. | +| Compatible hooks | Matching lifecycle, prompt, tool, compaction, and subagent hooks. Claude prompt hooks insert the event JSON literally at each `$ARGUMENTS` placeholder. Discovery permits 50 unique hook files and eight nested directories. Command-hook stdin is capped at 1 MiB; oversized decision events fail closed. String commands use a login shell; `args` or a `command` array spawn without a shell. Single-quoted names inside `$(...)` and backticks are inserted. Windows copies those inserted values without a POSIX apostrophe escape. | | Compatible middleware | Request rewrites map to prompt and tool hooks. Hermes `transform_tool_result` maps to OpenClaw tool-result middleware. Terminal-only output hooks produce a startup warning. | | Hermes availability | Cancelling a request after a local encoding failure preserves the active Hermes session. An absent or empty install needs no Python for discovery or hooks; installed or unknown providers still require it, and failed pre-tool guards prevent execution. Hermes timeouts above the Node timer limit use the default 120 seconds. | | Session-start context | Turn preparation waits for an in-flight session start. A failed start warns once instead of poisoning later turns; successful start context is consumed once. Concurrent prepares cannot bypass a pending start or remove a newer one. | diff --git a/src/index.test.ts b/src/index.test.ts index 9b5e8a4..c5167f0 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -36,6 +36,52 @@ async function writeNodeMonitor(root: string, script: string, ...args: string[]) } describe("native OpenClaw hook entry", () => { + it("inserts literal event data in prompt hooks and releases their deadlines", async () => { + const root = await tempDir("babelfish-prompt-evaluation-"); + const manifestDir = path.join(root, "claude-code", "prompts", ".claude-plugin"); + await fs.mkdir(manifestDir, { recursive: true }); + await fs.writeFile(path.join(manifestDir, "plugin.json"), JSON.stringify({ + hooks: { Stop: [{ hooks: [{ type: "prompt", prompt: "[$ARGUMENTS,$ARGUMENTS]", timeout: 37 }] }] }, + })); + vi.stubEnv("OPENCLAW_BABELFISH_ROOT", root); + vi.stubEnv("OPENCLAW_BABELFISH_HERMES_PLUGIN_DIR", path.join(root, "hermes")); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + const scheduled = vi.spyOn(globalThis, "setTimeout"); + const cleared = vi.spyOn(globalThis, "clearTimeout"); + const complete = vi.fn(async (_params: { messages: { content: string }[] }) => ({ text: '{"ok":true}' })); + try { + vi.resetModules(); + const entry = (await import("./index.js")).default; + const hooks = new Map unknown>(); + entry.register({ + ...hookTestApi(hooks), + config: { plugins: { entries: { babelfish: { llm: { allowAgentIdOverride: true, allowModelOverride: true } } } } }, + runtime: { llm: { complete } }, + }); + const stop = hooks.get("before_agent_finalize")!; + const value = "$& $$ $` $'"; + const context = { agentId: "agent", modelId: "model" }; + await stop({ value }, context); + const prompt = complete.mock.calls[0][0].messages[0].content; + expect(JSON.parse(prompt)).toEqual([expect.objectContaining({ value }), expect.objectContaining({ value })]); + + await stop({}, {}); + expect(warn).toHaveBeenCalledWith(expect.stringContaining("requires active OpenClaw agent and model context")); + expect(complete).toHaveBeenCalledOnce(); + complete.mockImplementationOnce(() => { throw new Error("host failed"); }); + await stop({}, context); + expect(warn).toHaveBeenCalledWith(expect.stringContaining("host failed")); + const deadlines = scheduled.mock.results.filter((_, index) => scheduled.mock.calls[index][1] === 37_000); + expect(deadlines).toHaveLength(2); + for (const timer of deadlines) expect(cleared).toHaveBeenCalledWith(timer.value); + } finally { + scheduled.mockRestore(); + cleared.mockRestore(); + warn.mockRestore(); + vi.unstubAllEnvs(); + } + }); + it("keeps imported guards active when Hermes is empty and Python is missing", async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), "babelfish-no-hermes-")); const plugin = path.join(root, "codex", "guard"); diff --git a/src/index.ts b/src/index.ts index 5a8f336..67f9556 100644 --- a/src/index.ts +++ b/src/index.ts @@ -330,10 +330,8 @@ function configurePromptHooks(api: OpenClawApi): void { promptHookEvaluator = async (template, payload, timeoutMs) => { const argumentsJson = JSON.stringify(payload); const prompt = template.includes("$ARGUMENTS") - ? template.replaceAll("$ARGUMENTS", argumentsJson) + ? template.replaceAll("$ARGUMENTS", () => argumentsJson) : `${template}\n\n${argumentsJson}`; - const controller = new AbortController(); - const timer = setTimeout(() => controller.abort(), timeoutMs); const model = typeof payload.model === "string" && payload.model ? payload.model : undefined; const agentId = typeof payload.agent_id === "string" && payload.agent_id ? payload.agent_id @@ -341,16 +339,23 @@ function configurePromptHooks(api: OpenClawApi): void { if (!model || !agentId) { throw new Error("Prompt hook requires active OpenClaw agent and model context"); } - const response = await api.runtime!.llm!.complete({ - messages: [{ role: "user", content: prompt }], - maxTokens: 300, - temperature: 0, - systemPrompt: 'Evaluate the hook instructions. Return only JSON: {"ok":true} to allow, or {"ok":false,"reason":"..."} to block.', - purpose: "babelfish-hook-evaluation", - signal: controller.signal, - model, - agentId, - }).finally(() => clearTimeout(timer)); + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), timeoutMs); + let response; + try { + response = await api.runtime!.llm!.complete({ + messages: [{ role: "user", content: prompt }], + maxTokens: 300, + temperature: 0, + systemPrompt: 'Evaluate the hook instructions. Return only JSON: {"ok":true} to allow, or {"ok":false,"reason":"..."} to block.', + purpose: "babelfish-hook-evaluation", + signal: controller.signal, + model, + agentId, + }); + } finally { + clearTimeout(timer); + } const match = response.text.match(/\{[\s\S]*\}/); const decision = match ? record(JSON.parse(match[0])) : {}; if (decision.ok === true) return undefined;