diff --git a/.claude/settings.json b/.claude/settings.json index 09269096d..d75f3bf6b 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -1,105 +1,43 @@ { "env": { - "CLAUDE_FLOW_AUTO_COMMIT": "false", - "CLAUDE_FLOW_AUTO_PUSH": "false", - "CLAUDE_FLOW_HOOKS_ENABLED": "true", - "CLAUDE_FLOW_TELEMETRY_ENABLED": "true", - "CLAUDE_FLOW_REMOTE_EXECUTION": "true", - "CLAUDE_FLOW_CHECKPOINTS_ENABLED": "true" - }, - "permissions": { - "allow": [ - "Bash(npx claude-flow:*)", - "Bash(npm run lint)", - "Bash(npm run test:*)", - "Bash(npm test:*)", - "Bash(git status)", - "Bash(git diff:*)", - "Bash(git log:*)", - "Bash(git add:*)", - "Bash(git commit:*)", - "Bash(git push)", - "Bash(git config:*)", - "Bash(git tag:*)", - "Bash(git branch:*)", - "Bash(git checkout:*)", - "Bash(git stash:*)", - "Bash(jq:*)", - "Bash(node:*)", - "Bash(which:*)", - "Bash(pwd)", - "Bash(ls:*)" - ], - "deny": [ - "Bash(rm -rf /)" - ] + "CLAUDE_FLOW_HOOKS_ENABLED": "true" }, "hooks": { - "PreToolUse": [ - { - "matcher": "Bash", - "hooks": [ - { - "type": "command", - "command": "cat | jq -r '.tool_input.command // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx claude-flow@alpha hooks pre-command --command '{}' --validate-safety true --prepare-resources true 2>/dev/null || true", - "timeout": 8 - } - ] - }, - { - "matcher": "Write|Edit|MultiEdit", - "hooks": [ - { - "type": "command", - "command": "cat | jq -r '.tool_input.file_path // .tool_input.path // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx claude-flow@alpha hooks pre-edit --file '{}' --auto-assign-agents true --load-context true 2>/dev/null || true", - "timeout": 8 - } - ] - } - ], "PostToolUse": [ - { - "matcher": "Bash", - "hooks": [ - { - "type": "command", - "command": "cat | jq -r '.tool_input.command // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx claude-flow@alpha hooks post-command --command '{}' --track-metrics true --store-results true 2>/dev/null || true", - "timeout": 8 - } - ] - }, { "matcher": "Write|Edit|MultiEdit", "hooks": [ - { - "type": "command", - "command": "cat | jq -r '.tool_input.file_path // .tool_input.path // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx claude-flow@alpha hooks post-edit --file '{}' --format true --update-memory true 2>/dev/null || true", - "timeout": 8 - }, { "type": "command", "command": "scripts/gpu-hotpath-hook.sh", "timeout": 10 - } - ] - } - ], - "PreCompact": [ - { - "matcher": "manual", - "hooks": [ + }, { "type": "command", - "command": "/bin/bash -c 'INPUT=$(cat); CUSTOM=$(echo \"$INPUT\" | jq -r \".custom_instructions // \\\"\\\"\"); echo \"🔄 PreCompact Guidance:\"; echo \"📋 IMPORTANT: Review CLAUDE.md in project root for:\"; echo \" • 54 available agents and concurrent usage patterns\"; echo \" • Swarm coordination strategies (hierarchical, mesh, adaptive)\"; echo \" • SPARC methodology workflows with batchtools optimization\"; echo \" • Critical concurrent execution rules (GOLDEN RULE: 1 MESSAGE = ALL OPERATIONS)\"; if [ -n \"$CUSTOM\" ]; then echo \"🎯 Custom compact instructions: $CUSTOM\"; fi; echo \"✅ Ready for compact operation\"'" + "command": "cat | jq -r '.tool_input.file_path // .tool_input.path // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx ruflo@latest hooks post-edit --file '{}' --format true --update-memory true 2>/dev/null || true", + "timeout": 8 } ] }, { - "matcher": "auto", + "matcher": "Bash", "hooks": [ { "type": "command", - "command": "/bin/bash -c 'echo \"🔄 Auto-Compact Guidance (Context Window Full):\"; echo \"📋 CRITICAL: Before compacting, ensure you understand:\"; echo \" • All 54 agents available in .claude/agents/ directory\"; echo \" • Concurrent execution patterns from CLAUDE.md\"; echo \" • Batchtools optimization for 300% performance gains\"; echo \" • Swarm coordination strategies for complex tasks\"; echo \"⚡ Apply GOLDEN RULE: Always batch operations in single messages\"; echo \"✅ Auto-compact proceeding with full agent context\"'" + "command": "cat | jq -r '.tool_input.command // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx ruflo@latest hooks post-command --command '{}' --track-metrics true 2>/dev/null || true", + "timeout": 8 + } + ] + } + ], + "PreToolUse": [ + { + "matcher": "Write|Edit|MultiEdit", + "hooks": [ + { + "type": "command", + "command": "cat | jq -r '.tool_input.file_path // .tool_input.path // empty' | tr '\\n' '\\0' | xargs -0 -I {} npx ruflo@latest hooks pre-edit --file '{}' --auto-assign-agents true 2>/dev/null || true", + "timeout": 8 } ] } @@ -109,16 +47,10 @@ "hooks": [ { "type": "command", - "command": "npx claude-flow@alpha hooks session-end --generate-summary true --persist-state true --export-metrics true" + "command": "npx ruflo@latest hooks session-end --generate-summary true --persist-state true 2>/dev/null || true" } ] } ] - }, - "includeCoAuthoredBy": true, - "enabledMcpjsonServers": ["claude-flow", "ruv-swarm"], - "statusLine": { - "type": "command", - "command": ".claude/statusline-command.sh" } } diff --git a/scripts/cuda-kernel-guard.sh b/scripts/cuda-kernel-guard.sh new file mode 100755 index 000000000..3f4f3143d --- /dev/null +++ b/scripts/cuda-kernel-guard.sh @@ -0,0 +1,137 @@ +#!/bin/bash +# CUDA Kernel Guard — detects CPU/host leaks in .cu kernel files. +# +# Production H100 system. CUDA kernels must be pure device code. +# This guard catches patterns that force CPU synchronization, +# host memory access, or debug output in production kernels. +# +# Usage: +# scripts/cuda-kernel-guard.sh +# +# Exit codes: +# 0 = clean +# 1 = violation detected +# +# Suppress reviewed lines with: // cuda-ok: + +set -euo pipefail + +FILE="${1:-}" +if [ -z "$FILE" ] || [ ! -f "$FILE" ]; then + exit 0 +fi + +# Only check .cu and .cuh files +case "$FILE" in + *.cu|*.cuh) ;; + *) exit 0 ;; +esac + +LEAK_PATTERNS=( + # ─── printf in device code (forces implicit sync, kills perf) ─── + # One printf in a kernel with 1024 threads = 1024 serial host writes. + 'printf\s*\(' + + # ─── Host API calls from device code (illegal or sync-forcing) ─── + 'cudaMalloc' + 'cudaFree' + 'cudaMemcpy\s*\(' + 'cudaMemset\s*\(' + 'cudaDeviceSynchronize' + 'cudaStreamSynchronize' + 'cudaEventSynchronize' + + # ─── Host memory access patterns ─── + # malloc/free/new/delete in device code = UB or host fallback + '\bmalloc\s*\(' + '\bfree\s*\(' + '\bnew\s+\w+[\[\(]' + '\bdelete\s+\w+' + + # ─── Host function markers in kernel files ─── + # __host__ functions shouldn't be in kernel .cu files (they belong in .rs host code) + '__host__\s+[^_]' + + # ─── CPU-side file I/O (absolutely forbidden in kernels) ─── + '\bfopen\s*\(' + '\bfwrite\s*\(' + '\bfprintf\s*\(' + '\bstdout\b' + '\bstderr\b' + + # ─── assert() in device code (triggers host trap, kills kernel) ─── + # Use __trap() or return early instead + '\bassert\s*\(' + + # ─── Double-precision math in f32 kernels (use f suffix) ─── + # exp/log/sin/cos are DOUBLE precision (4x slower on H100 tensor cores). + # expf/logf/sinf/cosf are single-precision (correct for f32 kernels). + # __expf/__logf are fast-math (~2 ULP) — optional, not required. + # Only flag the double-precision versions (no f suffix, not prefixed with __) + '[^_a-z]\bexp\s*\([^f]' + '[^_a-z]\blog\s*\([^f]' + '[^_a-z]\bsin\s*\([^f]' + '[^_a-z]\bcos\s*\([^f]' + '[^_a-z]\bsqrt\s*\([^f]' +) + +# Patterns that are OK (suppress false positives) +SAFE_PATTERNS=( + '// cuda-ok:' + '// debug-only:' + '#if 0' + '#ifdef CUDA_DEBUG' +) + +found=0 + +for pattern in "${LEAK_PATTERNS[@]}"; do + hits=$(grep -nE "$pattern" "$FILE" 2>/dev/null || true) + + # Filter out safe patterns + for safe in "${SAFE_PATTERNS[@]}"; do + if [ -n "$hits" ]; then + hits=$(echo "$hits" | grep -v "$safe" || true) + fi + done + + # Filter out comments: full-line (//, /*, *) and trailing /* */ comments + if [ -n "$hits" ]; then + hits=$(echo "$hits" | grep -v '[0-9]*:\s*//' \ + | grep -v '[0-9]*:\s*\*' \ + | grep -v '[0-9]*:\s*/\*' \ + || true) + # For trailing comments: if the match is ONLY inside /* ... */, skip it + if [ -n "$hits" ]; then + hits=$(echo "$hits" | while IFS= read -r line; do + # Strip trailing /* comment */ from the line, then re-check pattern + stripped=$(echo "$line" | sed 's|/\*[^*]*\*/||g') + if echo "$stripped" | grep -qE "$pattern" 2>/dev/null; then + echo "$line" + fi + done || true) + fi + fi + + if [ -n "$hits" ]; then + if [ "$found" -eq 0 ]; then + echo "CUDA KERNEL LEAK: $FILE" + found=1 + fi + echo "$hits" | while IFS= read -r line; do + echo " $line" + done + fi +done + +if [ "$found" -eq 1 ]; then + echo "" + echo "⛔ CPU/host operations detected in CUDA kernel." + echo " printf → remove or guard with #ifdef CUDA_DEBUG" + echo " cudaMalloc/Free → pre-allocate in host .rs code" + echo " sinf/cosf/expf/logf → use __sinf/__cosf/__expf/__logf" + echo " assert → use __trap() or return" + exit 1 +fi + +exit 0 diff --git a/scripts/gpu-hotpath-hook.sh b/scripts/gpu-hotpath-hook.sh index f064f7578..fb7125a55 100755 --- a/scripts/gpu-hotpath-hook.sh +++ b/scripts/gpu-hotpath-hook.sh @@ -11,23 +11,35 @@ if [ -z "$FILE" ] || [ ! -f "$FILE" ]; then exit 0 fi -# Only check Rust files +# Check Rust and CUDA files +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" + case "$FILE" in - *.rs) ;; + *.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 -SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -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 - exit 0