feat(hooks): code-limits PostToolUse reports added lines over 80 columns in our code, once per file per session

This commit is contained in:
bchanot committed 2026-11-02 18:38:38 +01:00
1 parent 22ff57b57b
commit f21e2655ab
2 files changed
+172

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env bash
# code-limits.sh — PostToolUse (Edit|Write): the lines a file gained over 80
# columns, in OUR code only (lib/code-origin.sh), once per file per session,
# as additionalContext. A signal, never a gate: every failure exits 0 with
# no output (BDR-083, BDR-087, BDR-037).
set -u
export LC_ALL=C.UTF-8 # gawk length() and ${#} count characters (LRN-033)
exec 2>/dev/null # a hook never writes to stderr
LIMIT=80
CAP=5
MAX_BYTES=524288
EXT_RE='\.(sh|bash|ts|js|mjs|py|rs|jq)$'
SID_RE='^[A-Za-z0-9_-]+$'
LIB="$(cd "$(dirname "$(readlink -f "$0")")/../lib" 2>/dev/null && pwd)"
field() { printf '%s' "$PAYLOAD" | jq -r "$1 // empty" 2>/dev/null; }
hash16() { # hash16 <text> -> 16 hex chars, empty when no hasher exists
local h
h="$(printf '%s' "$1" | { sha256sum 2>/dev/null \
|| shasum -a 256 2>/dev/null; } | cut -c1-16)"
printf '%s' "$h"
}
is_ours() { # is_ours <file> <state dir>: origin cached per repo
local repo key line
repo="$(git -C "$(dirname "$1")" rev-parse --show-toplevel 2>/dev/null)" \
|| return 1
key="$2/origin-$(hash16 "$repo")"
if [ -s "$key" ]; then line="$(cat "$key")"
else
line="$(bash "$LIB/code-origin.sh" "$repo" 2>/dev/null)"
case "$line" in *"no commit"*) ;; *) printf '%s\n' "$line" > "$key" ;;
esac
fi
[ "${line%% *}" = ours ]
}
numbered_added() { # numbered_added <file> -> "<line no>\t<text>" per add
local dir; dir="$(dirname "$1")"
if git -C "$dir" ls-files --error-unmatch -- "$1" >/dev/null 2>&1; then
git -C "$dir" diff --no-ext-diff --no-textconv --no-color -U0 -- "$1" \
| awk '/^@@/ { split($3, a, ","); n = substr(a[1], 2); next }
/^\+\+\+ / { next }
/^\+/ { print n "\t" substr($0, 2); n++ }'
else
field '.tool_input.content // .tool_input.new_string' \
| awk '{ print (ENVIRON["CL_EDIT"] ? "?" : NR) "\t" $0 }'
fi
}
over_lines() { # over_lines <file> -> "<line no>:<width>" per long add
local rec n line
while IFS= read -r rec; do
n="${rec%%$'\t'*}"; line="${rec#*$'\t'}"
[ "${#line}" -gt "$LIMIT" ] || continue
printf '%s:%s\n' "$n" "${#line}"
done < <(numbered_added "$1")
}
report() { # report <file> <hits>
local n list
n="$(printf '%s\n' "$2" | wc -l | tr -d ' ')"
list="$(printf '%s\n' "$2" | head -"$CAP" \
| sed -E 's/^([^:]*):(.*)$/L\1 (\2)/' | paste -sd, - | sed 's/,/, /g')"
jq -cn --arg c "code-limits: $1 has $n added line(s) over $LIMIT \
columns: $list; our code keeps $LIMIT (CLAUDE.md Limits)." \
'{hookSpecificOutput:{hookEventName:"PostToolUse",additionalContext:$c}}'
}
checkable() { # checkable <file> <session>: worth reading at all
[[ "$1" =~ $EXT_RE ]] && [[ "$2" =~ $SID_RE ]] && [ -f "$1" ] \
&& [ "$(wc -c < "$1")" -le "$MAX_BYTES" ]
}
main() {
command -v jq >/dev/null 2>&1 || return 0
PAYLOAD="$(head -c "$MAX_BYTES")"
local file sid dir mark hits
file="$(field .tool_input.file_path)"; sid="$(field .session_id)"
checkable "$file" "$sid" || return 0
[ -n "$(field .tool_input.new_string)" ] && export CL_EDIT=1
dir="${TMPDIR:-/tmp}/claude-code-limits-$sid"
mark="$dir/$(hash16 "$file")"
[ "$mark" != "$dir/" ] && mkdir -p "$dir" 2>/dev/null || return 0
[ -e "$mark" ] && return 0
is_ours "$file" "$dir" || return 0
hits="$(over_lines "$file")"
[ -n "$hits" ] || return 0
: > "$mark"
report "$file" "$hits"
}
main 2>/dev/null
exit 0
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env bash
# lib/tests/code-limits.test.sh — hooks/code-limits.sh (BDR-125 phase 2):
# lines a file gained over 80 columns, in OUR code, once per file per
# session, additionalContext only, silent on every other path.
set -u
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
H="$ROOT/hooks/code-limits.sh"
WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
export TMPDIR="$WORK/tmp"; mkdir -p "$TMPDIR"
pass=0; fail=0
check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1));
printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; }
long() { printf 'x%.0s' $(seq 1 "$1"); }
has() { printf '%s' "$1" | grep -c -- "$2"; }
mkrepo() { # mkrepo <dir> <origin>
git init -q "$1" && git -C "$1" config claude.origin "$2"
git -C "$1" config user.email t@t && git -C "$1" config user.name t
printf 'a\n' > "$1/a.sh" && git -C "$1" add a.sh
git -C "$1" commit -q -m init
}
fire() { # fire <file> [session] [payload extra json] -> additionalContext
jq -n --arg f "$1" --arg s "${2:-s1}" --argjson x "${3:-{\}}" \
'{session_id:$s, tool_name:"Edit", tool_input:({file_path:$f} + $x)}' \
| bash "$H" | jq -r '.hookSpecificOutput.additionalContext // empty'
}
mkrepo "$WORK/ours" ours
printf 'a\n%s\n' "$(long 90)" > "$WORK/ours/a.sh"
out="$(fire "$WORK/ours/a.sh")"
check T1-reports-long "$(has "$out" 'L2 (90)')" 1
check T2-once-per-session "$(fire "$WORK/ours/a.sh")" ""
check T3-new-session "$(has "$(fire "$WORK/ours/a.sh" s2)" 'L2 (90)')" 1
mkrepo "$WORK/foreign" foreign
printf 'a\n%s\n' "$(long 90)" > "$WORK/foreign/a.sh"
check T4-foreign-silent "$(fire "$WORK/foreign/a.sh")" ""
printf 'a\n%s\n' "$(long 80)" > "$WORK/ours/a.sh"
check T5-80-is-fine "$(fire "$WORK/ours/a.sh" s3)" ""
{ printf 'a\n'; for i in 1 2 3 4 5 6 7; do long $((85 + i)); echo; done; } \
> "$WORK/ours/a.sh"
out="$(fire "$WORK/ours/a.sh" s4)"
check T6-count-all "$(has "$out" 'has 7 added line')" 1
check T7-cap-5 "$(printf '%s' "$out" | grep -o 'L[0-9]* (' | wc -l \
| tr -d ' ')" 5
big="$(long 95)"
printf '%s\n' "$big" > "$WORK/ours/new.ts"
x="$(jq -n --arg c "$big" '{content:$c}')"
check T8-untracked-write "$(has "$(fire "$WORK/ours/new.ts" s5 "$x")" \
'L1 (95)')" 1
printf 'short\n%s\n' "$big" > "$WORK/ours/old.ts"
x="$(jq -n '{new_string:"short"}')"
check T9-untracked-edit-payload-only "$(fire "$WORK/ours/old.ts" s6 "$x")" ""
printf '%s\n' "$big" > "$WORK/ours/doc.md"
check T10-md-silent "$(fire "$WORK/ours/doc.md" s7)" ""
check T11-bad-session-silent "$(fire "$WORK/ours/new.ts" '../x' \
"$(jq -n --arg c "$big" '{content:$c}')")" ""
out=$(jq -n --arg f "$WORK/ours/new.ts" \
'{session_id:"s8", tool_input:{file_path:$f}}' \
| PATH=/nonexistent "$BASH" "$H"); rc=$?
check T12-no-jq-rc0 "$rc" 0
check T13-no-jq-silent "$out" ""
mkdir -p "$WORK/fresh" && git -C "$WORK/fresh" init -q
printf '%s\n' "$big" > "$WORK/fresh/a.sh"
fire "$WORK/fresh/a.sh" s9 >/dev/null
check T14-no-commit-not-cached \
"$(find "$TMPDIR/claude-code-limits-s9" -name 'origin-*' | wc -l \
| tr -d ' ')" 0
printf 'PASS=%d FAIL=%d\n' "$pass" "$fail"
[ "$fail" -eq 0 ]