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.
- package/hooks/blocking/followup_pr_dedupe.py +71 -13
- package/hooks/blocking/gh_api_budget_gate.py +286 -0
- package/hooks/blocking/merge_review_gate.py +4 -0
- package/hooks/blocking/merge_review_gate_parts/create_folders.py +6 -2
- package/hooks/blocking/merge_review_gate_parts/open_request.py +40 -15
- package/hooks/blocking/resolve_thread_reply_gate.py +116 -1
- package/hooks/blocking/test_bash_dispatcher_interpreter_starts.py +8 -3
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +1 -0
- package/hooks/blocking/test_cloud_graphql_gate.py +4 -0
- package/hooks/blocking/test_followup_pr_dedupe.py +99 -0
- package/hooks/blocking/test_gh_api_budget_gate.py +602 -0
- package/hooks/blocking/test_gh_global_account_switch_gate.py +4 -0
- package/hooks/blocking/test_headless_claude_broker_gate.py +4 -0
- package/hooks/blocking/test_merge_review_gate.py +133 -0
- package/hooks/blocking/test_python_windows_invocation_gate.py +4 -0
- package/hooks/blocking/test_resolve_thread_reply_gate.py +430 -0
- package/hooks/blocking/tests/test_create_folders.py +7 -0
- package/hooks/blocking/tests/test_open_request.py +21 -0
- package/hooks/features/bash-dispatchers.md +3 -0
- package/hooks/features/conduct-gates.md +3 -3
- package/hooks/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +4 -0
- package/hooks/hooks_constants/followup_pr_dedupe_constants.py +5 -0
- package/hooks/hooks_constants/gh_api_budget.py +186 -0
- package/hooks/hooks_constants/gh_api_budget_gate_constants.py +215 -0
- package/hooks/hooks_constants/gh_argument_lists.py +169 -0
- package/hooks/hooks_constants/gh_shell_loop_scan.py +386 -0
- package/hooks/hooks_constants/merge_review_gate_constants.py +5 -0
- package/hooks/hooks_constants/pytest_invocation.py +24 -9
- package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +21 -1
- package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +5 -1
- package/hooks/hooks_constants/test_gh_api_budget.py +125 -0
- package/hooks/hooks_constants/test_gh_argument_lists.py +49 -0
- package/hooks/hooks_constants/test_gh_shell_loop_scan.py +50 -0
- package/hooks/hooks_constants/test_pytest_invocation.py +25 -0
- package/hooks/hooks_constants/test_shell_command_mutation.py +11 -0
- package/hooks/hooks_constants/transcript_skill_scan_constants.py +22 -0
- package/hooks/session/test_npx_pele_guidance.py +4 -0
- package/hooks/test_transcript_skill_scan.py +72 -0
- package/hooks/transcript_skill_scan.py +138 -4
- package/hooks/validators/config/security_checks_constants.py +88 -0
- package/hooks/validators/security_checks.py +186 -1
- package/hooks/validators/test_security_checks.py +149 -0
- package/package.json +1 -1
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
"""Tests for gh_api_budget_gate, which keeps agents from draining the GitHub API limit."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import subprocess
|
|
7
|
+
import time
|
|
8
|
+
from io import StringIO
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from unittest.mock import patch
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
import gh_api_budget_gate as gate
|
|
15
|
+
import hooks_constants.gh_api_budget as budget_reader
|
|
16
|
+
|
|
17
|
+
_FIVE_MINUTES_IN_SECONDS = 300
|
|
18
|
+
_RESET_EPOCH_SECONDS = 1_900_000_000
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _RateLimitQueries:
|
|
22
|
+
"""Stand-in for the ``gh api rate_limit`` subprocess that counts its calls."""
|
|
23
|
+
|
|
24
|
+
def __init__(self) -> None:
|
|
25
|
+
self.call_count = 0
|
|
26
|
+
self.stdout_text = ""
|
|
27
|
+
self.raised_error: BaseException | None = None
|
|
28
|
+
self.return_code = 0
|
|
29
|
+
|
|
30
|
+
def answer_with(self, limit: int, used: int) -> None:
|
|
31
|
+
self.stdout_text = json.dumps(
|
|
32
|
+
{"resources": {"core": {"limit": limit, "used": used, "reset": _RESET_EPOCH_SECONDS}}}
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def __call__(
|
|
36
|
+
self, *all_arguments: object, **all_options: object
|
|
37
|
+
) -> subprocess.CompletedProcess[str]:
|
|
38
|
+
self.call_count += 1
|
|
39
|
+
if self.raised_error is not None:
|
|
40
|
+
raise self.raised_error
|
|
41
|
+
return subprocess.CompletedProcess(
|
|
42
|
+
args=all_arguments, returncode=self.return_code, stdout=self.stdout_text, stderr=""
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@pytest.fixture
|
|
47
|
+
def config_root(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
|
|
48
|
+
"""Point the managed root at a temporary folder."""
|
|
49
|
+
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))
|
|
50
|
+
return tmp_path
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@pytest.fixture
|
|
54
|
+
def rate_queries(monkeypatch: pytest.MonkeyPatch, config_root: Path) -> _RateLimitQueries:
|
|
55
|
+
"""Replace the rate limit subprocess with a counting stand-in that reports a fresh limit."""
|
|
56
|
+
queries = _RateLimitQueries()
|
|
57
|
+
queries.answer_with(limit=5000, used=10)
|
|
58
|
+
monkeypatch.setattr(budget_reader.subprocess, "run", queries)
|
|
59
|
+
return queries
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _cache_path(config_root: Path) -> Path:
|
|
63
|
+
return config_root / "cache" / "gh_api_budget.json"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _write_cache(config_root: Path, read_at: float, limit: int | None, used: int | None) -> None:
|
|
67
|
+
cache_path = _cache_path(config_root)
|
|
68
|
+
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
|
69
|
+
cache_path.write_text(
|
|
70
|
+
json.dumps(
|
|
71
|
+
{"read_at": read_at, "limit": limit, "used": used, "reset": _RESET_EPOCH_SECONDS}
|
|
72
|
+
),
|
|
73
|
+
encoding="utf-8",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _stdout_from_main(payload: dict[str, object]) -> str:
|
|
78
|
+
"""Return what main() wrote to stdout for one hook payload."""
|
|
79
|
+
captured_stdout = StringIO()
|
|
80
|
+
with (
|
|
81
|
+
patch("sys.stdin", StringIO(json.dumps(payload))),
|
|
82
|
+
patch("sys.stdout", captured_stdout),
|
|
83
|
+
patch.object(gate, "log_hook_block"),
|
|
84
|
+
):
|
|
85
|
+
gate.main()
|
|
86
|
+
return captured_stdout.getvalue()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _bash_reason(command: str) -> str:
|
|
90
|
+
"""Return the deny reason main() gives a Bash command, or an empty string when it passes."""
|
|
91
|
+
stdout_text = _stdout_from_main({"tool_name": "Bash", "tool_input": {"command": command}})
|
|
92
|
+
if not stdout_text:
|
|
93
|
+
return ""
|
|
94
|
+
specific_output = json.loads(stdout_text)["hookSpecificOutput"]
|
|
95
|
+
assert specific_output["permissionDecision"] == "deny"
|
|
96
|
+
return specific_output["permissionDecisionReason"]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@pytest.mark.parametrize(
|
|
100
|
+
"command",
|
|
101
|
+
[
|
|
102
|
+
"gh run watch 123",
|
|
103
|
+
"gh run watch",
|
|
104
|
+
"gh -R o/r run watch 123 --exit-status",
|
|
105
|
+
"gh.exe run watch 123",
|
|
106
|
+
"gh pr checks 12 --watch",
|
|
107
|
+
"gh pr checks --watch --interval 10",
|
|
108
|
+
"cd repo && gh run watch 123",
|
|
109
|
+
'bash -c "gh run watch 1"',
|
|
110
|
+
'pwsh -Command "gh pr checks 12 --watch"',
|
|
111
|
+
],
|
|
112
|
+
)
|
|
113
|
+
def test_should_detect_watch_commands(command: str) -> None:
|
|
114
|
+
assert gate.runs_gh_watch_command(command) is True
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@pytest.mark.parametrize(
|
|
118
|
+
"command",
|
|
119
|
+
[
|
|
120
|
+
"gh pr checks 12",
|
|
121
|
+
"gh run view 123",
|
|
122
|
+
"gh run watch --help",
|
|
123
|
+
'echo "gh run watch"',
|
|
124
|
+
'git commit -m "gh run watch"',
|
|
125
|
+
'grep "gh pr checks --watch" notes.md',
|
|
126
|
+
"watch -n 120 date",
|
|
127
|
+
],
|
|
128
|
+
)
|
|
129
|
+
def test_should_not_detect_watch_in_other_commands(command: str) -> None:
|
|
130
|
+
assert gate.runs_gh_watch_command(command) is False
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@pytest.mark.parametrize(
|
|
134
|
+
"command",
|
|
135
|
+
[
|
|
136
|
+
"while true; do gh run view 1; sleep 30; done",
|
|
137
|
+
"while true; do gh run view 1; sleep 0.5; done",
|
|
138
|
+
"while true; do gh run view 1; sleep 30s; sleep 20; done",
|
|
139
|
+
'while true; do gh run view "1"; sleep "30"; done',
|
|
140
|
+
"sleep 120 && while :; do gh run view 1; sleep 10; done",
|
|
141
|
+
'while :; do bash -c "gh run view 1"; sleep 10; done',
|
|
142
|
+
"until gh run view 1 --exit-status; do sleep 10; done",
|
|
143
|
+
"for i in 1 2 3; do gh run view 1; sleep 5; done",
|
|
144
|
+
"while true\ndo\n gh run view 1\n sleep 30\ndone",
|
|
145
|
+
"while true; do gh run view 1; done",
|
|
146
|
+
"while :; do gh api repos/o/r/actions/runs; done",
|
|
147
|
+
"for ((;;)); do gh run view 1; done",
|
|
148
|
+
"until gh run view 1 --exit-status; do :; done",
|
|
149
|
+
'while [ "$(gh run view 1 --json status -q .status)" != completed ]; do sleep 5; done',
|
|
150
|
+
'bash -c "while true; do gh run view 1; sleep 5; done"',
|
|
151
|
+
"while ($true) { gh run view 1; Start-Sleep 30 }",
|
|
152
|
+
"while ($true) { gh run view 1; Start-Sleep -Seconds 30 }",
|
|
153
|
+
"for (;;) { gh run view 1; Start-Sleep -Milliseconds 500 }",
|
|
154
|
+
"while ($true) { gh run view 1 }",
|
|
155
|
+
"do { gh run view 1; Start-Sleep 5 } while ($true)",
|
|
156
|
+
"foreach ($each in 1..5) { gh run view 1; Start-Sleep -s 2 }",
|
|
157
|
+
"watch -n 5 gh run view 1",
|
|
158
|
+
"watch -n5 'gh run view 1'",
|
|
159
|
+
"watch gh run view 1",
|
|
160
|
+
"watch --interval=30 gh pr checks 12",
|
|
161
|
+
"while :; do for i in 1 2; do gh run view $i; done; sleep 5; done",
|
|
162
|
+
],
|
|
163
|
+
)
|
|
164
|
+
def test_should_detect_tight_gh_polling_loops(command: str) -> None:
|
|
165
|
+
assert gate.runs_tight_gh_polling_loop(command) is True
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
@pytest.mark.parametrize(
|
|
169
|
+
"command",
|
|
170
|
+
[
|
|
171
|
+
"while ($true) { gh run view 1; Start-Sleep 5}",
|
|
172
|
+
"while ($true) { gh run view 1; Start-Sleep -Seconds 5}",
|
|
173
|
+
"while ($true) {gh run view 1}",
|
|
174
|
+
"while($true){gh run view 1; Start-Sleep 5}",
|
|
175
|
+
"foreach ($i in 1..9) {gh run view $i; Start-Sleep 5}",
|
|
176
|
+
],
|
|
177
|
+
)
|
|
178
|
+
def test_should_detect_powershell_loops_with_glued_braces(command: str) -> None:
|
|
179
|
+
assert gate.runs_tight_gh_polling_loop(command) is True
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@pytest.mark.parametrize(
|
|
183
|
+
"command",
|
|
184
|
+
[
|
|
185
|
+
"1..100 | ForEach-Object { gh run view $_; Start-Sleep 5 }",
|
|
186
|
+
"1..100 | % { gh run view $_; Start-Sleep 5 }",
|
|
187
|
+
],
|
|
188
|
+
)
|
|
189
|
+
def test_should_detect_tight_powershell_pipeline_loops(command: str) -> None:
|
|
190
|
+
assert gate.runs_tight_gh_polling_loop(command) is True
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@pytest.mark.parametrize(
|
|
194
|
+
"command",
|
|
195
|
+
[
|
|
196
|
+
"1..100 | ForEach-Object { gh run view $_; Start-Sleep 60 }",
|
|
197
|
+
"1..100 | % { gh run view $_; Start-Sleep 60 }",
|
|
198
|
+
"1..100 | ForEach-Object { gh run view $_ }",
|
|
199
|
+
"1..100 | % { gh run view $_ }",
|
|
200
|
+
],
|
|
201
|
+
)
|
|
202
|
+
def test_should_pass_slow_and_sleepless_powershell_pipeline_loops(command: str) -> None:
|
|
203
|
+
assert gate.runs_tight_gh_polling_loop(command) is False
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@pytest.mark.parametrize(
|
|
207
|
+
"command",
|
|
208
|
+
[
|
|
209
|
+
"for i in 1 2 3; do gh pr view $i; done; while [ ! -f x ]; do sleep 5; done",
|
|
210
|
+
"for i in 1 2 3; do gh pr view $i; done; for j in 1 2; do sleep 2; done",
|
|
211
|
+
"while :; do gh run view ${id}; sleep 60; done",
|
|
212
|
+
"while :; do { gh run view 1; }; sleep 90; done",
|
|
213
|
+
"while ($true) { if ($x) { gh run view 1 }; Start-Sleep 90 }",
|
|
214
|
+
"while ($true) { do { gh run view 1 } while ($x); Start-Sleep 90 }",
|
|
215
|
+
],
|
|
216
|
+
)
|
|
217
|
+
def test_should_read_each_separate_loop_on_its_own(command: str) -> None:
|
|
218
|
+
assert gate.runs_tight_gh_polling_loop(command) is False
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@pytest.mark.parametrize(
|
|
222
|
+
"command",
|
|
223
|
+
[
|
|
224
|
+
"while ($true) { gh run view 1 }; Start-Sleep 60",
|
|
225
|
+
"while ($true) { gh run view 1 }\nStart-Sleep 60",
|
|
226
|
+
"while ($true) { $s = gh run view 1; Start-Sleep 5 }",
|
|
227
|
+
"while ($true) { $s = gh run view 1 | ConvertFrom-Json; Start-Sleep 5 }",
|
|
228
|
+
],
|
|
229
|
+
)
|
|
230
|
+
def test_should_close_a_one_segment_loop_body_and_read_assigned_gh_in_loops(
|
|
231
|
+
command: str,
|
|
232
|
+
) -> None:
|
|
233
|
+
assert gate.runs_tight_gh_polling_loop(command) is True
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
@pytest.mark.parametrize(
|
|
237
|
+
"command",
|
|
238
|
+
[
|
|
239
|
+
"foreach ($i in 1..9) { gh run view $i }; Start-Sleep 5",
|
|
240
|
+
"while ($x) { gh run view 1 }; Start-Sleep 5",
|
|
241
|
+
"1..100 | ForEach-Object { gh run view $_ }; Start-Sleep 5",
|
|
242
|
+
"1..100 | ForEach-Object { gh run view $_ } | Out-Null; Start-Sleep 5",
|
|
243
|
+
],
|
|
244
|
+
)
|
|
245
|
+
def test_should_not_count_a_sleep_after_a_one_segment_loop_body(command: str) -> None:
|
|
246
|
+
assert gate.runs_tight_gh_polling_loop(command) is False
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
@pytest.mark.parametrize(
|
|
250
|
+
"command",
|
|
251
|
+
["$s = gh run watch 1", "$script:s = gh run watch 1", "$s += gh run watch 1"],
|
|
252
|
+
)
|
|
253
|
+
def test_should_deny_a_watch_assigned_to_a_powershell_variable(
|
|
254
|
+
config_root: Path, rate_queries: _RateLimitQueries, command: str
|
|
255
|
+
) -> None:
|
|
256
|
+
assert _bash_reason(command) == _bash_reason("gh run watch 1") != ""
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@pytest.mark.parametrize("command", ['$s = "gh run watch 1"', "$s = 'gh'"])
|
|
260
|
+
def test_should_pass_gh_text_assigned_to_a_powershell_variable(
|
|
261
|
+
config_root: Path, rate_queries: _RateLimitQueries, command: str
|
|
262
|
+
) -> None:
|
|
263
|
+
_write_cache(config_root, time.time(), limit=5000, used=4999)
|
|
264
|
+
assert _bash_reason(command) == ""
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def test_should_deny_an_assigned_read_on_a_spent_budget(
|
|
268
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
269
|
+
) -> None:
|
|
270
|
+
_write_cache(config_root, time.time(), limit=5000, used=4000)
|
|
271
|
+
assert "4000 of 5000" in _bash_reason("$s = gh pr view 1")
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def test_should_pass_an_assigned_read_just_under_four_fifths(
|
|
275
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
276
|
+
) -> None:
|
|
277
|
+
_write_cache(config_root, time.time(), limit=5000, used=3999)
|
|
278
|
+
assert _bash_reason("$s = gh pr view 1") == ""
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def test_should_pass_a_token_assignment_with_no_cache_and_no_gh_on_path(
|
|
282
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
283
|
+
) -> None:
|
|
284
|
+
rate_queries.raised_error = FileNotFoundError("gh")
|
|
285
|
+
command = "$env:GH_TOKEN = gh auth token -u account-a; gh repo list; Remove-Item Env:GH_TOKEN"
|
|
286
|
+
assert _bash_reason(command) == ""
|
|
287
|
+
assert rate_queries.call_count == 1
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@pytest.mark.parametrize(
|
|
291
|
+
"command",
|
|
292
|
+
["if ($x) {gh run watch 1}", "try {gh run watch 1} catch {}"],
|
|
293
|
+
)
|
|
294
|
+
def test_should_detect_watch_behind_a_glued_brace(command: str) -> None:
|
|
295
|
+
assert gate.runs_gh_watch_command(command) is True
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def test_should_not_detect_watch_in_quoted_braced_text() -> None:
|
|
299
|
+
assert gate.runs_gh_watch_command('echo "{gh run watch}"') is False
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def test_should_classify_a_read_behind_a_glued_brace_as_budgeted() -> None:
|
|
303
|
+
assert gate.runs_budgeted_gh_read("if ($x) {gh pr view 1}") is True
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
@pytest.mark.parametrize(
|
|
307
|
+
"command",
|
|
308
|
+
[
|
|
309
|
+
"for x in a b c; do gh pr view $x; done",
|
|
310
|
+
"foreach ($x in $all) { gh pr view $x }",
|
|
311
|
+
"while read pr; do gh pr view $pr; done < prs.txt",
|
|
312
|
+
"while true; do gh run view 1; sleep 60; done",
|
|
313
|
+
"while true; do gh run view 1; sleep 1m; done",
|
|
314
|
+
"while :; do gh run view 1; sleep 30; sleep 30; done",
|
|
315
|
+
"gh pr list | while read n; do echo $n; sleep 1; done",
|
|
316
|
+
"gh run view 1; for f in a b; do echo $f; sleep 1; done",
|
|
317
|
+
"until gh run view 1 --exit-status; do sleep 60; done",
|
|
318
|
+
"while ($true) { gh run view 1; Start-Sleep -Seconds 90 }",
|
|
319
|
+
"while ($true) { gh run view 1; Start-Sleep -Milliseconds 60000 }",
|
|
320
|
+
"while true; do gh run view 1; sleep $delay; done",
|
|
321
|
+
"while true; do date; sleep 1; done",
|
|
322
|
+
"watch -n 60 gh run view 1",
|
|
323
|
+
"watch -n 1 date",
|
|
324
|
+
"gh run view 1",
|
|
325
|
+
'echo "while true; do gh run view 1; sleep 1; done"',
|
|
326
|
+
],
|
|
327
|
+
)
|
|
328
|
+
def test_should_pass_finite_slow_and_gh_free_loops(command: str) -> None:
|
|
329
|
+
assert gate.runs_tight_gh_polling_loop(command) is False
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def test_watch_reason_names_a_loop_form_that_passes_the_loop_check(config_root: Path) -> None:
|
|
333
|
+
reason = _bash_reason("gh run watch 123")
|
|
334
|
+
assert "60" in reason
|
|
335
|
+
assert "gh run view" in reason
|
|
336
|
+
suggested_loop = reason.split("`")[1]
|
|
337
|
+
assert "gh run view" in suggested_loop
|
|
338
|
+
assert gate.runs_tight_gh_polling_loop(suggested_loop) is False
|
|
339
|
+
assert gate.runs_gh_watch_command(suggested_loop) is False
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
@pytest.mark.parametrize(
|
|
343
|
+
"command",
|
|
344
|
+
[
|
|
345
|
+
"gh api repos/o/r/pulls",
|
|
346
|
+
"gh api -X GET repos/o/r/pulls -f state=open",
|
|
347
|
+
"gh api --paginate repos/o/r/issues",
|
|
348
|
+
"gh pr view 12",
|
|
349
|
+
"gh pr list",
|
|
350
|
+
"gh pr status",
|
|
351
|
+
"gh pr checks 12",
|
|
352
|
+
"gh pr diff 12",
|
|
353
|
+
"gh issue view 4",
|
|
354
|
+
"gh run view 123",
|
|
355
|
+
"gh run list -L 5",
|
|
356
|
+
"gh run download 123",
|
|
357
|
+
"gh release view v1",
|
|
358
|
+
"gh search prs repo:o/r",
|
|
359
|
+
"gh status",
|
|
360
|
+
"gh -R o/r repo view",
|
|
361
|
+
"cd repo && gh pr view 12",
|
|
362
|
+
"x=$(gh api user -q .login)",
|
|
363
|
+
],
|
|
364
|
+
)
|
|
365
|
+
def test_should_classify_remote_reads_as_budgeted(command: str) -> None:
|
|
366
|
+
assert gate.runs_budgeted_gh_read(command) is True
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
@pytest.mark.parametrize(
|
|
370
|
+
"command",
|
|
371
|
+
[
|
|
372
|
+
"gh pr create --title t --body b",
|
|
373
|
+
"gh pr merge 12 --squash",
|
|
374
|
+
"gh pr comment 12 --body hi",
|
|
375
|
+
"gh pr ready 12",
|
|
376
|
+
"gh workflow run ci.yml",
|
|
377
|
+
"gh api -X POST repos/o/r/issues -f title=t",
|
|
378
|
+
"gh api --method=PATCH repos/o/r/issues/1",
|
|
379
|
+
"gh api repos/o/r/issues -f title=t",
|
|
380
|
+
"gh api repos/o/r/issues --input body.json",
|
|
381
|
+
"gh api graphql -f query='query{viewer{login}}'",
|
|
382
|
+
"gh api rate_limit",
|
|
383
|
+
"gh auth status",
|
|
384
|
+
"gh auth token",
|
|
385
|
+
"gh --version",
|
|
386
|
+
"gh help",
|
|
387
|
+
"gh pr view 12 --help",
|
|
388
|
+
"gh",
|
|
389
|
+
'echo "gh pr view 12"',
|
|
390
|
+
"git log --oneline",
|
|
391
|
+
],
|
|
392
|
+
)
|
|
393
|
+
def test_should_not_budget_writes_local_calls_and_other_buckets(command: str) -> None:
|
|
394
|
+
assert gate.runs_budgeted_gh_read(command) is False
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def test_fresh_cache_at_four_fifths_denies_a_read_with_used_limit_and_reset(
|
|
398
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
399
|
+
) -> None:
|
|
400
|
+
_write_cache(config_root, time.time(), limit=5000, used=4000)
|
|
401
|
+
reason = _bash_reason("gh pr view 12")
|
|
402
|
+
assert "4000" in reason
|
|
403
|
+
assert "5000" in reason
|
|
404
|
+
assert time.strftime("%H:%M", time.gmtime(_RESET_EPOCH_SECONDS)) in reason
|
|
405
|
+
assert "git" in reason
|
|
406
|
+
assert "wait" in reason.lower()
|
|
407
|
+
assert rate_queries.call_count == 0
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def test_out_of_range_reset_still_denies_without_a_reset_time(
|
|
411
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
412
|
+
) -> None:
|
|
413
|
+
cache_path = _cache_path(config_root)
|
|
414
|
+
cache_path.parent.mkdir(parents=True)
|
|
415
|
+
cache_path.write_text(
|
|
416
|
+
json.dumps({"read_at": time.time(), "limit": 5000, "used": 4500, "reset": 10**30}),
|
|
417
|
+
encoding="utf-8",
|
|
418
|
+
)
|
|
419
|
+
reason = _bash_reason("gh pr view 12")
|
|
420
|
+
assert "4500 of 5000" in reason
|
|
421
|
+
assert "resets at" not in reason
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def test_fresh_cache_just_under_four_fifths_passes(
|
|
425
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
426
|
+
) -> None:
|
|
427
|
+
_write_cache(config_root, time.time(), limit=5000, used=3999)
|
|
428
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
429
|
+
assert rate_queries.call_count == 0
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
@pytest.mark.parametrize(
|
|
433
|
+
"command",
|
|
434
|
+
[
|
|
435
|
+
"gh pr create --title t --body b",
|
|
436
|
+
"gh pr merge 12 --squash",
|
|
437
|
+
"gh api -X POST repos/o/r/issues -f title=t",
|
|
438
|
+
"gh api graphql -f query='query{viewer{login}}'",
|
|
439
|
+
"gh api rate_limit",
|
|
440
|
+
"gh auth status",
|
|
441
|
+
"gh --version",
|
|
442
|
+
"gh pr view 12 --help",
|
|
443
|
+
],
|
|
444
|
+
)
|
|
445
|
+
def test_spent_budget_still_passes_writes_and_unbudgeted_calls(
|
|
446
|
+
config_root: Path, rate_queries: _RateLimitQueries, command: str
|
|
447
|
+
) -> None:
|
|
448
|
+
_write_cache(config_root, time.time(), limit=5000, used=4999)
|
|
449
|
+
assert _bash_reason(command) == ""
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def test_stale_cache_triggers_one_query_and_rewrites_the_file(
|
|
453
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
454
|
+
) -> None:
|
|
455
|
+
_write_cache(config_root, time.time() - _FIVE_MINUTES_IN_SECONDS - 1, limit=5000, used=4500)
|
|
456
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
457
|
+
assert _bash_reason("gh pr view 13") == ""
|
|
458
|
+
assert rate_queries.call_count == 1
|
|
459
|
+
rewritten_cache = json.loads(_cache_path(config_root).read_text(encoding="utf-8"))
|
|
460
|
+
assert rewritten_cache["used"] == 10
|
|
461
|
+
assert rewritten_cache["limit"] == 5000
|
|
462
|
+
assert rewritten_cache["reset"] == _RESET_EPOCH_SECONDS
|
|
463
|
+
assert time.time() - rewritten_cache["read_at"] < _FIVE_MINUTES_IN_SECONDS
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def test_missing_cache_query_that_reports_a_spent_budget_denies(
|
|
467
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
468
|
+
) -> None:
|
|
469
|
+
rate_queries.answer_with(limit=5000, used=4800)
|
|
470
|
+
assert "4800" in _bash_reason("gh run view 1")
|
|
471
|
+
assert rate_queries.call_count == 1
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
@pytest.mark.parametrize(
|
|
475
|
+
"raised_error",
|
|
476
|
+
[
|
|
477
|
+
subprocess.TimeoutExpired(cmd="gh", timeout=10),
|
|
478
|
+
FileNotFoundError("gh"),
|
|
479
|
+
PermissionError("gh"),
|
|
480
|
+
],
|
|
481
|
+
)
|
|
482
|
+
def test_failed_query_allows_and_is_not_retried_inside_five_minutes(
|
|
483
|
+
config_root: Path, rate_queries: _RateLimitQueries, raised_error: BaseException
|
|
484
|
+
) -> None:
|
|
485
|
+
rate_queries.raised_error = raised_error
|
|
486
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
487
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
488
|
+
assert rate_queries.call_count == 1
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
def test_nonzero_query_exit_allows(config_root: Path, rate_queries: _RateLimitQueries) -> None:
|
|
492
|
+
rate_queries.answer_with(limit=5000, used=4900)
|
|
493
|
+
rate_queries.return_code = 1
|
|
494
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
495
|
+
assert rate_queries.call_count == 1
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
@pytest.mark.parametrize(
|
|
499
|
+
"stdout_text",
|
|
500
|
+
[
|
|
501
|
+
"not json",
|
|
502
|
+
"{}",
|
|
503
|
+
json.dumps({"resources": {"core": {"limit": 0, "used": 0}}}),
|
|
504
|
+
json.dumps({"resources": {"core": {"used": 4900}}}),
|
|
505
|
+
json.dumps([1, 2]),
|
|
506
|
+
],
|
|
507
|
+
)
|
|
508
|
+
def test_unreadable_query_output_allows(
|
|
509
|
+
config_root: Path, rate_queries: _RateLimitQueries, stdout_text: str
|
|
510
|
+
) -> None:
|
|
511
|
+
rate_queries.stdout_text = stdout_text
|
|
512
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
513
|
+
assert rate_queries.call_count == 1
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
@pytest.mark.parametrize(
|
|
517
|
+
"cache_text", ["{", "[]", '{"read_at": "soon", "limit": 5000, "used": 4900}', ""]
|
|
518
|
+
)
|
|
519
|
+
def test_corrupt_cache_counts_as_stale(
|
|
520
|
+
config_root: Path, rate_queries: _RateLimitQueries, cache_text: str
|
|
521
|
+
) -> None:
|
|
522
|
+
cache_path = _cache_path(config_root)
|
|
523
|
+
cache_path.parent.mkdir(parents=True)
|
|
524
|
+
cache_path.write_text(cache_text, encoding="utf-8")
|
|
525
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
526
|
+
assert rate_queries.call_count == 1
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def test_unwritable_cache_folder_allows(
|
|
530
|
+
config_root: Path, rate_queries: _RateLimitQueries, monkeypatch: pytest.MonkeyPatch
|
|
531
|
+
) -> None:
|
|
532
|
+
def _refuse_replace(*all_arguments: object) -> None:
|
|
533
|
+
raise PermissionError("cache")
|
|
534
|
+
|
|
535
|
+
monkeypatch.setattr(budget_reader.os, "replace", _refuse_replace)
|
|
536
|
+
rate_queries.answer_with(limit=5000, used=10)
|
|
537
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
def test_unreadable_cache_file_allows(
|
|
541
|
+
config_root: Path, rate_queries: _RateLimitQueries, monkeypatch: pytest.MonkeyPatch
|
|
542
|
+
) -> None:
|
|
543
|
+
_write_cache(config_root, time.time(), limit=5000, used=4900)
|
|
544
|
+
|
|
545
|
+
def _refuse_read(*all_arguments: object, **all_options: object) -> str:
|
|
546
|
+
raise PermissionError("cache")
|
|
547
|
+
|
|
548
|
+
monkeypatch.setattr(Path, "read_text", _refuse_read)
|
|
549
|
+
assert _bash_reason("gh pr view 12") == ""
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def test_command_without_gh_reads_no_cache_and_runs_no_query(
|
|
553
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
554
|
+
) -> None:
|
|
555
|
+
_write_cache(config_root, time.time() - _FIVE_MINUTES_IN_SECONDS - 1, limit=5000, used=4900)
|
|
556
|
+
assert _bash_reason("git log --oneline; while true; do date; sleep 1; done") == ""
|
|
557
|
+
assert rate_queries.call_count == 0
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def test_watch_command_is_denied_with_the_timed_read_advice(
|
|
561
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
562
|
+
) -> None:
|
|
563
|
+
reason = _bash_reason("gh run watch 123")
|
|
564
|
+
assert "watch" in reason
|
|
565
|
+
assert "60" in reason
|
|
566
|
+
assert "task notification" in reason
|
|
567
|
+
assert rate_queries.call_count == 0
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def test_tight_loop_is_denied_with_the_sixty_second_advice(
|
|
571
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
572
|
+
) -> None:
|
|
573
|
+
reason = _bash_reason("while true; do gh run view 1; sleep 10; done")
|
|
574
|
+
assert "60" in reason
|
|
575
|
+
assert "sleep" in reason
|
|
576
|
+
assert reason != _bash_reason("gh run watch 1")
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
def test_watch_refusal_comes_before_the_loop_refusal(
|
|
580
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
581
|
+
) -> None:
|
|
582
|
+
watch_reason = _bash_reason("gh run watch 1")
|
|
583
|
+
assert _bash_reason("while true; do gh run watch 1; sleep 5; done") == watch_reason
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def test_powershell_payload_with_watch_is_denied(
|
|
587
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
588
|
+
) -> None:
|
|
589
|
+
stdout_text = _stdout_from_main(
|
|
590
|
+
{"tool_name": "PowerShell", "tool_input": {"command": "gh pr checks 12 --watch"}}
|
|
591
|
+
)
|
|
592
|
+
assert json.loads(stdout_text)["hookSpecificOutput"]["permissionDecision"] == "deny"
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def test_other_tool_payload_emits_nothing(
|
|
596
|
+
config_root: Path, rate_queries: _RateLimitQueries
|
|
597
|
+
) -> None:
|
|
598
|
+
_write_cache(config_root, time.time(), limit=5000, used=4900)
|
|
599
|
+
assert (
|
|
600
|
+
_stdout_from_main({"tool_name": "Write", "tool_input": {"command": "gh run watch 1"}}) == ""
|
|
601
|
+
)
|
|
602
|
+
assert rate_queries.call_count == 0
|
|
@@ -45,6 +45,10 @@ def test_global_account_change_is_detected(command: str) -> None:
|
|
|
45
45
|
assert gate.changes_global_gh_account(command) is True
|
|
46
46
|
|
|
47
47
|
|
|
48
|
+
def test_should_detect_an_account_switch_behind_a_launcher_and_env_unset() -> None:
|
|
49
|
+
assert gate.changes_global_gh_account("time -p env -u FOO gh auth switch --user account-b") is True
|
|
50
|
+
|
|
51
|
+
|
|
48
52
|
@pytest.mark.parametrize(
|
|
49
53
|
"command",
|
|
50
54
|
[
|
|
@@ -48,6 +48,10 @@ def test_headless_claude_without_the_broker_is_detected(command: str) -> None:
|
|
|
48
48
|
assert gate.starts_unbrokered_headless_claude(command) is True
|
|
49
49
|
|
|
50
50
|
|
|
51
|
+
def test_should_detect_headless_claude_behind_a_launcher_and_env_unset() -> None:
|
|
52
|
+
assert gate.starts_unbrokered_headless_claude("nohup env -u CLAUDECODE claude -p task") is True
|
|
53
|
+
|
|
54
|
+
|
|
51
55
|
@pytest.mark.parametrize(
|
|
52
56
|
"command",
|
|
53
57
|
[
|