# Hooks

Added in v1.4.0. A hook is a Python or JavaScript script that Agenvoy runs at a fixed point of every task. It receives that point's data as JSON on stdin and answers whether the task may go on — for blocking a phrase, capping a token budget, or logging and notifying when a tool runs or a task ends.

## Where hooks live

| Location | Scope |
|---|---|
| `~/.config/agenvoy/hooks/<state>.py` or `.js` | Global, every task |
| `<work dir>/.config/agenvoy/hooks/<state>.py` or `.js` | Tasks whose working directory is that project; overrides the global hook of the same state |
| `~/.config/agenvoy/hooks/demo/` | Demo script for every state (`.py` and `.js`), installed by `make build` and shipped in release archives |

The file name is the state name; any other name is ignored. When a directory has both `.py` and `.js` for one state, `.py` wins. Hooks are loaded when a task starts; a script removed mid-task is skipped (no block), and one added mid-task takes effect from the next task.

## States

| State | Fires | stdin fields |
|---|---|---|
| `before_user_send` | Before a new user message runs | `session`, `task_hash`, `input`, `time` |
| `before_tool_call` | Before each tool call | `session`, `task_hash`, `tool_name`, `tool_args`, `input_totals`, `output_totals`, `hit_totals`, `time` |
| `after_tool_call` | After a tool call succeeds | `session`, `task_hash`, `tool_name`, `tool_args`, `tool_result`, `input_tokens`, `output_tokens`, `hit`, `input_totals`, `output_totals`, `hit_totals`, `during_ms`, `time` |
| `after_tool_error` | After a tool call fails | `session`, `task_hash`, `tool_name`, `tool_args`, `error`, `time` |
| `after_ask_user` | After `ask_user` is answered, including when a task paused on a question resumes | `session`, `task_hash`, `answers` (`[{question, answer}]`), `time` |
| `after_task_done` | After a task finishes | `session`, `task_hash`, `input`, `result`, `input_totals`, `output_totals`, `hit_totals`, `during_ms`, `time` |
| `after_task_cancel` | After the user cancels a task (not on pause, error or a pending question) | `session`, `task_hash`, `input`, `error` (always `user cancel it`), `input_totals`, `output_totals`, `hit_totals`, `time` |

`input_totals` / `output_totals` are the task's token totals so far; `input_tokens` / `output_tokens` cover the request that produced the tool call; `hit` / `hit_totals` are cache-hit percentages; `during_ms` is that tool call's duration (`after_tool_call`) or the whole task's (`after_task_done`); `time` is RFC 3339.

## Output protocol

The last stdout line must be `{"success": 1|0, "debug": "...", "error": "..."}`:

| Result | Effect |
|---|---|
| `success: 1` | The task continues; a non-empty `debug` is shown to the user as the hook's message |
| `success: 0` in `before_tool_call` | That call does not run; the agent gets the tool name and `error` as the tool result, and the task continues |
| `success: 0` in `after_tool_call` | The result is kept and the agent receives a steer message with the tool name and `error`; the task continues |
| `success: 0` in `before_user_send`, `after_tool_error`, `after_ask_user` | The task stops and shows `Cancel: <State>: <error>` |
| `success: 0` in `after_task_done`, `after_task_cancel` | No effect; the task has already ended |

A crash, a last line that is not that JSON, or running past 30 seconds (`HOOK_TIMEOUT`) counts as a block. Earlier stdout lines and stderr go to the debug log as `[Hook Function Log]`.

The script runs in the sandbox (`go-pkg/sandbox`) with the task's working directory and may only write under `$HOME`; a hook keeps running to completion even when the task itself is canceled.

## Where messages appear

- **TUI** — each `debug` message is a `⏺ <State>: <message>` block; a hook stop shows as `Cancel: <State>: <error>`.
- **Web UI** — since v1.4.0 each assistant message that has hook messages gets a fixed-height, scrollable block at its bottom, paged with previous / next and jumping to the newest entry as it arrives; hook stop reasons are shown there too. The block is rebuilt from the session action log on reload.
- **Action log** — every message is recorded as a `hook` entry with its task hash.

## Managing hook files

Since v1.4.1 the installed scripts can be managed outside the agent:

| Surface | What it does |
|---|---|
| TUI `/hook` | Lists every installed `<state>.py` / `<state>.js`; when a state has both, the `.py` is marked `(primary)` in blue. `Enter` opens the file, `d` moves it to `~/.config/agenvoy/hooks/.Trash` (recorded in file history). With nothing installed the popup says so and `Enter` opens the hooks folder |
| Web UI **Hooks** tab | Always lists all seven states by name (`BeforeUserSend` ... `AfterTaskCancel`), with `not added` or the installed languages under each. A state has a `.py` and a `.js` tab, `.py` first; a file that does not exist yet is labeled `(new)` and prefilled with its demo. **Save** writes the open tab, **Delete** trashes it and **Open** opens it with the system default app (both disabled for a `(new)` file); unsaved edits are kept while switching tabs |
| REST | `GET /v1/hooks`, `GET /v1/hook/*name`, `PUT /v1/hook`, `DELETE /v1/hook`, all **local** — see [Role, Skill, Hook and Allowlist Endpoints](/rest-api-skills#hooks) |

These all act on the global directory `~/.config/agenvoy/hooks/`; project hooks under `<work dir>/.config/agenvoy/hooks/` are edited as ordinary files.

## Writing hooks with the agent

The agent writes hooks through three tools (see [Tool and Skill Tools](/built-in-tools-extension)): `read_hook` returns the state's demo and the installed hook, `write_hook` writes, patches or trashes the script without a confirmation prompt, and `test_hook` runs it once with sample stdin. The `hook` topic of `reasoning_guide` gives the order: read, write or patch, then test and fix until `test_hook` returns the expected `success`. Subagents and scheduled runs cannot use `write_hook` or `test_hook`.
