claude-dev-env 8.117.2 → 8.117.4

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 (77) hide show
  1. package/_shared/pr-loop/scripts/pyproject.toml +1 -1
  2. package/_shared/process-tree/scripts/pyproject.toml +1 -1
  3. package/bin/cde.mjs +4 -3
  4. package/bin/cde.test.mjs +87 -1
  5. package/bin/codex-compat.mjs +4 -3
  6. package/bin/codex-compat.test.mjs +91 -1
  7. package/bin/install-plan.mjs +2 -1
  8. package/bin/install.mjs +24 -7
  9. package/bin/install.plan.test.mjs +15 -0
  10. package/bin/install.test.mjs +61 -0
  11. package/bin/python-probe.mjs +42 -0
  12. package/bin/python-probe.test.mjs +19 -2
  13. package/docs/rule-guides/features-start-with-an-eval.md +3 -3
  14. package/docs/rule-guides/proof-before-pull-request.md +2 -2
  15. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +11 -4
  16. package/hooks/blocking/build_eval_before_eval_write_gate.py +10 -4
  17. package/hooks/blocking/followup_pr_dedupe.py +381 -64
  18. package/hooks/blocking/gate_child_process.py +187 -0
  19. package/hooks/blocking/gate_run_reads.py +116 -35
  20. package/hooks/blocking/merge_review_gate.py +93 -10
  21. package/hooks/blocking/merge_review_gate_parts/create_folders.py +346 -0
  22. package/hooks/blocking/merge_review_gate_parts/create_options.py +86 -0
  23. package/hooks/blocking/merge_review_gate_parts/folder_state.py +107 -0
  24. package/hooks/blocking/merge_review_gate_parts/open_request.py +199 -64
  25. package/hooks/blocking/merge_review_gate_parts/quoted_lines.py +283 -0
  26. package/hooks/blocking/merge_review_gate_parts/wrapped_creates.py +126 -0
  27. package/hooks/blocking/pr_lifecycle_skill_gate.py +61 -18
  28. package/hooks/blocking/pre_tool_use_dispatcher.py +25 -3
  29. package/hooks/blocking/pull_request_proof.py +5 -51
  30. package/hooks/blocking/pull_request_proof_edit_gate.py +19 -5
  31. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +54 -0
  32. package/hooks/blocking/test_build_eval_before_eval_write_gate.py +64 -0
  33. package/hooks/blocking/test_followup_pr_dedupe.py +464 -1
  34. package/hooks/blocking/test_gate_child_process.py +64 -0
  35. package/hooks/blocking/test_gate_run_reads.py +364 -4
  36. package/hooks/blocking/test_merge_review_gate.py +832 -39
  37. package/hooks/blocking/test_pr_lifecycle_skill_gate.py +130 -4
  38. package/hooks/blocking/test_pre_tool_use_dispatcher.py +64 -0
  39. package/hooks/blocking/test_pull_request_proof.py +16 -0
  40. package/hooks/blocking/test_pull_request_proof_edit_gate.py +60 -0
  41. package/hooks/blocking/tests/test_create_folders.py +290 -0
  42. package/hooks/blocking/tests/test_create_options.py +34 -0
  43. package/hooks/blocking/tests/test_folder_state.py +40 -0
  44. package/hooks/blocking/tests/test_open_request.py +350 -44
  45. package/hooks/blocking/tests/test_quoted_lines.py +86 -0
  46. package/hooks/blocking/tests/test_wrapped_creates.py +42 -0
  47. package/hooks/features/bash-dispatchers.md +1 -1
  48. package/hooks/features/conduct-gates.md +7 -7
  49. package/hooks/features/write-edit-blocking.md +3 -3
  50. package/hooks/hooks_constants/build_eval_before_eval_write_gate_constants.py +5 -0
  51. package/hooks/hooks_constants/followup_pr_dedupe_constants.py +34 -1
  52. package/hooks/hooks_constants/gate_fault.py +52 -0
  53. package/hooks/hooks_constants/gate_fault_constants.py +13 -0
  54. package/hooks/hooks_constants/gate_run_reads_constants.py +9 -1
  55. package/hooks/hooks_constants/merge_review_gate_constants.py +94 -1
  56. package/hooks/hooks_constants/piped_pytest_blocker_constants.py +2 -0
  57. package/hooks/hooks_constants/pr_lifecycle_skill_gate_constants.py +6 -1
  58. package/hooks/hooks_constants/pre_tool_use_stdin.py +23 -3
  59. package/hooks/hooks_constants/shell_command_pipeline.py +68 -8
  60. package/hooks/hooks_constants/shell_command_segments.py +2 -2
  61. package/hooks/hooks_constants/shell_command_wrappers.py +3 -2
  62. package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +7 -0
  63. package/hooks/hooks_constants/test_gate_fault.py +41 -0
  64. package/hooks/hooks_constants/test_piped_pytest_blocker_constants.py +5 -0
  65. package/hooks/hooks_constants/test_pr_lifecycle_skill_gate_constants.py +10 -0
  66. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +97 -0
  67. package/hooks/hooks_constants/test_shell_command_pipeline.py +55 -0
  68. package/hooks/hooks_constants/test_shell_command_segments.py +15 -0
  69. package/hooks/hooks_constants/test_shell_command_wrappers.py +6 -0
  70. package/hooks/hooks_constants/transcript_skill_scan_constants.py +6 -0
  71. package/hooks/pyproject.toml +2 -3
  72. package/hooks/test_python_floor.py +197 -0
  73. package/hooks/test_transcript_skill_scan.py +324 -0
  74. package/hooks/transcript_skill_scan.py +233 -2
  75. package/hooks/validators/code_quality_checks.py +119 -17
  76. package/hooks/validators/test_code_quality_checks.py +110 -4
  77. package/package.json +1 -1
@@ -4,6 +4,7 @@ import io
4
4
  import os
5
5
  import subprocess
6
6
  import sys
7
+ from collections.abc import Callable
7
8
  from pathlib import Path
8
9
 
9
10
  import pytest
@@ -15,7 +16,12 @@ for each_directory in (HOOKS_DIRECTORY, SCRIPTS_DIRECTORY):
15
16
  sys.path.insert(0, str(each_directory))
16
17
 
17
18
  import merge_review_receipt
19
+ from blocking import gate_run_reads
18
20
  from blocking import merge_review_gate as gate
21
+ from blocking.gate_run_reads import read_deadlines_started
22
+ from blocking.merge_review_gate_parts import open_request
23
+ from hooks_constants.gate_run_reads_constants import READS_RAN_OUT_SENTENCE
24
+ from hooks_constants.merge_review_gate_constants import UNKNOWN_OPEN_TARGET_REASON
19
25
 
20
26
  CLEAN_HEAD = "abc1234def5678abc1234def5678abc1234def56"
21
27
  NEW_HEAD = "9999999aaaaaaa9999999aaaaaaa9999999aaaaa"
@@ -61,13 +67,19 @@ def _reason(payload: dict[str, object]) -> str | None:
61
67
  return decision["hookSpecificOutput"]["permissionDecisionReason"]
62
68
 
63
69
 
70
+ def _deny_reason(payload: dict[str, object]) -> str:
71
+ reason = _reason(payload)
72
+ assert reason is not None, "the gate allowed the call"
73
+ return reason
74
+
75
+
64
76
  def test_should_deny_a_merge_with_no_review() -> None:
65
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
77
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
66
78
 
67
79
 
68
80
  def test_should_deny_a_merge_whose_last_pass_found_findings() -> None:
69
81
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 2)
70
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
82
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
71
83
 
72
84
 
73
85
  def test_should_allow_a_merge_pinned_to_the_clean_head() -> None:
@@ -82,7 +94,7 @@ def test_should_allow_a_merge_whose_findings_are_all_ruled_out_with_a_link() ->
82
94
 
83
95
  def test_should_deny_a_merge_with_a_finding_left_open() -> None:
84
96
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 3, 2, RULINGS_LINK)
85
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
97
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
86
98
 
87
99
 
88
100
  @pytest.mark.parametrize(
@@ -115,7 +127,9 @@ def test_should_deny_a_merge_with_a_finding_left_open() -> None:
115
127
  {"head": CLEAN_HEAD, "findings": -1, "ruled_out": -1, "rulings": RULINGS_LINK},
116
128
  ],
117
129
  )
118
- def test_is_clean_receipt_should_reject_a_receipt_it_cannot_back(receipt: dict[str, object]) -> None:
130
+ def test_is_clean_receipt_should_reject_a_receipt_it_cannot_back(
131
+ receipt: dict[str, object],
132
+ ) -> None:
119
133
  assert gate.is_clean_receipt(receipt, "acme", "tools", 12) is False
120
134
 
121
135
 
@@ -123,28 +137,26 @@ def test_is_clean_receipt_should_accept_zero_findings() -> None:
123
137
  assert gate.is_clean_receipt({"head": CLEAN_HEAD, "findings": 0}, "acme", "tools", 12) is True
124
138
 
125
139
 
126
- @pytest.mark.parametrize(
127
- "rulings", ["x", "https://github.com/acme/tools/pull/13#issuecomment-1"]
128
- )
140
+ @pytest.mark.parametrize("rulings", ["x", "https://github.com/acme/tools/pull/13#issuecomment-1"])
129
141
  def test_should_deny_a_merge_whose_rulings_link_is_not_a_comment_on_this_pull_request(
130
142
  rulings: str,
131
143
  ) -> None:
132
144
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 2, 2, rulings)
133
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
145
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
134
146
 
135
147
 
136
148
  def test_should_name_the_ruled_out_route_in_the_deny_reason() -> None:
137
- assert "--ruled-out" in _reason(_merge_tool(CLEAN_HEAD))
149
+ assert "--ruled-out" in _deny_reason(_merge_tool(CLEAN_HEAD))
138
150
 
139
151
 
140
152
  def test_should_deny_a_merge_pinned_to_a_head_pushed_after_the_review() -> None:
141
153
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 0)
142
- assert "Review the new head" in _reason(_merge_tool(NEW_HEAD))
154
+ assert "Review the new head" in _deny_reason(_merge_tool(NEW_HEAD))
143
155
 
144
156
 
145
157
  def test_should_deny_an_unpinned_merge_after_a_clean_review() -> None:
146
158
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 0)
147
- assert "names no head" in _reason(_merge_tool())
159
+ assert "names no head" in _deny_reason(_merge_tool())
148
160
 
149
161
 
150
162
  def test_should_deny_auto_merge_that_names_no_head() -> None:
@@ -153,7 +165,7 @@ def test_should_deny_auto_merge_that_names_no_head() -> None:
153
165
  "tool_name": "mcp__github__enable_pr_auto_merge",
154
166
  "tool_input": {"owner": "acme", "repo": "tools", "pullNumber": 12},
155
167
  }
156
- assert f"--match-head-commit {CLEAN_HEAD}" in _reason(payload)
168
+ assert f"--match-head-commit {CLEAN_HEAD}" in _deny_reason(payload)
157
169
 
158
170
 
159
171
  def test_should_allow_any_merge_in_an_exempt_repository(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -169,13 +181,13 @@ def test_should_read_a_gh_pr_merge_pinned_with_a_short_head() -> None:
169
181
 
170
182
  def test_should_deny_gh_pr_merge_auto() -> None:
171
183
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 0)
172
- assert "Auto-merge" in _reason(
184
+ assert "Auto-merge" in _deny_reason(
173
185
  _bash("gh pr merge https://github.com/acme/tools/pull/12 --auto")
174
186
  )
175
187
 
176
188
 
177
189
  def test_should_deny_gh_pr_merge_with_no_repository() -> None:
178
- assert "names no repository" in _reason(_bash("gh pr merge 12 --squash"))
190
+ assert "names no repository" in _deny_reason(_bash("gh pr merge 12 --squash"))
179
191
 
180
192
 
181
193
  def test_should_read_a_gh_api_merge_with_a_sha_field() -> None:
@@ -229,7 +241,7 @@ def test_should_allow_a_merge_call_that_only_disables_auto_merge(command: str) -
229
241
  ],
230
242
  )
231
243
  def test_should_check_a_merge_chained_with_a_disable_auto_call(command: str) -> None:
232
- assert "no clean review" in _reason(_bash(command))
244
+ assert "no clean review" in _deny_reason(_bash(command))
233
245
 
234
246
 
235
247
  def test_should_ignore_commands_that_merge_nothing() -> None:
@@ -239,11 +251,11 @@ def test_should_ignore_commands_that_merge_nothing() -> None:
239
251
 
240
252
  def test_should_name_the_configured_review_command(monkeypatch: pytest.MonkeyPatch) -> None:
241
253
  monkeypatch.setenv("CLAUDE_MERGE_REVIEW_COMMAND", "/team-review")
242
- assert "`/team-review`" in _reason(_merge_tool(CLEAN_HEAD))
254
+ assert "`/team-review`" in _deny_reason(_merge_tool(CLEAN_HEAD))
243
255
 
244
256
 
245
257
  def test_should_name_the_low_fix_pass_after_three_full_reviews_in_the_deny_reason() -> None:
246
- reason = _reason(_merge_tool(CLEAN_HEAD))
258
+ reason = _deny_reason(_merge_tool(CLEAN_HEAD))
247
259
  assert "three full passes" in reason
248
260
  assert "`/code-review low --fix 12`" in reason
249
261
 
@@ -256,13 +268,13 @@ def test_should_allow_auto_merge_pinned_to_the_clean_head() -> None:
256
268
 
257
269
  def test_should_deny_pinned_auto_merge_with_no_clean_review() -> None:
258
270
  command = f"gh pr merge 12 --repo acme/tools --auto --match-head-commit {CLEAN_HEAD}"
259
- assert "no clean review" in _reason(_bash(command))
271
+ assert "no clean review" in _deny_reason(_bash(command))
260
272
 
261
273
 
262
274
  def test_should_deny_auto_merge_pinned_to_a_newer_head() -> None:
263
275
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 0)
264
276
  command = f"gh pr merge 12 --repo acme/tools --auto --match-head-commit {NEW_HEAD}"
265
- assert "Review the new head" in _reason(_bash(command))
277
+ assert "Review the new head" in _deny_reason(_bash(command))
266
278
 
267
279
 
268
280
  def _git(folder: Path, *all_arguments: str) -> str:
@@ -349,7 +361,7 @@ def _bash_in(folder: Path, command: str) -> dict[str, object]:
349
361
 
350
362
  def test_should_deny_a_pull_request_open_with_no_review(branch_checkout: tuple[Path, str]) -> None:
351
363
  folder, head = branch_checkout
352
- reason = _reason(_create_tool(folder))
364
+ reason = _deny_reason(_create_tool(folder))
353
365
  assert f"no clean review for head {head}" in reason
354
366
  assert f"acme/tools open {head} 0" in reason
355
367
  assert "--ruled-out" in reason
@@ -369,7 +381,7 @@ def test_should_deny_a_pull_request_open_whose_rulings_leave_a_finding_unproven(
369
381
  ) -> None:
370
382
  folder, head = ruled_checkout
371
383
  _record_open_pass(head, 3, 2, "a.py:1,b.py:2")
372
- assert "no clean review" in _reason(_create_tool(folder))
384
+ assert "no clean review" in _deny_reason(_create_tool(folder))
373
385
 
374
386
 
375
387
  def test_should_allow_a_pull_request_open_reviewed_clean_at_its_head(
@@ -385,7 +397,7 @@ def test_should_deny_a_pull_request_open_whose_pass_found_findings(
385
397
  ) -> None:
386
398
  folder, head = branch_checkout
387
399
  merge_review_receipt.record_open_review("acme/tools", head, 2)
388
- assert "no clean review" in _reason(_create_tool(folder))
400
+ assert "no clean review" in _deny_reason(_create_tool(folder))
389
401
 
390
402
 
391
403
  def test_should_deny_a_pull_request_open_after_a_push_past_the_review(
@@ -393,13 +405,13 @@ def test_should_deny_a_pull_request_open_after_a_push_past_the_review(
393
405
  ) -> None:
394
406
  folder, _head = branch_checkout
395
407
  merge_review_receipt.record_open_review("acme/tools", NEW_HEAD, 0)
396
- assert "no clean review" in _reason(_create_tool(folder))
408
+ assert "no clean review" in _deny_reason(_create_tool(folder))
397
409
 
398
410
 
399
411
  def test_should_read_gh_pr_create_with_repo_and_head(branch_checkout: tuple[Path, str]) -> None:
400
412
  folder, head = branch_checkout
401
413
  command = "gh pr create --repo acme/tools --head feature --title t --body b"
402
- assert "no clean review" in _reason(_bash_in(folder, command))
414
+ assert "no clean review" in _deny_reason(_bash_in(folder, command))
403
415
  merge_review_receipt.record_open_review("acme/tools", head, 0)
404
416
  assert _reason(_bash_in(folder, command)) is None
405
417
 
@@ -413,11 +425,11 @@ def test_should_read_gh_pr_create_repository_from_origin(branch_checkout: tuple[
413
425
  def test_should_read_the_pull_request_script_create(branch_checkout: tuple[Path, str]) -> None:
414
426
  folder, head = branch_checkout
415
427
  command = "python scripts/pull_request.py create --repo acme/tools --head feature"
416
- assert f"head {head}" in _reason(_bash_in(folder, command))
428
+ assert f"head {head}" in _deny_reason(_bash_in(folder, command))
417
429
 
418
430
 
419
431
  def test_should_deny_a_pull_request_open_whose_head_cannot_be_read(tmp_path: Path) -> None:
420
- reason = _reason(_create_tool(tmp_path, head="missing-branch"))
432
+ reason = _deny_reason(_create_tool(tmp_path, head="missing-branch"))
421
433
  assert "names no repository or head commit" in reason
422
434
 
423
435
 
@@ -427,7 +439,7 @@ def test_should_read_the_pushed_head_of_a_branch_outside_any_checkout(
427
439
  _folder, head = branch_checkout
428
440
  outside = tmp_path / "outside"
429
441
  outside.mkdir()
430
- assert f"no clean review for head {head}" in _reason(_create_tool(outside))
442
+ assert f"no clean review for head {head}" in _deny_reason(_create_tool(outside))
431
443
  merge_review_receipt.record_open_review("acme/tools", head, 0)
432
444
  assert _reason(_create_tool(outside)) is None
433
445
 
@@ -462,22 +474,22 @@ def test_should_deny_a_merge_pinned_to_a_head_whose_open_pass_only_ruled_finding
462
474
  ) -> None:
463
475
  _folder, head = ruled_checkout
464
476
  _record_open_pass(head, 2, 2, "a.py:1,b.py:2")
465
- assert "no clean review" in _reason(_merge_tool(head))
477
+ assert "no clean review" in _deny_reason(_merge_tool(head))
466
478
 
467
479
 
468
480
  def test_should_deny_a_merge_pinned_past_the_head_reviewed_before_open() -> None:
469
481
  merge_review_receipt.record_open_review("acme/tools", CLEAN_HEAD, 0)
470
- assert "no clean review" in _reason(_merge_tool(NEW_HEAD))
482
+ assert "no clean review" in _deny_reason(_merge_tool(NEW_HEAD))
471
483
 
472
484
 
473
485
  def test_should_deny_an_unpinned_merge_with_only_an_open_review() -> None:
474
486
  merge_review_receipt.record_open_review("acme/tools", CLEAN_HEAD, 0)
475
- assert "no clean review" in _reason(_merge_tool())
487
+ assert "no clean review" in _deny_reason(_merge_tool())
476
488
 
477
489
 
478
490
  def test_should_not_accept_an_open_review_from_another_repository() -> None:
479
491
  merge_review_receipt.record_open_review("acme/other", CLEAN_HEAD, 0)
480
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
492
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
481
493
 
482
494
 
483
495
  def test_should_accept_an_open_review_recorded_under_the_old_owner() -> None:
@@ -488,14 +500,16 @@ def test_should_accept_an_open_review_recorded_under_the_old_owner() -> None:
488
500
  def test_should_deny_a_merge_whose_numbered_pass_found_findings_at_the_open_reviewed_head() -> None:
489
501
  merge_review_receipt.record_open_review("acme/tools", CLEAN_HEAD, 0)
490
502
  merge_review_receipt.record_review("acme/tools", 12, CLEAN_HEAD, 2)
491
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
503
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
492
504
 
493
505
 
494
506
  def test_should_ignore_an_open_receipt_whose_head_is_not_a_commit(receipt_folder: Path) -> None:
495
507
  heads_folder = receipt_folder / "heads"
496
508
  heads_folder.mkdir()
497
- (heads_folder / "acme__tools__a.json").write_text('{"head": "a", "findings": 0}', encoding="utf-8")
498
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD))
509
+ (heads_folder / "acme__tools__a.json").write_text(
510
+ '{"head": "a", "findings": 0}', encoding="utf-8"
511
+ )
512
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD))
499
513
 
500
514
 
501
515
  def test_should_check_a_merge_chained_after_a_reviewed_open(
@@ -507,7 +521,7 @@ def test_should_check_a_merge_chained_after_a_reviewed_open(
507
521
  "gh pr create --repo acme/tools --head feature --fill && "
508
522
  f"gh pr merge 12 --repo acme/tools --match-head-commit {NEW_HEAD}"
509
523
  )
510
- assert "no clean review" in _reason(_bash_in(folder, command))
524
+ assert "no clean review" in _deny_reason(_bash_in(folder, command))
511
525
 
512
526
 
513
527
  def test_should_read_the_pushed_branch_before_the_local_one(
@@ -530,7 +544,7 @@ def test_should_read_the_pushed_branch_before_the_local_one(
530
544
  _git(folder, "push", "--quiet", str(github_repository), "HEAD:feature")
531
545
  _git(folder, "reset", "--quiet", "--hard", local_head)
532
546
  merge_review_receipt.record_open_review("acme/tools", local_head, 0)
533
- assert f"head {pushed_head}" in _reason(_create_tool(folder))
547
+ assert f"head {pushed_head}" in _deny_reason(_create_tool(folder))
534
548
 
535
549
 
536
550
  def test_should_read_the_fetched_branch_before_the_local_one_for_a_shell_open(
@@ -554,12 +568,12 @@ def test_should_read_the_fetched_branch_before_the_local_one_for_a_shell_open(
554
568
  _git(folder, "reset", "--quiet", "--hard", local_head)
555
569
  merge_review_receipt.record_open_review("acme/tools", local_head, 0)
556
570
  command = "gh pr create --repo acme/tools --head feature"
557
- assert f"head {fetched_head}" in _reason(_bash_in(folder, command))
571
+ assert f"head {fetched_head}" in _deny_reason(_bash_in(folder, command))
558
572
 
559
573
 
560
574
  def test_should_not_match_an_open_receipt_on_a_short_pinned_head() -> None:
561
575
  merge_review_receipt.record_open_review("acme/tools", CLEAN_HEAD, 0)
562
- assert "no clean review" in _reason(_merge_tool(CLEAN_HEAD[:3]))
576
+ assert "no clean review" in _deny_reason(_merge_tool(CLEAN_HEAD[:3]))
563
577
 
564
578
 
565
579
  def test_should_allow_an_open_in_an_exempt_repository_with_an_unread_head(
@@ -589,6 +603,785 @@ def test_should_read_the_head_in_the_folder_a_cd_moves_to(
589
603
  elsewhere = tmp_path / "elsewhere"
590
604
  elsewhere.mkdir()
591
605
  assert _reason(_bash_in(elsewhere, f'cd "{folder}" && gh pr create --fill')) is None
592
- assert "names no repository or head commit" in _reason(
606
+ assert "names no repository or head commit" in _deny_reason(
593
607
  _bash_in(folder, f'cd "{elsewhere}" && gh pr create --repo acme/tools --fill')
594
608
  )
609
+
610
+
611
+ @pytest.fixture
612
+ def two_branch_checkout(branch_checkout: tuple[Path, str]) -> tuple[Path, str, str]:
613
+ folder, first_head = branch_checkout
614
+ _git(folder, "checkout", "--quiet", "-b", "second")
615
+ _git(
616
+ folder,
617
+ "-c",
618
+ "user.name=t",
619
+ "-c",
620
+ "user.email=t@example.com",
621
+ "commit",
622
+ "--quiet",
623
+ "--allow-empty",
624
+ "-m",
625
+ "second",
626
+ )
627
+ return folder, first_head, _git(folder, "rev-parse", "HEAD")
628
+
629
+
630
+ def test_should_deny_the_second_chained_open_when_only_the_first_head_was_reviewed(
631
+ two_branch_checkout: tuple[Path, str, str],
632
+ ) -> None:
633
+ folder, first_head, second_head = two_branch_checkout
634
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
635
+ command = (
636
+ "gh pr create --repo acme/tools --head feature --fill && "
637
+ "gh pr create --repo acme/tools --head second --fill"
638
+ )
639
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
640
+
641
+
642
+ def test_should_deny_a_script_create_chained_after_a_reviewed_gh_create(
643
+ two_branch_checkout: tuple[Path, str, str],
644
+ ) -> None:
645
+ folder, first_head, second_head = two_branch_checkout
646
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
647
+ command = (
648
+ "gh pr create --repo acme/tools --head feature --fill; "
649
+ "python scripts/pull_request.py create --repo acme/tools --head second"
650
+ )
651
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
652
+
653
+
654
+ def test_should_deny_the_second_open_on_its_own_line_when_only_the_first_head_was_reviewed(
655
+ two_branch_checkout: tuple[Path, str, str],
656
+ ) -> None:
657
+ folder, first_head, second_head = two_branch_checkout
658
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
659
+ command = (
660
+ "gh pr create --repo acme/tools --head feature --fill\n"
661
+ "gh pr create --repo acme/tools --head second --fill"
662
+ )
663
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
664
+
665
+
666
+ def test_should_read_the_origin_once_for_creates_in_one_folder(
667
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
668
+ ) -> None:
669
+ all_started_commands: list[list[str]] = []
670
+
671
+ def answer_origin_and_head(all_command_words: list[str], *_all_arguments: object) -> str:
672
+ all_started_commands.append(all_command_words)
673
+ if "get-url" in all_command_words:
674
+ return "https://github.com/acme/tools.git\n"
675
+ return CLEAN_HEAD + "\n"
676
+
677
+ monkeypatch.setattr(open_request, "child_stdout", answer_origin_and_head)
678
+ merge_review_receipt.record_open_review("acme/tools", CLEAN_HEAD, 0)
679
+ command = "gh pr create --head one --fill && gh pr create --head two --fill"
680
+ assert _reason(_bash_in(tmp_path, command)) is None
681
+ all_origin_reads = [
682
+ each_command for each_command in all_started_commands if "get-url" in each_command
683
+ ]
684
+ assert len(all_origin_reads) == 1
685
+ assert len(all_started_commands) == 3
686
+
687
+
688
+ def test_should_name_the_first_unreviewed_open_in_command_order(
689
+ two_branch_checkout: tuple[Path, str, str],
690
+ ) -> None:
691
+ folder, first_head, second_head = two_branch_checkout
692
+ command = (
693
+ "gh pr create --repo acme/tools --head second --fill && "
694
+ "gh pr create --repo acme/tools --head feature --fill"
695
+ )
696
+ reason = _deny_reason(_bash_in(folder, command))
697
+ assert f"no clean review for head {second_head}" in reason
698
+ assert first_head not in reason
699
+
700
+
701
+ def test_should_allow_chained_opens_when_every_head_was_reviewed(
702
+ two_branch_checkout: tuple[Path, str, str],
703
+ ) -> None:
704
+ folder, first_head, second_head = two_branch_checkout
705
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
706
+ merge_review_receipt.record_open_review("acme/tools", second_head, 0)
707
+ command = (
708
+ "gh pr create --repo acme/tools --head feature --fill && "
709
+ "gh pr create --repo acme/tools --head second --fill"
710
+ )
711
+ assert _reason(_bash_in(folder, command)) is None
712
+
713
+
714
+ def _clock_past_the_read_deadline() -> Callable[[], float]:
715
+ all_readings = iter([0.0])
716
+ return lambda: next(all_readings, 1000.0)
717
+
718
+
719
+ def test_should_deny_a_reviewed_shell_open_whose_head_reads_ran_out_of_time(
720
+ branch_checkout: tuple[Path, str],
721
+ ) -> None:
722
+ folder, head = branch_checkout
723
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
724
+ payload = _bash_in(folder, "gh pr create --repo acme/tools --head feature --fill")
725
+ assert _reason(payload) is None
726
+ with read_deadlines_started(clock=_clock_past_the_read_deadline()):
727
+ assert "names no repository or head commit" in _deny_reason(payload)
728
+
729
+
730
+ def test_should_start_no_github_listing_once_the_read_deadline_passed(
731
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch
732
+ ) -> None:
733
+ folder, head = branch_checkout
734
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
735
+ all_started_commands: list[list[str]] = []
736
+ start_before_patch = gate_run_reads.subprocess.Popen
737
+
738
+ def record_and_start(
739
+ all_arguments: list[str], **all_keyword_arguments: object
740
+ ) -> subprocess.Popen[str]:
741
+ all_started_commands.append(all_arguments)
742
+ return start_before_patch(all_arguments, **all_keyword_arguments)
743
+
744
+ monkeypatch.setattr(gate_run_reads.subprocess, "Popen", record_and_start)
745
+ with read_deadlines_started(clock=_clock_past_the_read_deadline()):
746
+ assert "names no repository or head commit" in _deny_reason(_create_tool(folder))
747
+ assert all_started_commands == []
748
+
749
+
750
+ def test_should_say_a_second_run_may_pass_when_a_git_read_ran_out_of_time(
751
+ branch_checkout: tuple[Path, str],
752
+ ) -> None:
753
+ folder, _head = branch_checkout
754
+ payload = _bash_in(folder, "gh pr create --repo acme/tools --head feature --fill")
755
+ with read_deadlines_started(clock=_clock_past_the_read_deadline()):
756
+ assert _reason(payload) == UNKNOWN_OPEN_TARGET_REASON + READS_RAN_OUT_SENTENCE
757
+
758
+
759
+ def test_should_say_a_second_run_may_pass_when_the_github_listing_was_skipped(
760
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
761
+ ) -> None:
762
+ _git_on_path(tmp_path, monkeypatch, "exit 1")
763
+ with read_deadlines_started(clock=_clock_past_the_read_deadline()):
764
+ reason = _reason(_create_tool(tmp_path))
765
+ assert reason == UNKNOWN_OPEN_TARGET_REASON + READS_RAN_OUT_SENTENCE
766
+
767
+
768
+ def test_should_not_say_a_second_run_may_pass_when_no_read_ran_out_of_time(
769
+ branch_checkout: tuple[Path, str],
770
+ ) -> None:
771
+ folder, head = branch_checkout
772
+ payload = _bash_in(folder, "gh pr create --repo acme/tools --head feature --fill")
773
+ with read_deadlines_started(clock=lambda: 0.0):
774
+ reason = _deny_reason(payload)
775
+ assert f"no clean review for head {head}" in reason
776
+ assert READS_RAN_OUT_SENTENCE not in reason
777
+
778
+
779
+ def _raise_check_fault(*_all_arguments: object) -> str | None:
780
+ raise KeyError("head")
781
+
782
+
783
+ def test_should_deny_an_open_whose_check_raises(
784
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch
785
+ ) -> None:
786
+ folder, _head = branch_checkout
787
+ monkeypatch.setattr(gate, "open_deny_reason", _raise_check_fault)
788
+ reason = _deny_reason(_bash_in(folder, "gh pr create --repo acme/tools --head feature --fill"))
789
+ assert "merge review gate hit a fault" in reason
790
+ assert "KeyError" in reason
791
+
792
+
793
+ def test_should_deny_a_merge_whose_check_raises(monkeypatch: pytest.MonkeyPatch) -> None:
794
+ monkeypatch.setattr(gate, "deny_reason", _raise_check_fault)
795
+ reason = _deny_reason(_bash(f"gh pr merge 12 --repo acme/tools --match-head-commit {CLEAN_HEAD}"))
796
+ assert "merge review gate hit a fault" in reason
797
+ assert "KeyError" in reason
798
+
799
+
800
+ def test_should_leave_a_call_that_opens_and_merges_nothing_to_run(
801
+ monkeypatch: pytest.MonkeyPatch,
802
+ ) -> None:
803
+ monkeypatch.setattr(gate, "open_deny_reason", _raise_check_fault)
804
+ monkeypatch.setattr(gate, "deny_reason", _raise_check_fault)
805
+ assert gate.decision_for(_bash("git status")) is None
806
+
807
+
808
+ @pytest.mark.parametrize(
809
+ "command",
810
+ [
811
+ "gh pr new --head second --fill",
812
+ "gh -R acme/tools pr create --head second",
813
+ "gh --repo=acme/tools pr create --head second",
814
+ "GH_REPO=acme/tools gh pr create --head second",
815
+ 'bash -c "gh pr create --head second"',
816
+ "python3.11 pull_request.py create --head second",
817
+ "python3 -u pull_request.py create --head second",
818
+ "python -X dev pull_request.py create --head second",
819
+ "py -3 pull_request.py create --head second",
820
+ "uv run python pull_request.py create --head second",
821
+ "uv run pull_request.py create --head second",
822
+ "python3 ~/.agents/skills/pull-request/scripts/pull_request.py create --head second",
823
+ ],
824
+ )
825
+ def test_should_deny_every_create_spelling_whose_head_has_no_review(
826
+ two_branch_checkout: tuple[Path, str, str], command: str
827
+ ) -> None:
828
+ folder, first_head, second_head = two_branch_checkout
829
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
830
+ reviewed_command = command.replace("--head second", "--head feature")
831
+ assert _reason(_bash_in(folder, reviewed_command)) is None
832
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
833
+
834
+
835
+ def test_should_deny_a_create_whose_last_repository_flag_names_an_unreviewed_repository(
836
+ branch_checkout: tuple[Path, str],
837
+ ) -> None:
838
+ folder, head = branch_checkout
839
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
840
+ assert _reason(_bash_in(folder, "gh -R o/r pr create --repo acme/tools --head feature")) is None
841
+ assert "no clean review" in _deny_reason(
842
+ _bash_in(folder, "gh -R o/r pr create -R acme/tools --repo=a/b --repo=a/b --head feature")
843
+ )
844
+
845
+
846
+ @pytest.mark.parametrize(
847
+ "command",
848
+ [
849
+ "export GH_REPO=a/b ; gh pr create --head feature",
850
+ "export GH_REPO=acme/tools && gh pr create --head feature",
851
+ "GH_REPO=a/b ; gh pr create --head feature",
852
+ "export GH_REPO ; gh pr create --head feature",
853
+ "declare -x GH_REPO=a/b ; gh pr create --head feature",
854
+ "typeset -x GH_REPO=a/b ; gh pr create --head feature",
855
+ "eval 'export GH_REPO=a/b' ; gh pr create --head feature",
856
+ "bash -c 'export GH_REPO=a/b ; gh pr create --head feature'",
857
+ "GH_REPO=a/b bash -c 'gh pr create --head feature'",
858
+ "env GH_REPO=a/b sh -c 'gh pr create --head feature'",
859
+ "gh pr create --head feature ; export GH_REPO=a/b",
860
+ "export GH_REPO+=a/b ; gh pr create --head feature",
861
+ "GH_REPO+=a/b ; gh pr create --head feature",
862
+ "GH_REPO+=a/b gh pr create --head feature",
863
+ "GH_REPO=acme/tools GH_REPO+=x gh pr create --head feature",
864
+ ],
865
+ )
866
+ def test_should_deny_as_unread_a_create_with_no_repository_when_the_command_sets_gh_repo(
867
+ branch_checkout: tuple[Path, str], command: str
868
+ ) -> None:
869
+ folder, head = branch_checkout
870
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
871
+ reason = _deny_reason(_bash_in(folder, command))
872
+ assert "names no repository or head commit" in reason
873
+ assert "--repo owner/name" in reason
874
+
875
+
876
+ @pytest.mark.parametrize(
877
+ "command",
878
+ [
879
+ "export GH_REPO=a/b ; gh pr create --repo acme/tools --head feature",
880
+ "export GH_REPO=a/b ; gh pr create -R acme/tools --head feature",
881
+ "export GH_REPO=a/b ; GH_REPO=acme/tools gh pr create --head feature",
882
+ "GH_REPO=acme/tools gh pr create --head feature ; gh pr create --head feature",
883
+ "echo GH_REPO=a/b ; gh pr create --head feature",
884
+ "gh pr create --head feature --body 'export GH_REPO=a/b'",
885
+ "GH_REPO+=a/b gh pr create --repo acme/tools --head feature",
886
+ "GH_REPO=a/b GH_REPO=acme/tools gh pr create --head feature",
887
+ "echo GH_REPO+=a/b ; gh pr create --head feature",
888
+ ],
889
+ )
890
+ def test_should_read_a_create_that_names_its_own_repository_when_the_command_sets_gh_repo(
891
+ branch_checkout: tuple[Path, str], command: str
892
+ ) -> None:
893
+ folder, head = branch_checkout
894
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
895
+ assert _reason(_bash_in(folder, command)) is None
896
+
897
+
898
+ def test_should_deny_as_unread_a_create_past_the_unwrap_depth(
899
+ branch_checkout: tuple[Path, str],
900
+ ) -> None:
901
+ folder, head = branch_checkout
902
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
903
+ assert _reason(_bash_in(folder, "eval " * 18 + "'echo hi'")) is None
904
+ command = "eval " * 18 + "'gh pr create --head feature'"
905
+ assert "names no repository or head commit" in _deny_reason(_bash_in(folder, command))
906
+
907
+
908
+ @pytest.mark.parametrize(
909
+ "command",
910
+ [
911
+ "gh pr create --title --repo=x/y --head feature",
912
+ "gh pr create --head feature --title --repo x/y",
913
+ "gh pr create --head feature --title -Hsecond",
914
+ "gh pr create --head feature --body -H --title second",
915
+ "gh pr create --head feature -bHsecond",
916
+ "gh pr create --head feature -tH second",
917
+ ],
918
+ )
919
+ def test_should_read_a_flag_word_that_is_the_value_of_another_option_as_that_value(
920
+ two_branch_checkout: tuple[Path, str, str], command: str
921
+ ) -> None:
922
+ folder, first_head, _second_head = two_branch_checkout
923
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
924
+ assert _reason(_bash_in(folder, command)) is None
925
+
926
+
927
+ @pytest.mark.parametrize(
928
+ "command",
929
+ [
930
+ "gh pr create --head feature --body '$(gh pr create --head second)'",
931
+ "gh pr create --head feature --title '`gh pr create --head second`'",
932
+ "gh pr create --head feature ; echo '$(gh pr create --head second)'",
933
+ "gh pr create --head feature ; echo \\$(gh pr create --head second)",
934
+ "gh pr create --head feature ; echo \\`gh pr create --head second\\`",
935
+ ],
936
+ )
937
+ def test_should_read_no_create_in_a_substitution_bash_does_not_run(
938
+ two_branch_checkout: tuple[Path, str, str], command: str
939
+ ) -> None:
940
+ folder, first_head, _second_head = two_branch_checkout
941
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
942
+ assert _reason(_bash_in(folder, command)) is None
943
+
944
+
945
+ @pytest.mark.parametrize(
946
+ "command",
947
+ [
948
+ 'gh pr create --head feature --body "$(gh pr create --head second)"',
949
+ 'gh pr create --head feature --body "$(gh pr create --title \\"x\\" --head second)"',
950
+ "gh pr create --head feature ; echo $(gh pr create --head second)",
951
+ ],
952
+ )
953
+ def test_should_deny_a_create_in_a_substitution_bash_runs_on_an_unreviewed_head(
954
+ two_branch_checkout: tuple[Path, str, str], command: str
955
+ ) -> None:
956
+ folder, first_head, second_head = two_branch_checkout
957
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
958
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
959
+
960
+
961
+ @pytest.mark.parametrize(
962
+ "command",
963
+ [
964
+ "gh pr create --head feature ; case x in x) gh pr create --head second ;; esac",
965
+ "gh pr create --head feature ; case x in x) gh -R acme/tools pr create --head second ;; esac",
966
+ "case x in x) gh pr create --head feature ;; y) gh pr create --head second ;; esac",
967
+ ],
968
+ )
969
+ def test_should_deny_a_create_in_a_case_arm_on_an_unreviewed_head(
970
+ two_branch_checkout: tuple[Path, str, str], command: str
971
+ ) -> None:
972
+ folder, first_head, second_head = two_branch_checkout
973
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
974
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
975
+
976
+
977
+ def test_should_deny_a_create_whose_short_flag_cluster_names_an_unreviewed_head(
978
+ two_branch_checkout: tuple[Path, str, str],
979
+ ) -> None:
980
+ folder, first_head, second_head = two_branch_checkout
981
+ merge_review_receipt.record_open_review("acme/tools", second_head, 0)
982
+ assert _reason(_bash_in(folder, "gh pr create --repo acme/tools -dHsecond")) is None
983
+ assert f"no clean review for head {first_head}" in _deny_reason(
984
+ _bash_in(folder, "gh pr create --repo acme/tools -dHfeature")
985
+ )
986
+ assert f"no clean review for head {first_head}" in _deny_reason(
987
+ _bash_in(folder, "gh pr create --repo acme/tools -dHfeature --head second")
988
+ )
989
+ assert f"no clean review for head {first_head}" in _deny_reason(
990
+ _bash_in(folder, "gh pr create --repo acme/tools --head second -dH feature")
991
+ )
992
+
993
+
994
+ @pytest.fixture
995
+ def reviewed_sibling(two_branch_checkout: tuple[Path, str, str]) -> tuple[Path, str]:
996
+ folder, first_head, second_head = two_branch_checkout
997
+ _git(folder, "worktree", "add", "--quiet", str(folder.parent / "sibling"), "feature")
998
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
999
+ return folder, second_head
1000
+
1001
+
1002
+ @pytest.mark.parametrize(
1003
+ "command",
1004
+ [
1005
+ "(cd ../sibling && gh pr create --fill) ; gh pr create --fill",
1006
+ "pushd ../sibling && gh pr create --fill && popd && gh pr create --fill",
1007
+ ],
1008
+ )
1009
+ def test_should_read_the_create_after_a_subshell_or_popd_in_the_starting_folder(
1010
+ reviewed_sibling: tuple[Path, str], command: str
1011
+ ) -> None:
1012
+ folder, second_head = reviewed_sibling
1013
+ assert _reason(_bash_in(folder, "cd ../sibling && gh pr create --fill")) is None
1014
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1015
+
1016
+
1017
+ def test_should_deny_a_create_after_a_folder_change_it_cannot_follow(
1018
+ reviewed_sibling: tuple[Path, str], monkeypatch: pytest.MonkeyPatch
1019
+ ) -> None:
1020
+ folder, _second_head = reviewed_sibling
1021
+ monkeypatch.chdir(folder)
1022
+ assert _reason(_bash_in(folder, "gh pr create --repo acme/tools --head feature --fill")) is None
1023
+ command = 'cd "$SIBLING" && gh pr create --repo acme/tools --head feature --fill'
1024
+ assert _reason(_bash_in(folder, command)) == UNKNOWN_OPEN_TARGET_REASON
1025
+
1026
+
1027
+ def test_should_read_no_create_in_a_heredoc_body(
1028
+ two_branch_checkout: tuple[Path, str, str],
1029
+ ) -> None:
1030
+ folder, first_head, _second_head = two_branch_checkout
1031
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1032
+ command = "gh pr create --head feature --body-file - <<'EOF'\ngh pr create --head second\nEOF"
1033
+ assert _reason(_bash_in(folder, command)) is None
1034
+
1035
+
1036
+ def test_should_read_the_repository_of_a_create_whose_label_quotes_a_create(
1037
+ two_branch_checkout: tuple[Path, str, str], monkeypatch: pytest.MonkeyPatch
1038
+ ) -> None:
1039
+ folder, _first_head, second_head = two_branch_checkout
1040
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/tools")
1041
+ assert _reason(_bash_in(folder, "gh pr create --head second")) is None
1042
+ command = 'gh pr create --head second --label "see gh pr create" --repo acme/other'
1043
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1044
+
1045
+
1046
+ def test_should_read_the_repository_gh_repo_names(
1047
+ two_branch_checkout: tuple[Path, str, str], monkeypatch: pytest.MonkeyPatch
1048
+ ) -> None:
1049
+ folder, _first_head, second_head = two_branch_checkout
1050
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/tools")
1051
+ command = "GH_REPO=acme/other gh pr create --head second"
1052
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1053
+
1054
+
1055
+ @pytest.mark.parametrize(
1056
+ "command",
1057
+ [
1058
+ "GH_REPO=acme/other gh pr create --head feature",
1059
+ "GH_REPO=acme/other env -u GH_REPO gh pr create --head feature",
1060
+ "GH_REPO=acme/other env -i gh pr create --head feature",
1061
+ ],
1062
+ )
1063
+ def test_should_ask_for_an_origin_review_of_a_first_create_behind_a_gh_repo_prefix(
1064
+ branch_checkout: tuple[Path, str], command: str
1065
+ ) -> None:
1066
+ folder, head = branch_checkout
1067
+ merge_review_receipt.main(["acme/other", "open", head, "0"], io.StringIO())
1068
+ assert f"acme/tools has no clean review for head {head}" in _deny_reason(
1069
+ _bash_in(folder, command)
1070
+ )
1071
+
1072
+
1073
+ def _git_on_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, script_body: str) -> None:
1074
+ bin_folder = tmp_path / "fake-bin"
1075
+ bin_folder.mkdir()
1076
+ fake_git = bin_folder / "git"
1077
+ fake_git.write_text("#!/bin/sh\n" + script_body + "\n", encoding="utf-8")
1078
+ fake_git.chmod(0o755)
1079
+ monkeypatch.setenv("PATH", f"{bin_folder}{os.pathsep}{os.environ['PATH']}")
1080
+
1081
+
1082
+ def test_should_say_a_second_run_may_pass_when_a_git_read_hits_its_own_timeout(
1083
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
1084
+ ) -> None:
1085
+ _git_on_path(tmp_path, monkeypatch, "exec sleep 30")
1086
+ monkeypatch.setattr(open_request, "GIT_TIMEOUT_SECONDS", 0.3)
1087
+ payload = _bash_in(tmp_path, "gh pr create --repo acme/tools --head feature --fill")
1088
+ assert _reason(payload) == UNKNOWN_OPEN_TARGET_REASON + READS_RAN_OUT_SENTENCE
1089
+
1090
+
1091
+ def test_should_not_say_a_second_run_may_pass_when_git_fails_fast(
1092
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
1093
+ ) -> None:
1094
+ _git_on_path(tmp_path, monkeypatch, "exit 1")
1095
+ monkeypatch.setattr(open_request, "GIT_TIMEOUT_SECONDS", 0.3)
1096
+ payload = _bash_in(tmp_path, "gh pr create --repo acme/tools --head feature --fill")
1097
+ assert _reason(payload) == UNKNOWN_OPEN_TARGET_REASON
1098
+
1099
+
1100
+ def test_should_say_a_second_run_may_pass_when_the_tool_head_lookup_hits_its_own_timeout(
1101
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
1102
+ ) -> None:
1103
+ _git_on_path(tmp_path, monkeypatch, 'if [ "$1" = ls-remote ]; then exec sleep 30; fi\nexit 1')
1104
+ monkeypatch.setattr(open_request, "REMOTE_LOOKUP_TIMEOUT_SECONDS", 0.3)
1105
+ reason = _reason(_create_tool(tmp_path))
1106
+ assert reason is not None
1107
+ assert reason.endswith(READS_RAN_OUT_SENTENCE)
1108
+
1109
+
1110
+ def _is_running(process_id: int) -> bool:
1111
+ try:
1112
+ os.kill(process_id, 0)
1113
+ except ProcessLookupError:
1114
+ return False
1115
+ status_file = Path(f"/proc/{process_id}/stat")
1116
+ if not status_file.exists():
1117
+ return True
1118
+ return status_file.read_text(encoding="utf-8").rsplit(")", 1)[1].split()[0] != "Z"
1119
+
1120
+
1121
+ def test_should_leave_no_process_behind_when_the_tool_head_lookup_times_out(
1122
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
1123
+ ) -> None:
1124
+ process_id_file = tmp_path / "grandchild.pid"
1125
+ _git_on_path(
1126
+ tmp_path,
1127
+ monkeypatch,
1128
+ 'if [ "$1" = ls-remote ]; then\n'
1129
+ f" sleep 30 </dev/null >/dev/null 2>&1 & echo $! > {process_id_file}\n"
1130
+ " exec sleep 30\nfi\nexit 1",
1131
+ )
1132
+ monkeypatch.setattr(open_request, "REMOTE_LOOKUP_TIMEOUT_SECONDS", 0.5)
1133
+ reason = _reason(_create_tool(tmp_path))
1134
+ assert reason is not None
1135
+ assert reason.endswith(READS_RAN_OUT_SENTENCE)
1136
+ assert not _is_running(int(process_id_file.read_text(encoding="utf-8")))
1137
+
1138
+
1139
+ def test_should_not_say_a_second_run_may_pass_when_the_tool_head_lookup_fails_fast(
1140
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
1141
+ ) -> None:
1142
+ _git_on_path(tmp_path, monkeypatch, "exit 1")
1143
+ monkeypatch.setattr(open_request, "REMOTE_LOOKUP_TIMEOUT_SECONDS", 0.3)
1144
+ reason = _reason(_create_tool(tmp_path))
1145
+ assert reason is not None
1146
+ assert READS_RAN_OUT_SENTENCE not in reason
1147
+
1148
+
1149
+ @pytest.mark.parametrize(
1150
+ "command",
1151
+ [
1152
+ "cat <<'BODY' | bash\ngh pr create --head second\nBODY",
1153
+ "cat > x.sh <<'BODY'\ngh pr create --head second\nBODY\nbash x.sh",
1154
+ ". /dev/stdin <<'BODY'\ngh pr create --head second\nBODY",
1155
+ "eval \"$(cat <<'BODY'\ngh pr create --head second\nBODY\n)\"",
1156
+ "echo gh pr create --head second | bash; x=`gh pr create --head feature`",
1157
+ "echo gh pr create --head second | bash; bash -c 'gh pr create --head feature'",
1158
+ "echo gh pr create --head second | bash; eval 'gh pr create --head feature'",
1159
+ ],
1160
+ )
1161
+ def test_should_deny_an_unreviewed_first_create_a_shell_reads_from_its_input(
1162
+ two_branch_checkout: tuple[Path, str, str], command: str
1163
+ ) -> None:
1164
+ folder, first_head, second_head = two_branch_checkout
1165
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1166
+ assert _reason(_bash_in(folder, "gh pr create --head feature")) is None
1167
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1168
+
1169
+
1170
+ def test_should_not_read_a_create_in_the_sibling_a_piped_brace_group_moves_to(
1171
+ reviewed_sibling: tuple[Path, str],
1172
+ ) -> None:
1173
+ folder, _second_head = reviewed_sibling
1174
+ assert _reason(_bash_in(folder, "cd ../sibling; gh pr create --fill")) is None
1175
+ assert _reason(_bash_in(folder, "{ cd ../sibling; } | true; gh pr create --fill")) is not None
1176
+
1177
+
1178
+ @pytest.mark.parametrize(
1179
+ "command",
1180
+ [
1181
+ "gh pr create -Hsecond",
1182
+ "gh pr create --head feature --head second",
1183
+ "gh pr create --head=feature -H second",
1184
+ ],
1185
+ )
1186
+ def test_should_deny_a_create_when_any_head_it_names_is_unreviewed(
1187
+ two_branch_checkout: tuple[Path, str, str], command: str
1188
+ ) -> None:
1189
+ folder, first_head, second_head = two_branch_checkout
1190
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1191
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1192
+
1193
+
1194
+ @pytest.mark.parametrize(
1195
+ "command",
1196
+ [
1197
+ "gh pr create --head feature",
1198
+ "gh pr create --head feature && gh pr create --head=feature --fill",
1199
+ ],
1200
+ )
1201
+ def test_should_open_reviewed_single_and_chained_creates(
1202
+ two_branch_checkout: tuple[Path, str, str], command: str
1203
+ ) -> None:
1204
+ folder, first_head, _second_head = two_branch_checkout
1205
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1206
+ assert _reason(_bash_in(folder, command)) is None
1207
+ assert _reason(_create_tool(folder)) is None
1208
+
1209
+
1210
+ def test_should_deny_a_tool_merge_whose_number_overflows() -> None:
1211
+ payload = _merge_tool(CLEAN_HEAD)
1212
+ payload["tool_input"]["pullNumber"] = float("1e999")
1213
+ assert "OverflowError" in _deny_reason(payload)
1214
+
1215
+
1216
+ def _raise_value_error(_all_payload_fields: dict[str, object]) -> None:
1217
+ raise ValueError("parse broke")
1218
+
1219
+
1220
+ @pytest.mark.parametrize(
1221
+ "payload",
1222
+ [
1223
+ _bash("gh pr create --fill"),
1224
+ _bash("python3 scripts/pull_request.py create"),
1225
+ _bash("gh pr merge 12"),
1226
+ _bash("gh api -X PUT repos/acme/tools/pulls/12/merge"),
1227
+ _bash("gh pr new --fill"),
1228
+ _bash("gh -R o/r pr create --fill"),
1229
+ _bash("gh --repo o/r pr merge 5"),
1230
+ _bash("cd sub && gh.exe pr create --fill"),
1231
+ _bash("env GH_REPO=o/r gh pr merge 5"),
1232
+ _bash("python3 scripts/pull_request.py create --fill"),
1233
+ {"tool_name": "mcp__github__create_pull_request", "tool_input": {}},
1234
+ {"tool_name": "mcp__github__enable_pr_auto_merge", "tool_input": {}},
1235
+ ],
1236
+ )
1237
+ def test_should_deny_an_open_or_merge_whose_payload_read_raises(
1238
+ monkeypatch: pytest.MonkeyPatch, payload: dict[str, object]
1239
+ ) -> None:
1240
+ monkeypatch.setattr(gate, "open_calls_from", _raise_value_error)
1241
+ all_escaped_errors: list[ValueError] = []
1242
+ try:
1243
+ reason = _reason(payload)
1244
+ except ValueError as error:
1245
+ all_escaped_errors.append(error)
1246
+ reason = None
1247
+ assert all_escaped_errors == []
1248
+ assert reason is not None and "ValueError" in reason
1249
+
1250
+
1251
+ @pytest.mark.parametrize("command", ["git status", "ls -la", "git log --oneline", "echo new"])
1252
+ def test_should_pass_a_payload_read_fault_on_other_commands_to_the_shared_helper(
1253
+ monkeypatch: pytest.MonkeyPatch, command: str
1254
+ ) -> None:
1255
+ monkeypatch.setattr(gate, "open_calls_from", _raise_value_error)
1256
+ with pytest.raises(ValueError):
1257
+ gate.decision_for(_bash(command))
1258
+
1259
+
1260
+ ALL_ESCAPED_QUOTE_VALUES = [
1261
+ 'gh pr create --head feature --body "say \\"x\\" ; gh pr create --head second"',
1262
+ 'gh pr create --head feature --label "say \\"x\\" gh pr create --head second"',
1263
+ 'gh pr create --head feature --body "see \\"x\\" --head second"',
1264
+ "gh pr create --head feature --body 'say \\\"x\\\" ; gh pr create --head second'",
1265
+ ]
1266
+
1267
+
1268
+ @pytest.mark.parametrize("command", ALL_ESCAPED_QUOTE_VALUES)
1269
+ def test_should_read_no_create_or_head_inside_a_quoted_value_with_an_escaped_quote(
1270
+ two_branch_checkout: tuple[Path, str, str], command: str
1271
+ ) -> None:
1272
+ folder, first_head, _second_head = two_branch_checkout
1273
+ assert f"no clean review for head {first_head}" in _deny_reason(_bash_in(folder, command))
1274
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1275
+ assert _reason(_bash_in(folder, command)) is None
1276
+
1277
+
1278
+ def test_should_read_a_create_after_a_value_an_escaped_quote_does_not_close(
1279
+ two_branch_checkout: tuple[Path, str, str],
1280
+ ) -> None:
1281
+ folder, first_head, second_head = two_branch_checkout
1282
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1283
+ command = 'gh pr create --head feature --body "a \\" b" ; gh pr create --head second'
1284
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1285
+
1286
+
1287
+ def test_should_check_the_checkout_head_too_when_a_create_joins_its_head_to_dash_h(
1288
+ two_branch_checkout: tuple[Path, str, str],
1289
+ ) -> None:
1290
+ folder, first_head, second_head = two_branch_checkout
1291
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1292
+ assert _reason(_bash_in(folder, "gh pr create --head feature")) is None
1293
+ assert f"no clean review for head {second_head}" in _deny_reason(
1294
+ _bash_in(folder, "gh pr create -Hfeature")
1295
+ )
1296
+
1297
+
1298
+ ALL_LATER_CREATES_BASH_RUNS = [
1299
+ "gh pr create --head feature --body $'it\\'s' ; gh pr create --head second",
1300
+ "gh pr create --head feature --body $'it\\'s' && gh pr create --head second",
1301
+ "gh pr create --head feature --body $'it\\'s' | gh pr create --head second",
1302
+ "gh pr create --head feature --body $'it\\'s' & gh pr create --head second",
1303
+ "gh pr create --head feature --body $'\\'' ; gh pr create --head second",
1304
+ 'gh pr create --head feature && bash -c "true\ngh pr create --head second"',
1305
+ "gh pr create --head feature && sh -c 'true\ngh pr create --head second'",
1306
+ 'gh pr create --head feature && eval "true\ngh pr create --head second"',
1307
+ "gh pr create --head feature ; eval 'ls\nenv gh pr create -Hsecond --repo acme/tools'",
1308
+ 'gh pr create --head feature ; bash -c "if true; then X=1\n\n'
1309
+ 'gh pr create --head feature --head second; fi"',
1310
+ "gh pr create --head feature --body \"$(echo '\"')\" ; gh pr create --head second",
1311
+ "gh pr create --head feature ; bash -c -- 'gh pr create --head second'",
1312
+ "gh pr create --head feature ; eval 'gh pr create --head second --body \"a b\"'",
1313
+ "gh pr create --head feature ; eval \"gh pr create --head second --body 'a b'\"",
1314
+ "gh pr create --head feature ; case x in x) eval 'gh pr create --head second';; esac",
1315
+ "gh pr create --head feature ; case x in (x) bash -c 'gh pr create --head second';; esac",
1316
+ "gh pr create --head feature ; (gh pr create --head second) > log",
1317
+ "gh pr create --head feature ; (bash -c 'gh pr create --head second') 2>&1 | tee log",
1318
+ "gh pr create --head feature ; case x in x) (gh pr create --head second) > log;; esac",
1319
+ ]
1320
+
1321
+
1322
+ @pytest.mark.parametrize("command", ALL_LATER_CREATES_BASH_RUNS)
1323
+ def test_should_deny_a_later_create_bash_runs_on_an_unreviewed_head(
1324
+ two_branch_checkout: tuple[Path, str, str], command: str
1325
+ ) -> None:
1326
+ folder, first_head, second_head = two_branch_checkout
1327
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1328
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1329
+
1330
+
1331
+ def test_should_check_a_create_after_a_line_break_in_bash_c_in_the_folder_before_it(
1332
+ two_branch_checkout: tuple[Path, str, str],
1333
+ ) -> None:
1334
+ folder, first_head, second_head = two_branch_checkout
1335
+ _git(folder, "checkout", "--quiet", "feature")
1336
+ _git(folder, "worktree", "add", "--quiet", str(folder.parent / "usub"), "second")
1337
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1338
+ command = 'gh pr create ; cd ../usub && bash -c "true\ngh pr create"'
1339
+ assert f"no clean review for head {second_head}" in _deny_reason(_bash_in(folder, command))
1340
+
1341
+
1342
+ def test_should_allow_a_later_create_inside_a_quoted_value_after_a_line_break(
1343
+ two_branch_checkout: tuple[Path, str, str],
1344
+ ) -> None:
1345
+ folder, first_head, _second_head = two_branch_checkout
1346
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1347
+ command = 'gh pr create --head feature --body "a\ngh pr create --head second"'
1348
+ assert _reason(_bash_in(folder, command)) is None
1349
+
1350
+
1351
+ def test_should_allow_a_reviewed_create_in_a_substitution_whose_body_holds_a_line_break(
1352
+ branch_checkout: tuple[Path, str],
1353
+ ) -> None:
1354
+ folder, head = branch_checkout
1355
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
1356
+ command = "x=$(gh pr create --head feature --body 'a\nb'\nls '')"
1357
+ assert _reason(_bash_in(folder, command)) is None
1358
+
1359
+
1360
+ def test_should_allow_a_reviewed_create_after_an_eval_script_bash_cannot_parse(
1361
+ branch_checkout: tuple[Path, str],
1362
+ ) -> None:
1363
+ folder, head = branch_checkout
1364
+ merge_review_receipt.record_open_review("acme/tools", head, 0)
1365
+ command = "bash -c \"eval 'gh pr create --title 'a b' --body \"x; gh pr create --head feature --body \"y\""
1366
+ assert _reason(_bash_in(folder, command)) is None
1367
+
1368
+
1369
+ def test_should_allow_a_reviewed_create_a_wrapped_script_runs_before_another_line(
1370
+ two_branch_checkout: tuple[Path, str, str],
1371
+ ) -> None:
1372
+ folder, first_head, _second_head = two_branch_checkout
1373
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1374
+ command = "gh pr create --head feature && bash -c 'gh pr create --head feature\nls'"
1375
+ assert _reason(_bash_in(folder, command)) is None
1376
+
1377
+
1378
+ def test_should_allow_a_body_a_heredoc_substitution_writes_with_quotes_inside(
1379
+ two_branch_checkout: tuple[Path, str, str],
1380
+ ) -> None:
1381
+ folder, first_head, _second_head = two_branch_checkout
1382
+ merge_review_receipt.record_open_review("acme/tools", first_head, 0)
1383
+ command = (
1384
+ "gh pr create --head feature --body \"$(cat <<'BODY'\n"
1385
+ 'see "q" ; gh pr create --head second\nBODY\n)"'
1386
+ )
1387
+ assert _reason(_bash_in(folder, command)) is None