# Hooks

> Run your own commands, or call a URL, when zelk is about to act, has acted, or reaches other points in a session.

A hook runs your command, or posts to a URL, at a point in a session:
before a tool runs, after it, when you send a prompt, when a run stops, and
others. A hook can refuse an action, rewrite it, or add context for the
model. The format follows [Claude Code's hooks](https://code.claude.com/docs/en/hooks),
so most of its hook scripts work as they are.

## Where hooks live

| File | Runs |
|---|---|
| `~/.config/zelk/hooks.json` | always: these are yours |
| `.zelk/hooks.json` at the workspace root | only once you trust that exact file |

Your hooks run before the project's. A file that is malformed, unreadable
or uses something zelk doesn't support stops zelk at start, rather than
leaving a safety hook silently off. zelk doesn't read
`.claude/settings.json`. An administrator's managed policy can turn all
hooks off (`"hooks": false`); `zelk policy` shows whether one does.

## A first hook

This project hook refuses any Bash command that runs `rm`:

```json title=".zelk/hooks.json"
{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Bash",
      "hooks": [{"type": "command", "command": "./scripts/check-bash.sh", "timeout": 10}]
    }]
  }
}
```

```sh title="scripts/check-bash.sh"
#!/bin/sh
# Refuse any command that mentions rm.
if grep -q '"command":"[^"]*\brm\b'; then
  echo "Removing files is not allowed in this repository." >&2
  exit 2
fi
```

The command reads the event as one JSON object on standard input. Exit 0
lets things go on; exit 2 refuses, with standard error as the reason the
model is given. Any other exit code is a failure, which doesn't block
unless the hook sets `"onFailure": "block"`: set it on a hook that must not
let an action through when it breaks or times out.

A command runs with `/bin/sh -c` in the workspace, with
`ZELK_PROJECT_DIR` (and `CLAUDE_PROJECT_DIR`) naming it. On Windows, which
has no `/bin/sh`, it runs with the `bash.exe` of Git for Windows that runs
the Bash tool (`-bash` names another), and every path in a hook's input and
environment is written as zelk writes paths to the model: with the drive and
forward slashes, `C:/work/proj`. As in Claude Code, a command hook may
name its shell, `"shell": "bash"` or `"shell": "powershell"`; a PowerShell
hook runs without your profile, in PowerShell 7 or Windows PowerShell 5.1
as on the rest of Windows, and reads its input on standard input
(`[Console]::In.ReadToEnd()` or `$input`); its errors reach the model as
text. On Windows without Git for Windows, a hook
that names no shell runs in PowerShell, and one that asks for Bash fails.
When MXC is active, PowerShell is also the default for session hooks, and
zelk rejects trusted hooks that request Bash at startup.
[MXC issue #1061](https://github.com/microsoft/mxc/issues/1061)
**Hooks run
outside the sandbox, with your full environment**: they can read your files
and credentials, as any program you run can. `timeout` is in seconds, 60 by
default.

## Events

| Event | When | A hook can |
|---|---|---|
| `PreToolUse` | before a tool call is decided | allow, deny or ask; rewrite the input; add context |
| `PermissionRequest` | before zelk asks you to approve a call | allow or deny in your place |
| `PermissionDenied` | after a call is refused | add a note for the model |
| `PostToolUse`, `PostToolUseFailure` | after a tool call | add context or feedback |
| `PostToolBatch` | after a turn's tool calls | add context |
| `UserPromptSubmit` | when you send a message | block it; add context |
| `SessionStart` | when a session starts, resumes or compacts | add context |
| `Stop`, `SubagentStop` | when a run, or a subagent's, is about to end | block, sending it back to work |
| `SubagentStart` | when a subagent starts | add context |
| `PreCompact` | before a compaction | block it |
| `PreModelSwitch`, `PostModelSwitch` | around a model switch | block it; add context |
| `WorktreeCreate`, `WorktreeRemove` | when zelk makes or removes a worktree | do it in git's place |

`InstructionsLoaded`, `SkillLoaded`, `CwdChanged`, `Notification`,
`SessionEnd`, `Interrupt` and `PostCompact` can only observe.

A `matcher` selects tool names for tool events (`Bash|Edit`, or `mcp__.*`
for every MCP tool), the source for `SessionStart`, and the agent type for
subagent events. Omitted, empty or `*` selects everything. Only literal
text, `.*` and leading `^` or trailing `$` are supported; other regular
expression syntax is an error.

A hook decides more precisely by printing one JSON object, as Claude Code's
do:

```json
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"Use the deployment pipeline"}}
```

## HTTP hooks

A hook can post the event to a URL instead of running a command:

```json
{"type": "http", "url": "https://policy.example.com/hooks/pre-tool",
 "headers": {"Authorization": "Bearer $POLICY_TOKEN"},
 "allowedEnvVars": ["POLICY_TOKEN"], "timeout": 10, "onFailure": "block"}
```

A header may name variables as `$NAME` or `${NAME}`, but only those
`allowedEnvVars` lists; any other is empty, so a header carries nothing you
didn't give the hook. A 2xx answer counts as exit 0, and a JSON object in
its body decides as a command's output does. Redirects aren't followed, and
any other answer is a failure. Posts go through your proxy.

## Trusting a project's hooks

A repository's hooks run commands on your machine, so they run only once
you trust that exact file for that workspace. Changing any byte of it, or
moving the workspace, asks again. Trust covers the hook definitions, not
the scripts they run: read those before you trust.

The terminal UI shows every project command at start; press `y` to trust
and run them, or `n` or Esc to go on without them for this launch. Print
mode, `zelk exec` and `zelk acp` never ask: they skip untrusted project
hooks and warn. Review and trust them from a shell:

```sh
zelk hooks list      # the project's hooks, whether they're trusted, and the file's SHA-256
zelk hooks trust     # trust them as listed
```

`zelk project list` and `zelk project trust` do the same for everything
a project's `.zelk` files bring at once: the hooks, and the rules and
protected paths in `.zelk/settings.json`, whose allow rules also need
trust. With another state directory, give the same `-state` to these
commands and to the runs that use them.

## Where to go deeper

- [The hooks contract](/internals/hooks/): every event's input and output,
  exit codes, decisions and what zelk doesn't support
- [Permissions](/concepts/permissions/): where hooks sit in each decision
