claude-dev-env 8.119.2 → 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.
@@ -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))
@@ -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,
@@ -0,0 +1,125 @@
1
+ """Tests for gh_api_budget, which reads the GitHub core REST limit through a five minute cache."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import subprocess
7
+ import sys
8
+ import time
9
+ from pathlib import Path
10
+
11
+ import pytest
12
+
13
+ _HOOKS_ROOT = Path(__file__).resolve().parent.parent
14
+ if str(_HOOKS_ROOT) not in sys.path:
15
+ sys.path.insert(0, str(_HOOKS_ROOT))
16
+
17
+ import hooks_constants.gh_api_budget as budget_reader
18
+
19
+ _RESET_EPOCH_SECONDS = 1_900_000_000
20
+
21
+
22
+ class _RateLimitQueries:
23
+ """Stand-in for the ``gh api rate_limit`` subprocess that counts its calls."""
24
+
25
+ def __init__(self, limit: int, used: int) -> None:
26
+ self.call_count = 0
27
+ self.all_creation_flags: list[object] = []
28
+ self.stdout_text = json.dumps(
29
+ {"resources": {"core": {"limit": limit, "used": used, "reset": _RESET_EPOCH_SECONDS}}}
30
+ )
31
+
32
+ def __call__(
33
+ self, *all_arguments: object, **all_options: object
34
+ ) -> subprocess.CompletedProcess[str]:
35
+ self.call_count += 1
36
+ self.all_creation_flags.append(all_options.get("creationflags"))
37
+ return subprocess.CompletedProcess(
38
+ args=all_arguments, returncode=0, stdout=self.stdout_text, stderr=""
39
+ )
40
+
41
+
42
+ @pytest.fixture
43
+ def cache_path(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
44
+ """Point the managed root at a temporary folder and return the cache file inside it."""
45
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))
46
+ return tmp_path / "cache" / "gh_api_budget.json"
47
+
48
+
49
+ @pytest.fixture
50
+ def rate_queries(monkeypatch: pytest.MonkeyPatch) -> _RateLimitQueries:
51
+ """Replace the rate limit subprocess with a stand-in that reports 4200 of 5000 used."""
52
+ queries = _RateLimitQueries(limit=5000, used=4200)
53
+ monkeypatch.setattr(budget_reader.subprocess, "run", queries)
54
+ return queries
55
+
56
+
57
+ def _write_cache(cache_path: Path, read_at: float, used: int) -> None:
58
+ cache_path.parent.mkdir(parents=True, exist_ok=True)
59
+ cache_path.write_text(
60
+ json.dumps(
61
+ {"read_at": read_at, "limit": 5000, "used": used, "reset": _RESET_EPOCH_SECONDS}
62
+ ),
63
+ encoding="utf-8",
64
+ )
65
+
66
+
67
+ @pytest.mark.parametrize(
68
+ ("limit", "used", "is_spent"),
69
+ [(5000, 3999, False), (5000, 4000, True), (60, 48, True), (60, 47, False), (0, 0, False)],
70
+ )
71
+ def test_should_count_the_budget_spent_at_four_fifths(
72
+ limit: int, used: int, is_spent: bool
73
+ ) -> None:
74
+ budget = budget_reader.CoreRateBudget(limit=limit, used=used, reset=None)
75
+ assert budget_reader.is_core_budget_spent(budget) is is_spent
76
+
77
+
78
+ def test_should_read_a_fresh_cache_without_a_query(
79
+ cache_path: Path, rate_queries: _RateLimitQueries
80
+ ) -> None:
81
+ _write_cache(cache_path, time.time(), used=4000)
82
+ assert budget_reader.read_core_rate_budget() == budget_reader.CoreRateBudget(
83
+ limit=5000, used=4000, reset=_RESET_EPOCH_SECONDS
84
+ )
85
+ assert rate_queries.call_count == 0
86
+
87
+
88
+ def test_should_query_once_for_a_stale_cache_and_store_the_answer(
89
+ cache_path: Path, rate_queries: _RateLimitQueries
90
+ ) -> None:
91
+ _write_cache(cache_path, time.time() - 301, used=10)
92
+ expected_budget = budget_reader.CoreRateBudget(
93
+ limit=5000, used=4200, reset=_RESET_EPOCH_SECONDS
94
+ )
95
+ assert budget_reader.read_core_rate_budget() == expected_budget
96
+ assert budget_reader.read_core_rate_budget() == expected_budget
97
+ assert rate_queries.call_count == 1
98
+ assert json.loads(cache_path.read_text(encoding="utf-8"))["used"] == 4200
99
+
100
+
101
+ def test_should_start_the_rate_limit_query_without_a_console_window(
102
+ cache_path: Path, rate_queries: _RateLimitQueries, monkeypatch: pytest.MonkeyPatch
103
+ ) -> None:
104
+ windows_no_console_flag = 0x08000000
105
+ monkeypatch.setattr(
106
+ budget_reader, "hidden_window_creation_flags", lambda: windows_no_console_flag
107
+ )
108
+ budget_reader.read_core_rate_budget()
109
+ assert rate_queries.all_creation_flags == [windows_no_console_flag]
110
+
111
+
112
+ def test_should_return_none_and_cache_the_failure_when_gh_is_missing(
113
+ cache_path: Path, monkeypatch: pytest.MonkeyPatch
114
+ ) -> None:
115
+ all_calls: list[object] = []
116
+
117
+ def _missing_gh(*all_arguments: object, **all_options: object) -> None:
118
+ all_calls.append(all_arguments)
119
+ raise FileNotFoundError("gh")
120
+
121
+ monkeypatch.setattr(budget_reader.subprocess, "run", _missing_gh)
122
+ assert budget_reader.read_core_rate_budget() is None
123
+ assert budget_reader.read_core_rate_budget() is None
124
+ assert len(all_calls) == 1
125
+ assert json.loads(cache_path.read_text(encoding="utf-8"))["limit"] is None
@@ -0,0 +1,49 @@
1
+ """Pin how gh_argument_lists finds the gh calls a shell command runs."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import pytest
6
+
7
+ from hooks_constants import gh_argument_lists
8
+
9
+
10
+ @pytest.mark.parametrize(
11
+ ("command", "all_expected_argument_lists"),
12
+ [
13
+ ("gh pr view 12", [["pr", "view", "12"]]),
14
+ ("$s = gh run watch 1", [["run", "watch", "1"]]),
15
+ ("{gh run view 1}", [["run", "view", "1", "}"]]),
16
+ ("watch -n 5 gh run view 1", [["run", "view", "1"]]),
17
+ ('bash -c "gh run view 1"', [["run", "view", "1"]]),
18
+ ('echo "gh run watch"', []),
19
+ ("git status", []),
20
+ ],
21
+ )
22
+ def test_should_return_the_arguments_of_each_gh_call(
23
+ command: str, all_expected_argument_lists: list[list[str]]
24
+ ) -> None:
25
+ assert gh_argument_lists.all_gh_call_argument_lists(command) == all_expected_argument_lists
26
+
27
+
28
+ def test_should_split_a_segment_at_each_glued_brace() -> None:
29
+ assert gh_argument_lists.brace_split_segments(["{gh", "run", "view", "1}"]) == [
30
+ ["{", "gh", "run", "view", "1", "}"]
31
+ ]
32
+ assert gh_argument_lists.brace_split_segments(["${name}"]) == [["${name}"]]
33
+
34
+
35
+ @pytest.mark.parametrize(
36
+ ("all_arguments", "expected_interval_and_command"),
37
+ [
38
+ (["-n", "5", "gh", "run", "view", "1"], (5.0, "gh run view 1")),
39
+ (["--interval=30", "gh", "pr", "view"], (30.0, "gh pr view")),
40
+ (["gh", "run", "view", "1"], (2.0, "gh run view 1")),
41
+ (["-n", "$delay", "gh", "run", "view"], (None, "gh run view")),
42
+ ],
43
+ )
44
+ def test_should_read_the_watch_interval_and_the_command_it_runs(
45
+ all_arguments: list[str], expected_interval_and_command: tuple[float | None, str]
46
+ ) -> None:
47
+ assert (
48
+ gh_argument_lists.watch_interval_and_command(all_arguments) == expected_interval_and_command
49
+ )
@@ -0,0 +1,50 @@
1
+ """Pin how gh_shell_loop_scan reads shell loops and ``watch`` calls that poll gh."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import pytest
6
+
7
+ from hooks_constants import gh_shell_loop_scan
8
+
9
+
10
+ @pytest.mark.parametrize(
11
+ ("command_text", "is_tight"),
12
+ [
13
+ ("while :; do gh run view 1; sleep 30; done", True),
14
+ ("while ($true) { gh run view 1 }", True),
15
+ ("for (;;) { gh run view 1; Start-Sleep -Milliseconds 500 }", True),
16
+ ("while :; do gh run view 1; sleep .5; done", True),
17
+ ("while ($true) { gh run view 1; Start-Sleep -Seconds 5 -Verbose }", True),
18
+ ("while ($true) { gh run view 1; Start-Sleep -Duration 00:00:05 }", True),
19
+ ("while ($true) { gh run view 1; Start-Sleep -Duration 00:01:30 }", False),
20
+ ("while :; do gh run view 1; sleep 5 2>/dev/null; done", True),
21
+ ("while :; do gh run view 1; sleep 5 > /dev/null; done", True),
22
+ ("while :; do gh run view 1; sleep 90 2>/dev/null; done", False),
23
+ ("while :; do gh run view 1; sleep `cat delay`; done", False),
24
+ ("while :; do gh run view 1; sleep 30; sleep 30; done", False),
25
+ ("while :; do gh run view 1; sleep 1m; done", False),
26
+ ("for x in a b c; do gh pr view $x; done", False),
27
+ ("while :; do gh run view 1; sleep $delay; done", False),
28
+ ("while :; do date; sleep 5; done", False),
29
+ ],
30
+ )
31
+ def test_should_flag_a_loop_that_calls_gh_with_too_little_sleep(
32
+ command_text: str, is_tight: bool
33
+ ) -> None:
34
+ assert gh_shell_loop_scan.is_tight_gh_loop_text(command_text) is is_tight
35
+
36
+
37
+ @pytest.mark.parametrize(
38
+ ("all_segment_tokens", "is_tight"),
39
+ [
40
+ (["watch", "-n", "5", "gh", "run", "view", "1"], True),
41
+ (["watch", "gh", "run", "view", "1"], True),
42
+ (["watch", "-n", "60", "gh", "run", "view", "1"], False),
43
+ (["watch", "-n", "5", "date"], False),
44
+ (["gh", "run", "view", "1"], False),
45
+ ],
46
+ )
47
+ def test_should_flag_a_watch_on_gh_under_sixty_seconds(
48
+ all_segment_tokens: list[str], is_tight: bool
49
+ ) -> None:
50
+ assert gh_shell_loop_scan.is_tight_gh_watch_segment(all_segment_tokens) is is_tight
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-dev-env",
3
- "version": "8.119.2",
3
+ "version": "8.120.0",
4
4
  "description": "Claude Code development standards — rules, hooks, agents, commands, and skills",
5
5
  "type": "module",
6
6
  "bin": {