claude-dev-env 8.116.1 → 8.116.3
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/docs/rule-guides/proof-before-pull-request.md +4 -2
- package/hooks/blocking/followup_pr_dedupe.py +118 -33
- package/hooks/blocking/gate_run_reads.py +286 -0
- package/hooks/blocking/pr_lifecycle_skill_gate.py +33 -33
- package/hooks/blocking/pull_request_proof.py +60 -63
- package/hooks/blocking/test_followup_pr_dedupe.py +56 -0
- package/hooks/blocking/test_gate_run_reads.py +300 -0
- package/hooks/blocking/test_pr_lifecycle_skill_gate.py +89 -1
- package/hooks/blocking/test_pull_request_proof.py +63 -2
- package/hooks/features/conduct-gates.md +1 -1
- package/hooks/hooks.json +1 -1
- package/hooks/hooks_constants/gate_run_reads_constants.py +10 -0
- package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +0 -4
- package/hooks/pyproject.toml +1 -0
- package/package.json +1 -1
|
@@ -7,17 +7,24 @@ from io import BytesIO, StringIO, TextIOWrapper
|
|
|
7
7
|
from pathlib import Path
|
|
8
8
|
from unittest.mock import patch
|
|
9
9
|
|
|
10
|
+
import pytest
|
|
11
|
+
|
|
10
12
|
HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
|
|
11
13
|
if str(HOOKS_DIRECTORY) not in sys.path:
|
|
12
14
|
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
13
15
|
|
|
14
16
|
from blocking import followup_pr_dedupe as gate_dedupe
|
|
17
|
+
from blocking import gate_run_reads
|
|
15
18
|
from blocking import pr_lifecycle_skill_gate as gate
|
|
19
|
+
from blocking.gate_run_reads import read_deadlines_started
|
|
20
|
+
from hooks_constants.followup_pr_dedupe_constants import ALL_GIT_REMOTE_COMMAND_WORDS
|
|
21
|
+
from hooks_constants.gate_run_reads_constants import PROOF_AND_LOOK_READS_SECONDS, READS_RAN_OUT_SENTENCE
|
|
16
22
|
from hooks_constants.pr_lifecycle_skill_gate_constants import DENY_REASON
|
|
17
23
|
from hooks_constants.pull_request_proof_constants import (
|
|
18
24
|
MISSING_BUILD_EVAL_REASON,
|
|
19
25
|
MISSING_PROOF_REASON,
|
|
20
26
|
UNPROVEN_PROOF_REASON,
|
|
27
|
+
UNREADABLE_CHANGES_REASON,
|
|
21
28
|
)
|
|
22
29
|
|
|
23
30
|
WHAT_THIS_DOES_SECTION = (
|
|
@@ -309,7 +316,7 @@ def test_hook_has_its_own_pre_tool_use_registration() -> None:
|
|
|
309
316
|
{
|
|
310
317
|
"type": "command",
|
|
311
318
|
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/blocking/pr_lifecycle_skill_gate.py",
|
|
312
|
-
"timeout":
|
|
319
|
+
"timeout": 30,
|
|
313
320
|
}
|
|
314
321
|
],
|
|
315
322
|
}
|
|
@@ -354,6 +361,87 @@ def test_loaded_skill_allows_a_followup_when_the_read_fails(tmp_path: Path) -> N
|
|
|
354
361
|
assert gate.decision_for(payload) is None
|
|
355
362
|
|
|
356
363
|
|
|
364
|
+
PICTURE_LINE = "\n"
|
|
365
|
+
CREATE_WORDS = " ".join(("gh", "pr", "create"))
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _unstarted_children(monkeypatch: pytest.MonkeyPatch) -> list[list[str]]:
|
|
369
|
+
all_asked_commands: list[list[str]] = []
|
|
370
|
+
|
|
371
|
+
def refuse_to_start(all_command_words: list[str], **all_options: object) -> None:
|
|
372
|
+
all_asked_commands.append(list(all_command_words))
|
|
373
|
+
raise OSError("this test starts no child")
|
|
374
|
+
|
|
375
|
+
monkeypatch.setattr(gate_run_reads.subprocess, "Popen", refuse_to_start)
|
|
376
|
+
return all_asked_commands
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def _origin_reads(all_asked_commands: list[list[str]]) -> int:
|
|
380
|
+
return sum(each_command == list(ALL_GIT_REMOTE_COMMAND_WORDS) for each_command in all_asked_commands)
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def _quoted_shell_create_without_repository(tmp_path: Path, body: str) -> dict[str, object]:
|
|
384
|
+
(tmp_path / "body.md").write_text(body, encoding="utf-8")
|
|
385
|
+
return {
|
|
386
|
+
"tool_name": "Bash",
|
|
387
|
+
"tool_input": {"command": CREATE_WORDS + ' --title "fix: quoted title" --body-file body.md'},
|
|
388
|
+
"cwd": str(tmp_path),
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def test_one_gate_run_should_read_the_origin_once_for_a_followup_body(
|
|
393
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
394
|
+
) -> None:
|
|
395
|
+
all_asked_commands = _unstarted_children(monkeypatch)
|
|
396
|
+
payload = _quoted_shell_create_without_repository(tmp_path, PROVEN_FOLLOWUP_BODY + PICTURE_LINE)
|
|
397
|
+
assert gate.decision_for(payload) is None
|
|
398
|
+
assert _origin_reads(all_asked_commands) == 1
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def test_gate_should_not_read_the_origin_when_no_body_names_a_parent(
|
|
402
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
403
|
+
) -> None:
|
|
404
|
+
all_asked_commands = _unstarted_children(monkeypatch)
|
|
405
|
+
body = PROVEN_FOLLOWUP_BODY.replace("Follow-up to #1731", "Adds a guard.") + PICTURE_LINE
|
|
406
|
+
payload = _quoted_shell_create_without_repository(tmp_path, body)
|
|
407
|
+
assert gate.decision_for(payload) is None
|
|
408
|
+
assert _origin_reads(all_asked_commands) == 0
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
class LateClock:
|
|
412
|
+
"""Reads zero at the start, then the end of the proof and look slice on every later read."""
|
|
413
|
+
|
|
414
|
+
def __init__(self) -> None:
|
|
415
|
+
self.read_count = 0
|
|
416
|
+
|
|
417
|
+
def __call__(self) -> float:
|
|
418
|
+
self.read_count += 1
|
|
419
|
+
return 0.0 if self.read_count == 1 else float(PROOF_AND_LOOK_READS_SECONDS)
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def _mcp_create_without_repository(tmp_path: Path) -> dict[str, object]:
|
|
423
|
+
body = PROVEN_FOLLOWUP_BODY.replace("Follow-up to #1731", "Adds a guard.")
|
|
424
|
+
return {
|
|
425
|
+
"tool_name": "mcp__github__create_pull_request",
|
|
426
|
+
"tool_input": {"title": "fix: x", "body": body, "head": "feature"},
|
|
427
|
+
"cwd": str(tmp_path),
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def test_deny_should_add_the_reads_ran_out_sentence_when_the_deadline_cut_a_read(tmp_path: Path) -> None:
|
|
432
|
+
payload = _mcp_create_without_repository(tmp_path)
|
|
433
|
+
with read_deadlines_started(LateClock()):
|
|
434
|
+
decision = gate.decision_for(payload)
|
|
435
|
+
assert decision is not None
|
|
436
|
+
assert decision["hookSpecificOutput"]["permissionDecisionReason"] == UNREADABLE_CHANGES_REASON + READS_RAN_OUT_SENTENCE
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def test_deny_should_keep_its_reason_when_every_read_had_time(tmp_path: Path) -> None:
|
|
440
|
+
decision = gate.decision_for(_mcp_create_without_repository(tmp_path))
|
|
441
|
+
assert decision is not None
|
|
442
|
+
assert decision["hookSpecificOutput"]["permissionDecisionReason"] == UNREADABLE_CHANGES_REASON
|
|
443
|
+
|
|
444
|
+
|
|
357
445
|
def test_loaded_skill_still_denies_a_new_pull_request_without_proof(tmp_path: Path) -> None:
|
|
358
446
|
exit_code, output = _run_main(_payload("gh pr create --body 'Adds a gate.'", _transcript(tmp_path, "pr-lifecycle")))
|
|
359
447
|
assert exit_code == 0
|
|
@@ -12,6 +12,7 @@ if str(HOOKS_DIRECTORY) not in sys.path:
|
|
|
12
12
|
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
13
13
|
|
|
14
14
|
from blocking import pull_request_proof as gate
|
|
15
|
+
from blocking.pr_lifecycle_skill_gate import decision_for
|
|
15
16
|
from hooks_constants.pull_request_proof_constants import (
|
|
16
17
|
COMPARE_FILE_LIST_LIMIT,
|
|
17
18
|
COMPARE_FILE_STATUS_QUERY,
|
|
@@ -764,7 +765,7 @@ def test_should_deny_a_visible_file_on_a_full_compare_page(
|
|
|
764
765
|
assert "module_0.py" not in reason
|
|
765
766
|
|
|
766
767
|
|
|
767
|
-
def
|
|
768
|
+
def test_should_allow_a_page_whose_proof_links_an_artifact_without_a_read(
|
|
768
769
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
769
770
|
) -> None:
|
|
770
771
|
all_endpoints: list[str] = []
|
|
@@ -777,7 +778,7 @@ def test_should_allow_a_page_the_compare_api_lists_when_the_proof_links_an_artif
|
|
|
777
778
|
tmp_path,
|
|
778
779
|
)
|
|
779
780
|
assert gate.missing_look_reason(payload) is None
|
|
780
|
-
assert all_endpoints == [
|
|
781
|
+
assert all_endpoints == []
|
|
781
782
|
|
|
782
783
|
|
|
783
784
|
def test_should_skip_the_compare_api_without_a_named_repository(
|
|
@@ -1112,6 +1113,66 @@ def test_remote_added_workflow_files_should_report_none_when_a_full_page_lists_n
|
|
|
1112
1113
|
assert gate.remote_added_workflow_files(None, "main", "feature") is None
|
|
1113
1114
|
|
|
1114
1115
|
|
|
1116
|
+
WAIVER_BODY = (
|
|
1117
|
+
"## What this does\nAdds a guard to the tool.\n\n"
|
|
1118
|
+
"```mermaid\nflowchart LR\n A[call] --> B[guard]\n```\n\n"
|
|
1119
|
+
"## Existing work\nNothing found; I searched `hooks/` and open pull requests.\n\n"
|
|
1120
|
+
"## Proof in practice\nRan `python3 tool.py --check` and it printed `ok`.\n"
|
|
1121
|
+
"Not run: the first GitHub Actions workflow run.\n\n"
|
|
1122
|
+
"## After merge\nThe session records the run link for `new.yml`.\n"
|
|
1123
|
+
)
|
|
1124
|
+
|
|
1125
|
+
|
|
1126
|
+
def test_quoted_shell_create_should_look_up_the_added_workflows_once(
|
|
1127
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
1128
|
+
) -> None:
|
|
1129
|
+
all_endpoints: list[str] = []
|
|
1130
|
+
compare_endpoint = "repos/owner/name/compare/main...some-branch"
|
|
1131
|
+
all_responses = {compare_endpoint: _workflow_compare_lines(1, [f"{WORKFLOW_DIRECTORY}/new.yml"])}
|
|
1132
|
+
monkeypatch.setattr(gate, "_gh_api_lines", _fake_gh_api(all_responses, all_endpoints))
|
|
1133
|
+
(tmp_path / "body.md").write_text(WAIVER_BODY, encoding="utf-8")
|
|
1134
|
+
payload = _shell(
|
|
1135
|
+
'gh pr create --title "fix: quoted title" --body-file body.md'
|
|
1136
|
+
" --head some-branch --base main --repo owner/name",
|
|
1137
|
+
tmp_path,
|
|
1138
|
+
)
|
|
1139
|
+
assert gate.missing_proof_reason(payload) is None
|
|
1140
|
+
assert all_endpoints.count(compare_endpoint) == 1
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
def test_look_check_should_read_nothing_when_every_body_shows_a_picture(
|
|
1144
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
1145
|
+
) -> None:
|
|
1146
|
+
all_endpoints: list[str] = []
|
|
1147
|
+
monkeypatch.setattr(gate, "_gh_api_lines", _fake_gh_api({}, all_endpoints))
|
|
1148
|
+
body = PROVEN_BODY + "\n"
|
|
1149
|
+
payload = _shell_create_elsewhere(tmp_path, "--repo o/r --base main --head feature", body)
|
|
1150
|
+
assert gate.missing_look_reason(payload) is None
|
|
1151
|
+
assert all_endpoints == []
|
|
1152
|
+
no_picture_payload = _shell_create_elsewhere(tmp_path, "--repo o/r --base main --head feature")
|
|
1153
|
+
assert gate.missing_look_reason(no_picture_payload) == UNREADABLE_CHANGES_REASON
|
|
1154
|
+
assert all_endpoints == ["repos/o/r/compare/main...feature"]
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
def test_one_gate_run_should_resolve_the_merge_base_once_for_the_waiver_and_look_checks(
|
|
1158
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
1159
|
+
) -> None:
|
|
1160
|
+
clone_path = _repository_with_workflow_change(tmp_path, "new.yml")
|
|
1161
|
+
all_head_references: list[str] = []
|
|
1162
|
+
original_first_merge_base = gate._first_merge_base
|
|
1163
|
+
|
|
1164
|
+
def counted_first_merge_base(working_directory: str, head_reference: str) -> str | None:
|
|
1165
|
+
all_head_references.append(head_reference)
|
|
1166
|
+
return original_first_merge_base(working_directory, head_reference)
|
|
1167
|
+
|
|
1168
|
+
monkeypatch.setattr(gate, "_first_merge_base", counted_first_merge_base)
|
|
1169
|
+
payload = _mcp_create_on_branch(WAIVER_BODY, clone_path)
|
|
1170
|
+
assert decision_for(payload) is None
|
|
1171
|
+
assert all_head_references == ["HEAD"]
|
|
1172
|
+
assert decision_for(payload) is None
|
|
1173
|
+
assert all_head_references == ["HEAD", "HEAD"]
|
|
1174
|
+
|
|
1175
|
+
|
|
1115
1176
|
def test_unproven_line_should_stay_denied_when_it_is_about_something_other_than_a_run(tmp_path: Path) -> None:
|
|
1116
1177
|
clone_path = _repository_with_workflow_change(tmp_path, NEW_WORKFLOW_NAME)
|
|
1117
1178
|
unrelated_line = "Untested on Windows."
|
|
@@ -39,7 +39,7 @@ This family stops a chat reply, edited message, or tool call when its payload br
|
|
|
39
39
|
- `blocking/step_note_gate.py` runs on `PreToolUse`, matcher `*`, timeout `15` seconds in `hooks.json`.
|
|
40
40
|
- `blocking/wakeup_cap_gate.py` runs on `PreToolUse`, matcher `ScheduleWakeup|CronCreate|mcp__.*__(send_later|create_trigger|update_trigger)`, timeout `10` seconds in `hooks.json`.
|
|
41
41
|
- `blocking/verify_before_acting.py` runs on `PostToolUse`, matcher `Write|Edit|MultiEdit|NotebookEdit|Agent|Task|apply_patch|Bash|PowerShell|mcp__.*`, timeout `10` seconds in `hooks.json`.
|
|
42
|
-
- `blocking/pr_lifecycle_skill_gate.py` runs on `PreToolUse`, matcher `Bash|PowerShell|mcp__.*__(create_pull_request|merge_pull_request|enable_pr_auto_merge|update_pull_request|actions_run_trigger)`, timeout `
|
|
42
|
+
- `blocking/pr_lifecycle_skill_gate.py` runs on `PreToolUse`, matcher `Bash|PowerShell|mcp__.*__(create_pull_request|merge_pull_request|enable_pr_auto_merge|update_pull_request|actions_run_trigger)`, timeout `30` seconds in `hooks.json`. The gate's git and GitHub reads end by 21 s after its read clock starts (a 20 s deadline and a 1 s grace for a stopped child).
|
|
43
43
|
- `blocking/resolve_thread_reply_gate.py` runs on `PreToolUse`, matcher `Bash|PowerShell|mcp__.*__resolve_review_thread|mcp__.*__pull_request_review_write`, timeout `10` seconds in `hooks.json`.
|
|
44
44
|
- `blocking/verify_before_commit_gate.py` runs on `PreToolUse`, matcher `Bash|PowerShell`, timeout `30` seconds in `hooks.json`.
|
|
45
45
|
- `blocking/build_eval_before_eval_write_gate.py` runs on `PreToolUse`, matcher `Write|Edit|MultiEdit|NotebookEdit|apply_patch`, timeout `10` seconds in `hooks.json`.
|
package/hooks/hooks.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Constants for the read deadlines of one pull request lifecycle gate run."""
|
|
2
|
+
|
|
3
|
+
PROOF_AND_LOOK_READS_SECONDS = 12
|
|
4
|
+
FOLLOWUP_READS_SECONDS = 8
|
|
5
|
+
KILL_GRACE_SECONDS = 1
|
|
6
|
+
MOST_CUT_READS_PER_RUN = 2
|
|
7
|
+
STARTUP_AND_SCAN_MARGIN_SECONDS = 5
|
|
8
|
+
READS_RAN_OUT_SENTENCE = (
|
|
9
|
+
" The gate's git and GitHub reads ran out of time on this run, so a second run may pass."
|
|
10
|
+
)
|
|
@@ -6,7 +6,3 @@ from hooks_constants import followup_pr_dedupe_constants as constants
|
|
|
6
6
|
def test_parent_pattern_reads_both_spellings_and_stops_at_the_number() -> None:
|
|
7
7
|
assert re.search(constants.FOLLOWUP_PARENT_PATTERN, "followup to #12", re.IGNORECASE)
|
|
8
8
|
assert re.search(constants.FOLLOWUP_PARENT_PATTERN, "Follow-up to #1731", re.IGNORECASE).group(1) == "1731"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def test_read_timeout_fits_inside_the_hook_timeout() -> None:
|
|
12
|
-
assert constants.READ_TIMEOUT_SECONDS * 2 < 10
|
package/hooks/pyproject.toml
CHANGED
|
@@ -94,6 +94,7 @@ ignore = [
|
|
|
94
94
|
# Residual production carve-outs re-homed on #57 (gate friction on multi-line touch).
|
|
95
95
|
"validators/run_all_validators.py" = ["PLW1510"]
|
|
96
96
|
"validators/exempt_paths.py" = ["F401"]
|
|
97
|
+
"blocking/gate_run_reads.py" = ["UP047"]
|
|
97
98
|
"test_*.py" = ["B011", "PLR2004", "F401", "F841", "PLW1510", "B905", "B033", "SIM115", "B007", "B018", "B904"]
|
|
98
99
|
"*_test.py" = ["B011", "PLR2004", "F401", "F841", "PLW1510", "B905", "B033", "SIM115", "B007", "B018", "B904"]
|
|
99
100
|
"conftest.py" = ["B011", "F401", "F841", "PLW1510"]
|