#!/usr/bin/env python3 """gate-check — PreToolUse hook that enforces the test-first gate. Blocks edits to *source* files while an active skepticism run is still in a pre-work phase (goal / red-oracle / oracle-review). This makes the "no production code before the tests are written and reviewed" gate ungameable by the model — prose alone gets rationalized away. Design principles: - OPT-IN: only activates when an active run exists in the project (a state.md under .skepticism/runs//). With no active run it allows everything, so the plugin never interferes with normal work in other repos. - FAIL-OPEN: any parsing/IO error allows the edit. A buggy gate must never brick the user's ability to edit files. - Edits to tests and to the run's own artifacts are always allowed — you must be able to build the oracle during the oracle phases. - TRACK-AWARE: a run may list extra paths under `- protected:` in state.md that stay blocked for the WHOLE run, not just the early phases. The model track uses this for the held-out split, which nothing in the work ever writes. """ import sys, os, json, glob, re PRE_WORK_PHASES = {"goal", "red-oracle", "oracle-review", # legacy names, so a run started before the rename still gates "spec", "red-tests", "test-review"} TEST_PATH_RE = re.compile(r"(^|/)(tests?|__tests__|spec)(/|$)|(^|/)test_|(_test|\.test|\.spec)\.", re.I) # Everything the framework owns lives under .skepticism/. The legacy location is # still read so a run started before the move can finish. RUN_ROOT = ".skepticism" LEGACY_RUN_ROOT = "docs/skepticism" STATE_GLOBS = (f"{RUN_ROOT}/runs/*/state.md", f"{LEGACY_RUN_ROOT}/*/state.md") def allow(): sys.exit(0) def deny(reason: str): print(json.dumps({ "hookSpecificOutput": { "hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": reason, } })) sys.exit(0) def main(): try: payload = json.load(sys.stdin) except Exception: allow() # fail-open tool_input = payload.get("tool_input", {}) or {} target = tool_input.get("file_path") or tool_input.get("path") or "" if not target: allow() target_abs = os.path.abspath(target) # Paths a run protects for its whole life, and runs still in a pre-work # phase. The first list is collected from every active run; the second # only from runs that have not reached phase 4. protected = [] blocking = [] for state_file in [p for g in STATE_GLOBS for p in glob.glob(g)]: try: text = open(state_file, encoding="utf-8").read() except Exception: continue for line in re.findall(r"^- protected:\s*(.+)$", text, re.M): protected += [x.strip() for x in line.split(",") if x.strip()] m = re.search(r"^- phase:\s*(\S+)", text, re.M) if m and m.group(1).strip() in PRE_WORK_PHASES: blocking.append(state_file) # A protected path is denied in every phase, including phase 4 onwards. for pattern in protected: for match in glob.glob(pattern) + glob.glob(os.path.join(pattern, "**"), recursive=True): if os.path.abspath(match) == target_abs: deny( "skepticism gate: this path is protected for the whole run " f"(matched '{pattern}'). Nothing in the work writes to it — " "on the model track this is the held-out split, and writing " "to it is leakage. Remove it from '- protected:' in state.md " "only if you are certain it is not an evaluation artefact." ) if not blocking: allow() # Always-allowed targets even during the gate: # - tests (you must write them now) # - the run's own artifacts and project config under .skepticism/ rel = os.path.relpath(target_abs, os.getcwd()) if (TEST_PATH_RE.search(rel) or rel.startswith(RUN_ROOT + os.sep) or rel.startswith(LEGACY_RUN_ROOT + os.sep)): allow() deny( "skepticism gate: an active run is in a pre-work phase " f"({', '.join(blocking)}). The deliverable is blocked until the oracle " "is built and passes the adversarial review (phase 3). Build or adjust " "the oracle, or advance the run's state to 'work' once oracle-review " "passes." ) if __name__ == "__main__": try: main() except Exception: allow() # fail-open on any unexpected error