#!/bin/bash
# PostToolUse hook: Warn if CLAUDE.md grows too large
# Claude Code's instruction adherence degrades when CLAUDE.md exceeds ~300 lines.
# This hook checks line count after every edit and warns if it's getting too big.
# NOTE: This script needs chmod +x to be executable

PROJECT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"

# Read the tool input from stdin to get the modified file path
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('tool_input',{}).get('file_path',''))" 2>/dev/null)

# Only check if the modified file is CLAUDE.md
if [[ "$FILE_PATH" != */CLAUDE.md ]]; then
    exit 0
fi

# Check if the file exists
if [ ! -f "$FILE_PATH" ]; then
    exit 0
fi

# Count lines
LINE_COUNT=$(wc -l < "$FILE_PATH" | tr -d ' ')

# Warn if over threshold
# Research shows LLMs follow ~150-200 instructions reliably.
# Claude Code's system prompt uses ~50 of those. Keep CLAUDE.md under 300 lines.
WARNING_THRESHOLD=300

if [ "$LINE_COUNT" -gt "$WARNING_THRESHOLD" ]; then
    echo "" >&2
    echo "WARNING: CLAUDE.md is $LINE_COUNT lines (threshold: $WARNING_THRESHOLD)." >&2
    echo "  Large CLAUDE.md files degrade instruction adherence." >&2
    echo "  Consider moving detailed content to reference files and linking from CLAUDE.md." >&2
    echo "" >&2
fi

exit 0
