Skip to main content

Declarative hook framework for Claude Code

Project description

captain-hook

captain-hook banner

PyPI Python Docs License: PolyForm Noncommercial

Declarative hook framework for Claude Code. Write hooks as data, test them inline, and ship them to CI in the same shape they run in production.

Install

There's no install step. Run everything through uvx.

uvx capt-hook init

uvx fetches captain-hook into a throwaway environment and runs it, so you never add it to pyproject.toml. Every command in this README works the same way once you prefix it with uvx.

First hook

uvx capt-hook init scaffolds .claude/hooks/, wires Claude Code's settings, and installs the skills. One command and you're live.

uvx capt-hook init

A hook is declarative Python with an event, some conditions, and an action. This one stops the agent from finishing a UI change it never looked at.

# .claude/hooks/visual_review.py
from captain_hook import gate, TouchedFile, UsedSkill

# A Stop gate: before the agent finishes, block if it edited UI files without doing a visual review.
gate(
    # the one-line reason shown to the agent when the gate fires
    "You edited UI files. Open them with agent-browser and verify they render before finishing.",
    # fires only if UI files changed
    only_if=[TouchedFile("**/src/routes/**", "**/src/components/**")],
    # already reviewed -> don't block
    skip_if=[UsedSkill("agent-browser")],
)

Conditions match tools, files, commands, and even which skills the agent used.

Test your hooks

Every deterministic hook carries inline tests, so a broken hook fails like broken code. Run them from your project root, where --hooks defaults to .claude/hooks.

# .claude/hooks/safety.py
from captain_hook import Allow, Block, Input, block_command

block_command(
    ["git", "stash"],
    reason="Use the team's VCS workflow for shelving changes",
    hint="Commit a WIP change instead of stashing",
    tests={
        Input(command="git stash"): Block(),
        Input(command="git status"): Allow(),
    },
)
uvx capt-hook test

init already wired Claude Code's settings. Each event runs uvx capt-hook run <Event>, with the event JSON arriving on stdin and the verdict written to stdout. Re-run uvx capt-hook register-hooks only after you add hooks on a new event; it writes .claude/settings.local.json for you.

Agent Skills & plugin

captain-hook ships two Agent Skills so you don't have to write hooks by hand. bootstrapping-hooks mines your repo's docs, CI, and git history into proposed gates and nudges. translating-styleguides turns a STYLEGUIDE.md into enforced rules. uvx capt-hook init installs both into .claude/skills/, or you can add them as a plugin.

/plugin marketplace add yasyf/captain-hook
/plugin install captain-hook@captain-hook

What this solves

captain-hook covers these jobs:

  • Block dangerous tool calls before they execute on PreToolUse, like force-push, package-manager footguns, and raw rm -rf.
  • Drive the agent with feedback that fires on the patterns it actually emits, such as repeated failures, weakened tests, and missed conventions.
  • Enforce multi-step workflows with Stop gates and artifact validation, so the agent can't declare "done" without running tests, writing a report, or completing a checklist.
  • Keep all of the above testable. Every hook ships with inline tests = {...} that uvx capt-hook test runs in CI, so you catch broken hooks the way you catch broken code.

Docs

Read the docs for the full guide to conditions, primitives, LLM hooks, workflows, state, and real-world patterns.

For working on captain-hook itself, see the development guide.

License

Licensed under PolyForm Noncommercial 1.0.0, free for noncommercial use.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

capt_hook-3.1.0.tar.gz (122.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

capt_hook-3.1.0-py3-none-any.whl (159.9 kB view details)

Uploaded Python 3

File details

Details for the file capt_hook-3.1.0.tar.gz.

File metadata

  • Download URL: capt_hook-3.1.0.tar.gz
  • Upload date:
  • Size: 122.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for capt_hook-3.1.0.tar.gz
Algorithm Hash digest
SHA256 ad95b1786196f794fa7975eff57d656250e767db8d3221bdd49ff7ca707d36a8
MD5 23d7fca7fb3d19aea13da785114b3068
BLAKE2b-256 a69c0748e25b3c4e1efd0944e24c5a9c6867844b76f1ba3539c4735f10f89e6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for capt_hook-3.1.0.tar.gz:

Publisher: release-pypi.yml on yasyf/captain-hook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file capt_hook-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: capt_hook-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 159.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for capt_hook-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f4f53d5ce7674bbf9b5814bc194a878734e6524f542faab0ace5d1293b2de5b
MD5 a615c9d01bc387dfd1f5a340bd046704
BLAKE2b-256 cc49493725e8d16e67f2d9649a8998955a23fe43915d6770780a682744906dc8

See more details on using hashes here.

Provenance

The following attestation bundles were made for capt_hook-3.1.0-py3-none-any.whl:

Publisher: release-pypi.yml on yasyf/captain-hook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page