#!/bin/sh
# Binding inner-loop gate. The deterministic checks an agent is *told* to run
# before finishing must also run before a commit can land — so drift cannot be
# committed locally, not just blocked at the PR.
#
# Enable once per clone:
#   git config core.hooksPath harness/githooks
#
# Skip in an emergency with --no-verify (CI still enforces the same checks).
# Pure-stdlib gates only — the hook must work without the project venv.
set -e

python3 harness/scripts/check_traceability.py
# Requirement IDs are append-only vs the committed ledger — never deleted or renumbered.
python3 harness/scripts/check_append_only_ids.py

# Non-blocking hygiene nudge: possible narrative / changelog comments in the
# staged diff. Does not fail the commit — review and delete if they are noise.
if command -v rg >/dev/null 2>&1; then
  staged_diff=$(git diff --cached --diff-filter=ACMR -- \
    '*.py' '*.md' '*.ts' '*.tsx' '*.js' 2>/dev/null || true)
  if [ -n "$staged_diff" ]; then
    if printf '%s\n' "$staged_diff" | rg -n --no-messages \
      '^\+.*(#|//|\*)\s*(NEW:|CHANGED|previously|this now|switched from|updated to|\(rev [0-9]{4})' \
      2>/dev/null; then
      echo "⚠ Possible narrative comments detected in staged diff." >&2
      echo "  Review against harness/skills/hygiene.md before committing." >&2
    fi
  fi
fi
