/** * Claude Code PreToolUse hook: `dev-diagnosis hook`. * * Claude Code runs the hook before a tool call, with the event as * JSON on stdin: `{ hook_event_name, tool_name, tool_input, cwd }`. * When the tool reads a file (Read, Edit, Write) whose path matches * an `.aiignore` entry, the hook blocks the call — exit code 2, with * the reason on stderr. Claude Code shows stderr to the agent as the * reason, so it moves on instead of retrying the read (harperzoe7's * method: "Exit code 2 does it, or a JSON block decision" — exit 2 * is the smaller of the two, and it needs no output contract). * * Anything else exits 0: a tool with no file path, a tool that does * not read a file, a path outside the repo, a file no `.aiignore` * matches, and — deliberately — malformed or empty input. A hook * that fails open cannot block the agent on a broken event; the * `.aiignore` enforcement is advisory, the scan is the verdict. * * The match is the same rule the scan uses (findAiignoreMatch in * lib/context.mjs): the root `.aiignore` plus one per ancestor * directory of the file. Only `.aiignore` is consulted — the hook * enforces what the repo already agreed not to hand the agent, and * nothing else. It never writes anything. */ import fs from 'node:fs'; import path from 'node:path'; import { findAiignoreMatch } from './context.mjs'; // Tools that act on one specific file. A Glob pattern is not a path // and a Bash command has no file to match, so neither is judged — // only a tool whose input names the file it will read. const FILE_TOOLS = new Set(['Read', 'Edit', 'Write']); /** * Parse a hook event from stdin text. Returns the event object, or * null when the input is empty, not JSON, or not an object — the * caller treats null as "allow", so a broken event never blocks. */ export function parseHookEvent(text) { const trimmed = String(text ?? '').trim(); if (!trimmed) return null; let parsed; try { parsed = JSON.parse(trimmed); } catch { return null; } if (parsed == null || typeof parsed !== 'object' || Array.isArray(parsed)) return null; return parsed; } /** * Judge one parsed hook event. `options.root` overrides the repo root * (tests); otherwise the event's `cwd` wins, then the process cwd. * Returns `{ blocked, reason, filePath, pattern }` — `filePath` is * the repo-relative path that was judged (null when there was nothing * to judge), `pattern` the `.aiignore` entry that matched (null when * allowed). A blocked reason names the entry and its file, so the * agent can see what to change instead of only that it was blocked. */ export function evaluateHookEvent(event, options = {}) { const allowed = { blocked: false, reason: null, filePath: null, pattern: null }; if (event == null || typeof event !== 'object') return allowed; const toolName = event.tool_name; if (toolName != null && !FILE_TOOLS.has(toolName)) return allowed; const input = event.tool_input; const filePath = input != null && typeof input === 'object' ? input.file_path : null; if (typeof filePath !== 'string' || filePath === '') return allowed; const root = path.resolve(options.root || event.cwd || process.cwd()); const absolute = path.resolve(root, filePath); const relative = path.relative(root, absolute).split(path.sep).join('/'); // A path outside the repo has no .aiignore above it to judge by — // and the hook must not invent one — so it is allowed. if (relative === '' || relative.startsWith('../') || path.isAbsolute(relative)) return allowed; const match = findAiignoreMatch(root, relative); if (match == null) return { ...allowed, filePath: relative }; return { blocked: true, reason: `Blocked by .aiignore: ${relative} matches '${match.pattern}' in ${match.file} — the repo says agents should not read this file.`, filePath: relative, pattern: match.pattern, }; } /** * Run the hook on stdin text: parse, judge, and say what the process * should do. Returns `{ code, message }` — code 2 with the reason * when the read is blocked, 0 otherwise. `message` is the stderr * text for a block, null when allowed. Reading stdin is the caller's * job (the CLI reads fd 0); this function is pure in the text. */ export function runHook(text, options = {}) { const event = parseHookEvent(text); const judged = evaluateHookEvent(event, options); if (!judged.blocked) return { code: 0, message: null }; return { code: 2, message: judged.reason }; } /** * Read the whole of stdin (fd 0) as text. Returns '' when stdin is a * terminal (no pipe): a hook is always invoked with piped JSON, so a * TTY means a human ran it by hand, and the caller reports that * instead of blocking on input that will never arrive. */ export function readHookStdin() { if (process.stdin.isTTY) return ''; try { return fs.readFileSync(0, 'utf8'); } catch { return ''; } }