Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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. Fixture preparation and each source app's install have separate timings.

- Load the MCP SDK when a client or server starts, reducing startup time for everyday CLI commands and plugin registration.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
46 changes: 46 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, (event: unknown, ctx: unknown) => 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");
Expand Down
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,27 +330,32 @@ 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
: undefined;
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;
Expand Down
Loading