@jesdi/skills 0.0.63 → 0.0.64
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/README.md +15 -0
- package/package.json +2 -2
- package/skills/babysit-pr/SKILL.md +54 -0
- package/skills/babysit-pr/babysit.py +998 -0
- package/skills/babysit-pr/fixtures/gh +310 -0
- package/skills/babysit-pr/hooks/post-pr-create.sh +56 -0
- package/skills/babysit-pr/hooks.json +10 -0
- package/skills/babysit-pr/judge-prompt.md +10 -0
- package/skills/babysit-pr/test_babysit.py +242 -0
- package/skills/babysit-pr/test_babysit_judge.py +286 -0
- package/skills/babysit-pr/test_babysit_judge_diff.py +25 -0
- package/skills/babysit-pr/test_babysit_judge_isolation.py +61 -0
- package/skills/babysit-pr/test_checks_regressions.py +210 -0
- package/skills/babysit-pr/test_comment_fidelity.py +88 -0
- package/skills/babysit-pr/test_comment_identity.py +70 -0
- package/skills/babysit-pr/test_e2e_setup.py +74 -0
- package/skills/babysit-pr/test_handoff_takeover.py +141 -0
- package/skills/babysit-pr/test_handoff_terminal.py +29 -0
- package/skills/babysit-pr/test_hook_quoting.py +25 -0
- package/skills/babysit-pr/test_hooks.py +62 -0
- package/skills/babysit-pr/test_lease_regressions.py +198 -0
- package/skills/babysit-pr/test_merge_and_handoff.py +260 -0
- package/skills/babysit-pr/test_merge_base_pin.py +10 -0
- package/skills/babysit-pr/test_merge_gate.py +124 -0
- package/skills/babysit-pr/test_merge_session_safety.py +117 -0
- package/skills/babysit-pr/test_merged_pr_labels.py +144 -0
- package/skills/babysit-pr/test_owner_comment_edits.py +26 -0
- package/skills/babysit-pr/test_owner_comments.py +316 -0
- package/skills/babysit-pr/test_retry_and_stall_hardening.py +143 -0
- package/skills/babysit-pr/test_rounds_and_checks.py +305 -0
- package/skills/babysit-pr/test_terminal_label_regressions.py +117 -0
- package/skills/babysit-pr/test_trusted_merge_risk.py +184 -0
- package/skills/babysit-pr/test_update_plan.py +157 -0
- package/skills/implement-spec/README.md +1 -1
- package/skills/implement-spec/SKILL.md +1 -1
- package/skills/implement-spec/driver_final.py +49 -10
- package/skills/implement-spec/prompts/pr-writer.md +3 -2
- package/skills/implement-spec/tests/test_final.py +112 -1
- package/skills/implement-spec/tests/test_tidy.py +2 -2
- package/skills/implement-spec/tests/test_walk.py +2 -2
- package/skills/pr-risk/SKILL.md +33 -0
- package/skills/pr-risk/hooks/pre-pr-create.sh +274 -0
- package/skills/pr-risk/hooks.json +10 -0
- package/skills/pr-risk/merge_policy.py +81 -0
- package/skills/pr-risk/merge_risk.py +314 -0
- package/skills/pr-risk/test_hook_commands.py +47 -0
- package/skills/pr-risk/test_hook_heredoc.py +16 -0
- package/skills/pr-risk/test_hook_substitution.py +40 -0
- package/skills/pr-risk/test_hooks.py +175 -0
- package/skills/pr-risk/test_merge_risk.py +97 -0
- package/skills/pr-risk/test_merge_risk_gate.py +217 -0
- package/skills/pr-risk/test_merge_risk_regressions.py +37 -0
- package/skills/pr-risk/test_merge_risk_structure.py +101 -0
- package/skills-manifest.json +15 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Regression coverage for PR command recognition and forbidden creation modes."""
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from test_hooks import VALID_BODY, assert_allow, assert_deny, make_repo, run_hook
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.mark.parametrize("agent", ["claude", "codex"])
|
|
8
|
+
@pytest.mark.parametrize("command", [
|
|
9
|
+
"if true; then gh pr create --fill; fi",
|
|
10
|
+
"if gh pr create --fill; then true; fi",
|
|
11
|
+
"if false; then true; else gh pr create --fill; fi",
|
|
12
|
+
"while false; do gh pr create --fill; done",
|
|
13
|
+
"gh --repo owner/repo pr create --fill",
|
|
14
|
+
"gh --repo=owner/repo pr create --fill",
|
|
15
|
+
"gh -R owner/repo pr create --fill",
|
|
16
|
+
"gh -Rowner/repo pr create --fill",
|
|
17
|
+
"gh pr --repo owner/repo create --fill",
|
|
18
|
+
"gh pr create --web --body-file body.md",
|
|
19
|
+
"gh pr create --fill --body-file body.md",
|
|
20
|
+
"gh pr create --fill-first --body-file body.md",
|
|
21
|
+
])
|
|
22
|
+
def test_creation_bypasses_are_denied_only_with_policy(tmp_path, agent, command):
|
|
23
|
+
repo = make_repo(tmp_path)
|
|
24
|
+
(repo / "body.md").write_text(VALID_BODY)
|
|
25
|
+
payload = {"tool_name": "Bash", "tool_input": {"command": command}, "cwd": str(repo)}
|
|
26
|
+
if agent == "claude":
|
|
27
|
+
payload["hook_event_name"] = "PreToolUse"
|
|
28
|
+
assert_deny(run_hook(payload), "the body must come from --body or --body-file",
|
|
29
|
+
"/pr, then /pr-risk")
|
|
30
|
+
(repo / ".merge-risk.json").unlink()
|
|
31
|
+
proc = run_hook(payload)
|
|
32
|
+
assert_allow(proc)
|
|
33
|
+
assert proc.stdout == ""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@pytest.mark.parametrize("command", [
|
|
37
|
+
"if true; then gh pr create --body-file body.md; fi",
|
|
38
|
+
"gh --repo owner/repo pr create --body-file body.md",
|
|
39
|
+
"gh pr create --title --web --body-file body.md",
|
|
40
|
+
"echo gh --repo owner/repo pr create --fill",
|
|
41
|
+
"gh --repo owner/repo pr view 12",
|
|
42
|
+
])
|
|
43
|
+
def test_valid_creations_and_other_commands_are_allowed(tmp_path, command):
|
|
44
|
+
repo = make_repo(tmp_path)
|
|
45
|
+
(repo / "body.md").write_text(VALID_BODY)
|
|
46
|
+
assert_allow(run_hook({"tool_name": "Bash", "tool_input": {"command": command},
|
|
47
|
+
"cwd": str(repo)}))
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Heredoc substitutions run as the shell runs them: continuations folded, before the line's `cd`."""
|
|
2
|
+
from test_hooks import assert_deny, claude, make_repo, run_hook
|
|
3
|
+
|
|
4
|
+
REASON = "the body must come from --body or --body-file"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_continuation_inside_heredoc_does_not_hide_substitution(tmp_path):
|
|
8
|
+
repo = make_repo(tmp_path)
|
|
9
|
+
assert_deny(run_hook(claude("cat <<EOF\n$\\\n(gh pr create --fill)\nEOF", repo)), REASON)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_heredoc_substitution_runs_before_the_commands_cd(tmp_path):
|
|
13
|
+
repo = make_repo(tmp_path)
|
|
14
|
+
outside = tmp_path / "outside"
|
|
15
|
+
outside.mkdir()
|
|
16
|
+
assert_deny(run_hook(claude(f"cd {outside} <<EOF\n$(gh pr create --fill)\nEOF", repo)), REASON)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Acceptance tests: `gh pr create` inside a command substitution, and shell quoting of the body."""
|
|
2
|
+
from test_hooks import NO_BLOCK_BODY, VALID_BODY, assert_allow, assert_deny, claude, make_repo, run_hook
|
|
3
|
+
|
|
4
|
+
BODY_FLAG_REASON = "the body must come from --body or --body-file"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_substituted_fill_is_denied(tmp_path):
|
|
8
|
+
repo = make_repo(tmp_path)
|
|
9
|
+
assert_deny(run_hook(claude("url=$(gh pr create --fill)", repo)), BODY_FLAG_REASON)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_substituted_body_file_without_block_is_denied(tmp_path):
|
|
13
|
+
repo = make_repo(tmp_path)
|
|
14
|
+
(repo / "body.md").write_text(NO_BLOCK_BODY)
|
|
15
|
+
cmd = "url=$(gh pr create --title x --body-file /tmp/body.md)".replace("/tmp/body.md", str(repo / "body.md"))
|
|
16
|
+
assert_deny(run_hook(claude(cmd, repo)), "/pr, then /pr-risk")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_backtick_substitution_is_denied(tmp_path):
|
|
20
|
+
repo = make_repo(tmp_path)
|
|
21
|
+
assert_deny(run_hook(claude("url=`gh pr create --fill`", repo)), BODY_FLAG_REASON)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_single_quoted_substitution_body_is_literal_and_denied(tmp_path):
|
|
25
|
+
repo = make_repo(tmp_path)
|
|
26
|
+
(repo / "body.md").write_text(VALID_BODY)
|
|
27
|
+
cmd = "gh pr create --title x --body '$(cat body.md)'"
|
|
28
|
+
assert_deny(run_hook(claude(cmd, repo)), "/pr, then /pr-risk")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_double_quoted_substitution_body_is_expanded_and_allowed(tmp_path):
|
|
32
|
+
repo = make_repo(tmp_path)
|
|
33
|
+
(repo / "body.md").write_text(VALID_BODY)
|
|
34
|
+
assert_allow(run_hook(claude('gh pr create --title x --body "$(cat body.md)"', repo)))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_substitution_without_pr_create_allows_with_no_output(tmp_path):
|
|
38
|
+
repo = make_repo(tmp_path)
|
|
39
|
+
proc = run_hook(claude("x=$(git rev-parse HEAD) && echo $x", repo))
|
|
40
|
+
assert proc.returncode == 0 and proc.stdout.strip() == ""
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""Acceptance tests for `hooks/pre-pr-create.sh`: agent payload JSON on stdin, agent output JSON on stdout.
|
|
2
|
+
|
|
3
|
+
Deny is `{"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny",
|
|
4
|
+
"permissionDecisionReason": ...}}`. Allow is empty stdout (or any output that is not a deny).
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import subprocess
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
HOOK = Path(__file__).with_name("hooks") / "pre-pr-create.sh"
|
|
11
|
+
|
|
12
|
+
VALID_BODY = '''## Summary
|
|
13
|
+
|
|
14
|
+
Text.
|
|
15
|
+
|
|
16
|
+
## Merge Danger
|
|
17
|
+
|
|
18
|
+
```merge-risk v1
|
|
19
|
+
door: two-way
|
|
20
|
+
blast_radius: isolated
|
|
21
|
+
touches: [docs, tests]
|
|
22
|
+
rollback: revert
|
|
23
|
+
verdict: auto
|
|
24
|
+
reasons:
|
|
25
|
+
- "Only skill prose and its tests change"
|
|
26
|
+
```
|
|
27
|
+
'''
|
|
28
|
+
NO_BLOCK_BODY = "## Summary\n\nText, no block.\n"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def git(cwd, *args):
|
|
32
|
+
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args],
|
|
33
|
+
cwd=cwd, check=True, capture_output=True, text=True)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def make_repo(tmp_path, policy=True):
|
|
37
|
+
"""A clone of an origin whose base has no policy; the policy, when asked, is only in the working tree."""
|
|
38
|
+
origin = tmp_path / "origin.git"
|
|
39
|
+
seed = tmp_path / "seed"
|
|
40
|
+
repo = tmp_path / "repo"
|
|
41
|
+
git(tmp_path, "init", "--bare", "-b", "main", str(origin))
|
|
42
|
+
seed.mkdir()
|
|
43
|
+
git(seed, "init", "-b", "main")
|
|
44
|
+
(seed / "README.md").write_text("hi\n")
|
|
45
|
+
git(seed, "add", ".")
|
|
46
|
+
git(seed, "commit", "-m", "init")
|
|
47
|
+
git(seed, "push", str(origin), "main")
|
|
48
|
+
git(tmp_path, "clone", str(origin), str(repo))
|
|
49
|
+
if policy:
|
|
50
|
+
(repo / ".merge-risk.json").write_text('{"agentMerge": true}\n')
|
|
51
|
+
return repo
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def run_hook(payload):
|
|
55
|
+
return subprocess.run(["bash", str(HOOK)], input=json.dumps(payload),
|
|
56
|
+
text=True, capture_output=True, timeout=30)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def claude(command, cwd):
|
|
60
|
+
return {"hook_event_name": "PreToolUse", "tool_name": "Bash",
|
|
61
|
+
"tool_input": {"command": command}, "cwd": str(cwd)}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def decision(proc):
|
|
65
|
+
assert proc.returncode == 0, proc.stderr
|
|
66
|
+
if not proc.stdout.strip():
|
|
67
|
+
return None, ""
|
|
68
|
+
out = json.loads(proc.stdout)["hookSpecificOutput"]
|
|
69
|
+
assert out["hookEventName"] == "PreToolUse"
|
|
70
|
+
return out["permissionDecision"], out.get("permissionDecisionReason", "")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def assert_deny(proc, *needles):
|
|
74
|
+
verdict, reason = decision(proc)
|
|
75
|
+
assert verdict == "deny", proc.stdout
|
|
76
|
+
for n in needles:
|
|
77
|
+
assert n in reason, reason
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def assert_allow(proc):
|
|
81
|
+
assert decision(proc)[0] != "deny", proc.stdout
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_body_file_without_block_is_denied_with_the_fix(tmp_path):
|
|
85
|
+
repo = make_repo(tmp_path)
|
|
86
|
+
body = tmp_path / "body.md"
|
|
87
|
+
body.write_text(NO_BLOCK_BODY)
|
|
88
|
+
proc = run_hook(claude(f"gh pr create --title x --body-file {body}", repo))
|
|
89
|
+
assert_deny(proc, "/pr, then /pr-risk")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_valid_block_in_body_substitution_is_allowed(tmp_path):
|
|
93
|
+
repo = make_repo(tmp_path)
|
|
94
|
+
body = tmp_path / "body.md"
|
|
95
|
+
body.write_text(VALID_BODY)
|
|
96
|
+
proc = run_hook(claude(f'gh pr create --title x --body "$(cat {body})"', repo))
|
|
97
|
+
assert_allow(proc)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_fill_variants_and_no_body_flag_are_denied(tmp_path):
|
|
101
|
+
repo = make_repo(tmp_path)
|
|
102
|
+
for cmd in ("gh pr create --fill", "gh pr create --fill-first", "gh pr create --web",
|
|
103
|
+
"gh pr create --title x"):
|
|
104
|
+
assert_deny(run_hook(claude(cmd, repo)), "the body must come from --body or --body-file")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_repo_without_policy_allows_with_no_output(tmp_path):
|
|
108
|
+
repo = make_repo(tmp_path, policy=False)
|
|
109
|
+
proc = run_hook(claude("gh pr create --fill", repo))
|
|
110
|
+
assert proc.returncode == 0 and proc.stdout.strip() == ""
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_other_command_allows_with_no_output(tmp_path):
|
|
114
|
+
repo = make_repo(tmp_path)
|
|
115
|
+
proc = run_hook(claude("gh pr view 12", repo))
|
|
116
|
+
assert proc.returncode == 0 and proc.stdout.strip() == ""
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_policy_only_in_working_tree_still_enforces(tmp_path):
|
|
120
|
+
repo = make_repo(tmp_path)
|
|
121
|
+
assert (repo / ".merge-risk.json").exists()
|
|
122
|
+
assert_deny(run_hook(claude("gh pr create --fill", repo)))
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_codex_payload_is_denied(tmp_path):
|
|
126
|
+
repo = make_repo(tmp_path)
|
|
127
|
+
proc = run_hook({"tool_name": "Bash", "tool_input": {"command": "gh pr create --fill"},
|
|
128
|
+
"cwd": str(repo)})
|
|
129
|
+
assert proc.returncode == 0, proc.stderr
|
|
130
|
+
out = json.loads(proc.stdout)["hookSpecificOutput"]
|
|
131
|
+
assert out["hookEventName"] == "PreToolUse"
|
|
132
|
+
assert out["permissionDecision"] == "deny"
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_gh_pr_create_inside_a_longer_command_is_denied(tmp_path):
|
|
136
|
+
repo = make_repo(tmp_path)
|
|
137
|
+
outside = tmp_path / "outside"
|
|
138
|
+
outside.mkdir()
|
|
139
|
+
assert_deny(run_hook(claude(f"cd {repo} && gh pr create --fill", outside)))
|
|
140
|
+
(repo / "body.md").write_text(VALID_BODY)
|
|
141
|
+
assert_allow(run_hook(claude(f"cd {repo} && gh pr create --title x --body-file body.md", outside)))
|
|
142
|
+
(repo / "bad.md").write_text(NO_BLOCK_BODY)
|
|
143
|
+
assert_deny(run_hook(claude(f"cd {repo} && gh pr create --title x --body-file bad.md", outside)),
|
|
144
|
+
"/pr, then /pr-risk")
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_relative_body_file_resolves_against_cwd(tmp_path):
|
|
148
|
+
repo = make_repo(tmp_path)
|
|
149
|
+
(repo / "body.md").write_text(VALID_BODY)
|
|
150
|
+
assert_allow(run_hook(claude("gh pr create --title x --body-file body.md", repo)))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def heredoc_command(body):
|
|
154
|
+
return f"gh pr create --title x --body \"$(cat <<'EOF'\n{body}EOF\n)\""
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_valid_block_in_quoted_heredoc_is_allowed(tmp_path):
|
|
158
|
+
repo = make_repo(tmp_path)
|
|
159
|
+
assert_allow(run_hook(claude(heredoc_command(VALID_BODY), repo)))
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_heredoc_body_without_block_is_denied(tmp_path):
|
|
163
|
+
repo = make_repo(tmp_path)
|
|
164
|
+
assert_deny(run_hook(claude(heredoc_command(NO_BLOCK_BODY), repo)), "/pr, then /pr-risk")
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def test_malformed_existing_blocks_are_denied(tmp_path):
|
|
168
|
+
repo = make_repo(tmp_path)
|
|
169
|
+
bad_value = VALID_BODY.replace("door: two-way", "door: sideways")
|
|
170
|
+
missing_field = VALID_BODY.replace("rollback: revert\n", "")
|
|
171
|
+
duplicate = VALID_BODY + "\n" + VALID_BODY
|
|
172
|
+
for name, body in (("value", bad_value), ("missing", missing_field), ("dup", duplicate)):
|
|
173
|
+
(repo / f"{name}.md").write_text(body)
|
|
174
|
+
proc = run_hook(claude(f"gh pr create --title x --body-file {name}.md", repo))
|
|
175
|
+
assert_deny(proc, "/pr, then /pr-risk")
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Acceptance tests for `merge_risk.py parse|validate`, through the command line only.
|
|
2
|
+
|
|
3
|
+
`parse` prints one JSON object: door, blast_radius, touches, rollback, verdict,
|
|
4
|
+
reasons and errors (a list of messages, empty when the block is valid).
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
SCRIPT = Path(__file__).with_name("merge_risk.py").resolve()
|
|
12
|
+
|
|
13
|
+
VALID = '''door: two-way # one-way | two-way
|
|
14
|
+
blast_radius: isolated
|
|
15
|
+
touches: [docs, tests]
|
|
16
|
+
rollback: revert
|
|
17
|
+
verdict: auto
|
|
18
|
+
reasons:
|
|
19
|
+
- "Only skill prose and its tests change"
|
|
20
|
+
'''
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def body(block=VALID):
|
|
24
|
+
return f"## Summary\n\nText.\n\n## Merge Danger\n\n```merge-risk v1\n{block}```\n"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def run(cmd, tmp_path, text):
|
|
28
|
+
f = tmp_path / "body.md"
|
|
29
|
+
f.write_text(text)
|
|
30
|
+
return subprocess.run([sys.executable, str(SCRIPT), cmd, str(f)],
|
|
31
|
+
text=True, capture_output=True, timeout=30)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def parsed(tmp_path, text):
|
|
35
|
+
proc = run("parse", tmp_path, text)
|
|
36
|
+
assert proc.returncode == 0, proc.stderr
|
|
37
|
+
return json.loads(proc.stdout)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def errors_of(tmp_path, text):
|
|
41
|
+
return parsed(tmp_path, text)["errors"]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_valid_block_parses(tmp_path):
|
|
45
|
+
out = parsed(tmp_path, body())
|
|
46
|
+
assert out["door"] == "two-way"
|
|
47
|
+
assert out["touches"] == ["docs", "tests"]
|
|
48
|
+
assert out["reasons"] == ["Only skill prose and its tests change"]
|
|
49
|
+
assert out["errors"] == []
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_unknown_category_is_invalid(tmp_path):
|
|
53
|
+
errs = errors_of(tmp_path, body(VALID.replace("[docs, tests]", "[docs, styles]")))
|
|
54
|
+
assert "unknown category: styles" in errs
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_touches_as_item_list_is_invalid(tmp_path):
|
|
58
|
+
block = VALID.replace("touches: [docs, tests]", "touches:\n - docs\n - tests")
|
|
59
|
+
assert errors_of(tmp_path, body(block))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_two_blocks_are_invalid(tmp_path):
|
|
63
|
+
assert "more than one block" in errors_of(tmp_path, body() + "\n" + body())
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_no_block_is_invalid(tmp_path):
|
|
67
|
+
assert "no merge-risk block" in errors_of(tmp_path, "## Summary\n\nNothing here.\n")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_missing_key_is_named(tmp_path):
|
|
71
|
+
errs = errors_of(tmp_path, body(VALID.replace("rollback: revert\n", "")))
|
|
72
|
+
assert any("rollback" in e for e in errs)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_unknown_key_is_named(tmp_path):
|
|
76
|
+
errs = errors_of(tmp_path, body(VALID + "colour: red\n"))
|
|
77
|
+
assert any("colour" in e for e in errs)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_enum_outside_list_is_named(tmp_path):
|
|
81
|
+
errs = errors_of(tmp_path, body(VALID.replace("door: two-way", "door: sideways")))
|
|
82
|
+
assert any("door" in e and "sideways" in e for e in errs)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_empty_reasons_is_invalid(tmp_path):
|
|
86
|
+
block = VALID.split("reasons:")[0] + "reasons:\n"
|
|
87
|
+
assert any("reasons" in e for e in errors_of(tmp_path, body(block)))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_validate_exits_zero_for_valid(tmp_path):
|
|
91
|
+
assert run("validate", tmp_path, body()).returncode == 0
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_validate_exits_nonzero_and_prints_errors(tmp_path):
|
|
95
|
+
proc = run("validate", tmp_path, body(VALID.replace("[docs, tests]", "[docs, styles]")))
|
|
96
|
+
assert proc.returncode != 0
|
|
97
|
+
assert "unknown category: styles" in proc.stdout + proc.stderr
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"""Acceptance tests for `merge_risk.py gate --body <file> --base <ref> --head <sha>`.
|
|
2
|
+
|
|
3
|
+
Each test builds a temp git repo: `origin/<base>` is a remote-tracking ref that holds the
|
|
4
|
+
policy, the head commit is the PR. The gate prints one JSON object on stdout:
|
|
5
|
+
`verdict` ("auto" | "human"), `reasons` (every reason that made it human),
|
|
6
|
+
`categories` (categories of the diff) and `countedLines`.
|
|
7
|
+
"""
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
SCRIPT = Path(__file__).with_name("merge_risk.py").resolve()
|
|
15
|
+
ENV = {**os.environ, "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@t",
|
|
16
|
+
"GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@t",
|
|
17
|
+
"GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_SYSTEM": os.devnull}
|
|
18
|
+
|
|
19
|
+
POLICY = ".merge-risk.json"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def git(repo, *args):
|
|
23
|
+
p = subprocess.run(["git", *args], cwd=repo, env=ENV, text=True,
|
|
24
|
+
capture_output=True, check=True)
|
|
25
|
+
return p.stdout.strip()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def write(repo, files):
|
|
29
|
+
for name, content in files.items():
|
|
30
|
+
f = Path(repo) / name
|
|
31
|
+
f.parent.mkdir(parents=True, exist_ok=True)
|
|
32
|
+
f.write_text(content)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def commit(repo, files, msg="c"):
|
|
36
|
+
write(repo, files)
|
|
37
|
+
git(repo, "add", "-A")
|
|
38
|
+
git(repo, "commit", "-m", msg)
|
|
39
|
+
return git(repo, "rev-parse", "HEAD")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def make_repo(tmp_path, base_files, head_files, base="main"):
|
|
43
|
+
"""Base commit published as origin/<base>; head commit on top of it. Returns (repo, sha)."""
|
|
44
|
+
repo = tmp_path / "repo"
|
|
45
|
+
repo.mkdir()
|
|
46
|
+
git(repo, "init", "-q", "-b", "work")
|
|
47
|
+
base_sha = commit(repo, base_files or {"README.md": "hi\n"}, "base")
|
|
48
|
+
git(repo, "update-ref", f"refs/remotes/origin/{base}", base_sha)
|
|
49
|
+
return repo, commit(repo, head_files, "head")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def policy(**kw):
|
|
53
|
+
return {POLICY: json.dumps(kw)}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def block(door="two-way", touches="docs", rollback="revert", verdict="auto"):
|
|
57
|
+
return (
|
|
58
|
+
"## Summary\n\nText.\n\n## Merge Danger\n\n```merge-risk v1\n"
|
|
59
|
+
f"door: {door}\nblast_radius: isolated\ntouches: [{touches}]\n"
|
|
60
|
+
f"rollback: {rollback}\nverdict: {verdict}\n"
|
|
61
|
+
'reasons:\n - "Small and reversible"\n```\n'
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def gate(tmp_path, repo, sha, body, base="main"):
|
|
66
|
+
f = tmp_path / "body.md"
|
|
67
|
+
f.write_text(body)
|
|
68
|
+
proc = subprocess.run(
|
|
69
|
+
[sys.executable, str(SCRIPT), "gate", "--body", str(f), "--base", base, "--head", sha],
|
|
70
|
+
cwd=repo, env=ENV, text=True, capture_output=True, timeout=60)
|
|
71
|
+
assert proc.returncode == 0, f"gate exited {proc.returncode}: {proc.stderr or proc.stdout}"
|
|
72
|
+
return json.loads(proc.stdout)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_no_block_is_human(tmp_path):
|
|
76
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True), {"docs/a.md": "x\n"})
|
|
77
|
+
out = gate(tmp_path, repo, sha, "## Summary\n\nNo block here.\n")
|
|
78
|
+
assert out["verdict"] == "human"
|
|
79
|
+
assert "no merge-risk block" in out["reasons"]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_repo_paths_add_to_defaults(tmp_path):
|
|
83
|
+
repo, sha = make_repo(
|
|
84
|
+
tmp_path, policy(agentMerge=True, paths={"money": ["src/billing/**"]}),
|
|
85
|
+
{"src/billing/invoice.py": "x\n", "db/migrations/0042_add.sql": "x\n"})
|
|
86
|
+
out = gate(tmp_path, repo, sha, block(touches="money, schema/migration"))
|
|
87
|
+
assert sorted(out["categories"]) == ["money", "schema/migration"]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_repo_paths_extend_default_globs_of_existing_category(tmp_path):
|
|
91
|
+
repo, sha = make_repo(
|
|
92
|
+
tmp_path, policy(agentMerge=True, paths={"docs": ["notes/**"]}),
|
|
93
|
+
{"docs/a.md": "x\n", "notes/b.txt": "x\n"})
|
|
94
|
+
out = gate(tmp_path, repo, sha, block(touches="docs"))
|
|
95
|
+
assert out["categories"] == ["docs"]
|
|
96
|
+
assert out["verdict"] == "auto"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_repo_always_human_replaces_defaults(tmp_path):
|
|
100
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True, alwaysHuman=["money"]),
|
|
101
|
+
{"Dockerfile": "FROM x\n"})
|
|
102
|
+
out = gate(tmp_path, repo, sha, block(touches="infra"))
|
|
103
|
+
assert out["verdict"] == "auto"
|
|
104
|
+
assert out["reasons"] == []
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_pr_cannot_loosen_its_own_policy(tmp_path):
|
|
108
|
+
repo, sha = make_repo(
|
|
109
|
+
tmp_path, policy(agentMerge=True),
|
|
110
|
+
{"db/migrations/0042_add.sql": "x\n", **policy(agentMerge=True, alwaysHuman=[])})
|
|
111
|
+
out = gate(tmp_path, repo, sha, block(touches="schema/migration, config"))
|
|
112
|
+
assert out["verdict"] == "human"
|
|
113
|
+
assert "schema/migration is always human" in out["reasons"]
|
|
114
|
+
assert "config: .merge-risk.json changed" in out["reasons"]
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_first_policy_file_is_not_used_by_its_own_pr(tmp_path):
|
|
118
|
+
repo, sha = make_repo(tmp_path, {"README.md": "hi\n"}, policy(agentMerge=True))
|
|
119
|
+
out = gate(tmp_path, repo, sha, block(touches="config"))
|
|
120
|
+
assert out["verdict"] == "human"
|
|
121
|
+
assert "agentMerge is off" in out["reasons"]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def test_unknown_policy_key_is_human(tmp_path):
|
|
125
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True, autoMerge=True),
|
|
126
|
+
{"docs/a.md": "x\n"})
|
|
127
|
+
out = gate(tmp_path, repo, sha, block())
|
|
128
|
+
assert out["verdict"] == "human"
|
|
129
|
+
assert "policy: unknown key autoMerge" in out["reasons"]
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def test_skill_md_is_logic_not_docs(tmp_path):
|
|
133
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True),
|
|
134
|
+
{"skills/pr-risk/SKILL.md": "x\n"})
|
|
135
|
+
out = gate(tmp_path, repo, sha, block(touches="docs"))
|
|
136
|
+
assert out["verdict"] == "human"
|
|
137
|
+
assert "diff touches logic, not in touches" in out["reasons"]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_instruction_file_under_docs_counts_as_logic_with_its_lines(tmp_path):
|
|
141
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True, judgeMaxLines=3),
|
|
142
|
+
{"docs/SKILL.md": "a\nb\nc\nd\ne\n"})
|
|
143
|
+
out = gate(tmp_path, repo, sha, block(touches="docs, logic"))
|
|
144
|
+
assert out["categories"] == ["logic"]
|
|
145
|
+
assert out["countedLines"] == 5
|
|
146
|
+
assert out["verdict"] == "human"
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def test_one_way_door_is_human_whatever_verdict_says(tmp_path):
|
|
150
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True), {"docs/a.md": "x\n"})
|
|
151
|
+
out = gate(tmp_path, repo, sha, block(door="one-way", verdict="auto"))
|
|
152
|
+
assert out["verdict"] == "human"
|
|
153
|
+
assert "door one-way" in out["reasons"]
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_rollback_other_than_revert_is_human(tmp_path):
|
|
157
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True), {"docs/a.md": "x\n"})
|
|
158
|
+
out = gate(tmp_path, repo, sha, block(rollback="revert+redeploy"))
|
|
159
|
+
assert out["verdict"] == "human"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_undeclared_migration_is_human(tmp_path):
|
|
163
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True),
|
|
164
|
+
{"src/app.py": "x\n", "tests/test_app.py": "x\n",
|
|
165
|
+
"db/migrations/0042_add.sql": "x\n"})
|
|
166
|
+
out = gate(tmp_path, repo, sha, block(touches="logic, tests"))
|
|
167
|
+
assert out["verdict"] == "human"
|
|
168
|
+
assert "diff touches schema/migration, not in touches" in out["reasons"]
|
|
169
|
+
assert "schema/migration is always human" in out["reasons"]
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_always_human_category_declared_only_in_body_is_human(tmp_path):
|
|
173
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True), {"src/app.py": "x\n"})
|
|
174
|
+
out = gate(tmp_path, repo, sha, block(touches="logic, auth/security"))
|
|
175
|
+
assert out["verdict"] == "human"
|
|
176
|
+
assert "auth/security is always human" in out["reasons"]
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_agent_merge_false_is_human(tmp_path):
|
|
180
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=False), {"docs/a.md": "x\n"})
|
|
181
|
+
out = gate(tmp_path, repo, sha, block())
|
|
182
|
+
assert out["verdict"] == "human"
|
|
183
|
+
assert "agentMerge is off" in out["reasons"]
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def counted_diff(n):
|
|
187
|
+
return {"src/app.py": "x\n" * n, "pnpm-lock.yaml": "l\n" * 4000,
|
|
188
|
+
"docs/guide.md": "d\n" * 900}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_counted_lines_at_the_limit_is_auto(tmp_path):
|
|
192
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True), counted_diff(1500))
|
|
193
|
+
out = gate(tmp_path, repo, sha, block(touches="deps, docs, logic"))
|
|
194
|
+
assert out["countedLines"] == 1500
|
|
195
|
+
assert out["verdict"] == "auto"
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def test_counted_lines_over_the_limit_is_human(tmp_path):
|
|
199
|
+
repo, sha = make_repo(tmp_path, policy(agentMerge=True), counted_diff(1501))
|
|
200
|
+
out = gate(tmp_path, repo, sha, block(touches="deps, docs, logic"))
|
|
201
|
+
assert out["verdict"] == "human"
|
|
202
|
+
assert "1501 counted lines > 1500" in out["reasons"]
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_stacked_base_uses_origin_parent_never_main(tmp_path):
|
|
206
|
+
repo = tmp_path / "repo"
|
|
207
|
+
repo.mkdir()
|
|
208
|
+
git(repo, "init", "-q", "-b", "work")
|
|
209
|
+
main_sha = commit(repo, {"README.md": "hi\n"}, "main") # main: no policy
|
|
210
|
+
git(repo, "update-ref", "refs/remotes/origin/main", main_sha)
|
|
211
|
+
parent_sha = commit(repo, {**policy(agentMerge=True), "db/migrations/0001.sql": "x\n"},
|
|
212
|
+
"parent")
|
|
213
|
+
git(repo, "update-ref", "refs/remotes/origin/feat/parent", parent_sha)
|
|
214
|
+
sha = commit(repo, {"docs/a.md": "x\n"}, "head")
|
|
215
|
+
out = gate(tmp_path, repo, sha, block(), base="feat/parent")
|
|
216
|
+
assert out["categories"] == ["docs"]
|
|
217
|
+
assert out["verdict"] == "auto"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Gate regressions exercised through its CLI in temporary repositories."""
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from test_merge_risk_gate import block, commit, gate, git, make_repo, policy, write
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.mark.parametrize("pattern", ["src/billing[12]/**", "src/billing[!3]/**"])
|
|
8
|
+
def test_unsupported_policy_glob_requires_human(tmp_path, pattern):
|
|
9
|
+
repo, sha = make_repo(
|
|
10
|
+
tmp_path, policy(agentMerge=True, paths={"money": [pattern]}),
|
|
11
|
+
{"src/billing1/invoice.py": "amount = 1\n"})
|
|
12
|
+
out = gate(tmp_path, repo, sha, block(touches="logic"))
|
|
13
|
+
assert out["verdict"] == "human"
|
|
14
|
+
assert "policy: unsupported glob syntax in paths" in out["reasons"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.mark.parametrize("was_generated", [False, True])
|
|
18
|
+
def test_deleted_file_uses_merge_base_marker_and_base_tip_policy(tmp_path, was_generated):
|
|
19
|
+
repo, _ = make_repo(
|
|
20
|
+
tmp_path,
|
|
21
|
+
{**policy(agentMerge=False),
|
|
22
|
+
"src/app.py": ("# @generated\n" if was_generated else "") + "x\n" * 1501},
|
|
23
|
+
{"README.md": "hi\n"})
|
|
24
|
+
ancestor = git(repo, "rev-parse", "HEAD")
|
|
25
|
+
(repo / "src/app.py").unlink()
|
|
26
|
+
sha = commit(repo, {}, "PR deletes app")
|
|
27
|
+
# Build an independent base commit without moving the PR's checked-out branch.
|
|
28
|
+
write(repo, {**policy(agentMerge=True),
|
|
29
|
+
"src/app.py": ("" if was_generated else "# @generated\n") + "x\n" * 1501})
|
|
30
|
+
git(repo, "add", "-A")
|
|
31
|
+
tree = git(repo, "write-tree")
|
|
32
|
+
base_tip = git(repo, "commit-tree", tree, "-p", ancestor, "-m", "base changes marker")
|
|
33
|
+
git(repo, "update-ref", "refs/remotes/origin/main", base_tip)
|
|
34
|
+
out = gate(tmp_path, repo, sha, block(touches="logic"))
|
|
35
|
+
assert "agentMerge is off" not in out["reasons"]
|
|
36
|
+
assert out["countedLines"] == (0 if was_generated else 1501)
|
|
37
|
+
assert out["verdict"] == ("auto" if was_generated else "human")
|