claude-dev-env 8.117.3 → 8.117.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.
Files changed (75) hide show
  1. package/_shared/pr-loop/scripts/pyproject.toml +1 -1
  2. package/_shared/process-tree/scripts/pyproject.toml +1 -1
  3. package/bin/cde.mjs +4 -3
  4. package/bin/cde.test.mjs +87 -1
  5. package/bin/codex-compat.mjs +4 -3
  6. package/bin/codex-compat.test.mjs +91 -1
  7. package/bin/install-plan.mjs +2 -1
  8. package/bin/install.mjs +24 -7
  9. package/bin/install.plan.test.mjs +15 -0
  10. package/bin/install.test.mjs +61 -0
  11. package/bin/python-probe.mjs +42 -0
  12. package/bin/python-probe.test.mjs +19 -2
  13. package/docs/rule-guides/features-start-with-an-eval.md +3 -3
  14. package/docs/rule-guides/proof-before-pull-request.md +2 -2
  15. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +11 -4
  16. package/hooks/blocking/build_eval_before_eval_write_gate.py +10 -4
  17. package/hooks/blocking/followup_pr_dedupe.py +381 -64
  18. package/hooks/blocking/gate_child_process.py +187 -0
  19. package/hooks/blocking/gate_run_reads.py +116 -35
  20. package/hooks/blocking/merge_review_gate.py +93 -10
  21. package/hooks/blocking/merge_review_gate_parts/create_folders.py +346 -0
  22. package/hooks/blocking/merge_review_gate_parts/create_options.py +86 -0
  23. package/hooks/blocking/merge_review_gate_parts/folder_state.py +107 -0
  24. package/hooks/blocking/merge_review_gate_parts/open_request.py +199 -64
  25. package/hooks/blocking/merge_review_gate_parts/quoted_lines.py +283 -0
  26. package/hooks/blocking/merge_review_gate_parts/wrapped_creates.py +126 -0
  27. package/hooks/blocking/pr_lifecycle_skill_gate.py +61 -18
  28. package/hooks/blocking/pre_tool_use_dispatcher.py +25 -3
  29. package/hooks/blocking/pull_request_proof.py +5 -51
  30. package/hooks/blocking/pull_request_proof_edit_gate.py +19 -5
  31. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +54 -0
  32. package/hooks/blocking/test_build_eval_before_eval_write_gate.py +64 -0
  33. package/hooks/blocking/test_followup_pr_dedupe.py +464 -1
  34. package/hooks/blocking/test_gate_child_process.py +64 -0
  35. package/hooks/blocking/test_gate_run_reads.py +364 -4
  36. package/hooks/blocking/test_merge_review_gate.py +832 -39
  37. package/hooks/blocking/test_pr_lifecycle_skill_gate.py +130 -4
  38. package/hooks/blocking/test_pre_tool_use_dispatcher.py +64 -0
  39. package/hooks/blocking/test_pull_request_proof.py +16 -0
  40. package/hooks/blocking/test_pull_request_proof_edit_gate.py +60 -0
  41. package/hooks/blocking/tests/test_create_folders.py +290 -0
  42. package/hooks/blocking/tests/test_create_options.py +34 -0
  43. package/hooks/blocking/tests/test_folder_state.py +40 -0
  44. package/hooks/blocking/tests/test_open_request.py +350 -44
  45. package/hooks/blocking/tests/test_quoted_lines.py +86 -0
  46. package/hooks/blocking/tests/test_wrapped_creates.py +42 -0
  47. package/hooks/features/bash-dispatchers.md +1 -1
  48. package/hooks/features/conduct-gates.md +7 -7
  49. package/hooks/features/write-edit-blocking.md +3 -3
  50. package/hooks/hooks_constants/build_eval_before_eval_write_gate_constants.py +5 -0
  51. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +34 -1
  52. package/hooks/hooks_constants/gate_fault.py +52 -0
  53. package/hooks/hooks_constants/gate_fault_constants.py +13 -0
  54. package/hooks/hooks_constants/gate_run_reads_constants.py +9 -1
  55. package/hooks/hooks_constants/merge_review_gate_constants.py +94 -1
  56. package/hooks/hooks_constants/piped_pytest_blocker_constants.py +2 -0
  57. package/hooks/hooks_constants/pr_lifecycle_skill_gate_constants.py +6 -1
  58. package/hooks/hooks_constants/pre_tool_use_stdin.py +23 -3
  59. package/hooks/hooks_constants/shell_command_pipeline.py +68 -8
  60. package/hooks/hooks_constants/shell_command_segments.py +2 -2
  61. package/hooks/hooks_constants/shell_command_wrappers.py +3 -2
  62. package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +7 -0
  63. package/hooks/hooks_constants/test_gate_fault.py +41 -0
  64. package/hooks/hooks_constants/test_piped_pytest_blocker_constants.py +5 -0
  65. package/hooks/hooks_constants/test_pr_lifecycle_skill_gate_constants.py +10 -0
  66. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +97 -0
  67. package/hooks/hooks_constants/test_shell_command_pipeline.py +55 -0
  68. package/hooks/hooks_constants/test_shell_command_segments.py +15 -0
  69. package/hooks/hooks_constants/test_shell_command_wrappers.py +6 -0
  70. package/hooks/hooks_constants/transcript_skill_scan_constants.py +6 -0
  71. package/hooks/pyproject.toml +2 -3
  72. package/hooks/test_python_floor.py +197 -0
  73. package/hooks/test_transcript_skill_scan.py +324 -0
  74. package/hooks/transcript_skill_scan.py +233 -2
  75. package/package.json +1 -1
@@ -0,0 +1,187 @@
1
+ """Start, hold and stop the child processes the gate reads from.
2
+
3
+ ::
4
+
5
+ started_child(["git", "status"]) -> a child with its output piped
6
+ job_holding(child) -> its Windows job, or None on POSIX
7
+ stop_child(child, job) -> the child and every process it started end
8
+ close_job(job) -> the job handle closes
9
+
10
+ A stopped child gets a short grace to close its pipes, so a grandchild
11
+ that holds the output pipe cannot hold the gate.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import ctypes
17
+ import os
18
+ import signal
19
+ import subprocess
20
+ import sys
21
+ from collections.abc import Mapping, Sequence
22
+ from pathlib import Path
23
+ from typing import Any
24
+
25
+ hooks_directory = str(Path(__file__).resolve().parent.parent)
26
+ if hooks_directory not in sys.path:
27
+ sys.path.insert(0, hooks_directory)
28
+
29
+ from hooks_constants.gate_run_reads_constants import (
30
+ ALL_WINDOWS_TREE_KILL_WORDS,
31
+ CLOSE_HANDLE_CALL,
32
+ KERNEL32_LIBRARY_NAME,
33
+ KILL_GRACE_SECONDS,
34
+ PROCESS_HANDLE_ATTRIBUTE,
35
+ STOPPED_JOB_EXIT_CODE,
36
+ TERMINATE_JOB_CALL,
37
+ WINDOWS_TREE_KILL_SECONDS,
38
+ )
39
+ from hooks_constants.subprocess_window import hidden_window_creation_flags
40
+
41
+
42
+ def started_child(
43
+ all_command_words: Sequence[str],
44
+ working_directory: str | None,
45
+ all_environment_variables: Mapping[str, str] | None,
46
+ ) -> subprocess.Popen[str] | None:
47
+ """Start one read-only child with its output piped, or None when it cannot start.
48
+
49
+ Args:
50
+ all_command_words: The program and its arguments.
51
+ working_directory: The directory the child runs in, or None for the gate's own.
52
+ all_environment_variables: The child's whole environment, or None for the gate's own.
53
+ """
54
+ try:
55
+ return subprocess.Popen(
56
+ list(all_command_words),
57
+ cwd=working_directory or None,
58
+ env=dict(all_environment_variables) if all_environment_variables is not None else None,
59
+ stdin=subprocess.DEVNULL,
60
+ stdout=subprocess.PIPE,
61
+ stderr=subprocess.DEVNULL,
62
+ text=True,
63
+ encoding="utf-8",
64
+ errors="replace",
65
+ start_new_session=sys.platform != "win32",
66
+ creationflags=hidden_window_creation_flags(),
67
+ )
68
+ except OSError:
69
+ return None
70
+
71
+
72
+ def _drain_within(child: subprocess.Popen[str], grace_seconds: float) -> None:
73
+ try:
74
+ child.communicate(timeout=grace_seconds)
75
+ except subprocess.TimeoutExpired:
76
+ return
77
+
78
+
79
+ def _kernel32() -> Any:
80
+ if sys.platform != "win32":
81
+ raise OSError("kernel32 exists only on Windows")
82
+ kernel32 = ctypes.WinDLL(KERNEL32_LIBRARY_NAME, use_last_error=True)
83
+ kernel32.CreateJobObjectW.restype = ctypes.c_void_p
84
+ kernel32.CreateJobObjectW.argtypes = (ctypes.c_void_p, ctypes.c_wchar_p)
85
+ kernel32.AssignProcessToJobObject.argtypes = (ctypes.c_void_p, ctypes.c_void_p)
86
+ kernel32.TerminateJobObject.argtypes = (ctypes.c_void_p, ctypes.c_uint)
87
+ kernel32.CloseHandle.argtypes = (ctypes.c_void_p,)
88
+ return kernel32
89
+
90
+
91
+ def job_holding(child: subprocess.Popen[str]) -> int | None:
92
+ """Put the child in a new Windows job object and return the job, or None.
93
+
94
+ ::
95
+
96
+ POSIX -> None
97
+ Windows, the job is made -> the job handle
98
+ Windows, the job call fails -> None
99
+
100
+ Args:
101
+ child: The child ``started_child`` gave.
102
+ """
103
+ if sys.platform != "win32":
104
+ return None
105
+ try:
106
+ kernel32 = _kernel32()
107
+ job = kernel32.CreateJobObjectW(None, None)
108
+ if job and kernel32.AssignProcessToJobObject(job, int(getattr(child, PROCESS_HANDLE_ATTRIBUTE))):
109
+ return int(job)
110
+ if job:
111
+ kernel32.CloseHandle(job)
112
+ except (OSError, AttributeError, TypeError, ValueError, ctypes.ArgumentError):
113
+ return None
114
+ return None
115
+
116
+
117
+ def close_job(job: int | None) -> bool:
118
+ """Close the job handle, and return whether the close succeeded.
119
+
120
+ ::
121
+
122
+ None -> False, nothing to close
123
+ a job handle -> True once Windows closes it
124
+
125
+ Args:
126
+ job: The job handle ``job_holding`` gave, or None.
127
+ """
128
+ return _job_call_succeeded(job, CLOSE_HANDLE_CALL)
129
+
130
+
131
+ def _job_call_succeeded(job: int | None, call_name: str, *all_arguments: int) -> bool:
132
+ if job is None:
133
+ return False
134
+ try:
135
+ return bool(getattr(_kernel32(), call_name)(job, *all_arguments))
136
+ except (OSError, AttributeError, TypeError, ValueError, ctypes.ArgumentError):
137
+ return False
138
+
139
+
140
+ def _stop_windows_tree(child: subprocess.Popen[str], job: int | None) -> None:
141
+ if not _job_call_succeeded(job, TERMINATE_JOB_CALL, STOPPED_JOB_EXIT_CODE):
142
+ _run_taskkill(child)
143
+ if child.poll() is None:
144
+ child.kill()
145
+ _drain_within(child, KILL_GRACE_SECONDS - WINDOWS_TREE_KILL_SECONDS)
146
+
147
+
148
+ def _run_taskkill(child: subprocess.Popen[str]) -> None:
149
+ try:
150
+ subprocess.run(
151
+ [*ALL_WINDOWS_TREE_KILL_WORDS, str(child.pid)],
152
+ stdin=subprocess.DEVNULL,
153
+ stdout=subprocess.DEVNULL,
154
+ stderr=subprocess.DEVNULL,
155
+ check=False,
156
+ timeout=WINDOWS_TREE_KILL_SECONDS,
157
+ creationflags=hidden_window_creation_flags(),
158
+ )
159
+ except (subprocess.TimeoutExpired, OSError):
160
+ return
161
+
162
+
163
+ def stop_child(child: subprocess.Popen[str], job: int | None) -> None:
164
+ """End the child and every process it started, inside ``KILL_GRACE_SECONDS`` in all.
165
+
166
+ ::
167
+
168
+ POSIX the child leads its own process group -> SIGKILL to the group, then the pipe grace
169
+ Windows the child's job object -> end the job, then the pipe grace
170
+ Windows no job could be made -> bounded hidden taskkill /T /F, then the pipe grace
171
+
172
+ A grandchild that holds the output pipe dies with the child, so the pipe
173
+ closes, even when the child itself already exited. Should one escape, the
174
+ grace still ends the wait.
175
+
176
+ Args:
177
+ child: The child to stop.
178
+ job: The job handle ``job_holding`` gave, or None.
179
+ """
180
+ if sys.platform == "win32":
181
+ _stop_windows_tree(child, job)
182
+ return
183
+ try:
184
+ os.killpg(child.pid, signal.SIGKILL)
185
+ except OSError:
186
+ child.kill()
187
+ _drain_within(child, KILL_GRACE_SECONDS)
@@ -19,54 +19,80 @@ host's timeout. Two deadlines are fixed points from that start::
19
19
 
20
20
  A read gets the smaller of its own timeout and the time left in its
21
21
  slice. A read with no time left is skipped and comes back unanswered. A
22
- child that outlives its allowance is killed, its pipes get a short grace,
23
- and it comes back unanswered. The run notes each read the deadline cut
22
+ child that outlives its allowance is killed with every process it started,
23
+ its pipes get a short grace, and it comes back unanswered. The run notes each read the deadline cut
24
24
  short: a skipped read, and a stopped read whose allowance was below its
25
25
  own timeout. With no deadlines started, as in a library call or a
26
- unit test, each read keeps its own timeout.
26
+ unit test, each read keeps its own timeout. The run also counts every read
27
+ that stopped at a timeout, its own or the shared deadline, which tells a
28
+ read that gave no answer in time apart from one that failed fast.
29
+
30
+ A transcript scan answers to the same deadlines through
31
+ ``transcript_lines_within_slice``, which reads the clock before each line
32
+ and before each file block, so one long line cannot hold the gate. The run
33
+ counts each scan the deadline stopped, apart from the reads it cut short.
27
34
  """
28
35
 
29
36
  from __future__ import annotations
30
37
 
38
+ import functools
39
+ import math
31
40
  import subprocess
32
41
  import sys
33
42
  import threading
34
43
  import time
35
- from collections.abc import Callable, Hashable, Iterator, Sequence
44
+ from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping, Sequence
36
45
  from contextlib import contextmanager
37
46
  from contextvars import ContextVar
38
47
  from dataclasses import dataclass, field
39
48
  from enum import Enum
40
49
  from pathlib import Path
41
- from typing import TypeVar, cast
50
+ from typing import BinaryIO, TypeVar, cast
42
51
 
43
52
  hooks_directory = str(Path(__file__).resolve().parent.parent)
44
53
  if hooks_directory not in sys.path:
45
54
  sys.path.insert(0, hooks_directory)
46
55
 
56
+ from blocking.gate_child_process import close_job, job_holding, started_child, stop_child
47
57
  from hooks_constants.gate_run_reads_constants import (
58
+ EVAL_WRITE_SCAN_SECONDS,
48
59
  FOLLOWUP_READS_SECONDS,
49
- KILL_GRACE_SECONDS,
50
60
  PROOF_AND_LOOK_READS_SECONDS,
51
61
  )
52
- from hooks_constants.subprocess_window import hidden_window_creation_flags
62
+ from hooks_constants.transcript_skill_scan_constants import TRANSCRIPT_BLOCK_BYTES, TRANSCRIPT_LINE_BYTE_CAP
63
+ from transcript_skill_scan import ScanCaps, transcript_lines_newest_first
53
64
 
54
65
  AnswerType = TypeVar("AnswerType")
55
66
 
56
67
 
57
68
  class ReadSlice(Enum):
58
- """Which deadline a read answers to, as seconds from the start of the gate process."""
69
+ """Which deadline a read answers to, as seconds from the start of the gate process.
70
+
71
+ The lifecycle gate reads in ``PROOF_AND_LOOK`` and then ``FOLLOWUP``. The
72
+ eval-write gate scans its transcripts in ``EVAL_WRITE``.
73
+ """
59
74
 
60
75
  PROOF_AND_LOOK = PROOF_AND_LOOK_READS_SECONDS
61
76
  FOLLOWUP = PROOF_AND_LOOK_READS_SECONDS + FOLLOWUP_READS_SECONDS
77
+ EVAL_WRITE = EVAL_WRITE_SCAN_SECONDS
62
78
 
63
79
 
64
80
  @dataclass
65
81
  class GateRun:
66
- """The answers one gate run has read, and whether a deadline cut one of its reads short."""
82
+ """What one gate run read and where its clock stopped it.
83
+
84
+ ::
85
+
86
+ answer_by_question every answer read in the run, unanswered ones included
87
+ was_read_cut_short a deadline skipped a read or stopped one below its own timeout
88
+ timed_out_read_count reads that stopped at a timeout, their own or the shared deadline
89
+ cut_scan_count transcript scans the deadline stopped
90
+ """
67
91
 
68
92
  answer_by_question: dict[Hashable, object] = field(default_factory=dict)
69
93
  was_read_cut_short: bool = False
94
+ timed_out_read_count: int = 0
95
+ cut_scan_count: int = 0
70
96
 
71
97
 
72
98
  @dataclass(frozen=True)
@@ -168,30 +194,75 @@ def _note_read_cut_short() -> None:
168
194
  run.was_read_cut_short = True
169
195
 
170
196
 
171
- def _started_child(all_command_words: Sequence[str], working_directory: str | None) -> subprocess.Popen[str] | None:
172
- try:
173
- return subprocess.Popen(
174
- list(all_command_words),
175
- cwd=working_directory or None,
176
- stdin=subprocess.DEVNULL,
177
- stdout=subprocess.PIPE,
178
- stderr=subprocess.DEVNULL,
179
- text=True,
180
- encoding="utf-8",
181
- errors="replace",
182
- creationflags=hidden_window_creation_flags(),
183
- )
184
- except OSError:
185
- return None
197
+ def _note_read_timed_out() -> None:
198
+ run = open_gate_run.get()
199
+ if run is not None:
200
+ run.timed_out_read_count += 1
186
201
 
187
202
 
188
- def _stop_child(child: subprocess.Popen[str]) -> None:
189
- """Kill the child, then wait a bounded grace, so a grandchild that keeps the output pipe open cannot hold the gate."""
190
- child.kill()
191
- try:
192
- child.communicate(timeout=KILL_GRACE_SECONDS)
193
- except subprocess.TimeoutExpired:
194
- return
203
+ def _note_scan_cut_short() -> None:
204
+ run = open_gate_run.get()
205
+ if run is not None:
206
+ run.cut_scan_count += 1
207
+
208
+
209
+ def lines_within_slice(all_lines: Iterable[str], read_slice: ReadSlice) -> Iterator[str]:
210
+ """Yield each line until the slice's deadline passes, then stop and count the scan as cut.
211
+
212
+ ::
213
+
214
+ no deadlines started -> every line
215
+ the deadline passes after line 3 -> lines 1 to 3; the open run counts one cut scan
216
+ the deadline passed before the scan -> no line; the open run counts one cut scan
217
+
218
+ A scan that reads through this answers to the same clock as the reads, so
219
+ a long transcript cannot hold the gate past its slice.
220
+
221
+ Args:
222
+ all_lines: The lines the scan reads, in the order it reads them.
223
+ read_slice: The slice whose deadline the scan answers to.
224
+ """
225
+ for each_line in all_lines:
226
+ if not _scan_may_continue(read_slice):
227
+ return
228
+ yield each_line
229
+
230
+
231
+ def _scan_may_continue(read_slice: ReadSlice) -> bool:
232
+ if read_allowance(math.inf, read_slice) is not None:
233
+ return True
234
+ _note_scan_cut_short()
235
+ return False
236
+
237
+
238
+ def transcript_lines_within_slice(
239
+ transcript: BinaryIO,
240
+ encoding: str,
241
+ errors: str,
242
+ read_slice: ReadSlice,
243
+ block_size: int = TRANSCRIPT_BLOCK_BYTES,
244
+ line_byte_cap: int = TRANSCRIPT_LINE_BYTE_CAP,
245
+ ) -> Iterator[str]:
246
+ """Yield the transcript's lines newest first until the slice's deadline passes.
247
+
248
+ ::
249
+
250
+ b"one\\r\\ntwo\\n", time left -> "two", "one"
251
+ a 2 GB newest line, deadline in 3 s -> stops between blocks at the deadline; one cut scan
252
+ a line over the cap, time left -> the newer lines, then stops; one cut scan
253
+
254
+ Args:
255
+ transcript: The transcript opened in binary mode.
256
+ encoding: The text encoding of the transcript.
257
+ errors: The decode errors policy, such as ``"replace"``.
258
+ read_slice: The slice whose deadline the scan answers to.
259
+ block_size: The bytes read from the file at a time.
260
+ line_byte_cap: The most bytes one line may hold.
261
+ """
262
+ keeps_reading = functools.partial(_scan_may_continue, read_slice)
263
+ caps = ScanCaps(line_byte_cap, on_cap_passed=_note_scan_cut_short)
264
+ all_lines = transcript_lines_newest_first(transcript, encoding, errors, block_size, keeps_reading, caps)
265
+ return lines_within_slice(all_lines, read_slice)
195
266
 
196
267
 
197
268
  def child_stdout(
@@ -199,6 +270,7 @@ def child_stdout(
199
270
  own_timeout_seconds: float,
200
271
  read_slice: ReadSlice,
201
272
  working_directory: str | None = None,
273
+ all_environment_variables: Mapping[str, str] | None = None,
202
274
  ) -> str | None:
203
275
  """Run one read-only child and return its output, or None when it gives no answer.
204
276
 
@@ -214,22 +286,30 @@ def child_stdout(
214
286
  own_timeout_seconds: The timeout the read carries on its own.
215
287
  read_slice: The slice whose deadline the read answers to.
216
288
  working_directory: The directory the child runs in, or None for the gate's own.
289
+ all_environment_variables: The child's whole environment, or None for the gate's own.
217
290
  """
218
291
  allowance = read_allowance(own_timeout_seconds, read_slice)
219
292
  if allowance is None:
220
293
  _note_read_cut_short()
221
294
  return None
222
- child = _started_child(all_command_words, working_directory)
295
+ child = started_child(all_command_words, working_directory, all_environment_variables)
223
296
  if child is None:
224
297
  return None
225
- return _answer_within(child, allowance, own_timeout_seconds)
298
+ job = job_holding(child)
299
+ try:
300
+ return _answer_within(child, allowance, own_timeout_seconds, job)
301
+ finally:
302
+ close_job(job)
226
303
 
227
304
 
228
- def _answer_within(child: subprocess.Popen[str], allowance: float, own_timeout_seconds: float) -> str | None:
305
+ def _answer_within(
306
+ child: subprocess.Popen[str], allowance: float, own_timeout_seconds: float, job: int | None
307
+ ) -> str | None:
229
308
  try:
230
309
  printed_text = child.communicate(timeout=allowance)[0]
231
310
  except subprocess.TimeoutExpired:
232
- _stop_child(child)
311
+ stop_child(child, job)
312
+ _note_read_timed_out()
233
313
  if allowance < own_timeout_seconds:
234
314
  _note_read_cut_short()
235
315
  return None
@@ -281,6 +361,7 @@ def answer_within_allowance(
281
361
  raise all_errors[0]
282
362
  if all_answers:
283
363
  return all_answers[0]
364
+ _note_read_timed_out()
284
365
  if allowance < own_timeout_seconds:
285
366
  _note_read_cut_short()
286
367
  return None
@@ -7,6 +7,8 @@ link to the posted rulings. The open and the merge then name that exact head::
7
7
 
8
8
  open, open receipt says 0 findings at abc123, branch head abc123 -> allow
9
9
  open, no open receipt for the branch head -> deny, review first
10
+ two chained opens, the second head has no clean open receipt -> deny, review it first
11
+ open whose head the reads could not answer in time -> deny, head unknown, rerun may pass
10
12
  merge, receipt says 0 findings at abc123, merge pinned to abc123 -> allow
11
13
  merge, open receipt clean at abc123, merge pinned to abc123 -> allow
12
14
  merge, receipt rules out all 3 findings with a link, pinned -> allow
@@ -17,6 +19,7 @@ link to the posted rulings. The open and the merge then name that exact head::
17
19
  auto-merge that names no head -> deny, pin it
18
20
  gh pr merge --disable-auto, which only turns auto-merge off -> allow
19
21
  repository listed in CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES -> allow
22
+ open or merge whose check raises -> deny, naming the error
20
23
 
21
24
  The deny reason names the review command CLAUDE_MERGE_REVIEW_COMMAND sets,
22
25
  or ``/code-review high --max-findings all`` when it is unset.
@@ -37,7 +40,13 @@ hooks_directory = str(Path(__file__).resolve().parent.parent)
37
40
  if hooks_directory not in sys.path:
38
41
  sys.path.insert(0, hooks_directory)
39
42
 
40
- from blocking.merge_review_gate_parts.open_request import OpenRequest, open_request_from
43
+ from blocking.gate_run_reads import gate_run, read_deadlines_started
44
+ from blocking.merge_review_gate_parts.open_request import (
45
+ OpenCall,
46
+ OpenRequest,
47
+ open_calls_from,
48
+ open_request_for,
49
+ )
41
50
  from blocking.merge_review_gate_parts.receipts import (
42
51
  has_clean_open_review,
43
52
  has_zero_finding_open_review,
@@ -45,6 +54,8 @@ from blocking.merge_review_gate_parts.receipts import (
45
54
  receipt_path,
46
55
  same_commit,
47
56
  )
57
+ from hooks_constants.gate_fault import gate_fault_reason
58
+ from hooks_constants.gate_run_reads_constants import READS_RAN_OUT_SENTENCE
48
59
  from hooks_constants.hook_specific_output_keys import (
49
60
  HOOK_SPECIFIC_OUTPUT_KEY,
50
61
  PERMISSION_DECISION_KEY,
@@ -58,18 +69,23 @@ from hooks_constants.merge_review_gate_constants import (
58
69
  AUTO_OPTION,
59
70
  API_SHA_FIELD_PATTERN,
60
71
  AUTO_OPTION_PATTERN,
72
+ CREATE_TOOL_SUFFIX,
61
73
  DEFAULT_REVIEW_COMMAND,
62
74
  DENY_DECISION,
63
75
  DISABLE_AUTO_OPTION,
64
76
  END_OF_OPTIONS_WORD,
65
77
  EXEMPT_REPOSITORIES_ENVIRONMENT_VARIABLE,
66
78
  EXPECTED_HEAD_FIELD,
79
+ GATE_NAME,
67
80
  GH_API_MERGE_PATTERN,
68
81
  GH_PR_MERGE_CALL_PATTERN,
69
82
  GH_PR_MERGE_PATTERN,
70
83
  ALL_GH_PR_MERGE_SUBCOMMAND_WORDS,
71
84
  GH_PROGRAM_WORD_PATTERN,
72
85
  HOOK_EVENT_NAME,
86
+ LOOSE_GH_PROGRAM_PATTERN,
87
+ LOOSE_OPEN_OR_MERGE_WORD_PATTERN,
88
+ LOOSE_PULL_REQUEST_SCRIPT_PATTERN,
73
89
  MATCH_HEAD_PATTERN,
74
90
  MERGE_TOOL_SUFFIX,
75
91
  NO_CLEAN_OPEN_REVIEW_REASON_TEMPLATE,
@@ -254,7 +270,11 @@ def _merge_arguments(all_words: list[str]) -> list[str] | None:
254
270
  for each_index in range(len(all_words) - len(ALL_GH_PR_MERGE_SUBCOMMAND_WORDS)):
255
271
  is_gh_program = GH_PROGRAM_WORD_PATTERN.search(all_words[each_index]) is not None
256
272
  subcommand_end = each_index + 1 + len(ALL_GH_PR_MERGE_SUBCOMMAND_WORDS)
257
- if is_gh_program and tuple(all_words[each_index + 1 : subcommand_end]) == ALL_GH_PR_MERGE_SUBCOMMAND_WORDS:
273
+ if (
274
+ is_gh_program
275
+ and tuple(all_words[each_index + 1 : subcommand_end])
276
+ == ALL_GH_PR_MERGE_SUBCOMMAND_WORDS
277
+ ):
258
278
  return all_words[subcommand_end:]
259
279
  return None
260
280
 
@@ -394,19 +414,73 @@ def deny_reason(request: MergeRequest) -> str | None:
394
414
  def decision_for(all_payload_fields: dict[str, object]) -> dict[str, object] | None:
395
415
  """Return a deny decision for an open or merge with no clean review, else None.
396
416
 
417
+ The gate first reads from the payload alone whether the call opens or
418
+ merges a pull request. Only then does it read git, GitHub, and receipts::
419
+
420
+ call opens and merges nothing -> None, no read runs
421
+ two chained creates, second head unreviewed -> deny naming the second head
422
+ an open whose head read ran out of time -> deny, head unknown, + READS_RAN_OUT_SENTENCE
423
+ an open or merge whose check raises KeyError -> deny naming KeyError
424
+ a create or merge whose payload read raises -> deny naming the error
425
+
426
+ A payload read that raises for any other call goes on to the shared helper.
427
+
397
428
  Args:
398
429
  all_payload_fields: The parsed PreToolUse input.
399
430
 
400
431
  Returns:
401
432
  The hook output that denies the call, or None to allow it.
402
433
  """
403
- open_request = open_request_from(all_payload_fields)
404
- merge_request = merge_request_from(all_payload_fields)
405
- reason = (open_deny_reason(open_request) if open_request is not None else None) or (
406
- deny_reason(merge_request) if merge_request is not None else None
407
- )
408
- if reason is None:
434
+ all_open_calls, merge_request, fault_reason = _payload_reading(all_payload_fields)
435
+ if fault_reason is not None:
436
+ return _deny_decision(fault_reason)
437
+ if not all_open_calls and merge_request is None:
409
438
  return None
439
+ try:
440
+ reason = _checked_reason(all_open_calls, merge_request)
441
+ except Exception as error:
442
+ reason = gate_fault_reason(GATE_NAME, error)
443
+ return None if reason is None else _deny_decision(reason)
444
+
445
+
446
+ def _payload_reading(
447
+ all_payload_fields: dict[str, object],
448
+ ) -> tuple[list[OpenCall], MergeRequest | None, str | None]:
449
+ try:
450
+ return open_calls_from(all_payload_fields), merge_request_from(all_payload_fields), None
451
+ except Exception as error:
452
+ if not _is_open_or_merge_text(all_payload_fields):
453
+ raise
454
+ return [], None, gate_fault_reason(GATE_NAME, error)
455
+
456
+
457
+ def _is_open_or_merge_text(all_payload_fields: dict[str, object]) -> bool:
458
+ tool_name = all_payload_fields.get("tool_name")
459
+ tool_input = all_payload_fields.get("tool_input")
460
+ if not isinstance(tool_name, str):
461
+ return False
462
+ if tool_name.endswith((CREATE_TOOL_SUFFIX, MERGE_TOOL_SUFFIX, AUTO_MERGE_TOOL_SUFFIX)):
463
+ return True
464
+ command = tool_input.get("command") if isinstance(tool_input, dict) else None
465
+ if tool_name not in ALL_SHELL_TOOL_NAMES or not isinstance(command, str):
466
+ return False
467
+ is_gh_action = bool(LOOSE_GH_PROGRAM_PATTERN.search(command) and LOOSE_OPEN_OR_MERGE_WORD_PATTERN.search(command))
468
+ return is_gh_action or LOOSE_PULL_REQUEST_SCRIPT_PATTERN.search(command) is not None
469
+
470
+
471
+ def _checked_reason(
472
+ all_open_calls: list[OpenCall], merge_request: MergeRequest | None
473
+ ) -> str | None:
474
+ with gate_run() as run:
475
+ reason = _first_open_deny_reason(all_open_calls) or (
476
+ deny_reason(merge_request) if merge_request is not None else None
477
+ )
478
+ if reason is not None and (run.was_read_cut_short or run.timed_out_read_count > 0):
479
+ return reason + READS_RAN_OUT_SENTENCE
480
+ return reason
481
+
482
+
483
+ def _deny_decision(reason: str) -> dict[str, object]:
410
484
  return {
411
485
  HOOK_SPECIFIC_OUTPUT_KEY: {
412
486
  "hookEventName": HOOK_EVENT_NAME,
@@ -416,9 +490,18 @@ def decision_for(all_payload_fields: dict[str, object]) -> dict[str, object] | N
416
490
  }
417
491
 
418
492
 
493
+ def _first_open_deny_reason(all_open_calls: list[OpenCall]) -> str | None:
494
+ for each_call in all_open_calls:
495
+ reason = open_deny_reason(open_request_for(each_call))
496
+ if reason is not None:
497
+ return reason
498
+ return None
499
+
500
+
419
501
  def main() -> int:
420
- """Read one payload, print a deny when needed, and exit zero."""
421
- return print_decision_from_stdin(decision_for)
502
+ """Start the read deadlines, read one payload, print a deny when needed, and exit zero."""
503
+ with read_deadlines_started():
504
+ return print_decision_from_stdin(decision_for)
422
505
 
423
506
 
424
507
  if __name__ == "__main__":