import json import os from pathlib import Path import re import subprocess import sys MAX_EVENT_BYTES = 1048576 MAX_PROMPT_CHARS = 2000 PROSE_SUFFIXES = ('.md', '.mdx', '.rst', '.txt') CLAIM = re.compile(r'\b(?:done|finished|completed?|fixed|implemented|resolved|works|fertig|erledigt|behoben|umgesetzt|' r'funktioniert|implementiert|abgeschlossen)\b', re.I) # shortcut: matches the wording, not real command output; parse each client's transcript if claims get faked. EVIDENCE = re.compile(r'\b(?:tests?|builds?|lint\w*|typecheck\w*|compiled?|pass(?:ed|es)?|fail(?:ed|s|ures?)?|exit|' r'verified|verification|not run|skipped|unverified|getestet|geprüft|ungeprüft|doctor|prüfung)\b', re.I) CORRECTION = re.compile(r"^\s*(?:nein|nope|no|falsch|wrong)\b" r"|\b(?:ist|war) (?:falsch|nicht richtig|nicht korrekt)\b|\b(?:stimmt|passt) nicht\b" r"|\b(?:schon wieder|immer noch nicht|nicht so|so nicht|niemals|du sollst (?:nicht|doch)|" r"(?:habe|hab|hatte) ich (?:doch )?gesagt|ich (?:sagte|hatte gesagt))\b" r"|\b(?:that'?s (?:wrong|not right)|not what i (?:asked|wanted|said)|i (?:said|told you)|" r"you (?:forgot|missed|ignored)|you (?:should|must) not|don'?t (?:do|use|add)|" r"stop (?:doing|using|adding)|never)\b", re.I) BLOCK_REASON = ('The last message claims completion for changed code but cites no check. Run the cheapest relevant check ' 'and report its real result, or state plainly that it was not run and why.') CORRECTION_CONTEXT = ('This prompt may correct you. If it reveals a durable rule rather than a one-off fix, propose that rule ' 'in one line and ask for approval. After approval, store it in the user\'s configuration repository: ' '`shared/` for all clients, or `scripts/commands/add-instruction` for one client. Ask for the ' 'repository path if unknown. Never change instructions without approval.') def read_event(): raw = '' if sys.stdin.isatty() else sys.stdin.buffer.read(MAX_EVENT_BYTES + 1).decode('utf-8-sig') if len(raw) > MAX_EVENT_BYTES or not raw.strip(): return {} event = json.loads(raw) return event if isinstance(event, dict) else {} def has_code_changes(workspace): if not isinstance(workspace, str) or not Path(workspace).is_dir(): return False # core.fsmonitor from a repository's config would otherwise run its command here. status = subprocess.check_output(['git', '-c', 'core.fsmonitor=false', '-C', workspace, 'status', '--porcelain', '-z'], stderr=subprocess.DEVNULL, timeout=3) entries = status.decode('utf-8', 'replace').split('\0') return any(entry and not entry.lower().endswith(PROSE_SUFFIXES) for entry in entries) def check_completion(event): message = event.get('last_assistant_message') # stop_hook_active means this hook already pushed back once; a second block could loop forever. if event.get('stop_hook_active') or not isinstance(message, str): return if CLAIM.search(message) and not EVIDENCE.search(message) and has_code_changes(event.get('cwd', os.getcwd())): print(json.dumps({'decision': 'block', 'reason': BLOCK_REASON})) def show_correction(event): prompt = event.get('prompt') if not isinstance(prompt, str) or len(prompt) > MAX_PROMPT_CHARS or prompt.lstrip().startswith('/'): return if CORRECTION.search(prompt): print(json.dumps({'hookSpecificOutput': {'hookEventName': 'UserPromptSubmit', 'additionalContext': CORRECTION_CONTEXT}})) def main(): handlers = {'stop': check_completion, 'prompt': show_correction} try: handlers[sys.argv[1]](read_event()) except (IndexError, KeyError, ValueError, OSError, RecursionError, subprocess.SubprocessError): return if __name__ == '__main__': main()