Skip to content

Command Hooks

Out-of-process command hooks let external scripts observe, guard, rewrite, and inject context into the oxide-code agent loop without compiling Rust code.

Declare hooks in config.toml as [[hooks]] entries:

[[hooks]]
event = "PreToolUse"
matcher = "mcp__*"
command = ["/usr/local/bin/guard-mcp.sh"]
timeout_ms = 5000
on_error = "block"
Field Type Default Description
event string Required Lifecycle event name (SessionStart, PreToolUse, Stop, and others below)
command string[] Required Executable path and arguments invoked when the hook fires
matcher string None Optional tool name filter supporting exact names and * globs (mcp__*)
on_error string "allow" Error policy ("allow" or "block") honored on blocking-capable events
timeout_ms integer 30000 Maximum execution time in milliseconds before the process is killed
Event When It Fires Blocking Context Injection
SessionStart Before the first turn of a session run No Appended to system instruction
UserPromptSubmit Before the user prompt is sent to the model Yes Appended to latest user prompt
PreToolUse Before a tool call executes Yes Appended to tool result
PermissionRequest Before a tool call executes, to decide allow, deny, or ask Yes Ignored
PostToolUse After a tool call finishes executing No Appended to tool result
PreCompact Before conversation compaction runs No Returned as compaction guidance
SubagentStop When a spawned subagent finishes executing No Ignored
TurnEnd After each model turn completes No Ignored
Stop When the agent loop finishes No Ignored
SessionEnd When a session run exits (normal or error) No Ignored

Each hook process receives a single JSON object on stdin with cwd set to the agent working directory:

{
"event": "PreToolUse",
"cwd": "/workspace",
"tool_name": "bash",
"tool_input": { "command": "ls -la" }
}

Event-specific optional fields are omitted when absent:

  • prompt: User prompt text on UserPromptSubmit.
  • turn: 1-based turn counter on TurnEnd.
  • tool_name and tool_input: Tool name and JSON arguments on PreToolUse, PermissionRequest, and PostToolUse.
  • tool_output and tool_is_error: Tool output string and error flag on PostToolUse.
  • reason: Optional reason string on PermissionRequest.
  • Exit 0 (allow): Execution proceeds. If stdout is a valid JSON object, ox reads systemMessage and hookSpecificOutput:

    {
    "systemMessage": "Remind the model to run tests.",
    "hookSpecificOutput": {
    "updatedInput": { "command": "echo safe" },
    "additionalContext": "Repository policy requires clean diffs.",
    "permissionDecision": "allow",
    "permissionDecisionReason": "Approved by local policy."
    }
    }
    • updatedInput replaces the tool input JSON on PreToolUse and chains into subsequent matching hooks.
    • permissionDecision ("allow", "deny", or "ask") and optional permissionDecisionReason are honored on PermissionRequest. When a hook returns no permissionDecision, the request falls through to the next matching hook and then to the host’s default permission handler.
    • systemMessage and additionalContext are combined and injected into the model context according to the event table above.
    • Plain text or empty stdout on exit 0 is ignored.
  • Exit 2 (block): Short-circuits the hook chain and blocks the operation on PreToolUse, PermissionRequest, and UserPromptSubmit. Trimmed stderr (falling back to stdout) is returned as the block reason.

  • Any other exit code, spawn failure, or timeout: Controlled by on_error. With "allow" (default), the hook fails open and execution continues. With "block" on PreToolUse, PermissionRequest, or UserPromptSubmit, the error is converted into a block so guard hooks fail closed.

When matcher is omitted or "*", the hook matches every invocation of its event. Otherwise, matcher matches the tool name using * as a wildcard for zero or more characters (for example, mcp__*, read*, or *_edit_*).