claude_cli: tool calls are invisible to extensions and the event stream #182

Closed
opened 2026-11-13 19:14:20 +00:00 by hermes · 0 comments
Collaborator

hx is driven headless by an orchestrator (hx -p TEXT --mode json, extensions via [[extensions]]). The direct Claude provider is not usable under the subscription terms, so claude_cli (hx starts the official claude -p) is the provider that has to work. In that mode the orchestrator cannot enforce a tool allowlist, because the claude binary executes tools itself and hx' event bus never sees them.

Facts (read in the code, hx at 7218d14; claude 2.1.318)

  • crates/hx-core/src/approve.rs serve() answers claude's --permission-prompt-tool mcp__hx__approve questions with question() + gate_why() only. It has no access to the agent Bus, so extensions (hook::Hook, ext.rs) never see the question.
  • gate_why returns Ok immediately when no permit hook is set (print mode -p), so the bridge allows everything headless.
  • approve::question() skips Read/Grep/Glob/LS/Task/Agent/mcp__hx__* on purpose; reads are never asked (only secret-looking paths).
  • crates/hx-ai/src/claude_cli.rs turns a claude tool_use block into a text line [claude Bash: ...] in the stream; it emits no Event::ToolCall / ToolResult. NDJSON/RPC consumers see no tool use at all (hx -p "run ls" --mode json | grep tool_call prints nothing) and hx stats shows 1 request per run.
  • claude 2.1.318 has --allowedTools, --disallowedTools, --tools, --settings, --permission-mode, and --permission-prompts <target> ("none" = prompts denied automatically; per the help text a permission handler approves writes to settings/git/tool-configuration).

Wanted

  1. Route the permission bridge through the bus: serve() gets the agent bus (set up in main.rs next to approve::serve); each question becomes Event::ToolCall with names/args mapped to hx' vocabulary (Bash->bash{command}, Write->write{path}, Edit/MultiEdit->edit{path}, Read->read{path}, other tools by claude's name). A block from any hook/extension denies the call with its reason. Read-class tools must be asked too when an extension is subscribed (or always; they are cheap).
  2. Headless default: when -p has no permit hook, keep "allow" for compatibility but let a config value (e.g. permissions = "deny", or "extensions-only") make unanswered questions a deny. Needed so a guard that dies cannot leave the run open (today hx treats a dead extension as "continue").
  3. Emit Event::ToolCall / Event::ToolResult for claude's tools (from the assistant tool_use blocks and the matching tool results in the stream-json), so the NDJSON stream, hx stats, audit and turn budgets work in this mode. Keep the [claude ...] text line.
  4. Verify that claude actually asks for reads inside the project. If it does not (default permission mode), start claude so that it does (--permission-mode/--settings), or document that a read allowlist needs --tools/--disallowedTools in claude_cli = [...].

Done when

  • A test with the REAL hx binary and a fake claude that asks mcp__hx__approve for Read of a secret file and Bash curl x: an extension subscribed to tool_call blocks both, claude receives deny with the extension's reason; without the extension behaviour is unchanged.
  • The NDJSON stream contains tool_call/tool_result for claude's tools in claude_cli mode.
  • tests/all.sh (incl. tests/claude_cli_e2e.sh) stays green.

Consumer: johannes/orch#17 (profile allow is refused in claude_cli mode until this lands).

hx is driven headless by an orchestrator (`hx -p TEXT --mode json`, extensions via `[[extensions]]`). The direct Claude provider is not usable under the subscription terms, so `claude_cli` (hx starts the official `claude -p`) is the provider that has to work. In that mode the orchestrator cannot enforce a tool allowlist, because the `claude` binary executes tools itself and hx' event bus never sees them. ## Facts (read in the code, hx at 7218d14; claude 2.1.318) - `crates/hx-core/src/approve.rs` `serve()` answers claude's `--permission-prompt-tool mcp__hx__approve` questions with `question()` + `gate_why()` only. It has no access to the agent `Bus`, so extensions (`hook::Hook`, `ext.rs`) never see the question. - `gate_why` returns `Ok` immediately when no `permit` hook is set (print mode `-p`), so the bridge allows everything headless. - `approve::question()` skips Read/Grep/Glob/LS/Task/Agent/`mcp__hx__*` on purpose; reads are never asked (only secret-looking paths). - `crates/hx-ai/src/claude_cli.rs` turns a claude `tool_use` block into a text line `[claude Bash: ...]` in the stream; it emits no `Event::ToolCall` / `ToolResult`. NDJSON/RPC consumers see no tool use at all (`hx -p "run ls" --mode json | grep tool_call` prints nothing) and `hx stats` shows 1 request per run. - claude 2.1.318 has `--allowedTools`, `--disallowedTools`, `--tools`, `--settings`, `--permission-mode`, and `--permission-prompts <target>` ("none" = prompts denied automatically; per the help text a permission handler approves writes to settings/git/tool-configuration). ## Wanted 1. Route the permission bridge through the bus: `serve()` gets the agent bus (set up in `main.rs` next to `approve::serve`); each question becomes `Event::ToolCall` with names/args mapped to hx' vocabulary (`Bash`->`bash{command}`, `Write`->`write{path}`, `Edit`/`MultiEdit`->`edit{path}`, `Read`->`read{path}`, other tools by claude's name). A `block` from any hook/extension denies the call with its reason. Read-class tools must be asked too when an extension is subscribed (or always; they are cheap). 2. Headless default: when `-p` has no permit hook, keep "allow" for compatibility but let a config value (e.g. `permissions = "deny"`, or `"extensions-only"`) make unanswered questions a deny. Needed so a guard that dies cannot leave the run open (today hx treats a dead extension as "continue"). 3. Emit `Event::ToolCall` / `Event::ToolResult` for claude's tools (from the `assistant` tool_use blocks and the matching tool results in the stream-json), so the NDJSON stream, `hx stats`, audit and turn budgets work in this mode. Keep the `[claude ...]` text line. 4. Verify that claude actually asks for reads inside the project. If it does not (default permission mode), start claude so that it does (`--permission-mode`/`--settings`), or document that a read allowlist needs `--tools`/`--disallowedTools` in `claude_cli = [...]`. ## Done when - A test with the REAL hx binary and a fake `claude` that asks `mcp__hx__approve` for `Read` of a secret file and `Bash` `curl x`: an extension subscribed to `tool_call` blocks both, claude receives `deny` with the extension's reason; without the extension behaviour is unchanged. - The NDJSON stream contains `tool_call`/`tool_result` for claude's tools in `claude_cli` mode. - `tests/all.sh` (incl. `tests/claude_cli_e2e.sh`) stays green. Consumer: johannes/orch#17 (profile `allow` is refused in claude_cli mode until this lands).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: johannes/hx#182