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
|
@@ -8,7 +8,7 @@ import sys
|
|
|
8
8
|
import urllib.error
|
|
9
9
|
import urllib.request
|
|
10
10
|
from collections.abc import Callable, Iterator, Mapping
|
|
11
|
-
from dataclasses import dataclass
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
12
|
from enum import Enum
|
|
13
13
|
from http import HTTPStatus
|
|
14
14
|
from pathlib import Path
|
|
@@ -45,6 +45,7 @@ from hooks_constants.followup_pr_dedupe_constants import (
|
|
|
45
45
|
OPEN_PULL_REQUESTS_PATH_TEMPLATE,
|
|
46
46
|
OPTION_AND_VALUE_WIDTH,
|
|
47
47
|
OPTION_PREFIX,
|
|
48
|
+
PREFIX_ORIGIN_FOLLOWUP_SENTENCE,
|
|
48
49
|
PYTHON_PROGRAM_NAME_PATTERN,
|
|
49
50
|
READ_TIMEOUT_SECONDS,
|
|
50
51
|
REPOSITORY_OPTION,
|
|
@@ -75,11 +76,20 @@ from hooks_constants.shell_command_wrappers import (
|
|
|
75
76
|
|
|
76
77
|
@dataclass(frozen=True)
|
|
77
78
|
class PullRequestDraft:
|
|
78
|
-
"""The repository and body of a pull request about to be opened.
|
|
79
|
+
"""The repository and body of a pull request about to be opened.
|
|
80
|
+
|
|
81
|
+
Attributes:
|
|
82
|
+
owner: Repository owner.
|
|
83
|
+
repo: Repository name.
|
|
84
|
+
body: The create's body.
|
|
85
|
+
is_prefix_origin_check: True for the origin draft a create gets when only a
|
|
86
|
+
``GH_REPO=`` prefix names its repository. Equal drafts compare equal either way.
|
|
87
|
+
"""
|
|
79
88
|
|
|
80
89
|
owner: str
|
|
81
90
|
repo: str
|
|
82
91
|
body: str
|
|
92
|
+
is_prefix_origin_check: bool = field(default=False, compare=False)
|
|
83
93
|
|
|
84
94
|
|
|
85
95
|
@dataclass(frozen=True)
|
|
@@ -435,16 +445,28 @@ def _arguments_draft(
|
|
|
435
445
|
repository_value = option_value(all_arguments, ALL_REPOSITORY_OPTIONS)
|
|
436
446
|
if repository_value is None and is_gh_repository_set:
|
|
437
447
|
return UnreadOriginDraft(body, GH_REPO_SET_FOLLOWUP_REASON_TEMPLATE)
|
|
438
|
-
if repository_value is
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
448
|
+
if repository_value is None:
|
|
449
|
+
return _origin_draft(body, working_directory)
|
|
450
|
+
repository = named_repository(repository_value)
|
|
451
|
+
return None if repository is None else PullRequestDraft(repository[0], repository[1], body)
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def _origin_draft(body: str, working_directory: str) -> PullRequestDraft | UnreadOriginDraft | None:
|
|
455
|
+
remote_url = _origin_remote_url(working_directory)
|
|
456
|
+
if remote_url is None:
|
|
457
|
+
return UnreadOriginDraft(body)
|
|
458
|
+
repository = _github_repository(remote_url)
|
|
445
459
|
return None if repository is None else PullRequestDraft(repository[0], repository[1], body)
|
|
446
460
|
|
|
447
461
|
|
|
462
|
+
def _is_prefix_named_create(all_segment_tokens: list[str]) -> bool:
|
|
463
|
+
program, all_arguments = segment_program_and_arguments(all_segment_tokens)
|
|
464
|
+
if program != GH_PROGRAM_NAME or prefix_repository(all_segment_tokens) is None:
|
|
465
|
+
return False
|
|
466
|
+
all_action_arguments = _gh_action_arguments(all_arguments, ALL_GH_CREATE_WORDS)
|
|
467
|
+
return all_action_arguments is not None and option_value(all_action_arguments, ALL_REPOSITORY_OPTIONS) is None
|
|
468
|
+
|
|
469
|
+
|
|
448
470
|
def sets_gh_repository(command: str) -> bool:
|
|
449
471
|
"""Return True when a command sets or exports ``GH_REPO`` outside a create's own prefix.
|
|
450
472
|
|
|
@@ -492,14 +514,47 @@ def _assigned_name(argument: str) -> str:
|
|
|
492
514
|
|
|
493
515
|
|
|
494
516
|
def _shell_followup_drafts(command: str, working_directory: str) -> list[PullRequestDraft | UnreadOriginDraft]:
|
|
517
|
+
"""Return the follow-up draft of each create in a shell command, in command order.
|
|
518
|
+
|
|
519
|
+
::
|
|
520
|
+
|
|
521
|
+
gh pr create --repo o/r --body "Follow-up to #7" -> [o/r]
|
|
522
|
+
GH_REPO=o/r env -u GH_REPO gh pr create --body "Follow-up to #7" -> [o/r, origin]
|
|
523
|
+
|
|
524
|
+
A create whose repository comes only from a ``GH_REPO=`` prefix also
|
|
525
|
+
gets an origin draft, since a wrapper such as ``env -u GH_REPO`` can drop
|
|
526
|
+
the prefix before gh runs. A prefix value that names no repository, such
|
|
527
|
+
as ``GH_REPO=nonsense``, still gets the origin draft. That origin draft is
|
|
528
|
+
left out when the origin remote gives no GitHub repository, since gh then
|
|
529
|
+
has none to open in.
|
|
530
|
+
|
|
531
|
+
Args:
|
|
532
|
+
command: The shell command text.
|
|
533
|
+
working_directory: The folder the command runs in.
|
|
534
|
+
"""
|
|
495
535
|
is_gh_repository_set = sets_gh_repository(command)
|
|
496
|
-
all_drafts =
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
536
|
+
all_drafts: list[PullRequestDraft | UnreadOriginDraft | None] = []
|
|
537
|
+
for each_segment in all_command_segments(command):
|
|
538
|
+
all_arguments = create_arguments(each_segment)
|
|
539
|
+
if all_arguments is None:
|
|
540
|
+
continue
|
|
541
|
+
draft = _arguments_draft(all_arguments, working_directory, is_gh_repository_set)
|
|
542
|
+
all_drafts.append(draft)
|
|
543
|
+
if not isinstance(draft, UnreadOriginDraft) and _is_prefix_named_create(each_segment):
|
|
544
|
+
all_drafts.append(_prefix_origin_draft(all_arguments, working_directory))
|
|
500
545
|
return list(dict.fromkeys(each_draft for each_draft in all_drafts if each_draft is not None))
|
|
501
546
|
|
|
502
547
|
|
|
548
|
+
def _prefix_origin_draft(all_arguments: list[str], working_directory: str) -> PullRequestDraft | None:
|
|
549
|
+
body = body_from_arguments(all_arguments, working_directory)
|
|
550
|
+
if body is None or followup_parent_number(body) is None:
|
|
551
|
+
return None
|
|
552
|
+
origin_draft = _origin_draft(body, working_directory)
|
|
553
|
+
if not isinstance(origin_draft, PullRequestDraft):
|
|
554
|
+
return None
|
|
555
|
+
return PullRequestDraft(origin_draft.owner, origin_draft.repo, body, is_prefix_origin_check=True)
|
|
556
|
+
|
|
557
|
+
|
|
503
558
|
def shell_command(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
504
559
|
"""Return the command text of a Bash or PowerShell call, or None for any other tool.
|
|
505
560
|
|
|
@@ -641,6 +696,7 @@ def duplicate_followup_reason(
|
|
|
641
696
|
::
|
|
642
697
|
|
|
643
698
|
open pull request #9 says "Follow-up to #7" -> DUPLICATE_FOLLOWUP_REASON_TEMPLATE
|
|
699
|
+
that #9 is in origin, found for a GH_REPO= prefix -> PREFIX_ORIGIN_FOLLOWUP_SENTENCE + the same
|
|
644
700
|
the read gives no answer inside its allowance -> UNANSWERED_FOLLOWUP_REASON_TEMPLATE
|
|
645
701
|
the read answers with a GitHub server error -> UNANSWERED_FOLLOWUP_REASON_TEMPLATE
|
|
646
702
|
no --repo, and the origin read gives nothing -> UNREAD_ORIGIN_FOLLOWUP_REASON_TEMPLATE
|
|
@@ -672,6 +728,8 @@ def duplicate_followup_reason(
|
|
|
672
728
|
if answer is UnreadOpenPullRequests.FAILED:
|
|
673
729
|
continue
|
|
674
730
|
reason = _reason_for_parent(parent_number, answer)
|
|
731
|
+
if reason is not None and each_draft.is_prefix_origin_check:
|
|
732
|
+
return PREFIX_ORIGIN_FOLLOWUP_SENTENCE + reason
|
|
675
733
|
if reason is not None:
|
|
676
734
|
return reason
|
|
677
735
|
return None
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""PreToolUse gate: keep agents from draining the GitHub API limit.
|
|
3
|
+
|
|
4
|
+
The gate refuses three kinds of gh call, checked in this order::
|
|
5
|
+
|
|
6
|
+
gh run watch 123 watch command
|
|
7
|
+
gh pr checks 12 --watch watch command
|
|
8
|
+
while :; do gh run view 1; sleep 10; done tight polling loop
|
|
9
|
+
watch -n 5 gh run view 1 tight polling loop
|
|
10
|
+
gh pr view 12 (core limit 4000 of 5000 used) spent budget
|
|
11
|
+
|
|
12
|
+
A watch command polls every few seconds until a run ends. A polling loop is a
|
|
13
|
+
Bash ``while``, ``until`` or ``for`` loop, a PowerShell ``while``, ``do``,
|
|
14
|
+
``for`` or ``foreach`` loop or ``ForEach-Object`` or ``%`` pipeline loop, or
|
|
15
|
+
``watch``, that calls gh and sleeps under 60 seconds in total per pass. Each
|
|
16
|
+
outermost loop is read on its own, from its keyword through the ``done`` or
|
|
17
|
+
``}`` that closes it, and a loop nested inside it counts toward it. A brace
|
|
18
|
+
glued to a word, as in ``{gh run view 1}``, counts as its own token. An
|
|
19
|
+
endless loop such as ``while :`` or
|
|
20
|
+
``for (;;)``, or a ``while`` or ``until`` loop whose condition calls gh, is
|
|
21
|
+
refused when it calls gh with no sleep. Any other loop with no sleep passes,
|
|
22
|
+
and so does a loop whose sleep is a variable.
|
|
23
|
+
|
|
24
|
+
A gh read on the core REST limit is refused while ``used * 5 >= limit * 4``.
|
|
25
|
+
The reads are the ``gh api`` calls with no write method and no field or input
|
|
26
|
+
option, and the subcommands the constants table lists, such as ``pr view`` and
|
|
27
|
+
``run list``. Writes, local commands, ``gh api graphql`` and ``gh api
|
|
28
|
+
rate_limit`` pass. ``hooks_constants/gh_api_budget.py`` reads the limit and
|
|
29
|
+
caches it for five minutes, and a limit it cannot read lets the call through.
|
|
30
|
+
|
|
31
|
+
The program is read past wrappers, chained commands and a leading PowerShell
|
|
32
|
+
``$name =`` assignment, so ``bash -c "gh run watch 1"`` and ``$s = gh run
|
|
33
|
+
watch 1`` are refused, while quoted text such as ``echo "gh run watch"``
|
|
34
|
+
passes. A command with no gh call reads no cache and runs no query.
|
|
35
|
+
|
|
36
|
+
Hosted by ``blocking/bash_pre_tool_use_dispatcher.py`` for the Bash and
|
|
37
|
+
PowerShell tools.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
from __future__ import annotations
|
|
41
|
+
|
|
42
|
+
import json
|
|
43
|
+
import sys
|
|
44
|
+
import time
|
|
45
|
+
from itertools import pairwise
|
|
46
|
+
from pathlib import Path
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
_hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
50
|
+
if _hooks_root_directory not in sys.path:
|
|
51
|
+
sys.path.insert(0, _hooks_root_directory)
|
|
52
|
+
from hooks_constants.bash_pre_tool_use_dispatcher_constants import (
|
|
53
|
+
ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
|
|
54
|
+
DENY_DECISION,
|
|
55
|
+
HOOK_EVENT_NAME,
|
|
56
|
+
)
|
|
57
|
+
from hooks_constants.cloud_graphql_gate_constants import (
|
|
58
|
+
ALL_GH_HELP_FLAGS,
|
|
59
|
+
GH_API_SUBCOMMAND,
|
|
60
|
+
GH_ENDPOINT_PATH_SEPARATOR,
|
|
61
|
+
GITHUB_GRAPHQL_ENDPOINT_PATTERN,
|
|
62
|
+
)
|
|
63
|
+
from hooks_constants.gh_api_budget import (
|
|
64
|
+
CoreRateBudget,
|
|
65
|
+
is_core_budget_spent,
|
|
66
|
+
read_core_rate_budget,
|
|
67
|
+
)
|
|
68
|
+
from hooks_constants.gh_api_budget_gate_constants import (
|
|
69
|
+
ALL_GH_API_BODY_OPTIONS,
|
|
70
|
+
ALL_GH_API_METHOD_OPTIONS,
|
|
71
|
+
ALL_GH_API_READ_METHODS,
|
|
72
|
+
ALL_GH_API_SHORT_BODY_OPTIONS,
|
|
73
|
+
ALL_GH_API_UNBUDGETED_ENDPOINTS,
|
|
74
|
+
ALL_GH_PR_CHECKS_COMMAND_WORDS,
|
|
75
|
+
ALL_GH_READ_COMMAND_GROUPS,
|
|
76
|
+
ALL_GH_READ_SUBCOMMANDS_BY_COMMAND_GROUP,
|
|
77
|
+
ALL_GH_RUN_WATCH_COMMAND_WORDS,
|
|
78
|
+
GATE_HOOK_NAME,
|
|
79
|
+
GH_API_SHORT_METHOD_OPTION,
|
|
80
|
+
GH_COMMAND_PATH_LENGTH,
|
|
81
|
+
GH_SHORT_OPTION_LENGTH,
|
|
82
|
+
GH_WATCH_OPTION,
|
|
83
|
+
RESET_TIME_FORMAT,
|
|
84
|
+
SPENT_BUDGET_DENY_REASON_TEMPLATE,
|
|
85
|
+
SPENT_BUDGET_RESET_SENTENCE_TEMPLATE,
|
|
86
|
+
TIGHT_POLLING_LOOP_DENY_REASON,
|
|
87
|
+
WATCH_COMMAND_DENY_REASON,
|
|
88
|
+
)
|
|
89
|
+
from hooks_constants.gh_argument_lists import all_gh_call_argument_lists
|
|
90
|
+
from hooks_constants.gh_command_words import gh_command_words_and_options
|
|
91
|
+
from hooks_constants.gh_command_words_constants import GH_OPTION_VALUE_SEPARATOR
|
|
92
|
+
from hooks_constants.gh_shell_loop_scan import (
|
|
93
|
+
is_tight_gh_loop_text,
|
|
94
|
+
is_tight_gh_watch_segment,
|
|
95
|
+
)
|
|
96
|
+
from hooks_constants.hook_block_logger import log_hook_block
|
|
97
|
+
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
98
|
+
from hooks_constants.pytest_invocation import unquoted_token
|
|
99
|
+
from hooks_constants.shell_command_wrappers import (
|
|
100
|
+
all_wrapped_command_segments,
|
|
101
|
+
all_wrapped_command_texts,
|
|
102
|
+
)
|
|
103
|
+
except ImportError as import_error:
|
|
104
|
+
raise ImportError(
|
|
105
|
+
"The gh API budget gate cannot import its dependencies; "
|
|
106
|
+
"ensure the hooks directory is importable."
|
|
107
|
+
) from import_error
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _is_gh_watch_call(all_gh_arguments: list[str]) -> bool:
|
|
111
|
+
all_command_words, all_option_names = gh_command_words_and_options(all_gh_arguments)
|
|
112
|
+
if any(each_option in ALL_GH_HELP_FLAGS for each_option in all_option_names):
|
|
113
|
+
return False
|
|
114
|
+
command_path = tuple(all_command_words[:GH_COMMAND_PATH_LENGTH])
|
|
115
|
+
if command_path == ALL_GH_RUN_WATCH_COMMAND_WORDS:
|
|
116
|
+
return True
|
|
117
|
+
return command_path == ALL_GH_PR_CHECKS_COMMAND_WORDS and GH_WATCH_OPTION in all_option_names
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def runs_gh_watch_command(command: str) -> bool:
|
|
121
|
+
"""Return True when the command runs ``gh run watch`` or ``gh pr checks --watch``.
|
|
122
|
+
|
|
123
|
+
::
|
|
124
|
+
|
|
125
|
+
gh run watch 123 -> True
|
|
126
|
+
cd repo && gh pr checks --watch -> True
|
|
127
|
+
echo "gh run watch" -> False
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
command: The shell command text the agent is about to run.
|
|
131
|
+
"""
|
|
132
|
+
return any(_is_gh_watch_call(each) for each in all_gh_call_argument_lists(command))
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def runs_tight_gh_polling_loop(command: str) -> bool:
|
|
136
|
+
"""Return True when a shell loop or ``watch`` calls gh with under 60 seconds of sleep.
|
|
137
|
+
|
|
138
|
+
::
|
|
139
|
+
|
|
140
|
+
while :; do gh run view 1; sleep 30; done -> True
|
|
141
|
+
while ($true) { gh run view 1 } -> True
|
|
142
|
+
watch -n 5 gh run view 1 -> True
|
|
143
|
+
while :; do gh run view 1; sleep 60; done -> False
|
|
144
|
+
for x in a b c; do gh pr view $x; done -> False
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
command: The shell command text the agent is about to run.
|
|
148
|
+
"""
|
|
149
|
+
if any(is_tight_gh_watch_segment(each) for each in all_wrapped_command_segments(command)):
|
|
150
|
+
return True
|
|
151
|
+
return any(is_tight_gh_loop_text(each) for each in all_wrapped_command_texts(command))
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _gh_api_method(all_gh_arguments: list[str]) -> str | None:
|
|
155
|
+
"""Return the HTTP method a ``gh api`` call names, uppercased, or None when it names none."""
|
|
156
|
+
all_unquoted_arguments = [unquoted_token(each) for each in all_gh_arguments]
|
|
157
|
+
for each_previous_argument, each_argument in pairwise(["", *all_unquoted_arguments]):
|
|
158
|
+
if each_previous_argument in ALL_GH_API_METHOD_OPTIONS:
|
|
159
|
+
return each_argument.upper()
|
|
160
|
+
option, separator, attached_method = each_argument.partition(GH_OPTION_VALUE_SEPARATOR)
|
|
161
|
+
if option in ALL_GH_API_METHOD_OPTIONS and separator:
|
|
162
|
+
return attached_method.upper()
|
|
163
|
+
is_glued_short_method = each_argument.startswith(GH_API_SHORT_METHOD_OPTION)
|
|
164
|
+
if is_glued_short_method and len(each_argument) > GH_SHORT_OPTION_LENGTH:
|
|
165
|
+
return each_argument[GH_SHORT_OPTION_LENGTH:].upper()
|
|
166
|
+
return None
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _is_budgeted_api_call(
|
|
170
|
+
all_gh_arguments: list[str], all_command_words: list[str], all_option_names: list[str]
|
|
171
|
+
) -> bool:
|
|
172
|
+
endpoint = unquoted_token(all_command_words[1]) if len(all_command_words) > 1 else ""
|
|
173
|
+
is_unbudgeted_endpoint = (
|
|
174
|
+
endpoint.strip(GH_ENDPOINT_PATH_SEPARATOR).lower() in ALL_GH_API_UNBUDGETED_ENDPOINTS
|
|
175
|
+
or GITHUB_GRAPHQL_ENDPOINT_PATTERN.search(endpoint) is not None
|
|
176
|
+
)
|
|
177
|
+
if not endpoint or is_unbudgeted_endpoint:
|
|
178
|
+
return False
|
|
179
|
+
method = _gh_api_method(all_gh_arguments)
|
|
180
|
+
if method is not None:
|
|
181
|
+
return method in ALL_GH_API_READ_METHODS
|
|
182
|
+
return not any(
|
|
183
|
+
each_option in ALL_GH_API_BODY_OPTIONS
|
|
184
|
+
or each_option[:GH_SHORT_OPTION_LENGTH] in ALL_GH_API_SHORT_BODY_OPTIONS
|
|
185
|
+
for each_option in all_option_names
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _is_budgeted_gh_read(all_gh_arguments: list[str]) -> bool:
|
|
190
|
+
all_command_words, all_option_names = gh_command_words_and_options(all_gh_arguments)
|
|
191
|
+
if not all_command_words or any(each in ALL_GH_HELP_FLAGS for each in all_option_names):
|
|
192
|
+
return False
|
|
193
|
+
command_group = all_command_words[0]
|
|
194
|
+
if command_group == GH_API_SUBCOMMAND:
|
|
195
|
+
return _is_budgeted_api_call(all_gh_arguments, all_command_words, all_option_names)
|
|
196
|
+
if command_group in ALL_GH_READ_COMMAND_GROUPS:
|
|
197
|
+
return True
|
|
198
|
+
all_read_subcommands = ALL_GH_READ_SUBCOMMANDS_BY_COMMAND_GROUP.get(command_group, frozenset())
|
|
199
|
+
return len(all_command_words) > 1 and all_command_words[1] in all_read_subcommands
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def runs_budgeted_gh_read(command: str) -> bool:
|
|
203
|
+
"""Return True when the command runs a gh read that draws on the core REST limit.
|
|
204
|
+
|
|
205
|
+
::
|
|
206
|
+
|
|
207
|
+
gh pr view 12 -> True
|
|
208
|
+
gh api repos/o/r/pulls -> True
|
|
209
|
+
gh api -X POST repos/o/r/issues -> False
|
|
210
|
+
gh api graphql -f query=@q.graphql -> False
|
|
211
|
+
gh auth status -> False
|
|
212
|
+
|
|
213
|
+
Args:
|
|
214
|
+
command: The shell command text the agent is about to run.
|
|
215
|
+
"""
|
|
216
|
+
return any(_is_budgeted_gh_read(each) for each in all_gh_call_argument_lists(command))
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _spent_budget_reason(budget: CoreRateBudget) -> str:
|
|
220
|
+
reset_sentence = ""
|
|
221
|
+
try:
|
|
222
|
+
if budget.reset is not None:
|
|
223
|
+
reset_time = time.strftime(RESET_TIME_FORMAT, time.gmtime(budget.reset))
|
|
224
|
+
reset_sentence = SPENT_BUDGET_RESET_SENTENCE_TEMPLATE.format(reset_time=reset_time)
|
|
225
|
+
except (OverflowError, OSError, ValueError):
|
|
226
|
+
reset_sentence = ""
|
|
227
|
+
return SPENT_BUDGET_DENY_REASON_TEMPLATE.format(
|
|
228
|
+
used=budget.used, limit=budget.limit, reset_sentence=reset_sentence
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _deny_reason(command: str, all_gh_argument_lists: list[list[str]]) -> str | None:
|
|
233
|
+
"""Return the first refusal's reason for a command that calls gh, or None when it passes."""
|
|
234
|
+
if any(_is_gh_watch_call(each) for each in all_gh_argument_lists):
|
|
235
|
+
return WATCH_COMMAND_DENY_REASON
|
|
236
|
+
if runs_tight_gh_polling_loop(command):
|
|
237
|
+
return TIGHT_POLLING_LOOP_DENY_REASON
|
|
238
|
+
if not any(_is_budgeted_gh_read(each) for each in all_gh_argument_lists):
|
|
239
|
+
return None
|
|
240
|
+
budget = read_core_rate_budget()
|
|
241
|
+
if budget is None or not is_core_budget_spent(budget):
|
|
242
|
+
return None
|
|
243
|
+
return _spent_budget_reason(budget)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def main() -> None:
|
|
247
|
+
"""Deny a gh watch command, a tight gh polling loop, or a gh read on a spent budget."""
|
|
248
|
+
hook_payload = read_hook_input_dictionary_from_stdin()
|
|
249
|
+
if hook_payload is None:
|
|
250
|
+
return
|
|
251
|
+
tool_name = hook_payload.get("tool_name")
|
|
252
|
+
if tool_name not in ALL_BASH_AND_POWERSHELL_TOOL_NAMES:
|
|
253
|
+
return
|
|
254
|
+
tool_input = hook_payload.get("tool_input")
|
|
255
|
+
if not isinstance(tool_input, dict):
|
|
256
|
+
return
|
|
257
|
+
command = tool_input.get("command", "")
|
|
258
|
+
if not isinstance(command, str):
|
|
259
|
+
return
|
|
260
|
+
all_gh_argument_lists = all_gh_call_argument_lists(command)
|
|
261
|
+
if not all_gh_argument_lists:
|
|
262
|
+
return
|
|
263
|
+
deny_reason = _deny_reason(command, all_gh_argument_lists)
|
|
264
|
+
if deny_reason is None:
|
|
265
|
+
return
|
|
266
|
+
log_hook_block(GATE_HOOK_NAME, HOOK_EVENT_NAME, deny_reason, str(tool_name), command)
|
|
267
|
+
_write_deny_decision(deny_reason)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _write_deny_decision(deny_reason: str) -> None:
|
|
271
|
+
sys.stdout.write(
|
|
272
|
+
json.dumps(
|
|
273
|
+
{
|
|
274
|
+
"hookSpecificOutput": {
|
|
275
|
+
"hookEventName": HOOK_EVENT_NAME,
|
|
276
|
+
"permissionDecision": DENY_DECISION,
|
|
277
|
+
"permissionDecisionReason": deny_reason,
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
)
|
|
281
|
+
)
|
|
282
|
+
sys.stdout.flush()
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
if __name__ == "__main__":
|
|
286
|
+
main()
|
|
@@ -44,6 +44,7 @@ from blocking.gate_run_reads import gate_run, read_deadlines_started
|
|
|
44
44
|
from blocking.merge_review_gate_parts.open_request import (
|
|
45
45
|
OpenCall,
|
|
46
46
|
OpenRequest,
|
|
47
|
+
ShellOpenCall,
|
|
47
48
|
open_calls_from,
|
|
48
49
|
open_request_for,
|
|
49
50
|
)
|
|
@@ -94,6 +95,7 @@ from hooks_constants.merge_review_gate_constants import (
|
|
|
94
95
|
OPTION_WITH_VALUE_PATTERN,
|
|
95
96
|
OWNER_FIELD,
|
|
96
97
|
PERMISSION_DECISION_REASON_KEY,
|
|
98
|
+
PREFIX_ORIGIN_CHECK_SENTENCE,
|
|
97
99
|
PR_NUMBER_PATTERN,
|
|
98
100
|
PR_URL_PATTERN,
|
|
99
101
|
RECEIPT_HEAD_KEY,
|
|
@@ -493,6 +495,8 @@ def _deny_decision(reason: str) -> dict[str, object]:
|
|
|
493
495
|
def _first_open_deny_reason(all_open_calls: list[OpenCall]) -> str | None:
|
|
494
496
|
for each_call in all_open_calls:
|
|
495
497
|
reason = open_deny_reason(open_request_for(each_call))
|
|
498
|
+
if reason is not None and isinstance(each_call, ShellOpenCall) and each_call.is_prefix_origin_check:
|
|
499
|
+
return reason + PREFIX_ORIGIN_CHECK_SENTENCE
|
|
496
500
|
if reason is not None:
|
|
497
501
|
return reason
|
|
498
502
|
return None
|
|
@@ -128,7 +128,9 @@ def all_placed_creates(command: str, folder: str | None) -> list[PlacedCreate]:
|
|
|
128
128
|
are read for a wrapper too. A text whose quotes do not close is read as
|
|
129
129
|
written and with its quoted line breaks joined, each in POSIX and raw
|
|
130
130
|
words. ``wrapped_creates.unplaced_creates`` gives each create the
|
|
131
|
-
walk did not place, and it comes back unfollowed.
|
|
131
|
+
walk did not place, and it comes back unfollowed. Two creates with the
|
|
132
|
+
same options stay two creates when their command words differ, so a
|
|
133
|
+
``GH_REPO=`` prefix on one of them is kept.
|
|
132
134
|
|
|
133
135
|
Args:
|
|
134
136
|
command: The shell command text.
|
|
@@ -142,7 +144,9 @@ def all_placed_creates(command: str, folder: str | None) -> list[PlacedCreate]:
|
|
|
142
144
|
PlacedCreate(each_arguments, None, False, each_words)
|
|
143
145
|
for each_arguments, each_words in unplaced_creates(command, all_placed_arguments)
|
|
144
146
|
)
|
|
145
|
-
return list(
|
|
147
|
+
return list(
|
|
148
|
+
{(each_create, each_create.all_command_words): each_create for each_create in all_creates}.values()
|
|
149
|
+
)
|
|
146
150
|
|
|
147
151
|
|
|
148
152
|
def _walk_text(walk: _Walk, text: str, state: FolderState, depth: int) -> FolderState:
|
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import os
|
|
6
6
|
import sys
|
|
7
7
|
import tempfile
|
|
8
|
-
from dataclasses import dataclass
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
|
|
11
11
|
hooks_directory = str(Path(__file__).resolve().parent.parent.parent)
|
|
@@ -100,12 +100,15 @@ class ShellOpenCall:
|
|
|
100
100
|
folder: The folder in effect at the create, after each folder change before it.
|
|
101
101
|
is_folder_followed: False when a folder change before it could not be followed,
|
|
102
102
|
or when the create names no repository in a command that sets ``GH_REPO``.
|
|
103
|
+
is_prefix_origin_check: True for the origin call a create gets when only a
|
|
104
|
+
``GH_REPO=`` prefix names its repository. Equal calls compare equal either way.
|
|
103
105
|
"""
|
|
104
106
|
|
|
105
107
|
repository: tuple[str, str] | None
|
|
106
108
|
branch: str
|
|
107
109
|
folder: str | None
|
|
108
110
|
is_folder_followed: bool = True
|
|
111
|
+
is_prefix_origin_check: bool = field(default=False, compare=False)
|
|
109
112
|
|
|
110
113
|
|
|
111
114
|
OpenCall = ToolOpenCall | ShellOpenCall
|
|
@@ -121,6 +124,12 @@ def open_calls_from(all_payload_fields: dict[str, object]) -> list[OpenCall]:
|
|
|
121
124
|
gh pr merge 12 -> []
|
|
122
125
|
|
|
123
126
|
Each walked create gives one call per head it names, in its own folder.
|
|
127
|
+
A create whose repository comes only from a ``GH_REPO=`` prefix also gives
|
|
128
|
+
one origin call per head, since a wrapper such as ``env -u GH_REPO`` can
|
|
129
|
+
drop the prefix before gh runs::
|
|
130
|
+
|
|
131
|
+
GH_REPO=o/r env -u GH_REPO gh pr create -> [ShellOpenCall o/r, ShellOpenCall origin]
|
|
132
|
+
|
|
124
133
|
The pattern reading of the first create adds one call no walk call equals.
|
|
125
134
|
|
|
126
135
|
Args:
|
|
@@ -212,17 +221,33 @@ def _shell_open_calls(command: str, folder: str | None) -> list[OpenCall]:
|
|
|
212
221
|
|
|
213
222
|
|
|
214
223
|
def _placed_open_calls(placed_create: PlacedCreate, is_gh_repository_set: bool) -> list[ShellOpenCall]:
|
|
215
|
-
|
|
224
|
+
flag_value = _flag_value(placed_create)
|
|
225
|
+
prefix_value = prefix_repository(list(placed_create.all_command_words)) if flag_value is None else None
|
|
226
|
+
repository_value = flag_value if flag_value is not None else prefix_value
|
|
227
|
+
repository = named_repository(repository_value) if repository_value is not None else None
|
|
216
228
|
is_repository_read = repository is not None or not is_gh_repository_set
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
229
|
+
all_calls: list[ShellOpenCall] = []
|
|
230
|
+
for each_head in _placed_heads(placed_create) or [CURRENT_HEAD_REFERENCE]:
|
|
231
|
+
all_calls.append(
|
|
232
|
+
ShellOpenCall(
|
|
233
|
+
repository=repository,
|
|
234
|
+
branch=each_head,
|
|
235
|
+
folder=placed_create.folder,
|
|
236
|
+
is_folder_followed=placed_create.is_folder_followed and is_repository_read,
|
|
237
|
+
is_prefix_origin_check=prefix_value is not None and repository is None,
|
|
238
|
+
)
|
|
223
239
|
)
|
|
224
|
-
|
|
225
|
-
|
|
240
|
+
if prefix_value is not None:
|
|
241
|
+
all_calls.append(
|
|
242
|
+
ShellOpenCall(
|
|
243
|
+
repository=None,
|
|
244
|
+
branch=each_head,
|
|
245
|
+
folder=placed_create.folder,
|
|
246
|
+
is_folder_followed=placed_create.is_folder_followed,
|
|
247
|
+
is_prefix_origin_check=True,
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
return all_calls
|
|
226
251
|
|
|
227
252
|
|
|
228
253
|
def _option_words(placed_create: PlacedCreate) -> list[str]:
|
|
@@ -230,10 +255,9 @@ def _option_words(placed_create: PlacedCreate) -> list[str]:
|
|
|
230
255
|
return all_gh_words if program == GH_PROGRAM_NAME else list(placed_create.all_arguments)
|
|
231
256
|
|
|
232
257
|
|
|
233
|
-
def
|
|
258
|
+
def _flag_value(placed_create: PlacedCreate) -> str | None:
|
|
234
259
|
all_values = all_option_values(_option_words(placed_create), LONG_REPOSITORY_OPTION, REPOSITORY_SHORT_LETTER)
|
|
235
|
-
|
|
236
|
-
return named_repository(flag_value) if flag_value is not None else None
|
|
260
|
+
return all_values[-1] if all_values else None
|
|
237
261
|
|
|
238
262
|
|
|
239
263
|
def _placed_heads(placed_create: PlacedCreate) -> list[str]:
|
|
@@ -249,8 +273,9 @@ def _floor_open_call(command: str, folder: str | None) -> ShellOpenCall | None:
|
|
|
249
273
|
|
|
250
274
|
GH_REPO=o/r env -u GH_REPO gh pr create --fill -> origin, HEAD
|
|
251
275
|
|
|
252
|
-
There gh opens in origin
|
|
253
|
-
|
|
276
|
+
There gh opens in origin. The walk also asks for a clean pass on origin
|
|
277
|
+
for every create a ``GH_REPO=`` prefix names, so this call repeats that
|
|
278
|
+
check for the first create.
|
|
254
279
|
"""
|
|
255
280
|
options_only = CREATE_TEXT_OPTION_PATTERN.sub(" ", command)
|
|
256
281
|
create_match = GH_PR_CREATE_PATTERN.search(
|