claude-dev-env 8.116.2 → 8.116.4
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/hooks/blocking/followup_pr_dedupe.py +59 -41
- package/hooks/blocking/gate_run_reads.py +6 -4
- package/hooks/blocking/pull_request_proof.py +76 -78
- package/hooks/blocking/test_followup_pr_dedupe.py +81 -3
- package/hooks/blocking/test_gate_run_reads.py +84 -25
- package/hooks/blocking/test_pr_lifecycle_skill_gate.py +38 -0
- package/hooks/blocking/test_pull_request_proof.py +67 -0
- package/hooks/pyproject.toml +1 -0
- package/package.json +1 -1
|
@@ -192,34 +192,20 @@ def _all_create_argument_lists(command: str) -> Iterator[list[str]]:
|
|
|
192
192
|
|
|
193
193
|
def _arguments_draft(all_arguments: list[str], working_directory: str) -> PullRequestDraft | None:
|
|
194
194
|
body = body_from_arguments(all_arguments, working_directory)
|
|
195
|
+
if body is None or followup_parent_number(body) is None:
|
|
196
|
+
return None
|
|
195
197
|
repository = _repository_from_arguments(all_arguments, working_directory)
|
|
196
|
-
if
|
|
198
|
+
if repository is None:
|
|
197
199
|
return None
|
|
198
200
|
return PullRequestDraft(repository[0], repository[1], body)
|
|
199
201
|
|
|
200
202
|
|
|
201
|
-
def
|
|
203
|
+
def _shell_followup_drafts(command: str, working_directory: str) -> list[PullRequestDraft]:
|
|
202
204
|
all_drafts = (
|
|
203
205
|
_arguments_draft(all_arguments, working_directory)
|
|
204
206
|
for all_arguments in _all_create_argument_lists(command)
|
|
205
207
|
)
|
|
206
|
-
return
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
def _names_a_parent(all_payload_fields: Mapping[str, object]) -> bool:
|
|
210
|
-
create_fields = create_tool_fields(all_payload_fields)
|
|
211
|
-
if create_fields is not None:
|
|
212
|
-
body = create_fields.get("body")
|
|
213
|
-
return isinstance(body, str) and followup_parent_number(body) is not None
|
|
214
|
-
command = shell_command(all_payload_fields)
|
|
215
|
-
if command is None:
|
|
216
|
-
return False
|
|
217
|
-
working_directory = _working_directory(all_payload_fields)
|
|
218
|
-
return any(
|
|
219
|
-
(body := body_from_arguments(all_arguments, working_directory)) is not None
|
|
220
|
-
and followup_parent_number(body) is not None
|
|
221
|
-
for all_arguments in _all_create_argument_lists(command)
|
|
222
|
-
)
|
|
208
|
+
return list(dict.fromkeys(each_draft for each_draft in all_drafts if each_draft is not None))
|
|
223
209
|
|
|
224
210
|
|
|
225
211
|
def shell_command(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
@@ -281,18 +267,50 @@ def create_tool_fields(all_payload_fields: Mapping[str, object]) -> Mapping[str,
|
|
|
281
267
|
return _dispatch_fields(tool_input) if tool_name.endswith(WORKFLOW_DISPATCH_TOOL_SUFFIX) else None
|
|
282
268
|
|
|
283
269
|
|
|
284
|
-
def
|
|
285
|
-
"""Return
|
|
270
|
+
def followup_drafts(all_payload_fields: Mapping[str, object]) -> list[PullRequestDraft]:
|
|
271
|
+
"""Return one draft per distinct repository and body among the creates whose body names a parent.
|
|
272
|
+
|
|
273
|
+
::
|
|
274
|
+
|
|
275
|
+
create on o/r "Follow-up to #7" && create on o/r "Follow-up to #8" -> both drafts
|
|
276
|
+
create_pull_request {owner, repo, body "Follow-up to #7"} -> that one draft
|
|
277
|
+
create whose body names no parent, or any other tool -> []
|
|
278
|
+
|
|
279
|
+
A body that names no parent gives no draft before any repository or
|
|
280
|
+
origin read. A quoted create, which the command parser reads twice,
|
|
281
|
+
gives one draft.
|
|
282
|
+
|
|
283
|
+
Args:
|
|
284
|
+
all_payload_fields: The parsed PreToolUse input.
|
|
285
|
+
|
|
286
|
+
Returns:
|
|
287
|
+
The drafts, in command order.
|
|
288
|
+
"""
|
|
286
289
|
create_fields = create_tool_fields(all_payload_fields)
|
|
287
290
|
if create_fields is not None:
|
|
288
291
|
owner, repo, body = (create_fields.get(each_key) for each_key in ("owner", "repo", "body"))
|
|
289
|
-
if isinstance(
|
|
290
|
-
return
|
|
291
|
-
return
|
|
292
|
+
if not isinstance(body, str) or followup_parent_number(body) is None:
|
|
293
|
+
return []
|
|
294
|
+
return [PullRequestDraft(owner, repo, body)] if isinstance(owner, str) and isinstance(repo, str) else []
|
|
292
295
|
command = shell_command(all_payload_fields)
|
|
293
296
|
if command is None:
|
|
297
|
+
return []
|
|
298
|
+
return _shell_followup_drafts(command, _working_directory(all_payload_fields))
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _open_pull_requests_once(
|
|
302
|
+
owner: str, repo: str, read_open: OpenPullRequestReader
|
|
303
|
+
) -> list[Mapping[str, object]] | None:
|
|
304
|
+
return answer_once(("open pull requests", owner, repo), lambda: _open_pull_requests_or_none(owner, repo, read_open))
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _open_pull_requests_or_none(
|
|
308
|
+
owner: str, repo: str, read_open: OpenPullRequestReader
|
|
309
|
+
) -> list[Mapping[str, object]] | None:
|
|
310
|
+
try:
|
|
311
|
+
return read_open(owner, repo)
|
|
312
|
+
except Exception:
|
|
294
313
|
return None
|
|
295
|
-
return _shell_draft(command, _working_directory(all_payload_fields))
|
|
296
314
|
|
|
297
315
|
|
|
298
316
|
def _reason_for_parent(
|
|
@@ -318,25 +336,25 @@ def duplicate_followup_reason(
|
|
|
318
336
|
) -> str | None:
|
|
319
337
|
"""Return a deny reason when an open pull request already follows up the same parent.
|
|
320
338
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
339
|
+
Every create body that names a parent is checked, in command order, and
|
|
340
|
+
the first duplicate denies. A read that fails skips that create. When no
|
|
341
|
+
create body carries a "Follow-up to #N" line, nothing is read, not even
|
|
342
|
+
the origin remote.
|
|
324
343
|
|
|
325
344
|
Args:
|
|
326
345
|
all_payload_fields: The parsed PreToolUse input.
|
|
327
346
|
read_open: Reads the open pull requests of an owner and repository;
|
|
328
347
|
None reads them from the GitHub API.
|
|
329
348
|
"""
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
return _reason_for_parent(parent_number, all_open_pull_requests)
|
|
349
|
+
reader = read_open or read_open_pull_requests
|
|
350
|
+
for each_draft in followup_drafts(all_payload_fields):
|
|
351
|
+
parent_number = followup_parent_number(each_draft.body)
|
|
352
|
+
if parent_number is None:
|
|
353
|
+
continue
|
|
354
|
+
all_open_pull_requests = _open_pull_requests_once(each_draft.owner, each_draft.repo, reader)
|
|
355
|
+
if all_open_pull_requests is None:
|
|
356
|
+
continue
|
|
357
|
+
reason = _reason_for_parent(parent_number, all_open_pull_requests)
|
|
358
|
+
if reason is not None:
|
|
359
|
+
return reason
|
|
360
|
+
return None
|
|
@@ -38,7 +38,7 @@ from contextvars import ContextVar
|
|
|
38
38
|
from dataclasses import dataclass, field
|
|
39
39
|
from enum import Enum
|
|
40
40
|
from pathlib import Path
|
|
41
|
-
from typing import cast
|
|
41
|
+
from typing import TypeVar, cast
|
|
42
42
|
|
|
43
43
|
hooks_directory = str(Path(__file__).resolve().parent.parent)
|
|
44
44
|
if hooks_directory not in sys.path:
|
|
@@ -51,6 +51,8 @@ from hooks_constants.gate_run_reads_constants import (
|
|
|
51
51
|
)
|
|
52
52
|
from hooks_constants.subprocess_window import hidden_window_creation_flags
|
|
53
53
|
|
|
54
|
+
AnswerType = TypeVar("AnswerType")
|
|
55
|
+
|
|
54
56
|
|
|
55
57
|
class ReadSlice(Enum):
|
|
56
58
|
"""Which deadline a read answers to, as seconds from the start of the gate process."""
|
|
@@ -120,7 +122,7 @@ def read_deadlines_started(clock: Callable[[], float] = time.monotonic) -> Itera
|
|
|
120
122
|
started_read_deadlines.reset(token)
|
|
121
123
|
|
|
122
124
|
|
|
123
|
-
def answer_once
|
|
125
|
+
def answer_once(question: Hashable, read: Callable[[], AnswerType]) -> AnswerType:
|
|
124
126
|
"""Return the open run's answer to ``question``, reading it the first time it is asked.
|
|
125
127
|
|
|
126
128
|
Args:
|
|
@@ -234,7 +236,7 @@ def _answer_within(child: subprocess.Popen[str], allowance: float, own_timeout_s
|
|
|
234
236
|
return printed_text if child.returncode == 0 else None
|
|
235
237
|
|
|
236
238
|
|
|
237
|
-
def _answers_in_a_daemon_thread
|
|
239
|
+
def _answers_in_a_daemon_thread(
|
|
238
240
|
read: Callable[[float], AnswerType], allowance: float
|
|
239
241
|
) -> tuple[list[AnswerType], list[Exception]]:
|
|
240
242
|
all_answers: list[AnswerType] = []
|
|
@@ -252,7 +254,7 @@ def _answers_in_a_daemon_thread[AnswerType](
|
|
|
252
254
|
return all_answers, all_errors
|
|
253
255
|
|
|
254
256
|
|
|
255
|
-
def answer_within_allowance
|
|
257
|
+
def answer_within_allowance(
|
|
256
258
|
read: Callable[[float], AnswerType], own_timeout_seconds: float, read_slice: ReadSlice
|
|
257
259
|
) -> AnswerType | None:
|
|
258
260
|
"""Run one read in a daemon thread and return its answer, or None when the allowance runs out first.
|
|
@@ -112,10 +112,17 @@ from hooks_constants.shell_command_wrappers import segment_program_and_arguments
|
|
|
112
112
|
|
|
113
113
|
|
|
114
114
|
class CreateCall(NamedTuple):
|
|
115
|
-
"""The title and
|
|
115
|
+
"""The title, body, head, base and repository one pull request create call passes.
|
|
116
|
+
|
|
117
|
+
None in title or body marks a value this gate cannot read. None in head,
|
|
118
|
+
base or repository marks a value the create does not name.
|
|
119
|
+
"""
|
|
116
120
|
|
|
117
121
|
title: str | None
|
|
118
122
|
body: str | None
|
|
123
|
+
head: str | None
|
|
124
|
+
base: str | None
|
|
125
|
+
repository: str | None
|
|
119
126
|
|
|
120
127
|
|
|
121
128
|
def _heading_level(line: str, heading_pattern: str) -> int | None:
|
|
@@ -235,6 +242,9 @@ def _shell_create_calls(command: str, working_directory: str) -> list[CreateCall
|
|
|
235
242
|
CreateCall(
|
|
236
243
|
title_from_arguments(all_create_arguments),
|
|
237
244
|
body_from_arguments(all_create_arguments, working_directory),
|
|
245
|
+
_flag_value(all_create_arguments, ALL_HEAD_FLAGS, HEAD_FLAG_ASSIGNMENT_PREFIX) or None,
|
|
246
|
+
_flag_value(all_create_arguments, ALL_BASE_FLAGS, BASE_FLAG_ASSIGNMENT_PREFIX) or None,
|
|
247
|
+
_flag_value(all_create_arguments, ALL_REPOSITORY_FLAGS, REPOSITORY_FLAG_ASSIGNMENT_PREFIX) or None,
|
|
238
248
|
)
|
|
239
249
|
for all_create_arguments in _all_create_argument_lists(command)
|
|
240
250
|
]
|
|
@@ -249,59 +259,29 @@ def _flag_value(all_create_arguments: list[str], all_flags: frozenset[str], assi
|
|
|
249
259
|
return None
|
|
250
260
|
|
|
251
261
|
|
|
252
|
-
def
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
command = shell_command(all_payload_fields)
|
|
256
|
-
if command is None:
|
|
257
|
-
return None
|
|
258
|
-
all_flag_values = [
|
|
259
|
-
_flag_value(all_create_arguments, all_flags, assignment_prefix)
|
|
260
|
-
for all_create_arguments in _all_create_argument_lists(command)
|
|
261
|
-
]
|
|
262
|
-
return next((each_value for each_value in all_flag_values if each_value), None)
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
def _named_create_field(
|
|
266
|
-
all_payload_fields: Mapping[str, object], field_name: str, all_flags: frozenset[str], assignment_prefix: str
|
|
267
|
-
) -> str | None:
|
|
268
|
-
create_fields = create_tool_fields(all_payload_fields)
|
|
269
|
-
field_value = None if create_fields is None else create_fields.get(field_name)
|
|
270
|
-
if isinstance(field_value, str) and field_value:
|
|
271
|
-
return field_value
|
|
272
|
-
return _shell_flag_value(all_payload_fields, all_flags, assignment_prefix)
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
def _named_head_branch(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
276
|
-
return _named_create_field(all_payload_fields, "head", ALL_HEAD_FLAGS, HEAD_FLAG_ASSIGNMENT_PREFIX)
|
|
262
|
+
def _named_text(all_create_fields: Mapping[str, object], field_name: str) -> str | None:
|
|
263
|
+
field_value = all_create_fields.get(field_name)
|
|
264
|
+
return field_value if isinstance(field_value, str) and field_value else None
|
|
277
265
|
|
|
278
266
|
|
|
279
|
-
def
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return _shell_flag_value(all_payload_fields, ALL_REPOSITORY_FLAGS, REPOSITORY_FLAG_ASSIGNMENT_PREFIX)
|
|
267
|
+
def _mcp_create_call(all_create_fields: Mapping[str, object]) -> CreateCall:
|
|
268
|
+
title = all_create_fields.get("title")
|
|
269
|
+
body = all_create_fields.get("body")
|
|
270
|
+
owner = _named_text(all_create_fields, "owner")
|
|
271
|
+
repository_name = _named_text(all_create_fields, "repo")
|
|
272
|
+
return CreateCall(
|
|
273
|
+
title if isinstance(title, str) else None,
|
|
274
|
+
body if isinstance(body, str) else "",
|
|
275
|
+
_named_text(all_create_fields, "head"),
|
|
276
|
+
_named_text(all_create_fields, "base"),
|
|
277
|
+
f"{owner}/{repository_name}" if owner and repository_name else None,
|
|
278
|
+
)
|
|
292
279
|
|
|
293
280
|
|
|
294
281
|
def _create_calls(all_payload_fields: Mapping[str, object]) -> list[CreateCall]:
|
|
295
282
|
create_fields = create_tool_fields(all_payload_fields)
|
|
296
283
|
if create_fields is not None:
|
|
297
|
-
|
|
298
|
-
body = create_fields.get("body")
|
|
299
|
-
return [
|
|
300
|
-
CreateCall(
|
|
301
|
-
title if isinstance(title, str) else None,
|
|
302
|
-
body if isinstance(body, str) else "",
|
|
303
|
-
)
|
|
304
|
-
]
|
|
284
|
+
return [_mcp_create_call(create_fields)]
|
|
305
285
|
command = shell_command(all_payload_fields)
|
|
306
286
|
if command is None:
|
|
307
287
|
return []
|
|
@@ -576,36 +556,38 @@ def missing_proof_reason(all_payload_fields: Mapping[str, object]) -> str | None
|
|
|
576
556
|
the run, and the line uses the word run outside code spans and says workflow
|
|
577
557
|
or GitHub Actions or names a covered workflow file; see
|
|
578
558
|
_first_unwaived_admission. The same covered line is exempt from the merge word.
|
|
559
|
+
The waiver reads the head branch of the create whose body holds the admission.
|
|
579
560
|
|
|
580
561
|
Args:
|
|
581
562
|
all_payload_fields: The parsed PreToolUse input.
|
|
582
563
|
"""
|
|
583
|
-
|
|
584
|
-
|
|
564
|
+
working_directory = str(all_payload_fields.get("cwd") or "")
|
|
565
|
+
for each_call in _create_calls(all_payload_fields):
|
|
566
|
+
if each_call.body is None:
|
|
585
567
|
return MISSING_BODY_REASON
|
|
586
|
-
section = proof_section(
|
|
568
|
+
section = proof_section(each_call.body)
|
|
587
569
|
if not section:
|
|
588
570
|
return MISSING_PROOF_REASON
|
|
589
|
-
section_reason = _proof_section_reason(section,
|
|
571
|
+
section_reason = _proof_section_reason(section, each_call, working_directory)
|
|
590
572
|
if section_reason is not None:
|
|
591
573
|
return section_reason
|
|
592
574
|
return None
|
|
593
575
|
|
|
594
576
|
|
|
595
577
|
def _proof_section_reason(
|
|
596
|
-
section: str,
|
|
578
|
+
section: str, create_call: CreateCall | None = None, working_directory: str = ""
|
|
597
579
|
) -> str | None:
|
|
598
580
|
if COMMAND_MARKER not in section:
|
|
599
581
|
return MISSING_PROOF_REASON
|
|
600
582
|
if _proof_runs_only_tests(section):
|
|
601
583
|
return TEST_ONLY_PROOF_REASON
|
|
602
584
|
all_admissions = unproven_admissions(section)
|
|
603
|
-
if
|
|
585
|
+
if create_call is None or create_call.body is None:
|
|
604
586
|
unwaived_admission = all_admissions[0] if all_admissions else None
|
|
605
587
|
all_covered_names: list[str] = []
|
|
606
588
|
else:
|
|
607
589
|
needs_waiver = bool(all_admissions) or merge_word_line(section) is not None
|
|
608
|
-
all_covered_names = _covered_workflow_names(
|
|
590
|
+
all_covered_names = _covered_workflow_names(create_call, working_directory) if needs_waiver else []
|
|
609
591
|
unwaived_admission = _first_unwaived_admission(all_admissions, all_covered_names)
|
|
610
592
|
if unwaived_admission is not None:
|
|
611
593
|
return UNPROVEN_PROOF_REASON.format(admission=unwaived_admission)
|
|
@@ -992,11 +974,8 @@ def remote_added_workflow_files(
|
|
|
992
974
|
return None
|
|
993
975
|
|
|
994
976
|
|
|
995
|
-
def _added_workflow_files_for_create(
|
|
996
|
-
|
|
997
|
-
head_branch = _named_head_branch(all_payload_fields)
|
|
998
|
-
repository = _named_repository(all_payload_fields)
|
|
999
|
-
base_branch = _named_base_branch(all_payload_fields)
|
|
977
|
+
def _added_workflow_files_for_create(create_call: CreateCall, working_directory: str) -> list[str]:
|
|
978
|
+
repository, base_branch, head_branch = create_call.repository, create_call.base, create_call.head
|
|
1000
979
|
return answer_once(
|
|
1001
980
|
("added workflow files", working_directory, repository, base_branch, head_branch),
|
|
1002
981
|
lambda: _read_added_workflow_files(working_directory, repository, base_branch, head_branch),
|
|
@@ -1067,7 +1046,7 @@ def _admission_is_about_a_first_run(admission: str, all_workflow_names: list[str
|
|
|
1067
1046
|
)
|
|
1068
1047
|
|
|
1069
1048
|
|
|
1070
|
-
def _covered_workflow_names(
|
|
1049
|
+
def _covered_workflow_names(create_call: CreateCall, working_directory: str) -> list[str]:
|
|
1071
1050
|
"""Return the added workflow files whose first run the body plans for after the merge.
|
|
1072
1051
|
|
|
1073
1052
|
::
|
|
@@ -1082,17 +1061,17 @@ def _covered_workflow_names(body: str, all_payload_fields: Mapping[str, object])
|
|
|
1082
1061
|
count as that prose. A sentence with a negation ahead of the verb does not count.
|
|
1083
1062
|
|
|
1084
1063
|
Args:
|
|
1085
|
-
|
|
1086
|
-
|
|
1064
|
+
create_call: The create whose body and head branch are read.
|
|
1065
|
+
working_directory: The session directory the hook payload names.
|
|
1087
1066
|
"""
|
|
1088
|
-
after_merge = _section_text(body, AFTER_MERGE_HEADING_PATTERN)
|
|
1067
|
+
after_merge = _section_text(create_call.body or "", AFTER_MERGE_HEADING_PATTERN)
|
|
1089
1068
|
if not after_merge:
|
|
1090
1069
|
return []
|
|
1091
1070
|
outside_fence_text = _text_outside_fences(after_merge)
|
|
1092
1071
|
if not _prose_names_a_recorder(re.sub(INLINE_CODE_SPAN_PATTERN, "", outside_fence_text)):
|
|
1093
1072
|
return []
|
|
1094
1073
|
all_names = []
|
|
1095
|
-
for each_path in _added_workflow_files_for_create(
|
|
1074
|
+
for each_path in _added_workflow_files_for_create(create_call, working_directory):
|
|
1096
1075
|
each_name = each_path.rsplit("/", 1)[-1]
|
|
1097
1076
|
if f"`{each_path}`" in outside_fence_text or f"`{each_name}`" in outside_fence_text:
|
|
1098
1077
|
all_names.append(each_name)
|
|
@@ -1124,29 +1103,48 @@ def missing_look_reason(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
|
1124
1103
|
git cannot read the branch, compare API lists only .py files -> None
|
|
1125
1104
|
git cannot read the branch, compare API returns 300 .py files -> UNREADABLE_CHANGES_REASON
|
|
1126
1105
|
neither git nor the compare API can read it, no picture -> UNREADABLE_CHANGES_REASON
|
|
1106
|
+
create --head a && create --head b, only b changes page.html -> MISSING_LOOK_REASON
|
|
1127
1107
|
|
|
1128
1108
|
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.
|
|
1109
|
+
the check passes and reads neither git nor the compare API. Each create
|
|
1110
|
+
is checked against its own head, base and repository, in command order,
|
|
1111
|
+
and the first deny wins.
|
|
1130
1112
|
|
|
1131
1113
|
Args:
|
|
1132
1114
|
all_payload_fields: The parsed PreToolUse input.
|
|
1133
1115
|
"""
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1116
|
+
working_directory = str(all_payload_fields.get("cwd") or "")
|
|
1117
|
+
all_calls_without_picture = [
|
|
1118
|
+
each_call
|
|
1119
|
+
for each_call in _create_calls(all_payload_fields)
|
|
1120
|
+
if each_call.body is not None
|
|
1121
|
+
and not re.search(PROOF_IMAGE_PATTERN, proof_section(each_call.body) or "", re.IGNORECASE)
|
|
1138
1122
|
]
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
all_visible_files = remote_visible_changed_files(
|
|
1146
|
-
_named_repository(all_payload_fields), _named_base_branch(all_payload_fields), head_branch
|
|
1147
|
-
)
|
|
1123
|
+
all_reasons = (_look_reason_for_create(each_call, working_directory) for each_call in all_calls_without_picture)
|
|
1124
|
+
return next((each_reason for each_reason in all_reasons if each_reason is not None), None)
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
def _look_reason_for_create(create_call: CreateCall, working_directory: str) -> str | None:
|
|
1128
|
+
all_visible_files = _visible_files_for_create(create_call, working_directory)
|
|
1148
1129
|
if all_visible_files == []:
|
|
1149
1130
|
return None
|
|
1150
1131
|
if all_visible_files is None:
|
|
1151
1132
|
return UNREADABLE_CHANGES_REASON
|
|
1152
1133
|
return MISSING_LOOK_REASON.format(changed_files=CHANGED_FILES_SEPARATOR.join(all_visible_files))
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
def _visible_files_for_create(create_call: CreateCall, working_directory: str) -> list[str] | None:
|
|
1137
|
+
repository, base_branch, head_branch = create_call.repository, create_call.base, create_call.head
|
|
1138
|
+
return answer_once(
|
|
1139
|
+
("visible changed files", working_directory, repository, base_branch, head_branch),
|
|
1140
|
+
lambda: _read_visible_files(working_directory, repository, base_branch, head_branch),
|
|
1141
|
+
)
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
def _read_visible_files(
|
|
1145
|
+
working_directory: str, repository: str | None, base_branch: str | None, head_branch: str | None
|
|
1146
|
+
) -> list[str] | None:
|
|
1147
|
+
all_visible_files = visible_changed_files(working_directory, head_branch)
|
|
1148
|
+
if all_visible_files is None:
|
|
1149
|
+
return remote_visible_changed_files(repository, base_branch, head_branch)
|
|
1150
|
+
return all_visible_files
|
|
@@ -16,6 +16,8 @@ if str(HOOKS_DIRECTORY) not in sys.path:
|
|
|
16
16
|
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
17
17
|
|
|
18
18
|
from blocking import followup_pr_dedupe as dedupe
|
|
19
|
+
from blocking.gate_run_reads import gate_run
|
|
20
|
+
from hooks_constants.followup_pr_dedupe_constants import DUPLICATE_FOLLOWUP_REASON_TEMPLATE
|
|
19
21
|
|
|
20
22
|
OPEN_FOLLOWUP = {
|
|
21
23
|
"number": 1769,
|
|
@@ -27,7 +29,6 @@ OTHER_FOLLOWUP = {
|
|
|
27
29
|
"html_url": "https://github.com/jl-cmd/claude-dev-env/pull/1777",
|
|
28
30
|
"body": "follow-up to #17",
|
|
29
31
|
}
|
|
30
|
-
ELAPSED_SLACK_SECONDS = 8.0
|
|
31
32
|
|
|
32
33
|
|
|
33
34
|
def _reader(all_open: list[Mapping[str, object]]) -> dedupe.OpenPullRequestReader:
|
|
@@ -134,10 +135,8 @@ def test_read_open_pull_requests_should_bound_the_whole_read(monkeypatch: pytest
|
|
|
134
135
|
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
|
135
136
|
monkeypatch.setattr(dedupe, "GITHUB_API_ROOT", f"http://127.0.0.1:{server.server_address[1]}")
|
|
136
137
|
monkeypatch.setattr(dedupe, "READ_TIMEOUT_SECONDS", 1)
|
|
137
|
-
started = time.monotonic()
|
|
138
138
|
with pytest.raises(TimeoutError):
|
|
139
139
|
dedupe.read_open_pull_requests("jl-cmd", "claude-dev-env")
|
|
140
|
-
assert time.monotonic() - started < 1 + ELAPSED_SLACK_SECONDS
|
|
141
140
|
server.shutdown()
|
|
142
141
|
|
|
143
142
|
|
|
@@ -184,3 +183,82 @@ def test_create_tool_fields_should_read_a_pull_request_dispatch_as_a_create() ->
|
|
|
184
183
|
def test_create_tool_fields_should_skip_a_call_that_opens_no_pull_request() -> None:
|
|
185
184
|
assert dedupe.create_tool_fields(_dispatch_payload({"work_order": "wo-1"})) is None
|
|
186
185
|
assert dedupe.create_tool_fields({"tool_name": "Bash", "tool_input": {"command": "gh pr list"}}) is None
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _open_followup(number: int, parent_number: int) -> dict[str, object]:
|
|
189
|
+
return {
|
|
190
|
+
"number": number,
|
|
191
|
+
"html_url": f"https://github.com/o/r/pull/{number}",
|
|
192
|
+
"body": f"Follow-up to #{parent_number}",
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _duplicate_reason(open_followup: Mapping[str, object], parent_number: int) -> str:
|
|
197
|
+
return DUPLICATE_FOLLOWUP_REASON_TEMPLATE.format(
|
|
198
|
+
open_number=open_followup["number"], parent_number=parent_number, open_url=open_followup["html_url"]
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _keyed_reader(
|
|
203
|
+
all_open_by_repository: Mapping[tuple[str, str], list[Mapping[str, object]] | Exception],
|
|
204
|
+
all_asked_repositories: list[tuple[str, str]],
|
|
205
|
+
) -> dedupe.OpenPullRequestReader:
|
|
206
|
+
def read(owner: str, repo: str) -> list[Mapping[str, object]]:
|
|
207
|
+
all_asked_repositories.append((owner, repo))
|
|
208
|
+
answer = all_open_by_repository[(owner, repo)]
|
|
209
|
+
if isinstance(answer, Exception):
|
|
210
|
+
raise answer
|
|
211
|
+
return answer
|
|
212
|
+
|
|
213
|
+
return read
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _chained_inline_creates(all_repositories_and_bodies: list[tuple[str, str]]) -> dict[str, object]:
|
|
217
|
+
all_creates = [
|
|
218
|
+
f'gh pr create --repo {each_repository} --title "fix: {each_index}" --body "{each_body}"'
|
|
219
|
+
for each_index, (each_repository, each_body) in enumerate(all_repositories_and_bodies)
|
|
220
|
+
]
|
|
221
|
+
return {"tool_name": "Bash", "tool_input": {"command": " && ".join(all_creates)}}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def test_chained_creates_should_deny_when_only_the_second_body_repeats_an_open_followup() -> None:
|
|
225
|
+
open_followup = _open_followup(9, 7)
|
|
226
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
227
|
+
payload = _chained_inline_creates([("o/r", "Adds a hook."), ("o/r", "Follow-up to #7")])
|
|
228
|
+
reason = dedupe.duplicate_followup_reason(
|
|
229
|
+
payload, _keyed_reader({("o", "r"): [open_followup]}, all_asked_repositories)
|
|
230
|
+
)
|
|
231
|
+
assert reason == _duplicate_reason(open_followup, 7)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_followup_drafts_should_count_a_quoted_create_once() -> None:
|
|
235
|
+
payload = _chained_inline_creates([("o/r", "Follow-up to #7"), ("o/r", "Follow-up to #8")])
|
|
236
|
+
assert dedupe.followup_drafts(payload) == [
|
|
237
|
+
dedupe.PullRequestDraft("o", "r", "Follow-up to #7"),
|
|
238
|
+
dedupe.PullRequestDraft("o", "r", "Follow-up to #8"),
|
|
239
|
+
]
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_chained_followups_should_read_one_open_list_per_repository() -> None:
|
|
243
|
+
open_followup = _open_followup(11, 8)
|
|
244
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
245
|
+
payload = _chained_inline_creates([("o/r", "Follow-up to #7"), ("o/r", "Follow-up to #8")])
|
|
246
|
+
with gate_run():
|
|
247
|
+
reason = dedupe.duplicate_followup_reason(
|
|
248
|
+
payload, _keyed_reader({("o", "r"): [open_followup]}, all_asked_repositories)
|
|
249
|
+
)
|
|
250
|
+
assert reason == _duplicate_reason(open_followup, 8)
|
|
251
|
+
assert all_asked_repositories == [("o", "r")]
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def test_followup_read_failure_should_skip_only_that_create() -> None:
|
|
255
|
+
open_followup = _open_followup(9, 7)
|
|
256
|
+
all_asked_repositories: list[tuple[str, str]] = []
|
|
257
|
+
all_open_by_repository: dict[tuple[str, str], list[Mapping[str, object]] | Exception] = {
|
|
258
|
+
("a", "r"): OSError("network down"),
|
|
259
|
+
("b", "r"): [open_followup],
|
|
260
|
+
}
|
|
261
|
+
payload = _chained_inline_creates([("a/r", "Follow-up to #5"), ("b/r", "Follow-up to #7")])
|
|
262
|
+
reason = dedupe.duplicate_followup_reason(payload, _keyed_reader(all_open_by_repository, all_asked_repositories))
|
|
263
|
+
assert reason == _duplicate_reason(open_followup, 7)
|
|
264
|
+
assert all_asked_repositories == [("a", "r"), ("b", "r")]
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"""Behavior tests for the answers one pull request lifecycle gate run remembers."""
|
|
2
2
|
|
|
3
|
+
import ast
|
|
3
4
|
import json
|
|
5
|
+
import subprocess
|
|
4
6
|
import sys
|
|
5
|
-
import
|
|
7
|
+
import threading
|
|
6
8
|
from pathlib import Path
|
|
7
9
|
|
|
8
10
|
import pytest
|
|
@@ -31,14 +33,25 @@ from hooks_constants.gate_run_reads_constants import (
|
|
|
31
33
|
)
|
|
32
34
|
|
|
33
35
|
CLOCK_START = 100.0
|
|
36
|
+
OLDEST_SUPPORTED_PYTHON = (3, 11)
|
|
34
37
|
HIDDEN_WINDOW_FLAGS = 0x08000000
|
|
35
38
|
SLEEP_THEN_PRINT = "import time; time.sleep(30); print('late')"
|
|
36
|
-
|
|
39
|
+
HELD_READ_GIVES_UP_SECONDS = 30
|
|
40
|
+
GRANDCHILD_WAITS_FOR_RELEASE = (
|
|
41
|
+
"import os, sys, time\n"
|
|
42
|
+
"gave_up_path, release_path = sys.argv[1], sys.argv[2]\n"
|
|
43
|
+
"polls_left = 300\n"
|
|
44
|
+
"while polls_left and not os.path.exists(release_path):\n"
|
|
45
|
+
" time.sleep(0.1)\n"
|
|
46
|
+
" polls_left -= 1\n"
|
|
47
|
+
"if not os.path.exists(release_path):\n"
|
|
48
|
+
" open(gave_up_path, 'w').close()\n"
|
|
49
|
+
)
|
|
50
|
+
CHILD_STARTS_A_GRANDCHILD = (
|
|
37
51
|
"import subprocess, sys, time;"
|
|
38
|
-
" subprocess.Popen([sys.executable, '-c',
|
|
52
|
+
" subprocess.Popen([sys.executable, '-c', *sys.argv[1:]]);"
|
|
39
53
|
" time.sleep(30)"
|
|
40
54
|
)
|
|
41
|
-
ELAPSED_SLACK_SECONDS = 8.0
|
|
42
55
|
GATE_COMMAND_SUFFIX = "hooks/blocking/pr_lifecycle_" + "skill_gate.py"
|
|
43
56
|
|
|
44
57
|
|
|
@@ -50,6 +63,31 @@ class FakeClock:
|
|
|
50
63
|
return self.now
|
|
51
64
|
|
|
52
65
|
|
|
66
|
+
class RecordedChild(subprocess.Popen[str]):
|
|
67
|
+
"""A child that notes the timeout of each wait its caller makes."""
|
|
68
|
+
|
|
69
|
+
def __init__(self, *all_arguments: object, **all_options: object) -> None:
|
|
70
|
+
self.all_wait_timeouts: list[object] = []
|
|
71
|
+
super().__init__(*all_arguments, **all_options)
|
|
72
|
+
|
|
73
|
+
def communicate(self, *all_arguments: object, **all_options: object) -> tuple[str, str]:
|
|
74
|
+
self.all_wait_timeouts.append(all_options.get("timeout"))
|
|
75
|
+
return super().communicate(*all_arguments, **all_options)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@pytest.fixture
|
|
79
|
+
def all_started_children(monkeypatch: pytest.MonkeyPatch) -> list[RecordedChild]:
|
|
80
|
+
all_children: list[RecordedChild] = []
|
|
81
|
+
|
|
82
|
+
def start(*all_arguments: object, **all_options: object) -> RecordedChild:
|
|
83
|
+
child = RecordedChild(*all_arguments, **all_options)
|
|
84
|
+
all_children.append(child)
|
|
85
|
+
return child
|
|
86
|
+
|
|
87
|
+
monkeypatch.setattr(gate_run_reads.subprocess, "Popen", start)
|
|
88
|
+
return all_children
|
|
89
|
+
|
|
90
|
+
|
|
53
91
|
def _counting_read(all_reads: list[str], answer: str | None):
|
|
54
92
|
def read() -> str | None:
|
|
55
93
|
all_reads.append("read")
|
|
@@ -157,46 +195,62 @@ def test_child_stdout_should_start_its_child_with_the_hidden_window_flags(monkey
|
|
|
157
195
|
assert all_seen_flags == [HIDDEN_WINDOW_FLAGS]
|
|
158
196
|
|
|
159
197
|
|
|
160
|
-
def test_child_stdout_should_kill_a_child_that_outlives_its_own_timeout(
|
|
198
|
+
def test_child_stdout_should_kill_a_child_that_outlives_its_own_timeout(
|
|
199
|
+
all_started_children: list[RecordedChild],
|
|
200
|
+
) -> None:
|
|
161
201
|
with gate_run() as run:
|
|
162
|
-
started = time.monotonic()
|
|
163
202
|
assert child_stdout([sys.executable, "-c", SLEEP_THEN_PRINT], 0.5, ReadSlice.PROOF_AND_LOOK) is None
|
|
164
|
-
|
|
165
|
-
assert
|
|
203
|
+
(child,) = all_started_children
|
|
204
|
+
assert child.all_wait_timeouts == [0.5, KILL_GRACE_SECONDS]
|
|
205
|
+
assert child.wait() != 0
|
|
166
206
|
assert not run.was_read_cut_short
|
|
167
207
|
|
|
168
208
|
|
|
169
|
-
def test_child_stdout_should_stop_waiting_when_a_grandchild_holds_the_pipe() -> None:
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
209
|
+
def test_child_stdout_should_stop_waiting_when_a_grandchild_holds_the_pipe(tmp_path: Path) -> None:
|
|
210
|
+
gave_up_path = tmp_path / "gave-up"
|
|
211
|
+
release_path = tmp_path / "release"
|
|
212
|
+
command = [
|
|
213
|
+
sys.executable,
|
|
214
|
+
"-c",
|
|
215
|
+
CHILD_STARTS_A_GRANDCHILD,
|
|
216
|
+
GRANDCHILD_WAITS_FOR_RELEASE,
|
|
217
|
+
str(gave_up_path),
|
|
218
|
+
str(release_path),
|
|
219
|
+
]
|
|
220
|
+
answer = child_stdout(command, 1, ReadSlice.PROOF_AND_LOOK)
|
|
221
|
+
release_path.touch()
|
|
222
|
+
assert answer is None
|
|
223
|
+
assert not gave_up_path.exists()
|
|
173
224
|
|
|
174
225
|
|
|
175
|
-
def test_child_stdout_should_mark_the_run_when_the_deadline_cuts_a_read_short(
|
|
226
|
+
def test_child_stdout_should_mark_the_run_when_the_deadline_cuts_a_read_short(
|
|
227
|
+
all_started_children: list[RecordedChild],
|
|
228
|
+
) -> None:
|
|
176
229
|
clock = FakeClock()
|
|
177
230
|
with read_deadlines_started(clock), gate_run() as run:
|
|
178
231
|
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS - 0.4
|
|
179
|
-
started = time.monotonic()
|
|
180
232
|
assert child_stdout([sys.executable, "-c", SLEEP_THEN_PRINT], 5, ReadSlice.PROOF_AND_LOOK) is None
|
|
181
|
-
|
|
182
|
-
assert
|
|
233
|
+
(child,) = all_started_children
|
|
234
|
+
assert child.all_wait_timeouts == [pytest.approx(0.4), KILL_GRACE_SECONDS]
|
|
235
|
+
assert child.wait() != 0
|
|
183
236
|
assert run.was_read_cut_short
|
|
184
237
|
|
|
185
238
|
|
|
186
|
-
def _slow_read(allowance: float) -> list[str]:
|
|
187
|
-
time.sleep(allowance + 30)
|
|
188
|
-
return ["late"]
|
|
189
|
-
|
|
190
|
-
|
|
191
239
|
def test_answer_within_allowance_should_bound_the_whole_read() -> None:
|
|
192
240
|
clock = FakeClock()
|
|
241
|
+
release = threading.Event()
|
|
242
|
+
|
|
243
|
+
def held_read(allowance: float) -> list[str]:
|
|
244
|
+
release.wait(HELD_READ_GIVES_UP_SECONDS)
|
|
245
|
+
return ["late"]
|
|
246
|
+
|
|
193
247
|
with read_deadlines_started(clock), gate_run() as run:
|
|
194
248
|
clock.now = CLOCK_START + PROOF_AND_LOOK_READS_SECONDS + FOLLOWUP_READS_SECONDS - 0.3
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
249
|
+
try:
|
|
250
|
+
answer = answer_within_allowance(held_read, 4, ReadSlice.FOLLOWUP)
|
|
251
|
+
finally:
|
|
252
|
+
release.set()
|
|
198
253
|
assert answer is None
|
|
199
|
-
assert elapsed < 0.3 + ELAPSED_SLACK_SECONDS
|
|
200
254
|
assert run.was_read_cut_short
|
|
201
255
|
|
|
202
256
|
|
|
@@ -239,3 +293,8 @@ def test_registered_timeout_should_outlast_every_read_deadline_and_kill_grace()
|
|
|
239
293
|
+ KILL_GRACE_SECONDS * MOST_CUT_READS_PER_RUN
|
|
240
294
|
)
|
|
241
295
|
assert _registered_gate_timeout_seconds() >= last_read_ends_at + STARTUP_AND_SCAN_MARGIN_SECONDS
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def test_gate_run_reads_should_parse_under_the_oldest_supported_python() -> None:
|
|
299
|
+
source = Path(gate_run_reads.__file__).read_text(encoding="utf-8")
|
|
300
|
+
assert ast.parse(source, feature_version=OLDEST_SUPPORTED_PYTHON).body
|
|
@@ -436,6 +436,44 @@ def test_deny_should_add_the_reads_ran_out_sentence_when_the_deadline_cut_a_read
|
|
|
436
436
|
assert decision["hookSpecificOutput"]["permissionDecisionReason"] == UNREADABLE_CHANGES_REASON + READS_RAN_OUT_SENTENCE
|
|
437
437
|
|
|
438
438
|
|
|
439
|
+
class SettableClock:
|
|
440
|
+
"""Reads the seconds it was last set to, starting at zero."""
|
|
441
|
+
|
|
442
|
+
def __init__(self) -> None:
|
|
443
|
+
self.seconds = 0.0
|
|
444
|
+
|
|
445
|
+
def __call__(self) -> float:
|
|
446
|
+
return self.seconds
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def test_deny_should_add_the_reads_ran_out_sentence_when_the_deadline_cuts_the_second_head(
|
|
450
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
451
|
+
) -> None:
|
|
452
|
+
all_asked_commands = _unstarted_children(monkeypatch)
|
|
453
|
+
clock = SettableClock()
|
|
454
|
+
original_gh_api_lines = gate.proof._gh_api_lines
|
|
455
|
+
|
|
456
|
+
def compare_that_spends_the_slice(endpoint: str, query: str) -> list[str] | None:
|
|
457
|
+
if endpoint.endswith("...x"):
|
|
458
|
+
clock.seconds = PROOF_AND_LOOK_READS_SECONDS + 1.0
|
|
459
|
+
return [json.dumps(["M", "", "tool.py"])]
|
|
460
|
+
return original_gh_api_lines(endpoint, query)
|
|
461
|
+
|
|
462
|
+
monkeypatch.setattr(gate.proof, "_gh_api_lines", compare_that_spends_the_slice)
|
|
463
|
+
body = PROVEN_FOLLOWUP_BODY.replace("Follow-up to #1731", "Adds a guard.")
|
|
464
|
+
(tmp_path / "body.md").write_text(body, encoding="utf-8")
|
|
465
|
+
all_creates = [
|
|
466
|
+
CREATE_WORDS + f' --title "fix: {each_head}" --body-file body.md --repo o/r --base main --head {each_head}'
|
|
467
|
+
for each_head in ("x", "y")
|
|
468
|
+
]
|
|
469
|
+
payload = {"tool_name": "Bash", "tool_input": {"command": " && ".join(all_creates)}, "cwd": str(tmp_path)}
|
|
470
|
+
with read_deadlines_started(clock):
|
|
471
|
+
decision = gate.decision_for(payload)
|
|
472
|
+
assert decision is not None
|
|
473
|
+
assert decision["hookSpecificOutput"]["permissionDecisionReason"] == UNREADABLE_CHANGES_REASON + READS_RAN_OUT_SENTENCE
|
|
474
|
+
assert not any(each_command[:1] == ["gh"] for each_command in all_asked_commands)
|
|
475
|
+
|
|
476
|
+
|
|
439
477
|
def test_deny_should_keep_its_reason_when_every_read_had_time(tmp_path: Path) -> None:
|
|
440
478
|
decision = gate.decision_for(_mcp_create_without_repository(tmp_path))
|
|
441
479
|
assert decision is not None
|
|
@@ -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.gate_run_reads import gate_run
|
|
15
16
|
from blocking.pr_lifecycle_skill_gate import decision_for
|
|
16
17
|
from hooks_constants.pull_request_proof_constants import (
|
|
17
18
|
COMPARE_FILE_LIST_LIMIT,
|
|
@@ -21,6 +22,7 @@ from hooks_constants.pull_request_proof_constants import (
|
|
|
21
22
|
MISSING_EVAL_SECTION_REASON,
|
|
22
23
|
MISSING_EXISTING_WORK_REASON,
|
|
23
24
|
MISSING_FLOWCHART_REASON,
|
|
25
|
+
MISSING_LOOK_REASON,
|
|
24
26
|
MISSING_PROOF_REASON,
|
|
25
27
|
TEST_ONLY_PROOF_REASON,
|
|
26
28
|
UNPROVEN_PROOF_REASON,
|
|
@@ -1662,3 +1664,68 @@ def test_create_calls_and_other_tools_should_pass_the_edit_check(tmp_path: Path)
|
|
|
1662
1664
|
unrun_body = "## Proof in practice\nRan `python run.py`.\nNot run: x.\n"
|
|
1663
1665
|
assert gate.edit_proof_reason(_mcp_create(unrun_body)) is None
|
|
1664
1666
|
assert gate.edit_proof_reason(_shell("git push -u origin b", tmp_path)) is None
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
def _chained_creates(tmp_path: Path, all_flags_and_bodies: list[tuple[str, str]]) -> str:
|
|
1670
|
+
all_creates = []
|
|
1671
|
+
for each_index, (each_flags, each_body) in enumerate(all_flags_and_bodies):
|
|
1672
|
+
body_path = tmp_path / f"body_{each_index}.md"
|
|
1673
|
+
body_path.write_text(each_body, encoding="utf-8")
|
|
1674
|
+
all_creates.append(f'gh pr create --title "fix: {each_index}" --body-file "{body_path}" {each_flags}')
|
|
1675
|
+
return " && ".join(all_creates)
|
|
1676
|
+
|
|
1677
|
+
|
|
1678
|
+
def _clone_with_quiet_branch(tmp_path: Path) -> Path:
|
|
1679
|
+
clone_path = _repository_with_change(tmp_path / "repository", "page.html")
|
|
1680
|
+
subprocess.run(["git", "-C", str(clone_path), "branch", "quiet", "main"], check=True)
|
|
1681
|
+
return clone_path
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
def test_chained_creates_should_deny_when_only_the_second_branch_changes_a_page(tmp_path: Path) -> None:
|
|
1685
|
+
clone_path = _clone_with_quiet_branch(tmp_path)
|
|
1686
|
+
subprocess.run(["git", "-C", str(clone_path), "checkout", "-q", "main"], check=True)
|
|
1687
|
+
command = _chained_creates(tmp_path, [("--head quiet", PROVEN_BODY), ("--head feature", PROVEN_BODY)])
|
|
1688
|
+
reason = gate.missing_look_reason(_shell(command, clone_path))
|
|
1689
|
+
assert reason == MISSING_LOOK_REASON.format(changed_files="page.html")
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
def test_chained_create_without_a_head_should_read_its_own_checkout(tmp_path: Path) -> None:
|
|
1693
|
+
clone_path = _clone_with_quiet_branch(tmp_path)
|
|
1694
|
+
picture_body = PROVEN_BODY + "\n"
|
|
1695
|
+
command = _chained_creates(tmp_path, [("", PROVEN_BODY), ("--head quiet", picture_body)])
|
|
1696
|
+
reason = gate.missing_look_reason(_shell(command, clone_path))
|
|
1697
|
+
assert reason == MISSING_LOOK_REASON.format(changed_files="page.html")
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
def test_chained_creates_should_check_each_waiver_against_its_own_head(
|
|
1701
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
1702
|
+
) -> None:
|
|
1703
|
+
all_endpoints: list[str] = []
|
|
1704
|
+
all_responses = {
|
|
1705
|
+
"repos/o/r/compare/main...x": _workflow_compare_lines(1, ["tool.py"]),
|
|
1706
|
+
"repos/o/r/compare/main...y": _workflow_compare_lines(1, [f"{WORKFLOW_DIRECTORY}/new.yml"]),
|
|
1707
|
+
}
|
|
1708
|
+
monkeypatch.setattr(gate, "_gh_api_lines", _fake_gh_api(all_responses, all_endpoints))
|
|
1709
|
+
shared_flags = "--repo o/r --base main"
|
|
1710
|
+
command = _chained_creates(
|
|
1711
|
+
tmp_path, [(f"--head x {shared_flags}", PROVEN_BODY), (f"--head y {shared_flags}", WAIVER_BODY)]
|
|
1712
|
+
)
|
|
1713
|
+
assert gate.missing_proof_reason(_shell(command, tmp_path)) is None
|
|
1714
|
+
assert all_endpoints == ["repos/o/r/compare/main...y"]
|
|
1715
|
+
|
|
1716
|
+
|
|
1717
|
+
def test_chained_creates_should_read_the_compare_api_once_per_distinct_head(
|
|
1718
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
1719
|
+
) -> None:
|
|
1720
|
+
all_endpoints: list[str] = []
|
|
1721
|
+
python_only = [_compare_line("M", "", "tool.py")]
|
|
1722
|
+
all_responses = {"repos/o/r/compare/main...x": python_only, "repos/o/r/compare/main...y": python_only}
|
|
1723
|
+
monkeypatch.setattr(gate, "_gh_api_lines", _fake_gh_api(all_responses, all_endpoints))
|
|
1724
|
+
shared_flags = "--repo o/r --base main"
|
|
1725
|
+
command = _chained_creates(
|
|
1726
|
+
tmp_path,
|
|
1727
|
+
[(f"--head {each_head} {shared_flags}", PROVEN_BODY) for each_head in ("x", "y", "x")],
|
|
1728
|
+
)
|
|
1729
|
+
with gate_run():
|
|
1730
|
+
assert gate.missing_look_reason(_shell(command, tmp_path)) is None
|
|
1731
|
+
assert all_endpoints == ["repos/o/r/compare/main...x", "repos/o/r/compare/main...y"]
|
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"]
|