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.
Configuration
Section titled “Configuration”Declare hooks in config.toml as [[hooks]] entries:
[[hooks]]event = "PreToolUse"matcher = "mcp__*"command = ["/usr/local/bin/guard-mcp.sh"]timeout_ms = 5000on_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 |
Lifecycle Events
Section titled “Lifecycle Events”| 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 |
Stdin Payload
Section titled “Stdin Payload”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 onUserPromptSubmit.turn: 1-based turn counter onTurnEnd.tool_nameandtool_input: Tool name and JSON arguments onPreToolUse,PermissionRequest, andPostToolUse.tool_outputandtool_is_error: Tool output string and error flag onPostToolUse.reason: Optional reason string onPermissionRequest.
Exit Codes and Stdout
Section titled “Exit Codes and Stdout”-
Exit
0(allow): Execution proceeds. If stdout is a valid JSON object,oxreadssystemMessageandhookSpecificOutput:{"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."}}updatedInputreplaces the tool input JSON onPreToolUseand chains into subsequent matching hooks.permissionDecision("allow","deny", or"ask") and optionalpermissionDecisionReasonare honored onPermissionRequest. When a hook returns nopermissionDecision, the request falls through to the next matching hook and then to the host’s default permission handler.systemMessageandadditionalContextare combined and injected into the model context according to the event table above.- Plain text or empty stdout on exit
0is ignored.
-
Exit
2(block): Short-circuits the hook chain and blocks the operation onPreToolUse,PermissionRequest, andUserPromptSubmit. 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"onPreToolUse,PermissionRequest, orUserPromptSubmit, the error is converted into a block so guard hooks fail closed.
Matcher Globs
Section titled “Matcher Globs”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_*).