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
@@ -1,254 +0,0 @@
1
- """Behavior tests for the owned pull request merge Stop gate."""
2
-
3
- from __future__ import annotations
4
-
5
- import io
6
- import json
7
- import subprocess
8
- from pathlib import Path
9
-
10
- import pytest
11
-
12
- import owned_pr_merge_stop_gate
13
- import owned_pr_session_record
14
-
15
- SESSION_ID = "session-abc"
16
- PULL_REQUEST = {
17
- "owner": "jl-cmd",
18
- "repository": "snake-game",
19
- "number": "9",
20
- "url": "https://github.com/jl-cmd/snake-game/pull/9",
21
- }
22
- RECORDED_PULL_REQUEST = {
23
- "owner": "jl-cmd-projects",
24
- "repository": "claude-dev-env",
25
- "number": "2300",
26
- "url": "https://github.com/jl-cmd-projects/claude-dev-env/pull/2300",
27
- }
28
- RECORDED_HEAD = "6ea405f886de9e3a434376a0b580cc3648d11a3c"
29
- FIRST_HEAD = "abc1234def5678abc1234def5678abc1234def56"
30
- PUSHED_HEAD = "9999999aaaaaaa9999999aaaaaaa9999999aaaaa"
31
-
32
-
33
- @pytest.fixture(autouse=True)
34
- def isolated_temp_folder(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
35
- monkeypatch.setattr(owned_pr_session_record.tempfile, "gettempdir", lambda: str(tmp_path))
36
- monkeypatch.setenv("HOME", str(tmp_path))
37
-
38
-
39
- def _gh_reports(
40
- monkeypatch: pytest.MonkeyPatch, state_text: str, exit_code: int = 0
41
- ) -> list[list[str]]:
42
- all_calls: list[list[str]] = []
43
-
44
- def fake_run(all_arguments: list[str], **_: object) -> subprocess.CompletedProcess[str]:
45
- all_calls.append(all_arguments)
46
- return subprocess.CompletedProcess(all_arguments, exit_code, stdout=state_text, stderr="")
47
-
48
- monkeypatch.setattr(owned_pr_merge_stop_gate.subprocess, "run", fake_run)
49
- return all_calls
50
-
51
-
52
- def _run_gate(
53
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
54
- ) -> str:
55
- hook_input = {
56
- "hook_event_name": "Stop",
57
- "session_id": SESSION_ID,
58
- "last_assistant_message": reply_text,
59
- }
60
- monkeypatch.setattr(
61
- "sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
62
- )
63
- assert owned_pr_merge_stop_gate.main() == 0
64
- return capsys.readouterr().out
65
-
66
-
67
- def test_main_should_block_while_a_recorded_pull_request_is_open(
68
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
69
- ) -> None:
70
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
71
- all_calls = _gh_reports(monkeypatch, "open\n")
72
- decision = json.loads(_run_gate(monkeypatch, capsys, "Draft PR is open."))
73
- assert decision["decision"] == "block"
74
- assert PULL_REQUEST["url"] in decision["reason"]
75
- assert "/efficient-review" in decision["reason"]
76
- assert all_calls == [
77
- ["gh", "api", "repos/jl-cmd/snake-game/pulls/9", "--jq", '.state + " " + .head.sha']
78
- ]
79
-
80
-
81
- def test_main_should_allow_once_the_pull_request_is_closed(
82
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
83
- ) -> None:
84
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
85
- _gh_reports(monkeypatch, "closed\n")
86
- assert _run_gate(monkeypatch, capsys, "Merged.") == ""
87
-
88
-
89
- def test_main_should_allow_a_blocked_line_that_names_the_pull_request(
90
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
91
- ) -> None:
92
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
93
- _gh_reports(monkeypatch, "open\n")
94
- reply_text = "Review is clean.\nBlocked: jl-cmd/snake-game#9 - the owner must approve the merge"
95
- assert _run_gate(monkeypatch, capsys, reply_text) == ""
96
-
97
-
98
- def test_main_should_block_when_the_blocked_line_names_another_pull_request(
99
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
100
- ) -> None:
101
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
102
- _gh_reports(monkeypatch, "open\n")
103
- reply_text = "Blocked: jl-cmd/snake-game#90 - the owner must approve the merge"
104
- assert json.loads(_run_gate(monkeypatch, capsys, reply_text))["decision"] == "block"
105
-
106
-
107
- def test_main_should_allow_when_the_state_lookup_fails(
108
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
109
- ) -> None:
110
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
111
- _gh_reports(monkeypatch, "", exit_code=1)
112
- assert _run_gate(monkeypatch, capsys, "Draft PR is open.") == ""
113
-
114
-
115
- def test_main_should_allow_a_session_that_opened_no_owned_pull_request(
116
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
117
- ) -> None:
118
- all_calls = _gh_reports(monkeypatch, "open\n")
119
- assert _run_gate(monkeypatch, capsys, "Done.") == ""
120
- assert all_calls == []
121
-
122
-
123
- def test_open_head_should_read_the_github_state_and_head(monkeypatch: pytest.MonkeyPatch) -> None:
124
- _gh_reports(monkeypatch, f"open {FIRST_HEAD}\n")
125
- assert owned_pr_merge_stop_gate.open_head(PULL_REQUEST) == FIRST_HEAD
126
-
127
-
128
- def test_open_head_should_read_a_closed_pull_request_as_none(monkeypatch: pytest.MonkeyPatch) -> None:
129
- _gh_reports(monkeypatch, f"closed {FIRST_HEAD}\n")
130
- assert owned_pr_merge_stop_gate.open_head(PULL_REQUEST) is None
131
-
132
-
133
- def test_is_reported_blocked_should_accept_the_url() -> None:
134
- reply_text = f"- Blocked: {PULL_REQUEST['url']} - needs the owner"
135
- assert owned_pr_merge_stop_gate.is_reported_blocked(PULL_REQUEST, reply_text)
136
-
137
-
138
- def test_unfinished_pull_requests_should_list_the_open_one(monkeypatch: pytest.MonkeyPatch) -> None:
139
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
140
- _gh_reports(monkeypatch, f"open {FIRST_HEAD}\n")
141
- hook_input: dict[str, object] = {"session_id": SESSION_ID, "last_assistant_message": "Open."}
142
- assert owned_pr_merge_stop_gate.unfinished_pull_requests(hook_input) == [
143
- {**PULL_REQUEST, "head": FIRST_HEAD}
144
- ]
145
-
146
-
147
- def test_block_reason_for_should_name_each_pull_request_and_the_blocked_form() -> None:
148
- reason = owned_pr_merge_stop_gate.block_reason_for([PULL_REQUEST])
149
- assert PULL_REQUEST["url"] in reason
150
- assert "Blocked: <owner>/<repository>#<number>" in reason
151
-
152
-
153
- def test_main_should_report_a_failed_lookup_on_stderr(
154
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
155
- ) -> None:
156
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
157
- _gh_reports(monkeypatch, "", exit_code=1)
158
- hook_input = {"hook_event_name": "Stop", "session_id": SESSION_ID, "last_assistant_message": "Open."}
159
- monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8"))))
160
- owned_pr_merge_stop_gate.main()
161
- assert "could not read repos/jl-cmd/snake-game/pulls/9" in capsys.readouterr().err
162
-
163
-
164
- def test_is_reported_blocked_should_accept_a_bold_blocked_label() -> None:
165
- reply_text = "**Blocked:** jl-cmd/snake-game#9 - the owner must approve the merge"
166
- assert owned_pr_merge_stop_gate.is_reported_blocked(PULL_REQUEST, reply_text)
167
-
168
-
169
- def test_is_reported_blocked_should_skip_a_longer_owner_that_ends_with_the_name() -> None:
170
- suffix_pull_request = {**PULL_REQUEST, "owner": "cmd", "url": "https://github.com/cmd/snake-game/pull/9"}
171
- reply_text = "Blocked: jl-cmd/snake-game#9 - the owner must approve the merge"
172
- assert not owned_pr_merge_stop_gate.is_reported_blocked(suffix_pull_request, reply_text)
173
-
174
-
175
- def test_block_reason_for_should_name_a_user_hold_as_a_blocker() -> None:
176
- assert "asked to hold the PR or to read it first" in owned_pr_merge_stop_gate.block_reason_for([PULL_REQUEST])
177
-
178
-
179
- def test_is_reported_blocked_should_require_the_colon_after_blocked() -> None:
180
- reply_text = "Blocked while CI runs on jl-cmd/snake-game#9."
181
- assert not owned_pr_merge_stop_gate.is_reported_blocked(PULL_REQUEST, reply_text)
182
-
183
-
184
- def _gh_serves(
185
- monkeypatch: pytest.MonkeyPatch, all_pull_requests: dict[str, tuple[str, str]]
186
- ) -> None:
187
- def fake_run(all_arguments: list[str], **_: object) -> subprocess.CompletedProcess[str]:
188
- state, head = all_pull_requests[all_arguments[2]]
189
- answer = state if all_arguments[4] == ".state" else f"{state} {head}"
190
- return subprocess.CompletedProcess(all_arguments, 0, stdout=answer + "\n", stderr="")
191
-
192
- monkeypatch.setattr(owned_pr_merge_stop_gate.subprocess, "run", fake_run)
193
-
194
-
195
- RECORDED_ENDPOINT = "repos/jl-cmd-projects/claude-dev-env/pulls/2300"
196
- PULL_REQUEST_ENDPOINT = "repos/jl-cmd/snake-game/pulls/9"
197
-
198
-
199
- def test_main_should_block_once_per_head_on_the_recorded_pull_request(
200
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
201
- ) -> None:
202
- owned_pr_session_record.record_pull_request(SESSION_ID, RECORDED_PULL_REQUEST)
203
- _gh_serves(monkeypatch, {RECORDED_ENDPOINT: ("open", RECORDED_HEAD)})
204
- first_decision = json.loads(_run_gate(monkeypatch, capsys, "Review is clean."))
205
- assert first_decision["decision"] == "block"
206
- assert RECORDED_PULL_REQUEST["url"] in first_decision["reason"]
207
- assert _run_gate(monkeypatch, capsys, "Waiting on CI.") == ""
208
- assert _run_gate(monkeypatch, capsys, "Still waiting on CI.") == ""
209
-
210
-
211
- def test_main_should_block_again_after_the_head_moves(
212
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
213
- ) -> None:
214
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
215
- _gh_serves(monkeypatch, {PULL_REQUEST_ENDPOINT: ("open", FIRST_HEAD)})
216
- assert json.loads(_run_gate(monkeypatch, capsys, "Open."))["decision"] == "block"
217
- _gh_serves(monkeypatch, {PULL_REQUEST_ENDPOINT: ("open", PUSHED_HEAD)})
218
- assert json.loads(_run_gate(monkeypatch, capsys, "Pushed a fix."))["decision"] == "block"
219
- assert _run_gate(monkeypatch, capsys, "Waiting on CI.") == ""
220
-
221
-
222
- def test_main_should_block_every_stop_when_github_gives_no_head(
223
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
224
- ) -> None:
225
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
226
- _gh_reports(monkeypatch, "open\n")
227
- assert json.loads(_run_gate(monkeypatch, capsys, "Open."))["decision"] == "block"
228
- assert json.loads(_run_gate(monkeypatch, capsys, "Open."))["decision"] == "block"
229
-
230
-
231
- def test_main_should_list_only_the_pull_request_with_a_new_head(
232
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
233
- ) -> None:
234
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
235
- _gh_serves(monkeypatch, {PULL_REQUEST_ENDPOINT: ("open", FIRST_HEAD)})
236
- assert json.loads(_run_gate(monkeypatch, capsys, "Open."))["decision"] == "block"
237
- owned_pr_session_record.record_pull_request(SESSION_ID, RECORDED_PULL_REQUEST)
238
- _gh_serves(
239
- monkeypatch,
240
- {PULL_REQUEST_ENDPOINT: ("open", FIRST_HEAD), RECORDED_ENDPOINT: ("open", RECORDED_HEAD)},
241
- )
242
- reason = json.loads(_run_gate(monkeypatch, capsys, "Opened another."))["reason"]
243
- assert RECORDED_PULL_REQUEST["url"] in reason
244
- assert PULL_REQUEST["url"] not in reason
245
-
246
-
247
- def test_main_should_keep_the_blocked_line_release_before_the_first_block(
248
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
249
- ) -> None:
250
- owned_pr_session_record.record_pull_request(SESSION_ID, PULL_REQUEST)
251
- _gh_serves(monkeypatch, {PULL_REQUEST_ENDPOINT: ("open", FIRST_HEAD)})
252
- reply_text = "Blocked: jl-cmd/snake-game#9 - the user holds the merge"
253
- assert _run_gate(monkeypatch, capsys, reply_text) == ""
254
- assert json.loads(_run_gate(monkeypatch, capsys, "Open."))["decision"] == "block"
@@ -1,396 +0,0 @@
1
- import io
2
- import json
3
- from pathlib import Path
4
-
5
- import pytest
6
-
7
- import session_title_turn_gate
8
- from open_tasks_transcript import TaskItem
9
-
10
- REMOTE_TITLE_TOOL = "mcp__claude-code-remote__set_session_title"
11
- DESKTOP_TITLE_TOOL = "mcp__ccd_session_mgmt__set_session_title"
12
- TOOL_LISTING = {
13
- "type": "attachment",
14
- "attachment": {
15
- "type": "prompt_snapshot",
16
- "tools": [{"name": "Bash"}, {"name": REMOTE_TITLE_TOOL}],
17
- },
18
- }
19
- DEFERRED_LISTING = {
20
- "type": "attachment",
21
- "attachment": {"type": "deferred_tools_delta", "addedNames": [DESKTOP_TITLE_TOOL]},
22
- }
23
- PROMPT = {"type": "user", "message": {"role": "user", "content": "Fix the gate"}}
24
- WORK_CALL = {
25
- "type": "assistant",
26
- "message": {"content": [{"type": "tool_use", "id": "w1", "name": "Bash", "input": {}}]},
27
- }
28
- WORK_RESULT = {
29
- "type": "user",
30
- "message": {"content": [{"type": "tool_result", "tool_use_id": "w1", "content": "ok"}]},
31
- }
32
- SESSION_ID = "s-title"
33
- RED_FLAG_TITLE = "\U0001f6a9 Merge approval"
34
- HOURGLASS_TITLE = "\u23f3 Export"
35
- QUESTION_PROMPT = {"type": "user", "message": {"role": "user", "content": "did the export fail?"}}
36
- REPLY = {"type": "assistant", "message": {"content": [{"type": "text", "text": "Done."}]}}
37
-
38
-
39
- def title_call(
40
- tool_use_id: str, tool_name: str = REMOTE_TITLE_TOOL, title: str = "✅ Gate"
41
- ) -> dict[str, object]:
42
- return {
43
- "type": "assistant",
44
- "message": {
45
- "content": [
46
- {
47
- "type": "tool_use",
48
- "id": tool_use_id,
49
- "name": tool_name,
50
- "input": {"title": title},
51
- }
52
- ]
53
- },
54
- }
55
-
56
-
57
- def title_result(tool_use_id: str, is_error: bool = False) -> dict[str, object]:
58
- return {
59
- "type": "user",
60
- "message": {
61
- "content": [
62
- {
63
- "type": "tool_result",
64
- "tool_use_id": tool_use_id,
65
- "content": "ok",
66
- "is_error": is_error,
67
- }
68
- ]
69
- },
70
- }
71
-
72
-
73
- def run_gate(
74
- monkeypatch: pytest.MonkeyPatch,
75
- capsys: pytest.CaptureFixture[str],
76
- tmp_path: Path,
77
- all_entries: list[dict[str, object]],
78
- is_stop_hook_active: bool = False,
79
- ) -> tuple[int, str]:
80
- transcript_path = tmp_path / "transcript.jsonl"
81
- transcript_path.write_text(
82
- "\n".join(json.dumps(each) for each in all_entries) + "\n", encoding="utf-8"
83
- )
84
- hook_input = {
85
- "hook_event_name": "Stop",
86
- "session_id": SESSION_ID,
87
- "stop_hook_active": is_stop_hook_active,
88
- "transcript_path": str(transcript_path),
89
- }
90
- monkeypatch.setattr(
91
- "sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
92
- )
93
- exit_code = session_title_turn_gate.main()
94
- return exit_code, capsys.readouterr().out
95
-
96
-
97
- @pytest.fixture(autouse=True)
98
- def isolated_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
99
- monkeypatch.setenv("HOME", str(tmp_path))
100
- monkeypatch.setenv("USERPROFILE", str(tmp_path))
101
- monkeypatch.delenv("CLAUDE_CONFIG_DIR", raising=False)
102
- monkeypatch.delenv("CLAUDE_CODE_TASK_LIST_ID", raising=False)
103
-
104
-
105
- def write_task_store(home_directory: Path, all_statuses: list[str]) -> None:
106
- task_directory = home_directory / ".claude" / "tasks" / SESSION_ID
107
- task_directory.mkdir(parents=True)
108
- for each_number, each_status in enumerate(all_statuses, start=1):
109
- each_task = {"id": str(each_number), "subject": f"Step {each_number}", "status": each_status}
110
- (task_directory / f"{each_number}.json").write_text(json.dumps(each_task), encoding="utf-8")
111
-
112
-
113
- def earlier_turn(title: str) -> list[dict[str, object]]:
114
- return [TOOL_LISTING, PROMPT, title_call("t0", title=title), title_result("t0"), REPLY]
115
-
116
-
117
- def block_reason(
118
- monkeypatch: pytest.MonkeyPatch,
119
- capsys: pytest.CaptureFixture[str],
120
- tmp_path: Path,
121
- all_entries: list[dict[str, object]],
122
- ) -> str:
123
- stdout_text = run_gate(monkeypatch, capsys, tmp_path, all_entries)[1]
124
- return json.loads(stdout_text)["reason"] if stdout_text else ""
125
-
126
-
127
- def test_should_hold_a_work_turn_in_a_session_with_no_title(
128
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
129
- ) -> None:
130
- exit_code, stdout_text = run_gate(
131
- monkeypatch, capsys, tmp_path, [TOOL_LISTING, PROMPT, WORK_CALL, WORK_RESULT, REPLY]
132
- )
133
- block_output = json.loads(stdout_text)
134
- assert exit_code == 0
135
- assert block_output["decision"] == "block"
136
- assert "no title yet" in block_output["reason"]
137
- assert REMOTE_TITLE_TOOL in block_output["reason"]
138
- assert all(
139
- each_emoji in block_output["reason"]
140
- for each_emoji in ("\U0001f6a9", "\U0001f6a7", "✅", "⏳")
141
- )
142
-
143
-
144
- def test_should_pass_a_turn_that_set_a_title(
145
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
146
- ) -> None:
147
- write_task_store(tmp_path, ["completed"])
148
- all_entries = [
149
- TOOL_LISTING,
150
- PROMPT,
151
- WORK_CALL,
152
- WORK_RESULT,
153
- title_call("t1", title=HOURGLASS_TITLE),
154
- title_result("t1"),
155
- REPLY,
156
- ]
157
- assert run_gate(monkeypatch, capsys, tmp_path, all_entries) == (0, "")
158
-
159
-
160
- @pytest.mark.parametrize(
161
- ("earlier_title", "all_statuses"),
162
- [
163
- (HOURGLASS_TITLE, ["completed", "in_progress"]),
164
- ("✅ Gate", []),
165
- ("✅ Gate", ["completed"]),
166
- ("\U0001f6a7 Org secret", ["pending"]),
167
- ],
168
- )
169
- def test_should_pass_a_work_turn_whose_carried_title_still_fits(
170
- monkeypatch: pytest.MonkeyPatch,
171
- capsys: pytest.CaptureFixture[str],
172
- tmp_path: Path,
173
- earlier_title: str,
174
- all_statuses: list[str],
175
- ) -> None:
176
- if all_statuses:
177
- write_task_store(tmp_path, all_statuses)
178
- all_entries = [*earlier_turn(earlier_title), PROMPT, WORK_CALL, WORK_RESULT, REPLY]
179
- assert run_gate(monkeypatch, capsys, tmp_path, all_entries) == (0, "")
180
-
181
-
182
- def test_should_hold_a_carried_red_flag_after_a_prompt_that_gives_work(
183
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
184
- ) -> None:
185
- all_entries = [*earlier_turn(RED_FLAG_TITLE), PROMPT, WORK_CALL, WORK_RESULT, REPLY]
186
- reason_text = block_reason(monkeypatch, capsys, tmp_path, all_entries)
187
- assert RED_FLAG_TITLE in reason_text
188
- assert "red flag" in reason_text
189
-
190
-
191
- def test_should_pass_a_carried_red_flag_after_a_question(
192
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
193
- ) -> None:
194
- all_entries = [*earlier_turn(RED_FLAG_TITLE), QUESTION_PROMPT, WORK_CALL, WORK_RESULT, REPLY]
195
- assert run_gate(monkeypatch, capsys, tmp_path, all_entries) == (0, "")
196
-
197
-
198
- def test_should_hold_a_carried_check_mark_while_items_are_open(
199
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
200
- ) -> None:
201
- write_task_store(tmp_path, ["completed", "pending"])
202
- all_entries = [*earlier_turn("✅ Gate"), QUESTION_PROMPT, WORK_CALL, WORK_RESULT, REPLY]
203
- assert "open items" in block_reason(monkeypatch, capsys, tmp_path, all_entries)
204
-
205
-
206
- def test_should_hold_a_carried_hourglass_once_every_item_is_completed(
207
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
208
- ) -> None:
209
- write_task_store(tmp_path, ["completed", "completed"])
210
- all_entries = [*earlier_turn(HOURGLASS_TITLE), PROMPT, WORK_CALL, WORK_RESULT, REPLY]
211
- assert "every task list item is completed" in block_reason(
212
- monkeypatch, capsys, tmp_path, all_entries
213
- )
214
-
215
-
216
- def test_should_pass_a_held_turn_even_with_no_title(
217
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
218
- ) -> None:
219
- all_entries = [TOOL_LISTING, PROMPT, WORK_CALL, WORK_RESULT, REPLY]
220
- assert run_gate(monkeypatch, capsys, tmp_path, all_entries, is_stop_hook_active=True) == (0, "")
221
-
222
-
223
- def test_should_pass_a_tool_free_turn_with_no_title(
224
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
225
- ) -> None:
226
- assert run_gate(monkeypatch, capsys, tmp_path, [TOOL_LISTING, PROMPT, REPLY]) == (0, "")
227
-
228
-
229
- def test_should_keep_showing_the_earlier_title_when_this_turns_title_call_was_denied(
230
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
231
- ) -> None:
232
- all_entries = [
233
- *earlier_turn(RED_FLAG_TITLE),
234
- PROMPT,
235
- WORK_CALL,
236
- WORK_RESULT,
237
- title_call("t1", title="⏳ bad title."),
238
- title_result("t1", is_error=True),
239
- REPLY,
240
- ]
241
- assert RED_FLAG_TITLE in block_reason(monkeypatch, capsys, tmp_path, all_entries)
242
-
243
-
244
- def test_should_ignore_a_title_set_on_a_sidechain(
245
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
246
- ) -> None:
247
- sidechain_call = {**title_call("s1"), "isSidechain": True}
248
- sidechain_result = {**title_result("s1"), "isSidechain": True}
249
- all_entries = [
250
- TOOL_LISTING,
251
- PROMPT,
252
- sidechain_call,
253
- sidechain_result,
254
- WORK_CALL,
255
- WORK_RESULT,
256
- REPLY,
257
- ]
258
- assert "no title yet" in block_reason(monkeypatch, capsys, tmp_path, all_entries)
259
-
260
-
261
- def test_should_start_a_turn_at_an_image_only_prompt(
262
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
263
- ) -> None:
264
- write_task_store(tmp_path, ["pending"])
265
- image_prompt = {
266
- "type": "user",
267
- "message": {"content": [{"type": "image", "source": {"type": "base64", "data": ""}}]},
268
- }
269
- all_entries = [*earlier_turn("✅ Gate"), image_prompt, WORK_CALL, WORK_RESULT, REPLY]
270
- assert "open items" in block_reason(monkeypatch, capsys, tmp_path, all_entries)
271
-
272
-
273
- def test_should_keep_the_turn_across_a_stop_block_and_a_meta_entry(
274
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
275
- ) -> None:
276
- write_task_store(tmp_path, ["pending"])
277
- stop_block = {
278
- "type": "attachment",
279
- "attachment": {"type": "hook_blocking_error", "hookEvent": "Stop"},
280
- }
281
- meta_entry = {"type": "user", "isMeta": True, "message": {"content": "Stop hook feedback"}}
282
- all_entries = [
283
- TOOL_LISTING,
284
- PROMPT,
285
- WORK_CALL,
286
- WORK_RESULT,
287
- title_call("t1"),
288
- title_result("t1"),
289
- REPLY,
290
- stop_block,
291
- meta_entry,
292
- REPLY,
293
- ]
294
- assert run_gate(monkeypatch, capsys, tmp_path, all_entries) == (0, "")
295
-
296
-
297
- def test_should_name_a_deferred_title_tool_in_the_reason(
298
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
299
- ) -> None:
300
- all_entries = [DEFERRED_LISTING, PROMPT, WORK_CALL, WORK_RESULT, REPLY]
301
- assert DESKTOP_TITLE_TOOL in block_reason(monkeypatch, capsys, tmp_path, all_entries)
302
-
303
-
304
- @pytest.mark.parametrize(
305
- "all_entries",
306
- [
307
- [PROMPT, WORK_CALL, WORK_RESULT, REPLY],
308
- [
309
- DEFERRED_LISTING,
310
- {
311
- "type": "attachment",
312
- "attachment": {
313
- "type": "deferred_tools_delta",
314
- "removedNames": [DESKTOP_TITLE_TOOL],
315
- },
316
- },
317
- PROMPT,
318
- WORK_CALL,
319
- WORK_RESULT,
320
- REPLY,
321
- ],
322
- ],
323
- )
324
- def test_should_pass_a_session_without_the_title_tool(
325
- monkeypatch: pytest.MonkeyPatch,
326
- capsys: pytest.CaptureFixture[str],
327
- tmp_path: Path,
328
- all_entries: list[dict[str, object]],
329
- ) -> None:
330
- assert run_gate(monkeypatch, capsys, tmp_path, all_entries) == (0, "")
331
-
332
-
333
- def test_should_pass_when_the_transcript_cannot_be_read(
334
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
335
- ) -> None:
336
- hook_input = {"hook_event_name": "Stop", "transcript_path": str(tmp_path / "missing.jsonl")}
337
- monkeypatch.setattr(
338
- "sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
339
- )
340
- assert (session_title_turn_gate.main(), capsys.readouterr().out) == (0, "")
341
-
342
-
343
- def test_should_return_the_last_title_that_came_back_without_an_error() -> None:
344
- all_entries = [
345
- title_call("t1", title=HOURGLASS_TITLE),
346
- title_result("t1"),
347
- title_call("t2", title=RED_FLAG_TITLE),
348
- title_result("t2", is_error=True),
349
- ]
350
- assert session_title_turn_gate.shown_title(all_entries) == HOURGLASS_TITLE
351
- assert session_title_turn_gate.shown_title([PROMPT]) is None
352
-
353
-
354
- @pytest.mark.parametrize(
355
- ("title", "expected_status"),
356
- [(RED_FLAG_TITLE, "\U0001f6a9"), ("⏳️ Export", "⏳"), ("Export", None)],
357
- )
358
- def test_should_read_the_status_emoji_without_its_variation_selector(
359
- title: str, expected_status: str | None
360
- ) -> None:
361
- assert session_title_turn_gate.title_status(title) == expected_status
362
-
363
-
364
- @pytest.mark.parametrize(
365
- ("title", "is_ask_turn", "all_statuses", "is_expected_stale"),
366
- [
367
- (RED_FLAG_TITLE, True, [], True),
368
- (RED_FLAG_TITLE, False, [], False),
369
- ("✅ Gate", False, ["pending"], True),
370
- ("✅ Gate", True, ["completed"], False),
371
- ("✅ Gate", True, ["completed", "deleted"], False),
372
- (HOURGLASS_TITLE, True, ["completed", "deleted"], True),
373
- (HOURGLASS_TITLE, True, ["completed"], True),
374
- (HOURGLASS_TITLE, True, ["in_progress"], False),
375
- (HOURGLASS_TITLE, True, [], False),
376
- ],
377
- )
378
- def test_should_call_a_status_stale_only_when_the_session_state_contradicts_it(
379
- title: str, is_ask_turn: bool, all_statuses: list[str], is_expected_stale: bool
380
- ) -> None:
381
- all_items = [
382
- TaskItem(str(each_number), "Step", each_status)
383
- for each_number, each_status in enumerate(all_statuses)
384
- ]
385
- stale_reason = session_title_turn_gate.stale_status_reason(title, is_ask_turn, all_items)
386
- assert (stale_reason is not None) is is_expected_stale
387
- if stale_reason is not None:
388
- assert title in stale_reason
389
-
390
-
391
- def test_should_build_the_block_reason_from_the_stale_status() -> None:
392
- all_entries = [*earlier_turn(RED_FLAG_TITLE), PROMPT, WORK_CALL, WORK_RESULT, REPLY]
393
- reason_text = session_title_turn_gate.turn_block_reason(all_entries, False, True, [])
394
- assert reason_text is not None
395
- assert reason_text.startswith("The title '" + RED_FLAG_TITLE)
396
- assert session_title_turn_gate.turn_block_reason(all_entries, False, False, []) is None
@@ -1,40 +0,0 @@
1
- """Constants for the owned pull request merge Stop gate."""
2
-
3
- import re
4
-
5
- HOOK_EVENT_NAME = "Stop"
6
- SESSION_ID_KEY = "session_id"
7
- LAST_ASSISTANT_MESSAGE_KEY = "last_assistant_message"
8
- DECISION_KEY = "decision"
9
- BLOCK_DECISION = "block"
10
- REASON_KEY = "reason"
11
- ALLOW_EXIT_CODE = 0
12
- GH_PROGRAM_NAME = "gh"
13
- GH_API_SUBCOMMAND = "api"
14
- GH_JQ_OPTION = "--jq"
15
- PULL_REQUEST_ENDPOINT_TEMPLATE = "repos/{owner}/{repository}/pulls/{number}"
16
- PULL_REQUEST_STATE_QUERY = '.state + " " + .head.sha'
17
- STATE_AND_HEAD_SEPARATOR = " "
18
- OPEN_PULL_REQUEST_STATE = "open"
19
- HEAD_FIELD = "head"
20
- URL_FIELD = "url"
21
- STATE_QUERY_TIMEOUT_SECONDS = 10
22
- LOOKUP_FAILURE_TEMPLATE = "owned_pr_merge_stop_gate: could not read {endpoint}: {detail}\n"
23
- BLOCKED_LINE_PATTERN: re.Pattern[str] = re.compile(
24
- r"^\s*(?:[-*]\s*)?blocked\s*:(.*)$", re.IGNORECASE
25
- )
26
- BLOCKED_TEXT_GROUP_INDEX = 1
27
- BOLD_MARKER = "**"
28
- NAME_START_PATTERN = r"(?<![0-9A-Za-z_./-])"
29
- NAME_END_PATTERN = r"(?![0-9A-Za-z/])"
30
- PULL_REQUEST_REFERENCE_TEMPLATE = "{owner}/{repository}#{number}"
31
- PULL_REQUEST_LINE_TEMPLATE = "\n- {url}"
32
- BLOCK_REASON_PREFIX = "You opened these owned pull requests and they are still open:"
33
- BLOCK_REASON_SUFFIX = (
34
- "\nThe repository owner approves the whole owned PR loop, the merge included."
35
- " Keep working now: run /efficient-review until a pass is clean, mark the PR ready,"
36
- " babysit CI and review threads, then merge it and read it back as merged."
37
- " End the turn early only with one line per pull request:"
38
- " 'Blocked: <owner>/<repository>#<number> - <what only the owner can give>'."
39
- " A user who asked to hold the PR or to read it first is such a blocker."
40
- )