diff --git a/scripts/slot/PROTOCOL.md b/scripts/slot/PROTOCOL.md index 0717cbc99..523846bb9 100644 --- a/scripts/slot/PROTOCOL.md +++ b/scripts/slot/PROTOCOL.md @@ -230,9 +230,9 @@ The validator refuses when any of these fails, and lists every failure, not the | Result | Action | |---|---| -| write done | push `branch` to the allowed target repo against its fixed base branch (new branch, fails if it exists), open the PR (ready, not draft) with `pr_body` + `Part of fizzyedit/work#N`, labels `area:`, `kind:` from result.json (never the issue's work-kind labels), `gate:yes` if risky or targeting dvui; dvui also dispatches `review` with `{pr, repo}` to work | -| rework done | push to the PR's branch, fast-forward only (refuse if head moved), label `rework:` | -| review done | comment the findings on the PR's pr-thread issue in work (`REVIEW: clean|changes @`); commit status `agent-review` on `head_sha` (success when clean); follow-ups appended to the PR's follow-up list; `changes` dispatches `rework` (round + 1), round 3 labels `stuck` instead; `clean` runs `gh pr merge --auto` only for Fizzy, never dvui | +| write done | push `branch` to the allowed target repo against its fixed base branch (new branch, fails if it exists), open the PR (ready, not draft) with `pr_body` + `Part of fizzyedit/work#N`, labels `area:`, `kind:` from result.json (never the issue's work-kind labels), `gate:yes` if risky or targeting a gated repo; a gated repo (no bridge) also dispatches `pr` with `{pr, sha, author_kind, repo, round: 1}` to work | +| rework done | push to the PR's branch, fast-forward only (refuse if head moved), label `rework:`; a gated repo also dispatches `pr` with the rework's round | +| review done | comment the findings on the PR's pr-thread issue in work (`REVIEW: clean|changes @`), creating it if missing; the thread's title starts with `thread_title(repo, pr)`: `PR #N:` for fizzy, `PR #N:` for another repo (`PR heronin#9:`); commit status `agent-review` on `head_sha` (success when clean); follow-ups appended to the PR's follow-up list; `changes` dispatches `rework` (round + 1), round 3 labels `stuck` instead; `clean` runs `gh pr merge --auto` only for Fizzy, never dvui | | declined | comment `APPROACH:` + the decline on the issue, label `arch:needed`, remove `ready` | | failed / timeout | comment one line + the report tail on the issue; label `attempt:`; at attempt 2, `stuck` | | refused (validator) | comment the reasons on the issue; label `stuck`; nothing pushed | @@ -249,7 +249,7 @@ write.yml on: issues labeled ready | repository_dispatch write | workflow_disp permissions: {} (no GITHUB_TOKEN rights) steps: download job; slot run; upload out publish runs-on [self-hosted, publish] environment: publish if: always() && plan succeeded steps: checkout fizzy@base.sha; slot validate; create-github-app-token; slot publish -review.yml on: repository_dispatch pr {pr, sha, author_kind} (sent by fizzy's bridge) +review.yml on: repository_dispatch pr {pr, sha, author_kind, repo, round} (fizzy's bridge; publish for gated repos) run runs-on [self-hosted, slot, review-for-] rework.yml on: repository_dispatch rework {pr, round, findings | ci | conflict} run runs-on [self-hosted, slot, kind-] diff --git a/scripts/slot/common.py b/scripts/slot/common.py index 2047b6544..9e0b4003e 100644 --- a/scripts/slot/common.py +++ b/scripts/slot/common.py @@ -23,6 +23,12 @@ class JobError(ValueError): ALLOWED_REF_OWNERS = ('fizzyedit',) +def thread_title(repo, number): + """The title prefix of a PR's pr-thread issue in work: the one key from a PR to its thread.""" + name = '' if repo == 'fizzyedit/fizzy' else repo.removeprefix('fizzyedit/') + return f'PR {name}#{number}:' + + def validate_refs(refs): """Validate reference checkouts against trusted owner policy.""" try: diff --git a/scripts/slot/publish.py b/scripts/slot/publish.py index b8b2a0328..76e8877d0 100644 --- a/scripts/slot/publish.py +++ b/scripts/slot/publish.py @@ -7,7 +7,7 @@ import subprocess import tempfile -from slot.common import target +from slot.common import target, thread_title from slot.validate import gh, run @@ -91,11 +91,13 @@ def publish(job_dir, out): labels(repo, number, ['stuck']) return result = json.loads((out / 'result.json').read_bytes()) - def dispatch_review(pr): - receipt = f'' + def dispatch_review(pr, round_number, receipt_kind='review'): + receipt = f'' comments = gh('api', '--paginate', f'repos/{repo}/issues/{number}/comments') if not any(receipt in c['body'] for c in comments): - payload = dict(event_type='review', client_payload=dict(pr=pr, repo=target_repo)) + sha = gh('api', f'repos/{target_repo}/pulls/{pr}')['head']['sha'] + payload = dict(event_type='pr', client_payload=dict(pr=pr, sha=sha, author_kind=result['agent_kind'], + repo=target_repo, round=round_number)) run(os.environ.get('SLOT_GH', 'gh'), 'api', f'repos/{repo}/dispatches', '--input', '-', data=json.dumps(payload).encode()) comment(repo, number, 'Review dispatched.\n' + receipt, receipt + '\n' + marker) @@ -114,7 +116,7 @@ def dispatch_review(pr): if not any(marker in pr['body'] for pr in prs): raise ValueError('branch belongs to another job') if destination['gate']: - dispatch_review(prs[0]['number']) + dispatch_review(prs[0]['number'], 1) return push(job, out, result, job['branch'], replace=True) body = result['pr_body'] + f'\n\nPart of {repo}#{number}\n\n' + marker @@ -126,22 +128,30 @@ def dispatch_review(pr): args += ['--label', 'gate:yes'] created = gh(*args) if destination['gate']: - dispatch_review(int(created.rstrip('/').split('/')[-1])) + dispatch_review(int(created.rstrip('/').split('/')[-1]), 1) elif job['kind'] == 'rework': info = job['rework'] pr = gh('api', f'repos/{target_repo}/pulls/{info["pr"]}') comments = gh('api', '--paginate', f'repos/{target_repo}/issues/{info["pr"]}/comments') - if any(marker in c['body'] for c in comments): - return - push(job, out, result, pr['head']['ref'], job['base']['sha']) - labels(target_repo, info['pr'], [f'rework:{info["round"]}']) - comment(target_repo, info['pr'], 'Rework published.', marker) + if not any(marker in c['body'] for c in comments): + push(job, out, result, pr['head']['ref'], job['base']['sha']) + labels(target_repo, info['pr'], [f'rework:{info["round"]}']) + comment(target_repo, info['pr'], 'Rework published.', marker) + if destination['gate']: + dispatch_review(info['pr'], info['round'], 'rework-review') else: info, review = job['for_review'], result['review'] pr_number, sha = info['pr'], info['head_sha'] + title = thread_title(target_repo, pr_number) threads = gh('issue', 'list', '--repo', repo, '--label', 'pr-thread', '--state', 'all', - '--search', f'PR #{pr_number}: in:title', '--json', 'number,title') - threads = [t for t in threads if t['title'].startswith(f'PR #{pr_number}:')] + '--search', f'"{title}" in:title', '--json', 'number,title') + threads = [t for t in threads if t['title'].startswith(title)] + if not threads: + pull = gh('api', f'repos/{target_repo}/pulls/{pr_number}') + created = gh('issue', 'create', '--repo', repo, '--title', f'{title} {pull["title"][:90]}', + '--label', 'pr-thread', '--body', + f'Thread for https://github.com/{target_repo}/pull/{pr_number}. Work issue: #{number}') + threads = [dict(number=int(created.rstrip('/').split('/')[-1]))] if len(threads) != 1: raise ValueError('expected exactly one PR thread') text = (f'REVIEW: {review["verdict"]} @{sha}\nReviewer: slot/{result["agent_kind"]}\n' diff --git a/scripts/slot/slot_run.py b/scripts/slot/slot_run.py index c46e1df58..a381698ab 100644 --- a/scripts/slot/slot_run.py +++ b/scripts/slot/slot_run.py @@ -389,7 +389,7 @@ def heartbeat(): for name in ('pr-body.md', 'report.md', 'review.json', 'decline.md'): source = work / name if source.is_file(): - shutil.copyfile(source, out / 'agent' / name) + (out / 'agent' / name).write_bytes(source.read_bytes().removeprefix(b'\xef\xbb\xbf')) source.unlink() if rc == 64 or rc in config.get('auth_exit', []): raise BrokenSlot('agent not signed in') diff --git a/scripts/slot/tests/test_publish.py b/scripts/slot/tests/test_publish.py index c112731a8..cc656ba4d 100644 --- a/scripts/slot/tests/test_publish.py +++ b/scripts/slot/tests/test_publish.py @@ -154,20 +154,97 @@ def test_github_paginated_arrays_are_combined(self): self.assertEqual(gh('api', '--paginate', 'repos/a/b/issues/1/comments'), [dict(body='one'), dict(body='two')]) + def pull(self, repo, number): + self.state['objects'][f'repos/{repo}/pulls/{number}'] = dict( + state='open', title='example', head=dict(sha=self.head, ref='scripts/example'), base=dict(sha=self.sha)) + self.state['objects'][f'repos/{repo}/issues/{number}'] = dict(labels=[]) + self.state_path.write_text(json.dumps(self.state)) + + def pr_payload(self, repo, pr, round_number): + return dict(event_type='pr', client_payload=dict(pr=pr, sha=self.head, author_kind='codex', + repo=repo, round=round_number)) + + def publish_creating_threads(self): + """publish_twice with `gh issue create` answered in-process, appending to the fake's threads.""" + from slot.validate import gh as real_gh + def gh(*args): + if list(args[:2]) != ['issue', 'create']: + return real_gh(*args) + state = json.loads(self.state_path.read_text()) + title = args[args.index('--title') + 1] + state['threads'].append(dict(number=900 + len(state['threads']), title=title, + body=args[args.index('--body') + 1])) + self.state_path.write_text(json.dumps(state)) + return f'https://github.com/fizzyedit/work/issues/{state["threads"][-1]["number"]}' + with patch('slot.publish.gh', side_effect=gh): + return self.publish_twice() + def test_dvui_write_dispatches_review_once(self): self.job['base'].update(repo='fizzyedit/dvui', branch='zig-0.17') + self.pull('fizzyedit/dvui', 1) state, push = self.publish_twice() create = next(c for c in state['calls'] if c[:2] == ['pr', 'create']) self.assertEqual(create[create.index('--repo') + 1], 'fizzyedit/dvui') self.assertEqual(create[create.index('--base') + 1], 'zig-0.17') self.assertIn('gate:yes', create) - self.assertEqual(state['dispatches'], [dict(event_type='review', - client_payload=dict(pr=1, repo='fizzyedit/dvui'))]) + self.assertEqual(state['dispatches'], [self.pr_payload('fizzyedit/dvui', 1, 1)]) + self.assertEqual(push.call_count, 1) + + def test_heronin_write_dispatches_pr_once(self): + self.job['base']['repo'] = 'fizzyedit/heronin' + self.pull('fizzyedit/heronin', 1) + state, _ = self.publish_twice() + self.assertEqual(state['dispatches'], [self.pr_payload('fizzyedit/heronin', 1, 1)]) + + def test_heronin_review_comments_on_its_own_thread(self): + self.review() + self.job['base']['repo'] = 'fizzyedit/heronin' + self.pull('fizzyedit/heronin', 9) + self.state['threads'] = [dict(number=722, title='PR #9: fizzy one'), + dict(number=801, title='PR heronin#9: heronin one')] + self.state_path.write_text(json.dumps(self.state)) + state, _ = self.publish_twice() + def reviews(issue): + return [c for c in state['comments'].get(f'repos/fizzyedit/work/issues/{issue}/comments', []) + if c['body'].startswith('REVIEW:')] + self.assertEqual(len(reviews(801)), 1) + self.assertEqual(reviews(722), []) + search = next(c for c in state['calls'] if c[:2] == ['issue', 'list']) + self.assertEqual(search[search.index('--search') + 1], '"PR heronin#9:" in:title') + + def test_missing_heronin_thread_is_created_once(self): + self.review() + self.job['base']['repo'] = 'fizzyedit/heronin' + self.pull('fizzyedit/heronin', 9) + state, _ = self.publish_creating_threads() + created = [t for t in state['threads'] if t['number'] >= 900] + self.assertEqual(len(created), 1) + self.assertEqual(created[0]['title'], 'PR heronin#9: example') + self.assertEqual(created[0]['body'], + 'Thread for https://github.com/fizzyedit/heronin/pull/9. Work issue: #722') + comments = state['comments'][f'repos/fizzyedit/work/issues/{created[0]["number"]}/comments'] + self.assertEqual(len([c for c in comments if c['body'].startswith('REVIEW:')]), 1) + + def test_dvui_rework_dispatches_review_round_once(self): + self.job.update(kind='rework', base=dict(repo='fizzyedit/dvui', sha=self.head), + rework=dict(pr=9, round=2)) + self.pull('fizzyedit/dvui', 9) + state, push = self.publish_twice() self.assertEqual(push.call_count, 1) + self.assertEqual(state['dispatches'], [self.pr_payload('fizzyedit/dvui', 9, 2)]) + + def test_fizzy_rework_dispatches_nothing(self): + self.job.update(kind='rework', base=dict(repo='fizzyedit/fizzy', sha=self.head), + rework=dict(pr=9, round=2)) + self.pull('fizzyedit/fizzy', 9) + state, _ = self.publish_twice() + self.assertNotIn('dispatches', state) def test_dvui_clean_review_never_auto_merges(self): self.review() self.job['base']['repo'] = 'fizzyedit/dvui' + self.state['threads'] = [dict(number=722, title='PR dvui#9: example')] + self.state_path.write_text(json.dumps(self.state)) self.result['review'].update(verdict='clean', findings=[]) state, _ = self.publish_twice() self.assertIn('repos/fizzyedit/dvui/statuses/' + self.head, state['statuses']) @@ -194,13 +271,15 @@ def test_dvui_existing_pr_resumes_review_dispatch(self): self.job['base']['repo'] = 'fizzyedit/dvui' self.state['prs'] = [dict(number=12, branch=self.job['branch'], body=f'')] - self.state_path.write_text(json.dumps(self.state)) + self.pull('fizzyedit/dvui', 12) state, push = self.publish_twice() push.assert_not_called() - self.assertEqual(state['dispatches'][0]['client_payload'], dict(pr=12, repo='fizzyedit/dvui')) + self.assertEqual(state['dispatches'], [self.pr_payload('fizzyedit/dvui', 12, 1)]) def test_dvui_changes_dispatch_carries_repo(self): self.review() self.job['base']['repo'] = 'fizzyedit/dvui' + self.state['threads'] = [dict(number=722, title='PR dvui#9: example')] + self.state_path.write_text(json.dumps(self.state)) state, _ = self.publish_twice() self.assertEqual(state['dispatches'][0]['client_payload']['repo'], 'fizzyedit/dvui') diff --git a/scripts/slot/tests/test_slot_run.py b/scripts/slot/tests/test_slot_run.py index 7598ea6bb..439005d8c 100644 --- a/scripts/slot/tests/test_slot_run.py +++ b/scripts/slot/tests/test_slot_run.py @@ -333,6 +333,19 @@ def test_read_agent_drops_leading_bom(self): (out / 'agent' / 'result.json').write_bytes(b'\xef\xbb\xbf{"ok": true}') self.assertEqual(json.loads(slot_run.read_agent(out, 'result.json')), dict(ok=True)) + def test_bom_led_review_json_is_copied_without_bom(self): + agent = self.root / 'bom_review_agent.py' + agent.write_text("from pathlib import Path\n" + "import json, sys\n" + "prompt = sys.stdin.read()\n" + "path = Path(prompt.split('Review path: ', 1)[1].splitlines()[0])\n" + "review = json.dumps(dict(verdict='clean', findings=[], follow_ups=[]))\n" + "path.write_bytes(b'\\xef\\xbb\\xbf' + review.encode())\n") + self.job['kind'] = 'review' + self.assertEqual(self.run_slot(agent=agent), 0, self.detail) + self.assertEqual(self.result('done')['review']['verdict'], 'clean') + self.assertTrue((self.out / 'agent/review.json').read_bytes().startswith(b'{')) + def test_execute_resolves_command_through_child_path(self): bin_dir = Path(self.temp.name) / 'pathbin' bin_dir.mkdir()