Claude Code & Cursor PreToolUse + Bounded Stop Hooks

Prose in CLAUDE.md and .cursorrules is context, not enforcement. Use cross-platform Node.js hooks to fail closed on secrets and cap verification retries.

1. Measure Idle Token Burn Before Editing

Monolithic 300-line CLAUDE.md files and multiple MCP servers can burn 10,000+ idle tokens on every turn before you type a prompt. Audit your workspace first.

git clone https://github.com/tolga571/agent-context-audit.git
node ./agent-context-audit/bin/audit.mjs .

2. Deterministic PreToolUse Blocking (Exit Code 2)

Use Node.js instead of jq/bash so the exact same hook runs on Windows, macOS, and Linux. Return exit code 2 to block .env reads or destructive git commands deterministically.

if (/\.env(\.[A-Za-z0-9_-]+)?$/i.test(filePath)) {
  console.error(`Blocked sensitive file read: ${filePath}`);
  process.exit(2);
}

3. Bounded Stop Verification (No Infinite Token Loops)

Track stop_hook_active and a local retry counter capped at MAX_RETRIES=2 so failed unit tests feed stderr back twice instead of draining your monthly quota.

if (payload.stop_hook_active && retries >= MAX_RETRIES) {
  process.exit(0); // break loop safely
}
Run Free Context Audit CLI Get AgentGuardrails Pro ($29)