claude-dev-env 8.116.5 → 8.117.1

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 (117) hide show
  1. package/bin/ever-registered-hooks.mjs +2 -0
  2. package/bin/ever-registered-hooks.test.mjs +4 -0
  3. package/bin/install-constants.mjs +3 -1
  4. package/bin/install.cursor-rules.test.mjs +64 -2
  5. package/bin/install.mjs +15 -2
  6. package/bin/install.prune.test.mjs +92 -0
  7. package/bin/install.test.mjs +1 -0
  8. package/docs/model-choice/escalation.md +3 -3
  9. package/docs/model-choice/subagents-and-fan-out.md +2 -2
  10. package/docs/model-choice/which-model.md +1 -1
  11. package/docs/references/working-style.md +1 -0
  12. package/docs/rule-guides/question-presentation.md +6 -0
  13. package/docs/rule-guides/session-title.md +8 -9
  14. package/docs/rule-guides/subagent-model-effort.md +13 -13
  15. package/hooks/advisory/owned_pr_review_loop.py +2 -3
  16. package/hooks/blocking/bash_post_call_dispatcher.py +17 -9
  17. package/hooks/blocking/bash_pre_tool_use_dispatcher.py +12 -8
  18. package/hooks/blocking/final_reply_gate.py +5 -9
  19. package/hooks/blocking/open_tasks_stop_gate.py +9 -20
  20. package/hooks/blocking/open_tasks_transcript.py +8 -3
  21. package/hooks/blocking/pii_prevention_blocker_parts/repository_exemption.py +7 -2
  22. package/hooks/blocking/question_brief_gate.py +382 -0
  23. package/hooks/blocking/step_note_gate.py +5 -2
  24. package/hooks/blocking/task_subject_length_gate.py +111 -0
  25. package/hooks/blocking/test_bash_post_call_dispatcher.py +36 -1
  26. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +57 -0
  27. package/hooks/blocking/test_final_reply_gate.py +26 -2
  28. package/hooks/blocking/test_open_tasks_stop_gate.py +21 -11
  29. package/hooks/blocking/test_question_brief_gate.py +916 -0
  30. package/hooks/blocking/test_step_note_gate.py +26 -0
  31. package/hooks/blocking/test_task_subject_length_gate.py +221 -0
  32. package/hooks/blocking/test_turn_review_child.py +166 -0
  33. package/hooks/blocking/test_turn_review_claim.py +194 -0
  34. package/hooks/blocking/test_turn_review_findings.py +124 -1
  35. package/hooks/blocking/test_turn_review_gate.py +1312 -10
  36. package/hooks/blocking/test_turn_review_repositories.py +65 -0
  37. package/hooks/blocking/test_turn_review_snapshot.py +8 -0
  38. package/hooks/blocking/test_turn_review_state.py +132 -0
  39. package/hooks/blocking/test_turn_review_transcript.py +125 -0
  40. package/hooks/blocking/tests/test_repository_exemption.py +11 -0
  41. package/hooks/blocking/turn_review_child.py +145 -0
  42. package/hooks/blocking/turn_review_claim.py +306 -0
  43. package/hooks/blocking/turn_review_findings.py +173 -13
  44. package/hooks/blocking/turn_review_gate.py +259 -262
  45. package/hooks/blocking/turn_review_repositories.py +58 -0
  46. package/hooks/blocking/turn_review_snapshot.py +5 -1
  47. package/hooks/blocking/turn_review_state.py +260 -0
  48. package/hooks/blocking/turn_review_transcript.py +158 -0
  49. package/hooks/features/bash-dispatchers.md +1 -3
  50. package/hooks/features/conduct-gates.md +19 -9
  51. package/hooks/features/lifecycle-cleanup.md +1 -1
  52. package/hooks/features/spawn-routing.md +1 -1
  53. package/hooks/hooks.json +24 -11
  54. package/hooks/hooks_constants/final_reply_gate_constants.py +0 -3
  55. package/hooks/hooks_constants/hook_time_budget.py +61 -0
  56. package/hooks/hooks_constants/hosted_hook_runner.py +51 -21
  57. package/hooks/hooks_constants/hosted_hook_runner_constants.py +3 -0
  58. package/hooks/hooks_constants/nested_project_hooks_constants.py +8 -0
  59. package/hooks/hooks_constants/once_per_session_constants.py +1 -0
  60. package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +0 -4
  61. package/hooks/hooks_constants/owned_pr_review_loop_constants.py +0 -4
  62. package/hooks/hooks_constants/pre_tool_use_context_runner.py +23 -3
  63. package/hooks/hooks_constants/pre_tool_use_context_runner_constants.py +5 -0
  64. package/hooks/hooks_constants/pre_tool_use_stdin.py +1 -1
  65. package/hooks/hooks_constants/question_brief_gate_constants.py +66 -0
  66. package/hooks/hooks_constants/session_title_constants.py +8 -29
  67. package/hooks/hooks_constants/stop_hook_hold.py +54 -0
  68. package/hooks/hooks_constants/stop_hook_hold_constants.py +9 -0
  69. package/hooks/hooks_constants/subagent_effort_levels.json +6 -0
  70. package/hooks/hooks_constants/subagent_effort_route_constants.py +20 -19
  71. package/hooks/hooks_constants/task_subject_length_gate_constants.py +43 -0
  72. package/hooks/hooks_constants/test_hook_time_budget.py +104 -0
  73. package/hooks/hooks_constants/test_hosted_hook_runner.py +12 -0
  74. package/hooks/hooks_constants/test_pre_tool_use_context_runner.py +16 -0
  75. package/hooks/hooks_constants/test_pre_tool_use_stdin.py +6 -0
  76. package/hooks/hooks_constants/test_stop_hook_hold.py +40 -0
  77. package/hooks/hooks_constants/thread_spawn_pace_hook_constants.py +1 -2
  78. package/hooks/hooks_constants/turn_review_gate_constants.py +246 -12
  79. package/hooks/lifecycle/nested_project_hooks.py +34 -4
  80. package/hooks/lifecycle/test_nested_project_hooks.py +52 -0
  81. package/hooks/once_per_session.py +33 -4
  82. package/hooks/open_task_asks.py +1 -1
  83. package/hooks/owned_pr_session_record.py +1 -76
  84. package/hooks/routing/subagent_effort_route.py +107 -13
  85. package/hooks/routing/subagent_effort_route_hook.py +9 -8
  86. package/hooks/routing/test_subagent_effort_route.py +201 -14
  87. package/hooks/routing/test_subagent_effort_route_hook.py +56 -0
  88. package/hooks/routing/test_thread_spawn_pace_hook.py +32 -0
  89. package/hooks/routing/thread_spawn_pace_hook.py +3 -3
  90. package/hooks/session/background_silence_prompt.py +1 -1
  91. package/hooks/session/test_background_silence_prompt.py +8 -0
  92. package/hooks/session/test_tracking_nudge.py +125 -2
  93. package/hooks/session/tracking_nudge.py +28 -18
  94. package/hooks/session_title_transcript.py +216 -0
  95. package/hooks/test_once_per_session.py +10 -0
  96. package/hooks/test_owned_pr_session_record.py +0 -26
  97. package/hooks/test_session_title_transcript.py +142 -0
  98. package/hooks/validation/post_tool_use_dispatcher.py +38 -21
  99. package/hooks/validation/test_post_tool_use_dispatcher.py +43 -0
  100. package/package.json +1 -1
  101. package/rules/question-presentation.md +4 -4
  102. package/rules/subagent-model-effort.md +1 -1
  103. package/scripts/account_broker_support.py +24 -5
  104. package/scripts/claude_account_worker.py +4 -3
  105. package/scripts/dev_env_scripts_constants/account_broker_constants.py +1 -0
  106. package/scripts/policy_lint/adapter_configuration.py +12 -11
  107. package/scripts/policy_lint/config/approved_test_pairs.py +27 -0
  108. package/scripts/policy_lint/config/constants.py +2 -2
  109. package/scripts/test_account_broker_support.py +43 -1
  110. package/scripts/test_claude_account_worker.py +1 -0
  111. package/scripts/tests/test_adapter_configuration.py +10 -2
  112. package/scripts/tests/test_policy_lint_rules_pairing.py +24 -0
  113. package/hooks/blocking/owned_pr_merge_stop_gate.py +0 -185
  114. package/hooks/blocking/session_title_turn_gate.py +0 -417
  115. package/hooks/blocking/test_owned_pr_merge_stop_gate.py +0 -254
  116. package/hooks/blocking/test_session_title_turn_gate.py +0 -396
  117. package/hooks/hooks_constants/owned_pr_merge_stop_gate_constants.py +0 -40
@@ -0,0 +1,65 @@
1
+ import subprocess
2
+ import tempfile
3
+ from pathlib import Path
4
+
5
+ import pytest
6
+
7
+ from turn_review_repositories import _opted_in_repository_roots, _opted_in_repository_slugs
8
+
9
+
10
+ @pytest.fixture(autouse=True)
11
+ def isolated_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
12
+ home_directory = tmp_path / "home"
13
+ home_directory.mkdir()
14
+ state_directory = tmp_path / "state"
15
+ state_directory.mkdir()
16
+ monkeypatch.setenv("HOME", str(home_directory))
17
+ monkeypatch.setenv("USERPROFILE", str(home_directory))
18
+ monkeypatch.setattr(tempfile, "tempdir", str(state_directory))
19
+ return home_directory
20
+
21
+
22
+ def checkout(folder: Path, origin_slug: str) -> Path:
23
+ folder.mkdir(parents=True, exist_ok=True)
24
+ subprocess.run(["git", "init", "-q", "-b", "main"], cwd=folder, check=True, capture_output=True)
25
+ subprocess.run(
26
+ ["git", "remote", "add", "origin", f"https://github.com/{origin_slug}.git"],
27
+ cwd=folder,
28
+ check=True,
29
+ capture_output=True,
30
+ )
31
+ return folder
32
+
33
+
34
+ def test_should_read_the_listed_slugs_without_letter_case_or_blanks(
35
+ monkeypatch: pytest.MonkeyPatch,
36
+ ) -> None:
37
+ monkeypatch.setenv("CLAUDE_TURN_REVIEW_REPOSITORIES", " Owner/App , ,owner/LIB, ")
38
+ assert _opted_in_repository_slugs() == frozenset({"owner/app", "owner/lib"})
39
+
40
+
41
+ def test_should_take_only_a_checkout_whose_origin_is_listed(
42
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
43
+ ) -> None:
44
+ project_directory = tmp_path / "workspace"
45
+ listed_checkout = checkout(project_directory / "app", "owner/app")
46
+ checkout(project_directory / "docs", "owner/docs")
47
+ (project_directory / "notes").mkdir()
48
+ (project_directory / "readme.txt").write_text("notes\n")
49
+ monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project_directory))
50
+ assert _opted_in_repository_roots(frozenset({"owner/app"})) == [listed_checkout.resolve()]
51
+
52
+
53
+ def test_should_list_the_project_folder_first_then_its_children_by_name(
54
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
55
+ ) -> None:
56
+ project_directory = checkout(tmp_path / "workspace", "owner/workspace")
57
+ zeta_checkout = checkout(project_directory / "zeta", "owner/zeta")
58
+ alpha_checkout = checkout(project_directory / "alpha", "owner/alpha")
59
+ monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project_directory))
60
+ all_slugs = frozenset({"owner/workspace", "owner/zeta", "owner/alpha"})
61
+ assert _opted_in_repository_roots(all_slugs) == [
62
+ project_directory.resolve(),
63
+ alpha_checkout.resolve(),
64
+ zeta_checkout.resolve(),
65
+ ]
@@ -3,6 +3,7 @@ from pathlib import Path
3
3
 
4
4
  import pytest
5
5
 
6
+ from hooks_constants.hook_time_budget import time_budget
6
7
  from turn_review_snapshot import turn_changed_paths, working_tree_snapshot
7
8
 
8
9
 
@@ -105,3 +106,10 @@ def test_should_keep_the_leading_space_of_a_changed_path(repository: Path) -> No
105
106
  (repository / " notes.txt").write_text("new\n")
106
107
  turn_commit = working_tree_snapshot(repository, base_commit)
107
108
  assert turn_changed_paths(repository, base_commit, turn_commit) == [" notes.txt"]
109
+
110
+
111
+ def test_should_take_no_snapshot_once_the_time_budget_is_spent(repository: Path) -> None:
112
+ (repository / "service.py").write_text("call(1)\n")
113
+ with time_budget(0):
114
+ assert working_tree_snapshot(repository, None) is None
115
+ assert working_tree_snapshot(repository, None) is not None
@@ -0,0 +1,132 @@
1
+ import json
2
+ import os
3
+ import tempfile
4
+ from pathlib import Path
5
+
6
+ import pytest
7
+
8
+ from turn_review_state import (
9
+ _Finding,
10
+ _is_lock_taken,
11
+ _load_state,
12
+ _log,
13
+ _next_finding_id,
14
+ _release_and_close,
15
+ _ReviewState,
16
+ _save_state,
17
+ _state_path,
18
+ )
19
+
20
+ SESSION_ID = "state-session"
21
+ BASE_COMMIT = "a" * 40
22
+ TURN_COMMIT = "b" * 40
23
+
24
+
25
+ @pytest.fixture(autouse=True)
26
+ def isolated_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
27
+ home_directory = tmp_path / "home"
28
+ home_directory.mkdir()
29
+ state_directory = tmp_path / "state"
30
+ state_directory.mkdir()
31
+ monkeypatch.setenv("HOME", str(home_directory))
32
+ monkeypatch.setenv("USERPROFILE", str(home_directory))
33
+ monkeypatch.setattr(tempfile, "tempdir", str(state_directory))
34
+ return home_directory
35
+
36
+
37
+ def log_lines(home_directory: Path) -> list[str]:
38
+ log_path = home_directory / ".claude" / "logs" / "turn-review.log"
39
+ return log_path.read_text().splitlines() if log_path.exists() else []
40
+
41
+
42
+ def test_should_load_every_field_a_save_wrote() -> None:
43
+ saved_state = _ReviewState(
44
+ all_baselines={"/work/app": BASE_COMMIT},
45
+ blocks_in_a_row=2,
46
+ is_turn_open=True,
47
+ last_wake_at=100.5,
48
+ open_turn_prompt_at=90.0,
49
+ all_findings={
50
+ "TR-3": _Finding(
51
+ "src/service.py:3 - drops the retry",
52
+ "/work/app",
53
+ BASE_COMMIT,
54
+ TURN_COMMIT,
55
+ "upheld",
56
+ "Nothing retries.",
57
+ )
58
+ },
59
+ )
60
+ _save_state(SESSION_ID, saved_state)
61
+ assert _state_path(SESSION_ID).name == "turn-review-state-session.json"
62
+ assert _load_state(SESSION_ID) == saved_state
63
+
64
+
65
+ def test_should_leave_out_saved_entries_in_another_form() -> None:
66
+ finding_record = {
67
+ "text": "src/service.py:3 - drops the retry",
68
+ "repository": "/work/app",
69
+ "base": BASE_COMMIT,
70
+ "turn": TURN_COMMIT,
71
+ "status": "open",
72
+ "judge_reason": None,
73
+ }
74
+ _state_path(SESSION_ID).write_text(
75
+ json.dumps(
76
+ {
77
+ "baselines": {"/work/app": BASE_COMMIT, "/work/lib": "HEAD~1"},
78
+ "blocks_in_a_row": 1,
79
+ "turn_open": 1,
80
+ "last_wake_at": True,
81
+ "findings": {
82
+ "TR-1": finding_record,
83
+ "TR-01": finding_record,
84
+ "TR-2": {**finding_record, "status": "closed"},
85
+ "TR-4": {**finding_record, "turn": "main"},
86
+ },
87
+ }
88
+ )
89
+ )
90
+ loaded_state = _load_state(SESSION_ID)
91
+ assert loaded_state.all_baselines == {"/work/app": BASE_COMMIT}
92
+ assert loaded_state.blocks_in_a_row == 1
93
+ assert loaded_state.is_turn_open is False
94
+ assert loaded_state.last_wake_at is None
95
+ assert list(loaded_state.all_findings) == ["TR-1"]
96
+
97
+
98
+ def test_should_give_an_empty_state_for_a_file_that_is_not_json() -> None:
99
+ _state_path(SESSION_ID).write_text("{not json")
100
+ assert _load_state(SESSION_ID) == _ReviewState()
101
+
102
+
103
+ def test_should_number_the_next_finding_after_the_highest_id() -> None:
104
+ finding = _Finding("text", "/work/app", BASE_COMMIT, TURN_COMMIT)
105
+ review_state = _ReviewState(all_findings={"TR-2": finding, "TR-10": finding})
106
+ assert _next_finding_id(review_state) == "TR-11"
107
+ assert _next_finding_id(_ReviewState()) == "TR-1"
108
+
109
+
110
+ def test_should_log_one_dated_line_each_call_and_log_a_failed_save(isolated_home: Path) -> None:
111
+ _log("[turn_review_gate] first\n")
112
+ _log("[turn_review_gate] second\n")
113
+ _state_path(SESSION_ID).mkdir()
114
+ _save_state(SESSION_ID, _ReviewState(blocks_in_a_row=1))
115
+ all_lines = log_lines(isolated_home)
116
+ assert len(all_lines) == 3
117
+ assert all_lines[0].endswith(" [turn_review_gate] first")
118
+ assert all_lines[0][:4].isdigit()
119
+ assert all_lines[1].endswith(" [turn_review_gate] second")
120
+ assert "[turn_review_gate] the review state was not saved: " in all_lines[2]
121
+
122
+
123
+ def test_should_refuse_a_second_lock_until_the_first_is_released(tmp_path: Path) -> None:
124
+ lock_path = tmp_path / "state.lock"
125
+ lock_path.write_text("x")
126
+ first_descriptor = os.open(lock_path, os.O_RDWR)
127
+ second_descriptor = os.open(lock_path, os.O_RDWR)
128
+ assert _is_lock_taken(first_descriptor) is True
129
+ assert _is_lock_taken(second_descriptor) is False
130
+ _release_and_close(first_descriptor, True)
131
+ assert _is_lock_taken(second_descriptor) is True
132
+ _release_and_close(second_descriptor, True)
@@ -0,0 +1,125 @@
1
+ import json
2
+ import tempfile
3
+ from pathlib import Path
4
+
5
+ import pytest
6
+
7
+ from turn_review_transcript import _ReversedTranscript, _reply_text, _typed_user_messages
8
+
9
+
10
+ @pytest.fixture(autouse=True)
11
+ def isolated_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
12
+ home_directory = tmp_path / "home"
13
+ home_directory.mkdir()
14
+ state_directory = tmp_path / "state"
15
+ state_directory.mkdir()
16
+ monkeypatch.setenv("HOME", str(home_directory))
17
+ monkeypatch.setenv("USERPROFILE", str(home_directory))
18
+ monkeypatch.setattr(tempfile, "tempdir", str(state_directory))
19
+ return home_directory
20
+
21
+
22
+ def assistant_entry(message_id: str, text: str) -> dict[str, object]:
23
+ return {
24
+ "type": "assistant",
25
+ "message": {"id": message_id, "content": [{"type": "text", "text": text}]},
26
+ }
27
+
28
+
29
+ def typed_entry(text: str, **all_extra_fields: object) -> dict[str, object]:
30
+ return {
31
+ "type": "user",
32
+ "origin": {"kind": "human"},
33
+ "message": {"content": text},
34
+ **all_extra_fields,
35
+ }
36
+
37
+
38
+ def tool_result_entry() -> dict[str, object]:
39
+ return {
40
+ "type": "user",
41
+ "toolUseResult": {"stdout": "ok"},
42
+ "message": {"content": [{"type": "tool_result", "content": "ok"}]},
43
+ }
44
+
45
+
46
+ def stop_input(
47
+ tmp_path: Path, all_entries: list[dict[str, object]], **all_extra_fields: object
48
+ ) -> dict:
49
+ transcript_path = tmp_path / "session.jsonl"
50
+ transcript_path.write_text(
51
+ "\n".join(json.dumps(each_entry) for each_entry in all_entries) + "\n"
52
+ )
53
+ return {"transcript_path": str(transcript_path), **all_extra_fields}
54
+
55
+
56
+ def test_should_take_the_reply_field_even_when_it_is_empty(tmp_path: Path) -> None:
57
+ hook_input = stop_input(
58
+ tmp_path, [assistant_entry("m1", "Dismiss TR-1: an older reply")], last_assistant_message=""
59
+ )
60
+ assert _reply_text(hook_input, _ReversedTranscript(hook_input)) == ""
61
+
62
+
63
+ def test_should_join_the_entries_of_the_last_assistant_message_oldest_first(tmp_path: Path) -> None:
64
+ hook_input = stop_input(
65
+ tmp_path,
66
+ [
67
+ assistant_entry("m1", "earlier reply"),
68
+ typed_entry("next question"),
69
+ assistant_entry("m2", "first part"),
70
+ assistant_entry("m2", "second part"),
71
+ tool_result_entry(),
72
+ ],
73
+ )
74
+ assert _reply_text(hook_input, _ReversedTranscript(hook_input)) == "first part\nsecond part"
75
+
76
+
77
+ def test_should_still_see_every_entry_after_the_reply_read_part_of_the_transcript(
78
+ tmp_path: Path,
79
+ ) -> None:
80
+ hook_input = stop_input(
81
+ tmp_path, [typed_entry("first ask"), assistant_entry("m1", "the reply")]
82
+ )
83
+ transcript = _ReversedTranscript(hook_input)
84
+ assert _reply_text(hook_input, transcript) == "the reply"
85
+ assert _typed_user_messages(transcript) == ["first ask"]
86
+ assert len(list(transcript)) == 2
87
+
88
+
89
+ def test_should_keep_the_last_five_typed_messages_oldest_first_and_cut_each(tmp_path: Path) -> None:
90
+ hook_input = stop_input(
91
+ tmp_path,
92
+ [
93
+ typed_entry("first"),
94
+ typed_entry("second"),
95
+ typed_entry("x" * 2500),
96
+ typed_entry("<system-reminder>injected</system-reminder>"),
97
+ typed_entry("meta words", isMeta=True),
98
+ typed_entry("third"),
99
+ {"type": "user", "origin": {"kind": "peer"}, "message": {"content": "peer words"}},
100
+ typed_entry("fourth"),
101
+ assistant_entry("m1", "a reply"),
102
+ typed_entry("fifth"),
103
+ ],
104
+ )
105
+ assert _typed_user_messages(_ReversedTranscript(hook_input)) == [
106
+ "second",
107
+ "x" * 2000,
108
+ "third",
109
+ "fourth",
110
+ "fifth",
111
+ ]
112
+
113
+
114
+ def test_should_log_a_line_that_nests_too_deep_and_read_the_lines_before_it(
115
+ tmp_path: Path, isolated_home: Path
116
+ ) -> None:
117
+ hook_input = stop_input(tmp_path, [typed_entry("the ask")])
118
+ with Path(hook_input["transcript_path"]).open("a") as transcript_file:
119
+ transcript_file.write("[" * 100_000 + "]" * 100_000 + "\n")
120
+ assert _typed_user_messages(_ReversedTranscript(hook_input)) == ["the ask"]
121
+ log_text = (isolated_home / ".claude" / "logs" / "turn-review.log").read_text()
122
+ assert (
123
+ log_text.count("[turn_review_gate] a transcript line could not be read: it nests too deep")
124
+ == 1
125
+ )
@@ -15,6 +15,7 @@ import subprocess
15
15
  from pathlib import Path
16
16
 
17
17
  import pytest
18
+ from hooks_constants.hook_time_budget import time_budget
18
19
  from pii_prevention_blocker_parts.repository_exemption import (
19
20
  _is_repository_exempt_from_pii_scan,
20
21
  _owner_repo_slug_from_origin_url,
@@ -110,3 +111,13 @@ def test_repository_origin_slug_reads_the_origin_remote(tmp_path: Path) -> None:
110
111
  repository_root = tmp_path / "repo"
111
112
  _init_repo_with_github_origin(repository_root, "SlugOwner/slug-repo")
112
113
  assert repository_origin_slug(repository_root) == "slugowner/slug-repo"
114
+
115
+
116
+ def test_should_read_no_origin_slug_once_the_time_budget_is_spent(tmp_path: Path) -> None:
117
+ subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
118
+ subprocess.run(
119
+ ["git", "remote", "add", "origin", "https://github.com/Owner/Repo.git"], cwd=tmp_path, check=True
120
+ )
121
+ with time_budget(0):
122
+ assert repository_origin_slug(tmp_path) is None
123
+ assert repository_origin_slug(tmp_path) == "owner/repo"
@@ -0,0 +1,145 @@
1
+ """Start the review and judge runs for the turn review gate.
2
+
3
+ Each run is a separate ``claude -p`` process that starts in the checkout it
4
+ reads::
5
+
6
+ review -> claude -p "/code-review low A..B"
7
+ judge -> claude -p --model sonnet --effort high, prompt on stdin
8
+
9
+ The review and judge
10
+ runs carry ``CLAUDE_TURN_REVIEW_CHILD=1`` and none of the parent session's id,
11
+ messaging or ingress variables. Each may read
12
+ files and run ``git diff``, ``git show`` and ``git log``; every other tool is
13
+ denied. A run that times out, cannot start or ends with another exit code than
14
+ 0 writes one log line and gives no output.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import os
20
+ import shutil
21
+ import subprocess
22
+ from dataclasses import dataclass
23
+ from pathlib import Path
24
+
25
+ from hooks_constants.subprocess_window import hidden_window_creation_flags
26
+ from hooks_constants.turn_review_gate_constants import (
27
+ ALL_PARENT_SESSION_VARIABLES,
28
+ ALL_REVIEW_ALLOWED_TOOLS,
29
+ ALL_REVIEW_DISALLOWED_TOOLS,
30
+ JUDGE_EFFORT,
31
+ JUDGE_MODEL_ALIAS,
32
+ REVIEW_CHILD_ENVIRONMENT_VARIABLE,
33
+ REVIEW_CHILD_MARKER,
34
+ REVIEW_EFFORT,
35
+ REVIEW_EXECUTABLE_NAME,
36
+ REVIEW_MODEL_ALIAS,
37
+ REVIEW_MODEL_EFFORT,
38
+ REVIEW_PROMPT_TEMPLATE,
39
+ REVIEW_SETTINGS_TEXT,
40
+ REVIEW_SETTING_SOURCES_TEXT,
41
+ REVIEW_TOOLS_TEXT,
42
+ )
43
+ from turn_review_state import _log
44
+
45
+
46
+ def _review_command(base_commit: str, turn_commit: str) -> list[str]:
47
+ review_prompt = REVIEW_PROMPT_TEMPLATE.format(
48
+ effort=REVIEW_EFFORT, base_commit=base_commit, turn_commit=turn_commit
49
+ )
50
+ return [
51
+ shutil.which(REVIEW_EXECUTABLE_NAME) or REVIEW_EXECUTABLE_NAME,
52
+ "-p",
53
+ review_prompt,
54
+ "--model",
55
+ REVIEW_MODEL_ALIAS,
56
+ "--effort",
57
+ REVIEW_MODEL_EFFORT,
58
+ *_read_only_child_options(),
59
+ ]
60
+
61
+
62
+ def _judge_command() -> list[str]:
63
+ return [
64
+ shutil.which(REVIEW_EXECUTABLE_NAME) or REVIEW_EXECUTABLE_NAME,
65
+ "-p",
66
+ "--model",
67
+ JUDGE_MODEL_ALIAS,
68
+ "--effort",
69
+ JUDGE_EFFORT,
70
+ *_read_only_child_options(),
71
+ ]
72
+
73
+
74
+ def _read_only_child_options() -> list[str]:
75
+ return [
76
+ "--output-format",
77
+ "json",
78
+ "--permission-mode",
79
+ "dontAsk",
80
+ "--setting-sources",
81
+ REVIEW_SETTING_SOURCES_TEXT,
82
+ "--tools",
83
+ REVIEW_TOOLS_TEXT,
84
+ "--no-session-persistence",
85
+ "--allowedTools",
86
+ *ALL_REVIEW_ALLOWED_TOOLS,
87
+ "--disallowedTools",
88
+ *ALL_REVIEW_DISALLOWED_TOOLS,
89
+ "--settings",
90
+ REVIEW_SETTINGS_TEXT,
91
+ ]
92
+
93
+
94
+ def _review_environment() -> dict[str, str]:
95
+ all_child_environment_variables = {
96
+ each_name: each_value
97
+ for each_name, each_value in os.environ.items()
98
+ if each_name not in ALL_PARENT_SESSION_VARIABLES
99
+ }
100
+ all_child_environment_variables[REVIEW_CHILD_ENVIRONMENT_VARIABLE] = REVIEW_CHILD_MARKER
101
+ return all_child_environment_variables
102
+
103
+
104
+ @dataclass(frozen=True)
105
+ class _ChildRun:
106
+ repository_root: Path
107
+ all_command_parts: list[str]
108
+ prompt_text: str | None
109
+ failure_template: str
110
+
111
+ def write_failure(self, failure_text: str) -> None:
112
+ _log(
113
+ self.failure_template.format(
114
+ repository=self.repository_root.name, reason=failure_text
115
+ )
116
+ )
117
+
118
+
119
+ def _child_stdout(child_run: _ChildRun, remaining_seconds: float) -> str | None:
120
+ closed_stdin = subprocess.DEVNULL if child_run.prompt_text is None else None
121
+ try:
122
+ completed_process = subprocess.run(
123
+ child_run.all_command_parts,
124
+ check=False,
125
+ capture_output=True,
126
+ text=True,
127
+ encoding="utf-8",
128
+ errors="replace",
129
+ timeout=remaining_seconds,
130
+ cwd=str(child_run.repository_root),
131
+ env=_review_environment(),
132
+ creationflags=hidden_window_creation_flags(),
133
+ stdin=closed_stdin,
134
+ input=child_run.prompt_text,
135
+ )
136
+ except subprocess.TimeoutExpired:
137
+ child_run.write_failure("timed out")
138
+ return None
139
+ except OSError as start_error:
140
+ child_run.write_failure(str(start_error))
141
+ return None
142
+ if completed_process.returncode != 0:
143
+ child_run.write_failure(f"exit {completed_process.returncode}")
144
+ return None
145
+ return completed_process.stdout