CLI chat tool hooks
Kompile supports synchronous, Claude Code-style command hooks for:
PreToolUse: before a tool executes; can deny execution.PostToolUse: after a successful tool result.PostToolUseFailure: after an error result or thrown tool exception.
Hooks run at the Kompile execution boundary in local chat, /tool, MCP stdio, and the daemon's socket MCP transport. They do not intercept a vendor CLI's own native tools (for example Claude's native Bash); configure that CLI's hooks separately. Existing Claude Code visibility hooks are unchanged.
Management
Type /hooks to list effective hooks, IDs, scopes and configuration paths, plus management help. Changes take effect on the next tool call; adding or editing a hook does not execute its command.
/hooks add {"event":"PreToolUse","matcher":"^(write|edit)$","command":"./scripts/check-edit.sh","timeout":30}
/hooks edit <id> {"command":"./scripts/new-check.sh","timeout":45}
/hooks disable <id>
/hooks enable <id>
/hooks get <id>
/hooks delete <id>
/hooks global list
/hooks global add {"event":"PostToolUseFailure","matcher":"^bash$","command":"/trusted/path/report-failure.sh"}
/hooks helpPrefix operations with project or global; mutations and get default to project. Bare list includes both scopes. Management-generated IDs are stable. edit is a partial update: omitted fields stay unchanged. Changing command replaces that entry's handlers with a single command; changing only timeout updates all its command handlers. You can also edit the JSON files directly.
Managing hooks conversationally with an agent
You do not need to hand-craft hook JSON — just ask the agent inside chat:
"Add a project
PreToolUsehook onwriteandeditthat runs./scripts/check-edit.shwith a 30-second timeout."
After you confirm, the agent calls the hooks tool (list, get, add, edit/update, delete, enable, disable). Mutations always require explicit user confirmation (confirmed=true) plus the hooks permission. Delegated children may inspect hooks but cannot modify them. The human /hooks command supplies confirmation for the explicitly requested operation.
Configuration
- Global:
~/.kompile/hooks.json - Project:
<working-directory>/.kompile/hooks.json
Both scopes run, global first; project entries do not override global entries. Matching handlers run sequentially in file order. Any before-hook denial stops execution, so a later allow cannot override it. Configuration is snapshotted once per invocation. Updates use atomic replacement and cooperative cross-process file locks. Each entry records a stable id, a tool-ID matcher regex, enabled flag, and one or more command handlers (command, timeout).
Matchers are case-sensitive regular expressions against Kompile tool IDs, not arguments or Claude's Bash(...) permission expressions. Empty/omitted matchers and * match every tool. Use ^read$ for exact matching or ^(write|edit|edit_patch)$ for several tools. Provider namespace prefixes are not added to these IDs. Bundle gateway calls are observed as mcp_tool_call; inspect tool_input to distinguish the remote operation. Nested calls in the same host invocation do not double-fire hooks.
Handler protocol
Commands execute in the working directory using bash -c on Unix or cmd.exe /c on Windows. Kompile passes a single JSON payload on stdin (never interpolating tool arguments into the shell command string) containing hook_event_name, session_id, cwd, tool_name, and tool_input — plus tool_response (title, output, metadata) for PostToolUse, or error and is_interrupt for PostToolUseFailure.
Environment: KOMPILE_PROJECT_DIR, CLAUDE_PROJECT_DIR, KOMPILE_SESSION_ID. Nested hosts inherit KOMPILE_HOOK_RUNNING=1 to avoid recursive hook execution. Daemon connections carry this marker per client; it never disables hooks for other clients.
- Exit
0: proceed. Plain stdout is feedback; stdout beginning with{or[is parsed as structured hook protocol JSON (hookSpecificOutputwithpermissionDecision: "allow" | "deny" | "ask",permissionDecisionReason,continue: false,additionalContext, orsystemMessage). Put diagnostic messages on stderr. - Exit
2: block a before-hook and return its stderr reason. - Other nonzero exits, invalid configuration/protocol, launch errors and timeouts also fail closed before execution. This intentionally differs from Claude Code's non-blocking treatment of some hook errors.
allowonly proceeds to existing tool permission checks; it never grants extra authority.askholds the call with an error for explicit user handling; it does not synthesize a vendor permission dialog.- Post-hook failures/denials are reported alongside the actual tool result; they cannot undo a completed operation or turn success into a tool failure.
Timeout defaults to 60 seconds; integer range 1–600 seconds. Each stdout/stderr stream is limited to 64 KiB; excess output is a hook error. Stdin and both streams are handled concurrently. Timeout/cancellation terminates the hook and its observed child processes. Background/asynchronous handlers are unsupported.
Safety and limitations
Hooks execute shell commands with your account's privileges. Only configure trusted commands, and review project hook files before authorizing them. The hooks permission defaults to ASK in local chat and can be managed using normal permission controls. MCP hosts use their existing permission policy; management still requires explicit confirmation.
Existing judge, gateway, workflow and product checks remain in force. Calls rejected before reaching execution do not fire hooks. Restricted product personas have no generic shell-hook authority. Workflow participants need edit capability for shell hooks, even when the triggering tool is read-only. updatedInput rewrites are deliberately unsupported: changing arguments after host supervision would bypass review of those arguments. HTTP, prompt/agent hooks, async handlers and non-tool lifecycle events are not included.
The management tool itself is exempt from hooks, so a failing hook can be disabled or deleted. If a JSON file is malformed, repair that file in your editor using the paths above. Never put credentials in hook feedback: it is included in tool results and may reach the model and transcript.
Protocol inspiration: Claude Code hooks reference.