|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# hook-version: 1.0.0 |
| 3 | +""" |
| 4 | +UserPromptSubmit Hook: Creation Request ADR Enforcer |
| 5 | +
|
| 6 | +Fires at UserPromptSubmit time — BEFORE the model begins processing — and checks |
| 7 | +whether the user's prompt contains creation keywords. If a creation request is |
| 8 | +detected without a recent ADR session, it injects a strong context message |
| 9 | +reminding Claude that an ADR is mandatory before any other action. |
| 10 | +
|
| 11 | +This hook complements the PreToolUse:Agent creation-protocol-enforcer.py by |
| 12 | +catching the requirement earlier in the pipeline, before routing has occurred. |
| 13 | +
|
| 14 | +Allow-through conditions: |
| 15 | +- No creation keywords found in prompt |
| 16 | +- .adr-session.json exists and was modified within the last 900 seconds |
| 17 | +- ADR_PROTOCOL_BYPASS=1 env var is set |
| 18 | +""" |
| 19 | + |
| 20 | +import json |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import time |
| 24 | +import traceback |
| 25 | +from pathlib import Path |
| 26 | + |
| 27 | +sys.path.insert(0, str(Path(__file__).parent / "lib")) |
| 28 | +from hook_utils import context_output, empty_output |
| 29 | +from stdin_timeout import read_stdin |
| 30 | + |
| 31 | +_BYPASS_ENV = "ADR_PROTOCOL_BYPASS" |
| 32 | +_ADR_SESSION_FILE = ".adr-session.json" |
| 33 | +_STALENESS_THRESHOLD_SECONDS = 900 |
| 34 | +_EVENT_NAME = "UserPromptSubmit" |
| 35 | + |
| 36 | +_CREATION_KEYWORDS = [ |
| 37 | + "create", |
| 38 | + "scaffold", |
| 39 | + "build a new", |
| 40 | + "build a ", |
| 41 | + "add a new", |
| 42 | + "add new", |
| 43 | + "new agent", |
| 44 | + "new skill", |
| 45 | + "new pipeline", |
| 46 | + "new hook", |
| 47 | + "new feature", |
| 48 | + "new workflow", |
| 49 | + "new plugin", |
| 50 | + "implement new", |
| 51 | + "i need a ", |
| 52 | + "i need an ", |
| 53 | + "we need a ", |
| 54 | + "we need an ", |
| 55 | +] |
| 56 | + |
| 57 | +_WARNING_TEXT = """\ |
| 58 | +[creation-enforcer] CREATION REQUEST DETECTED — ADR IS MANDATORY BEFORE ANY OTHER ACTION |
| 59 | +
|
| 60 | +You MUST complete these steps BEFORE dispatching any agent or writing any files: |
| 61 | +1. Write ADR at adr/{name}.md (use kebab-case name describing what you're creating) |
| 62 | +2. Register: python3 scripts/adr-query.py register --adr adr/{name}.md |
| 63 | +3. Only THEN proceed to routing and agent dispatch. |
| 64 | +
|
| 65 | +Skipping this step will be blocked by the pretool-adr-creation-gate hook.\ |
| 66 | +""" |
| 67 | + |
| 68 | + |
| 69 | +def _has_creation_keywords(prompt: str) -> bool: |
| 70 | + """Return True if the prompt contains any creation keyword (case-insensitive).""" |
| 71 | + lower = prompt.lower() |
| 72 | + return any(kw in lower for kw in _CREATION_KEYWORDS) |
| 73 | + |
| 74 | + |
| 75 | +def _adr_session_is_recent(base_dir: Path) -> bool: |
| 76 | + """Return True if .adr-session.json exists and was modified within the threshold.""" |
| 77 | + adr_session_path = base_dir / _ADR_SESSION_FILE |
| 78 | + if not adr_session_path.exists(): |
| 79 | + return False |
| 80 | + try: |
| 81 | + mtime = os.path.getmtime(adr_session_path) |
| 82 | + age = time.time() - mtime |
| 83 | + return age <= _STALENESS_THRESHOLD_SECONDS |
| 84 | + except OSError: |
| 85 | + return False |
| 86 | + |
| 87 | + |
| 88 | +def main() -> None: |
| 89 | + """Run the UserPromptSubmit creation enforcement check.""" |
| 90 | + debug = os.environ.get("CLAUDE_HOOKS_DEBUG") |
| 91 | + |
| 92 | + raw = read_stdin(timeout=2) |
| 93 | + try: |
| 94 | + event = json.loads(raw) |
| 95 | + except (json.JSONDecodeError, ValueError): |
| 96 | + empty_output(_EVENT_NAME).print_and_exit() |
| 97 | + |
| 98 | + # Bypass env var. |
| 99 | + if os.environ.get(_BYPASS_ENV) == "1": |
| 100 | + if debug: |
| 101 | + print( |
| 102 | + f"[creation-enforcer] Bypassed via {_BYPASS_ENV}=1", |
| 103 | + file=sys.stderr, |
| 104 | + ) |
| 105 | + empty_output(_EVENT_NAME).print_and_exit() |
| 106 | + |
| 107 | + # UserPromptSubmit event uses the "prompt" field for the user message. |
| 108 | + prompt = event.get("prompt", "") if isinstance(event, dict) else "" |
| 109 | + if not prompt: |
| 110 | + empty_output(_EVENT_NAME).print_and_exit() |
| 111 | + |
| 112 | + # Check for creation keywords. |
| 113 | + if not _has_creation_keywords(prompt): |
| 114 | + if debug: |
| 115 | + print( |
| 116 | + "[creation-enforcer] No creation keywords found — allowing through", |
| 117 | + file=sys.stderr, |
| 118 | + ) |
| 119 | + empty_output(_EVENT_NAME).print_and_exit() |
| 120 | + |
| 121 | + # Resolve project root. |
| 122 | + cwd_str = event.get("cwd") or os.environ.get("CLAUDE_PROJECT_DIR", ".") |
| 123 | + base_dir = Path(cwd_str).resolve() |
| 124 | + |
| 125 | + # Check whether a recent ADR session exists. |
| 126 | + if _adr_session_is_recent(base_dir): |
| 127 | + if debug: |
| 128 | + print( |
| 129 | + "[creation-enforcer] Recent .adr-session.json found — allowing through", |
| 130 | + file=sys.stderr, |
| 131 | + ) |
| 132 | + empty_output(_EVENT_NAME).print_and_exit() |
| 133 | + |
| 134 | + if debug: |
| 135 | + print( |
| 136 | + "[creation-enforcer] Creation keywords found, no recent ADR session — injecting warning", |
| 137 | + file=sys.stderr, |
| 138 | + ) |
| 139 | + |
| 140 | + # No recent ADR session — inject strong advisory context. |
| 141 | + context_output(_EVENT_NAME, _WARNING_TEXT).print_and_exit() |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + try: |
| 146 | + main() |
| 147 | + except SystemExit: |
| 148 | + raise |
| 149 | + except Exception as e: |
| 150 | + if os.environ.get("CLAUDE_HOOKS_DEBUG"): |
| 151 | + traceback.print_exc(file=sys.stderr) |
| 152 | + else: |
| 153 | + print( |
| 154 | + f"[creation-enforcer] Error: {type(e).__name__}: {e}", |
| 155 | + file=sys.stderr, |
| 156 | + ) |
| 157 | + # Fail open — never exit non-zero on unexpected errors. |
| 158 | + sys.exit(0) |
0 commit comments