hookc

agent
Security Audit
Fail
Health Pass
  • License — License: MIT
  • Description — Repository has a description
  • Active repo — Last push 0 days ago
  • Community trust — 10 GitHub stars
Code Fail
  • network request — Outbound network request in benchmarks/gen.py
  • rm -rf — Recursive force deletion command in benchmarks/hookc.toml
  • rm -rf — Recursive force deletion command in benchmarks/hooks/guard.js
  • rm -rf — Recursive force deletion command in benchmarks/hooks/guard.py
  • rm -rf — Recursive force deletion command in benchmarks/hooks/guard.sh
  • rm -rf — Recursive force deletion command in test/gen_corpus.py
Permissions Pass
  • Permissions — No dangerous permissions requested

No AI report is available for this listing yet.

SUMMARY

A native gate for agent tool-call hooks: skips the real hook when a call cannot match. Claude Code, Codex CLI, Gemini CLI, Cursor, Antigravity. C99, libc only.

README.md

hookc

Native tools for the path between a coding agent and its model. One C99 binary, libc only.

  • The gate: your Python or Node tool-call hook runs only when a call could concern it; every other call exits in under 2 ms. Claude Code, Codex CLI, Gemini CLI, Cursor and Antigravity read their payloads from one table of rows.
  • hookc compact shortens build, test and diff output before the model reads it, and keeps every failure line.
  • hookc prefix-audit finds what breaks prompt-cache reuse in instruction files and request logs, and prices it.
The agent runtime sends the hook event to hookc. No trigger matched: exit 0 in under 2 ms. Trigger matched, or anything hookc is unsure of: exec your hook with the same stdin bytes.

Measured

make bench: 200 runs per row (40 per log in the 12-log suite), wall time including process spawn. Linux: 8 vCPU AMD EPYC VPS. macOS: M4 Pro. Inputs come from a seeded generator, so every byte count is identical on both machines. Full tables: benchmarks/.

Gate, a no-match pre-tool shell-command guard on a Claude Code payload:

Hook Linux p50 macOS p50
Python 30.5 ms 18.5 ms
Node 45.5 ms 44.9 ms
Bash, with jq 6.6 ms 4.5 ms
hookc in front of the Python hook 1.0 ms 1.8 ms

Compactor, generated logs with planted failure lines:

Log Bytes in Bytes out Failure lines kept Linux p50 macOS p50
pytest 133,763 1,356 9 of 9 2.4 ms 2.1 ms
cargo test 81,900 1,191 11 of 11 1.7 ms 2.0 ms
jest 101,016 737 7 of 7 2.1 ms 2.1 ms
make 57,794 1,997 6 of 6 2.0 ms 1.9 ms
git diff with a lockfile 281,507 1,164 13 of 13 2.3 ms 1.9 ms
1 MiB mixed 1,123,827 15,845 99 of 99 12.5 ms 4.6 ms
12 logs, one failure at line 3,000 of 5,000 2,212,912 3,144 12 of 12 3.0 ms 2.2 ms

Prefix audit, one 20-request session logged four ways:

Session Prefix reuse Uncached input Input cost, Anthropic prices
Stable prefix 1.00 1.0x ideal 1.00x ideal
Timestamp in the system prompt 0.38 9.4x 4.70x
Tool list reordered each turn 0.00 14.5x 6.98x
Instructions prepended to the first message 0.62 6.2x 3.28x

An audit of a 0.6 MB trace takes 5.0 to 5.3 ms on Linux and 3.1 to 3.5 ms on macOS.

Install

git clone https://github.com/abhaymettu/hookc && cd hookc
make && make install PREFIX=~/.local     # hookc and hook-bench, libc only

Runtimes

Runtime Gate compact --hook Checked against
Claude Code PreToolUse, PostToolUse, UserPromptSubmit yes hooks reference
Codex CLI PreToolUse, PostToolUse, UserPromptSubmit no: no output replacement docs and source
Gemini CLI BeforeTool, AfterTool, BeforeAgent no: only a deny reason replaces output source
Cursor preToolUse, postToolUse, beforeShellExecution, beforeReadFile, afterFileEdit, beforeSubmitPrompt no: MCP tools only docs
Antigravity PreToolUse, PostToolUse, PreInvocation no: the hook never sees the output shipped docs, binary, a captured payload
OpenCode no: in-process plugins, no process to remove no docs and source
Pi through the pi-hooks shim (Claude Code shape) no source

Each gated runtime is a golden fixture in the test suite: payloads in its documented shape through reference hooks that read that shape, directly and through hookc. Sources, commits and payload paths: docs/RUNTIMES.md.

Gate a hook

  1. See what your hooks cost now:

    hook-bench
    
  2. Write a starter config from every runtime's hook config it finds (it edits none of them; --runtime cursor reads one):

    hookc init
    
  3. Add triggers to ~/.config/hookc/hookc.toml: the strings or regexes your hook acts on.

    [guard]
    runtime = "gemini-cli"            # optional; detected from the payload when absent
    event = "pre-tool"                # pre-tool | post-tool | prompt, or a native event name
    tools = ["run_shell_command"]
    field = "command"                 # command | file_path | prompt
    contains = ["git push", "rm "]
    regex = ["^sudo[[:space:]]"]      # POSIX extended
    

    A hook that answers every call it ignores with fixed bytes, such as {"decision": "allow"}, sets skip_stdout to those bytes, so a skip replies exactly as the hook would.

  4. Change the hook's command in the runtime's config as hookc init printed:

    -  "command": "python3 ~/hooks/guard.py"
    +  "command": "/home/you/.local/bin/hookc guard -- python3 ~/hooks/guard.py"
    
  5. Check the config, then prove the gate changes nothing:

    hookc check
    hook-bench --wrap      # MISMATCH if any output differs through hookc
    

Compact tool output

As a pipe:

cargo test 2>&1 | hookc compact
running 1500 tests
test encode::tests::retry_index_0 ... ok
[... 998 similar lines collapsed ...]
test encode::tests::render_split_999 ... ok
test parser::tests::nested_depth_limit ... FAILED
test retry::tests::render_store_1001 ... ok
[... 497 similar lines collapsed ...]
test batch::tests::decode_merge_1499 ... ok

The panic, its src/parser.rs:212:9 location and the assertion diff that follow are kept verbatim.

As a post-tool hook on the shell tool, so the model reads the short form. Of the runtimes above only Claude Code lets a hook replace tool output; elsewhere --hook prints nothing and the pipe form still works:

{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "/home/you/.local/bin/hookc compact --hook" }] }

It removes two kinds of line: copies of an identical line, and pass-shaped lines of a known tool (Compiling x, test a ... ok, t.py::t PASSED). Each removal is counted in a marker. A line holding an error word (error, fail, panic, assert, 32 more) or a file:line location is never removed unless an identical copy stays. ANSI escapes go; \r progress redraws keep their last frame. Lockfile and minified hunks in a diff collapse to one counted line. --mode pytest|cargo|jest|make|diff narrows the rules; the default auto applies them all.

Input over 4 MiB, not UTF-8, or holding a NUL passes through byte for byte. The hook prints nothing, so the model sees the original output, on any payload it cannot read exactly.

Audit prompt-cache prefixes

A provider reuses a cached prompt up to the first byte that differs from the last request. A timestamp near the top re-bills everything after it, every turn.

hookc prefix-audit AGENTS.md
AGENTS.md: 1 volatile span; prefix stable for the first 33 of 4655 bytes
  3:17  timestamp  2026-12-03T23:13:05

Instruction files are scanned for timestamps, UUIDs, Unix epochs, PIDs, random /tmp paths and git status lines. A request log (JSONL, one Anthropic, OpenAI or Gemini request body per line, as an API proxy records it) gets the turn, byte offset, segment and cause of every invalidation:

hookc prefix-audit --pricing anthropic requests.jsonl
requests.jsonl: 20 requests, 19 prefix invalidations
  turn 2  offset 11222  system  system-edit (timestamp)  lost 7548 bytes
  ...
reuse 0.380 of an append-only ideal; uncached 388662 bytes (~97165 tokens) vs 41414 ideal; cost 4.704x ideal (anthropic: read 0.10, write 1.25)

Causes: system-edit, tool-reorder, tool-change, history-edit, model-change. Presets anthropic, openai, gemini, or --read R --write W; the formulas are in DESIGN.md. --json for scripts. --check FILE runs as a session-start hook: it warns on a volatile span and never blocks. No API call is made.

How the gate decides

Every runtime's payload maps onto one event: a kind (pre-tool, post-tool, prompt), an optional tool name, and the strings command, file_path and prompt. A row per runtime and native event says where each one sits (tool_input.command in Claude Code, toolCall.args.CommandLine in Antigravity). The row is picked by the payload's event member, and only among the entry's runtime rows when it is set.

hookc skips only when it is certain no trigger matched. It runs your hook when:

  1. A trigger matches a field value.
  2. The payload is over 4 MiB, not strict JSON or not UTF-8, or repeats a key on a path it reads.
  3. No row matches the event, two matching rows disagree, or the entry's event does not hold.
  4. The field is absent, or any of its paths holds a non-string.
  5. hookc.toml is missing or invalid, or has no entry for the name.

The real command sits after -- in the runtime's hook config, so a broken config still reaches it. Your hook gets the exact stdin bytes, so its exit code, stdout and stderr are its own. A tool not in tools is skipped.

The contract

hookc is correct for a hook that gives one fixed reply (exit 0, stdout equal to skip_stdout, empty by default, no stderr) on every call matching none of its triggers. Make triggers a superset of what the hook acts on. hook-bench --wrap checks this on sample payloads built from the rows (--payload FILE adds your own recorded ones).

Upgrading from 0.2

  • The default config is ~/.config/hookc/hookc.toml ($XDG_CONFIG_HOME). ~/.claude/hookc.toml is still read when that file is absent.
  • An entry without runtime is matched by payload shape, so a 0.2 config gates exactly as before; event = "PreToolUse" keeps its meaning.
  • A tool name holding a NUL character now runs the hook.

Tests

make test runs 20,347 checks, each naming a claim in docs/CLAIMS.md:

  • Gate: 291 payloads through four reference hooks, directly and through hookc; exit code, stdout and stderr byte-identical on all 1,164 pairs. Each hook writes a sentinel when it runs, checked against an independent Python model of the decision order.
  • Runtimes: 280 golden payloads, each in one runtime's documented shape, through 15 reference hooks that read those shapes, with each entry's runtime set and with it detected: 8,400 gated runs, byte-identical to the hook run directly, sentinel checked against the model. Its first run caught a fixture entry whose tools left out a tool its hook acts on.
  • Compactor and audit: byte-identical output to independent Python models written from DESIGN.md, on every generated log, adversarial case and mode, and every trace under five pricings.
  • Every planted failure line survives, and every recall run removes lines, so recall on untouched output cannot pass.

make asan repeats the suite under ASan and UBSan. make mutate applies 64 mutants to the C source and requires every one to fail the suite.

Limits

  • Triggers are substrings and POSIX extended regexes, matched as bytes. \b and \s are not portable; use [[:space:]].
  • Gated events: the rows in the Runtimes table. Others always run the hook.
  • The audit counts bytes, not tokens; both ratios are independent of the conversion. Real caches also need a minimum prefix length and reuse in blocks, so its reuse is an upper bound.
  • macOS and Linux. No Windows.

Related

claw-hooks gates calls of chosen programs with its own checker protocol; claude-hook-replay profiles simulated hook calls. hookc wraps the hook you already have in whichever runtime runs it, benches your real configured hooks, and ships the differential test that shows the gate changes nothing.

License

MIT

Reviews (0)

No results found