claude-dev-env 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +3 -2
- package/agents/CLAUDE.md +1 -0
- package/agents/code-verifier.md +3 -3
- package/agents/skill-writer-agent.md +84 -0
- package/audit-rubrics/category_rubrics/category-o-docstring-vs-impl-drift.md +141 -41
- package/audit-rubrics/prompts/category-o-docstring-vs-impl-drift.md +29 -13
- package/docs/CLAUDE.md +1 -0
- package/docs/references/CLAUDE.md +1 -0
- package/docs/references/code-review-enforcement.md +97 -0
- package/docs/wsl-docker-cowork-starter-matrix.md +89 -0
- package/hooks/blocking/CLAUDE.md +8 -1
- package/hooks/blocking/code_review_enforcement_config_bootstrap.py +53 -0
- package/hooks/blocking/code_review_gate_deny.py +74 -0
- package/hooks/blocking/code_review_pr_create_gate.py +194 -0
- package/hooks/blocking/code_review_push_gate.py +140 -0
- package/hooks/blocking/code_review_stamp_directory_write_blocker.py +340 -0
- package/hooks/blocking/code_review_stamp_store.py +233 -0
- package/hooks/blocking/code_review_stamp_write_blocker_parts/__init__.py +7 -0
- package/hooks/blocking/code_review_stamp_write_blocker_parts/conftest.py +15 -0
- package/hooks/blocking/code_review_stamp_write_blocker_parts/obfuscated_stamp_path_reference.py +212 -0
- package/hooks/blocking/code_review_stamp_write_blocker_parts/split_directory_change_into_stamp.py +138 -0
- package/hooks/blocking/code_review_stamp_write_blocker_parts/test_obfuscated_stamp_path_reference.py +49 -0
- package/hooks/blocking/code_review_stamp_write_blocker_parts/test_split_directory_change_into_stamp.py +38 -0
- package/hooks/blocking/code_verifier_spawn_preflight_gate.py +39 -27
- package/hooks/blocking/config/code_review_enforcement_constants.py +110 -0
- package/hooks/blocking/config/test_code_review_enforcement_constants.py +108 -0
- package/hooks/blocking/config/verified_commit_constants.py +13 -9
- package/hooks/blocking/conftest.py +2 -0
- package/hooks/blocking/convergence_gate_blocker.py +112 -23
- package/hooks/blocking/destructive_command_blocker.py +19 -6
- package/hooks/blocking/pr_description_proof_of_work.py +52 -34
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +4 -1
- package/hooks/blocking/test_code_review_enforcement_config_bootstrap.py +62 -0
- package/hooks/blocking/test_code_review_gate_deny.py +54 -0
- package/hooks/blocking/test_code_review_pr_create_gate.py +185 -0
- package/hooks/blocking/test_code_review_push_gate.py +189 -0
- package/hooks/blocking/test_code_review_stamp_directory_write_blocker.py +180 -0
- package/hooks/blocking/test_code_review_stamp_store.py +205 -0
- package/hooks/blocking/test_code_verifier_spawn_preflight_gate.py +124 -2
- package/hooks/blocking/test_convergence_gate_blocker.py +153 -5
- package/hooks/blocking/test_destructive_command_blocker.py +1 -1
- package/hooks/blocking/test_destructive_command_blocker_deny_mode.py +45 -0
- package/hooks/blocking/test_pr_description_proof_of_work.py +151 -0
- package/hooks/blocking/test_pre_tool_use_dispatcher.py +8 -8
- package/hooks/blocking/test_verification_verdict_store.py +920 -903
- package/hooks/blocking/test_volatile_path_in_post_blocker.py +114 -2
- package/hooks/blocking/verification_verdict_store.py +27 -5
- package/hooks/blocking/verified_commit_gate_parts/gated_invocations.py +29 -17
- package/hooks/blocking/verified_commit_gate_parts/tests/test_gated_invocations.py +35 -0
- package/hooks/blocking/volatile_path_in_post_blocker.py +69 -8
- package/hooks/git-hooks/git_hooks_constants/__init__.py +6 -0
- package/hooks/git-hooks/pre_push.py +89 -2
- package/hooks/git-hooks/test_pre_push.py +103 -0
- package/hooks/hooks.json +16 -1
- package/hooks/hooks_constants/CLAUDE.md +1 -0
- package/hooks/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +8 -0
- package/hooks/hooks_constants/code_rules_path_utils_constants.py +1 -0
- package/hooks/hooks_constants/code_verifier_spawn_preflight_gate_constants.py +26 -11
- package/hooks/hooks_constants/convergence_gate_blocker_constants.py +20 -3
- package/hooks/hooks_constants/destructive_command_segment_constants.py +3 -1
- package/hooks/hooks_constants/pr_description_proof_of_work_constants.py +0 -4
- package/hooks/hooks_constants/pre_tool_use_dispatcher_constants.py +4 -0
- package/hooks/hooks_constants/pyproject_config_discovery_constants.py +16 -0
- package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +24 -0
- package/hooks/hooks_constants/test_pre_tool_use_dispatcher_constants.py +6 -0
- package/hooks/hooks_constants/volatile_path_in_post_blocker_constants.py +8 -1
- package/hooks/validators/CLAUDE.md +1 -0
- package/hooks/validators/mypy_integration.py +63 -50
- package/hooks/validators/pyproject_config_discovery.py +101 -0
- package/hooks/validators/ruff_integration.py +211 -23
- package/hooks/validators/run_all_validators.py +21 -14
- package/hooks/validators/test_mypy_integration.py +32 -0
- package/hooks/validators/test_pyproject_config_discovery.py +94 -0
- package/hooks/validators/test_ruff_integration.py +68 -1
- package/hooks/validators/test_run_all_validators.py +25 -0
- package/hooks/validators/test_run_all_validators_config_discovery.py +123 -0
- package/package.json +1 -1
- package/rules/docstring-prose-matches-implementation.md +20 -43
- package/rules/durable-post-artifacts.md +7 -0
- package/scripts/codec_forwarding_test_support.py +83 -0
- package/scripts/conftest.py +8 -0
- package/scripts/dev_env_scripts_constants/CLAUDE.md +1 -1
- package/scripts/dev_env_scripts_constants/claude_chain_constants.py +44 -1
- package/scripts/dev_env_scripts_constants/code_review_constants.py +129 -12
- package/scripts/dev_env_scripts_constants/test_code_review_constants.py +55 -0
- package/scripts/invoke_code_review.py +550 -38
- package/scripts/resolve_worker_spawn.py +8 -1
- package/scripts/test_invoke_code_review.py +298 -4
- package/scripts/test_invoke_code_review_codec.py +77 -0
- package/scripts/test_resolve_worker_spawn_codec.py +101 -0
- package/skills/autoconverge/SKILL.md +9 -3
- package/skills/autoconverge/reference/convergence.md +2 -1
- package/skills/autoconverge/reference/multi-pr.md +6 -1
- package/skills/autoconverge/reference/stop-conditions.md +16 -10
- package/skills/autoconverge/workflow/converge.clean-audit.test.mjs +1 -1
- package/skills/autoconverge/workflow/converge.codex-gate.test.mjs +175 -3
- package/skills/autoconverge/workflow/converge.contract.test.mjs +19 -0
- package/skills/autoconverge/workflow/converge.mjs +24 -8
- package/skills/autoconverge/workflow/converge_multi.mjs +7 -3
- package/skills/autoconverge/workflow/converge_multi.run-input.test.mjs +5 -0
- package/skills/fresh-branch/CLAUDE.md +2 -0
- package/skills/fresh-branch/SKILL.md +2 -0
- package/skills/fresh-branch/scripts/create_fresh_branch.py +78 -180
- package/skills/fresh-branch/scripts/fresh_branch_git_commands.py +285 -0
- package/skills/fresh-branch/scripts/fresh_branch_scripts_constants/fresh_branch_cli_constants.py +1 -0
- package/skills/fresh-branch/scripts/pytest.ini +4 -0
- package/skills/fresh-branch/scripts/test_create_fresh_branch.py +98 -0
- package/skills/fresh-branch/scripts/test_fresh_branch_git_commands.py +310 -0
- package/skills/prototype/SKILL.md +86 -0
- package/skills/prototype/reference/honest-limitations.md +23 -0
- package/skills/prototype/reference/promotion-tasks.md +23 -0
- package/skills/prototype/scripts/build_sandbox_settings.py +249 -0
- package/skills/prototype/scripts/conftest.py +15 -0
- package/skills/prototype/scripts/launch_sandbox.py +205 -0
- package/skills/prototype/scripts/probe_sandbox_safety.py +311 -0
- package/skills/prototype/scripts/prototype_scripts_constants/__init__.py +1 -0
- package/skills/prototype/scripts/prototype_scripts_constants/config/__init__.py +0 -0
- package/skills/prototype/scripts/prototype_scripts_constants/config/build_sandbox_settings_constants.py +41 -0
- package/skills/prototype/scripts/prototype_scripts_constants/config/launch_sandbox_constants.py +23 -0
- package/skills/prototype/scripts/prototype_scripts_constants/config/probe_sandbox_safety_constants.py +45 -0
- package/skills/prototype/scripts/prototype_scripts_constants/config/prototype_common_constants.py +10 -0
- package/skills/prototype/scripts/test_build_sandbox_settings.py +275 -0
- package/skills/prototype/scripts/test_launch_sandbox.py +303 -0
- package/skills/prototype/scripts/test_probe_sandbox_safety.py +284 -0
- package/skills/prototype/workflows/promotion.md +27 -0
- package/skills/prototype/workflows/sandbox.md +35 -0
- package/skills/skill-builder/CLAUDE.md +3 -3
- package/skills/skill-builder/SKILL.md +5 -5
- package/skills/skill-builder/references/CLAUDE.md +1 -1
- package/skills/skill-builder/references/delegation-map.md +3 -3
- package/skills/skill-builder/references/description-field.md +1 -1
- package/skills/skill-builder/references/skill-modularity.md +2 -3
- package/skills/skill-builder/workflows/CLAUDE.md +1 -1
- package/skills/skill-builder/workflows/improve-skill.md +1 -1
- package/skills/skill-builder/workflows/new-skill.md +2 -2
- package/skills/team-advisor/SKILL.md +2 -2
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# WSL / Docker / Cowork starter matrix
|
|
2
|
+
|
|
3
|
+
Attribution and policy surface for host memory consumers that sit under WSL2, Docker Desktop, and the Claude Cowork HCS VM. Source evidence: forensic capture summarized on [issue #256](https://github.com/jl-cmd/claude-dev-env/issues/256) (`results/05-wsl-who-started.md` in the local ram-process-sprawl evidence pack). This file records supported starters, whether each component is required for day-to-day agent work, and how to shut it down. It does **not** apply a `.wslconfig` memory cap.
|
|
4
|
+
|
|
5
|
+
## Hard rules
|
|
6
|
+
|
|
7
|
+
1. **No unmeasured `.wslconfig` memory cap.** Do not set `memory=` (or other hard caps) in `%UserProfile%\.wslconfig` until (a) the owner of the WSL VM commit is known for the workload under test and (b) a before/after private-working-set measurement is recorded on the same host boot window. A blind cap is out of scope for this matrix.
|
|
8
|
+
2. **Proven starters only.** Rows below use only process-parent, service, HCS owner, registry, and product-log evidence from the capture. Proximity without a parent edge is labeled **unknown**, not a starter claim.
|
|
9
|
+
3. **Two HCS VMs are not one.** Owner=`WSL` maps to `vmmemWSL`. Owner=`cowork-vm-*` maps to plain `vmmem`. Treat them as separate shutdown and policy targets.
|
|
10
|
+
|
|
11
|
+
## Starter matrix
|
|
12
|
+
|
|
13
|
+
| Component | Starter / owner (supported) | Required for daily agent work? | Shutdown / stop |
|
|
14
|
+
|-----------|----------------------------|--------------------------------|-----------------|
|
|
15
|
+
| **WSLService** (`wslservice.exe`) | Windows service **WSLService**, StartMode=Auto, parent `services.exe` since boot | **Platform yes** if any WSL2 distro is used; leave Auto unless WSL is retired on the host | `Stop-Service WSLService` only when deliberately disabling WSL; normal idle path is distro shutdown, not service kill |
|
|
16
|
+
| **`vmmemWSL`** (WSL2 utility VM) | HCS VM Owner=`WSL`; worker chain `vmcompute` → `vmwp` → `vmmemWSL` | **Yes while any distro is Running** (`wsl -l -v`) | `wsl --shutdown` (stops all WSL2 distros and the WSL utility VM; disrupts Docker's WSL backend too) |
|
|
17
|
+
| **First user wake of WSL VM** | **Unknown** in the capture (no Security 4688; no surviving user `wsl.exe` from the create second). Docker was **not** up yet at that create time | N/A — attribution gap | Same as `vmmemWSL` once running |
|
|
18
|
+
| **Docker Desktop / `com.docker.backend`** | Docker Desktop launches backend (product log). **HKCU Run** key registers Docker Desktop for logon. Windows service `com.docker.service` was **Stopped** / Manual — engine path is Desktop/backend user-mode | **Only when containers or Docker tooling are in active use** | Quit Docker Desktop (tray → Quit); confirm `com.docker.backend` gone. Optional: remove or disable the HKCU Run value named `Docker Desktop` so logon does not relaunch it |
|
|
19
|
+
| **Live `wsl.exe` for `docker-desktop` + Ubuntu integration** | Parent **`com.docker.backend.exe` (services)** | Same as Docker Desktop | Stop Docker Desktop; or `wsl --shutdown` (broader blast radius) |
|
|
20
|
+
| **Live `wsl.exe` for `code-index-mcp`** | Parent **`codex.exe`** ← **ChatGPT.exe** ← explorer. Command shape: `wsl.exe …/code-index-mcp` | **Only while Codex/ChatGPT needs the Ubuntu MCP indexer** | Exit Codex / ChatGPT app-server session; confirm no `wsl.exe` whose command line is `code-index-mcp` |
|
|
21
|
+
| **Plain `vmmem` (cowork HCS VM)** | HCS VM Owner=`cowork-vm-*` (name match). Chain `vmcompute` → `vmwp` → `vmmem`. **Exact user process that created the VM is unknown** (no live parent edge). Claude Desktop cmdlines carry `cowork-*` schemes; that is label association, not a proven create edge | **Only while Claude Cowork VM features are in use** | Quit Claude Desktop / Cowork UI that owns the session; if the HCS VM remains Running, treat full stop as an open procedure (see open questions). Do **not** assume `wsl --shutdown` stops this VM — it is not Owner=`WSL` |
|
|
22
|
+
| **grok as WSL parent** | **None** in the capture (high-confidence negative) | N/A | N/A |
|
|
23
|
+
| **claude as parent of live `wsl.exe`** | **None** in the capture (high-confidence negative) | N/A for WSL shells; see cowork row for the separate HCS VM | N/A for `wsl.exe` |
|
|
24
|
+
|
|
25
|
+
### Capture facts that stay fixed for this matrix
|
|
26
|
+
|
|
27
|
+
These are host-capture facts the matrix must not rewrite:
|
|
28
|
+
|
|
29
|
+
- Docker autostart comes from the **HKCU `Run` key** entry for Docker Desktop (not the stopped `com.docker.service`).
|
|
30
|
+
- **Codex `code-index-mcp`** is a live holder of `wsl.exe`; it is not the create-time owner of `vmmemWSL` when Codex starts hours later.
|
|
31
|
+
- **cowork-vm** is a **separate** HCS VM and plain `vmmem` consumer; it is not `vmmemWSL`.
|
|
32
|
+
- **grok** and **claude** were **not** parents of any live `wsl.exe` in the capture.
|
|
33
|
+
|
|
34
|
+
## Open questions
|
|
35
|
+
|
|
36
|
+
Named gaps only — do not fill these with guesses in policy or code:
|
|
37
|
+
|
|
38
|
+
1. **Exact user-mode process that first woke the WSL VM** at the `vmmemWSL` create second (Security 4688 / Sysmon not available in the capture).
|
|
39
|
+
2. **Exact process that created HCS `cowork-vm-*` / plain `vmmem`** (parent chain ends at `vmwp` / `vmcompute`).
|
|
40
|
+
3. **Whether a given Docker start was pure logon Run vs interactive tray open** (Run key proves Docker can autostart; a dead parent PID on the backend does not by itself prove which path fired).
|
|
41
|
+
4. **Identity of dead parents** of mid-session Ubuntu `wslhost` processes whose PPID is already recycled.
|
|
42
|
+
5. **Documented, safe idle-stop for the cowork HCS VM** when Claude UI is gone but `hcsdiag` still lists Owner=`cowork-vm-*` Running.
|
|
43
|
+
6. **Whether historical sessions of grok/claude started WSL earlier in a boot** — unprovable without process-creation audit history; live snapshot negatives do not extend backward.
|
|
44
|
+
|
|
45
|
+
To close (1), (2), (4), or (6): enable process-creation audit (Security 4688) or Sysmon with filters on `wsl.exe`, `vmwp.exe`, and Docker/Claude/Codex image paths, then re-capture on a clean boot.
|
|
46
|
+
|
|
47
|
+
## Policy options (with costs)
|
|
48
|
+
|
|
49
|
+
Choose explicitly. None of these options includes an unmeasured `.wslconfig` `memory=` write.
|
|
50
|
+
|
|
51
|
+
| Option | Action | RAM / sprawl effect (directional) | Cost / risk |
|
|
52
|
+
|--------|--------|-----------------------------------|-------------|
|
|
53
|
+
| **P0 — Observe only** | Keep matrix; no host change | None until a component is stopped | Continues dual-VM + Docker + Codex WSL hold when those apps run |
|
|
54
|
+
| **P1 — Docker on demand** | Remove or disable HKCU Run `Docker Desktop`; start Desktop only when containers are needed | Avoids Docker backend + docker-desktop/Ubuntu integration `wsl.exe` on boots/sessions that never use Docker | Manual start latency; first container work pays cold start; any script that assumes Docker is already up fails until launch |
|
|
55
|
+
| **P2 — Docker fully off when idle** | Quit Docker Desktop after use; optional P1 | Frees backend private set and Docker-held WSL distro activity | Must re-open Desktop before compose/build; `wsl -l -v` may still show Running until `wsl --shutdown` or Docker stops holding distros |
|
|
56
|
+
| **P3 — Codex indexer off when idle** | Exit ChatGPT/Codex app-server when not reviewing; disable code-index MCP if product settings allow | Drops Codex-held `wsl.exe … code-index-mcp` edges | Codex features that need the Ubuntu indexer fail until restart; does **not** by itself tear down `vmmemWSL` if Docker or another client still holds a distro |
|
|
57
|
+
| **P4 — WSL idle shutdown** | When no Docker/Codex/other WSL client is needed: `wsl --shutdown` | Tears down Ubuntu + docker-desktop distros and `vmmemWSL` | **Breaks** any live Docker WSL backend and any in-distro MCP until restart; never use mid-task if containers or WSL MCP are active |
|
|
58
|
+
| **P5 — Cowork VM idle policy** | Quit Claude Cowork/Desktop when the VM is not needed; re-check `hcsdiag list` for Owner=`cowork-vm-*` | Targets plain `vmmem` (~multi-GB private in the capture) without touching Owner=`WSL` | Create/stop procedure for a leftover Running cowork VM is still an open question; wrong kill path can disrupt Cowork artifacts |
|
|
59
|
+
| **P6 — Process-creation audit** | Turn on 4688 or Sysmon for wsl/vmwp/Docker/Claude/Codex | No direct RAM win; closes open questions (1)(2)(4)(6) | Audit volume, privacy review, and storage for event logs |
|
|
60
|
+
| **P7 — `.wslconfig` memory cap** | Set `memory=` under `[wsl2]` | Caps WSL utility VM commit **only after** measured before/after on this host | **Blocked here until measured.** Risk: OOM inside distros, Docker backend instability, false “fix” that leaves cowork `vmmem` untouched |
|
|
61
|
+
|
|
62
|
+
### Measurement gate for P7 (and any memory cap)
|
|
63
|
+
|
|
64
|
+
Before any `.wslconfig` memory write:
|
|
65
|
+
|
|
66
|
+
1. Record `wsl -l -v`, `hcsdiag list`, and private working set for `vmmemWSL` / plain `vmmem` / `com.docker.backend`.
|
|
67
|
+
2. Name which component the cap is meant to bound (Owner=`WSL` only — cowork is outside `.wslconfig`).
|
|
68
|
+
3. Apply cap; reboot or `wsl --shutdown` + re-start workload as required for the setting to apply.
|
|
69
|
+
4. Re-record the same counters under the same workload.
|
|
70
|
+
5. Commit the before/after note next to the policy decision (issue comment or evidence pack). Without that note, leave `.wslconfig` without a memory cap.
|
|
71
|
+
|
|
72
|
+
## Quick identification commands
|
|
73
|
+
|
|
74
|
+
Read-only checks agents and operators use on Windows:
|
|
75
|
+
|
|
76
|
+
```text
|
|
77
|
+
wsl -l -v
|
|
78
|
+
hcsdiag list
|
|
79
|
+
Get-CimInstance Win32_Service -Filter "Name='WSLService'"
|
|
80
|
+
Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Run
|
|
81
|
+
Get-CimInstance Win32_Process -Filter "Name='wsl.exe'" | Select ProcessId, ParentProcessId, CommandLine
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Resolve each `wsl.exe` ParentProcessId to an image name before blaming an agent binary. A missing parent is **unknown**, not proof of a named starter.
|
|
85
|
+
|
|
86
|
+
## Related
|
|
87
|
+
|
|
88
|
+
- Issue: [jl-cmd/claude-dev-env#256](https://github.com/jl-cmd/claude-dev-env/issues/256) (child of epic #252)
|
|
89
|
+
- Local forensic write-up: `results/05-wsl-who-started.md` under the host evidence pack path named in the issue body
|
package/hooks/blocking/CLAUDE.md
CHANGED
|
@@ -9,6 +9,7 @@ PreToolUse hooks that deny (block) tool calls when a rule is violated. The main
|
|
|
9
9
|
| `config/` | Shared constants for the verified-commit gate family (`verified_commit_constants.py`) |
|
|
10
10
|
| `tdd_enforcer_parts/` | Concern modules the `tdd_enforcer.py` entry hook wires together: path classification, content analysis, candidate-path resolution, freshness, git-tracking restore detection, decisions, and constants |
|
|
11
11
|
| `verified_commit_gate_parts/` | Concern modules the `verified_commit_gate.py` entry hook wires together: command tokenization, directory-change resolution, gated git-invocation resolution, deny-reason resolution, and deny-payload assembly |
|
|
12
|
+
| `code_review_stamp_write_blocker_parts/` | Concern modules the `code_review_stamp_directory_write_blocker.py` entry hook wires together: the split directory-change-into-stamp matcher and the obfuscated-stamp-path-write matcher |
|
|
12
13
|
| `claude_md_orphan_file_blocker_parts/` | Concern modules the `claude_md_orphan_file_blocker.py` entry hook wires together: reference extraction, subtree scan, scan plan, decision, and constants |
|
|
13
14
|
| `package_inventory_stale_blocker_parts/` | Concern modules the `package_inventory_stale_blocker.py` entry hook wires together: inventory detection, decision, and constants |
|
|
14
15
|
| `inventory_intent_records/` | The shared per-session pending-intent store both inventory blockers read to break the file/row add-order deadlock |
|
|
@@ -71,7 +72,10 @@ The check modules it calls are the `code_rules_<concern>.py` files below.
|
|
|
71
72
|
| `block_main_commit.py` | PreToolUse (Bash) | `git commit`/`git push` directly to `main` |
|
|
72
73
|
| `bot_mention_comment_blocker.py` | PreToolUse (Write/Edit) | PR review comments that @-mention a bot |
|
|
73
74
|
| `claude_md_orphan_file_blocker.py` | PreToolUse (Write/Edit/MultiEdit) | Per-directory `CLAUDE.md` table cells naming a bare filename absent from the directory subtree |
|
|
74
|
-
| `
|
|
75
|
+
| `code_review_pr_create_gate.py` | PreToolUse (Bash/MCP GitHub) | `gh pr create` or the MCP `create_pull_request` tool without a clean `xhigh` code-review stamp covering the branch surface |
|
|
76
|
+
| `code_review_push_gate.py` | PreToolUse (Bash/PowerShell) | `git push` without a clean `low` code-review stamp covering the branch surface |
|
|
77
|
+
| `code_review_stamp_directory_write_blocker.py` | PreToolUse (Bash/PowerShell/Write/Edit/MultiEdit) | Shell or file-tool writes into `~/.claude/code-review-stamps/`, and shell references to the stamp store module or its mint call, outside the sanctioned invoker |
|
|
78
|
+
| `code_verifier_spawn_preflight_gate.py` | PreToolUse (Agent) | Spawning the `code-verifier` subagent when the branch has a merge conflict vs its base or a CODE_RULES violation on a working-tree-added line, or the CODE_RULES engine fails to load |
|
|
75
79
|
| `convergence_gate_blocker.py` | PreToolUse (Bash) | Convergence workflow actions on a conflicting PR |
|
|
76
80
|
| `conventional_pr_title_gate.py` | PreToolUse (Bash) | `gh pr create`/`gh pr edit` with a `--title` that is not a Conventional Commit, in a repo whose CI runs a semantic-pull-request title check |
|
|
77
81
|
| `destructive_command_blocker.py` | PreToolUse (Bash/PowerShell) | Shell commands with destructive literals (`rm -rf`, `git reset --hard`, etc.) |
|
|
@@ -121,6 +125,9 @@ The check modules it calls are the `code_rules_<concern>.py` files below.
|
|
|
121
125
|
| File | Role |
|
|
122
126
|
|---|---|
|
|
123
127
|
| `_gh_body_arg_utils.py` | Parsing helpers for `gh_body_arg_blocker.py` |
|
|
128
|
+
| `code_review_enforcement_config_bootstrap.py` | Binds `config.code_review_enforcement_constants` to the sibling `config/` file by explicit location, so the code-review gate family resolves its constants regardless of a foreign `config` package's `sys.path` order |
|
|
129
|
+
| `code_review_gate_deny.py` | Shared deny scaffold for the push and PR-create code-review gates: the `hookSpecificOutput` deny-payload builder and the log-and-emit helper, so both gates share one deny shape |
|
|
130
|
+
| `code_review_stamp_store.py` | Reads and writes the per-work-tree code-review stamp files under `~/.claude/code-review-stamps/`, and decides whether a clean stamp at the needed effort covers the live branch surface |
|
|
124
131
|
| `pr_description_body_audit.py` | Body audit logic for `pr_description_enforcer.py` |
|
|
125
132
|
| `pr_description_command_parser.py` | `gh` command parsing for `pr_description_enforcer.py` |
|
|
126
133
|
| `pr_description_pr_number.py` | PR number extraction logic |
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Deterministic loader for the code-review enforcement constants module.
|
|
2
|
+
|
|
3
|
+
The stamp store and every code-review gate import their shared constants as
|
|
4
|
+
``from config.code_review_enforcement_constants import ...``. In the installed
|
|
5
|
+
hook tree a second, unrelated ``config`` package can sit ahead of this package
|
|
6
|
+
on ``sys.path`` and win that dotted name by path order, so the import binds to
|
|
7
|
+
the wrong file and raises ImportError on any constant the stale copy lacks.
|
|
8
|
+
This module binds the dotted name to its sibling ``config/`` file by explicit
|
|
9
|
+
location, so resolution never depends on ``sys.path`` order.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import importlib.util
|
|
15
|
+
import sys
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def register_code_review_enforcement_constants() -> None:
|
|
20
|
+
"""Bind the code-review enforcement constants dotted name to its config file.
|
|
21
|
+
|
|
22
|
+
::
|
|
23
|
+
|
|
24
|
+
sys.path = ["<installed hooks>", "<blocking>"] # foreign config first
|
|
25
|
+
register_code_review_enforcement_constants()
|
|
26
|
+
from config.code_review_enforcement_constants import STAMP_DIRECTORY_NAME
|
|
27
|
+
ok: bound to <blocking>/config/code_review_enforcement_constants.py
|
|
28
|
+
|
|
29
|
+
A ``from config.code_review_enforcement_constants import`` that follows this
|
|
30
|
+
call reads the entry straight from ``sys.modules``, so it resolves to this
|
|
31
|
+
file whatever else owns the ``config`` name. An entry already cached is left
|
|
32
|
+
in place, so the call is idempotent and never displaces a module a caller
|
|
33
|
+
loaded on purpose.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
None. The effect is the ``sys.modules`` registration.
|
|
37
|
+
"""
|
|
38
|
+
_bind_config_module_by_location(
|
|
39
|
+
"config.code_review_enforcement_constants",
|
|
40
|
+
Path(__file__).resolve().parent / "config" / "code_review_enforcement_constants.py",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _bind_config_module_by_location(dotted_name: str, constants_file_path: Path) -> None:
|
|
45
|
+
"""Register a dotted config name against an explicit file path in sys.modules."""
|
|
46
|
+
if dotted_name in sys.modules:
|
|
47
|
+
return
|
|
48
|
+
module_spec = importlib.util.spec_from_file_location(dotted_name, constants_file_path)
|
|
49
|
+
if module_spec is None or module_spec.loader is None:
|
|
50
|
+
return
|
|
51
|
+
constants_module = importlib.util.module_from_spec(module_spec)
|
|
52
|
+
sys.modules[dotted_name] = constants_module
|
|
53
|
+
module_spec.loader.exec_module(constants_module)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Shared deny scaffold for the code-review PreToolUse gates.
|
|
2
|
+
|
|
3
|
+
The push gate and the PR-create gate deny a blocked tool call the same way: a
|
|
4
|
+
``hookSpecificOutput`` payload carrying the deny decision and a corrective
|
|
5
|
+
reason, logged through ``log_hook_block`` before it reaches stdout. This module
|
|
6
|
+
holds that one payload builder and the log-and-emit helper, so the two gates
|
|
7
|
+
share a single deny shape and stay in step.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import sys
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
_blocking_directory = str(Path(__file__).resolve().parent)
|
|
18
|
+
_hooks_directory = str(Path(__file__).resolve().parent.parent)
|
|
19
|
+
for each_bootstrap_directory in (_blocking_directory, _hooks_directory):
|
|
20
|
+
if each_bootstrap_directory not in sys.path:
|
|
21
|
+
sys.path.insert(0, each_bootstrap_directory)
|
|
22
|
+
|
|
23
|
+
from code_review_enforcement_config_bootstrap import (
|
|
24
|
+
register_code_review_enforcement_constants,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
register_code_review_enforcement_constants()
|
|
28
|
+
|
|
29
|
+
from config.code_review_enforcement_constants import (
|
|
30
|
+
DENY_PERMISSION_DECISION,
|
|
31
|
+
PRE_TOOL_USE_HOOK_EVENT_NAME,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
from hooks_constants.hook_block_logger import log_hook_block
|
|
35
|
+
except ImportError as import_error:
|
|
36
|
+
raise ImportError(
|
|
37
|
+
"the code_review_gate_deny dependencies did not import; "
|
|
38
|
+
"ensure the hooks directory is importable."
|
|
39
|
+
) from import_error
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def build_code_review_deny_payload(deny_reason: str) -> dict[str, dict[str, str]]:
|
|
43
|
+
"""Build the PreToolUse deny payload for a blocked code-review gate action.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
deny_reason: The corrective message naming why the action is denied.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
The ``hookSpecificOutput`` deny payload.
|
|
50
|
+
"""
|
|
51
|
+
return {
|
|
52
|
+
"hookSpecificOutput": {
|
|
53
|
+
"hookEventName": PRE_TOOL_USE_HOOK_EVENT_NAME,
|
|
54
|
+
"permissionDecision": DENY_PERMISSION_DECISION,
|
|
55
|
+
"permissionDecisionReason": deny_reason,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def log_and_emit_code_review_deny(deny_reason: str, tool_name: str, hook_module_name: str) -> None:
|
|
61
|
+
"""Log a code-review gate block and write its deny payload to stdout.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
deny_reason: The corrective message naming why the action is denied.
|
|
65
|
+
tool_name: The name of the gated tool, recorded in the block log.
|
|
66
|
+
hook_module_name: The calling gate's module name, recorded in the log.
|
|
67
|
+
"""
|
|
68
|
+
log_hook_block(
|
|
69
|
+
calling_hook_name=hook_module_name,
|
|
70
|
+
hook_event=PRE_TOOL_USE_HOOK_EVENT_NAME,
|
|
71
|
+
block_reason=deny_reason,
|
|
72
|
+
tool_name=tool_name if isinstance(tool_name, str) else None,
|
|
73
|
+
)
|
|
74
|
+
sys.stdout.write(json.dumps(build_code_review_deny_payload(deny_reason)) + "\n")
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""PreToolUse gate: PR create lands only behind a clean xhigh code-review stamp.
|
|
2
|
+
|
|
3
|
+
Fires on Bash/PowerShell ``gh pr create`` and on the MCP
|
|
4
|
+
``create_pull_request`` tool. Resolves the repository from the session
|
|
5
|
+
working directory (or the payload cwd), computes the live change-surface
|
|
6
|
+
hash, and allows the create only when a clean stamp at effort ``xhigh`` or
|
|
7
|
+
higher covers that exact hash under ``~/.claude/code-review-stamps/``.
|
|
8
|
+
|
|
9
|
+
Empty surfaces and repos with no resolvable upstream base are out of scope
|
|
10
|
+
and allowed. Push lands behind ``code_review_push_gate`` and commit behind
|
|
11
|
+
``verified_commit_gate``; this gate covers pull-request creation only.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
import re
|
|
18
|
+
import sys
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
_blocking_directory = str(Path(__file__).resolve().parent)
|
|
23
|
+
_hooks_directory = str(Path(__file__).resolve().parent.parent)
|
|
24
|
+
for each_bootstrap_directory in (_blocking_directory, _hooks_directory):
|
|
25
|
+
if each_bootstrap_directory not in sys.path:
|
|
26
|
+
sys.path.insert(0, each_bootstrap_directory)
|
|
27
|
+
|
|
28
|
+
from code_review_enforcement_config_bootstrap import (
|
|
29
|
+
register_code_review_enforcement_constants,
|
|
30
|
+
)
|
|
31
|
+
from verified_commit_config_bootstrap import register_verified_commit_constants
|
|
32
|
+
|
|
33
|
+
register_code_review_enforcement_constants()
|
|
34
|
+
register_verified_commit_constants()
|
|
35
|
+
|
|
36
|
+
from code_review_gate_deny import (
|
|
37
|
+
build_code_review_deny_payload,
|
|
38
|
+
log_and_emit_code_review_deny,
|
|
39
|
+
)
|
|
40
|
+
from code_review_stamp_store import live_surface_hash, stamp_covers_surface
|
|
41
|
+
from config.code_review_enforcement_constants import (
|
|
42
|
+
ALL_GATED_SHELL_TOOL_NAMES,
|
|
43
|
+
GH_PR_CREATE_INVOCATION_PATTERN,
|
|
44
|
+
HASH_PREVIEW_LENGTH,
|
|
45
|
+
MCP_CREATE_PULL_REQUEST_TOOL_NAME,
|
|
46
|
+
PR_CREATE_GATE_CORRECTIVE_MESSAGE,
|
|
47
|
+
PR_CREATE_GATE_HOOK_MODULE_NAME,
|
|
48
|
+
PR_CREATE_REQUIRED_EFFORT,
|
|
49
|
+
)
|
|
50
|
+
from verification_verdict_store import resolve_repo_root
|
|
51
|
+
except ImportError as import_error:
|
|
52
|
+
raise ImportError(
|
|
53
|
+
"the code_review_pr_create_gate dependencies did not import; "
|
|
54
|
+
"ensure the hooks directory is importable."
|
|
55
|
+
) from import_error
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _strip_quoted_regions(command_text: str) -> str:
|
|
59
|
+
"""Blank out single- and double-quoted spans so prose is not gated."""
|
|
60
|
+
quote_pattern = re.compile(r"\"[^\"]*\"|'[^']*'")
|
|
61
|
+
return quote_pattern.sub(lambda each_match: " " * len(each_match.group(0)), command_text)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def command_invokes_gh_pr_create(command_text: str) -> bool:
|
|
65
|
+
"""Decide whether a shell command invokes ``gh pr create``.
|
|
66
|
+
|
|
67
|
+
::
|
|
68
|
+
|
|
69
|
+
gh pr create --title T --body-file b.md -> True
|
|
70
|
+
echo "gh pr create" -> False (quoted prose)
|
|
71
|
+
gh pr edit 1 --title T -> False
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
command_text: The raw command string from the tool payload.
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
True when the command contains a real ``gh pr create`` invocation.
|
|
78
|
+
"""
|
|
79
|
+
quote_stripped_command = _strip_quoted_regions(command_text)
|
|
80
|
+
invocation_pattern = re.compile(GH_PR_CREATE_INVOCATION_PATTERN, re.IGNORECASE)
|
|
81
|
+
return invocation_pattern.search(quote_stripped_command) is not None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def is_mcp_create_pull_request_tool(tool_name: str) -> bool:
|
|
85
|
+
"""Decide whether a tool name is the MCP create-pull-request tool.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
tool_name: The PreToolUse ``tool_name`` field.
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
True when the name is the configured MCP create-PR tool.
|
|
92
|
+
"""
|
|
93
|
+
return tool_name == MCP_CREATE_PULL_REQUEST_TOOL_NAME
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def deny_reason_for_directory(target_directory: str) -> str | None:
|
|
97
|
+
"""Decide whether a PR create from a directory must be blocked.
|
|
98
|
+
|
|
99
|
+
::
|
|
100
|
+
|
|
101
|
+
no repo / empty surface / covering xhigh stamp -> None (allow)
|
|
102
|
+
production surface without xhigh-or-higher stamp -> corrective (deny)
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
target_directory: The session or repo directory the create targets.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
The deny reason when the surface needs a clean xhigh stamp and none
|
|
109
|
+
covers it; None when the create may proceed.
|
|
110
|
+
"""
|
|
111
|
+
repo_root = resolve_repo_root(target_directory)
|
|
112
|
+
if repo_root is None:
|
|
113
|
+
return None
|
|
114
|
+
surface_hash = live_surface_hash(repo_root)
|
|
115
|
+
if surface_hash is None:
|
|
116
|
+
return None
|
|
117
|
+
if stamp_covers_surface(repo_root, surface_hash, PR_CREATE_REQUIRED_EFFORT):
|
|
118
|
+
return None
|
|
119
|
+
hash_preview = surface_hash[:HASH_PREVIEW_LENGTH]
|
|
120
|
+
return (
|
|
121
|
+
f"{PR_CREATE_GATE_CORRECTIVE_MESSAGE} (repo: {repo_root}, surface sha256 {hash_preview}...)"
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _deny_for_directory_or_none(
|
|
126
|
+
session_directory: str,
|
|
127
|
+
) -> dict[str, dict[str, str]] | None:
|
|
128
|
+
"""Return the deny payload for a directory that lacks a covering stamp."""
|
|
129
|
+
deny_reason = deny_reason_for_directory(session_directory)
|
|
130
|
+
if deny_reason is None:
|
|
131
|
+
return None
|
|
132
|
+
return build_code_review_deny_payload(deny_reason)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _shell_command_targets_pr_create(all_pretooluse_payload: dict[str, object]) -> bool:
|
|
136
|
+
"""Decide whether a shell payload carries a real ``gh pr create``."""
|
|
137
|
+
tool_input = all_pretooluse_payload.get("tool_input", {})
|
|
138
|
+
if not isinstance(tool_input, dict):
|
|
139
|
+
return False
|
|
140
|
+
command_text = tool_input.get("command", "")
|
|
141
|
+
if not isinstance(command_text, str) or not command_text:
|
|
142
|
+
return False
|
|
143
|
+
return command_invokes_gh_pr_create(command_text)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def decision_for_payload(
|
|
147
|
+
all_pretooluse_payload: dict[str, object],
|
|
148
|
+
) -> dict[str, dict[str, str]] | None:
|
|
149
|
+
"""Build the deny decision for a gated PR-create attempt.
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
all_pretooluse_payload: The PreToolUse hook payload.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
The deny decision mapping when the create lacks a covering xhigh
|
|
156
|
+
stamp; None when the create may proceed.
|
|
157
|
+
"""
|
|
158
|
+
tool_name = all_pretooluse_payload.get("tool_name", "")
|
|
159
|
+
if not isinstance(tool_name, str):
|
|
160
|
+
return None
|
|
161
|
+
session_directory = all_pretooluse_payload.get("cwd", ".")
|
|
162
|
+
if not isinstance(session_directory, str):
|
|
163
|
+
session_directory = "."
|
|
164
|
+
if is_mcp_create_pull_request_tool(tool_name):
|
|
165
|
+
return _deny_for_directory_or_none(session_directory)
|
|
166
|
+
if tool_name not in ALL_GATED_SHELL_TOOL_NAMES:
|
|
167
|
+
return None
|
|
168
|
+
if not _shell_command_targets_pr_create(all_pretooluse_payload):
|
|
169
|
+
return None
|
|
170
|
+
return _deny_for_directory_or_none(session_directory)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def main() -> None:
|
|
174
|
+
"""Read the PreToolUse payload and decide whether to allow the PR create."""
|
|
175
|
+
try:
|
|
176
|
+
pretooluse_payload = json.load(sys.stdin)
|
|
177
|
+
except json.JSONDecodeError:
|
|
178
|
+
return
|
|
179
|
+
deny_decision = decision_for_payload(pretooluse_payload)
|
|
180
|
+
if deny_decision is None:
|
|
181
|
+
return
|
|
182
|
+
tool_name = pretooluse_payload.get("tool_name", "")
|
|
183
|
+
deny_reason = deny_decision["hookSpecificOutput"]["permissionDecisionReason"]
|
|
184
|
+
if not isinstance(deny_reason, str):
|
|
185
|
+
return
|
|
186
|
+
log_and_emit_code_review_deny(
|
|
187
|
+
deny_reason,
|
|
188
|
+
tool_name if isinstance(tool_name, str) else "",
|
|
189
|
+
PR_CREATE_GATE_HOOK_MODULE_NAME,
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
if __name__ == "__main__":
|
|
194
|
+
main()
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""PreToolUse gate: git push lands only behind a clean low code-review stamp.
|
|
2
|
+
|
|
3
|
+
Fires on Bash/PowerShell tool calls. When the command carries a ``git push``,
|
|
4
|
+
the gate resolves each repository the push targets, computes the live
|
|
5
|
+
change-surface hash against the merge base, and allows the push only when a
|
|
6
|
+
clean stamp at effort ``low`` or higher covers that exact hash under
|
|
7
|
+
``~/.claude/code-review-stamps/``.
|
|
8
|
+
|
|
9
|
+
Allowed without a stamp:
|
|
10
|
+
|
|
11
|
+
- a command carrying the bypass marker (``# code-review-skip``) as a trailing
|
|
12
|
+
shell comment,
|
|
13
|
+
- a repository with no resolvable upstream base,
|
|
14
|
+
- a mechanically exempt surface (docs and image files, pytest test files, and
|
|
15
|
+
Python files whose docstring-stripped AST is unchanged), and
|
|
16
|
+
- a surface a clean ``low``-or-higher stamp already covers.
|
|
17
|
+
|
|
18
|
+
Commit lands behind ``verified_commit_gate``; only push is gated here.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import json
|
|
24
|
+
import sys
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
_blocking_directory = str(Path(__file__).resolve().parent)
|
|
29
|
+
_hooks_directory = str(Path(__file__).resolve().parent.parent)
|
|
30
|
+
for each_bootstrap_directory in (_blocking_directory, _hooks_directory):
|
|
31
|
+
if each_bootstrap_directory not in sys.path:
|
|
32
|
+
sys.path.insert(0, each_bootstrap_directory)
|
|
33
|
+
|
|
34
|
+
from code_review_enforcement_config_bootstrap import (
|
|
35
|
+
register_code_review_enforcement_constants,
|
|
36
|
+
)
|
|
37
|
+
from verified_commit_config_bootstrap import register_verified_commit_constants
|
|
38
|
+
|
|
39
|
+
register_code_review_enforcement_constants()
|
|
40
|
+
register_verified_commit_constants()
|
|
41
|
+
|
|
42
|
+
from code_review_gate_deny import log_and_emit_code_review_deny
|
|
43
|
+
from code_review_stamp_store import live_surface_hash, stamp_covers_surface
|
|
44
|
+
from config.code_review_enforcement_constants import (
|
|
45
|
+
ALL_GATED_SHELL_TOOL_NAMES,
|
|
46
|
+
CODE_REVIEW_BYPASS_MARKER,
|
|
47
|
+
GATED_PUSH_SUBCOMMANDS,
|
|
48
|
+
HASH_PREVIEW_LENGTH,
|
|
49
|
+
PUSH_GATE_CORRECTIVE_MESSAGE,
|
|
50
|
+
PUSH_GATE_HOOK_MODULE_NAME,
|
|
51
|
+
PUSH_REQUIRED_EFFORT,
|
|
52
|
+
)
|
|
53
|
+
from verification_verdict_store import (
|
|
54
|
+
is_verification_exempt_diff,
|
|
55
|
+
resolve_merge_base,
|
|
56
|
+
resolve_repo_root,
|
|
57
|
+
)
|
|
58
|
+
from verified_commit_gate_parts.command_tokenization import (
|
|
59
|
+
command_carries_trailing_marker,
|
|
60
|
+
)
|
|
61
|
+
from verified_commit_gate_parts.gated_invocations import gated_repo_directories
|
|
62
|
+
except ImportError as import_error:
|
|
63
|
+
raise ImportError(
|
|
64
|
+
"the code_review_push_gate dependencies did not import; "
|
|
65
|
+
"ensure the hooks directory is importable."
|
|
66
|
+
) from import_error
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def deny_reason_for_directory(target_directory: str) -> str | None:
|
|
70
|
+
"""Decide whether a push from a directory must be blocked.
|
|
71
|
+
|
|
72
|
+
Allowed when the directory is no repo, has no upstream base, is a
|
|
73
|
+
mechanically exempt surface, or a clean low-or-higher stamp covers it.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
target_directory: The directory the push targets.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
The deny reason when the surface lacks a covering low stamp, else None.
|
|
80
|
+
"""
|
|
81
|
+
repo_root = resolve_repo_root(target_directory)
|
|
82
|
+
if repo_root is None:
|
|
83
|
+
return None
|
|
84
|
+
merge_base_sha = resolve_merge_base(repo_root)
|
|
85
|
+
if merge_base_sha is None:
|
|
86
|
+
return None
|
|
87
|
+
if is_verification_exempt_diff(repo_root, merge_base_sha):
|
|
88
|
+
return None
|
|
89
|
+
surface_hash = live_surface_hash(repo_root)
|
|
90
|
+
if surface_hash is None:
|
|
91
|
+
return None
|
|
92
|
+
if stamp_covers_surface(repo_root, surface_hash, PUSH_REQUIRED_EFFORT):
|
|
93
|
+
return None
|
|
94
|
+
hash_preview = surface_hash[:HASH_PREVIEW_LENGTH]
|
|
95
|
+
return f"{PUSH_GATE_CORRECTIVE_MESSAGE} (repo: {repo_root}, surface sha256 {hash_preview}...)"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _emit_first_denied_directory(command_text: str, session_directory: str, tool_name: str) -> None:
|
|
99
|
+
"""Deny the push against the first target directory that lacks a stamp."""
|
|
100
|
+
all_target_directories = gated_repo_directories(
|
|
101
|
+
command_text, session_directory, all_gated_subcommands=GATED_PUSH_SUBCOMMANDS
|
|
102
|
+
)
|
|
103
|
+
for each_target_directory in all_target_directories:
|
|
104
|
+
deny_reason = deny_reason_for_directory(each_target_directory)
|
|
105
|
+
if deny_reason is None:
|
|
106
|
+
continue
|
|
107
|
+
log_and_emit_code_review_deny(deny_reason, tool_name, PUSH_GATE_HOOK_MODULE_NAME)
|
|
108
|
+
return
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def main() -> None:
|
|
112
|
+
"""Read the PreToolUse payload and decide whether to allow the push.
|
|
113
|
+
|
|
114
|
+
Allows the command without a stamp when it carries the bypass marker
|
|
115
|
+
(``CODE_REVIEW_BYPASS_MARKER``) as a genuine trailing shell comment;
|
|
116
|
+
otherwise denies a push whose surface lacks a covering low stamp.
|
|
117
|
+
"""
|
|
118
|
+
try:
|
|
119
|
+
pretooluse_payload = json.load(sys.stdin)
|
|
120
|
+
except json.JSONDecodeError:
|
|
121
|
+
return
|
|
122
|
+
tool_name = pretooluse_payload.get("tool_name", "")
|
|
123
|
+
if tool_name not in ALL_GATED_SHELL_TOOL_NAMES:
|
|
124
|
+
return
|
|
125
|
+
tool_input = pretooluse_payload.get("tool_input", {})
|
|
126
|
+
if not isinstance(tool_input, dict):
|
|
127
|
+
return
|
|
128
|
+
command_text = tool_input.get("command", "")
|
|
129
|
+
if not isinstance(command_text, str) or not command_text:
|
|
130
|
+
return
|
|
131
|
+
if command_carries_trailing_marker(command_text, CODE_REVIEW_BYPASS_MARKER):
|
|
132
|
+
return
|
|
133
|
+
session_directory = pretooluse_payload.get("cwd", ".")
|
|
134
|
+
if not isinstance(session_directory, str):
|
|
135
|
+
session_directory = "."
|
|
136
|
+
_emit_first_denied_directory(command_text, session_directory, tool_name)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
if __name__ == "__main__":
|
|
140
|
+
main()
|