#!/bin/bash # Claude Code PostToolUse hook: HARD ERROR on GPU→CPU leaks in hot-path files. # Reads JSON from stdin (Claude Code hook input), extracts file_path, runs guard. # Exit 1 = leak detected → Claude Code blocks the edit and must fix immediately. set -euo pipefail INPUT=$(cat) FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // empty' 2>/dev/null) if [ -z "$FILE" ] || [ ! -f "$FILE" ]; then exit 0 fi # Check Rust and CUDA files SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" case "$FILE" in *.rs) GUARD="$SCRIPT_DIR/gpu-hotpath-guard.sh" if [ -x "$GUARD" ]; then OUTPUT=$("$GUARD" "$FILE" 2>&1) || { echo "⛔ GPU HOT-PATH HARD ERROR in $FILE" echo "$OUTPUT" echo "" echo "CPU usage in GPU hot paths is NOT allowed. Fix the code — do not annotate with // gpu-ok:" exit 1 } fi ;; *.cu|*.cuh) GUARD="$SCRIPT_DIR/cuda-kernel-guard.sh" if [ -x "$GUARD" ]; then OUTPUT=$("$GUARD" "$FILE" 2>&1) || { echo "⛔ CUDA KERNEL HARD ERROR in $FILE" echo "$OUTPUT" echo "" echo "CPU/host operations in CUDA kernels are NOT allowed." exit 1 } fi ;; *) exit 0 ;; esac exit 0