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
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import json
6
+ import shlex
6
7
  import sys
7
8
  from collections.abc import Iterable, Iterator
8
9
  from pathlib import Path
@@ -15,16 +16,28 @@ from blocking.pr_lifecycle_skill_gate import deny_decision, transcript_paths
15
16
  from hooks_constants.pr_lifecycle_skill_gate_constants import SHELL_TOOL_NAMES
16
17
  from hooks_constants.pre_tool_use_stdin import print_decision_from_stdin
17
18
  from hooks_constants.resolve_thread_reply_gate_constants import (
19
+ ALL_REPLY_SCRIPT_FLAG_OPTIONS,
20
+ ALL_REPLY_SCRIPT_VALUE_OPTIONS,
21
+ ABSOLUTE_LAUNCHER_FOLDER_PATTERN,
18
22
  ALL_REPLY_TOOL_NAME_SUFFIXES,
23
+ REPLY_SCRIPT_LAUNCHER_NAME_PATTERN,
19
24
  ALL_THREAD_ID_INPUT_KEYS,
20
25
  COMMENT_ANCHOR_PATTERN,
21
26
  COMMENT_ID_INPUT_KEY,
22
27
  MISSING_REPLY_REASON,
28
+ REPLY_SCRIPT_COMMENT_ID_OPTION,
29
+ REPLY_SCRIPT_FILE_NAME,
30
+ REPLY_SCRIPT_REQUEST_SUBCOMMAND,
31
+ REPLY_SCRIPT_THREAD_ID_OPTION,
23
32
  RESOLVE_TOOL_NAME_PATTERN,
24
33
  RESOLVE_THREAD_METHOD,
25
34
  REVIEW_WRITE_METHOD_KEY,
26
35
  REVIEW_WRITE_TOOL_NAME_PATTERN,
27
36
  SHELL_COMMAND_KEY,
37
+ SHELL_OPTION_PREFIX,
38
+ SHELL_OPTION_VALUE_SEPARATOR,
39
+ SHELL_PATH_SEPARATOR,
40
+ SHELL_UNREADABLE_CHARACTER_PATTERN,
28
41
  SHELL_REPLY_COMMAND_PATTERN,
29
42
  SHELL_REPLY_COMMENT_ID_PATTERN,
30
43
  SHELL_RESOLVE_COMMAND_PATTERN,
@@ -100,15 +113,114 @@ def is_reply_call(tool_call: TranscriptToolCall) -> bool:
100
113
  mcp__github__add_reply_to_pull_request_comment -> True
101
114
  Bash "gh api repos/o/r/pulls/7/comments/99/replies -f body=x" -> True
102
115
  Bash "gh api graphql" with addPullRequestReviewThreadReply -> True
116
+ Bash "python review_reply_post.py request --comment-id 99" -> True
117
+ Bash "python review_reply_post.py post --request r.json" -> False
103
118
  mcp__github__add_issue_comment -> False
104
119
 
120
+ The reply script exits 0 only once its reply is on the thread, so one
121
+ plain call of it counts when its result, or its background task notice,
122
+ shows no error. ``reply_script_targets`` gives the forms that count.
123
+
105
124
  Args:
106
125
  tool_call: One tool call read from the transcript.
107
126
  """
108
127
  if tool_call.name.endswith(ALL_REPLY_TOOL_NAME_SUFFIXES):
109
128
  return True
110
129
  command = _shell_command(tool_call.name, tool_call.tool_input)
111
- return command is not None and SHELL_REPLY_COMMAND_PATTERN.search(command) is not None
130
+ return command is not None and (
131
+ SHELL_REPLY_COMMAND_PATTERN.search(command) is not None or reply_script_targets(command) is not None
132
+ )
133
+
134
+
135
+ def _is_reply_script_launcher(word: str) -> bool:
136
+ folder, separator, name = word.rpartition(SHELL_PATH_SEPARATOR)
137
+ return REPLY_SCRIPT_LAUNCHER_NAME_PATTERN.fullmatch(name) is not None and (
138
+ not separator or ABSOLUTE_LAUNCHER_FOLDER_PATTERN.match(folder + separator) is not None
139
+ )
140
+
141
+
142
+ def _reply_script_request_words(command: str) -> list[str] | None:
143
+ if SHELL_UNREADABLE_CHARACTER_PATTERN.search(command) is not None:
144
+ return None
145
+ try:
146
+ all_words = shlex.split(command)
147
+ except ValueError:
148
+ return None
149
+ if all_words and _is_reply_script_launcher(all_words[0]):
150
+ all_words = all_words[1:]
151
+ if not all_words or all_words[0].rpartition(SHELL_PATH_SEPARATOR)[2] != REPLY_SCRIPT_FILE_NAME:
152
+ return None
153
+ all_script_words = all_words[1:]
154
+ if not all_script_words or all_script_words[0] != REPLY_SCRIPT_REQUEST_SUBCOMMAND:
155
+ return None
156
+ return all_script_words[1:]
157
+
158
+
159
+ def _reply_script_option_values(all_request_words: list[str]) -> dict[str, str] | None:
160
+ value_by_option: dict[str, str] = {}
161
+ all_pending_words = list(reversed(all_request_words))
162
+ while all_pending_words:
163
+ option, separator, attached_value = all_pending_words.pop().partition(SHELL_OPTION_VALUE_SEPARATOR)
164
+ if option in value_by_option:
165
+ return None
166
+ if option in ALL_REPLY_SCRIPT_FLAG_OPTIONS and not separator:
167
+ value_by_option[option] = ""
168
+ continue
169
+ if option not in ALL_REPLY_SCRIPT_VALUE_OPTIONS:
170
+ return None
171
+ option_value = attached_value if separator else (all_pending_words.pop() if all_pending_words else "")
172
+ if not option_value or option_value.startswith(SHELL_OPTION_PREFIX):
173
+ return None
174
+ value_by_option[option] = option_value
175
+ return value_by_option
176
+
177
+
178
+ def reply_script_targets(command: str) -> tuple[frozenset[str], frozenset[str]] | None:
179
+ """Return the thread ids and comment ids one reply script call answers, or None for any other command.
180
+
181
+ ::
182
+
183
+ python ci/review_reply_post.py request --pull 7 --comment-id 9 --thread-id PRRT_a -> ({"PRRT_a"}, {"9"})
184
+ /usr/bin/python3 review_reply_post.py request --comment-id=9 --supersede -> ({}, {"9"})
185
+ python review_reply_post.py request --comment-id 9 || true -> None
186
+ python review_reply_post.py request --comment-id 9 --he -> None
187
+ python review_reply_post.py request --comment-id 9 --comment 5 -> None
188
+ python review_reply_post.py request --comment-id 9 --comment-id 5 -> None
189
+ python review_reply_post.py request --comment-id 9 --hel{p,p} -> None
190
+ ./python review_reply_post.py request --comment-id 9 -> None
191
+ python -u review_reply_post.py request --comment-id 9 -> None
192
+ python review_reply_post.py post --request r.json -> None
193
+ echo review_reply_post.py request --comment-id 9 -> None
194
+
195
+ The command must be one plain call: an optional launcher, the script, then
196
+ ``request``. The launcher is ``python``, ``python3``, ``python3.<n>`` or
197
+ ``py``, with or without ``.exe``, bare or by an absolute path, which
198
+ starts with ``/`` or with a drive letter and ``:/``. The script's parser accepts any prefix of an option
199
+ name, so every word after ``request`` must be an option the gate knows by
200
+ its full name, each given once, with its value: ``--pull``,
201
+ ``--comment-id``, ``--thread-id``, ``--body-file``, ``--wait-seconds``, or
202
+ the bare ``--supersede``. A value that starts with ``-`` is refused. A
203
+ command that holds ``;``, ``&``, ``|``, a redirect, a subshell, a
204
+ substitution, a variable, a ``#``, a brace, a glob, ``~``, ``!``, a
205
+ backslash or a line break is no reply, since the words bash runs, or the
206
+ exit code it reports, can differ from the text.
207
+
208
+ Args:
209
+ command: The shell command text.
210
+ """
211
+ all_request_words = _reply_script_request_words(command)
212
+ value_by_option = None if all_request_words is None else _reply_script_option_values(all_request_words)
213
+ if value_by_option is None:
214
+ return None
215
+ comment_id = value_by_option.get(REPLY_SCRIPT_COMMENT_ID_OPTION, "")
216
+ if not (comment_id.isascii() and comment_id.isdigit()):
217
+ return None
218
+ thread_id = value_by_option.get(REPLY_SCRIPT_THREAD_ID_OPTION)
219
+ if thread_id is None:
220
+ return frozenset(), frozenset({comment_id})
221
+ if THREAD_ID_PATTERN.fullmatch(thread_id) is None:
222
+ return None
223
+ return frozenset({thread_id}), frozenset({comment_id})
112
224
 
113
225
 
114
226
  def _reply_comment_ids(all_tool_input_fields: dict[str, object]) -> frozenset[str]:
@@ -124,6 +236,9 @@ def _reply_targets(tool_call: TranscriptToolCall) -> tuple[frozenset[str], froze
124
236
  command = _shell_command(tool_call.name, tool_call.tool_input)
125
237
  if command is None:
126
238
  return frozenset(), _reply_comment_ids(tool_call.tool_input)
239
+ script_targets = reply_script_targets(command)
240
+ if script_targets is not None:
241
+ return script_targets
127
242
  return frozenset(THREAD_ID_PATTERN.findall(command)), frozenset(SHELL_REPLY_COMMENT_ID_PATTERN.findall(command))
128
243
 
129
244
 
@@ -3,8 +3,9 @@
3
3
  A Bash tool call starts one interpreter, the dispatcher's own. Each roster
4
4
  entry then runs in that process through runpy, so the roster's exact content is
5
5
  the chain's cost after that single start. The roster holds the MSYS rewriter,
6
- the headless Claude broker gate, and the gh global account gate; any other hook
7
- added here fails this test.
6
+ the headless Claude broker gate, the gh global account gate, the gh API budget
7
+ gate, the cloud GraphQL gate, the Python Windows invocation gate, and the
8
+ transcript root search gate; any other hook added here fails this test.
8
9
  """
9
10
 
10
11
  import sys
@@ -22,7 +23,7 @@ from hooks_constants.bash_pre_tool_use_dispatcher_constants import (
22
23
  )
23
24
 
24
25
 
25
- def test_bash_roster_starts_only_the_msys_rewriter_and_the_five_gates() -> None:
26
+ def test_bash_roster_starts_only_the_msys_rewriter_and_the_six_gates() -> None:
26
27
  assert ALL_BASH_HOSTED_HOOK_ENTRIES == (
27
28
  BashHostedHookEntry(
28
29
  script_relative_path="blocking/msys_rev_path_rewriter.py",
@@ -38,6 +39,10 @@ def test_bash_roster_starts_only_the_msys_rewriter_and_the_five_gates() -> None:
38
39
  applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
39
40
  replaced_by_plugin_name="shell-guards",
40
41
  ),
42
+ BashHostedHookEntry(
43
+ script_relative_path="blocking/gh_api_budget_gate.py",
44
+ applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
45
+ ),
41
46
  BashHostedHookEntry(
42
47
  script_relative_path="blocking/cloud_graphql_gate.py",
43
48
  applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
@@ -274,6 +274,7 @@ def test_should_drop_the_gates_the_shell_guards_mod_holds_while_it_is_on(
274
274
  all_script_paths = [each_entry.script_relative_path for each_entry in select_applicable_entries(BASH_TOOL_NAME)]
275
275
  assert all_script_paths == [
276
276
  "blocking/msys_rev_path_rewriter.py",
277
+ "blocking/gh_api_budget_gate.py",
277
278
  "blocking/cloud_graphql_gate.py",
278
279
  "blocking/python_windows_invocation_gate.py",
279
280
  "blocking/transcript_root_search_gate.py",
@@ -116,6 +116,10 @@ def test_should_detect_a_github_graphql_call(command: str) -> None:
116
116
  assert gate.calls_github_graphql(command) is True
117
117
 
118
118
 
119
+ def test_should_detect_a_graphql_call_behind_a_launcher_and_env_unset() -> None:
120
+ assert gate.calls_github_graphql("nohup env -u FOO gh api graphql -f query=x") is True
121
+
122
+
119
123
  @pytest.mark.parametrize(
120
124
  "command",
121
125
  [
@@ -536,6 +536,105 @@ def test_gh_repo_environment_should_name_the_repository_when_the_origin_is_unrea
536
536
  assert all_asked_repositories == [("o", "r")]
537
537
 
538
538
 
539
+ @pytest.mark.parametrize(
540
+ "command",
541
+ [
542
+ 'GH_REPO=o/r env -u GH_REPO gh pr create --title "fix: a" --body "Follow-up to #7"',
543
+ 'GH_REPO=o/r env -i gh pr create --title "fix: a" --body "Follow-up to #7"',
544
+ 'GH_REPO=o/r nohup env -u GH_REPO gh pr create --title "fix: a" --body "Follow-up to #7"',
545
+ 'GH_REPO=o/r nice -n 5 env -u GH_REPO stdbuf -o0 gh pr create --title "fix: a" --body "Follow-up to #7"',
546
+ 'gh pr create --repo o/r --title "fix: a" --body "Follow-up to #7" ; '
547
+ 'GH_REPO=o/r gh pr create --title "fix: a" --body "Follow-up to #7"',
548
+ ],
549
+ )
550
+ def test_should_find_an_origin_duplicate_for_a_create_behind_a_gh_repo_prefix(
551
+ tmp_path: Path, command: str
552
+ ) -> None:
553
+ checkout_path = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
554
+ open_followup = _open_followup(9, 7)
555
+ all_asked_repositories: list[tuple[str, str]] = []
556
+ payload = {"tool_name": "Bash", "cwd": str(checkout_path), "tool_input": {"command": command}}
557
+ reader = _keyed_reader({("o", "r"): [], ("acme", "tools"): [open_followup]}, all_asked_repositories)
558
+ reason = dedupe.duplicate_followup_reason(payload, reader)
559
+ assert reason is not None
560
+ assert _duplicate_reason(open_followup, 7) in reason
561
+ assert ("acme", "tools") in all_asked_repositories
562
+
563
+
564
+ def test_should_tell_a_create_behind_a_gh_repo_prefix_with_an_origin_duplicate_to_name_its_repository_with_the_repo_flag(
565
+ tmp_path: Path,
566
+ ) -> None:
567
+ checkout_path = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
568
+ command = 'GH_REPO=acme/other env -u GH_REPO gh pr create --title "fix: a" --body "Follow-up to #7"'
569
+ payload = {"tool_name": "Bash", "cwd": str(checkout_path), "tool_input": {"command": command}}
570
+ reader = _keyed_reader({("acme", "other"): [], ("acme", "tools"): [_open_followup(9, 7)]}, [])
571
+ reason = dedupe.duplicate_followup_reason(payload, reader)
572
+ assert reason is not None
573
+ assert "GH_REPO=" in reason
574
+ assert "--repo owner/name" in reason
575
+
576
+
577
+ @pytest.mark.parametrize(
578
+ "command",
579
+ [
580
+ 'GH_REPO=nonsense env -u GH_REPO gh pr create --title "fix: a" --body "Follow-up to #7"',
581
+ 'GH_REPO=nonsense gh pr create --title "fix: a" --body "Follow-up to #7"',
582
+ 'gh pr create --repo o/r --title x --body y ; GH_REPO=nonsense env -u GH_REPO gh pr create --title "fix: a" --body "Follow-up to #7"',
583
+ ],
584
+ )
585
+ def test_should_find_an_origin_duplicate_for_a_create_behind_a_gh_repo_prefix_the_gate_cannot_parse(
586
+ tmp_path: Path, command: str
587
+ ) -> None:
588
+ checkout_path = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
589
+ open_followup = _open_followup(9, 7)
590
+ all_asked_repositories: list[tuple[str, str]] = []
591
+ payload = {"tool_name": "Bash", "cwd": str(checkout_path), "tool_input": {"command": command}}
592
+ reader = _keyed_reader({("o", "r"): [], ("acme", "tools"): [open_followup]}, all_asked_repositories)
593
+ reason = dedupe.duplicate_followup_reason(payload, reader)
594
+ assert reason is not None
595
+ assert _duplicate_reason(open_followup, 7) in reason
596
+ assert ("acme", "tools") in all_asked_repositories
597
+
598
+
599
+ def test_should_lead_the_origin_duplicate_reason_for_a_gh_repo_prefix_with_the_repo_flag_way_out(
600
+ tmp_path: Path,
601
+ ) -> None:
602
+ checkout_path = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
603
+ open_followup = _open_followup(9, 7)
604
+ command = 'GH_REPO=acme/other env -u GH_REPO gh pr create --title "fix: a" --body "Follow-up to #7"'
605
+ payload = {"tool_name": "Bash", "cwd": str(checkout_path), "tool_input": {"command": command}}
606
+ reader = _keyed_reader({("acme", "other"): [], ("acme", "tools"): [open_followup]}, [])
607
+ reason = dedupe.duplicate_followup_reason(payload, reader)
608
+ assert reason is not None
609
+ assert reason.index("--repo owner/name") < reason.index(_duplicate_reason(open_followup, 7))
610
+
611
+
612
+ def test_should_allow_a_create_that_names_acme_other_with_its_own_repo_flag_when_only_origin_holds_the_followup(
613
+ tmp_path: Path,
614
+ ) -> None:
615
+ checkout_path = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
616
+ command = 'GH_REPO=acme/other gh pr create --repo acme/other --title "fix: a" --body "Follow-up to #7"'
617
+ payload = {"tool_name": "Bash", "cwd": str(checkout_path), "tool_input": {"command": command}}
618
+ all_asked_repositories: list[tuple[str, str]] = []
619
+ reader = _keyed_reader(
620
+ {("acme", "other"): [], ("acme", "tools"): [_open_followup(9, 7)]}, all_asked_repositories
621
+ )
622
+ assert dedupe.duplicate_followup_reason(payload, reader) is None
623
+ assert all_asked_repositories == [("acme", "other")]
624
+
625
+
626
+ def test_should_allow_a_create_that_names_its_repository_with_its_own_repo_flag_when_origin_holds_a_duplicate(
627
+ tmp_path: Path,
628
+ ) -> None:
629
+ checkout_path = _checkout_with_origin(tmp_path, "https://github.com/acme/tools.git")
630
+ all_asked_repositories: list[tuple[str, str]] = []
631
+ command = 'gh pr create --repo o/r --title "fix: a" --body "Follow-up to #7"'
632
+ payload = {"tool_name": "Bash", "cwd": str(checkout_path), "tool_input": {"command": command}}
633
+ reader = _keyed_reader({("o", "r"): [], ("acme", "tools"): [_open_followup(9, 7)]}, all_asked_repositories)
634
+ assert dedupe.duplicate_followup_reason(payload, reader) is None
635
+ assert all_asked_repositories == [("o", "r")]
636
+
637
+
539
638
  def test_all_pull_request_argument_lists_should_list_the_named_action_only() -> None:
540
639
  command = "gh pr edit 7 --body x && python pull_request.py edit --number 8 && gh pr create --body y"
541
640
  all_argument_lists = list(dedupe.all_pull_request_argument_lists(command, ["pr", "edit"], "edit"))