Back to Discover

#ai coding agents

1 prompt found

Claude Code Hooks Config Writer for Team Repos: settings.json PreToolUse and PostToolUse Matchers, Exit Code 2 Blocking, stdin JSON Parsing with jq, Auto Format After Edits, Stop Hook Loop Guard, and a Hook Test Plan
๐Ÿค– AI Tools

Claude Code Hooks Config Writer for Team Repos: settings.json PreToolUse and PostToolUse Matchers, Exit Code 2 Blocking, stdin JSON Parsing with jq, Auto Format After Edits, Stop Hook Loop Guard, and a Hook Test Plan

PpromptstudioยทOct 7, 2026
No rating

Write Claude Code hooks a team can commit: the right settings file scope, PreToolUse guards that block destructive shell commands and edits to protected paths with exit code 2 and a reason Claude can read, PostToolUse formatting on edited files, a Stop hook that checks work without looping forever, and a plan to test each hook before trusting it.

Act as a developer productivity engineer who maintains Claude Code setups for a team, writes hooks as small shell or Python scripts under .claude/hooks, and treats every hook as code that runs with the developer's own permissions. Inputs: - Repo stack, package manager, formatter, linter, and test or typecheck commands: [RepoStack] - Commands or actions Claude must never run without asking: [BlockedActions] - Files or folders Claude must not edit: [ProtectedPaths] - What should happen automatically after edits or at the end of a task: [Automations] - Scope: personal only, shared with the team, or both: [Scope] - Output format: [Format] Generate: 1. Settings file choice from Scope: user settings in the home .claude folder, project settings in .claude/settings.json committed to git, or .claude/settings.local.json for personal overrides that stay out of git. 2. A hooks block in valid JSON: hook events as keys (PreToolUse, PostToolUse, Stop, and Notification or UserPromptSubmit only if Automations needs them), each with a matcher for tool names such as Bash or Edit|Write|MultiEdit, and command hooks pointing at scripts via the CLAUDE_PROJECT_DIR environment variable. 3. A PreToolUse guard for BlockedActions: read the JSON on stdin with jq, pull tool_input.command, match the dangerous patterns, print a reason to stderr, and exit 2 so the call is blocked and Claude sees why. Exit 0 lets the call through. 4. A path guard for ProtectedPaths on edit tools: read tool_input.file_path, block matches with exit 2 and a clear message. 5. PostToolUse automations from Automations and RepoStack: format only the edited file, keep output quiet, set a timeout, and never fail the session because a formatter is missing. 6. A Stop hook when Automations asks for a final check: run the typecheck or tests, exit 2 with the failures on stderr so Claude keeps working, and read stop_hook_active from stdin so it does not loop forever. 7. Each script in full with a shebang, set options, and a comment on what it reads and returns. Remind the team to make scripts executable. 8. A test plan: pipe sample JSON into each script by hand and check the exit code, then open the hooks menu in Claude Code to confirm they loaded, then try one blocked command in a scratch branch. 9. A security note: hooks run arbitrary commands with your account's access, so review hook changes in pull requests like any other code. Constraints: - Only use hook events, fields, and exit code behavior you are sure of; mark anything else "confirm in the current Claude Code hooks docs". - Quote paths that may contain spaces. No em dashes.