Skip to main content

magi/
land.rs

1//! Landing the winner: watch the pull request, fix what it complains about,
2//! and merge it.
3//!
4//! Opening the pull request used to be where magi stopped and the operator
5//! started: watch the checks, read what the review bots found, push a fix,
6//! wait again, merge. That loop is mechanical, it takes an hour of wall-clock
7//! time per pull request, and doing it by hand six times in one session is how
8//! a queue that drains unattended stops being unattended. So it lives here.
9//!
10//! # Shape
11//!
12//! [`PrState`] is one observation of a pull request and [`decide`] is the whole
13//! policy as a *pure* function of it. Nothing in [`decide`] talks to `gh`,
14//! which is what makes "green with an unresolved comment is a fix, not a merge"
15//! an assertion in a test rather than a claim in a comment. [`land`] is the
16//! only part that performs I/O: observe, decide, act, repeat.
17//!
18//! # What it refuses to do
19//!
20//! Merging is the one irreversible thing magi can do to a repository, so the
21//! loop is built to stop rather than to guess:
22//!
23//! * A red pull request is never merged. When the budget runs out the pull
24//!   request is left open with a comment naming what is still failing, because
25//!   a magi that force-merges a red pull request is worse than one that stops.
26//! * A pull request whose checks cannot be read at all (`gh` reported no
27//!   rollup) is not merged either. Landing is for repositories with CI; with no
28//!   signal there is nothing to be green.
29//! * A pull request a human merged or closed underneath us is
30//!   [`Step::Done`] - the person won, and their decision is not an error.
31//!
32//! # Why `--subject` is not optional
33//!
34//! A candidate branch holds one commit whose subject is
35//! `magi: candidate A (uncommitted work)`, and `gh pr merge --squash` prefers a
36//! single commit's message over the pull request title. Merging without
37//! [`merge_argv`]'s explicit `--subject` therefore writes a `main` history that
38//! says nothing about what landed. `AGENTS.md` records the trap; this module is
39//! where it is prevented.
40
41use std::collections::{BTreeMap, BTreeSet};
42use std::fmt::Write as _;
43use std::path::{Path, PathBuf};
44use std::sync::Arc;
45use std::time::Duration;
46
47use anyhow::{Context as _, Result, bail};
48use jiff::Timestamp;
49use serde::{Deserialize, Serialize};
50
51use crate::agent::{self, Invocation, SeatState};
52use crate::ask;
53use crate::config::MergeMode;
54use crate::git;
55use crate::proc::Quiet as _;
56use crate::prompt;
57use crate::run::{
58    ContestedHandoff, LandApproval, LandReruns, MergeOutcome, RunState, RunStatus, tail,
59};
60
61/// How often the pull request is re-read while its checks are still running.
62///
63/// Thirty seconds: a CI matrix takes minutes, so anything shorter is spent
64/// entirely on `gh` invocations, and anything much longer adds latency to every
65/// single round of a loop that already waits for agents.
66pub const POLL: Duration = Duration::from_secs(30);
67
68/// How long one wait may last before landing gives up on the checks finishing.
69///
70/// A workflow that has not settled in forty-five minutes is stuck on a runner
71/// queue, a missing approval, or a hung job - none of which more polling fixes,
72/// and all of which a person needs to see.
73pub const WAIT_CEILING: Duration = Duration::from_secs(45 * 60);
74
75/// How long the checks may stay unreadable before landing gives up on them.
76///
77/// GitHub registers a workflow run some seconds after the branch is pushed, so
78/// immediately after a pull request is opened "no checks" and "no CI in this
79/// repository" look identical. Measured on run 01c2: magi opened pull request
80/// 22, read `unknown` four seconds later, refused to merge on a guess and
81/// marked the run blocked - and every check on that pull request was green
82/// minutes afterwards, with the whole competition then re-run from scratch for
83/// a task that was already finished. Three minutes is well past the observed
84/// registration delay and still bounded, so a repository that genuinely has no
85/// checks costs one three-minute wait and then says so.
86pub const CHECKS_GRACE: Duration = Duration::from_secs(3 * 60);
87
88/// Bytes of failing log kept per check. The fixer needs the assertion and the
89/// frame around it, not the forty thousand lines of `cargo` output above it.
90const LOG_TAIL: usize = 4_000;
91
92/// Failing checks whose logs are fetched. Beyond a handful the failures share a
93/// cause, and fetching each one costs a `gh` round trip.
94const MAX_LOGS: usize = 3;
95
96/// Marker carried by every comment magi posts on a pull request.
97///
98/// Without it magi's own "still failing" comment is indistinguishable from a
99/// reviewer's, and the next observation would hand magi's own prose to the
100/// fixer as a finding.
101pub const MARKER: &str = "<!-- magi:land -->";
102
103/// Markers a bot puts in a comment to say that the comment is not a review.
104///
105/// CodeRabbit labels its own machinery in HTML comments - the trigger notice,
106/// the walkthrough summary, the "thanks for using" footer - and its actual
107/// findings arrive as *inline* review comments with a path and a line. Taking
108/// the bot at its word is more honest than guessing from prose, and it is the
109/// difference between a fix round that has something to fix and one that asks
110/// an agent to act on a quota notice.
111const NOT_A_REVIEW: [&str; 3] = [
112    "skip review by coderabbit.ai",
113    "summarize by coderabbit.ai",
114    "<!-- tips_start -->",
115];
116
117/// Where a pull request is in its life.
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119pub enum PrLifecycle {
120    /// Still ours to land.
121    Open,
122    /// Already merged, by us or by a person.
123    Merged,
124    /// Closed without merging.
125    Closed,
126}
127
128/// The aggregate verdict of a pull request's checks.
129#[derive(Debug, Clone, Copy, PartialEq, Eq)]
130pub enum Checks {
131    /// At least one check has not finished.
132    Pending,
133    /// Every check passed (a skipped check counts as passed: the review
134    /// workflow skips release and bot pull requests by design).
135    Green,
136    /// At least one check finished without passing.
137    Red,
138    /// Nothing readable - no rollup at all, or a status magi does not know.
139    Unknown,
140}
141
142impl PrLifecycle {
143    /// Stable lower-case name, as the API and the reports spell it.
144    pub fn as_str(self) -> &'static str {
145        match self {
146            Self::Open => "open",
147            Self::Merged => "merged",
148            Self::Closed => "closed",
149        }
150    }
151}
152
153impl Checks {
154    /// Stable lower-case name, as the API and the reports spell it.
155    pub fn as_str(self) -> &'static str {
156        match self {
157            Self::Pending => "pending",
158            Self::Green => "green",
159            Self::Red => "red",
160            Self::Unknown => "unknown",
161        }
162    }
163}
164
165/// One outstanding review comment, human or bot.
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct ReviewComment {
168    /// Login of whoever wrote it.
169    pub author: String,
170    /// File it was left on, for inline review comments.
171    pub path: Option<String>,
172    /// Line it was left on, when the comment is inline and still anchored.
173    pub line: Option<u64>,
174    /// The comment itself, as written.
175    pub body: String,
176}
177
178/// One observation of a pull request.
179#[derive(Debug, Clone, PartialEq, Eq)]
180pub struct PrState {
181    /// Pull request url, as `gh` reports it.
182    pub url: String,
183    /// Pull request number.
184    pub number: u64,
185    /// Open, merged, or closed.
186    pub state: PrLifecycle,
187    /// Aggregate check verdict.
188    pub checks: Checks,
189    /// Names of the checks that finished without passing.
190    pub failing: Vec<String>,
191    /// Comments that still want an answer, human and bot.
192    pub review_comments: Vec<ReviewComment>,
193    /// Whether the forge itself considers the failures blocking.
194    pub blocking: Blocking,
195}
196
197/// Whether a failing check actually stands between the pull request and
198/// `main`, according to the forge.
199///
200/// The rollup lists every check equally, so `coverage` going red on a
201/// repository that deliberately does not require it looked exactly like a
202/// broken build - and magi answered by spending a fix round on a change that
203/// was fine. Pull request 37 had to be merged by hand for that reason: the
204/// only red check was `editorconfig`, which was failing because the *action*
205/// could not fetch its own binary, and which the repository does not require.
206///
207/// `mergeStateStatus` is where GitHub applies the required-check set, so it
208/// is the one field that can tell the difference.
209#[derive(Debug, Clone, Copy, PartialEq, Eq)]
210pub enum Blocking {
211    /// Required checks are satisfied and the branch merges cleanly.
212    No,
213    /// Something required is failing or missing.
214    Yes,
215    /// The branch no longer merges: the base moved under it.
216    Conflict,
217    /// The forge did not say - an older `gh`, or a token without the scope.
218    /// Treated as `Yes`, because refusing to guess is the rule everywhere
219    /// else in this module.
220    Unsaid,
221}
222
223impl Blocking {
224    /// Read `mergeStateStatus`, which is upper-case in `gh`'s output.
225    fn of(raw: &str) -> Self {
226        match raw.to_ascii_uppercase().as_str() {
227            // Mergeable. `UNSTABLE` is the interesting one: mergeable, with a
228            // non-required check failing or still running.
229            "CLEAN" | "UNSTABLE" | "HAS_HOOKS" => Self::No,
230            "DIRTY" => Self::Conflict,
231            "" | "UNKNOWN" => Self::Unsaid,
232            // BLOCKED, BEHIND, DRAFT: something has to change first.
233            _ => Self::Yes,
234        }
235    }
236
237    /// Does this stand between the pull request and the base branch?
238    #[must_use]
239    pub fn stops_a_merge(self) -> bool {
240        !matches!(self, Self::No)
241    }
242}
243
244/// What the loop decided to do next. Pure, so the policy is testable.
245#[derive(Debug, Clone, PartialEq, Eq)]
246pub enum Step {
247    /// Checks are still running; re-read the pull request after [`POLL`].
248    Wait,
249    /// The base moved and the branch no longer merges: rebase it.
250    ///
251    /// Not a fix round. Nothing is wrong with the change - a competition
252    /// that runs for two hours against a repository merging pull requests
253    /// all day conflicts on the way in, and that is arithmetic rather than a
254    /// defect. Pull requests 35 and 37 were both rebased by hand for exactly
255    /// this.
256    Rebase,
257    /// Red checks or unresolved comments; run a fix round.
258    Fix {
259        /// What is unhappy, in one line, for the run log and the fix prompt.
260        reason: String,
261    },
262    /// Green and nothing outstanding; merge it.
263    Merge,
264    /// The pull request left our hands.
265    Done {
266        /// Did it land, or was it closed?
267        merged: bool,
268    },
269    /// Stop and leave the pull request to a person.
270    GiveUp {
271        /// Why magi stopped, in one line.
272        reason: String,
273    },
274}
275
276/// The outcome to record when `gh pr merge` exits non-zero, given what the
277/// pull request looked like immediately afterwards.
278///
279/// `gh pr merge` merges server-side first and only then does local work -
280/// deleting the branch, switching back to a base branch - so a non-zero exit
281/// does not mean the merge did not happen. In a jj-colocated repository it
282/// reliably does not mean that: git HEAD is detached, and `--delete-branch`
283/// ends with "could not determine current branch: not on any branch" *after*
284/// the merge has landed. Run ec12 merged pull request 28 into `main` and
285/// recorded `ok: false`, and its task was held waiting for a merge that was
286/// already done.
287///
288/// So the forge is asked, and its answer wins - the same authority [`decide`]
289/// gives the pull request's own state over everything else. The recorded
290/// detail carries both facts, because "the command failed and the merge
291/// happened anyway" is exactly what someone reading the run later needs to
292/// know.
293///
294/// `None` means the merge really did not happen, including when the pull
295/// request could not be read at all: an unreadable answer is not evidence of
296/// success.
297pub(crate) fn merged_after_all(
298    argv: &[String],
299    stderr: &str,
300    after: Option<PrLifecycle>,
301) -> Option<MergeOutcome> {
302    if after? != PrLifecycle::Merged {
303        return None;
304    }
305    Some(MergeOutcome {
306        mode: MergeMode::Pr,
307        ok: true,
308        detail: format!(
309            "gh {} (the command reported `{}`, but the pull request is merged)",
310            argv.join(" "),
311            stderr.trim()
312        ),
313        empty: false,
314    })
315}
316
317/// Decide the next step. No I/O.
318///
319/// `round` counts the fix rounds already spent, so `round == budget` means the
320/// budget is gone. A wait never spends a round: waiting is free, and a slow CI
321/// must not consume the allowance meant for actual fixes.
322///
323/// The order of the tests is the policy:
324///
325/// 1. **The pull request's own state wins.** A merge or a close that happened
326///    underneath us is the end of the story regardless of what the checks say.
327/// 2. **Pending beats red.** A check that is still running may yet fail, and one
328///    fix round that addresses every failure is cheaper than two that each
329///    address half - the fix pushes and restarts the whole suite anyway.
330/// 3. **Comments outrank green.** An unresolved comment holds the merge even
331///    when CI is happy; that is what a review is for.
332/// 4. **Unreadable is not absent.** Checks that cannot be read yet are waited
333///    on for [`CHECKS_GRACE`], because a pull request opened a moment ago has
334///    not been given its workflow runs yet. Past the grace they are treated as
335///    genuinely missing and magi stops rather than merge on a guess.
336pub fn decide(pr: &PrState, round: usize, budget: usize, waited: Duration) -> Step {
337    decide_with(pr, round, budget, waited, CiExpectation::Expected)
338}
339
340/// Whether the repository's CI is expected to report on this pull request.
341#[derive(Debug, Clone, Copy, PartialEq, Eq)]
342pub enum CiExpectation {
343    /// Checks will come: wait for them, judge them (the default, and what
344    /// [`decide`] always uses).
345    Expected,
346    /// The repository explicitly disables CI expectations with `[merge] ci = "none"`.
347    /// Ignore all check statuses while retaining conflicts and review comments.
348    Absent,
349}
350
351/// [`decide`] with the CI expectation made explicit. `Absent` ignores the
352/// rollup, but still requires conflicts and review comments to be resolved.
353pub fn decide_with(
354    pr: &PrState,
355    round: usize,
356    budget: usize,
357    waited: Duration,
358    ci: CiExpectation,
359) -> Step {
360    match pr.state {
361        PrLifecycle::Merged => return Step::Done { merged: true },
362        PrLifecycle::Closed => return Step::Done { merged: false },
363        PrLifecycle::Open => {}
364    }
365
366    // Before the checks: every check on a branch that cannot land is an
367    // answer about a state that cannot land.
368    if pr.blocking == Blocking::Conflict {
369        return Step::Rebase;
370    }
371
372    let spent = round >= budget;
373    let checks = match ci {
374        CiExpectation::Expected => pr.checks,
375        CiExpectation::Absent => Checks::Green,
376    };
377    match checks {
378        Checks::Pending => Step::Wait,
379        Checks::Unknown if waited < CHECKS_GRACE => Step::Wait,
380        Checks::Unknown => Step::GiveUp {
381            reason: format!(
382                "no check status is readable on the pull request after {} minute(s); \
383                 refusing to merge on a guess",
384                CHECKS_GRACE.as_secs() / 60
385            ),
386        },
387        // Red, but the forge says it does not stand in the way: the failing
388        // checks are ones this repository chose not to require. Spending a fix
389        // round on them asks an agent to repair something nobody is gating on
390        // - and pull request 37's only red check was an *action* that could
391        // not fetch its own binary. Merge, and name them so the record is
392        // honest about what was red when it landed.
393        Checks::Red if pr.blocking == Blocking::Unsaid && waited < CHECKS_GRACE => Step::Wait,
394        Checks::Red if !pr.blocking.stops_a_merge() && pr.review_comments.is_empty() => Step::Merge,
395        Checks::Red => {
396            let what = format!(
397                "{} check(s) failing: {}",
398                pr.failing.len(),
399                pr.failing.join(", ")
400            );
401            if spent {
402                Step::GiveUp {
403                    reason: format!("{what} — still red after {budget} fix round(s)"),
404                }
405            } else {
406                Step::Fix { reason: what }
407            }
408        }
409        Checks::Green if pr.review_comments.is_empty() => Step::Merge,
410        Checks::Green => {
411            let what = format!(
412                "{} but {} review comment(s) are unresolved: {}",
413                if ci == CiExpectation::Absent {
414                    "CI is not expected"
415                } else {
416                    "checks are green"
417                },
418                pr.review_comments.len(),
419                authors(&pr.review_comments)
420            );
421            if spent {
422                Step::GiveUp {
423                    reason: format!("{what} — still unresolved after {budget} fix round(s)"),
424                }
425            } else {
426                Step::Fix { reason: what }
427            }
428        }
429    }
430}
431
432/// Distinct comment authors, in the order they first appear.
433fn authors(comments: &[ReviewComment]) -> String {
434    let mut seen: Vec<&str> = Vec::new();
435    for c in comments {
436        if !seen.contains(&c.author.as_str()) {
437            seen.push(&c.author);
438        }
439    }
440    seen.join(", ")
441}
442
443/// The argv magi merges with, minus the program name.
444///
445/// `--subject` is the point of this function existing: see the module docs.
446pub fn merge_argv(number: u64, subject: &str) -> Vec<String> {
447    vec![
448        "pr".to_owned(),
449        "merge".to_owned(),
450        number.to_string(),
451        "--squash".to_owned(),
452        "--delete-branch".to_owned(),
453        "--subject".to_owned(),
454        subject.to_owned(),
455    ]
456}
457
458/// [`merge_argv`] pinned to the commit the decision was made about.
459///
460/// `--match-head-commit` makes the forge refuse the merge if the branch moved
461/// after it was observed, so a push landing between the look and the merge is
462/// never merged unseen.
463pub fn merge_argv_at(number: u64, subject: &str, head: &str) -> Vec<String> {
464    let mut argv = merge_argv(number, subject);
465    argv.push("--match-head-commit".to_owned());
466    argv.push(head.to_owned());
467    argv
468}
469
470/// Take auto-merge back. magi no longer arms auto-merge, but a run recorded by
471/// an older build may still have one standing on the forge, so the disarm
472/// paths (and `RunState::land_armed_head`) stay. Run before anything that changes the head, so an
473/// armed merge never outlives the commit that was approved for it.
474pub fn disable_automerge_argv(number: u64) -> Vec<String> {
475    ["pr", "merge", &number.to_string(), "--disable-auto"]
476        .map(str::to_owned)
477        .to_vec()
478}
479
480/// May the direct merge go ahead, judged from a fresh read?
481///
482/// The pull request must still be open on the approved head, the checks must
483/// have been read for that very head, and the policy must still say merge.
484/// Pure, so the head guard is asserted without a forge.
485fn direct_merge_is_safe(
486    fresh: Option<&Seen>,
487    approved_head: &str,
488    shown: &BTreeSet<String>,
489    round: usize,
490    budget: usize,
491    waited: Duration,
492    ci: CiExpectation,
493) -> bool {
494    let Some(fresh) = fresh else {
495        return false;
496    };
497    if fresh.pr.state != PrLifecycle::Open {
498        return false;
499    }
500    let Some(bound) = bound_head(&fresh.head, &fresh.rollup_head, None) else {
501        return false;
502    };
503    if !bound.eq_ignore_ascii_case(approved_head) {
504        return false;
505    }
506    let mut pr = fresh.pr.clone();
507    pr.review_comments.retain(|c| !shown.contains(&c.body));
508    decide_with(&pr, round, budget, waited, ci) == Step::Merge
509}
510
511/// What GitHub is still waiting for, for the stop reason when an armed merge
512/// does not happen in time. No I/O.
513///
514/// Required checks that are pending or failing are named. A check whose
515/// required-ness could not be read is never assumed optional: every unsettled
516/// check is listed and the reason says so.
517fn waiting_on(
518    merge_state: &str,
519    contexts: &[CheckInfo],
520    required_set: Option<&BTreeSet<String>>,
521) -> String {
522    let state = if merge_state.is_empty() {
523        "unknown"
524    } else {
525        merge_state
526    };
527    let tag = |c: &CheckInfo| match c.verdict {
528        Verdict::Fail => "failed",
529        _ => "pending",
530    };
531    let unsettled: Vec<&CheckInfo> = contexts
532        .iter()
533        .filter(|c| c.verdict != Verdict::Pass)
534        .collect();
535    let required: Vec<String> = unsettled
536        .iter()
537        .filter(|c| c.required == Some(true))
538        .map(|c| format!("{} ({})", c.label, tag(c)))
539        .collect();
540    let unknown: Vec<String> = unsettled
541        .iter()
542        .filter(|c| c.required.is_none())
543        .map(|c| format!("{} ({})", c.label, tag(c)))
544        .collect();
545    // Required contexts the rollup never listed: nothing reported them, so no
546    // pending/failing entry exists to name. Matched case-insensitively against
547    // the labels as the rollup spells them, and no further.
548    let never: Vec<&str> = required_set
549        .map(|set| {
550            set.iter()
551                .filter(|name| !contexts.iter().any(|c| c.label.eq_ignore_ascii_case(name)))
552                .map(String::as_str)
553                .collect()
554        })
555        .unwrap_or_default();
556    let mut out = format!("merge state: {state}");
557    if !never.is_empty() {
558        let _ = write!(
559            out,
560            "; required checks never reported: {}",
561            never.join(", ")
562        );
563    }
564    if !required.is_empty() {
565        let _ = write!(
566            out,
567            "; required checks not passing: {}",
568            required.join(", ")
569        );
570    }
571    if !unknown.is_empty() {
572        let _ = write!(
573            out,
574            "; whether these are required could not be read, so they may be: {}",
575            unknown.join(", ")
576        );
577    }
578    if required.is_empty() && unknown.is_empty() && never.is_empty() {
579        if required_set.is_some() {
580            out.push_str(
581                "; no required check is pending, failing or unreported, so GitHub is probably \
582                 waiting for a review or another branch rule",
583            );
584        } else {
585            out.push_str(
586                "; the required check list could not be read, so a required check that was \
587                 never reported cannot be ruled out",
588            );
589        }
590    }
591    out
592}
593
594/// The squash subject to merge under.
595///
596/// The pull request title, unless it is empty or is a candidate branch's commit
597/// subject that leaked into the title - in which case the task's own first line
598/// is used, because `magi: candidate A (uncommitted work)` in `main` tells a
599/// reader nothing about what landed.
600pub fn merge_subject(pr_title: &str, instruction: &str) -> String {
601    let title = pr_title.trim();
602    if !title.is_empty() && !title.starts_with("magi: candidate") {
603        return title.to_owned();
604    }
605    let first = instruction
606        .lines()
607        .map(str::trim)
608        .find(|l| !l.is_empty())
609        .unwrap_or("magi: land the winning candidate");
610    first.trim_start_matches(['#', ' ']).to_owned()
611}
612
613/// The choice that lets the merge happen, verbatim as the owner taps it.
614pub const APPROVE: &str = "merge";
615
616/// The choice that leaves the pull request open.
617pub const HOLD: &str = "hold";
618
619/// A bounded repair followed by a new review and a fresh owner decision.
620pub const REPAIR: &str = "fix findings and re-review";
621
622/// Whether review evidence offers a meaningful repair choice. This does not
623/// decide whether owner approval is required.
624pub fn repair_available(state: &RunState) -> bool {
625    state.reviews.last().is_some_and(|r| {
626        state.open_findings().iter().any(|f| f.severity.blocks())
627            || r.final_votes()
628                .iter()
629                .any(|(_, _, v)| *v == crate::verdict::ReviewVote::Reject)
630    })
631}
632
633/// All applicable reasons for consulting the owner, independent of repair choices.
634#[derive(Debug, Clone, Copy, PartialEq, Eq)]
635pub enum ApprovalReason {
636    /// Universal owner approval is enabled for every merge.
637    EveryMerge,
638    /// Contested-merge approval is enabled and blocking findings remain open.
639    OpenBlockingFindings,
640    /// Contested-merge approval is enabled and a final reviewer vote rejects.
641    FinalReject,
642    /// An existing owner decision must be honored even if controls change.
643    ExistingDecision,
644    /// Merge repair requires a fresh owner decision, even on the same head.
645    AfterRepair,
646}
647
648/// Disabling fresh approval controls never bypasses an existing owner decision
649/// or the fresh decision owed after repair, even if repair kept the same head.
650pub fn approval_reasons(state: &RunState) -> Vec<ApprovalReason> {
651    let mut reasons = Vec::new();
652    if state.owner_hold(crate::config::OwnerHold::LandApproval) {
653        reasons.push(ApprovalReason::EveryMerge);
654    }
655    if state.owner_hold(crate::config::OwnerHold::HoldContestedMerge) {
656        if state.open_findings().iter().any(|f| f.severity.blocks()) {
657            reasons.push(ApprovalReason::OpenBlockingFindings);
658        }
659        if state.reviews.last().is_some_and(|r| {
660            r.final_votes()
661                .iter()
662                .any(|(_, _, v)| *v == crate::verdict::ReviewVote::Reject)
663        }) {
664            reasons.push(ApprovalReason::FinalReject);
665        }
666    }
667    if state.land_approval.is_some() {
668        reasons.push(ApprovalReason::ExistingDecision);
669    }
670    if !state.merge_repairs.is_empty() {
671        reasons.push(ApprovalReason::AfterRepair);
672    }
673    reasons
674}
675
676/// Bound free-form approval evidence by characters, including an explicit
677/// ellipsis when abbreviated. Keep UTF-8 text intact in questions and panels.
678fn approval_excerpt(text: &str, max_chars: usize) -> String {
679    if text.chars().count() <= max_chars {
680        text.to_owned()
681    } else {
682        let mut excerpt: String = text.chars().take(max_chars.saturating_sub(1)).collect();
683        excerpt.push('…');
684        excerpt
685    }
686}
687
688/// Persisted verbatim in the question and panel at creation. Deputies and
689/// resumed questions use that snapshot, never reconstruct past policy from
690/// today's settings. Fixer claims and successful gates are not resolutions.
691fn approval_evidence(state: &RunState) -> String {
692    const TITLE_CHARS: usize = 100;
693    const REASON_CHARS: usize = 300;
694    let reason = |text: &str| approval_excerpt(text, REASON_CHARS);
695    let ja = words(&state.config.graph.language).html_lang == "ja";
696    let mut s = if ja {
697        "承認が必要な理由:\n"
698    } else {
699        "Approval required because:\n"
700    }
701    .to_owned();
702    for reason in approval_reasons(state) {
703        let label = match (reason, ja) {
704            (ApprovalReason::EveryMerge, false) => {
705                "land_approval=true: owner approval for every merge"
706            }
707            (ApprovalReason::EveryMerge, true) => {
708                "land_approval=true: すべてのマージに所有者の承認が必要"
709            }
710            (ApprovalReason::OpenBlockingFindings, false) => {
711                "hold_contested_merge=true: open Major-or-above internal findings"
712            }
713            (ApprovalReason::OpenBlockingFindings, true) => {
714                "hold_contested_merge=true: Major 以上の内部指摘が未解決"
715            }
716            (ApprovalReason::FinalReject, false) => {
717                "hold_contested_merge=true: final reviewer reject after reconsideration (even without Major findings)"
718            }
719            (ApprovalReason::FinalReject, true) => {
720                "hold_contested_merge=true: 再考後の最終票が reject(Major 指摘がなくても承認が必要)"
721            }
722            (ApprovalReason::ExistingDecision, false) => {
723                "existing owner decision: changing controls cannot bypass it"
724            }
725            (ApprovalReason::ExistingDecision, true) => {
726                "既存の所有者判断: 設定変更では迂回できません"
727            }
728            (ApprovalReason::AfterRepair, false) => {
729                "fresh owner approval after merge repair, even with an unchanged head"
730            }
731            (ApprovalReason::AfterRepair, true) => {
732                "マージ修復後の新しい所有者承認(head に変更がなくても必要)"
733            }
734        };
735        let control = match reason {
736            ApprovalReason::EveryMerge => Some(crate::config::OwnerHold::LandApproval),
737            ApprovalReason::OpenBlockingFindings | ApprovalReason::FinalReject => {
738                Some(crate::config::OwnerHold::HoldContestedMerge)
739            }
740            _ => None,
741        };
742        let source = control
743            .filter(|c| !c.base_value(&state.config.graph))
744            .and_then(|c| state.branch_owner_holds.iter().find(|h| h.control == c));
745        if let Some(source) = source {
746            if ja {
747                let _ = writeln!(
748                    s,
749                    "- {}=true(レビュー対象ブランチの {} 由来, {})",
750                    source.control.key(),
751                    source.file,
752                    source.head
753                );
754            } else {
755                let _ = writeln!(
756                    s,
757                    "- {}=true from the reviewed branch's {} ({})",
758                    source.control.key(),
759                    source.file,
760                    source.head
761                );
762            }
763        }
764        let _ = writeln!(s, "- {label}");
765    }
766    s.push_str(if ja {
767        "\n再考後の各レビュアーの最終票:\n"
768    } else {
769        "\nFinal reviewer votes after reconsideration:\n"
770    });
771    if let Some(round) = state.reviews.last() {
772        let votes = round.final_votes();
773        for r in &round.reviews {
774            let vote = votes
775                .iter()
776                .find(|(seat, _, _)| *seat == r.reviewer)
777                .map(|(_, _, v)| v.label())
778                .unwrap_or(if ja { "記録なし" } else { "not recorded" });
779            let _ = writeln!(s, "#{} ({}): {vote}", r.reviewer, r.agent);
780            let rv = round
781                .reconsideration
782                .iter()
783                .find(|rv| rv.reviewer == r.reviewer);
784            if let Some(rv) = rv.filter(|rv| rv.vote.is_some()) {
785                let initial = r.vote.map(|v| v.label()).unwrap_or("?");
786                let _ = writeln!(
787                    s,
788                    "  {}: {initial} → {vote}",
789                    if ja { "再考" } else { "reconsideration" }
790                );
791                let _ = writeln!(
792                    s,
793                    "  {}: {}",
794                    if ja { "理由" } else { "reason" },
795                    reason(if rv.reason.trim().is_empty() {
796                        if ja { "記録なし" } else { "not recorded" }
797                    } else {
798                        &rv.reason
799                    })
800                );
801            } else {
802                let _ = writeln!(
803                    s,
804                    "  {}: {}",
805                    if ja {
806                        "初回票を保持・理由"
807                    } else {
808                        "initial vote retained; reason"
809                    },
810                    reason(if r.summary.trim().is_empty() {
811                        if ja { "記録なし" } else { "not recorded" }
812                    } else {
813                        &r.summary
814                    })
815                );
816                if let Some(rv) = rv {
817                    let _ = writeln!(
818                        s,
819                        "  {}: {}",
820                        if ja {
821                            "再投票の回答なし"
822                        } else {
823                            "no reconsideration vote"
824                        },
825                        reason(rv.failed.as_deref().unwrap_or(if ja {
826                            "理由の記録なし"
827                        } else {
828                            "reason not recorded"
829                        }))
830                    );
831                }
832            }
833            if let Some(failed) = &r.failed {
834                let _ = writeln!(
835                    s,
836                    "  {}: {}",
837                    if ja { "回答なし" } else { "no answer" },
838                    reason(failed)
839                );
840            }
841        }
842    } else {
843        s.push_str(if ja {
844            "レビュー記録なし\n"
845        } else {
846            "No review recorded\n"
847        });
848    }
849    s.push_str(if ja { "\n内部レビュー指摘(最終ラウンド): 採用申告や gate 成功だけでは個別指摘の解決確認にはなりません。\n" } else { "\nInternal review findings (last round): adoption claims or a passing gate alone do not confirm individual resolution.\n" });
850    let findings = state.last_round_findings();
851    for f in &findings {
852        let _ = writeln!(
853            s,
854            "[{}] {} {}:{}: {}\n{}",
855            f.id,
856            f.severity,
857            f.file.as_deref().unwrap_or("?"),
858            f.line.map(|l| l.to_string()).unwrap_or_else(|| "?".into()),
859            approval_excerpt(&f.title, TITLE_CHARS),
860            reason(&f.detail)
861        );
862        let mut evidence = false;
863        for fix in state.reviews.iter().filter_map(|r| r.fix.as_ref()).chain(
864            state
865                .operator_fixes
866                .iter()
867                .filter_map(|request| request.fix.as_ref()),
868        ) {
869            if fix.addressed.contains(&f.id) {
870                evidence = true;
871                let _ = writeln!(
872                    s,
873                    "  {} ({}): {}",
874                    if ja {
875                        "採用申告(解決未確認)"
876                    } else {
877                        "claimed addressed (resolution unconfirmed)"
878                    },
879                    fix.agent,
880                    reason(&fix.notes)
881                );
882            }
883            for rejection in fix.rejected.iter().filter(|r| r.id == f.id) {
884                evidence = true;
885                let _ = writeln!(
886                    s,
887                    "  {}: {}",
888                    if ja { "不採用理由" } else { "declined" },
889                    reason(&rejection.why)
890                );
891            }
892        }
893        if !evidence {
894            s.push_str(if ja {
895                "  採用・解決確認の記録なし\n"
896            } else {
897                "  No adoption or resolution evidence recorded\n"
898            });
899        }
900    }
901    if findings.is_empty() {
902        s.push_str(if ja {
903            "最終ラウンドに指摘の記録なし\n"
904        } else {
905            "No findings recorded in the last round\n"
906        });
907    }
908    s
909}
910
911/// Shared choices for web, CLI and consultation questions.
912pub fn approval_choices(state: &RunState) -> Vec<String> {
913    let mut choices = vec![
914        APPROVE.to_owned(),
915        HOLD.to_owned(),
916        REPAIR.to_owned(),
917        crate::merge_decision::WITHDRAW.to_owned(),
918        crate::merge_decision::DISMISS.to_owned(),
919    ];
920    choices.extend(
921        state
922            .config
923            .agents
924            .iter()
925            .map(|a| format!("{}{id}", crate::merge_decision::REIMPLEMENT, id = a.id)),
926    );
927    choices
928}
929
930/// Extend unanswered legacy cards in place. Answered cards keep their choices;
931/// a legacy repair answer gains only its missing dispatch action.
932pub fn upgrade_repair_choice(q: &mut ask::Question, state: &RunState) -> bool {
933    if q.node != APPROVAL_NODE
934        || !state
935            .land_approval
936            .as_ref()
937            .is_some_and(|a| a.question == q.id)
938    {
939        return false;
940    }
941    let before = (q.choices.clone(), q.actions.clone(), q.merge_target.clone());
942    if q.status.open() {
943        // Older cards attached Resume to hold; a hold must remain answerable
944        // without dispatching a question action.
945        q.actions.remove(HOLD);
946        for choice in approval_choices(state) {
947            if !q.choices.contains(&choice) {
948                q.choices.push(choice);
949            }
950        }
951        if q.merge_target.is_none() {
952            q.merge_target = state.pr.as_ref().map(|pr| crate::merge_decision::Target {
953                pr: pr.url.clone(),
954                head: state.land_approval.as_ref().unwrap().head.clone(),
955                language: state.config.graph.language.clone(),
956            });
957        }
958    }
959    for choice in q.choices.clone() {
960        if (q.status.open()
961            && crate::merge_decision::stops(&choice)
962            && choice != HOLD
963            && choice != crate::merge_decision::DISMISS)
964            || (choice == REPAIR && (q.status.open() || q.resolution().as_deref() == Some(REPAIR)))
965        {
966            q.actions.insert(
967                choice,
968                ask::ChoiceAction::Resume {
969                    run: state.id.clone(),
970                },
971            );
972        }
973    }
974    before != (q.choices.clone(), q.actions.clone(), q.merge_target.clone())
975}
976
977/// Graph node recorded on the approval question.
978///
979/// The phone keys its high-stakes card off this rather than off the choice
980/// strings, so renaming a button cannot silently downgrade the card that
981/// guards the one irreversible action magi takes.
982pub const APPROVAL_NODE: &str = "land-approval";
983
984/// Unified diff lines carried in the panel before it is truncated.
985///
986/// Four hundred: the panel is read on a 390px phone, where a diff line often
987/// wraps to two rows, so this is already a few thousand rows of scrolling -
988/// past that nobody is reading, and the bytes still count against the panel's
989/// 8 MiB cap. A larger diff is not hidden: the note says how many lines were
990/// cut and which worktree holds the whole patch.
991pub const DIFF_MAX_LINES: usize = 400;
992
993/// What the owner's answer to the approval question means.
994#[derive(Debug, Clone, Copy, PartialEq, Eq)]
995pub enum Approval {
996    /// The owner said [`APPROVE`]. Merge.
997    Merge,
998    /// Repair the existing PR, then ask again; never approval to merge.
999    Repair,
1000    /// Anything else, including silence. Leave the pull request open.
1001    Hold,
1002}
1003
1004/// Read the owner's answer, where `None` is an unanswered question.
1005///
1006/// Silence is a hold. A timed-out question means the owner never saw it or
1007/// never decided, and defaulting an irreversible merge to "yes" would make this
1008/// gate worse than no gate at all: it would merge unattended while claiming to
1009/// have asked. Only the exact [`APPROVE`] choice merges, so an answer this
1010/// function does not recognise holds too.
1011pub fn approval(answer: Option<&str>) -> Approval {
1012    match answer {
1013        Some(a) if a.trim().eq_ignore_ascii_case(APPROVE) => Approval::Merge,
1014        Some(a) if a.trim() == REPAIR => Approval::Repair,
1015        _ => Approval::Hold,
1016    }
1017}
1018
1019/// What [`approval_gate`] found on one check of the owner's merge decision.
1020#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1021enum ApprovalGate {
1022    /// The owner said [`APPROVE`]. Merge.
1023    Approved,
1024    /// The owner explicitly requested repair, without authorizing a merge.
1025    Repair,
1026    /// The owner said anything else, the question timed out, or it was
1027    /// closed with no decision recorded.
1028    Held,
1029    /// Filed and still waiting - the caller parks rather than blocking on it.
1030    Pending,
1031}
1032
1033/// Escape text for HTML, including both quote characters.
1034///
1035/// Every string in the panel is agent-influenced: a branch name, a file path, a
1036/// commit subject, a review comment. The sandboxed frame stops such text from
1037/// *running*, but it does not stop a `<` from ending the document early or a
1038/// `"` from ending an attribute and inventing a new one - the panel would then
1039/// render a lie, or not render at all. Both quotes are escaped because the same
1040/// function is used inside attributes, where remembering which quote style the
1041/// caller used is one mistake away from an injected attribute.
1042fn esc(s: &str) -> String {
1043    let mut out = String::with_capacity(s.len());
1044    for c in s.chars() {
1045        match c {
1046            '&' => out.push_str("&amp;"),
1047            '<' => out.push_str("&lt;"),
1048            '>' => out.push_str("&gt;"),
1049            '"' => out.push_str("&quot;"),
1050            '\'' => out.push_str("&#39;"),
1051            _ => out.push(c),
1052        }
1053    }
1054    out
1055}
1056
1057/// One row of the diffstat table.
1058#[derive(Debug, Clone, PartialEq, Eq)]
1059struct StatRow {
1060    path: String,
1061    /// `None` for a binary file, which `git` reports as `-`.
1062    added: Option<u64>,
1063    removed: Option<u64>,
1064}
1065
1066impl StatRow {
1067    /// Lines touched, for sorting. A binary file counts as zero rather than as
1068    /// unknown, which puts it at the bottom where it needs no attention.
1069    fn churn(&self) -> u64 {
1070        self.added.unwrap_or(0) + self.removed.unwrap_or(0)
1071    }
1072}
1073
1074/// Parse `git diff --numstat` into rows, biggest churn first.
1075///
1076/// `--numstat` and not `--stat`: the `+++---` bar in `--stat` is *scaled* to the
1077/// terminal width, so counting its characters would print fabricated numbers in
1078/// the one table an operator approves an irreversible action from.
1079fn parse_numstat(numstat: &str) -> Vec<StatRow> {
1080    let mut rows: Vec<StatRow> = numstat
1081        .lines()
1082        .filter_map(|line| {
1083            let mut parts = line.splitn(3, '\t');
1084            let added = parts.next()?.trim();
1085            let removed = parts.next()?.trim();
1086            let path = parts.next()?.trim();
1087            if path.is_empty() {
1088                return None;
1089            }
1090            Some(StatRow {
1091                path: path.to_owned(),
1092                added: added.parse().ok(),
1093                removed: removed.parse().ok(),
1094            })
1095        })
1096        .collect();
1097    // Path breaks the tie so the same change always renders the same table; an
1098    // operator comparing two panels should not see rows shuffle.
1099    rows.sort_by(|a, b| b.churn().cmp(&a.churn()).then_with(|| a.path.cmp(&b.path)));
1100    rows
1101}
1102
1103/// How one diff line is shown: a gutter character, a style, and the body to
1104/// print - which is the line minus its marker, so the marker appears exactly
1105/// once, in the gutter.
1106///
1107/// The gutter is why this exists at all. The operator may be colour blind, or
1108/// reading in sunlight with the screen dimmed, so an added line is never
1109/// distinguished by its background alone: `+` and `-` sit in a fixed column,
1110/// the same mark they already read in a terminal.
1111fn diff_row(line: &str) -> (&'static str, &'static str, &str) {
1112    if line.starts_with("+++") || line.starts_with("---") {
1113        (" ", "color:#57606a;font-weight:600", line)
1114    } else if let Some(body) = line.strip_prefix('+') {
1115        ("+", "background:#e6ffec;color:#0a3622", body)
1116    } else if let Some(body) = line.strip_prefix('-') {
1117        ("-", "background:#ffebe9;color:#5c1a17", body)
1118    } else if line.starts_with("@@") {
1119        ("~", "background:#eef2ff;color:#3730a3", line)
1120    } else if let Some(body) = line.strip_prefix(' ') {
1121        (" ", "", body)
1122    } else {
1123        (" ", "color:#57606a;font-weight:600", line)
1124    }
1125}
1126
1127/// The handful of words the approval panel says in its own voice.
1128///
1129/// magi's own text, not an agent's, so `[graph] language` has to reach it too:
1130/// the operator asked why the merge question spoke English on a repository
1131/// configured for Japanese, and "because that string is a literal in Rust" is
1132/// not an answer. Only the languages magi can actually check are translated;
1133/// anything else falls back to English rather than shipping a guess, and that
1134/// fallback is deliberate.
1135struct Words {
1136    html_lang: &'static str,
1137    task: &'static str,
1138    what_changed: &'static str,
1139    review_verdict: &'static str,
1140    reviewer: &'static str,
1141    reviewer_no_answer: &'static str,
1142    checks: &'static str,
1143    nothing_failing: &'static str,
1144    files_changed: &'static str,
1145    commits: &'static str,
1146    no_commits: &'static str,
1147    comments: &'static str,
1148    no_comments: &'static str,
1149    diff: &'static str,
1150    truncated: &'static str,
1151    lands_as: &'static str,
1152}
1153
1154const EN: Words = Words {
1155    html_lang: "en",
1156    task: "Task",
1157    what_changed: "What changed",
1158    review_verdict: "Review verdict",
1159    reviewer: "Reviewer",
1160    reviewer_no_answer: "produced no answer",
1161    checks: "Checks",
1162    nothing_failing: "Nothing failing.",
1163    files_changed: "file(s) changed",
1164    commits: "Commits being squashed",
1165    no_commits: "No commit subjects could be read from the branch.",
1166    comments: "External PR review comments",
1167    no_comments: "No unaddressed external PR comments at this observation. This does not mean internal findings are resolved.",
1168    diff: "Diff",
1169    truncated: "Truncated",
1170    lands_as: "They land as one commit titled",
1171};
1172
1173const JA: Words = Words {
1174    html_lang: "ja",
1175    task: "タスク",
1176    what_changed: "変更内容",
1177    review_verdict: "レビューの結論",
1178    reviewer: "レビュアー",
1179    reviewer_no_answer: "回答なし",
1180    checks: "チェック",
1181    nothing_failing: "失敗しているものはありません。",
1182    files_changed: "ファイル変更",
1183    commits: "squash されるコミット",
1184    no_commits: "ブランチからコミット件名を読めませんでした。",
1185    comments: "外部 PR レビューコメント",
1186    no_comments: "この時点で未対応の外部 PR コメントはありません。内部指摘の解決を意味するものではありません。",
1187    diff: "差分",
1188    truncated: "省略",
1189    lands_as: "これらは次の件名の1コミットとして入ります:",
1190};
1191
1192impl Words {
1193    /// The clause after the merge subject. Split out because word order moves:
1194    /// Japanese puts the subject before the verb, so a shared template with a
1195    /// hole in the middle would read as machine translation.
1196    fn lands_as_tail(&self) -> &'static str {
1197        if self.html_lang == "ja" {
1198            "。この件名も承認の対象です。"
1199        } else {
1200            ", which you are approving too."
1201        }
1202    }
1203
1204    /// The question's own one-line summary, which is what a phone shows first.
1205    fn approval_summary(&self, number: u64, subject: &str) -> String {
1206        if self.html_lang == "ja" {
1207            format!("プルリクエスト #{number} をマージ: {subject}")
1208        } else {
1209            format!("merge pull request #{number}: {subject}")
1210        }
1211    }
1212
1213    /// The body under the summary, above the panel.
1214    fn approval_detail(
1215        &self,
1216        url: &str,
1217        base: &str,
1218        subject: &str,
1219        contested: Option<&ContestedHandoff>,
1220    ) -> String {
1221        let body = if self.html_lang == "ja" {
1222            format!(
1223                "{url} はチェックが緑で、`{base}` へ `{subject}` として squash \
1224                 できる状態です。差分の要約・パッチ・squash されるコミットは\
1225                 下のパネルにあります。"
1226            )
1227        } else {
1228            format!(
1229                "{url} is green and ready to squash into `{base}` as `{subject}`. \
1230                 The panel holds the diffstat, the patch and the commits being squashed."
1231            )
1232        };
1233        match contested {
1234            Some(c) => format!("{}\n\n{body}", self.contested_reason(url, c)),
1235            None => body,
1236        }
1237    }
1238
1239    /// Why this question exists although merge approvals are off: the open
1240    /// blocking findings and who rejected. Short enough for a phone.
1241    fn contested_reason(&self, url: &str, c: &ContestedHandoff) -> String {
1242        const SHOWN: usize = 5;
1243        const TITLE_CHARS: usize = 100;
1244        let ja = self.html_lang == "ja";
1245        let mut out = if ja {
1246            format!(
1247                "{url} は、マージ承認がオフでも保留しています。レビューが予算切れで終わった\
1248                 時点で、却下票を伴う重大な未解決の指摘が残っているためです。\n"
1249            )
1250        } else {
1251            format!(
1252                "{url} is held for approval although merge approvals are off: the \
1253                 review ended with blocking findings still open and a reviewer \
1254                 voting reject.\n"
1255            )
1256        };
1257        for f in c.findings.iter().take(SHOWN) {
1258            let at = match (&f.file, f.line) {
1259                (Some(file), Some(line)) => format!("{file}:{line}"),
1260                (Some(file), None) => file.clone(),
1261                _ => (if ja { "場所未指定" } else { "no location" }).to_owned(),
1262            };
1263            let title: String = f.title.chars().take(TITLE_CHARS).collect();
1264            let _ = writeln!(out, "- {} {} {at}: {title}", f.id, f.severity);
1265        }
1266        if c.findings.len() > SHOWN {
1267            let more = c.findings.len() - SHOWN;
1268            let _ = writeln!(
1269                out,
1270                "{}",
1271                if ja {
1272                    format!("- ほか {more} 件")
1273                } else {
1274                    format!("- and {more} more")
1275                }
1276            );
1277        }
1278        let seats: Vec<String> = c
1279            .rejecters
1280            .iter()
1281            .map(|(seat, agent)| format!("#{seat} ({agent})"))
1282            .collect();
1283        let _ = write!(
1284            out,
1285            "{} {}",
1286            if ja {
1287                "却下したレビュアー:"
1288            } else {
1289                "Rejected by reviewer:"
1290            },
1291            seats.join(", ")
1292        );
1293        out
1294    }
1295
1296    /// The truncation note, written whole in each language for the same reason.
1297    fn truncated_note(
1298        &self,
1299        omitted: usize,
1300        total: usize,
1301        shown: usize,
1302        where_: &str,
1303        base: &str,
1304        head: &str,
1305    ) -> String {
1306        if self.html_lang == "ja" {
1307            format!(
1308                "先頭 {shown} 行のあと、差分 {total} 行のうち {omitted} 行を省略しました。\
1309                 全体は <code>{where_}</code>(<code>git diff {base}...{head}</code>)と\
1310                 プルリクエストにあります。"
1311            )
1312        } else {
1313            format!(
1314                "{omitted} of {total} diff lines omitted after the first {shown}. \
1315                 The whole patch is in <code>{where_}</code> \
1316                 (<code>git diff {base}...{head}</code>) and on the pull request."
1317            )
1318        }
1319    }
1320}
1321
1322/// Pick the panel's language. Codes and names both, because `[graph] language`
1323/// has always accepted either.
1324fn words(language: &str) -> &'static Words {
1325    if crate::lang::is_japanese(language) {
1326        &JA
1327    } else {
1328        &EN
1329    }
1330}
1331
1332/// The approval panel's html: what is about to land, and the evidence for it.
1333///
1334/// Pure, so the whole document is asserted in tests without `gh`, without a
1335/// network and without a repository. The caller gathers `diffstat`
1336/// (`git diff --numstat`), `diff` (the unified patch), `commits` (the subjects
1337/// being squashed) and `subject` (what the squash will be called) from the
1338/// winner's worktree.
1339///
1340/// It emits no `<script>`, no `<form>` and no remote url, because the frame's
1341/// content security policy blocks all three: anything of the sort here would be
1342/// dead markup that misleads the next reader into thinking it works.
1343pub fn approval_panel(
1344    state: &RunState,
1345    pr: &PrState,
1346    diffstat: &str,
1347    diff: &str,
1348    commits: &[String],
1349    subject: &str,
1350) -> String {
1351    let rows = parse_numstat(diffstat);
1352    let w = words(&state.config.graph.language);
1353    let mut h = String::with_capacity(4_096 + diff.len().min(200_000));
1354
1355    let _ = writeln!(
1356        h,
1357        "<!doctype html>\n<html lang=\"{}\">\n<head>\n<meta charset=\"utf-8\">\n\
1358         <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">",
1359        w.html_lang
1360    );
1361    let _ = writeln!(
1362        h,
1363        "<title>merge #{} — {}</title>\n</head>",
1364        pr.number,
1365        esc(subject)
1366    );
1367    h.push_str(
1368        "<body style=\"margin:0;padding:12px;font:15px/1.5 -apple-system,\
1369         'Segoe UI',system-ui,sans-serif;color:#1f2328;background:#fff;\
1370         word-break:break-word\">\n",
1371    );
1372
1373    // The decision, in the words the operator is approving.
1374    let _ = writeln!(
1375        h,
1376        "<h1 style=\"margin:0 0 4px;font-size:19px\">Merge #{} into \
1377         <code style=\"background:#f6f8fa;padding:1px 4px;border-radius:4px\">{}</code></h1>\n\
1378         <p style=\"margin:0 0 4px;font-size:17px;font-weight:600\">{}</p>\n\
1379         <p style=\"margin:0 0 12px;font-size:13px;color:#57606a\">squash merge · run {} · \
1380         <a href=\"{}\" target=\"_blank\" rel=\"noopener noreferrer\" style=\"color:#0969da\">{}</a></p>",
1381        pr.number,
1382        esc(&state.base_branch),
1383        esc(subject),
1384        esc(&state.id),
1385        esc(&pr.url),
1386        esc(&pr.url),
1387    );
1388
1389    let _ = writeln!(
1390        h,
1391        "<section style=\"padding:12px;background:#fff8c5;border:1px solid #9a6700;border-radius:6px;white-space:pre-wrap\">{}</section>",
1392        esc(&approval_evidence(state))
1393    );
1394
1395    // The task, verbatim: the operator's own words for what was asked, so the
1396    // panel does not make them reconstruct the request from a diffstat.
1397    let _ = writeln!(
1398        h,
1399        "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>\n\
1400         <p style=\"margin:0;font-size:13px;white-space:pre-wrap\">{}</p>",
1401        w.task,
1402        esc(&state.instruction)
1403    );
1404
1405    // The winner's own account of what it did and why, when there is one.
1406    if let Some(summary) = state
1407        .winner()
1408        .map(|c| c.summary.as_str())
1409        .filter(|s| !s.is_empty())
1410    {
1411        let _ = writeln!(
1412            h,
1413            "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>\n\
1414             <p style=\"margin:0;font-size:13px;white-space:pre-wrap\">{}</p>",
1415            w.what_changed,
1416            esc(summary)
1417        );
1418    }
1419
1420    // Only the last round's reviewer prose is current. Its votes and
1421    // remaining findings are shown explicitly in the evidence above.
1422    if let Some(round) = state.reviews.last() {
1423        let _ = writeln!(
1424            h,
1425            "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1426            w.review_verdict
1427        );
1428        for r in &round.reviews {
1429            // A seat the review loop counted as answered has real prose in
1430            // `summary`; one it counted against `incomplete` (see
1431            // `graph::Runner::review_loop`) never produced any and left it
1432            // empty - which must not be read back as a blank verdict, since
1433            // an empty box here looks like "nothing to say" rather than
1434            // "never answered".
1435            let body = match &r.failed {
1436                Some(reason) => format!("{}: {}", w.reviewer_no_answer, esc(reason)),
1437                None => esc(&r.summary),
1438            };
1439            let _ = writeln!(
1440                h,
1441                "<div style=\"margin:0 0 8px;padding:8px;background:#f6f8fa;\
1442                 border-radius:6px\">\
1443                 <div style=\"font-size:12px;color:#57606a\">{} {} · {}</div>\
1444                 <div style=\"white-space:pre-wrap;font-size:13px\">{}</div></div>",
1445                w.reviewer,
1446                r.reviewer,
1447                esc(&r.agent),
1448                body,
1449            );
1450        }
1451    }
1452
1453    let _ = writeln!(
1454        h,
1455        "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}: {}</h2>",
1456        w.checks,
1457        esc(pr.checks.as_str())
1458    );
1459    if pr.failing.is_empty() {
1460        let _ = writeln!(
1461            h,
1462            "<p style=\"margin:0;font-size:13px;color:#57606a\">{}</p>",
1463            w.nothing_failing
1464        );
1465    } else {
1466        h.push_str("<ul style=\"margin:0;padding-left:20px;font-size:13px\">\n");
1467        for f in &pr.failing {
1468            let _ = writeln!(h, "<li>{}</li>", esc(f));
1469        }
1470        h.push_str("</ul>\n");
1471    }
1472
1473    // Diffstat as a real table, so a phone reads what moved without scrolling
1474    // sideways through a terminal bar chart.
1475    let _ = writeln!(
1476        h,
1477        "<h2 style=\"margin:16px 0 6px;font-size:15px\">{} {}</h2>",
1478        rows.len(),
1479        w.files_changed
1480    );
1481    h.push_str(
1482        "<table style=\"width:100%;border-collapse:collapse;font-size:13px\">\n\
1483         <thead><tr>\
1484         <th style=\"text-align:left;border-bottom:1px solid #d0d7de;padding:4px 2px\">file</th>\
1485         <th style=\"text-align:right;border-bottom:1px solid #d0d7de;padding:4px 2px\">added</th>\
1486         <th style=\"text-align:right;border-bottom:1px solid #d0d7de;padding:4px 2px\">removed\
1487         </th></tr></thead>\n<tbody>\n",
1488    );
1489    let mut total_added = 0u64;
1490    let mut total_removed = 0u64;
1491    for r in &rows {
1492        total_added += r.added.unwrap_or(0);
1493        total_removed += r.removed.unwrap_or(0);
1494        let cell = |n: Option<u64>| match n {
1495            Some(n) => n.to_string(),
1496            None => "bin".to_owned(),
1497        };
1498        let _ = writeln!(
1499            h,
1500            "<tr>\
1501             <td style=\"padding:4px 2px;border-bottom:1px solid #eaeef2;\
1502             font-family:ui-monospace,monospace\">{}</td>\
1503             <td style=\"padding:4px 2px;border-bottom:1px solid #eaeef2;text-align:right;\
1504             color:#0a3622\">{}</td>\
1505             <td style=\"padding:4px 2px;border-bottom:1px solid #eaeef2;text-align:right;\
1506             color:#5c1a17\">{}</td></tr>",
1507            esc(&r.path),
1508            cell(r.added),
1509            cell(r.removed),
1510        );
1511    }
1512    let _ = writeln!(
1513        h,
1514        "</tbody>\n<tfoot><tr style=\"font-weight:600\">\
1515         <td style=\"padding:4px 2px\">total</td>\
1516         <td style=\"padding:4px 2px;text-align:right\">{total_added}</td>\
1517         <td style=\"padding:4px 2px;text-align:right\">{total_removed}</td>\
1518         </tr></tfoot>\n</table>"
1519    );
1520
1521    // The commits being squashed, and the subject that replaces them.
1522    let _ = writeln!(
1523        h,
1524        "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1525        w.commits
1526    );
1527    if commits.is_empty() {
1528        h.push_str(&format!(
1529            "<p style=\"margin:0;font-size:13px;color:#57606a\">{}</p>\n",
1530            w.no_commits
1531        ));
1532    } else {
1533        h.push_str("<ol style=\"margin:0;padding-left:20px;font-size:13px\">\n");
1534        for c in commits {
1535            let _ = writeln!(h, "<li>{}</li>", esc(c));
1536        }
1537        h.push_str("</ol>\n");
1538    }
1539    let _ = writeln!(
1540        h,
1541        "<p style=\"margin:8px 0 0;font-size:13px\">{} <strong>{}</strong>{}</p>",
1542        w.lands_as,
1543        esc(subject),
1544        w.lands_as_tail()
1545    );
1546
1547    // The review comments that shaped this branch, and who asked for them.
1548    let _ = writeln!(
1549        h,
1550        "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1551        w.comments
1552    );
1553    if pr.review_comments.is_empty() {
1554        h.push_str(&format!(
1555            "<p style=\"margin:0;font-size:13px;color:#57606a\">{}</p>\n",
1556            w.no_comments
1557        ));
1558    } else {
1559        for c in &pr.review_comments {
1560            let anchor = match (&c.path, c.line) {
1561                (Some(p), Some(l)) => format!("{p}:{l}"),
1562                (Some(p), None) => p.clone(),
1563                _ => "pull request thread".to_owned(),
1564            };
1565            let _ = writeln!(
1566                h,
1567                "<div style=\"margin:0 0 8px;padding:8px;background:#f6f8fa;border-radius:6px\">\
1568                 <div style=\"font-size:12px;color:#57606a\">{} · {}</div>\
1569                 <div style=\"white-space:pre-wrap;font-size:13px\">{}</div></div>",
1570                esc(&c.author),
1571                esc(&anchor),
1572                esc(&tail(&c.body, 800)),
1573            );
1574        }
1575    }
1576
1577    // The patch itself.
1578    let total = diff.lines().count();
1579    let shown = total.min(DIFF_MAX_LINES);
1580    let _ = writeln!(
1581        h,
1582        "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1583        w.diff
1584    );
1585    h.push_str(
1586        "<div style=\"font:12px/1.45 ui-monospace,SFMono-Regular,Menlo,monospace;\
1587         border:1px solid #d0d7de;border-radius:6px;overflow-x:auto\">\n",
1588    );
1589    for line in diff.lines().take(shown) {
1590        let (gutter, style, body) = diff_row(line);
1591        let _ = writeln!(
1592            h,
1593            "<div style=\"display:flex;{style}\">\
1594             <span style=\"flex:0 0 1.4em;text-align:center;user-select:none;\
1595             border-right:1px solid #d0d7de\">{gutter}</span>\
1596             <span style=\"white-space:pre;padding-left:6px\">{}</span></div>",
1597            esc(body),
1598        );
1599    }
1600    h.push_str("</div>\n");
1601    if total > shown {
1602        let omitted = total - shown;
1603        let head = state.winner().map_or("HEAD", |w| w.branch.as_str());
1604        let where_ = state.winner().map_or_else(
1605            || state.repo.display().to_string(),
1606            |w| w.worktree.display().to_string(),
1607        );
1608        let _ = writeln!(
1609            h,
1610            "<p style=\"margin:8px 0 0;padding:8px;background:#fff8c5;border-radius:6px;\
1611             font-size:13px\">{}: {}</p>",
1612            w.truncated,
1613            w.truncated_note(
1614                omitted,
1615                total,
1616                shown,
1617                &esc(&where_),
1618                &esc(&state.base_branch),
1619                &esc(head),
1620            ),
1621        );
1622    }
1623
1624    h.push_str("</body>\n</html>\n");
1625    h
1626}
1627
1628/// Historical contested hand-off evidence for the question detail. Approval
1629/// itself is decided by `approval_reasons`, including reject-only cases.
1630fn contested_to_ask(state: &RunState) -> Option<ContestedHandoff> {
1631    if state.owner_hold(crate::config::OwnerHold::HoldContestedMerge) {
1632        state.contested_handoff.clone()
1633    } else {
1634        None
1635    }
1636}
1637
1638/// What a merge-approval question's deputy is told: the pull request, the run,
1639/// what each answer does, and the evidence saved when the question was filed.
1640///
1641/// Rebuilt for every deputy turn, it is still a snapshot of that moment, so it
1642/// says so and points at `magi show` / `gh pr view` for anything current.
1643/// `state` is `None` for a run that cannot be read; what is missing is named as
1644/// missing, and no past approval basis is rebuilt from today's state.
1645pub fn deputy_brief(q: &ask::Question, state: Option<&RunState>) -> String {
1646    let mut s = format!(
1647        "This is the merge approval for run {run} (`magi show {run}`). The question's \
1648         own text above names the pull request. Answering `{APPROVE}` squash-merges \
1649         it into the base branch; `{HOLD}` leaves the pull request open. Silence is a \
1650         hold: the owner not answering never merges. When the owner tells you to \
1651         merge, in whatever words, settle `{APPROVE}`; when they ask for what another \
1652         offered choice does, settle that one. If you cannot tell what they want, ask \
1653         with `magi ask --thread`.\n\n\
1654         This brief is a snapshot from when your turn started: check `magi show {run}` \
1655         and `gh pr view` (read-only) before telling the owner anything current. The \
1656         driver carries out the merge, repair, withdrawal or replacement: do not \
1657         change files, branches or the pull request yourself.",
1658        run = q.run
1659    );
1660    if let Some(target) = &q.merge_target {
1661        s.push_str(&format!("\n\nThis decision names PR {} at head `{}`. A settled `{APPROVE}` merges exactly that head; a newer head is never merged on this answer. Withdrawal explicitly closes the PR and ends the task. Replacement closes the old PR before dispatching a new attempt with the selected implementer; do not start a separate task for it yourself.", target.pr, target.head));
1662    }
1663    if q.choices.iter().any(|c| c == REPAIR) {
1664        s.push_str("\n\n`fix findings and re-review` requests one repair and bounded re-review on the existing PR. It never approves a merge. The driver performs the repair; do not edit or queue another task for it. Updated review and gate results require a fresh merge decision.");
1665    }
1666    if q.detail.contains("Approval required because:") || q.detail.contains("承認が必要な理由:")
1667    {
1668        s.push_str("\n\nSaved approval evidence (at question creation):\n");
1669        s.push_str(&q.detail);
1670    } else {
1671        s.push_str("\n\nApproval reasons and final review evidence were not recorded in this legacy question. Do not infer its original trigger from current settings; consult the saved panel and review history.");
1672    }
1673    let Some(state) = state else {
1674        s.push_str(
1675            "\n\nThe run's record could not be read, so the pull request, the panel \
1676             summary and any contested findings are not known to you beyond the \
1677             question's own text. Say so to the owner rather than guessing.",
1678        );
1679        return s;
1680    };
1681    if let Some(pr) = &state.pr {
1682        s.push_str(&format!(
1683            "\n\nPull request #{} {} (recorded state: {}, last seen).",
1684            pr.number, pr.url, pr.state
1685        ));
1686    }
1687    s.push_str(&format!("\nBase branch: `{}`.", state.base_branch));
1688    if let Some(w) = state.winner() {
1689        s.push_str(&format!("\nWinning branch: `{}`.", w.branch));
1690    }
1691
1692    s
1693}
1694
1695// Releasing a task permits work to resume; it does not renew merge consent.
1696// Check the persisted operator decision both when reusing a card and at the
1697// final merge boundary, since a hold/release can arrive between those reads.
1698fn merge_consent_superseded(state: &RunState, question: &ask::Question) -> Result<bool> {
1699    if !question.status.open() && approval(question.resolution().as_deref()) != Approval::Merge {
1700        return Ok(false);
1701    }
1702    let Some(task_id) = state
1703        .origin
1704        .as_ref()
1705        .and_then(|origin| origin.task.as_ref())
1706    else {
1707        return Ok(false);
1708    };
1709    let task = crate::queue::Queue::open()
1710        .get(task_id)
1711        .context("cannot verify operator decisions for the merge approval")?;
1712    Ok(task
1713        .operator_decided_at
1714        .is_some_and(|at| at > question.answered_at.unwrap_or(question.asked_at)))
1715}
1716
1717/// Ask the owner before merging, with the whole case attached as a panel.
1718///
1719/// The evidence is gathered from the winner's own worktree with the `git` CLI,
1720/// never from the network, so a phone on a slow link gets the diff magi is
1721/// looking at rather than a link it has to go and open.
1722///
1723/// Never blocks. `land` used to sit inside [`ask::ask_and_wait`]'s poll loop
1724/// for up to a day right here, which held the whole run's task claim - and
1725/// the daemon's one slot with it - for exactly as long as the owner took to
1726/// notice their phone. [`ApprovalGate::Pending`] is the answer that lets the
1727/// caller park the run and hand the slot back instead: the question is on
1728/// disk either way, so nothing about the wait itself changes, only who is
1729/// blocked on it.
1730///
1731/// Idempotent across resumes: called again for a run already waiting on its
1732/// own question, this finds that question by [`crate::ask::Questions::list`]
1733/// rather than filing a second one - asking twice would double the
1734/// notification for one decision, and leave the first question's panel an
1735/// orphan nobody's answer ever reaches.
1736async fn approval_gate(
1737    state: &mut RunState,
1738    pr: &PrState,
1739    subject: &str,
1740    contested: Option<&ContestedHandoff>,
1741    head: &str,
1742) -> Result<ApprovalGate> {
1743    let _chat_merge = crate::chat_merge::target_lock_wait(&crate::run::home(), &pr.url).await?;
1744    let store = ask::Questions::open();
1745    // An answer belongs to the commit its panel showed. A question recorded
1746    // for another head - or none recorded at all, as for a question filed
1747    // before heads were tracked - is never reused: a fix or rebase push made
1748    // a commit the owner has not seen, and their word does not carry over.
1749    let reusable = state.land_approval.as_ref().and_then(|a| {
1750        store
1751            .list()
1752            .into_iter()
1753            .find(|q| q.id == a.question)
1754            .filter(|q| {
1755                q.status != ask::QuestionStatus::Abandoned
1756                    && (a.head.eq_ignore_ascii_case(head)
1757                        || q.resolution()
1758                            .as_deref()
1759                            .is_some_and(crate::merge_decision::stops))
1760            })
1761    });
1762    let reusable = match reusable {
1763        Some(q) if merge_consent_superseded(state, &q)? => None,
1764        other => other,
1765    };
1766    if reusable.is_none() {
1767        for stale in store
1768            .list()
1769            .into_iter()
1770            .filter(|q| q.run == state.id && q.node == APPROVAL_NODE && q.status.open())
1771        {
1772            let why =
1773                "the pull request head or operator decision changed; asked again for approval";
1774            if let Err(e) = store.update(&stale.id, |q| {
1775                q.abandon(why);
1776                Ok(())
1777            }) {
1778                tracing::warn!("could not retire the superseded approval question: {e:#}");
1779            }
1780        }
1781    }
1782
1783    let q = match reusable {
1784        Some(mut q) => {
1785            if upgrade_repair_choice(&mut q, state) {
1786                q = store
1787                    .update(&q.id, |current| {
1788                        upgrade_repair_choice(current, state);
1789                        Ok(())
1790                    })?
1791                    .0;
1792            }
1793            q
1794        }
1795        None => {
1796            let worktree = match state.winner() {
1797                Some(w) => w.worktree.clone(),
1798                None => state.repo.clone(),
1799            };
1800            // The diff is built from the commit being approved, not from the
1801            // branch name, which may already point somewhere else.
1802            let head = if head.is_empty() {
1803                state
1804                    .winner()
1805                    .map_or_else(|| "HEAD".to_owned(), |w| w.branch.clone())
1806            } else {
1807                head.to_owned()
1808            };
1809            // The diff is against what the remote's base holds, never the
1810            // local branch of that name; unreadable means less evidence.
1811            let remote = &state.config.merge.remote;
1812            let tracking = format!("{remote}/{}", state.base_branch);
1813            let base = if git::rev_exists(&worktree, &tracking).await {
1814                tracking
1815            } else {
1816                String::new()
1817            };
1818            let range = format!("{base}...{head}");
1819            // A failed `git` must not decide the merge: the panel degrades to
1820            // less evidence and the owner still chooses. Merging because the
1821            // diff could not be read would be the worst of both.
1822            let numstat = if base.is_empty() {
1823                String::new()
1824            } else {
1825                git::git_raw(&worktree, &["diff", "--numstat", "-M", &range])
1826                    .await
1827                    .map(|o| o.stdout)
1828                    .unwrap_or_default()
1829            };
1830            let diff = if base.is_empty() {
1831                String::new()
1832            } else {
1833                git::diff(&worktree, &base, &head).await.unwrap_or_default()
1834            };
1835            let commits: Vec<String> = if base.is_empty() {
1836                Vec::new()
1837            } else {
1838                git::git_raw(
1839                    &worktree,
1840                    &[
1841                        "log",
1842                        "--reverse",
1843                        "--format=%s",
1844                        &format!("{base}..{head}"),
1845                    ],
1846                )
1847                .await
1848                .map(|o| o.stdout)
1849                .unwrap_or_default()
1850                .lines()
1851                .filter(|l| !l.trim().is_empty())
1852                .map(str::to_owned)
1853                .collect()
1854            };
1855
1856            let w = words(&state.config.graph.language);
1857            let html = approval_panel(state, pr, &numstat, &diff, &commits, subject);
1858            let mut fresh = ask::Question::new(
1859                state.id.clone(),
1860                APPROVAL_NODE.to_owned(),
1861                "land".to_owned(),
1862                w.approval_summary(pr.number, subject),
1863                format!(
1864                    "{}\n\n{}",
1865                    approval_evidence(state),
1866                    w.approval_detail(&pr.url, &state.base_branch, subject, contested)
1867                ),
1868                approval_choices(state),
1869            );
1870            fresh.merge_target = Some(crate::merge_decision::Target {
1871                pr: pr.url.clone(),
1872                head: head.clone(),
1873                language: state.config.graph.language.clone(),
1874            });
1875            if fresh.choices.iter().any(|c| c == REPAIR) {
1876                fresh.actions.insert(
1877                    REPAIR.to_owned(),
1878                    ask::ChoiceAction::Resume {
1879                        run: state.id.clone(),
1880                    },
1881                );
1882                fresh.detail.push_str("\n\n`fix findings and re-review` runs one repair and bounded review on this PR; it does not approve a merge. A fresh decision follows the updated review and gate.");
1883            }
1884            for choice in fresh.choices.clone() {
1885                if crate::merge_decision::stops(&choice)
1886                    && choice != HOLD
1887                    && choice != crate::merge_decision::DISMISS
1888                {
1889                    fresh.actions.insert(
1890                        choice,
1891                        ask::ChoiceAction::Resume {
1892                            run: state.id.clone(),
1893                        },
1894                    );
1895                }
1896            }
1897            store
1898                .put_panel(&mut fresh, &html, &[])
1899                .context("write the merge approval panel")?;
1900            store
1901                .put_executable(&mut fresh)
1902                .context("file the merge approval question")?;
1903            state.land_approval = Some(LandApproval {
1904                question: fresh.id.clone(),
1905                head: head.clone(),
1906            });
1907            state.event(
1908                "land",
1909                format!("asking for merge approval ({})", fresh.short()),
1910            );
1911            state.save()?;
1912            if let Err(e) = ask::notify(&state.config.notify, &fresh).await {
1913                // A broken webhook is not a reason to lose the merge: the
1914                // question is already on disk and the web UI already shows
1915                // it, so the operator still has a way in.
1916                tracing::warn!(
1917                    "could not notify about merge approval question {}: {e:#} - \
1918                     the web UI is the only surface for it now",
1919                    fresh.short()
1920                );
1921            }
1922            fresh
1923        }
1924    };
1925
1926    Ok(match q.status {
1927        ask::QuestionStatus::Open => ApprovalGate::Pending,
1928        // Nobody answered before `state.config.graph.answer_timeout` passed,
1929        // or the question was closed with no decision recorded underneath
1930        // this run - either way there is nothing left to wait on.
1931        ask::QuestionStatus::Abandoned => ApprovalGate::Held,
1932        // The merge gate does not speak `--thread`: an owner who talked back
1933        // instead of choosing never reaches `Answered`, so this arm only
1934        // ever sees an actual decision.
1935        ask::QuestionStatus::Answered => match approval(q.resolution().as_deref()) {
1936            Approval::Merge => ApprovalGate::Approved,
1937            Approval::Repair if !state.reviews.is_empty() => {
1938                if !state.merge_repairs.iter().any(|r| r.question == q.id) {
1939                    state.merge_repairs.push(crate::run::MergeRepair {
1940                        question: q.id.clone(),
1941                        head: head.to_owned(),
1942                        pr: pr.url.clone(),
1943                        reviews: state.reviews.clone(),
1944                        gate: state.gate.clone(),
1945                        gate_head: state.gate_head.clone(),
1946                        gate_fixes: state.gate_fixes.clone(),
1947                        pre_gate: state.pre_gate.clone(),
1948                        phase: crate::run::MergeRepairPhase::Requested,
1949                        failure: None,
1950                    });
1951                    state.land_approval = None;
1952                    state.event(
1953                        "repair",
1954                        "owner requested fix findings and re-review; merge is not approved",
1955                    );
1956                    state.save()?;
1957                }
1958                ApprovalGate::Repair
1959            }
1960            Approval::Repair | Approval::Hold => ApprovalGate::Held,
1961        },
1962    })
1963}
1964
1965/// Fold a rollup's entries into one verdict plus the failing checks' labels.
1966/// No I/O. An empty rollup is `Unknown`, never green.
1967fn rollup_verdict(rollup: &[GhCheck]) -> (Checks, Vec<String>) {
1968    let mut failing = Vec::new();
1969    let mut pending = false;
1970    let mut unknown = false;
1971    for check in rollup {
1972        match check.verdict() {
1973            Verdict::Pass => {}
1974            Verdict::Pending => pending = true,
1975            Verdict::Fail => failing.push(check.label()),
1976            Verdict::Unknown => unknown = true,
1977        }
1978    }
1979    let checks = if rollup.is_empty() {
1980        Checks::Unknown
1981    } else if pending {
1982        Checks::Pending
1983    } else if !failing.is_empty() {
1984        Checks::Red
1985    } else if unknown {
1986        Checks::Unknown
1987    } else {
1988        Checks::Green
1989    };
1990    (checks, failing)
1991}
1992
1993/// Parse `gh pr view --json url,number,state,statusCheckRollup,reviews,comments`
1994/// output into a [`PrState`]. No I/O.
1995pub fn parse_pr(json: &str) -> Result<PrState> {
1996    let raw: GhPr = serde_json::from_str(json).context("parse `gh pr view --json ...` output")?;
1997    let state = match raw.state.to_ascii_uppercase().as_str() {
1998        "OPEN" => PrLifecycle::Open,
1999        "MERGED" => PrLifecycle::Merged,
2000        "CLOSED" => PrLifecycle::Closed,
2001        other => bail!("unknown pull request state `{other}`"),
2002    };
2003
2004    let (checks, failing) = rollup_verdict(&raw.status_check_rollup);
2005
2006    let mut review_comments = Vec::new();
2007    for r in raw.reviews {
2008        push_if_outstanding(
2009            &mut review_comments,
2010            ReviewComment {
2011                author: r.author.login,
2012                path: None,
2013                line: None,
2014                body: r.body,
2015            },
2016        );
2017    }
2018    for c in raw.comments {
2019        push_if_outstanding(
2020            &mut review_comments,
2021            ReviewComment {
2022                author: c.author.login,
2023                path: None,
2024                line: None,
2025                body: c.body,
2026            },
2027        );
2028    }
2029
2030    Ok(PrState {
2031        url: raw.url,
2032        number: raw.number,
2033        state,
2034        checks,
2035        failing,
2036        review_comments,
2037        blocking: Blocking::of(&raw.merge_state_status),
2038    })
2039}
2040
2041/// One rollup entry as the release watcher reads it.
2042#[derive(Debug, Clone, PartialEq, Eq)]
2043pub(crate) struct CheckView {
2044    pub name: String,
2045    pub verdict: Verdict,
2046    /// Workflow run behind the check, when its url names one.
2047    pub run: Option<String>,
2048    pub url: Option<String>,
2049}
2050
2051/// A pull request's lifecycle, head commit and per-check verdicts, without
2052/// [`rollup_verdict`]'s aggregation (a pending check anywhere hides a failure
2053/// from it, which is exactly what the release watcher must not inherit).
2054#[derive(Debug, Clone, PartialEq, Eq)]
2055pub(crate) struct RollupView {
2056    pub url: String,
2057    pub number: u64,
2058    pub state: PrLifecycle,
2059    pub head: String,
2060    pub checks: Vec<CheckView>,
2061}
2062
2063/// Parse `gh pr view --json url,number,state,headRefOid,statusCheckRollup`
2064/// output. No I/O.
2065pub(crate) fn parse_rollup(json: &str) -> Result<RollupView> {
2066    let raw: GhPr = serde_json::from_str(json).context("parse `gh pr view --json ...` output")?;
2067    let state = match raw.state.to_ascii_uppercase().as_str() {
2068        "OPEN" => PrLifecycle::Open,
2069        "MERGED" => PrLifecycle::Merged,
2070        "CLOSED" => PrLifecycle::Closed,
2071        other => bail!("unknown pull request state `{other}`"),
2072    };
2073    let checks = raw
2074        .status_check_rollup
2075        .iter()
2076        .map(|c| CheckView {
2077            name: c.label(),
2078            verdict: c.verdict(),
2079            run: c.url().and_then(run_of),
2080            url: c.url().map(str::to_owned),
2081        })
2082        .collect();
2083    Ok(RollupView {
2084        url: raw.url,
2085        number: raw.number,
2086        state,
2087        head: raw.head_ref_oid,
2088        checks,
2089    })
2090}
2091
2092/// Read just a pull request's lifecycle state - open, merged, or closed -
2093/// with none of the checks/reviews/comments [`land`] itself needs to decide
2094/// what to do next.
2095///
2096/// For a caller that only ever wants one fact and must not risk anything
2097/// else: `magi fold --merged` uses this to confirm a URL the operator hands
2098/// it is actually a merged pull request *before* touching a run's state, so a
2099/// typo or a still-open PR fails loudly instead of quietly recording a merge
2100/// that never happened.
2101pub async fn lifecycle(repo: &Path, pr_url: &str) -> Result<PrLifecycle> {
2102    let view = gh(
2103        repo,
2104        &[
2105            "pr".to_owned(),
2106            "view".to_owned(),
2107            pr_url.to_owned(),
2108            "--json".to_owned(),
2109            "state".to_owned(),
2110        ],
2111    )
2112    .await?;
2113    if !view.0 {
2114        bail!("gh pr view {pr_url}: {}", view.1);
2115    }
2116    // `parse_pr` reads every other field of `GhPr` as its serde default
2117    // (empty string, empty vec, zero) when this narrower `--json` selection
2118    // does not carry them - harmless, since only `.state` is read back.
2119    Ok(parse_pr(&view.1)?.state)
2120}
2121
2122/// A pull request the operator merged outside of `land::land`'s own loop,
2123/// found by asking GitHub about the run's own winning branch rather than
2124/// requiring the operator to go and find the URL themselves.
2125#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2126pub struct ExternalMerge {
2127    /// The pull request's URL, ready to hand to [`correct_manual_merge`].
2128    pub url: String,
2129    /// The pull request's number.
2130    pub number: u64,
2131}
2132
2133#[derive(Debug, Deserialize)]
2134#[serde(rename_all = "camelCase")]
2135struct GhMergedPr {
2136    url: String,
2137    number: u64,
2138    merged_at: String,
2139    base_ref_name: String,
2140}
2141
2142/// Pure half of [`find_external_merge`]: given the raw `gh pr list --head
2143/// <branch> --state merged --json url,number,mergedAt,baseRefName` output,
2144/// decide whether exactly one of the pull requests it lists could actually
2145/// be *this* run's.
2146///
2147/// A branch name alone does not prove it: [`RunState::branch_for`] derives it
2148/// from the run's own short id, so a collision with some other, unrelated
2149/// task's merged pull request from a same-named branch is rare but not
2150/// impossible once branches are deleted and ids run out. Filtering on
2151/// `base_ref_name` (the branch this run actually targets) and `merged_at`
2152/// (which cannot predate the run itself) rules that case out. More than one
2153/// survivor is exactly as uninformative as zero — something this run cannot
2154/// tell apart from another — so only a unique survivor is returned.
2155fn pick_merged_pr(
2156    json: &str,
2157    base_branch: &str,
2158    created_at: Timestamp,
2159) -> Result<Option<ExternalMerge>> {
2160    let raw: Vec<GhMergedPr> =
2161        serde_json::from_str(json).context("parse `gh pr list ... --json ...` output")?;
2162    let mut matches: Vec<ExternalMerge> = Vec::new();
2163    for pr in raw {
2164        if pr.base_ref_name != base_branch {
2165            continue;
2166        }
2167        let Ok(merged_at) = pr.merged_at.parse::<Timestamp>() else {
2168            continue;
2169        };
2170        if merged_at < created_at {
2171            continue;
2172        }
2173        matches.push(ExternalMerge {
2174            url: pr.url,
2175            number: pr.number,
2176        });
2177    }
2178    if matches.len() == 1 {
2179        Ok(matches.pop())
2180    } else {
2181        Ok(None)
2182    }
2183}
2184
2185/// What `gh pr list --head <branch> --base <base> --state open` found.
2186#[derive(Debug, Clone, PartialEq, Eq)]
2187pub enum OpenPr {
2188    /// Nothing open: the caller creates one.
2189    None,
2190    /// Exactly one: the caller adopts it instead of creating a second.
2191    One {
2192        /// The pull request's URL.
2193        url: String,
2194        /// Its current title.
2195        title: String,
2196    },
2197    /// More than one: magi does not pick between them.
2198    Many(Vec<String>),
2199}
2200
2201#[derive(Debug, Deserialize)]
2202#[serde(rename_all = "camelCase")]
2203struct GhOpenPr {
2204    // `url` and `baseRefName` are required: a record missing either must be a
2205    // parse error, not a pull request that silently fails the base filter and
2206    // reads as "none open" (which would go on to create a duplicate).
2207    url: String,
2208    #[serde(default)]
2209    title: String,
2210    base_ref_name: String,
2211    // A fork's branch of the same name is not the task head; `gh pr list --head`
2212    // matches by branch name alone, so the filter has to be ours.
2213    #[serde(default)]
2214    is_cross_repository: bool,
2215}
2216
2217/// Pure half of [`find_open_pr`]: classify the raw `--json
2218/// number,url,title,baseRefName` output. Entries whose base is not `base` are
2219/// dropped even though the query already filtered on it, so a stub or an old
2220/// `gh` that ignores `--base` cannot get a pull request into the wrong branch
2221/// adopted.
2222pub fn pick_open_pr(json: &str, base: &str) -> Result<OpenPr> {
2223    let raw: Vec<GhOpenPr> =
2224        serde_json::from_str(json).context("parse `gh pr list ... --json ...` output")?;
2225    let mut hits: Vec<GhOpenPr> = raw
2226        .into_iter()
2227        .filter(|p| p.base_ref_name == base && !p.is_cross_repository)
2228        .collect();
2229    Ok(match hits.len() {
2230        0 => OpenPr::None,
2231        1 => {
2232            let p = hits.remove(0);
2233            OpenPr::One {
2234                url: p.url,
2235                title: p.title,
2236            }
2237        }
2238        _ => OpenPr::Many(hits.into_iter().map(|p| p.url).collect()),
2239    })
2240}
2241
2242/// Open pull requests whose head is `branch` and whose base is `base`. A
2243/// failing `gh` is an error carrying its own output, never "none": guessing
2244/// there is how a duplicate gets created.
2245pub async fn find_open_pr(repo: &Path, branch: &str, base: &str) -> Result<OpenPr> {
2246    let (ok, out) = gh(
2247        repo,
2248        &[
2249            "pr".to_owned(),
2250            "list".to_owned(),
2251            "--head".to_owned(),
2252            branch.to_owned(),
2253            "--base".to_owned(),
2254            base.to_owned(),
2255            "--state".to_owned(),
2256            "open".to_owned(),
2257            "--json".to_owned(),
2258            "number,url,title,baseRefName,isCrossRepository".to_owned(),
2259        ],
2260    )
2261    .await?;
2262    if !ok {
2263        bail!("gh pr list failed: {out}");
2264    }
2265    pick_open_pr(&out, base)
2266}
2267
2268#[derive(Debug, Deserialize)]
2269#[serde(rename_all = "camelCase")]
2270struct GhPrHead {
2271    head_ref_name: String,
2272    base_ref_name: String,
2273    state: String,
2274    // Required, like `GhOpenPr`'s fields: a record that cannot say whether the
2275    // head lives in a fork must be a parse error, never "same repository".
2276    is_cross_repository: bool,
2277    // Required too: it is what ties the pull request to the commits that were
2278    // actually proven to be on the base.
2279    head_ref_oid: String,
2280}
2281
2282/// Why [`closable`] said no, and whether asking again later could say yes.
2283#[derive(Debug, Clone, PartialEq, Eq)]
2284pub struct Refusal {
2285    /// A later attempt may succeed (the head moved, the view was unreadable);
2286    /// `false` means this pull request is simply not the run's to close.
2287    pub retry: bool,
2288    /// What was wrong.
2289    pub why: String,
2290}
2291
2292impl Refusal {
2293    fn final_(why: String) -> Self {
2294        Self { retry: false, why }
2295    }
2296}
2297
2298/// Pure half of [`close_superseded_pr`]: read `gh pr view --json
2299/// headRefName,baseRefName,state,isCrossRepository` and say whether closing
2300/// is safe, `Err` carrying the reason when it is not.
2301///
2302/// Closing is outward-facing, so every property is checked on what the forge
2303/// says now, not on what magi recorded: the head must be exactly `branch` in
2304/// this repository (a fork's branch of the same name is somebody else's), the
2305/// base must be `base`, and the pull request must still be open.
2306pub fn closable(
2307    json: &str,
2308    branch: &str,
2309    base: &str,
2310    verified: &[String],
2311) -> std::result::Result<(), Refusal> {
2312    let pr: GhPrHead = serde_json::from_str(json).map_err(|e| Refusal {
2313        retry: true,
2314        why: format!("could not read the pull request ({e})"),
2315    })?;
2316    if pr.head_ref_name != branch {
2317        return Err(Refusal::final_(format!(
2318            "its head is `{}`, not this run's `{branch}`",
2319            pr.head_ref_name
2320        )));
2321    }
2322    if pr.is_cross_repository {
2323        return Err(Refusal::final_("its head lives in a fork".to_owned()));
2324    }
2325    if pr.base_ref_name != base {
2326        return Err(Refusal::final_(format!(
2327            "it targets `{}`, not `{base}`",
2328            pr.base_ref_name
2329        )));
2330    }
2331    if !pr.state.eq_ignore_ascii_case("open") {
2332        return Err(Refusal::final_(format!(
2333            "it is already {}",
2334            pr.state.to_ascii_lowercase()
2335        )));
2336    }
2337    // Last, so a pull request that is not this run's at all is reported as
2338    // such. A head that is this branch but not a commit checked against the
2339    // base (somebody pushed since) may well get checked next time: retry.
2340    if !verified.contains(&pr.head_ref_oid) {
2341        return Err(Refusal {
2342            retry: true,
2343            why: format!(
2344                "its head {} is not a commit this run checked against the base",
2345                crate::already::short_sha(&pr.head_ref_oid)
2346            ),
2347        });
2348    }
2349    Ok(())
2350}
2351
2352/// Does `remote` point at something a forge could host - a URL or an scp-style
2353/// `user@host:path` - rather than a filesystem path or nothing at all? Judged
2354/// from the URL alone, so it answers the same on every machine, whatever `gh`
2355/// happens to be installed or logged in to.
2356async fn remote_is_forge(repo: &Path, remote: &str) -> bool {
2357    let Ok(url) = git::git(repo, &["remote", "get-url", remote]).await else {
2358        return false;
2359    };
2360    is_forge_url(url.trim())
2361}
2362
2363fn is_forge_url(url: &str) -> bool {
2364    url.contains("://") && !url.starts_with("file://")
2365        || url
2366            .split_once(':')
2367            .is_some_and(|(host, _)| host.contains('@') && !host.contains(['/', '\\']))
2368}
2369
2370/// Does this `gh` failure mean there is no GitHub to ask, as opposed to a
2371/// request that failed?
2372fn forge_unavailable(message: &str) -> bool {
2373    message.contains("known GitHub host") || message.contains("spawn gh")
2374}
2375
2376/// The comment left on a pull request closed because its change is already on
2377/// the base.
2378pub fn superseded_comment(base: &str, evidence: &crate::already::Evidence) -> String {
2379    let how = match evidence.proof {
2380        crate::already::Proof::PatchId => format!(
2381            "carried by commit {} on `{base}` with the same patch",
2382            evidence.names()
2383        ),
2384        crate::already::Proof::Ancestry => {
2385            format!("already in the history of `{base}` as {}", evidence.names())
2386        }
2387        crate::already::Proof::Tree => format!(
2388            "already part of `{base}` (merging this branch changes nothing at {})",
2389            crate::already::short_sha(&evidence.tip)
2390        ),
2391    };
2392    format!(
2393        "Closing: everything this branch adds is {how}, so there is nothing left to \
2394         land. This pull request was closed automatically after that was verified; \
2395         reopen it if you disagree."
2396    )
2397}
2398
2399/// Close the open pull request for `branch`, if there is one and it is
2400/// provably this run's, with a comment naming what supersedes it. `Ok(Ok(url))` is
2401/// the URL closed; `Ok(Err(why))` is a final "nothing to close" (no pull request,
2402/// not this run's, no forge); `Err` is anything a later attempt could resolve (a
2403/// failed `gh` call, a head that moved since it was checked), which the caller
2404/// must not treat as settled.
2405///
2406/// The recorded `state.pr` is preferred, else the forge is asked for an open
2407/// pull request on `branch`; either way the candidate is re-read and passed
2408/// through [`closable`] before anything is written; `verified` lists the
2409/// commits proven to be on the base, and the pull request's head must be one.
2410pub async fn close_superseded_pr(
2411    state: &mut RunState,
2412    branch: &str,
2413    evidence: &crate::already::Evidence,
2414    verified: &[String],
2415) -> Result<std::result::Result<String, String>> {
2416    let repo = state.repo.clone();
2417    let base = state.base_branch.clone();
2418    let url = match state.pr.as_ref().filter(|p| p.state == "open") {
2419        Some(p) => p.url.clone(),
2420        // No recorded pull request. A forge that cannot be asked at all (no
2421        // GitHub remote, no `gh`) says nothing about this run, so that is
2422        // "none found"; any other lookup failure is an error, because "could
2423        // not look" is not "nothing there" and the caller must retry.
2424        None if !remote_is_forge(&repo, &state.config.merge.remote).await => {
2425            return Ok(Err(
2426                "the remote is not a forge, so there is no pull request".to_owned(),
2427            ));
2428        }
2429        None => match find_open_pr(&repo, branch, &base).await {
2430            Err(e) if forge_unavailable(&format!("{e:#}")) => {
2431                return Ok(Err(format!("no forge to ask: {e:#}")));
2432            }
2433            Err(e) => return Err(e),
2434            Ok(OpenPr::One { url, .. }) => url,
2435            Ok(OpenPr::None) => return Ok(Err("no open pull request".to_owned())),
2436            Ok(OpenPr::Many(urls)) => {
2437                return Ok(Err(format!(
2438                    "{} open pull requests name it; not choosing between them",
2439                    urls.len()
2440                )));
2441            }
2442        },
2443    };
2444    let (ok, view) = gh(
2445        &repo,
2446        &[
2447            "pr".to_owned(),
2448            "view".to_owned(),
2449            url.clone(),
2450            "--json".to_owned(),
2451            "headRefName,headRefOid,baseRefName,state,isCrossRepository".to_owned(),
2452        ],
2453    )
2454    .await?;
2455    if !ok {
2456        bail!("gh pr view {url} failed: {view}");
2457    }
2458    if let Err(refusal) = closable(&view, branch, &base, verified) {
2459        // A pull request whose head cannot be tied to what was proven is not
2460        // one to walk away from: the caller keeps the run resumable.
2461        if refusal.retry {
2462            bail!("left {url} open: {}", refusal.why);
2463        }
2464        return Ok(Err(format!("left {url} open: {}", refusal.why)));
2465    }
2466    let (ok, out) = gh(
2467        &repo,
2468        &[
2469            "pr".to_owned(),
2470            "close".to_owned(),
2471            url.clone(),
2472            "--comment".to_owned(),
2473            superseded_comment(&base, evidence),
2474        ],
2475    )
2476    .await?;
2477    if !ok {
2478        bail!("gh pr close {url} failed: {out}");
2479    }
2480    if let Some(p) = state.pr.as_mut().filter(|p| p.url == url) {
2481        p.state = "closed".to_owned();
2482    }
2483    Ok(Ok(url))
2484}
2485
2486/// `gh pr edit <url> --title <title>`, for an adopted pull request whose title
2487/// differs from the one this run computed. Only the title: the body may have
2488/// been edited by the owner and cannot be compared.
2489pub async fn set_pr_title(state: &mut RunState, repo: &Path, url: &str, title: &str) -> Result<()> {
2490    let (title, _) = crate::github_text::prepare(state, title, "");
2491    edit_pr_title(repo, url, &title).await
2492}
2493
2494/// [`set_pr_title`] for a title the owner explicitly chose to post unmodified
2495/// (`github_text::POST_ORIGINAL` or `github_text::USE_REWRITE`): the posting
2496/// gate is skipped on purpose.
2497pub async fn set_pr_title_verbatim(repo: &Path, url: &str, title: &str) -> Result<()> {
2498    edit_pr_title(repo, url, title).await
2499}
2500
2501/// Post both fields of the owner's chosen rewrite to an adopted pull request,
2502/// unmodified and without the posting gate.
2503pub async fn set_pr_text_verbatim(repo: &Path, url: &str, title: &str, body: &str) -> Result<()> {
2504    let (ok, _) = gh(
2505        repo,
2506        &[
2507            "pr".to_owned(),
2508            "edit".to_owned(),
2509            url.to_owned(),
2510            "--title".to_owned(),
2511            title.to_owned(),
2512            "--body".to_owned(),
2513            body.to_owned(),
2514        ],
2515    )
2516    .await
2517    .map_err(|_| anyhow::anyhow!("could not run gh pr edit for the chosen rewrite"))?;
2518    if !ok {
2519        // Command output may echo the chosen text; keep it out of run events.
2520        bail!("gh pr edit failed while posting the chosen rewrite");
2521    }
2522    Ok(())
2523}
2524
2525async fn edit_pr_title(repo: &Path, url: &str, title: &str) -> Result<()> {
2526    let (ok, out) = gh(
2527        repo,
2528        &[
2529            "pr".to_owned(),
2530            "edit".to_owned(),
2531            url.to_owned(),
2532            "--title".to_owned(),
2533            title.to_owned(),
2534        ],
2535    )
2536    .await?;
2537    if !ok {
2538        bail!("gh pr edit failed: {out}");
2539    }
2540    Ok(())
2541}
2542
2543/// Ask GitHub whether this run's winning candidate branch was actually merged
2544/// somewhere `land::land`'s own loop never saw — the gap `magi fold
2545/// --merged` exists to close, minus the operator having to find the URL by
2546/// hand.
2547///
2548/// `Ok(None)` covers every case where nothing can be said with confidence: no
2549/// winner decided yet (nothing to check a branch for), no merged pull request
2550/// found, or [`pick_merged_pr`] found more than one candidate and would not
2551/// guess between them. Never wired to a weaker, URL-less signal like
2552/// [`branch_is_ancestor`] — a caller wanting that has to ask for it
2553/// separately, precisely because it cannot drive an automatic correction on
2554/// its own (see that function's own doc).
2555pub async fn find_external_merge(state: &RunState) -> Result<Option<ExternalMerge>> {
2556    let Some(winner) = state.winner() else {
2557        return Ok(None);
2558    };
2559    let branch = state.pr_branch().unwrap_or(&winner.branch).to_owned();
2560    let out = gh(
2561        &state.repo,
2562        &[
2563            "pr".to_owned(),
2564            "list".to_owned(),
2565            "--head".to_owned(),
2566            branch.clone(),
2567            "--state".to_owned(),
2568            "merged".to_owned(),
2569            "--json".to_owned(),
2570            "url,number,mergedAt,baseRefName".to_owned(),
2571        ],
2572    )
2573    .await?;
2574    if !out.0 {
2575        bail!("gh pr list --head {branch}: {}", out.1);
2576    }
2577    pick_merged_pr(&out.1, &state.base_branch, state.created_at)
2578}
2579
2580/// Whether `branch` is, right now, an ancestor of `base_branch` in the local
2581/// git graph — the weaker, URL-less signal that a branch landed somewhere.
2582///
2583/// Deliberately never consulted by [`find_external_merge`]: a base branch
2584/// that has moved since the run started can make an old, abandoned branch
2585/// look like an ancestor of the *current* base for reasons that have nothing
2586/// to do with a merge (a later commit that happens to supersede it, an
2587/// unrelated squash), and there is no pull request URL here to confirm
2588/// against or to land through anyway. Its only honest use is a weaker
2589/// notice — "this looks merged, go check" — never an automatic rewrite of
2590/// `status`/`merge`.
2591pub async fn branch_is_ancestor(repo: &Path, branch: &str, base_branch: &str) -> Result<bool> {
2592    let out = tokio::process::Command::new("git")
2593        .args(["merge-base", "--is-ancestor", branch, base_branch])
2594        .current_dir(repo)
2595        .quiet()
2596        .stdin(std::process::Stdio::null())
2597        .output()
2598        .await
2599        .context("spawn git merge-base --is-ancestor")?;
2600    Ok(out.status.success())
2601}
2602
2603/// Parse `host/owner/repo` out of a forge URL, with no network access.
2604///
2605/// The host is part of the slug, not discarded: `owner/repo` alone would
2606/// treat `github.example.com/o/r` and `github.com/o/r` as the same
2607/// repository, which is exactly the mix-up the same-repo guard exists to
2608/// catch. Returns `None` for anything that doesn't have a `<host>/<path>`
2609/// shape at all.
2610fn forge_slug(url: &str) -> Option<(String, &str)> {
2611    let rest = url.rsplit("://").next()?;
2612    let (host, path) = rest.split_once('/')?;
2613    if host.is_empty() {
2614        return None;
2615    }
2616    Some((host.to_ascii_lowercase(), path))
2617}
2618
2619/// Parse `host/owner/repo` out of a GitHub pull request URL, with no network
2620/// access - the first half of the same-repo guard [`correct_manual_merge`]
2621/// applies before it writes anything.
2622///
2623/// Returns `None` for anything that does not look like
2624/// `https://<host>/<owner>/<repo>/pull/<n>`, which the caller treats as
2625/// fail-closed: a URL this cannot make sense of refuses rather than guesses.
2626pub(crate) fn slug_of_pr_url(url: &str) -> Option<String> {
2627    let (host, path) = forge_slug(url)?;
2628    let mut segments = path.split('/');
2629    let owner = segments.next()?;
2630    let repo = segments.next()?;
2631    let kind = segments.next()?;
2632    if owner.is_empty() || repo.is_empty() || kind != "pull" {
2633        return None;
2634    }
2635    Some(format!("{host}/{owner}/{repo}"))
2636}
2637
2638/// Parse `host/owner/repo` out of a plain repository URL (no `/pull/<n>`
2639/// suffix), the shape `gh repo view --json url` returns - the other half of
2640/// the same-repo guard, matched against [`slug_of_pr_url`]'s output.
2641fn slug_of_repo_url(url: &str) -> Option<String> {
2642    let (host, path) = forge_slug(url)?;
2643    let mut segments = path.split('/');
2644    let owner = segments.next()?;
2645    let repo = segments.next()?;
2646    if owner.is_empty() || repo.is_empty() {
2647        return None;
2648    }
2649    Some(format!("{host}/{owner}/{repo}"))
2650}
2651
2652/// Refuse to correct a run against a pull request from a different
2653/// repository than the one it is recorded against.
2654///
2655/// This is the guard the shun/8c75 incident argued for: an operator ran
2656/// `magi fold --merged <shun PR url>` meaning to correct an old `Blocked` run
2657/// in a different repository, omitted the run id, and the id defaulted to
2658/// this machine's most recently created run - an unrelated, still-in-progress
2659/// run in a completely different repository - which then had its `status`
2660/// rewritten to `merged` from a pull request it had nothing to do with.
2661/// `correct_manual_merge` now requires an explicit id (see `magi fold`'s own
2662/// CLI help), but a mistyped or stale id could still name a run in a
2663/// different repository than the one the URL belongs to, so this checks that
2664/// independently rather than trusting the id alone.
2665///
2666/// Comparison is case-insensitive - GitHub owner/repo names are - and a
2667/// mismatch names both slugs rather than just refusing, so an operator whose
2668/// local checkout's `origin` is a fork of the repository the pull request was
2669/// opened against (a legitimate setup this cannot tell apart from a genuine
2670/// mix-up) can judge for themselves rather than being blocked with no way to
2671/// see why.
2672pub(crate) fn ensure_same_repo(run_repo_slug: &str, pr_repo_slug: &str) -> Result<()> {
2673    if run_repo_slug.eq_ignore_ascii_case(pr_repo_slug) {
2674        return Ok(());
2675    }
2676    bail!(
2677        "refusing to correct this run: it is recorded against {run_repo_slug}, but the pull \
2678         request URL belongs to {pr_repo_slug} - pass the run id whose repository the URL \
2679         actually belongs to (or, if `origin` is a fork opened against a different upstream, \
2680         verify by hand before treating this as a false positive)"
2681    );
2682}
2683
2684/// Ask the forge which `host/owner/repo` a local checkout's `origin` remote
2685/// actually resolves to, for the same-repo guard in [`correct_manual_merge`].
2686///
2687/// Asking `gh` rather than parsing `git remote -v` locally is deliberate: it
2688/// normalizes case, SSH vs. HTTPS remotes, and a renamed or transferred
2689/// repository the same way GitHub itself would recognize it, so the
2690/// comparison in [`ensure_same_repo`] is against the same canonical slug on
2691/// both sides. Reads `url` rather than `nameWithOwner` so the host is part of
2692/// the answer too - `nameWithOwner` alone cannot tell a `github.com` repo from
2693/// a same-named one on a GitHub Enterprise host.
2694async fn repo_slug(repo: &Path) -> Result<String> {
2695    let out = gh(
2696        repo,
2697        &[
2698            "repo".to_owned(),
2699            "view".to_owned(),
2700            "--json".to_owned(),
2701            "url".to_owned(),
2702        ],
2703    )
2704    .await?;
2705    if !out.0 {
2706        bail!("gh repo view --json url: {}", out.1);
2707    }
2708    #[derive(Debug, Deserialize)]
2709    struct GhRepo {
2710        url: String,
2711    }
2712    let parsed: GhRepo = serde_json::from_str(&out.1)
2713        .with_context(|| format!("parse `gh repo view` output: {}", out.1))?;
2714    slug_of_repo_url(&parsed.url)
2715        .with_context(|| format!("could not parse a host/owner/repo out of {}", parsed.url))
2716}
2717
2718/// Confirm `url` is actually a merged pull request, then rewrite `state`'s
2719/// `status` and `merge` exactly as the automatic land loop (`land::land`)
2720/// would have written them had magi opened and merged this pull request
2721/// itself.
2722///
2723/// This is `magi fold --merged`'s whole implementation, and also what the
2724/// web `fold-merged` route calls once it has a URL in hand — an operator
2725/// recovery path for a merge magi could not finish on its own: a PR title too
2726/// long for the GraphQL mutation, `gh pr create` unreachable, a stale token -
2727/// closed by hand with a pull request magi never opened and so never
2728/// recorded. Reusing `land::land` rather than writing `status`/`merge`
2729/// directly keeps this one authoritative: a merged pull request decides
2730/// `Step::Done { merged: true }` on the very first read, before any of
2731/// `land`'s own checks/fix/rebase machinery can run, which is what makes it
2732/// safe to call here even though this pull request was never magi's own.
2733///
2734/// [`ensure_same_repo`] is checked before anything else: a pull request from
2735/// a different repository than the one `state` is recorded against is
2736/// refused outright, regardless of its lifecycle. This is the guard for a
2737/// URL an *operator* hands in - the CLI or the web route - where a stale or
2738/// mistyped run id could otherwise get corrected from an unrelated
2739/// repository's pull request (see the shun/8c75 incident in `magi fold`'s own
2740/// CLI help). The automatic janitor sweep (`clean::reconcile_external_merges`)
2741/// goes through [`correct_confirmed_external_merge`] instead, which skips
2742/// this check: its `url` was never operator-supplied, it comes from
2743/// [`find_external_merge`] querying `gh` from inside `state.repo` itself, so
2744/// it is already guaranteed to name a pull request in that same repository -
2745/// re-deriving and re-checking the repository here would only be a second
2746/// `gh repo view` call that can fail for reasons that have nothing to do with
2747/// correctness (a rate limit, a network blip), turning a self-heal that would
2748/// otherwise have succeeded into a run left `Blocked` for another pass.
2749///
2750/// [`lifecycle`] is checked next and separately so a mistyped or still-open
2751/// URL fails loudly without writing anything, rather than handing an open
2752/// pull request to the full autonomous loop by accident.
2753///
2754/// Correcting `status` this way does not run `bump::after_merge`
2755/// (`src/bump.rs`): that call is made only from `graph::Runner::run_land`,
2756/// which this path never goes through. A release version bump the change
2757/// might have earned is therefore not filed automatically and has to be
2758/// requested by hand - recorded as an event on the run so the gap is visible
2759/// to whoever reads it later, not just wherever this was called from. Follow-up
2760/// tasks for findings the merge left open (`crate::followup`) *are* filed here.
2761///
2762/// Returns the status before and after, so every caller (CLI, janitor, web
2763/// route) can build its own log line or response from the same pair rather
2764/// than each re-deriving it.
2765pub async fn correct_manual_merge(
2766    state: &mut RunState,
2767    url: &str,
2768) -> Result<(RunStatus, RunStatus)> {
2769    let Some(pr_slug) = slug_of_pr_url(url) else {
2770        bail!(
2771            "could not parse an owner/repo out of {url}; refusing to guess which repository \
2772             this pull request belongs to"
2773        );
2774    };
2775    let run_slug = repo_slug(&state.repo).await?;
2776    ensure_same_repo(&run_slug, &pr_slug)?;
2777    correct_merge(state, url).await
2778}
2779
2780/// The janitor's own entry point into the same correction
2781/// [`correct_manual_merge`] performs for an operator-supplied URL, minus the
2782/// same-repo guard - see that function's own doc for why skipping it here is
2783/// safe rather than a hole: [`clean::reconcile_external_merges`] only ever
2784/// calls this with a `url` [`find_external_merge`] already found by querying
2785/// `state.repo`'s own remote, so the guard could never do anything here but
2786/// fail on its own transient errors.
2787///
2788/// [`crate::clean`] is the only caller; `pub(crate)` rather than private only
2789/// because it lives in a different module.
2790pub(crate) async fn correct_confirmed_external_merge(
2791    state: &mut RunState,
2792    url: &str,
2793) -> Result<(RunStatus, RunStatus)> {
2794    correct_merge(state, url).await
2795}
2796
2797/// Same pull request, same repository: the url, or the number within one repo.
2798fn names_same_pr(a: &RunState, url: &str, number: u64, repo: &Path) -> bool {
2799    let Some(pr) = a.pr.as_ref() else {
2800        return false;
2801    };
2802    if !url.is_empty()
2803        && pr
2804            .url
2805            .trim_end_matches('/')
2806            .eq_ignore_ascii_case(url.trim_end_matches('/'))
2807    {
2808        return true;
2809    }
2810    number > 0
2811        && pr.number == number
2812        && match (a.repo.canonicalize(), repo.canonicalize()) {
2813            (Ok(x), Ok(y)) => x == y,
2814            _ => a.repo == repo,
2815        }
2816}
2817
2818/// Rewrite `pr.state` to a final state on every run record under `home` that
2819/// `decide` picks, and report how many were rewritten.
2820///
2821/// Lifecycle writes through to other runs are deliberately narrow (task-head
2822/// ownership is recorded separately): only a **terminal** run (never one a
2823/// driver may still be writing), only a proven-stopped driver with no daemon
2824/// claim (the shared liveness judge), only a record
2825/// whose `pr.state` is still `open`, and only `merged` / `closed` ever goes in.
2826/// The record is read as folding reads it (no schema check: every bump so far
2827/// only added fields, and the whole struct round-trips) and written through
2828/// [`RunState::save_under`], the path every record uses, so nothing but
2829/// `pr.state` and an event line changes (and `updated_at`, as for any save).
2830/// A run that cannot be read or written is skipped with a warning.
2831fn rewrite_open_prs(
2832    home: &Path,
2833    decide: &mut dyn FnMut(&RunState) -> Option<PrLifecycle>,
2834) -> usize {
2835    let now = Timestamp::now();
2836    let mut changed = 0;
2837    for id in crate::run::list_ids_in(&home.join("runs")) {
2838        let Ok(_owner) = crate::pr_head::owner_lock(home, &id) else {
2839            continue;
2840        };
2841        let path = home.join("runs").join(&id).join("run.json");
2842        let Ok(body) = std::fs::read_to_string(&path) else {
2843            continue;
2844        };
2845        let Ok(mut state) = serde_json::from_str::<RunState>(&body) else {
2846            continue;
2847        };
2848        if !state.status.done()
2849            || state.pr.as_ref().is_none_or(|p| p.state != "open")
2850            || state.liveness_under(home, crate::daemon::is_working_on(home, &id, now))
2851                != crate::run::Liveness::Dead
2852        {
2853            continue;
2854        }
2855        let Some(to @ (PrLifecycle::Merged | PrLifecycle::Closed)) = decide(&state) else {
2856            continue;
2857        };
2858        if let Some(pr) = state.pr.as_mut() {
2859            pr.state = to.as_str().to_owned();
2860        }
2861        let url = state.pr.as_ref().map(|p| p.url.clone()).unwrap_or_default();
2862        state.event(
2863            "land",
2864            format!("recorded {url} as {}: another run settled it", to.as_str()),
2865        );
2866        match state.save_under(home) {
2867            Ok(()) => changed += 1,
2868            Err(e) => tracing::warn!("write pr state through to run {id}: {e:#}"),
2869        }
2870    }
2871    changed
2872}
2873
2874/// A run reached a final state for its pull request: tell every other terminal
2875/// run in the same repository that names the same pull request (handed-over
2876/// predecessors, blocked attempts, anything), so their records stop saying
2877/// `open`. Best effort; `run`'s own record is the caller's.
2878pub(crate) fn write_pr_state_through(run: &RunState, to: PrLifecycle) {
2879    if to == PrLifecycle::Open {
2880        return;
2881    }
2882    let Some(home) = crate::run::try_home() else {
2883        return;
2884    };
2885    write_pr_state_through_in(&home, run, to);
2886}
2887
2888pub(crate) fn write_pr_state_through_in(home: &Path, run: &RunState, to: PrLifecycle) -> usize {
2889    let Some(pr) = run.pr.as_ref() else {
2890        return 0;
2891    };
2892    let (url, number) = (pr.url.clone(), pr.number);
2893    rewrite_open_prs(home, &mut |other| {
2894        (other.id != run.id && names_same_pr(other, &url, number, &run.repo)).then_some(to)
2895    })
2896}
2897
2898/// Terminal runs whose record still says their pull request is open, as
2899/// `(run id, repo, url)`, for [`repair_stale_pr_states`].
2900pub(crate) fn stale_open_prs(home: &Path) -> Vec<(String, PathBuf, String)> {
2901    let now = Timestamp::now();
2902    let mut out = Vec::new();
2903    for id in crate::run::list_ids_in(&home.join("runs")) {
2904        let path = home.join("runs").join(&id).join("run.json");
2905        let Ok(body) = std::fs::read_to_string(&path) else {
2906            continue;
2907        };
2908        let Ok(state) = serde_json::from_str::<RunState>(&body) else {
2909            continue;
2910        };
2911        if let Some(pr) = state.pr.as_ref()
2912            && state.status.done()
2913            && pr.state == "open"
2914            && !pr.url.is_empty()
2915            && !crate::daemon::is_working_on(home, &id, now)
2916        {
2917            out.push((id, state.repo.clone(), pr.url.clone()));
2918        }
2919    }
2920    out
2921}
2922
2923/// Apply forge answers (`pr url -> state`) to every stale terminal record.
2924/// A url with no answer (the forge was unreadable) changes nothing.
2925pub(crate) fn apply_pr_states(home: &Path, known: &BTreeMap<String, PrLifecycle>) -> usize {
2926    rewrite_open_prs(home, &mut |s| {
2927        s.pr.as_ref().and_then(|p| known.get(&p.url)).copied()
2928    })
2929}
2930
2931/// One-time (and idempotent) repair of records that froze an `open` pull
2932/// request: ask the forge about each distinct pull request that a terminal run
2933/// still calls open - at most `max_lookups` of them - and rewrite the merged
2934/// and closed ones. A genuinely open pull request is left alone, and a lookup
2935/// that fails is "unknown, change nothing". Returns `(records rewritten,
2936/// lookups that failed)`.
2937pub async fn repair_stale_pr_states(home: &Path, max_lookups: usize) -> (usize, usize) {
2938    let mut known = BTreeMap::new();
2939    let mut failed = 0;
2940    let mut seen = BTreeSet::new();
2941    for (_, repo, url) in stale_open_prs(home) {
2942        if known.len() + failed >= max_lookups || !seen.insert(url.clone()) {
2943            continue;
2944        }
2945        match lifecycle(&repo, &url).await {
2946            Ok(state) => {
2947                known.insert(url, state);
2948            }
2949            Err(e) => {
2950                tracing::warn!("repair pr state of {url}: {e:#}");
2951                failed += 1;
2952            }
2953        }
2954    }
2955    (apply_pr_states(home, &known), failed)
2956}
2957
2958async fn correct_merge(state: &mut RunState, url: &str) -> Result<(RunStatus, RunStatus)> {
2959    match lifecycle(&state.repo, url).await? {
2960        PrLifecycle::Merged => {}
2961        other => bail!(
2962            "{url} is {}, not merged; refusing to record {} as merged on a guess",
2963            other.as_str(),
2964            state.id
2965        ),
2966    }
2967    let before = state.status;
2968    if let Err(e) = land(state, url).await {
2969        // `land` sets `status` to `Landing` and saves before its first read
2970        // of the pull request — see its own doc — so a failure here (a
2971        // transient `gh` hiccup between the two forge reads this function
2972        // makes) can leave the run stuck on that in-between value with
2973        // nothing left driving it. Land it on the same terminal shape an
2974        // automated `land` failure lands on instead of leaving it stuck.
2975        state.status = RunStatus::Blocked;
2976        state.event("fold", format!("manual-merge correction failed: {e:#}"));
2977        state.save()?;
2978        return Err(e).context(format!("confirming the merge of {url}"));
2979    }
2980    state.event(
2981        "fold",
2982        "operator recorded this pull request as a manual merge; this run never \
2983         re-entered `land`, so `bump::after_merge` did not run for it - a release \
2984         bump this change might warrant has to be filed by hand",
2985    );
2986    // Unlike the bump, the findings the merge left open are filed on this
2987    // path too: nothing else would ever carry them forward.
2988    if state.status == RunStatus::Merged {
2989        crate::followup::after_merge(state, url).await;
2990    }
2991    state.save()?;
2992    Ok((before, state.status))
2993}
2994
2995/// Parse `gh api repos/{owner}/{repo}/pulls/<n>/comments` into inline review
2996/// comments. No I/O.
2997///
2998/// `gh pr view` does not surface inline comments, and inline is exactly where
2999/// both review bots put their findings - a landing loop that read only the
3000/// top-level thread would never see the thing it is supposed to fix.
3001pub fn parse_inline_comments(json: &str) -> Result<Vec<ReviewComment>> {
3002    let raw: Vec<GhInline> =
3003        serde_json::from_str(json).context("parse `gh api .../pulls/<n>/comments` output")?;
3004    let mut out = Vec::new();
3005    for c in raw {
3006        push_if_outstanding(
3007            &mut out,
3008            ReviewComment {
3009                author: c.user.login,
3010                path: c.path,
3011                line: c.line,
3012                body: c.body,
3013            },
3014        );
3015    }
3016    Ok(out)
3017}
3018
3019/// Keep a comment only when it asks for something.
3020///
3021/// An inline comment always does: it names a file and a line. A top-level
3022/// comment is dropped when it is empty, when it is magi's own, or when it is
3023/// [noise](is_noise).
3024fn push_if_outstanding(out: &mut Vec<ReviewComment>, comment: ReviewComment) {
3025    if comment.body.trim().is_empty() || comment.body.contains(MARKER) {
3026        return;
3027    }
3028    if comment.path.is_none() && is_noise(&comment.body) {
3029        return;
3030    }
3031    out.push(comment);
3032}
3033
3034/// Is this comment body machinery rather than a finding?
3035///
3036/// Two tests, both structural, because guessing from prose is how a "looks
3037/// good to me" turns into a fix round:
3038///
3039/// 1. The bot said so - the body carries one of the [`NOT_A_REVIEW`] markers
3040///    with which CodeRabbit labels its trigger notice, its walkthrough, and its
3041///    footer.
3042/// 2. It asks for nothing - once HTML comments, `<details>` blocks, headings,
3043///    horizontal rules, and the bot's own status banner are removed, every
3044///    remaining line is a task-list item. That is exactly the shape of the
3045///    comment the Claude review job posts while it is still working.
3046///
3047/// Anything else is input, including bot prose. A bot that writes a paragraph
3048/// has said something, and the fix prompt tells the fixer it may decline a
3049/// comment with an argument - a wasted sentence in a prompt is cheaper than a
3050/// missed finding.
3051pub fn is_noise(body: &str) -> bool {
3052    if NOT_A_REVIEW.iter().any(|m| body.contains(m)) {
3053        return true;
3054    }
3055    let mut content = false;
3056    for line in strip_blocks(body).lines() {
3057        let line = unquote(line);
3058        if line.is_empty() || is_checklist(line) || is_decoration(line) || is_banner(line) {
3059            continue;
3060        }
3061        content = true;
3062        break;
3063    }
3064    !content
3065}
3066
3067/// Remove HTML comments and collapsed `<details>` blocks.
3068fn strip_blocks(body: &str) -> String {
3069    let mut out = String::with_capacity(body.len());
3070    let mut rest = body;
3071    loop {
3072        let open = ["<!--", "<details>"]
3073            .iter()
3074            .filter_map(|tag| rest.find(tag).map(|i| (i, *tag)))
3075            .min_by_key(|(i, _)| *i);
3076        let Some((at, tag)) = open else {
3077            out.push_str(rest);
3078            return out;
3079        };
3080        out.push_str(&rest[..at]);
3081        let after = &rest[at + tag.len()..];
3082        let close = if tag == "<!--" { "-->" } else { "</details>" };
3083        match after.find(close) {
3084            Some(end) => rest = &after[end + close.len()..],
3085            // Unterminated: the rest of the body is inside the block.
3086            None => return out,
3087        }
3088    }
3089}
3090
3091/// Strip blockquote markers, which both bots wrap their callouts in.
3092fn unquote(line: &str) -> &str {
3093    let mut s = line.trim();
3094    while let Some(rest) = s.strip_prefix('>') {
3095        s = rest.trim_start();
3096    }
3097    s.trim()
3098}
3099
3100/// `- [ ]` / `- [x]`, in any of the bullet styles GitHub renders.
3101fn is_checklist(line: &str) -> bool {
3102    let rest = line
3103        .strip_prefix("- ")
3104        .or_else(|| line.strip_prefix("* "))
3105        .unwrap_or("");
3106    let rest = rest.trim_start();
3107    matches!(
3108        rest.get(..3),
3109        Some("[ ]") | Some("[x]") | Some("[X]") | Some("[*]")
3110    )
3111}
3112
3113/// A heading, a horizontal rule, or a callout tag - shape, never content.
3114fn is_decoration(line: &str) -> bool {
3115    line.starts_with('#')
3116        || line.starts_with("[!")
3117        || (line.len() >= 3 && line.chars().all(|c| matches!(c, '-' | '=' | '*' | '_')))
3118}
3119
3120/// A line that is nothing but emphasis and links.
3121///
3122/// Both review jobs open with a status banner
3123/// (`**Claude finished ... in 4m 14s** —— [View job](url)`). It reads as prose
3124/// to a line-based test and asks for nothing, so it is measured the same way a
3125/// heading is: strip the markup, and if no word survives, it was decoration.
3126fn is_banner(line: &str) -> bool {
3127    let plain = drop_spans(line, "**", "**");
3128    let plain = if plain.contains("](") {
3129        drop_spans(&plain, "[", ")")
3130    } else {
3131        plain
3132    };
3133    !plain.chars().any(char::is_alphanumeric)
3134}
3135
3136/// Remove every `open` .. `close` span, including the delimiters. An
3137/// unterminated span swallows the rest of the input, which is what a reader
3138/// sees too.
3139fn drop_spans(s: &str, open: &str, close: &str) -> String {
3140    let mut out = String::with_capacity(s.len());
3141    let mut rest = s;
3142    while let Some(at) = rest.find(open) {
3143        out.push_str(&rest[..at]);
3144        let after = &rest[at + open.len()..];
3145        match after.find(close) {
3146            Some(end) => rest = &after[end + close.len()..],
3147            None => return out,
3148        }
3149    }
3150    out.push_str(rest);
3151    out
3152}
3153
3154/// The lock that keeps at most one run per repository actually moving the
3155/// base branch at a time: a rebase push, or `gh pr merge`.
3156///
3157/// Deliberately narrow. Everything else in [`land`]'s loop - watching CI,
3158/// running a fix round in the winner's own worktree, waiting on the owner's
3159/// approval - touches nothing a *different* run in the same repository could
3160/// collide with, and holding a lock across any of that would serialise one
3161/// run's CI wait (up to [`WAIT_CEILING`]) against another run's land-approval
3162/// resume, which is precisely the "must not wait on another task" property
3163/// the daemon's slot-freeing exists to give a resume. Only the two moments
3164/// that actually write to the shared base branch need mutual exclusion, and
3165/// both are brief.
3166///
3167/// One entry per repository, each its own `tokio::sync::Mutex`, so two
3168/// different repositories' runs never wait on each other. The outer
3169/// `std::sync::Mutex` guards only the map itself, held long enough to find or
3170/// insert an entry and clone its `Arc`, never across an `.await`.
3171fn repo_merge_lock(repo: &Path) -> Arc<tokio::sync::Mutex<()>> {
3172    static LOCKS: std::sync::LazyLock<
3173        std::sync::Mutex<BTreeMap<PathBuf, Arc<tokio::sync::Mutex<()>>>>,
3174    > = std::sync::LazyLock::new(|| std::sync::Mutex::new(BTreeMap::new()));
3175    LOCKS
3176        .lock()
3177        .unwrap_or_else(std::sync::PoisonError::into_inner)
3178        .entry(repo.to_path_buf())
3179        .or_insert_with(|| Arc::new(tokio::sync::Mutex::new(())))
3180        .clone()
3181}
3182
3183/// `owner/repo` out of a pull request url, falling back to the checkout's
3184/// directory name when the url is not the usual `host/owner/repo/pull/N`.
3185fn repo_label(repo: &Path, pr_url: &str) -> String {
3186    let parts: Vec<&str> = pr_url.split('/').collect();
3187    if let Some(at) = parts.iter().rposition(|p| *p == "pull")
3188        && at >= 2
3189        && !parts[at - 1].is_empty()
3190        && !parts[at - 2].is_empty()
3191    {
3192        return format!("{}/{}", parts[at - 2], parts[at - 1]);
3193    }
3194    repo.file_name()
3195        .map(|n| n.to_string_lossy().into_owned())
3196        .unwrap_or_default()
3197}
3198
3199/// The operator-facing sentence for a merge that went ahead with red checks,
3200/// or `None` when the checks were not red. Judged on `checks`, not on
3201/// `failing`, which can be non-empty on a green observation.
3202fn red_merge_summary(repo_name: &str, pr: &PrState) -> Option<String> {
3203    (pr.checks == Checks::Red).then(|| {
3204        let details = crate::notices::format_list(&pr.failing)
3205            .map(|names| format!(": {names}"))
3206            .unwrap_or_default();
3207        format!(
3208            "Merged {repo_name} PR #{} with red checks{details} ({})",
3209            pr.number, pr.url
3210        )
3211    })
3212}
3213
3214/// After a merge that succeeded: record which checks were red and tell the
3215/// operator. The decision to merge is already made; this only makes it audible.
3216/// Best-effort - a broken notifier never fails the run.
3217async fn announce_red_merge(state: &mut RunState, pr: &PrState) {
3218    let repo_name = repo_label(&state.repo, &pr.url);
3219    let Some(summary) = red_merge_summary(&repo_name, pr) else {
3220        return;
3221    };
3222    if let Some(rec) = state.pr.as_mut() {
3223        rec.red_at_merge = pr.failing.clone();
3224    }
3225    state.event("land", summary.clone());
3226    // The notice pages through `[notify]` itself, once, unless a question
3227    // already carries the cause.
3228    crate::notices::raise_with(
3229        crate::notices::merged_red(&state.id, &summary),
3230        &state.config.notify,
3231    );
3232}
3233
3234/// Run the loop against a real pull request until it merges or the budget runs
3235/// out.
3236///
3237/// The caller decides whether landing happens at all: this is only reached when
3238/// `graph.land` is on. Returns the last observation, so the caller can report
3239/// what magi was looking at when it stopped.
3240pub async fn land(state: &mut RunState, pr_url: &str) -> Result<PrState> {
3241    land_with(state, pr_url, &GhForge).await
3242}
3243
3244/// The forge calls whose timing the loop's decisions depend on, behind a seam
3245/// so a test can script what the pull request looks like from one observation
3246/// to the next. Comments, logs and rebases stay on `gh` and `git` directly.
3247trait Forge {
3248    async fn close(&self, repo: &Path, pr_url: &str, comment: &str) -> Result<()> {
3249        let (ok, out) = gh(
3250            repo,
3251            &[
3252                "pr".into(),
3253                "close".into(),
3254                pr_url.into(),
3255                "--comment".into(),
3256                comment.into(),
3257            ],
3258        )
3259        .await?;
3260        if !ok {
3261            bail!("PR closure failed: {out}");
3262        }
3263        Ok(())
3264    }
3265    async fn attribution(&self, _state: &RunState, _seen: &Seen) -> Option<CiEvidence> {
3266        None
3267    }
3268    async fn view(&self, repo: &Path, pr_url: &str) -> Result<Seen>;
3269    async fn merge(&self, repo: &Path, argv: &[String]) -> Result<(bool, String)>;
3270    async fn poll(&self);
3271    /// Rerun the failed jobs of one workflow run.
3272    async fn rerun(&self, repo: &Path, run: &str) -> Result<()>;
3273    /// Unix seconds, behind the seam so a test owns the clock.
3274    fn now(&self) -> i64;
3275    /// The check contexts the base branch requires, for a stop reason only.
3276    /// `None` when they could not be read, which is not the same as none.
3277    async fn required_contexts(&self, repo: &Path, base: &str) -> Option<BTreeSet<String>>;
3278    #[allow(clippy::too_many_arguments)]
3279    async fn fix(
3280        &self,
3281        state: &mut RunState,
3282        pr: &PrState,
3283        round: usize,
3284        budget: usize,
3285        reason: &str,
3286        logs: &str,
3287    ) -> Result<Fixed>;
3288}
3289
3290struct GhForge;
3291
3292impl Forge for GhForge {
3293    async fn attribution(&self, state: &RunState, seen: &Seen) -> Option<CiEvidence> {
3294        collect_ci_evidence(state, seen).await
3295    }
3296    async fn view(&self, repo: &Path, pr_url: &str) -> Result<Seen> {
3297        observe(repo, pr_url).await
3298    }
3299    async fn merge(&self, repo: &Path, argv: &[String]) -> Result<(bool, String)> {
3300        gh(repo, argv).await
3301    }
3302    async fn poll(&self) {
3303        tokio::time::sleep(POLL).await;
3304    }
3305    async fn rerun(&self, repo: &Path, run: &str) -> Result<()> {
3306        let args = [
3307            "run".to_owned(),
3308            "rerun".to_owned(),
3309            run.to_owned(),
3310            "--failed".to_owned(),
3311        ];
3312        let (ok, out) = gh(repo, &args).await?;
3313        if !ok {
3314            bail!("gh run rerun {run}: {out}");
3315        }
3316        Ok(())
3317    }
3318    fn now(&self) -> i64 {
3319        jiff::Timestamp::now().as_second()
3320    }
3321    async fn required_contexts(&self, repo: &Path, base: &str) -> Option<BTreeSet<String>> {
3322        required_contexts_of(repo, base).await
3323    }
3324    async fn fix(
3325        &self,
3326        state: &mut RunState,
3327        pr: &PrState,
3328        round: usize,
3329        budget: usize,
3330        reason: &str,
3331        logs: &str,
3332    ) -> Result<Fixed> {
3333        fix_round(state, pr, round, budget, reason, logs).await
3334    }
3335}
3336
3337/// Percent-encode a branch name for a URL path segment (`/` included).
3338fn encode_path_segment(s: &str) -> String {
3339    let mut out = String::new();
3340    for b in s.bytes() {
3341        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
3342            out.push(b as char);
3343        } else {
3344            let _ = write!(out, "%{b:02X}");
3345        }
3346    }
3347    out
3348}
3349
3350/// Read the required contexts of `base` from classic branch protection and
3351/// from rulesets, once, for a stop reason. Organisation-level rulesets that
3352/// these two endpoints do not list are missed. Any unreadable source makes the
3353/// whole answer `None`: a partial set would read as a complete one.
3354async fn required_contexts_of(repo: &Path, base: &str) -> Option<BTreeSet<String>> {
3355    let enc = encode_path_segment(base);
3356    let mut all = BTreeSet::new();
3357    // Classic protection answers 404 for an unprotected branch, which is
3358    // "nothing required here", not a failure to read.
3359    let classic = gh(
3360        repo,
3361        &[
3362            "api".to_owned(),
3363            format!("repos/{{owner}}/{{repo}}/branches/{enc}/protection/required_status_checks"),
3364        ],
3365    )
3366    .await
3367    .ok()?;
3368    if classic.0 {
3369        all.extend(parse_classic_required(&classic.1)?);
3370    } else if !classic.1.contains("404") {
3371        return None;
3372    }
3373    let rules = gh(
3374        repo,
3375        &[
3376            "api".to_owned(),
3377            format!("repos/{{owner}}/{{repo}}/rules/branches/{enc}"),
3378        ],
3379    )
3380    .await
3381    .ok()?;
3382    if !rules.0 {
3383        return None;
3384    }
3385    all.extend(parse_ruleset_required(&rules.1)?);
3386    Some(all)
3387}
3388
3389/// `required_status_checks` of classic protection: `contexts` plus the
3390/// `checks[].context` form.
3391fn parse_classic_required(json: &str) -> Option<BTreeSet<String>> {
3392    let v: serde_json::Value = serde_json::from_str(json).ok()?;
3393    let mut out = BTreeSet::new();
3394    for c in v.get("contexts")?.as_array()? {
3395        out.insert(c.as_str()?.to_owned());
3396    }
3397    for c in v
3398        .get("checks")
3399        .and_then(|c| c.as_array())
3400        .into_iter()
3401        .flatten()
3402    {
3403        if let Some(name) = c.get("context").and_then(|n| n.as_str()) {
3404            out.insert(name.to_owned());
3405        }
3406    }
3407    Some(out)
3408}
3409
3410/// `rules/branches/<base>`: every `required_status_checks` rule's contexts.
3411fn parse_ruleset_required(json: &str) -> Option<BTreeSet<String>> {
3412    let v: serde_json::Value = serde_json::from_str(json).ok()?;
3413    let mut out = BTreeSet::new();
3414    for rule in v.as_array()? {
3415        if rule.get("type").and_then(|t| t.as_str()) != Some("required_status_checks") {
3416            continue;
3417        }
3418        let checks = rule
3419            .pointer("/parameters/required_status_checks")?
3420            .as_array()?;
3421        for c in checks {
3422            out.insert(c.get("context")?.as_str()?.to_owned());
3423        }
3424    }
3425    Some(out)
3426}
3427
3428/// Is the observation older than the commit a fix round pushed?
3429///
3430/// The forge takes a few seconds to move the pull request to a new head and
3431/// attach that head's check runs, and until it has, the rollup is the previous
3432/// head's - all green, which is exactly what a merge decision must not read.
3433/// An absent or different head counts as not yet: guessing "close enough"
3434/// would reopen the hole.
3435fn awaiting_new_head(awaiting: Option<&str>, observed: &str) -> bool {
3436    awaiting.is_some_and(|want| !observed.eq_ignore_ascii_case(want))
3437}
3438
3439/// The commit an observation may be decided on, or `None` while it cannot be
3440/// trusted to describe one.
3441///
3442/// `None` when a pushed commit is awaited and the pull request is not on it,
3443/// when the head is unreadable, or when the rollup (`statusCheckRollup` is the
3444/// last commit's) is not the head's: that is the previous commit's checks. The
3445/// caller re-polls on `None`. A rollup that cannot be read (including one
3446/// longer than a page) leaves `rollup_head` empty, so the wait runs to
3447/// [`WAIT_CEILING`] and stops - a safe failure, never a merge.
3448fn bound_head<'a>(
3449    seen_head: &'a str,
3450    rollup_head: &str,
3451    awaiting: Option<&str>,
3452) -> Option<&'a str> {
3453    if seen_head.is_empty()
3454        || awaiting_new_head(awaiting, seen_head)
3455        || !rollup_head.eq_ignore_ascii_case(seen_head)
3456    {
3457        return None;
3458    }
3459    Some(seen_head)
3460}
3461
3462/// What a refused `gh pr merge` means.
3463#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3464enum Refused {
3465    /// The branch policy is not satisfied *yet*: go back to waiting.
3466    Pending,
3467    /// Checks have settled and the merge state still says no, seen for the
3468    /// first time. The forge updates `mergeStateStatus` a moment after the last
3469    /// check finishes, so one more look is allowed before believing it.
3470    Recheck,
3471    /// Nothing is in flight and the policy still refuses: a person has to
3472    /// supply what it asks for (a review, say).
3473    Final,
3474}
3475
3476/// Judged from the pull request's state after the refusal, never from the
3477/// refusal's wording, which belongs to the forge and changes.
3478fn classify_refusal(
3479    after: Option<&Seen>,
3480    rechecked: bool,
3481    observed_head: &str,
3482    ci: CiExpectation,
3483) -> Refused {
3484    let Some(after) = after else {
3485        // Unreadable is not evidence of anything; the next loop reads again.
3486        return Refused::Pending;
3487    };
3488    if after.pr.state != PrLifecycle::Open {
3489        return Refused::Final;
3490    }
3491    // The branch moved after it was observed (the forge refuses a merge pinned
3492    // to the old commit): not a verdict on anything, look again.
3493    if !after.head.eq_ignore_ascii_case(observed_head) {
3494        return Refused::Pending;
3495    }
3496    // The same binding the loop applies: checks of another commit say nothing.
3497    if bound_head(&after.head, &after.rollup_head, None).is_none() {
3498        return Refused::Pending;
3499    }
3500    let state = after.merge_state.to_ascii_uppercase();
3501    if (ci == CiExpectation::Expected
3502        && matches!(after.pr.checks, Checks::Pending | Checks::Unknown))
3503        || state.is_empty()
3504        || state == "UNKNOWN"
3505    {
3506        return Refused::Pending;
3507    }
3508    if rechecked {
3509        Refused::Final
3510    } else {
3511        Refused::Recheck
3512    }
3513}
3514
3515/// Take auto-merge back from the forge and forget that it was armed.
3516///
3517/// `Err` carries the forge's message: the caller must not push or move on, as
3518/// an armed merge left behind could still fire for a commit nobody approved.
3519async fn disarm<F: Forge>(
3520    forge: &F,
3521    state: &mut RunState,
3522    repo: &Path,
3523    number: u64,
3524) -> std::result::Result<(), String> {
3525    let argv = disable_automerge_argv(number);
3526    let out = {
3527        let merge_lock = repo_merge_lock(repo);
3528        let _merge_slot = merge_lock.lock().await;
3529        forge.merge(repo, &argv).await
3530    };
3531    match out {
3532        Ok((true, _)) => {
3533            state.land_armed_head = None;
3534            state.event("land", "auto-merge disabled");
3535            state.save().map_err(|e| format!("{e:#}"))?;
3536            Ok(())
3537        }
3538        Ok((false, msg)) => Err(msg),
3539        Err(e) => Err(format!("{e:#}")),
3540    }
3541}
3542
3543/// [`stop`], first taking back an armed auto-merge so a pull request magi
3544/// gave up on does not merge on its own later. A failure to disarm is added
3545/// to the reason rather than hiding it.
3546async fn stop_disarmed<F: Forge>(
3547    forge: &F,
3548    state: &mut RunState,
3549    repo: &Path,
3550    pr: &PrState,
3551    why: &str,
3552) -> Result<()> {
3553    if state.land_armed_head.is_none() {
3554        return stop(state, repo, pr, why).await;
3555    }
3556    match disarm(forge, state, repo, pr.number).await {
3557        Ok(()) => stop(state, repo, pr, why).await,
3558        Err(e) => {
3559            let why = format!("{why} (auto-merge could not be disabled and may still fire: {e})");
3560            stop(state, repo, pr, &why).await
3561        }
3562    }
3563}
3564
3565/// Check the persisted card again around awaits that can outlive an owner tap.
3566fn owner_requested_stop(state: &RunState) -> Result<bool> {
3567    let Some(binding) = &state.land_approval else {
3568        return Ok(false);
3569    };
3570    let q = ask::Questions::open().get(&binding.question)?;
3571    Ok(q.run == state.id
3572        && q.node == APPROVAL_NODE
3573        && q.resolution()
3574            .as_deref()
3575            .is_some_and(crate::merge_decision::stops))
3576}
3577
3578/// Read stop choices before checks, repair or rebase. These are about stopping
3579/// ownership, so a changed head never cancels them as it cancels merge consent.
3580async fn owner_stop<F: Forge>(state: &mut RunState, pr: &mut PrState, forge: &F) -> Result<bool> {
3581    use crate::merge_decision as md;
3582    let Some(binding) = &state.land_approval else {
3583        return Ok(false);
3584    };
3585    let q = ask::Questions::open().get(&binding.question)?;
3586    let Some(choice) = q.resolution().filter(|c| md::stops(c)) else {
3587        return Ok(false);
3588    };
3589    if q.run != state.id || q.node != APPROVAL_NODE {
3590        bail!("stop decision names another run");
3591    }
3592    let target = q.merge_target.clone().unwrap_or_else(|| md::Target {
3593        pr: pr.url.clone(),
3594        head: binding.head.clone(),
3595        language: state.config.graph.language.clone(),
3596    });
3597    if target.pr != pr.url {
3598        bail!("stop decision names another PR");
3599    }
3600    if state.merge_decision.is_none() {
3601        state.merge_decision = Some(md::Decision {
3602            question: q.id.clone(),
3603            target,
3604            choice: choice.clone(),
3605            closed: false,
3606            replacement_run: None,
3607            replacement_pr: None,
3608        });
3609        state.save()?;
3610    }
3611    let repo = state.repo.clone();
3612    // Always disable, including arms left by an older driver.
3613    if let Err(e) = disarm(forge, state, &repo, pr.number).await {
3614        stop(state, &repo, pr, &format!("owner stop requested, but auto-merge could not be disabled: {e}; no repair or replacement started")).await?;
3615        return Ok(true);
3616    }
3617    if choice.eq_ignore_ascii_case(HOLD) {
3618        stop(
3619            state,
3620            &repo,
3621            pr,
3622            "owner held the decision: PR left open; no further repair or merge work",
3623        )
3624        .await?;
3625        return Ok(true);
3626    }
3627    if md::selected_agent(&choice).is_some()
3628        || state
3629            .origin
3630            .as_ref()
3631            .and_then(|o| o.task.as_ref())
3632            .is_some()
3633    {
3634        let owners: Vec<_> = crate::queue::Queue::open()
3635            .list()
3636            .into_iter()
3637            .filter(|task| task.runs.last() == Some(&state.id))
3638            .collect();
3639        let owner = (owners.len() == 1).then(|| &owners[0]);
3640        if !owner.is_some_and(|task| {
3641            task.runs.last() == Some(&state.id)
3642                && (!task
3643                    .operator_decided_at
3644                    .is_some_and(|at| at > q.answered_at.unwrap_or(q.asked_at))
3645                    || task.resumption.as_ref().is_some_and(|release| {
3646                        release.question_id.as_deref() == Some(q.id.as_str())
3647                            && task.operator_decided_at == Some(release.at)
3648                    }))
3649        }) {
3650            stop(state, &repo, pr, "replacement requires the current original queue task owner and an unsuperseded answer; PR left open").await?;
3651            return Ok(true);
3652        }
3653    }
3654    if pr.state == PrLifecycle::Merged {
3655        stop(
3656            state,
3657            &repo,
3658            pr,
3659            "PR already merged; withdrawal/replacement cannot be applied",
3660        )
3661        .await?;
3662        return Ok(true);
3663    }
3664    let comment = format!(
3665        "{MARKER}\nOwner decision on question {}: {}. Previous attempt: `{}`. Original instruction and review history remain in the task record.",
3666        q.id, choice, state.id
3667    );
3668    if pr.state == PrLifecycle::Open {
3669        if let Err(e) = forge.close(&repo, &pr.url, &comment).await {
3670            stop(
3671                state,
3672                &repo,
3673                pr,
3674                &format!(
3675                    "owner requested closure but it failed: {e}; task held, no replacement started"
3676                ),
3677            )
3678            .await?;
3679            return Ok(true);
3680        }
3681    }
3682    let confirmed = forge.view(&repo, &pr.url).await?;
3683    if confirmed.pr.state != PrLifecycle::Closed {
3684        stop(
3685            state,
3686            &repo,
3687            pr,
3688            "PR closure was not confirmed; task held, no replacement started",
3689        )
3690        .await?;
3691        return Ok(true);
3692    }
3693    *pr = confirmed.pr;
3694    state.merge_decision.as_mut().unwrap().closed = true;
3695    if let Some(record) = &mut state.pr {
3696        record.state = "closed".into();
3697    }
3698    state.status = RunStatus::Superseded;
3699    state.merge = Some(MergeOutcome {
3700        mode: MergeMode::Pr,
3701        ok: false,
3702        detail: format!("owner decision: {choice}; PR closure confirmed"),
3703        empty: false,
3704    });
3705    state.event(
3706        "land",
3707        format!(
3708            "{} applied: PR closure confirmed; old execution retired",
3709            choice
3710        ),
3711    );
3712    state.save()?;
3713    Ok(true)
3714}
3715
3716/// Keep an unstarted verification resumable without charging a task attempt.
3717fn check_landing_resources(
3718    state: &mut RunState,
3719    node: &str,
3720    outcomes: &[crate::run::CommandOutcome],
3721) -> Result<()> {
3722    if let Some(blocked) = outcomes.iter().find(|outcome| outcome.resource_blocked) {
3723        let detail = format!("{}: {}", blocked.command, blocked.output_tail);
3724        state
3725            .resource_block
3726            .get_or_insert_with(|| crate::run::ResourceBlock {
3727                cause: crate::run::ResourceBlockCause::Other,
3728                node: node.into(),
3729                kind: "shared build cache".into(),
3730                detail: detail.clone(),
3731                at: jiff::Timestamp::now(),
3732            });
3733        state.event("resource", &detail);
3734        state.save()?;
3735        bail!("landing verification could not start: {detail}");
3736    }
3737    Ok(())
3738}
3739
3740/// Gate the local winner and publish that exact SHA, retaining the saved lease.
3741/// A push completed before a crash is recoverable only when its SHA was gated.
3742async fn publish_landing_head<F: Forge>(
3743    state: &mut RunState,
3744    observed: &str,
3745    pr_url: &str,
3746    forge: &F,
3747) -> Result<String> {
3748    crate::pr_head::ensure_owner(state)?;
3749    let winner = state.winner().cloned().context("landing has no winner")?;
3750    let head = git::rev_parse(&winner.worktree, "HEAD").await?;
3751    let expected = state.pr_pushed_head.as_deref().unwrap_or(observed);
3752    anyhow::ensure!(
3753        observed == expected
3754            || (observed == head
3755                && state.gate_head.as_deref() == Some(&head)
3756                && state.gate_allows_progress()),
3757        "public head moved outside this run; refusing to overwrite it"
3758    );
3759    anyhow::ensure!(
3760        git::is_clean(&winner.worktree).await?,
3761        "landing worktree is dirty"
3762    );
3763    if state.gate_head.as_deref() != Some(&head)
3764        || !state.gate_ran
3765        || !state.gate_commands_allow_progress()
3766    {
3767        state.reconcile_verification_head(&head);
3768        state.gate.clear();
3769        state.gate_head = None;
3770        state.gate_ran = false;
3771        state.pre_gate.clear();
3772        state.pre_gate_commit = None;
3773        state.save()?;
3774        let inherited = crate::repo_lock::WaitPolicy::current();
3775        let wait = crate::repo_lock::WaitPolicy::new(
3776            Duration::from_secs(state.config.graph.timeout_resource_wait),
3777            move || inherited.cancelled(),
3778        );
3779        let mut outcomes = crate::graph::run_landing_gate(state, &winner, &wait).await?;
3780        loop {
3781            if outcomes.iter().any(|outcome| outcome.resource_blocked) {
3782                state.gate = outcomes.clone();
3783                check_landing_resources(state, "gate", &outcomes)?;
3784            }
3785            if outcomes.iter().all(|o| o.ok()) {
3786                break;
3787            }
3788            match Box::pin(crate::graph::repair_landing_gate(state, &winner, &outcomes)).await? {
3789                crate::graph::GateFix::Retry => {
3790                    outcomes = crate::graph::run_landing_gate(state, &winner, &wait).await?;
3791                }
3792                crate::graph::GateFix::Stop => break,
3793                crate::graph::GateFix::Defer => {
3794                    // The repair checks e2e immediately, before the next gate.
3795                    // Preserve a cache wait here as well as below; otherwise
3796                    // the daemon charges an attempt for unstarted verification.
3797                    if let Some(e2e) = &state.gate_repair_e2e {
3798                        let outcomes = e2e.outcomes.clone();
3799                        check_landing_resources(state, "e2e", &outcomes)?;
3800                    }
3801                    bail!("landing gate repair deferred");
3802                }
3803            }
3804        }
3805        state.gate = outcomes;
3806        state.gate_ran = true;
3807        state.save()?;
3808    }
3809    let inherited = crate::repo_lock::WaitPolicy::current();
3810    let wait = crate::repo_lock::WaitPolicy::new(
3811        Duration::from_secs(state.config.graph.timeout_resource_wait),
3812        move || inherited.cancelled(),
3813    );
3814    let checked = state
3815        .gate_head
3816        .clone()
3817        .context("landing gate has no checked SHA")?;
3818    anyhow::ensure!(
3819        git::rev_parse(&winner.worktree, "HEAD").await? == checked
3820            && git::is_clean(&winner.worktree).await?,
3821        "landing tree changed after gate"
3822    );
3823    let e2e = crate::graph::check_gate_repair_e2e(state, &winner, &wait).await?;
3824    if matches!(e2e, crate::graph::GateFix::Defer) {
3825        let outcomes = state
3826            .gate_repair_e2e
3827            .as_ref()
3828            .context("deferred landing e2e has no outcomes")?
3829            .outcomes
3830            .clone();
3831        check_landing_resources(state, "e2e", &outcomes)?;
3832    }
3833    anyhow::ensure!(
3834        matches!(e2e, crate::graph::GateFix::Retry),
3835        "post-repair e2e has not passed on the landing head; nothing published"
3836    );
3837    anyhow::ensure!(
3838        git::rev_parse(&winner.worktree, "HEAD").await? == checked
3839            && git::is_clean(&winner.worktree).await?,
3840        "landing tree changed during post-repair e2e"
3841    );
3842    anyhow::ensure!(
3843        crate::graph::verify_current_head(state, &winner, &wait).await?,
3844        "current head e2e has not passed; nothing published"
3845    );
3846    anyhow::ensure!(
3847        state.gate_allows_progress(),
3848        "landing gate failed; nothing published"
3849    );
3850    crate::pr_head::ensure_owner(state)?;
3851    if owner_requested_stop(state)? {
3852        bail!("owner requested stop before publication");
3853    }
3854    let current = forge.view(&state.repo, pr_url).await?;
3855    anyhow::ensure!(
3856        current.pr.state == PrLifecycle::Open,
3857        "PR closed or merged while gating; nothing published"
3858    );
3859    anyhow::ensure!(
3860        current.head == observed || current.head == checked,
3861        "PR head changed while gating; nothing published"
3862    );
3863    anyhow::ensure!(
3864        crate::foreign_checkout::write_back(state, &checked).await?,
3865        "foreign checkout delivery is blocked; see the run events"
3866    );
3867    let public = state.pr_branch().unwrap_or(&winner.branch).to_owned();
3868    let lease = format!("--force-with-lease=refs/heads/{public}:{observed}");
3869    let refspec = format!("{checked}:refs/heads/{public}");
3870    let lock = repo_merge_lock(&state.repo);
3871    let _slot = lock.lock().await;
3872    let pushed = git::git_raw(
3873        &winner.worktree,
3874        &["push", &lease, &state.config.merge.remote, &refspec],
3875    )
3876    .await?;
3877    anyhow::ensure!(
3878        pushed.ok(),
3879        "publishing gated landing head failed: {}",
3880        pushed.stderr
3881    );
3882    state.pr_pushed_head = Some(checked.clone());
3883    state.land_sync_pending = false;
3884    state.save()?;
3885    Ok(checked)
3886}
3887
3888/// A scratch rebase moves the winner ref before its index and files. Recover
3889/// only a tree proven unchanged since the saved start; never discard edits.
3890async fn restore_landing_worktree(state: &mut RunState) -> Result<()> {
3891    let winner = state.winner().cloned().context("landing has no winner")?;
3892    let inherited = crate::repo_lock::WaitPolicy::current();
3893    let wait = crate::repo_lock::WaitPolicy::new(
3894        Duration::from_secs(state.config.graph.timeout_resource_wait),
3895        move || inherited.cancelled(),
3896    );
3897    let _coordination = crate::repo_lock::SeatLease::operation_with(
3898        &winner.worktree,
3899        state.config.coordination.as_ref(),
3900        &wait,
3901    )
3902    .await?;
3903    if !git::is_clean(&winner.worktree).await? {
3904        let from = state
3905            .land_rebase_from
3906            .as_deref()
3907            .context("landing worktree is dirty")?;
3908        let head = git::rev_parse(&winner.worktree, "HEAD").await?;
3909        anyhow::ensure!(head != from, "landing worktree is dirty");
3910        for args in [
3911            vec!["diff", "--cached", "--quiet", from],
3912            vec!["diff", "--quiet"],
3913        ] {
3914            anyhow::ensure!(
3915                git::git_raw(&winner.worktree, &args).await?.ok(),
3916                "landing worktree has edits beyond the interrupted rebase"
3917            );
3918        }
3919        // Preserve build artifacts and local files, including ignored ones.
3920        // read-tree can overwrite ignored paths, so reject collisions with
3921        // the new tree (including file/directory conflicts) before updating.
3922        let untracked = git::git(&winner.worktree, &["ls-files", "--others", "-z"]).await?;
3923        let tracked = git::git(
3924            &winner.worktree,
3925            &["ls-tree", "-r", "--name-only", "-z", "HEAD"],
3926        )
3927        .await?;
3928        for local in untracked.split('\0').filter(|p| !p.is_empty()) {
3929            anyhow::ensure!(
3930                !tracked.split('\0').filter(|p| !p.is_empty()).any(|path| {
3931                    path == local
3932                        || path
3933                            .strip_prefix(local)
3934                            .is_some_and(|rest| rest.starts_with('/'))
3935                        || local
3936                            .strip_prefix(path)
3937                            .is_some_and(|rest| rest.starts_with('/'))
3938                }),
3939                "landing worktree has an untracked or ignored path conflicting with HEAD: {local}"
3940            );
3941        }
3942        git::git(&winner.worktree, &["read-tree", "-m", "-u", "HEAD"]).await?;
3943    }
3944    if let (Some(checkout), Some(from)) = (&state.foreign_checkout, &state.land_rebase_from) {
3945        anyhow::ensure!(
3946            git::is_ancestor(&winner.worktree, &checkout.synced_head, from).await,
3947            "landing synchronization started outside the foreign checkout history"
3948        );
3949        if !git::is_ancestor(&winner.worktree, from, "HEAD").await {
3950            // The rebase's tree already includes the base and the fixer's
3951            // resolutions. Retain that exact tree, but join the pre-sync
3952            // history so delivery remains fast-forward. Keep the checkpoint
3953            // until this succeeds: a crash after rebase (or after this merge)
3954            // must recover through this same idempotent path before gating.
3955            git::git(
3956                &winner.worktree,
3957                &[
3958                    "-c",
3959                    "core.hooksPath=/dev/null",
3960                    "merge",
3961                    "--strategy=ours",
3962                    "--no-ff",
3963                    "--no-autostash",
3964                    "--no-edit",
3965                    "-m",
3966                    "Merge pre-sync history into the repaired landing tree",
3967                    from,
3968                ],
3969            )
3970            .await?;
3971        }
3972    }
3973    crate::worktree_setup::prepare(&state.config, &state.repo, &winner.worktree).await?;
3974    state.land_rebase_from = None;
3975    state.save()?;
3976    Ok(())
3977}
3978
3979async fn rebase_landing_worktree(
3980    state: &mut RunState,
3981    onto: &str,
3982) -> Result<crate::rebase::Rebased> {
3983    restore_landing_worktree(state).await?;
3984    let winner = state.winner().cloned().context("landing has no winner")?;
3985    state.land_sync_pending = true;
3986    state.land_rebase_from = Some(git::rev_parse(&winner.worktree, "HEAD").await?);
3987    state.save()?;
3988    let scratch = state.dir().join("rebase");
3989    let result = crate::rebase::rebase_with_fixer(state, &scratch, &winner.branch, onto).await?;
3990    if result == crate::rebase::Rebased::Applied {
3991        restore_landing_worktree(state).await?;
3992    }
3993    Ok(result)
3994}
3995
3996/// Reconcile a stopped landing before changing its branch. This also handles
3997/// old records whose graph status or merge detail no longer identifies land.
3998async fn resume_landing_head<F: Forge>(
3999    state: &mut RunState,
4000    observed: &str,
4001    pr_url: &str,
4002    forge: &F,
4003) -> Result<String> {
4004    crate::pr_head::ensure_owner(state)?;
4005    let winner = state.winner().cloned().context("landing has no winner")?;
4006    let local = git::rev_parse(&winner.worktree, "HEAD").await?;
4007    let expected = state
4008        .pr_pushed_head
4009        .as_deref()
4010        .or(state.gate_head.as_deref())
4011        .context("missing public head lease")?;
4012    anyhow::ensure!(
4013        observed == expected
4014            || (observed == local
4015                && ((state.gate_head.as_deref() == Some(&local) && state.gate_allows_progress())
4016                    || git::is_ancestor(&winner.worktree, expected, &local).await)),
4017        "public head moved outside this run; refusing to sync"
4018    );
4019    // An operator may have extended and published the winner while approval
4020    // was pending. Matching local/public history can be adopted as the lease,
4021    // but publish_landing_head must gate its new SHA before proceeding. A
4022    // remote-only or divergent update still cannot be overwritten.
4023    // Save reconciliation before rebasing again; a prior push may have reached
4024    // the remote immediately before the previous driver's last save.
4025    state.pr_pushed_head = Some(observed.to_owned());
4026    state.save()?;
4027    restore_landing_worktree(state).await?;
4028    let remote = state.config.merge.remote.clone();
4029    let base = state.base_branch.clone();
4030    let fetched = git::fetch(&state.repo, &remote, &base).await?;
4031    anyhow::ensure!(
4032        fetched.ok(),
4033        "cannot fetch landing base: {}",
4034        fetched.stderr
4035    );
4036    let onto = format!("{remote}/{base}");
4037    let tip = git::rev_parse(&state.repo, &onto).await?;
4038    if !git::is_ancestor(&state.repo, &tip, &local).await {
4039        match rebase_landing_worktree(state, &onto).await? {
4040            crate::rebase::Rebased::Applied => {}
4041            crate::rebase::Rebased::Stopped(why) => bail!("landing rebase stopped: {why}"),
4042        }
4043    }
4044    publish_landing_head(state, observed, pr_url, forge).await
4045}
4046
4047async fn land_with<F: Forge>(state: &mut RunState, pr_url: &str, forge: &F) -> Result<PrState> {
4048    crate::pr_head::ensure_owner(state)?;
4049    state.land_unrelated = None;
4050    let reopening_hold = crate::merge_decision::resumable_hold(state);
4051    let repo = state.repo.clone();
4052    let budget = state.config.graph.land_rounds;
4053    let mut round = 0usize;
4054    // Counted apart from `round`: a rebase is not a fix, and a base that
4055    // moved is not the change's fault.
4056    let mut rebases = 0usize;
4057    let ci = state.config.merge.ci.expectation();
4058    if ci == CiExpectation::Absent {
4059        state.event(
4060            "land",
4061            "CI not expected ([merge] ci = \"none\"); check status ignored",
4062        );
4063    }
4064    let mut waited = Duration::ZERO;
4065    // Comment bodies the fixer has already been shown. A comment is
4066    // outstanding until it has been handed over once; after that it is a
4067    // recorded decision, not an open question, and re-feeding it would loop the
4068    // budget away on a comment the fixer already declined with an argument.
4069    let mut shown: BTreeSet<String> = BTreeSet::new();
4070    // Await the last successfully published head, including across resumes.
4071    let mut awaiting_head: Option<String> = state.pr_pushed_head.clone();
4072    // Whether a settled-checks refusal has already been given its one re-look.
4073    let mut rechecked = false;
4074
4075    // Marks the run resumable through exactly this function, not through a
4076    // fresh competition: `RunStatus::resumable` excludes only `Merged`,
4077    // `Ready` and `Failed`, and `merge`'s own re-entry guard looks for this
4078    // status specifically to know a resumed run belongs back in `land`
4079    // rather than at a second `gh pr create`. Set on every entry - fresh or
4080    // resumed - because a resume that parked here again must keep reading
4081    // `Landing`, not whatever a first pass through `merge` left behind.
4082    state.status = RunStatus::Landing;
4083    state.event("land", format!("watching {pr_url}"));
4084    state.save()?;
4085
4086    // An armed merge recorded by an earlier pass may or may not have reached
4087    // the forge (the record is written before the call). It is taken back on
4088    // the first observation, where a pull request is known to stop with.
4089    let mut resumed_armed = state.land_armed_head.is_some();
4090
4091    loop {
4092        let seen = forge.view(&repo, pr_url).await?;
4093        let mut pr = seen.pr.clone();
4094        pr.review_comments.retain(|c| !shown.contains(&c.body));
4095        state.pr = Some(crate::run::PrRecord {
4096            url: pr.url.clone(),
4097            number: pr.number,
4098            state: pr.state.as_str().to_owned(),
4099            checks: pr.checks.as_str().to_owned(),
4100            round,
4101            rounds: budget,
4102            red_at_merge: Vec::new(),
4103        });
4104        state.save()?;
4105
4106        if pr.state == PrLifecycle::Open {
4107            state.observe_branch_owner_holds(&seen.head).await;
4108            state.save()?;
4109        }
4110        crate::pr_head::ensure_owner(state)?;
4111        if reopening_hold {
4112            anyhow::ensure!(
4113                pr.state == PrLifecycle::Open,
4114                "held PR is no longer open; cannot request a fresh decision"
4115            );
4116            // An explicit resume authorizes a fresh question only. Preserve the
4117            // answered card, and never enter repair/rebase/merge on this pass.
4118            disarm(forge, state, &repo, pr.number)
4119                .await
4120                .map_err(anyhow::Error::msg)?;
4121            let previous = state.merge_decision.take();
4122            let binding = state.land_approval.take();
4123            state.config.graph.land_approval = true;
4124            let subject = merge_subject(
4125                crate::graph::landing_title(state, &seen.title),
4126                &crate::graph::landing_subject_source(state),
4127            );
4128            let contested = contested_to_ask(state);
4129            if let Err(error) =
4130                approval_gate(state, &pr, &subject, contested.as_ref(), &seen.head).await
4131            {
4132                state.merge_decision = previous;
4133                state.land_approval = binding;
4134                return Err(error);
4135            }
4136            state.end_pass_parked(crate::run::ParkCause::Question);
4137            state.parked = true;
4138            state.event("land", format!("explicit resume reopened held decision {}; parked awaiting a fresh owner answer", previous.as_ref().map(|d| d.question.as_str()).unwrap_or_default()));
4139            state.save()?;
4140            return Ok(pr);
4141        }
4142        if owner_stop(state, &mut pr, forge).await? {
4143            return Ok(pr);
4144        }
4145
4146        if std::mem::take(&mut resumed_armed) && pr.state == PrLifecycle::Open {
4147            // An approval already given for the same head is reused, so
4148            // nothing is asked twice. A failed disable
4149            // stops the run with the record kept: pushing on could change the
4150            // head under an arm that is still live.
4151            if let Err(e) = disarm(forge, state, &repo, pr.number).await {
4152                let why = format!(
4153                    "a previous pass may have armed auto-merge and it could not be disabled \
4154                     on resume: {e}"
4155                );
4156                stop(state, &repo, &pr, &why).await?;
4157                return Ok(pr);
4158            }
4159        }
4160
4161        // The head moved away from the one auto-merge was armed on, by
4162        // someone other than this loop. The arm is pinned and would refuse to
4163        // merge the new commit, but the approval was for the old one: take it
4164        // back and go through the normal path again.
4165        if pr.state == PrLifecycle::Open
4166            && !seen.head.is_empty()
4167            && state
4168                .land_armed_head
4169                .as_deref()
4170                .is_some_and(|armed| !armed.eq_ignore_ascii_case(&seen.head))
4171        {
4172            if let Err(e) = disarm(forge, state, &repo, pr.number).await {
4173                let why = format!(
4174                    "the head moved while auto-merge was armed and it could not be disabled: {e}"
4175                );
4176                stop(state, &repo, &pr, &why).await?;
4177                return Ok(pr);
4178            }
4179        }
4180
4181        if state.land_sync_pending && pr.state == PrLifecycle::Open {
4182            // Disarming can outlive an owner answer. Consume it before our
4183            // own rebase invalidates the head shown on the decision card.
4184            if owner_stop(state, &mut pr, forge).await? {
4185                return Ok(pr);
4186            }
4187            let repair_answer = state.land_approval.as_ref().is_some_and(|binding| {
4188                binding.head.eq_ignore_ascii_case(&seen.head)
4189                    && ask::Questions::open()
4190                        .get(&binding.question)
4191                        .is_ok_and(|q| {
4192                            q.run == state.id
4193                                && q.node == APPROVAL_NODE
4194                                && q.status == ask::QuestionStatus::Answered
4195                                && matches!(approval(q.resolution().as_deref()), Approval::Repair)
4196                        })
4197            });
4198            if repair_answer {
4199                let subject = merge_subject(
4200                    crate::graph::landing_title(state, &seen.title),
4201                    &crate::graph::landing_subject_source(state),
4202                );
4203                let contested = contested_to_ask(state);
4204                if matches!(
4205                    approval_gate(state, &pr, &subject, contested.as_ref(), &seen.head).await?,
4206                    ApprovalGate::Repair
4207                ) {
4208                    if let Err(why) = disarm(forge, state, &repo, pr.number).await {
4209                        state.event("repair", format!("auto-merge could not be disabled: {why}; repair preflight will retry"));
4210                        state.save()?;
4211                    }
4212                    return Ok(pr);
4213                }
4214            }
4215            let synced = resume_landing_head(state, &seen.head, pr_url, forge).await;
4216            // A stop detected before publication must apply its durable
4217            // decision, even when verification returned an error meanwhile.
4218            if owner_stop(state, &mut pr, forge).await? {
4219                return Ok(pr);
4220            }
4221            let head = synced?;
4222            awaiting_head = Some(head);
4223            waited = Duration::ZERO;
4224            rechecked = false;
4225            // Read CI and approvals afresh, never from the pre-push observation.
4226            continue;
4227        }
4228
4229        if pr.state == PrLifecycle::Open {
4230            if bound_head(&seen.head, &seen.rollup_head, awaiting_head.as_deref()).is_none() {
4231                if waited >= WAIT_CEILING {
4232                    let want = awaiting_head.as_deref().unwrap_or_default();
4233                    let why = format!(
4234                        "the pull request's checks were still not about one readable head after \
4235                         {} minutes (expected {}, pull request points at {}, checks are for {}); \
4236                         someone may have pushed over it",
4237                        WAIT_CEILING.as_secs() / 60,
4238                        if want.is_empty() { "any" } else { want },
4239                        if seen.head.is_empty() {
4240                            "nothing readable"
4241                        } else {
4242                            &seen.head
4243                        },
4244                        if seen.rollup_head.is_empty() {
4245                            "nothing readable"
4246                        } else {
4247                            &seen.rollup_head
4248                        },
4249                    );
4250                    stop_disarmed(forge, state, &repo, &pr, &why).await?;
4251                    return Ok(pr);
4252                }
4253                waited += POLL;
4254                forge.poll().await;
4255                continue;
4256            }
4257            // Reset once, when the awaited head first shows up; resetting on
4258            // every re-observation would let a standing refusal wait forever.
4259            if awaiting_head.take().is_some() {
4260                // The checks now being read belong to the new head; give them
4261                // the same grace a fresh pull request gets.
4262                waited = Duration::ZERO;
4263            }
4264        }
4265
4266        // The merge-state grace is head-bound and durable. Reruns reset the
4267        // checks clock, but must not extend this deadline across resumes.
4268        let decision_wait = if pr.checks == Checks::Red && pr.blocking == Blocking::Unsaid {
4269            let first = state
4270                .land_unsaid
4271                .get_or_insert_with(|| (seen.head.clone(), forge.now()));
4272            if !first.0.eq_ignore_ascii_case(&seen.head) {
4273                *first = (seen.head.clone(), forge.now());
4274            }
4275            let elapsed = Duration::from_secs(forge.now().saturating_sub(first.1).max(0) as u64);
4276            state.save()?;
4277            elapsed
4278        } else {
4279            waited
4280        };
4281        let proposed = decide_with(&pr, round, budget, decision_wait, ci);
4282        if pr.checks == Checks::Red && pr.blocking == Blocking::Unsaid && proposed == Step::Wait {
4283            if waited >= WAIT_CEILING {
4284                stop_disarmed(
4285                    forge,
4286                    state,
4287                    &repo,
4288                    &pr,
4289                    "merge state remained unreadable; refusing to merge on a guess",
4290                )
4291                .await?;
4292                return Ok(pr);
4293            }
4294            waited += POLL;
4295            forge.poll().await;
4296            continue;
4297        }
4298
4299        // A failed workflow run is rerun once, judged by its own run rather
4300        // than by the rollup's aggregate: another run still pending, or a fix
4301        // budget already spent, must not keep a flaky failure from its one
4302        // rerun. It spends no round and no attempt.
4303        if ci == CiExpectation::Expected
4304            && pr.state == PrLifecycle::Open
4305            && state.config.graph.rerun_flaky_checks
4306        {
4307            let include_optional = pr.checks == Checks::Red
4308                && matches!(proposed, Step::Fix { .. } | Step::GiveUp { .. });
4309            let failing = rerunnable_with(&seen.contexts, include_optional);
4310            let now = forge.now();
4311            match rerun_step(&failing, state.land_reruns.as_ref(), &seen.head, now) {
4312                RerunStep::Rerun(runs) => {
4313                    let rec = state.land_reruns.get_or_insert_with(Default::default);
4314                    if !rec.head.eq_ignore_ascii_case(&seen.head) {
4315                        *rec = LandReruns {
4316                            head: seen.head.clone(),
4317                            runs: BTreeMap::new(),
4318                        };
4319                    }
4320                    for run in &runs {
4321                        rec.runs.insert(run.clone(), now);
4322                    }
4323                    // Durable before the call: a restart must not
4324                    // rerun the same run twice. A save that fails
4325                    // means no rerun.
4326                    state.save()?;
4327                    let mut any = false;
4328                    for run in &runs {
4329                        let names = failing
4330                            .iter()
4331                            .filter(|(_, r)| r.as_deref() == Some(run))
4332                            .map(|(l, _)| l.as_str())
4333                            .collect::<Vec<_>>()
4334                            .join(", ");
4335                        match forge.rerun(&repo, run).await {
4336                            Ok(()) => {
4337                                any = true;
4338                                state.event(
4339                                    "land",
4340                                    format!("reran failed check(s) {names} (run {run})"),
4341                                );
4342                            }
4343                            // Stays recorded: never a retry loop.
4344                            Err(e) => state.event(
4345                                "land",
4346                                format!("could not rerun check(s) {names} (run {run}): {e:#}"),
4347                            ),
4348                        }
4349                    }
4350                    state.save()?;
4351                    if any {
4352                        waited = Duration::ZERO;
4353                        forge.poll().await;
4354                        continue;
4355                    }
4356                }
4357                RerunStep::Grace => {
4358                    if waited >= WAIT_CEILING {
4359                        let why = format!(
4360                            "checks were still failing after a rerun and {} minutes",
4361                            WAIT_CEILING.as_secs() / 60
4362                        );
4363                        stop(state, &repo, &pr, &why).await?;
4364                        return Ok(pr);
4365                    }
4366                    waited += POLL;
4367                    forge.poll().await;
4368                    continue;
4369                }
4370                RerunStep::Fix => {}
4371            }
4372        }
4373
4374        let step = proposed;
4375        // Armed on exactly this head: the forge is doing the waiting. A
4376        // decision to merge (or keep waiting) is only watched, never re-armed
4377        // and never re-approved; anything else - a red check, a comment, a
4378        // conflict - falls through to its own arm, which disarms first.
4379        let armed_here = state
4380            .land_armed_head
4381            .as_deref()
4382            .is_some_and(|armed| armed.eq_ignore_ascii_case(&seen.head));
4383        if armed_here && matches!(step, Step::Merge | Step::Wait) {
4384            if waited >= WAIT_CEILING {
4385                let required = if seen.base.is_empty() {
4386                    None
4387                } else {
4388                    forge.required_contexts(&repo, &seen.base).await
4389                };
4390                let why = format!(
4391                    "auto-merge was armed on {} but the pull request did not merge within {} \
4392                     minutes ({})",
4393                    seen.head,
4394                    WAIT_CEILING.as_secs() / 60,
4395                    waiting_on(&seen.merge_state, &seen.contexts, required.as_ref())
4396                );
4397                stop_disarmed(forge, state, &repo, &pr, &why).await?;
4398                return Ok(pr);
4399            }
4400            waited += POLL;
4401            forge.poll().await;
4402            continue;
4403        }
4404        if armed_here && !matches!(step, Step::Done { .. }) {
4405            // Not merging or waiting any more: whatever is next may change the
4406            // head or give up, and neither may leave the arm standing.
4407            if let Err(e) = disarm(forge, state, &repo, pr.number).await {
4408                let why = format!("auto-merge could not be disabled: {e}");
4409                stop(state, &repo, &pr, &why).await?;
4410                return Ok(pr);
4411            }
4412        }
4413        if pr.checks == Checks::Red
4414            && matches!(step, Step::Fix { .. } | Step::GiveUp { .. })
4415            && pr.review_comments.is_empty()
4416            && let Some(evidence) = forge.attribution(state, &seen).await
4417            && evidence.unrelated(&seen.head, &pr.failing)
4418        {
4419            if owner_stop(state, &mut pr, forge).await? {
4420                return Ok(pr);
4421            }
4422            // Attribution does not authorize action against an unseen push.
4423            let fresh = forge.view(&repo, pr_url).await?;
4424            if fresh.head != seen.head
4425                || fresh.rollup_head != seen.rollup_head
4426                || fresh.pr.state != PrLifecycle::Open
4427                || fresh.pr.checks != Checks::Red
4428                || fresh.pr.failing != pr.failing
4429                || fresh
4430                    .pr
4431                    .review_comments
4432                    .iter()
4433                    .any(|c| !shown.contains(&c.body))
4434            {
4435                continue;
4436            }
4437            hold_unrelated(state, &pr, evidence).await?;
4438            return Ok(pr);
4439        }
4440        match step {
4441            Step::Wait => {
4442                if waited >= WAIT_CEILING {
4443                    let why = format!(
4444                        "checks were still running after {} minutes",
4445                        WAIT_CEILING.as_secs() / 60
4446                    );
4447                    stop(state, &repo, &pr, &why).await?;
4448                    return Ok(pr);
4449                }
4450                waited += POLL;
4451                forge.poll().await;
4452            }
4453            Step::Done { merged } => {
4454                if merged {
4455                    state.pr_merged_head = (!seen.head.is_empty()).then(|| seen.head.clone());
4456                }
4457                // Auto-merge is pinned to the approved head, but the forge's
4458                // own handling of a later push is not something magi can
4459                // see: if the pull request merged on another commit, say so
4460                // loudly rather than record a clean landing.
4461                if let Some(armed) = state.land_armed_head.take() {
4462                    if merged && !seen.head.is_empty() && !armed.eq_ignore_ascii_case(&seen.head) {
4463                        let msg = format!(
4464                            "{} merged on {} but the owner approved {armed}; review what landed",
4465                            pr.url, seen.head
4466                        );
4467                        tracing::warn!("{msg}");
4468                        state.event("land", msg);
4469                        // Only an arm left by an older build can get here.
4470                        // The wording is fixed so a repeat does not relight
4471                        // the notice.
4472                        crate::notices::raise_with(
4473                            crate::notices::Notice::warn(
4474                                &format!("merged-unapproved-head:{}", state.id),
4475                                "A pull request merged on a commit the owner did not approve; \
4476                                 review what landed",
4477                            )
4478                            .link(crate::notices::Link::Run {
4479                                id: state.id.clone(),
4480                            }),
4481                            &state.config.notify,
4482                        );
4483                    }
4484                }
4485                state.status = if merged {
4486                    RunStatus::Merged
4487                } else {
4488                    RunStatus::Ready
4489                };
4490                let detail = if merged {
4491                    format!("{} was merged", pr.url)
4492                } else {
4493                    format!("{} was closed without merging", pr.url)
4494                };
4495                state.merge = Some(MergeOutcome {
4496                    mode: MergeMode::Pr,
4497                    ok: merged,
4498                    detail: detail.clone(),
4499                    empty: false,
4500                });
4501                state.event("land", detail);
4502                state.save()?;
4503                write_pr_state_through(state, pr.state);
4504                return Ok(pr);
4505            }
4506            Step::Merge => {
4507                state.check_delivery_evidence(&seen.head)?;
4508                let subject = merge_subject(
4509                    crate::graph::landing_title(state, &seen.title),
4510                    &crate::graph::landing_subject_source(state),
4511                );
4512                // The owner sees the panel before the one irreversible step,
4513                // and an unanswered question is a hold: silence never merges.
4514                //
4515                // The dedicated policy includes existing consent and post-repair
4516                // decisions; repair choice availability never grants merge authority.
4517                let contested = contested_to_ask(state);
4518                if !approval_reasons(state).is_empty() {
4519                    match approval_gate(state, &pr, &subject, contested.as_ref(), &seen.head)
4520                        .await?
4521                    {
4522                        ApprovalGate::Approved => {}
4523                        ApprovalGate::Repair => {
4524                            // The driver consumes the saved request under the existing
4525                            // run/task claim. No merge, no second run or task.
4526                            if let Err(why) = disarm(forge, state, &repo, pr.number).await {
4527                                state.event("repair", format!("auto-merge could not be disabled: {why}; repair preflight will retry"));
4528                                state.save()?;
4529                            }
4530                            return Ok(pr);
4531                        }
4532                        ApprovalGate::Held => {
4533                            // Another entry point may have saved a stop after
4534                            // the first read above. Apply its durable action.
4535                            if owner_stop(state, &mut pr, forge).await? {
4536                                return Ok(pr);
4537                            }
4538                            stop(
4539                                state,
4540                                &repo,
4541                                &pr,
4542                                "the owner did not approve the merge (held or unanswered)",
4543                            )
4544                            .await?;
4545                            return Ok(pr);
4546                        }
4547                        // Filed (or still standing from an earlier visit) and
4548                        // not yet answered. Park here rather than wait: the
4549                        // question survives on disk, the daemon hands this
4550                        // run's slot to something else, and a later resume
4551                        // re-enters `land`, finds the same question, and
4552                        // either merges or stops depending on what it says
4553                        // by then.
4554                        ApprovalGate::Pending => {
4555                            state.end_pass_parked(crate::run::ParkCause::Question);
4556                            state.parked = true;
4557                            state.event(
4558                                "land",
4559                                "parked awaiting merge approval - resumes once answered",
4560                            );
4561                            state.save()?;
4562                            return Ok(pr);
4563                        }
4564                    }
4565                }
4566                // Open and past the gate above, so `seen.head` is the commit
4567                // the checks were bound to and the owner approved.
4568                //
4569                // Merge directly, bound to that commit. Auto-merge is not
4570                // armed any more: the forge checks `--match-head-commit` only
4571                // when the request is made, so an arm outlives the head it
4572                // was made for, and a push of another commit whose
4573                // requirements are met first would merge without approval.
4574                // A direct merge is checked by the forge at the moment it
4575                // happens, so only the approved head can land.
4576                let observed_head = seen.head.clone();
4577                // Read the pull request again just before: the state seen
4578                // above can be a moment old.
4579                {
4580                    let fresh = forge.view(&repo, pr_url).await.ok();
4581                    if !direct_merge_is_safe(
4582                        fresh.as_ref(),
4583                        &observed_head,
4584                        &shown,
4585                        round,
4586                        budget,
4587                        waited,
4588                        ci,
4589                    ) {
4590                        if waited >= WAIT_CEILING {
4591                            let why = "the pull request did not settle on the approved head \
4592                                       before it could be merged";
4593                            stop(state, &repo, &pr, why).await?;
4594                            return Ok(pr);
4595                        }
4596                        state.event(
4597                            "land",
4598                            "the pull request changed before merging; looking again",
4599                        );
4600                        waited += POLL;
4601                        forge.poll().await;
4602                        continue;
4603                    }
4604                }
4605                let argv = merge_argv_at(pr.number, &subject, &observed_head);
4606                let out = {
4607                    let merge_lock = repo_merge_lock(&repo);
4608                    let _merge_slot = merge_lock.lock().await;
4609                    crate::pr_head::ensure_owner(state)?;
4610                    if let Some(task_id) = state
4611                        .origin
4612                        .as_ref()
4613                        .and_then(|origin| origin.task.as_ref())
4614                    {
4615                        let task = crate::queue::Queue::open()
4616                            .get(task_id)
4617                            .context("cannot verify the task owner immediately before merge")?;
4618                        if task.runs.last() != Some(&state.id)
4619                            || task.operator_held()
4620                            || task.status == crate::queue::TaskStatus::Done
4621                        {
4622                            bail!(
4623                                "task ownership or an operator hold superseded this merge approval"
4624                            );
4625                        }
4626                    }
4627                    if let Some(binding) = &state.land_approval {
4628                        let current = ask::Questions::open().get(&binding.question)?;
4629                        if merge_consent_superseded(state, &current)?
4630                            || current.run != state.id
4631                            || !binding.head.eq_ignore_ascii_case(&observed_head)
4632                            || approval(current.resolution().as_deref()) != Approval::Merge
4633                            || current.merge_target.as_ref().is_some_and(|t| {
4634                                t.pr != pr.url || !t.head.eq_ignore_ascii_case(&observed_head)
4635                            })
4636                        {
4637                            bail!(
4638                                "merge decision is stale or no longer authorizes the reviewed head"
4639                            );
4640                        }
4641                    }
4642                    forge.merge(&repo, &argv).await?
4643                };
4644                if out.0 {
4645                    // Exit 0 is not proof of a merge: with a merge queue `gh`
4646                    // enqueues (or reports the pull request already queued)
4647                    // and succeeds while it is still open. Ask the forge; an
4648                    // unreadable answer is not a confirmation either, so it
4649                    // is looked at again rather than recorded as merged.
4650                    let confirmed = forge.view(&repo, pr_url).await.is_ok_and(|c| {
4651                        if c.pr.state == PrLifecycle::Merged {
4652                            state.pr_merged_head = (!c.head.is_empty()).then_some(c.head);
4653                            true
4654                        } else {
4655                            false
4656                        }
4657                    });
4658                    if !confirmed {
4659                        if waited >= WAIT_CEILING {
4660                            let why = "the merge request succeeded but the pull request \
4661                                       could not be confirmed merged after waiting";
4662                            stop(state, &repo, &pr, why).await?;
4663                            return Ok(pr);
4664                        }
4665                        state.event(
4666                            "land",
4667                            "merge accepted but the pull request is not confirmed merged yet; waiting",
4668                        );
4669                        state.save()?;
4670                        waited += POLL;
4671                        forge.poll().await;
4672                        continue;
4673                    }
4674                    pr.state = PrLifecycle::Merged;
4675                    state.status = RunStatus::Merged;
4676                    state.merge = Some(MergeOutcome {
4677                        mode: MergeMode::Pr,
4678                        ok: true,
4679                        detail: format!("gh {}", argv.join(" ")),
4680                        empty: false,
4681                    });
4682                    // The last `state.pr` snapshot is whatever the poll before
4683                    // this merge observed - still `open` - and nothing below
4684                    // refreshes it from GitHub again, so the UI's round rail
4685                    // would otherwise keep animating a merged run forever.
4686                    if let Some(pr_record) = state.pr.as_mut() {
4687                        pr_record.state = pr.state.as_str().to_owned();
4688                    }
4689                    state.event("land", format!("merged {} as `{subject}`", pr.url));
4690                    announce_red_merge(state, &pr).await;
4691                    state.save()?;
4692                    write_pr_state_through(state, pr.state);
4693                    return Ok(pr);
4694                }
4695                let after_seen = forge.view(&repo, pr_url).await.ok();
4696                let after = after_seen.as_ref().map(|s| s.pr.state);
4697                if let Some(outcome) = merged_after_all(&argv, &out.1, after) {
4698                    state.pr_merged_head = after_seen
4699                        .as_ref()
4700                        .map(|s| s.head.clone())
4701                        .filter(|h| !h.is_empty());
4702                    pr.state = PrLifecycle::Merged;
4703                    state.status = RunStatus::Merged;
4704                    state.merge = Some(outcome);
4705                    if let Some(pr_record) = state.pr.as_mut() {
4706                        pr_record.state = pr.state.as_str().to_owned();
4707                    }
4708                    state.event("land", format!("merged {} as `{subject}`", pr.url));
4709                    announce_red_merge(state, &pr).await;
4710                    state.save()?;
4711                    write_pr_state_through(state, pr.state);
4712                    return Ok(pr);
4713                }
4714                let verdict = classify_refusal(after_seen.as_ref(), rechecked, &observed_head, ci);
4715                match verdict {
4716                    Refused::Final => {
4717                        let merge_state = after_seen
4718                            .as_ref()
4719                            .map(|s| s.merge_state.as_str())
4720                            .filter(|m| !m.is_empty())
4721                            .unwrap_or("unknown");
4722                        // Which refusal this was decides what a person has to
4723                        // do about it, so the two are worded apart; both carry
4724                        // the forge's own message.
4725                        let why = format!(
4726                            "the merge was refused: {} (merge state: {merge_state})",
4727                            out.1
4728                        );
4729                        stop(state, &repo, &pr, &why).await?;
4730                        return Ok(pr);
4731                    }
4732                    verdict => {
4733                        // Back to the top, which re-decides and passes the
4734                        // approval gate again, a poll later. `waited` is not
4735                        // reset here: only a pushed fix restarts it, or a
4736                        // standing refusal would wait forever.
4737                        if waited >= WAIT_CEILING {
4738                            let why = format!(
4739                                "the merge was still refused after {} minutes: {}",
4740                                WAIT_CEILING.as_secs() / 60,
4741                                out.1
4742                            );
4743                            stop(state, &repo, &pr, &why).await?;
4744                            return Ok(pr);
4745                        }
4746                        if verdict == Refused::Recheck {
4747                            rechecked = true;
4748                        }
4749                        state.event(
4750                            "land",
4751                            "merge refused while the branch policy is not satisfied yet; waiting",
4752                        );
4753                        state.save()?;
4754                        waited += POLL;
4755                        forge.poll().await;
4756                    }
4757                }
4758            }
4759            Step::Rebase => {
4760                // Bounded by the same budget as a fix, because a rebase that
4761                // keeps being needed means the base moves faster than this
4762                // run can land and a person should decide what to do. It
4763                // spends none of that budget: the change is not what is
4764                // wrong.
4765                if rebases >= budget {
4766                    let why = format!(
4767                        "the base moved under this branch {budget} time(s) and it still does \
4768                         not merge; rebasing again would only race it"
4769                    );
4770                    stop(state, &repo, &pr, &why).await?;
4771                    return Ok(pr);
4772                }
4773                rebases += 1;
4774                let Some(branch) = state.winner().map(|w| w.branch.clone()) else {
4775                    stop(
4776                        state,
4777                        &repo,
4778                        &pr,
4779                        "the pull request conflicts and this run has no winning branch to rebase",
4780                    )
4781                    .await?;
4782                    return Ok(pr);
4783                };
4784                let base = state.base_branch.clone();
4785                state.event(
4786                    "land",
4787                    format!("{} no longer merges; rebasing onto {base}", pr.url),
4788                );
4789                state.save()?;
4790
4791                // Onto the base as the *remote* has it: the local ref may be
4792                // behind, and rebasing onto a stale base produces a branch
4793                // that conflicts all over again.
4794                let remote = state.config.merge.remote.clone();
4795                git::fetch(&repo, &remote, &base).await.ok();
4796                if owner_stop(state, &mut pr, forge).await? {
4797                    return Ok(pr);
4798                }
4799                let onto = format!("{remote}/{base}");
4800                let rebased = match rebase_landing_worktree(state, &onto).await {
4801                    Ok(crate::rebase::Rebased::Applied) => Ok(None),
4802                    Ok(crate::rebase::Rebased::Stopped(why)) => Ok(Some(why)),
4803                    Err(e) => Err(e),
4804                };
4805                if owner_stop(state, &mut pr, forge).await? {
4806                    return Ok(pr);
4807                }
4808                match rebased {
4809                    Ok(None) => {
4810                        let published =
4811                            publish_landing_head(state, &seen.head, pr_url, forge).await;
4812                        if owner_stop(state, &mut pr, forge).await? {
4813                            return Ok(pr);
4814                        }
4815                        let head = published?;
4816                        crate::graph::refresh_reviewed_commits(state, &branch).await;
4817                        state.pr_pushed_head = Some(head.clone());
4818                        awaiting_head = Some(head);
4819                        rechecked = false;
4820                        state.event("land", format!("rebased {branch} onto {base}"));
4821                        state.save()?;
4822                        // The forge has to re-run its checks against the
4823                        // rebased head before anything else can be decided.
4824                        waited = Duration::ZERO;
4825                        tokio::time::sleep(POLL).await;
4826                    }
4827                    // The fixer's rounds are spent (or it could not finish):
4828                    // that is a decision for a person.
4829                    Ok(Some(conflict)) => {
4830                        let why = format!(
4831                            "{} conflicts with {base} and the rebase did not apply: {}",
4832                            pr.url,
4833                            conflict.chars().take(600).collect::<String>()
4834                        );
4835                        stop(state, &repo, &pr, &why).await?;
4836                        return Ok(pr);
4837                    }
4838                    Err(e) if e.is::<crate::repo_lock::ResourceBlocked>() => return Err(e),
4839                    Err(e) => {
4840                        let why = format!("could not rebase {branch} onto {base}: {e:#}");
4841                        stop(state, &repo, &pr, &why).await?;
4842                        return Ok(pr);
4843                    }
4844                }
4845            }
4846            Step::GiveUp { reason } => {
4847                stop(state, &repo, &pr, &reason).await?;
4848                return Ok(pr);
4849            }
4850            Step::Fix { reason } => {
4851                round += 1;
4852                waited = Duration::ZERO;
4853                for c in &pr.review_comments {
4854                    shown.insert(c.body.clone());
4855                }
4856                state.event("land", format!("round {round}: {reason}"));
4857                state.save()?;
4858
4859                let logs = failing_logs(&repo, &seen.failing_urls).await;
4860                let was_red = pr.checks == Checks::Red;
4861                if owner_stop(state, &mut pr, forge).await? {
4862                    return Ok(pr);
4863                }
4864                let fixed = forge.fix(state, &pr, round, budget, &reason, &logs).await?;
4865                if owner_stop(state, &mut pr, forge).await? {
4866                    return Ok(pr);
4867                }
4868                match fixed {
4869                    Fixed::Committed { head } => {
4870                        // Whatever the next look shows may still be the
4871                        // previous head; see `awaiting_new_head`.
4872                        state.pr_pushed_head = Some(head.clone());
4873                        awaiting_head = Some(head);
4874                        rechecked = false;
4875                        waited = Duration::ZERO;
4876                        forge.poll().await;
4877                    }
4878                    Fixed::Declined if was_red => {
4879                        // Review comments may have required this round even
4880                        // when CI was unrelated. Once the fixer has read them,
4881                        // use the same structural attribution before charging
4882                        // a no-commit stop to the task.
4883                        if let Some(evidence) = forge.attribution(state, &seen).await
4884                            && evidence.unrelated(&seen.head, &pr.failing)
4885                        {
4886                            let fresh = forge.view(&repo, pr_url).await?;
4887                            if fresh.head != seen.head
4888                                || fresh.rollup_head != seen.rollup_head
4889                                || fresh.pr.state != PrLifecycle::Open
4890                                || fresh.pr.checks != Checks::Red
4891                                || fresh.pr.failing != pr.failing
4892                                || fresh
4893                                    .pr
4894                                    .review_comments
4895                                    .iter()
4896                                    .any(|c| !shown.contains(&c.body))
4897                            {
4898                                continue;
4899                            }
4900                            if owner_stop(state, &mut pr, forge).await? {
4901                                return Ok(pr);
4902                            }
4903                            hold_unrelated(state, &pr, evidence).await?;
4904                            return Ok(pr);
4905                        }
4906                        let why = format!(
4907                            "the fixer produced no commit while {} check(s) were failing \
4908                             ({}); stopping instead of looping on an unchanged tree",
4909                            pr.failing.len(),
4910                            pr.failing.join(", ")
4911                        );
4912                        stop(state, &repo, &pr, &why).await?;
4913                        return Ok(pr);
4914                    }
4915                    // Comment-driven round with no commit: the fixer read the
4916                    // comments and changed nothing, which is a decision it is
4917                    // allowed to make. The comments are recorded as shown, so
4918                    // the next observation sees a clean pull request.
4919                    Fixed::Declined => state.event(
4920                        "land",
4921                        format!("round {round}: fixer declined the comments, nothing committed"),
4922                    ),
4923                    Fixed::Failed(why) => {
4924                        stop(state, &repo, &pr, &format!("the fix round failed: {why}")).await?;
4925                        return Ok(pr);
4926                    }
4927                }
4928                state.save()?;
4929            }
4930        }
4931    }
4932}
4933
4934/// Owner decision for a CI failure independently reproduced on the base.
4935pub const CI_HOLD_NODE: &str = "land-ci-unrelated";
4936
4937/// Durable machine-hold evidence, bound to one observed PR head.
4938#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4939pub struct CiHold {
4940    /// Head that exhibited the independently reproduced failures.
4941    pub head: String,
4942    /// Typed owner question controlling resumption.
4943    pub question: String,
4944    /// Complete structured facts used to attribute the failures.
4945    pub evidence: CiEvidence,
4946}
4947
4948/// Only structured forge annotations are compared, never log prose.
4949#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
4950struct CiFailure {
4951    path: String,
4952    start_line: u64,
4953    end_line: u64,
4954    title: String,
4955    message: String,
4956}
4957
4958#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4959struct CiCheckEvidence {
4960    label: String,
4961    head_check: u64,
4962    base_check: u64,
4963    app: u64,
4964    run: String,
4965    base_run: String,
4966    head_failures: Vec<CiFailure>,
4967    base_failures: Vec<CiFailure>,
4968    rerun_exhausted: bool,
4969}
4970
4971/// Pinned Git diff and forge failure annotations used for CI attribution.
4972#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4973pub struct CiEvidence {
4974    head: String,
4975    base: String,
4976    changed: Vec<String>,
4977    checks: Vec<CiCheckEvidence>,
4978}
4979
4980impl CiEvidence {
4981    /// Every red check must have the same located failure on the base, outside
4982    /// the diff. Changes to CI/build configuration invalidate this inference.
4983    fn unrelated(&self, head: &str, failing: &[String]) -> bool {
4984        if self.head != head
4985            || self.base.is_empty()
4986            || failing.is_empty()
4987            || self.changed.iter().any(|p| {
4988                p.starts_with(".github/")
4989                    || p.starts_with(".config/")
4990                    || p.starts_with(".cargo/")
4991                    || matches!(
4992                        p.rsplit('/').next().unwrap_or(p),
4993                        "Cargo.toml"
4994                            | "Cargo.lock"
4995                            | "Makefile.toml"
4996                            | "build.rs"
4997                            | "rust-toolchain.toml"
4998                    )
4999            })
5000        {
5001            return false;
5002        }
5003        failing.iter().all(|label| {
5004            let matches: Vec<_> = self.checks.iter().filter(|c| &c.label == label).collect();
5005            matches.len() == 1
5006                && matches[0].rerun_exhausted
5007                && !matches[0].head_failures.is_empty()
5008                && matches[0].head_failures.iter().all(|f| {
5009                    // Workflow-level exit-code annotations do not identify a
5010                    // failing test or source file, and prove nothing here.
5011                    !f.path.is_empty()
5012                        && !f.path.starts_with('.')
5013                        && !f.path.contains("..")
5014                        && !f.path.starts_with('/')
5015                        && !self.changed.contains(&f.path)
5016                        && matches[0].base_failures.contains(f)
5017                })
5018        })
5019    }
5020}
5021
5022async fn ci_json(repo: &Path, endpoint: String) -> Option<serde_json::Value> {
5023    let (ok, body) = gh(repo, &["api".into(), endpoint]).await.ok()?;
5024    if !ok {
5025        return None;
5026    }
5027    serde_json::from_str(&body).ok()
5028}
5029
5030async fn ci_annotations(repo: &Path, check: &serde_json::Value) -> Option<Vec<CiFailure>> {
5031    let count = check.pointer("/output/annotations_count")?.as_u64()?;
5032    if count == 0 || count >= 100 {
5033        return None;
5034    }
5035    let id = check.get("id")?.as_u64()?;
5036    let value = ci_json(
5037        repo,
5038        format!("repos/{{owner}}/{{repo}}/check-runs/{id}/annotations?per_page=100"),
5039    )
5040    .await?;
5041    let annotations = value.as_array()?;
5042    if annotations.len() != count as usize {
5043        return None;
5044    }
5045    annotations
5046        .iter()
5047        .filter(|a| a["annotation_level"] == "failure")
5048        .map(|a| {
5049            Some(CiFailure {
5050                path: a["path"].as_str()?.into(),
5051                start_line: a["start_line"].as_u64()?,
5052                end_line: a["end_line"].as_u64()?,
5053                title: a["title"].as_str()?.into(),
5054                message: a["message"].as_str()?.into(),
5055            })
5056        })
5057        .collect()
5058}
5059
5060async fn ci_runs(repo: &Path, sha: &str) -> Option<Vec<serde_json::Value>> {
5061    let value = ci_json(
5062        repo,
5063        format!("repos/{{owner}}/{{repo}}/commits/{sha}/check-runs?per_page=100&filter=latest"),
5064    )
5065    .await?;
5066    let runs = value["check_runs"].as_array()?;
5067    if value["total_count"].as_u64()? != runs.len() as u64 || runs.len() >= 100 {
5068        return None;
5069    }
5070    Some(runs.clone())
5071}
5072
5073async fn collect_ci_evidence(state: &RunState, seen: &Seen) -> Option<CiEvidence> {
5074    let repo = &state.repo;
5075    let base_ref = format!("refs/remotes/{}/{}", state.config.merge.remote, seen.base);
5076    let base = git::git_raw(repo, &["rev-parse", "--verify", &base_ref])
5077        .await
5078        .ok()?;
5079    if !base.ok() {
5080        return None;
5081    }
5082    let range = format!("{}...{}", base.stdout, seen.head);
5083    // No rename detection: both old and new paths must count as changed.
5084    let diff = git::git_raw(
5085        repo,
5086        &["diff", "--no-renames", "--name-only", "-z", &range, "--"],
5087    )
5088    .await
5089    .ok()?;
5090    if !diff.ok() {
5091        return None;
5092    }
5093    let head_runs = ci_runs(repo, &seen.head).await?;
5094    let base_runs = ci_runs(repo, &base.stdout).await?;
5095    let mut checks = Vec::new();
5096    for label in &seen.pr.failing {
5097        let matching = |runs: &[serde_json::Value]| {
5098            runs.iter()
5099                .filter(|r| r["name"].as_str() == Some(label))
5100                .cloned()
5101                .collect::<Vec<_>>()
5102        };
5103        let heads = matching(&head_runs);
5104        let bases = matching(&base_runs);
5105        if heads.len() != 1 || bases.len() != 1 {
5106            return None;
5107        }
5108        let h = &heads[0];
5109        let b = &bases[0];
5110        if h["conclusion"] != "failure"
5111            || b["conclusion"] != "failure"
5112            || h["status"] != "completed"
5113            || b["status"] != "completed"
5114            || h["head_sha"].as_str()? != seen.head
5115            || b["head_sha"].as_str()? != base.stdout
5116            || h.pointer("/app/id")?.as_u64()? != b.pointer("/app/id")?.as_u64()?
5117        {
5118            return None;
5119        }
5120        let run = run_of(h["details_url"].as_str()?)?;
5121        let contexts: Vec<_> = seen.contexts.iter().filter(|c| &c.label == label).collect();
5122        if contexts.len() != 1
5123            || contexts[0].run.as_ref() != Some(&run)
5124            || contexts[0].verdict != Verdict::Fail
5125        {
5126            return None;
5127        }
5128        // A persisted request alone is not a completed rerun: require the
5129        // workflow's structured attempt number as well.
5130        let workflow =
5131            ci_json(repo, format!("repos/{{owner}}/{{repo}}/actions/runs/{run}")).await?;
5132        let base_run = run_of(b["details_url"].as_str()?)?;
5133        let base_workflow = ci_json(
5134            repo,
5135            format!("repos/{{owner}}/{{repo}}/actions/runs/{base_run}"),
5136        )
5137        .await?;
5138        if base_workflow["head_sha"].as_str()? != base.stdout
5139            || base_workflow["head_branch"].as_str()? != seen.base
5140            || base_workflow["status"] != "completed"
5141            || base_workflow["conclusion"] != "failure"
5142            || base_workflow["workflow_id"].as_u64()? != workflow["workflow_id"].as_u64()?
5143        {
5144            return None;
5145        }
5146        let exhausted = state
5147            .land_reruns
5148            .as_ref()
5149            .is_some_and(|r| r.head == seen.head && r.runs.contains_key(&run))
5150            && workflow["head_sha"].as_str()? == seen.head
5151            && workflow["run_attempt"].as_u64()? > 1
5152            && workflow["status"] == "completed";
5153        checks.push(CiCheckEvidence {
5154            label: label.clone(),
5155            head_check: h["id"].as_u64()?,
5156            base_check: b["id"].as_u64()?,
5157            app: h.pointer("/app/id")?.as_u64()?,
5158            run,
5159            base_run,
5160            head_failures: ci_annotations(repo, h).await?,
5161            base_failures: ci_annotations(repo, b).await?,
5162            rerun_exhausted: exhausted,
5163        });
5164    }
5165    Some(CiEvidence {
5166        head: seen.head.clone(),
5167        base: base.stdout,
5168        changed: diff
5169            .stdout
5170            .split('\0')
5171            .filter(|p| !p.is_empty())
5172            .map(str::to_owned)
5173            .collect(),
5174        checks,
5175    })
5176}
5177
5178async fn hold_unrelated(state: &mut RunState, pr: &PrState, evidence: CiEvidence) -> Result<()> {
5179    let detail = serde_json::to_string(&evidence)?;
5180    let resume = "daemon: CI を確認して同じ run を再開する".to_owned();
5181    let hold = "operator: 保留して後で判断する".to_owned();
5182    let mut q = ask::Question::new(
5183        state.id.clone(),
5184        CI_HOLD_NODE.into(),
5185        "land".into(),
5186        "base でも再現する CI の失敗を検出したため、試行を消費せず保留しました".into(),
5187        format!(
5188            "必須チェックや承認は省略しません。再開時にも同じ workflow run は自動再実行しません。\n構造化された判定根拠:\n{detail}"
5189        ),
5190        vec![resume.clone(), hold.clone()],
5191    );
5192    q.actions.insert(
5193        resume,
5194        ask::ChoiceAction::Resume {
5195            run: state.id.clone(),
5196        },
5197    );
5198    q.actions.insert(hold, ask::ChoiceAction::Hold);
5199    ask::Questions::open().put_executable(&mut q)?;
5200    state.land_unrelated = Some(CiHold {
5201        head: evidence.head.clone(),
5202        question: q.id.clone(),
5203        evidence,
5204    });
5205    state.event("land", format!("[ci-unrelated] base reproduces every located failure after rerun; owner question {}: {detail}", q.id));
5206    state.end_pass_parked(crate::run::ParkCause::Question);
5207    state.parked = true;
5208    state.save()?;
5209    if let Err(e) = ask::notify(&state.config.notify, &q).await {
5210        tracing::warn!("could not notify about CI hold: {e:#}");
5211    }
5212    // A durable question and evidence are sufficient even if commenting fails.
5213    let body = stop_comment(
5214        &state.id,
5215        "CI failures independently reproduced on the base; awaiting an owner decision without charging the task attempt",
5216    );
5217    let (_, body) = crate::github_text::prepare(state, "", &body);
5218    gh(
5219        &state.repo,
5220        &[
5221            "pr".into(),
5222            "comment".into(),
5223            pr.number.to_string(),
5224            "--body".into(),
5225            body,
5226        ],
5227    )
5228    .await
5229    .ok();
5230    Ok(())
5231}
5232
5233/// One observation, plus the two things [`PrState`] deliberately does not carry:
5234/// the title (needed for the squash subject) and where the failing checks'
5235/// logs live.
5236#[derive(Clone)]
5237struct Seen {
5238    pr: PrState,
5239    title: String,
5240    failing_urls: Vec<(String, String)>,
5241    /// `headRefOid`: the commit this observation, checks included, is about.
5242    head: String,
5243    /// The commit the checks belong to, read from the same GraphQL node as
5244    /// the checks themselves. Empty when unreadable.
5245    rollup_head: String,
5246    /// `mergeStateStatus` as the forge spelled it. [`Blocking`] folds BLOCKED,
5247    /// BEHIND and DRAFT together, and a stop reason has to say which.
5248    merge_state: String,
5249    /// Every check of the rollup, for naming what an armed merge waits on.
5250    contexts: Vec<CheckInfo>,
5251    /// `baseRefName`, to look up which contexts the base requires.
5252    base: String,
5253}
5254
5255/// One check of the rollup as far as a stop reason needs it.
5256#[derive(Clone)]
5257struct CheckInfo {
5258    label: String,
5259    verdict: Verdict,
5260    required: Option<bool>,
5261    /// The workflow run the check belongs to; `None` for an external status.
5262    run: Option<String>,
5263}
5264
5265/// What to do with a red head before a fix round is spent on it.
5266#[derive(Debug, Clone, PartialEq, Eq)]
5267enum RerunStep {
5268    /// Rerun these workflow runs (each not yet rerun for this head).
5269    Rerun(Vec<String>),
5270    /// Reran already and the forge may still report the old failure: wait.
5271    Grace,
5272    /// Nothing to rerun, or the rerun did not help: the fixer's turn.
5273    Fix,
5274}
5275
5276/// Failed checks a rerun may help: a failure of a check that blocks (`required`
5277/// unreadable counts), as `(label, run)`. Judged one workflow run at a time;
5278/// pending checks in another run do not delay this run's rerun.
5279#[cfg(test)]
5280fn rerunnable(contexts: &[CheckInfo]) -> Vec<(String, Option<String>)> {
5281    rerunnable_with(contexts, false)
5282}
5283
5284fn rerunnable_with(
5285    contexts: &[CheckInfo],
5286    include_optional: bool,
5287) -> Vec<(String, Option<String>)> {
5288    contexts
5289        .iter()
5290        .filter(|c| c.verdict == Verdict::Fail && (include_optional || c.required != Some(false)))
5291        // `gh run rerun --failed` refuses a run with a job still going.
5292        .filter(|c| {
5293            c.run.as_ref().is_none_or(|r| {
5294                !contexts.iter().any(|o| {
5295                    matches!(o.verdict, Verdict::Pending | Verdict::Unknown)
5296                        && o.run.as_ref() == Some(r)
5297                })
5298            })
5299        })
5300        .map(|c| (c.label.clone(), c.run.clone()))
5301        .collect()
5302}
5303
5304/// Pure: rerun once per run id, wait out the grace, then hand over.
5305fn rerun_step(
5306    failing: &[(String, Option<String>)],
5307    record: Option<&LandReruns>,
5308    head: &str,
5309    now: i64,
5310) -> RerunStep {
5311    // A failure with no workflow run (an external status) cannot be rerun, and
5312    // the fixer's push re-runs everything anyway.
5313    if failing.is_empty() || failing.iter().any(|(_, run)| run.is_none()) {
5314        return RerunStep::Fix;
5315    }
5316    // Reruns belong to the head they were made on.
5317    let record = record.filter(|r| r.head.eq_ignore_ascii_case(head));
5318    let mut fresh: Vec<String> = Vec::new();
5319    let mut recent = false;
5320    for run in failing.iter().filter_map(|(_, run)| run.as_deref()) {
5321        match record.and_then(|r| r.runs.get(run)) {
5322            None => {
5323                if !fresh.iter().any(|f| f == run) {
5324                    fresh.push(run.to_owned());
5325                }
5326            }
5327            Some(&at) if now - at < crate::release_watch::RERUN_GRACE => recent = true,
5328            Some(_) => {}
5329        }
5330    }
5331    if !fresh.is_empty() {
5332        RerunStep::Rerun(fresh)
5333    } else if recent {
5334        RerunStep::Grace
5335    } else {
5336        RerunStep::Fix
5337    }
5338}
5339
5340/// Read the pull request: `gh pr view` for the top-level thread and merge
5341/// state, `gh api graphql` for the last commit and its checks, `gh api` for the
5342/// inline review comments `gh pr view` does not report.
5343async fn observe(repo: &Path, pr_url: &str) -> Result<Seen> {
5344    let view = gh(
5345        repo,
5346        &[
5347            "pr".to_owned(),
5348            "view".to_owned(),
5349            pr_url.to_owned(),
5350            "--json".to_owned(),
5351            "url,number,state,title,reviews,comments,mergeStateStatus,headRefOid,baseRefName"
5352                .to_owned(),
5353        ],
5354    )
5355    .await?;
5356    if !view.0 {
5357        bail!("gh pr view {pr_url}: {}", view.1);
5358    }
5359    let number = parse_pr(&view.1)?.number;
5360    let node = last_commit_node(repo, number).await;
5361    let mut seen = seen_from(&view.1, node.as_deref())?;
5362
5363    let inline = gh(
5364        repo,
5365        &[
5366            "api".to_owned(),
5367            format!("repos/{{owner}}/{{repo}}/pulls/{}/comments", seen.pr.number),
5368        ],
5369    )
5370    .await?;
5371    if inline.0 {
5372        match parse_inline_comments(&inline.1) {
5373            Ok(mut comments) => seen.pr.review_comments.append(&mut comments),
5374            // An unreadable inline thread must not end a landing: the rollup
5375            // and the top-level thread are still real signal.
5376            Err(e) => tracing::warn!("inline review comments unreadable: {e}"),
5377        }
5378    } else {
5379        tracing::warn!("gh api pulls/{}/comments: {}", seen.pr.number, inline.1);
5380    }
5381    Ok(seen)
5382}
5383
5384/// Build a [`Seen`] from the `gh pr view` json and the last-commit node
5385/// response. No I/O.
5386///
5387/// The commit oid and the checks are read from the *same* node, so the rollup
5388/// is bound to the commit it belongs to by construction; two reads that agree
5389/// before and after another response prove nothing about what that response
5390/// held. The view's own rollup is never used. A node that is missing,
5391/// unreadable, carries GraphQL errors, or whose checks run past the page
5392/// leaves `rollup_head` empty and the checks `Unknown`, which the loop treats
5393/// as "look again" and never as a reason to merge.
5394fn seen_from(view_json: &str, node_json: Option<&str>) -> Result<Seen> {
5395    let mut pr = parse_pr(view_json)?;
5396    let raw: GhPr = serde_json::from_str(view_json).context("re-read pull request json")?;
5397
5398    let mut rollup_head = String::new();
5399    let mut failing_urls = Vec::new();
5400    let mut contexts = Vec::new();
5401    let mut checks = Checks::Unknown;
5402    let mut failing = Vec::new();
5403    if let Some((oid, rollup)) = node_json.and_then(parse_last_commit_node) {
5404        (checks, failing) = rollup_verdict(&rollup);
5405        failing_urls = rollup
5406            .iter()
5407            .filter(|c| c.verdict() == Verdict::Fail)
5408            .filter_map(|c| c.url().map(|u| (c.label(), u.to_owned())))
5409            .collect();
5410        contexts = rollup
5411            .iter()
5412            .map(|c| CheckInfo {
5413                label: c.label(),
5414                verdict: c.verdict(),
5415                required: c.is_required,
5416                run: c.url().and_then(run_of),
5417            })
5418            .collect();
5419        rollup_head = oid;
5420    }
5421    pr.checks = checks;
5422    pr.failing = failing;
5423
5424    Ok(Seen {
5425        pr,
5426        title: raw.title,
5427        failing_urls,
5428        head: raw.head_ref_oid,
5429        rollup_head,
5430        merge_state: raw.merge_state_status,
5431        contexts,
5432        base: raw.base_ref_name,
5433    })
5434}
5435
5436/// The last commit's oid and its checks from one GraphQL response, `None`
5437/// when anything about it cannot be trusted.
5438fn parse_last_commit_node(json: &str) -> Option<(String, Vec<GhCheck>)> {
5439    let v: serde_json::Value = serde_json::from_str(json).ok()?;
5440    if v.get("errors").is_some_and(|e| !e.is_null()) {
5441        return None;
5442    }
5443    let commit = v.pointer("/data/repository/pullRequest/commits/nodes/0/commit")?;
5444    let oid = commit.get("oid")?.as_str().filter(|o| !o.is_empty())?;
5445    let contexts = commit.pointer("/statusCheckRollup/contexts");
5446    let Some(contexts) = contexts.filter(|c| !c.is_null()) else {
5447        // No rollup at all: the commit has no checks.
5448        return Some((oid.to_owned(), Vec::new()));
5449    };
5450    if contexts.pointer("/pageInfo/hasNextPage")?.as_bool()? {
5451        return None;
5452    }
5453    let nodes = contexts.get("nodes")?.as_array()?;
5454    let rollup = nodes
5455        .iter()
5456        .map(|n| serde_json::from_value::<GhCheck>(n.clone()))
5457        .collect::<Result<Vec<_>, _>>()
5458        .ok()?;
5459    Some((oid.to_owned(), rollup))
5460}
5461
5462/// The pull request's last commit and its checks, in one response. `None`
5463/// when the forge could not be asked.
5464async fn last_commit_node(repo: &Path, number: u64) -> Option<String> {
5465    let out = gh(
5466        repo,
5467        &[
5468            "api".to_owned(),
5469            "graphql".to_owned(),
5470            "-F".to_owned(),
5471            "owner={owner}".to_owned(),
5472            "-F".to_owned(),
5473            "repo={repo}".to_owned(),
5474            "-F".to_owned(),
5475            format!("number={number}"),
5476            "-f".to_owned(),
5477            "query=query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,\
5478             name:$repo){pullRequest(number:$number){commits(last:1){nodes{commit{oid \
5479             statusCheckRollup{contexts(first:100){pageInfo{hasNextPage} nodes{\
5480             ... on CheckRun{name status conclusion detailsUrl \
5481             isRequired(pullRequestNumber:$number)} \
5482             ... on StatusContext{context state targetUrl \
5483             isRequired(pullRequestNumber:$number)}}}}}}}}}}"
5484                .to_owned(),
5485        ],
5486    )
5487    .await
5488    .ok()?;
5489    out.0.then_some(out.1)
5490}
5491
5492/// What a fix round did.
5493#[doc(hidden)]
5494#[derive(Debug, PartialEq)]
5495pub enum Fixed {
5496    /// The fixer committed something, and this is the head that was pushed.
5497    Committed {
5498        /// The commit now at the tip of the pushed branch.
5499        head: String,
5500    },
5501    /// The fixer ran and chose to change nothing.
5502    Declined,
5503    /// The fixer could not run, or said nothing usable.
5504    Failed(String),
5505}
5506
5507/// Hand the failures and the comments to the fixer, then commit and push.
5508///
5509/// The fixer works in the winner's own worktree so its commits land on the
5510/// branch the pull request is built from, and it runs with `allow_write` for
5511/// the same reason.
5512#[doc(hidden)]
5513pub async fn fix_round(
5514    state: &mut RunState,
5515    pr: &PrState,
5516    round: usize,
5517    budget: usize,
5518    reason: &str,
5519    logs: &str,
5520) -> Result<Fixed> {
5521    let winner = state
5522        .winner()
5523        .cloned()
5524        .context("landing needs a winning candidate; none is recorded on this run")?;
5525    let roles = state
5526        .config
5527        .resolve_roles()
5528        .context("resolve the roster for the fix round")?;
5529    // Same chain as the review loop (see `crate::fixer`): the configured
5530    // fixers in order, otherwise the winner's own author continuing its own
5531    // conversation - the competition is over, so its context is pure benefit.
5532    let attempts = crate::fixer::attempts(state, &roles, &winner);
5533    let ids: Vec<String> = attempts.iter().map(|(s, _)| s.id.clone()).collect();
5534
5535    let prompt = fix_prompt(state, pr, round, budget, reason, logs);
5536    let mut artifacts = agent::artifacts_dir(&state.dir());
5537    if let Some(request) = state
5538        .merge_repairs
5539        .last()
5540        .filter(|r| r.phase == crate::run::MergeRepairPhase::Fixing)
5541    {
5542        artifacts = artifacts.join(format!("merge-repair-{}", request.question));
5543    }
5544    let prompt = if state.config.cache_dir().is_some() {
5545        format!("{prompt}\n\n{}", prompt::build_cache_note("fix", true))
5546    } else {
5547        prompt
5548    };
5549    // Read before the fixer runs: it normally commits for itself, so HEAD has
5550    // already moved by the time it returns and a later read would see no
5551    // progress (run 20261004-041622-5769 stopped on a fix that had landed).
5552    let before = git::rev_parse(&winner.worktree, "HEAD").await?;
5553    // Each id at most once, forward only: the next one is asked only when the
5554    // call advances, and only the last attempt's result is judged below, so an
5555    // exhausted chain ends as a single failed fixer does.
5556    let mut last = None;
5557    for (i, (spec, seat_key)) in attempts.into_iter().enumerate() {
5558        if owner_requested_stop(state)? {
5559            return Ok(Fixed::Declined);
5560        }
5561        let mut seat = seat_of(state, &seat_key, &spec.id);
5562        let stem = if i == 0 {
5563            format!("land-{round}")
5564        } else {
5565            format!("land-{round}-{}", spec.id)
5566        };
5567        let out = agent::invoke(
5568            &spec,
5569            &mut seat,
5570            &Invocation {
5571                coordination: state.config.coordination.as_ref(),
5572                fetched_base: state.fetched_base.as_ref(),
5573                cwd: &winner.worktree,
5574                prompt: &prompt,
5575                timeout: Duration::from_secs(state.config.graph.timeout_fix),
5576                allow_write: true,
5577                sessions: state.config.graph.sessions,
5578                artifacts: &artifacts,
5579                stem: &stem,
5580                run: &state.id,
5581                node: "land",
5582                cache_dir: state.config.cache_dir().as_deref(),
5583                attachments: &[],
5584                writable: &[],
5585            },
5586        )
5587        .await;
5588        state.seats.insert(seat.key.clone(), seat);
5589        if let Some(next) = ids.get(i + 1)
5590            && agent::chain_advances(&out)
5591        {
5592            let (class, why) = crate::fixer::failure_of(&out);
5593            crate::graph::record_handover(state, "land", &seat_key, &spec.id, next, &class, &why);
5594            continue;
5595        }
5596        last = Some(out);
5597        break;
5598    }
5599    let out = last.expect("the fixer chain always has an entry");
5600
5601    match out {
5602        Ok(o) if o.quota_exhausted() => {
5603            return Ok(Fixed::Failed(
5604                "rate limited (quota); the fixer could not run".to_owned(),
5605            ));
5606        }
5607        Ok(o) if !o.usable() => {
5608            return Ok(Fixed::Failed(format!(
5609                "the fixer produced nothing usable ({})",
5610                crate::notices::turn_status(o.exit_code, o.timed_out, o.quota_exhausted())
5611            )));
5612        }
5613        Ok(_) => {}
5614        Err(e) if e.is::<crate::repo_lock::ResourceBlocked>() => return Err(e),
5615        Err(e) => return Ok(Fixed::Failed(format!("{e:#}"))),
5616    }
5617
5618    if owner_requested_stop(state)? {
5619        return Ok(Fixed::Declined);
5620    }
5621
5622    // An agent that edited files but never committed would otherwise push
5623    // nothing and look like a refusal.
5624    let wait = crate::repo_lock::WaitPolicy::new(
5625        Duration::from_secs(state.config.graph.timeout_resource_wait),
5626        || false,
5627    );
5628    let coordination = crate::repo_lock::SeatLease::operation_with(
5629        &winner.worktree,
5630        state.config.coordination.as_ref(),
5631        &wait,
5632    )
5633    .await?;
5634    match git::rescue_commit(
5635        &winner.worktree,
5636        &format!("magi: land round {round} fixes (uncommitted work)"),
5637    )
5638    .await
5639    {
5640        Ok(r) => state.note_withheld("land", &r.withheld),
5641        Err(e)
5642            if state
5643                .merge_repairs
5644                .last()
5645                .is_some_and(|r| r.phase == crate::run::MergeRepairPhase::Fixing) =>
5646        {
5647            return Ok(Fixed::Failed(format!("saving repair commit failed: {e:#}")));
5648        }
5649        Err(_) => {}
5650    }
5651    let after = git::rev_parse(&winner.worktree, "HEAD").await?;
5652    drop(coordination);
5653    if after == before {
5654        return Ok(Fixed::Declined);
5655    }
5656
5657    if owner_requested_stop(state)? {
5658        return Ok(Fixed::Declined);
5659    }
5660    crate::pr_head::ensure_owner(state)?;
5661    let remote = state.config.merge.remote.clone();
5662    anyhow::ensure!(
5663        crate::foreign_checkout::write_back(state, &after).await?,
5664        "foreign checkout delivery is blocked; see the run events"
5665    );
5666    let public_head = state.pr_branch().unwrap_or(&winner.branch).to_owned();
5667    let push = if state.pr_head.is_some() {
5668        let expected = state
5669            .pr_pushed_head
5670            .as_deref()
5671            .context("missing public head lease")?;
5672        git::push_as(
5673            &winner.worktree,
5674            &remote,
5675            &winner.branch,
5676            &public_head,
5677            expected,
5678        )
5679        .await?
5680    } else {
5681        git::push(&winner.worktree, &remote, &winner.branch).await?
5682    };
5683    if !push.ok() {
5684        return Ok(Fixed::Failed(format!(
5685            "pushing {} to {remote} failed: {}",
5686            public_head, push.stderr
5687        )));
5688    }
5689    state.event(
5690        "land",
5691        format!("round {round}: pushed a fix to {public_head}"),
5692    );
5693    state.pr_pushed_head = Some(after.clone());
5694    state.save()?;
5695    Ok(Fixed::Committed { head: after })
5696}
5697
5698/// Fetch or create a seat, keeping its conversation across nodes.
5699pub(crate) fn seat_of(state: &mut RunState, key: &str, agent: &str) -> SeatState {
5700    crate::fixer::seat_for(state, key, agent)
5701}
5702
5703/// Recheck the live PR and local worktree before spending the requested fix.
5704/// A moved/closed PR is held, never repaired using obsolete evidence.
5705pub(crate) async fn repair_preflight(state: &RunState, url: &str, head: &str) -> Result<PrState> {
5706    let seen = observe(&state.repo, url).await?;
5707    let winner = state.winner().context("no winning branch to repair")?;
5708    if seen.pr.state != PrLifecycle::Open || seen.head != head {
5709        anyhow::bail!(
5710            "PR is closed or its head changed; review the current head before requesting repair again"
5711        );
5712    }
5713    if git::rev_parse(&winner.worktree, "HEAD").await? != head
5714        || !git::is_clean(&winner.worktree).await?
5715    {
5716        anyhow::bail!(
5717            "repair worktree is missing, dirty or at a different head; restore the reviewed branch and resume this run"
5718        );
5719    }
5720    // Always disable any forge-side reservation, including one not recorded
5721    // by this run. A repair request is never merge authorization.
5722    let view = gh(
5723        &state.repo,
5724        &[
5725            "pr".into(),
5726            "view".into(),
5727            url.into(),
5728            "--json".into(),
5729            "autoMergeRequest,headRefOid,state,headRefName,baseRefName,isCrossRepository".into(),
5730        ],
5731    )
5732    .await?;
5733    if !view.0 {
5734        bail!("cannot check auto-merge: {}", view.1);
5735    }
5736    let live: serde_json::Value = serde_json::from_str(&view.1)?;
5737    let public = state.pr_branch().unwrap_or(&winner.branch);
5738    if live["state"] != "OPEN"
5739        || live["headRefOid"] != head
5740        || live["headRefName"] != public
5741        || live["baseRefName"] != state.base_branch
5742        || live["isCrossRepository"] != false
5743    {
5744        bail!("PR changed during repair preflight; review its current head explicitly");
5745    }
5746    let auto_merge = live
5747        .get("autoMergeRequest")
5748        .context("cannot read the PR auto-merge reservation; repair held")?;
5749    if !auto_merge.is_null() {
5750        let out = gh(&state.repo, &disable_automerge_argv(seen.pr.number)).await?;
5751        if !out.0 {
5752            bail!("cannot disable auto-merge; repair held: {}", out.1);
5753        }
5754    }
5755    Ok(seen.pr)
5756}
5757
5758/// What the fixer is told.
5759fn fix_prompt(
5760    state: &RunState,
5761    pr: &PrState,
5762    round: usize,
5763    budget: usize,
5764    reason: &str,
5765    logs: &str,
5766) -> String {
5767    let mut s = format!(
5768        "Your patch is open as a pull request and it is not landing. Land round \
5769         {round} of {budget}.\n\n\
5770         Pull request: {}\n\n\
5771         What is holding it: {reason}\n\n\
5772         # The task\n\n{}\n",
5773        pr.url, state.instruction
5774    );
5775
5776    if pr.failing.is_empty() {
5777        s.push_str("\n# Failing checks\n\n(none)\n");
5778    } else {
5779        let _ = write!(s, "\n# Failing checks\n\n- {}\n", pr.failing.join("\n- "));
5780        if logs.trim().is_empty() {
5781            s.push_str(
5782                "\nNo log could be read; investigate the failure with necessary focused checks.\n",
5783            );
5784        } else {
5785            let _ = write!(s, "\n## Failing log tails\n\n{logs}\n");
5786        }
5787    }
5788
5789    if pr.review_comments.is_empty() {
5790        s.push_str("\n# Review comments\n\n(none)\n");
5791    } else {
5792        s.push_str("\n# Review comments\n");
5793        for c in &pr.review_comments {
5794            let where_ = match (&c.path, c.line) {
5795                (Some(p), Some(l)) => format!(" ({p}:{l})"),
5796                (Some(p), None) => format!(" ({p})"),
5797                _ => String::new(),
5798            };
5799            let _ = write!(s, "\n## {}{where_}\n\n{}\n", c.author, c.body.trim());
5800        }
5801    }
5802
5803    s.push_str(
5804        "\n# Rules\n\n\
5805         1. Fix the cause, never the symptom. Do not delete, skip, or weaken a \
5806            failing test; do not silence a lint with an allow attribute; do not \
5807            stretch a timeout to hide a race. If the check is right, the code is \
5808            wrong.\n\
5809         2. Change nothing the checks and the comments did not raise. A \
5810            drive-by refactor turns a one-line fix into a pull request that \
5811            needs reviewing again.\n\
5812         3. If a comment is wrong, say so with a checkable argument and change \
5813            nothing for it. A declined comment with a reason is a correct \
5814            outcome; a change made to appease a reviewer is not.\n\
5815         4. Commit in this worktree. magi pushes to the pull request's branch \
5816            for you; do not push, merge, or close anything yourself.\n\
5817         5. Never name yourself, your vendor, or your model, anywhere.\n\n\
5818         # Output\n\n\
5819         Say what you changed and why, and what you declined and why.",
5820    );
5821
5822    let language = &state.config.graph.language;
5823    if !(language.trim().is_empty() || language.eq_ignore_ascii_case("en")) {
5824        let _ = write!(s, "\n\nWrite all prose in {language}.");
5825    }
5826    // After the language line, so the exception is the last word on it.
5827    s.push_str(&crate::prompt::github_rule(
5828        language,
5829        &state.config.graph.github_language,
5830    ));
5831    if let Some(overlay) = state.config.prompts.overlay("fix") {
5832        let _ = write!(s, "\n\n{overlay}");
5833    }
5834    s.push('\n');
5835    s.push_str(crate::prompt::child_run_ownership());
5836    s
5837}
5838
5839/// Failing log tails, the way the operator collects them by hand:
5840/// `gh run view --log-failed`.
5841async fn failing_logs(repo: &Path, failing: &[(String, String)]) -> String {
5842    let mut out = String::new();
5843    for (name, url) in failing.iter().take(MAX_LOGS) {
5844        let args = match (job_of(url), run_of(url)) {
5845            (Some(job), _) => vec![
5846                "run".to_owned(),
5847                "view".to_owned(),
5848                "--log-failed".to_owned(),
5849                "--job".to_owned(),
5850                job,
5851            ],
5852            (None, Some(run)) => vec![
5853                "run".to_owned(),
5854                "view".to_owned(),
5855                run,
5856                "--log-failed".to_owned(),
5857            ],
5858            // Not a GitHub Actions check - an external status has no log here.
5859            (None, None) => continue,
5860        };
5861        let (ok, body) = match gh(repo, &args).await {
5862            Ok(v) => v,
5863            Err(e) => (false, format!("{e:#}")),
5864        };
5865        if !ok && body.trim().is_empty() {
5866            continue;
5867        }
5868        let _ = write!(out, "### {name}\n\n```\n{}\n```\n\n", tail(&body, LOG_TAIL));
5869    }
5870    out
5871}
5872
5873/// Job id out of a check's `detailsUrl`
5874/// (`https://github.com/o/r/actions/runs/<run>/job/<job>`).
5875fn job_of(details_url: &str) -> Option<String> {
5876    let after = details_url.split("/job/").nth(1)?;
5877    let id: String = after.chars().take_while(char::is_ascii_digit).collect();
5878    (!id.is_empty()).then_some(id)
5879}
5880
5881/// Workflow run id out of a check's `detailsUrl`.
5882pub(crate) fn run_of(details_url: &str) -> Option<String> {
5883    let after = details_url.split("/actions/runs/").nth(1)?;
5884    let id: String = after.chars().take_while(char::is_ascii_digit).collect();
5885    (!id.is_empty()).then_some(id)
5886}
5887
5888/// The comment `stop` posts. Fixed English, whatever `[graph] language` says:
5889/// it lands on GitHub, not in front of the operator. Pure so a test can hold
5890/// it to that.
5891fn stop_comment(run_id: &str, why: &str) -> String {
5892    format!(
5893        "{MARKER}\nmagi stopped landing this pull request: {why}\n\n\
5894         The branch is untouched and the run is `{run_id}`. Nothing was merged."
5895    )
5896}
5897
5898/// Leave the pull request open, say why on it, and mark the run blocked.
5899///
5900/// The comment is what makes an unattended stop actionable: the operator wakes
5901/// up to a pull request that explains itself rather than to a silent queue.
5902async fn stop(state: &mut RunState, repo: &Path, pr: &PrState, why: &str) -> Result<()> {
5903    let body = stop_comment(&state.id, why);
5904    let (_, mut body) = crate::github_text::prepare(state, "", &body);
5905    if !body.contains(MARKER) {
5906        body = format!("{MARKER}\n{body}");
5907    }
5908    let posted = gh(
5909        repo,
5910        &[
5911            "pr".to_owned(),
5912            "comment".to_owned(),
5913            pr.number.to_string(),
5914            "--body".to_owned(),
5915            body,
5916        ],
5917    )
5918    .await;
5919    match posted {
5920        Ok((true, _)) => {}
5921        Ok((false, out)) => tracing::warn!("could not comment on {}: {out}", pr.url),
5922        Err(e) => tracing::warn!("could not comment on {}: {e:#}", pr.url),
5923    }
5924    state.status = RunStatus::Blocked;
5925    state.merge = Some(MergeOutcome {
5926        mode: MergeMode::Pr,
5927        ok: false,
5928        detail: why.to_owned(),
5929        empty: false,
5930    });
5931    state.event("land", format!("stopped: {why}"));
5932    state.save()?;
5933    Ok(())
5934}
5935
5936/// Run `gh` in `repo`, returning success and the combined output.
5937///
5938/// Combined because `gh` reports a refused merge on stderr and the pull request
5939/// json on stdout, and both are evidence.
5940///
5941/// `GH_REPO` is stripped from the child's environment: every call site here
5942/// passes an explicit `cwd` (or a full pull request URL) meaning to operate
5943/// on *that* checkout's own remote, and `gh` prefers `GH_REPO` over the
5944/// checkout it is sitting in when no `--repo` flag is given. Left unset, a
5945/// `GH_REPO` the operator happens to have exported for an unrelated script
5946/// would silently redirect [`repo_slug`] (and every other cwd-scoped call
5947/// below) to a different repository than the one actually on disk - which
5948/// for the same-repo guard in [`correct_manual_merge`] would mean the check
5949/// could be made to agree with whatever repository a forged `--merged` URL
5950/// claims, defeating it entirely.
5951pub(crate) async fn gh(cwd: &Path, args: &[String]) -> Result<(bool, String)> {
5952    let out = tokio::process::Command::new("gh")
5953        .args(args)
5954        .current_dir(cwd)
5955        .env_remove("GH_REPO")
5956        .quiet()
5957        .stdin(std::process::Stdio::null())
5958        .output()
5959        .await
5960        .with_context(|| format!("spawn gh {}", args.join(" ")))?;
5961    let mut body = String::from_utf8_lossy(&out.stdout).into_owned();
5962    let err = String::from_utf8_lossy(&out.stderr);
5963    if body.trim().is_empty() {
5964        body = err.into_owned();
5965    } else if !err.trim().is_empty() {
5966        body.push_str(&err);
5967    }
5968    Ok((out.status.success(), body.trim().to_owned()))
5969}
5970
5971/// Verdict of one entry in the status rollup.
5972#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5973pub(crate) enum Verdict {
5974    Pass,
5975    Fail,
5976    Pending,
5977    Unknown,
5978}
5979
5980impl Verdict {
5981    /// Short operator-facing check result.
5982    pub(crate) fn label(self) -> &'static str {
5983        match self {
5984            Self::Pass => "passed",
5985            Self::Fail => "failed",
5986            Self::Pending => "pending",
5987            Self::Unknown => "unknown",
5988        }
5989    }
5990}
5991
5992#[derive(Debug, Deserialize)]
5993#[serde(rename_all = "camelCase")]
5994struct GhPr {
5995    #[serde(default)]
5996    url: String,
5997    #[serde(default)]
5998    number: u64,
5999    #[serde(default)]
6000    state: String,
6001    #[serde(default)]
6002    title: String,
6003    #[serde(default)]
6004    status_check_rollup: Vec<GhCheck>,
6005    /// GitHub's own verdict on whether the pull request can be merged.
6006    ///
6007    /// Worth asking for because it is the only place the *required* check set
6008    /// is applied: the rollup lists every check equally, so a repository that
6009    /// deliberately does not require `coverage` still looks red here. See
6010    /// [`Blocking`].
6011    #[serde(default)]
6012    merge_state_status: String,
6013    /// The commit the pull request currently points at. Compared with the
6014    /// commit a fix round pushed, it is how the loop knows the forge has moved
6015    /// on and the rollup belongs to the new head.
6016    #[serde(default)]
6017    head_ref_oid: String,
6018    #[serde(default)]
6019    base_ref_name: String,
6020    #[serde(default)]
6021    reviews: Vec<GhReview>,
6022    #[serde(default)]
6023    comments: Vec<GhComment>,
6024}
6025
6026/// One rollup entry. `gh` mixes two GraphQL types in this array: a `CheckRun`
6027/// has `name`/`status`/`conclusion`, while a `StatusContext` - the old commit
6028/// status API, which is how CodeRabbit reports - has `context`/`state` and no
6029/// conclusion at all.
6030#[derive(Debug, Deserialize)]
6031#[serde(rename_all = "camelCase")]
6032struct GhCheck {
6033    #[serde(default)]
6034    name: Option<String>,
6035    #[serde(default)]
6036    context: Option<String>,
6037    #[serde(default)]
6038    status: Option<String>,
6039    #[serde(default)]
6040    conclusion: Option<String>,
6041    #[serde(default)]
6042    state: Option<String>,
6043    #[serde(default)]
6044    details_url: Option<String>,
6045    #[serde(default)]
6046    target_url: Option<String>,
6047    /// Whether the base branch requires this check. Only the GraphQL node
6048    /// carries it; `None` is "not read", never "not required".
6049    #[serde(default)]
6050    is_required: Option<bool>,
6051}
6052
6053impl GhCheck {
6054    /// Name to show a human and hand to the fixer.
6055    fn label(&self) -> String {
6056        self.name
6057            .clone()
6058            .or_else(|| self.context.clone())
6059            .unwrap_or_else(|| "(unnamed check)".to_owned())
6060    }
6061
6062    /// Where this check's logs live, when it has any.
6063    fn url(&self) -> Option<&str> {
6064        self.details_url
6065            .as_deref()
6066            .or(self.target_url.as_deref())
6067            .filter(|u| !u.is_empty())
6068    }
6069
6070    /// Did it pass?
6071    ///
6072    /// `SKIPPED` and `NEUTRAL` count as passed: the Claude review workflow
6073    /// skips release and bot pull requests by design, and a skip that blocked
6074    /// landing would block exactly the pull requests that need no review.
6075    /// `CANCELLED` counts as failed - a cancelled check did not pass, and
6076    /// merging over one is merging over a check that never ran.
6077    fn verdict(&self) -> Verdict {
6078        if let Some(status) = self.status.as_deref() {
6079            if !status.eq_ignore_ascii_case("COMPLETED") {
6080                return Verdict::Pending;
6081            }
6082        }
6083        let outcome = self
6084            .conclusion
6085            .as_deref()
6086            .or(self.state.as_deref())
6087            .unwrap_or("");
6088        match outcome.to_ascii_uppercase().as_str() {
6089            "SUCCESS" | "SKIPPED" | "NEUTRAL" => Verdict::Pass,
6090            "FAILURE" | "ERROR" | "TIMED_OUT" | "CANCELLED" | "STARTUP_FAILURE"
6091            | "ACTION_REQUIRED" => Verdict::Fail,
6092            "PENDING" | "EXPECTED" | "QUEUED" | "IN_PROGRESS" | "WAITING" | "REQUESTED" => {
6093                Verdict::Pending
6094            }
6095            _ => Verdict::Unknown,
6096        }
6097    }
6098}
6099
6100#[derive(Debug, Deserialize)]
6101struct GhAuthor {
6102    #[serde(default)]
6103    login: String,
6104}
6105
6106#[derive(Debug, Deserialize)]
6107struct GhReview {
6108    #[serde(default)]
6109    author: GhAuthor,
6110    #[serde(default)]
6111    body: String,
6112}
6113
6114#[derive(Debug, Deserialize)]
6115struct GhComment {
6116    #[serde(default)]
6117    author: GhAuthor,
6118    #[serde(default)]
6119    body: String,
6120}
6121
6122#[derive(Debug, Deserialize)]
6123struct GhUser {
6124    #[serde(default)]
6125    login: String,
6126}
6127
6128#[derive(Debug, Deserialize)]
6129struct GhInline {
6130    #[serde(default)]
6131    user: GhUser,
6132    #[serde(default)]
6133    path: Option<String>,
6134    #[serde(default)]
6135    line: Option<u64>,
6136    #[serde(default)]
6137    body: String,
6138}
6139
6140impl Default for GhAuthor {
6141    fn default() -> Self {
6142        Self {
6143            login: "(unknown)".to_owned(),
6144        }
6145    }
6146}
6147
6148impl Default for GhUser {
6149    fn default() -> Self {
6150        Self {
6151            login: "(unknown)".to_owned(),
6152        }
6153    }
6154}
6155
6156#[cfg(test)]
6157pub(crate) mod tests {
6158    use super::*;
6159    use crate::run::{Candidate, ReviewRecord, ReviewRound, Tally};
6160
6161    fn head_json(head: &str, base: &str, state: &str, cross: bool) -> String {
6162        format!(
6163            r#"{{"headRefName":"{head}","headRefOid":"aaa","baseRefName":"{base}","state":"{state}","isCrossRepository":{cross}}}"#
6164        )
6165    }
6166
6167    #[test]
6168    fn a_pull_request_is_closed_only_when_its_head_is_exactly_the_runs_branch() {
6169        let ok = head_json("magi/27b2/A", "main", "OPEN", false);
6170        assert_eq!(
6171            closable(&ok, "magi/27b2/A", "main", &["aaa".to_owned()]),
6172            Ok(())
6173        );
6174        for (json, why) in [
6175            (head_json("magi/27b2/B", "main", "OPEN", false), "head"),
6176            (head_json("magi/27b2/A-2", "main", "OPEN", false), "head"),
6177            (head_json("magi/27b2/A", "main", "OPEN", true), "fork"),
6178            (head_json("magi/27b2/A", "dev", "OPEN", false), "targets"),
6179            (head_json("magi/27b2/A", "main", "MERGED", false), "already"),
6180            (head_json("magi/27b2/A", "main", "CLOSED", false), "already"),
6181        ] {
6182            let err = closable(&json, "magi/27b2/A", "main", &["aaa".to_owned()])
6183                .unwrap_err()
6184                .why;
6185            assert!(err.contains(why), "{json}: {err}");
6186        }
6187        // A head that moved on past what was verified is left alone.
6188        let moved = head_json("magi/27b2/A", "main", "OPEN", false);
6189        let err = closable(&moved, "magi/27b2/A", "main", &["bbb".to_owned()]).unwrap_err();
6190        assert!(err.retry && err.why.contains("not a commit"), "{err:?}");
6191        assert!(
6192            !closable(
6193                &head_json("x", "main", "OPEN", false),
6194                "magi/27b2/A",
6195                "main",
6196                &[]
6197            )
6198            .unwrap_err()
6199            .retry
6200        );
6201        assert!(is_forge_url("https://github.com/o/r.git"));
6202        assert!(is_forge_url("git@github.com:o/r.git"));
6203        assert!(!is_forge_url("/tmp/origin.git"));
6204        assert!(!is_forge_url("C:\\work\\origin.git"));
6205        assert!(!is_forge_url("file:///tmp/origin.git"));
6206        assert!(forge_unavailable(
6207            "gh pr list failed: none of the git remotes configured for this repository point to a known GitHub host."
6208        ));
6209        assert!(!forge_unavailable(
6210            "gh pr list failed: error connecting to api.github.com"
6211        ));
6212        assert!(closable("not json", "magi/27b2/A", "main", &[]).is_err());
6213        // A record that does not say whether it is a fork is not trusted.
6214        assert!(
6215            closable(
6216                r#"{"headRefName":"b","headRefOid":"aaa","baseRefName":"main","state":"OPEN"}"#,
6217                "b",
6218                "main",
6219                &["aaa".to_owned()]
6220            )
6221            .is_err()
6222        );
6223    }
6224
6225    #[test]
6226    fn the_close_comment_names_the_commit_on_the_base() {
6227        let e = crate::already::Evidence {
6228            proof: crate::already::Proof::PatchId,
6229            tip: "1234567890".to_owned(),
6230            commits: vec!["0e368de0000".to_owned()],
6231        };
6232        let c = superseded_comment("main", &e);
6233        assert!(c.contains("0e368de") && c.contains("`main`"), "{c}");
6234    }
6235
6236    /// Real `gh pr view` output for the open pull request #10 (Renovate's apm bump), trimmed to four checks and its one comment. Every check passed or was skipped by the review workflow, and the only comment is CodeRabbit's trigger notice.
6237    const GREEN_OPEN: &str = r####"{
6238  "url": "https://github.com/yukimemi/magi/pull/10",
6239  "number": 10,
6240  "state": "OPEN",
6241  "mergeStateStatus": "CLEAN",
6242  "statusCheckRollup": [
6243    {
6244      "__typename": "CheckRun",
6245      "conclusion": "SKIPPED",
6246      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33356278334/job/99378963755",
6247      "name": "review",
6248      "status": "COMPLETED",
6249      "workflowName": "claude-review"
6250    },
6251    {
6252      "__typename": "CheckRun",
6253      "conclusion": "SUCCESS",
6254      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33356278338/job/99378963144",
6255      "name": "check (ubuntu-latest)",
6256      "status": "COMPLETED",
6257      "workflowName": "CI"
6258    },
6259    {
6260      "__typename": "CheckRun",
6261      "conclusion": "SUCCESS",
6262      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33356278338/job/99378963095",
6263      "name": "rustfmt",
6264      "status": "COMPLETED",
6265      "workflowName": "CI"
6266    },
6267    {
6268      "__typename": "StatusContext",
6269      "context": "CodeRabbit",
6270      "state": "SUCCESS",
6271      "targetUrl": ""
6272    }
6273  ],
6274  "reviews": [],
6275  "comments": [
6276    {
6277      "author": {
6278        "login": "coderabbitai"
6279      },
6280      "authorAssociation": "NONE",
6281      "body": "<!-- This is an auto-generated comment: summarize by coderabbit.ai -->\n<!-- This is an auto-generated comment: skip review by coderabbit.ai -->\n\n> [!IMPORTANT]\n> - [ ] <!-- {\"checkboxId\":\"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe\"} --> 🔍 Trigger review\n> \n> This repository does not receive automatic reviews because it has fewer than 10 stars.\n> \n> <details>\n> <summary>⚙️ Run configuration</summary>\n> \n> **Configuration used**: defaults\n> \n> **Review profile**: CHILL\n> \n> **Plan**: Pro Plus\n> \n> **Run ID**: `78e70bf3-c5a0-4269-a96c-2afb2dba7eff`\n> \n> </details>\n\n<!-- end of auto-generated comment: skip review by coderabbit.ai -->\n\n<!-- tips_start -->\n\n---\n\nThanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=yukimemi/magi&utm_content=10)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.\n\n<details>\n<summary>❤️ Share</summary>\n\n- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%2"
6282    }
6283  ]
6284}"####;
6285
6286    /// Real output for the open pull request #9 (the daily kata-apply), whose `editorconfig` check failed while everything else passed.
6287    const RED_OPEN: &str = r####"{
6288  "url": "https://github.com/yukimemi/magi/pull/9",
6289  "number": 9,
6290  "state": "OPEN",
6291  "mergeStateStatus": "UNSTABLE",
6292  "statusCheckRollup": [
6293    {
6294      "__typename": "CheckRun",
6295      "conclusion": "SUCCESS",
6296      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323744",
6297      "name": "check (ubuntu-latest)",
6298      "status": "COMPLETED",
6299      "workflowName": "CI"
6300    },
6301    {
6302      "__typename": "CheckRun",
6303      "conclusion": "SUCCESS",
6304      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323811",
6305      "name": "rustfmt",
6306      "status": "COMPLETED",
6307      "workflowName": "CI"
6308    },
6309    {
6310      "__typename": "CheckRun",
6311      "conclusion": "FAILURE",
6312      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323572",
6313      "name": "editorconfig",
6314      "status": "COMPLETED",
6315      "workflowName": "CI"
6316    },
6317    {
6318      "__typename": "StatusContext",
6319      "context": "CodeRabbit",
6320      "state": "SUCCESS",
6321      "targetUrl": ""
6322    }
6323  ],
6324  "reviews": [],
6325  "comments": [
6326    {
6327      "author": {
6328        "login": "coderabbitai"
6329      },
6330      "authorAssociation": "NONE",
6331      "body": "<!-- This is an auto-generated comment: summarize by coderabbit.ai -->\n<!-- This is an auto-generated comment: skip review by coderabbit.ai -->\n\n> [!IMPORTANT]\n> - [ ] <!-- {\"checkboxId\":\"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe\"} --> 🔍 Trigger review\n> \n> This repository does not receive automatic reviews because it has fewer than 10 stars.\n> \n> <details>\n> <summary>⚙️ Run configuration</summary>\n> \n> **Configuration used**: defaults\n> \n> **Review profile**: CHILL\n> \n> **Plan**: Team\n> \n> **Run ID**: `91e0dc24-6040-4c3d-92c6-f7d2b542523d`\n> \n> </details>\n\n<!-- end of auto-generated comment: skip review by coderabbit.ai -->\n\n<!-- tips_start -->\n\n---\n\nThanks for using [CodeRabbit](https://coderab"
6332    }
6333  ]
6334}"####;
6335
6336    /// Pull request #9's real payload with its `editorconfig` check rewound to the `IN_PROGRESS` / `conclusion: null` pair `gh` reports while a job is still in flight.
6337    const PENDING_OPEN: &str = r####"{
6338  "url": "https://github.com/yukimemi/magi/pull/9",
6339  "number": 9,
6340  "state": "OPEN",
6341  "statusCheckRollup": [
6342    {
6343      "__typename": "CheckRun",
6344      "conclusion": "SUCCESS",
6345      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323744",
6346      "name": "check (ubuntu-latest)",
6347      "status": "COMPLETED",
6348      "workflowName": "CI"
6349    },
6350    {
6351      "__typename": "CheckRun",
6352      "conclusion": "SUCCESS",
6353      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323811",
6354      "name": "rustfmt",
6355      "status": "COMPLETED",
6356      "workflowName": "CI"
6357    },
6358    {
6359      "__typename": "CheckRun",
6360      "conclusion": null,
6361      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323572",
6362      "name": "editorconfig",
6363      "status": "IN_PROGRESS",
6364      "workflowName": "CI"
6365    },
6366    {
6367      "__typename": "StatusContext",
6368      "context": "CodeRabbit",
6369      "state": "SUCCESS",
6370      "targetUrl": ""
6371    }
6372  ],
6373  "reviews": [],
6374  "comments": []
6375}"####;
6376
6377    /// Real output for pull request #16 after it was merged - the shape landing sees when a person merged underneath it.
6378    const MERGED: &str = r####"{
6379  "url": "https://github.com/yukimemi/magi/pull/16",
6380  "number": 16,
6381  "state": "MERGED",
6382  "statusCheckRollup": [
6383    {
6384      "__typename": "CheckRun",
6385      "conclusion": "SUCCESS",
6386      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33636587933/job/100268878095",
6387      "name": "check (ubuntu-latest)",
6388      "status": "COMPLETED",
6389      "workflowName": "CI"
6390    },
6391    {
6392      "__typename": "CheckRun",
6393      "conclusion": "SUCCESS",
6394      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33636587918/job/100268876427",
6395      "name": "review",
6396      "status": "COMPLETED",
6397      "workflowName": "claude-review"
6398    }
6399  ],
6400  "reviews": [],
6401  "comments": []
6402}"####;
6403
6404    /// Pull request #12's real payload - a green pull request carrying CodeRabbit's walkthrough and a Claude review that found a real bug - rewound to the `OPEN` state it was in when that review was posted.
6405    const REVIEWED_OPEN: &str = r####"{
6406  "url": "https://github.com/yukimemi/magi/pull/12",
6407  "number": 12,
6408  "state": "OPEN",
6409  "statusCheckRollup": [
6410    {
6411      "__typename": "CheckRun",
6412      "conclusion": "SUCCESS",
6413      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33571212506/job/100065355258",
6414      "name": "check (ubuntu-latest)",
6415      "status": "COMPLETED",
6416      "workflowName": "CI"
6417    },
6418    {
6419      "__typename": "CheckRun",
6420      "conclusion": "SUCCESS",
6421      "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33571212566/job/100065355810",
6422      "name": "review",
6423      "status": "COMPLETED",
6424      "workflowName": "claude-review"
6425    }
6426  ],
6427  "reviews": [
6428    {
6429      "author": {
6430        "login": "claude"
6431      },
6432      "state": "COMMENTED",
6433      "body": ""
6434    }
6435  ],
6436  "comments": [
6437    {
6438      "author": {
6439        "login": "coderabbitai"
6440      },
6441      "authorAssociation": "NONE",
6442      "body": "<!-- This is an auto-generated comment: summarize by coderabbit.ai -->\n<!-- This is an auto-generated comment: skip review by coderabbit.ai -->\n\n> [!IMPORTANT]\n> - [ ] <!-- {\"checkboxId\":\"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe\"} --> 🔍 Trigger review\n> \n> This repository does not receive automatic reviews because it has fewer than 10 stars.\n> \n> <details>\n> <summary>⚙️ Run configuration</summary>\n> \n> **Configuration used**: defaults\n> \n> **Review profile**: CHILL\n> \n> **Plan**: Team\n> \n> **Run ID**: `72058bf3-b7df-41d9-8e4d-a06a31be4a26`\n> \n> </details>\n\n<!-- end of auto-generated comment: skip review by coderabbit.ai -->\n\n<!-- tips_start -->\n\n---\n\nThanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=yukimemi/magi&utm_content=12)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.\n\n<details>\n<summa"
6443    },
6444    {
6445      "author": {
6446        "login": "claude"
6447      },
6448      "authorAssociation": "NONE",
6449      "body": "**Claude finished @yukimemi's task in 3m 52s** —— [View job](https://github.com/yukimemi/magi/actions/runs/33571212566)\n\n---\n### Review: `magi review <branch>` — cheap-half-only graph\n\nRead through `src/graph.rs`, `src/main.rs`, `src/prompt.rs`, and the new/edited tests, and traced the claimed degeneration (`prep` short-circuits on a non-empty candidate list, `implement` skips because `commits != 0`, `judge`/`vote` skip on `viable().len() == 1`, `tally` skips because it's pre-set, `fold_losers` has no losers) against the actual code — it holds up. CI (`cargo make check`) is green on this PR.\n\n**Correctness**\n\n- One real bug, flagged inline on `src/graph.rs:1255`: the fixer-agent fallback (`self.roles.implementers[winner.index].clone()`) is unreachable in the normal graph (a real candidate's `winner.agent` always resolves via `config.agent(...)`), but a review-only run's `winner.agent` is always the `\"(existing branch)\"` sentinel, so this fallback now runs on *every* review-only fix that has no dedicated `[roles] fixer`. `graph.candidates` has no lower-bound validation, so a `magi.toml` tuned for review-only use (`candidates = 0`, plausible given this PR's own cost rationale) would panic with an out-of-bounds index the first time a"
6450    }
6451  ]
6452}"####;
6453
6454    /// Real `gh api repos/{owner}/{repo}/pulls/12/comments` output: one inline finding with its file and line.
6455    const INLINE: &str = r####"[
6456  {
6457    "user": {
6458      "login": "claude[bot]"
6459    },
6460    "path": "src/graph.rs",
6461    "line": 231,
6462    "body": "Minor edge case: unlike `implement()` (which sets `c.empty = commits == 0 || patch.trim().is_empty()`, `src/graph.rs:472`), the seeded review-only candidate always sets `empty: false` once `commits > 0` is confirmed, without checking whether the diff itself is actually empty (e.g. a commit immediately followed by a revert nets zero file changes). Such a branch would pass `Runner::review`'s validation and proceed into a review round with an empty patch, where `implement()`'s equivalent path would"
6463  }
6464]"####;
6465
6466    /// CodeRabbit's real trigger notice: a checkbox, a `<details>` block, and its own "skip review" marker.
6467    const CODERABBIT_TRIGGER: &str = r####"<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
6468<!-- This is an auto-generated comment: skip review by coderabbit.ai -->
6469
6470> [!IMPORTANT]
6471> - [ ] <!-- {"checkboxId":"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe"} --> 🔍 Trigger review
6472> 
6473> This repository does not receive automatic reviews because it has fewer than 10 stars.
6474> 
6475> <details>
6476> <summary>⚙️ Run configuration</summary>
6477> 
6478> **Configuration used**: defaults
6479> 
6480> **Review profile**: CHILL
6481> 
6482> **Plan**: Team
6483> 
6484> **Run ID**: `c1e2a68f-87fc-4b35-9ec4-e75c7854966a`
6485> 
6486> </details>
6487
6488<!-- end of auto-generated comment: skip review by coderabbit.ai -->
6489
6490<!-- tips_start -->
6491
6492---
6493
6494Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=yukimemi/magi&utm_content=16)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
6495
6496<details>
6497<summary>❤️ Share</summary>
6498
6499- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20off"####;
6500
6501    /// The Claude review job's real comment while it is still working: a heading and a task list, and nothing that asks for a change.
6502    const CLAUDE_CHECKLIST: &str = r####"**Claude finished @yukimemi's task in 4m 14s** —— [View job](https://github.com/yukimemi/magi/actions/runs/33636587918)
6503
6504---
6505### Reviewing PR #16
6506
6507- [x] Read AGENTS.md conventions
6508- [x] Review `src/daemon.rs` changes
6509- [x] Review `src/main.rs` changes (new `doctor` reporting)
6510- [x] Review `src/web.rs` changes (reuse of unreadable-run count)
6511- [x] Check test coverage for new behavior
6512- [x] Run verification commands (blocked — see note)
6513- [x] Post findings"####;
6514
6515    /// The same job's real comment on pull request #12 once it had something to say.
6516    const CLAUDE_FINDING: &str = r####"**Claude finished @yukimemi's task in 3m 52s** —— [View job](https://github.com/yukimemi/magi/actions/runs/33571212566)
6517
6518---
6519### Review: `magi review <branch>` — cheap-half-only graph
6520
6521Read through `src/graph.rs`, `src/main.rs`, `src/prompt.rs`, and the new/edited tests, and traced the claimed degeneration (`prep` short-circuits on a non-empty candidate list, `implement` skips because `commits != 0`, `judge`/`vote` skip on `viable().len() == 1`, `tally` skips because it's pre-set, `fold_losers` has no losers) against the actual code — it holds up. CI (`cargo make check`) is green on this PR.
6522
6523**Correctness**
6524
6525- One real bug, flagged inline on `src/graph.rs:1255`: the fixer-agent fallback (`self.roles.implementers[winner.index].clone()`) is unreachable in the normal graph (a real candidate's `winner.agent` always resolves via `config.agent(...)`), but a review-only run's `winner.agent` is always the `"(existing branch)"` sentinel, so this fallback now runs on *every* review-only fix that has no dedicated `[roles] fixer`. `graph.candidates` has no lower-bound validation, so a `magi.toml` tuned for review-only use (`candidates = 0`, plausible given this PR's own cost rationale) would panic with an out-of-bounds index the first time a"####;
6526
6527    fn pr(checks: Checks, failing: &[&str], comments: usize) -> PrState {
6528        PrState {
6529            url: "https://github.com/yukimemi/magi/pull/16".to_owned(),
6530            number: 16,
6531            state: PrLifecycle::Open,
6532            checks,
6533            // These tests are about red-means-fix, so a red here is one the
6534            // forge gates on. Without saying so they would assert the new
6535            // "merge past a check nobody requires" path by accident.
6536            blocking: if matches!(checks, Checks::Red) {
6537                Blocking::Yes
6538            } else {
6539                Blocking::No
6540            },
6541            failing: failing.iter().map(|s| (*s).to_owned()).collect(),
6542            review_comments: (0..comments)
6543                .map(|i| ReviewComment {
6544                    author: "coderabbitai".to_owned(),
6545                    path: Some("src/graph.rs".to_owned()),
6546                    line: Some(231),
6547                    body: format!("finding {i}"),
6548                })
6549                .collect(),
6550        }
6551    }
6552
6553    #[test]
6554    fn expected_ci_is_exactly_decide_and_absent_ci_never_waits_for_checks() {
6555        use CiExpectation::{Absent, Expected};
6556        for checks in [Checks::Pending, Checks::Unknown, Checks::Green, Checks::Red] {
6557            let p = pr(checks, &["x"], 0);
6558            for waited in [Duration::ZERO, CHECKS_GRACE] {
6559                assert_eq!(
6560                    decide_with(&p, 0, 4, waited, Expected),
6561                    decide(&p, 0, 4, waited)
6562                );
6563            }
6564        }
6565        // Nothing will ever report: no wait, no give-up, no fix round.
6566        for checks in [Checks::Pending, Checks::Unknown, Checks::Red] {
6567            let p = pr(checks, &["x"], 0);
6568            assert_eq!(decide_with(&p, 0, 4, Duration::ZERO, Absent), Step::Merge);
6569            assert_eq!(decide_with(&p, 4, 4, CHECKS_GRACE, Absent), Step::Merge);
6570        }
6571        // Third-party statuses never change the review decision, even at budget.
6572        for comments in [0, 1] {
6573            for round in [0, 4] {
6574                let absent = decide_with(
6575                    &pr(Checks::Unknown, &[], comments),
6576                    round,
6577                    4,
6578                    CHECKS_GRACE,
6579                    Absent,
6580                );
6581                for checks in [Checks::Green, Checks::Red, Checks::Pending] {
6582                    assert_eq!(
6583                        decide_with(
6584                            &pr(checks, &["third-party"], comments),
6585                            round,
6586                            4,
6587                            Duration::ZERO,
6588                            Absent
6589                        ),
6590                        absent
6591                    );
6592                }
6593                if comments != 0 {
6594                    assert!(matches!(absent, Step::Fix { .. } | Step::GiveUp { .. }));
6595                    assert_eq!(matches!(absent, Step::GiveUp { .. }), round == 4);
6596                }
6597            }
6598        }
6599        let empty = pr(Checks::Unknown, &[], 0);
6600        for waited in [Duration::ZERO, CHECKS_GRACE + POLL] {
6601            assert_eq!(decide_with(&empty, 0, 4, waited, Absent), Step::Merge);
6602        }
6603        assert_eq!(
6604            decide_with(&empty, 0, 4, Duration::ZERO, Expected),
6605            Step::Wait
6606        );
6607        assert_eq!(
6608            decide_with(&empty, 0, 4, CHECKS_GRACE, Expected),
6609            Step::GiveUp { reason: "no check status is readable on the pull request after 3 minute(s); refusing to merge on a guess".to_owned() }
6610        );
6611        // A conflict is not a check: it still wants the rebase.
6612        let mut p = pr(Checks::Unknown, &[], 0);
6613        p.blocking = Blocking::Conflict;
6614        assert_eq!(decide_with(&p, 0, 4, Duration::ZERO, Absent), Step::Rebase);
6615        // And a pull request that left our hands is still done.
6616        p.state = PrLifecycle::Merged;
6617        assert_eq!(
6618            decide_with(&p, 0, 4, Duration::ZERO, Absent),
6619            Step::Done { merged: true }
6620        );
6621    }
6622
6623    #[test]
6624    fn a_green_pull_request_with_nothing_outstanding_parses_as_ready_to_merge() {
6625        let state = parse_pr(GREEN_OPEN).expect("green fixture parses");
6626        assert_eq!(state.number, 10);
6627        assert_eq!(state.state, PrLifecycle::Open);
6628        assert_eq!(state.checks, Checks::Green);
6629        assert!(state.failing.is_empty());
6630        assert!(
6631            state.review_comments.is_empty(),
6632            "the only comment is CodeRabbit's trigger notice: {:?}",
6633            state.review_comments
6634        );
6635        assert_eq!(decide(&state, 0, 4, Duration::ZERO), Step::Merge);
6636    }
6637
6638    #[test]
6639    fn a_failing_check_parses_as_red_and_is_named() {
6640        let state = parse_pr(RED_OPEN).expect("red fixture parses");
6641        assert_eq!(state.checks, Checks::Red);
6642        assert_eq!(state.failing, vec!["editorconfig".to_owned()]);
6643        // The captured payload says `UNSTABLE` - mergeable, with a check
6644        // nobody requires red - which is exactly the shape that had to be
6645        // merged by hand. Asserted separately, in
6646        // `a_red_check_nobody_requires_does_not_buy_a_fix_round`. What this
6647        // test is about is that a red check is *named*, so the reason a fixer
6648        // is handed says which one; so it asks the blocking question here.
6649        let mut blocking = state.clone();
6650        blocking.blocking = Blocking::Yes;
6651        match decide(&blocking, 0, 4, Duration::ZERO) {
6652            Step::Fix { reason } => {
6653                assert!(reason.contains("editorconfig"), "reason: {reason}");
6654                assert!(reason.contains("failing"), "reason: {reason}");
6655            }
6656            other => panic!("expected a fix round, got {other:?}"),
6657        }
6658    }
6659
6660    #[test]
6661    fn a_check_still_running_parses_as_pending_and_is_waited_for() {
6662        let state = parse_pr(PENDING_OPEN).expect("pending fixture parses");
6663        assert_eq!(state.checks, Checks::Pending);
6664        assert_eq!(decide(&state, 0, 4, Duration::ZERO), Step::Wait);
6665    }
6666
6667    #[test]
6668    fn a_pull_request_merged_underneath_us_is_done_rather_than_a_failure() {
6669        let state = parse_pr(MERGED).expect("merged fixture parses");
6670        assert_eq!(state.state, PrLifecycle::Merged);
6671        assert_eq!(
6672            decide(&state, 0, 4, Duration::ZERO),
6673            Step::Done { merged: true }
6674        );
6675    }
6676
6677    #[test]
6678    fn a_review_that_found_something_is_outstanding_and_holds_the_merge() {
6679        let state = parse_pr(REVIEWED_OPEN).expect("reviewed fixture parses");
6680        assert_eq!(state.checks, Checks::Green);
6681        let authors: Vec<&str> = state
6682            .review_comments
6683            .iter()
6684            .map(|c| c.author.as_str())
6685            .collect();
6686        assert_eq!(
6687            authors,
6688            vec!["claude"],
6689            "CodeRabbit's walkthrough is machinery; Claude's review is a finding"
6690        );
6691        match decide(&state, 0, 4, Duration::ZERO) {
6692            Step::Fix { reason } => assert!(reason.contains("unresolved"), "reason: {reason}"),
6693            other => panic!("expected a fix round, got {other:?}"),
6694        }
6695    }
6696
6697    #[test]
6698    fn inline_review_comments_keep_their_file_and_line() {
6699        let comments = parse_inline_comments(INLINE).expect("inline fixture parses");
6700        assert_eq!(comments.len(), 1);
6701        assert_eq!(comments[0].author, "claude[bot]");
6702        assert_eq!(comments[0].path.as_deref(), Some("src/graph.rs"));
6703        assert_eq!(comments[0].line, Some(231));
6704        assert!(comments[0].body.contains("empty"), "{}", comments[0].body);
6705    }
6706
6707    #[test]
6708    fn a_status_only_bot_comment_does_not_trigger_a_fix_round() {
6709        assert!(
6710            is_noise(CODERABBIT_TRIGGER),
6711            "CodeRabbit's trigger notice declares itself not a review"
6712        );
6713        assert!(
6714            is_noise(CLAUDE_CHECKLIST),
6715            "a progress checklist asks for nothing"
6716        );
6717        assert!(
6718            !is_noise(CLAUDE_FINDING),
6719            "a review that names a bug is input, not noise"
6720        );
6721
6722        let mut clean = pr(Checks::Green, &[], 0);
6723        clean.review_comments.push(ReviewComment {
6724            author: "coderabbitai".to_owned(),
6725            path: None,
6726            line: None,
6727            body: CODERABBIT_TRIGGER.to_owned(),
6728        });
6729        clean.review_comments.retain(|c| !is_noise(&c.body));
6730        assert_eq!(decide(&clean, 0, 4, Duration::ZERO), Step::Merge);
6731
6732        let mut found = pr(Checks::Green, &[], 0);
6733        found.review_comments.push(ReviewComment {
6734            author: "claude".to_owned(),
6735            path: None,
6736            line: None,
6737            body: CLAUDE_FINDING.to_owned(),
6738        });
6739        found.review_comments.retain(|c| !is_noise(&c.body));
6740        assert!(matches!(
6741            decide(&found, 0, 4, Duration::ZERO),
6742            Step::Fix { .. }
6743        ));
6744    }
6745
6746    #[test]
6747    fn the_policy_table_holds_for_every_combination_that_matters() {
6748        let cases: Vec<(&str, PrState, usize, usize, Duration, Step)> = vec![
6749            (
6750                "pending checks are waited for, even on the last round",
6751                pr(Checks::Pending, &[], 0),
6752                4,
6753                4,
6754                Duration::ZERO,
6755                Step::Wait,
6756            ),
6757            (
6758                "red checks are fixed",
6759                pr(Checks::Red, &["editorconfig"], 0),
6760                0,
6761                4,
6762                Duration::ZERO,
6763                Step::Fix {
6764                    reason: "1 check(s) failing: editorconfig".to_owned(),
6765                },
6766            ),
6767            (
6768                "green with comments is fixed, not merged",
6769                pr(Checks::Green, &[], 2),
6770                1,
6771                4,
6772                Duration::ZERO,
6773                Step::Fix {
6774                    reason: "checks are green but 2 review comment(s) are unresolved: coderabbitai"
6775                        .to_owned(),
6776                },
6777            ),
6778            (
6779                "green and clean merges",
6780                pr(Checks::Green, &[], 0),
6781                3,
6782                4,
6783                Duration::ZERO,
6784                Step::Merge,
6785            ),
6786            (
6787                "an unreadable rollup is waited on while the grace lasts",
6788                pr(Checks::Unknown, &[], 0),
6789                0,
6790                4,
6791                Duration::ZERO,
6792                Step::Wait,
6793            ),
6794            (
6795                "an unreadable rollup is never merged once the grace is spent",
6796                pr(Checks::Unknown, &[], 0),
6797                0,
6798                4,
6799                CHECKS_GRACE,
6800                Step::GiveUp {
6801                    reason: "no check status is readable on the pull request after 3 minute(s); \
6802                             refusing to merge on a guess"
6803                        .to_owned(),
6804                },
6805            ),
6806        ];
6807        for (what, state, round, budget, waited, want) in cases {
6808            assert_eq!(decide(&state, round, budget, waited), want, "{what}");
6809        }
6810    }
6811
6812    #[test]
6813    fn the_forge_verdict_survives_the_round_trip_from_gh() {
6814        // Read off `gh pr view --json ...,mergeStateStatus`, because a field
6815        // requested but never parsed is the kind of thing that looks wired up
6816        // and answers `Unsaid` forever.
6817        let green = parse_pr(GREEN_OPEN).expect("parse");
6818        assert_eq!(green.blocking, Blocking::No);
6819        let red = parse_pr(RED_OPEN).expect("parse");
6820        assert_eq!(
6821            red.blocking,
6822            Blocking::No,
6823            "`UNSTABLE` is mergeable: the red check is one nobody requires"
6824        );
6825        assert_eq!(red.checks, Checks::Red, "and it is still reported as red");
6826        // A payload from an older `gh` has no such field at all.
6827        let quiet =
6828            parse_pr(&GREEN_OPEN.replace("\"mergeStateStatus\": \"CLEAN\",", "")).expect("parse");
6829        assert_eq!(quiet.blocking, Blocking::Unsaid);
6830    }
6831
6832    #[test]
6833    fn a_red_check_nobody_requires_does_not_buy_a_fix_round() {
6834        // Pull request 37's only red check was `editorconfig`, failing
6835        // because the action could not fetch its own binary after
6836        // editorconfig-checker v4 renamed its release assets. The repository
6837        // does not require it. magi answered by asking a fixer to repair a
6838        // change that was fine, and the pull request had to be merged by hand.
6839        let mut nonblocking = pr(Checks::Red, &["editorconfig", "coverage"], 0);
6840        nonblocking.blocking = Blocking::No;
6841        assert_eq!(
6842            decide(&nonblocking, 0, 4, Duration::ZERO),
6843            Step::Merge,
6844            "the forge says nothing is in the way, so nothing is"
6845        );
6846
6847        // The same red, gated on: that is a fix round, as before.
6848        let mut blocking = pr(Checks::Red, &["test (ubuntu-latest)"], 0);
6849        blocking.blocking = Blocking::Yes;
6850        assert!(matches!(
6851            decide(&blocking, 0, 4, Duration::ZERO),
6852            Step::Fix { .. }
6853        ));
6854
6855        // A review comment still outranks green-enough: a non-required red
6856        // must not become a way to merge past an unanswered reviewer.
6857        let mut commented = pr(Checks::Red, &["coverage"], 1);
6858        commented.blocking = Blocking::No;
6859        assert!(matches!(
6860            decide(&commented, 0, 4, Duration::ZERO),
6861            Step::Fix { .. }
6862        ));
6863
6864        // And silence from the forge is not consent.
6865        let mut unsaid = pr(Checks::Red, &["coverage"], 0);
6866        unsaid.blocking = Blocking::Unsaid;
6867        assert_eq!(decide(&unsaid, 0, 4, Duration::ZERO), Step::Wait);
6868        assert!(matches!(
6869            decide(&unsaid, 0, 4, CHECKS_GRACE),
6870            Step::Fix { .. }
6871        ));
6872    }
6873
6874    #[test]
6875    fn a_red_merge_is_announced_with_every_failing_check_and_a_green_one_is_not() {
6876        let mut red = pr(Checks::Red, &["test (windows-latest)", "coverage"], 0);
6877        red.blocking = Blocking::No;
6878        assert_eq!(
6879            decide(&red, 0, 4, Duration::ZERO),
6880            Step::Merge,
6881            "announcing must not change the decision"
6882        );
6883        let said = red_merge_summary("yukimemi/magi", &red).expect("red merge is announced");
6884        assert!(said.contains("yukimemi/magi"), "{said}");
6885        assert!(said.contains("#16"), "{said}");
6886        assert!(
6887            said.contains("https://github.com/yukimemi/magi/pull/16"),
6888            "{said}"
6889        );
6890        assert!(
6891            said.contains("test (windows-latest)") && said.contains("coverage"),
6892            "{said}"
6893        );
6894
6895        // `failing` can be left over on a green observation; only `checks` counts.
6896        let green = pr(Checks::Green, &["stale"], 0);
6897        assert_eq!(red_merge_summary("yukimemi/magi", &green), None);
6898    }
6899
6900    #[test]
6901    fn red_merge_notices_omit_empty_check_details() {
6902        for checks in [vec![], vec!["", "  "]] {
6903            let red = pr(Checks::Red, &checks, 0);
6904            let summary = red_merge_summary("owner/repo", &red).unwrap();
6905            let notice = crate::notices::merged_red("run-1", &summary);
6906            assert_eq!(
6907                notice.message,
6908                format!("Merged owner/repo PR #16 with red checks ({})", red.url)
6909            );
6910            assert_eq!(notice.severity, crate::notices::Severity::Warn);
6911        }
6912        let red = pr(Checks::Red, &["", " test ", " "], 0);
6913        let summary = red_merge_summary("owner/repo", &red).unwrap();
6914        assert!(summary.contains("with red checks: test ("), "{summary}");
6915    }
6916
6917    #[test]
6918    fn the_repo_label_comes_from_the_pull_request_url() {
6919        let p = Path::new("/tmp/checkout");
6920        assert_eq!(
6921            repo_label(p, "https://github.com/yukimemi/magi/pull/16"),
6922            "yukimemi/magi"
6923        );
6924        assert_eq!(repo_label(p, "not a url"), "checkout");
6925    }
6926
6927    #[test]
6928    fn a_branch_the_base_moved_under_is_rebased_not_fixed() {
6929        // Pull requests 35 and 37 were both rebased by hand: a competition
6930        // that runs for two hours against a repository merging pull requests
6931        // all day conflicts on the way in, and that is arithmetic rather
6932        // than a defect in the change.
6933        let mut conflicted = pr(Checks::Green, &[], 0);
6934        conflicted.blocking = Blocking::Conflict;
6935        assert_eq!(decide(&conflicted, 0, 4, Duration::ZERO), Step::Rebase);
6936
6937        // Decided before the checks, and even with the rounds spent: every
6938        // check on a branch that cannot land is an answer about a state that
6939        // cannot land, and a conflict is not the change's fault.
6940        let mut red = pr(Checks::Red, &["test (ubuntu-latest)"], 2);
6941        red.blocking = Blocking::Conflict;
6942        assert_eq!(decide(&red, 4, 4, Duration::ZERO), Step::Rebase);
6943
6944        // The lifecycle still wins over everything, conflict included.
6945        let mut merged = pr(Checks::Red, &[], 0);
6946        merged.blocking = Blocking::Conflict;
6947        merged.state = PrLifecycle::Merged;
6948        assert_eq!(
6949            decide(&merged, 0, 4, Duration::ZERO),
6950            Step::Done { merged: true }
6951        );
6952    }
6953
6954    #[test]
6955    fn the_forge_verdict_is_read_off_merge_state_status() {
6956        // The spellings that mean "mergeable". `UNSTABLE` is the one that
6957        // matters: mergeable, with a non-required check red or still running.
6958        for ok in ["CLEAN", "UNSTABLE", "unstable", "HAS_HOOKS"] {
6959            assert_eq!(Blocking::of(ok), Blocking::No, "{ok}");
6960            assert!(!Blocking::of(ok).stops_a_merge(), "{ok}");
6961        }
6962        assert_eq!(Blocking::of("DIRTY"), Blocking::Conflict);
6963        assert_eq!(Blocking::of("BLOCKED"), Blocking::Yes);
6964        assert_eq!(Blocking::of("BEHIND"), Blocking::Yes);
6965        // An older `gh`, or a token without the scope, says nothing - and
6966        // refusing to guess is the rule everywhere else in this module.
6967        for quiet in ["", "UNKNOWN"] {
6968            assert_eq!(Blocking::of(quiet), Blocking::Unsaid);
6969            assert!(Blocking::of(quiet).stops_a_merge());
6970        }
6971    }
6972
6973    #[test]
6974    fn a_merge_command_that_failed_after_merging_is_still_a_merge() {
6975        let argv = merge_argv(28, "fix: retry uploads on transient network errors");
6976        // The exact stderr from run ec12, in a jj-colocated repository.
6977        let jj = "could not determine current branch: failed to run git: not on any branch";
6978
6979        let landed = merged_after_all(&argv, jj, Some(PrLifecycle::Merged))
6980            .expect("the forge says merged, so it merged");
6981        assert!(landed.ok);
6982        assert!(
6983            landed.detail.contains("but the pull request is merged"),
6984            "the record must not read as a clean success: {}",
6985            landed.detail
6986        );
6987        assert!(
6988            landed.detail.contains("not on any branch"),
6989            "and it must keep what the command actually said: {}",
6990            landed.detail
6991        );
6992
6993        // A pull request still open means the merge really failed.
6994        assert!(merged_after_all(&argv, jj, Some(PrLifecycle::Open)).is_none());
6995        assert!(merged_after_all(&argv, jj, Some(PrLifecycle::Closed)).is_none());
6996        // And an unreadable answer is not evidence of success.
6997        assert!(merged_after_all(&argv, jj, None).is_none());
6998    }
6999
7000    #[test]
7001    fn a_pull_request_closed_underneath_us_is_done_and_not_merged() {
7002        let mut state = pr(Checks::Red, &["editorconfig"], 3);
7003        state.state = PrLifecycle::Closed;
7004        assert_eq!(
7005            decide(&state, 0, 4, Duration::ZERO),
7006            Step::Done { merged: false },
7007            "a human closing the pull request ends the loop, whatever CI says"
7008        );
7009    }
7010
7011    #[test]
7012    fn the_last_round_gives_up_with_a_reason_naming_what_is_still_failing() {
7013        let red = decide(
7014            &pr(Checks::Red, &["editorconfig", "test (macos)"], 0),
7015            4,
7016            4,
7017            Duration::ZERO,
7018        );
7019        match red {
7020            Step::GiveUp { reason } => {
7021                assert!(reason.contains("editorconfig"), "reason: {reason}");
7022                assert!(reason.contains("test (macos)"), "reason: {reason}");
7023                assert!(reason.contains("4 fix round(s)"), "reason: {reason}");
7024            }
7025            other => panic!("expected a give-up, got {other:?}"),
7026        }
7027
7028        let commented = decide(&pr(Checks::Green, &[], 1), 2, 2, Duration::ZERO);
7029        match commented {
7030            Step::GiveUp { reason } => {
7031                assert!(reason.contains("unresolved"), "reason: {reason}");
7032                assert!(reason.contains("2 fix round(s)"), "reason: {reason}");
7033            }
7034            other => panic!("expected a give-up, got {other:?}"),
7035        }
7036    }
7037
7038    #[test]
7039    fn the_merge_command_squashes_deletes_the_branch_and_sets_its_own_subject() {
7040        let candidate_commit = "magi: candidate A (uncommitted work)";
7041        let subject = merge_subject(candidate_commit, "add retries to the uploader");
7042        let argv = merge_argv(16, &subject);
7043
7044        assert!(argv.contains(&"--squash".to_owned()));
7045        assert!(argv.contains(&"--delete-branch".to_owned()));
7046        assert!(argv.contains(&"--subject".to_owned()));
7047        assert_eq!(
7048            argv.last().map(String::as_str),
7049            Some("add retries to the uploader"),
7050            "the subject must not be the candidate commit message"
7051        );
7052        assert_ne!(subject, candidate_commit);
7053    }
7054
7055    #[test]
7056    fn a_real_pull_request_title_is_used_as_the_squash_subject_verbatim() {
7057        assert_eq!(
7058            merge_subject("feat: a queue, an unattended loop, and a phone UI", "task"),
7059            "feat: a queue, an unattended loop, and a phone UI"
7060        );
7061        assert_eq!(
7062            merge_subject("", "# port the retry logic\n\ndetails"),
7063            "port the retry logic",
7064            "an empty title falls back to the task's first line, heading marks stripped"
7065        );
7066    }
7067
7068    #[test]
7069    fn a_failing_checks_details_url_yields_the_job_to_read_logs_from() {
7070        let url = "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323572";
7071        assert_eq!(job_of(url).as_deref(), Some("100114323572"));
7072        assert_eq!(run_of(url).as_deref(), Some("33587406996"));
7073        assert_eq!(job_of("https://coderabbit.ai/status"), None);
7074        assert_eq!(run_of(""), None);
7075    }
7076
7077    #[test]
7078    fn magis_own_stop_comment_is_never_read_back_as_a_finding() {
7079        let mut out = Vec::new();
7080        push_if_outstanding(
7081            &mut out,
7082            ReviewComment {
7083                author: "yukimemi".to_owned(),
7084                path: None,
7085                line: None,
7086                body: format!("{MARKER}\nmagi stopped landing this pull request: 1 check failing"),
7087            },
7088        );
7089        assert!(out.is_empty());
7090    }
7091
7092    /// A run with no tally, so [`RunState::winner`] is `None` and the panel
7093    /// falls back to the repository - which keeps these tests free of a
7094    /// worktree, a `git` invocation and a network.
7095    fn run_state() -> RunState {
7096        let mut state = RunState::new(
7097            std::path::PathBuf::from("/repo/magi"),
7098            "main".to_owned(),
7099            "abcdef1234".to_owned(),
7100            "add retries to the uploader".to_owned(),
7101            crate::config::Config::default(),
7102        );
7103        // Tests run in parallel against one persistent home and the ids
7104        // `RunState::new` draws from the clock can repeat, so two tests would
7105        // share a run's questions. The home also outlives the process, so the
7106        // counter alone would reuse an earlier run's ids.
7107        static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
7108        let nanos = std::time::SystemTime::now()
7109            .duration_since(std::time::UNIX_EPOCH)
7110            .map_or(0, |d| d.subsec_nanos() % 1_000_000);
7111        state.id = format!(
7112            "20261004-{nanos:06}-{:04x}",
7113            NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
7114        );
7115        state
7116    }
7117
7118    fn green_pr() -> PrState {
7119        PrState {
7120            url: "https://github.com/yukimemi/magi/pull/42".to_owned(),
7121            number: 42,
7122            state: PrLifecycle::Open,
7123            checks: Checks::Green,
7124            // The forge sees nothing in the way unless a test says otherwise.
7125            blocking: Blocking::No,
7126            failing: Vec::new(),
7127            review_comments: vec![ReviewComment {
7128                author: "coderabbitai".to_owned(),
7129                path: Some("src/land.rs".to_owned()),
7130                line: Some(212),
7131                body: "this branch never checks the exit code".to_owned(),
7132            }],
7133        }
7134    }
7135
7136    #[test]
7137    fn land_fixer_defers_review_and_verification_without_a_cache() {
7138        let state = run_state();
7139        let p = fix_prompt(&state, &green_pr(), 1, 2, "red", "");
7140        assert!(p.contains(crate::prompt::child_run_ownership()), "{p}");
7141        assert!(p.contains("Commit in this worktree"), "{p}");
7142    }
7143
7144    #[test]
7145    fn github_facing_land_text_is_english_whatever_the_language() {
7146        let mut state = run_state();
7147        state.config.graph.language = "ja".to_owned();
7148        let comment = stop_comment(&state.id, "checks are still red");
7149        assert!(crate::github_text::check("", &comment).is_empty());
7150        assert!(comment.is_ascii(), "{comment}");
7151        assert!(comment.starts_with(MARKER));
7152
7153        let p = fix_prompt(&state, &green_pr(), 1, 2, "red", "");
7154        let ja_at = p.find("Write all prose in ja").unwrap();
7155        let rule_at = p.find(crate::prompt::GITHUB_ENGLISH_HEADING).unwrap();
7156        assert!(ja_at < rule_at, "{p}");
7157        assert!(p.contains("stays in Japanese"), "{p}");
7158
7159        state.config.graph.language = "en".to_owned();
7160        let p = fix_prompt(&state, &green_pr(), 1, 2, "red", "");
7161        assert!(p.contains(crate::prompt::GITHUB_ENGLISH_HEADING), "{p}");
7162        assert!(!p.contains("does not apply"), "{p}");
7163    }
7164
7165    const NUMSTAT: &str = "12\t3\tsrc/land.rs\n40\t1\tsrc/web.rs\n-\t-\tassets/logo.png";
7166
7167    fn panel() -> String {
7168        approval_panel(
7169            &run_state(),
7170            &green_pr(),
7171            NUMSTAT,
7172            "diff --git a/src/land.rs b/src/land.rs\n@@ -1,2 +1,2 @@\n-old line\n+new line\n context",
7173            &[
7174                "land: ask before merging".to_owned(),
7175                "land: colour the diff".to_owned(),
7176            ],
7177            "feat: merge approval from the phone",
7178        )
7179    }
7180
7181    #[test]
7182    fn the_approval_panel_carries_the_whole_case_for_the_merge() {
7183        let html = panel();
7184        for needle in [
7185            "42",
7186            "main",
7187            "src/land.rs",
7188            "src/web.rs",
7189            "assets/logo.png",
7190            "feat: merge approval from the phone",
7191            "land: ask before merging",
7192            "land: colour the diff",
7193            "coderabbitai",
7194            "this branch never checks the exit code",
7195            "green",
7196        ] {
7197            assert!(html.contains(needle), "the panel must state `{needle}`");
7198        }
7199    }
7200
7201    async fn landing_git_fixture() -> (tempfile::TempDir, RunState, String) {
7202        crate::run::pin_test_home();
7203        let tmp = tempfile::tempdir().unwrap();
7204        let repo = tmp.path().join("repo");
7205        let remote = tmp.path().join("remote.git");
7206        std::fs::create_dir(&repo).unwrap();
7207        git::git(tmp.path(), &["init", "--bare", remote.to_str().unwrap()])
7208            .await
7209            .unwrap();
7210        git::git(&repo, &["init", "-b", "main"]).await.unwrap();
7211        git::git(&repo, &["config", "user.name", "Test"])
7212            .await
7213            .unwrap();
7214        git::git(&repo, &["config", "user.email", "test@example.test"])
7215            .await
7216            .unwrap();
7217        std::fs::write(repo.join("base"), "base").unwrap();
7218        git::git(&repo, &["add", "."]).await.unwrap();
7219        git::git(&repo, &["commit", "-m", "base"]).await.unwrap();
7220        git::git(
7221            &repo,
7222            &["remote", "add", "origin", remote.to_str().unwrap()],
7223        )
7224        .await
7225        .unwrap();
7226        git::git(&repo, &["push", "origin", "main"]).await.unwrap();
7227        git::git(&repo, &["checkout", "-b", "winner"])
7228            .await
7229            .unwrap();
7230        std::fs::write(repo.join("change"), "change").unwrap();
7231        git::git(&repo, &["add", "."]).await.unwrap();
7232        git::git(&repo, &["commit", "-m", "change"]).await.unwrap();
7233        let head = git::rev_parse(&repo, "HEAD").await.unwrap();
7234        git::git(&repo, &["push", "origin", "HEAD:refs/heads/task-head"])
7235            .await
7236            .unwrap();
7237        let mut state = run_state();
7238        state.repo = repo.clone();
7239        state.base_commit = git::rev_parse(&repo, "origin/main").await.unwrap();
7240        state.config.graph.land = true;
7241        state.config.graph.land_approval = true;
7242        state.pr_head = Some("task-head".into());
7243        state.pr_pushed_head = Some(head.clone());
7244        state.candidates = vec![winning_candidate("change")];
7245        state.candidates[0].worktree = repo;
7246        state.candidates[0].branch = "winner".into();
7247        state.tally = Some(uncontested_tally());
7248        state.gate_ran = true;
7249        state.gate_head = Some(head.clone());
7250        state.land_sync_pending = true;
7251        state.save().unwrap();
7252        (tmp, state, head)
7253    }
7254
7255    async fn resume_observed_head(state: &mut RunState, head: &str) -> Result<String> {
7256        let forge = Scripted::new(vec![seen(head, Checks::Green, "CLEAN", false)], vec![]);
7257        resume_landing_head(state, head, &green_pr().url, &forge).await
7258    }
7259
7260    async fn advance_landing_base(state: &RunState) {
7261        git::git(&state.repo, &["checkout", "main"]).await.unwrap();
7262        std::fs::write(state.repo.join("advance"), "advance").unwrap();
7263        git::git(&state.repo, &["add", "."]).await.unwrap();
7264        git::git(&state.repo, &["commit", "-m", "advance base"])
7265            .await
7266            .unwrap();
7267        git::git(&state.repo, &["push", "origin", "main"])
7268            .await
7269            .unwrap();
7270        git::git(&state.repo, &["checkout", "winner"])
7271            .await
7272            .unwrap();
7273    }
7274
7275    async fn foreign_landing_fixture() -> (tempfile::TempDir, RunState, String, std::path::PathBuf)
7276    {
7277        let (tmp, mut state, old) = landing_git_fixture().await;
7278        let path = tmp.path().join("foreign");
7279        git::worktree_add_branch(&state.repo, &path, "task-head", &old)
7280            .await
7281            .unwrap();
7282        state.foreign_checkout = Some(
7283            crate::foreign_checkout::inspect(&state.repo, "task-head", &path)
7284                .await
7285                .unwrap(),
7286        );
7287        state.config.merge.mode = crate::config::MergeMode::Pr;
7288        state.save().unwrap();
7289        (tmp, state, old, path)
7290    }
7291
7292    #[tokio::test]
7293    async fn foreign_landing_sync_and_interrupted_resumes_preserve_delivery_ancestry() {
7294        for phase in 0..4 {
7295            let (_tmp, mut state, old, path) = foreign_landing_fixture().await;
7296            advance_landing_base(&state).await;
7297            state.config.verify.gate = vec!["test -f advance && test -f change".into()];
7298            if phase == 0 {
7299                // The live Step::Rebase path uses this same synchronization
7300                // and publication pair, without going through resume.
7301                assert_eq!(
7302                    rebase_landing_worktree(&mut state, "origin/main")
7303                        .await
7304                        .unwrap(),
7305                    crate::rebase::Rebased::Applied
7306                );
7307            } else {
7308                interrupt_landing_rebase(&mut state).await;
7309                let repaired_tree = git::tree_of(&state.repo, "HEAD").await.unwrap();
7310                if phase == 2 {
7311                    git::sync_to_head(&state.repo).await.unwrap();
7312                } else if phase == 3 {
7313                    // Crash after joining history, before clearing its saved
7314                    // start. Repeating restoration must not add another merge.
7315                    restore_landing_worktree(&mut state).await.unwrap();
7316                    let joined = git::rev_parse(&state.repo, "HEAD").await.unwrap();
7317                    state.land_rebase_from = Some(old.clone());
7318                    state.save().unwrap();
7319                    restore_landing_worktree(&mut state).await.unwrap();
7320                    assert_eq!(git::rev_parse(&state.repo, "HEAD").await.unwrap(), joined);
7321                }
7322                state.save().unwrap();
7323                state = RunState::load(&state.id).unwrap();
7324                restore_landing_worktree(&mut state).await.unwrap();
7325                assert_eq!(
7326                    git::tree_of(&state.repo, "HEAD").await.unwrap(),
7327                    repaired_tree
7328                );
7329            }
7330            let head = if phase == 0 {
7331                let forge = Scripted::new(vec![seen(&old, Checks::Green, "CLEAN", false)], vec![]);
7332                publish_landing_head(&mut state, &old, &green_pr().url, &forge)
7333                    .await
7334                    .unwrap()
7335            } else {
7336                resume_observed_head(&mut state, &old).await.unwrap()
7337            };
7338            assert!(git::is_ancestor(&state.repo, &old, &head).await);
7339            assert!(git::is_ancestor(&state.repo, "origin/main", &head).await);
7340            assert_eq!(git::rev_parse(&path, "HEAD").await.unwrap(), head);
7341            assert_eq!(
7342                git::remote_head(&state.repo, "origin", "task-head")
7343                    .await
7344                    .unwrap(),
7345                Some(head.clone())
7346            );
7347            assert_eq!(state.gate_head.as_deref(), Some(head.as_str()));
7348            assert_eq!(state.foreign_checkout.as_ref().unwrap().synced_head, head);
7349            assert!(state.gate_allows_progress());
7350            assert!(state.land_rebase_from.is_none());
7351            assert!(!state.land_sync_pending);
7352            assert!(git::is_clean(&path).await.unwrap());
7353        }
7354    }
7355
7356    #[tokio::test]
7357    async fn foreign_landing_conflict_repair_can_resume_and_publish_without_losing_ancestry() {
7358        let (_tmp, mut state, old, path) = foreign_landing_fixture().await;
7359        git::git(&state.repo, &["checkout", "main"]).await.unwrap();
7360        std::fs::write(state.repo.join("change"), "conflicting base change").unwrap();
7361        git::commit_all(&state.repo, "conflicting base")
7362            .await
7363            .unwrap();
7364        git::git(&state.repo, &["push", "origin", "main"])
7365            .await
7366            .unwrap();
7367        git::git(&state.repo, &["checkout", "winner"])
7368            .await
7369            .unwrap();
7370        landing_gate_fixer(
7371            &mut state,
7372            "cat >/dev/null; printf resolved > change; printf '{\"addressed\":[],\"rejected\":[],\"notes\":\"resolved\"}'",
7373        );
7374        state.config.graph.gate_fix_rounds = 0;
7375        state.config.verify.gate = vec!["exit 1".into()];
7376        assert!(resume_observed_head(&mut state, &old).await.is_err());
7377        assert!(state.rebase_fixes.iter().any(|fix| fix.finished));
7378        let repaired = git::rev_parse(&state.repo, "HEAD").await.unwrap();
7379        assert!(git::is_ancestor(&state.repo, &old, &repaired).await);
7380        assert_eq!(
7381            std::fs::read_to_string(state.repo.join("change")).unwrap(),
7382            "resolved"
7383        );
7384        // A red gate must neither deliver nor publish the repaired tree.
7385        assert_eq!(git::rev_parse(&path, "HEAD").await.unwrap(), old);
7386        assert_eq!(
7387            git::remote_head(&state.repo, "origin", "task-head")
7388                .await
7389                .unwrap(),
7390            Some(old.clone())
7391        );
7392        state = RunState::load(&state.id).unwrap();
7393        state.config.verify.gate = vec!["grep -q resolved change".into()];
7394        let head = resume_observed_head(&mut state, &old).await.unwrap();
7395        assert_eq!(head, repaired);
7396        assert_eq!(git::rev_parse(&path, "HEAD").await.unwrap(), head);
7397        assert_eq!(
7398            std::fs::read_to_string(path.join("change")).unwrap(),
7399            "resolved"
7400        );
7401        assert_eq!(
7402            git::remote_head(&state.repo, "origin", "task-head")
7403                .await
7404                .unwrap(),
7405            Some(head.clone())
7406        );
7407        assert_eq!(state.gate_head.as_deref(), Some(head.as_str()));
7408        assert!(git::is_ancestor(&state.repo, "origin/main", &head).await);
7409        assert!(git::is_clean(&path).await.unwrap());
7410    }
7411
7412    #[tokio::test]
7413    async fn landing_resume_gates_rebased_sha_and_publishes_it_to_the_existing_head() {
7414        let (_tmp, mut state, old) = landing_git_fixture().await;
7415        advance_landing_base(&state).await;
7416        state.config.verify.gate = vec!["test -f advance".into()];
7417        let head = resume_observed_head(&mut state, &old).await.unwrap();
7418        assert_ne!(head, old);
7419        assert_eq!(state.gate_head.as_deref(), Some(head.as_str()));
7420        assert!(state.gate_allows_progress());
7421        assert_eq!(
7422            git::remote_head(&state.repo, "origin", "task-head")
7423                .await
7424                .unwrap(),
7425            Some(head.clone())
7426        );
7427        assert_eq!(state.pr_pushed_head, Some(head));
7428        assert!(!state.land_sync_pending);
7429        assert!(state.config.graph.land_approval);
7430    }
7431
7432    fn landing_gate_fixer(state: &mut RunState, script: &str) {
7433        state.config.disk.min_free_bytes = 0;
7434        state.config.graph.gate_fix_rounds = 1;
7435        state.config.agents = vec![crate::config::AgentSpec {
7436            id: "mock".into(),
7437            kind: crate::config::AgentKind::Command,
7438            model: None,
7439            command: vec!["sh".into(), "-c".into(), script.into()],
7440            extra_args: Vec::new(),
7441            env: Default::default(),
7442            prompt_delivery: None,
7443            max_turn_secs: None,
7444        }];
7445        state.candidates[0].agent = "mock".into();
7446    }
7447
7448    #[tokio::test]
7449    async fn landing_resume_revalidates_an_operator_published_winner_extension() {
7450        let (_tmp, mut state, old) = landing_git_fixture().await;
7451        std::fs::write(state.repo.join("change"), "operator update").unwrap();
7452        git::commit_all(&state.repo, "extend winner").await.unwrap();
7453        let updated = git::rev_parse(&state.repo, "HEAD").await.unwrap();
7454        git::git(
7455            &state.repo,
7456            &["push", "origin", "HEAD:refs/heads/task-head"],
7457        )
7458        .await
7459        .unwrap();
7460        state.config.verify.gate = vec!["exit 1".into()];
7461        state.config.graph.gate_fix_rounds = 0;
7462        assert!(resume_observed_head(&mut state, &updated).await.is_err());
7463        assert_ne!(old, updated);
7464        assert_eq!(state.gate_head.as_ref(), Some(&updated));
7465        assert!(!state.gate_allows_progress());
7466        assert!(state.land_sync_pending);
7467        state.config.verify.gate = vec!["grep -q 'operator update' change".into()];
7468        let head = resume_observed_head(&mut state, &updated).await.unwrap();
7469        assert_eq!(head, updated);
7470        assert!(state.gate_allows_progress());
7471        assert!(!state.land_sync_pending);
7472        let forge = Scripted::new(vec![seen(&head, Checks::Green, "CLEAN", false)], vec![]);
7473        land_with(&mut state, &green_pr().url, &forge)
7474            .await
7475            .unwrap();
7476        assert!(state.parked);
7477        assert_eq!(state.land_approval.as_ref().unwrap().head, head);
7478        assert!(forge.argvs().is_empty());
7479    }
7480
7481    #[tokio::test]
7482    async fn landing_resume_refuses_remote_only_and_divergent_operator_updates() {
7483        for divergent in [false, true] {
7484            let (_tmp, mut state, old) = landing_git_fixture().await;
7485            if divergent {
7486                git::git(&state.repo, &["reset", "--hard", "origin/main"])
7487                    .await
7488                    .unwrap();
7489            }
7490            std::fs::write(state.repo.join("external"), "external").unwrap();
7491            git::commit_all(&state.repo, "external update")
7492                .await
7493                .unwrap();
7494            let external = git::rev_parse(&state.repo, "HEAD").await.unwrap();
7495            git::git(
7496                &state.repo,
7497                &["push", "--force", "origin", "HEAD:refs/heads/task-head"],
7498            )
7499            .await
7500            .unwrap();
7501            if !divergent {
7502                git::git(&state.repo, &["reset", "--hard", &old])
7503                    .await
7504                    .unwrap();
7505            }
7506            let error = resume_observed_head(&mut state, &external)
7507                .await
7508                .unwrap_err();
7509            assert!(
7510                error
7511                    .to_string()
7512                    .contains("public head moved outside this run")
7513            );
7514            assert_eq!(state.pr_pushed_head.as_ref(), Some(&old));
7515            assert_eq!(
7516                git::remote_head(&state.repo, "origin", "task-head")
7517                    .await
7518                    .unwrap(),
7519                Some(external)
7520            );
7521        }
7522    }
7523
7524    #[tokio::test]
7525    async fn landing_resume_repairs_a_failed_rebased_gate_before_publication() {
7526        let (_tmp, mut state, old) = landing_git_fixture().await;
7527        advance_landing_base(&state).await;
7528        landing_gate_fixer(
7529            &mut state,
7530            "cat >/dev/null; printf fixed > change; printf '{\"addressed\":[],\"rejected\":[],\"notes\":\"fixed\"}'",
7531        );
7532        state.config.verify.gate =
7533            vec!["grep -q fixed change || { echo 'change needs repair'; exit 1; }".into()];
7534        state.config.verify.e2e = vec!["test -f advance".into()];
7535        // First save a red gate without spending a fixer round. Resume must
7536        // still offer the remaining budget for this exact same failed head.
7537        state.config.graph.gate_fix_rounds = 0;
7538        assert!(resume_observed_head(&mut state, &old).await.is_err());
7539        assert_eq!(
7540            git::remote_head(&state.repo, "origin", "task-head")
7541                .await
7542                .unwrap(),
7543            Some(old.clone())
7544        );
7545        state.config.graph.gate_fix_rounds = 1;
7546        let head = resume_observed_head(&mut state, &old).await.unwrap();
7547        assert_eq!(state.gate_fixes.len(), 1);
7548        assert!(state.gate_fixes[0].committed);
7549        assert!(state.gate_allows_progress());
7550        assert_eq!(state.gate_head.as_ref(), Some(&head));
7551        assert_eq!(
7552            git::remote_head(&state.repo, "origin", "task-head")
7553                .await
7554                .unwrap(),
7555            Some(head.clone())
7556        );
7557        let forge = Scripted::new(vec![seen(&head, Checks::Green, "CLEAN", false)], vec![]);
7558        land_with(&mut state, &green_pr().url, &forge)
7559            .await
7560            .unwrap();
7561        assert!(state.parked);
7562        assert_eq!(state.land_approval.as_ref().unwrap().head, head);
7563        assert!(forge.argvs().is_empty());
7564    }
7565
7566    #[tokio::test]
7567    async fn landing_gate_repairs_stop_at_the_saved_budget_without_publishing() {
7568        let (_tmp, mut state, old) = landing_git_fixture().await;
7569        advance_landing_base(&state).await;
7570        landing_gate_fixer(
7571            &mut state,
7572            "cat >/dev/null; printf fixed > change; printf '{\"addressed\":[],\"rejected\":[],\"notes\":\"fixed\"}'",
7573        );
7574        state.config.verify.gate = vec!["echo still red; exit 1".into()];
7575        for _ in 0..2 {
7576            assert!(resume_observed_head(&mut state, &old).await.is_err());
7577            assert_eq!(state.gate_fixes.len(), 1);
7578            assert_eq!(
7579                git::remote_head(&state.repo, "origin", "task-head")
7580                    .await
7581                    .unwrap(),
7582                Some(old.clone())
7583            );
7584        }
7585    }
7586
7587    #[tokio::test]
7588    async fn landing_gate_repair_cannot_publish_failed_e2e_on_repeated_resumes() {
7589        let (_tmp, mut state, old) = landing_git_fixture().await;
7590        advance_landing_base(&state).await;
7591        landing_gate_fixer(
7592            &mut state,
7593            "cat >/dev/null; printf fixed > change; printf '{\"addressed\":[],\"rejected\":[],\"notes\":\"fixed\"}'",
7594        );
7595        state.config.verify.gate =
7596            vec!["grep -q fixed change || { echo 'change needs repair'; exit 1; }".into()];
7597        // The repair makes the gate green while breaking an e2e invariant.
7598        state.config.verify.e2e = vec!["grep -q '^change$' change".into()];
7599        for resume in 0..3 {
7600            assert!(resume_observed_head(&mut state, &old).await.is_err());
7601            state = RunState::load(&state.id).unwrap();
7602            let evidence = state.gate_repair_e2e.as_ref().unwrap();
7603            let head = git::rev_parse(&state.repo, "HEAD").await.unwrap();
7604            assert_eq!(evidence.head, head);
7605            assert!(!evidence.passed);
7606            assert!(evidence.outcomes.iter().any(|o| !o.ok()));
7607            assert_eq!(state.gate_fixes.len(), 1);
7608            assert_eq!(
7609                git::remote_head(&state.repo, "origin", "task-head")
7610                    .await
7611                    .unwrap(),
7612                Some(old.clone())
7613            );
7614            if resume > 0 {
7615                assert!(
7616                    state.gate_allows_progress(),
7617                    "a green gate must not bypass red e2e"
7618                );
7619            }
7620        }
7621        // Once e2e genuinely succeeds on the same SHA, the saved requirement
7622        // can be satisfied without another fixer or approval bypass.
7623        state.config.verify.e2e = vec!["grep -q fixed change".into()];
7624        let head = resume_observed_head(&mut state, &old).await.unwrap();
7625        assert!(state.gate_repair_e2e.as_ref().unwrap().passed);
7626        assert_eq!(state.gate_repair_e2e.as_ref().unwrap().head, head);
7627        assert_eq!(state.gate_fixes.len(), 1);
7628        let forge = Scripted::new(vec![seen(&head, Checks::Green, "CLEAN", false)], vec![]);
7629        land_with(&mut state, &green_pr().url, &forge)
7630            .await
7631            .unwrap();
7632        assert!(state.parked);
7633        assert_eq!(state.land_approval.as_ref().unwrap().head, head);
7634        assert!(forge.argvs().is_empty());
7635    }
7636
7637    #[tokio::test]
7638    async fn landing_resumes_recheck_pending_deferred_and_stale_repair_e2e() {
7639        for phase in 0..3 {
7640            let (_tmp, mut state, old) = landing_git_fixture().await;
7641            advance_landing_base(&state).await;
7642            let head = resume_observed_head(&mut state, &old).await.unwrap();
7643            // Persist the shapes left by interruption, resource deferral,
7644            // and a successful check for a different (older) SHA.
7645            state.land_sync_pending = true;
7646            state.config.verify.e2e = vec!["echo e2e is red; exit 1".into()];
7647            state.gate_repair_e2e = Some(crate::run::GateRepairE2e {
7648                head: if phase == 2 {
7649                    old.clone()
7650                } else {
7651                    head.clone()
7652                },
7653                passed: phase == 2,
7654                outcomes: if phase == 1 {
7655                    vec![crate::run::CommandOutcome {
7656                        command: state.config.verify.e2e[0].clone(),
7657                        code: None,
7658                        output_tail: "verification resource unavailable".into(),
7659                        duration_ms: 0,
7660                        resource_blocked: true,
7661                    }]
7662                } else {
7663                    Vec::new()
7664                },
7665            });
7666            state.save().unwrap();
7667            for _ in 0..2 {
7668                state = RunState::load(&state.id).unwrap();
7669                assert!(resume_observed_head(&mut state, &head).await.is_err());
7670                let evidence = state.gate_repair_e2e.as_ref().unwrap();
7671                assert_eq!(evidence.head, head);
7672                assert!(!evidence.passed);
7673                assert!(!evidence.outcomes.is_empty());
7674                assert_eq!(
7675                    git::remote_head(&state.repo, "origin", "task-head")
7676                        .await
7677                        .unwrap(),
7678                    Some(head.clone())
7679                );
7680                assert!(state.land_sync_pending);
7681            }
7682        }
7683    }
7684
7685    async fn interrupt_landing_rebase(state: &mut RunState) {
7686        state.land_rebase_from = Some(git::rev_parse(&state.repo, "HEAD").await.unwrap());
7687        state.save().unwrap();
7688        let scratch = state.dir().join("rebase");
7689        assert_eq!(
7690            crate::rebase::rebase_with_fixer(state, &scratch, "winner", "origin/main")
7691                .await
7692                .unwrap(),
7693            crate::rebase::Rebased::Applied
7694        );
7695        assert!(!git::is_clean(&state.repo).await.unwrap());
7696    }
7697
7698    async fn landing_setup(state: &mut RunState) {
7699        git::git(
7700            &state.repo,
7701            &["config", "extensions.worktreeConfig", "true"],
7702        )
7703        .await
7704        .unwrap();
7705        state.config.worktree.setup =
7706            vec![toml::from_str("run = \"printf secret > .env\"").unwrap()];
7707        crate::worktree_setup::prepare(&state.config, &state.repo, &state.repo)
7708            .await
7709            .unwrap();
7710        state.config.verify.gate = vec!["test -f advance && test -f .env".into()];
7711    }
7712
7713    #[tokio::test]
7714    async fn landing_resume_restores_setup_after_both_fresh_and_interrupted_rebases() {
7715        for phase in 0..3 {
7716            let (_tmp, mut state, old) = landing_git_fixture().await;
7717            advance_landing_base(&state).await;
7718            landing_setup(&mut state).await;
7719            if phase > 0 {
7720                interrupt_landing_rebase(&mut state).await;
7721                if phase == 2 {
7722                    // Crash after clean removed setup products, before setup.
7723                    git::sync_to_head(&state.repo).await.unwrap();
7724                    assert!(!state.repo.join(".env").exists());
7725                }
7726                state = RunState::load(&state.id).unwrap();
7727            }
7728            let head = resume_observed_head(&mut state, &old).await.unwrap();
7729            assert_ne!(head, old);
7730            assert!(state.gate_allows_progress());
7731            assert!(state.land_rebase_from.is_none());
7732            assert_eq!(
7733                std::fs::read_to_string(state.repo.join(".env")).unwrap(),
7734                "secret"
7735            );
7736            assert!(git::is_clean(&state.repo).await.unwrap());
7737            assert!(
7738                !git::git(&state.repo, &["ls-tree", "--name-only", "HEAD"])
7739                    .await
7740                    .unwrap()
7741                    .contains(".env")
7742            );
7743        }
7744    }
7745
7746    #[tokio::test]
7747    async fn landing_restoration_protects_ignored_files_outside_setup_products() {
7748        let (_tmp, mut state, _) = landing_git_fixture().await;
7749        advance_landing_base(&state).await;
7750        landing_setup(&mut state).await;
7751        interrupt_landing_rebase(&mut state).await;
7752        // Exercise unusual names without asking Windows to create an invalid
7753        // control-character filename. Unix also covers embedded newlines.
7754        let locals = [
7755            "local notes",
7756            #[cfg(unix)]
7757            "local\nnotes",
7758        ];
7759        let excludes = git::git(&state.repo, &["rev-parse", "--git-path", "info/exclude"])
7760            .await
7761            .unwrap();
7762        std::fs::write(state.repo.join(excludes), "local*\n").unwrap();
7763        for local in locals {
7764            std::fs::write(state.repo.join(local), "keep these notes").unwrap();
7765            assert!(
7766                git::git_raw(&state.repo, &["check-ignore", "--quiet", local])
7767                    .await
7768                    .unwrap()
7769                    .ok()
7770            );
7771        }
7772        restore_landing_worktree(&mut state).await.unwrap();
7773        for local in locals {
7774            assert_eq!(
7775                std::fs::read_to_string(state.repo.join(local)).unwrap(),
7776                "keep these notes"
7777            );
7778        }
7779        assert_eq!(
7780            std::fs::read_to_string(state.repo.join(".env")).unwrap(),
7781            "secret"
7782        );
7783        assert!(git::is_clean(&state.repo).await.unwrap());
7784        assert!(state.land_rebase_from.is_none());
7785    }
7786
7787    #[tokio::test]
7788    async fn landing_restoration_rejects_ignored_paths_conflicting_with_the_new_tree() {
7789        for local in ["advance", "advance/notes", "new-directory"] {
7790            let (_tmp, mut state, _) = landing_git_fixture().await;
7791            advance_landing_base(&state).await;
7792            // Also cover a local file blocking a new tracked directory.
7793            git::git(&state.repo, &["checkout", "main"]).await.unwrap();
7794            std::fs::create_dir(state.repo.join("new-directory")).unwrap();
7795            std::fs::write(state.repo.join("new-directory/tracked"), "new").unwrap();
7796            git::git(&state.repo, &["add", "."]).await.unwrap();
7797            git::git(&state.repo, &["commit", "-m", "add directory"])
7798                .await
7799                .unwrap();
7800            git::git(&state.repo, &["push", "origin", "main"])
7801                .await
7802                .unwrap();
7803            git::git(&state.repo, &["checkout", "winner"])
7804                .await
7805                .unwrap();
7806            let excludes = git::git(&state.repo, &["rev-parse", "--git-path", "info/exclude"])
7807                .await
7808                .unwrap();
7809            std::fs::write(state.repo.join(excludes), "advance\nnew-directory\n").unwrap();
7810            let path = state.repo.join(local);
7811            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
7812            std::fs::write(&path, "local notes").unwrap();
7813            interrupt_landing_rebase(&mut state).await;
7814            let before = git::git(&state.repo, &["diff", "HEAD"]).await.unwrap();
7815            let checkpoint = state.land_rebase_from.clone();
7816            let error = restore_landing_worktree(&mut state).await.unwrap_err();
7817            assert!(
7818                error.to_string().contains("conflicting with HEAD"),
7819                "{error:#}"
7820            );
7821            assert_eq!(std::fs::read_to_string(&path).unwrap(), "local notes");
7822            assert_eq!(
7823                git::git(&state.repo, &["diff", "HEAD"]).await.unwrap(),
7824                before
7825            );
7826            assert_eq!(state.land_rebase_from, checkpoint);
7827        }
7828    }
7829
7830    #[tokio::test]
7831    async fn landing_resume_preserves_ignored_gate_artifacts_without_setup() {
7832        for interrupted in [false, true] {
7833            let (_tmp, mut state, old) = landing_git_fixture().await;
7834            assert!(state.config.worktree.setup.is_empty());
7835            // A gate product exists before the base moves, without setup metadata.
7836            let excludes = git::git(&state.repo, &["rev-parse", "--git-path", "info/exclude"])
7837                .await
7838                .unwrap();
7839            std::fs::write(state.repo.join(excludes), "target/\n").unwrap();
7840            state.config.verify.gate =
7841                vec!["mkdir -p target && printf artifact > target/probe".into()];
7842            state.gate_ran = false;
7843            resume_observed_head(&mut state, &old).await.unwrap();
7844            assert_eq!(
7845                std::fs::read_to_string(state.repo.join("target/probe")).unwrap(),
7846                "artifact"
7847            );
7848            advance_landing_base(&state).await;
7849            if interrupted {
7850                interrupt_landing_rebase(&mut state).await;
7851                state = RunState::load(&state.id).unwrap();
7852            }
7853            state.config.verify.gate =
7854                vec!["test -f advance && test \"$(cat target/probe)\" = artifact".into()];
7855            let head = resume_observed_head(&mut state, &old).await.unwrap();
7856            assert_ne!(head, old);
7857            assert!(state.gate_allows_progress());
7858            assert_eq!(state.gate_head.as_deref(), Some(head.as_str()));
7859            assert_eq!(
7860                std::fs::read_to_string(state.repo.join("target/probe")).unwrap(),
7861                "artifact"
7862            );
7863            assert!(state.land_rebase_from.is_none());
7864            assert_eq!(
7865                git::remote_head(&state.repo, "origin", "task-head")
7866                    .await
7867                    .unwrap(),
7868                Some(head.clone())
7869            );
7870            let forge = Scripted::new(vec![seen(&head, Checks::Green, "CLEAN", false)], vec![]);
7871            land_with(&mut state, &green_pr().url, &forge)
7872                .await
7873                .unwrap();
7874            assert!(state.parked);
7875            assert_eq!(state.land_approval.as_ref().unwrap().head, head);
7876            assert!(forge.argvs().is_empty());
7877        }
7878    }
7879
7880    #[tokio::test]
7881    #[cfg(unix)]
7882    async fn landing_restoration_waits_for_external_coordination_before_changing_files() {
7883        use std::os::fd::AsRawFd as _;
7884
7885        for interrupted in [false, true] {
7886            let (_tmp, mut state, _) = landing_git_fixture().await;
7887            advance_landing_base(&state).await;
7888            landing_setup(&mut state).await;
7889            if interrupted {
7890                interrupt_landing_rebase(&mut state).await;
7891            }
7892            state.config.worktree.setup =
7893                vec![toml::from_str("run = \"printf restored > .env\"").unwrap()];
7894            let common = crate::repo_lock::common_dir(&state.repo)
7895                .await
7896                .unwrap()
7897                .unwrap();
7898            std::fs::write(common.join("updater.contract"), "test-protocol-v1\n").unwrap();
7899            state.config.coordination = crate::config::test_coordination();
7900            state.config.graph.timeout_resource_wait = 0;
7901            let external = std::fs::OpenOptions::new()
7902                .read(true)
7903                .write(true)
7904                .create(true)
7905                .truncate(false)
7906                .open(common.join("updater.lock"))
7907                .unwrap();
7908            // SAFETY: the descriptor belongs to `external` and remains open.
7909            let locked =
7910                unsafe { libc::flock(external.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
7911            assert_eq!(locked, 0);
7912            let before = git::git(&state.repo, &["diff", "HEAD"]).await.unwrap();
7913            let checkpoint = state.land_rebase_from.clone();
7914            let error = restore_landing_worktree(&mut state).await.unwrap_err();
7915            assert!(error.is::<crate::repo_lock::ResourceBlocked>(), "{error:#}");
7916            assert_eq!(
7917                git::git(&state.repo, &["diff", "HEAD"]).await.unwrap(),
7918                before
7919            );
7920            assert_eq!(state.land_rebase_from, checkpoint);
7921            assert_eq!(
7922                std::fs::read_to_string(state.repo.join(".env")).unwrap(),
7923                "secret"
7924            );
7925
7926            drop(external);
7927            state.config.graph.timeout_resource_wait = 2;
7928            restore_landing_worktree(&mut state).await.unwrap();
7929            assert!(git::is_clean(&state.repo).await.unwrap());
7930            assert!(state.land_rebase_from.is_none());
7931            assert_eq!(
7932                std::fs::read_to_string(state.repo.join(".env")).unwrap(),
7933                "restored"
7934            );
7935        }
7936    }
7937
7938    #[tokio::test]
7939    #[cfg(unix)]
7940    async fn landing_restoration_inherits_cancellation_while_waiting_for_a_local_seat() {
7941        use std::sync::atomic::{AtomicBool, Ordering};
7942
7943        let (_tmp, mut state, _) = landing_git_fixture().await;
7944        advance_landing_base(&state).await;
7945        landing_setup(&mut state).await;
7946        interrupt_landing_rebase(&mut state).await;
7947        let common = crate::repo_lock::common_dir(&state.repo)
7948            .await
7949            .unwrap()
7950            .unwrap();
7951        std::fs::write(common.join("updater.contract"), "test-protocol-v1\n").unwrap();
7952        state.config.coordination = crate::config::test_coordination();
7953        let holder = crate::repo_lock::SeatLease::for_checkout(
7954            &state.repo,
7955            true,
7956            state.config.coordination.as_ref(),
7957        )
7958        .await
7959        .unwrap();
7960        assert!(holder.is_some());
7961        let cancelled = Arc::new(AtomicBool::new(false));
7962        let signal = cancelled.clone();
7963        let wait = crate::repo_lock::WaitPolicy::new(Duration::from_secs(30), move || {
7964            signal.load(Ordering::SeqCst)
7965        });
7966        let before = git::git(&state.repo, &["diff", "HEAD"]).await.unwrap();
7967        let checkpoint = state.land_rebase_from.clone();
7968        let (result, ()) = tokio::join!(
7969            tokio::time::timeout(
7970                Duration::from_secs(2),
7971                wait.scope(restore_landing_worktree(&mut state)),
7972            ),
7973            async {
7974                tokio::time::sleep(Duration::from_millis(100)).await;
7975                cancelled.store(true, Ordering::SeqCst);
7976            }
7977        );
7978        let error = result
7979            .expect("cancellation must release the waiting driver")
7980            .unwrap_err();
7981        assert!(error.is::<crate::repo_lock::ResourceBlocked>(), "{error:#}");
7982        assert_eq!(
7983            git::git(&state.repo, &["diff", "HEAD"]).await.unwrap(),
7984            before
7985        );
7986        assert_eq!(state.land_rebase_from, checkpoint);
7987        assert_eq!(
7988            std::fs::read_to_string(state.repo.join(".env")).unwrap(),
7989            "secret"
7990        );
7991        drop(holder);
7992        restore_landing_worktree(&mut state).await.unwrap();
7993        assert!(state.land_rebase_from.is_none());
7994        assert!(git::is_clean(&state.repo).await.unwrap());
7995    }
7996
7997    #[tokio::test]
7998    #[cfg(unix)]
7999    async fn landing_verification_inherits_cancellation_while_waiting_for_a_local_seat() {
8000        use std::sync::atomic::{AtomicBool, Ordering};
8001
8002        for e2e in [false, true] {
8003            let (_tmp, mut state, head) = landing_git_fixture().await;
8004            let common = crate::repo_lock::common_dir(&state.repo)
8005                .await
8006                .unwrap()
8007                .unwrap();
8008            std::fs::write(common.join("updater.contract"), "test-protocol-v1\n").unwrap();
8009            state.config.coordination = crate::config::test_coordination();
8010            if e2e {
8011                state.config.verify.e2e = vec!["true".into()];
8012                state.gate_repair_e2e = Some(crate::run::GateRepairE2e {
8013                    head: head.clone(),
8014                    passed: false,
8015                    outcomes: Vec::new(),
8016                });
8017            } else {
8018                state.config.verify.gate = vec!["true".into()];
8019                state.gate_ran = false;
8020            }
8021            let holder = crate::repo_lock::SeatLease::for_checkout(
8022                &state.repo,
8023                true,
8024                state.config.coordination.as_ref(),
8025            )
8026            .await
8027            .unwrap();
8028            assert!(holder.is_some());
8029            let cancelled = Arc::new(AtomicBool::new(false));
8030            let signal = cancelled.clone();
8031            let wait = crate::repo_lock::WaitPolicy::new(Duration::from_secs(30), move || {
8032                signal.load(Ordering::SeqCst)
8033            });
8034            let forge = Scripted::new(vec![seen(&head, Checks::Green, "CLEAN", false)], vec![]);
8035            let url = green_pr().url;
8036            let (result, ()) = tokio::join!(
8037                tokio::time::timeout(
8038                    Duration::from_secs(2),
8039                    wait.scope(publish_landing_head(&mut state, &head, &url, &forge)),
8040                ),
8041                async {
8042                    tokio::time::sleep(Duration::from_millis(100)).await;
8043                    cancelled.store(true, Ordering::SeqCst);
8044                }
8045            );
8046            let error = result
8047                .expect("cancellation must release the verification waiter")
8048                .unwrap_err();
8049            assert!(error.is::<crate::repo_lock::ResourceBlocked>(), "{error:#}");
8050            assert!(forge.argvs().is_empty());
8051            assert_eq!(
8052                git::remote_head(&state.repo, "origin", "task-head")
8053                    .await
8054                    .unwrap(),
8055                Some(head.clone())
8056            );
8057            drop(holder);
8058            // Cancellation must release its reservation so another attempt can verify.
8059            assert_eq!(
8060                publish_landing_head(&mut state, &head, &url, &forge)
8061                    .await
8062                    .unwrap(),
8063                head
8064            );
8065        }
8066    }
8067
8068    #[tokio::test]
8069    async fn landing_resume_preserves_user_edits_after_an_interrupted_rebase() {
8070        for staged in [false, true] {
8071            let (_tmp, mut state, old) = landing_git_fixture().await;
8072            advance_landing_base(&state).await;
8073            interrupt_landing_rebase(&mut state).await;
8074            std::fs::write(state.repo.join("change"), "user edit").unwrap();
8075            if staged {
8076                git::git(&state.repo, &["add", "change"]).await.unwrap();
8077            }
8078            assert!(resume_observed_head(&mut state, &old).await.is_err());
8079            assert_eq!(
8080                std::fs::read_to_string(state.repo.join("change")).unwrap(),
8081                "user edit"
8082            );
8083            assert_eq!(
8084                git::remote_head(&state.repo, "origin", "task-head")
8085                    .await
8086                    .unwrap(),
8087                Some(old)
8088            );
8089        }
8090    }
8091
8092    #[tokio::test]
8093    async fn landing_resume_returns_to_approval_wait_and_closed_pr_skips_sync() {
8094        let (_tmp, mut state, head) = landing_git_fixture().await;
8095        let forge = Scripted::new(vec![seen(&head, Checks::Green, "CLEAN", false)], vec![]);
8096        let id = state.id.clone();
8097        let pr = land_with(&mut state, &green_pr().url, &forge)
8098            .await
8099            .unwrap();
8100        assert_eq!(pr.state, PrLifecycle::Open);
8101        assert_eq!(state.id, id);
8102        assert!(state.parked);
8103        assert_eq!(state.status, RunStatus::Landing);
8104        assert_eq!(state.land_approval.as_ref().unwrap().head, head);
8105        assert!(forge.argvs().is_empty());
8106
8107        state.land_sync_pending = true;
8108        std::fs::write(state.repo.join("dirty"), "do not touch").unwrap();
8109        let mut closed = seen("foreign", Checks::Green, "CLEAN", false);
8110        closed.pr.state = PrLifecycle::Closed;
8111        let forge = Scripted::new(vec![closed], vec![]);
8112        assert_eq!(
8113            land_with(&mut state, &green_pr().url, &forge)
8114                .await
8115                .unwrap()
8116                .state,
8117            PrLifecycle::Closed
8118        );
8119        assert_eq!(
8120            std::fs::read_to_string(state.repo.join("dirty")).unwrap(),
8121            "do not touch"
8122        );
8123        assert!(forge.argvs().is_empty());
8124    }
8125
8126    #[tokio::test]
8127    async fn landing_resume_does_not_publish_a_failed_rebased_gate() {
8128        let (_tmp, mut state, old) = landing_git_fixture().await;
8129        advance_landing_base(&state).await;
8130        state.config.verify.gate = vec!["exit 1".into()];
8131        assert!(resume_observed_head(&mut state, &old).await.is_err());
8132        assert_ne!(state.gate_head.as_deref(), Some(old.as_str()));
8133        assert!(!state.gate_allows_progress());
8134        assert_eq!(
8135            git::remote_head(&state.repo, "origin", "task-head")
8136                .await
8137                .unwrap(),
8138            Some(old)
8139        );
8140        assert!(state.land_sync_pending);
8141    }
8142
8143    #[tokio::test]
8144    async fn landing_publication_recovers_a_push_before_save_and_refuses_a_lease_conflict() {
8145        let (_tmp, mut state, old) = landing_git_fixture().await;
8146        advance_landing_base(&state).await;
8147        let pushed = resume_observed_head(&mut state, &old).await.unwrap();
8148        // Model a crash after the push but before saving its published SHA.
8149        state.pr_pushed_head = Some(old.clone());
8150        state.land_sync_pending = true;
8151        assert_eq!(
8152            resume_observed_head(&mut state, &pushed).await.unwrap(),
8153            pushed
8154        );
8155        // A writer races the observed PR snapshot: the explicit remote lease
8156        // must reject the push even though preflight saw our expected head.
8157        git::git(
8158            &state.repo,
8159            &["push", "--force", "origin", "main:refs/heads/task-head"],
8160        )
8161        .await
8162        .unwrap();
8163        assert!(
8164            publish_landing_head(
8165                &mut state,
8166                &pushed,
8167                &green_pr().url,
8168                &Scripted::new(vec![seen(&pushed, Checks::Green, "CLEAN", false)], vec![])
8169            )
8170            .await
8171            .unwrap_err()
8172            .to_string()
8173            .contains("publishing gated")
8174        );
8175        assert!(
8176            resume_observed_head(&mut state, "foreign-head")
8177                .await
8178                .is_err()
8179        );
8180    }
8181    #[tokio::test]
8182    async fn landing_publication_refuses_a_pr_closed_during_gate() {
8183        let (_tmp, mut state, head) = landing_git_fixture().await;
8184        let mut closed = seen(&head, Checks::Green, "CLEAN", false);
8185        closed.pr.state = PrLifecycle::Closed;
8186        let forge = Scripted::new(vec![closed], vec![]);
8187        assert!(
8188            publish_landing_head(&mut state, &head, &green_pr().url, &forge)
8189                .await
8190                .unwrap_err()
8191                .to_string()
8192                .contains("PR closed or merged")
8193        );
8194    }
8195
8196    /// A candidate selected by the uncontested tally.
8197    fn winning_candidate(summary: &str) -> Candidate {
8198        Candidate {
8199            index: 0,
8200            label: 'A',
8201            agent: "opus".to_owned(),
8202            branch: "magi/x/A".to_owned(),
8203            worktree: PathBuf::from("/wt/A"),
8204            summary: summary.to_owned(),
8205            stat: String::new(),
8206            files: 1,
8207            commits: 1,
8208            empty: false,
8209            failed: None,
8210            verified_noop: None,
8211            duration_ms: 0,
8212            folded: false,
8213        }
8214    }
8215
8216    fn uncontested_tally() -> Tally {
8217        Tally {
8218            first_choice: BTreeMap::from([('A', 1)]),
8219            borda: BTreeMap::new(),
8220            winner: 'A',
8221            rankings: 1,
8222            unanimous_initial: true,
8223            deliberated: false,
8224            changed_votes: 0,
8225            unanimous_final: true,
8226            tie_break: None,
8227            judges: 1,
8228            present: 1,
8229            quorum: 1,
8230            met_quorum: true,
8231            uncontested: None,
8232        }
8233    }
8234
8235    fn review_record(reviewer: usize, agent: &str, summary: &str) -> ReviewRecord {
8236        ReviewRecord {
8237            attempts: 0,
8238            reviewer,
8239            agent: agent.to_owned(),
8240            summary: summary.to_owned(),
8241            findings: Vec::new(),
8242            vote: None,
8243            failed: None,
8244            duration_ms: 0,
8245        }
8246    }
8247
8248    fn review_round(round: usize, reviews: Vec<ReviewRecord>) -> ReviewRound {
8249        let answered = reviews.len();
8250        ReviewRound {
8251            round,
8252            head: "abc1234".to_owned(),
8253            verified_head: None,
8254            verified_at: None,
8255            reviews,
8256            e2e: Vec::new(),
8257            verify_retried: false,
8258            e2e_deferred: false,
8259            e2e_defer_reason: None,
8260            fix: None,
8261            blocking: 0,
8262            answered,
8263            expected: answered,
8264            clean: true,
8265            progressed: false,
8266            vote_split: false,
8267            reconsideration: Vec::new(),
8268            verdict: None,
8269        }
8270    }
8271
8272    #[test]
8273    fn the_approval_panel_states_the_task_verbatim_in_either_language() {
8274        let en = panel();
8275        assert!(en.contains("Task"), "{en}");
8276        assert!(en.contains("add retries to the uploader"), "{en}");
8277
8278        let mut state = run_state();
8279        state.config.graph.language = "ja".to_owned();
8280        let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8281        assert!(ja.contains("タスク"), "{ja}");
8282        assert!(
8283            ja.contains("add retries to the uploader"),
8284            "the task itself is not translated: {ja}"
8285        );
8286    }
8287
8288    #[test]
8289    fn the_approval_panel_omits_what_changed_and_review_verdict_with_no_data() {
8290        // `run_state()` has no candidates, no tally and no reviews - exactly
8291        // the shape a run has before anything has judged or reviewed it, and
8292        // the panel must not print an empty box for either.
8293        let html = panel();
8294        assert!(!html.contains("What changed"), "{html}");
8295        assert!(!html.contains("Review verdict"), "{html}");
8296    }
8297
8298    #[test]
8299    fn the_approval_panel_omits_what_changed_when_the_winners_summary_is_empty() {
8300        let mut state = run_state();
8301        state.candidates = vec![winning_candidate("")];
8302        state.tally = Some(uncontested_tally());
8303        let html = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8304        assert!(
8305            !html.contains("What changed"),
8306            "an empty summary must not render an empty box: {html}"
8307        );
8308    }
8309
8310    #[test]
8311    fn the_approval_panel_shows_the_winners_own_account_in_either_language() {
8312        let mut state = run_state();
8313        state.candidates = vec![winning_candidate(
8314            "Added a retry loop around the uploader PUT call.",
8315        )];
8316        state.tally = Some(uncontested_tally());
8317        let en = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8318        assert!(en.contains("What changed"), "{en}");
8319        assert!(
8320            en.contains("Added a retry loop around the uploader PUT call."),
8321            "{en}"
8322        );
8323
8324        state.config.graph.language = "ja".to_owned();
8325        let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8326        assert!(ja.contains("変更内容"), "{ja}");
8327        assert!(
8328            ja.contains("Added a retry loop around the uploader PUT call."),
8329            "{ja}"
8330        );
8331    }
8332
8333    #[test]
8334    fn the_approval_panel_shows_only_the_last_review_rounds_verdict() {
8335        let mut state = run_state();
8336        state.reviews = vec![
8337            review_round(
8338                1,
8339                vec![review_record(1, "alpha", "found a race, sent back")],
8340            ),
8341            review_round(2, vec![review_record(1, "alpha", "race is fixed, clean")]),
8342        ];
8343        let en = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8344        assert!(en.contains("Review verdict"), "{en}");
8345        assert!(en.contains("race is fixed, clean"), "{en}");
8346        assert!(
8347            !en.contains("found a race, sent back"),
8348            "only the round that actually cleared the merge should show: {en}"
8349        );
8350
8351        state.config.graph.language = "ja".to_owned();
8352        let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8353        assert!(ja.contains("レビューの結論"), "{ja}");
8354        assert!(ja.contains("レビュアー"), "{ja}");
8355        assert!(ja.contains("race is fixed, clean"), "{ja}");
8356    }
8357
8358    /// The `incomplete_review = "warn"` policy (see
8359    /// `graph::Runner::review_loop`) can push a `clean` round to
8360    /// `state.reviews` while one seat's own record still has `failed: Some`
8361    /// and an empty `summary` - a seat that never answered, not one that
8362    /// answered with nothing to say.
8363    fn unanswered_review_record(reviewer: usize, agent: &str, reason: &str) -> ReviewRecord {
8364        ReviewRecord {
8365            attempts: 0,
8366            reviewer,
8367            agent: agent.to_owned(),
8368            summary: String::new(),
8369            findings: Vec::new(),
8370            vote: None,
8371            failed: Some(reason.to_owned()),
8372            duration_ms: 0,
8373        }
8374    }
8375
8376    #[test]
8377    fn the_approval_panel_never_shows_an_unanswered_seat_as_a_blank_verdict() {
8378        let mut state = run_state();
8379        state.reviews = vec![review_round(
8380            1,
8381            vec![
8382                review_record(1, "alpha", "clean, nothing to add"),
8383                unanswered_review_record(2, "beta", "timed out"),
8384            ],
8385        )];
8386        let en = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8387        assert!(en.contains("clean, nothing to add"), "{en}");
8388        assert!(
8389            en.contains("produced no answer: timed out"),
8390            "a seat that never answered must say so, not render a blank box: {en}"
8391        );
8392        assert!(
8393            !en.contains("<div style=\"white-space:pre-wrap;font-size:13px\"></div>"),
8394            "no reviewer box may be left empty: {en}"
8395        );
8396
8397        state.config.graph.language = "ja".to_owned();
8398        let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
8399        assert!(ja.contains("回答なし: timed out"), "{ja}");
8400    }
8401
8402    #[test]
8403    fn the_approval_panel_contains_nothing_the_frames_policy_would_block() {
8404        let html = panel();
8405        assert!(!html.contains("<script"), "no script survives the csp");
8406        assert!(!html.contains("<form"), "form-action is 'none'");
8407        let pr = green_pr();
8408        assert_eq!(
8409            html.matches("http").count(),
8410            html.matches(pr.url.as_str()).count(),
8411            "the only http url in the panel is the pull request's own link"
8412        );
8413        assert!(
8414            html.contains("target=\"_blank\""),
8415            "the PR link must open in a new tab so it escapes the iframe"
8416        );
8417        assert!(
8418            html.contains("rel=\"noopener noreferrer\""),
8419            "external links opening in new tabs must carry noopener noreferrer"
8420        );
8421    }
8422
8423    #[test]
8424    fn added_and_removed_diff_lines_are_distinguishable_without_colour() {
8425        let html = panel();
8426        assert!(
8427            html.contains(">+</span>"),
8428            "an added line carries a `+` in the gutter, not only a background"
8429        );
8430        assert!(
8431            html.contains(">-</span>"),
8432            "a removed line carries a `-` in the gutter, not only a background"
8433        );
8434        assert!(
8435            html.contains(">new line</span>"),
8436            "the marker is moved to the gutter, so the body is printed once without it"
8437        );
8438    }
8439
8440    #[test]
8441    fn a_diff_past_the_threshold_is_cut_with_an_honest_count() {
8442        let total = DIFF_MAX_LINES + 100;
8443        let diff: String = (0..total).map(|i| format!("+line {i}\n")).collect();
8444        let html = approval_panel(
8445            &run_state(),
8446            &green_pr(),
8447            NUMSTAT,
8448            &diff,
8449            &[],
8450            "feat: something long",
8451        );
8452        assert!(
8453            html.contains(&format!("100 of {total} diff lines omitted")),
8454            "the note must say exactly how much was cut"
8455        );
8456        assert!(html.contains(&format!("line {}", DIFF_MAX_LINES - 1)));
8457        assert!(
8458            !html.contains(&format!("line {DIFF_MAX_LINES}")),
8459            "nothing past the threshold is rendered"
8460        );
8461        assert!(
8462            html.contains("/repo/magi"),
8463            "the note says where the rest is"
8464        );
8465    }
8466
8467    #[test]
8468    fn a_path_with_html_metacharacters_is_escaped_rather_than_rendered() {
8469        let html = approval_panel(
8470            &run_state(),
8471            &green_pr(),
8472            "1\t2\tsrc/<b>&\"x\"'.rs",
8473            "",
8474            &[],
8475            "subject",
8476        );
8477        assert!(html.contains("src/&lt;b&gt;&amp;&quot;x&quot;&#39;.rs"));
8478        assert!(
8479            !html.contains("<b>"),
8480            "an agent-influenced path must never become markup"
8481        );
8482    }
8483
8484    #[tokio::test]
8485    async fn the_merge_lock_serialises_one_repository_but_never_a_different_one() {
8486        let a = std::path::PathBuf::from("/repo/a");
8487        let b = std::path::PathBuf::from("/repo/b");
8488
8489        let held = repo_merge_lock(&a).lock_owned().await;
8490
8491        // A second, concurrent land run against the *same* repository must
8492        // wait - `try_lock` fails while `held` is alive.
8493        assert!(
8494            repo_merge_lock(&a).try_lock().is_err(),
8495            "a second merge into the same repository must not proceed concurrently"
8496        );
8497
8498        // A run against a *different* repository must not be blocked by it -
8499        // this is what keeps a slow rebase or `gh pr merge` in one
8500        // repository from also stalling a land-approval resume in another.
8501        assert!(
8502            repo_merge_lock(&b).try_lock().is_ok(),
8503            "a different repository's merge lock must be independent"
8504        );
8505
8506        drop(held);
8507        assert!(
8508            repo_merge_lock(&a).try_lock().is_ok(),
8509            "the lock is released once the holder is done"
8510        );
8511    }
8512
8513    pub(crate) fn repair_state(major: bool, reject: bool) -> RunState {
8514        let mut state = landing_state();
8515        let mut rec = review_record(1, "alpha", "precise rejection reason");
8516        rec.vote = Some(if reject {
8517            crate::verdict::ReviewVote::Reject
8518        } else {
8519            crate::verdict::ReviewVote::Approve
8520        });
8521        if major {
8522            rec.findings = contested().findings;
8523        }
8524        let mut round = review_round(1, vec![rec]);
8525        round.clean = false;
8526        round.blocking = usize::from(major);
8527        state.reviews.push(round);
8528        state.config.graph.review_rounds = 1;
8529        state
8530    }
8531
8532    #[test]
8533    fn approval_policy_controls_are_independent_and_keep_all_reasons() {
8534        for every in [false, true] {
8535            for contested in [false, true] {
8536                for major in [false, true] {
8537                    for reject in [false, true] {
8538                        let mut state = repair_state(major, reject);
8539                        state.config.graph.land_approval = every;
8540                        state.config.graph.hold_contested_merge = contested;
8541                        let reasons = approval_reasons(&state);
8542                        assert_eq!(reasons.contains(&ApprovalReason::EveryMerge), every);
8543                        assert_eq!(
8544                            reasons.contains(&ApprovalReason::OpenBlockingFindings),
8545                            contested && major
8546                        );
8547                        assert_eq!(
8548                            reasons.contains(&ApprovalReason::FinalReject),
8549                            contested && reject
8550                        );
8551                        assert_eq!(
8552                            reasons.len(),
8553                            usize::from(every)
8554                                + usize::from(contested && major)
8555                                + usize::from(contested && reject)
8556                        );
8557                    }
8558                }
8559            }
8560        }
8561    }
8562
8563    #[test]
8564    fn approval_policy_and_rendering_follow_reconsideration_in_both_directions() {
8565        use crate::verdict::ReviewVote::{Approve, Reject};
8566        for (initial_reject, final_vote) in [(true, Approve), (false, Reject)] {
8567            let mut state = repair_state(false, initial_reject);
8568            state.reviews[0]
8569                .reconsideration
8570                .push(crate::run::ReviewRevoteRecord {
8571                    reviewer: 1,
8572                    agent: "alpha".into(),
8573                    vote: Some(final_vote),
8574                    reason: "changed after reading the panel <evidence>".into(),
8575                    failed: None,
8576                });
8577            assert_eq!(
8578                approval_reasons(&state).contains(&ApprovalReason::FinalReject),
8579                final_vote == Reject
8580            );
8581            for language in ["en", "ja"] {
8582                state.config.graph.language = language.into();
8583                let evidence = approval_evidence(&state);
8584                assert!(evidence.contains(&format!("→ {}", final_vote.label())));
8585                assert!(evidence.contains("changed after reading the panel <evidence>"));
8586                let html = approval_panel(&state, &green_pr(), "", "", &[], "subject");
8587                assert!(html.contains("changed after reading the panel &lt;evidence&gt;"));
8588                assert!(!html.contains("<evidence>"));
8589            }
8590            state.reviews[0].reconsideration[0].vote = None;
8591            state.reviews[0].reconsideration[0].failed = Some("timed out".into());
8592            assert_eq!(
8593                approval_reasons(&state).contains(&ApprovalReason::FinalReject),
8594                initial_reject
8595            );
8596            assert!(approval_evidence(&state).contains("timed out"));
8597        }
8598    }
8599
8600    #[tokio::test]
8601    async fn approval_merge_paths_apply_controls_to_major_and_reject_independently() {
8602        for every in [false, true] {
8603            for contested in [false, true] {
8604                for (major, reject) in [(false, false), (false, true), (true, false), (true, true)]
8605                {
8606                    let mut state = repair_state(major, reject);
8607                    state.config.graph.land_approval = every;
8608                    state.config.graph.hold_contested_merge = contested;
8609                    let forge = Scripted::new(
8610                        vec![seen("abc", Checks::Green, "CLEAN", false)],
8611                        vec![(true, "")],
8612                    );
8613                    land_with(&mut state, URL, &forge).await.unwrap();
8614                    let expected = every || (contested && (major || reject));
8615                    assert_eq!(state.land_approval.is_some(), expected);
8616                    assert_eq!(forge.calls().contains(&"merge"), !expected);
8617                    if expected {
8618                        assert!(state.parked);
8619                    }
8620                }
8621            }
8622        }
8623    }
8624
8625    #[tokio::test]
8626    async fn reviewed_branch_hold_is_read_before_landing_decision() {
8627        for (base, control) in [
8628            (false, "land_approval"),
8629            (true, "land_approval"),
8630            (false, "hold_contested_merge"),
8631        ] {
8632            let (_tmp, mut state, _) = landing_git_fixture().await;
8633            state.config.graph.land_approval = base;
8634            state.config.graph.hold_contested_merge = false;
8635            state.reviewed_commits = Some(Vec::new());
8636            if control == "hold_contested_merge" {
8637                state.reviews = repair_state(false, true).reviews;
8638            }
8639            std::fs::write(
8640                state.repo.join("magi.toml"),
8641                format!("[graph]\n{control} = {}\n", !base),
8642            )
8643            .unwrap();
8644            git::git(&state.repo, &["add", "magi.toml"]).await.unwrap();
8645            git::git(&state.repo, &["commit", "-m", "approval policy"])
8646                .await
8647                .unwrap();
8648            let head = git::rev_parse(&state.repo, "HEAD").await.unwrap();
8649            state.land_sync_pending = false;
8650            state.pr_pushed_head = Some(head.clone());
8651            state.gate_head = Some(head.clone());
8652            let forge = Scripted::new(
8653                vec![seen(&head, Checks::Green, "CLEAN", false)],
8654                vec![(true, "")],
8655            );
8656            land_with(&mut state, URL, &forge).await.unwrap();
8657            assert!(
8658                state.land_approval.is_some(),
8659                "base={base}, control={control}"
8660            );
8661            assert!(!forge.calls().contains(&"merge"));
8662            if !base {
8663                let question = ask::Questions::open()
8664                    .get(&state.land_approval.as_ref().unwrap().question)
8665                    .unwrap();
8666                assert!(question.detail.contains(&format!(
8667                    "{control}=true from the reviewed branch's magi.toml"
8668                )));
8669            }
8670        }
8671    }
8672
8673    #[tokio::test]
8674    async fn reviewed_branch_hold_asks_and_survives_removal_and_resume() {
8675        let (_tmp, mut state, _) = landing_git_fixture().await;
8676        state.config.graph.land_approval = false;
8677        state.config.graph.hold_contested_merge = false;
8678        state.reviewed_commits = Some(Vec::new());
8679        std::fs::write(
8680            state.repo.join("magi.toml"),
8681            "[graph]\nland_approval = true\n[verify]\ngate = ['untrusted']\n",
8682        )
8683        .unwrap();
8684        git::git(&state.repo, &["add", "magi.toml"]).await.unwrap();
8685        git::git(&state.repo, &["commit", "-m", "require approval"])
8686            .await
8687            .unwrap();
8688        let head = git::rev_parse(&state.repo, "HEAD").await.unwrap();
8689        let gate = state.config.verify.gate.clone();
8690        state.observe_branch_owner_holds(&head).await;
8691        assert_eq!(state.config.verify.gate, gate);
8692        assert_eq!(approval_reasons(&state), [ApprovalReason::EveryMerge]);
8693        git::git(&state.repo, &["rm", "magi.toml"]).await.unwrap();
8694        git::git(&state.repo, &["commit", "-m", "remove config"])
8695            .await
8696            .unwrap();
8697        let later = git::rev_parse(&state.repo, "HEAD").await.unwrap();
8698        state = serde_json::from_str(&serde_json::to_string(&state).unwrap()).unwrap();
8699        state.observe_branch_owner_holds(&later).await;
8700        assert_eq!(state.branch_owner_holds[0].head, head);
8701        state.land_sync_pending = false;
8702        state.pr_pushed_head = Some(later.clone());
8703        state.gate_head = Some(later.clone());
8704        let forge = Scripted::new(
8705            vec![seen(&later, Checks::Green, "CLEAN", false)],
8706            vec![(true, "")],
8707        );
8708        land_with(&mut state, URL, &forge).await.unwrap();
8709        assert!(!forge.calls().contains(&"merge"));
8710        let question = ask::Questions::open()
8711            .get(&state.land_approval.as_ref().unwrap().question)
8712            .unwrap();
8713        assert!(question.detail.contains("Approval required because:"));
8714        assert!(
8715            question
8716                .detail
8717                .contains("land_approval=true from the reviewed branch's magi.toml")
8718        );
8719    }
8720
8721    #[tokio::test]
8722    async fn approval_snapshot_exposes_minor_findings_and_survives_disabled_controls() {
8723        let mut state = repair_state(false, true);
8724        state.reviews[0].clean = true;
8725        let mut finding = contested().findings.remove(0);
8726        finding.severity = crate::verdict::Severity::Minor;
8727        state.reviews[0].reviews[0].findings.push(finding);
8728        assert!(state.open_findings().is_empty());
8729        let mut pr = green_pr();
8730        pr.review_comments.clear();
8731        for language in ["en", "ja"] {
8732            state.config.graph.language = language.into();
8733            let html = approval_panel(&state, &pr, "", "", &[], "subject");
8734            assert!(html.contains("[R3-1-1] minor"));
8735            assert!(html.contains(if language == "ja" {
8736                "内部指摘の解決を意味するものではありません"
8737            } else {
8738                "This does not mean internal findings are resolved"
8739            }));
8740            assert!(
8741                html.find(if language == "ja" {
8742                    "承認が必要な理由:"
8743                } else {
8744                    "Approval required because:"
8745                })
8746                .unwrap()
8747                    < html.find("[R3-1-1]").unwrap()
8748            );
8749        }
8750        state.config.graph.language = "en".into();
8751        let observed = seen("abc", Checks::Green, "CLEAN", false);
8752        assert_eq!(
8753            approval_gate(&mut state, &observed.pr, "subject", None, "abc")
8754                .await
8755                .unwrap(),
8756            ApprovalGate::Pending
8757        );
8758        let id = state.land_approval.as_ref().unwrap().question.clone();
8759        let q = ask::Questions::open().get(&id).unwrap();
8760        assert!(q.detail.starts_with("Approval required because:"));
8761        assert!(q.detail.contains("final reviewer reject"));
8762        assert!(q.detail.contains("[R3-1-1] minor"));
8763        assert!(
8764            q.detail
8765                .contains("No adoption or resolution evidence recorded")
8766        );
8767        state.config.graph.land_approval = false;
8768        state.config.graph.hold_contested_merge = false;
8769        state.reviews.clear();
8770        assert_eq!(approval_reasons(&state), [ApprovalReason::ExistingDecision]);
8771        assert!(deputy_brief(&q, Some(&state)).contains("final reviewer reject"));
8772        assert!(deputy_brief(&q, None).contains("[R3-1-1] minor"));
8773        let forge = Scripted::new(vec![observed], vec![]);
8774        land_with(&mut state, URL, &forge).await.unwrap();
8775        assert!(!forge.calls().contains(&"merge"));
8776        assert_eq!(state.land_approval.as_ref().unwrap().question, id);
8777        assert_eq!(ask::Questions::open().get(&id).unwrap().detail, q.detail);
8778    }
8779
8780    #[tokio::test]
8781    async fn approval_evidence_keeps_all_findings_and_bounds_unicode_prose() {
8782        let mut state = repair_state(false, true);
8783        state.config.graph.land_approval = false;
8784        let long_reason = format!("{}REASON_TAIL", "拒否🦀".repeat(200));
8785        let long_detail = format!("{}DETAIL_TAIL", "詳細🦀".repeat(200));
8786        let long_title = format!("{}TITLE_TAIL", "題🦀".repeat(100));
8787        state.reviews[0].reviews[0].summary = long_reason.clone();
8788        for i in 1..=10 {
8789            let mut finding = contested().findings.remove(0);
8790            finding.id = format!("R1-1-{i}");
8791            finding.severity = if i == 9 {
8792                crate::verdict::Severity::Major
8793            } else {
8794                crate::verdict::Severity::Minor
8795            };
8796            finding.title = long_title.clone();
8797            finding.detail = long_detail.clone();
8798            state.reviews[0].reviews[0].findings.push(finding);
8799        }
8800        state.reviews[0].fix = Some(crate::run::FixRecord {
8801            agent: "fixer".into(),
8802            addressed: vec!["R1-1-9".into()],
8803            rejected: vec![crate::verdict::Rejection {
8804                id: "R1-1-10".into(),
8805                why: long_reason.clone(),
8806            }],
8807            notes: long_detail.clone(),
8808            committed: false,
8809            failed: None,
8810            duration_ms: 0,
8811            continuation: None,
8812        });
8813        assert_eq!(
8814            approval_reasons(&state),
8815            [
8816                ApprovalReason::OpenBlockingFindings,
8817                ApprovalReason::FinalReject
8818            ]
8819        );
8820        let excerpt = approval_excerpt(&long_reason, 300);
8821        assert_eq!(excerpt.chars().count(), 300);
8822        assert!(excerpt.ends_with('…'));
8823        let boundary = "拒".repeat(300);
8824        assert_eq!(approval_excerpt(&boundary, 300), boundary);
8825        for language in ["en", "ja"] {
8826            state.config.graph.language = language.into();
8827            for revote in [
8828                None,
8829                Some(crate::verdict::ReviewVote::Reject),
8830                Some(crate::verdict::ReviewVote::Approve),
8831            ] {
8832                state.reviews[0].reviews[0].vote = Some(crate::verdict::ReviewVote::Reject);
8833                state.reviews[0].reconsideration.clear();
8834                if let Some(vote) = revote {
8835                    state.reviews[0].reviews[0].vote = Some(crate::verdict::ReviewVote::Approve);
8836                    state.reviews[0]
8837                        .reconsideration
8838                        .push(crate::run::ReviewRevoteRecord {
8839                            reviewer: 1,
8840                            agent: "alpha".into(),
8841                            vote: Some(vote),
8842                            reason: long_reason.clone(),
8843                            failed: None,
8844                        });
8845                }
8846                let evidence = approval_evidence(&state);
8847                let final_vote = revote.unwrap_or(crate::verdict::ReviewVote::Reject);
8848                assert!(evidence.contains(&format!("#1 (alpha): {}", final_vote.label())));
8849                assert!(approval_reasons(&state).contains(&ApprovalReason::OpenBlockingFindings));
8850                assert_eq!(
8851                    approval_reasons(&state).contains(&ApprovalReason::FinalReject),
8852                    final_vote == crate::verdict::ReviewVote::Reject
8853                );
8854                assert!(evidence.contains(&excerpt));
8855                assert!(evidence.contains("[R1-1-8] minor"));
8856                assert!(evidence.contains("[R1-1-9] major"));
8857                assert!(evidence.contains("[R1-1-10] minor"));
8858                for tail in ["REASON_TAIL", "DETAIL_TAIL", "TITLE_TAIL"] {
8859                    assert!(!evidence.contains(tail));
8860                }
8861                assert!(evidence.contains(if language == "ja" {
8862                    "採用申告(解決未確認)"
8863                } else {
8864                    "claimed addressed (resolution unconfirmed)"
8865                }));
8866                assert!(evidence.contains(if language == "ja" {
8867                    "不採用理由"
8868                } else {
8869                    "declined"
8870                }));
8871                let major_evidence = evidence
8872                    .split("[R1-1-9] major")
8873                    .nth(1)
8874                    .unwrap()
8875                    .split("[R1-1-10]")
8876                    .next()
8877                    .unwrap();
8878                assert!(major_evidence.contains(if language == "ja" {
8879                    "採用申告(解決未確認)"
8880                } else {
8881                    "claimed addressed (resolution unconfirmed)"
8882                }));
8883                let last_evidence = evidence.split("[R1-1-10] minor").nth(1).unwrap();
8884                assert!(last_evidence.contains(if language == "ja" {
8885                    "不採用理由"
8886                } else {
8887                    "declined"
8888                }));
8889                let html = approval_panel(&state, &green_pr(), "", "", &[], "subject");
8890                assert!(html.contains(&esc(&evidence)));
8891                assert_eq!(
8892                    approval_gate(&mut state, &green_pr(), "subject", None, "abc")
8893                        .await
8894                        .unwrap(),
8895                    ApprovalGate::Pending
8896                );
8897                let id = &state.land_approval.take().unwrap().question;
8898                let q = ask::Questions::open().get(id).unwrap();
8899                assert!(q.detail.starts_with(&evidence));
8900            }
8901            state.reviews[0].reviews[0].vote = Some(crate::verdict::ReviewVote::Reject);
8902        }
8903    }
8904
8905    #[test]
8906    fn merge_repair_choice_covers_major_only_and_reject_only_without_contested_and() {
8907        for (major, reject) in [(true, false), (false, true), (true, true)] {
8908            let state = repair_state(major, reject);
8909            assert!(repair_available(&state));
8910            assert_eq!(&approval_choices(&state)[..3], [APPROVE, HOLD, REPAIR]);
8911        }
8912        assert!(!repair_available(&repair_state(false, false)));
8913        let mut clean = repair_state(true, false);
8914        clean.reviews[0].clean = true;
8915        assert_eq!(&approval_choices(&clean)[..3], [APPROVE, HOLD, REPAIR]);
8916        assert_eq!(approval(Some(REPAIR)), Approval::Repair);
8917    }
8918
8919    #[tokio::test]
8920    async fn merge_repair_upgrades_legacy_questions_and_snapshots_exact_evidence_once() {
8921        let mut state = repair_state(true, false);
8922        let pr = green_pr();
8923        approval_gate(&mut state, &pr, "feat: x", None, "abc")
8924            .await
8925            .unwrap();
8926        let store = ask::Questions::open();
8927        let id = state.land_approval.as_ref().unwrap().question.clone();
8928        // Simulate an unanswered two-choice question created by an older build.
8929        store
8930            .update(&id, |q| {
8931                q.choices = vec![APPROVE.into(), HOLD.into()];
8932                Ok(())
8933            })
8934            .unwrap();
8935        let upgraded = store.get(&id).unwrap();
8936        assert_eq!(upgraded.choices, approval_choices(&state));
8937        assert!(!upgraded.actions.contains_key(HOLD));
8938        assert_eq!(store.get(&id).unwrap().choices, upgraded.choices);
8939        assert!(
8940            store
8941                .list()
8942                .iter()
8943                .any(|q| q.id == id && q.choices == upgraded.choices)
8944        );
8945        assert!(
8946            crate::prompt::chat_consult(&upgraded).contains("--reply 'fix findings and re-review'")
8947        );
8948        assert!(deputy_brief(&upgraded, Some(&state)).contains("never approves a merge"));
8949        store
8950            .update(&id, |q| q.answer(ask::Answer::Choice(REPAIR.into())))
8951            .unwrap();
8952        // An earlier build may have recorded the repair answer and stopped
8953        // before any driver consumed it. Reconcile that durable decision too.
8954        store
8955            .update(&id, |q| {
8956                q.actions.clear();
8957                Ok(())
8958            })
8959            .unwrap();
8960        assert_eq!(
8961            store.get(&id).unwrap().chosen_action(),
8962            Some(&ask::ChoiceAction::Resume {
8963                run: state.id.clone()
8964            })
8965        );
8966        let binding = state.land_approval.clone();
8967        assert_eq!(
8968            approval_gate(&mut state, &pr, "feat: x", None, "abc")
8969                .await
8970                .unwrap(),
8971            ApprovalGate::Repair
8972        );
8973        assert!(state.land_approval.is_none());
8974        assert_eq!(state.merge_repairs.len(), 1);
8975        let request = &state.merge_repairs[0];
8976        assert_eq!(request.question, id);
8977        assert_eq!(request.head, "abc");
8978        assert_eq!(request.pr, pr.url);
8979        assert_eq!(
8980            request.reviews[0].reviews[0].summary,
8981            "precise rejection reason"
8982        );
8983        assert_eq!(request.reviews[0].reviews[0].findings[0].id, "R3-1-1");
8984        assert_eq!(
8985            request.reviews[0].reviews[0].findings[0].detail,
8986            state.reviews[0].reviews[0].findings[0].detail
8987        );
8988        // A repeated consumption of the same answer never dispatches another repair.
8989        state.land_approval = binding;
8990        assert_eq!(
8991            approval_gate(&mut state, &pr, "feat: x", None, "abc")
8992                .await
8993                .unwrap(),
8994            ApprovalGate::Repair
8995        );
8996        assert_eq!(state.merge_repairs.len(), 1);
8997        state.land_approval = None;
8998        state.config.graph.land_approval = false;
8999        state.config.graph.hold_contested_merge = false;
9000        state.reviews.clear();
9001        assert_eq!(approval_reasons(&state), [ApprovalReason::AfterRepair]);
9002        assert_eq!(
9003            approval_gate(&mut state, &pr, "feat: x", None, "abc")
9004                .await
9005                .unwrap(),
9006            ApprovalGate::Pending
9007        );
9008        let fresh_id = &state.land_approval.as_ref().unwrap().question;
9009        assert_ne!(fresh_id, &id);
9010        let fresh = store.get(fresh_id).unwrap();
9011        assert!(
9012            fresh
9013                .detail
9014                .contains("fresh owner approval after merge repair")
9015        );
9016        assert!(fresh.detail.contains("No review recorded"));
9017        assert!(!fresh.detail.contains("precise rejection reason"));
9018    }
9019
9020    #[tokio::test]
9021    async fn merge_repair_answer_never_merges_and_hold_never_requests_repair() {
9022        for choice in [REPAIR, HOLD] {
9023            let mut state = repair_state(false, true);
9024            let observed = seen("abc", Checks::Green, "CLEAN", false);
9025            approval_gate(&mut state, &observed.pr, "feat: x", None, "abc")
9026                .await
9027                .unwrap();
9028            let id = state.land_approval.as_ref().unwrap().question.clone();
9029            ask::Questions::open()
9030                .update(&id, |q| q.answer(ask::Answer::Choice(choice.into())))
9031                .unwrap();
9032            let forge = Scripted::new(vec![observed], vec![(true, "")]);
9033            land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
9034                .await
9035                .unwrap();
9036            assert!(
9037                forge
9038                    .argvs()
9039                    .iter()
9040                    .all(|a| a.iter().any(|s| s == "--disable-auto"))
9041            );
9042            assert!(!forge.calls().contains(&"fix"));
9043            assert_eq!(state.merge_repairs.len(), usize::from(choice == REPAIR));
9044            assert_ne!(state.status, RunStatus::Merged);
9045        }
9046    }
9047
9048    #[test]
9049    fn only_the_merge_choice_merges_and_silence_holds() {
9050        let table = [
9051            (None, Approval::Hold),
9052            (Some("merge"), Approval::Merge),
9053            (Some(" merge\n"), Approval::Merge),
9054            (Some("hold"), Approval::Hold),
9055            (Some(""), Approval::Hold),
9056            (Some("yes"), Approval::Hold),
9057        ];
9058        for (answer, want) in table {
9059            assert_eq!(
9060                approval(answer),
9061                want,
9062                "answer {answer:?} must resolve to {want:?}"
9063            );
9064        }
9065    }
9066
9067    #[tokio::test]
9068    async fn a_first_visit_to_the_merge_gate_files_a_question_and_returns_pending_at_once() {
9069        let mut state = landing_state();
9070        state.config.graph.land_approval = true;
9071        let pr = green_pr();
9072
9073        let gate = approval_gate(&mut state, &pr, "feat: x", None, "abc")
9074            .await
9075            .unwrap();
9076        assert_eq!(gate, ApprovalGate::Pending, "nobody has answered yet");
9077        assert!(
9078            !state.parked,
9079            "approval_gate itself never sets `parked`; only its caller does"
9080        );
9081
9082        let store = ask::Questions::open();
9083        let filed: Vec<_> = store
9084            .list()
9085            .into_iter()
9086            .filter(|q| q.run == state.id)
9087            .collect();
9088        assert_eq!(filed.len(), 1, "exactly one question is filed");
9089        assert_eq!(filed[0].node, APPROVAL_NODE);
9090        assert_eq!(filed[0].choices, approval_choices(&state));
9091        assert!(filed[0].status.open());
9092
9093        // A second visit - standing in for a resumed run whose slot the
9094        // daemon handed to something else while nobody had answered - must
9095        // find the same question rather than filing a second one.
9096        let again = approval_gate(&mut state, &pr, "feat: x", None, "abc")
9097            .await
9098            .unwrap();
9099        assert_eq!(again, ApprovalGate::Pending);
9100        let still_one = store
9101            .list()
9102            .into_iter()
9103            .filter(|q| q.run == state.id)
9104            .count();
9105        assert_eq!(
9106            still_one, 1,
9107            "asking twice must not double-file the question"
9108        );
9109    }
9110
9111    #[tokio::test]
9112    async fn approving_the_existing_question_is_read_back_as_approved() {
9113        crate::run::pin_test_home();
9114        let mut state = run_state();
9115        state.config.graph.land_approval = true;
9116        let pr = green_pr();
9117        assert_eq!(
9118            approval_gate(&mut state, &pr, "feat: x", None, "abc")
9119                .await
9120                .unwrap(),
9121            ApprovalGate::Pending
9122        );
9123
9124        let store = ask::Questions::open();
9125        let mut q = store
9126            .list()
9127            .into_iter()
9128            .find(|q| q.run == state.id)
9129            .expect("filed above");
9130        let talks = crate::talk::Talks::open();
9131        let cfg = crate::config::Config {
9132            agents: vec![crate::config::AgentSpec {
9133                id: "mock".into(),
9134                kind: crate::config::AgentKind::Command,
9135                model: None,
9136                command: vec!["true".into()],
9137                extra_args: Vec::new(),
9138                env: Default::default(),
9139                prompt_delivery: None,
9140                max_turn_secs: None,
9141            }],
9142            ..crate::config::Config::default()
9143        };
9144        let mut talk = crate::talk::begin(&talks, &cfg, state.repo.clone(), Some("mock")).unwrap();
9145        crate::consult::begin(&store, &talks, &q, &talk).unwrap();
9146        crate::talk::drain(&mut talk, &talks).unwrap();
9147        assert_eq!(
9148            approval_gate(&mut state, &pr, "feat: x", None, "abc")
9149                .await
9150                .unwrap(),
9151            ApprovalGate::Pending,
9152            "consultation cannot approve or hold the merge"
9153        );
9154        q = store.get(&q.id).unwrap();
9155        assert!(q.answer.is_none());
9156        // The owner's merge answer (from the chat or the card) is the approval.
9157        q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
9158        store.put(&mut q).unwrap();
9159
9160        assert_eq!(
9161            approval_gate(&mut state, &pr, "feat: x", None, "abc")
9162                .await
9163                .unwrap(),
9164            ApprovalGate::Approved
9165        );
9166    }
9167
9168    #[tokio::test]
9169    async fn legacy_hold_actions_are_removed_on_get_and_list_before_answering() {
9170        crate::run::pin_test_home();
9171        let store = ask::Questions::open();
9172        for via_list in [false, true] {
9173            let mut state = repair_state(false, true);
9174            let pr = green_pr();
9175            approval_gate(&mut state, &pr, "feat: x", None, "abc")
9176                .await
9177                .unwrap();
9178            let id = state.land_approval.as_ref().unwrap().question.clone();
9179            store
9180                .update(&id, |q| {
9181                    q.actions.insert(
9182                        HOLD.to_owned(),
9183                        ask::ChoiceAction::Resume {
9184                            run: state.id.clone(),
9185                        },
9186                    );
9187                    Ok(())
9188                })
9189                .unwrap();
9190            let q = if via_list {
9191                store.list().into_iter().find(|q| q.id == id).unwrap()
9192            } else {
9193                store.get(&id).unwrap()
9194            };
9195            assert!(!q.actions.contains_key(HOLD));
9196            store
9197                .update(&id, |q| {
9198                    // Check the persisted migration, without another read upgrade.
9199                    assert!(!q.actions.contains_key(HOLD));
9200                    assert!(!upgrade_repair_choice(q, &state));
9201                    q.answer(ask::Answer::Choice(HOLD.to_owned()))
9202                })
9203                .unwrap();
9204            assert!(store.get(&id).unwrap().chosen_action().is_none());
9205            assert_eq!(
9206                approval_gate(&mut state, &pr, "feat: x", None, "abc")
9207                    .await
9208                    .unwrap(),
9209                ApprovalGate::Held
9210            );
9211        }
9212    }
9213
9214    #[tokio::test]
9215    async fn explicit_hold_stays_held_but_expired_approval_is_reasked() {
9216        crate::run::pin_test_home();
9217        let store = ask::Questions::open();
9218
9219        let mut held_state = run_state();
9220        held_state.config.graph.land_approval = true;
9221        let pr = green_pr();
9222        approval_gate(&mut held_state, &pr, "feat: x", None, "abc")
9223            .await
9224            .unwrap();
9225        let mut q = store
9226            .list()
9227            .into_iter()
9228            .find(|q| q.run == held_state.id)
9229            .expect("filed above");
9230        q.answer(ask::Answer::Choice(HOLD.to_owned())).unwrap();
9231        store.put(&mut q).unwrap();
9232        assert_eq!(
9233            approval_gate(&mut held_state, &pr, "feat: x", None, "abc")
9234                .await
9235                .unwrap(),
9236            ApprovalGate::Held
9237        );
9238
9239        let mut abandoned_state = run_state();
9240        abandoned_state.config.graph.land_approval = true;
9241        approval_gate(&mut abandoned_state, &pr, "feat: x", None, "abc")
9242            .await
9243            .unwrap();
9244        let mut q = store
9245            .list()
9246            .into_iter()
9247            .find(|q| q.run == abandoned_state.id)
9248            .expect("filed above");
9249        q.abandon("no answer within the timeout");
9250        store.put(&mut q).unwrap();
9251        assert_eq!(
9252            approval_gate(&mut abandoned_state, &pr, "feat: x", None, "abc")
9253                .await
9254                .unwrap(),
9255            ApprovalGate::Pending,
9256            "an expired approval must ask again without merging"
9257        );
9258        assert_eq!(
9259            store.get(&q.id).unwrap().status,
9260            ask::QuestionStatus::Abandoned
9261        );
9262        assert_ne!(
9263            abandoned_state.land_approval.as_ref().unwrap().question,
9264            q.id
9265        );
9266    }
9267
9268    fn contested() -> ContestedHandoff {
9269        let finding = |id: &str, n: u32| crate::verdict::Finding {
9270            id: id.to_owned(),
9271            severity: crate::verdict::Severity::Major,
9272            file: Some("src/a.rs".to_owned()),
9273            line: Some(n),
9274            title: format!("problem {id}"),
9275            detail: String::new(),
9276        };
9277        ContestedHandoff {
9278            findings: (1..=7).map(|n| finding(&format!("R3-1-{n}"), n)).collect(),
9279            rejecters: vec![(1, "alpha".to_owned())],
9280        }
9281    }
9282
9283    #[test]
9284    fn the_contested_record_is_asked_about_unless_the_switch_is_off() {
9285        let mut state = run_state();
9286        assert!(contested_to_ask(&state).is_none(), "nothing recorded");
9287        state.contested_handoff = Some(contested());
9288        assert!(contested_to_ask(&state).is_some());
9289        state.config.graph.hold_contested_merge = false;
9290        assert!(
9291            contested_to_ask(&state).is_none(),
9292            "the switch restores today"
9293        );
9294    }
9295
9296    #[test]
9297    fn the_deputy_brief_carries_the_pr_the_findings_and_names_what_is_missing() {
9298        let q = ask::Question::new(
9299            "run-1".to_owned(),
9300            APPROVAL_NODE.to_owned(),
9301            "land".to_owned(),
9302            "Merge?".to_owned(),
9303            String::new(),
9304            vec![APPROVE.to_owned(), HOLD.to_owned()],
9305        );
9306        let none = deputy_brief(&q, None);
9307        assert!(none.contains("could not be read"), "{none}");
9308        assert!(none.contains("Silence is a hold"), "{none}");
9309
9310        let mut state = run_state();
9311        state.pr = Some(crate::run::PrRecord {
9312            url: "https://example.test/pull/7".to_owned(),
9313            number: 7,
9314            state: "open".to_owned(),
9315            checks: "green".to_owned(),
9316            round: 0,
9317            rounds: 3,
9318            red_at_merge: Vec::new(),
9319        });
9320        state.contested_handoff = Some(contested());
9321        let b = deputy_brief(&q, Some(&state));
9322        assert!(b.contains("https://example.test/pull/7"), "{b}");
9323        assert!(b.contains("not recorded in this legacy question"), "{b}");
9324        assert!(!b.contains("This question was filed although"));
9325        state.contested_handoff = None;
9326        assert!(deputy_brief(&q, Some(&state)).contains("not recorded in this legacy question"));
9327    }
9328
9329    #[test]
9330    fn the_contested_question_names_the_pr_the_findings_and_the_rejecter() {
9331        for lang in ["en", "ja"] {
9332            let mut cfg = crate::config::Config::default();
9333            cfg.graph.language = lang.to_owned();
9334            let w = words(&cfg.graph.language);
9335            let text = w.approval_detail(
9336                "https://github.com/yukimemi/magi/pull/42",
9337                "main",
9338                "feat: x",
9339                Some(&contested()),
9340            );
9341            assert!(text.contains("pull/42"), "{text}");
9342            assert!(
9343                text.contains("R3-1-1 major src/a.rs:1: problem R3-1-1"),
9344                "{text}"
9345            );
9346            assert!(text.contains("R3-1-5"), "{text}");
9347            assert!(!text.contains("R3-1-6"), "the list is capped: {text}");
9348            assert!(text.contains("2"), "the rest are counted: {text}");
9349            assert!(text.contains("#1 (alpha)"), "{text}");
9350        }
9351        let plain = words("en").approval_detail("u", "main", "s", None);
9352        assert!(!plain.contains("reject"), "{plain}");
9353    }
9354
9355    #[tokio::test]
9356    async fn a_contested_question_is_filed_once_and_a_resume_finds_the_same_one() {
9357        crate::run::pin_test_home();
9358        let mut state = run_state();
9359        state.config.graph.land_approval = false;
9360        state.contested_handoff = Some(contested());
9361        let pr = green_pr();
9362        let c = contested_to_ask(&state);
9363        assert_eq!(
9364            approval_gate(&mut state, &pr, "feat: x", c.as_ref(), "abc")
9365                .await
9366                .unwrap(),
9367            ApprovalGate::Pending,
9368            "silence is a hold"
9369        );
9370        let store = ask::Questions::open();
9371        let filed: Vec<_> = store
9372            .list()
9373            .into_iter()
9374            .filter(|q| q.run == state.id)
9375            .collect();
9376        assert_eq!(filed.len(), 1);
9377        assert!(filed[0].detail.contains("R3-1-1"), "{}", filed[0].detail);
9378
9379        assert_eq!(
9380            approval_gate(&mut state, &pr, "feat: x", c.as_ref(), "abc")
9381                .await
9382                .unwrap(),
9383            ApprovalGate::Pending
9384        );
9385        let mut q = store
9386            .list()
9387            .into_iter()
9388            .find(|q| q.run == state.id)
9389            .unwrap();
9390        assert_eq!(q.id, filed[0].id, "the same question after a resume");
9391        q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
9392        store.put(&mut q).unwrap();
9393        assert_eq!(
9394            approval_gate(&mut state, &pr, "feat: x", c.as_ref(), "abc")
9395                .await
9396                .unwrap(),
9397            ApprovalGate::Approved
9398        );
9399    }
9400
9401    #[test]
9402    fn the_diffstat_table_is_ordered_by_churn_with_binaries_last() {
9403        let rows = parse_numstat(NUMSTAT);
9404        assert_eq!(
9405            rows.iter().map(|r| r.path.as_str()).collect::<Vec<_>>(),
9406            ["src/web.rs", "src/land.rs", "assets/logo.png"]
9407        );
9408        assert_eq!(rows[2].added, None, "a binary file has no line counts");
9409    }
9410    #[test]
9411    fn the_approval_speaks_the_language_the_repository_is_configured_for() {
9412        // Reported from a real run: the merge question arrived in English on a
9413        // repository with `language = "ja"`. magi's own strings have to follow
9414        // that setting too - "it is a literal in Rust" is not an answer.
9415        let mut state = run_state();
9416        state.config.graph.language = "ja".to_owned();
9417        let pr = green_pr();
9418        let commits = ["c1".to_owned()];
9419
9420        let ja = approval_panel(&state, &pr, "3\t1\tsrc/a.rs", "+ x", &commits, "feat: x");
9421        assert!(ja.contains("lang=\"ja\""), "the document must declare it");
9422        assert!(ja.contains("squash されるコミット"), "{ja}");
9423        assert!(ja.contains("レビューコメント"), "{ja}");
9424        assert!(ja.contains("差分"), "{ja}");
9425        assert!(
9426            !ja.contains("Commits being squashed"),
9427            "no English left over"
9428        );
9429
9430        let w = words("ja");
9431        assert!(w.approval_summary(17, "feat: x").contains("マージ"));
9432        assert!(
9433            w.approval_detail("http://x/1", "main", "feat: x", None)
9434                .contains("パネル")
9435        );
9436
9437        // The evidence itself is language-neutral and must survive either way.
9438        assert!(ja.contains("src/a.rs"), "the diffstat is not prose");
9439        assert!(ja.contains("feat: x"), "nor is the merge subject");
9440
9441        // English stays the default, and a language magi cannot check falls
9442        // back to it rather than shipping a guess.
9443        state.config.graph.language = "en".to_owned();
9444        let en = approval_panel(&state, &pr, "3\t1\tsrc/a.rs", "+ x", &commits, "feat: x");
9445        assert!(en.contains("Commits being squashed"), "{en}");
9446        assert_eq!(words("Klingon").html_lang, "en");
9447    }
9448
9449    /// A `gh pr list` result naming exactly one pull request whose base and
9450    /// merge time both fit the run is exactly the case
9451    /// [`find_external_merge`] exists to act on.
9452    #[test]
9453    fn pick_open_pr_classifies_by_count_and_base() {
9454        let one = r#"[{"number":58,"url":"https://x/pull/58","title":"t","baseRefName":"main"}]"#;
9455        assert_eq!(
9456            pick_open_pr(one, "main").unwrap(),
9457            OpenPr::One {
9458                url: "https://x/pull/58".into(),
9459                title: "t".into()
9460            }
9461        );
9462        assert_eq!(pick_open_pr("[]", "main").unwrap(), OpenPr::None);
9463        assert_eq!(pick_open_pr(one, "dev").unwrap(), OpenPr::None);
9464        let two = r#"[{"number":1,"url":"u1","title":"","baseRefName":"main"},
9465                     {"number":2,"url":"u2","title":"","baseRefName":"main"}]"#;
9466        assert_eq!(
9467            pick_open_pr(two, "main").unwrap(),
9468            OpenPr::Many(vec!["u1".into(), "u2".into()])
9469        );
9470        // A fork's same-named branch is not a pull request for this head.
9471        let fork = r#"[{"url":"f","baseRefName":"main","isCrossRepository":true}]"#;
9472        assert_eq!(pick_open_pr(fork, "main").unwrap(), OpenPr::None);
9473        assert!(pick_open_pr("not json", "main").is_err());
9474        // An incomplete record is an error, never "nothing open".
9475        assert!(pick_open_pr(r#"[{"url":"u","title":"t"}]"#, "main").is_err());
9476        assert!(pick_open_pr(r#"[{"title":"t","baseRefName":"main"}]"#, "main").is_err());
9477    }
9478
9479    #[test]
9480    fn pick_merged_pr_picks_the_unique_match() {
9481        let json = r#"[
9482            {"url": "https://github.com/o/r/pull/42", "number": 42,
9483             "mergedAt": "2026-09-20T10:00:00Z", "baseRefName": "main"}
9484        ]"#;
9485        let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
9486        let found = pick_merged_pr(json, "main", created_at)
9487            .expect("valid json")
9488            .expect("one unambiguous match");
9489        assert_eq!(found.url, "https://github.com/o/r/pull/42");
9490        assert_eq!(found.number, 42);
9491    }
9492
9493    /// Two candidates surviving the filter is exactly as uninformative as
9494    /// zero — a branch name can be reused across runs — so neither is
9495    /// preferred over the other and nothing is recorded automatically.
9496    #[test]
9497    fn pick_merged_pr_refuses_when_more_than_one_candidate_survives() {
9498        let json = r#"[
9499            {"url": "https://github.com/o/r/pull/42", "number": 42,
9500             "mergedAt": "2026-09-20T10:00:00Z", "baseRefName": "main"},
9501            {"url": "https://github.com/o/r/pull/43", "number": 43,
9502             "mergedAt": "2026-09-21T10:00:00Z", "baseRefName": "main"}
9503        ]"#;
9504        let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
9505        assert_eq!(pick_merged_pr(json, "main", created_at).unwrap(), None);
9506    }
9507
9508    /// A pull request that targets a different base branch cannot be this
9509    /// run's, whatever its head branch is named — a reused branch name from
9510    /// an unrelated task must not be recorded as this run's merge.
9511    #[test]
9512    fn pick_merged_pr_ignores_a_different_base_branch() {
9513        let json = r#"[
9514            {"url": "https://github.com/o/r/pull/42", "number": 42,
9515             "mergedAt": "2026-09-20T10:00:00Z", "baseRefName": "release"}
9516        ]"#;
9517        let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
9518        assert_eq!(pick_merged_pr(json, "main", created_at).unwrap(), None);
9519    }
9520
9521    /// A pull request merged before this run was even created cannot be this
9522    /// run's winner, no matter how its head branch is spelled.
9523    #[test]
9524    fn pick_merged_pr_ignores_a_merge_that_predates_the_run() {
9525        let json = r#"[
9526            {"url": "https://github.com/o/r/pull/42", "number": 42,
9527             "mergedAt": "2026-09-18T10:00:00Z", "baseRefName": "main"}
9528        ]"#;
9529        let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
9530        assert_eq!(pick_merged_pr(json, "main", created_at).unwrap(), None);
9531    }
9532
9533    #[test]
9534    fn slug_of_pr_url_reads_host_owner_and_repo() {
9535        assert_eq!(
9536            slug_of_pr_url("https://github.com/yukimemi/shun/pull/272").as_deref(),
9537            Some("github.com/yukimemi/shun")
9538        );
9539    }
9540
9541    #[test]
9542    fn slug_of_pr_url_refuses_a_url_with_no_pull_segment() {
9543        assert_eq!(slug_of_pr_url("https://github.com/yukimemi/shun"), None);
9544        assert_eq!(slug_of_pr_url("not a url at all"), None);
9545        assert_eq!(slug_of_pr_url("https://github.com"), None);
9546    }
9547
9548    #[test]
9549    fn slug_of_repo_url_reads_host_owner_and_repo() {
9550        assert_eq!(
9551            slug_of_repo_url("https://github.com/yukimemi/magi").as_deref(),
9552            Some("github.com/yukimemi/magi")
9553        );
9554        assert_eq!(slug_of_repo_url("https://github.com"), None);
9555    }
9556
9557    #[test]
9558    fn ensure_same_repo_accepts_a_matching_slug_regardless_of_case() {
9559        ensure_same_repo("github.com/yukimemi/magi", "GitHub.Com/YukiMemi/Magi")
9560            .expect("same repo, different case");
9561    }
9562
9563    /// The shun/8c75 incident: an id-less `--merged` picked this repository's
9564    /// own in-progress run and rewrote its status from a pull request in a
9565    /// completely different repository. This is the guard that must catch
9566    /// that even when an explicit (but wrong) id is given.
9567    #[test]
9568    fn ensure_same_repo_refuses_a_different_repo() {
9569        let err =
9570            ensure_same_repo("github.com/yukimemi/magi", "github.com/yukimemi/shun").unwrap_err();
9571        let msg = format!("{err:#}");
9572        assert!(msg.contains("github.com/yukimemi/magi"), "{msg}");
9573        assert!(msg.contains("github.com/yukimemi/shun"), "{msg}");
9574    }
9575
9576    /// Same owner/repo on two different forge hosts (a GitHub Enterprise
9577    /// instance mirroring a `github.com` repository's name, say) must not be
9578    /// treated as the same repository just because the trailing path
9579    /// matches.
9580    #[test]
9581    fn ensure_same_repo_refuses_the_same_slug_on_a_different_host() {
9582        let err = ensure_same_repo(
9583            "github.com/yukimemi/magi",
9584            "github.example.com/yukimemi/magi",
9585        )
9586        .unwrap_err();
9587        let msg = format!("{err:#}");
9588        assert!(msg.contains("github.com/yukimemi/magi"), "{msg}");
9589        assert!(msg.contains("github.example.com/yukimemi/magi"), "{msg}");
9590    }
9591
9592    /// No winner decided yet means there is no branch to ask GitHub about at
9593    /// all — `find_external_merge` must return `None` without ever spawning
9594    /// `gh`, which this proves by never providing a real repository to spawn
9595    /// it in.
9596    #[tokio::test]
9597    async fn find_external_merge_returns_none_without_a_winner() {
9598        let state = RunState::new(
9599            PathBuf::from("/no/such/repo"),
9600            "main".to_owned(),
9601            "0000000000000000000000000000000000000000".to_owned(),
9602            "irrelevant".to_owned(),
9603            crate::config::Config::default(),
9604        );
9605        assert_eq!(find_external_merge(&state).await.unwrap(), None);
9606    }
9607
9608    fn pr_run(home: &Path, id: &str, repo: &str, status: RunStatus, url: &str, state: &str) {
9609        let mut run = RunState::new(
9610            PathBuf::from(repo),
9611            "main".to_owned(),
9612            "abcdef1234".to_owned(),
9613            "x".to_owned(),
9614            crate::config::Config::default(),
9615        );
9616        run.id = id.to_owned();
9617        run.status = status;
9618        run.driver_exited = true;
9619        run.pr = Some(crate::run::PrRecord {
9620            number: url.rsplit('/').next().unwrap().parse().unwrap(),
9621            url: url.to_owned(),
9622            state: state.to_owned(),
9623            checks: "red".to_owned(),
9624            round: 0,
9625            rounds: 2,
9626            red_at_merge: Vec::new(),
9627        });
9628        run.save_under(home).unwrap();
9629    }
9630
9631    fn recorded(home: &Path, id: &str) -> String {
9632        let body = std::fs::read_to_string(home.join("runs").join(id).join("run.json")).unwrap();
9633        serde_json::from_str::<RunState>(&body)
9634            .unwrap()
9635            .pr
9636            .unwrap()
9637            .state
9638    }
9639
9640    const PR: &str = "https://github.com/o/r/pull/7";
9641
9642    #[test]
9643    fn write_through_updates_predecessors_and_siblings_only() {
9644        let tmp = tempfile::tempdir().unwrap();
9645        let h = tmp.path();
9646        pr_run(
9647            h,
9648            "20261004-100000-aaaa",
9649            "/repo/r",
9650            RunStatus::Superseded,
9651            PR,
9652            "open",
9653        );
9654        pr_run(
9655            h,
9656            "20261004-100100-bbbb",
9657            "/repo/r",
9658            RunStatus::Blocked,
9659            PR,
9660            "open",
9661        );
9662        // Not terminal: a driver may be writing it.
9663        pr_run(
9664            h,
9665            "20261004-100200-cccc",
9666            "/repo/r",
9667            RunStatus::Landing,
9668            PR,
9669            "open",
9670        );
9671        // Another repository's pull request with the same number.
9672        pr_run(
9673            h,
9674            "20261004-100300-dddd",
9675            "/repo/other",
9676            RunStatus::Blocked,
9677            "https://github.com/o/other/pull/7",
9678            "open",
9679        );
9680        // A different pull request of the same repository.
9681        pr_run(
9682            h,
9683            "20261004-100400-eeee",
9684            "/repo/r",
9685            RunStatus::Blocked,
9686            "https://github.com/o/r/pull/8",
9687            "open",
9688        );
9689        pr_run(
9690            h,
9691            "20261004-100500-ffff",
9692            "/repo/r",
9693            RunStatus::Merged,
9694            PR,
9695            "open",
9696        );
9697        let source = RunState::load_under("20261004-100500-ffff", h).unwrap();
9698
9699        assert_eq!(
9700            write_pr_state_through_in(h, &source, PrLifecycle::Merged),
9701            2
9702        );
9703        assert_eq!(recorded(h, "20261004-100000-aaaa"), "merged");
9704        assert_eq!(recorded(h, "20261004-100100-bbbb"), "merged");
9705        assert_eq!(recorded(h, "20261004-100200-cccc"), "open");
9706        assert_eq!(recorded(h, "20261004-100300-dddd"), "open");
9707        assert_eq!(recorded(h, "20261004-100400-eeee"), "open");
9708        // The source's own record is the caller's to write.
9709        assert_eq!(recorded(h, "20261004-100500-ffff"), "open");
9710        // Idempotent.
9711        assert_eq!(
9712            write_pr_state_through_in(h, &source, PrLifecycle::Merged),
9713            0
9714        );
9715        let hit = RunState::load_under("20261004-100000-aaaa", h).unwrap();
9716        assert!(hit.events.iter().any(|e| e.message.contains("merged")));
9717    }
9718
9719    #[test]
9720    fn lifecycle_updates_preserve_live_and_unknown_terminal_drivers() {
9721        let home = tempfile::tempdir().unwrap();
9722        let live_id = "20261004-100000-aaaa";
9723        let unknown_id = "20261004-100100-bbbb";
9724        for id in [live_id, unknown_id] {
9725            pr_run(home.path(), id, "repo", RunStatus::Ready, PR, "open");
9726            let mut s = RunState::load_under(id, home.path()).unwrap();
9727            s.driver_exited = false;
9728            // Model legacy driver evidence, not a newly reserved unstarted run.
9729            s.driver_claimed = None;
9730            if id == live_id {
9731                s.driver_pid = Some(std::process::id());
9732                s.driver_started_at = crate::proc::process_started_at(std::process::id());
9733            }
9734            s.save_under(home.path()).unwrap();
9735            if id == unknown_id {
9736                assert_eq!(
9737                    s.liveness_under(home.path(), false),
9738                    crate::run::Liveness::Unknown
9739                );
9740            }
9741        }
9742        let known = BTreeMap::from([(PR.to_owned(), PrLifecycle::Merged)]);
9743        assert_eq!(apply_pr_states(home.path(), &known), 0);
9744        for id in [live_id, unknown_id] {
9745            assert_eq!(recorded(home.path(), id), "open");
9746        }
9747    }
9748
9749    #[test]
9750    fn repair_rewrites_merged_and_closed_and_leaves_open_and_unknown() {
9751        let tmp = tempfile::tempdir().unwrap();
9752        let h = tmp.path();
9753        let url = |n: u32| format!("https://github.com/o/r/pull/{n}");
9754        pr_run(
9755            h,
9756            "20261004-100000-aaaa",
9757            "/repo/r",
9758            RunStatus::Superseded,
9759            &url(1),
9760            "open",
9761        );
9762        pr_run(
9763            h,
9764            "20261004-100100-bbbb",
9765            "/repo/r",
9766            RunStatus::Blocked,
9767            &url(2),
9768            "open",
9769        );
9770        pr_run(
9771            h,
9772            "20261004-100200-cccc",
9773            "/repo/r",
9774            RunStatus::Ready,
9775            &url(3),
9776            "open",
9777        );
9778        pr_run(
9779            h,
9780            "20261004-100300-dddd",
9781            "/repo/r",
9782            RunStatus::Ready,
9783            &url(4),
9784            "open",
9785        );
9786        pr_run(
9787            h,
9788            "20261004-100400-eeee",
9789            "/repo/r",
9790            RunStatus::Implementing,
9791            &url(1),
9792            "open",
9793        );
9794        assert_eq!(stale_open_prs(h).len(), 4);
9795
9796        let mut known = BTreeMap::new();
9797        known.insert(url(1), PrLifecycle::Merged);
9798        known.insert(url(2), PrLifecycle::Closed);
9799        known.insert(url(3), PrLifecycle::Open);
9800        // #4: the forge could not be read, so it has no answer.
9801        assert_eq!(apply_pr_states(h, &known), 2);
9802        assert_eq!(recorded(h, "20261004-100000-aaaa"), "merged");
9803        assert_eq!(recorded(h, "20261004-100100-bbbb"), "closed");
9804        assert_eq!(recorded(h, "20261004-100200-cccc"), "open");
9805        assert_eq!(recorded(h, "20261004-100300-dddd"), "open");
9806        assert_eq!(recorded(h, "20261004-100400-eeee"), "open");
9807        assert_eq!(apply_pr_states(h, &known), 0);
9808    }
9809
9810    // --- the land loop against a scripted forge -------------------------
9811
9812    use std::collections::VecDeque;
9813    use std::sync::Mutex;
9814
9815    /// Answers views from a script (the last one repeats), merges from a
9816    /// queue, and records every call so a test can assert the order.
9817    struct Scripted {
9818        evidence: Mutex<Option<CiEvidence>>,
9819        views: Mutex<VecDeque<Seen>>,
9820        merges: Mutex<VecDeque<(bool, String)>>,
9821        fix: Mutex<Option<Fixed>>,
9822        log: Mutex<Vec<&'static str>>,
9823        argvs: Mutex<Vec<Vec<String>>>,
9824        required: Mutex<Option<BTreeSet<String>>>,
9825        /// Set once a merge answered ok: the forge then reports `merged`,
9826        /// unless `queued` says the merge only entered a queue.
9827        merged: Mutex<bool>,
9828        queued: Mutex<bool>,
9829        /// Views fail once a merge answered ok.
9830        unreadable_after_merge: Mutex<bool>,
9831        /// Workflow runs asked to rerun, and whether that call fails.
9832        reruns: Mutex<Vec<String>>,
9833        rerun_fails: Mutex<bool>,
9834        /// Unix seconds; every poll moves it on by `POLL`.
9835        clock: Mutex<i64>,
9836    }
9837
9838    impl Scripted {
9839        fn new(views: Vec<Seen>, merges: Vec<(bool, &str)>) -> Self {
9840            Self {
9841                evidence: Mutex::new(None),
9842                views: Mutex::new(views.into()),
9843                merges: Mutex::new(
9844                    merges
9845                        .into_iter()
9846                        .map(|(ok, m)| (ok, m.to_owned()))
9847                        .collect(),
9848                ),
9849                fix: Mutex::new(None),
9850                log: Mutex::new(Vec::new()),
9851                argvs: Mutex::new(Vec::new()),
9852                required: Mutex::new(None),
9853                merged: Mutex::new(false),
9854                queued: Mutex::new(false),
9855                unreadable_after_merge: Mutex::new(false),
9856                reruns: Mutex::new(Vec::new()),
9857                rerun_fails: Mutex::new(false),
9858                clock: Mutex::new(1_000),
9859            }
9860        }
9861        fn argvs(&self) -> Vec<Vec<String>> {
9862            self.argvs.lock().unwrap().clone()
9863        }
9864        fn calls(&self) -> Vec<&'static str> {
9865            self.log.lock().unwrap().clone()
9866        }
9867    }
9868
9869    impl Forge for Scripted {
9870        async fn attribution(&self, _state: &RunState, _seen: &Seen) -> Option<CiEvidence> {
9871            self.evidence.lock().unwrap().clone()
9872        }
9873        async fn view(&self, _repo: &Path, _url: &str) -> Result<Seen> {
9874            self.log.lock().unwrap().push("view");
9875            if *self.unreadable_after_merge.lock().unwrap()
9876                && !self.argvs.lock().unwrap().is_empty()
9877            {
9878                anyhow::bail!("forge unreachable");
9879            }
9880            let mut v = self.views.lock().unwrap();
9881            let mut seen = if v.len() > 1 {
9882                v.pop_front().unwrap()
9883            } else {
9884                v[0].clone()
9885            };
9886            if *self.merged.lock().unwrap() {
9887                seen.pr.state = PrLifecycle::Merged;
9888            }
9889            Ok(seen)
9890        }
9891        async fn merge(&self, _repo: &Path, argv: &[String]) -> Result<(bool, String)> {
9892            self.log.lock().unwrap().push("merge");
9893            self.argvs.lock().unwrap().push(argv.to_vec());
9894            let out = self
9895                .merges
9896                .lock()
9897                .unwrap()
9898                .pop_front()
9899                .expect("unscripted merge");
9900            if out.0 && !*self.queued.lock().unwrap() && !argv.iter().any(|a| a == "--disable-auto")
9901            {
9902                *self.merged.lock().unwrap() = true;
9903            }
9904            Ok(out)
9905        }
9906        async fn poll(&self) {
9907            self.log.lock().unwrap().push("poll");
9908            *self.clock.lock().unwrap() += POLL.as_secs() as i64;
9909        }
9910        async fn rerun(&self, _repo: &Path, run: &str) -> Result<()> {
9911            self.log.lock().unwrap().push("rerun");
9912            self.reruns.lock().unwrap().push(run.to_owned());
9913            if *self.rerun_fails.lock().unwrap() {
9914                anyhow::bail!("gh run rerun: no");
9915            }
9916            Ok(())
9917        }
9918        fn now(&self) -> i64 {
9919            *self.clock.lock().unwrap()
9920        }
9921        async fn required_contexts(&self, _repo: &Path, _base: &str) -> Option<BTreeSet<String>> {
9922            self.required.lock().unwrap().clone()
9923        }
9924        async fn fix(
9925            &self,
9926            _state: &mut RunState,
9927            _pr: &PrState,
9928            _round: usize,
9929            _budget: usize,
9930            _reason: &str,
9931            _logs: &str,
9932        ) -> Result<Fixed> {
9933            self.log.lock().unwrap().push("fix");
9934            Ok(self.fix.lock().unwrap().take().expect("unscripted fix"))
9935        }
9936    }
9937
9938    const REFUSED: &str =
9939        "X Pull request #42 is not mergeable: the base branch policy prohibits the merge.";
9940
9941    fn seen(head: &str, checks: Checks, merge_state: &str, comments: bool) -> Seen {
9942        let mut pr = green_pr();
9943        pr.checks = checks;
9944        pr.blocking = Blocking::of(merge_state);
9945        if !comments {
9946            pr.review_comments.clear();
9947        }
9948        Seen {
9949            pr,
9950            title: "feat: x".to_owned(),
9951            failing_urls: Vec::new(),
9952            head: head.to_owned(),
9953            rollup_head: head.to_owned(),
9954            merge_state: merge_state.to_owned(),
9955            contexts: Vec::new(),
9956            base: "main".to_owned(),
9957        }
9958    }
9959
9960    fn landing_state() -> RunState {
9961        crate::run::pin_test_home();
9962        let mut state = run_state();
9963        state.config.graph.land_approval = false;
9964        state
9965    }
9966
9967    #[test]
9968    fn a_pushed_head_is_awaited_case_insensitively_and_an_unreadable_one_is_not_a_match() {
9969        assert!(!awaiting_new_head(None, "aaa"));
9970        assert!(!awaiting_new_head(Some("abc123"), "ABC123"));
9971        assert!(awaiting_new_head(Some("abc123"), "def456"));
9972        assert!(awaiting_new_head(Some("abc123"), ""));
9973    }
9974
9975    #[test]
9976    fn a_refusal_is_judged_by_the_pull_requests_state_not_by_its_wording() {
9977        let open = |c, m: &str| seen("a", c, m, false);
9978        let table = [
9979            (None, false, Refused::Pending),
9980            (
9981                Some(open(Checks::Pending, "BLOCKED")),
9982                false,
9983                Refused::Pending,
9984            ),
9985            (
9986                Some(open(Checks::Unknown, "BLOCKED")),
9987                false,
9988                Refused::Pending,
9989            ),
9990            (
9991                Some(open(Checks::Green, "UNKNOWN")),
9992                false,
9993                Refused::Pending,
9994            ),
9995            (Some(open(Checks::Green, "")), false, Refused::Pending),
9996            (
9997                Some(open(Checks::Green, "BLOCKED")),
9998                false,
9999                Refused::Recheck,
10000            ),
10001            (Some(open(Checks::Green, "BLOCKED")), true, Refused::Final),
10002        ];
10003        for (after, rechecked, want) in table {
10004            assert_eq!(
10005                classify_refusal(after.as_ref(), rechecked, "a", CiExpectation::Expected),
10006                want
10007            );
10008        }
10009        let mut closed = open(Checks::Green, "CLEAN");
10010        closed.pr.state = PrLifecycle::Closed;
10011        assert_eq!(
10012            classify_refusal(Some(&closed), false, "a", CiExpectation::Expected),
10013            Refused::Final
10014        );
10015    }
10016
10017    #[tokio::test]
10018    async fn a_normal_landing_merges_on_the_first_look() {
10019        let mut state = landing_state();
10020        let forge = Scripted::new(
10021            vec![seen("a", Checks::Green, "CLEAN", false)],
10022            vec![(true, "")],
10023        );
10024        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10025            .await
10026            .unwrap();
10027        // One fresh read, then the head-bound merge.
10028        assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
10029        assert_eq!(state.status, RunStatus::Merged);
10030    }
10031
10032    #[tokio::test]
10033    async fn a_successful_merge_command_that_only_queued_is_not_a_merge() {
10034        let mut state = landing_state();
10035        let forge = Scripted::new(
10036            vec![seen("a", Checks::Green, "CLEAN", false)],
10037            std::iter::repeat_n((true, ""), 100).collect(),
10038        );
10039        *forge.queued.lock().unwrap() = true;
10040        let task = async {
10041            land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10042                .await
10043                .unwrap();
10044        };
10045        // Still open after the command succeeded: it keeps watching and
10046        // never records a merge (it stops at the wait ceiling instead).
10047        task.await;
10048        assert_ne!(state.status, RunStatus::Merged);
10049    }
10050
10051    #[tokio::test]
10052    async fn an_unreadable_forge_after_a_merge_command_is_not_a_confirmation() {
10053        let mut state = landing_state();
10054        let forge = Scripted::new(
10055            vec![seen("a", Checks::Green, "CLEAN", false)],
10056            std::iter::repeat_n((true, ""), 100).collect(),
10057        );
10058        *forge.unreadable_after_merge.lock().unwrap() = true;
10059        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10060            .await
10061            .ok();
10062        assert_ne!(state.status, RunStatus::Merged);
10063    }
10064
10065    #[tokio::test]
10066    async fn a_task_public_head_is_awaited_on_land_entry() {
10067        let mut state = landing_state();
10068        state.pr_head = Some("magi/task-abcd-1234".into());
10069        state.pr_pushed_head = Some("new".into());
10070        let forge = Scripted::new(
10071            vec![
10072                seen("old", Checks::Green, "CLEAN", false),
10073                seen("new", Checks::Green, "CLEAN", false),
10074            ],
10075            vec![(true, "")],
10076        );
10077        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10078            .await
10079            .unwrap();
10080        assert_eq!(
10081            forge.calls(),
10082            ["view", "poll", "view", "view", "merge", "view"]
10083        );
10084        assert_eq!(state.status, RunStatus::Merged);
10085    }
10086
10087    #[tokio::test]
10088    async fn a_superseded_public_head_owner_cannot_reenter_land() {
10089        let mut state = landing_state();
10090        state.pr_taken_over_by = Some("next".into());
10091        let forge = Scripted::new(vec![seen("a", Checks::Green, "CLEAN", false)], vec![]);
10092        assert!(
10093            land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10094                .await
10095                .is_err()
10096        );
10097        assert!(forge.calls().is_empty());
10098    }
10099
10100    #[tokio::test]
10101    async fn after_a_pushed_fix_no_merge_is_tried_until_the_head_matches() {
10102        let mut state = landing_state();
10103        let forge = Scripted::new(
10104            vec![
10105                seen("old", Checks::Green, "CLEAN", true),
10106                // The forge has not moved to the new head yet: still green.
10107                seen("old", Checks::Green, "CLEAN", true),
10108                seen("new", Checks::Pending, "BLOCKED", true),
10109                seen("new", Checks::Green, "CLEAN", true),
10110            ],
10111            vec![(true, "")],
10112        );
10113        *forge.fix.lock().unwrap() = Some(Fixed::Committed {
10114            head: "NEW".to_owned(),
10115        });
10116        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10117            .await
10118            .unwrap();
10119        assert_eq!(
10120            forge.calls(),
10121            [
10122                "view", "fix", "poll", "view", "poll", "view", "poll", "view", "view", "merge",
10123                "view"
10124            ]
10125        );
10126        assert_eq!(state.status, RunStatus::Merged);
10127    }
10128
10129    #[tokio::test]
10130    async fn a_head_that_never_arrives_stops_naming_both_commits() {
10131        let mut state = landing_state();
10132        let forge = Scripted::new(
10133            vec![
10134                seen("old", Checks::Green, "CLEAN", true),
10135                seen("someone-elses", Checks::Green, "CLEAN", true),
10136            ],
10137            vec![],
10138        );
10139        *forge.fix.lock().unwrap() = Some(Fixed::Committed {
10140            head: "mine".to_owned(),
10141        });
10142        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10143            .await
10144            .unwrap();
10145        assert!(!forge.calls().contains(&"merge"));
10146        let why = state.merge.as_ref().unwrap().detail.clone();
10147        assert!(
10148            why.contains("mine") && why.contains("someone-elses"),
10149            "{why}"
10150        );
10151        assert_eq!(state.status, RunStatus::Blocked);
10152    }
10153
10154    #[tokio::test]
10155    async fn a_policy_refusal_while_checks_run_waits_and_then_merges() {
10156        let mut state = landing_state();
10157        let forge = Scripted::new(
10158            vec![
10159                seen("a", Checks::Green, "CLEAN", false),
10160                seen("a", Checks::Green, "CLEAN", false),
10161                seen("a", Checks::Pending, "BLOCKED", false),
10162                seen("a", Checks::Pending, "BLOCKED", false),
10163                seen("a", Checks::Green, "CLEAN", false),
10164            ],
10165            vec![(false, REFUSED), (true, "")],
10166        );
10167        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10168            .await
10169            .unwrap();
10170        assert_eq!(
10171            forge.calls(),
10172            [
10173                "view", "view", "merge", "view", "poll", "view", "poll", "view", "view", "merge",
10174                "view"
10175            ]
10176        );
10177        assert_eq!(state.status, RunStatus::Merged);
10178    }
10179
10180    #[tokio::test]
10181    async fn a_refusal_that_outlives_settled_checks_stops_with_the_merge_state() {
10182        let mut state = landing_state();
10183        let forge = Scripted::new(
10184            vec![
10185                seen("a", Checks::Green, "CLEAN", false),
10186                seen("a", Checks::Green, "BLOCKED", false),
10187            ],
10188            vec![(false, REFUSED), (false, REFUSED)],
10189        );
10190        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10191            .await
10192            .unwrap();
10193        // One re-look is allowed for the forge's own lag, then it is final.
10194        assert_eq!(forge.calls().iter().filter(|c| **c == "merge").count(), 2);
10195        let why = state.merge.as_ref().unwrap().detail.clone();
10196        assert!(
10197            why.contains("policy prohibits") && why.contains("BLOCKED") && why.contains("refused"),
10198            "{why}"
10199        );
10200        assert_eq!(state.status, RunStatus::Blocked);
10201    }
10202
10203    #[test]
10204    fn a_decision_is_bound_to_a_head_only_when_every_signal_agrees() {
10205        assert_eq!(bound_head("abc", "abc", None), Some("abc"));
10206        assert_eq!(bound_head("abc", "ABC", Some("abc")), Some("abc"));
10207        // The pull request is still on the commit before the push.
10208        assert_eq!(bound_head("old", "old", Some("new")), None);
10209        // The checks are the previous commit's.
10210        assert_eq!(bound_head("new", "old", Some("new")), None);
10211        assert_eq!(bound_head("new", "old", None), None);
10212        // Nothing readable.
10213        assert_eq!(bound_head("", "", None), None);
10214        assert_eq!(bound_head("", "", Some("new")), None);
10215        assert_eq!(bound_head("abc", "", None), None);
10216    }
10217
10218    fn view_json(head: &str) -> String {
10219        format!(
10220            r#"{{"url":"https://github.com/o/r/pull/42","number":42,"state":"OPEN",
10221            "title":"t","headRefOid":"{head}","mergeStateStatus":"CLEAN",
10222            "reviews":[],"comments":[]}}"#
10223        )
10224    }
10225
10226    fn node_json(oid: &str, check: &str, has_next: bool) -> String {
10227        format!(
10228            r#"{{"data":{{"repository":{{"pullRequest":{{"commits":{{"nodes":[{{"commit":
10229            {{"oid":"{oid}","statusCheckRollup":{{"contexts":{{"pageInfo":{{"hasNextPage":{has_next}}},
10230            "nodes":[{{"__typename":"CheckRun","name":"ci","status":"COMPLETED",
10231            "conclusion":"{check}","detailsUrl":"https://example.test/1"}}]}}}}}}}}]}}}}}}}}}}"#
10232        )
10233    }
10234
10235    #[test]
10236    fn rollup_is_bound_to_the_commit_in_the_same_node() {
10237        // The view already points at the new head, but the node still answers
10238        // for the old commit with its red check: the head stays unbound.
10239        let s = seen_from(&view_json("new"), Some(&node_json("old", "FAILURE", false))).unwrap();
10240        assert_eq!(s.rollup_head, "old");
10241        assert_eq!(s.pr.checks, Checks::Red);
10242        assert_eq!(bound_head(&s.head, &s.rollup_head, Some("new")), None);
10243        assert_eq!(s.failing_urls.len(), 1);
10244    }
10245
10246    #[test]
10247    fn checks_come_from_the_node_not_the_view() {
10248        let view = view_json("new").replace(
10249            r#""reviews""#,
10250            r#""statusCheckRollup":[{"name":"ci","status":"COMPLETED","conclusion":"FAILURE"}],"reviews""#,
10251        );
10252        let s = seen_from(&view, Some(&node_json("new", "SUCCESS", false))).unwrap();
10253        assert_eq!(s.pr.checks, Checks::Green);
10254        assert!(s.pr.failing.is_empty());
10255        assert_eq!(
10256            bound_head(&s.head, &s.rollup_head, Some("new")),
10257            Some("new")
10258        );
10259    }
10260
10261    #[test]
10262    fn an_unreadable_or_paged_node_leaves_the_head_unbound() {
10263        for node in [
10264            None,
10265            Some("not json".to_owned()),
10266            Some(r#"{"errors":[{"message":"x"}]}"#.to_owned()),
10267            Some(node_json("new", "SUCCESS", true)),
10268        ] {
10269            let s = seen_from(&view_json("new"), node.as_deref()).unwrap();
10270            assert!(s.rollup_head.is_empty());
10271            assert_eq!(s.pr.checks, Checks::Unknown);
10272            assert_eq!(bound_head(&s.head, &s.rollup_head, None), None);
10273        }
10274    }
10275
10276    #[test]
10277    fn the_merge_command_is_pinned_to_the_observed_head() {
10278        let argv = merge_argv_at(7, "feat: x", "deadbeef");
10279        let at = argv
10280            .iter()
10281            .position(|a| a == "--match-head-commit")
10282            .unwrap();
10283        assert_eq!(argv[at + 1], "deadbeef");
10284    }
10285
10286    #[tokio::test]
10287    async fn stale_checks_after_a_fix_push_never_reach_a_merge() {
10288        let mut state = landing_state();
10289        // The pull request is on the pushed head but the rollup is still the
10290        // previous commit's red, non-required result.
10291        let mut stale = seen("new", Checks::Red, "CLEAN", false);
10292        stale.rollup_head = "old".to_owned();
10293        let forge = Scripted::new(
10294            vec![seen("old", Checks::Green, "CLEAN", true), stale],
10295            vec![],
10296        );
10297        *forge.fix.lock().unwrap() = Some(Fixed::Committed {
10298            head: "new".to_owned(),
10299        });
10300        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10301            .await
10302            .unwrap();
10303        assert!(!forge.calls().contains(&"merge"));
10304        assert_eq!(state.status, RunStatus::Blocked);
10305        let why = state.merge.as_ref().unwrap().detail.clone();
10306        assert!(why.contains("new") && why.contains("old"), "{why}");
10307    }
10308
10309    #[test]
10310    fn a_refusal_read_against_another_commits_checks_is_pending() {
10311        let mut after = seen("a", Checks::Green, "BLOCKED", false);
10312        after.rollup_head = "old".to_owned();
10313        assert_eq!(
10314            classify_refusal(Some(&after), true, "a", CiExpectation::Expected),
10315            Refused::Pending
10316        );
10317    }
10318
10319    #[tokio::test]
10320    async fn absent_ci_empty_rollup_reaches_approval_then_merges_on_the_approved_head() {
10321        let mut state = landing_state();
10322        state.config.merge.ci = crate::config::MergeCi::None;
10323        state.config.graph.land_approval = true;
10324        let forge = Scripted::new(
10325            vec![seen("a", Checks::Unknown, "CLEAN", false)],
10326            vec![(true, "")],
10327        );
10328        land_with(&mut state, URL, &forge).await.unwrap();
10329        assert!(state.parked);
10330        assert_eq!(forge.calls(), ["view"]);
10331        let store = ask::Questions::open();
10332        let mut q = store
10333            .get(&state.land_approval.as_ref().unwrap().question)
10334            .unwrap();
10335        q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
10336        store.put(&mut q).unwrap();
10337
10338        land_with(&mut state, URL, &forge).await.unwrap();
10339        assert_eq!(forge.calls(), ["view", "view", "view", "merge", "view"]);
10340        assert_eq!(state.status, RunStatus::Merged);
10341        assert_eq!(
10342            value_of(&forge.argvs()[0], "--match-head-commit"),
10343            Some("a")
10344        );
10345    }
10346
10347    #[tokio::test]
10348    async fn absent_ci_ignores_failed_workflow_runs_without_rerunning_them() {
10349        let mut state = landing_state();
10350        state.config.merge.ci = crate::config::MergeCi::None;
10351        let forge = Scripted::new(vec![red("a", Some("7"))], vec![(true, "")]);
10352        land_with(&mut state, URL, &forge).await.unwrap();
10353        assert!(forge.reruns.lock().unwrap().is_empty());
10354        assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
10355        assert_eq!(state.status, RunStatus::Merged);
10356    }
10357
10358    #[test]
10359    fn absent_ci_still_requires_a_readable_matching_head_and_resolved_comments() {
10360        let shown = BTreeSet::new();
10361        let guard = |s: Option<&Seen>| {
10362            direct_merge_is_safe(s, "a", &shown, 0, 4, Duration::ZERO, CiExpectation::Absent)
10363        };
10364        let mut fresh = seen("a", Checks::Unknown, "CLEAN", false);
10365        assert!(guard(Some(&fresh)));
10366        assert!(!guard(None));
10367        fresh.rollup_head = "old".to_owned();
10368        assert!(!guard(Some(&fresh)));
10369        fresh.rollup_head = "a".to_owned();
10370        fresh.head = "b".to_owned();
10371        assert!(!guard(Some(&fresh)));
10372        assert!(!guard(Some(&seen("a", Checks::Unknown, "CLEAN", true))));
10373        assert!(!guard(Some(&seen("a", Checks::Unknown, "DIRTY", false))));
10374    }
10375
10376    #[test]
10377    fn absent_ci_refusal_does_not_wait_for_third_party_checks() {
10378        for checks in [Checks::Unknown, Checks::Pending, Checks::Red, Checks::Green] {
10379            let after = seen("a", checks, "BLOCKED", false);
10380            assert_eq!(
10381                classify_refusal(Some(&after), false, "a", CiExpectation::Absent),
10382                Refused::Recheck
10383            );
10384            assert_eq!(
10385                classify_refusal(Some(&after), true, "a", CiExpectation::Absent),
10386                Refused::Final
10387            );
10388        }
10389    }
10390
10391    #[tokio::test]
10392    async fn a_matching_head_with_red_non_required_checks_still_merges() {
10393        let mut state = landing_state();
10394        let forge = Scripted::new(
10395            vec![seen("a", Checks::Red, "CLEAN", false)],
10396            vec![(true, "")],
10397        );
10398        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10399            .await
10400            .unwrap();
10401        assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
10402        assert_eq!(state.status, RunStatus::Merged);
10403    }
10404
10405    #[tokio::test]
10406    async fn a_merge_refused_because_the_head_moved_looks_again_instead_of_failing() {
10407        let mut state = landing_state();
10408        let forge = Scripted::new(
10409            vec![
10410                seen("a", Checks::Green, "CLEAN", false),
10411                seen("a", Checks::Green, "CLEAN", false),
10412                // Re-viewed after the refusal: someone pushed.
10413                seen("b", Checks::Green, "BLOCKED", false),
10414                seen("b", Checks::Green, "CLEAN", false),
10415            ],
10416            vec![(false, REFUSED), (true, "")],
10417        );
10418        land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
10419            .await
10420            .unwrap();
10421        assert_eq!(
10422            forge.calls(),
10423            [
10424                "view", "view", "merge", "view", "poll", "view", "view", "merge", "view"
10425            ]
10426        );
10427        assert_eq!(state.status, RunStatus::Merged);
10428    }
10429
10430    fn merged_view(head: &str) -> Seen {
10431        let mut m = seen(head, Checks::Green, "CLEAN", false);
10432        m.pr.state = PrLifecycle::Merged;
10433        m
10434    }
10435
10436    fn has(argv: &[String], flag: &str) -> bool {
10437        argv.iter().any(|a| a == flag)
10438    }
10439
10440    fn value_of<'a>(argv: &'a [String], flag: &str) -> Option<&'a str> {
10441        let at = argv.iter().position(|a| a == flag)?;
10442        argv.get(at + 1).map(String::as_str)
10443    }
10444
10445    const URL: &str = "https://github.com/o/r/pull/42";
10446
10447    #[tokio::test]
10448    async fn the_merge_step_merges_directly_on_the_observed_head_and_never_arms() {
10449        let mut state = landing_state();
10450        let forge = Scripted::new(
10451            vec![
10452                seen("abc", Checks::Green, "CLEAN", false),
10453                seen("abc", Checks::Green, "CLEAN", false),
10454            ],
10455            vec![(true, "")],
10456        );
10457        land_with(&mut state, URL, &forge).await.unwrap();
10458        assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
10459        let argv = &forge.argvs()[0];
10460        assert!(has(argv, "--squash") && has(argv, "--subject"));
10461        assert!(!has(argv, "--auto") && !has(argv, "--admin"));
10462        assert_eq!(value_of(argv, "--match-head-commit"), Some("abc"));
10463        assert_eq!(state.status, RunStatus::Merged);
10464        assert!(state.land_armed_head.is_none());
10465    }
10466
10467    #[tokio::test]
10468    async fn a_resume_disables_an_arm_left_by_an_older_build_before_merging() {
10469        let mut state = landing_state();
10470        state.land_armed_head = Some("a".to_owned());
10471        let forge = Scripted::new(
10472            vec![seen("a", Checks::Green, "CLEAN", false)],
10473            vec![(true, ""), (true, "")],
10474        );
10475        land_with(&mut state, URL, &forge).await.unwrap();
10476        let argvs = forge.argvs();
10477        assert!(has(&argvs[0], "--disable-auto"));
10478        assert!(!has(&argvs[1], "--auto"));
10479        assert_eq!(value_of(&argvs[1], "--match-head-commit"), Some("a"));
10480        assert_eq!(state.status, RunStatus::Merged);
10481        assert!(state.land_armed_head.is_none());
10482    }
10483
10484    #[tokio::test]
10485    async fn an_approval_never_carries_over_to_a_new_head() {
10486        crate::run::pin_test_home();
10487        let mut state = run_state();
10488        state.config.graph.land_approval = true;
10489        let pr = green_pr();
10490        let store = ask::Questions::open();
10491
10492        approval_gate(&mut state, &pr, "feat: x", None, "aaa")
10493            .await
10494            .unwrap();
10495        let mut q = store
10496            .list()
10497            .into_iter()
10498            .find(|q| q.run == state.id)
10499            .unwrap();
10500        q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
10501        store.put(&mut q).unwrap();
10502        assert_eq!(
10503            approval_gate(&mut state, &pr, "feat: x", None, "AAA")
10504                .await
10505                .unwrap(),
10506            ApprovalGate::Approved,
10507            "the same head keeps its approval"
10508        );
10509
10510        // A new head is a new question, not the old word.
10511        assert_eq!(
10512            approval_gate(&mut state, &pr, "feat: x", None, "bbb")
10513                .await
10514                .unwrap(),
10515            ApprovalGate::Pending
10516        );
10517        let all: Vec<_> = store
10518            .list()
10519            .into_iter()
10520            .filter(|q| q.run == state.id)
10521            .collect();
10522        assert_eq!(all.len(), 2);
10523
10524        // A question recorded before heads were tracked is not reused either.
10525        state.land_approval = None;
10526        assert_eq!(
10527            approval_gate(&mut state, &pr, "feat: x", None, "bbb")
10528                .await
10529                .unwrap(),
10530            ApprovalGate::Pending
10531        );
10532        let open = store
10533            .list()
10534            .into_iter()
10535            .filter(|q| q.run == state.id && q.status.open())
10536            .count();
10537        assert_eq!(open, 1, "the superseded question was retired");
10538    }
10539
10540    #[tokio::test]
10541    async fn the_merge_is_bound_to_the_head_it_was_decided_on() {
10542        let mut state = landing_state();
10543        let forge = Scripted::new(
10544            vec![
10545                seen("a", Checks::Green, "CLEAN", false),
10546                // The fresh read before the merge: someone pushed.
10547                seen("b", Checks::Green, "CLEAN", false),
10548            ],
10549            vec![(true, "")],
10550        );
10551        land_with(&mut state, URL, &forge).await.unwrap();
10552        let argvs = forge.argvs();
10553        assert_eq!(argvs.len(), 1, "no merge was tried on the moved head");
10554        assert!(!has(&argvs[0], "--auto"));
10555        assert_eq!(value_of(&argvs[0], "--match-head-commit"), Some("b"));
10556        assert_eq!(state.status, RunStatus::Merged);
10557    }
10558
10559    fn passing(label: &str) -> CheckInfo {
10560        CheckInfo {
10561            label: label.to_owned(),
10562            verdict: Verdict::Pass,
10563            required: Some(false),
10564            run: None,
10565        }
10566    }
10567
10568    fn failing(label: &str, run: Option<&str>) -> (String, Option<String>) {
10569        (label.to_owned(), run.map(str::to_owned))
10570    }
10571
10572    fn reran(head: &str, runs: &[(&str, i64)]) -> LandReruns {
10573        LandReruns {
10574            head: head.to_owned(),
10575            runs: runs.iter().map(|(r, at)| ((*r).to_owned(), *at)).collect(),
10576        }
10577    }
10578
10579    #[test]
10580    fn a_failed_run_is_rerun_once_then_waited_on_then_left_to_the_fixer() {
10581        let f = [failing("test (macos)", Some("7"))];
10582        assert_eq!(
10583            rerun_step(&f, None, "h", 100),
10584            RerunStep::Rerun(vec!["7".to_owned()])
10585        );
10586        let rec = reran("h", &[("7", 100)]);
10587        let grace = crate::release_watch::RERUN_GRACE;
10588        assert_eq!(
10589            rerun_step(&f, Some(&rec), "h", 100 + grace - 1),
10590            RerunStep::Grace
10591        );
10592        assert_eq!(rerun_step(&f, Some(&rec), "h", 100 + grace), RerunStep::Fix);
10593    }
10594
10595    #[test]
10596    fn a_failure_without_a_run_id_goes_to_the_fixer() {
10597        let f = [failing("a", Some("7")), failing("ci/external", None)];
10598        assert_eq!(rerun_step(&f, None, "h", 0), RerunStep::Fix);
10599        assert_eq!(rerun_step(&[], None, "h", 0), RerunStep::Fix);
10600    }
10601
10602    #[test]
10603    fn a_moved_head_forgets_the_reruns() {
10604        let f = [failing("a", Some("7"))];
10605        let rec = reran("old", &[("7", 100)]);
10606        assert_eq!(
10607            rerun_step(&f, Some(&rec), "new", 101),
10608            RerunStep::Rerun(vec!["7".to_owned()])
10609        );
10610    }
10611
10612    #[test]
10613    fn only_unrerun_runs_are_rerun_and_duplicates_collapse() {
10614        let f = [
10615            failing("a", Some("7")),
10616            failing("b", Some("7")),
10617            failing("c", Some("8")),
10618        ];
10619        assert_eq!(
10620            rerun_step(&f, None, "h", 0),
10621            RerunStep::Rerun(vec!["7".to_owned(), "8".to_owned()])
10622        );
10623        let rec = reran("h", &[("7", 0)]);
10624        assert_eq!(
10625            rerun_step(&f, Some(&rec), "h", 1),
10626            RerunStep::Rerun(vec!["8".to_owned()])
10627        );
10628    }
10629
10630    #[test]
10631    fn only_blocking_failures_are_candidates_for_a_rerun() {
10632        let check = |label: &str, verdict, required, run: &str| CheckInfo {
10633            label: label.to_owned(),
10634            verdict,
10635            required,
10636            run: Some(run.to_owned()),
10637        };
10638        let got = rerunnable(&[
10639            check("passed", Verdict::Pass, Some(true), "1"),
10640            check("optional", Verdict::Fail, Some(false), "2"),
10641            check("required", Verdict::Fail, Some(true), "3"),
10642            check("unknown", Verdict::Fail, None, "4"),
10643        ]);
10644        let labels: Vec<_> = got.iter().map(|(l, _)| l.as_str()).collect();
10645        assert_eq!(labels, ["required", "unknown"]);
10646    }
10647
10648    fn unrelated_ci_fixture() -> CiEvidence {
10649        let failure = CiFailure {
10650            path: "tests/web_render.rs".into(),
10651            start_line: 42,
10652            end_line: 42,
10653            title: "question_identity_disclosure_copy_and_live_updates".into(),
10654            message: "assertion failed".into(),
10655        };
10656        CiEvidence {
10657            head: "a".into(),
10658            base: "base-sha".into(),
10659            changed: vec![
10660                "AGENTS.md".into(),
10661                "src/talk.rs".into(),
10662                "tests/consult_investigation.rs".into(),
10663            ],
10664            checks: vec![CiCheckEvidence {
10665                label: "coverage".into(),
10666                head_check: 70,
10667                base_check: 60,
10668                app: 1,
10669                run: "7".into(),
10670                base_run: "6".into(),
10671                head_failures: vec![failure.clone()],
10672                base_failures: vec![failure],
10673                rerun_exhausted: true,
10674            }],
10675        }
10676    }
10677
10678    #[test]
10679    fn unrelated_ci_requires_complete_structural_evidence_for_every_failure() {
10680        let good = unrelated_ci_fixture();
10681        let red = vec!["coverage".into()];
10682        assert!(good.unrelated("a", &red));
10683        assert!(!good.unrelated("another-head", &red));
10684        assert!(!good.unrelated("a", &["coverage".into(), "test".into()]));
10685        assert!(!good.unrelated("a", &[]));
10686        for mutation in 0..8 {
10687            let mut bad = good.clone();
10688            match mutation {
10689                0 => bad.checks[0].rerun_exhausted = false,
10690                1 => bad.checks[0].base_failures.clear(),
10691                2 => bad.checks[0].head_failures.clear(),
10692                3 => bad.changed.push("tests/web_render.rs".into()),
10693                4 => bad.changed.push(".github/workflows/ci.yml".into()),
10694                5 => bad.checks[0].base_failures[0].message = "different failure".into(),
10695                6 => bad.checks.push(bad.checks[0].clone()),
10696                _ => bad.base.clear(),
10697            }
10698            assert!(!bad.unrelated("a", &red), "mutation {mutation}");
10699        }
10700    }
10701
10702    #[test]
10703    fn red_unknown_merge_state_waits_before_repair_but_never_implies_merge() {
10704        let pr = seen("a", Checks::Red, "UNKNOWN", false).pr;
10705        assert_eq!(decide(&pr, 0, 2, Duration::ZERO), Step::Wait);
10706        assert!(matches!(decide(&pr, 0, 2, CHECKS_GRACE), Step::Fix { .. }));
10707        assert!(matches!(
10708            decide(&pr, 2, 2, CHECKS_GRACE),
10709            Step::GiveUp { .. }
10710        ));
10711        let optional = CheckInfo {
10712            label: "coverage".into(),
10713            verdict: Verdict::Fail,
10714            required: Some(false),
10715            run: Some("7".into()),
10716        };
10717        assert!(rerunnable_with(std::slice::from_ref(&optional), false).is_empty());
10718        assert_eq!(
10719            rerunnable_with(&[optional], true),
10720            vec![("coverage".into(), Some("7".into()))]
10721        );
10722    }
10723
10724    #[tokio::test]
10725    async fn unrelated_ci_after_a_comment_round_does_not_charge_a_no_commit_stop() {
10726        let mut state = landing_state();
10727        let mut red = coverage_unknown();
10728        red.pr = seen("a", Checks::Red, "BLOCKED", true).pr;
10729        red.pr.failing = vec!["coverage".into()];
10730        let forge = Scripted::new(vec![red], vec![]);
10731        *forge.evidence.lock().unwrap() = Some(unrelated_ci_fixture());
10732        *forge.fix.lock().unwrap() = Some(Fixed::Declined);
10733        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10734        assert!(forge.calls().contains(&"fix"));
10735        assert!(state.land_unrelated.is_some());
10736        assert!(state.parked);
10737        assert!(
10738            !state
10739                .events
10740                .iter()
10741                .any(|e| e.message.contains("fixer produced no commit"))
10742        );
10743    }
10744
10745    fn coverage_unknown() -> Seen {
10746        let mut s = red("a", Some("7"));
10747        s.pr.blocking = Blocking::Unsaid;
10748        s.merge_state = "UNKNOWN".into();
10749        s.pr.failing = vec!["coverage".into()];
10750        s.contexts[0].label = "coverage".into();
10751        s.contexts[0].required = Some(false);
10752        s
10753    }
10754
10755    #[tokio::test]
10756    async fn run_196c_unknown_then_unstable_does_not_spend_a_fix_round() {
10757        let mut state = landing_state();
10758        let unknown = coverage_unknown();
10759        let mut unstable = unknown.clone();
10760        unstable.pr.blocking = Blocking::of("UNSTABLE");
10761        unstable.merge_state = "UNSTABLE".into();
10762        let forge = Scripted::new(vec![unknown, unstable], vec![(true, "")]);
10763        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10764        assert!(!forge.calls().contains(&"fix"));
10765        assert!(!forge.calls().contains(&"rerun"));
10766        assert_eq!(state.status, RunStatus::Merged);
10767        assert_eq!(state.pr.unwrap().round, 0);
10768    }
10769
10770    #[tokio::test]
10771    async fn run_196c_persistent_unknown_reruns_optional_once_then_parks_with_evidence() {
10772        let mut state = landing_state();
10773        let forge = Scripted::new(vec![coverage_unknown()], vec![]);
10774        *forge.evidence.lock().unwrap() = Some(unrelated_ci_fixture());
10775        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10776        assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
10777        assert!(!forge.calls().contains(&"fix"));
10778        assert!(state.parked);
10779        assert_eq!(state.status, RunStatus::Landing);
10780        assert_eq!(state.pr.as_ref().unwrap().round, 0);
10781        let hold = state.land_unrelated.as_ref().unwrap();
10782        let q = ask::Questions::open().get(&hold.question).unwrap();
10783        assert!(
10784            q.actions
10785                .values()
10786                .any(|a| matches!(a, ask::ChoiceAction::Resume { .. }))
10787        );
10788        assert!(
10789            state
10790                .events
10791                .iter()
10792                .any(|e| e.message.contains("[ci-unrelated]") && e.message.contains("base-sha"))
10793        );
10794        let first = state.land_unsaid.clone();
10795        // A resume on this head neither extends UNKNOWN grace nor reruns 7.
10796        state.parked = false;
10797        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10798        assert_eq!(state.land_unsaid, first);
10799        assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
10800    }
10801
10802    fn red(head: &str, run: Option<&str>) -> Seen {
10803        let mut s = seen(head, Checks::Red, "BLOCKED", false);
10804        s.pr.failing = vec!["test (macos)".to_owned()];
10805        s.contexts = vec![CheckInfo {
10806            label: "test (macos)".to_owned(),
10807            verdict: Verdict::Fail,
10808            required: Some(true),
10809            run: run.map(str::to_owned),
10810        }];
10811        s
10812    }
10813
10814    const RERUN_PR: &str = "https://github.com/o/r/pull/42";
10815
10816    #[tokio::test]
10817    async fn a_flaky_red_check_is_rerun_without_a_fix_round_and_then_merges() {
10818        let mut state = landing_state();
10819        let forge = Scripted::new(
10820            vec![
10821                red("a", Some("7")),
10822                seen("a", Checks::Pending, "BLOCKED", false),
10823                seen("a", Checks::Green, "CLEAN", false),
10824            ],
10825            vec![(true, "")],
10826        );
10827        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10828        assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
10829        assert!(!forge.calls().contains(&"fix"));
10830        assert_eq!(state.status, RunStatus::Merged);
10831        assert_eq!(state.pr.as_ref().unwrap().round, 0);
10832        let argvs = forge.argvs();
10833        assert_eq!(value_of(&argvs[0], "--match-head-commit"), Some("a"));
10834        assert!(
10835            state
10836                .events
10837                .iter()
10838                .any(|e| e.message.contains("test (macos)") && e.message.contains("run 7"))
10839        );
10840    }
10841
10842    #[tokio::test]
10843    async fn still_red_after_the_grace_goes_to_the_fixer_and_a_run_is_not_rerun_twice() {
10844        let mut state = landing_state();
10845        let forge = Scripted::new(
10846            vec![
10847                red("a", Some("7")),
10848                seen("a", Checks::Green, "CLEAN", false),
10849            ],
10850            vec![(true, "")],
10851        );
10852        *forge.fix.lock().unwrap() = Some(Fixed::Declined);
10853        // The red view repeats for the grace, then the fixer is asked once.
10854        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10855        assert_eq!(forge.reruns.lock().unwrap().len(), 1);
10856        let calls = forge.calls();
10857        let rerun_at = calls.iter().position(|c| *c == "rerun").unwrap();
10858        let fix_at = calls.iter().position(|c| *c == "fix");
10859        assert!(fix_at.is_none_or(|f| f > rerun_at));
10860    }
10861
10862    #[tokio::test]
10863    async fn a_failed_run_is_rerun_while_another_run_is_still_pending() {
10864        let mut state = landing_state();
10865        let mut v = red("a", Some("7"));
10866        v.pr.checks = Checks::Pending;
10867        v.contexts.push(CheckInfo {
10868            label: "slow".to_owned(),
10869            verdict: Verdict::Pending,
10870            required: Some(true),
10871            run: Some("8".to_owned()),
10872        });
10873        let forge = Scripted::new(
10874            vec![v, seen("a", Checks::Green, "CLEAN", false)],
10875            vec![(true, "")],
10876        );
10877        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10878        assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
10879        assert_eq!(state.status, RunStatus::Merged);
10880    }
10881
10882    #[test]
10883    fn a_run_with_a_job_still_going_is_not_rerun_yet() {
10884        let mut v = red("a", Some("7"));
10885        v.contexts.push(CheckInfo {
10886            label: "same run, still going".to_owned(),
10887            verdict: Verdict::Pending,
10888            required: Some(true),
10889            run: Some("7".to_owned()),
10890        });
10891        assert!(rerunnable(&v.contexts).is_empty());
10892    }
10893
10894    #[tokio::test]
10895    async fn the_rerun_is_made_even_when_the_fix_budget_is_spent() {
10896        let mut state = landing_state();
10897        state.config.graph.land_rounds = 0;
10898        let forge = Scripted::new(
10899            vec![
10900                red("a", Some("7")),
10901                seen("a", Checks::Green, "CLEAN", false),
10902            ],
10903            vec![(true, "")],
10904        );
10905        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10906        assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
10907        assert_eq!(state.status, RunStatus::Merged);
10908        assert!(!forge.calls().contains(&"fix"));
10909        assert_eq!(state.pr.as_ref().unwrap().round, 0);
10910    }
10911
10912    #[tokio::test]
10913    async fn the_last_fix_rounds_new_head_gets_its_rerun() {
10914        let mut state = landing_state();
10915        state.config.graph.land_rounds = 1;
10916        let forge = Scripted::new(
10917            vec![
10918                seen("a", Checks::Green, "CLEAN", true),
10919                red("b", Some("7")),
10920                seen("b", Checks::Green, "CLEAN", false),
10921            ],
10922            vec![(true, "")],
10923        );
10924        *forge.fix.lock().unwrap() = Some(Fixed::Committed {
10925            head: "b".to_owned(),
10926        });
10927        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10928        assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
10929        assert_eq!(forge.calls().iter().filter(|c| **c == "fix").count(), 1);
10930        assert_eq!(state.pr.as_ref().unwrap().round, 1);
10931        assert_eq!(state.status, RunStatus::Merged);
10932        assert_eq!(state.land_reruns.as_ref().unwrap().head, "b");
10933        assert_eq!(
10934            value_of(&forge.argvs()[0], "--match-head-commit"),
10935            Some("b")
10936        );
10937    }
10938
10939    #[tokio::test]
10940    async fn a_failed_rerun_call_falls_through_to_the_fix_in_the_same_lap() {
10941        let mut state = landing_state();
10942        let forge = Scripted::new(vec![red("a", Some("7"))], vec![]);
10943        *forge.rerun_fails.lock().unwrap() = true;
10944        *forge.fix.lock().unwrap() = Some(Fixed::Declined);
10945        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10946        assert_eq!(forge.reruns.lock().unwrap().len(), 1, "never retried");
10947        assert_eq!(
10948            forge.calls().iter().filter(|c| **c == "fix").count(),
10949            1,
10950            "{:?}",
10951            forge.calls()
10952        );
10953    }
10954
10955    #[tokio::test]
10956    async fn a_saved_rerun_record_stops_a_restart_from_rerunning() {
10957        let mut state = landing_state();
10958        let forge = Scripted::new(vec![red("a", Some("7"))], vec![]);
10959        // The record was written just before the previous pass died.
10960        state.land_reruns = Some(reran("a", &[("7", *forge.clock.lock().unwrap())]));
10961        *forge.fix.lock().unwrap() = Some(Fixed::Declined);
10962        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10963        assert!(forge.reruns.lock().unwrap().is_empty());
10964    }
10965
10966    #[tokio::test]
10967    async fn the_switch_off_and_a_missing_run_id_go_straight_to_the_fixer() {
10968        let mut state = landing_state();
10969        state.config.graph.rerun_flaky_checks = false;
10970        let forge = Scripted::new(vec![red("a", Some("7"))], vec![]);
10971        *forge.fix.lock().unwrap() = Some(Fixed::Declined);
10972        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10973        assert!(forge.reruns.lock().unwrap().is_empty());
10974        assert!(forge.calls().contains(&"fix"));
10975
10976        let mut state = landing_state();
10977        let forge = Scripted::new(vec![red("a", None)], vec![]);
10978        *forge.fix.lock().unwrap() = Some(Fixed::Declined);
10979        land_with(&mut state, RERUN_PR, &forge).await.unwrap();
10980        assert!(forge.reruns.lock().unwrap().is_empty());
10981        assert!(forge.calls().contains(&"fix"));
10982    }
10983
10984    fn names(xs: &[&str]) -> BTreeSet<String> {
10985        xs.iter().map(|x| (*x).to_owned()).collect()
10986    }
10987
10988    #[test]
10989    fn a_required_check_the_rollup_never_listed_is_named() {
10990        let req = names(&["build"]);
10991        let why = waiting_on("BLOCKED", &[passing("review")], Some(&req));
10992        assert!(why.contains("never reported: build"), "{why}");
10993        assert!(!why.contains("probably waiting for a review"), "{why}");
10994    }
10995
10996    #[test]
10997    fn an_unreadable_required_list_is_not_read_as_a_review_wait() {
10998        let why = waiting_on("BLOCKED", &[passing("review")], None);
10999        assert!(why.contains("could not be read"), "{why}");
11000        assert!(!why.contains("probably waiting for a review"), "{why}");
11001    }
11002
11003    #[test]
11004    fn all_required_reported_keeps_the_review_guess() {
11005        let req = names(&["build"]);
11006        let why = waiting_on("BLOCKED", &[passing("build")], Some(&req));
11007        assert!(why.contains("probably waiting for a review"), "{why}");
11008        assert!(!why.contains("never reported"), "{why}");
11009    }
11010
11011    #[test]
11012    fn required_names_match_the_rollup_ignoring_case_only() {
11013        let req = names(&["Build"]);
11014        let why = waiting_on("BLOCKED", &[passing("build")], Some(&req));
11015        assert!(!why.contains("never reported"), "{why}");
11016    }
11017
11018    #[test]
11019    fn required_contexts_are_read_from_protection_and_rulesets() {
11020        let classic = r#"{"contexts":["build"],"checks":[{"context":"lint","app_id":1}]}"#;
11021        assert_eq!(
11022            parse_classic_required(classic),
11023            Some(names(&["build", "lint"]))
11024        );
11025        let rules = r#"[{"type":"pull_request","parameters":{}},
11026            {"type":"required_status_checks","parameters":{"required_status_checks":[{"context":"test"}]}}]"#;
11027        assert_eq!(parse_ruleset_required(rules), Some(names(&["test"])));
11028        assert_eq!(parse_ruleset_required("nope"), None);
11029        assert_eq!(encode_path_segment("release/1.x"), "release%2F1.x");
11030    }
11031
11032    #[test]
11033    fn the_direct_merge_guard_needs_the_approved_head_bound_to_its_checks() {
11034        let shown = BTreeSet::new();
11035        let ok = seen("a", Checks::Green, "CLEAN", false);
11036        let guard = |s: Option<&Seen>| {
11037            direct_merge_is_safe(
11038                s,
11039                "A",
11040                &shown,
11041                0,
11042                4,
11043                Duration::ZERO,
11044                CiExpectation::Expected,
11045            )
11046        };
11047        assert!(guard(Some(&ok)));
11048        assert!(!guard(None));
11049        assert!(!guard(Some(&seen("b", Checks::Green, "CLEAN", false))));
11050        let mut stale = ok.clone();
11051        stale.rollup_head = "old".to_owned();
11052        assert!(!guard(Some(&stale)));
11053        assert!(!guard(Some(&seen("a", Checks::Pending, "BLOCKED", false))));
11054        assert!(!guard(Some(&merged_view("a"))));
11055    }
11056
11057    #[test]
11058    fn the_rollup_node_carries_whether_each_check_is_required() {
11059        let node = node_json("new", "SUCCESS", false)
11060            .replace(r#""name":"ci","#, r#""name":"ci","isRequired":true,"#);
11061        let s = seen_from(&view_json("new"), Some(&node)).unwrap();
11062        assert_eq!(s.contexts.len(), 1);
11063        assert_eq!(s.contexts[0].required, Some(true));
11064        let s = seen_from(&view_json("new"), Some(&node_json("new", "SUCCESS", false))).unwrap();
11065        assert_eq!(s.contexts[0].required, None);
11066    }
11067
11068    #[tokio::test]
11069    async fn a_resume_that_cannot_disable_a_recorded_arm_stops_and_keeps_the_record() {
11070        let mut state = landing_state();
11071        state.land_armed_head = Some("a".to_owned());
11072        let forge = Scripted::new(
11073            vec![seen("a", Checks::Green, "CLEAN", true)],
11074            vec![(false, "disable exploded")],
11075        );
11076        land_with(&mut state, URL, &forge).await.unwrap();
11077        assert!(!forge.calls().contains(&"fix"));
11078        assert_eq!(state.status, RunStatus::Blocked);
11079        assert_eq!(state.land_armed_head.as_deref(), Some("a"));
11080        let why = state.merge.as_ref().unwrap().detail.clone();
11081        assert!(why.contains("disable exploded"), "{why}");
11082    }
11083}