Skip to main content

HookBrake

Git-aware loop circuit-breaker, PostToolUse secret vault, lossless-pointer test/diff compactor, and local session waste auditor for AI coding agents.

PyPI Version CI License: MIT Python 3.10+


Why HookBrake exists

Autonomous coding agents (Claude Code, OpenAI Codex CLI, Gemini CLI, Cursor) regularly hit four failure modes at the tool-execution boundary:

  1. Zero-progress retry loops: An agent runs the same failing pytest command or re-reads the exact same unchanged file 5 times in a row, burning quota without changing a single byte on disk.
  2. Test/lockfile context flooding: A single pytest traceback dump, cargo test run, or git diff package-lock.json injects 30,000+ chars into context, forcing premature compaction loops.
  3. Secrets leaking via tool stdout: PreToolUse command checks miss secrets printed at runtime (cat output, verbose env traces, debug HTTP responses), writing raw tokens straight into cloud LLM context and .jsonl transcripts.
  4. Silent cache invalidations: One volatile prefix change breaks prompt caching across turns, multiplying input costs invisibly.

Instead of shipping another static token counter or regex one-liner, HookBrake handles both retroactive proof (hookbrake audit) and real-time runtime interception (hookbrake hook) in one pure-stdlib Python CLI (<30ms per hook call, zero external dependencies).


Quickstart

1. Audit your existing local sessions first (read-only)

Before enabling any hook, measure how much waste happened in your past Claude Code, Codex CLI, or Gemini CLI transcripts:

pip install hookbrake
hookbrake audit
=== HookBrake Session Waste Audit ===
  Sessions scanned:       14
  Tool calls scanned:     612
  Oversized outputs:      19
  Compaction savings:     184,320 chars (~46,080 tokens)
  Secrets exposed:        2
    Breakdown:            {'GITHUB_PAT': 1, 'OPENAI_KEY': 1}
  Stalled loops detected: 5
    - Bash repeated 4x in session-a18f.jsonl
  Cache-break events:     3
    - Turn 19 (session-a18f.jsonl): cache_read dropped 42,100 -> 1,800 while cache_creation spiked to 41,200

2. Install hooks for your coding agent

# Claude Code (~/.claude/settings.json)
hookbrake install --claude

# Gemini CLI (~/.gemini/settings.json) and Cursor (~/.cursor/hooks.json)
hookbrake install --claude --gemini --cursor

(Backups are automatically saved to *.hookbrake.bak. Remove cleanly anytime with hookbrake install --claude --uninstall.)


What Happens at Runtime

PreToolUse — Git-Aware Loop Breaker & Firewall

  • Real TDD vs. Thrashing: HookBrake hashes (tool_name, args, git_state_hash) in SQLite (~/.hookbrake/state.db), including mtime/size of dirty files. Editing code and re-running pytest is allowed; re-running the identical failing command 3 times with zero file edits trips the breaker (exit code 2) and prompts the agent to change strategy.
  • Read-thrash & Ping-Pong detection: Catches repeated reads of unchanged files and alternating A -> B -> A -> B non-edit cycles.
  • Blast-radius & exfiltration rules: Blocks rm -rf /|~, curl | bash, git push --force main|master, .env/SSH key exfiltration, and tampering with hook config files.
  • Test-integrity check: Warns (or hard-blocks with --strict-tests) if an agent writes @pytest.mark.skip, it.skip, assert True, or except Exception: pass into test files.

PostToolUse — Secret Vault & Lossless-Pointer Diet

  • Secret Redactor: Scrubs OpenAI, Anthropic, AWS, GitHub PAT, PyPI, Stripe, Slack, Google API keys, Bearer tokens, and PEM private keys into deterministic <REDACTED:TYPE:sha6> markers before tool output enters LLM context.
  • Structured Output Compactor:
    • Collapses lockfile hunks (package-lock.json, poetry.lock, uv.lock, Cargo.lock) inside git diff while leaving source code diffs untouched.
    • Strips verbose passing test dots/lines from pytest, unittest, jest, cargo test, and go test while preserving failing assertions and tracebacks.
    • Saves the complete untouched log to ~/.hookbrake/artifacts/<sha>.log and appends the exact path so nothing is lost.

CLI Commands

hookbrake audit [path] [--agent all|claude|codex|gemini] [--json]
hookbrake wrap -- <command> [args...]   # Guard any CLI/agent command without native hooks
hookbrake compact <file|-> [--threshold 4000] [--save-artifacts] [--json]
hookbrake redact <file|-> [--json]
hookbrake install [--claude] [--gemini] [--cursor] [--uninstall]
hookbrake stats [--json]

Universal Command Wrapper (hookbrake wrap)

For agents or scripts without native PreToolUse / PostToolUse hooks (such as Codex pipelines or CI test runners), wrap any command directly:

hookbrake wrap -- pytest -q

HookBrake enforces the firewall + git-aware loop breaker before running the command, scrubs leaked secrets from stdout/stderr, and compacts passing noise while preserving failure tracebacks and saving the raw log to ~/.hookbrake/artifacts/.

Configuration (hookbrake.toml)

Optional overrides in project root (hookbrake.toml) or ~/.hookbrake/config.toml:

max_identical_retries = 3
max_unchanged_rereads = 3
max_ping_pong_cycles = 4
compact_threshold_chars = 4000
redact_secrets = true
block_destructive_commands = true
block_exfiltration = true
check_test_tampering = true
strict_tests = false

How HookBrake Compares

Focus Existing Single-Purpose Scripts HookBrake
Loop Detection Naive consecutive command counting SQLite + git status/mtime state fingerprint (allows real TDD edits)
PostToolUse Diet Truncation cuts off bottom tracebacks Lockfile-aware + test-failure-preserving + raw log artifact pointer
Secret Safety Blocks cat .env in PreToolUse only Scrubs leaked tokens inside runtime tool stdout in PostToolUse
Harness Scope Single agent (usually Claude-only) Normalized adapters for Claude Code, Gemini CLI, Cursor, generic JSON
Audit Before Install Separate log viewer script Built-in hookbrake audit across Claude, Codex, and Gemini logs

License

MIT

Metadata

Release files for hookbrake 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hookbrake 0.1.1
File Size Uploaded Provenance Actions
hookbrake-0.1.1.tar.gz 35.6 kB With provenance Details for hookbrake-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hookbrake 0.1.1
File Interpreter ABI Platform Actions
hookbrake-0.1.1-py3-none-any.whl Python 3 none any Details for hookbrake-0.1.1-py3-none-any.whl

Total release size: 67.3 kB

Release files / hookbrake-0.1.1.tar.gz

Download URL hookbrake-0.1.1.tar.gz
Size 35.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e895fe919d1c3ea86b4d0854a111fed1d28dcb08cc8c7943db9e6c1734c5574a
BLAKE2b-256 checksum
How to use checksums
c347486e6a2ad7ca0918fd2078ecaa17542e2f72782e236625a92e05e5ed25a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / hookbrake-0.1.1-py3-none-any.whl

Download URL hookbrake-0.1.1-py3-none-any.whl
Size 31.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b36c820846d40a382cc2f5140458ec88785ec69e59fecb117a3147af7abdf27f
BLAKE2b-256 checksum
How to use checksums
36c02cd66f1c409380af6f5555d925daac230b2bc9a3b67a89a0153dd6814a5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for hookbrake 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for hookbrake-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page