A pre-commit hook runs before Git records a commit, making it a natural enforcement point for checks on staged changes. The reliable design separates certainty from judgment: deterministic tools such as secret scanners may reject the commit, while AI review should advise because probabilistic blockers eventually reject correct work and encourage --no-verify bypasses. section 5.3.2
Use a pre-commit hook with explicit guardrails:
- Scan staged content with a deterministic secret scanner, such as gitleaks, and give that check blocking authority. section 5.3.2
- Send the diff to an AI reviewer for questions linters cannot answer, including intent, project conventions, error handling, naming, and test coverage. section 5.3.2
- Require structured output and parse its verdict as data; never decide whether to block by grepping free-form text for a word such as PASS. section 5.3.2
- Keep model findings advisory and print concerns without blocking, preserving developer trust in the deterministic gate. section 5.3.2
- Treat hooks as one enforcement layer: unauthorized changes may also require CI gates or sandboxes because agent policy files remain advisory without external controls. section 5.5.1
Listing 5.4 Pre-commit hook example
#!/bin/bash
# .git/hooks/pre-commit
set -euo pipefail
DIFF=$(git diff --cached)
[ -z "$DIFF" ] && exit 0
# Deterministic checks block. This is the real gate.
gitleaks protect --staged --redact || exit 1
# The model advises. It cannot block on its own.
VERDICT=$(printf '%s' "$DIFF" | claude -p "Review this diff against our
conventions: error handling, test coverage for new public functions,
naming. Reply with exactly one line of JSON and nothing else:
{\"verdict\":\"clean\"} or {\"verdict\":\"concerns\",\"notes\":\"...\"}")
if [ "$(printf '%s' "$VERDICT" | jq -r '.verdict')" = "concerns" ]; then
echo "AI reviewer raised concerns (advisory, not blocking):"
printf '%s' "$VERDICT" | jq -r '.notes'
fi
exit 0Inspect a compact Bash implementation that scans staged changes with gitleaks, requests one-line JSON from an AI reviewer, and reports advisory concerns. section 5.3.2


