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,1324 +0,0 @@
1
- import io
2
- import json
3
- from pathlib import Path
4
-
5
- import pytest
6
-
7
- import reply_length_gate
8
- from hooks_constants.reply_length_gate_constants import (
9
- MAXIMUM_SENTENCE_COUNT,
10
- MAXIMUM_WORDS_PER_SENTENCE,
11
- )
12
-
13
- REPLY_TOOL_NAME = "mcp__hearthbot__reply"
14
- POST_TOOL_NAME = "mcp__hearthbot__post_message"
15
- OVER_SENTENCE_LIMIT_TEXT = " ".join(
16
- f"Status line {each_number} is ready." for each_number in range(MAXIMUM_SENTENCE_COUNT + 1)
17
- )
18
- OVER_WORD_LIMIT_SENTENCE = (
19
- "One two three "
20
- + " ".join(f"word{each_number}" for each_number in range(3, MAXIMUM_WORDS_PER_SENTENCE + 1))
21
- + "."
22
- )
23
- AT_WORD_LIMIT_SENTENCE = (
24
- "One two three "
25
- + " ".join(f"word{each_number}" for each_number in range(3, MAXIMUM_WORDS_PER_SENTENCE))
26
- + "."
27
- )
28
-
29
-
30
- def run_gate(
31
- monkeypatch: pytest.MonkeyPatch,
32
- capsys: pytest.CaptureFixture[str],
33
- tool_name: str,
34
- tool_input: dict[str, object],
35
- transcript_path: Path | None = None,
36
- ) -> tuple[int, str]:
37
- hook_input: dict[str, object] = {
38
- "hook_event_name": "PreToolUse",
39
- "tool_name": tool_name,
40
- "tool_input": tool_input,
41
- }
42
- if transcript_path is not None:
43
- hook_input["transcript_path"] = str(transcript_path)
44
- monkeypatch.setattr(
45
- "sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
46
- )
47
- exit_code = reply_length_gate.main()
48
- return exit_code, capsys.readouterr().err
49
-
50
-
51
- @pytest.fixture(autouse=True)
52
- def isolated_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
53
- monkeypatch.setenv("HOME", str(tmp_path))
54
- monkeypatch.setenv("USERPROFILE", str(tmp_path))
55
- switch_path = tmp_path / ".claude" / "visual-reply-mode.json"
56
- switch_path.parent.mkdir(exist_ok=True)
57
- switch_path.write_text('{"enabled": true}', encoding="utf-8")
58
-
59
-
60
- def test_should_allow_three_short_sentences(
61
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
62
- ) -> None:
63
- exit_code, stderr_text = run_gate(
64
- monkeypatch,
65
- capsys,
66
- REPLY_TOOL_NAME,
67
- {"text": "The fix is in. Tests pass. It ships with the next release."},
68
- )
69
- assert (exit_code, stderr_text) == (0, "")
70
-
71
-
72
- def test_should_allow_a_sentence_at_the_word_limit(
73
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
74
- ) -> None:
75
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": AT_WORD_LIMIT_SENTENCE})
76
- assert exit_code == 0
77
-
78
-
79
- def test_should_deny_too_many_sentences_and_name_the_count(
80
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
81
- ) -> None:
82
- exit_code, stderr_text = run_gate(
83
- monkeypatch,
84
- capsys,
85
- REPLY_TOOL_NAME,
86
- {"text": OVER_SENTENCE_LIMIT_TEXT},
87
- )
88
- assert exit_code == 2
89
- assert f"{MAXIMUM_SENTENCE_COUNT + 1} sentences" in stderr_text
90
- assert f"limit {MAXIMUM_SENTENCE_COUNT}" in stderr_text
91
-
92
-
93
- def test_should_deny_a_sentence_over_the_word_limit_and_name_it(
94
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
95
- ) -> None:
96
- exit_code, stderr_text = run_gate(
97
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": OVER_WORD_LIMIT_SENTENCE}
98
- )
99
- assert exit_code == 2
100
- assert f"{MAXIMUM_WORDS_PER_SENTENCE + 1} words" in stderr_text
101
- assert f"limit {MAXIMUM_WORDS_PER_SENTENCE}" in stderr_text
102
- assert "One two three" in stderr_text
103
-
104
-
105
- def test_should_check_the_project_chat_post_tool(
106
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
107
- ) -> None:
108
- exit_code, _ = run_gate(monkeypatch, capsys, POST_TOOL_NAME, {"text": OVER_SENTENCE_LIMIT_TEXT})
109
- assert exit_code == 2
110
-
111
-
112
- def test_should_count_each_line_of_a_list_as_a_sentence(
113
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
114
- ) -> None:
115
- exit_code, stderr_text = run_gate(
116
- monkeypatch,
117
- capsys,
118
- REPLY_TOOL_NAME,
119
- {"text": "\n".join(f"- item {each_number}" for each_number in range(MAXIMUM_SENTENCE_COUNT + 1))},
120
- )
121
- assert exit_code == 2
122
- assert f"{MAXIMUM_SENTENCE_COUNT + 1} sentences" in stderr_text
123
-
124
-
125
- def test_should_not_count_urls_as_words(
126
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
127
- ) -> None:
128
- long_url = "https://example.com/" + "/".join(["segment"] * 20)
129
- exit_code, _ = run_gate(
130
- monkeypatch,
131
- capsys,
132
- REPLY_TOOL_NAME,
133
- {"text": f"The draft is up at {long_url} for review."},
134
- )
135
- assert exit_code == 0
136
-
137
-
138
- def test_should_not_count_link_targets_as_words(
139
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
140
- ) -> None:
141
- exit_code, _ = run_gate(
142
- monkeypatch,
143
- capsys,
144
- REPLY_TOOL_NAME,
145
- {
146
- "text": "Read [the pull request](https://example.com/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p) now."
147
- },
148
- )
149
- assert exit_code == 0
150
-
151
-
152
- def test_should_not_count_backticked_spans_as_words(
153
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
154
- ) -> None:
155
- exit_code, _ = run_gate(
156
- monkeypatch,
157
- capsys,
158
- REPLY_TOOL_NAME,
159
- {
160
- "text": "Run `one two three four five six seven eight nine ten eleven twelve thirteen` now."
161
- },
162
- )
163
- assert exit_code == 0
164
-
165
-
166
- def test_should_not_count_fenced_blocks(
167
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
168
- ) -> None:
169
- fenced_block = "```\nOne.\nTwo.\nThree.\nFour.\n" + OVER_WORD_LIMIT_SENTENCE + "\n```"
170
- exit_code, _ = run_gate(
171
- monkeypatch,
172
- capsys,
173
- REPLY_TOOL_NAME,
174
- {"text": f"Paste this block.\n{fenced_block}"},
175
- )
176
- assert exit_code == 0
177
-
178
-
179
- def test_should_not_split_on_decimal_points(
180
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
181
- ) -> None:
182
- exit_code, _ = run_gate(
183
- monkeypatch,
184
- capsys,
185
- REPLY_TOOL_NAME,
186
- {"text": "Version 4.5 shipped. Speed rose 2.5 times. Nothing else moved."},
187
- )
188
- assert exit_code == 0
189
-
190
-
191
- def test_should_allow_other_tools(
192
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
193
- ) -> None:
194
- exit_code, _ = run_gate(
195
- monkeypatch, capsys, "mcp__other__reply", {"text": "One. Two. Three. Four."}
196
- )
197
- assert exit_code == 0
198
-
199
-
200
- def test_should_allow_a_call_without_text(
201
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
202
- ) -> None:
203
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"card": {"blocks": []}})
204
- assert exit_code == 0
205
-
206
-
207
- def test_should_allow_empty_stdin(monkeypatch: pytest.MonkeyPatch) -> None:
208
- monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"")))
209
- assert reply_length_gate.main() == 0
210
-
211
-
212
- def test_should_deny_a_pr_number_with_no_link_and_name_it(
213
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
214
- ) -> None:
215
- exit_code, stderr_text = run_gate(
216
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "Both land in PR 5256."}
217
- )
218
- assert exit_code == 2
219
- assert "PR 5256" in stderr_text
220
-
221
-
222
- def test_should_deny_a_bare_hash_number(
223
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
224
- ) -> None:
225
- exit_code, stderr_text = run_gate(
226
- monkeypatch, capsys, POST_TOOL_NAME, {"text": "It merged in #4347."}
227
- )
228
- assert exit_code == 2
229
- assert "#4347" in stderr_text
230
-
231
-
232
- def test_should_allow_a_pr_number_inside_its_link(
233
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
234
- ) -> None:
235
- exit_code, stderr_text = run_gate(
236
- monkeypatch,
237
- capsys,
238
- REPLY_TOOL_NAME,
239
- {"text": "Both land in [PR 5256](https://github.com/owner/repo/pull/5256)."},
240
- )
241
- assert (exit_code, stderr_text) == (0, "")
242
-
243
-
244
- def test_should_allow_a_bare_pull_request_url(
245
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
246
- ) -> None:
247
- exit_code, _ = run_gate(
248
- monkeypatch,
249
- capsys,
250
- REPLY_TOOL_NAME,
251
- {"text": "Pull request: https://github.com/owner/repo/pull/5256"},
252
- )
253
- assert exit_code == 0
254
-
255
-
256
- def test_should_deny_a_reply_that_says_likely_and_name_the_word(
257
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
258
- ) -> None:
259
- exit_code, stderr_text = run_gate(
260
- monkeypatch,
261
- capsys,
262
- REPLY_TOOL_NAME,
263
- {"text": "The back-test flagged the garland star. It likely flags the call button."},
264
- )
265
- assert exit_code == 2
266
- assert 'Banned word "likely"' in stderr_text
267
-
268
-
269
- def test_should_allow_the_same_reply_without_the_banned_word(
270
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
271
- ) -> None:
272
- exit_code, stderr_text = run_gate(
273
- monkeypatch,
274
- capsys,
275
- REPLY_TOOL_NAME,
276
- {"text": "The back-test flagged the garland star. It flags the call button in 7 themes."},
277
- )
278
- assert (exit_code, stderr_text) == (0, "")
279
-
280
-
281
- @pytest.mark.parametrize(
282
- "reply_text",
283
- ["Really fast.", "A real-world case.", "In reality it passed.", "ACTUALLY done."],
284
- )
285
- def test_should_deny_default_banned_words_in_any_case_and_spacing(
286
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
287
- ) -> None:
288
- exit_code, _ = run_gate(monkeypatch, capsys, POST_TOOL_NAME, {"text": reply_text})
289
- assert exit_code == 2
290
-
291
-
292
- @pytest.mark.parametrize(
293
- "reply_text",
294
- [
295
- "I realized the cache missed.",
296
- "Run `actual_count` again.",
297
- "See https://example.com/likely.",
298
- ],
299
- )
300
- def test_should_allow_banned_words_inside_longer_words_code_and_urls(
301
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
302
- ) -> None:
303
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": reply_text})
304
- assert exit_code == 0
305
-
306
-
307
- def test_should_replace_the_defaults_with_the_configured_list(
308
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
309
- ) -> None:
310
- config_path = tmp_path / ".claude" / "reply-banned-words.json"
311
- config_path.parent.mkdir(exist_ok=True)
312
- config_path.write_text(json.dumps({"banned_words": ["synergy"]}), encoding="utf-8")
313
- likely_exit_code, _ = run_gate(
314
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "It likely passed."}
315
- )
316
- synergy_exit_code, stderr_text = run_gate(
317
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "Synergy shipped."}
318
- )
319
- assert (likely_exit_code, synergy_exit_code) == (0, 2)
320
- assert 'Banned word "synergy"' in stderr_text
321
-
322
-
323
- def test_should_read_the_list_from_the_path_the_environment_names(
324
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
325
- ) -> None:
326
- config_path = tmp_path / "words.json"
327
- config_path.write_text(json.dumps({"banned_words": ["basically"]}), encoding="utf-8")
328
- monkeypatch.setenv("CLAUDE_REPLY_BANNED_WORDS_PATH", str(config_path))
329
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "Basically done."})
330
- assert exit_code == 2
331
-
332
-
333
- def test_should_keep_the_defaults_when_the_config_file_is_malformed(
334
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
335
- ) -> None:
336
- config_path = tmp_path / ".claude" / "reply-banned-words.json"
337
- config_path.parent.mkdir(exist_ok=True)
338
- config_path.write_text("{not json", encoding="utf-8")
339
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "It likely passed."})
340
- assert exit_code == 2
341
-
342
-
343
- DECISION_TOOL_NAME = "mcp__hearthbot__ask_decision"
344
-
345
-
346
- def test_should_deny_a_decision_card_that_hedges_in_an_option(
347
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
348
- ) -> None:
349
- exit_code, stderr_text = run_gate(
350
- monkeypatch,
351
- capsys,
352
- DECISION_TOOL_NAME,
353
- {
354
- "question": "Exclude the call button?",
355
- "context": "Crops show the call button flagged in 7 themes.",
356
- "options": [
357
- {"label": "Exclude it", "consequence": "Three themes probably pass."},
358
- {"label": "Leave it", "consequence": "Three themes keep a warning."},
359
- ],
360
- "recommended": 0,
361
- },
362
- )
363
- assert exit_code == 2
364
- assert '"probably"' in stderr_text
365
-
366
-
367
- def short_card(**all_overrides: object) -> dict[str, object]:
368
- card: dict[str, object] = {
369
- "question": "Exclude the call button?",
370
- "context": "Crops show the call button flagged in 7 themes.",
371
- "reason": "Seven themes pass with it excluded.",
372
- "options": [
373
- {"label": "Exclude it", "consequence": "Seven themes pass."},
374
- {"label": "Leave it", "consequence": "Seven themes keep a warning."},
375
- ],
376
- "recommended": 0,
377
- }
378
- card.update(all_overrides)
379
- return card
380
-
381
-
382
- def test_should_allow_a_card_context_of_two_sentences_at_the_word_limit(
383
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
384
- ) -> None:
385
- exit_code, stderr_text = run_gate(
386
- monkeypatch,
387
- capsys,
388
- DECISION_TOOL_NAME,
389
- short_card(context=" ".join([AT_WORD_LIMIT_SENTENCE] * 2)),
390
- )
391
- assert (exit_code, stderr_text) == (0, "")
392
-
393
-
394
- def test_should_deny_a_card_context_of_four_sentences(
395
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
396
- ) -> None:
397
- exit_code, stderr_text = run_gate(
398
- monkeypatch,
399
- capsys,
400
- DECISION_TOOL_NAME,
401
- short_card(context="One. Two. Three. Four."),
402
- )
403
- assert exit_code == 2
404
- assert "Card context too long: 4 sentences, limit 2." in stderr_text
405
- assert "publish an Artifact page with a tap button per item and a Send button" in stderr_text
406
-
407
-
408
- @pytest.mark.parametrize(
409
- "context_text",
410
- [
411
- "Three themes fail.\n- `Cedar`\n- `Birch`",
412
- "Open one.\n- https://github.com/owner/repo/pull/12\n- https://github.com/owner/repo/pull/13",
413
- ],
414
- )
415
- def test_should_deny_a_card_context_list_whose_items_are_code_or_urls(
416
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], context_text: str
417
- ) -> None:
418
- exit_code, stderr_text = run_gate(
419
- monkeypatch,
420
- capsys,
421
- DECISION_TOOL_NAME,
422
- short_card(context=context_text),
423
- )
424
- assert exit_code == 2
425
- assert "Card context holds a list of items" in stderr_text
426
-
427
-
428
- def test_should_deny_a_card_context_that_lists_several_items_and_name_the_page_flow(
429
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
430
- ) -> None:
431
- exit_code, stderr_text = run_gate(
432
- monkeypatch,
433
- capsys,
434
- DECISION_TOOL_NAME,
435
- short_card(context="Three themes fail.\n- Cedar\n- Birch"),
436
- )
437
- assert exit_code == 2
438
- assert "Card context holds a list of items" in stderr_text
439
- assert "publish an Artifact page with a tap button per item and a Send button" in stderr_text
440
- assert "send a short reply that links the page" in stderr_text
441
-
442
-
443
- def test_should_deny_a_long_card_consequence(
444
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
445
- ) -> None:
446
- exit_code, stderr_text = run_gate(
447
- monkeypatch,
448
- capsys,
449
- DECISION_TOOL_NAME,
450
- short_card(
451
- options=[
452
- {"label": "Exclude it", "consequence": OVER_WORD_LIMIT_SENTENCE},
453
- {"label": "Leave it", "consequence": "Seven themes keep a warning."},
454
- ]
455
- ),
456
- )
457
- assert exit_code == 2
458
- assert f"Sentence too long: {MAXIMUM_WORDS_PER_SENTENCE + 1} words, limit {MAXIMUM_WORDS_PER_SENTENCE}" in stderr_text
459
-
460
-
461
- @pytest.mark.parametrize("card_field_name", ["question", "reason"])
462
- def test_should_deny_a_long_sentence_in_the_card_question_and_reason(
463
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], card_field_name: str
464
- ) -> None:
465
- exit_code, stderr_text = run_gate(
466
- monkeypatch,
467
- capsys,
468
- DECISION_TOOL_NAME,
469
- short_card(**{card_field_name: OVER_WORD_LIMIT_SENTENCE}),
470
- )
471
- assert exit_code == 2
472
- assert f"Sentence too long: {MAXIMUM_WORDS_PER_SENTENCE + 1} words" in stderr_text
473
-
474
-
475
- @pytest.mark.parametrize(
476
- "reply_text",
477
- ["It maybe passed.", "Perhaps it passed.", "It might be the cache.", "I am not sure it passed."],
478
- )
479
- def test_should_deny_the_hedges_the_default_list_adds(
480
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
481
- ) -> None:
482
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": reply_text})
483
- assert exit_code == 2
484
-
485
-
486
- def test_should_ignore_a_banned_word_in_a_card_field_that_holds_no_prose(
487
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
488
- ) -> None:
489
- exit_code, stderr_text = run_gate(
490
- monkeypatch,
491
- capsys,
492
- DECISION_TOOL_NAME,
493
- {
494
- "question": "Exclude the call button?",
495
- "context": "Crops show the call button flagged in 7 themes.",
496
- "options": [
497
- {"id": "maybe", "label": "Exclude it", "consequence": "Seven themes pass."},
498
- {"id": "leave", "label": "Leave it", "consequence": "Seven themes keep a warning."},
499
- ],
500
- "kind": "maybe",
501
- "recommended": 0,
502
- },
503
- )
504
- assert (exit_code, stderr_text) == (0, "")
505
-
506
-
507
- @pytest.mark.parametrize(
508
- "card_field_name",
509
- ["question", "context", "label", "consequence"],
510
- )
511
- def test_should_deny_a_banned_word_in_each_card_prose_field(
512
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], card_field_name: str
513
- ) -> None:
514
- option = {"label": "Exclude it", "consequence": "Seven themes pass."}
515
- card: dict[str, object] = {
516
- "question": "Exclude the call button?",
517
- "context": "Crops show the call button flagged.",
518
- "options": [option],
519
- }
520
- if card_field_name in option:
521
- option[card_field_name] = "It maybe passes."
522
- else:
523
- card[card_field_name] = "It maybe passes."
524
- exit_code, stderr_text = run_gate(monkeypatch, capsys, DECISION_TOOL_NAME, card)
525
- assert exit_code == 2
526
- assert '"maybe"' in stderr_text
527
-
528
-
529
- def test_should_apply_the_configured_list_to_decision_cards(
530
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
531
- ) -> None:
532
- config_path = tmp_path / ".claude" / "reply-banned-words.json"
533
- config_path.parent.mkdir(exist_ok=True)
534
- config_path.write_text(json.dumps({"banned_words": ["synergy"]}), encoding="utf-8")
535
- probably_exit_code, _ = run_gate(
536
- monkeypatch,
537
- capsys,
538
- DECISION_TOOL_NAME,
539
- {"question": "Ship it?", "context": "It probably passes."},
540
- )
541
- synergy_exit_code, stderr_text = run_gate(
542
- monkeypatch,
543
- capsys,
544
- DECISION_TOOL_NAME,
545
- {"question": "Ship it?", "context": "Synergy shipped."},
546
- )
547
- assert (probably_exit_code, synergy_exit_code) == (0, 2)
548
- assert 'Banned word "synergy"' in stderr_text
549
-
550
-
551
- QUOTED_COLON_SENTENCE = "That second reading matters: one message added 6.7k to Messages."
552
-
553
-
554
- def test_should_deny_the_quoted_mid_sentence_colon(
555
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
556
- ) -> None:
557
- exit_code, stderr_text = run_gate(
558
- monkeypatch, capsys, POST_TOOL_NAME, {"text": QUOTED_COLON_SENTENCE}
559
- )
560
- assert exit_code == 2
561
- assert "colon" in stderr_text
562
-
563
-
564
- @pytest.mark.parametrize(
565
- "allowed_text",
566
- [
567
- "That second reading matters. One message added 6.7k to Messages.",
568
- "Two checks failed:\n- lint\n- types",
569
- "It fired at 9:47 today.",
570
- "The call `a: int` passed.",
571
- "See https://example.com/a: b for it.",
572
- "Both land in [PR 5256](https://github.com/owner/repo/pull/5256).",
573
- "```\nflag: x\n```",
574
- ],
575
- )
576
- def test_should_allow_a_colon_that_introduces_a_list_or_sits_in_a_span(
577
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], allowed_text: str
578
- ) -> None:
579
- exit_code, stderr_text = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": allowed_text})
580
- assert (exit_code, stderr_text) == (0, "")
581
-
582
-
583
- def test_should_deny_an_em_dash_and_allow_one_in_code(
584
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
585
- ) -> None:
586
- denied_code, denied_text = run_gate(
587
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "The fix \u2014 a rename \u2014 is in."}
588
- )
589
- allowed_code, _ = run_gate(
590
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "The fix uses `\u2014` in a label."}
591
- )
592
- assert (denied_code, allowed_code) == (2, 0)
593
- assert "em dash" in denied_text
594
-
595
-
596
- WIDGET_TOOL_NAME = "mcp__hearthbot__post_widget"
597
- TWO_SENTENCE_REPLY = "The deploy stopped on a missing file. The page shows each step."
598
-
599
-
600
- def write_transcript(transcript_path: Path, all_entries: list[dict[str, object]]) -> Path:
601
- transcript_path.write_text(
602
- "\n".join(json.dumps(each_entry) for each_entry in all_entries), encoding="utf-8"
603
- )
604
- return transcript_path
605
-
606
-
607
- def prompt_entry(text: str) -> dict[str, object]:
608
- return {"type": "user", "message": {"role": "user", "content": text}}
609
-
610
-
611
- def tool_call_entry(tool_name: str) -> dict[str, object]:
612
- return {
613
- "type": "assistant",
614
- "message": {"content": [{"type": "tool_use", "id": "t1", "name": tool_name, "input": {}}]},
615
- }
616
-
617
-
618
- def turn_mode_off(tmp_path: Path) -> None:
619
- switch_path = tmp_path / ".claude" / "visual-reply-mode.json"
620
- switch_path.parent.mkdir(exist_ok=True)
621
- switch_path.write_text('{"enabled": false}', encoding="utf-8")
622
-
623
-
624
- def test_should_deny_an_abbreviation_in_a_reply(
625
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
626
- ) -> None:
627
- exit_code, stderr_text = run_gate(
628
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "The API deploy is fixed."}
629
- )
630
- assert exit_code == 2
631
- assert 'Abbreviation "API"' in stderr_text
632
-
633
-
634
- def test_should_allow_an_abbreviation_inside_a_pull_request_link(
635
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
636
- ) -> None:
637
- exit_code, _ = run_gate(
638
- monkeypatch,
639
- capsys,
640
- REPLY_TOOL_NAME,
641
- {"text": "It merged in [PR 5256](https://github.com/owner/repo/pull/5256)."},
642
- )
643
- assert exit_code == 0
644
-
645
-
646
- def test_should_deny_a_tracker_item_number(
647
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
648
- ) -> None:
649
- exit_code, stderr_text = run_gate(
650
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "Item 6 is ready for you."}
651
- )
652
- assert exit_code == 2
653
- assert 'Tracker number "Item 6"' in stderr_text
654
-
655
-
656
- def test_should_deny_an_abbreviation_in_a_decision_card(
657
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
658
- ) -> None:
659
- exit_code, stderr_text = run_gate(
660
- monkeypatch,
661
- capsys,
662
- DECISION_TOOL_NAME,
663
- {"question": "Ship the API fix?", "options": [{"label": "Ship it", "consequence": "It merges."}]},
664
- )
665
- assert exit_code == 2
666
- assert 'Abbreviation "API"' in stderr_text
667
-
668
-
669
- def test_should_deny_two_sentences_with_no_visual_this_turn(
670
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
671
- ) -> None:
672
- transcript_path = write_transcript(
673
- tmp_path / "t.jsonl",
674
- [prompt_entry("why did the deploy stop"), tool_call_entry("Bash")],
675
- )
676
- exit_code, stderr_text = run_gate(
677
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": TWO_SENTENCE_REPLY}, transcript_path
678
- )
679
- assert exit_code == 2
680
- assert "no visual this turn" in stderr_text
681
-
682
-
683
- def test_should_allow_two_sentences_after_a_widget_this_turn(
684
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
685
- ) -> None:
686
- transcript_path = write_transcript(
687
- tmp_path / "t.jsonl",
688
- [prompt_entry("why did the deploy stop"), tool_call_entry(WIDGET_TOOL_NAME)],
689
- )
690
- exit_code, _ = run_gate(
691
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": TWO_SENTENCE_REPLY}, transcript_path
692
- )
693
- assert exit_code == 0
694
-
695
-
696
- def test_should_deny_when_the_only_widget_came_before_the_last_prompt(
697
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
698
- ) -> None:
699
- transcript_path = write_transcript(
700
- tmp_path / "t.jsonl",
701
- [
702
- prompt_entry("why did the deploy stop"),
703
- tool_call_entry(WIDGET_TOOL_NAME),
704
- prompt_entry("and the logs"),
705
- ],
706
- )
707
- exit_code, _ = run_gate(
708
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": TWO_SENTENCE_REPLY}, transcript_path
709
- )
710
- assert exit_code == 2
711
-
712
-
713
- def test_should_allow_one_sentence_with_no_visual(
714
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
715
- ) -> None:
716
- transcript_path = write_transcript(tmp_path / "t.jsonl", [prompt_entry("merge it")])
717
- exit_code, _ = run_gate(
718
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "It merged."}, transcript_path
719
- )
720
- assert exit_code == 0
721
-
722
-
723
- def test_should_deny_an_anchor_link_inside_widget_code(
724
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
725
- ) -> None:
726
- exit_code, stderr_text = run_gate(
727
- monkeypatch,
728
- capsys,
729
- WIDGET_TOOL_NAME,
730
- {"widget_code": '<div><a href="https://claude.ai/artifact/x">Review page</a></div>'},
731
- )
732
- assert exit_code == 2
733
- assert "does not open in the Claude app" in stderr_text
734
-
735
-
736
- def test_should_allow_widget_code_with_no_anchor_and_advise_a_flowchart(
737
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
738
- ) -> None:
739
- hook_input = {
740
- "hook_event_name": "PreToolUse",
741
- "tool_name": WIDGET_TOOL_NAME,
742
- "tool_input": {"widget_code": "<div>Three steps</div>"},
743
- }
744
- monkeypatch.setattr(
745
- "sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
746
- )
747
- exit_code = reply_length_gate.main()
748
- assert exit_code == 0
749
- advice_output = json.loads(capsys.readouterr().out)
750
- assert "Redraw this widget as one SVG flowchart" in advice_output["hookSpecificOutput"]["additionalContext"]
751
-
752
-
753
- def test_should_skip_the_mode_checks_when_the_switch_turns_the_mode_off(
754
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
755
- ) -> None:
756
- turn_mode_off(tmp_path)
757
- transcript_path = write_transcript(tmp_path / "t.jsonl", [prompt_entry("status")])
758
- all_exit_codes = [
759
- run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "The API deploy is fixed."})[0],
760
- run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "Which build ships first?"})[0],
761
- run_gate(
762
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": TWO_SENTENCE_REPLY}, transcript_path
763
- )[0],
764
- run_gate(monkeypatch, capsys, WIDGET_TOOL_NAME, {"widget_code": '<a href="x">go</a>'})[0],
765
- ]
766
- assert all_exit_codes == [0, 0, 0, 0]
767
-
768
-
769
- def test_should_keep_the_length_check_when_the_mode_is_off(
770
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
771
- ) -> None:
772
- turn_mode_off(tmp_path)
773
- exit_code, _ = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": OVER_SENTENCE_LIMIT_TEXT})
774
- assert exit_code == 2
775
-
776
-
777
- def test_should_deny_the_quoted_cause_with_no_source(
778
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
779
- ) -> None:
780
- exit_code, stderr_text = run_gate(
781
- monkeypatch,
782
- capsys,
783
- POST_TOOL_NAME,
784
- {"text": "That window was Windows PowerShell without Administrator, so the write did not land."},
785
- )
786
- assert exit_code == 2
787
- assert "Cause with no source" in stderr_text
788
-
789
-
790
- @pytest.mark.parametrize(
791
- "reply_text",
792
- [
793
- "The write did not land, because the next read printed `Right after write: 1`.",
794
- "The job failed because of [run 12](https://github.com/owner/repo/actions/runs/12).",
795
- ],
796
- )
797
- def test_should_allow_a_cause_that_cites_evidence(
798
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
799
- ) -> None:
800
- exit_code, stderr_text = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": reply_text})
801
- assert (exit_code, stderr_text) == (0, "")
802
-
803
-
804
- @pytest.mark.parametrize(
805
- "reply_text",
806
- [
807
- "That window lacked Administrator, so the write did not land.\n\n```powershell\nRestart-Service x\n```",
808
- "The error shows the hook is the only line. So the filter wrote nothing.",
809
- "One line survived because its start did not match my filter. Run this.",
810
- ],
811
- )
812
- def test_should_deny_a_cause_whose_own_sentence_cites_nothing(
813
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
814
- ) -> None:
815
- exit_code, stderr_text = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": reply_text})
816
- assert exit_code == 2
817
- assert "Cause with no source" in stderr_text
818
-
819
-
820
- def test_should_allow_a_reply_with_no_causal_claim(
821
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
822
- ) -> None:
823
- exit_code, _ = run_gate(
824
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": "The runner restarted. Also, so far so good."}
825
- )
826
- assert exit_code == 0
827
-
828
-
829
- UPDATE_TOOL_NAME = "mcp__hearthbot__update_message"
830
- QUESTION_CARD_TOOL_NAME = "AskUserQuestion"
831
- RUN_LINK_REPLY = (
832
- "The last build passed in [run 18239174012]"
833
- "(https://github.com/owner/repo/actions/runs/18239174012)."
834
- )
835
-
836
-
837
- def write_tool_result_transcript(transcript_path: Path, result_text: str, is_error: bool = False) -> Path:
838
- all_entries = [
839
- {"type": "user", "message": {"content": "how close are we"}},
840
- {
841
- "type": "assistant",
842
- "message": {"content": [{"type": "tool_use", "id": "t1", "name": "Bash", "input": {}}]},
843
- },
844
- {
845
- "type": "user",
846
- "message": {
847
- "content": [
848
- {
849
- "type": "tool_result",
850
- "tool_use_id": "t1",
851
- "is_error": is_error,
852
- "content": [{"type": "text", "text": result_text}],
853
- }
854
- ]
855
- },
856
- },
857
- ]
858
- transcript_path.write_text(
859
- "\n".join(json.dumps(each_entry) for each_entry in all_entries), encoding="utf-8"
860
- )
861
- return transcript_path
862
-
863
-
864
- @pytest.mark.parametrize("tool_name", [REPLY_TOOL_NAME, POST_TOOL_NAME, UPDATE_TOOL_NAME])
865
- def test_should_deny_an_estimate_and_quote_it(
866
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tool_name: str
867
- ) -> None:
868
- exit_code, stderr_text = run_gate(
869
- monkeypatch, capsys, tool_name, {"text": "My estimate is one to two hours."}
870
- )
871
- assert exit_code == 2
872
- assert 'No tool validated this: "My estimate is one to two hours."' in stderr_text
873
-
874
-
875
- @pytest.mark.parametrize(
876
- "reply_text",
877
- [
878
- "I didn\u2019t confirm this one.",
879
- "I have not checked the queue.",
880
- "I assumed the build passed.",
881
- "Run 18239174012 is unverified.",
882
- "About 20 minutes left.",
883
- "It will take two hours.",
884
- "ETA is 3-4 hours.",
885
- "Estimate from the last check is two builds.",
886
- "I did not run the suite.",
887
- "The tests have not yet been run.",
888
- "The job takes 39 minutes.",
889
- "The migration takes seventy minutes.",
890
- "I was not able to verify the fix.",
891
- "The fix has not been validated.",
892
- "The fix was not verified in production.",
893
- "The suite takes about 90 seconds.",
894
- ],
895
- )
896
- def test_should_deny_each_unvalidated_claim_shape(
897
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
898
- ) -> None:
899
- exit_code, stderr_text = run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": reply_text})
900
- assert exit_code == 2
901
- assert "No tool validated this" in stderr_text
902
-
903
-
904
- @pytest.mark.parametrize(
905
- "reply_text",
906
- [
907
- "Two builds are left in the queue.",
908
- "I will check the queue again within 15 minutes.",
909
- "The last queue build took 39 minutes.",
910
- "The gate now refuses `guess` and `assumed`.",
911
- "The job takes 39 minutes, see run 18239174012.",
912
- "The hook cannot run on Windows.",
913
- "The crash is not a test problem.",
914
- "That is not the check that failed.",
915
- "The migration did not run out of memory.",
916
- "We did not run into any errors.",
917
- "This is not something to check before the release.",
918
- "The result is not what we tested for.",
919
- "The migration did not run on the replica.",
920
- "The old parser did not validate the header length.",
921
- "The job has not run yet.",
922
- "The config file is not checked in.",
923
- "The release branch is not checked out.",
924
- ],
925
- )
926
- def test_should_allow_a_reply_with_no_unvalidated_claim(
927
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
928
- ) -> None:
929
- assert run_gate(monkeypatch, capsys, REPLY_TOOL_NAME, {"text": reply_text}) == (0, "")
930
-
931
-
932
- def test_should_deny_a_run_no_tool_result_shows(
933
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
934
- ) -> None:
935
- transcript_path = write_tool_result_transcript(tmp_path / "s.jsonl", "run 18239170000 ok")
936
- exit_code, stderr_text = run_gate(
937
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": RUN_LINK_REPLY}, transcript_path
938
- )
939
- assert exit_code == 2
940
- assert 'cites "run 18239174012"' in stderr_text
941
-
942
-
943
- def test_should_deny_a_run_only_an_error_result_shows(
944
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
945
- ) -> None:
946
- transcript_path = write_tool_result_transcript(
947
- tmp_path / "s.jsonl", "run 18239174012 ok", is_error=True
948
- )
949
- exit_code, stderr_text = run_gate(
950
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": RUN_LINK_REPLY}, transcript_path
951
- )
952
- assert exit_code == 2
953
- assert "no tool result in this session shows it" in stderr_text
954
-
955
-
956
- def test_should_allow_a_run_a_tool_result_shows(
957
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
958
- ) -> None:
959
- transcript_path = write_tool_result_transcript(tmp_path / "s.jsonl", "run 18239174012 took 39m")
960
- assert run_gate(
961
- monkeypatch, capsys, REPLY_TOOL_NAME, {"text": RUN_LINK_REPLY}, transcript_path
962
- ) == (0, "")
963
-
964
-
965
- def test_should_deny_a_question_card_that_admits_a_skipped_check(
966
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
967
- ) -> None:
968
- card = {
969
- "questions": [
970
- {
971
- "question": "Ship the gate now?",
972
- "header": "Ship",
973
- "options": [
974
- {"label": "Ship", "description": "I did not test the Stop hook."},
975
- {"label": "Wait", "description": "Run the suite first."},
976
- ],
977
- }
978
- ]
979
- }
980
- exit_code, stderr_text = run_gate(monkeypatch, capsys, QUESTION_CARD_TOOL_NAME, card)
981
- assert exit_code == 2
982
- assert "I did not test the Stop hook." in stderr_text
983
-
984
-
985
- def test_should_allow_a_question_card_with_no_unvalidated_claim(
986
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
987
- ) -> None:
988
- card = {
989
- "questions": [
990
- {
991
- "question": "Which gate design?",
992
- "header": "Design",
993
- "options": [{"label": "Fold in", "description": "One gate checks every reply."}],
994
- }
995
- ]
996
- }
997
- assert run_gate(monkeypatch, capsys, QUESTION_CARD_TOOL_NAME, card) == (0, "")
998
-
999
-
1000
- def test_should_collect_each_question_card_prose_field() -> None:
1001
- card = {
1002
- "questions": [
1003
- {"question": "Q?", "header": "H", "options": [{"label": "L", "description": "D"}, "x"]},
1004
- "not a question",
1005
- ]
1006
- }
1007
- assert reply_length_gate.all_question_card_texts(card) == ["Q?", "H", "L", "D"]
1008
-
1009
-
1010
- def test_should_quote_the_hedged_sentence() -> None:
1011
- assert reply_length_gate.unvalidated_claim_violation("Done. It should take a few minutes.") == (
1012
- 'No tool validated this: "It should take a few minutes.". Run the check yourself or through'
1013
- " a subagent, then state what the run, log line, output, or file and line shows. When you"
1014
- " cannot run it this turn, report it as `Open: <check> - <next step>`."
1015
- )
1016
-
1017
-
1018
- def test_should_skip_the_citation_check_without_a_transcript() -> None:
1019
- assert reply_length_gate.unseen_citation_violation(RUN_LINK_REPLY, None) is None
1020
-
1021
-
1022
- def test_should_leave_the_transcript_unread_when_the_text_cites_nothing(
1023
- monkeypatch: pytest.MonkeyPatch, tmp_path: Path
1024
- ) -> None:
1025
- all_read_paths: list[object] = []
1026
- monkeypatch.setattr(reply_length_gate, "successful_result_text", all_read_paths.append)
1027
- transcript_path = write_tool_result_transcript(tmp_path / "s.jsonl", "nothing cited")
1028
- assert reply_length_gate.unseen_citation_violation("Two builds left.", transcript_path) is None
1029
- assert all_read_paths == []
1030
-
1031
-
1032
- def test_should_count_a_pull_request_link_a_task_notification_shows(tmp_path: Path) -> None:
1033
- notification = {
1034
- "type": "user",
1035
- "origin": {"kind": "task-notification"},
1036
- "message": {"content": "<result>Opened https://github.com/o/r/pull/8127</result>"},
1037
- }
1038
- transcript_path = tmp_path / "s.jsonl"
1039
- transcript_path.write_text(json.dumps(notification), encoding="utf-8")
1040
- card_text = "Open as [PR 8127](https://github.com/o/r/pull/8127)."
1041
- assert reply_length_gate.unseen_citation_violation_with_hook_text(card_text, str(transcript_path)) is None
1042
- assert 'cites "/pull/8127"' in reply_length_gate.unseen_citation_violation(card_text, str(transcript_path))
1043
-
1044
-
1045
- def test_should_not_count_the_gate_echo_of_its_own_citation(tmp_path: Path) -> None:
1046
- feedback = {
1047
- "type": "user",
1048
- "isMeta": True,
1049
- "message": {"content": 'Stop hook feedback:\nThe text cites "/pull/8127", and no tool result in this'
1050
- " session shows it. Read it yourself or through a subagent first."},
1051
- }
1052
- transcript_path = tmp_path / "s.jsonl"
1053
- transcript_path.write_text(json.dumps(feedback), encoding="utf-8")
1054
- card_text = "Open as [PR 8127](https://github.com/o/r/pull/8127)."
1055
- assert 'cites "/pull/8127"' in reply_length_gate.unseen_citation_violation_with_hook_text(
1056
- card_text, str(transcript_path)
1057
- )
1058
-
1059
-
1060
- def test_should_skip_the_hook_text_read_when_a_tool_result_shows_the_link(
1061
- monkeypatch: pytest.MonkeyPatch, tmp_path: Path
1062
- ) -> None:
1063
- all_read_paths: list[object] = []
1064
- monkeypatch.setattr(reply_length_gate, "hook_supplied_text", all_read_paths.append)
1065
- transcript_path = write_tool_result_transcript(tmp_path / "s.jsonl", "pull/8127 opened")
1066
- card_text = "Open as [PR 8127](https://github.com/o/r/pull/8127)."
1067
- assert reply_length_gate.unseen_citation_violation_with_hook_text(card_text, str(transcript_path)) is None
1068
- assert all_read_paths == []
1069
-
1070
-
1071
- def test_should_split_prose_sentences_outside_fenced_blocks() -> None:
1072
- reply_text = "First one. Second one?\n```\nskipped. code\n```\nThird"
1073
- assert reply_length_gate.prose_sentences(reply_text) == ["First one.", "Second one?", "", "Third"]
1074
-
1075
-
1076
- LONG_GITHUB_COMMENT = " ".join(
1077
- "Sentence number one of a long comment that keeps going." for _ in range(MAXIMUM_SENTENCE_COUNT + 1)
1078
- )
1079
- SHORT_GITHUB_COMMENT = "Fixed the cap check. The test now covers it. Checks pass."
1080
-
1081
-
1082
- def test_should_deny_long_github_comment(
1083
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1084
- ) -> None:
1085
- exit_code, error_text = run_gate(
1086
- monkeypatch, capsys, "mcp__github__add_issue_comment", {"body": LONG_GITHUB_COMMENT}
1087
- )
1088
- assert exit_code == 2
1089
- assert f"{MAXIMUM_SENTENCE_COUNT + 1} sentences, limit {MAXIMUM_SENTENCE_COUNT}" in error_text
1090
-
1091
-
1092
- def test_should_allow_short_github_comment_with_code_and_details(
1093
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1094
- ) -> None:
1095
- comment_text = (
1096
- SHORT_GITHUB_COMMENT
1097
- + "\n```\n" + LONG_GITHUB_COMMENT + "\n```\n"
1098
- + "<details><summary>Log</summary>\n" + LONG_GITHUB_COMMENT + "\n</details>"
1099
- )
1100
- exit_code, error_text = run_gate(
1101
- monkeypatch, capsys, "mcp__github__add_issue_comment", {"body": comment_text}
1102
- )
1103
- assert (exit_code, error_text) == (0, "")
1104
-
1105
-
1106
- def test_should_read_gh_comment_body_and_skip_pr_create() -> None:
1107
- comment_command = f'gh pr comment 12 --body "{LONG_GITHUB_COMMENT}"'
1108
- create_command = f'gh pr create --body "Run gh pr comment 12 to reply. {LONG_GITHUB_COMMENT}"'
1109
- assert reply_length_gate.github_comment_texts("Bash", {"command": comment_command}) == [LONG_GITHUB_COMMENT]
1110
- assert reply_length_gate.github_comment_texts("Bash", {"command": create_command}) == []
1111
- multiline_command = 'gh pr comment 12 --body "' + LONG_GITHUB_COMMENT.replace(". ", ".\n") + '"'
1112
- assert len(reply_length_gate.github_comment_texts("Bash", {"command": multiline_command})) == 1
1113
- flagged_command = f'git push\ngh -R o/r pr comment 12 --body "note=x. {LONG_GITHUB_COMMENT}"'
1114
- assert reply_length_gate.github_comment_texts("Bash", {"command": flagged_command}) == [f"note=x. {LONG_GITHUB_COMMENT}"]
1115
- chained_command = f"git push && {comment_command}"
1116
- assert reply_length_gate.github_comment_texts("Bash", {"command": chained_command}) == [LONG_GITHUB_COMMENT]
1117
-
1118
-
1119
- def test_should_report_an_over_long_comment_text_with_the_comment_instruction() -> None:
1120
- violation = reply_length_gate.comment_length_violation(LONG_GITHUB_COMMENT)
1121
- assert violation is not None
1122
- assert violation.endswith(reply_length_gate.COMMENT_RETRY_INSTRUCTION)
1123
-
1124
-
1125
- def test_should_pass_a_comment_text_whose_long_part_sits_in_a_details_block() -> None:
1126
- comment_text = "Fixed.\n<details><summary>Log</summary>\n" + LONG_GITHUB_COMMENT + "\n</details>"
1127
- assert reply_length_gate.comment_length_violation(comment_text) is None
1128
-
1129
-
1130
- def test_joined_violations_should_put_each_violation_on_its_own_line() -> None:
1131
- assert reply_length_gate.joined_violations(["Too long.", None, "Em dash.\nNo link."]) == (
1132
- "Too long.\nEm dash.\nNo link."
1133
- )
1134
-
1135
-
1136
- def test_numbered_violations_should_number_each_line_once() -> None:
1137
- assert reply_length_gate.numbered_violations("Too long.\nEm dash.\nNo link.") == (
1138
- "1. Too long.\n2. Em dash.\n3. No link."
1139
- )
1140
- assert reply_length_gate.numbered_violations("Too long.") == "Too long."
1141
-
1142
-
1143
- def test_joined_violations_should_keep_one_violation_unchanged() -> None:
1144
- assert reply_length_gate.joined_violations([None, "Too long."]) == "Too long."
1145
-
1146
-
1147
- def test_collected_violations_should_keep_findings_made_before_a_check_raises() -> None:
1148
- def broken_check() -> str | None:
1149
- raise TypeError("malformed input")
1150
-
1151
- assert reply_length_gate.collected_violations([lambda: "Too long.", broken_check]) == "Too long."
1152
-
1153
-
1154
- def test_collected_violations_should_keep_findings_when_a_later_check_raises_any_error() -> None:
1155
- def broken_check() -> str | None:
1156
- raise RuntimeError("transcript unreadable")
1157
-
1158
- assert reply_length_gate.collected_violations([lambda: "Too long.", broken_check]) == "Too long."
1159
-
1160
-
1161
- def test_collected_violations_should_raise_when_a_check_breaks_before_any_finding() -> None:
1162
- def broken_check() -> str | None:
1163
- raise TypeError("malformed input")
1164
-
1165
- with pytest.raises(TypeError):
1166
- reply_length_gate.collected_violations([lambda: None, broken_check])
1167
-
1168
-
1169
- def test_should_name_every_broken_rule_in_one_refusal(
1170
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1171
- ) -> None:
1172
- exit_code, error_text = run_gate(
1173
- monkeypatch,
1174
- capsys,
1175
- POST_TOOL_NAME,
1176
- {"text": f"See PR 7452 and PR 7460.\n{OVER_WORD_LIMIT_SENTENCE}\nIt failed — again."},
1177
- )
1178
-
1179
- assert exit_code == reply_length_gate.BLOCK_EXIT_CODE
1180
- assert "PR 7452" in error_text
1181
- assert "PR 7460" in error_text
1182
- assert f"{MAXIMUM_WORDS_PER_SENTENCE + 1} words" in error_text
1183
- assert reply_length_gate.EM_DASH_MESSAGE in error_text
1184
- assert error_text.endswith(reply_length_gate.FIX_EVERY_ITEM_INSTRUCTION)
1185
-
1186
-
1187
- def test_should_number_nested_violations_once_in_one_refusal(
1188
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1189
- ) -> None:
1190
- exit_code, error_text = run_gate(
1191
- monkeypatch,
1192
- capsys,
1193
- POST_TOOL_NAME,
1194
- {"text": f"{OVER_SENTENCE_LIMIT_TEXT}\n{OVER_WORD_LIMIT_SENTENCE}\n{OVER_WORD_LIMIT_SENTENCE}\nIt failed — again."},
1195
- )
1196
-
1197
- assert exit_code == reply_length_gate.BLOCK_EXIT_CODE
1198
- all_numbered_lines = [each_line for each_line in error_text.splitlines() if each_line[:1].isdigit()]
1199
- assert [each_line.split(". ", 1)[0] for each_line in all_numbered_lines] == ["1", "2", "3", "4", "5"]
1200
- assert not any(each_line.split(". ", 1)[1][:1].isdigit() for each_line in all_numbered_lines)
1201
- assert f"Reply too long: {MAXIMUM_SENTENCE_COUNT + 4} sentences" in all_numbered_lines[0]
1202
- assert all_numbered_lines[1].count(f"{MAXIMUM_WORDS_PER_SENTENCE + 1} words") == 1
1203
- assert all_numbered_lines[2].count(f"{MAXIMUM_WORDS_PER_SENTENCE + 1} words") == 1
1204
- assert reply_length_gate.EM_DASH_MESSAGE in all_numbered_lines[3]
1205
-
1206
-
1207
- RESULTS_TABLE_REPLY = (
1208
- "| Check | Result |\n"
1209
- "|---|---|\n"
1210
- "| Unit tests | 65 passed |\n"
1211
- )
1212
- RESULTS_LINE_REPLY = "The run shows 65 passed, 0 failed, and 3 skipped."
1213
- RESULTS_PICTURE_DENIAL = "reports results with no picture attached"
1214
- CHART_ATTACHMENT = {"kind": "file", "ref": "/mnt/project-files/tests/chart.svg"}
1215
-
1216
-
1217
- def test_should_deny_a_results_table_with_no_picture_and_name_the_picture_rule(
1218
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1219
- ) -> None:
1220
- exit_code, error_text = run_gate(
1221
- monkeypatch, capsys, POST_TOOL_NAME, {"text": RESULTS_TABLE_REPLY}
1222
- )
1223
- assert exit_code == reply_length_gate.BLOCK_EXIT_CODE
1224
- assert RESULTS_PICTURE_DENIAL in error_text
1225
-
1226
-
1227
- def test_should_deny_three_figures_with_no_picture_and_name_the_picture_rule(
1228
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1229
- ) -> None:
1230
- exit_code, error_text = run_gate(
1231
- monkeypatch, capsys, POST_TOOL_NAME, {"text": RESULTS_LINE_REPLY}
1232
- )
1233
- assert exit_code == reply_length_gate.BLOCK_EXIT_CODE
1234
- assert RESULTS_PICTURE_DENIAL in error_text
1235
-
1236
-
1237
- def test_should_allow_the_same_results_with_a_chart_attached(
1238
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1239
- ) -> None:
1240
- exit_code, error_text = run_gate(
1241
- monkeypatch,
1242
- capsys,
1243
- POST_TOOL_NAME,
1244
- {"text": RESULTS_LINE_REPLY, "attached_outputs": [CHART_ATTACHMENT]},
1245
- )
1246
- assert (exit_code, error_text) == (0, "")
1247
-
1248
-
1249
- def test_should_allow_the_same_results_with_a_markdown_picture(
1250
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1251
- ) -> None:
1252
- exit_code, error_text = run_gate(
1253
- monkeypatch,
1254
- capsys,
1255
- POST_TOOL_NAME,
1256
- {"text": f"{RESULTS_LINE_REPLY}\n\n![Results chart](/mnt/project-files/tests/chart.png)"},
1257
- )
1258
- assert (exit_code, error_text) == (0, "")
1259
-
1260
-
1261
- def test_should_allow_an_edit_of_a_results_message_because_the_edit_tool_attaches_nothing(
1262
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1263
- ) -> None:
1264
- exit_code, error_text = run_gate(
1265
- monkeypatch, capsys, UPDATE_TOOL_NAME, {"text": RESULTS_LINE_REPLY}
1266
- )
1267
- assert (exit_code, error_text) == (0, "")
1268
-
1269
-
1270
- def test_should_allow_results_with_a_published_page_attached(
1271
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1272
- ) -> None:
1273
- exit_code, error_text = run_gate(
1274
- monkeypatch,
1275
- capsys,
1276
- POST_TOOL_NAME,
1277
- {
1278
- "text": RESULTS_LINE_REPLY,
1279
- "attached_outputs": [{"kind": "frame", "ref": "https://claude.ai/artifact/dashboard"}],
1280
- },
1281
- )
1282
- assert (exit_code, error_text) == (0, "")
1283
-
1284
-
1285
- def test_should_deny_results_whose_only_attachment_is_not_a_picture(
1286
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1287
- ) -> None:
1288
- exit_code, error_text = run_gate(
1289
- monkeypatch,
1290
- capsys,
1291
- POST_TOOL_NAME,
1292
- {
1293
- "text": RESULTS_LINE_REPLY,
1294
- "attached_outputs": [{"kind": "file", "ref": "/mnt/project-files/tests/results.md"}],
1295
- },
1296
- )
1297
- assert exit_code == reply_length_gate.BLOCK_EXIT_CODE
1298
- assert RESULTS_PICTURE_DENIAL in error_text
1299
-
1300
-
1301
- def test_should_allow_two_figures_with_no_picture(
1302
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1303
- ) -> None:
1304
- exit_code, error_text = run_gate(
1305
- monkeypatch, capsys, POST_TOOL_NAME, {"text": "The run shows 65 passed and 3 skipped."}
1306
- )
1307
- assert (exit_code, error_text) == (0, "")
1308
-
1309
-
1310
- def test_should_not_count_figures_in_code_spans_or_pull_request_links(
1311
- monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
1312
- ) -> None:
1313
- exit_code, error_text = run_gate(
1314
- monkeypatch,
1315
- capsys,
1316
- POST_TOOL_NAME,
1317
- {
1318
- "text": (
1319
- "Ran `pytest -k 1` and `pytest -k 2` and `pytest -k 3`, "
1320
- "see [pull request](https://github.com/jl-cmd-projects/claude-dev-env/pull/2224)."
1321
- )
1322
- },
1323
- )
1324
- assert (exit_code, error_text) == (0, "")