Skip to content

Anti-Tamper Hooks

codeknife hook claude and codeknife hook cursor are PreToolUse hook commands for AI coding agents. Your agent client runs one before each tool call and pipes the event in. CodeKnife answers with a deny payload when the call would skip git hooks, edit the gate’s config files, or use git plumbing to get around them. Use them when you let an agent commit code that must pass codeknife check.

Pipe an event in and read the decision on stdout:

Terminal window
printf '%s' '{"tool_name":"Bash","tool_input":{"command":"git commit --no-verify -m wip"}}' | codeknife hook claude
echo "exit=$?"
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"codeknife: skipping git hooks (`--no-verify`) is blocked for agents. Fix reported quality findings and commit normally."}}
exit=0

An allowed call prints nothing:

Terminal window
printf '%s' '{"tool_name":"Bash","tool_input":{"command":"cargo test --workspace"}}' | codeknife hook claude
echo "exit=$?"
exit=0

The hook reads all of stdin and parses it as one JSON object. It looks at these fields:

Field Used for
tool_name Picks the check (table below). Missing or non-string means “untagged”.
tool_input.command The shell command for Bash and Shell
tool_input.file_path The target file for Write, Edit, MultiEdit, StrReplace
tool_input.path Fallback target file for the same tools when file_path is absent
tool_input.notebook_path The target file for NotebookEdit

Each of command, file_path, path and notebook_path is looked up inside tool_input first and then at the top level of the event. So both of these are read the same way:

{"tool_name":"Bash","tool_input":{"command":"cargo test"}}
{"tool_name":"Shell","command":"cargo test"}

Both claude and cursor use the same classifier. The target only changes the output shape.

tool_name Check
Bash, Shell Shell command rules
Write, Edit, MultiEdit, StrReplace File path rules on file_path, else path
NotebookEdit File path rules on notebook_path
missing If a command field exists, shell command rules. Else if file_path or path exists, file path rules. Else allow.
anything else Allow. This includes Read.

The command is lowercased, then checked in this order. The first match wins.

Rule Matches when Deny reason
Skip hooks Any whitespace-separated token is --no-verify, or a token is -n and the command contains git commit codeknife: skipping git hooks (`--no-verify`) is blocked for agents. Fix reported quality findings and commit normally.
Hook redirection The command contains core.hookspath or .git/hooks anywhere codeknife: modifying or redirecting `.git/hooks` (`core.hooksPath`) is blocked for agents.
Git plumbing The command contains git commit-tree or git update-ref codeknife: low-level plumbing commands (`commit-tree` / `update-ref`) bypass quality hooks and are blocked.
Config tamper The command contains codeknife.toml, codeknife.baseline or arch.toml, and any of the mutator strings below codeknife: deleting or tampering with quality-gate configs (`codeknife.toml`, `codeknife.baseline`, `arch.toml`) is blocked.

Mutator strings (plain substring matches, trailing spaces included): rm , mv , cp , truncate , >, tee , sed , perl , awk , python, node , ruby , git checkout, git restore, chmod , ln , dd , unlink .

For edit tools, backslashes in the path are turned into forward slashes. The call is denied when:

  • the path contains /.git/hooks/ or starts with .git/hooks/, or
  • the file name is exactly codeknife.toml, codeknife.baseline or arch.toml, in any directory.

The deny reason is codeknife: `<path>` is a protected quality-gate file. Resolve findings in source code instead of weakening the gate., with the path as the agent sent it.

Each line below is a real run of codeknife hook claude with {"tool_name":"Bash","tool_input":{"command":"..."}} or the edit event shown.

Event Decision
git commit --no-verify -m wip deny (skip hooks)
git commit -n -m wip deny (skip hooks)
git push --no-verify deny (skip hooks)
git config core.hooksPath /dev/null deny (hook redirection)
rm .git/hooks/pre-commit deny (hook redirection)
ls .git/hooks deny (hook redirection)
git update-ref refs/heads/main abc deny (plumbing)
sed -i s/40/999/ codeknife.toml deny (config tamper)
rm codeknife.baseline deny (config tamper)
git checkout arch.toml deny (config tamper)
cp codeknife.toml /tmp/x deny (config tamper)
cat codeknife.toml allow
grep -n max codeknife.toml allow
cargo test --workspace allow
git commit --amend --no-edit allow
Write /repo/codeknife.toml deny
Edit /repo/sub/arch.toml deny
MultiEdit codeknife.baseline deny
Write .git/hooks/pre-commit deny
NotebookEdit x/codeknife.toml deny
Write C:\repo\codeknife.toml deny
Write src/lib.rs allow
Write Codeknife.toml allow
Read codeknife.toml allow
{} allow

The exit code is always 0 for a valid event. The decision is on stdout. Nothing is written to stderr.

Target Allow Deny
claude empty stdout one JSON line: hookSpecificOutput with hookEventName PreToolUse, permissionDecision deny, permissionDecisionReason set to the reason
cursor empty stdout one JSON line with permission deny, and user_message and agent_message both set to the reason

The cursor deny payload for the same --no-verify event:

{"agent_message":"codeknife: skipping git hooks (`--no-verify`) is blocked for agents. Fix reported quality findings and commit normally.","permission":"deny","user_message":"codeknife: skipping git hooks (`--no-verify`) is blocked for agents. Fix reported quality findings and commit normally."}

The hook never prints an explicit allow decision. An empty stdout with exit code 0 is the allow signal.

If stdin is not valid JSON, the hook prints an error on stderr and exits with 2:

Terminal window
printf '%s' 'not json' | codeknife hook claude
echo "exit=$?"
codeknife error: invalid hook JSON payload: expected ident at line 1 column 2
exit=2
Exit code When
0 Valid JSON event, allowed or denied
2 Stdin could not be read or is not valid JSON

CodeKnife does not install the hook. codeknife init writes only codeknife.toml, codeknife.baseline and arch.toml; it creates nothing under .claude/ or .cursor/ and touches no git hooks (see Init, Skill & Usage Spec). Register the command yourself as a PreToolUse command hook in your agent client’s settings:

  • Claude Code: codeknife hook claude
  • Cursor: codeknife hook cursor

The hook reads only stdin. It does not need --path and does not look at the repository.

There is no flag or environment variable that turns the classifier off. It only judges events that your agent client sends it. Commands you run yourself in a terminal never pass through it. To let an agent run a blocked command, remove the hook from the client’s configuration for that session.

The hook protects the gate files; the gate itself is codeknife check and the Loosening Guard, which reports config loosening in a diff even when a change gets past the hook.