# 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` | Every task; the only location read |
| `~/.config/agenvoy/hooks/demo/` | Demo script for every state (`.py` and `.js`), installed by `make build` and shipped in release archives |

Project hooks under `<work dir>/.config/agenvoy/hooks/` are no longer loaded, so a cloned repository cannot bring hooks that run on their own; put hooks in `~/.config/agenvoy/hooks/` (Removed in v1.5.0). The file name is the state name; any other name is ignored. When both `.py` and `.js` exist 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.
