#!/usr/bin/env bash
# Advisory — nudges the agent to run /evaluate-library before adding runtime
# dependencies. Non-blocking (exit 0). Stdout is injected as system-reminder
# context by the harness.
#
# Dispatches on payload shape:
#   .tool_input.command   → PreToolUse / Bash  (pnpm add / pnpm i <pkg>)
#   .tool_input.file_path → PostToolUse / Edit|Write (**/package.json edits)

set -euo pipefail

input=$(cat)

# --- PreToolUse / Bash path ---
cmd=$(printf '%s' "$input" | jq -r '.tool_input.command // ""')
if [[ -n "$cmd" ]]; then
  # Match: pnpm add <...> or pnpm i <pkg> — must have a space after keyword
  if [[ "$cmd" =~ (^|[[:space:]])pnpm[[:space:]]+(add[[:space:]]|i[[:space:]]) ]]; then
    # Skip dev-dependency installs — no policy evaluation needed for devDeps
    if [[ ! "$cmd" =~ (^|[[:space:]])(-D|--save-dev)([[:space:]]|$) ]]; then
      cat <<'EOF'
[library-policy-nudge] Runtime dependency detected — evaluate before adding.

Run the evaluate-library skill first:
  /evaluate-library <name> --tier <feature|core|app> --target <package-path>

This ensures the dependency is logged in docs/decisions/ before the pre-commit gate fires.
EOF
    fi
  fi
  exit 0
fi

# --- PostToolUse / Edit|Write path ---
file_path=$(printf '%s' "$input" | jq -r '.tool_input.file_path // ""')
if [[ "$file_path" == */package.json ]]; then
  cat <<'EOF'
[library-policy-nudge] package.json edited — verify any new runtime dependencies are evaluated.

If you added a runtime dependency, run the evaluate-library skill:
  /evaluate-library <name> --tier <feature|core|app> --target <package-path>

This ensures the dependency is logged in docs/decisions/ before the pre-commit gate fires.
EOF
fi

exit 0
