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.
- package/bin/install.prompt-context.test.mjs +5 -6
- package/docs/rule-guides/proof-before-pull-request.md +4 -2
- package/hooks/blocking/final_reply_gate.py +7 -29
- package/hooks/blocking/followup_pr_dedupe.py +118 -33
- package/hooks/blocking/gate_run_reads.py +284 -0
- package/hooks/blocking/pr_lifecycle_skill_gate.py +33 -33
- package/hooks/blocking/pull_request_proof.py +60 -63
- package/hooks/blocking/test_final_reply_gate.py +4 -196
- package/hooks/blocking/test_followup_pr_dedupe.py +59 -0
- package/hooks/blocking/test_gate_run_reads.py +241 -0
- package/hooks/blocking/test_pr_lifecycle_skill_gate.py +89 -1
- package/hooks/blocking/test_pull_request_proof.py +63 -2
- package/hooks/features/conduct-gates.md +6 -13
- package/hooks/hooks.json +1 -42
- package/hooks/hooks_constants/background_silence_prompt_constants.py +2 -0
- package/hooks/hooks_constants/gate_run_reads_constants.py +10 -0
- package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +0 -8
- package/hooks/hooks_constants/test_followup_pr_dedupe_constants.py +0 -4
- package/hooks/session/background_silence_prompt.py +1 -1
- package/hooks/test_context_budget_hooks.py +0 -1
- package/package.json +1 -1
- package/scripts/durable_post_lint.py +0 -12
- package/scripts/durable_post_lint_config/config/constants.py +1 -2
- package/scripts/policy_lint/adapter_configuration.py +0 -2
- package/scripts/policy_lint/config/constants.py +0 -2
- package/scripts/test_durable_post_lint.py +0 -53
- package/scripts/tests/test_adapter_configuration.py +0 -16
- package/hooks/blocking/chat_reply_gate.py +0 -260
- package/hooks/blocking/reply_length_gate.py +0 -804
- package/hooks/blocking/test_chat_reply_gate.py +0 -302
- package/hooks/blocking/test_reply_length_gate.py +0 -1324
- package/hooks/hooks_constants/chat_reply_gate_constants.py +0 -60
- package/hooks/hooks_constants/reply_length_gate_constants.py +0 -203
- package/hooks/hooks_constants/test_reply_length_gate_constants.py +0 -81
- package/hooks/hooks_constants/test_visual_reply_rules_constants.py +0 -50
- package/hooks/hooks_constants/visual_reply_rules_constants.py +0 -106
- package/hooks/test_visual_reply_rules.py +0 -362
- package/hooks/visual_reply_rules.py +0 -478
- package/rules/visual-reply-rules.json +0 -56
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
"""Deny pull request lifecycle actions until their skill appears in the transcript.
|
|
1
|
+
"""Deny pull request lifecycle actions until their skill appears in the transcript.
|
|
2
|
+
|
|
3
|
+
A gate silent at its 30 s ``hooks.json`` timeout lets the call through, so its git and GitHub reads end by 21 s.
|
|
4
|
+
``blocking/gate_run_reads.py`` draws that budget. A deny from a run it cut short says a second run may pass.
|
|
5
|
+
"""
|
|
2
6
|
|
|
3
7
|
from __future__ import annotations
|
|
4
8
|
|
|
@@ -75,15 +79,9 @@ from hooks_constants.hook_specific_output_keys import (
|
|
|
75
79
|
from hooks_constants.pre_tool_use_stdin import print_decision_from_stdin
|
|
76
80
|
from hooks_constants.setup_project_paths_constants import DECODE_ERRORS_POLICY, UTF8_ENCODING
|
|
77
81
|
from blocking.followup_pr_dedupe import create_tool_fields, duplicate_followup_reason
|
|
78
|
-
from blocking.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
missing_eval_section_reason,
|
|
82
|
-
missing_existing_work_reason,
|
|
83
|
-
missing_flowchart_reason,
|
|
84
|
-
missing_look_reason,
|
|
85
|
-
missing_proof_reason,
|
|
86
|
-
)
|
|
82
|
+
from blocking.gate_run_reads import gate_run, read_deadlines_started
|
|
83
|
+
from hooks_constants.gate_run_reads_constants import READS_RAN_OUT_SENTENCE
|
|
84
|
+
from blocking import pull_request_proof as proof
|
|
87
85
|
from hooks_constants.pull_request_proof_constants import MISSING_BUILD_EVAL_REASON
|
|
88
86
|
from transcript_skill_scan import (
|
|
89
87
|
invokes_skill_with_argument,
|
|
@@ -383,33 +381,34 @@ def decision_for(all_payload_fields: dict[str, object]) -> dict[str, object] | N
|
|
|
383
381
|
changed page whose proof shows no picture -> MISSING_LOOK_REASON
|
|
384
382
|
feature pull request, no /claude-api build-eval -> MISSING_BUILD_EVAL_REASON
|
|
385
383
|
second follow-up pull request for one parent -> DUPLICATE_FOLLOWUP_REASON_TEMPLATE
|
|
384
|
+
any deny above in a run whose deadline cut a read -> that reason + READS_RAN_OUT_SENTENCE
|
|
385
|
+
|
|
386
|
+
The checks share one gate run, so two checks that ask git or GitHub one question read it once.
|
|
386
387
|
|
|
387
388
|
Args:
|
|
388
389
|
all_payload_fields: The parsed PreToolUse input.
|
|
389
390
|
"""
|
|
390
391
|
if not _is_governed_action(all_payload_fields):
|
|
391
392
|
return None
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
if
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
duplicate_reason = duplicate_followup_reason(all_payload_fields)
|
|
412
|
-
return None if duplicate_reason is None else deny_decision(duplicate_reason)
|
|
393
|
+
with gate_run() as run:
|
|
394
|
+
reason = _first_broken_rule_reason(all_payload_fields)
|
|
395
|
+
note = READS_RAN_OUT_SENTENCE if run.was_read_cut_short else ""
|
|
396
|
+
return None if reason is None else deny_decision(reason + note)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def _first_broken_rule_reason(all_fields: dict[str, object]) -> str | None:
|
|
400
|
+
if _is_skill_unloaded(all_fields):
|
|
401
|
+
return DENY_REASON
|
|
402
|
+
return (
|
|
403
|
+
proof.missing_proof_reason(all_fields)
|
|
404
|
+
or proof.edit_proof_reason(all_fields)
|
|
405
|
+
or proof.missing_existing_work_reason(all_fields)
|
|
406
|
+
or proof.missing_flowchart_reason(all_fields)
|
|
407
|
+
or proof.missing_eval_section_reason(all_fields)
|
|
408
|
+
or proof.missing_look_reason(all_fields)
|
|
409
|
+
or (MISSING_BUILD_EVAL_REASON if proof.is_feature_pull_request(all_fields) and is_build_eval_missing(all_fields) else None)
|
|
410
|
+
or duplicate_followup_reason(all_fields)
|
|
411
|
+
)
|
|
413
412
|
|
|
414
413
|
|
|
415
414
|
def transcript_paths(all_payload_fields: dict[str, object]) -> list[str]:
|
|
@@ -482,8 +481,9 @@ def deny_decision(reason: str) -> dict[str, object]:
|
|
|
482
481
|
|
|
483
482
|
|
|
484
483
|
def main() -> int:
|
|
485
|
-
"""
|
|
486
|
-
|
|
484
|
+
"""Start the read deadlines, read one payload, print a deny when needed, and exit zero."""
|
|
485
|
+
with read_deadlines_started():
|
|
486
|
+
return print_decision_from_stdin(decision_for)
|
|
487
487
|
|
|
488
488
|
|
|
489
489
|
if __name__ == "__main__":
|
|
@@ -4,7 +4,6 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import json
|
|
6
6
|
import re
|
|
7
|
-
import subprocess
|
|
8
7
|
import sys
|
|
9
8
|
from collections.abc import Iterator, Mapping
|
|
10
9
|
from typing import NamedTuple
|
|
@@ -15,8 +14,15 @@ if hooks_directory not in sys.path:
|
|
|
15
14
|
sys.path.insert(0, hooks_directory)
|
|
16
15
|
|
|
17
16
|
from blocking.body_flowchart import fenced_line_flags, section_has_flowchart
|
|
18
|
-
from blocking.followup_pr_dedupe import
|
|
19
|
-
|
|
17
|
+
from blocking.followup_pr_dedupe import (
|
|
18
|
+
all_command_segments,
|
|
19
|
+
body_from_arguments,
|
|
20
|
+
create_tool_fields,
|
|
21
|
+
shell_command,
|
|
22
|
+
title_from_arguments,
|
|
23
|
+
)
|
|
24
|
+
from blocking.gate_run_reads import ReadSlice, answer_once, child_stdout
|
|
25
|
+
from hooks_constants.pr_lifecycle_skill_gate_constants import UPDATE_PULL_REQUEST_TOOL_SUFFIX
|
|
20
26
|
from hooks_constants.pull_request_proof_constants import (
|
|
21
27
|
ADDED_FILE_NAMES_QUERY,
|
|
22
28
|
GIT_URL_SUFFIX,
|
|
@@ -102,10 +108,7 @@ from hooks_constants.pytest_invocation import (
|
|
|
102
108
|
)
|
|
103
109
|
from hooks_constants.shell_command_pipeline import pipeline_segments_for_command
|
|
104
110
|
from hooks_constants.shell_command_segments import token_basename
|
|
105
|
-
from hooks_constants.shell_command_wrappers import
|
|
106
|
-
all_wrapped_command_texts,
|
|
107
|
-
segment_program_and_arguments,
|
|
108
|
-
)
|
|
111
|
+
from hooks_constants.shell_command_wrappers import segment_program_and_arguments
|
|
109
112
|
|
|
110
113
|
|
|
111
114
|
class CreateCall(NamedTuple):
|
|
@@ -206,21 +209,17 @@ def _command_arguments(
|
|
|
206
209
|
return _script_word_arguments(program, all_arguments, script_word)
|
|
207
210
|
|
|
208
211
|
|
|
209
|
-
def _shell_command(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
210
|
-
tool_input = all_payload_fields.get("tool_input")
|
|
211
|
-
command = tool_input.get("command") if isinstance(tool_input, dict) else None
|
|
212
|
-
if all_payload_fields.get("tool_name") not in SHELL_TOOL_NAMES or not isinstance(command, str):
|
|
213
|
-
return None
|
|
214
|
-
return command
|
|
215
|
-
|
|
216
|
-
|
|
217
212
|
def _all_argument_lists(command: str, all_gh_words: list[str], script_word: str) -> Iterator[list[str]]:
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
213
|
+
all_seen_arguments: set[tuple[str, ...]] = set()
|
|
214
|
+
for each_segment in all_command_segments(command):
|
|
215
|
+
all_arguments = _command_arguments(each_segment, all_gh_words, script_word)
|
|
216
|
+
if all_arguments is None:
|
|
217
|
+
continue
|
|
218
|
+
all_unquoted_arguments = tuple(_unquoted_arguments(all_arguments))
|
|
219
|
+
if all_unquoted_arguments in all_seen_arguments:
|
|
220
|
+
continue
|
|
221
|
+
all_seen_arguments.add(all_unquoted_arguments)
|
|
222
|
+
yield all_arguments
|
|
224
223
|
|
|
225
224
|
|
|
226
225
|
def _all_create_argument_lists(command: str) -> Iterator[list[str]]:
|
|
@@ -253,7 +252,7 @@ def _flag_value(all_create_arguments: list[str], all_flags: frozenset[str], assi
|
|
|
253
252
|
def _shell_flag_value(
|
|
254
253
|
all_payload_fields: Mapping[str, object], all_flags: frozenset[str], assignment_prefix: str
|
|
255
254
|
) -> str | None:
|
|
256
|
-
command =
|
|
255
|
+
command = shell_command(all_payload_fields)
|
|
257
256
|
if command is None:
|
|
258
257
|
return None
|
|
259
258
|
all_flag_values = [
|
|
@@ -303,7 +302,7 @@ def _create_calls(all_payload_fields: Mapping[str, object]) -> list[CreateCall]:
|
|
|
303
302
|
body if isinstance(body, str) else "",
|
|
304
303
|
)
|
|
305
304
|
]
|
|
306
|
-
command =
|
|
305
|
+
command = shell_command(all_payload_fields)
|
|
307
306
|
if command is None:
|
|
308
307
|
return []
|
|
309
308
|
working_directory = all_payload_fields.get("cwd")
|
|
@@ -324,7 +323,7 @@ def is_create_call(all_payload_fields: Mapping[str, object]) -> bool:
|
|
|
324
323
|
Args:
|
|
325
324
|
all_payload_fields: A tool name and input, shaped like a PreToolUse payload.
|
|
326
325
|
"""
|
|
327
|
-
command =
|
|
326
|
+
command = shell_command(all_payload_fields)
|
|
328
327
|
return create_tool_fields(all_payload_fields) is not None or (
|
|
329
328
|
command is not None and next(_all_create_argument_lists(command), None) is not None
|
|
330
329
|
)
|
|
@@ -650,7 +649,7 @@ def _edit_bodies(all_payload_fields: Mapping[str, object]) -> list[str]:
|
|
|
650
649
|
if isinstance(tool_name, str) and tool_name.endswith(UPDATE_PULL_REQUEST_TOOL_SUFFIX):
|
|
651
650
|
body = tool_input.get("body") if isinstance(tool_input, dict) else None
|
|
652
651
|
return [body] if isinstance(body, str) else []
|
|
653
|
-
command =
|
|
652
|
+
command = shell_command(all_payload_fields)
|
|
654
653
|
if command is None:
|
|
655
654
|
return []
|
|
656
655
|
working_directory = all_payload_fields.get("cwd")
|
|
@@ -736,19 +735,9 @@ class ChangedFile(NamedTuple):
|
|
|
736
735
|
|
|
737
736
|
|
|
738
737
|
def _git_output(working_directory: str, *all_git_arguments: str) -> str | None:
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
capture_output=True,
|
|
743
|
-
text=True,
|
|
744
|
-
encoding="utf-8",
|
|
745
|
-
errors="replace",
|
|
746
|
-
timeout=GIT_TIMEOUT_SECONDS,
|
|
747
|
-
check=False,
|
|
748
|
-
)
|
|
749
|
-
except (OSError, subprocess.SubprocessError):
|
|
750
|
-
return None
|
|
751
|
-
return completed.stdout if completed.returncode == 0 else None
|
|
738
|
+
return child_stdout(
|
|
739
|
+
["git", "-C", working_directory, *all_git_arguments], GIT_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK
|
|
740
|
+
)
|
|
752
741
|
|
|
753
742
|
|
|
754
743
|
def _git_lines(working_directory: str, *all_git_arguments: str) -> list[str] | None:
|
|
@@ -810,6 +799,14 @@ def visible_changed_files(working_directory: str, head_branch: object) -> list[s
|
|
|
810
799
|
|
|
811
800
|
|
|
812
801
|
def _merge_base_and_head(working_directory: str, head_branch: object) -> tuple[str, str] | None:
|
|
802
|
+
named_head = head_branch if isinstance(head_branch, str) else None
|
|
803
|
+
return answer_once(
|
|
804
|
+
("merge base and head", working_directory, named_head),
|
|
805
|
+
lambda: _read_merge_base_and_head(working_directory, named_head),
|
|
806
|
+
)
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
def _read_merge_base_and_head(working_directory: str, head_branch: str | None) -> tuple[str, str] | None:
|
|
813
810
|
if not working_directory:
|
|
814
811
|
return None
|
|
815
812
|
head_reference = _head_reference(working_directory, head_branch)
|
|
@@ -899,19 +896,8 @@ def added_workflow_files(working_directory: str, head_branch: object) -> list[st
|
|
|
899
896
|
|
|
900
897
|
|
|
901
898
|
def _gh_api_lines(endpoint: str, query: str) -> list[str] | None:
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
["gh", "api", endpoint, "--jq", query],
|
|
905
|
-
capture_output=True,
|
|
906
|
-
text=True,
|
|
907
|
-
encoding="utf-8",
|
|
908
|
-
errors="replace",
|
|
909
|
-
timeout=GH_TIMEOUT_SECONDS,
|
|
910
|
-
check=False,
|
|
911
|
-
)
|
|
912
|
-
except (OSError, subprocess.SubprocessError):
|
|
913
|
-
return None
|
|
914
|
-
return completed.stdout.splitlines() if completed.returncode == 0 else None
|
|
899
|
+
printed_text = child_stdout(["gh", "api", endpoint, "--jq", query], GH_TIMEOUT_SECONDS, ReadSlice.PROOF_AND_LOOK)
|
|
900
|
+
return None if printed_text is None else printed_text.splitlines()
|
|
915
901
|
|
|
916
902
|
|
|
917
903
|
def _compare_base_branch(repository: str, base_branch: str | None) -> str | None:
|
|
@@ -1010,13 +996,21 @@ def _added_workflow_files_for_create(all_payload_fields: Mapping[str, object]) -
|
|
|
1010
996
|
working_directory = str(all_payload_fields.get("cwd") or "")
|
|
1011
997
|
head_branch = _named_head_branch(all_payload_fields)
|
|
1012
998
|
repository = _named_repository(all_payload_fields)
|
|
999
|
+
base_branch = _named_base_branch(all_payload_fields)
|
|
1000
|
+
return answer_once(
|
|
1001
|
+
("added workflow files", working_directory, repository, base_branch, head_branch),
|
|
1002
|
+
lambda: _read_added_workflow_files(working_directory, repository, base_branch, head_branch),
|
|
1003
|
+
)
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
def _read_added_workflow_files(
|
|
1007
|
+
working_directory: str, repository: str | None, base_branch: str | None, head_branch: str | None
|
|
1008
|
+
) -> list[str]:
|
|
1013
1009
|
all_workflow_paths = None
|
|
1014
1010
|
if _local_checkout_may_answer(working_directory, repository, head_branch):
|
|
1015
1011
|
all_workflow_paths = added_workflow_files(working_directory, head_branch)
|
|
1016
1012
|
if all_workflow_paths is None:
|
|
1017
|
-
all_workflow_paths = remote_added_workflow_files(
|
|
1018
|
-
repository, _named_base_branch(all_payload_fields), head_branch
|
|
1019
|
-
)
|
|
1013
|
+
all_workflow_paths = remote_added_workflow_files(repository, base_branch, head_branch)
|
|
1020
1014
|
return all_workflow_paths or []
|
|
1021
1015
|
|
|
1022
1016
|
|
|
@@ -1131,11 +1125,18 @@ def missing_look_reason(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
|
1131
1125
|
git cannot read the branch, compare API returns 300 .py files -> UNREADABLE_CHANGES_REASON
|
|
1132
1126
|
neither git nor the compare API can read it, no picture -> UNREADABLE_CHANGES_REASON
|
|
1133
1127
|
|
|
1128
|
+
Every body is checked for a picture first. When each one shows a picture,
|
|
1129
|
+
the check passes and reads neither git nor the compare API.
|
|
1130
|
+
|
|
1134
1131
|
Args:
|
|
1135
1132
|
all_payload_fields: The parsed PreToolUse input.
|
|
1136
1133
|
"""
|
|
1137
|
-
|
|
1138
|
-
|
|
1134
|
+
all_bodies_without_picture = [
|
|
1135
|
+
each_body
|
|
1136
|
+
for each_body in _create_bodies(all_payload_fields)
|
|
1137
|
+
if each_body is not None and not re.search(PROOF_IMAGE_PATTERN, proof_section(each_body) or "", re.IGNORECASE)
|
|
1138
|
+
]
|
|
1139
|
+
if not all_bodies_without_picture:
|
|
1139
1140
|
return None
|
|
1140
1141
|
working_directory = all_payload_fields.get("cwd")
|
|
1141
1142
|
head_branch = _named_head_branch(all_payload_fields)
|
|
@@ -1146,10 +1147,6 @@ def missing_look_reason(all_payload_fields: Mapping[str, object]) -> str | None:
|
|
|
1146
1147
|
)
|
|
1147
1148
|
if all_visible_files == []:
|
|
1148
1149
|
return None
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
if all_visible_files is None:
|
|
1153
|
-
return UNREADABLE_CHANGES_REASON
|
|
1154
|
-
return MISSING_LOOK_REASON.format(changed_files=CHANGED_FILES_SEPARATOR.join(all_visible_files))
|
|
1155
|
-
return None
|
|
1150
|
+
if all_visible_files is None:
|
|
1151
|
+
return UNREADABLE_CHANGES_REASON
|
|
1152
|
+
return MISSING_LOOK_REASON.format(changed_files=CHANGED_FILES_SEPARATOR.join(all_visible_files))
|
|
@@ -39,205 +39,13 @@ def run_gate(
|
|
|
39
39
|
def isolated_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
40
40
|
monkeypatch.setenv("HOME", str(tmp_path))
|
|
41
41
|
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
42
|
-
monkeypatch.delenv("CLAUDE_REPLY_BANNED_WORDS_PATH", raising=False)
|
|
43
42
|
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
json.dumps({"type": "user", "message": {"content": [tool_result]}}), encoding="utf-8"
|
|
49
|
-
)
|
|
50
|
-
return transcript_path
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
@pytest.mark.parametrize(
|
|
54
|
-
("reply_text", "quoted_text"),
|
|
55
|
-
[
|
|
56
|
-
(GUESSED_REPLY, 'Banned word "guess"'),
|
|
57
|
-
(UNCONFIRMED_REPLY, 'No tool validated this: "Merge queue: I didn\u2019t confirm this one."'),
|
|
58
|
-
("It should take about 40 minutes.", "It should take about 40 minutes."),
|
|
59
|
-
],
|
|
60
|
-
)
|
|
61
|
-
def test_should_block_a_closing_reply_that_guesses(
|
|
62
|
-
monkeypatch: pytest.MonkeyPatch,
|
|
63
|
-
capsys: pytest.CaptureFixture[str],
|
|
64
|
-
reply_text: str,
|
|
65
|
-
quoted_text: str,
|
|
66
|
-
) -> None:
|
|
67
|
-
exit_code, stdout_text = run_gate(monkeypatch, capsys, reply_text)
|
|
68
|
-
decision = json.loads(stdout_text)
|
|
69
|
-
assert exit_code == 0
|
|
70
|
-
assert decision["decision"] == "block"
|
|
71
|
-
assert quoted_text in decision["reason"]
|
|
72
|
-
assert decision["reason"].endswith("Rewrite the reply without it.")
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
def test_should_block_a_run_no_tool_result_shows(
|
|
76
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
77
|
-
) -> None:
|
|
78
|
-
transcript_path = write_tool_result_transcript(tmp_path / "s.jsonl", "run 18239170000 ok")
|
|
79
|
-
_, stdout_text = run_gate(monkeypatch, capsys, MEASURED_REPLY, transcript_path)
|
|
80
|
-
assert 'cites "run 18239174012"' in json.loads(stdout_text)["reason"]
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def test_should_allow_a_run_a_tool_result_shows(
|
|
84
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
85
|
-
) -> None:
|
|
86
|
-
transcript_path = write_tool_result_transcript(tmp_path / "s.jsonl", "run 18239174012 took 39m")
|
|
87
|
-
assert run_gate(monkeypatch, capsys, MEASURED_REPLY, transcript_path) == (0, "")
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
CITED_PULL_URL = "https://github.com/acme/widgets/pull/8127"
|
|
91
|
-
CITED_PULL_REPLY = f"The control is open as [PR 8127]({CITED_PULL_URL})."
|
|
92
|
-
TASK_NOTIFICATION_ENTRY = {
|
|
93
|
-
"type": "user",
|
|
94
|
-
"message": {
|
|
95
|
-
"role": "user",
|
|
96
|
-
"content": (
|
|
97
|
-
"<task-notification>\n<status>completed</status>\n<result>1. PR "
|
|
98
|
-
f"[#8127]({CITED_PULL_URL}) is open. It is not armed.</result>\n</task-notification>"
|
|
99
|
-
),
|
|
100
|
-
},
|
|
101
|
-
"origin": {"kind": "task-notification", "producer": "session-task"},
|
|
102
|
-
"promptSource": "system",
|
|
103
|
-
}
|
|
104
|
-
OWN_ECHO_FEEDBACK = (
|
|
105
|
-
'Stop hook feedback:\nThe text cites "/pull/8127", and no tool result in this session'
|
|
106
|
-
" shows it. Read it yourself or through a subagent first. Rewrite the reply without it."
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
def write_transcript_entries(transcript_path: Path, all_entries: list[dict[str, object]]) -> Path:
|
|
111
|
-
transcript_path.write_text(
|
|
112
|
-
"\n".join(json.dumps(each_entry) for each_entry in all_entries), encoding="utf-8"
|
|
113
|
-
)
|
|
114
|
-
return transcript_path
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
@pytest.mark.parametrize(
|
|
118
|
-
"hook_entry",
|
|
119
|
-
[
|
|
120
|
-
TASK_NOTIFICATION_ENTRY,
|
|
121
|
-
{
|
|
122
|
-
"type": "user",
|
|
123
|
-
"isMeta": True,
|
|
124
|
-
"message": {
|
|
125
|
-
"role": "user",
|
|
126
|
-
"content": f"Stop hook feedback:\nThese pull requests are still open:\n- {CITED_PULL_URL}",
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
{
|
|
130
|
-
"type": "attachment",
|
|
131
|
-
"attachment": {
|
|
132
|
-
"type": "hook_additional_context",
|
|
133
|
-
"content": [f"You opened acme/widgets#8127 {CITED_PULL_URL}"],
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
"type": "attachment",
|
|
138
|
-
"attachment": {"type": "hook_system_message", "content": f"Watching {CITED_PULL_URL}"},
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
"type": "attachment",
|
|
142
|
-
"attachment": {
|
|
143
|
-
"type": "queued_command",
|
|
144
|
-
"origin": {"kind": "task-notification", "producer": "session-task"},
|
|
145
|
-
"prompt": f"<task-notification><result>Opened {CITED_PULL_URL}</result></task-notification>",
|
|
146
|
-
},
|
|
147
|
-
},
|
|
148
|
-
],
|
|
149
|
-
)
|
|
150
|
-
def test_should_allow_a_pull_request_link_hook_text_shows(
|
|
151
|
-
monkeypatch: pytest.MonkeyPatch,
|
|
152
|
-
capsys: pytest.CaptureFixture[str],
|
|
153
|
-
tmp_path: Path,
|
|
154
|
-
hook_entry: dict[str, object],
|
|
155
|
-
) -> None:
|
|
156
|
-
transcript_path = write_transcript_entries(tmp_path / "s.jsonl", [hook_entry])
|
|
157
|
-
assert run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path) == (0, "")
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
def test_should_block_a_pull_request_link_only_the_gate_feedback_echoes(
|
|
161
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
162
|
-
) -> None:
|
|
163
|
-
feedback_entry = {"type": "user", "isMeta": True, "message": {"role": "user", "content": OWN_ECHO_FEEDBACK}}
|
|
164
|
-
blocking_error_entry = {
|
|
165
|
-
"type": "attachment",
|
|
166
|
-
"attachment": {
|
|
167
|
-
"type": "hook_blocking_error",
|
|
168
|
-
"blockingError": {"blockingError": OWN_ECHO_FEEDBACK.removeprefix("Stop hook feedback:\n")},
|
|
169
|
-
},
|
|
170
|
-
}
|
|
171
|
-
transcript_path = write_transcript_entries(
|
|
172
|
-
tmp_path / "s.jsonl", [feedback_entry, blocking_error_entry]
|
|
173
|
-
)
|
|
174
|
-
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
175
|
-
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
def test_should_block_a_pull_request_link_the_gate_quotes_in_a_sentence(
|
|
179
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
180
|
-
) -> None:
|
|
181
|
-
quoting_feedback = (
|
|
182
|
-
f'Stop hook feedback:\nNo tool validated this: "{CITED_PULL_REPLY}". Run the check yourself'
|
|
183
|
-
" or through a subagent, then state what the run, log line, output, or file and line shows."
|
|
184
|
-
" When you cannot run it this turn, report it as `Open: <check> - <next step>`."
|
|
185
|
-
f'\nSentence too long: 40 words, limit 35: "Opened {CITED_PULL_URL} and...".'
|
|
186
|
-
)
|
|
187
|
-
feedback_entry = {"type": "user", "isMeta": True, "message": {"role": "user", "content": quoting_feedback}}
|
|
188
|
-
transcript_path = write_transcript_entries(tmp_path / "s.jsonl", [feedback_entry])
|
|
189
|
-
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
190
|
-
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
def test_should_block_a_pull_request_link_only_a_typed_queued_prompt_shows(
|
|
194
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
195
|
-
) -> None:
|
|
196
|
-
typed_entry = {
|
|
197
|
-
"type": "attachment",
|
|
198
|
-
"attachment": {"type": "queued_command", "origin": {"kind": "human"}, "prompt": f"Is {CITED_PULL_URL} done?"},
|
|
199
|
-
}
|
|
200
|
-
transcript_path = write_transcript_entries(tmp_path / "s.jsonl", [typed_entry])
|
|
201
|
-
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
202
|
-
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
def test_should_block_a_pull_request_link_no_text_shows(
|
|
206
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
207
|
-
) -> None:
|
|
208
|
-
transcript_path = write_transcript_entries(
|
|
209
|
-
tmp_path / "s.jsonl", [{**TASK_NOTIFICATION_ENTRY, "message": {"role": "user", "content": "PR 8126 opened."}}]
|
|
210
|
-
)
|
|
211
|
-
_, stdout_text = run_gate(monkeypatch, capsys, CITED_PULL_REPLY, transcript_path)
|
|
212
|
-
assert 'cites "/pull/8127"' in json.loads(stdout_text)["reason"]
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
def test_should_block_a_run_only_hook_text_shows(
|
|
216
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
217
|
-
) -> None:
|
|
218
|
-
transcript_path = write_transcript_entries(
|
|
219
|
-
tmp_path / "s.jsonl",
|
|
220
|
-
[{**TASK_NOTIFICATION_ENTRY, "message": {"role": "user", "content": "run 18239174012 took 39m"}}],
|
|
221
|
-
)
|
|
222
|
-
_, stdout_text = run_gate(monkeypatch, capsys, MEASURED_REPLY, transcript_path)
|
|
223
|
-
assert 'cites "run 18239174012"' in json.loads(stdout_text)["reason"]
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
def test_should_steer_a_skipped_check_to_an_open_line(
|
|
227
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
228
|
-
) -> None:
|
|
229
|
-
skipped_check_reply = "I have not checked the pivot, reach, or screen 26 captures yet."
|
|
230
|
-
_, stdout_text = run_gate(monkeypatch, capsys, skipped_check_reply)
|
|
231
|
-
block_reason = json.loads(stdout_text)["reason"]
|
|
232
|
-
assert "`Open: <check> - <next step>`" in block_reason
|
|
233
|
-
assert "leave the claim out" not in block_reason
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
def test_should_allow_an_open_line_for_a_skipped_check(
|
|
237
|
-
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
44
|
+
@pytest.mark.parametrize("reply_text", [GUESSED_REPLY, UNCONFIRMED_REPLY, MEASURED_REPLY])
|
|
45
|
+
def test_should_allow_a_closing_reply_the_removed_claim_checks_once_blocked(
|
|
46
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], reply_text: str
|
|
238
47
|
) -> None:
|
|
239
|
-
|
|
240
|
-
assert run_gate(monkeypatch, capsys, open_line) == (0, "")
|
|
48
|
+
assert run_gate(monkeypatch, capsys, reply_text) == (0, "")
|
|
241
49
|
|
|
242
50
|
|
|
243
51
|
def test_should_allow_a_closing_reply_with_no_message(
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
"""Behavior tests for the one-follow-up-pull-request-per-parent check."""
|
|
2
2
|
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
3
5
|
import sys
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
4
8
|
from collections.abc import Mapping
|
|
9
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
5
10
|
from pathlib import Path
|
|
6
11
|
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
7
14
|
HOOKS_DIRECTORY = Path(__file__).resolve().parent.parent
|
|
8
15
|
if str(HOOKS_DIRECTORY) not in sys.path:
|
|
9
16
|
sys.path.insert(0, str(HOOKS_DIRECTORY))
|
|
@@ -20,6 +27,7 @@ OTHER_FOLLOWUP = {
|
|
|
20
27
|
"html_url": "https://github.com/jl-cmd/claude-dev-env/pull/1777",
|
|
21
28
|
"body": "follow-up to #17",
|
|
22
29
|
}
|
|
30
|
+
ELAPSED_SLACK_SECONDS = 8.0
|
|
23
31
|
|
|
24
32
|
|
|
25
33
|
def _reader(all_open: list[Mapping[str, object]]) -> dedupe.OpenPullRequestReader:
|
|
@@ -94,6 +102,57 @@ def test_body_from_arguments_reads_inline_text_and_body_files(tmp_path: Path) ->
|
|
|
94
102
|
assert dedupe.body_from_arguments(["--fill"], str(tmp_path)) is None
|
|
95
103
|
|
|
96
104
|
|
|
105
|
+
def test_body_from_arguments_should_read_only_a_regular_body_file(tmp_path: Path) -> None:
|
|
106
|
+
assert dedupe.body_from_arguments(["--body-file", os.devnull], str(tmp_path)) is None
|
|
107
|
+
assert dedupe.body_from_arguments(["--body-file", "."], str(tmp_path)) is None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _trickling_server() -> ThreadingHTTPServer:
|
|
111
|
+
class TrickleHandler(BaseHTTPRequestHandler):
|
|
112
|
+
def log_message(self, *all_arguments: object) -> None:
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
def do_GET(self) -> None:
|
|
116
|
+
payload = json.dumps([OPEN_FOLLOWUP]).encode()
|
|
117
|
+
self.send_response(200)
|
|
118
|
+
self.send_header("Content-Length", str(len(payload)))
|
|
119
|
+
self.end_headers()
|
|
120
|
+
for each_byte in payload:
|
|
121
|
+
self.wfile.write(bytes([each_byte]))
|
|
122
|
+
self.wfile.flush()
|
|
123
|
+
time.sleep(0.3)
|
|
124
|
+
|
|
125
|
+
server = ThreadingHTTPServer(("127.0.0.1", 0), TrickleHandler)
|
|
126
|
+
server.daemon_threads = True
|
|
127
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
128
|
+
return server
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_read_open_pull_requests_should_bound_the_whole_read(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
132
|
+
server = _trickling_server()
|
|
133
|
+
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
|
|
134
|
+
monkeypatch.setenv("no_proxy", "127.0.0.1")
|
|
135
|
+
monkeypatch.setattr(dedupe, "GITHUB_API_ROOT", f"http://127.0.0.1:{server.server_address[1]}")
|
|
136
|
+
monkeypatch.setattr(dedupe, "READ_TIMEOUT_SECONDS", 1)
|
|
137
|
+
started = time.monotonic()
|
|
138
|
+
with pytest.raises(TimeoutError):
|
|
139
|
+
dedupe.read_open_pull_requests("jl-cmd", "claude-dev-env")
|
|
140
|
+
assert time.monotonic() - started < 1 + ELAPSED_SLACK_SECONDS
|
|
141
|
+
server.shutdown()
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def test_shell_command_should_return_only_a_shell_tool_command() -> None:
|
|
145
|
+
assert dedupe.shell_command({"tool_name": "PowerShell", "tool_input": {"command": "git push"}}) == "git push"
|
|
146
|
+
assert dedupe.shell_command({"tool_name": "Read", "tool_input": {"command": "git push"}}) is None
|
|
147
|
+
assert dedupe.shell_command({"tool_name": "Bash", "tool_input": {"command": 7}}) is None
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_all_command_segments_should_list_each_segment_and_wrapped_command() -> None:
|
|
151
|
+
all_segments = list(dedupe.all_command_segments("git add x && bash -c 'git push'"))
|
|
152
|
+
assert ["git", "add", "x"] in all_segments
|
|
153
|
+
assert ["git", "push"] in all_segments
|
|
154
|
+
|
|
155
|
+
|
|
97
156
|
def test_title_from_arguments_should_read_each_title_flag_shape() -> None:
|
|
98
157
|
assert dedupe.title_from_arguments(["--title", "feat: x", "--fill"]) == "feat: x"
|
|
99
158
|
assert dedupe.title_from_arguments(["-t", "fix: y"]) == "fix: y"
|