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
@@ -1070,6 +1070,139 @@ def test_should_ask_for_an_origin_review_of_a_first_create_behind_a_gh_repo_pref
1070
1070
  )
1071
1071
 
1072
1072
 
1073
+ ALL_LATER_PREFIXED_CREATES = [
1074
+ 'gh pr create --head feature --label "see gh pr create" --repo acme/other ; '
1075
+ "GH_REPO=acme/other env -u GH_REPO gh pr create --head feature",
1076
+ *(
1077
+ f"gh pr create --head feature --repo acme/other {separator} "
1078
+ f"GH_REPO=acme/other {wrapper} gh pr create --head feature"
1079
+ for separator in (";", "&&")
1080
+ for wrapper in (
1081
+ "env -u GH_REPO",
1082
+ "env -i",
1083
+ "sudo",
1084
+ "env --unset=GH_REPO",
1085
+ "env -uGH_REPO",
1086
+ "env -",
1087
+ "env --ignore-environment",
1088
+ "sudo --preserve-env",
1089
+ "nohup env -u GH_REPO",
1090
+ "nice env -u GH_REPO",
1091
+ "nice -n 5 env -u GH_REPO",
1092
+ "stdbuf -o0 env -u GH_REPO",
1093
+ "time env -u GH_REPO",
1094
+ "setsid env -u GH_REPO",
1095
+ "env nice env -u GH_REPO",
1096
+ "nohup nice env -i env -u GH_REPO",
1097
+ "nice -n 5 env -u GH_REPO stdbuf -o0",
1098
+ "time nohup env --unset GH_REPO nice",
1099
+ "nohup timeout -s KILL 60",
1100
+ )
1101
+ ),
1102
+ "echo 'gh pr create --head feature --label \"see gh pr create\" --repo acme/other' ; "
1103
+ "GH_REPO=acme/other env -u GH_REPO gh pr create --head feature",
1104
+ "gh pr create --head feature --repo acme/other ; GH_REPO=acme/other gh pr create --head feature",
1105
+ "gh pr create --head feature --repo acme/other ; env GH_REPO=acme/other gh pr create --head feature",
1106
+ "gh pr create --head feature --repo acme/other ; "
1107
+ "env -u GH_REPO GH_REPO=acme/other gh pr create --head feature",
1108
+ "gh pr create --head feature --repo acme/other ; "
1109
+ "GH_REPO=acme/other env -u GH_REPO python3 pull_request.py create --head feature",
1110
+ ]
1111
+
1112
+
1113
+ @pytest.mark.parametrize("command", ALL_LATER_PREFIXED_CREATES)
1114
+ def test_should_ask_for_an_origin_review_of_any_create_behind_a_gh_repo_prefix_with_the_prefix_repository_exempt(
1115
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch, command: str
1116
+ ) -> None:
1117
+ folder, head = branch_checkout
1118
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/other")
1119
+ assert f"acme/tools has no clean review for head {head}" in _deny_reason(
1120
+ _bash_in(folder, command)
1121
+ )
1122
+
1123
+
1124
+ @pytest.mark.parametrize("command", ALL_LATER_PREFIXED_CREATES)
1125
+ def test_should_ask_for_an_origin_review_of_any_create_behind_a_gh_repo_prefix_with_the_prefix_repository_reviewed(
1126
+ branch_checkout: tuple[Path, str], command: str
1127
+ ) -> None:
1128
+ folder, head = branch_checkout
1129
+ merge_review_receipt.main(["acme/other", "open", head, "0"], io.StringIO())
1130
+ assert f"acme/tools has no clean review for head {head}" in _deny_reason(
1131
+ _bash_in(folder, command)
1132
+ )
1133
+
1134
+
1135
+ ALL_ORIGIN_CREATES_BESIDE_AN_OTHER_REPOSITORY = [
1136
+ "GH_REPO=acme/other sudo gh pr create --head feature",
1137
+ "gh pr create --head feature --repo acme/other ; gh pr create --head feature --repo acme/tools",
1138
+ "gh pr create --head feature --repo acme/other && gh pr create --head feature",
1139
+ "gh pr create --head feature --repo acme/other ; gh pr create --head feature",
1140
+ "gh pr create --head feature --repo acme/other ; nohup env -u FOO gh pr create --head feature",
1141
+ "gh pr create --head feature --repo acme/other ; "
1142
+ "nice -n 5 env -u FOO stdbuf -o0 gh pr create --head feature",
1143
+ ]
1144
+
1145
+
1146
+ @pytest.mark.parametrize("command", ALL_ORIGIN_CREATES_BESIDE_AN_OTHER_REPOSITORY)
1147
+ def test_should_ask_for_an_origin_review_beside_an_exempt_other_repository(
1148
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch, command: str
1149
+ ) -> None:
1150
+ folder, head = branch_checkout
1151
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/other")
1152
+ assert f"acme/tools has no clean review for head {head}" in _deny_reason(
1153
+ _bash_in(folder, command)
1154
+ )
1155
+
1156
+
1157
+ @pytest.mark.parametrize("command", ALL_ORIGIN_CREATES_BESIDE_AN_OTHER_REPOSITORY)
1158
+ def test_should_ask_for_an_origin_review_beside_a_reviewed_other_repository(
1159
+ branch_checkout: tuple[Path, str], command: str
1160
+ ) -> None:
1161
+ folder, head = branch_checkout
1162
+ merge_review_receipt.main(["acme/other", "open", head, "0"], io.StringIO())
1163
+ assert f"acme/tools has no clean review for head {head}" in _deny_reason(
1164
+ _bash_in(folder, command)
1165
+ )
1166
+
1167
+
1168
+ def test_should_tell_a_create_behind_a_gh_repo_prefix_to_name_its_repository_with_the_repo_flag(
1169
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch
1170
+ ) -> None:
1171
+ folder, _head = branch_checkout
1172
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/other")
1173
+ reason = _deny_reason(
1174
+ _bash_in(folder, "GH_REPO=acme/other env -u GH_REPO gh pr create --head feature")
1175
+ )
1176
+ assert "GH_REPO=" in reason
1177
+ assert "--repo owner/name" in reason
1178
+
1179
+
1180
+ @pytest.mark.parametrize(
1181
+ "command",
1182
+ [
1183
+ "GH_REPO=nonsense gh pr create --head feature",
1184
+ "gh pr create --head feature --repo acme/other ; "
1185
+ "GH_REPO=nonsense env -u GH_REPO gh pr create --head feature",
1186
+ ],
1187
+ )
1188
+ def test_should_tell_a_create_behind_an_unreadable_gh_repo_prefix_to_name_its_repository_with_the_repo_flag(
1189
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch, command: str
1190
+ ) -> None:
1191
+ folder, head = branch_checkout
1192
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/other")
1193
+ reason = _deny_reason(_bash_in(folder, command))
1194
+ assert f"acme/tools has no clean review for head {head}" in reason
1195
+ assert "--repo owner/name" in reason
1196
+
1197
+
1198
+ def test_should_allow_a_create_that_names_an_exempt_repository_with_its_own_repo_flag(
1199
+ branch_checkout: tuple[Path, str], monkeypatch: pytest.MonkeyPatch
1200
+ ) -> None:
1201
+ folder, _head = branch_checkout
1202
+ monkeypatch.setenv("CLAUDE_MERGE_REVIEW_EXEMPT_REPOSITORIES", "acme/other")
1203
+ assert _reason(_bash_in(folder, "gh pr create --head feature --repo acme/other")) is None
1204
+
1205
+
1073
1206
  def _git_on_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, script_body: str) -> None:
1074
1207
  bin_folder = tmp_path / "fake-bin"
1075
1208
  bin_folder.mkdir()
@@ -56,6 +56,10 @@ def test_should_flag_isolated_python_without_utf8_mode(command: str) -> None:
56
56
  assert gate.runs_isolated_python_without_utf8_mode(command) is True
57
57
 
58
58
 
59
+ def test_should_flag_isolated_python_behind_a_launcher_and_env_unset() -> None:
60
+ assert gate.runs_isolated_python_without_utf8_mode("time -p env -u FOO python -I script.py") is True
61
+
62
+
59
63
  @pytest.mark.parametrize(
60
64
  "command",
61
65
  [
@@ -513,3 +513,433 @@ def should_allow_a_review_write_resolve_after_a_reply_on_that_thread(tmp_path: P
513
513
  def should_leave_a_review_write_that_submits_a_review_alone(tmp_path: Path) -> None:
514
514
  transcript_path = _write_transcript(tmp_path, [_prompt("Review the pull request.")])
515
515
  assert gate.decision_for(_review_write_payload(transcript_path, "create")) is None
516
+
517
+
518
+ REPLY_SCRIPT_COMMAND = (
519
+ f"python .github/ci/review_reply_post.py request --pull 7 --comment-id {FIRST_COMMENT_ID}"
520
+ f" --thread-id {FIRST_THREAD_ID} --body-file reply.md"
521
+ )
522
+
523
+
524
+ def _script_reply(tool_use_id: str, command: str = REPLY_SCRIPT_COMMAND, is_background: bool = False) -> str:
525
+ tool_input: dict[str, object] = {"command": command}
526
+ if is_background:
527
+ tool_input["run_in_background"] = True
528
+ return _call(tool_use_id, "Bash", tool_input)
529
+
530
+
531
+ def _task_notice(tool_use_id: str, exit_code: int = 0) -> str:
532
+ status = "completed" if exit_code == 0 else "failed"
533
+ text = (
534
+ f"<task-notification>\n<task-id>b1</task-id>\n<tool-use-id>{tool_use_id}</tool-use-id>\n"
535
+ f"<status>{status}</status>\n<summary>Background command {status} (exit code {exit_code})</summary>\n"
536
+ "</task-notification>"
537
+ )
538
+ return json.dumps({"type": "user", "origin": {"kind": "task-notification"}, "message": {"content": text}})
539
+
540
+
541
+ def should_allow_a_resolve_after_the_reply_script_posted(tmp_path: Path) -> None:
542
+ transcript_path = _write_transcript(
543
+ tmp_path,
544
+ [
545
+ _prompt("Address the review threads."),
546
+ _script_reply("reply-1"),
547
+ _result("reply-1", text="already posted https://github.com/o/r/pull/7#discussion_r1"),
548
+ ],
549
+ )
550
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
551
+
552
+
553
+ def should_allow_a_comment_id_resolve_after_the_reply_script_named_no_thread(tmp_path: Path) -> None:
554
+ command = f"python review_reply_post.py request --pull 7 --comment-id={FIRST_COMMENT_ID} --body-file reply.md"
555
+ transcript_path = _write_transcript(
556
+ tmp_path,
557
+ [_prompt("Address the review threads."), *_threads_read(), _script_reply("reply-1", command), _result("reply-1")],
558
+ )
559
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
560
+ resolve_command = f"gh api -X POST repos/o/r/pulls/7/ccr/comments/{FIRST_COMMENT_ID}/resolve"
561
+ assert gate.decision_for(_shell_resolve_payload(transcript_path, resolve_command)) is None
562
+
563
+
564
+ def should_deny_a_resolve_when_the_reply_script_did_not_post(tmp_path: Path) -> None:
565
+ transcript_path = _write_transcript(
566
+ tmp_path,
567
+ [
568
+ _prompt("Address the review threads."),
569
+ _script_reply("reply-1"),
570
+ _result("reply-1", is_error=True, text="not posted"),
571
+ ],
572
+ )
573
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
574
+
575
+
576
+ def should_deny_a_resolve_of_another_thread_after_the_reply_script(tmp_path: Path) -> None:
577
+ transcript_path = _write_transcript(
578
+ tmp_path,
579
+ [_prompt("Address the review threads."), *_threads_read(), _script_reply("reply-1"), _result("reply-1")],
580
+ )
581
+ decision = gate.decision_for(_resolve_payload(transcript_path, SECOND_THREAD_ID))
582
+ assert _deny_reason(decision) == MISSING_REPLY_REASON
583
+
584
+
585
+ def should_deny_a_resolve_after_a_script_call_that_requests_no_reply(tmp_path: Path) -> None:
586
+ all_commands = [
587
+ f"python review_reply_post.py post --request review-reply.json --branch b --sender s # {FIRST_THREAD_ID}",
588
+ f"cat review_reply_post.py; echo request --comment-id {FIRST_COMMENT_ID} {FIRST_THREAD_ID}",
589
+ f"python review_reply_post.py request --help; echo {FIRST_THREAD_ID}",
590
+ ]
591
+ for each_command in all_commands:
592
+ transcript_path = _write_transcript(
593
+ tmp_path,
594
+ [_prompt("Address the review threads."), _script_reply("reply-1", each_command), _result("reply-1")],
595
+ )
596
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
597
+
598
+
599
+ def should_allow_a_resolve_after_a_background_reply_script_exits_zero(tmp_path: Path) -> None:
600
+ transcript_path = _write_transcript(
601
+ tmp_path,
602
+ [
603
+ _prompt("Address the review threads."),
604
+ _script_reply("reply-1", is_background=True),
605
+ _result("reply-1", text="Command running in background with ID: b1"),
606
+ _task_notice("reply-1"),
607
+ ],
608
+ )
609
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
610
+
611
+
612
+ def should_deny_a_resolve_while_a_background_reply_script_still_runs(tmp_path: Path) -> None:
613
+ transcript_path = _write_transcript(
614
+ tmp_path,
615
+ [
616
+ _prompt("Address the review threads."),
617
+ _script_reply("reply-1", is_background=True),
618
+ _result("reply-1", text="Command running in background with ID: b1"),
619
+ ],
620
+ )
621
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
622
+
623
+
624
+ def should_deny_a_resolve_after_a_background_reply_script_fails(tmp_path: Path) -> None:
625
+ transcript_path = _write_transcript(
626
+ tmp_path,
627
+ [
628
+ _prompt("Address the review threads."),
629
+ _script_reply("reply-1", is_background=True),
630
+ _result("reply-1", text="Command running in background with ID: b1"),
631
+ _task_notice("reply-1", exit_code=3),
632
+ _task_notice("another-call"),
633
+ ],
634
+ )
635
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
636
+
637
+
638
+ def should_keep_a_posted_reply_across_a_background_task_notice(tmp_path: Path) -> None:
639
+ transcript_path = _write_transcript(
640
+ tmp_path,
641
+ [
642
+ _prompt("Address the review threads."),
643
+ *_threads_read(),
644
+ _reply("reply-1"),
645
+ _result("reply-1"),
646
+ _task_notice("another-call"),
647
+ ],
648
+ )
649
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
650
+
651
+
652
+ def should_credit_only_the_thread_the_reply_script_names_as_its_own_argument(tmp_path: Path) -> None:
653
+ command = (
654
+ f"python review_reply_post.py request --pull 7 --comment-id {FIRST_COMMENT_ID}"
655
+ f" --thread-id {FIRST_THREAD_ID} --body-file {SECOND_THREAD_ID}.md"
656
+ )
657
+ transcript_path = _write_transcript(
658
+ tmp_path, [_prompt("Address the review threads."), _script_reply("reply-1", command), _result("reply-1")]
659
+ )
660
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
661
+ decision = gate.decision_for(_resolve_payload(transcript_path, SECOND_THREAD_ID))
662
+ assert _deny_reason(decision) == MISSING_REPLY_REASON
663
+
664
+
665
+ def should_not_count_a_reply_script_call_the_parser_can_read_another_way() -> None:
666
+ plain_call = (
667
+ f"python3 .github/ci/review_reply_post.py request --pull 7 --comment-id {FIRST_COMMENT_ID}"
668
+ f" --thread-id {FIRST_THREAD_ID} --body-file reply.md"
669
+ )
670
+ assert gate.reply_script_targets(plain_call) == (frozenset({FIRST_THREAD_ID}), frozenset({str(FIRST_COMMENT_ID)}))
671
+ assert gate.reply_script_targets(plain_call + " --wait-seconds 30 --supersede") is not None
672
+ assert gate.reply_script_targets("/usr/bin/" + plain_call) is not None
673
+ all_misread_commands = [
674
+ f"{plain_call} --he",
675
+ f"{plain_call} --h",
676
+ f"{plain_call} --hel",
677
+ f"{plain_call} --hel{{p,p}}",
678
+ f"{plain_call} {{--help,--help}}",
679
+ f"{plain_call} --comment {SECOND_COMMENT_ID}",
680
+ f"{plain_call} --comment-i={SECOND_COMMENT_ID}",
681
+ f"{plain_call} --c {SECOND_COMMENT_ID}",
682
+ f"{plain_call} --thread {SECOND_THREAD_ID}",
683
+ f"{plain_call} --thread-i={SECOND_THREAD_ID}",
684
+ f"{plain_call} --comment-id {SECOND_COMMENT_ID}",
685
+ f"{plain_call} --thread-id={SECOND_THREAD_ID}",
686
+ f"{plain_call} --supersede --supersede",
687
+ f"{plain_call} --supersede=1",
688
+ f"{plain_call} --unknown 1",
689
+ f"{plain_call} extra",
690
+ f"{plain_call} --wait-seconds -1",
691
+ f"{plain_call} --wait-seconds",
692
+ f"{plain_call} --wait-seconds=",
693
+ f"{plain_call} --body-file repl?.md",
694
+ f"{plain_call} --body-file ~/reply.md",
695
+ f"{plain_call} --body-file reply[1].md",
696
+ f"{plain_call} --body-file reply\\ one.md",
697
+ "python3 review_reply_post.py request --pull 7 --thread-id not-a-thread --comment-id 9",
698
+ f"./{plain_call}",
699
+ f"bin/{plain_call}",
700
+ "python3 -u review_reply_post.py request --comment-id 9",
701
+ "py -3 review_reply_post.py request --comment-id 9",
702
+ "C:Python312/python.exe review_reply_post.py request --comment-id 9",
703
+ "CD:/Python312/python.exe review_reply_post.py request --comment-id 9",
704
+ ".venv/bin/python review_reply_post.py request --comment-id 9",
705
+ "python2 review_reply_post.py request --comment-id 9",
706
+ "python3.12x review_reply_post.py request --comment-id 9",
707
+ "pythonw.exe review_reply_post.py request --comment-id 9",
708
+ "python review_reply_post.py request --comment-id 9 --body-file 'reply (1).md'",
709
+ "uv run review_reply_post.py request --comment-id 9",
710
+ "~/bin/python3 review_reply_post.py request --comment-id 9",
711
+ ]
712
+ for each_command in all_misread_commands:
713
+ assert gate.reply_script_targets(each_command) is None, each_command
714
+
715
+
716
+ def should_not_count_a_reply_script_whose_exit_code_the_command_can_hide() -> None:
717
+ plain_call = f"python .github/ci/review_reply_post.py request --pull 7 --comment-id {FIRST_COMMENT_ID}"
718
+ assert gate.reply_script_targets(plain_call) is not None
719
+ all_masked_commands = [
720
+ f"{plain_call} || true",
721
+ f"{plain_call}; true",
722
+ f"{plain_call} | cat",
723
+ f"({plain_call}) || true",
724
+ f"{plain_call} &",
725
+ f"{plain_call} > out.txt",
726
+ f"{plain_call}\ntrue",
727
+ f"echo $({plain_call})",
728
+ f"{plain_call} --thread-id $THREAD",
729
+ f"{plain_call} # --comment-id {SECOND_COMMENT_ID}",
730
+ f"{plain_call} --help",
731
+ f"{plain_call} -h",
732
+ f"echo {plain_call}",
733
+ f"env X=1 {plain_call}",
734
+ f"python -c pass review_reply_post.py request --comment-id {FIRST_COMMENT_ID}",
735
+ "python review_reply_post.py request --comment-id abc",
736
+ "python review_reply_post.py request --pull 7",
737
+ f"python review_reply_post.py post --comment-id {FIRST_COMMENT_ID}",
738
+ f"python other_review_reply_post.py request --comment-id {FIRST_COMMENT_ID}",
739
+ f"python review_reply_post.py request --comment-id '{FIRST_COMMENT_ID}",
740
+ ]
741
+ for each_command in all_masked_commands:
742
+ assert gate.reply_script_targets(each_command) is None, each_command
743
+
744
+
745
+ def should_deny_a_resolve_after_a_masked_reply_script_call(tmp_path: Path) -> None:
746
+ transcript_path = _write_transcript(
747
+ tmp_path,
748
+ [
749
+ _prompt("Address the review threads."),
750
+ _script_reply("reply-1", REPLY_SCRIPT_COMMAND + " || true"),
751
+ _result("reply-1"),
752
+ ],
753
+ )
754
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
755
+
756
+
757
+ def _raw_notice(text: str) -> str:
758
+ return json.dumps({"type": "user", "origin": {"kind": "task-notification"}, "message": {"content": text}})
759
+
760
+
761
+ def should_deny_a_resolve_when_a_failed_notice_carries_a_passing_one(tmp_path: Path) -> None:
762
+ passing_block = (
763
+ "<task-notification><tool-use-id>other</tool-use-id><status>completed</status>"
764
+ "<summary>Background command completed (exit code 0)</summary></task-notification>"
765
+ )
766
+ failed_block = (
767
+ "<task-notification><tool-use-id>reply-1</tool-use-id><status>failed</status>"
768
+ "<summary>Background command failed (exit code 3)</summary></task-notification>"
769
+ )
770
+ quoting_summary = (
771
+ "<task-notification><tool-use-id>reply-1</tool-use-id><status>failed</status>"
772
+ '<summary>Background command "post (exit code 0)</summary></task-notification>'
773
+ "<task-notification><tool-use-id>reply-1</tool-use-id><status>completed</status>"
774
+ '<summary>(exit code 0)" failed (exit code 3)</summary></task-notification>'
775
+ )
776
+ quoted_status = (
777
+ "<task-notification><tool-use-id>reply-1</tool-use-id><status>failed</status>"
778
+ '<summary>Background command "<status>completed</status> (exit code 0)" failed (exit code 3)</summary>'
779
+ "</task-notification>"
780
+ )
781
+ for each_text in (passing_block + failed_block, failed_block + passing_block, quoting_summary, quoted_status):
782
+ transcript_path = _write_transcript(
783
+ tmp_path,
784
+ [
785
+ _prompt("Address the review threads."),
786
+ _script_reply("reply-1", is_background=True),
787
+ _result("reply-1", text="Command running in background with ID: b1"),
788
+ _raw_notice(each_text),
789
+ ],
790
+ )
791
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON, each_text
792
+
793
+
794
+ BACKGROUND_STARTED_TEXT = "Command running in background with ID: b1"
795
+
796
+
797
+ def _queued_command(text: str, origin_kind: str, command_mode: str) -> str:
798
+ return json.dumps(
799
+ {
800
+ "parentUuid": "00000000-0000-4000-8000-000000000001",
801
+ "isSidechain": False,
802
+ "attachment": {
803
+ "type": "queued_command",
804
+ "prompt": text,
805
+ "source_uuid": "00000000-0000-4000-8000-000000000002",
806
+ "delivery_id": "00000000-0000-4000-8000-000000000003",
807
+ "commandMode": command_mode,
808
+ "origin": {"kind": origin_kind},
809
+ "timestamp": "2026-01-01T00:00:00.000Z",
810
+ },
811
+ "type": "attachment",
812
+ "uuid": "00000000-0000-4000-8000-000000000004",
813
+ "timestamp": "2026-01-01T00:00:00.000Z",
814
+ "rendered": [{"content": text}],
815
+ "renderedRole": "system",
816
+ }
817
+ )
818
+
819
+
820
+ def _notice_text(tool_use_id: str, status: str = "completed", exit_code: int = 0) -> str:
821
+ return (
822
+ f"<task-notification>\n<task-id>b1</task-id>\n<tool-use-id>{tool_use_id}</tool-use-id>\n"
823
+ f"<output-file>/tmp/b1.output</output-file>\n<status>{status}</status>\n"
824
+ f'<summary>Background command "Post the reply" {status} (exit code {exit_code})</summary>\n'
825
+ "</task-notification>"
826
+ )
827
+
828
+
829
+ def _background_reply(run_in_background: object = True) -> list[str]:
830
+ return [
831
+ _call("reply-1", "Bash", {"command": REPLY_SCRIPT_COMMAND, "run_in_background": run_in_background}),
832
+ _result("reply-1", text=BACKGROUND_STARTED_TEXT),
833
+ ]
834
+
835
+
836
+ def should_allow_a_resolve_after_a_notice_queued_mid_turn_reports_exit_code_zero(tmp_path: Path) -> None:
837
+ all_lines = [_prompt("Address the review threads."), *_background_reply()]
838
+ queued_notice = _queued_command(_notice_text("reply-1"), "task-notification", "task-notification")
839
+ transcript_path = _write_transcript(tmp_path, [*all_lines, queued_notice])
840
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
841
+ for each_notice_text in (
842
+ _notice_text("reply-1", "failed", 3),
843
+ _notice_text("another-call"),
844
+ _notice_text("another-call") + _notice_text("reply-1", "failed", 3),
845
+ ):
846
+ queued_notice = _queued_command(each_notice_text, "task-notification", "task-notification")
847
+ transcript_path = _write_transcript(tmp_path, [*all_lines, queued_notice])
848
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
849
+
850
+
851
+ def should_deny_a_resolve_after_a_prompt_the_user_queued_mid_turn(tmp_path: Path) -> None:
852
+ all_lines = [_prompt("Address the review threads."), *_threads_read(), _reply("reply-1"), _result("reply-1")]
853
+ transcript_path = _write_transcript(tmp_path, all_lines)
854
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
855
+ for each_origin_kind in ("human", "peer"):
856
+ queued_prompt = _queued_command("Stop, leave the threads open.", each_origin_kind, "prompt")
857
+ transcript_path = _write_transcript(tmp_path, [*all_lines, queued_prompt])
858
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
859
+
860
+
861
+ def should_wait_for_the_notice_whatever_truthy_value_the_background_flag_holds(tmp_path: Path) -> None:
862
+ for each_flag in ("true", 1):
863
+ all_lines = [_prompt("Address the review threads."), *_background_reply(each_flag)]
864
+ transcript_path = _write_transcript(tmp_path, all_lines)
865
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
866
+ transcript_path = _write_transcript(tmp_path, [*all_lines, _task_notice("reply-1")])
867
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
868
+
869
+
870
+ def should_wait_for_the_notice_when_a_result_says_the_call_moved_to_the_background(tmp_path: Path) -> None:
871
+ for each_result_text in (
872
+ "Command was manually backgrounded by user with ID: b1",
873
+ "partial output\nCommand did not complete within its 120s timeout and was moved to the background (ID: b1).",
874
+ ):
875
+ all_lines = [
876
+ _prompt("Address the review threads."),
877
+ _script_reply("reply-1"),
878
+ _result("reply-1", text=each_result_text),
879
+ ]
880
+ transcript_path = _write_transcript(tmp_path, all_lines)
881
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
882
+ transcript_path = _write_transcript(tmp_path, [*all_lines, _raw_notice(_notice_text("reply-1"))])
883
+ assert gate.decision_for(_resolve_payload(transcript_path)) is None
884
+
885
+
886
+ def should_deny_a_resolve_when_a_notice_appends_exit_code_zero_after_a_failure(tmp_path: Path) -> None:
887
+ appended_text = (
888
+ "<task-notification>\n<tool-use-id>reply-1</tool-use-id>\n<status>completed</status>\n"
889
+ '<summary>Background command "d" failed (exit code 3) (exit code 0)</summary>\n</task-notification>'
890
+ )
891
+ transcript_path = _write_transcript(
892
+ tmp_path, [_prompt("Address the review threads."), *_background_reply(), _raw_notice(appended_text)]
893
+ )
894
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
895
+
896
+
897
+ def should_read_a_versioned_or_exe_launcher_and_a_bare_script_path() -> None:
898
+ all_plain_commands = [
899
+ "python3.12 review_reply_post.py request --comment-id 9",
900
+ "python.exe review_reply_post.py request --comment-id 9",
901
+ "py.exe ci/review_reply_post.py request --comment-id 9",
902
+ "/usr/bin/python3.13 review_reply_post.py request --comment-id 9",
903
+ "/python3 review_reply_post.py request --comment-id 9",
904
+ "C:/Python312/python.exe review_reply_post.py request --comment-id 9",
905
+ "ci/review_reply_post.py request --comment-id 9",
906
+ ]
907
+ for each_command in all_plain_commands:
908
+ assert gate.reply_script_targets(each_command) == (frozenset(), frozenset({"9"})), each_command
909
+
910
+
911
+ BACKGROUND_WORDS_IN_A_BODY = (
912
+ "Command running in background with ID: b1\nThe job is backgrounded and running in the background."
913
+ )
914
+
915
+
916
+ def should_count_a_reply_tool_whose_result_quotes_the_background_words(tmp_path: Path) -> None:
917
+ all_lines = [
918
+ _prompt("Address the review threads."),
919
+ *_threads_read(),
920
+ _reply("reply-1"),
921
+ _result("reply-1", text=BACKGROUND_WORDS_IN_A_BODY),
922
+ ]
923
+ assert gate.decision_for(_resolve_payload(_write_transcript(tmp_path, all_lines))) is None
924
+
925
+
926
+ def should_spend_the_reply_when_a_resolve_result_quotes_the_background_words(tmp_path: Path) -> None:
927
+ all_lines = [
928
+ _prompt("Address the review threads."),
929
+ *_threads_read(),
930
+ _reply("reply-1"),
931
+ _result("reply-1"),
932
+ _resolve("resolve-1"),
933
+ _result("resolve-1", text=BACKGROUND_WORDS_IN_A_BODY),
934
+ ]
935
+ transcript_path = _write_transcript(tmp_path, all_lines)
936
+ assert _deny_reason(gate.decision_for(_resolve_payload(transcript_path))) == MISSING_REPLY_REASON
937
+
938
+
939
+ def should_count_a_shell_reply_whose_output_mentions_the_background_mid_line(tmp_path: Path) -> None:
940
+ all_lines = [
941
+ _prompt("Address the review threads."),
942
+ _script_reply("reply-1"),
943
+ _result("reply-1", text="posted: the job is backgrounded and running in the background"),
944
+ ]
945
+ assert gate.decision_for(_resolve_payload(_write_transcript(tmp_path, all_lines))) is None
@@ -288,3 +288,10 @@ def test_should_leave_unread_a_create_past_the_unwrap_depth(command: str) -> Non
288
288
  def test_should_read_no_create_in_text_past_the_unwrap_depth_that_holds_none() -> None:
289
289
  assert _placements("eval " * 18 + "'echo hi'") == []
290
290
 
291
+
292
+ def test_should_keep_two_creates_apart_when_only_one_names_its_repository_with_a_gh_repo_prefix() -> None:
293
+ command = "gh pr create --repo o/r ; GH_REPO=o/r env -u GH_REPO gh pr create"
294
+ all_command_words = [each_create.all_command_words for each_create in all_placed_creates(command, START)]
295
+ assert len(all_command_words) == 2
296
+ assert all_command_words[1][0] == "GH_REPO=o/r"
297
+
@@ -532,3 +532,24 @@ def test_should_read_create_words_after_a_separator_inside_a_quoted_value(separa
532
532
  )
533
533
  def test_should_not_read_a_later_quoted_create(command: str) -> None:
534
534
  assert "two" not in _branches(command)
535
+
536
+
537
+ @pytest.mark.parametrize(
538
+ "command",
539
+ [
540
+ "GH_REPO=other/thing env -u GH_REPO gh pr create --head two",
541
+ "gh pr create --repo other/thing --head two ; GH_REPO=other/thing env -u GH_REPO gh pr create --head two",
542
+ "gh pr create --repo other/thing --head two && GH_REPO=other/thing gh pr create --head two",
543
+ ],
544
+ )
545
+ def test_should_add_an_origin_call_for_every_create_a_gh_repo_prefix_names(command: str) -> None:
546
+ payload = {"tool_name": "Bash", "tool_input": {"command": command}, "cwd": MAIN_FLOOR_START}
547
+ all_calls = open_calls_from(payload)
548
+ assert ShellOpenCall(repository=("other", "thing"), branch="two", folder=MAIN_FLOOR_START) in all_calls
549
+ assert ShellOpenCall(repository=None, branch="two", folder=MAIN_FLOOR_START) in all_calls
550
+
551
+
552
+ def test_should_add_no_origin_call_for_a_create_that_names_its_own_repository() -> None:
553
+ command = "GH_REPO=a/b gh pr create --repo other/thing --head two"
554
+ payload = {"tool_name": "Bash", "tool_input": {"command": command}, "cwd": MAIN_FLOOR_START}
555
+ assert {each_call.repository for each_call in open_calls_from(payload)} == {("other", "thing")}
@@ -8,6 +8,7 @@ This family rewrites a Git Bash command when path conversion would change a revi
8
8
  - `blocking/msys_rev_path_rewriter.py` adds a narrow `MSYS2_ARG_CONV_EXCL` prefix for affected revision and path tokens.
9
9
  - `blocking/headless_claude_broker_gate.py` denies a headless `claude -p` or `claude --print` call and names the broker command to run instead.
10
10
  - `blocking/gh_global_account_switch_gate.py` denies `gh auth switch`, `gh auth login`, and `gh auth logout` and names the per-command `GH_TOKEN` form to run instead.
11
+ - `blocking/gh_api_budget_gate.py` keeps an agent from draining the GitHub API limit. It denies `gh run watch` and `gh pr checks --watch` and names a background loop that sleeps 60 seconds between `gh run view` calls. It denies a Bash `while`, `until` or `for` loop, a PowerShell `while`, `do`, `for` or `foreach` loop, or `watch`, that calls `gh` with under 60 seconds of `sleep` or `Start-Sleep` per pass, and an endless loop such as `while :` or `for (;;)` that calls `gh` with no sleep. It denies a `gh` read on the core REST limit, such as `gh pr view` or a `gh api` GET, while four fifths or more of that limit is used. `hooks_constants/gh_api_budget.py` reads the limit from `gh api rate_limit` at most once every five minutes and caches it in `<managed root>/cache/gh_api_budget.json`. Writes, local commands, `gh api graphql` and `gh api rate_limit` pass, and a limit it cannot read lets the call through.
11
12
  - `blocking/cloud_graphql_gate.py` denies the `gh` commands that call GraphQL (`gh api graphql` with any flags before the endpoint, `gh pr`, `gh issue`, `gh project` and `gh discussion` except `gh pr diff` and `gh pr list --web`, `gh status`, `gh org list`, `gh release list`, `gh release view` with a tag, `gh workflow run` without `--ref`, and a few `repo`, `label`, `search`, `ruleset` and `gist` commands, read past `if`, `!` and command substitutions) and an HTTP client or `python -c` style call to `api.github.com/graphql`, with or without a port, inside a cloud session, where the proxy answers GraphQL with HTTP 403, and names the REST routes to run instead.
12
13
  - `blocking/python_windows_invocation_gate.py` denies, on a Windows host, `python`, `python3` or `py` run with `-I` and without `-X utf8`, because `-I` ignores `PYTHONUTF8` and non-ASCII output then stops with `UnicodeEncodeError: 'charmap'`. Under the PowerShell tool it also denies a `python -c "..."` argument that holds `\"`, because PowerShell ends the string at that quote, and names a script file or a single-quoted here-string to use instead.
13
14
  - `blocking/transcript_root_search_gate.py` denies a recursive walk or content search that starts at a Claude transcript root, a `projects` folder directly under `.claude`, `.claude-<name>` or `.claude-profiles/<profile>`, with no project folder after it, or with a bare `*` or `**` segment that descends into every project folder. It reads `find`, `grep -r`, `rg`, `ls -R`, `Get-ChildItem -Recurse` or `-Depth`, `Select-String`, and the Grep and Glob tools, and expands variables the command assigns, such as `$p="..."`. A one-level name listing, such as `Get-ChildItem <root> -Directory -Filter "*id*"`, passes. The deny reason names the fix: list the project folders by name, then read the one transcript. The gate reads the root path as the command spells it, so a walk that starts above the root, or a glob that spells the root name or a project-name prefix such as `pro*` or `C--*`, passes.
@@ -22,6 +23,7 @@ This family rewrites a Git Bash command when path conversion would change a revi
22
23
  - `blocking/msys_rev_path_rewriter.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds. `ALL_BASH_HOSTED_HOOK_ENTRIES` selects the `Bash` tool.
23
24
  - `blocking/headless_claude_broker_gate.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds, for the Bash and PowerShell tools.
24
25
  - `blocking/gh_global_account_switch_gate.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds, for the Bash and PowerShell tools.
26
+ - `blocking/gh_api_budget_gate.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds, for the Bash and PowerShell tools, when the command calls `gh`. Its `gh api rate_limit` query times out after `10` seconds.
25
27
  - `blocking/cloud_graphql_gate.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds, for the Bash and PowerShell tools, when `CLAUDE_CODE_REMOTE` is `true`.
26
28
  - `blocking/python_windows_invocation_gate.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds, for the Bash and PowerShell tools. The `-I` check runs only when `os.name` is `nt`.
27
29
  - `blocking/transcript_root_search_gate.py` runs inside that dispatcher on `PreToolUse`, matcher `Bash`, timeout `60` seconds, for the Bash and PowerShell tools, and on its own on `PreToolUse`, matcher `Grep|Glob`, timeout `10` seconds in `hooks.json`.
@@ -38,6 +40,7 @@ Preconditions:
38
40
  - **Pre-call dispatch.** Input is a clean Bash command. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_bash_pre_tool_use_dispatcher.py -q`. The adjacent test observes the combined decision and hosted precedence.
39
41
  - **Revision rewrite.** Input is a Git command with `origin/main:.claude/settings.json`. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_msys_rev_path_rewriter.py -q`. The adjacent test observes the `origin/main:` exclusion prefix.
40
42
  - **Broker gate.** Input is a `claude -p task` command. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_headless_claude_broker_gate.py -q`. The adjacent test observes a denial that names the broker command.
43
+ - **GitHub API budget gate.** Input is `gh run watch 123`, `while :; do gh run view 1; sleep 10; done`, and `gh pr view 12` with a fresh cache file at 4000 of 5000 used. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_gh_api_budget_gate.py -q`. The adjacent test observes each denial and its reason, a pass for writes, `gh api graphql` and a 60 second loop, and one rate limit query per five minutes.
41
44
  - **Cloud GraphQL gate.** Input is a `gh api graphql` command with `CLAUDE_CODE_REMOTE=true`. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_cloud_graphql_gate.py -q`. The adjacent test observes a denial that names the REST routes, and silence outside a cloud session.
42
45
  - **Python Windows invocation gate.** Input is `python -I s.py` on Windows, and a PowerShell `python -c "print(\"x\")"`. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_python_windows_invocation_gate.py -q`. The adjacent test observes a denial that names `-X utf8` or the here-string, and a pass for `-IX utf8`, `-I -X utf8`, and the Bash tool.
43
46
  - **Transcript root search gate.** Input is `Get-ChildItem $p -Recurse -File -Filter *.jsonl` with `$p` bound to `~/.claude-profiles/work/projects`, and a Grep call whose path is `~/.claude/projects`. Run `python -m pytest packages/claude-dev-env/hooks/blocking/test_transcript_root_search_gate.py -q`. The adjacent test observes a denial that names the list-then-read fix, and a pass for `Get-ChildItem $p -Directory -Filter "*01WUQeBG*"` and a walk inside one project folder.