Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Agent Tool Hooks?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/rafaelrr5-agent-tool-hooks)
---
name: agent-tool-hooks
description: "Enforce a rule on every agent tool call with a pre-call hook: rewrite it or refuse it."
version: 1.0.0
author: Rafael Rocha Ribeiro (github.com/Rafaelrr5)
license: MIT
platforms: [linux, macos, windows]
metadata:
agent:
tags: [hermes, hooks, guardrails, policy, tool-calling, automation]
related_skills: [delegated-agent-supervision, hermes-cron-jobs, delivery-verification]
---
# Agent tool hooks: rules the model cannot forget
A rule in a prompt is a request. The model follows it most of the time, and the
time it does not is the one that costs you. A **pre-call hook** is a small script
the agent runtime runs before every matching tool call. It sees the call's
arguments and answers with one of three things:
| Answer | Effect |
|---|---|
| nothing (`{}`) | the call runs unchanged |
| `{"action": "modify", "args": {...}}` | the given keys are merged into the call's arguments, then it runs |
| `{"action": "block", "message": "..."}` | the call never runs; the message becomes the tool's result, so the agent reads why |
Two worked hooks ship in `scripts/`, one per answer type:
- `force_background.py` (modify): every `terminal` call runs in the background
and notifies on exit. A behaviour preference, enforced instead of requested.
It is here to show the mechanics; read the last pitfall before enabling it.
- `refuse_force_push.py` (block): refuses `git push --force`, `+ref`, `--delete`
and `:ref` pushes that would rewrite `main`, `master`, `production` or
`release`, plus any force push with no explicit branch (the hook cannot tell
which branch is current, so it refuses).
## When to Use
- A rule must hold on **every** call, not most calls: never touch a protected
branch, never run outside a folder, always run in background, always add a flag.
- An incident where the agent did something it was told not to. Fix the gate,
not the prompt wording.
- Not for rules that need judgement about intent. A hook sees one call's
arguments, not the conversation, so it can only enforce what is visible in them.
## Wire format (Hermes)
Checked against upstream `agent/shell_hooks.py` and `hermes_cli/plugins.py` at
commit `4e4cf69` (November 2026).
- **stdin**: one JSON object: `hook_event_name`, `tool_name`, `tool_input` (the
call's arguments), `session_id`, `cwd`, `profile`, `extra`. The arguments are
under **`tool_input`**, not `args`.
- **stdout**: one JSON object, or nothing. The `decision` dialect
(`{"decision": "block", "reason": ...}`, `{"decision": "modify", "tool_input": ...}`)
is accepted too, which helps when porting a guard written for Claude Code.
- **Exit code 2** blocks the call even without JSON (Claude Code / Cursor
convention); stderr becomes the message.
- **modify merges**: each hook's `args` is shallow-merged over the original
arguments. Return only the keys you change. Several modify hooks accumulate.
- **block beats everything**: a block from any hook wins, regardless of order.
- **Failure is open by default**: a crash, timeout or garbage output counts as
"no opinion" and the call runs. Set `fail_closed: true` on a guard hook so a
broken guard refuses instead.
## Procedure
1. **Write the decision as a pure function** (`decide(payload) -> dict`) and keep
I/O in `main()`. The function is what you test; `main()` is three lines.
2. **Pick the failure mode on purpose.** A convenience hook (formatting, flags)
fails open: a broken hook must not stop work. A guard hook fails closed: read
stdin with no `try`, so malformed input exits non-zero, and register it with
`fail_closed: true`.
3. **Test it the way the runtime runs it**: a subprocess with JSON on stdin,
asserting the JSON on stdout. Cover the call that must change, the call that
must not, another tool, and garbage input.
```bash
python3 -m unittest discover -s skills/agent-tool-hooks/tests -v
```
4. **Register it** in `config.yaml`. `matcher` is a regex matched against the
whole tool name:
```yaml
hooks:
pre_tool_call:
- matcher: terminal
command: "python3 /path/to/force_background.py"
timeout: 10
- matcher: terminal
command: "python3 /path/to/refuse_force_push.py"
timeout: 10
fail_closed: true
```
5. **Give consent.** A configured hook does nothing until it is allowlisted for
that exact command string: accept it at the interactive prompt, launch once
with `--accept-hooks`, or set `HERMES_ACCEPT_HOOKS=1` (or `hooks_auto_accept: true`).
`hermes hooks doctor` reports each hook's state; changing the command string
needs consent again.
6. **Prove it fired.** `hermes hooks test pre_tool_call --for-tool terminal
--payload-file call.json` (with `{"args": {"command": "git push --force origin main"}}`
in the file) runs every matching hook through the production parser and prints
what each one contributed. Then make one real call in a session and watch
for the effect.
## Pitfalls
- **Reading the wrong key works by accident.** An earlier version of
`force_background.py` read `payload["args"]`, which is always absent. It still
appeared to work, because it then returned `{"background": true, "notify": true}`
and modify *merges*. A block hook with the same bug would silently allow
everything. Test against the real wire shape, not your assumption of it.
- **`hermes hooks test` runs hooks that are not allowlisted.** A green test does
not mean the hook fires in a session. `hermes hooks doctor` must say allowlisted.
- **`hermes hooks doctor` only reports.** Running it with `HERMES_ACCEPT_HOOKS=1`
does not write the allowlist; consent is recorded by a session or gateway
launch that accepts hooks.
- **String matching on shell commands is a heuristic.** `refuse_force_push.py`
splits on `&&`, `||`, `;` and `|`, so it catches `cd repo && git push -f`. It
will not see a push hidden in a script file or an alias. A hook is a seatbelt,
not a sandbox: pair it with branch protection on the server for anything that
truly matters.
- **A hook that is slow is a tax on every call.** Keep timeouts low (10 s) and
the script free of network calls.
- **A convenient hook can cost more than it saves.** Forcing every terminal
call into the background looked free and was removed from the real setup
after real use showed the cost: every command came back as a process id instead of its
output, so the agent had to poll for results it used to get at once, and
every exit notification woke a new turn that found nothing to do. Before
keeping a modify hook, count what it adds per call (extra round trips, empty
wake-ups, lost output), not only what it prevents. Scope it narrowly instead,
for example only for commands that start a server or a long build.
## Verification
- `python3 -m unittest discover -s skills/agent-tool-hooks/tests -v`: 7 tests,
covering modify, pass-through, other tools, fail-open, eight blocked push forms,
five allowed commands and the fail-closed exit.
- Through the production dispatcher (`_get_pre_tool_call_directive_details` in
`hermes_cli/plugins.py`, both hooks registered with consent):
`ls -la` came back with `background: True, notify: True` merged in;
`git push --force origin main` came back as `block` with the policy message;
`git push origin feature/x` passed; a `read_file` call was untouched.
A guard that exits 1 with no output was ignored with `fail_closed: false` and
became `block` ("failed closed: hook exited 1 with no directive") with
`fail_closed: true`.