claude-dev-env 8.115.0 → 8.116.0
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/docs/rule-guides/verified-work.md +1 -1
- package/hooks/advisory/commit_and_pull_request_words.py +346 -0
- package/hooks/advisory/piece_clauses.py +248 -0
- package/hooks/advisory/pieces_per_level_analysis.py +192 -0
- package/hooks/advisory/pieces_per_level_note.py +265 -0
- package/hooks/advisory/test_commit_and_pull_request_words.py +126 -0
- package/hooks/advisory/test_piece_clauses.py +183 -0
- package/hooks/advisory/test_pieces_per_level_note.py +389 -0
- package/hooks/advisory/test_pieces_per_level_note_bounds.py +215 -0
- package/hooks/advisory/test_pieces_per_level_note_deadline.py +282 -0
- package/hooks/features/README.md +1 -1
- package/hooks/features/session-context.md +4 -1
- package/hooks/hooks.json +10 -0
- package/hooks/hooks_constants/pieces_per_level_note_constants.py +255 -0
- package/hooks/hooks_constants/shell_command_wrappers.py +20 -8
- package/hooks/hooks_constants/test_shell_command_wrappers.py +7 -0
- package/package.json +1 -1
- package/scripts/tests/test_verified_work_rule.py +13 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""Labelled corpus for the signals that show an author's words naming several pieces.
|
|
2
|
+
|
|
3
|
+
Each case is an author's own subject, title, or body and the pieces a reader
|
|
4
|
+
would count in it. A single behaviour with a compound object or an alternative
|
|
5
|
+
names one piece.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import sys
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import pytest
|
|
14
|
+
|
|
15
|
+
advisory_directory = str(Path(__file__).resolve().parent)
|
|
16
|
+
if advisory_directory not in sys.path:
|
|
17
|
+
sys.path.insert(0, advisory_directory)
|
|
18
|
+
|
|
19
|
+
import piece_clauses
|
|
20
|
+
|
|
21
|
+
MIXED_SUBJECT = (
|
|
22
|
+
"fix(convert): always publish drawn icons, put Tips on the tray, stop the label smear"
|
|
23
|
+
)
|
|
24
|
+
MIXED_TITLE = "fix(convert): always publish, put Tips on the tray, draw a plain home label"
|
|
25
|
+
|
|
26
|
+
ALL_SINGLE_BEHAVIOUR_SUBJECTS = (
|
|
27
|
+
"fix(lock): darken or lighten the lock video behind lock elements",
|
|
28
|
+
"perf(themes): copy queued themes on a bounded thread pool",
|
|
29
|
+
"feat(evals): add theme metadata evals with frozen dataset and rerun commands",
|
|
30
|
+
"fix(convert): handle empty input",
|
|
31
|
+
"fix(hooks): stop the crash when the file is missing, empty or locked",
|
|
32
|
+
"docs(readme): explain the install, update and removal steps",
|
|
33
|
+
"chore: release 1.2.3",
|
|
34
|
+
"Merge branch 'main' into feature, and keep going",
|
|
35
|
+
"fix: handle the (a, b) pair",
|
|
36
|
+
"fix: show the `a, b` label",
|
|
37
|
+
"feat(installer): install and update the Pele npm package (#2213)",
|
|
38
|
+
"feat(io): add read and write helpers",
|
|
39
|
+
"fix(ui): align start and stop buttons",
|
|
40
|
+
"feat(io): add read, write and move helpers",
|
|
41
|
+
"fix(ui): show start and end dates",
|
|
42
|
+
"feat(x): add log and cache layers",
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _three_pair_body() -> str:
|
|
47
|
+
return "\n".join(
|
|
48
|
+
[
|
|
49
|
+
"## What this does",
|
|
50
|
+
"Publishes icons.",
|
|
51
|
+
"",
|
|
52
|
+
"## Proof in practice",
|
|
53
|
+
"### Publish",
|
|
54
|
+
"- Without the change: `run publish` printed `skipped`.",
|
|
55
|
+
"- With the change: `run publish` printed `published`.",
|
|
56
|
+
"### Tray",
|
|
57
|
+
"Without the change: `run tray` showed no Tips.",
|
|
58
|
+
"With the change: `run tray` showed Tips.",
|
|
59
|
+
"### Label",
|
|
60
|
+
"**Without it** the label smeared.",
|
|
61
|
+
"**With it** the label drew plain.",
|
|
62
|
+
"",
|
|
63
|
+
"## Risk",
|
|
64
|
+
"Low.",
|
|
65
|
+
]
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_a_mixed_subject_names_each_verb_led_clause() -> None:
|
|
70
|
+
all_pieces = piece_clauses.subject_pieces(MIXED_SUBJECT)
|
|
71
|
+
assert all_pieces == [
|
|
72
|
+
"always publish drawn icons",
|
|
73
|
+
"put Tips on the tray",
|
|
74
|
+
"stop the label smear",
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_a_mixed_title_names_each_verb_led_clause() -> None:
|
|
79
|
+
all_pieces = piece_clauses.subject_pieces(MIXED_TITLE)
|
|
80
|
+
assert all_pieces == [
|
|
81
|
+
"always publish",
|
|
82
|
+
"put Tips on the tray",
|
|
83
|
+
"draw a plain home label",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@pytest.mark.parametrize("subject", ALL_SINGLE_BEHAVIOUR_SUBJECTS)
|
|
88
|
+
def test_a_single_behaviour_subject_names_no_set_of_pieces(subject: str) -> None:
|
|
89
|
+
assert piece_clauses.subject_pieces(subject) == []
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@pytest.mark.parametrize(
|
|
93
|
+
"subject",
|
|
94
|
+
[
|
|
95
|
+
"fix(x): publish icons; draw the label",
|
|
96
|
+
"fix(x): publish icons and draw the label",
|
|
97
|
+
"fix(x): publish icons, and draw the label",
|
|
98
|
+
"feat!: add the flag, remove the old flag",
|
|
99
|
+
"fix(x): add retry to the uploader and log each failure",
|
|
100
|
+
],
|
|
101
|
+
)
|
|
102
|
+
def test_a_subject_that_joins_verb_led_clauses_names_two_pieces(subject: str) -> None:
|
|
103
|
+
assert len(piece_clauses.subject_pieces(subject)) == 2
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_a_subject_without_a_conventional_prefix_names_no_pieces() -> None:
|
|
107
|
+
assert piece_clauses.subject_pieces("publish icons, draw the label") == []
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_an_empty_subject_names_no_pieces() -> None:
|
|
111
|
+
assert piece_clauses.subject_pieces("") == []
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_a_body_with_three_proof_pairs_names_the_count() -> None:
|
|
115
|
+
assert piece_clauses.proof_pair_count(_three_pair_body()) == 3
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_a_body_with_one_proof_pair_counts_one() -> None:
|
|
119
|
+
body = "## Proof in practice\nWithout the change: `a` failed.\nWith the change: `a` passed.\n"
|
|
120
|
+
assert piece_clauses.proof_pair_count(body) == 1
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_a_one_line_pair_counts_once() -> None:
|
|
124
|
+
body = (
|
|
125
|
+
"## Proof in practice\nWithout the change the publish failed; with it the publish passed.\n"
|
|
126
|
+
)
|
|
127
|
+
assert piece_clauses.proof_pair_count(body) == 1
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_pairs_quoted_inside_a_fence_do_not_count() -> None:
|
|
131
|
+
body = "\n".join(
|
|
132
|
+
[
|
|
133
|
+
"## Proof in practice",
|
|
134
|
+
"```",
|
|
135
|
+
"Without the change: x",
|
|
136
|
+
"With the change: y",
|
|
137
|
+
"Without the change: x",
|
|
138
|
+
"With the change: y",
|
|
139
|
+
"```",
|
|
140
|
+
]
|
|
141
|
+
)
|
|
142
|
+
assert piece_clauses.proof_pair_count(body) == 0
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_a_body_with_no_proof_section_counts_no_pairs() -> None:
|
|
146
|
+
assert piece_clauses.proof_pair_count("## What this does\nOne line.\n") == 0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def test_bold_led_bullets_under_what_this_does_name_the_topics() -> None:
|
|
150
|
+
body = "\n".join(
|
|
151
|
+
[
|
|
152
|
+
"## What this does",
|
|
153
|
+
"- **Always publish.** Icons ship.",
|
|
154
|
+
"- **Tips on the tray:** shown.",
|
|
155
|
+
" - **Nested detail** is not a topic.",
|
|
156
|
+
"",
|
|
157
|
+
"## Risk",
|
|
158
|
+
"- **Low** risk.",
|
|
159
|
+
]
|
|
160
|
+
)
|
|
161
|
+
assert piece_clauses.topic_bullets(body) == ["Always publish", "Tips on the tray"]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def test_before_and_after_labels_are_not_topics() -> None:
|
|
165
|
+
body = "## What this does\n- **Before:** a\n- **After:** b\n"
|
|
166
|
+
assert piece_clauses.topic_bullets(body) == []
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_a_body_with_six_topics_names_six() -> None:
|
|
170
|
+
all_lines = ["## What this does"] + [
|
|
171
|
+
f"- **Topic {each_number}.** text" for each_number in range(6)
|
|
172
|
+
]
|
|
173
|
+
assert len(piece_clauses.topic_bullets("\n".join(all_lines))) == 6
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def test_bullets_inside_a_fence_are_not_topics() -> None:
|
|
177
|
+
body = "## What this does\n```\n- **A** x\n- **B** y\n```\n"
|
|
178
|
+
assert piece_clauses.topic_bullets(body) == []
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_a_trailing_issue_reference_stays_out_of_the_last_piece() -> None:
|
|
182
|
+
all_pieces = piece_clauses.subject_pieces("docs: link orphan pages and drop repeated sentences (#2223)")
|
|
183
|
+
assert all_pieces == ["link orphan pages", "drop repeated sentences"]
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
"""Behavior tests for the pieces-per-level note.
|
|
2
|
+
|
|
3
|
+
Every case runs the script the way the harness does: one JSON payload on
|
|
4
|
+
stdin, then a read of the exit code, stdout, and stderr. Each case must exit 0
|
|
5
|
+
with an empty stderr and no permission decision.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import subprocess
|
|
13
|
+
import sys
|
|
14
|
+
import time
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
import pytest
|
|
18
|
+
|
|
19
|
+
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
20
|
+
if hooks_root_directory not in sys.path:
|
|
21
|
+
sys.path.insert(0, hooks_root_directory)
|
|
22
|
+
advisory_directory = str(Path(__file__).resolve().parent)
|
|
23
|
+
if advisory_directory not in sys.path:
|
|
24
|
+
sys.path.insert(0, advisory_directory)
|
|
25
|
+
|
|
26
|
+
import pieces_per_level_analysis
|
|
27
|
+
from hooks_constants.pieces_per_level_note_constants import (
|
|
28
|
+
DEADLINE_ENVIRONMENT_VARIABLE,
|
|
29
|
+
MAXIMUM_COMMAND_CHARACTERS,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
SCRIPT_PATH = Path(__file__).resolve().parent / "pieces_per_level_note.py"
|
|
33
|
+
HOOKS_JSON_PATH = Path(__file__).resolve().parent.parent / "hooks.json"
|
|
34
|
+
RUN_TIMEOUT_SECONDS = 30
|
|
35
|
+
TEST_DEADLINE_SECONDS = "60"
|
|
36
|
+
HOSTILE_INPUT_SECONDS_BOUND = 1.0
|
|
37
|
+
HOSTILE_SUBPROCESS_SECONDS_BOUND = 8.0
|
|
38
|
+
|
|
39
|
+
MIXED_SUBJECT = (
|
|
40
|
+
"fix(convert): always publish drawn icons, put Tips on the tray, stop the label smear"
|
|
41
|
+
)
|
|
42
|
+
MIXED_TITLE = "fix(convert): always publish, put Tips on the tray, draw a plain home label"
|
|
43
|
+
ALL_SINGLE_BEHAVIOUR_SUBJECTS = (
|
|
44
|
+
"fix(lock): darken or lighten the lock video behind lock elements",
|
|
45
|
+
"perf(themes): copy queued themes on a bounded thread pool",
|
|
46
|
+
"feat(evals): add theme metadata evals with frozen dataset and rerun commands",
|
|
47
|
+
)
|
|
48
|
+
THREE_PAIR_BODY = "\n".join(
|
|
49
|
+
[
|
|
50
|
+
"## What this does",
|
|
51
|
+
"Publishes icons.",
|
|
52
|
+
"",
|
|
53
|
+
"## Proof in practice",
|
|
54
|
+
"Without the change: `run publish` printed `skipped`.",
|
|
55
|
+
"With the change: `run publish` printed `published`.",
|
|
56
|
+
"Without the change: `run tray` showed no Tips.",
|
|
57
|
+
"With the change: `run tray` showed Tips.",
|
|
58
|
+
"Without the change: `run label` smeared.",
|
|
59
|
+
"With the change: `run label` drew plain.",
|
|
60
|
+
]
|
|
61
|
+
)
|
|
62
|
+
SIX_TOPIC_BODY = "\n".join(
|
|
63
|
+
["## What this does"] + [f"- **Topic {each_number}.** text" for each_number in range(6)]
|
|
64
|
+
)
|
|
65
|
+
ONE_PAIR_BODY = "\n".join(
|
|
66
|
+
[
|
|
67
|
+
"## What this does",
|
|
68
|
+
"- **One topic.** text",
|
|
69
|
+
"",
|
|
70
|
+
"## Proof in practice",
|
|
71
|
+
"Without the change: `run a` failed.",
|
|
72
|
+
"With the change: `run a` passed.",
|
|
73
|
+
]
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _run(stdin_text: str) -> subprocess.CompletedProcess[str]:
|
|
78
|
+
return subprocess.run(
|
|
79
|
+
[sys.executable, str(SCRIPT_PATH)],
|
|
80
|
+
input=stdin_text,
|
|
81
|
+
capture_output=True,
|
|
82
|
+
text=True,
|
|
83
|
+
timeout=RUN_TIMEOUT_SECONDS,
|
|
84
|
+
check=False,
|
|
85
|
+
env={**os.environ, DEADLINE_ENVIRONMENT_VARIABLE: TEST_DEADLINE_SECONDS},
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _shell_payload(command: str, working_directory: str = "") -> str:
|
|
90
|
+
all_fields: dict[str, object] = {"tool_name": "Bash", "tool_input": {"command": command}}
|
|
91
|
+
if working_directory:
|
|
92
|
+
all_fields["cwd"] = working_directory
|
|
93
|
+
return json.dumps(all_fields)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _mcp_payload(title: str, body: str) -> str:
|
|
97
|
+
return json.dumps(
|
|
98
|
+
{
|
|
99
|
+
"tool_name": "mcp__github__create_pull_request",
|
|
100
|
+
"tool_input": {"owner": "acme", "repo": "widgets", "title": title, "body": body},
|
|
101
|
+
}
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _context(completed: subprocess.CompletedProcess[str]) -> str:
|
|
106
|
+
assert completed.returncode == 0
|
|
107
|
+
assert completed.stderr == ""
|
|
108
|
+
all_fields = json.loads(completed.stdout)
|
|
109
|
+
assert list(all_fields) == ["hookSpecificOutput"]
|
|
110
|
+
all_specific = all_fields["hookSpecificOutput"]
|
|
111
|
+
assert set(all_specific) == {"hookEventName", "additionalContext"}
|
|
112
|
+
assert all_specific["hookEventName"] == "PreToolUse"
|
|
113
|
+
return all_specific["additionalContext"]
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _assert_silent(completed: subprocess.CompletedProcess[str]) -> None:
|
|
117
|
+
assert completed.returncode == 0
|
|
118
|
+
assert completed.stderr == ""
|
|
119
|
+
assert completed.stdout == ""
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def test_the_mixed_commit_subject_gets_a_note_that_names_each_piece() -> None:
|
|
123
|
+
note = _context(_run(_shell_payload(f'git commit -m "{MIXED_SUBJECT}"')))
|
|
124
|
+
assert '"always publish drawn icons"' in note
|
|
125
|
+
assert '"put Tips on the tray"' in note
|
|
126
|
+
assert '"stop the label smear"' in note
|
|
127
|
+
assert "its own commit and its own stack level" in note
|
|
128
|
+
assert "`git reset --soft HEAD~1`" in note
|
|
129
|
+
assert "`gt create`" in note
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def test_a_heredoc_commit_message_gets_the_note() -> None:
|
|
133
|
+
command = (
|
|
134
|
+
f"git add -A && git commit -m \"$(cat <<'EOF'\n{MIXED_SUBJECT}\n\nbody; more\nEOF\n)\""
|
|
135
|
+
)
|
|
136
|
+
assert '"put Tips on the tray"' in _context(_run(_shell_payload(command)))
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@pytest.mark.parametrize(
|
|
140
|
+
"command",
|
|
141
|
+
[
|
|
142
|
+
f'git commit -am "{MIXED_SUBJECT}"',
|
|
143
|
+
f'git commit --message="{MIXED_SUBJECT}"',
|
|
144
|
+
f'git commit --message "{MIXED_SUBJECT}"',
|
|
145
|
+
f"git -C repo commit -m '{MIXED_SUBJECT}' -m 'second paragraph'",
|
|
146
|
+
f"pwsh -Command \"git commit -m '{MIXED_SUBJECT}'\"",
|
|
147
|
+
f'git add x && git commit -m "{MIXED_SUBJECT}" && git push',
|
|
148
|
+
],
|
|
149
|
+
)
|
|
150
|
+
def test_other_commit_shapes_get_the_note(command: str) -> None:
|
|
151
|
+
assert '"stop the label smear"' in _context(_run(_shell_payload(command)))
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def test_a_commit_message_file_resolves_against_the_payload_working_directory(
|
|
155
|
+
tmp_path: Path,
|
|
156
|
+
) -> None:
|
|
157
|
+
(tmp_path / "message.txt").write_text(f"{MIXED_SUBJECT}\n\nbody\n", encoding="utf-8")
|
|
158
|
+
note = _context(_run(_shell_payload("git commit -F message.txt", str(tmp_path))))
|
|
159
|
+
assert '"always publish drawn icons"' in note
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_a_commit_message_file_named_by_a_long_option_gets_the_note(tmp_path: Path) -> None:
|
|
163
|
+
(tmp_path / "message.txt").write_text(f"{MIXED_SUBJECT}\n", encoding="utf-8")
|
|
164
|
+
note = _context(_run(_shell_payload("git commit --file=message.txt", str(tmp_path))))
|
|
165
|
+
assert '"put Tips on the tray"' in note
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def test_the_first_message_is_the_subject_and_later_ones_are_body_paragraphs() -> None:
|
|
169
|
+
command = f'git commit -m "fix(x): one behaviour" -m "{MIXED_SUBJECT}"'
|
|
170
|
+
_assert_silent(_run(_shell_payload(command)))
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@pytest.mark.parametrize("subject", ALL_SINGLE_BEHAVIOUR_SUBJECTS)
|
|
174
|
+
def test_a_single_behaviour_commit_subject_is_silent(subject: str) -> None:
|
|
175
|
+
_assert_silent(_run(_shell_payload(f'git commit -m "{subject}"')))
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@pytest.mark.parametrize(
|
|
179
|
+
"command",
|
|
180
|
+
[
|
|
181
|
+
"git commit --amend",
|
|
182
|
+
"git commit -F -",
|
|
183
|
+
"git commit -F missing-file.txt",
|
|
184
|
+
f"git commit -m '{MIXED_SUBJECT}",
|
|
185
|
+
f'echo "{MIXED_SUBJECT}"',
|
|
186
|
+
"git status",
|
|
187
|
+
"gh pr view 12",
|
|
188
|
+
"gh pr create --fill",
|
|
189
|
+
"",
|
|
190
|
+
],
|
|
191
|
+
)
|
|
192
|
+
def test_a_command_with_no_readable_several_piece_message_is_silent(command: str) -> None:
|
|
193
|
+
_assert_silent(_run(_shell_payload(command)))
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def test_the_mixed_pull_request_title_gets_a_note_from_gh(tmp_path: Path) -> None:
|
|
197
|
+
(tmp_path / "body.md").write_text("## What this does\nOne.\n", encoding="utf-8")
|
|
198
|
+
command = f'gh pr create --title "{MIXED_TITLE}" --body-file body.md'
|
|
199
|
+
note = _context(_run(_shell_payload(command, str(tmp_path))))
|
|
200
|
+
assert "this pull request" in note
|
|
201
|
+
assert '"draw a plain home label"' in note
|
|
202
|
+
assert "`gh pr create --base <previous-branch>`" in note
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_the_pull_request_script_create_call_gets_a_note(tmp_path: Path) -> None:
|
|
206
|
+
(tmp_path / "body.md").write_text("## What this does\nOne.\n", encoding="utf-8")
|
|
207
|
+
command = (
|
|
208
|
+
f"python ~/.agents/skills/pull-request/scripts/pull_request.py create --repo a/b "
|
|
209
|
+
f'--base main --head h --title "{MIXED_TITLE}" --body-file body.md'
|
|
210
|
+
)
|
|
211
|
+
assert '"put Tips on the tray"' in _context(_run(_shell_payload(command, str(tmp_path))))
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def test_a_body_with_three_proof_pairs_gets_a_note_even_with_a_single_title(tmp_path: Path) -> None:
|
|
215
|
+
(tmp_path / "body.md").write_text(THREE_PAIR_BODY, encoding="utf-8")
|
|
216
|
+
command = 'gh pr create --title "fix(convert): publish icons" --body-file body.md'
|
|
217
|
+
note = _context(_run(_shell_payload(command, str(tmp_path))))
|
|
218
|
+
assert "3 without/with proof pairs under Proof in practice" in note
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def test_the_mcp_create_call_with_the_mixed_title_and_three_pairs_gets_a_note() -> None:
|
|
222
|
+
note = _context(_run(_mcp_payload(MIXED_TITLE, THREE_PAIR_BODY)))
|
|
223
|
+
assert '"always publish"' in note
|
|
224
|
+
assert "3 without/with proof pairs" in note
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def test_six_bold_led_topics_are_named_up_to_the_cap() -> None:
|
|
228
|
+
note = _context(_run(_mcp_payload("fix(convert): publish icons", SIX_TOPIC_BODY)))
|
|
229
|
+
assert '"Topic 0"' in note
|
|
230
|
+
assert '"Topic 5"' in note
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def test_one_topic_and_one_proof_pair_stay_silent() -> None:
|
|
234
|
+
_assert_silent(_run(_mcp_payload("fix(convert): publish icons", ONE_PAIR_BODY)))
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
@pytest.mark.parametrize("subject", ALL_SINGLE_BEHAVIOUR_SUBJECTS)
|
|
238
|
+
def test_a_single_behaviour_pull_request_title_is_silent(subject: str) -> None:
|
|
239
|
+
_assert_silent(_run(_mcp_payload(subject, ONE_PAIR_BODY)))
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_an_unreadable_body_file_leaves_the_title_signal_alone(tmp_path: Path) -> None:
|
|
243
|
+
command = f'gh pr create --title "{MIXED_TITLE}" --body-file missing.md'
|
|
244
|
+
assert '"put Tips on the tray"' in _context(_run(_shell_payload(command, str(tmp_path))))
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def test_an_unreadable_body_file_with_a_single_title_is_silent(tmp_path: Path) -> None:
|
|
248
|
+
command = 'gh pr create --title "fix(convert): publish icons" --body-file missing.md'
|
|
249
|
+
_assert_silent(_run(_shell_payload(command, str(tmp_path))))
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def test_a_note_over_the_piece_cap_counts_the_rest() -> None:
|
|
253
|
+
all_lines = ["## What this does"] + [
|
|
254
|
+
f"- **Topic {each_number}.** text" for each_number in range(9)
|
|
255
|
+
]
|
|
256
|
+
note = _context(_run(_mcp_payload("fix(x): publish icons", "\n".join(all_lines))))
|
|
257
|
+
assert "and 3 more" in note
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
@pytest.mark.parametrize(
|
|
261
|
+
"stdin_text",
|
|
262
|
+
[
|
|
263
|
+
"",
|
|
264
|
+
"not json",
|
|
265
|
+
"[1, 2]",
|
|
266
|
+
"null",
|
|
267
|
+
'{"tool_name": "Bash"}',
|
|
268
|
+
'{"tool_name": "Bash", "tool_input": "git commit"}',
|
|
269
|
+
'{"tool_name": "Bash", "tool_input": {"command": 7}}',
|
|
270
|
+
'{"tool_name": 7, "tool_input": {"command": "git commit -m x"}}',
|
|
271
|
+
'{"tool_name": "mcp__github__create_pull_request", "tool_input": {"title": 7, "body": []}}',
|
|
272
|
+
'{"tool_name": "Read", "tool_input": {"file_path": "a"}}',
|
|
273
|
+
'{"tool_name": "Bash", "tool_input": {"command": "git commit -m \\"unterminated"}}',
|
|
274
|
+
"{broken",
|
|
275
|
+
],
|
|
276
|
+
)
|
|
277
|
+
def test_a_malformed_or_unrelated_payload_is_silent(stdin_text: str) -> None:
|
|
278
|
+
_assert_silent(_run(stdin_text))
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def test_the_script_has_no_path_that_names_a_refusal() -> None:
|
|
282
|
+
all_source_texts = [
|
|
283
|
+
SCRIPT_PATH.read_text(encoding="utf-8"),
|
|
284
|
+
(SCRIPT_PATH.parent / "piece_clauses.py").read_text(encoding="utf-8"),
|
|
285
|
+
(
|
|
286
|
+
SCRIPT_PATH.parent.parent / "hooks_constants" / "pieces_per_level_note_constants.py"
|
|
287
|
+
).read_text(encoding="utf-8"),
|
|
288
|
+
]
|
|
289
|
+
for each_source in all_source_texts:
|
|
290
|
+
all_code_lines = [
|
|
291
|
+
each_line
|
|
292
|
+
for each_line in each_source.splitlines()
|
|
293
|
+
if not each_line.lstrip().startswith(('"', "#", "-", "Your", "::"))
|
|
294
|
+
]
|
|
295
|
+
code_text = "\n".join(all_code_lines)
|
|
296
|
+
assert "permissionDecision" not in code_text
|
|
297
|
+
assert "PERMISSION_DECISION" not in code_text
|
|
298
|
+
assert '"deny"' not in code_text
|
|
299
|
+
assert '"block"' not in code_text
|
|
300
|
+
assert "sys.exit(2" not in code_text
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def test_the_hook_is_registered_on_the_commit_and_pull_request_tools_as_a_pre_tool_use_note() -> (
|
|
304
|
+
None
|
|
305
|
+
):
|
|
306
|
+
all_registrations = json.loads(HOOKS_JSON_PATH.read_text(encoding="utf-8"))
|
|
307
|
+
all_matching_groups = [
|
|
308
|
+
each_group
|
|
309
|
+
for each_group in all_registrations["hooks"]["PreToolUse"]
|
|
310
|
+
if any(
|
|
311
|
+
"pieces_per_level_note.py" in each_hook.get("command", "") for each_hook in each_group["hooks"]
|
|
312
|
+
)
|
|
313
|
+
]
|
|
314
|
+
assert len(all_matching_groups) == 1
|
|
315
|
+
each_group = all_matching_groups[0]
|
|
316
|
+
assert each_group["matcher"] == "Bash|PowerShell|mcp__.*__create_pull_request"
|
|
317
|
+
assert [each_hook["type"] for each_hook in each_group["hooks"]] == ["command"]
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def test_title_pieces_come_first_and_the_body_topics_are_not_listed_twice() -> None:
|
|
321
|
+
body = "\n".join(
|
|
322
|
+
["## What this does", "- **Always publish.** a", "- **Home label.** b", "", THREE_PAIR_BODY]
|
|
323
|
+
)
|
|
324
|
+
note = _context(_run(_mcp_payload(MIXED_TITLE, body)))
|
|
325
|
+
assert '"always publish"' in note
|
|
326
|
+
assert '"Home label"' not in note
|
|
327
|
+
assert note.index('"draw a plain home label"') < note.index("3 without/with proof pairs")
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def test_a_heredoc_message_that_holds_a_double_quote_still_gets_the_note() -> None:
|
|
331
|
+
command = (
|
|
332
|
+
"git commit -m \"$(cat <<'EOF'\n"
|
|
333
|
+
+ MIXED_SUBJECT
|
|
334
|
+
+ "\n\nHe said \"stop\" and left.\nEOF\n)\""
|
|
335
|
+
)
|
|
336
|
+
assert '"stop the label smear"' in _context(_run(_shell_payload(command)))
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def test_a_heredoc_message_with_a_single_behaviour_subject_is_silent() -> None:
|
|
340
|
+
command = "git commit -m \"$(cat <<'EOF'\nfix(x): handle empty input\n\nHe said \"stop\".\nEOF\n)\""
|
|
341
|
+
_assert_silent(_run(_shell_payload(command)))
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
HOSTILE_BULLET_LINE = "- **Topic** text here\n"
|
|
345
|
+
ALL_HOSTILE_COMMANDS = {
|
|
346
|
+
"repeated commit words": "git commit " * 1000,
|
|
347
|
+
"repeated commit words at the cap": "git commit " * (MAXIMUM_COMMAND_CHARACTERS // 11),
|
|
348
|
+
"repeated heredoc starts": "git commit -m \"$(cat <<'EOF'\n" * 300,
|
|
349
|
+
"message over the cap": 'git commit -m "' + "a" * 200_000 + '"',
|
|
350
|
+
"bullet body over the cap": 'gh pr create --title t --body "'
|
|
351
|
+
+ HOSTILE_BULLET_LINE * 9_000
|
|
352
|
+
+ '"',
|
|
353
|
+
"mixed subject padded past the cap": f'git commit -m "{MIXED_SUBJECT}"'
|
|
354
|
+
+ " " * MAXIMUM_COMMAND_CHARACTERS,
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _decide_seconds(command: str) -> tuple[object, float]:
|
|
359
|
+
all_fields = {"tool_name": "Bash", "tool_input": {"command": command}, "cwd": ""}
|
|
360
|
+
started_at = time.perf_counter()
|
|
361
|
+
output = pieces_per_level_analysis.decide_hook_output(all_fields)
|
|
362
|
+
return output, time.perf_counter() - started_at
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
@pytest.mark.parametrize("command_name", list(ALL_HOSTILE_COMMANDS))
|
|
366
|
+
def test_a_hostile_command_gets_no_note_within_the_time_bound(command_name: str) -> None:
|
|
367
|
+
output, elapsed_seconds = _decide_seconds(ALL_HOSTILE_COMMANDS[command_name])
|
|
368
|
+
assert output is None
|
|
369
|
+
assert elapsed_seconds < HOSTILE_INPUT_SECONDS_BOUND
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
@pytest.mark.parametrize(
|
|
373
|
+
"command_name", ["repeated commit words", "message over the cap", "bullet body over the cap"]
|
|
374
|
+
)
|
|
375
|
+
def test_a_hostile_command_exits_zero_and_silent_inside_the_hook_timeout(
|
|
376
|
+
command_name: str,
|
|
377
|
+
) -> None:
|
|
378
|
+
started_at = time.perf_counter()
|
|
379
|
+
completed = _run(_shell_payload(ALL_HOSTILE_COMMANDS[command_name]))
|
|
380
|
+
elapsed_seconds = time.perf_counter() - started_at
|
|
381
|
+
_assert_silent(completed)
|
|
382
|
+
assert elapsed_seconds < HOSTILE_SUBPROCESS_SECONDS_BOUND
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def test_a_command_just_under_the_cap_still_gets_the_note() -> None:
|
|
386
|
+
command = f'git commit -m "{MIXED_SUBJECT}"'
|
|
387
|
+
command = command + " " * (MAXIMUM_COMMAND_CHARACTERS - len(command))
|
|
388
|
+
output, _ = _decide_seconds(command)
|
|
389
|
+
assert "stop the label smear" in json.dumps(output)
|