claude-dev-env 8.116.1 → 8.116.2
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 +284 -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 +59 -0
- package/hooks/blocking/test_gate_run_reads.py +241 -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/package.json +1 -1
|
@@ -4,7 +4,6 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import json
|
|
6
6
|
import re
|
|
7
|
-
import subprocess
|
|
8
7
|
import sys
|
|
9
8
|
from collections.abc import Iterator, Mapping
|
|
10
9
|
from typing import NamedTuple
|
|
@@ -15,8 +14,15 @@ if hooks_directory not in sys.path:
|
|
|
15
14
|
sys.path.insert(0, hooks_directory)
|
|
16
15
|
|
|
17
16
|
from blocking.body_flowchart import fenced_line_flags, section_has_flowchart
|
|
18
|
-
from blocking.followup_pr_dedupe import
|
|
19
|
-
|
|
17
|
+
from blocking.followup_pr_dedupe import (
|
|
18
|
+
all_command_segments,
|
|
19
|
+
body_from_arguments,
|
|
20
|
+
create_tool_fields,
|
|
21
|
+
shell_command,
|
|
22
|
+
title_from_arguments,
|
|
23
|
+
)
|
|
24
|
+
from blocking.gate_run_reads import ReadSlice, answer_once, child_stdout
|
|
25
|
+
from hooks_constants.pr_lifecycle_skill_gate_constants import UPDATE_PULL_REQUEST_TOOL_SUFFIX
|
|
20
26
|
from hooks_constants.pull_request_proof_constants import (
|
|
21
27
|
ADDED_FILE_NAMES_QUERY,
|
|
22
28
|
GIT_URL_SUFFIX,
|
|
@@ -102,10 +108,7 @@ from hooks_constants.pytest_invocation import (
|
|
|
102
108
|
)
|
|
103
109
|
from hooks_constants.shell_command_pipeline import pipeline_segments_for_command
|
|
104
110
|
from hooks_constants.shell_command_segments import token_basename
|
|
105
|
-
from hooks_constants.shell_command_wrappers import
|
|
106
|
-
all_wrapped_command_texts,
|
|
107
|
-
segment_program_and_arguments,
|
|
108
|
-
)
|
|
111
|
+
from hooks_constants.shell_command_wrappers import segment_program_and_arguments
|
|
109
112
|
|
|
110
113
|
|
|
111
114
|
class CreateCall(NamedTuple):
|
|
@@ -206,21 +209,17 @@ def _command_arguments(
|
|
|
206
209
|
return _script_word_arguments(program, all_arguments, script_word)
|
|
207
210
|
|
|
208
211
|
|
|
209
|
-
def _shell_command(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
210
|
-
tool_input = all_payload_fields.get("tool_input")
|
|
211
|
-
command = tool_input.get("command") if isinstance(tool_input, dict) else None
|
|
212
|
-
if all_payload_fields.get("tool_name") not in SHELL_TOOL_NAMES or not isinstance(command, str):
|
|
213
|
-
return None
|
|
214
|
-
return command
|
|
215
|
-
|
|
216
|
-
|
|
217
212
|
def _all_argument_lists(command: str, all_gh_words: list[str], script_word: str) -> Iterator[list[str]]:
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
213
|
+
all_seen_arguments: set[tuple[str, ...]] = set()
|
|
214
|
+
for each_segment in all_command_segments(command):
|
|
215
|
+
all_arguments = _command_arguments(each_segment, all_gh_words, script_word)
|
|
216
|
+
if all_arguments is None:
|
|
217
|
+
continue
|
|
218
|
+
all_unquoted_arguments = tuple(_unquoted_arguments(all_arguments))
|
|
219
|
+
if all_unquoted_arguments in all_seen_arguments:
|
|
220
|
+
continue
|
|
221
|
+
all_seen_arguments.add(all_unquoted_arguments)
|
|
222
|
+
yield all_arguments
|
|
224
223
|
|
|
225
224
|
|
|
226
225
|
def _all_create_argument_lists(command: str) -> Iterator[list[str]]:
|
|
@@ -253,7 +252,7 @@ def _flag_value(all_create_arguments: list[str], all_flags: frozenset[str], assi
|
|
|
253
252
|
def _shell_flag_value(
|
|
254
253
|
all_payload_fields: Mapping[str, object], all_flags: frozenset[str], assignment_prefix: str
|
|
255
254
|
) -> str | None:
|
|
256
|
-
command =
|
|
255
|
+
command = shell_command(all_payload_fields)
|
|
257
256
|
if command is None:
|
|
258
257
|
return None
|
|
259
258
|
all_flag_values = [
|
|
@@ -303,7 +302,7 @@ def _create_calls(all_payload_fields: Mapping[str, object]) -> list[CreateCall]:
|
|
|
303
302
|
body if isinstance(body, str) else "",
|
|
304
303
|
)
|
|
305
304
|
]
|
|
306
|
-
command =
|
|
305
|
+
command = shell_command(all_payload_fields)
|
|
307
306
|
if command is None:
|
|
308
307
|
return []
|
|
309
308
|
working_directory = all_payload_fields.get("cwd")
|
|
@@ -324,7 +323,7 @@ def is_create_call(all_payload_fields: Mapping[str, object]) -> bool:
|
|
|
324
323
|
Args:
|
|
325
324
|
all_payload_fields: A tool name and input, shaped like a PreToolUse payload.
|
|
326
325
|
"""
|
|
327
|
-
command =
|
|
326
|
+
command = shell_command(all_payload_fields)
|
|
328
327
|
return create_tool_fields(all_payload_fields) is not None or (
|
|
329
328
|
command is not None and next(_all_create_argument_lists(command), None) is not None
|
|
330
329
|
)
|
|
@@ -650,7 +649,7 @@ def _edit_bodies(all_payload_fields: Mapping[str, object]) -> list[str]:
|
|
|
650
649
|
if isinstance(tool_name, str) and tool_name.endswith(UPDATE_PULL_REQUEST_TOOL_SUFFIX):
|
|
651
650
|
body = tool_input.get("body") if isinstance(tool_input, dict) else None
|
|
652
651
|
return [body] if isinstance(body, str) else []
|
|
653
|
-
command =
|
|
652
|
+
command = shell_command(all_payload_fields)
|
|
654
653
|
if command is None:
|
|
655
654
|
return []
|
|
656
655
|
working_directory = all_payload_fields.get("cwd")
|
|
@@ -736,19 +735,9 @@ class ChangedFile(NamedTuple):
|
|
|
736
735
|
|
|
737
736
|
|
|
738
737
|
def _git_output(working_directory: str, *all_git_arguments: str) -> str | None:
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
capture_output=True,
|
|
743
|
-
text=True,
|
|
744
|
-
encoding="utf-8",
|
|
745
|
-
errors="replace",
|
|
746
|
-
timeout=GIT_TIMEOUT_SECONDS,
|
|
747
|
-
check=False,
|
|
748
|
-
)
|
|
749
|
-
except (OSError, subprocess.SubprocessError):
|
|
750
|
-
return None
|
|
751
|
-
return completed.stdout if completed.returncode == 0 else None
|
|
738
|
+
return child_stdout(
|
|
739
|
+
["git", "-C", working_directory, *all_git_arguments], GIT_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK
|
|
740
|
+
)
|
|
752
741
|
|
|
753
742
|
|
|
754
743
|
def _git_lines(working_directory: str, *all_git_arguments: str) -> list[str] | None:
|
|
@@ -810,6 +799,14 @@ def visible_changed_files(working_directory: str, head_branch: object) -> list[s
|
|
|
810
799
|
|
|
811
800
|
|
|
812
801
|
def _merge_base_and_head(working_directory: str, head_branch: object) -> tuple[str, str] | None:
|
|
802
|
+
named_head = head_branch if isinstance(head_branch, str) else None
|
|
803
|
+
return answer_once(
|
|
804
|
+
("merge base and head", working_directory, named_head),
|
|
805
|
+
lambda: _read_merge_base_and_head(working_directory, named_head),
|
|
806
|
+
)
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
def _read_merge_base_and_head(working_directory: str, head_branch: str | None) -> tuple[str, str] | None:
|
|
813
810
|
if not working_directory:
|
|
814
811
|
return None
|
|
815
812
|
head_reference = _head_reference(working_directory, head_branch)
|
|
@@ -899,19 +896,8 @@ def added_workflow_files(working_directory: str, head_branch: object) -> list[st
|
|
|
899
896
|
|
|
900
897
|
|
|
901
898
|
def _gh_api_lines(endpoint: str, query: str) -> list[str] | None:
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
["gh", "api", endpoint, "--jq", query],
|
|
905
|
-
capture_output=True,
|
|
906
|
-
text=True,
|
|
907
|
-
encoding="utf-8",
|
|
908
|
-
errors="replace",
|
|
909
|
-
timeout=GH_TIMEOUT_SECONDS,
|
|
910
|
-
check=False,
|
|
911
|
-
)
|
|
912
|
-
except (OSError, subprocess.SubprocessError):
|
|
913
|
-
return None
|
|
914
|
-
return completed.stdout.splitlines() if completed.returncode == 0 else None
|
|
899
|
+
printed_text = child_stdout(["gh", "api", endpoint, "--jq", query], GH_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK)
|
|
900
|
+
return None if printed_text is None else printed_text.splitlines()
|
|
915
901
|
|
|
916
902
|
|
|
917
903
|
def _compare_base_branch(repository: str, base_branch: str | None) -> str | None:
|
|
@@ -1010,13 +996,21 @@ def _added_workflow_files_for_create(all_payload_fields: Mapping[str, object]) -
|
|
|
1010
996
|
working_directory = str(all_payload_fields.get("cwd") or "")
|
|
1011
997
|
head_branch = _named_head_branch(all_payload_fields)
|
|
1012
998
|
repository = _named_repository(all_payload_fields)
|
|
999
|
+
base_branch = _named_base_branch(all_payload_fields)
|
|
1000
|
+
return answer_once(
|
|
1001
|
+
("added workflow files", working_directory, repository, base_branch, head_branch),
|
|
1002
|
+
lambda: _read_added_workflow_files(working_directory, repository, base_branch, head_branch),
|
|
1003
|
+
)
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
def _read_added_workflow_files(
|
|
1007
|
+
working_directory: str, repository: str | None, base_branch: str | None, head_branch: str | None
|
|
1008
|
+
) -> list[str]:
|
|
1013
1009
|
all_workflow_paths = None
|
|
1014
1010
|
if _local_checkout_may_answer(working_directory, repository, head_branch):
|
|
1015
1011
|
all_workflow_paths = added_workflow_files(working_directory, head_branch)
|
|
1016
1012
|
if all_workflow_paths is None:
|
|
1017
|
-
all_workflow_paths = remote_added_workflow_files(
|
|
1018
|
-
repository, _named_base_branch(all_payload_fields), head_branch
|
|
1019
|
-
)
|
|
1013
|
+
all_workflow_paths = remote_added_workflow_files(repository, base_branch, head_branch)
|
|
1020
1014
|
return all_workflow_paths or []
|
|
1021
1015
|
|
|
1022
1016
|
|
|
@@ -1131,11 +1125,18 @@ def missing_look_reason(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
|
1131
1125
|
git cannot read the branch, compare API returns 300 .py files -> UNREADABLE_CHANGES_REASON
|
|
1132
1126
|
neither git nor the compare API can read it, no picture -> UNREADABLE_CHANGES_REASON
|
|
1133
1127
|
|
|
1128
|
+
Every body is checked for a picture first. When each one shows a picture,
|
|
1129
|
+
the check passes and reads neither git nor the compare API.
|
|
1130
|
+
|
|
1134
1131
|
Args:
|
|
1135
1132
|
all_payload_fields: The parsed PreToolUse input.
|
|
1136
1133
|
"""
|
|
1137
|
-
|
|
1138
|
-
|
|
1134
|
+
all_bodies_without_picture = [
|
|
1135
|
+
each_body
|
|
1136
|
+
for each_body in _create_bodies(all_payload_fields)
|
|
1137
|
+
if each_body is not None and not re.search(PROOF_IMAGE_PATTERN, proof_section(each_body) or "", re.IGNORECASE)
|
|
1138
|
+
]
|
|
1139
|
+
if not all_bodies_without_picture:
|
|
1139
1140
|
return None
|
|
1140
1141
|
working_directory = all_payload_fields.get("cwd")
|
|
1141
1142
|
head_branch = _named_head_branch(all_payload_fields)
|
|
@@ -1146,10 +1147,6 @@ def missing_look_reason(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
|
1146
1147
|
)
|
|
1147
1148
|
if all_visible_files == []:
|
|
1148
1149
|
return None
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
if all_visible_files is None:
|
|
1153
|
-
return UNREADABLE_CHANGES_REASON
|
|
1154
|
-
return MISSING_LOOK_REASON.format(changed_files=CHANGED_FILES_SEPARATOR.join(all_visible_files))
|
|
1155
|
-
return None
|
|
1150
|
+
if all_visible_files is None:
|
|
1151
|
+
return UNREADABLE_CHANGES_REASON
|
|
1152
|
+
return MISSING_LOOK_REASON.format(changed_files=CHANGED_FILES_SEPARATOR.join(all_visible_files))
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
"""Behavior tests for the one-follow-up-pull-request-per-parent check."""
|
|
2
2
|
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
3
5
|
import sys
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
4
8
|
from collections.abc import Mapping
|
|
9
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
5
10
|
from pathlib import Path
|
|
6
11
|
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
7
14
|
HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
|
|
8
15
|
if str(HOOKS_DIRECTORY) not in sys.path:
|
|
9
16
|
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
@@ -20,6 +27,7 @@ OTHER_FOLLOWUP = {
|
|
|
20
27
|
"html_url": "https://github.com/jl-cmd/claude-dev-env/pull/1777",
|
|
21
28
|
"body": "follow-up to #17",
|
|
22
29
|
}
|
|
30
|
+
ELAPSED_SLACK_SECONDS = 8.0
|
|
23
31
|
|
|
24
32
|
|
|
25
33
|
def _reader(all_open: list[Mapping[str, object]]) -> dedupe.OpenPullRequestReader:
|
|
@@ -94,6 +102,57 @@ def test_body_from_arguments_reads_inline_text_and_body_files(tmp_path: Path) ->
|
|
|
94
102
|
assert dedupe.body_from_arguments(["--fill"], str(tmp_path)) is None
|
|
95
103
|
|
|
96
104
|
|
|
105
|
+
def test_body_from_arguments_should_read_only_a_regular_body_file(tmp_path: Path) -> None:
|
|
106
|
+
assert dedupe.body_from_arguments(["--body-file", os.devnull], str(tmp_path)) is None
|
|
107
|
+
assert dedupe.body_from_arguments(["--body-file", "."], str(tmp_path)) is None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _trickling_server() -> ThreadingHTTPServer:
|
|
111
|
+
class TrickleHandler(BaseHTTPRequestHandler):
|
|
112
|
+
def log_message(self, *all_arguments: object) -> None:
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
def do_GET(self) -> None:
|
|
116
|
+
payload = json.dumps([OPEN_FOLLOWUP]).encode()
|
|
117
|
+
self.send_response(200)
|
|
118
|
+
self.send_header("Content-Length", str(len(payload)))
|
|
119
|
+
self.end_headers()
|
|
120
|
+
for each_byte in payload:
|
|
121
|
+
self.wfile.write(bytes([each_byte]))
|
|
122
|
+
self.wfile.flush()
|
|
123
|
+
time.sleep(0.3)
|
|
124
|
+
|
|
125
|
+
server = ThreadingHTTPServer(("127.0.0.1", 0), TrickleHandler)
|
|
126
|
+
server.daemon_threads = True
|
|
127
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
128
|
+
return server
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_read_open_pull_requests_should_bound_the_whole_read(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
132
|
+
server = _trickling_server()
|
|
133
|
+
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
|
|
134
|
+
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
|
135
|
+
monkeypatch.setattr(dedupe, "GITHUB_API_ROOT", f"http://127.0.0.1:{server.server_address[1]}")
|
|
136
|
+
monkeypatch.setattr(dedupe, "READ_TIMEOUT_SECONDS", 1)
|
|
137
|
+
started = time.monotonic()
|
|
138
|
+
with pytest.raises(TimeoutError):
|
|
139
|
+
dedupe.read_open_pull_requests("jl-cmd", "claude-dev-env")
|
|
140
|
+
assert time.monotonic() - started < 1 + ELAPSED_SLACK_SECONDS
|
|
141
|
+
server.shutdown()
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def test_shell_command_should_return_only_a_shell_tool_command() -> None:
|
|
145
|
+
assert dedupe.shell_command({"tool_name": "PowerShell", "tool_input": {"command": "git push"}}) == "git push"
|
|
146
|
+
assert dedupe.shell_command({"tool_name": "Read", "tool_input": {"command": "git push"}}) is None
|
|
147
|
+
assert dedupe.shell_command({"tool_name": "Bash", "tool_input": {"command": 7}}) is None
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_all_command_segments_should_list_each_segment_and_wrapped_command() -> None:
|
|
151
|
+
all_segments = list(dedupe.all_command_segments("git add x && bash -c 'git push'"))
|
|
152
|
+
assert ["git", "add", "x"] in all_segments
|
|
153
|
+
assert ["git", "push"] in all_segments
|
|
154
|
+
|
|
155
|
+
|
|
97
156
|
def test_title_from_arguments_should_read_each_title_flag_shape() -> None:
|
|
98
157
|
assert dedupe.title_from_arguments(["--title", "feat: x", "--fill"]) == "feat: x"
|
|
99
158
|
assert dedupe.title_from_arguments(["-t", "fix: y"]) == "fix: y"
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"""Behavior tests for the answers one pull request lifecycle gate run remembers."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
import time
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
|
|
11
|
+
if str(HOOKS_DIRECTORY) not in sys.path:
|
|
12
|
+
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
13
|
+
|
|
14
|
+
from blocking import gate_run_reads
|
|
15
|
+
from blocking.gate_run_reads import (
|
|
16
|
+
ReadSlice,
|
|
17
|
+
answer_once,
|
|
18
|
+
answer_within_allowance,
|
|
19
|
+
child_stdout,
|
|
20
|
+
gate_run,
|
|
21
|
+
read_allowance,
|
|
22
|
+
read_deadlines_started,
|
|
23
|
+
)
|
|
24
|
+
from hooks_constants.followup_pr_dedupe_constants import READ_TIMEOUT_SECONDS
|
|
25
|
+
from hooks_constants.gate_run_reads_constants import (
|
|
26
|
+
FOLLOWUP_READS_SECONDS,
|
|
27
|
+
KILL_GRACE_SECONDS,
|
|
28
|
+
MOST_CUT_READS_PER_RUN,
|
|
29
|
+
PROOF_AND_LOOK_READS_SECONDS,
|
|
30
|
+
STARTUP_AND_SCAN_MARGIN_SECONDS,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
CLOCK_START = 100.0
|
|
34
|
+
HIDDEN_WINDOW_FLAGS = 0x08000000
|
|
35
|
+
SLEEP_THEN_PRINT = "import time; time.sleep(30); print('late')"
|
|
36
|
+
GRANDCHILD_HOLDS_THE_PIPE = (
|
|
37
|
+
"import subprocess, sys, time;"
|
|
38
|
+
" subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)']);"
|
|
39
|
+
" time.sleep(30)"
|
|
40
|
+
)
|
|
41
|
+
ELAPSED_SLACK_SECONDS = 8.0
|
|
42
|
+
GATE_COMMAND_SUFFIX = "hooks/blocking/pr_lifecycle_" + "skill_gate.py"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class FakeClock:
|
|
46
|
+
def __init__(self) -> None:
|
|
47
|
+
self.now = CLOCK_START
|
|
48
|
+
|
|
49
|
+
def __call__(self) -> float:
|
|
50
|
+
return self.now
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _counting_read(all_reads: list[str], answer: str | None):
|
|
54
|
+
def read() -> str | None:
|
|
55
|
+
all_reads.append("read")
|
|
56
|
+
return answer
|
|
57
|
+
|
|
58
|
+
return read
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_answer_once_should_read_a_question_once_inside_one_run() -> None:
|
|
62
|
+
all_reads: list[str] = []
|
|
63
|
+
with gate_run():
|
|
64
|
+
assert (
|
|
65
|
+
answer_once(("origin", "/repo"), _counting_read(all_reads, "owner/name"))
|
|
66
|
+
== "owner/name"
|
|
67
|
+
)
|
|
68
|
+
assert (
|
|
69
|
+
answer_once(("origin", "/repo"), _counting_read(all_reads, "other/name"))
|
|
70
|
+
== "owner/name"
|
|
71
|
+
)
|
|
72
|
+
assert all_reads == ["read"]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_answer_once_should_remember_an_unanswered_read() -> None:
|
|
76
|
+
all_reads: list[str] = []
|
|
77
|
+
with gate_run():
|
|
78
|
+
assert answer_once(("origin", "/repo"), _counting_read(all_reads, None)) is None
|
|
79
|
+
assert answer_once(("origin", "/repo"), _counting_read(all_reads, "owner/name")) is None
|
|
80
|
+
assert all_reads == ["read"]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_answer_once_should_read_each_time_with_no_run_open() -> None:
|
|
84
|
+
all_reads: list[str] = []
|
|
85
|
+
assert answer_once(("origin", "/repo"), _counting_read(all_reads, "a")) == "a"
|
|
86
|
+
assert answer_once(("origin", "/repo"), _counting_read(all_reads, "b")) == "b"
|
|
87
|
+
assert all_reads == ["read", "read"]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_gate_run_should_forget_its_answers_when_it_closes() -> None:
|
|
91
|
+
all_reads: list[str] = []
|
|
92
|
+
with gate_run() as first_run:
|
|
93
|
+
answer_once(("origin", "/repo"), _counting_read(all_reads, "a"))
|
|
94
|
+
with gate_run():
|
|
95
|
+
assert answer_once(("origin", "/repo"), _counting_read(all_reads, "b")) == "b"
|
|
96
|
+
assert first_run.answer_by_question == {("origin", "/repo"): "a"}
|
|
97
|
+
assert all_reads == ["read", "read"]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_read_allowance_should_keep_the_own_timeout_with_no_deadline_started() -> None:
|
|
101
|
+
assert read_allowance(5, ReadSlice.PROOF_AND_LOOK) == 5
|
|
102
|
+
assert read_allowance(4, ReadSlice.FOLLOWUP) == 4
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_read_allowance_should_take_the_smaller_of_the_own_timeout_and_the_time_left() -> None:
|
|
106
|
+
clock = FakeClock()
|
|
107
|
+
with read_deadlines_started(clock):
|
|
108
|
+
assert read_allowance(5, ReadSlice.PROOF_AND_LOOK) == 5
|
|
109
|
+
clock.now = CLOCK_START + 9
|
|
110
|
+
assert read_allowance(5, ReadSlice.PROOF_AND_LOOK) == 3
|
|
111
|
+
clock.now = CLOCK_START + 18
|
|
112
|
+
assert read_allowance(4, ReadSlice.FOLLOWUP) == 2
|
|
113
|
+
assert read_allowance(5, ReadSlice.PROOF_AND_LOOK) == 5
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_read_allowance_should_keep_the_reserved_slice_for_followup_reads() -> None:
|
|
117
|
+
clock = FakeClock()
|
|
118
|
+
with read_deadlines_started(clock):
|
|
119
|
+
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS - 0.5
|
|
120
|
+
assert read_allowance(5, ReadSlice.PROOF_AND_LOOK) == 0.5
|
|
121
|
+
assert read_allowance(4, ReadSlice.FOLLOWUP) == 4
|
|
122
|
+
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS
|
|
123
|
+
assert read_allowance(5, ReadSlice.PROOF_AND_LOOK) is None
|
|
124
|
+
assert read_allowance(4, ReadSlice.FOLLOWUP) == 4
|
|
125
|
+
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS + FOLLOWUP_READS_SECONDS
|
|
126
|
+
assert read_allowance(4, ReadSlice.FOLLOWUP) is None
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_child_stdout_should_skip_a_read_with_no_time_left(tmp_path: Path) -> None:
|
|
130
|
+
marker_path = tmp_path / "ran.txt"
|
|
131
|
+
clock = FakeClock()
|
|
132
|
+
with read_deadlines_started(clock), gate_run() as run:
|
|
133
|
+
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS
|
|
134
|
+
command = [sys.executable, "-c", f"open({str(marker_path)!r}, 'w').write('x')"]
|
|
135
|
+
assert child_stdout(command, 5, ReadSlice.PROOF_AND_LOOK) is None
|
|
136
|
+
assert not marker_path.exists()
|
|
137
|
+
assert run.was_read_cut_short
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_child_stdout_should_return_the_output_of_a_child_that_answers() -> None:
|
|
141
|
+
with gate_run() as run:
|
|
142
|
+
assert child_stdout([sys.executable, "-c", "print('answer')"], 5, ReadSlice.PROOF_AND_LOOK) == "answer\n"
|
|
143
|
+
assert child_stdout([sys.executable, "-c", "raise SystemExit(3)"], 5, ReadSlice.PROOF_AND_LOOK) is None
|
|
144
|
+
assert not run.was_read_cut_short
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_child_stdout_should_start_its_child_with_the_hidden_window_flags(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
148
|
+
all_seen_flags: list[object] = []
|
|
149
|
+
|
|
150
|
+
def refuse_to_start(*all_arguments: object, **all_options: object) -> None:
|
|
151
|
+
all_seen_flags.append(all_options.get("creationflags"))
|
|
152
|
+
raise OSError("this test starts no child")
|
|
153
|
+
|
|
154
|
+
monkeypatch.setattr(gate_run_reads, "hidden_window_creation_flags", lambda: HIDDEN_WINDOW_FLAGS)
|
|
155
|
+
monkeypatch.setattr(gate_run_reads.subprocess, "Popen", refuse_to_start)
|
|
156
|
+
assert child_stdout(["git", "status"], 5, ReadSlice.PROOF_AND_LOOK) is None
|
|
157
|
+
assert all_seen_flags == [HIDDEN_WINDOW_FLAGS]
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_child_stdout_should_kill_a_child_that_outlives_its_own_timeout() -> None:
|
|
161
|
+
with gate_run() as run:
|
|
162
|
+
started = time.monotonic()
|
|
163
|
+
assert child_stdout([sys.executable, "-c", SLEEP_THEN_PRINT], 0.5, ReadSlice.PROOF_AND_LOOK) is None
|
|
164
|
+
elapsed = time.monotonic() - started
|
|
165
|
+
assert elapsed < 0.5 + ELAPSED_SLACK_SECONDS
|
|
166
|
+
assert not run.was_read_cut_short
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_child_stdout_should_stop_waiting_when_a_grandchild_holds_the_pipe() -> None:
|
|
170
|
+
started = time.monotonic()
|
|
171
|
+
assert child_stdout([sys.executable, "-c", GRANDCHILD_HOLDS_THE_PIPE], 0.5, ReadSlice.PROOF_AND_LOOK) is None
|
|
172
|
+
assert time.monotonic() - started < 0.5 + KILL_GRACE_SECONDS + ELAPSED_SLACK_SECONDS
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def test_child_stdout_should_mark_the_run_when_the_deadline_cuts_a_read_short() -> None:
|
|
176
|
+
clock = FakeClock()
|
|
177
|
+
with read_deadlines_started(clock), gate_run() as run:
|
|
178
|
+
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS - 0.4
|
|
179
|
+
started = time.monotonic()
|
|
180
|
+
assert child_stdout([sys.executable, "-c", SLEEP_THEN_PRINT], 5, ReadSlice.PROOF_AND_LOOK) is None
|
|
181
|
+
elapsed = time.monotonic() - started
|
|
182
|
+
assert elapsed < 0.4 + ELAPSED_SLACK_SECONDS
|
|
183
|
+
assert run.was_read_cut_short
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _slow_read(allowance: float) -> list[str]:
|
|
187
|
+
time.sleep(allowance + 30)
|
|
188
|
+
return ["late"]
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_answer_within_allowance_should_bound_the_whole_read() -> None:
|
|
192
|
+
clock = FakeClock()
|
|
193
|
+
with read_deadlines_started(clock), gate_run() as run:
|
|
194
|
+
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS + FOLLOWUP_READS_SECONDS - 0.3
|
|
195
|
+
started = time.monotonic()
|
|
196
|
+
answer = answer_within_allowance(_slow_read, 4, ReadSlice.FOLLOWUP)
|
|
197
|
+
elapsed = time.monotonic() - started
|
|
198
|
+
assert answer is None
|
|
199
|
+
assert elapsed < 0.3 + ELAPSED_SLACK_SECONDS
|
|
200
|
+
assert run.was_read_cut_short
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def test_answer_within_allowance_should_return_the_answer_and_raise_the_read_error() -> None:
|
|
204
|
+
all_allowances: list[float] = []
|
|
205
|
+
|
|
206
|
+
def answering_read(allowance: float) -> list[str]:
|
|
207
|
+
all_allowances.append(allowance)
|
|
208
|
+
return ["ok"]
|
|
209
|
+
|
|
210
|
+
def failing_read(allowance: float) -> list[str]:
|
|
211
|
+
raise OSError("offline")
|
|
212
|
+
|
|
213
|
+
assert answer_within_allowance(answering_read, 4, ReadSlice.FOLLOWUP) == ["ok"]
|
|
214
|
+
assert all_allowances == [4]
|
|
215
|
+
with pytest.raises(OSError, match="offline"):
|
|
216
|
+
answer_within_allowance(failing_read, 4, ReadSlice.FOLLOWUP)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _registered_gate_timeout_seconds() -> int:
|
|
220
|
+
all_registration_fields = json.loads((HOOKS_DIRECTORY / "hooks.json").read_text(encoding="utf-8"))
|
|
221
|
+
all_timeouts = [
|
|
222
|
+
each_hook["timeout"]
|
|
223
|
+
for each_group in all_registration_fields["hooks"]["PreToolUse"]
|
|
224
|
+
for each_hook in each_group["hooks"]
|
|
225
|
+
if each_hook.get("command", "").endswith(GATE_COMMAND_SUFFIX)
|
|
226
|
+
]
|
|
227
|
+
assert len(all_timeouts) == 1
|
|
228
|
+
return all_timeouts[0]
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def test_both_followup_reads_should_fit_inside_their_reserved_slice() -> None:
|
|
232
|
+
assert READ_TIMEOUT_SECONDS * 2 <= FOLLOWUP_READS_SECONDS
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def test_registered_timeout_should_outlast_every_read_deadline_and_kill_grace() -> None:
|
|
236
|
+
last_read_ends_at = (
|
|
237
|
+
PROOF_AND_LOOK_READS_SECONDS
|
|
238
|
+
+ FOLLOWUP_READS_SECONDS
|
|
239
|
+
+ KILL_GRACE_SECONDS * MOST_CUT_READS_PER_RUN
|
|
240
|
+
)
|
|
241
|
+
assert _registered_gate_timeout_seconds() >= last_read_ends_at + STARTUP_AND_SCAN_MARGIN_SECONDS
|
|
@@ -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
|