Skip to content
Skills Library
Programming · Building & coding

write-hooks

/write-hooks

Write or audit a Claude Code or Copilot CLI hook. Use when something should run automatically on an event, or a hook misfires or never fires. Not for the skill carrying it (write-skills) or the plugin manifest and catalog.

Licence
MIT
Last repository change
27.10.2026

Assessment and security

98/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene13 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
3
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 27.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Write or audit a Claude Code or Copilot CLI hook. Use when something should run automatically on an event, or a hook misfires or never fires. Not for the skill carrying it (write-skills) or the plugin manifest and catalog.

Original author description

Write or audit a Claude Code or Copilot CLI hook. Use when something should run automatically on an event, or a hook misfires or never fires. Not for the skill carrying it (write-skills) or the plugin manifest and catalog.

How it works

  1. 1. Name the job
  2. 2. Pick event, defend neighbour
  3. 3. Narrow trigger
  4. 4. Capture real payload
  5. 5. Write contract, then code
  6. 6. Shipping in a plugin
  7. 7. Verify it fired
  8. Several hooks, one event
  9. Firing over and over
Original SKILL.md headings · 305 lines, 2,513 words
SKILL.md preview
---
name: write-hooks
description: Write or audit a Claude Code or Copilot CLI hook. Use when something should run automatically on an event, or a hook misfires or never fires. Not for the skill carrying it (write-skills) or the plugin manifest and catalog.
---

# Writing hooks

Hook = **contract**, not script. Five lines settle it: what fires it, what it reads,
what it emits, what happens on fail, how confirm fired. Write those before code.

Default shape: `type: "command"`, bash + `jq`, shell form, shipped in plugin's
`hooks/hooks.json` against `${CLAUDE_PLUGIN_ROOT}`. Deviate only where step says so.

**Route:** authoring, work the steps. Hooks already exist, or one isn't firing:
load [AUDIT.md](AUDIT.md) instead — those branches read hooks, they don't write them.

## 1. Name the job

One hook, one job. Hook that gates _and_ logs = two hooks — fail differently, belong on different failure postures.

| job         | it does                            | typical events                                        | channel                                                                      | when its own check breaks                                             |
| :---------- | :--------------------------------- | :---------------------------------------------------- | :--------------------------------------------------------------------------- | :-------------------------------------------------------------------- |
| **gate**    | decides whether something proceeds | `PreToolUse`, `PermissionRequest`, `Stop`             | `permissionDecision` · `decision:"block"`                                    | exit 0 + `systemMessage`. **Gate that cannot evaluate must not deny** |
| **brief**   | puts facts in agent's context      | `SessionStart`, `UserPromptSubmit`, `PostToolBatch`   | `hookSpecificOutput.additionalContext`                                       | emit nothing                                                          |
| **reflex**  | side effect only, decides nothing  | `PostToolUse` (`async`), `Notification`, `SessionEnd` | none synchronously — `async` + `asyncRewake` still reaches agent (pattern 6) | emit nothing                                                          |
| **rewrite** | changes payload in flight          | `PreToolUse`, `PostToolUse`, `MessageDisplay`         | `updatedInput` (needs `allow`) · `updatedToolOutput` · `displayContent`      | emit nothing; original stands                                         |

Last column load-bearing: blocking session cuz `jq` missing turns lint problem
into stuck terminal. Hooks not security boundary anyway — `if` filter fails open by design.

**Done when:** name job in one word, say what hook must _not_ also do.

## 2. Pick event, defend neighbour

Most bad hooks on adjacent event. Say why neighbour loses.

- **`PreToolUse` vs `PermissionRequest`** — `PreToolUse` runs every tool call, before any
  permission check, in _every_ mode incl `bypassPermissions`. Policy user
  can't opt out of goes there. `PermissionRequest` runs only when Claude Code about to ask _you_ —
  "answer on my behalf" goes there.
- **`PostToolUse` vs `PostToolBatch`** — `PostToolUse` fires once per tool, runs concurrently
  across parallel batch, so never sees the set. `PostToolBatch` fires exactly once with
  every call in batch. Anything keyed on _what this batch touched_ belongs there. Their
  `tool_response` shapes differ: `PostToolUse` gets tool's structured output object,
  `PostToolBatch` gets serialized result model sees.
- **`PostToolUse` vs `Stop`** — matcher on `Edit|Write` misses every file Claude writes by
  shelling out through Bash. Hook must see _every_ change → sweep tree once per turn
  at `Stop` instead of catching each call.
- **`UserPromptSubmit` vs `UserPromptExpansion`** — typing `/skillname` never calls Skill
  tool, so no `PreToolUse` fires. `UserPromptExpansion` only event on that path.
- **`Stop` vs `TaskCompleted`** — `Stop` means turn ended, not same as work
  finishing. `TaskCompleted` fires when task actually being closed.

`SessionStart` and `Setup` run with no conversation loaded: `command` and `mcp_tool` handlers
only.

**Done when:** name the one event, the one rejected, with reason.

## 3. Narrow trigger

Two filters, cheapest first. `matcher` picks group; `if` decides whether process spawns
at all. Narrowing here beats exiting early inside script — latter still pays for
process on every tool call.

**What matcher compared against** not same string on every event:

| events                                                                                          | matched against             |
| :---------------------------------------------------------------------------------------------- | :-------------------------- |
| `PreToolUse` `PostToolUse` `PostToolUseFailure` `PermissionRequest` `PermissionDenied`          | `tool_name`                 |
| `SubagentStart` `SubagentStop`                                                                  | `agent_type`                |
| `SessionStart` `ConfigChange`                                                                   | `source`                    |
| `Setup` `PreCompact` `PostCompact`                                                              | `trigger`                   |
| `SessionEnd`                                                                                    | `reason`                    |
| `Notification`                                                                                  | `notification_type`         |
| `UserPromptExpansion`                                                                           | `command_name`              |
| `FileChanged`                                                                                   | **basename** of file        |
| everything else (`Stop`, `UserPromptSubmit`, `PostToolBatch`, `TaskCompleted`, `CwdChanged`, …) | nothing — `matcher` ignored |

How matcher itself read depends on chars in it:

- omitted, `""`, `"*"`, or `".*"` → every occurrence.
- only letters, digits, `_`, `-`, spaces, `,`, `|` → exact string, or list separated by `|`
  or `,`. (On `FileChanged` exact-match set narrower — letters, digits, `_`, `|` only —
  so anything with dot takes regex path.)
- anything else → **unanchored JavaScript regex**. `Edit.*` therefore also matches
  `NotebookEdit`. Anchor it: `^Edit$`. Same for plugin subagent types, colon in
  `plugin:agent` forces regex path: `^my-plugin:reviewer$`.
- MCP tools need wildcard: `mcp__memory__.*`. Bare `mcp__memory` contains only exact-match
  chars, compared as literal string, matches nothing. Plugin's own bundled
  server scoped further: `mcp__plugin_<plugin>_<server>__<tool>`.

`if` holds exactly one permission rule — no `&&`, no `||`, no lists. Two conditions = two
handlers. Evaluated **only** on `PreToolUse`, `PostToolUse`, `PostToolUseFailure`,
`PermissionRequest`, `PermissionDenied`; any other event, handler carrying `if` filtered
out, never runs. For Bash checks each subcommand plus contents of
`$()` and backticks, strips leading `VAR=value`. Rule sees command as written, so wrappers hide what it
matches: `sudo rm -rf`, `xargs rm`, aliases never satisfy `Bash(rm *)` — and cuz `if`
stops process from spawning, no script runs to catch them either. Gate on destructive
command trades narrowing for coverage: spawn on bare `matcher`, parse inside script. Fails open when command won't parse.

```json
{
  "matcher": "Bash",
  "hooks": [
    {
      "type": "command",
      "if": "Bash(git push *)",
      "command": "bash \"${CLAUDE_PLUGIN_ROOT}/hooks/guard-push.sh\"",
      "timeout": 5
    }
  ]
}
```

That object is one matcher-group; the event key holds an array of them. The inner `hooks`
array is required — a command object straight under the event fails with
`Missing property "hooks"`.

**Done when:** hook process doesn't spawn on calls it couldn't act on.

## 4. Capture real payload

Capture one instead of writing fixture. Shell layers eat backslashes — hand-built payload
carrying Windows path like `C:\\Users\\...` arrives as invalid JSON, debug fixture
instead of hook. Register capture handler, trigger once, keep file:

```json
{
  "type": "command",
  "command": "cat > \"${CLAUDE_PLUGIN_DATA}/last-payload.json\""
}
```

Every payload carries `session_id`, `cwd`, `hook_event_name`, `transcript_path`, usually
`permission_mode`; inside subagent also `agent_id`, `agent_type`. Read rest
off captured file. Two fields worth knowing without capturing: `transcript_path` written
asynchronously, lags live conversation — on `Stop`/`SubagentStop` read
`last_assistant_message` instead of parsing transcript; `stop_hook_active` tells
`Stop` hook it's already inside continuation it caused.

**Done when:** real payload on disk to replay against.

## 5. Write contract, then code

Put contract in comment header, then satisfy it:

```bash
# fires:  PreToolUse, matcher Bash, if Bash(git push *)
# reads:  .tool_input.command
# emits:  deny + a replacement command, or nothing
# fails:  jq missing or parse error -> exit 0, systemMessage, guard inert
# verify: bash hooks/guard-push.sh < last-payload.json; echo $?
```

**Pick one output channel, stay on it.** Exit codes _or_ JSON on exit 0. On exit 2 message
Claude sees is stderr — JSON not consulted for it.

- **0** — no objection. Not approval: normal permission flow still runs.
- **2** — block. stderr becomes message. What "block" means per-event; events that can't
  block just show stderr and continue.
- **anything else** — non-blocking error. Action proceeds, transcript shows `hook
error` notice. **Exit 1 does not block.** Bare `set -e` in guard turns failed `grep` into
  exit 1, guard silently fails open; with `set -Eeuo pipefail`, guard every check
  allowed to not match. Exceptions: `WorktreeCreate`, any failure aborts; under Copilot CLI a
  `PreToolUse` non-zero exit or crash **denies the tool** (step 6).

JSON on exit 0, routed by who reads it. Sending message down wrong reader's channel lands
it nowhere useful:

| reader | field                                     | where it lands                                          |
| :----- | :---------------------------------------- | :------------------------------------------------------ |
| agent  | `hookSpecificOutput.additionalContext`    | system reminder beside tool result, prompt, or turn end |
| agent  | `permissionDecisionReason` (on `deny`)    | tool error Claude reads and reacts to                   |
| agent  | `reason` (with `decision: "block"`)       | feedback Claude acts on                                 |
| human  | `systemMessage`                           | warning line in transcript                              |
| human  | `stopReason` (with `continue: false`)     | why everything halted                                   |
| human  | `statusMessage` (hook config, not output) | spinner text while hook runs                            |
| human  | `terminalSequence`                        | desktop notification, window title, bell                |

Rules that fall out of that table:

- `additionalContext` must be **nested inside `hookSpecificOutput` alongside `hookEventName`**.
  At top level stripped silently — hook that says nothing, reports nothing, almost
  always this. `hookEventName` required; omit it, output not applied.
- Write context as **facts, not orders**. "This repo uses `bun test`" works; "You must run bun
  test before finishing" reads as injected instruction, trips prompt-injection defenses,
  gets surfaced to user instead of used.
- Deny reason names the **repair**: "Use `rg`, not `grep` — respects .gitignore here" beats
  "blocked by policy". Refusal with no alternative gets retried verbatim. `PreToolUse` also
  accepts `additionalContext`, so gate can explain itself without denying.
- `updatedInput` honored **only** with `permissionDecision: "allow"`. `ask`, `escalate` and
  `deny` all drop rewrite silently — gate that both prompts and corrects path cannot exist.
  Want prompt, put corrected path in `permissionDecisionReason` and let agent reissue.
- stdout must be **only** JSON object. Shell profile echoing on startup corrupts it;
  wrap such echoes in `if [[ $- == *i* ]]`. Output not starting with `{` treated as plain
  text, which several events accept as context. Strings cap at 10,000 chars, then spill to file.

Load [PATTERNS.md](PATTERNS.md), adapt closest pattern rather than starting cold (pattern 8
for a handler type other than `command`).

**Done when:** each of five contract lines true of code written, and the handler passes every
rule in **Several hooks, one event** and **Firing over and over** below: one rewriter per call,
unique script name, `timeout` set, loop-guarded, injected context still true on `--resume`.

## 6. Shipping in a plugin

Plugin hook runs on machines you don't own. That's the whole difference.

- **Declare dependency.** `hooks/hooks.json` takes top-level `description` — name `jq`
  there and in README, degrade instead of dying: `command -v jq >/dev/null || { echo
'{"systemMessage":"<name>: jq not found, guard inactive"}'; exit 0; }`. Undeclared `jq` is
  most common way shipped hook breaks for someone else.
- **Run the script through its interpreter, placeholder quoted**:
  `bash "${CLAUDE_PLUGIN_ROOT}/hooks/x.sh"` (`node …` for `.mjs`). A bare path needs the exec
  bit, which a Windows commit drops; the permission error is non-blocking, so a gate fails
  open. Exec form (`"command": "bash", "args": ["${CLAUDE_PLUGIN_ROOT}/hooks/x.sh"]`)
  substitutes each element as plain string, no shell parsing.
- **Windows**: shell form runs Git Bash, or PowerShell when Git Bash absent — where bash
  script won't parse. Either ship `.ps1` twin registered with `"shell": "powershell"`, or
  state bash-only in description. Commit hook scripts with LF endings (`*.sh text
eol=lf` in `.gitattributes`) — CRLF breaks bash under Git Bash, most common Mac→Windows
  failure. In PowerShell use `$env:CLAUDE_PLUGIN_ROOT`; bare
  `$CLAUDE_PLUGIN_ROOT` resolves to `$null`, silently strips path prefix. Exec form on
  Windows can't spawn `.cmd` or `.bat` shims. `commandWindows` is **not** a field; accepted, ignored.
- **`${CLAUDE_PLUGIN_ROOT}` moves on every update.** Never write state there — use
  `${CLAUDE_PLUGIN_DATA}`, survives updates. After mid-session plugin update, hooks keep
  running old path until `/reload-plugins`.
- **User config**: `${user_config.KEY}` rejected in shell-form command, cuz
  substituted value would be re-parsed by shell. Put in exec-form `args`, or read
  `$CLAUDE_PLUGIN_OPTION_<KEY>` from environment.
- **Plugin GitHub Copilot CLI also installs**: Copilot runs the same `hooks/hooks.json` by
  rules that fail silently. `PreToolUse` exiting non-zero or crashing denies the tool.
  Stdout is one JSON document of flat fields (`additionalContext`, `permissionDecision`), never
  `hookSpecificOutput`; plain text is dropped. Tools are `create`, `edit`, `skill`, so
  matchers name both spellings (`Write|Edit|create|edit`, `Skill|skill`); `SessionStart` adds
  `new`. Argument names, `Stop`, user notes, the `powershell` twin: PATTERNS.md pattern 11.

Outside plugin same shapes apply with `${CLAUDE_PROJECT_DIR}` and `.claude/settings.json`.
Hooks in skill or agent frontmatter scoped to that component's lifetime; `once: true`
honored _only_ in skill frontmatter. **`Stop` hook in agent frontmatter silently
re-registered as `SubagentStop`** — write it expecting that payload.

**Done when:** every external tool the script needs (`jq`, …) is named in the `hooks.json`
`description` and the README; each `command` runs its script through the interpreter,
placeholder quoted, or passes it in exec-form `args`; a PowerShell twin is registered or
bash-only is declared; `.sh` files are LF in `.gitattributes`; no state under
`${CLAUDE_PLUGIN_ROOT}`; if Copilot CLI also installs the plugin, the Copilot bullet holds;
each frontmatter hook fits its component's lifetime (agent-frontmatter `Stop` handles the
`SubagentStop` payload).

## 7. Verify it fired

```bash
bash hooks/guard-push.sh < last-payload.json; echo "exit=$?"  # should act
bash hooks/guard-push.sh < benign.json;       echo "exit=$?"  # should stay silent
echo '{}'   | bash hooks/guard-push.sh;       echo "exit=$?"  # empty payload
echo 'junk' | bash hooks/guard-push.sh;       echo "exit=$?"  # not even JSON
```

Then `/hooks` confirm registered under right event, source expected. Then
real thing: `claude --debug-file /tmp/h.log`, trigger it, read log —
`CLAUDE_CODE_DEBUG_LOG_LEVEL=verbose` adds matcher-level detail.

Hook injecting context can't be judged from Claude's reply — injection upstream of
everything you see, good answer proves nothing. Read transcript file; block's in
there verbatim.

**Done when:** watched it fire _and_ watched it correctly not fire, malformed
case exited 0 instead of erroring.

## Several hooks, one event

- Every matching hook runs **in parallel, to completion**. One hook's `deny` doesn't cancel
  another's side effects — log line already written.
- `PreToolUse` precedence: `deny` > `defer` > `ask` > `allow`. Two hooks rewriting same
  call: last to finish wins, non-deterministic order — **one rewriter per call.**
- Every hook's `additionalContext` kept; Claude receives all of them.
- Identical handlers deduplicated by command string + args (URL for HTTP hooks) — across
  sources, not per plugin. Comparison runs on raw strings before
  `${CLAUDE_PLUGIN_ROOT}` expansion, so two plugins shipping same script path dedupe to
  **one** run, other silently dropped. Name each plugin's hook script uniquely.
- Hooks **merge** across settings levels rather than replacing. Plugin's hook runs alongside
  user's and project's. Hook's `allow` never overrides deny rule from any scope, `ask` rule
  still sends call through full permission pipeline.

## Firing over and over

- **Async anything non-decisive.** `"async": true` (command hooks only) lets Claude keep working;
  hook's `additionalContext` arrives next turn. Async hook decides nothing — action
  already happened. `asyncRewake` plus exit 2 only way it reaches idle session.
- **Idempotent.** Same hook fires on same file many times per session. Worse, on
  `--resume` transcript **replays** old `additionalContext` verbatim rather than re-running
  hook, so commit SHA or timestamp injected goes stale, Claude reads it as current.
  Emit facts that stay true, or emit nothing. `SessionStart` does re-run on resume.
- **Loop guards.** `PostToolUse` hook whose output prompts another edit re-fires itself —
  formatter reports diff, Claude edits, hook reports again. Gate on _state_ (content
  hash, marker file), not event. For `Stop` and `SubagentStop`, read `stop_hook_active`,
  exit 0 when true; after 8 consecutive blocks Claude Code overrides hook anyway.
- **That flag exists only on `Stop` and `SubagentStop`.** `TeammateIdle`, `TaskCompleted`, and
  `TaskCreated` also fire repeatedly on same subject with nothing equivalent. Own loop safety
  there as PATTERNS.md pattern 3 does: a counter keyed on `session_id`+`agent_id`, "gave
  up" stored as a **distinct state** (deleting it at the cap re-arms the gate), injected
  text prefixed and skipped on read, fail open at every parse.
- **Timeouts.** 600s default for command, HTTP, MCP handlers; `UserPromptSubmit` 30s;
  `prompt` 30s; `agent` 60s. All `SessionEnd` hooks share **1.5s** budget — set explicit
  `timeout` (up to 60s) if cleanup needs longer. `UserPromptSubmit` hook that times out is
  cancelled, context discarded — since v2.1.196 transcript shows notice naming hook and
  timeout; earlier versions cancel silently.
Files in this package (3)
  • AUDIT.md 4 KB
  • PATTERNS.md 20 KB
  • SKILL.md 19 KB

Versions

RepositoryScoreLicence
This page · j0hanz/j0hanz-marketplace98MIT
motion-creative/motion-creative-plugin100MIT

Database snapshot: 13.11.2026 · package version 3c4c05a46dc4 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100write-skills
    Write, fix, or audit a skill's frontmatter, trigger description, and body. Use when a skill is asked for, never fires, or fires at the wrong moment. Not for a hook (write-hooks) or the plugin manifest and catalog.
  • 100claude-code-hook-development
    This skill should be used when the user asks to "create a hook", "add a hook", "write a hook", or mentions Claude Code hooks. Also suggest this skill when the user asks to "automatically do X" or "run X before/after Y" as these are good candidates for hooks.
  • 100agent-harness-reference
    Field reference for Claude Code and GitHub Copilot CLI hook contracts. Covers hook locations, events, matchers, payloads, decisions, exit codes, timeouts, plugin roots, and cloud limits with official URLs and versioned probes. Use for `harness contract`, `copilot cli hook…
  • 82hooks-author
    Write or fix .devin/hooks.v1.json hook files. Use when creating Devin CLI hooks (tool-call guards, lifecycle events, commit hooks) or debugging why a hook isn't firing.
  • 100copilot-guide
    Use when configuring GitHub Copilot CLI or cloud-agent hooks, policy hooks, plugins, or skills. Triggers on .github/hooks, ~/.copilot/hooks, policy.d, preToolUse, permissionRequest, agentStop, command/HTTP/prompt handlers, cloud sandbox differences, plugin.json, repository…
  • 100setup
    Use when the user wants to set up the frontend harness (Claude Code hooks, rules, skills, checks, git hooks) in a React (Vite) or Next.js project, add, remove or change its modules or checks, or move a harness fix into the plugin.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.