claude-dev-env 8.119.1 → 8.119.2

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 (29) hide show
  1. package/hooks/blocking/followup_pr_dedupe.py +71 -13
  2. package/hooks/blocking/merge_review_gate.py +4 -0
  3. package/hooks/blocking/merge_review_gate_parts/create_folders.py +6 -2
  4. package/hooks/blocking/merge_review_gate_parts/open_request.py +40 -15
  5. package/hooks/blocking/resolve_thread_reply_gate.py +116 -1
  6. package/hooks/blocking/test_cloud_graphql_gate.py +4 -0
  7. package/hooks/blocking/test_followup_pr_dedupe.py +99 -0
  8. package/hooks/blocking/test_gh_global_account_switch_gate.py +4 -0
  9. package/hooks/blocking/test_headless_claude_broker_gate.py +4 -0
  10. package/hooks/blocking/test_merge_review_gate.py +133 -0
  11. package/hooks/blocking/test_python_windows_invocation_gate.py +4 -0
  12. package/hooks/blocking/test_resolve_thread_reply_gate.py +430 -0
  13. package/hooks/blocking/tests/test_create_folders.py +7 -0
  14. package/hooks/blocking/tests/test_open_request.py +21 -0
  15. package/hooks/features/conduct-gates.md +3 -3
  16. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +5 -0
  17. package/hooks/hooks_constants/merge_review_gate_constants.py +5 -0
  18. package/hooks/hooks_constants/pytest_invocation.py +24 -9
  19. package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +21 -1
  20. package/hooks/hooks_constants/test_pytest_invocation.py +25 -0
  21. package/hooks/hooks_constants/test_shell_command_mutation.py +11 -0
  22. package/hooks/hooks_constants/transcript_skill_scan_constants.py +22 -0
  23. package/hooks/session/test_npx_pele_guidance.py +4 -0
  24. package/hooks/test_transcript_skill_scan.py +72 -0
  25. package/hooks/transcript_skill_scan.py +138 -4
  26. package/hooks/validators/config/security_checks_constants.py +88 -0
  27. package/hooks/validators/security_checks.py +186 -1
  28. package/hooks/validators/test_security_checks.py +149 -0
  29. 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 not None:
439
- repository = named_repository(repository_value)
440
- else:
441
- remote_url = _origin_remote_url(working_directory)
442
- if remote_url is None:
443
- return UnreadOriginDraft(body)
444
- repository = _github_repository(remote_url)
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
- _arguments_draft(all_arguments, working_directory, is_gh_repository_set)
498
- for all_arguments in all_create_argument_lists(command)
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
@@ -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(dict.fromkeys(all_creates))
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
- repository = _placed_repository(placed_create)
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
- return [
218
- ShellOpenCall(
219
- repository=repository,
220
- branch=each_head,
221
- folder=placed_create.folder,
222
- is_folder_followed=placed_create.is_folder_followed and is_repository_read,
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
- for each_head in _placed_heads(placed_create) or [CURRENT_HEAD_REFERENCE]
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 _placed_repository(placed_create: PlacedCreate) -> tuple[str, str] | None:
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
- flag_value = all_values[-1] if all_values else prefix_repository(list(placed_create.all_command_words))
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 and the walk reads ``o/r``, so this call is the
253
- one that asks for a clean pass on origin.
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(
@@ -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
 
@@ -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"))
@@ -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
  [