# Hook Output Protocol

What a hook prints to let a task continue or stop it, and where its messages are shown.

## Output protocol

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

| Result | Effect |
|---|---|
| `success: 1` with an empty `error` | The task continues; a non-empty `debug` is shown to the user as the hook's message. A non-empty `error` counts as `success: 0` whatever `success` says |
| `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 |

Only `success: 0` or a non-empty `error` stops anything. Since v1.5.0 a crash, a sandbox failure, a last line that is not that JSON, or running past 30 seconds (`HOOK_TIMEOUT`) skips the hook instead of blocking: the task goes on as if it passed and a `<State>: skipped: <error>` hook message is shown (before v1.5.0 these counted 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 (`$HOME` when there is none) and may only write under `$HOME`; since v1.5.0 `~/.config/agenvoy`, including the hooks directory itself, is read-only to it except `download/`. Networking is allowed unless the **Hook network** switch is off (`hook_network_disabled` in `config.json`, `/config` → Hook network or Web UI System → Security, since v1.5.0), which runs hooks with `NetworkDeny`; a hook that then fails with output matching `configs/jsons/network_error.json` is skipped with a message pointing to `/config` → Hook network. 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.
