#!/usr/bin/env bash
# run-scope-guard.sh  -  PreToolUse rules that depend on the active pipeline run.
#
# The decisions are in run-scope-guard.mjs; this wrapper exists because every
# hook the pipeline registers is a `bash "$HOME/.<host>/scripts/<name>"`
# command.
#
# Contract (Claude Code PreToolUse hook):
#   - Reads the tool-call JSON on stdin.
#   - Prints the hook JSON the host reads (a deny with its reason, or context
#     for the model) or nothing.
#   - Exits 0 on every path. A missing node or an internal error means no
#     output, and the call proceeds.

set -u

HERE="$(cd "$(dirname "$0")" 2>/dev/null && pwd || true)"
CORE="$HERE/run-scope-guard.mjs"
[ -f "$CORE" ] || exit 0
command -v node >/dev/null 2>&1 || exit 0

# Most Bash and WebFetch calls concern neither Figma nor run state; those end
# here without starting node.
INPUT="$(cat)"
case "$INPUT" in
  *'"tool_name":"Bash"'* | *'"tool_name": "Bash"'* | *'"tool_name":"WebFetch"'* | *'"tool_name": "WebFetch"'*)
    case "$INPUT" in
      *[Ff]igma* | *agent-state.json* | *tracker-state.json* | *" add "*) ;;
      *) exit 0 ;;
    esac
    ;;
esac

printf '%s' "$INPUT" | node "$CORE" 2>/dev/null || true
exit 0
