@jesdi/skills 0.0.101 → 0.0.102
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/package.json +1 -1
- package/skills/babysit-pr/SKILL.md +9 -2
- package/skills/babysit-pr/babysit.py +98 -36
- package/skills/babysit-pr/exam.py +156 -0
- package/skills/babysit-pr/fixtures/exam_host.py +47 -0
- package/skills/babysit-pr/fixtures/gh +107 -11
- package/skills/babysit-pr/test_babysit.py +8 -2
- package/skills/babysit-pr/test_e2e_setup.py +1 -1
- package/skills/babysit-pr/test_exam_merge_gate.py +189 -0
- package/skills/babysit-pr/test_exam_request.py +337 -0
- package/skills/babysit-pr/test_exam_required_check.py +145 -0
- package/skills/babysit-pr/test_exam_wait.py +353 -0
- package/skills/babysit-pr/test_merge_and_handoff.py +12 -6
- package/skills/babysit-pr/test_trusted_merge_risk.py +2 -2
- package/skills/pr-exam/SKILL.md +131 -0
- package/skills/pr-exam/fixtures/README.md +20 -0
- package/skills/pr-exam/fixtures/delta-new-concept/body.md +9 -0
- package/skills/pr-exam/fixtures/delta-new-concept/diff.patch +82 -0
- package/skills/pr-exam/fixtures/delta-new-concept/exam.json +46 -0
- package/skills/pr-exam/fixtures/delta-new-concept/passed.diff +66 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0001-single-sync-leader.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0002-stale-leader-writes.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0003-fencing-token.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0004-lease-expiry.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0005-injected-monotonic-clock.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0006-leader-lifecycle.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0007-lease-rollback.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0008-sync-blast-radius.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/request.json +43 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/body.md +9 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/diff.patch +66 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/exam.json +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/passed.diff +66 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0001-single-sync-leader.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0002-stale-leader-writes.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0003-fencing-token.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0004-lease-expiry.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0005-injected-monotonic-clock.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0006-leader-lifecycle.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0007-lease-rollback.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0008-sync-blast-radius.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/request.json +43 -0
- package/skills/pr-exam/fixtures/followup/body.md +9 -0
- package/skills/pr-exam/fixtures/followup/diff.patch +66 -0
- package/skills/pr-exam/fixtures/followup/exam.json +61 -0
- package/skills/pr-exam/fixtures/followup/records/0001-single-sync-leader.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0002-stale-leader-writes.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0003-fencing-token.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0004-lease-expiry.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0005-injected-monotonic-clock.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0006-lease-rollback.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0007-sync-blast-radius.md +10 -0
- package/skills/pr-exam/fixtures/followup/request.json +41 -0
- package/skills/pr-exam/fixtures/grading/answers.json +5 -0
- package/skills/pr-exam/fixtures/grading/attempt.json +10 -0
- package/skills/pr-exam/fixtures/grading/grades.json +14 -0
- package/skills/pr-exam/fixtures/grading/questions.json +26 -0
- package/skills/pr-exam/fixtures/grading/request.json +10 -0
- package/skills/pr-exam/fixtures/twelve-concepts/body.md +9 -0
- package/skills/pr-exam/fixtures/twelve-concepts/diff.patch +66 -0
- package/skills/pr-exam/fixtures/twelve-concepts/exam.json +195 -0
- package/skills/pr-exam/fixtures/twelve-concepts/request.json +15 -0
- package/skills/pr-exam/fixtures/typo-readme/body.md +1 -0
- package/skills/pr-exam/fixtures/typo-readme/diff.patch +7 -0
- package/skills/pr-exam/fixtures/typo-readme/exam.json +30 -0
- package/skills/pr-exam/fixtures/typo-readme/request.json +15 -0
- package/skills/pr-exam/pr_exam.py +705 -0
- package/skills/pr-exam/test_config.py +83 -0
- package/skills/pr-exam/test_exam_format.py +252 -0
- package/skills/pr-exam/test_followup_delta.py +90 -0
- package/skills/pr-exam/test_learning_records.py +267 -0
- package/skills/pr-exam/test_record_publication.py +45 -0
- package/skills/pr-exam/test_scoring.py +121 -0
- package/skills/pr-exam/test_served_shapes.py +38 -0
- package/skills/pr-exam/test_setup.py +349 -0
- package/skills/pr-exam/test_skill_fixtures.py +88 -0
- package/skills-manifest.json +9 -3
|
@@ -27,8 +27,11 @@ SCRIPT = Path(__file__).with_name("babysit.py").resolve()
|
|
|
27
27
|
FAKE_GH = Path(__file__).parent / "fixtures" / "gh"
|
|
28
28
|
LEASE = re.compile(r"<!-- babysit-pr lease: (\S+) (\S+) (\S+) -->")
|
|
29
29
|
INSTALL = "npx @jesdi/skills-cli install pr-risk"
|
|
30
|
+
STAGED = ("babysit.py", "exam.py", "judge-prompt.md") # the skill's own files a staged copy needs
|
|
30
31
|
T = datetime(2020, 1, 1, 10, 5, tzinfo=timezone.utc)
|
|
31
32
|
MIN = timedelta(minutes=1)
|
|
33
|
+
APP = "agent-ops-hel1" # PR_EXAM_APP of every world
|
|
34
|
+
BOT = f"{APP}[bot]" # the login the box's GitHub App posts statuses as
|
|
32
35
|
|
|
33
36
|
|
|
34
37
|
def iso(dt):
|
|
@@ -58,8 +61,11 @@ class World:
|
|
|
58
61
|
if not pr_risk: # no sibling pr-risk beside the script, no install under HOME
|
|
59
62
|
stage = tmp_path / "stage" / "skills" / "babysit-pr"
|
|
60
63
|
stage.mkdir(parents=True)
|
|
61
|
-
for name in
|
|
64
|
+
for name in STAGED:
|
|
62
65
|
shutil.copy(SCRIPT.parent / name, stage / name)
|
|
66
|
+
# pr-exam stays installed: exams are on by default, and every real run past `exam` has it
|
|
67
|
+
(stage.parent / "pr-exam").mkdir()
|
|
68
|
+
shutil.copy(SCRIPT.parent.parent / "pr-exam" / "pr_exam.py", stage.parent / "pr-exam")
|
|
63
69
|
self.script = stage / "babysit.py"
|
|
64
70
|
if pr_risk:
|
|
65
71
|
skill = self.home / ".agents" / "skills" / "pr-risk"
|
|
@@ -74,7 +80,7 @@ class World:
|
|
|
74
80
|
"next_id": max([c["id"] for c in comments] + [1000]) + 1}))
|
|
75
81
|
self.env = {**os.environ, "PATH": f"{bindir}{os.pathsep}{os.environ['PATH']}",
|
|
76
82
|
"HOME": str(self.home), "GH_FAKE_STATE": str(self.state),
|
|
77
|
-
"GH_FAKE_LOG": str(self.log)}
|
|
83
|
+
"GH_FAKE_LOG": str(self.log), "PR_EXAM_APP": APP}
|
|
78
84
|
|
|
79
85
|
def run(self, verb, session, repo="jesdi/demo", pr="58"):
|
|
80
86
|
p = subprocess.run(
|
|
@@ -25,7 +25,7 @@ babysit = importlib.util.module_from_spec(spec)
|
|
|
25
25
|
spec.loader.exec_module(babysit)
|
|
26
26
|
reviewed = Path('.claude/skills/pr-risk').resolve()
|
|
27
27
|
assert not reviewed.is_relative_to(Path.cwd()), reviewed
|
|
28
|
-
assert babysit.
|
|
28
|
+
assert babysit.trusted_install(*babysit.PR_RISK) == reviewed
|
|
29
29
|
assert Path(babysit.load_merge_risk().__file__).resolve() == reviewed / 'merge_risk.py'
|
|
30
30
|
assert Path('.agents/skills/babysit-pr').resolve() == Path('.claude/skills/babysit-pr').resolve()
|
|
31
31
|
"""
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Acceptance tests for the exam gate of `babysit.py merge` (spec specs/pr-exam, requirement 12).
|
|
2
|
+
|
|
3
|
+
Interface under test (extends `merge` of test_merge_and_handoff.py):
|
|
4
|
+
In a repo with exams enabled (`.pr-exam.json` on origin/<base> absent or enabled), `merge` goes
|
|
5
|
+
ahead only when the newest `exam` commit status of --sha that the box's GitHub App posted
|
|
6
|
+
(creator `$PR_EXAM_APP[bot]`, type Bot) is success, on every approval path; a status from any
|
|
7
|
+
other creator is ignored, and PR_EXAM_APP unset stops the merge naming it. Otherwise it
|
|
8
|
+
exits non-zero printing "exam not passed", runs no `gh pr merge`, posts no handoff comment and
|
|
9
|
+
keeps the lease and the `babysitting` label. Success with any description ("bypassed", "deferred")
|
|
10
|
+
is a pass. A pass changes nothing else. With `{"enabled": false}` the status is not read.
|
|
11
|
+
After a successful merge it POSTs {"repo", "pr"} once to $PR_EXAM_HOST/api/exam; a failing host
|
|
12
|
+
never fails the merge.
|
|
13
|
+
"""
|
|
14
|
+
import json
|
|
15
|
+
import sys
|
|
16
|
+
import threading
|
|
17
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
import pytest
|
|
21
|
+
|
|
22
|
+
sys.path.insert(0, str(Path(__file__).parent / "fixtures"))
|
|
23
|
+
from exam_host import FakeHost # noqa: E402
|
|
24
|
+
from test_babysit import BOT
|
|
25
|
+
from test_babysit_judge import commit as git_commit, git
|
|
26
|
+
from test_merge_and_handoff import EXAM_OK, commit, handoffs, merge, world
|
|
27
|
+
|
|
28
|
+
JUDGE = ("--judge", "agree", "--judge-risk", "2")
|
|
29
|
+
PATHS = {
|
|
30
|
+
"session": ("--session-approved",),
|
|
31
|
+
"judge": JUDGE,
|
|
32
|
+
"approved-sha": ("--approved-sha", "a1b2c3d"),
|
|
33
|
+
}
|
|
34
|
+
PENDING = {"context": "exam", "state": "pending", "description": "attempt 1", "creator": BOT}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def set_exam(w, sha, *statuses):
|
|
38
|
+
st = w.github()
|
|
39
|
+
st["statuses"] = {sha: list(statuses)}
|
|
40
|
+
w.state.write_text(json.dumps(st))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def run(w, tmp_path, path, sha="a1b2c3d"):
|
|
44
|
+
assert w.run("acquire", "s1").returncode == 0
|
|
45
|
+
return merge(w, tmp_path, sha, *PATHS[path], "--session", "s1")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def refused(w, p, host=None):
|
|
49
|
+
assert p.returncode != 0
|
|
50
|
+
assert "exam not passed" in p.output
|
|
51
|
+
assert not w.github().get("merges")
|
|
52
|
+
assert handoffs(w) == []
|
|
53
|
+
assert "needs-human" not in w.github()["labels"] and "babysitting" in w.github()["labels"]
|
|
54
|
+
assert [l[1] for l in w.active_leases()] == ["s1"]
|
|
55
|
+
if host is not None:
|
|
56
|
+
assert host.requests == []
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@pytest.mark.parametrize("path", PATHS)
|
|
60
|
+
@pytest.mark.parametrize("statuses", [[PENDING], []], ids=["pending", "no-status"])
|
|
61
|
+
def test_merge_is_refused_without_a_passed_exam(tmp_path, path, statuses):
|
|
62
|
+
w = world(tmp_path, approvals=("a1b2c3d",), statuses={"a1b2c3d": statuses})
|
|
63
|
+
with FakeHost() as host:
|
|
64
|
+
w.env["PR_EXAM_HOST"] = host.url
|
|
65
|
+
refused(w, run(w, tmp_path, path), host)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_a_failed_exam_does_not_pass_either(tmp_path):
|
|
69
|
+
w = world(tmp_path, statuses={"a1b2c3d": [{"context": "exam", "state": "failure", "creator": BOT}]})
|
|
70
|
+
refused(w, run(w, tmp_path, "session"))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_the_status_read_is_the_one_of_the_sha_not_of_an_earlier_commit(tmp_path):
|
|
74
|
+
w = world(tmp_path, head="a1b2c3d", commits=[commit("c1"), commit("a1b2c3d")],
|
|
75
|
+
statuses={"c1": [EXAM_OK]})
|
|
76
|
+
refused(w, run(w, tmp_path, "judge"))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_the_newest_exam_status_of_the_sha_counts(tmp_path):
|
|
80
|
+
w = world(tmp_path, statuses={"a1b2c3d": [PENDING, EXAM_OK]}) # newest first
|
|
81
|
+
refused(w, run(w, tmp_path, "judge"))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
FOREIGN = {"operator": "jesdi", "other-app": "other-app[bot]", "none": None}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@pytest.mark.parametrize("creator", FOREIGN.values(), ids=FOREIGN.keys())
|
|
88
|
+
def test_an_exam_success_posted_by_anyone_but_the_app_is_refused(tmp_path, creator):
|
|
89
|
+
foreign = {**EXAM_OK, "creator": creator}
|
|
90
|
+
w = world(tmp_path, statuses={"a1b2c3d": [foreign, PENDING]}) # newer than the App's pending
|
|
91
|
+
refused(w, run(w, tmp_path, "judge"))
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@pytest.mark.parametrize("creator", FOREIGN.values(), ids=FOREIGN.keys())
|
|
95
|
+
def test_the_apps_success_merges_whatever_anyone_else_posted_after_it(tmp_path, creator):
|
|
96
|
+
foreign = {"context": "exam", "state": "failure", "creator": creator}
|
|
97
|
+
w = world(tmp_path, statuses={"a1b2c3d": [foreign, EXAM_OK]}) # newest first
|
|
98
|
+
p = run(w, tmp_path, "judge")
|
|
99
|
+
assert p.returncode == 0, p.output
|
|
100
|
+
[m] = w.github()["merges"]
|
|
101
|
+
assert m["match"] == "a1b2c3d"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_merge_without_the_app_variable_stops_naming_it(tmp_path):
|
|
105
|
+
w = world(tmp_path)
|
|
106
|
+
w.env.pop("PR_EXAM_APP")
|
|
107
|
+
p = run(w, tmp_path, "judge")
|
|
108
|
+
assert p.returncode != 0
|
|
109
|
+
assert "PR_EXAM_APP" in p.output
|
|
110
|
+
assert not w.github().get("merges")
|
|
111
|
+
assert handoffs(w) == []
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_a_pass_merges_the_pinned_head_after_a_refusal(tmp_path):
|
|
115
|
+
w = world(tmp_path, statuses={"a1b2c3d": [PENDING]})
|
|
116
|
+
refused(w, run(w, tmp_path, "judge"))
|
|
117
|
+
set_exam(w, "a1b2c3d", EXAM_OK)
|
|
118
|
+
p = merge(w, tmp_path, "a1b2c3d", *JUDGE, "--session", "s1")
|
|
119
|
+
assert p.returncode == 0, p.output
|
|
120
|
+
[m] = w.github()["merges"]
|
|
121
|
+
assert m["match"] == "a1b2c3d" and m["auto"] is False
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@pytest.mark.parametrize("description", ["bypassed", "deferred"])
|
|
125
|
+
def test_a_bypassed_or_deferred_exam_merges_like_a_pass(tmp_path, description):
|
|
126
|
+
w = world(tmp_path, statuses={"a1b2c3d": [PENDING]})
|
|
127
|
+
refused(w, run(w, tmp_path, "judge"))
|
|
128
|
+
set_exam(w, "a1b2c3d", {"context": "exam", "state": "success", "description": description, "creator": BOT})
|
|
129
|
+
p = merge(w, tmp_path, "a1b2c3d", *JUDGE, "--session", "s1")
|
|
130
|
+
assert p.returncode == 0, p.output
|
|
131
|
+
assert len(w.github()["merges"]) == 1
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_a_pass_is_not_merge_approval(tmp_path):
|
|
135
|
+
w = world(tmp_path, policy={"maxAutoRisk": 0}, statuses={"a1b2c3d": [PENDING]})
|
|
136
|
+
refused(w, run(w, tmp_path, "judge")) # the exam is checked first, with no handoff
|
|
137
|
+
set_exam(w, "a1b2c3d", EXAM_OK)
|
|
138
|
+
p = merge(w, tmp_path, "a1b2c3d", *JUDGE, "--session", "s1")
|
|
139
|
+
assert p.returncode != 0
|
|
140
|
+
assert not w.github().get("merges")
|
|
141
|
+
[h] = handoffs(w)
|
|
142
|
+
assert "exam" not in h.lower()
|
|
143
|
+
assert "needs-human" in w.github()["labels"]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def test_exams_turned_off_do_not_look_at_the_exam_status(tmp_path):
|
|
147
|
+
w = world(tmp_path, statuses={"a1b2c3d": [PENDING]})
|
|
148
|
+
refused(w, run(w, tmp_path, "judge")) # the same world with exams on
|
|
149
|
+
cfg = git_commit(w.project, {".pr-exam.json": '{"enabled": false}'}, "exams off")
|
|
150
|
+
git(w.project, "update-ref", "refs/remotes/origin/main", cfg)
|
|
151
|
+
p = merge(w, tmp_path, "a1b2c3d", *JUDGE, "--session", "s1")
|
|
152
|
+
assert p.returncode == 0, p.output
|
|
153
|
+
assert len(w.github()["merges"]) == 1
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_after_a_merge_the_host_gets_one_request_for_the_pr(tmp_path):
|
|
157
|
+
w = world(tmp_path)
|
|
158
|
+
with FakeHost() as host:
|
|
159
|
+
w.env["PR_EXAM_HOST"] = host.url
|
|
160
|
+
p = merge(w, tmp_path, "a1b2c3d", *JUDGE)
|
|
161
|
+
assert p.returncode == 0, p.output
|
|
162
|
+
assert host.requests == [{"path": "/api/exam", "body": {"repo": "jesdi/demo", "pr": 58}}]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_a_failing_host_does_not_fail_the_merge(tmp_path):
|
|
166
|
+
seen = []
|
|
167
|
+
|
|
168
|
+
class Broken(BaseHTTPRequestHandler):
|
|
169
|
+
def do_POST(self):
|
|
170
|
+
seen.append(self.path)
|
|
171
|
+
self.send_response(500)
|
|
172
|
+
self.send_header("Content-Length", "0")
|
|
173
|
+
self.end_headers()
|
|
174
|
+
|
|
175
|
+
def log_message(self, *args):
|
|
176
|
+
pass
|
|
177
|
+
|
|
178
|
+
server = HTTPServer(("127.0.0.1", 0), Broken)
|
|
179
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
180
|
+
try:
|
|
181
|
+
w = world(tmp_path)
|
|
182
|
+
w.env["PR_EXAM_HOST"] = f"http://127.0.0.1:{server.server_port}"
|
|
183
|
+
p = merge(w, tmp_path, "a1b2c3d", *JUDGE)
|
|
184
|
+
finally:
|
|
185
|
+
server.shutdown()
|
|
186
|
+
server.server_close()
|
|
187
|
+
assert seen == ["/api/exam"]
|
|
188
|
+
assert p.returncode == 0, p.output
|
|
189
|
+
assert len(w.github()["merges"]) == 1
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"""Acceptance tests for `babysit.py exam --repo R --pr N` and `babysit.py checks` with exams.
|
|
2
|
+
|
|
3
|
+
Spec specs/pr-exam, requirements 9-11; scenarios "babysit-pr waits for the exam once the PR is
|
|
4
|
+
otherwise ready", "the exam isn't requested before CI is green", "exams turned off for a repo",
|
|
5
|
+
"an invalid config stops the run", "an agent's skip label is ignored", the owner's skip/defer
|
|
6
|
+
labels (request half).
|
|
7
|
+
|
|
8
|
+
Interface under test:
|
|
9
|
+
babysit.py exam --repo R --pr N
|
|
10
|
+
exit 0 when the PR is ready: asks the host once (POST $PR_EXAM_HOST/api/exam with exactly
|
|
11
|
+
{"repo", "pr"}) and prints one JSON object holding the host's `url` and `state`, then a line
|
|
12
|
+
with "bypassed" (state skipped) or "deferred" (a deferred open exam: debt true).
|
|
13
|
+
Not ready (a check pending, or owner-comments would hand off): prints "not ready", asks
|
|
14
|
+
nothing. Exams off in `.pr-exam.json`: prints "disabled", asks nothing. Invalid config,
|
|
15
|
+
PR_EXAM_HOST or PR_EXAM_APP unset, pr-exam not installed or an unreachable host: non-zero, with the reason.
|
|
16
|
+
babysit.py checks ...: the `exam` commit status never counts as pending, stalled or red.
|
|
17
|
+
`.pr-exam.json` is read from `origin/<base>` of the checkout in the current directory. `gh` is the
|
|
18
|
+
fake in fixtures/gh, the host is fixtures/exam_host.py, reached through PR_EXAM_HOST.
|
|
19
|
+
"""
|
|
20
|
+
import json
|
|
21
|
+
import shutil
|
|
22
|
+
import subprocess
|
|
23
|
+
import sys
|
|
24
|
+
from datetime import timedelta
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
import pytest
|
|
28
|
+
|
|
29
|
+
sys.path.insert(0, str(Path(__file__).parent / "fixtures"))
|
|
30
|
+
from exam_host import FakeHost # noqa: E402
|
|
31
|
+
from test_babysit import BOT, STAGED, SCRIPT, T, MIN, World, iso
|
|
32
|
+
from test_babysit_judge import git, commit
|
|
33
|
+
|
|
34
|
+
PR_EXAM_INSTALL = "npx @jesdi/skills-cli install pr-exam"
|
|
35
|
+
GREEN = [{"name": "test", "status": "completed", "conclusion": "success", "started_at": iso(T)}]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Exam(World):
|
|
39
|
+
def __init__(self, tmp_path, base_config=None, head_config=None, checks=GREEN, statuses=None,
|
|
40
|
+
**kw):
|
|
41
|
+
super().__init__(tmp_path, **kw)
|
|
42
|
+
repo = self.project
|
|
43
|
+
git(repo, "init", "-q", "-b", "work")
|
|
44
|
+
base = commit(repo, {"README.md": "hi\n", **(
|
|
45
|
+
{".pr-exam.json": base_config} if base_config is not None else {})}, "base")
|
|
46
|
+
git(repo, "update-ref", "refs/remotes/origin/main", base)
|
|
47
|
+
if head_config is not None:
|
|
48
|
+
commit(repo, {".pr-exam.json": head_config}, "pr config")
|
|
49
|
+
st = self.github()
|
|
50
|
+
st.update(head_sha="head1", base_ref="main", check_runs={"head1": list(checks)},
|
|
51
|
+
statuses={"head1": list(statuses or [])})
|
|
52
|
+
self.state.write_text(json.dumps(st))
|
|
53
|
+
|
|
54
|
+
def seed(self, **extra):
|
|
55
|
+
st = self.github()
|
|
56
|
+
st.update(extra)
|
|
57
|
+
self.state.write_text(json.dumps(st))
|
|
58
|
+
|
|
59
|
+
def exam(self, host_url, script=None):
|
|
60
|
+
env = {**self.env}
|
|
61
|
+
if host_url:
|
|
62
|
+
env["PR_EXAM_HOST"] = host_url
|
|
63
|
+
else:
|
|
64
|
+
env.pop("PR_EXAM_HOST", None)
|
|
65
|
+
p = subprocess.run([sys.executable, str(script or self.script), "exam", "--repo",
|
|
66
|
+
"jesdi/demo", "--pr", "58"], cwd=self.project, env=env, text=True,
|
|
67
|
+
capture_output=True)
|
|
68
|
+
p.output = p.stdout + p.stderr
|
|
69
|
+
return p
|
|
70
|
+
|
|
71
|
+
def checks(self):
|
|
72
|
+
p = subprocess.run([sys.executable, str(self.script), "checks", "--repo", "jesdi/demo",
|
|
73
|
+
"--pr", "58", "--wait-minutes", "60"], cwd=self.project,
|
|
74
|
+
env=self.env, text=True, capture_output=True)
|
|
75
|
+
p.output = p.stdout + p.stderr
|
|
76
|
+
return p
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def reported(p):
|
|
80
|
+
"""The JSON object `exam` printed (first line that parses as one)."""
|
|
81
|
+
for line in p.stdout.splitlines():
|
|
82
|
+
try:
|
|
83
|
+
obj = json.loads(line)
|
|
84
|
+
except ValueError:
|
|
85
|
+
continue
|
|
86
|
+
if isinstance(obj, dict):
|
|
87
|
+
return obj
|
|
88
|
+
raise AssertionError("no JSON object on stdout: " + p.output)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_a_ready_pr_in_a_repo_without_config_asks_the_host_once(tmp_path):
|
|
92
|
+
w = Exam(tmp_path)
|
|
93
|
+
with FakeHost({"state": "open"}) as host:
|
|
94
|
+
p = w.exam(host.url)
|
|
95
|
+
assert p.returncode == 0, p.output
|
|
96
|
+
assert len(host.requests) == 1
|
|
97
|
+
assert host.requests[0]["path"] == "/api/exam"
|
|
98
|
+
out = reported(p)
|
|
99
|
+
assert out["url"] == "https://box.example.ts.net/exam/jesdi/demo/58"
|
|
100
|
+
assert out["state"] == "open"
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def test_a_check_still_running_is_not_ready_and_asks_nothing(tmp_path):
|
|
104
|
+
w = Exam(tmp_path, checks=GREEN + [{"name": "e2e", "status": "in_progress",
|
|
105
|
+
"conclusion": None, "started_at": iso(T)}])
|
|
106
|
+
with FakeHost() as host:
|
|
107
|
+
p = w.exam(host.url)
|
|
108
|
+
assert "not ready" in p.output.lower(), p.output
|
|
109
|
+
assert host.requests == []
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_an_owner_comments_handoff_is_not_ready_and_asks_nothing(tmp_path):
|
|
113
|
+
w = Exam(tmp_path)
|
|
114
|
+
w.seed(comments=[{"id": 1001, "user": {"login": "alice"}, "body": "why this change?",
|
|
115
|
+
"created_at": iso(T), "updated_at": iso(T)}], next_id=1002)
|
|
116
|
+
with FakeHost() as host:
|
|
117
|
+
p = w.exam(host.url)
|
|
118
|
+
assert "not ready" in p.output.lower(), p.output
|
|
119
|
+
assert host.requests == []
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def test_exams_turned_off_say_disabled_and_ask_nothing(tmp_path):
|
|
123
|
+
w = Exam(tmp_path, base_config='{"enabled": false}')
|
|
124
|
+
with FakeHost() as host:
|
|
125
|
+
p = w.exam(host.url)
|
|
126
|
+
assert p.returncode == 0, p.output
|
|
127
|
+
assert "disabled" in p.output.lower(), p.output
|
|
128
|
+
assert host.requests == []
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
INVALID_AND_OFF = '{"enabled": false, "bypassLabels": {"exam:skip": "maybe"}}'
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@pytest.mark.parametrize("config", ['{"bypassLabels": {"exam:skip": "maybe"}}', INVALID_AND_OFF])
|
|
135
|
+
def test_an_invalid_bypass_mode_stops_naming_the_field_and_the_modes(tmp_path, config):
|
|
136
|
+
w = Exam(tmp_path, base_config=config) # `"enabled": false` never makes an invalid file "exams off"
|
|
137
|
+
with FakeHost() as host:
|
|
138
|
+
p = w.exam(host.url)
|
|
139
|
+
assert p.returncode != 0, p.output
|
|
140
|
+
for word in ("bypassLabels", "skip", "defer"):
|
|
141
|
+
assert word in p.output
|
|
142
|
+
assert host.requests == []
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_a_config_changed_only_on_the_pr_branch_has_no_effect(tmp_path):
|
|
146
|
+
w = Exam(tmp_path, base_config=None, head_config='{"enabled": false}')
|
|
147
|
+
with FakeHost() as host:
|
|
148
|
+
p = w.exam(host.url)
|
|
149
|
+
assert p.returncode == 0, p.output
|
|
150
|
+
assert len(host.requests) == 1
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_an_invalid_config_only_on_the_pr_branch_does_not_stop_the_run(tmp_path):
|
|
154
|
+
w = Exam(tmp_path, base_config='{"enabled": true}',
|
|
155
|
+
head_config='{"bypassLabels": {"exam:skip": "maybe"}}')
|
|
156
|
+
with FakeHost() as host:
|
|
157
|
+
p = w.exam(host.url)
|
|
158
|
+
assert p.returncode == 0, p.output
|
|
159
|
+
assert len(host.requests) == 1
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_the_request_is_only_repo_and_pr_and_a_non_owner_skip_label_changes_nothing(tmp_path):
|
|
163
|
+
w = Exam(tmp_path, base_config='{"bypassLabels": {"exam:skip": "skip"}}')
|
|
164
|
+
w.seed(labels=["exam:skip"], head_sha="head1")
|
|
165
|
+
with FakeHost({"state": "open"}) as host:
|
|
166
|
+
p = w.exam(host.url)
|
|
167
|
+
assert p.returncode == 0, p.output
|
|
168
|
+
assert host.requests[0]["body"] == {"repo": "jesdi/demo", "pr": 58}
|
|
169
|
+
assert reported(p)["state"] == "open"
|
|
170
|
+
assert "bypassed" not in p.output.lower()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def test_a_skipped_exam_is_reported_as_bypassed(tmp_path):
|
|
174
|
+
w = Exam(tmp_path, base_config='{"bypassLabels": {"exam:skip": "skip"}}')
|
|
175
|
+
with FakeHost({"state": "skipped", "pending": False}) as host:
|
|
176
|
+
p = w.exam(host.url)
|
|
177
|
+
assert p.returncode == 0, p.output
|
|
178
|
+
assert "bypassed" in p.output.lower()
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_a_deferred_exam_is_reported_as_deferred(tmp_path):
|
|
182
|
+
w = Exam(tmp_path, base_config='{"bypassLabels": {"exam:defer": "defer"}}')
|
|
183
|
+
with FakeHost({"state": "open", "debt": True, "pending": False}) as host:
|
|
184
|
+
p = w.exam(host.url)
|
|
185
|
+
assert p.returncode == 0, p.output
|
|
186
|
+
assert "deferred" in p.output.lower()
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@pytest.mark.parametrize("state", ["pending", "error", "failure"])
|
|
190
|
+
def test_checks_ignore_the_exam_status_whatever_its_state_and_age(tmp_path, state):
|
|
191
|
+
w = Exam(tmp_path, statuses=[{"context": "exam", "state": state, "creator": BOT}])
|
|
192
|
+
first = w.checks()
|
|
193
|
+
assert "green" in first.output.lower(), first.output
|
|
194
|
+
w.set_now(T + 3 * 24 * 60 * MIN) # three days later, far beyond --wait-minutes
|
|
195
|
+
later = w.checks().output
|
|
196
|
+
assert "green" in later.lower(), later
|
|
197
|
+
assert "stalled" not in later.lower()
|
|
198
|
+
assert "red" not in later.lower()
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def test_exams_enabled_without_the_host_variable_names_it(tmp_path):
|
|
202
|
+
w = Exam(tmp_path)
|
|
203
|
+
p = w.exam(None)
|
|
204
|
+
assert p.returncode != 0, p.output
|
|
205
|
+
assert "PR_EXAM_HOST" in p.output
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def test_exams_enabled_without_the_app_variable_names_it(tmp_path):
|
|
209
|
+
w = Exam(tmp_path)
|
|
210
|
+
w.env.pop("PR_EXAM_APP")
|
|
211
|
+
with FakeHost() as host:
|
|
212
|
+
p = w.exam(host.url)
|
|
213
|
+
assert p.returncode != 0, p.output
|
|
214
|
+
assert "PR_EXAM_APP" in p.output
|
|
215
|
+
assert host.requests == []
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def test_exams_enabled_without_pr_exam_installed_names_the_install_command(tmp_path):
|
|
219
|
+
w = Exam(tmp_path)
|
|
220
|
+
stage = tmp_path / "stage" / "skills" / "babysit-pr" # no sibling pr-exam, none under HOME
|
|
221
|
+
stage.mkdir(parents=True)
|
|
222
|
+
for name in STAGED:
|
|
223
|
+
shutil.copy(SCRIPT.parent / name, stage / name)
|
|
224
|
+
with FakeHost() as host:
|
|
225
|
+
p = w.exam(host.url, script=stage / "babysit.py")
|
|
226
|
+
assert p.returncode != 0, p.output
|
|
227
|
+
assert PR_EXAM_INSTALL in p.output
|
|
228
|
+
assert host.requests == []
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def test_an_unreachable_host_exits_non_zero_and_says_so(tmp_path):
|
|
232
|
+
w = Exam(tmp_path)
|
|
233
|
+
with FakeHost() as host:
|
|
234
|
+
dead = host.url
|
|
235
|
+
p = w.exam(dead) # the server is shut down: nothing listens there
|
|
236
|
+
assert p.returncode != 0, p.output
|
|
237
|
+
assert "unreachable" in p.output.lower()
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _stage(tmp_path):
|
|
241
|
+
"""babysit.py with no sibling pr-exam, so only the checkout and HOME can supply one."""
|
|
242
|
+
stage = tmp_path / "stage" / "skills" / "babysit-pr"
|
|
243
|
+
stage.mkdir(parents=True)
|
|
244
|
+
for name in STAGED:
|
|
245
|
+
shutil.copy(SCRIPT.parent / name, stage / name)
|
|
246
|
+
return stage / "babysit.py"
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _evil(marker):
|
|
250
|
+
return f"import pathlib\npathlib.Path({str(marker)!r}).write_text('ran')\nraise SystemExit(0)\n"
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _run_with_install(tmp_path, place):
|
|
254
|
+
w = Exam(tmp_path, base_config='{"enabled": true}')
|
|
255
|
+
marker = tmp_path / "marker"
|
|
256
|
+
script = place(w, _evil(marker)) or _stage(tmp_path)
|
|
257
|
+
with FakeHost() as host:
|
|
258
|
+
p = w.exam(host.url, script=script)
|
|
259
|
+
return p, marker, host
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _in_checkout(w, src):
|
|
263
|
+
for d in ("skills/pr-exam", ".claude/skills/pr-exam", ".agents/skills/pr-exam"):
|
|
264
|
+
(w.project / d).mkdir(parents=True, exist_ok=True)
|
|
265
|
+
(w.project / d / "pr_exam.py").write_text(src)
|
|
266
|
+
# Exercise the sibling-install candidate while babysit itself lives in the reviewed repo.
|
|
267
|
+
stage = w.project / "skills/babysit-pr"
|
|
268
|
+
stage.mkdir()
|
|
269
|
+
for name in STAGED:
|
|
270
|
+
shutil.copy(SCRIPT.parent / name, stage / name)
|
|
271
|
+
return stage / "babysit.py"
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _home_is_checkout(w, src):
|
|
275
|
+
w.env["HOME"] = str(w.project)
|
|
276
|
+
(w.project / ".claude/skills/pr-exam").mkdir(parents=True)
|
|
277
|
+
(w.project / ".claude/skills/pr-exam/pr_exam.py").write_text(src)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _home_symlink(w, src, skills=".claude/skills"):
|
|
281
|
+
(w.project / "vendored").mkdir()
|
|
282
|
+
(w.project / "vendored/pr_exam.py").write_text(src)
|
|
283
|
+
d = w.home / skills / "pr-exam"
|
|
284
|
+
d.mkdir(parents=True)
|
|
285
|
+
(d / "pr_exam.py").symlink_to(w.project / "vendored/pr_exam.py")
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def _codex_home_symlink(w, src):
|
|
289
|
+
_home_symlink(w, src, ".codex/skills")
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
@pytest.mark.parametrize("place", [_in_checkout, _home_is_checkout, _home_symlink, _codex_home_symlink])
|
|
293
|
+
def test_a_pr_exam_inside_the_reviewed_checkout_never_runs(tmp_path, place):
|
|
294
|
+
p, marker, host = _run_with_install(tmp_path, place)
|
|
295
|
+
assert not marker.exists(), "the PR's own pr_exam.py was executed"
|
|
296
|
+
assert p.returncode != 0, p.output
|
|
297
|
+
assert PR_EXAM_INSTALL in p.output
|
|
298
|
+
assert host.requests == []
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
@pytest.mark.parametrize("skills", [".claude/skills", ".codex/skills"]) # .codex: the box's Codex home
|
|
302
|
+
def test_a_trusted_install_under_home_still_works(tmp_path, skills):
|
|
303
|
+
w = Exam(tmp_path, base_config='{"enabled": true}')
|
|
304
|
+
d = w.home / skills / "pr-exam"
|
|
305
|
+
d.mkdir(parents=True)
|
|
306
|
+
shutil.copy(SCRIPT.parent.parent / "pr-exam" / "pr_exam.py", d / "pr_exam.py")
|
|
307
|
+
with FakeHost({"state": "open"}) as host:
|
|
308
|
+
p = w.exam(host.url, script=_stage(tmp_path))
|
|
309
|
+
assert p.returncode == 0, p.output
|
|
310
|
+
assert len(host.requests) == 1
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def test_an_install_committed_on_the_base_branch_is_used_not_the_working_tree_one(tmp_path):
|
|
314
|
+
w = Exam(tmp_path, base_config='{"enabled": true}')
|
|
315
|
+
real = (SCRIPT.parent.parent / "pr-exam" / "pr_exam.py").read_text()
|
|
316
|
+
base = commit(w.project, {".claude/skills/pr-exam/pr_exam.py": real}, "install pr-exam")
|
|
317
|
+
git(w.project, "update-ref", "refs/remotes/origin/main", base)
|
|
318
|
+
marker = tmp_path / "marker"
|
|
319
|
+
(w.project / ".claude/skills/pr-exam/pr_exam.py").write_text(_evil(marker)) # the PR's edit of it
|
|
320
|
+
with FakeHost({"state": "open"}) as host:
|
|
321
|
+
p = w.exam(host.url, script=_stage(tmp_path))
|
|
322
|
+
assert p.returncode == 0, p.output
|
|
323
|
+
assert not marker.exists(), "the working tree's pr_exam.py was executed"
|
|
324
|
+
assert len(host.requests) == 1
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@pytest.mark.parametrize("config", ['{"enabled": false}', INVALID_AND_OFF, '{"enabled": true}',
|
|
328
|
+
'{"enabled": "false"}', '{"bypassLabels": {}}', "[]", "{"])
|
|
329
|
+
def test_every_config_needs_the_install_to_decide_exams_turned_off_included(tmp_path, config):
|
|
330
|
+
# only pr_exam.py reads the config: without it nothing, not even `"enabled": false`, is trusted
|
|
331
|
+
w = Exam(tmp_path, base_config=config)
|
|
332
|
+
with FakeHost() as host:
|
|
333
|
+
p = w.exam(host.url, script=_stage(tmp_path))
|
|
334
|
+
assert p.returncode != 0, p.output
|
|
335
|
+
assert PR_EXAM_INSTALL in p.output
|
|
336
|
+
assert "disabled" not in p.output.lower(), p.output
|
|
337
|
+
assert host.requests == []
|