claude-dev-env 8.116.0 → 8.116.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/bin/install.prompt-context.test.mjs +5 -6
  2. package/docs/rule-guides/proof-before-pull-request.md +4 -2
  3. package/hooks/blocking/final_reply_gate.py +7 -29
  4. package/hooks/blocking/followup_pr_dedupe.py +118 -33
  5. package/hooks/blocking/gate_run_reads.py +284 -0
  6. package/hooks/blocking/pr_lifecycle_skill_gate.py +33 -33
  7. package/hooks/blocking/pull_request_proof.py +60 -63
  8. package/hooks/blocking/test_final_reply_gate.py +4 -196
  9. package/hooks/blocking/test_followup_pr_dedupe.py +59 -0
  10. package/hooks/blocking/test_gate_run_reads.py +241 -0
  11. package/hooks/blocking/test_pr_lifecycle_skill_gate.py +89 -1
  12. package/hooks/blocking/test_pull_request_proof.py +63 -2
  13. package/hooks/features/conduct-gates.md +6 -13
  14. package/hooks/hooks.json +1 -42
  15. package/hooks/hooks_constants/background_silence_prompt_constants.py +2 -0
  16. package/hooks/hooks_constants/gate_run_reads_constants.py +10 -0
  17. package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +0 -8
  18. package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +0 -4
  19. package/hooks/session/background_silence_prompt.py +1 -1
  20. package/hooks/test_context_budget_hooks.py +0 -1
  21. package/package.json +1 -1
  22. package/scripts/durable_post_lint.py +0 -12
  23. package/scripts/durable_post_lint_config/config/constants.py +1 -2
  24. package/scripts/policy_lint/adapter_configuration.py +0 -2
  25. package/scripts/policy_lint/config/constants.py +0 -2
  26. package/scripts/test_durable_post_lint.py +0 -53
  27. package/scripts/tests/test_adapter_configuration.py +0 -16
  28. package/hooks/blocking/chat_reply_gate.py +0 -260
  29. package/hooks/blocking/reply_length_gate.py +0 -804
  30. package/hooks/blocking/test_chat_reply_gate.py +0 -302
  31. package/hooks/blocking/test_reply_length_gate.py +0 -1324
  32. package/hooks/hooks_constants/chat_reply_gate_constants.py +0 -60
  33. package/hooks/hooks_constants/reply_length_gate_constants.py +0 -203
  34. package/hooks/hooks_constants/test_reply_length_gate_constants.py +0 -81
  35. package/hooks/hooks_constants/test_visual_reply_rules_constants.py +0 -50
  36. package/hooks/hooks_constants/visual_reply_rules_constants.py +0 -106
  37. package/hooks/test_visual_reply_rules.py +0 -362
  38. package/hooks/visual_reply_rules.py +0 -478
  39. package/rules/visual-reply-rules.json +0 -56
@@ -1,302 +0,0 @@
1
- import io
2
- import json
3
- import os
4
- import subprocess
5
- import sys
6
- from pathlib import Path
7
-
8
- import pytest
9
-
10
- import chat_reply_gate
11
- from hooks_constants.chat_reply_gate_constants import REPLY_LIMIT_REMINDER, SHOWN_DRAFT_HEADING
12
- from hooks_constants.reply_length_gate_constants import MAXIMUM_SENTENCE_COUNT
13
-
14
- CHAT_REPLY_GATE_SCRIPT = Path(__file__).resolve().parent / "chat_reply_gate.py"
15
-
16
- OVER_LIMIT_SENTENCES = [
17
- f"Status line {each_number} is ready." for each_number in range(MAXIMUM_SENTENCE_COUNT + 1)
18
- ]
19
- OVER_LIMIT_REPLY = " ".join(OVER_LIMIT_SENTENCES)
20
- FIRST_HELD_BATCH = " ".join(OVER_LIMIT_SENTENCES[: len(OVER_LIMIT_SENTENCES) // 2])
21
- LAST_HELD_BATCH = " ".join(OVER_LIMIT_SENTENCES[len(OVER_LIMIT_SENTENCES) // 2 :])
22
- THREE_SENTENCE_REPLY = "PR one merged. PR two is queued. Nothing else is open."
23
- PASSING_DRAFT = "PR one merged. Two others wait."
24
-
25
-
26
- @pytest.fixture(autouse=True)
27
- def default_banned_words(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
28
- monkeypatch.setenv("CLAUDE_REPLY_BANNED_WORDS_PATH", str(tmp_path / "absent.json"))
29
- monkeypatch.delenv("CHAT_REPLY_DRAFTER_ACTIVE", raising=False)
30
- monkeypatch.setattr(chat_reply_gate, "log_hook_block", lambda *_args, **_kwargs: None)
31
- monkeypatch.setattr(chat_reply_gate.tempfile, "gettempdir", lambda: str(tmp_path))
32
-
33
-
34
- def run_hook(
35
- monkeypatch: pytest.MonkeyPatch,
36
- capsys: pytest.CaptureFixture[str],
37
- hook_input: dict[str, object],
38
- ) -> tuple[int, dict[str, object] | None]:
39
- monkeypatch.setattr(
40
- "sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
41
- )
42
- exit_code = chat_reply_gate.main()
43
- output_text = capsys.readouterr().out
44
- return exit_code, json.loads(output_text)["hookSpecificOutput"] if output_text else None
45
-
46
-
47
- def display_input(delta: str, is_final: bool = True) -> dict[str, object]:
48
- return {
49
- "hook_event_name": "MessageDisplay",
50
- "session_id": "abc",
51
- "message_id": "m1",
52
- "delta": delta,
53
- "final": is_final,
54
- }
55
-
56
-
57
- def test_should_show_a_passing_draft_in_place_of_a_long_reply(
58
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
59
- ) -> None:
60
- monkeypatch.setattr(chat_reply_gate, "ask_drafter", lambda *_args: PASSING_DRAFT)
61
- exit_code, hook_output = run_hook(monkeypatch, capsys, display_input(OVER_LIMIT_REPLY))
62
- assert exit_code == 0
63
- assert hook_output == {"hookEventName": "MessageDisplay", "displayContent": PASSING_DRAFT}
64
-
65
-
66
- def test_should_show_a_reply_within_the_limit_unchanged(
67
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
68
- ) -> None:
69
- monkeypatch.setattr(chat_reply_gate, "ask_drafter", lambda *_args: pytest.fail("drafter called"))
70
- exit_code, hook_output = run_hook(monkeypatch, capsys, display_input(THREE_SENTENCE_REPLY))
71
- assert exit_code == 0
72
- assert hook_output is None
73
-
74
-
75
- def test_should_hold_batches_until_the_last_one_arrives(
76
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
77
- ) -> None:
78
- all_requests: list[str] = []
79
- monkeypatch.setattr(
80
- chat_reply_gate,
81
- "ask_drafter",
82
- lambda reply_text, _violation: all_requests.append(reply_text) or PASSING_DRAFT,
83
- )
84
- _, first_output = run_hook(
85
- monkeypatch, capsys, display_input(FIRST_HELD_BATCH + "\n", False)
86
- )
87
- _, last_output = run_hook(
88
- monkeypatch, capsys, display_input(LAST_HELD_BATCH)
89
- )
90
- assert first_output == {"hookEventName": "MessageDisplay", "displayContent": ""}
91
- assert last_output == {"hookEventName": "MessageDisplay", "displayContent": PASSING_DRAFT}
92
- assert all_requests == [FIRST_HELD_BATCH + "\n" + LAST_HELD_BATCH]
93
-
94
-
95
- def test_should_show_the_whole_held_message_when_it_passes(
96
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
97
- ) -> None:
98
- run_hook(monkeypatch, capsys, display_input("PR one merged.\n", False))
99
- _, hook_output = run_hook(monkeypatch, capsys, display_input("Nothing else is open."))
100
- assert hook_output == {
101
- "hookEventName": "MessageDisplay",
102
- "displayContent": "PR one merged.\nNothing else is open.",
103
- }
104
-
105
-
106
- def test_should_show_the_original_when_no_draft_passes(
107
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
108
- ) -> None:
109
- all_calls: list[str] = []
110
- monkeypatch.setattr(
111
- chat_reply_gate,
112
- "ask_drafter",
113
- lambda reply_text, _violation: all_calls.append(reply_text) or OVER_LIMIT_REPLY,
114
- )
115
- _, hook_output = run_hook(monkeypatch, capsys, display_input(OVER_LIMIT_REPLY))
116
- assert hook_output is None
117
- assert len(all_calls) == 2
118
-
119
-
120
- def test_should_tell_the_agent_which_draft_the_user_saw(
121
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
122
- ) -> None:
123
- monkeypatch.setattr(chat_reply_gate, "ask_drafter", lambda *_args: PASSING_DRAFT)
124
- prompt_input = {"hook_event_name": "UserPromptSubmit", "session_id": "abc"}
125
- _, first_output = run_hook(monkeypatch, capsys, prompt_input)
126
- run_hook(monkeypatch, capsys, display_input(OVER_LIMIT_REPLY))
127
- _, second_output = run_hook(monkeypatch, capsys, prompt_input)
128
- _, third_output = run_hook(monkeypatch, capsys, prompt_input)
129
- assert first_output is not None and second_output is not None
130
- assert str(first_output["additionalContext"]) == REPLY_LIMIT_REMINDER
131
- assert second_output["additionalContext"] == SHOWN_DRAFT_HEADING + PASSING_DRAFT
132
- assert third_output is None
133
-
134
-
135
- def _prompt_input(transcript_path: Path) -> dict[str, object]:
136
- return {
137
- "hook_event_name": "UserPromptSubmit",
138
- "session_id": "abc",
139
- "prompt": "status?",
140
- "transcript_path": str(transcript_path),
141
- }
142
-
143
-
144
- def test_should_add_the_reply_limit_once_per_session_and_again_after_a_compaction(
145
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
146
- ) -> None:
147
- transcript_path = tmp_path / "session.jsonl"
148
- transcript_path.write_text(json.dumps({"type": "user"}) + "\n", encoding="utf-8")
149
- _, first_output = run_hook(monkeypatch, capsys, _prompt_input(transcript_path))
150
- _, second_output = run_hook(monkeypatch, capsys, _prompt_input(transcript_path))
151
- with transcript_path.open("a", encoding="utf-8") as transcript:
152
- transcript.write(json.dumps({"type": "system", "subtype": "compact_boundary"}) + "\n")
153
- _, after_compaction_output = run_hook(monkeypatch, capsys, _prompt_input(transcript_path))
154
- _, later_output = run_hook(monkeypatch, capsys, _prompt_input(transcript_path))
155
- assert first_output is not None
156
- assert first_output["additionalContext"] == REPLY_LIMIT_REMINDER
157
- assert second_output is None
158
- assert after_compaction_output is not None
159
- assert after_compaction_output["additionalContext"] == REPLY_LIMIT_REMINDER
160
- assert later_output is None
161
-
162
-
163
- def test_replay_of_two_prompts_and_a_compaction_through_the_script(tmp_path: Path) -> None:
164
- transcript_path = tmp_path / "session.jsonl"
165
- transcript_path.write_text(json.dumps({"type": "user"}) + "\n", encoding="utf-8")
166
- environment = {
167
- **os.environ,
168
- "TMP": str(tmp_path),
169
- "TEMP": str(tmp_path),
170
- "TMPDIR": str(tmp_path),
171
- "CLAUDE_REPLY_BANNED_WORDS_PATH": str(tmp_path / "absent.json"),
172
- }
173
- environment.pop("CHAT_REPLY_DRAFTER_ACTIVE", None)
174
-
175
- def replay_prompt() -> str:
176
- return subprocess.run(
177
- [sys.executable, str(CHAT_REPLY_GATE_SCRIPT)],
178
- input=json.dumps(_prompt_input(transcript_path)),
179
- capture_output=True,
180
- text=True,
181
- env=environment,
182
- check=True,
183
- ).stdout
184
-
185
- first_stdout = replay_prompt()
186
- second_stdout = replay_prompt()
187
- with transcript_path.open("a", encoding="utf-8") as transcript:
188
- transcript.write(json.dumps({"type": "system", "subtype": "compact_boundary"}) + "\n")
189
- after_compaction_stdout = replay_prompt()
190
- assert REPLY_LIMIT_REMINDER in json.loads(first_stdout)["hookSpecificOutput"]["additionalContext"]
191
- assert second_stdout == ""
192
- assert (
193
- REPLY_LIMIT_REMINDER
194
- in json.loads(after_compaction_stdout)["hookSpecificOutput"]["additionalContext"]
195
- )
196
-
197
-
198
- def test_should_delete_the_shown_draft_at_session_end(
199
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
200
- ) -> None:
201
- monkeypatch.setattr(chat_reply_gate, "ask_drafter", lambda *_args: PASSING_DRAFT)
202
- run_hook(monkeypatch, capsys, {"hook_event_name": "UserPromptSubmit", "session_id": "abc"})
203
- run_hook(monkeypatch, capsys, display_input(OVER_LIMIT_REPLY))
204
- shown_draft_path = chat_reply_gate.state_file_path("claude-chat-reply-shown-", "abc")
205
- assert shown_draft_path.exists()
206
- assert list(Path(chat_reply_gate.tempfile.gettempdir()).glob("claude-once-*"))
207
- exit_code, hook_output = run_hook(
208
- monkeypatch, capsys, {"hook_event_name": "SessionEnd", "session_id": "abc"}
209
- )
210
- assert exit_code == 0
211
- assert hook_output is None
212
- assert not shown_draft_path.exists()
213
- assert not list(Path(chat_reply_gate.tempfile.gettempdir()).glob("claude-once-*"))
214
-
215
-
216
- def test_should_add_the_reply_limit_to_a_prompt_with_no_session_id(
217
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
218
- ) -> None:
219
- exit_code, hook_output = run_hook(
220
- monkeypatch, capsys, {"hook_event_name": "UserPromptSubmit", "prompt": "status?"}
221
- )
222
- assert exit_code == 0
223
- assert hook_output is not None
224
- assert hook_output["hookEventName"] == "UserPromptSubmit"
225
- assert f"{MAXIMUM_SENTENCE_COUNT} sentences or fewer" in str(hook_output["additionalContext"])
226
-
227
-
228
- def test_should_do_nothing_inside_the_drafter_process(
229
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
230
- ) -> None:
231
- monkeypatch.setenv("CHAT_REPLY_DRAFTER_ACTIVE", "1")
232
- exit_code, hook_output = run_hook(monkeypatch, capsys, display_input(OVER_LIMIT_REPLY))
233
- assert exit_code == 0
234
- assert hook_output is None
235
-
236
-
237
- def test_should_ignore_the_stop_event(
238
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
239
- ) -> None:
240
- exit_code, hook_output = run_hook(
241
- monkeypatch,
242
- capsys,
243
- {"hook_event_name": "Stop", "last_assistant_message": OVER_LIMIT_REPLY},
244
- )
245
- assert exit_code == 0
246
- assert hook_output is None
247
-
248
-
249
- def test_ask_drafter_should_read_the_result_field(monkeypatch: pytest.MonkeyPatch) -> None:
250
- class CompletedDrafter:
251
- stdout = json.dumps({"result": f" {PASSING_DRAFT}\n"})
252
-
253
- all_commands: list[list[str]] = []
254
- monkeypatch.setattr(chat_reply_gate.shutil, "which", lambda _name: "C:/npm/claude.cmd")
255
- monkeypatch.setattr(
256
- chat_reply_gate.subprocess,
257
- "run",
258
- lambda command, **_kwargs: all_commands.append(command) or CompletedDrafter(),
259
- )
260
- assert chat_reply_gate.ask_drafter(OVER_LIMIT_REPLY, "too long") == PASSING_DRAFT
261
- assert all_commands[0][:4] == ["C:/npm/claude.cmd", "-p", "--model", "haiku"]
262
-
263
-
264
- def test_ask_drafter_should_return_none_for_an_error_result(monkeypatch: pytest.MonkeyPatch) -> None:
265
- class FailedDrafter:
266
- stdout = json.dumps({"is_error": True, "result": "Credit balance is too low"})
267
-
268
- monkeypatch.setattr(chat_reply_gate.subprocess, "run", lambda *_args, **_kwargs: FailedDrafter())
269
- assert chat_reply_gate.ask_drafter(OVER_LIMIT_REPLY, "too long") is None
270
-
271
-
272
- def test_should_forget_the_shown_draft_when_a_later_reply_passes(
273
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
274
- ) -> None:
275
- monkeypatch.setattr(chat_reply_gate, "ask_drafter", lambda *_args: PASSING_DRAFT)
276
- run_hook(monkeypatch, capsys, display_input(OVER_LIMIT_REPLY))
277
- run_hook(monkeypatch, capsys, {**display_input(THREE_SENTENCE_REPLY), "message_id": "m2"})
278
- _, hook_output = run_hook(monkeypatch, capsys, {"hook_event_name": "UserPromptSubmit", "session_id": "abc"})
279
- assert hook_output is not None
280
- assert PASSING_DRAFT not in str(hook_output["additionalContext"])
281
-
282
-
283
- def test_should_show_a_batch_without_a_message_id_as_it_is(
284
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
285
- ) -> None:
286
- monkeypatch.setattr(chat_reply_gate, "ask_drafter", lambda *_args: pytest.fail("drafter called"))
287
- batch_input = {**display_input("PR one merged. PR two", is_final=False), "message_id": None}
288
- exit_code, hook_output = run_hook(monkeypatch, capsys, batch_input)
289
- assert exit_code == 0
290
- assert hook_output is None
291
-
292
-
293
- def test_drafter_environment_should_carry_the_guard(monkeypatch: pytest.MonkeyPatch) -> None:
294
- monkeypatch.setenv("CLAUDE_CODE_SESSION_ID", "parent")
295
- environment = chat_reply_gate.drafter_environment()
296
- assert environment["CHAT_REPLY_DRAFTER_ACTIVE"] == "1"
297
- assert "CLAUDE_CODE_SESSION_ID" not in environment
298
-
299
-
300
- def test_state_file_path_should_strip_unsafe_characters(tmp_path: Path) -> None:
301
- state_path = chat_reply_gate.state_file_path("claude-chat-reply-held-", "../a b/c")
302
- assert state_path == tmp_path / "claude-chat-reply-held-abc.txt"