1
0

block-read-hook.sh 1.2 KB

12345678910111213141516171819
  1. #!/usr/bin/env bash
  2. # PreToolUse hook (experiment): deny Read of codegraph-indexed source files and
  3. # steer the agent to codegraph_explore/codegraph_node instead. Tests whether
  4. # codegraph can FULLY replace Read for code-understanding once the escape hatch
  5. # is removed. Non-source reads (config, .env, markdown, new files) pass through.
  6. #
  7. # Wire via: claude ... --settings scripts/agent-eval/hook-settings.json
  8. set -uo pipefail
  9. input="$(cat)"
  10. fp="$(printf '%s' "$input" | jq -r '.tool_input.file_path // empty' 2>/dev/null)"
  11. case "$fp" in
  12. *.ts|*.tsx|*.js|*.jsx|*.mjs|*.cjs|*.py|*.go|*.rs|*.java|*.rb|*.php|*.swift|*.kt|*.kts|*.c|*.cc|*.cpp|*.h|*.hpp|*.cs|*.lua|*.vue|*.svelte)
  13. msg="Read is disabled for source files in this session — codegraph already has this file indexed (with line numbers, kept in sync on every change). Use codegraph_explore (several related symbols at once) or codegraph_node (one symbol's full source). If a symbol you need wasn't in a prior explore, run ANOTHER codegraph_explore with its exact name instead of reading the file."
  14. jq -n --arg m "$msg" '{reason:$m, hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$m}}'
  15. exit 0
  16. ;;
  17. esac
  18. exit 0