claude-dev-env 8.119.1 → 8.120.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/hooks/blocking/followup_pr_dedupe.py +71 -13
  2. package/hooks/blocking/gh_api_budget_gate.py +286 -0
  3. package/hooks/blocking/merge_review_gate.py +4 -0
  4. package/hooks/blocking/merge_review_gate_parts/create_folders.py +6 -2
  5. package/hooks/blocking/merge_review_gate_parts/open_request.py +40 -15
  6. package/hooks/blocking/resolve_thread_reply_gate.py +116 -1
  7. package/hooks/blocking/test_bash_dispatcher_interpreter_starts.py +8 -3
  8. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +1 -0
  9. package/hooks/blocking/test_cloud_graphql_gate.py +4 -0
  10. package/hooks/blocking/test_followup_pr_dedupe.py +99 -0
  11. package/hooks/blocking/test_gh_api_budget_gate.py +602 -0
  12. package/hooks/blocking/test_gh_global_account_switch_gate.py +4 -0
  13. package/hooks/blocking/test_headless_claude_broker_gate.py +4 -0
  14. package/hooks/blocking/test_merge_review_gate.py +133 -0
  15. package/hooks/blocking/test_python_windows_invocation_gate.py +4 -0
  16. package/hooks/blocking/test_resolve_thread_reply_gate.py +430 -0
  17. package/hooks/blocking/tests/test_create_folders.py +7 -0
  18. package/hooks/blocking/tests/test_open_request.py +21 -0
  19. package/hooks/features/bash-dispatchers.md +3 -0
  20. package/hooks/features/conduct-gates.md +3 -3
  21. package/hooks/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +4 -0
  22. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +5 -0
  23. package/hooks/hooks_constants/gh_api_budget.py +186 -0
  24. package/hooks/hooks_constants/gh_api_budget_gate_constants.py +215 -0
  25. package/hooks/hooks_constants/gh_argument_lists.py +169 -0
  26. package/hooks/hooks_constants/gh_shell_loop_scan.py +386 -0
  27. package/hooks/hooks_constants/merge_review_gate_constants.py +5 -0
  28. package/hooks/hooks_constants/pytest_invocation.py +24 -9
  29. package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +21 -1
  30. package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +5 -1
  31. package/hooks/hooks_constants/test_gh_api_budget.py +125 -0
  32. package/hooks/hooks_constants/test_gh_argument_lists.py +49 -0
  33. package/hooks/hooks_constants/test_gh_shell_loop_scan.py +50 -0
  34. package/hooks/hooks_constants/test_pytest_invocation.py +25 -0
  35. package/hooks/hooks_constants/test_shell_command_mutation.py +11 -0
  36. package/hooks/hooks_constants/transcript_skill_scan_constants.py +22 -0
  37. package/hooks/session/test_npx_pele_guidance.py +4 -0
  38. package/hooks/test_transcript_skill_scan.py +72 -0
  39. package/hooks/transcript_skill_scan.py +138 -4
  40. package/hooks/validators/config/security_checks_constants.py +88 -0
  41. package/hooks/validators/security_checks.py +186 -1
  42. package/hooks/validators/test_security_checks.py +149 -0
  43. package/package.json +1 -1
@@ -0,0 +1,169 @@
1
+ """Find the gh calls a shell command runs, past wrappers, glued braces, assignments and ``watch``.
2
+
3
+ ::
4
+
5
+ gh pr view 12 [["pr", "view", "12"]]
6
+ $s = gh run watch 1 [["run", "watch", "1"]]
7
+ {gh run view 1} [["run", "view", "1"]]
8
+ watch -n 5 gh run view 1 [["run", "view", "1"]]
9
+ echo "gh run watch" []
10
+
11
+ ``blocking/gh_api_budget_gate.py`` and ``gh_shell_loop_scan`` read gh calls
12
+ through this module.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from collections import deque
18
+
19
+ from hooks_constants.gh_api_budget_gate_constants import (
20
+ ALL_POWERSHELL_ASSIGNMENT_OPERATORS,
21
+ ALL_QUOTE_CHARACTERS,
22
+ ALL_WATCH_INTERVAL_OPTIONS,
23
+ ALL_WATCH_OPTIONS_WITH_A_VALUE,
24
+ BLOCK_END_CHARACTER,
25
+ BLOCK_START_CHARACTER,
26
+ GH_PROGRAM_NAME,
27
+ OPTION_PREFIX,
28
+ POWERSHELL_ASSIGNMENT_PREFIX_LENGTH,
29
+ POWERSHELL_VARIABLE_PATTERN,
30
+ WATCH_DEFAULT_INTERVAL_SECONDS,
31
+ WATCH_LONG_INTERVAL_OPTION_PREFIX,
32
+ WATCH_PROGRAM_NAME,
33
+ WATCH_SHORT_INTERVAL_OPTION,
34
+ )
35
+ from hooks_constants.piped_pytest_blocker_constants import WRAPPED_COMMAND_TOKEN_JOIN
36
+ from hooks_constants.pytest_invocation import unquoted_token
37
+ from hooks_constants.shell_command_wrappers import (
38
+ all_wrapped_command_segments,
39
+ segment_program_and_arguments,
40
+ )
41
+
42
+
43
+ def _watch_option_interval(option: str, all_remaining_arguments: deque[str]) -> str | None:
44
+ """Return the interval text one ``watch`` option sets, taking its separate value when it has one.
45
+
46
+ Returns None when the option sets no interval.
47
+ """
48
+ if option in ALL_WATCH_INTERVAL_OPTIONS or option in ALL_WATCH_OPTIONS_WITH_A_VALUE:
49
+ option_value = all_remaining_arguments.popleft() if all_remaining_arguments else ""
50
+ return option_value if option in ALL_WATCH_INTERVAL_OPTIONS else None
51
+ if option.startswith(WATCH_LONG_INTERVAL_OPTION_PREFIX):
52
+ return option.removeprefix(WATCH_LONG_INTERVAL_OPTION_PREFIX)
53
+ if option.startswith(WATCH_SHORT_INTERVAL_OPTION):
54
+ return option.removeprefix(WATCH_SHORT_INTERVAL_OPTION)
55
+ return None
56
+
57
+
58
+ def watch_interval_and_command(all_arguments: list[str]) -> tuple[float | None, str]:
59
+ """Return a ``watch`` call's interval in seconds, None when unreadable, and the command it runs."""
60
+ interval_text: str | None = None
61
+ all_remaining_arguments = deque(unquoted_token(each) for each in all_arguments)
62
+ while all_remaining_arguments and all_remaining_arguments[0].startswith(OPTION_PREFIX):
63
+ option = all_remaining_arguments.popleft()
64
+ option_interval = _watch_option_interval(option, all_remaining_arguments)
65
+ interval_text = interval_text if option_interval is None else option_interval
66
+ watched_command = WRAPPED_COMMAND_TOKEN_JOIN.join(all_remaining_arguments)
67
+ if interval_text is None:
68
+ return WATCH_DEFAULT_INTERVAL_SECONDS, watched_command
69
+ try:
70
+ return float(interval_text), watched_command
71
+ except ValueError:
72
+ return None, watched_command
73
+
74
+
75
+ def _brace_split_tokens(token: str) -> list[str]:
76
+ """Return one token with its glued PowerShell braces split off.
77
+
78
+ ::
79
+
80
+ {gh ["{", "gh"]
81
+ 5} ["5", "}"]
82
+ {} ["{", "}"]
83
+ ${name} ["${name}"]
84
+
85
+ Quoted text, which holds a quote or a space, stays whole.
86
+ """
87
+ is_quoted_text = token.startswith(tuple(ALL_QUOTE_CHARACTERS)) or any(
88
+ each_character.isspace() for each_character in token
89
+ )
90
+ if is_quoted_text:
91
+ return [token]
92
+ body = token.lstrip(BLOCK_START_CHARACTER)
93
+ opening_count = len(token) - len(body)
94
+ closing_count = 0
95
+ while body.endswith(BLOCK_END_CHARACTER) and body.count(BLOCK_END_CHARACTER) > body.count(
96
+ BLOCK_START_CHARACTER
97
+ ):
98
+ body = body.removesuffix(BLOCK_END_CHARACTER)
99
+ closing_count += 1
100
+ return [
101
+ *[BLOCK_START_CHARACTER] * opening_count,
102
+ *([body] if body else []),
103
+ *[BLOCK_END_CHARACTER] * closing_count,
104
+ ]
105
+
106
+
107
+ def brace_split_segments(all_segment_tokens: list[str]) -> list[list[str]]:
108
+ """Return one segment split at each glued ``{``, so ``{gh run view 1}`` runs gh.
109
+
110
+ Each new segment starts with its ``{``, the way a ``{`` the pipeline
111
+ parser splits at opens the next segment in ``gh_shell_loop_scan``.
112
+ """
113
+ all_parts = [
114
+ each_part
115
+ for each_token in all_segment_tokens
116
+ for each_part in _brace_split_tokens(each_token)
117
+ ]
118
+ all_segments: list[list[str]] = [[]]
119
+ for each_part in all_parts:
120
+ if each_part == BLOCK_START_CHARACTER:
121
+ all_segments.append([])
122
+ all_segments[-1].append(each_part)
123
+ return [each_segment for each_segment in all_segments if each_segment]
124
+
125
+
126
+ def all_gh_call_argument_lists(command: str, is_inside_watch: bool = False) -> list[list[str]]:
127
+ """Return the arguments of each gh call the command runs, past wrappers, braces and ``watch``."""
128
+ return [
129
+ each_gh_arguments
130
+ for each_wrapped_segment in all_wrapped_command_segments(command)
131
+ for each_segment in brace_split_segments(each_wrapped_segment)
132
+ for each_gh_arguments in _segment_gh_argument_lists(each_segment, is_inside_watch)
133
+ ]
134
+
135
+
136
+ def _tokens_past_assignment(all_segment_tokens: list[str]) -> list[str]:
137
+ """Return a segment's tokens past a leading PowerShell ``$name =`` prefix.
138
+
139
+ ::
140
+
141
+ $s = gh run watch 1 ["gh", "run", "watch", "1"]
142
+ $script:s += gh pr view 1 ["gh", "pr", "view", "1"]
143
+ gh run view 1 ["gh", "run", "view", "1"]
144
+ """
145
+ assignment_prefix = all_segment_tokens[:POWERSHELL_ASSIGNMENT_PREFIX_LENGTH]
146
+ is_assignment = (
147
+ len(assignment_prefix) == POWERSHELL_ASSIGNMENT_PREFIX_LENGTH
148
+ and POWERSHELL_VARIABLE_PATTERN.fullmatch(assignment_prefix[0]) is not None
149
+ and assignment_prefix[1] in ALL_POWERSHELL_ASSIGNMENT_OPERATORS
150
+ )
151
+ if is_assignment:
152
+ return all_segment_tokens[POWERSHELL_ASSIGNMENT_PREFIX_LENGTH:]
153
+ return all_segment_tokens
154
+
155
+
156
+ def _segment_gh_argument_lists(
157
+ all_segment_tokens: list[str], is_inside_watch: bool
158
+ ) -> list[list[str]]:
159
+ """Return the arguments of the gh call one segment runs, directly, as an assigned value, or through ``watch``."""
160
+ program_name, all_arguments = segment_program_and_arguments(
161
+ _tokens_past_assignment(all_segment_tokens)
162
+ )
163
+ normalized_program_name = program_name.lower()
164
+ if normalized_program_name == GH_PROGRAM_NAME:
165
+ return [all_arguments]
166
+ if normalized_program_name != WATCH_PROGRAM_NAME or is_inside_watch:
167
+ return []
168
+ _interval, watched_command = watch_interval_and_command(all_arguments)
169
+ return all_gh_call_argument_lists(watched_command, True)
@@ -0,0 +1,386 @@
1
+ """Find the shell loops that call gh, and read how long each pass sleeps.
2
+
3
+ ::
4
+
5
+ while :; do gh run view 1; sleep 30; done one loop, 30 seconds of sleep
6
+ while ($true) { gh run view 1 } one endless loop, no sleep
7
+ watch -n 5 gh run view 1 a watch every 5 seconds
8
+
9
+ ``blocking/gh_api_budget_gate.py`` refuses a loop or ``watch`` this module
10
+ reads as tight.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import shlex
16
+ from itertools import pairwise
17
+
18
+ from hooks_constants.gh_api_budget_gate_constants import (
19
+ ALL_BASH_LOOP_HEADER_KEYWORDS,
20
+ ALL_CONDITION_LOOP_KEYWORDS,
21
+ ALL_ENDLESS_CONDITIONS_BY_LOOP_KEYWORD,
22
+ ALL_LEADING_SHELL_KEYWORDS,
23
+ ALL_LOOP_KEYWORDS,
24
+ ALL_SLEEP_PROGRAM_NAMES,
25
+ BLOCK_CLOSER_CHARACTERS,
26
+ BLOCK_END_CHARACTER,
27
+ BLOCK_START_CHARACTER,
28
+ COUNTED_LOOP_KEYWORD,
29
+ ENDLESS_FOR_HEADER_MARKER,
30
+ LOOP_BODY_KEYWORD,
31
+ LOOP_END_KEYWORD,
32
+ LOOP_HEADER_CHARACTERS,
33
+ LOOP_HEADER_OPENER,
34
+ ALL_SECONDS_BY_TIMESPAN_FIELD,
35
+ ALL_SHELL_EXPANSION_MARKERS,
36
+ MILLISECONDS_PER_SECOND,
37
+ MINIMUM_POLL_INTERVAL_SECONDS,
38
+ MINIMUM_START_SLEEP_DURATION_PARAMETER_LENGTH,
39
+ MINIMUM_START_SLEEP_PARAMETER_LENGTH,
40
+ OPTION_PREFIX,
41
+ SECONDS_BY_SLEEP_UNIT,
42
+ SLEEP_DURATION_AMOUNT_GROUP,
43
+ SLEEP_DURATION_PATTERN,
44
+ SLEEP_DURATION_UNIT_GROUP,
45
+ SLEEP_PARAMETER_VALUE_SEPARATOR,
46
+ SLEEP_TIMESPAN_FIELD_SEPARATOR,
47
+ SLEEP_TIMESPAN_PATTERN,
48
+ START_SLEEP_DURATION_PARAMETER,
49
+ START_SLEEP_MILLISECONDS_PARAMETER,
50
+ START_SLEEP_SECONDS_PARAMETER,
51
+ WATCH_PROGRAM_NAME,
52
+ )
53
+ from hooks_constants.gh_argument_lists import (
54
+ all_gh_call_argument_lists,
55
+ brace_split_segments,
56
+ watch_interval_and_command,
57
+ )
58
+ from hooks_constants.piped_pytest_blocker_constants import WRAPPED_COMMAND_TOKEN_JOIN
59
+ from hooks_constants.pytest_invocation import unquoted_token
60
+ from hooks_constants.shell_command_pipeline import (
61
+ all_operator_aware_tokenizations,
62
+ join_line_continuations,
63
+ scannable_command_lines,
64
+ segments_with_following_operator,
65
+ )
66
+ from hooks_constants.shell_command_wrappers import segment_program_and_arguments
67
+
68
+
69
+ def _ordered_segments(command_text: str) -> list[list[str]]:
70
+ """Return the command's segments in run order from one tokenization, repeats kept.
71
+
72
+ The pipeline parser drops repeated segments and merges two tokenizations,
73
+ so two ``sleep 30`` segments would read as one there. A segment that
74
+ follows a ``{`` starts with that ``{``, so the loop reader can count
75
+ PowerShell blocks.
76
+ """
77
+ all_segments: list[list[str]] = []
78
+ for each_line in scannable_command_lines(join_line_continuations(command_text)):
79
+ all_tokenizations = all_operator_aware_tokenizations(each_line) if each_line.strip() else []
80
+ for each_tokenization in all_tokenizations[:1]:
81
+ all_segments.extend(_block_opened_segments(each_tokenization))
82
+ return all_segments
83
+
84
+
85
+ def _block_opened_segments(all_tokens: list[str]) -> list[list[str]]:
86
+ """Return one tokenization's segments, each segment after a ``{`` starting with that ``{``."""
87
+ all_segments: list[list[str]] = []
88
+ all_block_openers: list[str] = []
89
+ for each_segment, each_operator in segments_with_following_operator(all_tokens):
90
+ all_segments.extend(brace_split_segments([*all_block_openers, *each_segment]))
91
+ is_block_opener = each_operator == BLOCK_START_CHARACTER
92
+ all_block_openers = [BLOCK_START_CHARACTER] if is_block_opener else []
93
+ return all_segments
94
+
95
+
96
+ def _keyword_split_tokens(all_tokens: list[str]) -> list[str]:
97
+ """Return the tokens with a glued loop header such as ``while($true)`` split after its keyword."""
98
+ all_split_tokens: list[str] = []
99
+ for each_token in all_tokens:
100
+ keyword, opener, header = each_token.partition(LOOP_HEADER_OPENER)
101
+ if opener and keyword.lower() in ALL_LOOP_KEYWORDS:
102
+ all_split_tokens.extend([keyword, opener + header])
103
+ continue
104
+ all_split_tokens.append(each_token)
105
+ return all_split_tokens
106
+
107
+
108
+ def _leading_keywords(all_segment_tokens: list[str]) -> list[str]:
109
+ """Return the shell and loop keywords that open a segment, lowercased."""
110
+ all_keywords: list[str] = []
111
+ for each_token in _keyword_split_tokens(all_segment_tokens):
112
+ lowered_token = each_token.lower()
113
+ if (
114
+ lowered_token not in ALL_LOOP_KEYWORDS
115
+ and lowered_token not in ALL_LEADING_SHELL_KEYWORDS
116
+ ):
117
+ break
118
+ all_keywords.append(lowered_token)
119
+ return all_keywords
120
+
121
+
122
+ def _is_endless_loop(all_segments: list[list[str]]) -> bool:
123
+ """Return True when a loop header reads ``while :``, ``while ($true)``, ``until false`` or ``for (;;)``."""
124
+ all_tokens = _keyword_split_tokens(
125
+ [each for each_segment in all_segments for each in each_segment]
126
+ )
127
+ for each_token, each_next_token in pairwise(all_tokens):
128
+ keyword = each_token.lower()
129
+ all_endless_conditions = ALL_ENDLESS_CONDITIONS_BY_LOOP_KEYWORD.get(keyword, frozenset())
130
+ if each_next_token.strip(LOOP_HEADER_CHARACTERS).lower() in all_endless_conditions:
131
+ return True
132
+ is_endless_for_header = (
133
+ ENDLESS_FOR_HEADER_MARKER in each_next_token
134
+ and not each_next_token.strip(LOOP_HEADER_CHARACTERS)
135
+ )
136
+ if keyword == COUNTED_LOOP_KEYWORD and is_endless_for_header:
137
+ return True
138
+ return False
139
+
140
+
141
+ def _has_gh_loop_condition(all_segments: list[list[str]]) -> bool:
142
+ """Return True when a ``while`` or ``until`` condition calls gh."""
143
+ for each_segment in all_segments:
144
+ all_keywords = _leading_keywords(each_segment)
145
+ if not set(all_keywords) & ALL_CONDITION_LOOP_KEYWORDS:
146
+ continue
147
+ if _segment_calls_gh(_keyword_split_tokens(each_segment)[len(all_keywords) :]):
148
+ return True
149
+ return False
150
+
151
+
152
+ def _segment_calls_gh(all_segment_tokens: list[str]) -> bool:
153
+ """Return True when one segment runs gh, directly, through a wrapper, or in a ``$(...)``.
154
+
155
+ The tokens are joined back into command text twice: shell-quoted, so the
156
+ script of ``bash -c "gh run view 1"`` stays one argument, and plain, so a
157
+ ``$(gh run view 1)`` token is read as a substitution.
158
+ """
159
+ all_command_texts = (
160
+ shlex.join(all_segment_tokens),
161
+ WRAPPED_COMMAND_TOKEN_JOIN.join(all_segment_tokens),
162
+ )
163
+ return any(all_gh_call_argument_lists(each_text) for each_text in all_command_texts)
164
+
165
+
166
+ def _nesting_depth_steps(
167
+ all_segment_tokens: list[str], all_previous_segment_tokens: list[str]
168
+ ) -> list[int]:
169
+ """Return the block depth step each token of one segment takes, in token order.
170
+
171
+ ::
172
+
173
+ { gh run view 1 } [1, -1]
174
+ do [1] right after a Bash loop header
175
+ Start-Sleep 5 } [-1]
176
+
177
+ A ``{`` or a Bash ``do`` opens a block, and a ``}`` or a ``done`` closes
178
+ one. A ``do`` is a Bash body only when it starts the segment right after
179
+ a ``while``, ``until`` or ``for`` header. Any other ``do`` opens a
180
+ PowerShell ``do { } while`` loop, so only its ``{`` counts.
181
+ """
182
+ follows_bash_header = bool(
183
+ set(_leading_keywords(all_previous_segment_tokens)) & ALL_BASH_LOOP_HEADER_KEYWORDS
184
+ )
185
+ all_steps = [
186
+ _token_depth_step(
187
+ each_token.lower(), each_index == 0 and follows_bash_header, each_index == 0
188
+ )
189
+ for each_index, each_token in enumerate(all_segment_tokens)
190
+ ]
191
+ return [each_step for each_step in all_steps if each_step]
192
+
193
+
194
+ def _token_depth_step(
195
+ lowered_token: str, can_open_bash_body: bool, can_close_bash_body: bool
196
+ ) -> int:
197
+ """Return 1 for a token that opens a block, -1 for one that closes a block, else 0."""
198
+ if lowered_token == BLOCK_START_CHARACTER:
199
+ return 1
200
+ if lowered_token == BLOCK_END_CHARACTER:
201
+ return -1
202
+ if can_open_bash_body and lowered_token == LOOP_BODY_KEYWORD:
203
+ return 1
204
+ if can_close_bash_body and lowered_token == LOOP_END_KEYWORD:
205
+ return -1
206
+ return 0
207
+
208
+
209
+ def _loop_spans(all_segments: list[list[str]]) -> list[list[list[str]]]:
210
+ """Return the segments of each outermost loop, from its keyword through the block end that closes it.
211
+
212
+ ::
213
+
214
+ for i in 1 2; do gh pr view $i; done; while :; do sleep 5; done
215
+ -> [for ... done], [while ... done]
216
+ while :; do for i in 1 2; do gh run view $i; done; sleep 5; done
217
+ -> one span, since the inner loop sits in the outer one
218
+
219
+ A PowerShell ``do { } while (...)`` keeps the condition segment after
220
+ its ``}``, and a loop that never closes runs to the last segment. Depth
221
+ moves token by token, so a body that opens and closes inside one
222
+ segment, as in ``while ($x) { gh run view 1 }``, still closes its span.
223
+ """
224
+ all_spans: list[list[list[str]]] = []
225
+ depth = 0
226
+ span_start_index: int | None = None
227
+ span_start_depth = 0
228
+ has_span_body_opened = False
229
+ for each_index, each_segment in enumerate(all_segments):
230
+ is_loop_header = bool(set(_leading_keywords(each_segment)) & ALL_LOOP_KEYWORDS)
231
+ if span_start_index is None and is_loop_header:
232
+ span_start_index, span_start_depth, has_span_body_opened = each_index, depth, False
233
+ previous_segment = all_segments[each_index - 1] if each_index else []
234
+ deepest_depth = depth
235
+ for each_step in _nesting_depth_steps(each_segment, previous_segment):
236
+ depth = max(0, depth + each_step)
237
+ deepest_depth = max(deepest_depth, depth)
238
+ if span_start_index is None:
239
+ continue
240
+ has_span_body_opened = has_span_body_opened or deepest_depth > span_start_depth
241
+ if not has_span_body_opened or depth > span_start_depth:
242
+ continue
243
+ has_trailing_condition = bool(each_segment) and (
244
+ each_segment[-1].lower() in ALL_CONDITION_LOOP_KEYWORDS
245
+ )
246
+ span_end_index = each_index + int(has_trailing_condition)
247
+ all_spans.append(all_segments[span_start_index : span_end_index + 1])
248
+ span_start_index = None
249
+ if span_start_index is not None:
250
+ all_spans.append(all_segments[span_start_index:])
251
+ return all_spans
252
+
253
+
254
+ def _sleep_seconds(all_arguments: list[str]) -> float | None:
255
+ """Return the seconds one ``sleep`` or ``Start-Sleep`` call waits, or None when unreadable.
256
+
257
+ ::
258
+
259
+ sleep 30 / sleep 1m / sleep .5 30.0 / 60.0 / 0.5
260
+ Start-Sleep -Seconds 30 -Verbose 30.0
261
+ Start-Sleep -Milliseconds 500 0.5
262
+ Start-Sleep -Duration 00:01:30 90.0
263
+ sleep 5 2>/dev/null 5.0
264
+ sleep $delay None
265
+
266
+ A parameter that names no time unit, such as ``-Verbose`` or
267
+ ``-ErrorAction Stop``, is skipped together with its value. Any other
268
+ word that is not a duration, such as a redirect, is skipped too. Only a
269
+ shell expansion, a ``$`` or a backtick, makes the sleep unreadable.
270
+ """
271
+ total_seconds = 0.0
272
+ seconds_per_unit: float | None = 1.0
273
+ for each_argument in all_arguments:
274
+ argument = unquoted_token(each_argument).lower()
275
+ if not argument.strip(BLOCK_CLOSER_CHARACTERS):
276
+ break
277
+ if argument.startswith(OPTION_PREFIX):
278
+ parameter, _separator, argument = argument.partition(SLEEP_PARAMETER_VALUE_SEPARATOR)
279
+ seconds_per_unit = _start_sleep_unit_seconds(parameter)
280
+ if seconds_per_unit is None or not argument:
281
+ continue
282
+ duration_seconds = _duration_seconds(argument)
283
+ if duration_seconds is None:
284
+ if any(each_marker in argument for each_marker in ALL_SHELL_EXPANSION_MARKERS):
285
+ return None
286
+ continue
287
+ total_seconds += duration_seconds * seconds_per_unit
288
+ return total_seconds
289
+
290
+
291
+ def _start_sleep_unit_seconds(parameter: str) -> float | None:
292
+ """Return the seconds per unit a ``Start-Sleep`` parameter such as ``-Seconds``, ``-m`` or ``-Duration`` names.
293
+
294
+ ``-Duration`` takes a ``hh:mm:ss`` value already in seconds, so its unit
295
+ is one second. Any other parameter names no unit and returns None.
296
+ """
297
+ if len(parameter) < MINIMUM_START_SLEEP_PARAMETER_LENGTH:
298
+ return None
299
+ if START_SLEEP_MILLISECONDS_PARAMETER.startswith(parameter):
300
+ return 1.0 / MILLISECONDS_PER_SECOND
301
+ if START_SLEEP_SECONDS_PARAMETER.startswith(parameter):
302
+ return 1.0
303
+ if len(parameter) >= MINIMUM_START_SLEEP_DURATION_PARAMETER_LENGTH and (
304
+ START_SLEEP_DURATION_PARAMETER.startswith(parameter)
305
+ ):
306
+ return 1.0
307
+ return None
308
+
309
+
310
+ def _duration_seconds(duration_text: str) -> float | None:
311
+ """Return the seconds a duration such as ``30``, ``.5``, ``1m`` or ``00:01:30`` waits, or None."""
312
+ if SLEEP_TIMESPAN_PATTERN.fullmatch(duration_text):
313
+ all_fields = duration_text.split(SLEEP_TIMESPAN_FIELD_SEPARATOR)
314
+ return sum(
315
+ float(each_field) * each_field_seconds
316
+ for each_field, each_field_seconds in zip(all_fields, ALL_SECONDS_BY_TIMESPAN_FIELD, strict=False)
317
+ )
318
+ duration = SLEEP_DURATION_PATTERN.fullmatch(duration_text)
319
+ if duration is None:
320
+ return None
321
+ unit_seconds = SECONDS_BY_SLEEP_UNIT[duration.group(SLEEP_DURATION_UNIT_GROUP)]
322
+ return float(duration.group(SLEEP_DURATION_AMOUNT_GROUP)) * unit_seconds
323
+
324
+
325
+ def _total_sleep_seconds(all_segments: list[list[str]]) -> float | None:
326
+ """Return the seconds every sleep in the segments waits in total, or None when one is unreadable."""
327
+ total_seconds = 0.0
328
+ for each_segment in all_segments:
329
+ program_name, all_arguments = segment_program_and_arguments(each_segment)
330
+ if program_name.lower() not in ALL_SLEEP_PROGRAM_NAMES:
331
+ continue
332
+ seconds = _sleep_seconds(all_arguments)
333
+ if seconds is None:
334
+ return None
335
+ total_seconds += seconds
336
+ return total_seconds
337
+
338
+
339
+ def is_tight_gh_loop_text(command_text: str) -> bool:
340
+ """Return True when one command text holds a loop that calls gh with too little sleep.
341
+
342
+ ::
343
+
344
+ while :; do gh run view 1; sleep 30; done True
345
+ while :; do gh run view 1; sleep 60; done False
346
+
347
+ Args:
348
+ command_text: The shell command text, past any wrapper.
349
+ """
350
+ return any(
351
+ _is_tight_gh_loop_span(each_span)
352
+ for each_span in _loop_spans(_ordered_segments(command_text))
353
+ )
354
+
355
+
356
+ def _is_tight_gh_loop_span(all_segments: list[list[str]]) -> bool:
357
+ """Return True when one loop's segments call gh with too little sleep."""
358
+ if not any(_segment_calls_gh(each_segment) for each_segment in all_segments):
359
+ return False
360
+ total_seconds = _total_sleep_seconds(all_segments)
361
+ if total_seconds is None:
362
+ return False
363
+ if total_seconds == 0:
364
+ return _is_endless_loop(all_segments) or _has_gh_loop_condition(all_segments)
365
+ return total_seconds < MINIMUM_POLL_INTERVAL_SECONDS
366
+
367
+
368
+ def is_tight_gh_watch_segment(all_segment_tokens: list[str]) -> bool:
369
+ """Return True when one segment runs ``watch`` on a gh call under 60 seconds apart.
370
+
371
+ ::
372
+
373
+ watch -n 5 gh run view 1 True
374
+ watch -n 60 gh run view 1 False
375
+ watch -n 5 date False
376
+
377
+ Args:
378
+ all_segment_tokens: The tokens of one shell segment.
379
+ """
380
+ program_name, all_arguments = segment_program_and_arguments(all_segment_tokens)
381
+ if program_name.lower() != WATCH_PROGRAM_NAME:
382
+ return False
383
+ interval_seconds, watched_command = watch_interval_and_command(all_arguments)
384
+ if interval_seconds is None or interval_seconds >= MINIMUM_POLL_INTERVAL_SECONDS:
385
+ return False
386
+ return bool(all_gh_call_argument_lists(watched_command, True))
@@ -263,6 +263,11 @@ NO_CLEAN_OPEN_REVIEW_REASON_TEMPLATE = (
263
263
  "finding, comma separated>`, each path:line a line this head holds. Then open the pull "
264
264
  "request. A merge pinned to {head} accepts an open pass only at zero findings."
265
265
  )
266
+ PREFIX_ORIGIN_CHECK_SENTENCE = (
267
+ " This create names its repository only with a `GH_REPO=` prefix, and a wrapper such as "
268
+ "`env -u GH_REPO` can drop that prefix before gh runs, so the gate also checks the origin "
269
+ "repository. Name the repository with `--repo owner/name` on the create instead."
270
+ )
266
271
  UNKNOWN_OPEN_TARGET_REASON = (
267
272
  "This pull request names no repository or head commit the review gate can read. Check that "
268
273
  "the head branch is on GitHub and this session can reach it, or run it from the branch "
@@ -49,6 +49,8 @@ from hooks_constants.shell_command_pipeline import (
49
49
  segments_with_following_operator,
50
50
  )
51
51
  from hooks_constants.shell_command_segments import (
52
+ ALL_LAUNCHER_WRAPPER_COMMANDS,
53
+ LAUNCHER_DURATION_PATTERN,
52
54
  LEADING_ASSIGNMENT_PATTERN,
53
55
  effective_leading_program,
54
56
  token_basename,
@@ -186,20 +188,33 @@ def _wrapper_basename(token: str) -> str:
186
188
  def _wrapper_program_index(all_segment_tokens: list[str]) -> int | None:
187
189
  """Return the index of the program a wrapper step reads.
188
190
 
189
- A wrapper with its own option grammar is read where it stands, so the
190
- ``-u HOME`` behind ``env`` stays an option. Any other leading program is
191
- found past assignments and plain launchers such as ``time`` and ``nohup``.
191
+ ::
192
+
193
+ nohup env -u HOME gh pr the index of env
194
+ nice -n 5 stdbuf -o0 gh pr the index of gh
195
+
196
+ Assignments and plain launchers such as ``time``, ``nohup`` and
197
+ ``nice -n 5`` are stepped past with their options. A wrapper with its
198
+ own option grammar is read where it stands, so the ``-u HOME`` behind
199
+ ``env`` stays an option.
192
200
  """
201
+ has_seen_launcher = False
193
202
  for each_index, each_token in enumerate(all_segment_tokens):
194
203
  if LEADING_ASSIGNMENT_PATTERN.match(each_token) is not None:
195
204
  continue
196
- if _wrapper_basename(each_token) in ALL_WRAPPER_OPTION_GRAMMARS_BY_NAME:
205
+ program_basename = _wrapper_basename(each_token)
206
+ if program_basename in ALL_WRAPPER_OPTION_GRAMMARS_BY_NAME:
197
207
  return each_index
198
- break
199
- leading_program = effective_leading_program(all_segment_tokens)
200
- if leading_program is None:
201
- return None
202
- return all_segment_tokens.index(leading_program)
208
+ if program_basename in ALL_LAUNCHER_WRAPPER_COMMANDS:
209
+ has_seen_launcher = True
210
+ continue
211
+ is_launcher_option = each_token.startswith(SHORT_OPTION_PREFIX) or (
212
+ LAUNCHER_DURATION_PATTERN.match(each_token) is not None
213
+ )
214
+ if has_seen_launcher and is_launcher_option:
215
+ continue
216
+ return each_index
217
+ return None
203
218
 
204
219
 
205
220
  def _all_tokens_after_one_wrapper(all_segment_tokens: list[str]) -> list[str] | None:
@@ -11,6 +11,24 @@ SHELL_COMMAND_KEY = "command"
11
11
  SHELL_REPLY_COMMAND_PATTERN = re.compile(
12
12
  r"\bgh\s+api\b.*(?:/comments/\d+/replies\b|addPullRequestReviewThreadReply)", re.DOTALL
13
13
  )
14
+ REPLY_SCRIPT_FILE_NAME = "review_reply_post.py"
15
+ REPLY_SCRIPT_REQUEST_SUBCOMMAND = "request"
16
+ REPLY_SCRIPT_LAUNCHER_NAME_PATTERN = re.compile(r"(?:python(?:3(?:\.\d+)?)?|py)(?:\.exe)?")
17
+ REPLY_SCRIPT_COMMENT_ID_OPTION = "--comment-id"
18
+ REPLY_SCRIPT_THREAD_ID_OPTION = "--thread-id"
19
+ ALL_REPLY_SCRIPT_VALUE_OPTIONS = (
20
+ "--pull",
21
+ REPLY_SCRIPT_COMMENT_ID_OPTION,
22
+ REPLY_SCRIPT_THREAD_ID_OPTION,
23
+ "--body-file",
24
+ "--wait-seconds",
25
+ )
26
+ ALL_REPLY_SCRIPT_FLAG_OPTIONS = ("--supersede",)
27
+ SHELL_OPTION_PREFIX = "-"
28
+ SHELL_UNREADABLE_CHARACTER_PATTERN = re.compile(r"[;&|<>()`$#{}*?\[\]~!\\\n\r]")
29
+ SHELL_OPTION_VALUE_SEPARATOR = "="
30
+ SHELL_PATH_SEPARATOR = "/"
31
+ ABSOLUTE_LAUNCHER_FOLDER_PATTERN = re.compile(r"(?:[A-Za-z]:)?/")
14
32
  SHELL_RESOLVE_COMMAND_PATTERN = re.compile(r"\bgh\s+api\s+graphql\b.*\bresolveReviewThread\b", re.DOTALL)
15
33
  SHELL_RESOLVE_COMMENT_ID_PATTERN = re.compile(r"/pulls/\d+/ccr/comments/(\d+)/resolve\b")
16
34
  ALL_THREAD_ID_INPUT_KEYS = ("threadID", "threadId")
@@ -23,7 +41,9 @@ TEXT_FIELD = "text"
23
41
  MISSING_REPLY_REASON = (
24
42
  "This review thread has no reply on it that posted since the last user prompt and the last"
25
43
  " resolve of the thread. Post the reply on this thread with add_reply_to_pull_request_comment,"
26
- " or with gh api graphql addPullRequestReviewThreadReply and the thread's PRRT_ id, confirm its"
44
+ " with gh api graphql addPullRequestReviewThreadReply and the thread's PRRT_ id, or with a"
45
+ " review_reply_post.py request run as one plain command (python3, the script, request, then only"
46
+ " full option names, each once) that exits 0, confirm its"
27
47
  " result came back without an error, then resolve the thread by its PRRT_ id or by the comment id the reply answered. A comment id"
28
48
  " ties to a thread only through a pull_request_read get_review_comments result in this session."
29
49
  )
@@ -12,7 +12,7 @@ ALL_BASH_AND_POWERSHELL_TOOL_NAMES = ALL_CONSTANT_BINDINGS["ALL_BASH_AND_POWERSH
12
12
  BashHostedHookEntry = ALL_CONSTANT_BINDINGS["BashHostedHookEntry"]
13
13
 
14
14
 
15
- def test_roster_hosts_the_six_gates_and_hands_two_to_the_shell_guards_mod() -> None:
15
+ def test_roster_hosts_the_seven_gates_and_hands_two_to_the_shell_guards_mod() -> None:
16
16
  assert ALL_BASH_HOSTED_HOOK_ENTRIES == (
17
17
  BashHostedHookEntry(
18
18
  script_relative_path="blocking/msys_rev_path_rewriter.py",
@@ -28,6 +28,10 @@ def test_roster_hosts_the_six_gates_and_hands_two_to_the_shell_guards_mod() -> N
28
28
  applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
29
29
  replaced_by_plugin_name="shell-guards",
30
30
  ),
31
+ BashHostedHookEntry(
32
+ script_relative_path="blocking/gh_api_budget_gate.py",
33
+ applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
34
+ ),
31
35
  BashHostedHookEntry(
32
36
  script_relative_path="blocking/cloud_graphql_gate.py",
33
37
  applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,