thirdshift 0.20.5

Turn a GitHub issue into a ready-for-review pull request, unattended
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! Pull-request Processes share eligibility, captured-head checkout, session
//! execution and comment delivery. Recommend-only never publishes branch work.

use std::io::Write;
use std::os::unix::fs::OpenOptionsExt;

use super::*;
use crate::args::ReviewArgs;
use crate::github::review::ReviewPullRequest;
use crate::review::{Mode, PullRequestUrl, Reservation};

mod delivery;

pub(crate) fn run(command: ReviewArgs) -> Result<String> {
    let (pr, reservation) = match command.target {
        crate::review::Target::PullRequest(pr) => (pr, None),
        crate::review::Target::Author(author) => {
            interrupt::install()?;
            let config = UserConfig::load()?;
            logs::begin(Begin::ReviewRun { pass: true });
            logs::configured(&config);
            let launch = Git::new(std::env::current_dir()?);
            let repo =
                crate::issue::Repo::of_origin(&launch.run(&["config", "remote.origin.url"])?)
                    .context("review --author needs a GitHub origin")?;
            logs::pass_fired(&repo);
            if let Some(running) = command_limit::hold_pass(
                &repo,
                config.command_limit,
                command_limit::Sharing::UpToTheLimit,
            )? {
                logs::skipped(logs::Pass::Review, &repo, &running);
                progress::step(&running);
                return Ok(String::new());
            }
            let github = GitHub::new();
            let mut selected = None;
            for pr in github.waiting_review_pull_requests(&repo, &author, None)? {
                if let Some(held) = Reservation::take(&launch, &github, &pr)?
                    && github
                        .waiting_review_pull_requests(&repo, &author, Some(pr.issue.number))?
                        .contains(&pr)
                {
                    selected = Some((pr, Some(held)));
                    break;
                }
            }
            let Some(selected) = selected else {
                let reason = format!("no waiting pull request by {author} on {}", repo.slug());
                logs::skipped(logs::Pass::Review, &repo, &reason);
                progress::step(reason);
                return Ok(String::new());
            };
            selected
        }
    };
    ReviewProcess::built_in()?.execute(&pr, &command.flags, command.mode, false, reservation)
}

pub(super) fn custom(command: &ProcessRun, checking: bool) -> Result<String> {
    ensure!(
        command.issue.is_none(),
        "a Process takes an issue or a pull request, not both"
    );
    ensure!(
        command.goal != Some(Goal::Merged)
            && command.security_fix.is_none()
            && command.security_close.is_none()
            && !command.plan_only,
        "merge, security-fix, security-close and plan-only flags do not apply to pull-request Processes"
    );
    let process = if checking && command.name == "review-run" {
        ReviewProcess::built_in()?
    } else {
        let (path, file, identity) = load(&command.name)?;
        ensure!(
            has_pull_request(&file),
            "{} needs a pull-request worktree to take a pull request",
            path.display()
        );
        ReviewProcess {
            name: command.name.clone(),
            path,
            file,
            identity,
            built_in: false,
        }
    };
    process.execute(
        command
            .pull_request
            .as_ref()
            .context("pull-request input is required")?,
        &Flags {
            harness: command.asked.clone(),
            email: command.email.clone(),
            security_review: command.security_review,
            ..Flags::default()
        },
        Mode::Address,
        checking,
        None,
    )
}

struct ReviewProcess {
    name: String,
    path: PathBuf,
    file: ProcessFile,
    identity: Identity,
    built_in: bool,
}

impl ReviewProcess {
    fn built_in() -> Result<Self> {
        let file = load_inherited_builtin("review-run")?;
        ensure!(
            file.steps.len() == 3
                && matches!(file.steps[0].kind, DraftKind::ReviewAndAddress { .. })
                && matches!(file.steps[1].kind, DraftKind::SecurityReview { .. })
                && matches!(file.steps[2].kind, DraftKind::Delivery { .. }),
            "review-run needs review-and-address, security-review and delivery"
        );
        Ok(Self {
            name: "review-run".into(),
            path: "processes/review-run.toml".into(),
            file,
            identity: Identity::built_in("review-run", "Review run"),
            built_in: true,
        })
    }

    fn execute(
        &self,
        pr: &PullRequestUrl,
        flags: &Flags,
        mode: Mode,
        checking: bool,
        reservation: Option<Reservation>,
    ) -> Result<String> {
        interrupt::install()?;
        let config = UserConfig::load()?;
        let security_review = mode == Mode::Address && flags.security_review(&config);
        let launch = Git::new(std::env::current_dir()?);
        let origin = launch.run(&["config", "remote.origin.url"])?;
        ensure!(
            pr.issue.matches_origin(&origin),
            "origin mismatch: {} is not in {origin}",
            pr.url
        );
        let repo = pr.issue.repo();
        let mut command = CommandChoice::of(&flags.harness, &config.harness);
        if checking {
            let snapshot = GitHub::new().review_pull_request(pr)?;
            launch.fetch(&[
                &snapshot.branch,
                &snapshot.base,
                &snapshot.head,
                &snapshot.base_head,
            ])?;
            let checkout =
                SkillWorktree::on_pull_request(&launch, &snapshot.branch, &snapshot.head)?;
            let steps = self.resolve(checkout.path(), &mut command, security_review, &config)?;
            return Ok(steps
                .iter()
                .map(|step| {
                    format!(
                        "{}; {}; harness: {} ({}), model: {} ({}), effort: {} ({})\n",
                        step_plan(step, true),
                        step.choice,
                        step.choice.harness.name(),
                        step.sources.harness,
                        step.choice.model.as_deref().unwrap_or("default"),
                        step.sources.model,
                        step.choice.effort.as_deref().unwrap_or("default"),
                        step.sources.effort
                    )
                })
                .collect());
        }
        let mut notification = RunNotification::new(
            flags.notification(&config),
            &config.email,
            if self.built_in {
                About::ReviewRun
            } else {
                About::Process { name: &self.name }
            },
        )?;
        if reservation.is_none() {
            logs::begin(if self.built_in {
                Begin::ReviewRun { pass: false }
            } else {
                Begin::Process {
                    name: &self.name,
                    pass: false,
                }
            });
            logs::configured(&config);
        }
        logs::process_loaded(self.identity.clone());
        if reservation.is_none() {
            command_limit::hold_slot(&repo);
        }
        logs::started(
            if self.built_in {
                Work::ReviewRun(&repo)
            } else {
                Work::Process {
                    repo: &repo,
                    name: &self.name,
                }
            },
            &command.choice,
        );
        let mut last_log = None;
        let mut delivered = false;
        let result = (|| {
            let github = GitHub::new();
            // Validate before reserving, so an ineligible explicit PR changes
            // no label. Capture again after reservation for the actual run.
            github.review_pull_request(pr)?;
            let _reservation = match reservation {
                Some(held) => held,
                None => Reservation::take(&launch, &github, pr)?
                    .context("pull request is already reserved or labelled in-progress")?,
            };
            let snapshot = github.review_pull_request(pr)?;
            logs::issue_branch(Some(&snapshot.branch));
            logs::pull_request(&pr.url);
            progress::step(format_args!("working on pull request {}", pr.url));
            launch.fetch(&[
                &snapshot.branch,
                &snapshot.base,
                &snapshot.head,
                &snapshot.base_head,
            ])?;
            let mut checkout =
                SkillWorktree::on_pull_request(&launch, &snapshot.branch, &snapshot.head)?;
            let steps = self.resolve(checkout.path(), &mut command, security_review, &config)?;
            notification.built_with(&command.choice);
            let git = Git::new(checkout.path());
            skills::exclude(&git, "/.thirdshift-review-input.md")?;
            let input = github.review_input(pr, &snapshot)?;
            // The checked-out branch chooses this path. A symlink here would
            // send the packet write outside the worktree.
            write_review_input(
                &checkout.path().join(".thirdshift-review-input.md"),
                &input.markdown,
            )?;
            let results = step_results_dir(None)?;
            let mut earlier = Vec::new();
            let mut began = Vec::new();
            let mut findings = None;
            let mut validation_head = None;
            let mut last = String::new();
            // Captured before any session. A session can read a private
            // Security report and delete it, and its final message is posted.
            let earlier_private_reports = crate::security::review::private_report_material(&repo);
            for (index, step) in steps.iter().enumerate() {
                crate::resources::wait()?;
                // Once the captured head has been reviewed, later Steps still
                // work on that checkout. A remote head move must reach Delivery
                // so it can post the findings while withholding the fixes.
                let reviewed = findings.is_some();
                let before_each = || {
                    if reviewed && mode == Mode::Address {
                        compatible(&github, pr, &snapshot).map(|_| ())
                    } else {
                        unchanged(&github, pr, &snapshot)
                    }
                };
                if mode == Mode::Address && matches!(step.kind, StepKind::Delivery) {
                    compatible(&github, pr, &snapshot)?;
                } else if security_review || !matches!(step.kind, StepKind::SecurityReview) {
                    before_each()?;
                }
                let started = Instant::now();
                let began_at = Utc::now();
                progress::step(format_args!(
                    "running step {} with {}",
                    step.name, step.choice
                ));
                let outcome = (|| {
                    Ok(match &step.kind {
                        StepKind::ReviewAndAddress | StepKind::Skill { .. } => {
                            let reviewing = matches!(step.kind, StepKind::ReviewAndAddress);
                            if reviewing {
                                unchanged_checkout(&git, &snapshot)?;
                            }
                            let text = if let StepKind::Skill { name, path } = &step.kind {
                                prompt::process_step(
                                    &self.name,
                                    &step.name,
                                    name,
                                    path,
                                    prompt::EarlierSteps {
                                        results: &earlier,
                                        first: index == 0,
                                    },
                                    None,
                                    prompt::ProcessWorktree::PullRequest {
                                        url: pr,
                                        base: &snapshot.base_head,
                                    },
                                )
                            } else {
                                prompt::pull_request_review(
                                    pr,
                                    &snapshot.base_head,
                                    mode,
                                    config.review.commands_for(&repo),
                                    prompt::EarlierSteps {
                                        results: &earlier,
                                        first: index == 0,
                                    },
                                )
                            };
                            let logs = session::Logs::of_process_review(&repo, index + 1);
                            let (result, log) = session::Sessions::checked_within(
                                &logs,
                                checkout.path(),
                                &step.choice,
                                None,
                                None,
                                &before_each,
                                |sessions| {
                                    sessions.run_to_final_message(
                                        if reviewing {
                                            session::Purpose::CodeReview
                                        } else {
                                            session::Purpose::Ordinary
                                        },
                                        if reviewing {
                                            session::Kind::CodeReview
                                        } else {
                                            session::Kind::ProcessStep(index + 1)
                                        },
                                        &text,
                                    )
                                },
                            );
                            last_log = log;
                            let mut message = result?
                                .filter(|text| !text.trim().is_empty())
                                .context("session left no final report")?;
                            if reviewing {
                                validation_head = Some(git.run(&["rev-parse", "HEAD"])?);
                                // The validation section is not the only text
                                // Delivery posts. This report is posted too.
                                message = withhold_private(
                                    message,
                                    &[&earlier_private_reports],
                                    "Review report withheld: it included private Security review material.",
                                );
                                findings = Some(message.clone());
                            }
                            message
                        }
                        StepKind::Command(text) => run_command(
                            checkout.path(),
                            text,
                            &results,
                            &results.join(format!("{}.txt", step.name)),
                            Some(CommandInput::PullRequest(&pr.url)),
                        )?,
                        StepKind::SecurityReview => {
                            if !security_review {
                                "Security review skipped: not requested".to_string()
                            } else {
                                let text = prompt::pull_request_security_review(
                                    pr,
                                    &snapshot.base,
                                    &snapshot.branch,
                                );
                                let logs = session::Logs::of_process_review(&repo, index + 1);
                                let (review, log) = session::Sessions::checked_within(
                                    &logs,
                                    checkout.path(),
                                    &step.choice,
                                    None,
                                    step.review_default.as_ref(),
                                    &before_each,
                                    |sessions| {
                                        crate::security::review::run_on_pull_request(
                                            sessions,
                                            &checkout,
                                            pr,
                                            &snapshot.base_head,
                                            &text,
                                        )
                                    },
                                );
                                last_log = log.clone();
                                let entry =
                                    crate::security::review::Outcome::of(review, log).entry();
                                let current_private_reports =
                                    crate::security::review::private_report_material(&repo);
                                let entry = withhold_private(
                                    entry,
                                    &[&earlier_private_reports, &current_private_reports],
                                    "Security review results withheld: the report included private Security review material.",
                                );
                                let report = findings
                                    .as_mut()
                                    .context("Security review needs a review report")?;
                                report.push_str(&format!("\n\n### Security\n\n{entry}"));
                                entry
                            }
                        }
                        StepKind::Delivery => {
                            let head = git.run(&["rev-parse", "HEAD"])?;
                            if mode == Mode::Address
                                && validation_head.as_deref() != Some(head.as_str())
                            {
                                let text = prompt::pull_request_validation(
                                    pr,
                                    &head,
                                    config.review.commands_for(&repo),
                                );
                                let logs = session::Logs::of_process_review(&repo, index + 1);
                                let before_validation =
                                    || compatible(&github, pr, &snapshot).map(|_| ());
                                // Captured before the session. It can read and
                                // then remove the report, and Delivery posts
                                // its final message. A prompt not to copy the
                                // report is not a boundary.
                                let private_reports =
                                    crate::security::review::private_report_material(&repo);
                                let (result, log) = session::Sessions::checked_within(
                                    &logs,
                                    checkout.path(),
                                    &step.choice,
                                    None,
                                    None,
                                    &before_validation,
                                    |sessions| {
                                        sessions.run_to_final_message(
                                            session::Purpose::Ordinary,
                                            session::Kind::ProcessStep(index + 1),
                                            &text,
                                        )
                                    },
                                );
                                last_log = log;
                                let validation = result?
                                    .filter(|text| !text.trim().is_empty())
                                    .context("validation session left no final report")?;
                                ensure!(
                                    git.run(&["rev-parse", "HEAD"])? == head,
                                    "validation session changed the final Review run head"
                                );
                                let validation = withhold_private(
                                    validation,
                                    &[&earlier_private_reports, &private_reports],
                                    "Validation results withheld: the report included private Security review material.",
                                );
                                findings
                                    .as_mut()
                                    .context("delivery needs a review report")?
                                    .push_str(&format!(
                                        "\n\n### Final validation at {head}\n\n{validation}"
                                    ));
                            }
                            let findings = findings
                                .as_deref()
                                .context("delivery needs a review report")?;
                            delivery::Delivery {
                                git: &git,
                                github: &github,
                                pr,
                                snapshot: &snapshot,
                                mode,
                                input: &input,
                            }.run(findings, self.file.limits.repair_cap, |kind, failed, expected| {
                                let text = prompt::pull_request_ci_fix_repair(
                                    pr, &expected.base, &expected.branch, failed,
                                    config.review.commands_for(&repo),
                                );
                                let logs = session::Logs::of_process_review(&repo, index + 1);
                                let before_repair = || unchanged(&github, pr, expected);
                                let private_reports = crate::security::review::private_report_material(&repo);
                                let (result, log) = session::Sessions::checked_within(
                                    &logs, checkout.path(), &step.choice, None, None,
                                    &before_repair,
                                    |sessions| sessions.run_to_final_message(session::Purpose::Ordinary, kind, &text),
                                );
                                last_log = log;
                                let entry = result?.filter(|text| !text.trim().is_empty())
                                    .context("CI-fix Repair left no final report")?;
                                unchanged(&github, pr, expected)?;
                                let current_private_reports = crate::security::review::private_report_material(&repo);
                                Ok(withhold_private(entry,
                                    &[&earlier_private_reports, &private_reports, &current_private_reports],
                                    "Repair results withheld: the report included private Security review material.",
                                ))
                            })?;
                            delivered = true;
                            pr.url.clone()
                        }
                        StepKind::Implement
                        | StepKind::ArchitectureReview
                        | StepKind::SpecReview
                        | StepKind::SecurityAudit
                        | StepKind::ReproduceFindings
                        | StepKind::SelfMerge
                        | StepKind::RunPublishedIssue { .. }
                        | StepKind::LandTickets { .. }
                        | StepKind::FixFindings { .. }
                        | StepKind::CloseFindings => {
                            bail!("unsupported pull-request Step {}", step.name)
                        }
                    })
                })();
                finish_step(
                    step,
                    started.elapsed(),
                    (began_at, outcome),
                    &results,
                    &mut earlier,
                    &mut began,
                    &mut last,
                )?;
            }
            if delivered {
                checkout.discard_validation_artifacts();
            }
            Ok(last)
        })();
        skills::remove_written();
        let outcome = if result.is_ok() {
            if delivered {
                "ready for review"
            } else {
                "succeeded"
            }
        } else if interrupt::requested() {
            "interrupted"
        } else {
            "failed"
        };
        let account = Account {
            recovery: None,
            outcome,
            ended: match &result {
                Err(error) => Err(Cause::of(error)),
                Ok(_) => Ok(format!("{outcome}: {}", pr.url)),
            },
            interrupted: outcome == "interrupted",
            pr_url: Some(&pr.url),
            advice: &[],
            base_fix: None,
            log: last_log.as_deref(),
            ticket_lines: &[],
            review: None,
            triage: None,
            failed_reproduction: None,
            pre_audit_failure: None,
            security_findings: None,
            security_findings_waiting: false,
            security_fix: None,
            security_close: None,
            urls: vec![&pr.url],
        };
        logs::finished(&account);
        logs::ended(outcome);
        notification.finish(&account);
        result
    }

    fn resolve(
        &self,
        checkout: &Path,
        command: &mut CommandChoice,
        security_review: bool,
        config: &UserConfig,
    ) -> Result<Vec<ResolvedStep>> {
        let mut steps = self
            .file
            .steps
            .iter()
            .map(|step| resolve(&self.path, step, checkout, command))
            .collect::<Result<Vec<_>>>()?;
        command.check_steps(&mut steps);
        for step in &mut steps {
            if matches!(step.kind, StepKind::SecurityReview) {
                step.check_security_review(security_review, config);
            }
        }
        for step in &steps {
            if let Some(failure) = &step.start_failure {
                bail!("step {} cannot start: {failure}", step.name);
            }
        }
        Ok(steps)
    }
}

/// Session output is posted on the pull request. A prompt not to copy a
/// private Security report is not a boundary, so any stored report fragment
/// replaces the whole public text.
fn withhold_private(
    text: String,
    reports: &[&crate::security::review::PrivateReportMaterial],
    withheld: &str,
) -> String {
    if reports.iter().any(|report| report.exposes(&text)) {
        withheld.to_string()
    } else {
        text
    }
}

fn write_review_input(path: &Path, markdown: &str) -> Result<()> {
    let replacing = match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => {
            bail!(
                "review input {} is a symlink; refusing to follow it",
                path.display()
            );
        }
        Ok(metadata) if metadata.is_file() => true,
        Ok(_) => bail!("review input {} is not a regular file", path.display()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => false,
        Err(error) => {
            return Err(error)
                .with_context(|| format!("could not inspect review input {}", path.display()));
        }
    };
    let mut options = fs::OpenOptions::new();
    options
        .write(true)
        .custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC);
    if replacing {
        options.truncate(true);
    } else {
        options.create_new(true).mode(0o600);
    }
    let mut file = options.open(path).with_context(|| {
        format!(
            "refusing to follow a symlink for the review input {}",
            path.display()
        )
    })?;
    file.write_all(markdown.as_bytes())?;
    Ok(())
}

fn unchanged(github: &GitHub, pr: &PullRequestUrl, snapshot: &ReviewPullRequest) -> Result<()> {
    let current = compatible(github, pr, snapshot)?;
    ensure!(
        current.head == snapshot.head,
        "pull request changed during Review run"
    );
    Ok(())
}

/// Delivery may report a moved head, but never publish to a changed base,
/// branch or an ineligible pull request.
fn compatible(
    github: &GitHub,
    pr: &PullRequestUrl,
    snapshot: &ReviewPullRequest,
) -> Result<ReviewPullRequest> {
    let current = github.review_pull_request(pr)?;
    ensure!(
        current.base_head == snapshot.base_head
            && current.base == snapshot.base
            && current.branch == snapshot.branch,
        "pull request changed during Review run"
    );
    Ok(current)
}

fn unchanged_checkout(git: &Git, snapshot: &ReviewPullRequest) -> Result<()> {
    ensure!(
        git.run(&["rev-parse", "HEAD"])? == snapshot.head
            && git.run(&["branch", "--show-current"])? == snapshot.branch
            && git
                .run(&["status", "--porcelain", "--untracked-files=no"])?
                .is_empty(),
        "recommend-only session changed the branch; no comment posted"
    );
    Ok(())
}

pub(super) fn has_pull_request(file: &ProcessFile) -> bool {
    file.worktrees
        .iter()
        .any(|worktree| worktree.kind == WorktreeKind::PullRequest)
}

pub(super) fn validate(path: &Path, file: &ProcessFile) -> Result<()> {
    if !has_pull_request(file) {
        if let Some(line) = file.steps.iter().find_map(|step| {
            if let DraftKind::ReviewAndAddress { line } = step.kind {
                Some(line)
            } else {
                None
            }
        }) {
            return Err(rule(
                path,
                line,
                "review-and-address",
                "review-and-address needs a pull-request worktree",
            ));
        }
        return Ok(());
    }
    ensure!(
        file.worktrees.len() == 1,
        "{}: a pull-request Process needs exactly one pull-request worktree",
        path.display()
    );
    let worktree = &file.worktrees[0].name;
    let mut reviewed = false;
    for (index, step) in file.steps.iter().enumerate() {
        let reason = if step.worktree.as_ref().map(|(name, _)| name) != Some(worktree)
            || step.optional
            || step.parallel.is_some()
            || step.process.is_some()
        {
            Some(
                "pull-request Steps must use the pull-request worktree, be required and sequential, and cannot dispatch a child Process",
            )
        } else {
            match step.kind {
                DraftKind::ReviewAndAddress { .. } if !reviewed => {
                    reviewed = true;
                    None
                }
                DraftKind::SecurityReview { .. }
                    if reviewed
                        && file.steps[index + 1..].iter().all(|later| {
                            matches!(
                                later.kind,
                                DraftKind::SecurityReview { .. } | DraftKind::Delivery { .. }
                            )
                        }) =>
                {
                    None
                }
                DraftKind::Skill { .. } | DraftKind::Command { .. } => None,
                DraftKind::Delivery { .. } if reviewed && index + 1 == file.steps.len() => None,
                DraftKind::ReviewAndAddress { .. }
                | DraftKind::Delivery { .. }
                | DraftKind::Implement { .. }
                | DraftKind::ArchitectureReview { .. }
                | DraftKind::SpecReview { .. }
                | DraftKind::SecurityReview { .. }
                | DraftKind::SecurityAudit { .. }
                | DraftKind::ReproduceFindings { .. }
                | DraftKind::SelfMerge { .. }
                | DraftKind::RunPublishedIssue { .. }
                | DraftKind::LandTickets { .. }
                | DraftKind::FixFindings { .. }
                | DraftKind::CloseFindings { .. } => Some(
                    "pull-request Processes support skill, command, one review-and-address, security-review after the editing Steps, and delivery as the final Step after a review",
                ),
            }
        };
        if let Some(reason) = reason {
            return Err(rule(path, step.name_line, "pull-request", reason));
        }
    }
    Ok(())
}