agentleFS
pahlevikunpahlevikun/levistack/skills/agent-authoring/create-hook/SKILL.md

create-hook

Skill0 starsChanged 56 days agoMITView on GitHub

Create, wire up and test agent hooks: Claude Code settings.json or plugin hooks, Cursor hooks.json, or a shared hooks/ tree. Use when asked to run something automatically on an agent event, block or rewrite a tool call, format after edits, inject session context, or debug a hook that never fires.

  • Deletes or force-pushes
  • Commits and pushes

9 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 9 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Portable: Prefer Node over shell, and verify every binary the script calls exists on the hook's PATH.

    line 17
  2. Distinctive:

    If a repo already has hooks, extend its setup and keep unrelated entries.

    line 36
  3. Distinctive:

    Try it by hand: before the harness does:

    line 40
  4. Very specific:

    Add a Stop hook that blocks without checking stop_hook_active.

    line 63
  5. Distinctive:

    Put secrets or personal paths in a shared hook, or call the network in a hot-path hook.

    line 64

“Common” is measured: kept by at least 6 in 10 of the 9 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Create a hook

A hook is a script or check the harness runs on an agent event. The harness runs it, not the model, so it happens every time. Use a hook to observe, block, rewrite or add context. If the guidance only needs to be remembered, write a rule instead.

Principles

  1. Hooks are for what must happen every time. "Always format after an edit", "never run rm -rf /". Preferences belong in rules.
  2. Narrowest event, narrowest matcher. PreToolUse on Bash is better than a hook on every event. Filter in the script when a matcher gets tricky.
  3. Fail open unless it is a deliberate blocker. A crash, a timeout or unexpected input should let the action proceed. Reserve blocking (deny, exit 2, failClosed) for a rule you would defend.
  4. Fast and quiet. A hook on the hot path runs on every call. Set a short timeout, do no network work, and keep stdout to the JSON the harness expects.
  5. Deterministic over clever. A script beats a prompt hook when the result must be auditable. Use a prompt hook only for policy that is easier to describe than to code.
  6. Portable. Prefer Node over shell, and verify every binary the script calls exists on the hook's PATH.
  7. Opt-in for personal tooling. A hook shared through a plugin or repo affects everyone who installs it. Keep it conservative, or gate it behind a flag file.

Steps

  1. Gather requirements. Infer from the conversation; ask only for what is missing:
    • Trigger: which event, and which tool, command or file it should react to.
    • Behavior: audit, block, ask, rewrite input, inject context, or continue a workflow.
    • Scope: this user, this project, or a shared plugin or catalog.
    • Failure: fail open or closed.
  2. Pick the target.

    TargetConfigReference
    Claude Code, project or user.claude/settings.json, ~/.claude/settings.jsonclaude-code.md
    Claude Code pluginhooks/hooks.jsonclaude-code.md
    Cursor.cursor/hooks.json, ~/.cursor/hooks.jsoncursor.md
    A shared hooks tree (hooks/<name>.mjs with a launcher)the tree's own conventionits hooks/README.md

    If a repo already has hooks, extend its setup and keep unrelated entries.

  3. Choose the event from the table in the reference for your target. Check what that event can return; not every event can block.
  4. Write the script (Node by default). Read the JSON from stdin, decide, print only the output JSON, and exit 0. Block with the event's documented decision or with exit 2, never with prose on stdout. Ambiguous input returns allow.
  5. Wire it up with the smallest matcher that works. Start with no matcher or a simple one and tighten after it fires. Quote path placeholders ("$CLAUDE_PROJECT_DIR"), because paths contain spaces.
  6. Try it by hand before the harness does:

    node scripts/try-hook.mjs --event PreToolUse --tool Bash --command "git push --force" --expect ask -- node .claude/hooks/guard.mjs

    It builds a Claude Code payload, runs the command, and reports the exit code, the decision and any invalid JSON. --json '<payload>' sends a raw payload for other events or for Cursor. Run it for one input that should trigger and one that should pass. A hook behind an opt-in flag file reports allow until the flag exists.

  7. Trigger it for real in a session and read the harness's hook log (claude --debug-file <path>, or Cursor's Hooks tab). Confirm the matcher matched.
  8. Check the repo's gates. In a catalog with its own hook tests or sync step, add a test and run them.

Output at a glance

GoalClaude CodeCursor
Allowexit 0, no output{"permission":"allow"}
Ask the userPreToolUse permissionDecision: "ask"{"permission":"ask"}
BlockpermissionDecision: "deny" or exit 2 with a stderr reason{"permission":"deny"} or exit 2
Add contexthookSpecificOutput.additionalContextadditional_context (post events)

Do not

  • Print anything but JSON on stdout (a shell profile banner also breaks it).
  • Block on an ambiguous or unparseable input.
  • Add a Stop hook that blocks without checking stop_hook_active; it can loop.
  • Put secrets or personal paths in a shared hook, or call the network in a hot-path hook.
  • Overwrite a user's existing settings.json hooks; merge.

Done when

  • The event, matcher and handler are the narrowest that work.
  • try-hook.mjs shows the expected decision for one triggering input and one passing input.
  • It fired in a real session, and the log shows the expected result.
  • It fails open on bad input, unless blocking is its stated purpose.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.