1use 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
61pub const POLL: Duration = Duration::from_secs(30);
67
68pub const WAIT_CEILING: Duration = Duration::from_secs(45 * 60);
74
75pub const CHECKS_GRACE: Duration = Duration::from_secs(3 * 60);
87
88const LOG_TAIL: usize = 4_000;
91
92const MAX_LOGS: usize = 3;
95
96pub const MARKER: &str = "<!-- magi:land -->";
102
103const NOT_A_REVIEW: [&str; 3] = [
112 "skip review by coderabbit.ai",
113 "summarize by coderabbit.ai",
114 "<!-- tips_start -->",
115];
116
117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119pub enum PrLifecycle {
120 Open,
122 Merged,
124 Closed,
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
130pub enum Checks {
131 Pending,
133 Green,
136 Red,
138 Unknown,
140}
141
142impl PrLifecycle {
143 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 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#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct ReviewComment {
168 pub author: String,
170 pub path: Option<String>,
172 pub line: Option<u64>,
174 pub body: String,
176}
177
178#[derive(Debug, Clone, PartialEq, Eq)]
180pub struct PrState {
181 pub url: String,
183 pub number: u64,
185 pub state: PrLifecycle,
187 pub checks: Checks,
189 pub failing: Vec<String>,
191 pub review_comments: Vec<ReviewComment>,
193 pub blocking: Blocking,
195}
196
197#[derive(Debug, Clone, Copy, PartialEq, Eq)]
210pub enum Blocking {
211 No,
213 Yes,
215 Conflict,
217 Unsaid,
221}
222
223impl Blocking {
224 fn of(raw: &str) -> Self {
226 match raw.to_ascii_uppercase().as_str() {
227 "CLEAN" | "UNSTABLE" | "HAS_HOOKS" => Self::No,
230 "DIRTY" => Self::Conflict,
231 "" | "UNKNOWN" => Self::Unsaid,
232 _ => Self::Yes,
234 }
235 }
236
237 #[must_use]
239 pub fn stops_a_merge(self) -> bool {
240 !matches!(self, Self::No)
241 }
242}
243
244#[derive(Debug, Clone, PartialEq, Eq)]
246pub enum Step {
247 Wait,
249 Rebase,
257 Fix {
259 reason: String,
261 },
262 Merge,
264 Done {
266 merged: bool,
268 },
269 GiveUp {
271 reason: String,
273 },
274}
275
276pub(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
317pub fn decide(pr: &PrState, round: usize, budget: usize, waited: Duration) -> Step {
337 decide_with(pr, round, budget, waited, CiExpectation::Expected)
338}
339
340#[derive(Debug, Clone, Copy, PartialEq, Eq)]
342pub enum CiExpectation {
343 Expected,
346 Absent,
351}
352
353pub fn decide_with(
358 pr: &PrState,
359 round: usize,
360 budget: usize,
361 waited: Duration,
362 ci: CiExpectation,
363) -> Step {
364 match pr.state {
365 PrLifecycle::Merged => return Step::Done { merged: true },
366 PrLifecycle::Closed => return Step::Done { merged: false },
367 PrLifecycle::Open => {}
368 }
369
370 if pr.blocking == Blocking::Conflict {
373 return Step::Rebase;
374 }
375
376 if ci == CiExpectation::Absent {
377 return Step::Merge;
378 }
379
380 let spent = round >= budget;
381 match pr.checks {
382 Checks::Pending => Step::Wait,
383 Checks::Unknown if waited < CHECKS_GRACE => Step::Wait,
384 Checks::Unknown => Step::GiveUp {
385 reason: format!(
386 "no check status is readable on the pull request after {} minute(s); \
387 refusing to merge on a guess",
388 CHECKS_GRACE.as_secs() / 60
389 ),
390 },
391 Checks::Red if !pr.blocking.stops_a_merge() && pr.review_comments.is_empty() => Step::Merge,
398 Checks::Red => {
399 let what = format!(
400 "{} check(s) failing: {}",
401 pr.failing.len(),
402 pr.failing.join(", ")
403 );
404 if spent {
405 Step::GiveUp {
406 reason: format!("{what} — still red after {budget} fix round(s)"),
407 }
408 } else {
409 Step::Fix { reason: what }
410 }
411 }
412 Checks::Green if pr.review_comments.is_empty() => Step::Merge,
413 Checks::Green => {
414 let what = format!(
415 "checks are green but {} review comment(s) are unresolved: {}",
416 pr.review_comments.len(),
417 authors(&pr.review_comments)
418 );
419 if spent {
420 Step::GiveUp {
421 reason: format!("{what} — still unresolved after {budget} fix round(s)"),
422 }
423 } else {
424 Step::Fix { reason: what }
425 }
426 }
427 }
428}
429
430fn authors(comments: &[ReviewComment]) -> String {
432 let mut seen: Vec<&str> = Vec::new();
433 for c in comments {
434 if !seen.contains(&c.author.as_str()) {
435 seen.push(&c.author);
436 }
437 }
438 seen.join(", ")
439}
440
441pub fn merge_argv(number: u64, subject: &str) -> Vec<String> {
445 vec![
446 "pr".to_owned(),
447 "merge".to_owned(),
448 number.to_string(),
449 "--squash".to_owned(),
450 "--delete-branch".to_owned(),
451 "--subject".to_owned(),
452 subject.to_owned(),
453 ]
454}
455
456pub fn merge_argv_at(number: u64, subject: &str, head: &str) -> Vec<String> {
462 let mut argv = merge_argv(number, subject);
463 argv.push("--match-head-commit".to_owned());
464 argv.push(head.to_owned());
465 argv
466}
467
468pub fn disable_automerge_argv(number: u64) -> Vec<String> {
473 ["pr", "merge", &number.to_string(), "--disable-auto"]
474 .map(str::to_owned)
475 .to_vec()
476}
477
478fn direct_merge_is_safe(
484 fresh: Option<&Seen>,
485 approved_head: &str,
486 shown: &BTreeSet<String>,
487 round: usize,
488 budget: usize,
489 waited: Duration,
490) -> bool {
491 let Some(fresh) = fresh else {
492 return false;
493 };
494 if fresh.pr.state != PrLifecycle::Open {
495 return false;
496 }
497 let Some(bound) = bound_head(&fresh.head, &fresh.rollup_head, None) else {
498 return false;
499 };
500 if !bound.eq_ignore_ascii_case(approved_head) {
501 return false;
502 }
503 let mut pr = fresh.pr.clone();
504 pr.review_comments.retain(|c| !shown.contains(&c.body));
505 decide(&pr, round, budget, waited) == Step::Merge
506}
507
508fn waiting_on(
515 merge_state: &str,
516 contexts: &[CheckInfo],
517 required_set: Option<&BTreeSet<String>>,
518) -> String {
519 let state = if merge_state.is_empty() {
520 "unknown"
521 } else {
522 merge_state
523 };
524 let tag = |c: &CheckInfo| match c.verdict {
525 Verdict::Fail => "failed",
526 _ => "pending",
527 };
528 let unsettled: Vec<&CheckInfo> = contexts
529 .iter()
530 .filter(|c| c.verdict != Verdict::Pass)
531 .collect();
532 let required: Vec<String> = unsettled
533 .iter()
534 .filter(|c| c.required == Some(true))
535 .map(|c| format!("{} ({})", c.label, tag(c)))
536 .collect();
537 let unknown: Vec<String> = unsettled
538 .iter()
539 .filter(|c| c.required.is_none())
540 .map(|c| format!("{} ({})", c.label, tag(c)))
541 .collect();
542 let never: Vec<&str> = required_set
546 .map(|set| {
547 set.iter()
548 .filter(|name| !contexts.iter().any(|c| c.label.eq_ignore_ascii_case(name)))
549 .map(String::as_str)
550 .collect()
551 })
552 .unwrap_or_default();
553 let mut out = format!("merge state: {state}");
554 if !never.is_empty() {
555 let _ = write!(
556 out,
557 "; required checks never reported: {}",
558 never.join(", ")
559 );
560 }
561 if !required.is_empty() {
562 let _ = write!(
563 out,
564 "; required checks not passing: {}",
565 required.join(", ")
566 );
567 }
568 if !unknown.is_empty() {
569 let _ = write!(
570 out,
571 "; whether these are required could not be read, so they may be: {}",
572 unknown.join(", ")
573 );
574 }
575 if required.is_empty() && unknown.is_empty() && never.is_empty() {
576 if required_set.is_some() {
577 out.push_str(
578 "; no required check is pending, failing or unreported, so GitHub is probably \
579 waiting for a review or another branch rule",
580 );
581 } else {
582 out.push_str(
583 "; the required check list could not be read, so a required check that was \
584 never reported cannot be ruled out",
585 );
586 }
587 }
588 out
589}
590
591pub fn merge_subject(pr_title: &str, instruction: &str) -> String {
598 let title = pr_title.trim();
599 if !title.is_empty() && !title.starts_with("magi: candidate") {
600 return title.to_owned();
601 }
602 let first = instruction
603 .lines()
604 .map(str::trim)
605 .find(|l| !l.is_empty())
606 .unwrap_or("magi: land the winning candidate");
607 first.trim_start_matches(['#', ' ']).to_owned()
608}
609
610pub const APPROVE: &str = "merge";
612
613pub const HOLD: &str = "hold";
615
616pub const REPAIR: &str = "fix findings and re-review";
618
619pub fn repair_available(state: &RunState) -> bool {
622 state.reviews.last().is_some_and(|r| {
623 state.open_findings().iter().any(|f| f.severity.blocks())
624 || r.final_votes()
625 .iter()
626 .any(|(_, _, v)| *v == crate::verdict::ReviewVote::Reject)
627 })
628}
629
630pub fn approval_choices(state: &RunState) -> Vec<String> {
632 let mut choices = vec![
633 APPROVE.to_owned(),
634 HOLD.to_owned(),
635 REPAIR.to_owned(),
636 crate::merge_decision::WITHDRAW.to_owned(),
637 crate::merge_decision::DISMISS.to_owned(),
638 ];
639 choices.extend(
640 state
641 .config
642 .agents
643 .iter()
644 .map(|a| format!("{}{id}", crate::merge_decision::REIMPLEMENT, id = a.id)),
645 );
646 choices
647}
648
649pub fn upgrade_repair_choice(q: &mut ask::Question, state: &RunState) -> bool {
652 if q.node != APPROVAL_NODE
653 || !state
654 .land_approval
655 .as_ref()
656 .is_some_and(|a| a.question == q.id)
657 {
658 return false;
659 }
660 let before = (q.choices.clone(), q.actions.clone(), q.merge_target.clone());
661 if q.status.open() {
662 q.actions.remove(HOLD);
665 for choice in approval_choices(state) {
666 if !q.choices.contains(&choice) {
667 q.choices.push(choice);
668 }
669 }
670 if q.merge_target.is_none() {
671 q.merge_target = state.pr.as_ref().map(|pr| crate::merge_decision::Target {
672 pr: pr.url.clone(),
673 head: state.land_approval.as_ref().unwrap().head.clone(),
674 language: state.config.graph.language.clone(),
675 });
676 }
677 }
678 for choice in q.choices.clone() {
679 if (q.status.open()
680 && crate::merge_decision::stops(&choice)
681 && choice != HOLD
682 && choice != crate::merge_decision::DISMISS)
683 || (choice == REPAIR && (q.status.open() || q.resolution().as_deref() == Some(REPAIR)))
684 {
685 q.actions.insert(
686 choice,
687 ask::ChoiceAction::Resume {
688 run: state.id.clone(),
689 },
690 );
691 }
692 }
693 before != (q.choices.clone(), q.actions.clone(), q.merge_target.clone())
694}
695
696pub fn unhedged(message: &str, quote: &str) -> bool {
704 let quote = quote.trim();
705 !quote.is_empty() && message.contains(quote) && !hedged(message) && !retracts(message)
706}
707
708fn hedged(text: &str) -> bool {
712 const WORDS: &[&str] = &[
713 "maybe",
714 "probably",
715 "perhaps",
716 "might",
717 "if",
718 "unless",
719 "not",
720 "no",
721 "nope",
722 "stop",
723 "dont",
724 "abort",
725 "revert",
726 "undo",
727 "never",
728 "wait",
729 "hold",
730 "cancel",
731 "but",
732 "think",
733 "guess",
734 "suppose",
735 "unsure",
736 "yet",
737 "except",
738 "only",
739 "cannot",
740 "should",
741 "once",
742 "provided",
743 "providing",
744 "when",
745 "whenever",
746 "after",
747 "until",
748 "before",
749 "assuming",
750 "given",
751 "while",
752 "whether",
753 "depending",
754 "pending",
755 ];
756 const JA: &[&str] = &[
757 "かも",
758 "たぶん",
759 "多分",
760 "なら",
761 "たら",
762 "ちょっと待",
763 "しないで",
764 "しない",
765 "保留",
766 "まだ",
767 "ただし",
768 "やめ",
769 "いや",
770 "止め",
771 "だめ",
772 "ダメ",
773 "じゃない",
774 "ではない",
775 "ですか",
776 "かな",
777 "でしょう",
778 "思う",
779 "でも",
780 "けど",
781 "ただ",
782 "次第",
783 "場合",
784 "限り",
785 "条件",
786 "とき",
787 "まで",
788 "後で",
789 ];
790 if text.contains(['?', '?']) || JA.iter().any(|w| text.contains(w)) {
791 return true;
792 }
793 text.to_lowercase()
794 .replace('\u{2019}', "'")
795 .split(|c: char| !(c.is_alphanumeric() || c == '\'') || !c.is_ascii())
796 .filter(|w| !w.is_empty())
797 .any(|w| WORDS.contains(&w) || w.ends_with("n't"))
798}
799
800fn retracts(message: &str) -> bool {
803 let lower = message.to_lowercase();
804 [
805 "やっぱ",
806 "待って",
807 "撤回",
808 "never mind",
809 "actually",
810 "on second thought",
811 ]
812 .iter()
813 .any(|w| lower.contains(w))
814 || lower
815 .split(|c: char| !c.is_ascii_alphabetic())
816 .any(|w| w == "wait")
817}
818
819pub const APPROVAL_NODE: &str = "land-approval";
825
826pub const DIFF_MAX_LINES: usize = 400;
834
835#[derive(Debug, Clone, Copy, PartialEq, Eq)]
837pub enum Approval {
838 Merge,
840 Repair,
842 Hold,
844}
845
846pub fn approval(answer: Option<&str>) -> Approval {
854 match answer {
855 Some(a) if a.trim().eq_ignore_ascii_case(APPROVE) => Approval::Merge,
856 Some(a) if a.trim() == REPAIR => Approval::Repair,
857 _ => Approval::Hold,
858 }
859}
860
861#[derive(Debug, Clone, Copy, PartialEq, Eq)]
863enum ApprovalGate {
864 Approved,
866 Repair,
868 Held,
871 Pending,
873}
874
875fn esc(s: &str) -> String {
885 let mut out = String::with_capacity(s.len());
886 for c in s.chars() {
887 match c {
888 '&' => out.push_str("&"),
889 '<' => out.push_str("<"),
890 '>' => out.push_str(">"),
891 '"' => out.push_str("""),
892 '\'' => out.push_str("'"),
893 _ => out.push(c),
894 }
895 }
896 out
897}
898
899#[derive(Debug, Clone, PartialEq, Eq)]
901struct StatRow {
902 path: String,
903 added: Option<u64>,
905 removed: Option<u64>,
906}
907
908impl StatRow {
909 fn churn(&self) -> u64 {
912 self.added.unwrap_or(0) + self.removed.unwrap_or(0)
913 }
914}
915
916fn parse_numstat(numstat: &str) -> Vec<StatRow> {
922 let mut rows: Vec<StatRow> = numstat
923 .lines()
924 .filter_map(|line| {
925 let mut parts = line.splitn(3, '\t');
926 let added = parts.next()?.trim();
927 let removed = parts.next()?.trim();
928 let path = parts.next()?.trim();
929 if path.is_empty() {
930 return None;
931 }
932 Some(StatRow {
933 path: path.to_owned(),
934 added: added.parse().ok(),
935 removed: removed.parse().ok(),
936 })
937 })
938 .collect();
939 rows.sort_by(|a, b| b.churn().cmp(&a.churn()).then_with(|| a.path.cmp(&b.path)));
942 rows
943}
944
945fn diff_row(line: &str) -> (&'static str, &'static str, &str) {
954 if line.starts_with("+++") || line.starts_with("---") {
955 (" ", "color:#57606a;font-weight:600", line)
956 } else if let Some(body) = line.strip_prefix('+') {
957 ("+", "background:#e6ffec;color:#0a3622", body)
958 } else if let Some(body) = line.strip_prefix('-') {
959 ("-", "background:#ffebe9;color:#5c1a17", body)
960 } else if line.starts_with("@@") {
961 ("~", "background:#eef2ff;color:#3730a3", line)
962 } else if let Some(body) = line.strip_prefix(' ') {
963 (" ", "", body)
964 } else {
965 (" ", "color:#57606a;font-weight:600", line)
966 }
967}
968
969struct Words {
978 html_lang: &'static str,
979 task: &'static str,
980 what_changed: &'static str,
981 review_verdict: &'static str,
982 reviewer: &'static str,
983 reviewer_no_answer: &'static str,
984 checks: &'static str,
985 nothing_failing: &'static str,
986 files_changed: &'static str,
987 commits: &'static str,
988 no_commits: &'static str,
989 comments: &'static str,
990 no_comments: &'static str,
991 diff: &'static str,
992 truncated: &'static str,
993 lands_as: &'static str,
994}
995
996const EN: Words = Words {
997 html_lang: "en",
998 task: "Task",
999 what_changed: "What changed",
1000 review_verdict: "Review verdict",
1001 reviewer: "Reviewer",
1002 reviewer_no_answer: "produced no answer",
1003 checks: "Checks",
1004 nothing_failing: "Nothing failing.",
1005 files_changed: "file(s) changed",
1006 commits: "Commits being squashed",
1007 no_commits: "No commit subjects could be read from the branch.",
1008 comments: "Review comments",
1009 no_comments: "Nothing outstanding at this observation.",
1010 diff: "Diff",
1011 truncated: "Truncated",
1012 lands_as: "They land as one commit titled",
1013};
1014
1015const JA: Words = Words {
1016 html_lang: "ja",
1017 task: "タスク",
1018 what_changed: "変更内容",
1019 review_verdict: "レビューの結論",
1020 reviewer: "レビュアー",
1021 reviewer_no_answer: "回答なし",
1022 checks: "チェック",
1023 nothing_failing: "失敗しているものはありません。",
1024 files_changed: "ファイル変更",
1025 commits: "squash されるコミット",
1026 no_commits: "ブランチからコミット件名を読めませんでした。",
1027 comments: "レビューコメント",
1028 no_comments: "この時点で未対応のものはありません。",
1029 diff: "差分",
1030 truncated: "省略",
1031 lands_as: "これらは次の件名の1コミットとして入ります:",
1032};
1033
1034impl Words {
1035 fn lands_as_tail(&self) -> &'static str {
1039 if self.html_lang == "ja" {
1040 "。この件名も承認の対象です。"
1041 } else {
1042 ", which you are approving too."
1043 }
1044 }
1045
1046 fn approval_summary(&self, number: u64, subject: &str) -> String {
1048 if self.html_lang == "ja" {
1049 format!("プルリクエスト #{number} をマージ: {subject}")
1050 } else {
1051 format!("merge pull request #{number}: {subject}")
1052 }
1053 }
1054
1055 fn approval_detail(
1057 &self,
1058 url: &str,
1059 base: &str,
1060 subject: &str,
1061 contested: Option<&ContestedHandoff>,
1062 ) -> String {
1063 let body = if self.html_lang == "ja" {
1064 format!(
1065 "{url} はチェックが緑で、`{base}` へ `{subject}` として squash \
1066 できる状態です。差分の要約・パッチ・squash されるコミットは\
1067 下のパネルにあります。"
1068 )
1069 } else {
1070 format!(
1071 "{url} is green and ready to squash into `{base}` as `{subject}`. \
1072 The panel holds the diffstat, the patch and the commits being squashed."
1073 )
1074 };
1075 match contested {
1076 Some(c) => format!("{}\n\n{body}", self.contested_reason(url, c)),
1077 None => body,
1078 }
1079 }
1080
1081 fn contested_reason(&self, url: &str, c: &ContestedHandoff) -> String {
1084 const SHOWN: usize = 5;
1085 const TITLE_CHARS: usize = 100;
1086 let ja = self.html_lang == "ja";
1087 let mut out = if ja {
1088 format!(
1089 "{url} は、マージ承認がオフでも保留しています。レビューが予算切れで終わった\
1090 時点で、却下票を伴う重大な未解決の指摘が残っているためです。\n"
1091 )
1092 } else {
1093 format!(
1094 "{url} is held for approval although merge approvals are off: the \
1095 review ended with blocking findings still open and a reviewer \
1096 voting reject.\n"
1097 )
1098 };
1099 for f in c.findings.iter().take(SHOWN) {
1100 let at = match (&f.file, f.line) {
1101 (Some(file), Some(line)) => format!("{file}:{line}"),
1102 (Some(file), None) => file.clone(),
1103 _ => (if ja { "場所未指定" } else { "no location" }).to_owned(),
1104 };
1105 let title: String = f.title.chars().take(TITLE_CHARS).collect();
1106 let _ = writeln!(out, "- {} {} {at}: {title}", f.id, f.severity);
1107 }
1108 if c.findings.len() > SHOWN {
1109 let more = c.findings.len() - SHOWN;
1110 let _ = writeln!(
1111 out,
1112 "{}",
1113 if ja {
1114 format!("- ほか {more} 件")
1115 } else {
1116 format!("- and {more} more")
1117 }
1118 );
1119 }
1120 let seats: Vec<String> = c
1121 .rejecters
1122 .iter()
1123 .map(|(seat, agent)| format!("#{seat} ({agent})"))
1124 .collect();
1125 let _ = write!(
1126 out,
1127 "{} {}",
1128 if ja {
1129 "却下したレビュアー:"
1130 } else {
1131 "Rejected by reviewer:"
1132 },
1133 seats.join(", ")
1134 );
1135 out
1136 }
1137
1138 fn truncated_note(
1140 &self,
1141 omitted: usize,
1142 total: usize,
1143 shown: usize,
1144 where_: &str,
1145 base: &str,
1146 head: &str,
1147 ) -> String {
1148 if self.html_lang == "ja" {
1149 format!(
1150 "先頭 {shown} 行のあと、差分 {total} 行のうち {omitted} 行を省略しました。\
1151 全体は <code>{where_}</code>(<code>git diff {base}...{head}</code>)と\
1152 プルリクエストにあります。"
1153 )
1154 } else {
1155 format!(
1156 "{omitted} of {total} diff lines omitted after the first {shown}. \
1157 The whole patch is in <code>{where_}</code> \
1158 (<code>git diff {base}...{head}</code>) and on the pull request."
1159 )
1160 }
1161 }
1162}
1163
1164fn words(language: &str) -> &'static Words {
1167 if crate::lang::is_japanese(language) {
1168 &JA
1169 } else {
1170 &EN
1171 }
1172}
1173
1174pub fn approval_panel(
1186 state: &RunState,
1187 pr: &PrState,
1188 diffstat: &str,
1189 diff: &str,
1190 commits: &[String],
1191 subject: &str,
1192) -> String {
1193 let rows = parse_numstat(diffstat);
1194 let w = words(&state.config.graph.language);
1195 let mut h = String::with_capacity(4_096 + diff.len().min(200_000));
1196
1197 let _ = writeln!(
1198 h,
1199 "<!doctype html>\n<html lang=\"{}\">\n<head>\n<meta charset=\"utf-8\">\n\
1200 <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">",
1201 w.html_lang
1202 );
1203 let _ = writeln!(
1204 h,
1205 "<title>merge #{} — {}</title>\n</head>",
1206 pr.number,
1207 esc(subject)
1208 );
1209 h.push_str(
1210 "<body style=\"margin:0;padding:12px;font:15px/1.5 -apple-system,\
1211 'Segoe UI',system-ui,sans-serif;color:#1f2328;background:#fff;\
1212 word-break:break-word\">\n",
1213 );
1214
1215 let _ = writeln!(
1217 h,
1218 "<h1 style=\"margin:0 0 4px;font-size:19px\">Merge #{} into \
1219 <code style=\"background:#f6f8fa;padding:1px 4px;border-radius:4px\">{}</code></h1>\n\
1220 <p style=\"margin:0 0 4px;font-size:17px;font-weight:600\">{}</p>\n\
1221 <p style=\"margin:0 0 12px;font-size:13px;color:#57606a\">squash merge · run {} · \
1222 <a href=\"{}\" target=\"_blank\" rel=\"noopener noreferrer\" style=\"color:#0969da\">{}</a></p>",
1223 pr.number,
1224 esc(&state.base_branch),
1225 esc(subject),
1226 esc(&state.id),
1227 esc(&pr.url),
1228 esc(&pr.url),
1229 );
1230
1231 let _ = writeln!(
1234 h,
1235 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>\n\
1236 <p style=\"margin:0;font-size:13px;white-space:pre-wrap\">{}</p>",
1237 w.task,
1238 esc(&state.instruction)
1239 );
1240
1241 if let Some(summary) = state
1243 .winner()
1244 .map(|c| c.summary.as_str())
1245 .filter(|s| !s.is_empty())
1246 {
1247 let _ = writeln!(
1248 h,
1249 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>\n\
1250 <p style=\"margin:0;font-size:13px;white-space:pre-wrap\">{}</p>",
1251 w.what_changed,
1252 esc(summary)
1253 );
1254 }
1255
1256 if let Some(round) = state.reviews.last() {
1259 let _ = writeln!(
1260 h,
1261 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1262 w.review_verdict
1263 );
1264 for r in &round.reviews {
1265 let body = match &r.failed {
1272 Some(reason) => format!("{}: {}", w.reviewer_no_answer, esc(reason)),
1273 None => esc(&r.summary),
1274 };
1275 let _ = writeln!(
1276 h,
1277 "<div style=\"margin:0 0 8px;padding:8px;background:#f6f8fa;\
1278 border-radius:6px\">\
1279 <div style=\"font-size:12px;color:#57606a\">{} {} · {}</div>\
1280 <div style=\"white-space:pre-wrap;font-size:13px\">{}</div></div>",
1281 w.reviewer,
1282 r.reviewer,
1283 esc(&r.agent),
1284 body,
1285 );
1286 }
1287 }
1288
1289 let _ = writeln!(
1290 h,
1291 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}: {}</h2>",
1292 w.checks,
1293 esc(pr.checks.as_str())
1294 );
1295 if pr.failing.is_empty() {
1296 let _ = writeln!(
1297 h,
1298 "<p style=\"margin:0;font-size:13px;color:#57606a\">{}</p>",
1299 w.nothing_failing
1300 );
1301 } else {
1302 h.push_str("<ul style=\"margin:0;padding-left:20px;font-size:13px\">\n");
1303 for f in &pr.failing {
1304 let _ = writeln!(h, "<li>{}</li>", esc(f));
1305 }
1306 h.push_str("</ul>\n");
1307 }
1308
1309 let _ = writeln!(
1312 h,
1313 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{} {}</h2>",
1314 rows.len(),
1315 w.files_changed
1316 );
1317 h.push_str(
1318 "<table style=\"width:100%;border-collapse:collapse;font-size:13px\">\n\
1319 <thead><tr>\
1320 <th style=\"text-align:left;border-bottom:1px solid #d0d7de;padding:4px 2px\">file</th>\
1321 <th style=\"text-align:right;border-bottom:1px solid #d0d7de;padding:4px 2px\">added</th>\
1322 <th style=\"text-align:right;border-bottom:1px solid #d0d7de;padding:4px 2px\">removed\
1323 </th></tr></thead>\n<tbody>\n",
1324 );
1325 let mut total_added = 0u64;
1326 let mut total_removed = 0u64;
1327 for r in &rows {
1328 total_added += r.added.unwrap_or(0);
1329 total_removed += r.removed.unwrap_or(0);
1330 let cell = |n: Option<u64>| match n {
1331 Some(n) => n.to_string(),
1332 None => "bin".to_owned(),
1333 };
1334 let _ = writeln!(
1335 h,
1336 "<tr>\
1337 <td style=\"padding:4px 2px;border-bottom:1px solid #eaeef2;\
1338 font-family:ui-monospace,monospace\">{}</td>\
1339 <td style=\"padding:4px 2px;border-bottom:1px solid #eaeef2;text-align:right;\
1340 color:#0a3622\">{}</td>\
1341 <td style=\"padding:4px 2px;border-bottom:1px solid #eaeef2;text-align:right;\
1342 color:#5c1a17\">{}</td></tr>",
1343 esc(&r.path),
1344 cell(r.added),
1345 cell(r.removed),
1346 );
1347 }
1348 let _ = writeln!(
1349 h,
1350 "</tbody>\n<tfoot><tr style=\"font-weight:600\">\
1351 <td style=\"padding:4px 2px\">total</td>\
1352 <td style=\"padding:4px 2px;text-align:right\">{total_added}</td>\
1353 <td style=\"padding:4px 2px;text-align:right\">{total_removed}</td>\
1354 </tr></tfoot>\n</table>"
1355 );
1356
1357 let _ = writeln!(
1359 h,
1360 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1361 w.commits
1362 );
1363 if commits.is_empty() {
1364 h.push_str(&format!(
1365 "<p style=\"margin:0;font-size:13px;color:#57606a\">{}</p>\n",
1366 w.no_commits
1367 ));
1368 } else {
1369 h.push_str("<ol style=\"margin:0;padding-left:20px;font-size:13px\">\n");
1370 for c in commits {
1371 let _ = writeln!(h, "<li>{}</li>", esc(c));
1372 }
1373 h.push_str("</ol>\n");
1374 }
1375 let _ = writeln!(
1376 h,
1377 "<p style=\"margin:8px 0 0;font-size:13px\">{} <strong>{}</strong>{}</p>",
1378 w.lands_as,
1379 esc(subject),
1380 w.lands_as_tail()
1381 );
1382
1383 let _ = writeln!(
1385 h,
1386 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1387 w.comments
1388 );
1389 if pr.review_comments.is_empty() {
1390 h.push_str(&format!(
1391 "<p style=\"margin:0;font-size:13px;color:#57606a\">{}</p>\n",
1392 w.no_comments
1393 ));
1394 } else {
1395 for c in &pr.review_comments {
1396 let anchor = match (&c.path, c.line) {
1397 (Some(p), Some(l)) => format!("{p}:{l}"),
1398 (Some(p), None) => p.clone(),
1399 _ => "pull request thread".to_owned(),
1400 };
1401 let _ = writeln!(
1402 h,
1403 "<div style=\"margin:0 0 8px;padding:8px;background:#f6f8fa;border-radius:6px\">\
1404 <div style=\"font-size:12px;color:#57606a\">{} · {}</div>\
1405 <div style=\"white-space:pre-wrap;font-size:13px\">{}</div></div>",
1406 esc(&c.author),
1407 esc(&anchor),
1408 esc(&tail(&c.body, 800)),
1409 );
1410 }
1411 }
1412
1413 let total = diff.lines().count();
1415 let shown = total.min(DIFF_MAX_LINES);
1416 let _ = writeln!(
1417 h,
1418 "<h2 style=\"margin:16px 0 6px;font-size:15px\">{}</h2>",
1419 w.diff
1420 );
1421 h.push_str(
1422 "<div style=\"font:12px/1.45 ui-monospace,SFMono-Regular,Menlo,monospace;\
1423 border:1px solid #d0d7de;border-radius:6px;overflow-x:auto\">\n",
1424 );
1425 for line in diff.lines().take(shown) {
1426 let (gutter, style, body) = diff_row(line);
1427 let _ = writeln!(
1428 h,
1429 "<div style=\"display:flex;{style}\">\
1430 <span style=\"flex:0 0 1.4em;text-align:center;user-select:none;\
1431 border-right:1px solid #d0d7de\">{gutter}</span>\
1432 <span style=\"white-space:pre;padding-left:6px\">{}</span></div>",
1433 esc(body),
1434 );
1435 }
1436 h.push_str("</div>\n");
1437 if total > shown {
1438 let omitted = total - shown;
1439 let head = state.winner().map_or("HEAD", |w| w.branch.as_str());
1440 let where_ = state.winner().map_or_else(
1441 || state.repo.display().to_string(),
1442 |w| w.worktree.display().to_string(),
1443 );
1444 let _ = writeln!(
1445 h,
1446 "<p style=\"margin:8px 0 0;padding:8px;background:#fff8c5;border-radius:6px;\
1447 font-size:13px\">{}: {}</p>",
1448 w.truncated,
1449 w.truncated_note(
1450 omitted,
1451 total,
1452 shown,
1453 &esc(&where_),
1454 &esc(&state.base_branch),
1455 &esc(head),
1456 ),
1457 );
1458 }
1459
1460 h.push_str("</body>\n</html>\n");
1461 h
1462}
1463
1464fn contested_to_ask(state: &RunState) -> Option<ContestedHandoff> {
1468 if state.config.graph.hold_contested_merge {
1469 state.contested_handoff.clone()
1470 } else {
1471 None
1472 }
1473}
1474
1475pub fn deputy_brief(q: &ask::Question, state: Option<&RunState>) -> String {
1484 let mut s = format!(
1485 "This is the merge approval for run {run} (`magi show {run}`). The question's \
1486 own text above names the pull request. Answering `{APPROVE}` squash-merges \
1487 it into the base branch, which cannot be undone; `{HOLD}` leaves the pull \
1488 request open. Silence is a hold: the owner not answering never merges. Only \
1489 the owner choosing `{APPROVE}`, or clearly telling you to merge in their \
1490 own words, merges. Whether their wording is a clear, unconditional instruction \
1491 is your judgement alone: if it is doubtful, conditional, retracted or a \
1492 question, do not settle - ask back with `magi ask --thread`. Doubt and \
1493 silence are a hold.\n\n\
1494 This brief is a snapshot from when you were attached: check `magi show {run}` \
1495 and `gh pr view` (read-only) before telling the owner anything current. \
1496 You run with permission to write the question record, and what keeps you \
1497 from touching anything else is this brief and your instructions - so do \
1498 not change files, branches or the pull request.",
1499 run = q.run
1500 );
1501 if let Some(target) = &q.merge_target {
1502 s.push_str(&format!("\n\nThis decision names PR {} at head `{}`. Merge through chat requires the owner's latest whole message and quote to be exactly `merge {}`. Discussion alone never approves. For hold, repair, withdrawal or replacement, use the exact offered token from the owner's latest whole message. 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 yourself.", target.pr, target.head, target.head));
1503 }
1504 if q.choices.iter().any(|c| c == REPAIR) {
1505 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. Settle this choice only when the owner's whole latest message is exactly this label; otherwise discuss it with `--thread`. The driver performs the repair; do not edit or queue another task. Updated review and gate results require a fresh merge decision.");
1506 }
1507 let Some(state) = state else {
1508 s.push_str(
1509 "\n\nThe run's record could not be read, so the pull request, the panel \
1510 summary and any contested findings are not known to you beyond the \
1511 question's own text. Say so to the owner rather than guessing.",
1512 );
1513 return s;
1514 };
1515 if let Some(pr) = &state.pr {
1516 s.push_str(&format!(
1517 "\n\nPull request #{} {} (recorded state: {}, last seen).",
1518 pr.number, pr.url, pr.state
1519 ));
1520 }
1521 s.push_str(&format!("\nBase branch: `{}`.", state.base_branch));
1522 if let Some(w) = state.winner() {
1523 s.push_str(&format!("\nWinning branch: `{}`.", w.branch));
1524 }
1525 match contested_to_ask(state) {
1526 Some(c) => {
1527 s.push_str(
1528 "\n\nThis question was filed although merge approvals are off, because \
1529 the review hand-off is contested. Open findings:",
1530 );
1531 for f in &c.findings {
1532 let at = match (&f.file, f.line) {
1533 (Some(file), Some(line)) => format!(" ({file}:{line})"),
1534 (Some(file), None) => format!(" ({file})"),
1535 _ => String::new(),
1536 };
1537 s.push_str(&format!("\n- [{}] {}{at}: {}", f.id, f.severity, f.title));
1538 }
1539 let seats: Vec<String> = c.rejecters.iter().map(|(n, _)| format!("#{n}")).collect();
1540 s.push_str(&format!("\nReviewers who rejected: {}.", seats.join(", ")));
1541 }
1542 None => s.push_str("\n\nThe review hand-off was not recorded as contested."),
1543 }
1544 s
1545}
1546
1547fn merge_consent_superseded(state: &RunState, question: &ask::Question) -> Result<bool> {
1551 if !question.status.open() && approval(question.resolution().as_deref()) != Approval::Merge {
1552 return Ok(false);
1553 }
1554 let Some(task_id) = state
1555 .origin
1556 .as_ref()
1557 .and_then(|origin| origin.task.as_ref())
1558 else {
1559 return Ok(false);
1560 };
1561 let task = crate::queue::Queue::open()
1562 .get(task_id)
1563 .context("cannot verify operator decisions for the merge approval")?;
1564 Ok(task
1565 .operator_decided_at
1566 .is_some_and(|at| at > question.answered_at.unwrap_or(question.asked_at)))
1567}
1568
1569async fn approval_gate(
1589 state: &mut RunState,
1590 pr: &PrState,
1591 subject: &str,
1592 contested: Option<&ContestedHandoff>,
1593 head: &str,
1594) -> Result<ApprovalGate> {
1595 let store = ask::Questions::open();
1596 let reusable = state.land_approval.as_ref().and_then(|a| {
1601 store
1602 .list()
1603 .into_iter()
1604 .find(|q| q.id == a.question)
1605 .filter(|q| {
1606 a.head.eq_ignore_ascii_case(head)
1607 || q.resolution()
1608 .as_deref()
1609 .is_some_and(crate::merge_decision::stops)
1610 })
1611 });
1612 let reusable = match reusable {
1613 Some(q) if merge_consent_superseded(state, &q)? => None,
1614 other => other,
1615 };
1616 if reusable.is_none() {
1617 for stale in store
1618 .list()
1619 .into_iter()
1620 .filter(|q| q.run == state.id && q.node == APPROVAL_NODE && q.status.open())
1621 {
1622 let why =
1623 "the pull request head or operator decision changed; asked again for approval";
1624 if let Err(e) = store.update(&stale.id, |q| {
1625 q.abandon(why);
1626 Ok(())
1627 }) {
1628 tracing::warn!("could not retire the superseded approval question: {e:#}");
1629 }
1630 }
1631 }
1632
1633 let q = match reusable {
1634 Some(mut q) => {
1635 if upgrade_repair_choice(&mut q, state) {
1636 q = store
1637 .update(&q.id, |current| {
1638 upgrade_repair_choice(current, state);
1639 Ok(())
1640 })?
1641 .0;
1642 }
1643 q
1644 }
1645 None => {
1646 let worktree = match state.winner() {
1647 Some(w) => w.worktree.clone(),
1648 None => state.repo.clone(),
1649 };
1650 let head = if head.is_empty() {
1653 state
1654 .winner()
1655 .map_or_else(|| "HEAD".to_owned(), |w| w.branch.clone())
1656 } else {
1657 head.to_owned()
1658 };
1659 let remote = &state.config.merge.remote;
1662 let tracking = format!("{remote}/{}", state.base_branch);
1663 let base = if git::rev_exists(&worktree, &tracking).await {
1664 tracking
1665 } else {
1666 String::new()
1667 };
1668 let range = format!("{base}...{head}");
1669 let numstat = if base.is_empty() {
1673 String::new()
1674 } else {
1675 git::git_raw(&worktree, &["diff", "--numstat", "-M", &range])
1676 .await
1677 .map(|o| o.stdout)
1678 .unwrap_or_default()
1679 };
1680 let diff = if base.is_empty() {
1681 String::new()
1682 } else {
1683 git::diff(&worktree, &base, &head).await.unwrap_or_default()
1684 };
1685 let commits: Vec<String> = if base.is_empty() {
1686 Vec::new()
1687 } else {
1688 git::git_raw(
1689 &worktree,
1690 &[
1691 "log",
1692 "--reverse",
1693 "--format=%s",
1694 &format!("{base}..{head}"),
1695 ],
1696 )
1697 .await
1698 .map(|o| o.stdout)
1699 .unwrap_or_default()
1700 .lines()
1701 .filter(|l| !l.trim().is_empty())
1702 .map(str::to_owned)
1703 .collect()
1704 };
1705
1706 let w = words(&state.config.graph.language);
1707 let html = approval_panel(state, pr, &numstat, &diff, &commits, subject);
1708 let mut fresh = ask::Question::new(
1709 state.id.clone(),
1710 APPROVAL_NODE.to_owned(),
1711 "land".to_owned(),
1712 w.approval_summary(pr.number, subject),
1713 w.approval_detail(&pr.url, &state.base_branch, subject, contested),
1714 approval_choices(state),
1715 );
1716 fresh.merge_target = Some(crate::merge_decision::Target {
1717 pr: pr.url.clone(),
1718 head: head.clone(),
1719 language: state.config.graph.language.clone(),
1720 });
1721 if fresh.choices.iter().any(|c| c == REPAIR) {
1722 fresh.actions.insert(
1723 REPAIR.to_owned(),
1724 ask::ChoiceAction::Resume {
1725 run: state.id.clone(),
1726 },
1727 );
1728 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.");
1729 }
1730 for choice in fresh.choices.clone() {
1731 if crate::merge_decision::stops(&choice)
1732 && choice != HOLD
1733 && choice != crate::merge_decision::DISMISS
1734 {
1735 fresh.actions.insert(
1736 choice,
1737 ask::ChoiceAction::Resume {
1738 run: state.id.clone(),
1739 },
1740 );
1741 }
1742 }
1743 store
1744 .put_panel(&mut fresh, &html, &[])
1745 .context("write the merge approval panel")?;
1746 store
1747 .put(&mut fresh)
1748 .context("file the merge approval question")?;
1749 state.land_approval = Some(LandApproval {
1750 question: fresh.id.clone(),
1751 head: head.clone(),
1752 });
1753 state.event(
1754 "land",
1755 format!("asking for merge approval ({})", fresh.short()),
1756 );
1757 state.save()?;
1758 if let Err(e) = ask::notify(&state.config.notify, &fresh).await {
1759 tracing::warn!(
1763 "could not notify about merge approval question {}: {e:#} - \
1764 the web UI is the only surface for it now",
1765 fresh.short()
1766 );
1767 }
1768 fresh
1769 }
1770 };
1771
1772 Ok(match q.status {
1773 ask::QuestionStatus::Open => ApprovalGate::Pending,
1774 ask::QuestionStatus::Abandoned => ApprovalGate::Held,
1778 ask::QuestionStatus::Answered => match approval(q.resolution().as_deref()) {
1782 Approval::Merge => ApprovalGate::Approved,
1783 Approval::Repair if !state.reviews.is_empty() => {
1784 if !state.merge_repairs.iter().any(|r| r.question == q.id) {
1785 state.merge_repairs.push(crate::run::MergeRepair {
1786 question: q.id.clone(),
1787 head: head.to_owned(),
1788 pr: pr.url.clone(),
1789 reviews: state.reviews.clone(),
1790 gate: state.gate.clone(),
1791 gate_head: state.gate_head.clone(),
1792 gate_fixes: state.gate_fixes.clone(),
1793 pre_gate: state.pre_gate.clone(),
1794 phase: crate::run::MergeRepairPhase::Requested,
1795 failure: None,
1796 });
1797 state.land_approval = None;
1798 state.event(
1799 "repair",
1800 "owner requested fix findings and re-review; merge is not approved",
1801 );
1802 state.save()?;
1803 }
1804 ApprovalGate::Repair
1805 }
1806 Approval::Repair | Approval::Hold => ApprovalGate::Held,
1807 },
1808 })
1809}
1810
1811fn rollup_verdict(rollup: &[GhCheck]) -> (Checks, Vec<String>) {
1814 let mut failing = Vec::new();
1815 let mut pending = false;
1816 let mut unknown = false;
1817 for check in rollup {
1818 match check.verdict() {
1819 Verdict::Pass => {}
1820 Verdict::Pending => pending = true,
1821 Verdict::Fail => failing.push(check.label()),
1822 Verdict::Unknown => unknown = true,
1823 }
1824 }
1825 let checks = if rollup.is_empty() {
1826 Checks::Unknown
1827 } else if pending {
1828 Checks::Pending
1829 } else if !failing.is_empty() {
1830 Checks::Red
1831 } else if unknown {
1832 Checks::Unknown
1833 } else {
1834 Checks::Green
1835 };
1836 (checks, failing)
1837}
1838
1839pub fn parse_pr(json: &str) -> Result<PrState> {
1842 let raw: GhPr = serde_json::from_str(json).context("parse `gh pr view --json ...` output")?;
1843 let state = match raw.state.to_ascii_uppercase().as_str() {
1844 "OPEN" => PrLifecycle::Open,
1845 "MERGED" => PrLifecycle::Merged,
1846 "CLOSED" => PrLifecycle::Closed,
1847 other => bail!("unknown pull request state `{other}`"),
1848 };
1849
1850 let (checks, failing) = rollup_verdict(&raw.status_check_rollup);
1851
1852 let mut review_comments = Vec::new();
1853 for r in raw.reviews {
1854 push_if_outstanding(
1855 &mut review_comments,
1856 ReviewComment {
1857 author: r.author.login,
1858 path: None,
1859 line: None,
1860 body: r.body,
1861 },
1862 );
1863 }
1864 for c in raw.comments {
1865 push_if_outstanding(
1866 &mut review_comments,
1867 ReviewComment {
1868 author: c.author.login,
1869 path: None,
1870 line: None,
1871 body: c.body,
1872 },
1873 );
1874 }
1875
1876 Ok(PrState {
1877 url: raw.url,
1878 number: raw.number,
1879 state,
1880 checks,
1881 failing,
1882 review_comments,
1883 blocking: Blocking::of(&raw.merge_state_status),
1884 })
1885}
1886
1887#[derive(Debug, Clone, PartialEq, Eq)]
1889pub(crate) struct CheckView {
1890 pub name: String,
1891 pub verdict: Verdict,
1892 pub run: Option<String>,
1894 pub url: Option<String>,
1895}
1896
1897#[derive(Debug, Clone, PartialEq, Eq)]
1901pub(crate) struct RollupView {
1902 pub url: String,
1903 pub number: u64,
1904 pub state: PrLifecycle,
1905 pub head: String,
1906 pub checks: Vec<CheckView>,
1907}
1908
1909pub(crate) fn parse_rollup(json: &str) -> Result<RollupView> {
1912 let raw: GhPr = serde_json::from_str(json).context("parse `gh pr view --json ...` output")?;
1913 let state = match raw.state.to_ascii_uppercase().as_str() {
1914 "OPEN" => PrLifecycle::Open,
1915 "MERGED" => PrLifecycle::Merged,
1916 "CLOSED" => PrLifecycle::Closed,
1917 other => bail!("unknown pull request state `{other}`"),
1918 };
1919 let checks = raw
1920 .status_check_rollup
1921 .iter()
1922 .map(|c| CheckView {
1923 name: c.label(),
1924 verdict: c.verdict(),
1925 run: c.url().and_then(run_of),
1926 url: c.url().map(str::to_owned),
1927 })
1928 .collect();
1929 Ok(RollupView {
1930 url: raw.url,
1931 number: raw.number,
1932 state,
1933 head: raw.head_ref_oid,
1934 checks,
1935 })
1936}
1937
1938pub async fn lifecycle(repo: &Path, pr_url: &str) -> Result<PrLifecycle> {
1948 let view = gh(
1949 repo,
1950 &[
1951 "pr".to_owned(),
1952 "view".to_owned(),
1953 pr_url.to_owned(),
1954 "--json".to_owned(),
1955 "state".to_owned(),
1956 ],
1957 )
1958 .await?;
1959 if !view.0 {
1960 bail!("gh pr view {pr_url}: {}", view.1);
1961 }
1962 Ok(parse_pr(&view.1)?.state)
1966}
1967
1968#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1972pub struct ExternalMerge {
1973 pub url: String,
1975 pub number: u64,
1977}
1978
1979#[derive(Debug, Deserialize)]
1980#[serde(rename_all = "camelCase")]
1981struct GhMergedPr {
1982 url: String,
1983 number: u64,
1984 merged_at: String,
1985 base_ref_name: String,
1986}
1987
1988fn pick_merged_pr(
2002 json: &str,
2003 base_branch: &str,
2004 created_at: Timestamp,
2005) -> Result<Option<ExternalMerge>> {
2006 let raw: Vec<GhMergedPr> =
2007 serde_json::from_str(json).context("parse `gh pr list ... --json ...` output")?;
2008 let mut matches: Vec<ExternalMerge> = Vec::new();
2009 for pr in raw {
2010 if pr.base_ref_name != base_branch {
2011 continue;
2012 }
2013 let Ok(merged_at) = pr.merged_at.parse::<Timestamp>() else {
2014 continue;
2015 };
2016 if merged_at < created_at {
2017 continue;
2018 }
2019 matches.push(ExternalMerge {
2020 url: pr.url,
2021 number: pr.number,
2022 });
2023 }
2024 if matches.len() == 1 {
2025 Ok(matches.pop())
2026 } else {
2027 Ok(None)
2028 }
2029}
2030
2031#[derive(Debug, Clone, PartialEq, Eq)]
2033pub enum OpenPr {
2034 None,
2036 One {
2038 url: String,
2040 title: String,
2042 },
2043 Many(Vec<String>),
2045}
2046
2047#[derive(Debug, Deserialize)]
2048#[serde(rename_all = "camelCase")]
2049struct GhOpenPr {
2050 url: String,
2054 #[serde(default)]
2055 title: String,
2056 base_ref_name: String,
2057 #[serde(default)]
2060 is_cross_repository: bool,
2061}
2062
2063pub fn pick_open_pr(json: &str, base: &str) -> Result<OpenPr> {
2069 let raw: Vec<GhOpenPr> =
2070 serde_json::from_str(json).context("parse `gh pr list ... --json ...` output")?;
2071 let mut hits: Vec<GhOpenPr> = raw
2072 .into_iter()
2073 .filter(|p| p.base_ref_name == base && !p.is_cross_repository)
2074 .collect();
2075 Ok(match hits.len() {
2076 0 => OpenPr::None,
2077 1 => {
2078 let p = hits.remove(0);
2079 OpenPr::One {
2080 url: p.url,
2081 title: p.title,
2082 }
2083 }
2084 _ => OpenPr::Many(hits.into_iter().map(|p| p.url).collect()),
2085 })
2086}
2087
2088pub async fn find_open_pr(repo: &Path, branch: &str, base: &str) -> Result<OpenPr> {
2092 let (ok, out) = gh(
2093 repo,
2094 &[
2095 "pr".to_owned(),
2096 "list".to_owned(),
2097 "--head".to_owned(),
2098 branch.to_owned(),
2099 "--base".to_owned(),
2100 base.to_owned(),
2101 "--state".to_owned(),
2102 "open".to_owned(),
2103 "--json".to_owned(),
2104 "number,url,title,baseRefName,isCrossRepository".to_owned(),
2105 ],
2106 )
2107 .await?;
2108 if !ok {
2109 bail!("gh pr list failed: {out}");
2110 }
2111 pick_open_pr(&out, base)
2112}
2113
2114#[derive(Debug, Deserialize)]
2115#[serde(rename_all = "camelCase")]
2116struct GhPrHead {
2117 head_ref_name: String,
2118 base_ref_name: String,
2119 state: String,
2120 is_cross_repository: bool,
2123 head_ref_oid: String,
2126}
2127
2128#[derive(Debug, Clone, PartialEq, Eq)]
2130pub struct Refusal {
2131 pub retry: bool,
2134 pub why: String,
2136}
2137
2138impl Refusal {
2139 fn final_(why: String) -> Self {
2140 Self { retry: false, why }
2141 }
2142}
2143
2144pub fn closable(
2153 json: &str,
2154 branch: &str,
2155 base: &str,
2156 verified: &[String],
2157) -> std::result::Result<(), Refusal> {
2158 let pr: GhPrHead = serde_json::from_str(json).map_err(|e| Refusal {
2159 retry: true,
2160 why: format!("could not read the pull request ({e})"),
2161 })?;
2162 if pr.head_ref_name != branch {
2163 return Err(Refusal::final_(format!(
2164 "its head is `{}`, not this run's `{branch}`",
2165 pr.head_ref_name
2166 )));
2167 }
2168 if pr.is_cross_repository {
2169 return Err(Refusal::final_("its head lives in a fork".to_owned()));
2170 }
2171 if pr.base_ref_name != base {
2172 return Err(Refusal::final_(format!(
2173 "it targets `{}`, not `{base}`",
2174 pr.base_ref_name
2175 )));
2176 }
2177 if !pr.state.eq_ignore_ascii_case("open") {
2178 return Err(Refusal::final_(format!(
2179 "it is already {}",
2180 pr.state.to_ascii_lowercase()
2181 )));
2182 }
2183 if !verified.contains(&pr.head_ref_oid) {
2187 return Err(Refusal {
2188 retry: true,
2189 why: format!(
2190 "its head {} is not a commit this run checked against the base",
2191 crate::already::short_sha(&pr.head_ref_oid)
2192 ),
2193 });
2194 }
2195 Ok(())
2196}
2197
2198async fn remote_is_forge(repo: &Path, remote: &str) -> bool {
2203 let Ok(url) = git::git(repo, &["remote", "get-url", remote]).await else {
2204 return false;
2205 };
2206 is_forge_url(url.trim())
2207}
2208
2209fn is_forge_url(url: &str) -> bool {
2210 url.contains("://") && !url.starts_with("file://")
2211 || url
2212 .split_once(':')
2213 .is_some_and(|(host, _)| host.contains('@') && !host.contains(['/', '\\']))
2214}
2215
2216fn forge_unavailable(message: &str) -> bool {
2219 message.contains("known GitHub host") || message.contains("spawn gh")
2220}
2221
2222pub fn superseded_comment(base: &str, evidence: &crate::already::Evidence) -> String {
2225 let how = match evidence.proof {
2226 crate::already::Proof::PatchId => format!(
2227 "carried by commit {} on `{base}` with the same patch",
2228 evidence.names()
2229 ),
2230 crate::already::Proof::Ancestry => {
2231 format!("already in the history of `{base}` as {}", evidence.names())
2232 }
2233 crate::already::Proof::Tree => format!(
2234 "already part of `{base}` (merging this branch changes nothing at {})",
2235 crate::already::short_sha(&evidence.tip)
2236 ),
2237 };
2238 format!(
2239 "Closing: everything this branch adds is {how}, so there is nothing left to \
2240 land. This pull request was closed automatically after that was verified; \
2241 reopen it if you disagree."
2242 )
2243}
2244
2245pub async fn close_superseded_pr(
2257 state: &mut RunState,
2258 branch: &str,
2259 evidence: &crate::already::Evidence,
2260 verified: &[String],
2261) -> Result<std::result::Result<String, String>> {
2262 let repo = state.repo.clone();
2263 let base = state.base_branch.clone();
2264 let url = match state.pr.as_ref().filter(|p| p.state == "open") {
2265 Some(p) => p.url.clone(),
2266 None if !remote_is_forge(&repo, &state.config.merge.remote).await => {
2271 return Ok(Err(
2272 "the remote is not a forge, so there is no pull request".to_owned(),
2273 ));
2274 }
2275 None => match find_open_pr(&repo, branch, &base).await {
2276 Err(e) if forge_unavailable(&format!("{e:#}")) => {
2277 return Ok(Err(format!("no forge to ask: {e:#}")));
2278 }
2279 Err(e) => return Err(e),
2280 Ok(OpenPr::One { url, .. }) => url,
2281 Ok(OpenPr::None) => return Ok(Err("no open pull request".to_owned())),
2282 Ok(OpenPr::Many(urls)) => {
2283 return Ok(Err(format!(
2284 "{} open pull requests name it; not choosing between them",
2285 urls.len()
2286 )));
2287 }
2288 },
2289 };
2290 let (ok, view) = gh(
2291 &repo,
2292 &[
2293 "pr".to_owned(),
2294 "view".to_owned(),
2295 url.clone(),
2296 "--json".to_owned(),
2297 "headRefName,headRefOid,baseRefName,state,isCrossRepository".to_owned(),
2298 ],
2299 )
2300 .await?;
2301 if !ok {
2302 bail!("gh pr view {url} failed: {view}");
2303 }
2304 if let Err(refusal) = closable(&view, branch, &base, verified) {
2305 if refusal.retry {
2308 bail!("left {url} open: {}", refusal.why);
2309 }
2310 return Ok(Err(format!("left {url} open: {}", refusal.why)));
2311 }
2312 let (ok, out) = gh(
2313 &repo,
2314 &[
2315 "pr".to_owned(),
2316 "close".to_owned(),
2317 url.clone(),
2318 "--comment".to_owned(),
2319 superseded_comment(&base, evidence),
2320 ],
2321 )
2322 .await?;
2323 if !ok {
2324 bail!("gh pr close {url} failed: {out}");
2325 }
2326 if let Some(p) = state.pr.as_mut().filter(|p| p.url == url) {
2327 p.state = "closed".to_owned();
2328 }
2329 Ok(Ok(url))
2330}
2331
2332pub async fn set_pr_title(state: &mut RunState, repo: &Path, url: &str, title: &str) -> Result<()> {
2336 let (title, _) = crate::github_text::prepare(state, title, "");
2337 edit_pr_title(repo, url, &title).await
2338}
2339
2340pub async fn set_pr_title_verbatim(repo: &Path, url: &str, title: &str) -> Result<()> {
2344 edit_pr_title(repo, url, title).await
2345}
2346
2347pub async fn set_pr_text_verbatim(repo: &Path, url: &str, title: &str, body: &str) -> Result<()> {
2350 let (ok, _) = gh(
2351 repo,
2352 &[
2353 "pr".to_owned(),
2354 "edit".to_owned(),
2355 url.to_owned(),
2356 "--title".to_owned(),
2357 title.to_owned(),
2358 "--body".to_owned(),
2359 body.to_owned(),
2360 ],
2361 )
2362 .await
2363 .map_err(|_| anyhow::anyhow!("could not run gh pr edit for the chosen rewrite"))?;
2364 if !ok {
2365 bail!("gh pr edit failed while posting the chosen rewrite");
2367 }
2368 Ok(())
2369}
2370
2371async fn edit_pr_title(repo: &Path, url: &str, title: &str) -> Result<()> {
2372 let (ok, out) = gh(
2373 repo,
2374 &[
2375 "pr".to_owned(),
2376 "edit".to_owned(),
2377 url.to_owned(),
2378 "--title".to_owned(),
2379 title.to_owned(),
2380 ],
2381 )
2382 .await?;
2383 if !ok {
2384 bail!("gh pr edit failed: {out}");
2385 }
2386 Ok(())
2387}
2388
2389pub async fn find_external_merge(state: &RunState) -> Result<Option<ExternalMerge>> {
2402 let Some(winner) = state.winner() else {
2403 return Ok(None);
2404 };
2405 let branch = state.pr_branch().unwrap_or(&winner.branch).to_owned();
2406 let out = gh(
2407 &state.repo,
2408 &[
2409 "pr".to_owned(),
2410 "list".to_owned(),
2411 "--head".to_owned(),
2412 branch.clone(),
2413 "--state".to_owned(),
2414 "merged".to_owned(),
2415 "--json".to_owned(),
2416 "url,number,mergedAt,baseRefName".to_owned(),
2417 ],
2418 )
2419 .await?;
2420 if !out.0 {
2421 bail!("gh pr list --head {branch}: {}", out.1);
2422 }
2423 pick_merged_pr(&out.1, &state.base_branch, state.created_at)
2424}
2425
2426pub async fn branch_is_ancestor(repo: &Path, branch: &str, base_branch: &str) -> Result<bool> {
2438 let out = tokio::process::Command::new("git")
2439 .args(["merge-base", "--is-ancestor", branch, base_branch])
2440 .current_dir(repo)
2441 .quiet()
2442 .stdin(std::process::Stdio::null())
2443 .output()
2444 .await
2445 .context("spawn git merge-base --is-ancestor")?;
2446 Ok(out.status.success())
2447}
2448
2449fn forge_slug(url: &str) -> Option<(String, &str)> {
2457 let rest = url.rsplit("://").next()?;
2458 let (host, path) = rest.split_once('/')?;
2459 if host.is_empty() {
2460 return None;
2461 }
2462 Some((host.to_ascii_lowercase(), path))
2463}
2464
2465pub(crate) fn slug_of_pr_url(url: &str) -> Option<String> {
2473 let (host, path) = forge_slug(url)?;
2474 let mut segments = path.split('/');
2475 let owner = segments.next()?;
2476 let repo = segments.next()?;
2477 let kind = segments.next()?;
2478 if owner.is_empty() || repo.is_empty() || kind != "pull" {
2479 return None;
2480 }
2481 Some(format!("{host}/{owner}/{repo}"))
2482}
2483
2484fn slug_of_repo_url(url: &str) -> Option<String> {
2488 let (host, path) = forge_slug(url)?;
2489 let mut segments = path.split('/');
2490 let owner = segments.next()?;
2491 let repo = segments.next()?;
2492 if owner.is_empty() || repo.is_empty() {
2493 return None;
2494 }
2495 Some(format!("{host}/{owner}/{repo}"))
2496}
2497
2498pub(crate) fn ensure_same_repo(run_repo_slug: &str, pr_repo_slug: &str) -> Result<()> {
2519 if run_repo_slug.eq_ignore_ascii_case(pr_repo_slug) {
2520 return Ok(());
2521 }
2522 bail!(
2523 "refusing to correct this run: it is recorded against {run_repo_slug}, but the pull \
2524 request URL belongs to {pr_repo_slug} - pass the run id whose repository the URL \
2525 actually belongs to (or, if `origin` is a fork opened against a different upstream, \
2526 verify by hand before treating this as a false positive)"
2527 );
2528}
2529
2530async fn repo_slug(repo: &Path) -> Result<String> {
2541 let out = gh(
2542 repo,
2543 &[
2544 "repo".to_owned(),
2545 "view".to_owned(),
2546 "--json".to_owned(),
2547 "url".to_owned(),
2548 ],
2549 )
2550 .await?;
2551 if !out.0 {
2552 bail!("gh repo view --json url: {}", out.1);
2553 }
2554 #[derive(Debug, Deserialize)]
2555 struct GhRepo {
2556 url: String,
2557 }
2558 let parsed: GhRepo = serde_json::from_str(&out.1)
2559 .with_context(|| format!("parse `gh repo view` output: {}", out.1))?;
2560 slug_of_repo_url(&parsed.url)
2561 .with_context(|| format!("could not parse a host/owner/repo out of {}", parsed.url))
2562}
2563
2564pub async fn correct_manual_merge(
2612 state: &mut RunState,
2613 url: &str,
2614) -> Result<(RunStatus, RunStatus)> {
2615 let Some(pr_slug) = slug_of_pr_url(url) else {
2616 bail!(
2617 "could not parse an owner/repo out of {url}; refusing to guess which repository \
2618 this pull request belongs to"
2619 );
2620 };
2621 let run_slug = repo_slug(&state.repo).await?;
2622 ensure_same_repo(&run_slug, &pr_slug)?;
2623 correct_merge(state, url).await
2624}
2625
2626pub(crate) async fn correct_confirmed_external_merge(
2637 state: &mut RunState,
2638 url: &str,
2639) -> Result<(RunStatus, RunStatus)> {
2640 correct_merge(state, url).await
2641}
2642
2643fn names_same_pr(a: &RunState, url: &str, number: u64, repo: &Path) -> bool {
2645 let Some(pr) = a.pr.as_ref() else {
2646 return false;
2647 };
2648 if !url.is_empty()
2649 && pr
2650 .url
2651 .trim_end_matches('/')
2652 .eq_ignore_ascii_case(url.trim_end_matches('/'))
2653 {
2654 return true;
2655 }
2656 number > 0
2657 && pr.number == number
2658 && match (a.repo.canonicalize(), repo.canonicalize()) {
2659 (Ok(x), Ok(y)) => x == y,
2660 _ => a.repo == repo,
2661 }
2662}
2663
2664fn rewrite_open_prs(
2677 home: &Path,
2678 decide: &mut dyn FnMut(&RunState) -> Option<PrLifecycle>,
2679) -> usize {
2680 let now = Timestamp::now();
2681 let mut changed = 0;
2682 for id in crate::run::list_ids_in(&home.join("runs")) {
2683 let path = home.join("runs").join(&id).join("run.json");
2684 let Ok(body) = std::fs::read_to_string(&path) else {
2685 continue;
2686 };
2687 let Ok(mut state) = serde_json::from_str::<RunState>(&body) else {
2688 continue;
2689 };
2690 if !state.status.done()
2691 || state.pr.as_ref().is_none_or(|p| p.state != "open")
2692 || crate::daemon::is_working_on(home, &id, now)
2693 {
2694 continue;
2695 }
2696 let Some(to @ (PrLifecycle::Merged | PrLifecycle::Closed)) = decide(&state) else {
2697 continue;
2698 };
2699 if let Some(pr) = state.pr.as_mut() {
2700 pr.state = to.as_str().to_owned();
2701 }
2702 let url = state.pr.as_ref().map(|p| p.url.clone()).unwrap_or_default();
2703 state.event(
2704 "land",
2705 format!("recorded {url} as {}: another run settled it", to.as_str()),
2706 );
2707 match state.save_under(home) {
2708 Ok(()) => changed += 1,
2709 Err(e) => tracing::warn!("write pr state through to run {id}: {e:#}"),
2710 }
2711 }
2712 changed
2713}
2714
2715pub(crate) fn write_pr_state_through(run: &RunState, to: PrLifecycle) {
2720 if to == PrLifecycle::Open {
2721 return;
2722 }
2723 let Some(home) = crate::run::try_home() else {
2724 return;
2725 };
2726 write_pr_state_through_in(&home, run, to);
2727}
2728
2729pub(crate) fn write_pr_state_through_in(home: &Path, run: &RunState, to: PrLifecycle) -> usize {
2730 let Some(pr) = run.pr.as_ref() else {
2731 return 0;
2732 };
2733 let (url, number) = (pr.url.clone(), pr.number);
2734 rewrite_open_prs(home, &mut |other| {
2735 (other.id != run.id && names_same_pr(other, &url, number, &run.repo)).then_some(to)
2736 })
2737}
2738
2739pub(crate) fn stale_open_prs(home: &Path) -> Vec<(String, PathBuf, String)> {
2742 let now = Timestamp::now();
2743 let mut out = Vec::new();
2744 for id in crate::run::list_ids_in(&home.join("runs")) {
2745 let path = home.join("runs").join(&id).join("run.json");
2746 let Ok(body) = std::fs::read_to_string(&path) else {
2747 continue;
2748 };
2749 let Ok(state) = serde_json::from_str::<RunState>(&body) else {
2750 continue;
2751 };
2752 if let Some(pr) = state.pr.as_ref()
2753 && state.status.done()
2754 && pr.state == "open"
2755 && !pr.url.is_empty()
2756 && !crate::daemon::is_working_on(home, &id, now)
2757 {
2758 out.push((id, state.repo.clone(), pr.url.clone()));
2759 }
2760 }
2761 out
2762}
2763
2764pub(crate) fn apply_pr_states(home: &Path, known: &BTreeMap<String, PrLifecycle>) -> usize {
2767 rewrite_open_prs(home, &mut |s| {
2768 s.pr.as_ref().and_then(|p| known.get(&p.url)).copied()
2769 })
2770}
2771
2772pub async fn repair_stale_pr_states(home: &Path, max_lookups: usize) -> (usize, usize) {
2779 let mut known = BTreeMap::new();
2780 let mut failed = 0;
2781 let mut seen = BTreeSet::new();
2782 for (_, repo, url) in stale_open_prs(home) {
2783 if known.len() + failed >= max_lookups || !seen.insert(url.clone()) {
2784 continue;
2785 }
2786 match lifecycle(&repo, &url).await {
2787 Ok(state) => {
2788 known.insert(url, state);
2789 }
2790 Err(e) => {
2791 tracing::warn!("repair pr state of {url}: {e:#}");
2792 failed += 1;
2793 }
2794 }
2795 }
2796 (apply_pr_states(home, &known), failed)
2797}
2798
2799async fn correct_merge(state: &mut RunState, url: &str) -> Result<(RunStatus, RunStatus)> {
2800 match lifecycle(&state.repo, url).await? {
2801 PrLifecycle::Merged => {}
2802 other => bail!(
2803 "{url} is {}, not merged; refusing to record {} as merged on a guess",
2804 other.as_str(),
2805 state.id
2806 ),
2807 }
2808 let before = state.status;
2809 if let Err(e) = land(state, url).await {
2810 state.status = RunStatus::Blocked;
2817 state.event("fold", format!("manual-merge correction failed: {e:#}"));
2818 state.save()?;
2819 return Err(e).context(format!("confirming the merge of {url}"));
2820 }
2821 state.event(
2822 "fold",
2823 "operator recorded this pull request as a manual merge; this run never \
2824 re-entered `land`, so `bump::after_merge` did not run for it - a release \
2825 bump this change might warrant has to be filed by hand",
2826 );
2827 if state.status == RunStatus::Merged {
2830 crate::followup::after_merge(state, url).await;
2831 }
2832 state.save()?;
2833 Ok((before, state.status))
2834}
2835
2836pub fn parse_inline_comments(json: &str) -> Result<Vec<ReviewComment>> {
2843 let raw: Vec<GhInline> =
2844 serde_json::from_str(json).context("parse `gh api .../pulls/<n>/comments` output")?;
2845 let mut out = Vec::new();
2846 for c in raw {
2847 push_if_outstanding(
2848 &mut out,
2849 ReviewComment {
2850 author: c.user.login,
2851 path: c.path,
2852 line: c.line,
2853 body: c.body,
2854 },
2855 );
2856 }
2857 Ok(out)
2858}
2859
2860fn push_if_outstanding(out: &mut Vec<ReviewComment>, comment: ReviewComment) {
2866 if comment.body.trim().is_empty() || comment.body.contains(MARKER) {
2867 return;
2868 }
2869 if comment.path.is_none() && is_noise(&comment.body) {
2870 return;
2871 }
2872 out.push(comment);
2873}
2874
2875pub fn is_noise(body: &str) -> bool {
2893 if NOT_A_REVIEW.iter().any(|m| body.contains(m)) {
2894 return true;
2895 }
2896 let mut content = false;
2897 for line in strip_blocks(body).lines() {
2898 let line = unquote(line);
2899 if line.is_empty() || is_checklist(line) || is_decoration(line) || is_banner(line) {
2900 continue;
2901 }
2902 content = true;
2903 break;
2904 }
2905 !content
2906}
2907
2908fn strip_blocks(body: &str) -> String {
2910 let mut out = String::with_capacity(body.len());
2911 let mut rest = body;
2912 loop {
2913 let open = ["<!--", "<details>"]
2914 .iter()
2915 .filter_map(|tag| rest.find(tag).map(|i| (i, *tag)))
2916 .min_by_key(|(i, _)| *i);
2917 let Some((at, tag)) = open else {
2918 out.push_str(rest);
2919 return out;
2920 };
2921 out.push_str(&rest[..at]);
2922 let after = &rest[at + tag.len()..];
2923 let close = if tag == "<!--" { "-->" } else { "</details>" };
2924 match after.find(close) {
2925 Some(end) => rest = &after[end + close.len()..],
2926 None => return out,
2928 }
2929 }
2930}
2931
2932fn unquote(line: &str) -> &str {
2934 let mut s = line.trim();
2935 while let Some(rest) = s.strip_prefix('>') {
2936 s = rest.trim_start();
2937 }
2938 s.trim()
2939}
2940
2941fn is_checklist(line: &str) -> bool {
2943 let rest = line
2944 .strip_prefix("- ")
2945 .or_else(|| line.strip_prefix("* "))
2946 .unwrap_or("");
2947 let rest = rest.trim_start();
2948 matches!(
2949 rest.get(..3),
2950 Some("[ ]") | Some("[x]") | Some("[X]") | Some("[*]")
2951 )
2952}
2953
2954fn is_decoration(line: &str) -> bool {
2956 line.starts_with('#')
2957 || line.starts_with("[!")
2958 || (line.len() >= 3 && line.chars().all(|c| matches!(c, '-' | '=' | '*' | '_')))
2959}
2960
2961fn is_banner(line: &str) -> bool {
2968 let plain = drop_spans(line, "**", "**");
2969 let plain = if plain.contains("](") {
2970 drop_spans(&plain, "[", ")")
2971 } else {
2972 plain
2973 };
2974 !plain.chars().any(char::is_alphanumeric)
2975}
2976
2977fn drop_spans(s: &str, open: &str, close: &str) -> String {
2981 let mut out = String::with_capacity(s.len());
2982 let mut rest = s;
2983 while let Some(at) = rest.find(open) {
2984 out.push_str(&rest[..at]);
2985 let after = &rest[at + open.len()..];
2986 match after.find(close) {
2987 Some(end) => rest = &after[end + close.len()..],
2988 None => return out,
2989 }
2990 }
2991 out.push_str(rest);
2992 out
2993}
2994
2995fn repo_merge_lock(repo: &Path) -> Arc<tokio::sync::Mutex<()>> {
3013 static LOCKS: std::sync::LazyLock<
3014 std::sync::Mutex<BTreeMap<PathBuf, Arc<tokio::sync::Mutex<()>>>>,
3015 > = std::sync::LazyLock::new(|| std::sync::Mutex::new(BTreeMap::new()));
3016 LOCKS
3017 .lock()
3018 .unwrap_or_else(std::sync::PoisonError::into_inner)
3019 .entry(repo.to_path_buf())
3020 .or_insert_with(|| Arc::new(tokio::sync::Mutex::new(())))
3021 .clone()
3022}
3023
3024fn repo_label(repo: &Path, pr_url: &str) -> String {
3027 let parts: Vec<&str> = pr_url.split('/').collect();
3028 if let Some(at) = parts.iter().rposition(|p| *p == "pull")
3029 && at >= 2
3030 && !parts[at - 1].is_empty()
3031 && !parts[at - 2].is_empty()
3032 {
3033 return format!("{}/{}", parts[at - 2], parts[at - 1]);
3034 }
3035 repo.file_name()
3036 .map(|n| n.to_string_lossy().into_owned())
3037 .unwrap_or_default()
3038}
3039
3040fn red_merge_summary(repo_name: &str, pr: &PrState) -> Option<String> {
3044 (pr.checks == Checks::Red).then(|| {
3045 let details = crate::notices::format_list(&pr.failing)
3046 .map(|names| format!(": {names}"))
3047 .unwrap_or_default();
3048 format!(
3049 "Merged {repo_name} PR #{} with red checks{details} ({})",
3050 pr.number, pr.url
3051 )
3052 })
3053}
3054
3055async fn announce_red_merge(state: &mut RunState, pr: &PrState) {
3059 let repo_name = repo_label(&state.repo, &pr.url);
3060 let Some(summary) = red_merge_summary(&repo_name, pr) else {
3061 return;
3062 };
3063 if let Some(rec) = state.pr.as_mut() {
3064 rec.red_at_merge = pr.failing.clone();
3065 }
3066 state.event("land", summary.clone());
3067 crate::notices::raise_with(
3070 crate::notices::merged_red(&state.id, &summary),
3071 &state.config.notify,
3072 );
3073}
3074
3075pub async fn land(state: &mut RunState, pr_url: &str) -> Result<PrState> {
3082 land_with(state, pr_url, &GhForge).await
3083}
3084
3085trait Forge {
3089 async fn close(&self, repo: &Path, pr_url: &str, comment: &str) -> Result<()> {
3090 let (ok, out) = gh(
3091 repo,
3092 &[
3093 "pr".into(),
3094 "close".into(),
3095 pr_url.into(),
3096 "--comment".into(),
3097 comment.into(),
3098 ],
3099 )
3100 .await?;
3101 if !ok {
3102 bail!("PR closure failed: {out}");
3103 }
3104 Ok(())
3105 }
3106 async fn view(&self, repo: &Path, pr_url: &str) -> Result<Seen>;
3107 async fn merge(&self, repo: &Path, argv: &[String]) -> Result<(bool, String)>;
3108 async fn poll(&self);
3109 async fn rerun(&self, repo: &Path, run: &str) -> Result<()>;
3111 fn now(&self) -> i64;
3113 async fn required_contexts(&self, repo: &Path, base: &str) -> Option<BTreeSet<String>>;
3116 #[allow(clippy::too_many_arguments)]
3117 async fn fix(
3118 &self,
3119 state: &mut RunState,
3120 pr: &PrState,
3121 round: usize,
3122 budget: usize,
3123 reason: &str,
3124 logs: &str,
3125 ) -> Result<Fixed>;
3126}
3127
3128struct GhForge;
3129
3130impl Forge for GhForge {
3131 async fn view(&self, repo: &Path, pr_url: &str) -> Result<Seen> {
3132 observe(repo, pr_url).await
3133 }
3134 async fn merge(&self, repo: &Path, argv: &[String]) -> Result<(bool, String)> {
3135 gh(repo, argv).await
3136 }
3137 async fn poll(&self) {
3138 tokio::time::sleep(POLL).await;
3139 }
3140 async fn rerun(&self, repo: &Path, run: &str) -> Result<()> {
3141 let args = [
3142 "run".to_owned(),
3143 "rerun".to_owned(),
3144 run.to_owned(),
3145 "--failed".to_owned(),
3146 ];
3147 let (ok, out) = gh(repo, &args).await?;
3148 if !ok {
3149 bail!("gh run rerun {run}: {out}");
3150 }
3151 Ok(())
3152 }
3153 fn now(&self) -> i64 {
3154 jiff::Timestamp::now().as_second()
3155 }
3156 async fn required_contexts(&self, repo: &Path, base: &str) -> Option<BTreeSet<String>> {
3157 required_contexts_of(repo, base).await
3158 }
3159 async fn fix(
3160 &self,
3161 state: &mut RunState,
3162 pr: &PrState,
3163 round: usize,
3164 budget: usize,
3165 reason: &str,
3166 logs: &str,
3167 ) -> Result<Fixed> {
3168 fix_round(state, pr, round, budget, reason, logs).await
3169 }
3170}
3171
3172fn encode_path_segment(s: &str) -> String {
3174 let mut out = String::new();
3175 for b in s.bytes() {
3176 if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
3177 out.push(b as char);
3178 } else {
3179 let _ = write!(out, "%{b:02X}");
3180 }
3181 }
3182 out
3183}
3184
3185async fn required_contexts_of(repo: &Path, base: &str) -> Option<BTreeSet<String>> {
3190 let enc = encode_path_segment(base);
3191 let mut all = BTreeSet::new();
3192 let classic = gh(
3195 repo,
3196 &[
3197 "api".to_owned(),
3198 format!("repos/{{owner}}/{{repo}}/branches/{enc}/protection/required_status_checks"),
3199 ],
3200 )
3201 .await
3202 .ok()?;
3203 if classic.0 {
3204 all.extend(parse_classic_required(&classic.1)?);
3205 } else if !classic.1.contains("404") {
3206 return None;
3207 }
3208 let rules = gh(
3209 repo,
3210 &[
3211 "api".to_owned(),
3212 format!("repos/{{owner}}/{{repo}}/rules/branches/{enc}"),
3213 ],
3214 )
3215 .await
3216 .ok()?;
3217 if !rules.0 {
3218 return None;
3219 }
3220 all.extend(parse_ruleset_required(&rules.1)?);
3221 Some(all)
3222}
3223
3224fn parse_classic_required(json: &str) -> Option<BTreeSet<String>> {
3227 let v: serde_json::Value = serde_json::from_str(json).ok()?;
3228 let mut out = BTreeSet::new();
3229 for c in v.get("contexts")?.as_array()? {
3230 out.insert(c.as_str()?.to_owned());
3231 }
3232 for c in v
3233 .get("checks")
3234 .and_then(|c| c.as_array())
3235 .into_iter()
3236 .flatten()
3237 {
3238 if let Some(name) = c.get("context").and_then(|n| n.as_str()) {
3239 out.insert(name.to_owned());
3240 }
3241 }
3242 Some(out)
3243}
3244
3245fn parse_ruleset_required(json: &str) -> Option<BTreeSet<String>> {
3247 let v: serde_json::Value = serde_json::from_str(json).ok()?;
3248 let mut out = BTreeSet::new();
3249 for rule in v.as_array()? {
3250 if rule.get("type").and_then(|t| t.as_str()) != Some("required_status_checks") {
3251 continue;
3252 }
3253 let checks = rule
3254 .pointer("/parameters/required_status_checks")?
3255 .as_array()?;
3256 for c in checks {
3257 out.insert(c.get("context")?.as_str()?.to_owned());
3258 }
3259 }
3260 Some(out)
3261}
3262
3263fn awaiting_new_head(awaiting: Option<&str>, observed: &str) -> bool {
3271 awaiting.is_some_and(|want| !observed.eq_ignore_ascii_case(want))
3272}
3273
3274fn bound_head<'a>(
3284 seen_head: &'a str,
3285 rollup_head: &str,
3286 awaiting: Option<&str>,
3287) -> Option<&'a str> {
3288 if seen_head.is_empty()
3289 || awaiting_new_head(awaiting, seen_head)
3290 || !rollup_head.eq_ignore_ascii_case(seen_head)
3291 {
3292 return None;
3293 }
3294 Some(seen_head)
3295}
3296
3297#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3299enum Refused {
3300 Pending,
3302 Recheck,
3306 Final,
3309}
3310
3311fn classify_refusal(after: Option<&Seen>, rechecked: bool, observed_head: &str) -> Refused {
3314 let Some(after) = after else {
3315 return Refused::Pending;
3317 };
3318 if after.pr.state != PrLifecycle::Open {
3319 return Refused::Final;
3320 }
3321 if !after.head.eq_ignore_ascii_case(observed_head) {
3324 return Refused::Pending;
3325 }
3326 if bound_head(&after.head, &after.rollup_head, None).is_none() {
3328 return Refused::Pending;
3329 }
3330 let state = after.merge_state.to_ascii_uppercase();
3331 if matches!(after.pr.checks, Checks::Pending | Checks::Unknown)
3332 || state.is_empty()
3333 || state == "UNKNOWN"
3334 {
3335 return Refused::Pending;
3336 }
3337 if rechecked {
3338 Refused::Final
3339 } else {
3340 Refused::Recheck
3341 }
3342}
3343
3344async fn disarm<F: Forge>(
3349 forge: &F,
3350 state: &mut RunState,
3351 repo: &Path,
3352 number: u64,
3353) -> std::result::Result<(), String> {
3354 let argv = disable_automerge_argv(number);
3355 let out = {
3356 let merge_lock = repo_merge_lock(repo);
3357 let _merge_slot = merge_lock.lock().await;
3358 forge.merge(repo, &argv).await
3359 };
3360 match out {
3361 Ok((true, _)) => {
3362 state.land_armed_head = None;
3363 state.event("land", "auto-merge disabled");
3364 state.save().map_err(|e| format!("{e:#}"))?;
3365 Ok(())
3366 }
3367 Ok((false, msg)) => Err(msg),
3368 Err(e) => Err(format!("{e:#}")),
3369 }
3370}
3371
3372async fn stop_disarmed<F: Forge>(
3376 forge: &F,
3377 state: &mut RunState,
3378 repo: &Path,
3379 pr: &PrState,
3380 why: &str,
3381) -> Result<()> {
3382 if state.land_armed_head.is_none() {
3383 return stop(state, repo, pr, why).await;
3384 }
3385 match disarm(forge, state, repo, pr.number).await {
3386 Ok(()) => stop(state, repo, pr, why).await,
3387 Err(e) => {
3388 let why = format!("{why} (auto-merge could not be disabled and may still fire: {e})");
3389 stop(state, repo, pr, &why).await
3390 }
3391 }
3392}
3393
3394fn owner_requested_stop(state: &RunState) -> Result<bool> {
3396 let Some(binding) = &state.land_approval else {
3397 return Ok(false);
3398 };
3399 let q = ask::Questions::open().get(&binding.question)?;
3400 Ok(q.run == state.id
3401 && q.node == APPROVAL_NODE
3402 && q.resolution()
3403 .as_deref()
3404 .is_some_and(crate::merge_decision::stops))
3405}
3406
3407async fn owner_stop<F: Forge>(state: &mut RunState, pr: &mut PrState, forge: &F) -> Result<bool> {
3410 use crate::merge_decision as md;
3411 let Some(binding) = &state.land_approval else {
3412 return Ok(false);
3413 };
3414 let q = ask::Questions::open().get(&binding.question)?;
3415 let Some(choice) = q.resolution().filter(|c| md::stops(c)) else {
3416 return Ok(false);
3417 };
3418 if q.run != state.id || q.node != APPROVAL_NODE {
3419 bail!("stop decision names another run");
3420 }
3421 let target = q.merge_target.clone().unwrap_or_else(|| md::Target {
3422 pr: pr.url.clone(),
3423 head: binding.head.clone(),
3424 language: state.config.graph.language.clone(),
3425 });
3426 if target.pr != pr.url {
3427 bail!("stop decision names another PR");
3428 }
3429 if state.merge_decision.is_none() {
3430 state.merge_decision = Some(md::Decision {
3431 question: q.id.clone(),
3432 target,
3433 choice: choice.clone(),
3434 closed: false,
3435 replacement_run: None,
3436 replacement_pr: None,
3437 });
3438 state.save()?;
3439 }
3440 let repo = state.repo.clone();
3441 if let Err(e) = disarm(forge, state, &repo, pr.number).await {
3443 stop(state, &repo, pr, &format!("owner stop requested, but auto-merge could not be disabled: {e}; no repair or replacement started")).await?;
3444 return Ok(true);
3445 }
3446 if choice.eq_ignore_ascii_case(HOLD) {
3447 stop(
3448 state,
3449 &repo,
3450 pr,
3451 "owner held the decision: PR left open; no further repair or merge work",
3452 )
3453 .await?;
3454 return Ok(true);
3455 }
3456 if md::selected_agent(&choice).is_some()
3457 || state
3458 .origin
3459 .as_ref()
3460 .and_then(|o| o.task.as_ref())
3461 .is_some()
3462 {
3463 let owners: Vec<_> = crate::queue::Queue::open()
3464 .list()
3465 .into_iter()
3466 .filter(|task| task.runs.last() == Some(&state.id))
3467 .collect();
3468 let owner = (owners.len() == 1).then(|| &owners[0]);
3469 if !owner.is_some_and(|task| {
3470 task.runs.last() == Some(&state.id)
3471 && (!task
3472 .operator_decided_at
3473 .is_some_and(|at| at > q.answered_at.unwrap_or(q.asked_at))
3474 || task.resumption.as_ref().is_some_and(|release| {
3475 release.question_id.as_deref() == Some(q.id.as_str())
3476 && task.operator_decided_at == Some(release.at)
3477 }))
3478 }) {
3479 stop(state, &repo, pr, "replacement requires the current original queue task owner and an unsuperseded answer; PR left open").await?;
3480 return Ok(true);
3481 }
3482 }
3483 if pr.state == PrLifecycle::Merged {
3484 stop(
3485 state,
3486 &repo,
3487 pr,
3488 "PR already merged; withdrawal/replacement cannot be applied",
3489 )
3490 .await?;
3491 return Ok(true);
3492 }
3493 let comment = format!(
3494 "{MARKER}\nOwner decision on question {}: {}. Previous attempt: `{}`. Original instruction and review history remain in the task record.",
3495 q.id, choice, state.id
3496 );
3497 if pr.state == PrLifecycle::Open {
3498 if let Err(e) = forge.close(&repo, &pr.url, &comment).await {
3499 stop(
3500 state,
3501 &repo,
3502 pr,
3503 &format!(
3504 "owner requested closure but it failed: {e}; task held, no replacement started"
3505 ),
3506 )
3507 .await?;
3508 return Ok(true);
3509 }
3510 }
3511 let confirmed = forge.view(&repo, &pr.url).await?;
3512 if confirmed.pr.state != PrLifecycle::Closed {
3513 stop(
3514 state,
3515 &repo,
3516 pr,
3517 "PR closure was not confirmed; task held, no replacement started",
3518 )
3519 .await?;
3520 return Ok(true);
3521 }
3522 *pr = confirmed.pr;
3523 state.merge_decision.as_mut().unwrap().closed = true;
3524 if let Some(record) = &mut state.pr {
3525 record.state = "closed".into();
3526 }
3527 state.status = RunStatus::Superseded;
3528 state.merge = Some(MergeOutcome {
3529 mode: MergeMode::Pr,
3530 ok: false,
3531 detail: format!("owner decision: {choice}; PR closure confirmed"),
3532 empty: false,
3533 });
3534 state.event(
3535 "land",
3536 format!(
3537 "{} applied: PR closure confirmed; old execution retired",
3538 choice
3539 ),
3540 );
3541 state.save()?;
3542 Ok(true)
3543}
3544
3545async fn land_with<F: Forge>(state: &mut RunState, pr_url: &str, forge: &F) -> Result<PrState> {
3546 crate::pr_head::ensure_owner(state)?;
3547 let reopening_hold = crate::merge_decision::resumable_hold(state);
3548 let repo = state.repo.clone();
3549 let budget = state.config.graph.land_rounds;
3550 let mut round = 0usize;
3551 let mut rebases = 0usize;
3554 let mut waited = Duration::ZERO;
3555 let mut shown: BTreeSet<String> = BTreeSet::new();
3560 let mut awaiting_head: Option<String> = state.pr_pushed_head.clone();
3562 let mut rechecked = false;
3564
3565 state.status = RunStatus::Landing;
3573 state.event("land", format!("watching {pr_url}"));
3574 state.save()?;
3575
3576 let mut resumed_armed = state.land_armed_head.is_some();
3580
3581 loop {
3582 let seen = forge.view(&repo, pr_url).await?;
3583 let mut pr = seen.pr.clone();
3584 pr.review_comments.retain(|c| !shown.contains(&c.body));
3585 state.pr = Some(crate::run::PrRecord {
3586 url: pr.url.clone(),
3587 number: pr.number,
3588 state: pr.state.as_str().to_owned(),
3589 checks: pr.checks.as_str().to_owned(),
3590 round,
3591 rounds: budget,
3592 red_at_merge: Vec::new(),
3593 });
3594 state.save()?;
3595
3596 crate::pr_head::ensure_owner(state)?;
3597 if reopening_hold {
3598 anyhow::ensure!(
3599 pr.state == PrLifecycle::Open,
3600 "held PR is no longer open; cannot request a fresh decision"
3601 );
3602 disarm(forge, state, &repo, pr.number)
3605 .await
3606 .map_err(anyhow::Error::msg)?;
3607 let previous = state.merge_decision.take();
3608 let binding = state.land_approval.take();
3609 state.config.graph.land_approval = true;
3610 let subject = merge_subject(
3611 crate::graph::landing_title(state, &seen.title),
3612 &crate::graph::landing_subject_source(state),
3613 );
3614 let contested = contested_to_ask(state);
3615 if let Err(error) =
3616 approval_gate(state, &pr, &subject, contested.as_ref(), &seen.head).await
3617 {
3618 state.merge_decision = previous;
3619 state.land_approval = binding;
3620 return Err(error);
3621 }
3622 state.end_pass_parked(crate::run::ParkCause::Question);
3623 state.parked = true;
3624 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()));
3625 state.save()?;
3626 return Ok(pr);
3627 }
3628 if owner_stop(state, &mut pr, forge).await? {
3629 return Ok(pr);
3630 }
3631
3632 if std::mem::take(&mut resumed_armed) && pr.state == PrLifecycle::Open {
3633 if let Err(e) = disarm(forge, state, &repo, pr.number).await {
3638 let why = format!(
3639 "a previous pass may have armed auto-merge and it could not be disabled \
3640 on resume: {e}"
3641 );
3642 stop(state, &repo, &pr, &why).await?;
3643 return Ok(pr);
3644 }
3645 }
3646
3647 if pr.state == PrLifecycle::Open
3652 && !seen.head.is_empty()
3653 && state
3654 .land_armed_head
3655 .as_deref()
3656 .is_some_and(|armed| !armed.eq_ignore_ascii_case(&seen.head))
3657 {
3658 if let Err(e) = disarm(forge, state, &repo, pr.number).await {
3659 let why = format!(
3660 "the head moved while auto-merge was armed and it could not be disabled: {e}"
3661 );
3662 stop(state, &repo, &pr, &why).await?;
3663 return Ok(pr);
3664 }
3665 }
3666
3667 if pr.state == PrLifecycle::Open {
3668 if bound_head(&seen.head, &seen.rollup_head, awaiting_head.as_deref()).is_none() {
3669 if waited >= WAIT_CEILING {
3670 let want = awaiting_head.as_deref().unwrap_or_default();
3671 let why = format!(
3672 "the pull request's checks were still not about one readable head after \
3673 {} minutes (expected {}, pull request points at {}, checks are for {}); \
3674 someone may have pushed over it",
3675 WAIT_CEILING.as_secs() / 60,
3676 if want.is_empty() { "any" } else { want },
3677 if seen.head.is_empty() {
3678 "nothing readable"
3679 } else {
3680 &seen.head
3681 },
3682 if seen.rollup_head.is_empty() {
3683 "nothing readable"
3684 } else {
3685 &seen.rollup_head
3686 },
3687 );
3688 stop_disarmed(forge, state, &repo, &pr, &why).await?;
3689 return Ok(pr);
3690 }
3691 waited += POLL;
3692 forge.poll().await;
3693 continue;
3694 }
3695 if awaiting_head.take().is_some() {
3698 waited = Duration::ZERO;
3701 }
3702 }
3703
3704 if pr.state == PrLifecycle::Open && state.config.graph.rerun_flaky_checks {
3709 let failing = rerunnable(&seen.contexts);
3710 let now = forge.now();
3711 match rerun_step(&failing, state.land_reruns.as_ref(), &seen.head, now) {
3712 RerunStep::Rerun(runs) => {
3713 let rec = state.land_reruns.get_or_insert_with(Default::default);
3714 if !rec.head.eq_ignore_ascii_case(&seen.head) {
3715 *rec = LandReruns {
3716 head: seen.head.clone(),
3717 runs: BTreeMap::new(),
3718 };
3719 }
3720 for run in &runs {
3721 rec.runs.insert(run.clone(), now);
3722 }
3723 state.save()?;
3727 let mut any = false;
3728 for run in &runs {
3729 let names = failing
3730 .iter()
3731 .filter(|(_, r)| r.as_deref() == Some(run))
3732 .map(|(l, _)| l.as_str())
3733 .collect::<Vec<_>>()
3734 .join(", ");
3735 match forge.rerun(&repo, run).await {
3736 Ok(()) => {
3737 any = true;
3738 state.event(
3739 "land",
3740 format!("reran failed check(s) {names} (run {run})"),
3741 );
3742 }
3743 Err(e) => state.event(
3745 "land",
3746 format!("could not rerun check(s) {names} (run {run}): {e:#}"),
3747 ),
3748 }
3749 }
3750 state.save()?;
3751 if any {
3752 waited = Duration::ZERO;
3753 forge.poll().await;
3754 continue;
3755 }
3756 }
3757 RerunStep::Grace => {
3758 if waited >= WAIT_CEILING {
3759 let why = format!(
3760 "checks were still failing after a rerun and {} minutes",
3761 WAIT_CEILING.as_secs() / 60
3762 );
3763 stop(state, &repo, &pr, &why).await?;
3764 return Ok(pr);
3765 }
3766 waited += POLL;
3767 forge.poll().await;
3768 continue;
3769 }
3770 RerunStep::Fix => {}
3771 }
3772 }
3773
3774 let step = decide(&pr, round, budget, waited);
3775 let armed_here = state
3780 .land_armed_head
3781 .as_deref()
3782 .is_some_and(|armed| armed.eq_ignore_ascii_case(&seen.head));
3783 if armed_here && matches!(step, Step::Merge | Step::Wait) {
3784 if waited >= WAIT_CEILING {
3785 let required = if seen.base.is_empty() {
3786 None
3787 } else {
3788 forge.required_contexts(&repo, &seen.base).await
3789 };
3790 let why = format!(
3791 "auto-merge was armed on {} but the pull request did not merge within {} \
3792 minutes ({})",
3793 seen.head,
3794 WAIT_CEILING.as_secs() / 60,
3795 waiting_on(&seen.merge_state, &seen.contexts, required.as_ref())
3796 );
3797 stop_disarmed(forge, state, &repo, &pr, &why).await?;
3798 return Ok(pr);
3799 }
3800 waited += POLL;
3801 forge.poll().await;
3802 continue;
3803 }
3804 if armed_here && !matches!(step, Step::Done { .. }) {
3805 if let Err(e) = disarm(forge, state, &repo, pr.number).await {
3808 let why = format!("auto-merge could not be disabled: {e}");
3809 stop(state, &repo, &pr, &why).await?;
3810 return Ok(pr);
3811 }
3812 }
3813 match step {
3814 Step::Wait => {
3815 if waited >= WAIT_CEILING {
3816 let why = format!(
3817 "checks were still running after {} minutes",
3818 WAIT_CEILING.as_secs() / 60
3819 );
3820 stop(state, &repo, &pr, &why).await?;
3821 return Ok(pr);
3822 }
3823 waited += POLL;
3824 forge.poll().await;
3825 }
3826 Step::Done { merged } => {
3827 if let Some(armed) = state.land_armed_head.take() {
3832 if merged && !seen.head.is_empty() && !armed.eq_ignore_ascii_case(&seen.head) {
3833 let msg = format!(
3834 "{} merged on {} but the owner approved {armed}; review what landed",
3835 pr.url, seen.head
3836 );
3837 tracing::warn!("{msg}");
3838 state.event("land", msg);
3839 crate::notices::raise_with(
3843 crate::notices::Notice::warn(
3844 &format!("merged-unapproved-head:{}", state.id),
3845 "A pull request merged on a commit the owner did not approve; \
3846 review what landed",
3847 )
3848 .link(crate::notices::Link::Run {
3849 id: state.id.clone(),
3850 }),
3851 &state.config.notify,
3852 );
3853 }
3854 }
3855 state.status = if merged {
3856 RunStatus::Merged
3857 } else {
3858 RunStatus::Ready
3859 };
3860 let detail = if merged {
3861 format!("{} was merged", pr.url)
3862 } else {
3863 format!("{} was closed without merging", pr.url)
3864 };
3865 state.merge = Some(MergeOutcome {
3866 mode: MergeMode::Pr,
3867 ok: merged,
3868 detail: detail.clone(),
3869 empty: false,
3870 });
3871 state.event("land", detail);
3872 state.save()?;
3873 write_pr_state_through(state, pr.state);
3874 return Ok(pr);
3875 }
3876 Step::Merge => {
3877 let subject = merge_subject(
3878 crate::graph::landing_title(state, &seen.title),
3879 &crate::graph::landing_subject_source(state),
3880 );
3881 let contested = contested_to_ask(state);
3888 if state.config.graph.land_approval
3889 || contested.is_some()
3890 || repair_available(state)
3891 || !state.merge_repairs.is_empty()
3892 {
3893 match approval_gate(state, &pr, &subject, contested.as_ref(), &seen.head)
3894 .await?
3895 {
3896 ApprovalGate::Approved => {}
3897 ApprovalGate::Repair => {
3898 if let Err(why) = disarm(forge, state, &repo, pr.number).await {
3901 state.event("repair", format!("auto-merge could not be disabled: {why}; repair preflight will retry"));
3902 state.save()?;
3903 }
3904 return Ok(pr);
3905 }
3906 ApprovalGate::Held => {
3907 if owner_stop(state, &mut pr, forge).await? {
3910 return Ok(pr);
3911 }
3912 stop(
3913 state,
3914 &repo,
3915 &pr,
3916 "the owner did not approve the merge (held or unanswered)",
3917 )
3918 .await?;
3919 return Ok(pr);
3920 }
3921 ApprovalGate::Pending => {
3929 state.end_pass_parked(crate::run::ParkCause::Question);
3930 state.parked = true;
3931 state.event(
3932 "land",
3933 "parked awaiting merge approval - resumes once answered",
3934 );
3935 state.save()?;
3936 return Ok(pr);
3937 }
3938 }
3939 }
3940 let observed_head = seen.head.clone();
3951 {
3954 let fresh = forge.view(&repo, pr_url).await.ok();
3955 if !direct_merge_is_safe(
3956 fresh.as_ref(),
3957 &observed_head,
3958 &shown,
3959 round,
3960 budget,
3961 waited,
3962 ) {
3963 if waited >= WAIT_CEILING {
3964 let why = "the pull request did not settle on the approved head \
3965 before it could be merged";
3966 stop(state, &repo, &pr, why).await?;
3967 return Ok(pr);
3968 }
3969 state.event(
3970 "land",
3971 "the pull request changed before merging; looking again",
3972 );
3973 waited += POLL;
3974 forge.poll().await;
3975 continue;
3976 }
3977 }
3978 let argv = merge_argv_at(pr.number, &subject, &observed_head);
3979 let out = {
3980 let merge_lock = repo_merge_lock(&repo);
3981 let _merge_slot = merge_lock.lock().await;
3982 crate::pr_head::ensure_owner(state)?;
3983 if let Some(task_id) = state
3984 .origin
3985 .as_ref()
3986 .and_then(|origin| origin.task.as_ref())
3987 {
3988 let task = crate::queue::Queue::open()
3989 .get(task_id)
3990 .context("cannot verify the task owner immediately before merge")?;
3991 if task.runs.last() != Some(&state.id)
3992 || task.operator_held()
3993 || task.status == crate::queue::TaskStatus::Done
3994 {
3995 bail!(
3996 "task ownership or an operator hold superseded this merge approval"
3997 );
3998 }
3999 }
4000 if let Some(binding) = &state.land_approval {
4001 let current = ask::Questions::open().get(&binding.question)?;
4002 if merge_consent_superseded(state, ¤t)?
4003 || current.run != state.id
4004 || !binding.head.eq_ignore_ascii_case(&observed_head)
4005 || approval(current.resolution().as_deref()) != Approval::Merge
4006 || current.merge_target.as_ref().is_some_and(|t| {
4007 t.pr != pr.url || !t.head.eq_ignore_ascii_case(&observed_head)
4008 })
4009 {
4010 bail!(
4011 "merge decision is stale or no longer authorizes the reviewed head"
4012 );
4013 }
4014 }
4015 forge.merge(&repo, &argv).await?
4016 };
4017 if out.0 {
4018 let confirmed = forge
4024 .view(&repo, pr_url)
4025 .await
4026 .is_ok_and(|c| c.pr.state == PrLifecycle::Merged);
4027 if !confirmed {
4028 if waited >= WAIT_CEILING {
4029 let why = "the merge request succeeded but the pull request \
4030 could not be confirmed merged after waiting";
4031 stop(state, &repo, &pr, why).await?;
4032 return Ok(pr);
4033 }
4034 state.event(
4035 "land",
4036 "merge accepted but the pull request is not confirmed merged yet; waiting",
4037 );
4038 state.save()?;
4039 waited += POLL;
4040 forge.poll().await;
4041 continue;
4042 }
4043 pr.state = PrLifecycle::Merged;
4044 state.status = RunStatus::Merged;
4045 state.merge = Some(MergeOutcome {
4046 mode: MergeMode::Pr,
4047 ok: true,
4048 detail: format!("gh {}", argv.join(" ")),
4049 empty: false,
4050 });
4051 if let Some(pr_record) = state.pr.as_mut() {
4056 pr_record.state = pr.state.as_str().to_owned();
4057 }
4058 state.event("land", format!("merged {} as `{subject}`", pr.url));
4059 announce_red_merge(state, &pr).await;
4060 state.save()?;
4061 write_pr_state_through(state, pr.state);
4062 return Ok(pr);
4063 }
4064 let after_seen = forge.view(&repo, pr_url).await.ok();
4065 let after = after_seen.as_ref().map(|s| s.pr.state);
4066 if let Some(outcome) = merged_after_all(&argv, &out.1, after) {
4067 pr.state = PrLifecycle::Merged;
4068 state.status = RunStatus::Merged;
4069 state.merge = Some(outcome);
4070 if let Some(pr_record) = state.pr.as_mut() {
4071 pr_record.state = pr.state.as_str().to_owned();
4072 }
4073 state.event("land", format!("merged {} as `{subject}`", pr.url));
4074 announce_red_merge(state, &pr).await;
4075 state.save()?;
4076 write_pr_state_through(state, pr.state);
4077 return Ok(pr);
4078 }
4079 let verdict = classify_refusal(after_seen.as_ref(), rechecked, &observed_head);
4080 match verdict {
4081 Refused::Final => {
4082 let merge_state = after_seen
4083 .as_ref()
4084 .map(|s| s.merge_state.as_str())
4085 .filter(|m| !m.is_empty())
4086 .unwrap_or("unknown");
4087 let why = format!(
4091 "the merge was refused: {} (merge state: {merge_state})",
4092 out.1
4093 );
4094 stop(state, &repo, &pr, &why).await?;
4095 return Ok(pr);
4096 }
4097 verdict => {
4098 if waited >= WAIT_CEILING {
4103 let why = format!(
4104 "the merge was still refused after {} minutes: {}",
4105 WAIT_CEILING.as_secs() / 60,
4106 out.1
4107 );
4108 stop(state, &repo, &pr, &why).await?;
4109 return Ok(pr);
4110 }
4111 if verdict == Refused::Recheck {
4112 rechecked = true;
4113 }
4114 state.event(
4115 "land",
4116 "merge refused while the branch policy is not satisfied yet; waiting",
4117 );
4118 state.save()?;
4119 waited += POLL;
4120 forge.poll().await;
4121 }
4122 }
4123 }
4124 Step::Rebase => {
4125 if rebases >= budget {
4131 let why = format!(
4132 "the base moved under this branch {budget} time(s) and it still does \
4133 not merge; rebasing again would only race it"
4134 );
4135 stop(state, &repo, &pr, &why).await?;
4136 return Ok(pr);
4137 }
4138 rebases += 1;
4139 let Some(branch) = state.winner().map(|w| w.branch.clone()) else {
4140 stop(
4141 state,
4142 &repo,
4143 &pr,
4144 "the pull request conflicts and this run has no winning branch to rebase",
4145 )
4146 .await?;
4147 return Ok(pr);
4148 };
4149 let base = state.base_branch.clone();
4150 state.event(
4151 "land",
4152 format!("{} no longer merges; rebasing onto {base}", pr.url),
4153 );
4154 state.save()?;
4155
4156 let remote = state.config.merge.remote.clone();
4160 git::fetch(&repo, &remote, &base).await.ok();
4161 if owner_stop(state, &mut pr, forge).await? {
4162 return Ok(pr);
4163 }
4164 let scratch = state.dir().join("rebase");
4165 let onto = format!("{remote}/{base}");
4166 let rebased =
4167 match crate::rebase::rebase_with_fixer(state, &scratch, &branch, &onto).await {
4168 Ok(crate::rebase::Rebased::Applied) => Ok(None),
4169 Ok(crate::rebase::Rebased::Stopped(why)) => Ok(Some(why)),
4170 Err(e) => Err(e),
4171 };
4172 if owner_stop(state, &mut pr, forge).await? {
4173 return Ok(pr);
4174 }
4175 match rebased {
4176 Ok(None) => {
4177 let pushed = {
4178 let merge_lock = repo_merge_lock(&repo);
4179 let _merge_slot = merge_lock.lock().await;
4180 if let Some(head) = &state.pr_head {
4181 git::push_as(
4182 &repo,
4183 &state.config.merge.remote,
4184 &branch,
4185 head,
4186 state
4187 .pr_pushed_head
4188 .as_deref()
4189 .context("missing public head lease")?,
4190 )
4191 .await?
4192 } else {
4193 git::push_pinned(
4194 &repo,
4195 &state.config.merge.remote,
4196 &branch,
4197 &seen.head,
4198 )
4199 .await?
4200 }
4201 };
4202 if !pushed.ok() {
4203 let why = format!(
4204 "rebased {branch} but could not push it: {}",
4205 pushed.stderr.trim()
4206 );
4207 stop(state, &repo, &pr, &why).await?;
4208 return Ok(pr);
4209 }
4210 let head =
4213 match git::rev_parse(&repo, &format!("refs/heads/{branch}")).await {
4214 Ok(head) => head,
4215 Err(e) => {
4216 let why = format!(
4217 "rebased and pushed {branch} but could not read the pushed \
4218 commit: {e:#}"
4219 );
4220 stop(state, &repo, &pr, &why).await?;
4221 return Ok(pr);
4222 }
4223 };
4224 crate::graph::refresh_reviewed_commits(state, &branch).await;
4225 state.pr_pushed_head = Some(head.clone());
4226 awaiting_head = Some(head);
4227 rechecked = false;
4228 state.event("land", format!("rebased {branch} onto {base}"));
4229 state.save()?;
4230 waited = Duration::ZERO;
4233 tokio::time::sleep(POLL).await;
4234 }
4235 Ok(Some(conflict)) => {
4238 let why = format!(
4239 "{} conflicts with {base} and the rebase did not apply: {}",
4240 pr.url,
4241 conflict.chars().take(600).collect::<String>()
4242 );
4243 stop(state, &repo, &pr, &why).await?;
4244 return Ok(pr);
4245 }
4246 Err(e) => {
4247 let why = format!("could not rebase {branch} onto {base}: {e:#}");
4248 stop(state, &repo, &pr, &why).await?;
4249 return Ok(pr);
4250 }
4251 }
4252 }
4253 Step::GiveUp { reason } => {
4254 stop(state, &repo, &pr, &reason).await?;
4255 return Ok(pr);
4256 }
4257 Step::Fix { reason } => {
4258 round += 1;
4259 waited = Duration::ZERO;
4260 for c in &pr.review_comments {
4261 shown.insert(c.body.clone());
4262 }
4263 state.event("land", format!("round {round}: {reason}"));
4264 state.save()?;
4265
4266 let logs = failing_logs(&repo, &seen.failing_urls).await;
4267 let was_red = pr.checks == Checks::Red;
4268 if owner_stop(state, &mut pr, forge).await? {
4269 return Ok(pr);
4270 }
4271 let fixed = forge.fix(state, &pr, round, budget, &reason, &logs).await?;
4272 if owner_stop(state, &mut pr, forge).await? {
4273 return Ok(pr);
4274 }
4275 match fixed {
4276 Fixed::Committed { head } => {
4277 state.pr_pushed_head = Some(head.clone());
4280 awaiting_head = Some(head);
4281 rechecked = false;
4282 waited = Duration::ZERO;
4283 forge.poll().await;
4284 }
4285 Fixed::Declined if was_red => {
4286 let why = format!(
4287 "the fixer produced no commit while {} check(s) were failing \
4288 ({}); stopping instead of looping on an unchanged tree",
4289 pr.failing.len(),
4290 pr.failing.join(", ")
4291 );
4292 stop(state, &repo, &pr, &why).await?;
4293 return Ok(pr);
4294 }
4295 Fixed::Declined => state.event(
4300 "land",
4301 format!("round {round}: fixer declined the comments, nothing committed"),
4302 ),
4303 Fixed::Failed(why) => {
4304 stop(state, &repo, &pr, &format!("the fix round failed: {why}")).await?;
4305 return Ok(pr);
4306 }
4307 }
4308 state.save()?;
4309 }
4310 }
4311 }
4312}
4313
4314#[derive(Clone)]
4318struct Seen {
4319 pr: PrState,
4320 title: String,
4321 failing_urls: Vec<(String, String)>,
4322 head: String,
4324 rollup_head: String,
4327 merge_state: String,
4330 contexts: Vec<CheckInfo>,
4332 base: String,
4334}
4335
4336#[derive(Clone)]
4338struct CheckInfo {
4339 label: String,
4340 verdict: Verdict,
4341 required: Option<bool>,
4342 run: Option<String>,
4344}
4345
4346#[derive(Debug, Clone, PartialEq, Eq)]
4348enum RerunStep {
4349 Rerun(Vec<String>),
4351 Grace,
4353 Fix,
4355}
4356
4357fn rerunnable(contexts: &[CheckInfo]) -> Vec<(String, Option<String>)> {
4361 contexts
4362 .iter()
4363 .filter(|c| c.verdict == Verdict::Fail && c.required != Some(false))
4364 .filter(|c| {
4366 c.run.as_ref().is_none_or(|r| {
4367 !contexts.iter().any(|o| {
4368 matches!(o.verdict, Verdict::Pending | Verdict::Unknown)
4369 && o.run.as_ref() == Some(r)
4370 })
4371 })
4372 })
4373 .map(|c| (c.label.clone(), c.run.clone()))
4374 .collect()
4375}
4376
4377fn rerun_step(
4379 failing: &[(String, Option<String>)],
4380 record: Option<&LandReruns>,
4381 head: &str,
4382 now: i64,
4383) -> RerunStep {
4384 if failing.is_empty() || failing.iter().any(|(_, run)| run.is_none()) {
4387 return RerunStep::Fix;
4388 }
4389 let record = record.filter(|r| r.head.eq_ignore_ascii_case(head));
4391 let mut fresh: Vec<String> = Vec::new();
4392 let mut recent = false;
4393 for run in failing.iter().filter_map(|(_, run)| run.as_deref()) {
4394 match record.and_then(|r| r.runs.get(run)) {
4395 None => {
4396 if !fresh.iter().any(|f| f == run) {
4397 fresh.push(run.to_owned());
4398 }
4399 }
4400 Some(&at) if now - at < crate::release_watch::RERUN_GRACE => recent = true,
4401 Some(_) => {}
4402 }
4403 }
4404 if !fresh.is_empty() {
4405 RerunStep::Rerun(fresh)
4406 } else if recent {
4407 RerunStep::Grace
4408 } else {
4409 RerunStep::Fix
4410 }
4411}
4412
4413async fn observe(repo: &Path, pr_url: &str) -> Result<Seen> {
4417 let view = gh(
4418 repo,
4419 &[
4420 "pr".to_owned(),
4421 "view".to_owned(),
4422 pr_url.to_owned(),
4423 "--json".to_owned(),
4424 "url,number,state,title,reviews,comments,mergeStateStatus,headRefOid,baseRefName"
4425 .to_owned(),
4426 ],
4427 )
4428 .await?;
4429 if !view.0 {
4430 bail!("gh pr view {pr_url}: {}", view.1);
4431 }
4432 let number = parse_pr(&view.1)?.number;
4433 let node = last_commit_node(repo, number).await;
4434 let mut seen = seen_from(&view.1, node.as_deref())?;
4435
4436 let inline = gh(
4437 repo,
4438 &[
4439 "api".to_owned(),
4440 format!("repos/{{owner}}/{{repo}}/pulls/{}/comments", seen.pr.number),
4441 ],
4442 )
4443 .await?;
4444 if inline.0 {
4445 match parse_inline_comments(&inline.1) {
4446 Ok(mut comments) => seen.pr.review_comments.append(&mut comments),
4447 Err(e) => tracing::warn!("inline review comments unreadable: {e}"),
4450 }
4451 } else {
4452 tracing::warn!("gh api pulls/{}/comments: {}", seen.pr.number, inline.1);
4453 }
4454 Ok(seen)
4455}
4456
4457fn seen_from(view_json: &str, node_json: Option<&str>) -> Result<Seen> {
4468 let mut pr = parse_pr(view_json)?;
4469 let raw: GhPr = serde_json::from_str(view_json).context("re-read pull request json")?;
4470
4471 let mut rollup_head = String::new();
4472 let mut failing_urls = Vec::new();
4473 let mut contexts = Vec::new();
4474 let mut checks = Checks::Unknown;
4475 let mut failing = Vec::new();
4476 if let Some((oid, rollup)) = node_json.and_then(parse_last_commit_node) {
4477 (checks, failing) = rollup_verdict(&rollup);
4478 failing_urls = rollup
4479 .iter()
4480 .filter(|c| c.verdict() == Verdict::Fail)
4481 .filter_map(|c| c.url().map(|u| (c.label(), u.to_owned())))
4482 .collect();
4483 contexts = rollup
4484 .iter()
4485 .map(|c| CheckInfo {
4486 label: c.label(),
4487 verdict: c.verdict(),
4488 required: c.is_required,
4489 run: c.url().and_then(run_of),
4490 })
4491 .collect();
4492 rollup_head = oid;
4493 }
4494 pr.checks = checks;
4495 pr.failing = failing;
4496
4497 Ok(Seen {
4498 pr,
4499 title: raw.title,
4500 failing_urls,
4501 head: raw.head_ref_oid,
4502 rollup_head,
4503 merge_state: raw.merge_state_status,
4504 contexts,
4505 base: raw.base_ref_name,
4506 })
4507}
4508
4509fn parse_last_commit_node(json: &str) -> Option<(String, Vec<GhCheck>)> {
4512 let v: serde_json::Value = serde_json::from_str(json).ok()?;
4513 if v.get("errors").is_some_and(|e| !e.is_null()) {
4514 return None;
4515 }
4516 let commit = v.pointer("/data/repository/pullRequest/commits/nodes/0/commit")?;
4517 let oid = commit.get("oid")?.as_str().filter(|o| !o.is_empty())?;
4518 let contexts = commit.pointer("/statusCheckRollup/contexts");
4519 let Some(contexts) = contexts.filter(|c| !c.is_null()) else {
4520 return Some((oid.to_owned(), Vec::new()));
4522 };
4523 if contexts.pointer("/pageInfo/hasNextPage")?.as_bool()? {
4524 return None;
4525 }
4526 let nodes = contexts.get("nodes")?.as_array()?;
4527 let rollup = nodes
4528 .iter()
4529 .map(|n| serde_json::from_value::<GhCheck>(n.clone()))
4530 .collect::<Result<Vec<_>, _>>()
4531 .ok()?;
4532 Some((oid.to_owned(), rollup))
4533}
4534
4535async fn last_commit_node(repo: &Path, number: u64) -> Option<String> {
4538 let out = gh(
4539 repo,
4540 &[
4541 "api".to_owned(),
4542 "graphql".to_owned(),
4543 "-F".to_owned(),
4544 "owner={owner}".to_owned(),
4545 "-F".to_owned(),
4546 "repo={repo}".to_owned(),
4547 "-F".to_owned(),
4548 format!("number={number}"),
4549 "-f".to_owned(),
4550 "query=query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,\
4551 name:$repo){pullRequest(number:$number){commits(last:1){nodes{commit{oid \
4552 statusCheckRollup{contexts(first:100){pageInfo{hasNextPage} nodes{\
4553 ... on CheckRun{name status conclusion detailsUrl \
4554 isRequired(pullRequestNumber:$number)} \
4555 ... on StatusContext{context state targetUrl \
4556 isRequired(pullRequestNumber:$number)}}}}}}}}}}"
4557 .to_owned(),
4558 ],
4559 )
4560 .await
4561 .ok()?;
4562 out.0.then_some(out.1)
4563}
4564
4565#[doc(hidden)]
4567#[derive(Debug, PartialEq)]
4568pub enum Fixed {
4569 Committed {
4571 head: String,
4573 },
4574 Declined,
4576 Failed(String),
4578}
4579
4580#[doc(hidden)]
4586pub async fn fix_round(
4587 state: &mut RunState,
4588 pr: &PrState,
4589 round: usize,
4590 budget: usize,
4591 reason: &str,
4592 logs: &str,
4593) -> Result<Fixed> {
4594 let winner = state
4595 .winner()
4596 .cloned()
4597 .context("landing needs a winning candidate; none is recorded on this run")?;
4598 let roles = state
4599 .config
4600 .resolve_roles()
4601 .context("resolve the roster for the fix round")?;
4602 let attempts = crate::fixer::attempts(state, &roles, &winner);
4606 let ids: Vec<String> = attempts.iter().map(|(s, _)| s.id.clone()).collect();
4607
4608 let prompt = fix_prompt(state, pr, round, budget, reason, logs);
4609 let mut artifacts = agent::artifacts_dir(&state.dir());
4610 if let Some(request) = state
4611 .merge_repairs
4612 .last()
4613 .filter(|r| r.phase == crate::run::MergeRepairPhase::Fixing)
4614 {
4615 artifacts = artifacts.join(format!("merge-repair-{}", request.question));
4616 }
4617 let prompt = if state.config.cache_dir().is_some() {
4618 format!("{prompt}\n\n{}", prompt::build_cache_note("fix", true))
4619 } else {
4620 prompt
4621 };
4622 let before = git::rev_parse(&winner.worktree, "HEAD").await?;
4626 let mut last = None;
4630 for (i, (spec, seat_key)) in attempts.into_iter().enumerate() {
4631 if owner_requested_stop(state)? {
4632 return Ok(Fixed::Declined);
4633 }
4634 let mut seat = seat_of(state, &seat_key, &spec.id);
4635 let stem = if i == 0 {
4636 format!("land-{round}")
4637 } else {
4638 format!("land-{round}-{}", spec.id)
4639 };
4640 let out = agent::invoke(
4641 &spec,
4642 &mut seat,
4643 &Invocation {
4644 cwd: &winner.worktree,
4645 prompt: &prompt,
4646 timeout: Duration::from_secs(state.config.graph.timeout_fix),
4647 allow_write: true,
4648 unsandboxed: false,
4649 sessions: state.config.graph.sessions,
4650 artifacts: &artifacts,
4651 stem: &stem,
4652 run: &state.id,
4653 node: "land",
4654 cache_dir: state.config.cache_dir().as_deref(),
4655 attachments: &[],
4656 writable: &[],
4657 },
4658 )
4659 .await;
4660 state.seats.insert(seat.key.clone(), seat);
4661 if let Some(next) = ids.get(i + 1)
4662 && agent::chain_advances(&out)
4663 {
4664 let (class, why) = crate::fixer::failure_of(&out);
4665 crate::graph::record_handover(state, "land", &seat_key, &spec.id, next, &class, &why);
4666 continue;
4667 }
4668 last = Some(out);
4669 break;
4670 }
4671 let out = last.expect("the fixer chain always has an entry");
4672
4673 match out {
4674 Ok(o) if o.quota_exhausted() => {
4675 return Ok(Fixed::Failed(
4676 "rate limited (quota); the fixer could not run".to_owned(),
4677 ));
4678 }
4679 Ok(o) if !o.usable() => {
4680 return Ok(Fixed::Failed(format!(
4681 "the fixer produced nothing usable ({}{})",
4682 crate::notices::exit_status(o.exit_code),
4683 if o.timed_out {
4684 "; the turn timed out"
4685 } else {
4686 ""
4687 }
4688 )));
4689 }
4690 Ok(_) => {}
4691 Err(e) if e.is::<crate::repo_lock::ResourceBlocked>() => return Err(e),
4692 Err(e) => return Ok(Fixed::Failed(format!("{e:#}"))),
4693 }
4694
4695 if owner_requested_stop(state)? {
4696 return Ok(Fixed::Declined);
4697 }
4698
4699 let wait = crate::repo_lock::WaitPolicy::new(
4702 Duration::from_secs(state.config.graph.timeout_resource_wait),
4703 || false,
4704 );
4705 let coordination = crate::repo_lock::SeatLease::operation_with(&winner.worktree, &wait).await?;
4706 match git::rescue_commit(
4707 &winner.worktree,
4708 &format!("magi: land round {round} fixes (uncommitted work)"),
4709 )
4710 .await
4711 {
4712 Ok(r) => state.note_withheld("land", &r.withheld),
4713 Err(e)
4714 if state
4715 .merge_repairs
4716 .last()
4717 .is_some_and(|r| r.phase == crate::run::MergeRepairPhase::Fixing) =>
4718 {
4719 return Ok(Fixed::Failed(format!("saving repair commit failed: {e:#}")));
4720 }
4721 Err(_) => {}
4722 }
4723 let after = git::rev_parse(&winner.worktree, "HEAD").await?;
4724 drop(coordination);
4725 if after == before {
4726 return Ok(Fixed::Declined);
4727 }
4728
4729 if owner_requested_stop(state)? {
4730 return Ok(Fixed::Declined);
4731 }
4732 crate::pr_head::ensure_owner(state)?;
4733 let remote = state.config.merge.remote.clone();
4734 let public_head = state.pr_branch().unwrap_or(&winner.branch).to_owned();
4735 let push = if state.pr_head.is_some() {
4736 let expected = state
4737 .pr_pushed_head
4738 .as_deref()
4739 .context("missing public head lease")?;
4740 git::push_as(
4741 &winner.worktree,
4742 &remote,
4743 &winner.branch,
4744 &public_head,
4745 expected,
4746 )
4747 .await?
4748 } else {
4749 git::push(&winner.worktree, &remote, &winner.branch).await?
4750 };
4751 if !push.ok() {
4752 return Ok(Fixed::Failed(format!(
4753 "pushing {} to {remote} failed: {}",
4754 public_head, push.stderr
4755 )));
4756 }
4757 state.event(
4758 "land",
4759 format!("round {round}: pushed a fix to {public_head}"),
4760 );
4761 state.pr_pushed_head = Some(after.clone());
4762 state.save()?;
4763 Ok(Fixed::Committed { head: after })
4764}
4765
4766pub(crate) fn seat_of(state: &mut RunState, key: &str, agent: &str) -> SeatState {
4768 crate::fixer::seat_for(state, key, agent)
4769}
4770
4771pub(crate) async fn repair_preflight(state: &RunState, url: &str, head: &str) -> Result<PrState> {
4774 let seen = observe(&state.repo, url).await?;
4775 let winner = state.winner().context("no winning branch to repair")?;
4776 if seen.pr.state != PrLifecycle::Open || seen.head != head {
4777 anyhow::bail!(
4778 "PR is closed or its head changed; review the current head before requesting repair again"
4779 );
4780 }
4781 if git::rev_parse(&winner.worktree, "HEAD").await? != head
4782 || !git::is_clean(&winner.worktree).await?
4783 {
4784 anyhow::bail!(
4785 "repair worktree is missing, dirty or at a different head; restore the reviewed branch and resume this run"
4786 );
4787 }
4788 let view = gh(
4791 &state.repo,
4792 &[
4793 "pr".into(),
4794 "view".into(),
4795 url.into(),
4796 "--json".into(),
4797 "autoMergeRequest,headRefOid,state,headRefName,baseRefName,isCrossRepository".into(),
4798 ],
4799 )
4800 .await?;
4801 if !view.0 {
4802 bail!("cannot check auto-merge: {}", view.1);
4803 }
4804 let live: serde_json::Value = serde_json::from_str(&view.1)?;
4805 let public = state.pr_branch().unwrap_or(&winner.branch);
4806 if live["state"] != "OPEN"
4807 || live["headRefOid"] != head
4808 || live["headRefName"] != public
4809 || live["baseRefName"] != state.base_branch
4810 || live["isCrossRepository"] != false
4811 {
4812 bail!("PR changed during repair preflight; review its current head explicitly");
4813 }
4814 let auto_merge = live
4815 .get("autoMergeRequest")
4816 .context("cannot read the PR auto-merge reservation; repair held")?;
4817 if !auto_merge.is_null() {
4818 let out = gh(&state.repo, &disable_automerge_argv(seen.pr.number)).await?;
4819 if !out.0 {
4820 bail!("cannot disable auto-merge; repair held: {}", out.1);
4821 }
4822 }
4823 Ok(seen.pr)
4824}
4825
4826fn fix_prompt(
4828 state: &RunState,
4829 pr: &PrState,
4830 round: usize,
4831 budget: usize,
4832 reason: &str,
4833 logs: &str,
4834) -> String {
4835 let mut s = format!(
4836 "Your patch is open as a pull request and it is not landing. Land round \
4837 {round} of {budget}.\n\n\
4838 Pull request: {}\n\n\
4839 What is holding it: {reason}\n\n\
4840 # The task\n\n{}\n",
4841 pr.url, state.instruction
4842 );
4843
4844 if pr.failing.is_empty() {
4845 s.push_str("\n# Failing checks\n\n(none)\n");
4846 } else {
4847 let _ = write!(s, "\n# Failing checks\n\n- {}\n", pr.failing.join("\n- "));
4848 if logs.trim().is_empty() {
4849 s.push_str("\nNo log could be read; reproduce the failure locally.\n");
4850 } else {
4851 let _ = write!(s, "\n## Failing log tails\n\n{logs}\n");
4852 }
4853 }
4854
4855 if pr.review_comments.is_empty() {
4856 s.push_str("\n# Review comments\n\n(none)\n");
4857 } else {
4858 s.push_str("\n# Review comments\n");
4859 for c in &pr.review_comments {
4860 let where_ = match (&c.path, c.line) {
4861 (Some(p), Some(l)) => format!(" ({p}:{l})"),
4862 (Some(p), None) => format!(" ({p})"),
4863 _ => String::new(),
4864 };
4865 let _ = write!(s, "\n## {}{where_}\n\n{}\n", c.author, c.body.trim());
4866 }
4867 }
4868
4869 s.push_str(
4870 "\n# Rules\n\n\
4871 1. Fix the cause, never the symptom. Do not delete, skip, or weaken a \
4872 failing test; do not silence a lint with an allow attribute; do not \
4873 stretch a timeout to hide a race. If the check is right, the code is \
4874 wrong.\n\
4875 2. Change nothing the checks and the comments did not raise. A \
4876 drive-by refactor turns a one-line fix into a pull request that \
4877 needs reviewing again.\n\
4878 3. If a comment is wrong, say so with a checkable argument and change \
4879 nothing for it. A declined comment with a reason is a correct \
4880 outcome; a change made to appease a reviewer is not.\n\
4881 4. Commit in this worktree. magi pushes to the pull request's branch \
4882 for you; do not push, merge, or close anything yourself.\n\
4883 5. Never name yourself, your vendor, or your model, anywhere.\n\n\
4884 # Output\n\n\
4885 Say what you changed and why, and what you declined and why.",
4886 );
4887
4888 let language = &state.config.graph.language;
4889 if !(language.trim().is_empty() || language.eq_ignore_ascii_case("en")) {
4890 let _ = write!(s, "\n\nWrite all prose in {language}.");
4891 }
4892 s.push_str(&crate::prompt::github_rule(
4894 language,
4895 &state.config.graph.github_language,
4896 ));
4897 if let Some(overlay) = state.config.prompts.overlay("fix") {
4898 let _ = write!(s, "\n\n{overlay}");
4899 }
4900 s
4901}
4902
4903async fn failing_logs(repo: &Path, failing: &[(String, String)]) -> String {
4906 let mut out = String::new();
4907 for (name, url) in failing.iter().take(MAX_LOGS) {
4908 let args = match (job_of(url), run_of(url)) {
4909 (Some(job), _) => vec![
4910 "run".to_owned(),
4911 "view".to_owned(),
4912 "--log-failed".to_owned(),
4913 "--job".to_owned(),
4914 job,
4915 ],
4916 (None, Some(run)) => vec![
4917 "run".to_owned(),
4918 "view".to_owned(),
4919 run,
4920 "--log-failed".to_owned(),
4921 ],
4922 (None, None) => continue,
4924 };
4925 let (ok, body) = match gh(repo, &args).await {
4926 Ok(v) => v,
4927 Err(e) => (false, format!("{e:#}")),
4928 };
4929 if !ok && body.trim().is_empty() {
4930 continue;
4931 }
4932 let _ = write!(out, "### {name}\n\n```\n{}\n```\n\n", tail(&body, LOG_TAIL));
4933 }
4934 out
4935}
4936
4937fn job_of(details_url: &str) -> Option<String> {
4940 let after = details_url.split("/job/").nth(1)?;
4941 let id: String = after.chars().take_while(char::is_ascii_digit).collect();
4942 (!id.is_empty()).then_some(id)
4943}
4944
4945pub(crate) fn run_of(details_url: &str) -> Option<String> {
4947 let after = details_url.split("/actions/runs/").nth(1)?;
4948 let id: String = after.chars().take_while(char::is_ascii_digit).collect();
4949 (!id.is_empty()).then_some(id)
4950}
4951
4952fn stop_comment(run_id: &str, why: &str) -> String {
4956 format!(
4957 "{MARKER}\nmagi stopped landing this pull request: {why}\n\n\
4958 The branch is untouched and the run is `{run_id}`. Nothing was merged."
4959 )
4960}
4961
4962async fn stop(state: &mut RunState, repo: &Path, pr: &PrState, why: &str) -> Result<()> {
4967 let body = stop_comment(&state.id, why);
4968 let (_, mut body) = crate::github_text::prepare(state, "", &body);
4969 if !body.contains(MARKER) {
4970 body = format!("{MARKER}\n{body}");
4971 }
4972 let posted = gh(
4973 repo,
4974 &[
4975 "pr".to_owned(),
4976 "comment".to_owned(),
4977 pr.number.to_string(),
4978 "--body".to_owned(),
4979 body,
4980 ],
4981 )
4982 .await;
4983 match posted {
4984 Ok((true, _)) => {}
4985 Ok((false, out)) => tracing::warn!("could not comment on {}: {out}", pr.url),
4986 Err(e) => tracing::warn!("could not comment on {}: {e:#}", pr.url),
4987 }
4988 state.status = RunStatus::Blocked;
4989 state.merge = Some(MergeOutcome {
4990 mode: MergeMode::Pr,
4991 ok: false,
4992 detail: why.to_owned(),
4993 empty: false,
4994 });
4995 state.event("land", format!("stopped: {why}"));
4996 state.save()?;
4997 Ok(())
4998}
4999
5000pub(crate) async fn gh(cwd: &Path, args: &[String]) -> Result<(bool, String)> {
5016 let out = tokio::process::Command::new("gh")
5017 .args(args)
5018 .current_dir(cwd)
5019 .env_remove("GH_REPO")
5020 .quiet()
5021 .stdin(std::process::Stdio::null())
5022 .output()
5023 .await
5024 .with_context(|| format!("spawn gh {}", args.join(" ")))?;
5025 let mut body = String::from_utf8_lossy(&out.stdout).into_owned();
5026 let err = String::from_utf8_lossy(&out.stderr);
5027 if body.trim().is_empty() {
5028 body = err.into_owned();
5029 } else if !err.trim().is_empty() {
5030 body.push_str(&err);
5031 }
5032 Ok((out.status.success(), body.trim().to_owned()))
5033}
5034
5035#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5037pub(crate) enum Verdict {
5038 Pass,
5039 Fail,
5040 Pending,
5041 Unknown,
5042}
5043
5044#[derive(Debug, Deserialize)]
5045#[serde(rename_all = "camelCase")]
5046struct GhPr {
5047 #[serde(default)]
5048 url: String,
5049 #[serde(default)]
5050 number: u64,
5051 #[serde(default)]
5052 state: String,
5053 #[serde(default)]
5054 title: String,
5055 #[serde(default)]
5056 status_check_rollup: Vec<GhCheck>,
5057 #[serde(default)]
5064 merge_state_status: String,
5065 #[serde(default)]
5069 head_ref_oid: String,
5070 #[serde(default)]
5071 base_ref_name: String,
5072 #[serde(default)]
5073 reviews: Vec<GhReview>,
5074 #[serde(default)]
5075 comments: Vec<GhComment>,
5076}
5077
5078#[derive(Debug, Deserialize)]
5083#[serde(rename_all = "camelCase")]
5084struct GhCheck {
5085 #[serde(default)]
5086 name: Option<String>,
5087 #[serde(default)]
5088 context: Option<String>,
5089 #[serde(default)]
5090 status: Option<String>,
5091 #[serde(default)]
5092 conclusion: Option<String>,
5093 #[serde(default)]
5094 state: Option<String>,
5095 #[serde(default)]
5096 details_url: Option<String>,
5097 #[serde(default)]
5098 target_url: Option<String>,
5099 #[serde(default)]
5102 is_required: Option<bool>,
5103}
5104
5105impl GhCheck {
5106 fn label(&self) -> String {
5108 self.name
5109 .clone()
5110 .or_else(|| self.context.clone())
5111 .unwrap_or_else(|| "(unnamed check)".to_owned())
5112 }
5113
5114 fn url(&self) -> Option<&str> {
5116 self.details_url
5117 .as_deref()
5118 .or(self.target_url.as_deref())
5119 .filter(|u| !u.is_empty())
5120 }
5121
5122 fn verdict(&self) -> Verdict {
5130 if let Some(status) = self.status.as_deref() {
5131 if !status.eq_ignore_ascii_case("COMPLETED") {
5132 return Verdict::Pending;
5133 }
5134 }
5135 let outcome = self
5136 .conclusion
5137 .as_deref()
5138 .or(self.state.as_deref())
5139 .unwrap_or("");
5140 match outcome.to_ascii_uppercase().as_str() {
5141 "SUCCESS" | "SKIPPED" | "NEUTRAL" => Verdict::Pass,
5142 "FAILURE" | "ERROR" | "TIMED_OUT" | "CANCELLED" | "STARTUP_FAILURE"
5143 | "ACTION_REQUIRED" => Verdict::Fail,
5144 "PENDING" | "EXPECTED" | "QUEUED" | "IN_PROGRESS" | "WAITING" | "REQUESTED" => {
5145 Verdict::Pending
5146 }
5147 _ => Verdict::Unknown,
5148 }
5149 }
5150}
5151
5152#[derive(Debug, Deserialize)]
5153struct GhAuthor {
5154 #[serde(default)]
5155 login: String,
5156}
5157
5158#[derive(Debug, Deserialize)]
5159struct GhReview {
5160 #[serde(default)]
5161 author: GhAuthor,
5162 #[serde(default)]
5163 body: String,
5164}
5165
5166#[derive(Debug, Deserialize)]
5167struct GhComment {
5168 #[serde(default)]
5169 author: GhAuthor,
5170 #[serde(default)]
5171 body: String,
5172}
5173
5174#[derive(Debug, Deserialize)]
5175struct GhUser {
5176 #[serde(default)]
5177 login: String,
5178}
5179
5180#[derive(Debug, Deserialize)]
5181struct GhInline {
5182 #[serde(default)]
5183 user: GhUser,
5184 #[serde(default)]
5185 path: Option<String>,
5186 #[serde(default)]
5187 line: Option<u64>,
5188 #[serde(default)]
5189 body: String,
5190}
5191
5192impl Default for GhAuthor {
5193 fn default() -> Self {
5194 Self {
5195 login: "(unknown)".to_owned(),
5196 }
5197 }
5198}
5199
5200impl Default for GhUser {
5201 fn default() -> Self {
5202 Self {
5203 login: "(unknown)".to_owned(),
5204 }
5205 }
5206}
5207
5208#[cfg(test)]
5209pub(crate) mod tests {
5210 use super::*;
5211 use crate::run::{Candidate, ReviewRecord, ReviewRound, Tally};
5212
5213 fn head_json(head: &str, base: &str, state: &str, cross: bool) -> String {
5214 format!(
5215 r#"{{"headRefName":"{head}","headRefOid":"aaa","baseRefName":"{base}","state":"{state}","isCrossRepository":{cross}}}"#
5216 )
5217 }
5218
5219 #[test]
5220 fn a_pull_request_is_closed_only_when_its_head_is_exactly_the_runs_branch() {
5221 let ok = head_json("magi/27b2/A", "main", "OPEN", false);
5222 assert_eq!(
5223 closable(&ok, "magi/27b2/A", "main", &["aaa".to_owned()]),
5224 Ok(())
5225 );
5226 for (json, why) in [
5227 (head_json("magi/27b2/B", "main", "OPEN", false), "head"),
5228 (head_json("magi/27b2/A-2", "main", "OPEN", false), "head"),
5229 (head_json("magi/27b2/A", "main", "OPEN", true), "fork"),
5230 (head_json("magi/27b2/A", "dev", "OPEN", false), "targets"),
5231 (head_json("magi/27b2/A", "main", "MERGED", false), "already"),
5232 (head_json("magi/27b2/A", "main", "CLOSED", false), "already"),
5233 ] {
5234 let err = closable(&json, "magi/27b2/A", "main", &["aaa".to_owned()])
5235 .unwrap_err()
5236 .why;
5237 assert!(err.contains(why), "{json}: {err}");
5238 }
5239 let moved = head_json("magi/27b2/A", "main", "OPEN", false);
5241 let err = closable(&moved, "magi/27b2/A", "main", &["bbb".to_owned()]).unwrap_err();
5242 assert!(err.retry && err.why.contains("not a commit"), "{err:?}");
5243 assert!(
5244 !closable(
5245 &head_json("x", "main", "OPEN", false),
5246 "magi/27b2/A",
5247 "main",
5248 &[]
5249 )
5250 .unwrap_err()
5251 .retry
5252 );
5253 assert!(is_forge_url("https://github.com/o/r.git"));
5254 assert!(is_forge_url("git@github.com:o/r.git"));
5255 assert!(!is_forge_url("/tmp/origin.git"));
5256 assert!(!is_forge_url("C:\\work\\origin.git"));
5257 assert!(!is_forge_url("file:///tmp/origin.git"));
5258 assert!(forge_unavailable(
5259 "gh pr list failed: none of the git remotes configured for this repository point to a known GitHub host."
5260 ));
5261 assert!(!forge_unavailable(
5262 "gh pr list failed: error connecting to api.github.com"
5263 ));
5264 assert!(closable("not json", "magi/27b2/A", "main", &[]).is_err());
5265 assert!(
5267 closable(
5268 r#"{"headRefName":"b","headRefOid":"aaa","baseRefName":"main","state":"OPEN"}"#,
5269 "b",
5270 "main",
5271 &["aaa".to_owned()]
5272 )
5273 .is_err()
5274 );
5275 }
5276
5277 #[test]
5278 fn the_close_comment_names_the_commit_on_the_base() {
5279 let e = crate::already::Evidence {
5280 proof: crate::already::Proof::PatchId,
5281 tip: "1234567890".to_owned(),
5282 commits: vec!["0e368de0000".to_owned()],
5283 };
5284 let c = superseded_comment("main", &e);
5285 assert!(c.contains("0e368de") && c.contains("`main`"), "{c}");
5286 }
5287
5288 const GREEN_OPEN: &str = r####"{
5290 "url": "https://github.com/yukimemi/magi/pull/10",
5291 "number": 10,
5292 "state": "OPEN",
5293 "mergeStateStatus": "CLEAN",
5294 "statusCheckRollup": [
5295 {
5296 "__typename": "CheckRun",
5297 "conclusion": "SKIPPED",
5298 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33356278334/job/99378963755",
5299 "name": "review",
5300 "status": "COMPLETED",
5301 "workflowName": "claude-review"
5302 },
5303 {
5304 "__typename": "CheckRun",
5305 "conclusion": "SUCCESS",
5306 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33356278338/job/99378963144",
5307 "name": "check (ubuntu-latest)",
5308 "status": "COMPLETED",
5309 "workflowName": "CI"
5310 },
5311 {
5312 "__typename": "CheckRun",
5313 "conclusion": "SUCCESS",
5314 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33356278338/job/99378963095",
5315 "name": "rustfmt",
5316 "status": "COMPLETED",
5317 "workflowName": "CI"
5318 },
5319 {
5320 "__typename": "StatusContext",
5321 "context": "CodeRabbit",
5322 "state": "SUCCESS",
5323 "targetUrl": ""
5324 }
5325 ],
5326 "reviews": [],
5327 "comments": [
5328 {
5329 "author": {
5330 "login": "coderabbitai"
5331 },
5332 "authorAssociation": "NONE",
5333 "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"
5334 }
5335 ]
5336}"####;
5337
5338 const RED_OPEN: &str = r####"{
5340 "url": "https://github.com/yukimemi/magi/pull/9",
5341 "number": 9,
5342 "state": "OPEN",
5343 "mergeStateStatus": "UNSTABLE",
5344 "statusCheckRollup": [
5345 {
5346 "__typename": "CheckRun",
5347 "conclusion": "SUCCESS",
5348 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323744",
5349 "name": "check (ubuntu-latest)",
5350 "status": "COMPLETED",
5351 "workflowName": "CI"
5352 },
5353 {
5354 "__typename": "CheckRun",
5355 "conclusion": "SUCCESS",
5356 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323811",
5357 "name": "rustfmt",
5358 "status": "COMPLETED",
5359 "workflowName": "CI"
5360 },
5361 {
5362 "__typename": "CheckRun",
5363 "conclusion": "FAILURE",
5364 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323572",
5365 "name": "editorconfig",
5366 "status": "COMPLETED",
5367 "workflowName": "CI"
5368 },
5369 {
5370 "__typename": "StatusContext",
5371 "context": "CodeRabbit",
5372 "state": "SUCCESS",
5373 "targetUrl": ""
5374 }
5375 ],
5376 "reviews": [],
5377 "comments": [
5378 {
5379 "author": {
5380 "login": "coderabbitai"
5381 },
5382 "authorAssociation": "NONE",
5383 "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"
5384 }
5385 ]
5386}"####;
5387
5388 const PENDING_OPEN: &str = r####"{
5390 "url": "https://github.com/yukimemi/magi/pull/9",
5391 "number": 9,
5392 "state": "OPEN",
5393 "statusCheckRollup": [
5394 {
5395 "__typename": "CheckRun",
5396 "conclusion": "SUCCESS",
5397 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323744",
5398 "name": "check (ubuntu-latest)",
5399 "status": "COMPLETED",
5400 "workflowName": "CI"
5401 },
5402 {
5403 "__typename": "CheckRun",
5404 "conclusion": "SUCCESS",
5405 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323811",
5406 "name": "rustfmt",
5407 "status": "COMPLETED",
5408 "workflowName": "CI"
5409 },
5410 {
5411 "__typename": "CheckRun",
5412 "conclusion": null,
5413 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323572",
5414 "name": "editorconfig",
5415 "status": "IN_PROGRESS",
5416 "workflowName": "CI"
5417 },
5418 {
5419 "__typename": "StatusContext",
5420 "context": "CodeRabbit",
5421 "state": "SUCCESS",
5422 "targetUrl": ""
5423 }
5424 ],
5425 "reviews": [],
5426 "comments": []
5427}"####;
5428
5429 const MERGED: &str = r####"{
5431 "url": "https://github.com/yukimemi/magi/pull/16",
5432 "number": 16,
5433 "state": "MERGED",
5434 "statusCheckRollup": [
5435 {
5436 "__typename": "CheckRun",
5437 "conclusion": "SUCCESS",
5438 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33636587933/job/100268878095",
5439 "name": "check (ubuntu-latest)",
5440 "status": "COMPLETED",
5441 "workflowName": "CI"
5442 },
5443 {
5444 "__typename": "CheckRun",
5445 "conclusion": "SUCCESS",
5446 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33636587918/job/100268876427",
5447 "name": "review",
5448 "status": "COMPLETED",
5449 "workflowName": "claude-review"
5450 }
5451 ],
5452 "reviews": [],
5453 "comments": []
5454}"####;
5455
5456 const REVIEWED_OPEN: &str = r####"{
5458 "url": "https://github.com/yukimemi/magi/pull/12",
5459 "number": 12,
5460 "state": "OPEN",
5461 "statusCheckRollup": [
5462 {
5463 "__typename": "CheckRun",
5464 "conclusion": "SUCCESS",
5465 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33571212506/job/100065355258",
5466 "name": "check (ubuntu-latest)",
5467 "status": "COMPLETED",
5468 "workflowName": "CI"
5469 },
5470 {
5471 "__typename": "CheckRun",
5472 "conclusion": "SUCCESS",
5473 "detailsUrl": "https://github.com/yukimemi/magi/actions/runs/33571212566/job/100065355810",
5474 "name": "review",
5475 "status": "COMPLETED",
5476 "workflowName": "claude-review"
5477 }
5478 ],
5479 "reviews": [
5480 {
5481 "author": {
5482 "login": "claude"
5483 },
5484 "state": "COMMENTED",
5485 "body": ""
5486 }
5487 ],
5488 "comments": [
5489 {
5490 "author": {
5491 "login": "coderabbitai"
5492 },
5493 "authorAssociation": "NONE",
5494 "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"
5495 },
5496 {
5497 "author": {
5498 "login": "claude"
5499 },
5500 "authorAssociation": "NONE",
5501 "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"
5502 }
5503 ]
5504}"####;
5505
5506 const INLINE: &str = r####"[
5508 {
5509 "user": {
5510 "login": "claude[bot]"
5511 },
5512 "path": "src/graph.rs",
5513 "line": 231,
5514 "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"
5515 }
5516]"####;
5517
5518 const CODERABBIT_TRIGGER: &str = r####"<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
5520<!-- This is an auto-generated comment: skip review by coderabbit.ai -->
5521
5522> [!IMPORTANT]
5523> - [ ] <!-- {"checkboxId":"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe"} --> 🔍 Trigger review
5524>
5525> This repository does not receive automatic reviews because it has fewer than 10 stars.
5526>
5527> <details>
5528> <summary>⚙️ Run configuration</summary>
5529>
5530> **Configuration used**: defaults
5531>
5532> **Review profile**: CHILL
5533>
5534> **Plan**: Team
5535>
5536> **Run ID**: `c1e2a68f-87fc-4b35-9ec4-e75c7854966a`
5537>
5538> </details>
5539
5540<!-- end of auto-generated comment: skip review by coderabbit.ai -->
5541
5542<!-- tips_start -->
5543
5544---
5545
5546Thanks 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.
5547
5548<details>
5549<summary>❤️ Share</summary>
5550
5551- [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"####;
5552
5553 const CLAUDE_CHECKLIST: &str = r####"**Claude finished @yukimemi's task in 4m 14s** —— [View job](https://github.com/yukimemi/magi/actions/runs/33636587918)
5555
5556---
5557### Reviewing PR #16
5558
5559- [x] Read AGENTS.md conventions
5560- [x] Review `src/daemon.rs` changes
5561- [x] Review `src/main.rs` changes (new `doctor` reporting)
5562- [x] Review `src/web.rs` changes (reuse of unreadable-run count)
5563- [x] Check test coverage for new behavior
5564- [x] Run verification commands (blocked — see note)
5565- [x] Post findings"####;
5566
5567 const CLAUDE_FINDING: &str = r####"**Claude finished @yukimemi's task in 3m 52s** —— [View job](https://github.com/yukimemi/magi/actions/runs/33571212566)
5569
5570---
5571### Review: `magi review <branch>` — cheap-half-only graph
5572
5573Read 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.
5574
5575**Correctness**
5576
5577- 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"####;
5578
5579 fn pr(checks: Checks, failing: &[&str], comments: usize) -> PrState {
5580 PrState {
5581 url: "https://github.com/yukimemi/magi/pull/16".to_owned(),
5582 number: 16,
5583 state: PrLifecycle::Open,
5584 checks,
5585 blocking: if matches!(checks, Checks::Red) {
5589 Blocking::Yes
5590 } else {
5591 Blocking::No
5592 },
5593 failing: failing.iter().map(|s| (*s).to_owned()).collect(),
5594 review_comments: (0..comments)
5595 .map(|i| ReviewComment {
5596 author: "coderabbitai".to_owned(),
5597 path: Some("src/graph.rs".to_owned()),
5598 line: Some(231),
5599 body: format!("finding {i}"),
5600 })
5601 .collect(),
5602 }
5603 }
5604
5605 #[test]
5606 fn expected_ci_is_exactly_decide_and_absent_ci_never_waits_for_checks() {
5607 use CiExpectation::{Absent, Expected};
5608 for checks in [Checks::Pending, Checks::Unknown, Checks::Green, Checks::Red] {
5609 let p = pr(checks, &["x"], 0);
5610 for waited in [Duration::ZERO, CHECKS_GRACE] {
5611 assert_eq!(
5612 decide_with(&p, 0, 4, waited, Expected),
5613 decide(&p, 0, 4, waited)
5614 );
5615 }
5616 }
5617 for checks in [Checks::Pending, Checks::Unknown, Checks::Red] {
5619 let p = pr(checks, &["x"], 0);
5620 assert_eq!(decide_with(&p, 0, 4, Duration::ZERO, Absent), Step::Merge);
5621 assert_eq!(decide_with(&p, 4, 4, CHECKS_GRACE, Absent), Step::Merge);
5622 }
5623 let mut p = pr(Checks::Unknown, &[], 0);
5625 p.blocking = Blocking::Conflict;
5626 assert_eq!(decide_with(&p, 0, 4, Duration::ZERO, Absent), Step::Rebase);
5627 p.state = PrLifecycle::Merged;
5629 assert_eq!(
5630 decide_with(&p, 0, 4, Duration::ZERO, Absent),
5631 Step::Done { merged: true }
5632 );
5633 }
5634
5635 #[test]
5636 fn a_green_pull_request_with_nothing_outstanding_parses_as_ready_to_merge() {
5637 let state = parse_pr(GREEN_OPEN).expect("green fixture parses");
5638 assert_eq!(state.number, 10);
5639 assert_eq!(state.state, PrLifecycle::Open);
5640 assert_eq!(state.checks, Checks::Green);
5641 assert!(state.failing.is_empty());
5642 assert!(
5643 state.review_comments.is_empty(),
5644 "the only comment is CodeRabbit's trigger notice: {:?}",
5645 state.review_comments
5646 );
5647 assert_eq!(decide(&state, 0, 4, Duration::ZERO), Step::Merge);
5648 }
5649
5650 #[test]
5651 fn a_failing_check_parses_as_red_and_is_named() {
5652 let state = parse_pr(RED_OPEN).expect("red fixture parses");
5653 assert_eq!(state.checks, Checks::Red);
5654 assert_eq!(state.failing, vec!["editorconfig".to_owned()]);
5655 let mut blocking = state.clone();
5662 blocking.blocking = Blocking::Yes;
5663 match decide(&blocking, 0, 4, Duration::ZERO) {
5664 Step::Fix { reason } => {
5665 assert!(reason.contains("editorconfig"), "reason: {reason}");
5666 assert!(reason.contains("failing"), "reason: {reason}");
5667 }
5668 other => panic!("expected a fix round, got {other:?}"),
5669 }
5670 }
5671
5672 #[test]
5673 fn a_check_still_running_parses_as_pending_and_is_waited_for() {
5674 let state = parse_pr(PENDING_OPEN).expect("pending fixture parses");
5675 assert_eq!(state.checks, Checks::Pending);
5676 assert_eq!(decide(&state, 0, 4, Duration::ZERO), Step::Wait);
5677 }
5678
5679 #[test]
5680 fn a_pull_request_merged_underneath_us_is_done_rather_than_a_failure() {
5681 let state = parse_pr(MERGED).expect("merged fixture parses");
5682 assert_eq!(state.state, PrLifecycle::Merged);
5683 assert_eq!(
5684 decide(&state, 0, 4, Duration::ZERO),
5685 Step::Done { merged: true }
5686 );
5687 }
5688
5689 #[test]
5690 fn a_review_that_found_something_is_outstanding_and_holds_the_merge() {
5691 let state = parse_pr(REVIEWED_OPEN).expect("reviewed fixture parses");
5692 assert_eq!(state.checks, Checks::Green);
5693 let authors: Vec<&str> = state
5694 .review_comments
5695 .iter()
5696 .map(|c| c.author.as_str())
5697 .collect();
5698 assert_eq!(
5699 authors,
5700 vec!["claude"],
5701 "CodeRabbit's walkthrough is machinery; Claude's review is a finding"
5702 );
5703 match decide(&state, 0, 4, Duration::ZERO) {
5704 Step::Fix { reason } => assert!(reason.contains("unresolved"), "reason: {reason}"),
5705 other => panic!("expected a fix round, got {other:?}"),
5706 }
5707 }
5708
5709 #[test]
5710 fn inline_review_comments_keep_their_file_and_line() {
5711 let comments = parse_inline_comments(INLINE).expect("inline fixture parses");
5712 assert_eq!(comments.len(), 1);
5713 assert_eq!(comments[0].author, "claude[bot]");
5714 assert_eq!(comments[0].path.as_deref(), Some("src/graph.rs"));
5715 assert_eq!(comments[0].line, Some(231));
5716 assert!(comments[0].body.contains("empty"), "{}", comments[0].body);
5717 }
5718
5719 #[test]
5720 fn a_status_only_bot_comment_does_not_trigger_a_fix_round() {
5721 assert!(
5722 is_noise(CODERABBIT_TRIGGER),
5723 "CodeRabbit's trigger notice declares itself not a review"
5724 );
5725 assert!(
5726 is_noise(CLAUDE_CHECKLIST),
5727 "a progress checklist asks for nothing"
5728 );
5729 assert!(
5730 !is_noise(CLAUDE_FINDING),
5731 "a review that names a bug is input, not noise"
5732 );
5733
5734 let mut clean = pr(Checks::Green, &[], 0);
5735 clean.review_comments.push(ReviewComment {
5736 author: "coderabbitai".to_owned(),
5737 path: None,
5738 line: None,
5739 body: CODERABBIT_TRIGGER.to_owned(),
5740 });
5741 clean.review_comments.retain(|c| !is_noise(&c.body));
5742 assert_eq!(decide(&clean, 0, 4, Duration::ZERO), Step::Merge);
5743
5744 let mut found = pr(Checks::Green, &[], 0);
5745 found.review_comments.push(ReviewComment {
5746 author: "claude".to_owned(),
5747 path: None,
5748 line: None,
5749 body: CLAUDE_FINDING.to_owned(),
5750 });
5751 found.review_comments.retain(|c| !is_noise(&c.body));
5752 assert!(matches!(
5753 decide(&found, 0, 4, Duration::ZERO),
5754 Step::Fix { .. }
5755 ));
5756 }
5757
5758 #[test]
5759 fn the_policy_table_holds_for_every_combination_that_matters() {
5760 let cases: Vec<(&str, PrState, usize, usize, Duration, Step)> = vec![
5761 (
5762 "pending checks are waited for, even on the last round",
5763 pr(Checks::Pending, &[], 0),
5764 4,
5765 4,
5766 Duration::ZERO,
5767 Step::Wait,
5768 ),
5769 (
5770 "red checks are fixed",
5771 pr(Checks::Red, &["editorconfig"], 0),
5772 0,
5773 4,
5774 Duration::ZERO,
5775 Step::Fix {
5776 reason: "1 check(s) failing: editorconfig".to_owned(),
5777 },
5778 ),
5779 (
5780 "green with comments is fixed, not merged",
5781 pr(Checks::Green, &[], 2),
5782 1,
5783 4,
5784 Duration::ZERO,
5785 Step::Fix {
5786 reason: "checks are green but 2 review comment(s) are unresolved: coderabbitai"
5787 .to_owned(),
5788 },
5789 ),
5790 (
5791 "green and clean merges",
5792 pr(Checks::Green, &[], 0),
5793 3,
5794 4,
5795 Duration::ZERO,
5796 Step::Merge,
5797 ),
5798 (
5799 "an unreadable rollup is waited on while the grace lasts",
5800 pr(Checks::Unknown, &[], 0),
5801 0,
5802 4,
5803 Duration::ZERO,
5804 Step::Wait,
5805 ),
5806 (
5807 "an unreadable rollup is never merged once the grace is spent",
5808 pr(Checks::Unknown, &[], 0),
5809 0,
5810 4,
5811 CHECKS_GRACE,
5812 Step::GiveUp {
5813 reason: "no check status is readable on the pull request after 3 minute(s); \
5814 refusing to merge on a guess"
5815 .to_owned(),
5816 },
5817 ),
5818 ];
5819 for (what, state, round, budget, waited, want) in cases {
5820 assert_eq!(decide(&state, round, budget, waited), want, "{what}");
5821 }
5822 }
5823
5824 #[test]
5825 fn the_forge_verdict_survives_the_round_trip_from_gh() {
5826 let green = parse_pr(GREEN_OPEN).expect("parse");
5830 assert_eq!(green.blocking, Blocking::No);
5831 let red = parse_pr(RED_OPEN).expect("parse");
5832 assert_eq!(
5833 red.blocking,
5834 Blocking::No,
5835 "`UNSTABLE` is mergeable: the red check is one nobody requires"
5836 );
5837 assert_eq!(red.checks, Checks::Red, "and it is still reported as red");
5838 let quiet =
5840 parse_pr(&GREEN_OPEN.replace("\"mergeStateStatus\": \"CLEAN\",", "")).expect("parse");
5841 assert_eq!(quiet.blocking, Blocking::Unsaid);
5842 }
5843
5844 #[test]
5845 fn a_red_check_nobody_requires_does_not_buy_a_fix_round() {
5846 let mut nonblocking = pr(Checks::Red, &["editorconfig", "coverage"], 0);
5852 nonblocking.blocking = Blocking::No;
5853 assert_eq!(
5854 decide(&nonblocking, 0, 4, Duration::ZERO),
5855 Step::Merge,
5856 "the forge says nothing is in the way, so nothing is"
5857 );
5858
5859 let mut blocking = pr(Checks::Red, &["test (ubuntu-latest)"], 0);
5861 blocking.blocking = Blocking::Yes;
5862 assert!(matches!(
5863 decide(&blocking, 0, 4, Duration::ZERO),
5864 Step::Fix { .. }
5865 ));
5866
5867 let mut commented = pr(Checks::Red, &["coverage"], 1);
5870 commented.blocking = Blocking::No;
5871 assert!(matches!(
5872 decide(&commented, 0, 4, Duration::ZERO),
5873 Step::Fix { .. }
5874 ));
5875
5876 let mut unsaid = pr(Checks::Red, &["coverage"], 0);
5878 unsaid.blocking = Blocking::Unsaid;
5879 assert!(matches!(
5880 decide(&unsaid, 0, 4, Duration::ZERO),
5881 Step::Fix { .. }
5882 ));
5883 }
5884
5885 #[test]
5886 fn a_red_merge_is_announced_with_every_failing_check_and_a_green_one_is_not() {
5887 let mut red = pr(Checks::Red, &["test (windows-latest)", "coverage"], 0);
5888 red.blocking = Blocking::No;
5889 assert_eq!(
5890 decide(&red, 0, 4, Duration::ZERO),
5891 Step::Merge,
5892 "announcing must not change the decision"
5893 );
5894 let said = red_merge_summary("yukimemi/magi", &red).expect("red merge is announced");
5895 assert!(said.contains("yukimemi/magi"), "{said}");
5896 assert!(said.contains("#16"), "{said}");
5897 assert!(
5898 said.contains("https://github.com/yukimemi/magi/pull/16"),
5899 "{said}"
5900 );
5901 assert!(
5902 said.contains("test (windows-latest)") && said.contains("coverage"),
5903 "{said}"
5904 );
5905
5906 let green = pr(Checks::Green, &["stale"], 0);
5908 assert_eq!(red_merge_summary("yukimemi/magi", &green), None);
5909 }
5910
5911 #[test]
5912 fn red_merge_notices_omit_empty_check_details() {
5913 for checks in [vec![], vec!["", " "]] {
5914 let red = pr(Checks::Red, &checks, 0);
5915 let summary = red_merge_summary("owner/repo", &red).unwrap();
5916 let notice = crate::notices::merged_red("run-1", &summary);
5917 assert_eq!(
5918 notice.message,
5919 format!("Merged owner/repo PR #16 with red checks ({})", red.url)
5920 );
5921 assert_eq!(notice.severity, crate::notices::Severity::Warn);
5922 }
5923 let red = pr(Checks::Red, &["", " test ", " "], 0);
5924 let summary = red_merge_summary("owner/repo", &red).unwrap();
5925 assert!(summary.contains("with red checks: test ("), "{summary}");
5926 }
5927
5928 #[test]
5929 fn the_repo_label_comes_from_the_pull_request_url() {
5930 let p = Path::new("/tmp/checkout");
5931 assert_eq!(
5932 repo_label(p, "https://github.com/yukimemi/magi/pull/16"),
5933 "yukimemi/magi"
5934 );
5935 assert_eq!(repo_label(p, "not a url"), "checkout");
5936 }
5937
5938 #[test]
5939 fn a_branch_the_base_moved_under_is_rebased_not_fixed() {
5940 let mut conflicted = pr(Checks::Green, &[], 0);
5945 conflicted.blocking = Blocking::Conflict;
5946 assert_eq!(decide(&conflicted, 0, 4, Duration::ZERO), Step::Rebase);
5947
5948 let mut red = pr(Checks::Red, &["test (ubuntu-latest)"], 2);
5952 red.blocking = Blocking::Conflict;
5953 assert_eq!(decide(&red, 4, 4, Duration::ZERO), Step::Rebase);
5954
5955 let mut merged = pr(Checks::Red, &[], 0);
5957 merged.blocking = Blocking::Conflict;
5958 merged.state = PrLifecycle::Merged;
5959 assert_eq!(
5960 decide(&merged, 0, 4, Duration::ZERO),
5961 Step::Done { merged: true }
5962 );
5963 }
5964
5965 #[test]
5966 fn the_forge_verdict_is_read_off_merge_state_status() {
5967 for ok in ["CLEAN", "UNSTABLE", "unstable", "HAS_HOOKS"] {
5970 assert_eq!(Blocking::of(ok), Blocking::No, "{ok}");
5971 assert!(!Blocking::of(ok).stops_a_merge(), "{ok}");
5972 }
5973 assert_eq!(Blocking::of("DIRTY"), Blocking::Conflict);
5974 assert_eq!(Blocking::of("BLOCKED"), Blocking::Yes);
5975 assert_eq!(Blocking::of("BEHIND"), Blocking::Yes);
5976 for quiet in ["", "UNKNOWN"] {
5979 assert_eq!(Blocking::of(quiet), Blocking::Unsaid);
5980 assert!(Blocking::of(quiet).stops_a_merge());
5981 }
5982 }
5983
5984 #[test]
5985 fn a_merge_command_that_failed_after_merging_is_still_a_merge() {
5986 let argv = merge_argv(28, "fix: retry uploads on transient network errors");
5987 let jj = "could not determine current branch: failed to run git: not on any branch";
5989
5990 let landed = merged_after_all(&argv, jj, Some(PrLifecycle::Merged))
5991 .expect("the forge says merged, so it merged");
5992 assert!(landed.ok);
5993 assert!(
5994 landed.detail.contains("but the pull request is merged"),
5995 "the record must not read as a clean success: {}",
5996 landed.detail
5997 );
5998 assert!(
5999 landed.detail.contains("not on any branch"),
6000 "and it must keep what the command actually said: {}",
6001 landed.detail
6002 );
6003
6004 assert!(merged_after_all(&argv, jj, Some(PrLifecycle::Open)).is_none());
6006 assert!(merged_after_all(&argv, jj, Some(PrLifecycle::Closed)).is_none());
6007 assert!(merged_after_all(&argv, jj, None).is_none());
6009 }
6010
6011 #[test]
6012 fn a_pull_request_closed_underneath_us_is_done_and_not_merged() {
6013 let mut state = pr(Checks::Red, &["editorconfig"], 3);
6014 state.state = PrLifecycle::Closed;
6015 assert_eq!(
6016 decide(&state, 0, 4, Duration::ZERO),
6017 Step::Done { merged: false },
6018 "a human closing the pull request ends the loop, whatever CI says"
6019 );
6020 }
6021
6022 #[test]
6023 fn the_last_round_gives_up_with_a_reason_naming_what_is_still_failing() {
6024 let red = decide(
6025 &pr(Checks::Red, &["editorconfig", "test (macos)"], 0),
6026 4,
6027 4,
6028 Duration::ZERO,
6029 );
6030 match red {
6031 Step::GiveUp { reason } => {
6032 assert!(reason.contains("editorconfig"), "reason: {reason}");
6033 assert!(reason.contains("test (macos)"), "reason: {reason}");
6034 assert!(reason.contains("4 fix round(s)"), "reason: {reason}");
6035 }
6036 other => panic!("expected a give-up, got {other:?}"),
6037 }
6038
6039 let commented = decide(&pr(Checks::Green, &[], 1), 2, 2, Duration::ZERO);
6040 match commented {
6041 Step::GiveUp { reason } => {
6042 assert!(reason.contains("unresolved"), "reason: {reason}");
6043 assert!(reason.contains("2 fix round(s)"), "reason: {reason}");
6044 }
6045 other => panic!("expected a give-up, got {other:?}"),
6046 }
6047 }
6048
6049 #[test]
6050 fn the_merge_command_squashes_deletes_the_branch_and_sets_its_own_subject() {
6051 let candidate_commit = "magi: candidate A (uncommitted work)";
6052 let subject = merge_subject(candidate_commit, "add retries to the uploader");
6053 let argv = merge_argv(16, &subject);
6054
6055 assert!(argv.contains(&"--squash".to_owned()));
6056 assert!(argv.contains(&"--delete-branch".to_owned()));
6057 assert!(argv.contains(&"--subject".to_owned()));
6058 assert_eq!(
6059 argv.last().map(String::as_str),
6060 Some("add retries to the uploader"),
6061 "the subject must not be the candidate commit message"
6062 );
6063 assert_ne!(subject, candidate_commit);
6064 }
6065
6066 #[test]
6067 fn a_real_pull_request_title_is_used_as_the_squash_subject_verbatim() {
6068 assert_eq!(
6069 merge_subject("feat: a queue, an unattended loop, and a phone UI", "task"),
6070 "feat: a queue, an unattended loop, and a phone UI"
6071 );
6072 assert_eq!(
6073 merge_subject("", "# port the retry logic\n\ndetails"),
6074 "port the retry logic",
6075 "an empty title falls back to the task's first line, heading marks stripped"
6076 );
6077 }
6078
6079 #[test]
6080 fn a_failing_checks_details_url_yields_the_job_to_read_logs_from() {
6081 let url = "https://github.com/yukimemi/magi/actions/runs/33587406996/job/100114323572";
6082 assert_eq!(job_of(url).as_deref(), Some("100114323572"));
6083 assert_eq!(run_of(url).as_deref(), Some("33587406996"));
6084 assert_eq!(job_of("https://coderabbit.ai/status"), None);
6085 assert_eq!(run_of(""), None);
6086 }
6087
6088 #[test]
6089 fn magis_own_stop_comment_is_never_read_back_as_a_finding() {
6090 let mut out = Vec::new();
6091 push_if_outstanding(
6092 &mut out,
6093 ReviewComment {
6094 author: "yukimemi".to_owned(),
6095 path: None,
6096 line: None,
6097 body: format!("{MARKER}\nmagi stopped landing this pull request: 1 check failing"),
6098 },
6099 );
6100 assert!(out.is_empty());
6101 }
6102
6103 fn run_state() -> RunState {
6107 let mut state = RunState::new(
6108 std::path::PathBuf::from("/repo/magi"),
6109 "main".to_owned(),
6110 "abcdef1234".to_owned(),
6111 "add retries to the uploader".to_owned(),
6112 crate::config::Config::default(),
6113 );
6114 static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
6119 let nanos = std::time::SystemTime::now()
6120 .duration_since(std::time::UNIX_EPOCH)
6121 .map_or(0, |d| d.subsec_nanos() % 1_000_000);
6122 state.id = format!(
6123 "20261004-{nanos:06}-{:04x}",
6124 NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
6125 );
6126 state
6127 }
6128
6129 fn green_pr() -> PrState {
6130 PrState {
6131 url: "https://github.com/yukimemi/magi/pull/42".to_owned(),
6132 number: 42,
6133 state: PrLifecycle::Open,
6134 checks: Checks::Green,
6135 blocking: Blocking::No,
6137 failing: Vec::new(),
6138 review_comments: vec![ReviewComment {
6139 author: "coderabbitai".to_owned(),
6140 path: Some("src/land.rs".to_owned()),
6141 line: Some(212),
6142 body: "this branch never checks the exit code".to_owned(),
6143 }],
6144 }
6145 }
6146
6147 #[test]
6148 fn github_facing_land_text_is_english_whatever_the_language() {
6149 let mut state = run_state();
6150 state.config.graph.language = "ja".to_owned();
6151 let comment = stop_comment(&state.id, "checks are still red");
6152 assert!(crate::github_text::check("", &comment).is_empty());
6153 assert!(comment.is_ascii(), "{comment}");
6154 assert!(comment.starts_with(MARKER));
6155
6156 let p = fix_prompt(&state, &green_pr(), 1, 2, "red", "");
6157 let ja_at = p.find("Write all prose in ja").unwrap();
6158 let rule_at = p.find(crate::prompt::GITHUB_ENGLISH_HEADING).unwrap();
6159 assert!(ja_at < rule_at, "{p}");
6160 assert!(p.contains("stays in Japanese"), "{p}");
6161
6162 state.config.graph.language = "en".to_owned();
6163 let p = fix_prompt(&state, &green_pr(), 1, 2, "red", "");
6164 assert!(p.contains(crate::prompt::GITHUB_ENGLISH_HEADING), "{p}");
6165 assert!(!p.contains("does not apply"), "{p}");
6166 }
6167
6168 const NUMSTAT: &str = "12\t3\tsrc/land.rs\n40\t1\tsrc/web.rs\n-\t-\tassets/logo.png";
6169
6170 fn panel() -> String {
6171 approval_panel(
6172 &run_state(),
6173 &green_pr(),
6174 NUMSTAT,
6175 "diff --git a/src/land.rs b/src/land.rs\n@@ -1,2 +1,2 @@\n-old line\n+new line\n context",
6176 &[
6177 "land: ask before merging".to_owned(),
6178 "land: colour the diff".to_owned(),
6179 ],
6180 "feat: merge approval from the phone",
6181 )
6182 }
6183
6184 #[test]
6185 fn the_approval_panel_carries_the_whole_case_for_the_merge() {
6186 let html = panel();
6187 for needle in [
6188 "42",
6189 "main",
6190 "src/land.rs",
6191 "src/web.rs",
6192 "assets/logo.png",
6193 "feat: merge approval from the phone",
6194 "land: ask before merging",
6195 "land: colour the diff",
6196 "coderabbitai",
6197 "this branch never checks the exit code",
6198 "green",
6199 ] {
6200 assert!(html.contains(needle), "the panel must state `{needle}`");
6201 }
6202 }
6203
6204 fn winning_candidate(summary: &str) -> Candidate {
6207 Candidate {
6208 index: 0,
6209 label: 'A',
6210 agent: "opus".to_owned(),
6211 branch: "magi/x/A".to_owned(),
6212 worktree: PathBuf::from("/wt/A"),
6213 summary: summary.to_owned(),
6214 stat: String::new(),
6215 files: 1,
6216 commits: 1,
6217 empty: false,
6218 failed: None,
6219 verified_noop: None,
6220 duration_ms: 0,
6221 folded: false,
6222 }
6223 }
6224
6225 fn uncontested_tally() -> Tally {
6226 Tally {
6227 first_choice: BTreeMap::from([('A', 1)]),
6228 borda: BTreeMap::new(),
6229 winner: 'A',
6230 rankings: 1,
6231 unanimous_initial: true,
6232 deliberated: false,
6233 changed_votes: 0,
6234 unanimous_final: true,
6235 tie_break: None,
6236 judges: 1,
6237 present: 1,
6238 quorum: 1,
6239 met_quorum: true,
6240 uncontested: None,
6241 }
6242 }
6243
6244 fn review_record(reviewer: usize, agent: &str, summary: &str) -> ReviewRecord {
6245 ReviewRecord {
6246 attempts: 0,
6247 reviewer,
6248 agent: agent.to_owned(),
6249 summary: summary.to_owned(),
6250 findings: Vec::new(),
6251 vote: None,
6252 failed: None,
6253 duration_ms: 0,
6254 }
6255 }
6256
6257 fn review_round(round: usize, reviews: Vec<ReviewRecord>) -> ReviewRound {
6258 let answered = reviews.len();
6259 ReviewRound {
6260 round,
6261 head: "abc1234".to_owned(),
6262 verified_head: None,
6263 verified_at: None,
6264 reviews,
6265 e2e: Vec::new(),
6266 verify_retried: false,
6267 e2e_deferred: false,
6268 e2e_defer_reason: None,
6269 fix: None,
6270 blocking: 0,
6271 answered,
6272 expected: answered,
6273 clean: true,
6274 progressed: false,
6275 vote_split: false,
6276 reconsideration: Vec::new(),
6277 verdict: None,
6278 }
6279 }
6280
6281 #[test]
6282 fn the_approval_panel_states_the_task_verbatim_in_either_language() {
6283 let en = panel();
6284 assert!(en.contains("Task"), "{en}");
6285 assert!(en.contains("add retries to the uploader"), "{en}");
6286
6287 let mut state = run_state();
6288 state.config.graph.language = "ja".to_owned();
6289 let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6290 assert!(ja.contains("タスク"), "{ja}");
6291 assert!(
6292 ja.contains("add retries to the uploader"),
6293 "the task itself is not translated: {ja}"
6294 );
6295 }
6296
6297 #[test]
6298 fn the_approval_panel_omits_what_changed_and_review_verdict_with_no_data() {
6299 let html = panel();
6303 assert!(!html.contains("What changed"), "{html}");
6304 assert!(!html.contains("Review verdict"), "{html}");
6305 }
6306
6307 #[test]
6308 fn the_approval_panel_omits_what_changed_when_the_winners_summary_is_empty() {
6309 let mut state = run_state();
6310 state.candidates = vec![winning_candidate("")];
6311 state.tally = Some(uncontested_tally());
6312 let html = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6313 assert!(
6314 !html.contains("What changed"),
6315 "an empty summary must not render an empty box: {html}"
6316 );
6317 }
6318
6319 #[test]
6320 fn the_approval_panel_shows_the_winners_own_account_in_either_language() {
6321 let mut state = run_state();
6322 state.candidates = vec![winning_candidate(
6323 "Added a retry loop around the uploader PUT call.",
6324 )];
6325 state.tally = Some(uncontested_tally());
6326 let en = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6327 assert!(en.contains("What changed"), "{en}");
6328 assert!(
6329 en.contains("Added a retry loop around the uploader PUT call."),
6330 "{en}"
6331 );
6332
6333 state.config.graph.language = "ja".to_owned();
6334 let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6335 assert!(ja.contains("変更内容"), "{ja}");
6336 assert!(
6337 ja.contains("Added a retry loop around the uploader PUT call."),
6338 "{ja}"
6339 );
6340 }
6341
6342 #[test]
6343 fn the_approval_panel_shows_only_the_last_review_rounds_verdict() {
6344 let mut state = run_state();
6345 state.reviews = vec![
6346 review_round(
6347 1,
6348 vec![review_record(1, "alpha", "found a race, sent back")],
6349 ),
6350 review_round(2, vec![review_record(1, "alpha", "race is fixed, clean")]),
6351 ];
6352 let en = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6353 assert!(en.contains("Review verdict"), "{en}");
6354 assert!(en.contains("race is fixed, clean"), "{en}");
6355 assert!(
6356 !en.contains("found a race, sent back"),
6357 "only the round that actually cleared the merge should show: {en}"
6358 );
6359
6360 state.config.graph.language = "ja".to_owned();
6361 let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6362 assert!(ja.contains("レビューの結論"), "{ja}");
6363 assert!(ja.contains("レビュアー"), "{ja}");
6364 assert!(ja.contains("race is fixed, clean"), "{ja}");
6365 }
6366
6367 fn unanswered_review_record(reviewer: usize, agent: &str, reason: &str) -> ReviewRecord {
6373 ReviewRecord {
6374 attempts: 0,
6375 reviewer,
6376 agent: agent.to_owned(),
6377 summary: String::new(),
6378 findings: Vec::new(),
6379 vote: None,
6380 failed: Some(reason.to_owned()),
6381 duration_ms: 0,
6382 }
6383 }
6384
6385 #[test]
6386 fn the_approval_panel_never_shows_an_unanswered_seat_as_a_blank_verdict() {
6387 let mut state = run_state();
6388 state.reviews = vec![review_round(
6389 1,
6390 vec![
6391 review_record(1, "alpha", "clean, nothing to add"),
6392 unanswered_review_record(2, "beta", "timed out"),
6393 ],
6394 )];
6395 let en = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6396 assert!(en.contains("clean, nothing to add"), "{en}");
6397 assert!(
6398 en.contains("produced no answer: timed out"),
6399 "a seat that never answered must say so, not render a blank box: {en}"
6400 );
6401 assert!(
6402 !en.contains("<div style=\"white-space:pre-wrap;font-size:13px\"></div>"),
6403 "no reviewer box may be left empty: {en}"
6404 );
6405
6406 state.config.graph.language = "ja".to_owned();
6407 let ja = approval_panel(&state, &green_pr(), NUMSTAT, "", &[], "feat: x");
6408 assert!(ja.contains("回答なし: timed out"), "{ja}");
6409 }
6410
6411 #[test]
6412 fn the_approval_panel_contains_nothing_the_frames_policy_would_block() {
6413 let html = panel();
6414 assert!(!html.contains("<script"), "no script survives the csp");
6415 assert!(!html.contains("<form"), "form-action is 'none'");
6416 let pr = green_pr();
6417 assert_eq!(
6418 html.matches("http").count(),
6419 html.matches(pr.url.as_str()).count(),
6420 "the only http url in the panel is the pull request's own link"
6421 );
6422 assert!(
6423 html.contains("target=\"_blank\""),
6424 "the PR link must open in a new tab so it escapes the iframe"
6425 );
6426 assert!(
6427 html.contains("rel=\"noopener noreferrer\""),
6428 "external links opening in new tabs must carry noopener noreferrer"
6429 );
6430 }
6431
6432 #[test]
6433 fn added_and_removed_diff_lines_are_distinguishable_without_colour() {
6434 let html = panel();
6435 assert!(
6436 html.contains(">+</span>"),
6437 "an added line carries a `+` in the gutter, not only a background"
6438 );
6439 assert!(
6440 html.contains(">-</span>"),
6441 "a removed line carries a `-` in the gutter, not only a background"
6442 );
6443 assert!(
6444 html.contains(">new line</span>"),
6445 "the marker is moved to the gutter, so the body is printed once without it"
6446 );
6447 }
6448
6449 #[test]
6450 fn a_diff_past_the_threshold_is_cut_with_an_honest_count() {
6451 let total = DIFF_MAX_LINES + 100;
6452 let diff: String = (0..total).map(|i| format!("+line {i}\n")).collect();
6453 let html = approval_panel(
6454 &run_state(),
6455 &green_pr(),
6456 NUMSTAT,
6457 &diff,
6458 &[],
6459 "feat: something long",
6460 );
6461 assert!(
6462 html.contains(&format!("100 of {total} diff lines omitted")),
6463 "the note must say exactly how much was cut"
6464 );
6465 assert!(html.contains(&format!("line {}", DIFF_MAX_LINES - 1)));
6466 assert!(
6467 !html.contains(&format!("line {DIFF_MAX_LINES}")),
6468 "nothing past the threshold is rendered"
6469 );
6470 assert!(
6471 html.contains("/repo/magi"),
6472 "the note says where the rest is"
6473 );
6474 }
6475
6476 #[test]
6477 fn a_path_with_html_metacharacters_is_escaped_rather_than_rendered() {
6478 let html = approval_panel(
6479 &run_state(),
6480 &green_pr(),
6481 "1\t2\tsrc/<b>&\"x\"'.rs",
6482 "",
6483 &[],
6484 "subject",
6485 );
6486 assert!(html.contains("src/<b>&"x"'.rs"));
6487 assert!(
6488 !html.contains("<b>"),
6489 "an agent-influenced path must never become markup"
6490 );
6491 }
6492
6493 #[tokio::test]
6494 async fn the_merge_lock_serialises_one_repository_but_never_a_different_one() {
6495 let a = std::path::PathBuf::from("/repo/a");
6496 let b = std::path::PathBuf::from("/repo/b");
6497
6498 let held = repo_merge_lock(&a).lock_owned().await;
6499
6500 assert!(
6503 repo_merge_lock(&a).try_lock().is_err(),
6504 "a second merge into the same repository must not proceed concurrently"
6505 );
6506
6507 assert!(
6511 repo_merge_lock(&b).try_lock().is_ok(),
6512 "a different repository's merge lock must be independent"
6513 );
6514
6515 drop(held);
6516 assert!(
6517 repo_merge_lock(&a).try_lock().is_ok(),
6518 "the lock is released once the holder is done"
6519 );
6520 }
6521
6522 pub(crate) fn repair_state(major: bool, reject: bool) -> RunState {
6523 let mut state = landing_state();
6524 let mut rec = review_record(1, "alpha", "precise rejection reason");
6525 rec.vote = Some(if reject {
6526 crate::verdict::ReviewVote::Reject
6527 } else {
6528 crate::verdict::ReviewVote::Approve
6529 });
6530 if major {
6531 rec.findings = contested().findings;
6532 }
6533 let mut round = review_round(1, vec![rec]);
6534 round.clean = false;
6535 round.blocking = usize::from(major);
6536 state.reviews.push(round);
6537 state.config.graph.review_rounds = 1;
6538 state
6539 }
6540
6541 #[test]
6542 fn merge_repair_choice_covers_major_only_and_reject_only_without_contested_and() {
6543 for (major, reject) in [(true, false), (false, true), (true, true)] {
6544 let state = repair_state(major, reject);
6545 assert!(repair_available(&state));
6546 assert_eq!(&approval_choices(&state)[..3], [APPROVE, HOLD, REPAIR]);
6547 }
6548 assert!(!repair_available(&repair_state(false, false)));
6549 let mut clean = repair_state(true, false);
6550 clean.reviews[0].clean = true;
6551 assert_eq!(&approval_choices(&clean)[..3], [APPROVE, HOLD, REPAIR]);
6552 assert_eq!(approval(Some(REPAIR)), Approval::Repair);
6553 }
6554
6555 #[tokio::test]
6556 async fn merge_repair_upgrades_legacy_questions_and_snapshots_exact_evidence_once() {
6557 let mut state = repair_state(true, false);
6558 let pr = green_pr();
6559 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6560 .await
6561 .unwrap();
6562 let store = ask::Questions::open();
6563 let id = state.land_approval.as_ref().unwrap().question.clone();
6564 store
6566 .update(&id, |q| {
6567 q.choices = vec![APPROVE.into(), HOLD.into()];
6568 Ok(())
6569 })
6570 .unwrap();
6571 let upgraded = store.get(&id).unwrap();
6572 assert_eq!(upgraded.choices, approval_choices(&state));
6573 assert!(!upgraded.actions.contains_key(HOLD));
6574 assert_eq!(store.get(&id).unwrap().choices, upgraded.choices);
6575 assert!(
6576 store
6577 .list()
6578 .iter()
6579 .any(|q| q.id == id && q.choices == upgraded.choices)
6580 );
6581 assert!(
6582 crate::prompt::chat_consult(&upgraded).contains("--reply 'fix findings and re-review'")
6583 );
6584 assert!(deputy_brief(&upgraded, Some(&state)).contains("never approves a merge"));
6585 store
6586 .update(&id, |q| q.answer(ask::Answer::Choice(REPAIR.into())))
6587 .unwrap();
6588 store
6591 .update(&id, |q| {
6592 q.actions.clear();
6593 Ok(())
6594 })
6595 .unwrap();
6596 assert_eq!(
6597 store.get(&id).unwrap().chosen_action(),
6598 Some(&ask::ChoiceAction::Resume {
6599 run: state.id.clone()
6600 })
6601 );
6602 let binding = state.land_approval.clone();
6603 assert_eq!(
6604 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6605 .await
6606 .unwrap(),
6607 ApprovalGate::Repair
6608 );
6609 assert!(state.land_approval.is_none());
6610 assert_eq!(state.merge_repairs.len(), 1);
6611 let request = &state.merge_repairs[0];
6612 assert_eq!(request.question, id);
6613 assert_eq!(request.head, "abc");
6614 assert_eq!(request.pr, pr.url);
6615 assert_eq!(
6616 request.reviews[0].reviews[0].summary,
6617 "precise rejection reason"
6618 );
6619 assert_eq!(request.reviews[0].reviews[0].findings[0].id, "R3-1-1");
6620 assert_eq!(
6621 request.reviews[0].reviews[0].findings[0].detail,
6622 state.reviews[0].reviews[0].findings[0].detail
6623 );
6624 state.land_approval = binding;
6626 assert_eq!(
6627 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6628 .await
6629 .unwrap(),
6630 ApprovalGate::Repair
6631 );
6632 assert_eq!(state.merge_repairs.len(), 1);
6633 }
6634
6635 #[tokio::test]
6636 async fn merge_repair_answer_never_merges_and_hold_never_requests_repair() {
6637 for choice in [REPAIR, HOLD] {
6638 let mut state = repair_state(false, true);
6639 let observed = seen("abc", Checks::Green, "CLEAN", false);
6640 approval_gate(&mut state, &observed.pr, "feat: x", None, "abc")
6641 .await
6642 .unwrap();
6643 let id = state.land_approval.as_ref().unwrap().question.clone();
6644 ask::Questions::open()
6645 .update(&id, |q| q.answer(ask::Answer::Choice(choice.into())))
6646 .unwrap();
6647 let forge = Scripted::new(vec![observed], vec![(true, "")]);
6648 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
6649 .await
6650 .unwrap();
6651 assert!(
6652 forge
6653 .argvs()
6654 .iter()
6655 .all(|a| a.iter().any(|s| s == "--disable-auto"))
6656 );
6657 assert!(!forge.calls().contains(&"fix"));
6658 assert_eq!(state.merge_repairs.len(), usize::from(choice == REPAIR));
6659 assert_ne!(state.status, RunStatus::Merged);
6660 }
6661 }
6662
6663 #[test]
6664 fn only_the_merge_choice_merges_and_silence_holds() {
6665 let table = [
6666 (None, Approval::Hold),
6667 (Some("merge"), Approval::Merge),
6668 (Some(" merge\n"), Approval::Merge),
6669 (Some("hold"), Approval::Hold),
6670 (Some(""), Approval::Hold),
6671 (Some("yes"), Approval::Hold),
6672 ];
6673 for (answer, want) in table {
6674 assert_eq!(
6675 approval(answer),
6676 want,
6677 "answer {answer:?} must resolve to {want:?}"
6678 );
6679 }
6680 }
6681
6682 #[tokio::test]
6683 async fn a_first_visit_to_the_merge_gate_files_a_question_and_returns_pending_at_once() {
6684 let mut state = landing_state();
6685 state.config.graph.land_approval = true;
6686 let pr = green_pr();
6687
6688 let gate = approval_gate(&mut state, &pr, "feat: x", None, "abc")
6689 .await
6690 .unwrap();
6691 assert_eq!(gate, ApprovalGate::Pending, "nobody has answered yet");
6692 assert!(
6693 !state.parked,
6694 "approval_gate itself never sets `parked`; only its caller does"
6695 );
6696
6697 let store = ask::Questions::open();
6698 let filed: Vec<_> = store
6699 .list()
6700 .into_iter()
6701 .filter(|q| q.run == state.id)
6702 .collect();
6703 assert_eq!(filed.len(), 1, "exactly one question is filed");
6704 assert_eq!(filed[0].node, APPROVAL_NODE);
6705 assert_eq!(filed[0].choices, approval_choices(&state));
6706 assert!(filed[0].status.open());
6707
6708 let again = approval_gate(&mut state, &pr, "feat: x", None, "abc")
6712 .await
6713 .unwrap();
6714 assert_eq!(again, ApprovalGate::Pending);
6715 let still_one = store
6716 .list()
6717 .into_iter()
6718 .filter(|q| q.run == state.id)
6719 .count();
6720 assert_eq!(
6721 still_one, 1,
6722 "asking twice must not double-file the question"
6723 );
6724 }
6725
6726 #[tokio::test]
6727 async fn approving_the_existing_question_is_read_back_as_approved() {
6728 crate::run::pin_test_home();
6729 let mut state = run_state();
6730 state.config.graph.land_approval = true;
6731 let pr = green_pr();
6732 assert_eq!(
6733 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6734 .await
6735 .unwrap(),
6736 ApprovalGate::Pending
6737 );
6738
6739 let store = ask::Questions::open();
6740 let mut q = store
6741 .list()
6742 .into_iter()
6743 .find(|q| q.run == state.id)
6744 .expect("filed above");
6745 let talks = crate::talk::Talks::open();
6746 let cfg = crate::config::Config {
6747 agents: vec![crate::config::AgentSpec {
6748 id: "mock".into(),
6749 kind: crate::config::AgentKind::Command,
6750 model: None,
6751 command: vec!["true".into()],
6752 extra_args: Vec::new(),
6753 env: Default::default(),
6754 prompt_delivery: None,
6755 }],
6756 ..crate::config::Config::default()
6757 };
6758 let mut talk = crate::talk::begin(&talks, &cfg, state.repo.clone(), Some("mock")).unwrap();
6759 crate::consult::begin(&store, &talks, &q, &talk).unwrap();
6760 crate::talk::drain(&mut talk, &talks).unwrap();
6761 assert_eq!(
6762 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6763 .await
6764 .unwrap(),
6765 ApprovalGate::Pending,
6766 "consultation cannot approve or hold the merge"
6767 );
6768 q = store.get(&q.id).unwrap();
6769 assert!(q.answer.is_none());
6770 talk.turns.push(crate::talk::Turn {
6771 sender: None,
6772 breaks: Some(Vec::new()),
6773 messages: Vec::new(),
6774 posted_by: None,
6775 who: crate::talk::Who::Operator,
6776 body: "merge abc".into(),
6777 at: Timestamp::now(),
6778 attachments: Vec::new(),
6779 usage: None,
6780 });
6781 talks.put(&mut talk).unwrap();
6782 crate::consult::validate_answer(
6783 &q,
6784 &talks,
6785 &talk.id,
6786 crate::queue::CHAT_NODE,
6787 APPROVE,
6788 Some("merge abc"),
6789 )
6790 .unwrap();
6791 q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
6792 store.put(&mut q).unwrap();
6793
6794 assert_eq!(
6795 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6796 .await
6797 .unwrap(),
6798 ApprovalGate::Approved
6799 );
6800 }
6801
6802 #[tokio::test]
6803 async fn legacy_hold_actions_are_removed_on_get_and_list_before_answering() {
6804 crate::run::pin_test_home();
6805 let store = ask::Questions::open();
6806 for via_list in [false, true] {
6807 let mut state = repair_state(false, true);
6808 let pr = green_pr();
6809 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6810 .await
6811 .unwrap();
6812 let id = state.land_approval.as_ref().unwrap().question.clone();
6813 store
6814 .update(&id, |q| {
6815 q.actions.insert(
6816 HOLD.to_owned(),
6817 ask::ChoiceAction::Resume {
6818 run: state.id.clone(),
6819 },
6820 );
6821 Ok(())
6822 })
6823 .unwrap();
6824 let q = if via_list {
6825 store.list().into_iter().find(|q| q.id == id).unwrap()
6826 } else {
6827 store.get(&id).unwrap()
6828 };
6829 assert!(!q.actions.contains_key(HOLD));
6830 store
6831 .update(&id, |q| {
6832 assert!(!q.actions.contains_key(HOLD));
6834 assert!(!upgrade_repair_choice(q, &state));
6835 q.answer(ask::Answer::Choice(HOLD.to_owned()))
6836 })
6837 .unwrap();
6838 assert!(store.get(&id).unwrap().chosen_action().is_none());
6839 assert_eq!(
6840 approval_gate(&mut state, &pr, "feat: x", None, "abc")
6841 .await
6842 .unwrap(),
6843 ApprovalGate::Held
6844 );
6845 }
6846 }
6847
6848 #[tokio::test]
6849 async fn holding_or_abandoning_the_existing_question_is_read_back_as_held() {
6850 crate::run::pin_test_home();
6851 let store = ask::Questions::open();
6852
6853 let mut held_state = run_state();
6854 held_state.config.graph.land_approval = true;
6855 let pr = green_pr();
6856 approval_gate(&mut held_state, &pr, "feat: x", None, "abc")
6857 .await
6858 .unwrap();
6859 let mut q = store
6860 .list()
6861 .into_iter()
6862 .find(|q| q.run == held_state.id)
6863 .expect("filed above");
6864 q.answer(ask::Answer::Choice(HOLD.to_owned())).unwrap();
6865 store.put(&mut q).unwrap();
6866 assert_eq!(
6867 approval_gate(&mut held_state, &pr, "feat: x", None, "abc")
6868 .await
6869 .unwrap(),
6870 ApprovalGate::Held
6871 );
6872
6873 let mut abandoned_state = run_state();
6874 abandoned_state.config.graph.land_approval = true;
6875 approval_gate(&mut abandoned_state, &pr, "feat: x", None, "abc")
6876 .await
6877 .unwrap();
6878 let mut q = store
6879 .list()
6880 .into_iter()
6881 .find(|q| q.run == abandoned_state.id)
6882 .expect("filed above");
6883 q.abandon("no answer within the timeout");
6884 store.put(&mut q).unwrap();
6885 assert_eq!(
6886 approval_gate(&mut abandoned_state, &pr, "feat: x", None, "abc")
6887 .await
6888 .unwrap(),
6889 ApprovalGate::Held,
6890 "silence must never merge"
6891 );
6892 }
6893
6894 fn contested() -> ContestedHandoff {
6895 let finding = |id: &str, n: u32| crate::verdict::Finding {
6896 id: id.to_owned(),
6897 severity: crate::verdict::Severity::Major,
6898 file: Some("src/a.rs".to_owned()),
6899 line: Some(n),
6900 title: format!("problem {id}"),
6901 detail: String::new(),
6902 };
6903 ContestedHandoff {
6904 findings: (1..=7).map(|n| finding(&format!("R3-1-{n}"), n)).collect(),
6905 rejecters: vec![(1, "alpha".to_owned())],
6906 }
6907 }
6908
6909 #[test]
6910 fn the_contested_record_is_asked_about_unless_the_switch_is_off() {
6911 let mut state = run_state();
6912 assert!(contested_to_ask(&state).is_none(), "nothing recorded");
6913 state.contested_handoff = Some(contested());
6914 assert!(contested_to_ask(&state).is_some());
6915 state.config.graph.hold_contested_merge = false;
6916 assert!(
6917 contested_to_ask(&state).is_none(),
6918 "the switch restores today"
6919 );
6920 }
6921
6922 #[test]
6923 fn the_deputy_brief_carries_the_pr_the_findings_and_names_what_is_missing() {
6924 let q = ask::Question::new(
6925 "run-1".to_owned(),
6926 APPROVAL_NODE.to_owned(),
6927 "land".to_owned(),
6928 "Merge?".to_owned(),
6929 String::new(),
6930 vec![APPROVE.to_owned(), HOLD.to_owned()],
6931 );
6932 let none = deputy_brief(&q, None);
6933 assert!(none.contains("could not be read"), "{none}");
6934 assert!(none.contains("Silence is a hold"), "{none}");
6935
6936 let mut state = run_state();
6937 state.pr = Some(crate::run::PrRecord {
6938 url: "https://example.test/pull/7".to_owned(),
6939 number: 7,
6940 state: "open".to_owned(),
6941 checks: "green".to_owned(),
6942 round: 0,
6943 rounds: 3,
6944 red_at_merge: Vec::new(),
6945 });
6946 state.contested_handoff = Some(contested());
6947 let b = deputy_brief(&q, Some(&state));
6948 assert!(b.contains("https://example.test/pull/7"), "{b}");
6949 assert!(b.contains("[R3-1-1] major (src/a.rs:1)"), "{b}");
6950 assert!(b.contains("#1"), "the rejecting seat: {b}");
6951 state.contested_handoff = None;
6952 assert!(deputy_brief(&q, Some(&state)).contains("not recorded as contested"));
6953 }
6954
6955 #[test]
6956 fn the_contested_question_names_the_pr_the_findings_and_the_rejecter() {
6957 for lang in ["en", "ja"] {
6958 let mut cfg = crate::config::Config::default();
6959 cfg.graph.language = lang.to_owned();
6960 let w = words(&cfg.graph.language);
6961 let text = w.approval_detail(
6962 "https://github.com/yukimemi/magi/pull/42",
6963 "main",
6964 "feat: x",
6965 Some(&contested()),
6966 );
6967 assert!(text.contains("pull/42"), "{text}");
6968 assert!(
6969 text.contains("R3-1-1 major src/a.rs:1: problem R3-1-1"),
6970 "{text}"
6971 );
6972 assert!(text.contains("R3-1-5"), "{text}");
6973 assert!(!text.contains("R3-1-6"), "the list is capped: {text}");
6974 assert!(text.contains("2"), "the rest are counted: {text}");
6975 assert!(text.contains("#1 (alpha)"), "{text}");
6976 }
6977 let plain = words("en").approval_detail("u", "main", "s", None);
6978 assert!(!plain.contains("reject"), "{plain}");
6979 }
6980
6981 #[tokio::test]
6982 async fn a_contested_question_is_filed_once_and_a_resume_finds_the_same_one() {
6983 crate::run::pin_test_home();
6984 let mut state = run_state();
6985 state.config.graph.land_approval = false;
6986 state.contested_handoff = Some(contested());
6987 let pr = green_pr();
6988 let c = contested_to_ask(&state);
6989 assert_eq!(
6990 approval_gate(&mut state, &pr, "feat: x", c.as_ref(), "abc")
6991 .await
6992 .unwrap(),
6993 ApprovalGate::Pending,
6994 "silence is a hold"
6995 );
6996 let store = ask::Questions::open();
6997 let filed: Vec<_> = store
6998 .list()
6999 .into_iter()
7000 .filter(|q| q.run == state.id)
7001 .collect();
7002 assert_eq!(filed.len(), 1);
7003 assert!(filed[0].detail.contains("R3-1-1"), "{}", filed[0].detail);
7004
7005 assert_eq!(
7006 approval_gate(&mut state, &pr, "feat: x", c.as_ref(), "abc")
7007 .await
7008 .unwrap(),
7009 ApprovalGate::Pending
7010 );
7011 let mut q = store
7012 .list()
7013 .into_iter()
7014 .find(|q| q.run == state.id)
7015 .unwrap();
7016 assert_eq!(q.id, filed[0].id, "the same question after a resume");
7017 q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
7018 store.put(&mut q).unwrap();
7019 assert_eq!(
7020 approval_gate(&mut state, &pr, "feat: x", c.as_ref(), "abc")
7021 .await
7022 .unwrap(),
7023 ApprovalGate::Approved
7024 );
7025 }
7026
7027 #[test]
7028 fn the_diffstat_table_is_ordered_by_churn_with_binaries_last() {
7029 let rows = parse_numstat(NUMSTAT);
7030 assert_eq!(
7031 rows.iter().map(|r| r.path.as_str()).collect::<Vec<_>>(),
7032 ["src/web.rs", "src/land.rs", "assets/logo.png"]
7033 );
7034 assert_eq!(rows[2].added, None, "a binary file has no line counts");
7035 }
7036 #[test]
7037 fn the_approval_speaks_the_language_the_repository_is_configured_for() {
7038 let mut state = run_state();
7042 state.config.graph.language = "ja".to_owned();
7043 let pr = green_pr();
7044 let commits = ["c1".to_owned()];
7045
7046 let ja = approval_panel(&state, &pr, "3\t1\tsrc/a.rs", "+ x", &commits, "feat: x");
7047 assert!(ja.contains("lang=\"ja\""), "the document must declare it");
7048 assert!(ja.contains("squash されるコミット"), "{ja}");
7049 assert!(ja.contains("レビューコメント"), "{ja}");
7050 assert!(ja.contains("差分"), "{ja}");
7051 assert!(
7052 !ja.contains("Commits being squashed"),
7053 "no English left over"
7054 );
7055
7056 let w = words("ja");
7057 assert!(w.approval_summary(17, "feat: x").contains("マージ"));
7058 assert!(
7059 w.approval_detail("http://x/1", "main", "feat: x", None)
7060 .contains("パネル")
7061 );
7062
7063 assert!(ja.contains("src/a.rs"), "the diffstat is not prose");
7065 assert!(ja.contains("feat: x"), "nor is the merge subject");
7066
7067 state.config.graph.language = "en".to_owned();
7070 let en = approval_panel(&state, &pr, "3\t1\tsrc/a.rs", "+ x", &commits, "feat: x");
7071 assert!(en.contains("Commits being squashed"), "{en}");
7072 assert_eq!(words("Klingon").html_lang, "en");
7073 }
7074
7075 #[test]
7079 fn pick_open_pr_classifies_by_count_and_base() {
7080 let one = r#"[{"number":58,"url":"https://x/pull/58","title":"t","baseRefName":"main"}]"#;
7081 assert_eq!(
7082 pick_open_pr(one, "main").unwrap(),
7083 OpenPr::One {
7084 url: "https://x/pull/58".into(),
7085 title: "t".into()
7086 }
7087 );
7088 assert_eq!(pick_open_pr("[]", "main").unwrap(), OpenPr::None);
7089 assert_eq!(pick_open_pr(one, "dev").unwrap(), OpenPr::None);
7090 let two = r#"[{"number":1,"url":"u1","title":"","baseRefName":"main"},
7091 {"number":2,"url":"u2","title":"","baseRefName":"main"}]"#;
7092 assert_eq!(
7093 pick_open_pr(two, "main").unwrap(),
7094 OpenPr::Many(vec!["u1".into(), "u2".into()])
7095 );
7096 let fork = r#"[{"url":"f","baseRefName":"main","isCrossRepository":true}]"#;
7098 assert_eq!(pick_open_pr(fork, "main").unwrap(), OpenPr::None);
7099 assert!(pick_open_pr("not json", "main").is_err());
7100 assert!(pick_open_pr(r#"[{"url":"u","title":"t"}]"#, "main").is_err());
7102 assert!(pick_open_pr(r#"[{"title":"t","baseRefName":"main"}]"#, "main").is_err());
7103 }
7104
7105 #[test]
7106 fn pick_merged_pr_picks_the_unique_match() {
7107 let json = r#"[
7108 {"url": "https://github.com/o/r/pull/42", "number": 42,
7109 "mergedAt": "2026-09-20T10:00:00Z", "baseRefName": "main"}
7110 ]"#;
7111 let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
7112 let found = pick_merged_pr(json, "main", created_at)
7113 .expect("valid json")
7114 .expect("one unambiguous match");
7115 assert_eq!(found.url, "https://github.com/o/r/pull/42");
7116 assert_eq!(found.number, 42);
7117 }
7118
7119 #[test]
7123 fn pick_merged_pr_refuses_when_more_than_one_candidate_survives() {
7124 let json = r#"[
7125 {"url": "https://github.com/o/r/pull/42", "number": 42,
7126 "mergedAt": "2026-09-20T10:00:00Z", "baseRefName": "main"},
7127 {"url": "https://github.com/o/r/pull/43", "number": 43,
7128 "mergedAt": "2026-09-21T10:00:00Z", "baseRefName": "main"}
7129 ]"#;
7130 let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
7131 assert_eq!(pick_merged_pr(json, "main", created_at).unwrap(), None);
7132 }
7133
7134 #[test]
7138 fn pick_merged_pr_ignores_a_different_base_branch() {
7139 let json = r#"[
7140 {"url": "https://github.com/o/r/pull/42", "number": 42,
7141 "mergedAt": "2026-09-20T10:00:00Z", "baseRefName": "release"}
7142 ]"#;
7143 let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
7144 assert_eq!(pick_merged_pr(json, "main", created_at).unwrap(), None);
7145 }
7146
7147 #[test]
7150 fn pick_merged_pr_ignores_a_merge_that_predates_the_run() {
7151 let json = r#"[
7152 {"url": "https://github.com/o/r/pull/42", "number": 42,
7153 "mergedAt": "2026-09-18T10:00:00Z", "baseRefName": "main"}
7154 ]"#;
7155 let created_at: Timestamp = "2026-09-19T00:00:00Z".parse().unwrap();
7156 assert_eq!(pick_merged_pr(json, "main", created_at).unwrap(), None);
7157 }
7158
7159 #[test]
7160 fn slug_of_pr_url_reads_host_owner_and_repo() {
7161 assert_eq!(
7162 slug_of_pr_url("https://github.com/yukimemi/shun/pull/272").as_deref(),
7163 Some("github.com/yukimemi/shun")
7164 );
7165 }
7166
7167 #[test]
7168 fn slug_of_pr_url_refuses_a_url_with_no_pull_segment() {
7169 assert_eq!(slug_of_pr_url("https://github.com/yukimemi/shun"), None);
7170 assert_eq!(slug_of_pr_url("not a url at all"), None);
7171 assert_eq!(slug_of_pr_url("https://github.com"), None);
7172 }
7173
7174 #[test]
7175 fn slug_of_repo_url_reads_host_owner_and_repo() {
7176 assert_eq!(
7177 slug_of_repo_url("https://github.com/yukimemi/magi").as_deref(),
7178 Some("github.com/yukimemi/magi")
7179 );
7180 assert_eq!(slug_of_repo_url("https://github.com"), None);
7181 }
7182
7183 #[test]
7184 fn ensure_same_repo_accepts_a_matching_slug_regardless_of_case() {
7185 ensure_same_repo("github.com/yukimemi/magi", "GitHub.Com/YukiMemi/Magi")
7186 .expect("same repo, different case");
7187 }
7188
7189 #[test]
7194 fn ensure_same_repo_refuses_a_different_repo() {
7195 let err =
7196 ensure_same_repo("github.com/yukimemi/magi", "github.com/yukimemi/shun").unwrap_err();
7197 let msg = format!("{err:#}");
7198 assert!(msg.contains("github.com/yukimemi/magi"), "{msg}");
7199 assert!(msg.contains("github.com/yukimemi/shun"), "{msg}");
7200 }
7201
7202 #[test]
7207 fn ensure_same_repo_refuses_the_same_slug_on_a_different_host() {
7208 let err = ensure_same_repo(
7209 "github.com/yukimemi/magi",
7210 "github.example.com/yukimemi/magi",
7211 )
7212 .unwrap_err();
7213 let msg = format!("{err:#}");
7214 assert!(msg.contains("github.com/yukimemi/magi"), "{msg}");
7215 assert!(msg.contains("github.example.com/yukimemi/magi"), "{msg}");
7216 }
7217
7218 #[tokio::test]
7223 async fn find_external_merge_returns_none_without_a_winner() {
7224 let state = RunState::new(
7225 PathBuf::from("/no/such/repo"),
7226 "main".to_owned(),
7227 "0000000000000000000000000000000000000000".to_owned(),
7228 "irrelevant".to_owned(),
7229 crate::config::Config::default(),
7230 );
7231 assert_eq!(find_external_merge(&state).await.unwrap(), None);
7232 }
7233
7234 fn pr_run(home: &Path, id: &str, repo: &str, status: RunStatus, url: &str, state: &str) {
7235 let mut run = RunState::new(
7236 PathBuf::from(repo),
7237 "main".to_owned(),
7238 "abcdef1234".to_owned(),
7239 "x".to_owned(),
7240 crate::config::Config::default(),
7241 );
7242 run.id = id.to_owned();
7243 run.status = status;
7244 run.pr = Some(crate::run::PrRecord {
7245 number: url.rsplit('/').next().unwrap().parse().unwrap(),
7246 url: url.to_owned(),
7247 state: state.to_owned(),
7248 checks: "red".to_owned(),
7249 round: 0,
7250 rounds: 2,
7251 red_at_merge: Vec::new(),
7252 });
7253 run.save_under(home).unwrap();
7254 }
7255
7256 fn recorded(home: &Path, id: &str) -> String {
7257 let body = std::fs::read_to_string(home.join("runs").join(id).join("run.json")).unwrap();
7258 serde_json::from_str::<RunState>(&body)
7259 .unwrap()
7260 .pr
7261 .unwrap()
7262 .state
7263 }
7264
7265 const PR: &str = "https://github.com/o/r/pull/7";
7266
7267 #[test]
7268 fn write_through_updates_predecessors_and_siblings_only() {
7269 let tmp = tempfile::tempdir().unwrap();
7270 let h = tmp.path();
7271 pr_run(
7272 h,
7273 "20261004-100000-aaaa",
7274 "/repo/r",
7275 RunStatus::Superseded,
7276 PR,
7277 "open",
7278 );
7279 pr_run(
7280 h,
7281 "20261004-100100-bbbb",
7282 "/repo/r",
7283 RunStatus::Blocked,
7284 PR,
7285 "open",
7286 );
7287 pr_run(
7289 h,
7290 "20261004-100200-cccc",
7291 "/repo/r",
7292 RunStatus::Landing,
7293 PR,
7294 "open",
7295 );
7296 pr_run(
7298 h,
7299 "20261004-100300-dddd",
7300 "/repo/other",
7301 RunStatus::Blocked,
7302 "https://github.com/o/other/pull/7",
7303 "open",
7304 );
7305 pr_run(
7307 h,
7308 "20261004-100400-eeee",
7309 "/repo/r",
7310 RunStatus::Blocked,
7311 "https://github.com/o/r/pull/8",
7312 "open",
7313 );
7314 pr_run(
7315 h,
7316 "20261004-100500-ffff",
7317 "/repo/r",
7318 RunStatus::Merged,
7319 PR,
7320 "open",
7321 );
7322 let source = RunState::load_under("20261004-100500-ffff", h).unwrap();
7323
7324 assert_eq!(
7325 write_pr_state_through_in(h, &source, PrLifecycle::Merged),
7326 2
7327 );
7328 assert_eq!(recorded(h, "20261004-100000-aaaa"), "merged");
7329 assert_eq!(recorded(h, "20261004-100100-bbbb"), "merged");
7330 assert_eq!(recorded(h, "20261004-100200-cccc"), "open");
7331 assert_eq!(recorded(h, "20261004-100300-dddd"), "open");
7332 assert_eq!(recorded(h, "20261004-100400-eeee"), "open");
7333 assert_eq!(recorded(h, "20261004-100500-ffff"), "open");
7335 assert_eq!(
7337 write_pr_state_through_in(h, &source, PrLifecycle::Merged),
7338 0
7339 );
7340 let hit = RunState::load_under("20261004-100000-aaaa", h).unwrap();
7341 assert!(hit.events.iter().any(|e| e.message.contains("merged")));
7342 }
7343
7344 #[test]
7345 fn repair_rewrites_merged_and_closed_and_leaves_open_and_unknown() {
7346 let tmp = tempfile::tempdir().unwrap();
7347 let h = tmp.path();
7348 let url = |n: u32| format!("https://github.com/o/r/pull/{n}");
7349 pr_run(
7350 h,
7351 "20261004-100000-aaaa",
7352 "/repo/r",
7353 RunStatus::Superseded,
7354 &url(1),
7355 "open",
7356 );
7357 pr_run(
7358 h,
7359 "20261004-100100-bbbb",
7360 "/repo/r",
7361 RunStatus::Blocked,
7362 &url(2),
7363 "open",
7364 );
7365 pr_run(
7366 h,
7367 "20261004-100200-cccc",
7368 "/repo/r",
7369 RunStatus::Ready,
7370 &url(3),
7371 "open",
7372 );
7373 pr_run(
7374 h,
7375 "20261004-100300-dddd",
7376 "/repo/r",
7377 RunStatus::Ready,
7378 &url(4),
7379 "open",
7380 );
7381 pr_run(
7382 h,
7383 "20261004-100400-eeee",
7384 "/repo/r",
7385 RunStatus::Implementing,
7386 &url(1),
7387 "open",
7388 );
7389 assert_eq!(stale_open_prs(h).len(), 4);
7390
7391 let mut known = BTreeMap::new();
7392 known.insert(url(1), PrLifecycle::Merged);
7393 known.insert(url(2), PrLifecycle::Closed);
7394 known.insert(url(3), PrLifecycle::Open);
7395 assert_eq!(apply_pr_states(h, &known), 2);
7397 assert_eq!(recorded(h, "20261004-100000-aaaa"), "merged");
7398 assert_eq!(recorded(h, "20261004-100100-bbbb"), "closed");
7399 assert_eq!(recorded(h, "20261004-100200-cccc"), "open");
7400 assert_eq!(recorded(h, "20261004-100300-dddd"), "open");
7401 assert_eq!(recorded(h, "20261004-100400-eeee"), "open");
7402 assert_eq!(apply_pr_states(h, &known), 0);
7403 }
7404
7405 use std::collections::VecDeque;
7408 use std::sync::Mutex;
7409
7410 struct Scripted {
7413 views: Mutex<VecDeque<Seen>>,
7414 merges: Mutex<VecDeque<(bool, String)>>,
7415 fix: Mutex<Option<Fixed>>,
7416 log: Mutex<Vec<&'static str>>,
7417 argvs: Mutex<Vec<Vec<String>>>,
7418 required: Mutex<Option<BTreeSet<String>>>,
7419 merged: Mutex<bool>,
7422 queued: Mutex<bool>,
7423 unreadable_after_merge: Mutex<bool>,
7425 reruns: Mutex<Vec<String>>,
7427 rerun_fails: Mutex<bool>,
7428 clock: Mutex<i64>,
7430 }
7431
7432 impl Scripted {
7433 fn new(views: Vec<Seen>, merges: Vec<(bool, &str)>) -> Self {
7434 Self {
7435 views: Mutex::new(views.into()),
7436 merges: Mutex::new(
7437 merges
7438 .into_iter()
7439 .map(|(ok, m)| (ok, m.to_owned()))
7440 .collect(),
7441 ),
7442 fix: Mutex::new(None),
7443 log: Mutex::new(Vec::new()),
7444 argvs: Mutex::new(Vec::new()),
7445 required: Mutex::new(None),
7446 merged: Mutex::new(false),
7447 queued: Mutex::new(false),
7448 unreadable_after_merge: Mutex::new(false),
7449 reruns: Mutex::new(Vec::new()),
7450 rerun_fails: Mutex::new(false),
7451 clock: Mutex::new(1_000),
7452 }
7453 }
7454 fn argvs(&self) -> Vec<Vec<String>> {
7455 self.argvs.lock().unwrap().clone()
7456 }
7457 fn calls(&self) -> Vec<&'static str> {
7458 self.log.lock().unwrap().clone()
7459 }
7460 }
7461
7462 impl Forge for Scripted {
7463 async fn view(&self, _repo: &Path, _url: &str) -> Result<Seen> {
7464 self.log.lock().unwrap().push("view");
7465 if *self.unreadable_after_merge.lock().unwrap()
7466 && !self.argvs.lock().unwrap().is_empty()
7467 {
7468 anyhow::bail!("forge unreachable");
7469 }
7470 let mut v = self.views.lock().unwrap();
7471 let mut seen = if v.len() > 1 {
7472 v.pop_front().unwrap()
7473 } else {
7474 v[0].clone()
7475 };
7476 if *self.merged.lock().unwrap() {
7477 seen.pr.state = PrLifecycle::Merged;
7478 }
7479 Ok(seen)
7480 }
7481 async fn merge(&self, _repo: &Path, argv: &[String]) -> Result<(bool, String)> {
7482 self.log.lock().unwrap().push("merge");
7483 self.argvs.lock().unwrap().push(argv.to_vec());
7484 let out = self
7485 .merges
7486 .lock()
7487 .unwrap()
7488 .pop_front()
7489 .expect("unscripted merge");
7490 if out.0 && !*self.queued.lock().unwrap() && !argv.iter().any(|a| a == "--disable-auto")
7491 {
7492 *self.merged.lock().unwrap() = true;
7493 }
7494 Ok(out)
7495 }
7496 async fn poll(&self) {
7497 self.log.lock().unwrap().push("poll");
7498 *self.clock.lock().unwrap() += POLL.as_secs() as i64;
7499 }
7500 async fn rerun(&self, _repo: &Path, run: &str) -> Result<()> {
7501 self.log.lock().unwrap().push("rerun");
7502 self.reruns.lock().unwrap().push(run.to_owned());
7503 if *self.rerun_fails.lock().unwrap() {
7504 anyhow::bail!("gh run rerun: no");
7505 }
7506 Ok(())
7507 }
7508 fn now(&self) -> i64 {
7509 *self.clock.lock().unwrap()
7510 }
7511 async fn required_contexts(&self, _repo: &Path, _base: &str) -> Option<BTreeSet<String>> {
7512 self.required.lock().unwrap().clone()
7513 }
7514 async fn fix(
7515 &self,
7516 _state: &mut RunState,
7517 _pr: &PrState,
7518 _round: usize,
7519 _budget: usize,
7520 _reason: &str,
7521 _logs: &str,
7522 ) -> Result<Fixed> {
7523 self.log.lock().unwrap().push("fix");
7524 Ok(self.fix.lock().unwrap().take().expect("unscripted fix"))
7525 }
7526 }
7527
7528 const REFUSED: &str =
7529 "X Pull request #42 is not mergeable: the base branch policy prohibits the merge.";
7530
7531 fn seen(head: &str, checks: Checks, merge_state: &str, comments: bool) -> Seen {
7532 let mut pr = green_pr();
7533 pr.checks = checks;
7534 pr.blocking = Blocking::of(merge_state);
7535 if !comments {
7536 pr.review_comments.clear();
7537 }
7538 Seen {
7539 pr,
7540 title: "feat: x".to_owned(),
7541 failing_urls: Vec::new(),
7542 head: head.to_owned(),
7543 rollup_head: head.to_owned(),
7544 merge_state: merge_state.to_owned(),
7545 contexts: Vec::new(),
7546 base: "main".to_owned(),
7547 }
7548 }
7549
7550 fn landing_state() -> RunState {
7551 crate::run::pin_test_home();
7552 let mut state = run_state();
7553 state.config.graph.land_approval = false;
7554 state
7555 }
7556
7557 #[test]
7558 fn a_pushed_head_is_awaited_case_insensitively_and_an_unreadable_one_is_not_a_match() {
7559 assert!(!awaiting_new_head(None, "aaa"));
7560 assert!(!awaiting_new_head(Some("abc123"), "ABC123"));
7561 assert!(awaiting_new_head(Some("abc123"), "def456"));
7562 assert!(awaiting_new_head(Some("abc123"), ""));
7563 }
7564
7565 #[test]
7566 fn a_refusal_is_judged_by_the_pull_requests_state_not_by_its_wording() {
7567 let open = |c, m: &str| seen("a", c, m, false);
7568 let table = [
7569 (None, false, Refused::Pending),
7570 (
7571 Some(open(Checks::Pending, "BLOCKED")),
7572 false,
7573 Refused::Pending,
7574 ),
7575 (
7576 Some(open(Checks::Unknown, "BLOCKED")),
7577 false,
7578 Refused::Pending,
7579 ),
7580 (
7581 Some(open(Checks::Green, "UNKNOWN")),
7582 false,
7583 Refused::Pending,
7584 ),
7585 (Some(open(Checks::Green, "")), false, Refused::Pending),
7586 (
7587 Some(open(Checks::Green, "BLOCKED")),
7588 false,
7589 Refused::Recheck,
7590 ),
7591 (Some(open(Checks::Green, "BLOCKED")), true, Refused::Final),
7592 ];
7593 for (after, rechecked, want) in table {
7594 assert_eq!(classify_refusal(after.as_ref(), rechecked, "a"), want);
7595 }
7596 let mut closed = open(Checks::Green, "CLEAN");
7597 closed.pr.state = PrLifecycle::Closed;
7598 assert_eq!(classify_refusal(Some(&closed), false, "a"), Refused::Final);
7599 }
7600
7601 #[tokio::test]
7602 async fn a_normal_landing_merges_on_the_first_look() {
7603 let mut state = landing_state();
7604 let forge = Scripted::new(
7605 vec![seen("a", Checks::Green, "CLEAN", false)],
7606 vec![(true, "")],
7607 );
7608 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7609 .await
7610 .unwrap();
7611 assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
7613 assert_eq!(state.status, RunStatus::Merged);
7614 }
7615
7616 #[tokio::test]
7617 async fn a_successful_merge_command_that_only_queued_is_not_a_merge() {
7618 let mut state = landing_state();
7619 let forge = Scripted::new(
7620 vec![seen("a", Checks::Green, "CLEAN", false)],
7621 std::iter::repeat_n((true, ""), 100).collect(),
7622 );
7623 *forge.queued.lock().unwrap() = true;
7624 let task = async {
7625 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7626 .await
7627 .unwrap();
7628 };
7629 task.await;
7632 assert_ne!(state.status, RunStatus::Merged);
7633 }
7634
7635 #[tokio::test]
7636 async fn an_unreadable_forge_after_a_merge_command_is_not_a_confirmation() {
7637 let mut state = landing_state();
7638 let forge = Scripted::new(
7639 vec![seen("a", Checks::Green, "CLEAN", false)],
7640 std::iter::repeat_n((true, ""), 100).collect(),
7641 );
7642 *forge.unreadable_after_merge.lock().unwrap() = true;
7643 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7644 .await
7645 .ok();
7646 assert_ne!(state.status, RunStatus::Merged);
7647 }
7648
7649 #[tokio::test]
7650 async fn a_task_public_head_is_awaited_on_land_entry() {
7651 let mut state = landing_state();
7652 state.pr_head = Some("magi/task-abcd-1234".into());
7653 state.pr_pushed_head = Some("new".into());
7654 let forge = Scripted::new(
7655 vec![
7656 seen("old", Checks::Green, "CLEAN", false),
7657 seen("new", Checks::Green, "CLEAN", false),
7658 ],
7659 vec![(true, "")],
7660 );
7661 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7662 .await
7663 .unwrap();
7664 assert_eq!(
7665 forge.calls(),
7666 ["view", "poll", "view", "view", "merge", "view"]
7667 );
7668 assert_eq!(state.status, RunStatus::Merged);
7669 }
7670
7671 #[tokio::test]
7672 async fn a_superseded_public_head_owner_cannot_reenter_land() {
7673 let mut state = landing_state();
7674 state.pr_taken_over_by = Some("next".into());
7675 let forge = Scripted::new(vec![seen("a", Checks::Green, "CLEAN", false)], vec![]);
7676 assert!(
7677 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7678 .await
7679 .is_err()
7680 );
7681 assert!(forge.calls().is_empty());
7682 }
7683
7684 #[tokio::test]
7685 async fn after_a_pushed_fix_no_merge_is_tried_until_the_head_matches() {
7686 let mut state = landing_state();
7687 let forge = Scripted::new(
7688 vec![
7689 seen("old", Checks::Green, "CLEAN", true),
7690 seen("old", Checks::Green, "CLEAN", true),
7692 seen("new", Checks::Pending, "BLOCKED", true),
7693 seen("new", Checks::Green, "CLEAN", true),
7694 ],
7695 vec![(true, "")],
7696 );
7697 *forge.fix.lock().unwrap() = Some(Fixed::Committed {
7698 head: "NEW".to_owned(),
7699 });
7700 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7701 .await
7702 .unwrap();
7703 assert_eq!(
7704 forge.calls(),
7705 [
7706 "view", "fix", "poll", "view", "poll", "view", "poll", "view", "view", "merge",
7707 "view"
7708 ]
7709 );
7710 assert_eq!(state.status, RunStatus::Merged);
7711 }
7712
7713 #[tokio::test]
7714 async fn a_head_that_never_arrives_stops_naming_both_commits() {
7715 let mut state = landing_state();
7716 let forge = Scripted::new(
7717 vec![
7718 seen("old", Checks::Green, "CLEAN", true),
7719 seen("someone-elses", Checks::Green, "CLEAN", true),
7720 ],
7721 vec![],
7722 );
7723 *forge.fix.lock().unwrap() = Some(Fixed::Committed {
7724 head: "mine".to_owned(),
7725 });
7726 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7727 .await
7728 .unwrap();
7729 assert!(!forge.calls().contains(&"merge"));
7730 let why = state.merge.as_ref().unwrap().detail.clone();
7731 assert!(
7732 why.contains("mine") && why.contains("someone-elses"),
7733 "{why}"
7734 );
7735 assert_eq!(state.status, RunStatus::Blocked);
7736 }
7737
7738 #[tokio::test]
7739 async fn a_policy_refusal_while_checks_run_waits_and_then_merges() {
7740 let mut state = landing_state();
7741 let forge = Scripted::new(
7742 vec![
7743 seen("a", Checks::Green, "CLEAN", false),
7744 seen("a", Checks::Green, "CLEAN", false),
7745 seen("a", Checks::Pending, "BLOCKED", false),
7746 seen("a", Checks::Pending, "BLOCKED", false),
7747 seen("a", Checks::Green, "CLEAN", false),
7748 ],
7749 vec![(false, REFUSED), (true, "")],
7750 );
7751 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7752 .await
7753 .unwrap();
7754 assert_eq!(
7755 forge.calls(),
7756 [
7757 "view", "view", "merge", "view", "poll", "view", "poll", "view", "view", "merge",
7758 "view"
7759 ]
7760 );
7761 assert_eq!(state.status, RunStatus::Merged);
7762 }
7763
7764 #[tokio::test]
7765 async fn a_refusal_that_outlives_settled_checks_stops_with_the_merge_state() {
7766 let mut state = landing_state();
7767 let forge = Scripted::new(
7768 vec![
7769 seen("a", Checks::Green, "CLEAN", false),
7770 seen("a", Checks::Green, "BLOCKED", false),
7771 ],
7772 vec![(false, REFUSED), (false, REFUSED)],
7773 );
7774 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7775 .await
7776 .unwrap();
7777 assert_eq!(forge.calls().iter().filter(|c| **c == "merge").count(), 2);
7779 let why = state.merge.as_ref().unwrap().detail.clone();
7780 assert!(
7781 why.contains("policy prohibits") && why.contains("BLOCKED") && why.contains("refused"),
7782 "{why}"
7783 );
7784 assert_eq!(state.status, RunStatus::Blocked);
7785 }
7786
7787 #[test]
7788 fn a_decision_is_bound_to_a_head_only_when_every_signal_agrees() {
7789 assert_eq!(bound_head("abc", "abc", None), Some("abc"));
7790 assert_eq!(bound_head("abc", "ABC", Some("abc")), Some("abc"));
7791 assert_eq!(bound_head("old", "old", Some("new")), None);
7793 assert_eq!(bound_head("new", "old", Some("new")), None);
7795 assert_eq!(bound_head("new", "old", None), None);
7796 assert_eq!(bound_head("", "", None), None);
7798 assert_eq!(bound_head("", "", Some("new")), None);
7799 assert_eq!(bound_head("abc", "", None), None);
7800 }
7801
7802 fn view_json(head: &str) -> String {
7803 format!(
7804 r#"{{"url":"https://github.com/o/r/pull/42","number":42,"state":"OPEN",
7805 "title":"t","headRefOid":"{head}","mergeStateStatus":"CLEAN",
7806 "reviews":[],"comments":[]}}"#
7807 )
7808 }
7809
7810 fn node_json(oid: &str, check: &str, has_next: bool) -> String {
7811 format!(
7812 r#"{{"data":{{"repository":{{"pullRequest":{{"commits":{{"nodes":[{{"commit":
7813 {{"oid":"{oid}","statusCheckRollup":{{"contexts":{{"pageInfo":{{"hasNextPage":{has_next}}},
7814 "nodes":[{{"__typename":"CheckRun","name":"ci","status":"COMPLETED",
7815 "conclusion":"{check}","detailsUrl":"https://example.test/1"}}]}}}}}}}}]}}}}}}}}}}"#
7816 )
7817 }
7818
7819 #[test]
7820 fn rollup_is_bound_to_the_commit_in_the_same_node() {
7821 let s = seen_from(&view_json("new"), Some(&node_json("old", "FAILURE", false))).unwrap();
7824 assert_eq!(s.rollup_head, "old");
7825 assert_eq!(s.pr.checks, Checks::Red);
7826 assert_eq!(bound_head(&s.head, &s.rollup_head, Some("new")), None);
7827 assert_eq!(s.failing_urls.len(), 1);
7828 }
7829
7830 #[test]
7831 fn checks_come_from_the_node_not_the_view() {
7832 let view = view_json("new").replace(
7833 r#""reviews""#,
7834 r#""statusCheckRollup":[{"name":"ci","status":"COMPLETED","conclusion":"FAILURE"}],"reviews""#,
7835 );
7836 let s = seen_from(&view, Some(&node_json("new", "SUCCESS", false))).unwrap();
7837 assert_eq!(s.pr.checks, Checks::Green);
7838 assert!(s.pr.failing.is_empty());
7839 assert_eq!(
7840 bound_head(&s.head, &s.rollup_head, Some("new")),
7841 Some("new")
7842 );
7843 }
7844
7845 #[test]
7846 fn an_unreadable_or_paged_node_leaves_the_head_unbound() {
7847 for node in [
7848 None,
7849 Some("not json".to_owned()),
7850 Some(r#"{"errors":[{"message":"x"}]}"#.to_owned()),
7851 Some(node_json("new", "SUCCESS", true)),
7852 ] {
7853 let s = seen_from(&view_json("new"), node.as_deref()).unwrap();
7854 assert!(s.rollup_head.is_empty());
7855 assert_eq!(s.pr.checks, Checks::Unknown);
7856 assert_eq!(bound_head(&s.head, &s.rollup_head, None), None);
7857 }
7858 }
7859
7860 #[test]
7861 fn the_merge_command_is_pinned_to_the_observed_head() {
7862 let argv = merge_argv_at(7, "feat: x", "deadbeef");
7863 let at = argv
7864 .iter()
7865 .position(|a| a == "--match-head-commit")
7866 .unwrap();
7867 assert_eq!(argv[at + 1], "deadbeef");
7868 }
7869
7870 #[tokio::test]
7871 async fn stale_checks_after_a_fix_push_never_reach_a_merge() {
7872 let mut state = landing_state();
7873 let mut stale = seen("new", Checks::Red, "CLEAN", false);
7876 stale.rollup_head = "old".to_owned();
7877 let forge = Scripted::new(
7878 vec![seen("old", Checks::Green, "CLEAN", true), stale],
7879 vec![],
7880 );
7881 *forge.fix.lock().unwrap() = Some(Fixed::Committed {
7882 head: "new".to_owned(),
7883 });
7884 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7885 .await
7886 .unwrap();
7887 assert!(!forge.calls().contains(&"merge"));
7888 assert_eq!(state.status, RunStatus::Blocked);
7889 let why = state.merge.as_ref().unwrap().detail.clone();
7890 assert!(why.contains("new") && why.contains("old"), "{why}");
7891 }
7892
7893 #[test]
7894 fn a_refusal_read_against_another_commits_checks_is_pending() {
7895 let mut after = seen("a", Checks::Green, "BLOCKED", false);
7896 after.rollup_head = "old".to_owned();
7897 assert_eq!(classify_refusal(Some(&after), true, "a"), Refused::Pending);
7898 }
7899
7900 #[tokio::test]
7901 async fn a_matching_head_with_red_non_required_checks_still_merges() {
7902 let mut state = landing_state();
7903 let forge = Scripted::new(
7904 vec![seen("a", Checks::Red, "CLEAN", false)],
7905 vec![(true, "")],
7906 );
7907 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7908 .await
7909 .unwrap();
7910 assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
7911 assert_eq!(state.status, RunStatus::Merged);
7912 }
7913
7914 #[tokio::test]
7915 async fn a_merge_refused_because_the_head_moved_looks_again_instead_of_failing() {
7916 let mut state = landing_state();
7917 let forge = Scripted::new(
7918 vec![
7919 seen("a", Checks::Green, "CLEAN", false),
7920 seen("a", Checks::Green, "CLEAN", false),
7921 seen("b", Checks::Green, "BLOCKED", false),
7923 seen("b", Checks::Green, "CLEAN", false),
7924 ],
7925 vec![(false, REFUSED), (true, "")],
7926 );
7927 land_with(&mut state, "https://github.com/o/r/pull/42", &forge)
7928 .await
7929 .unwrap();
7930 assert_eq!(
7931 forge.calls(),
7932 [
7933 "view", "view", "merge", "view", "poll", "view", "view", "merge", "view"
7934 ]
7935 );
7936 assert_eq!(state.status, RunStatus::Merged);
7937 }
7938
7939 fn merged_view(head: &str) -> Seen {
7940 let mut m = seen(head, Checks::Green, "CLEAN", false);
7941 m.pr.state = PrLifecycle::Merged;
7942 m
7943 }
7944
7945 fn has(argv: &[String], flag: &str) -> bool {
7946 argv.iter().any(|a| a == flag)
7947 }
7948
7949 fn value_of<'a>(argv: &'a [String], flag: &str) -> Option<&'a str> {
7950 let at = argv.iter().position(|a| a == flag)?;
7951 argv.get(at + 1).map(String::as_str)
7952 }
7953
7954 const URL: &str = "https://github.com/o/r/pull/42";
7955
7956 #[tokio::test]
7957 async fn the_merge_step_merges_directly_on_the_observed_head_and_never_arms() {
7958 let mut state = landing_state();
7959 let forge = Scripted::new(
7960 vec![
7961 seen("abc", Checks::Green, "CLEAN", false),
7962 seen("abc", Checks::Green, "CLEAN", false),
7963 ],
7964 vec![(true, "")],
7965 );
7966 land_with(&mut state, URL, &forge).await.unwrap();
7967 assert_eq!(forge.calls(), ["view", "view", "merge", "view"]);
7968 let argv = &forge.argvs()[0];
7969 assert!(has(argv, "--squash") && has(argv, "--subject"));
7970 assert!(!has(argv, "--auto") && !has(argv, "--admin"));
7971 assert_eq!(value_of(argv, "--match-head-commit"), Some("abc"));
7972 assert_eq!(state.status, RunStatus::Merged);
7973 assert!(state.land_armed_head.is_none());
7974 }
7975
7976 #[tokio::test]
7977 async fn a_resume_disables_an_arm_left_by_an_older_build_before_merging() {
7978 let mut state = landing_state();
7979 state.land_armed_head = Some("a".to_owned());
7980 let forge = Scripted::new(
7981 vec![seen("a", Checks::Green, "CLEAN", false)],
7982 vec![(true, ""), (true, "")],
7983 );
7984 land_with(&mut state, URL, &forge).await.unwrap();
7985 let argvs = forge.argvs();
7986 assert!(has(&argvs[0], "--disable-auto"));
7987 assert!(!has(&argvs[1], "--auto"));
7988 assert_eq!(value_of(&argvs[1], "--match-head-commit"), Some("a"));
7989 assert_eq!(state.status, RunStatus::Merged);
7990 assert!(state.land_armed_head.is_none());
7991 }
7992
7993 #[tokio::test]
7994 async fn an_approval_never_carries_over_to_a_new_head() {
7995 crate::run::pin_test_home();
7996 let mut state = run_state();
7997 state.config.graph.land_approval = true;
7998 let pr = green_pr();
7999 let store = ask::Questions::open();
8000
8001 approval_gate(&mut state, &pr, "feat: x", None, "aaa")
8002 .await
8003 .unwrap();
8004 let mut q = store
8005 .list()
8006 .into_iter()
8007 .find(|q| q.run == state.id)
8008 .unwrap();
8009 q.answer(ask::Answer::Choice(APPROVE.to_owned())).unwrap();
8010 store.put(&mut q).unwrap();
8011 assert_eq!(
8012 approval_gate(&mut state, &pr, "feat: x", None, "AAA")
8013 .await
8014 .unwrap(),
8015 ApprovalGate::Approved,
8016 "the same head keeps its approval"
8017 );
8018
8019 assert_eq!(
8021 approval_gate(&mut state, &pr, "feat: x", None, "bbb")
8022 .await
8023 .unwrap(),
8024 ApprovalGate::Pending
8025 );
8026 let all: Vec<_> = store
8027 .list()
8028 .into_iter()
8029 .filter(|q| q.run == state.id)
8030 .collect();
8031 assert_eq!(all.len(), 2);
8032
8033 state.land_approval = None;
8035 assert_eq!(
8036 approval_gate(&mut state, &pr, "feat: x", None, "bbb")
8037 .await
8038 .unwrap(),
8039 ApprovalGate::Pending
8040 );
8041 let open = store
8042 .list()
8043 .into_iter()
8044 .filter(|q| q.run == state.id && q.status.open())
8045 .count();
8046 assert_eq!(open, 1, "the superseded question was retired");
8047 }
8048
8049 #[tokio::test]
8050 async fn the_merge_is_bound_to_the_head_it_was_decided_on() {
8051 let mut state = landing_state();
8052 let forge = Scripted::new(
8053 vec![
8054 seen("a", Checks::Green, "CLEAN", false),
8055 seen("b", Checks::Green, "CLEAN", false),
8057 ],
8058 vec![(true, "")],
8059 );
8060 land_with(&mut state, URL, &forge).await.unwrap();
8061 let argvs = forge.argvs();
8062 assert_eq!(argvs.len(), 1, "no merge was tried on the moved head");
8063 assert!(!has(&argvs[0], "--auto"));
8064 assert_eq!(value_of(&argvs[0], "--match-head-commit"), Some("b"));
8065 assert_eq!(state.status, RunStatus::Merged);
8066 }
8067
8068 fn passing(label: &str) -> CheckInfo {
8069 CheckInfo {
8070 label: label.to_owned(),
8071 verdict: Verdict::Pass,
8072 required: Some(false),
8073 run: None,
8074 }
8075 }
8076
8077 fn failing(label: &str, run: Option<&str>) -> (String, Option<String>) {
8078 (label.to_owned(), run.map(str::to_owned))
8079 }
8080
8081 fn reran(head: &str, runs: &[(&str, i64)]) -> LandReruns {
8082 LandReruns {
8083 head: head.to_owned(),
8084 runs: runs.iter().map(|(r, at)| ((*r).to_owned(), *at)).collect(),
8085 }
8086 }
8087
8088 #[test]
8089 fn a_failed_run_is_rerun_once_then_waited_on_then_left_to_the_fixer() {
8090 let f = [failing("test (macos)", Some("7"))];
8091 assert_eq!(
8092 rerun_step(&f, None, "h", 100),
8093 RerunStep::Rerun(vec!["7".to_owned()])
8094 );
8095 let rec = reran("h", &[("7", 100)]);
8096 let grace = crate::release_watch::RERUN_GRACE;
8097 assert_eq!(
8098 rerun_step(&f, Some(&rec), "h", 100 + grace - 1),
8099 RerunStep::Grace
8100 );
8101 assert_eq!(rerun_step(&f, Some(&rec), "h", 100 + grace), RerunStep::Fix);
8102 }
8103
8104 #[test]
8105 fn a_failure_without_a_run_id_goes_to_the_fixer() {
8106 let f = [failing("a", Some("7")), failing("ci/external", None)];
8107 assert_eq!(rerun_step(&f, None, "h", 0), RerunStep::Fix);
8108 assert_eq!(rerun_step(&[], None, "h", 0), RerunStep::Fix);
8109 }
8110
8111 #[test]
8112 fn a_moved_head_forgets_the_reruns() {
8113 let f = [failing("a", Some("7"))];
8114 let rec = reran("old", &[("7", 100)]);
8115 assert_eq!(
8116 rerun_step(&f, Some(&rec), "new", 101),
8117 RerunStep::Rerun(vec!["7".to_owned()])
8118 );
8119 }
8120
8121 #[test]
8122 fn only_unrerun_runs_are_rerun_and_duplicates_collapse() {
8123 let f = [
8124 failing("a", Some("7")),
8125 failing("b", Some("7")),
8126 failing("c", Some("8")),
8127 ];
8128 assert_eq!(
8129 rerun_step(&f, None, "h", 0),
8130 RerunStep::Rerun(vec!["7".to_owned(), "8".to_owned()])
8131 );
8132 let rec = reran("h", &[("7", 0)]);
8133 assert_eq!(
8134 rerun_step(&f, Some(&rec), "h", 1),
8135 RerunStep::Rerun(vec!["8".to_owned()])
8136 );
8137 }
8138
8139 #[test]
8140 fn only_blocking_failures_are_candidates_for_a_rerun() {
8141 let check = |label: &str, verdict, required, run: &str| CheckInfo {
8142 label: label.to_owned(),
8143 verdict,
8144 required,
8145 run: Some(run.to_owned()),
8146 };
8147 let got = rerunnable(&[
8148 check("passed", Verdict::Pass, Some(true), "1"),
8149 check("optional", Verdict::Fail, Some(false), "2"),
8150 check("required", Verdict::Fail, Some(true), "3"),
8151 check("unknown", Verdict::Fail, None, "4"),
8152 ]);
8153 let labels: Vec<_> = got.iter().map(|(l, _)| l.as_str()).collect();
8154 assert_eq!(labels, ["required", "unknown"]);
8155 }
8156
8157 fn red(head: &str, run: Option<&str>) -> Seen {
8158 let mut s = seen(head, Checks::Red, "BLOCKED", false);
8159 s.pr.failing = vec!["test (macos)".to_owned()];
8160 s.contexts = vec![CheckInfo {
8161 label: "test (macos)".to_owned(),
8162 verdict: Verdict::Fail,
8163 required: Some(true),
8164 run: run.map(str::to_owned),
8165 }];
8166 s
8167 }
8168
8169 const RERUN_PR: &str = "https://github.com/o/r/pull/42";
8170
8171 #[tokio::test]
8172 async fn a_flaky_red_check_is_rerun_without_a_fix_round_and_then_merges() {
8173 let mut state = landing_state();
8174 let forge = Scripted::new(
8175 vec![
8176 red("a", Some("7")),
8177 seen("a", Checks::Pending, "BLOCKED", false),
8178 seen("a", Checks::Green, "CLEAN", false),
8179 ],
8180 vec![(true, "")],
8181 );
8182 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8183 assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
8184 assert!(!forge.calls().contains(&"fix"));
8185 assert_eq!(state.status, RunStatus::Merged);
8186 assert_eq!(state.pr.as_ref().unwrap().round, 0);
8187 let argvs = forge.argvs();
8188 assert_eq!(value_of(&argvs[0], "--match-head-commit"), Some("a"));
8189 assert!(
8190 state
8191 .events
8192 .iter()
8193 .any(|e| e.message.contains("test (macos)") && e.message.contains("run 7"))
8194 );
8195 }
8196
8197 #[tokio::test]
8198 async fn still_red_after_the_grace_goes_to_the_fixer_and_a_run_is_not_rerun_twice() {
8199 let mut state = landing_state();
8200 let forge = Scripted::new(
8201 vec![
8202 red("a", Some("7")),
8203 seen("a", Checks::Green, "CLEAN", false),
8204 ],
8205 vec![(true, "")],
8206 );
8207 *forge.fix.lock().unwrap() = Some(Fixed::Declined);
8208 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8210 assert_eq!(forge.reruns.lock().unwrap().len(), 1);
8211 let calls = forge.calls();
8212 let rerun_at = calls.iter().position(|c| *c == "rerun").unwrap();
8213 let fix_at = calls.iter().position(|c| *c == "fix");
8214 assert!(fix_at.is_none_or(|f| f > rerun_at));
8215 }
8216
8217 #[tokio::test]
8218 async fn a_failed_run_is_rerun_while_another_run_is_still_pending() {
8219 let mut state = landing_state();
8220 let mut v = red("a", Some("7"));
8221 v.pr.checks = Checks::Pending;
8222 v.contexts.push(CheckInfo {
8223 label: "slow".to_owned(),
8224 verdict: Verdict::Pending,
8225 required: Some(true),
8226 run: Some("8".to_owned()),
8227 });
8228 let forge = Scripted::new(
8229 vec![v, seen("a", Checks::Green, "CLEAN", false)],
8230 vec![(true, "")],
8231 );
8232 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8233 assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
8234 assert_eq!(state.status, RunStatus::Merged);
8235 }
8236
8237 #[test]
8238 fn a_run_with_a_job_still_going_is_not_rerun_yet() {
8239 let mut v = red("a", Some("7"));
8240 v.contexts.push(CheckInfo {
8241 label: "same run, still going".to_owned(),
8242 verdict: Verdict::Pending,
8243 required: Some(true),
8244 run: Some("7".to_owned()),
8245 });
8246 assert!(rerunnable(&v.contexts).is_empty());
8247 }
8248
8249 #[tokio::test]
8250 async fn the_rerun_is_made_even_when_the_fix_budget_is_spent() {
8251 let mut state = landing_state();
8252 state.config.graph.land_rounds = 0;
8253 let forge = Scripted::new(
8254 vec![
8255 red("a", Some("7")),
8256 seen("a", Checks::Green, "CLEAN", false),
8257 ],
8258 vec![(true, "")],
8259 );
8260 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8261 assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
8262 assert_eq!(state.status, RunStatus::Merged);
8263 assert!(!forge.calls().contains(&"fix"));
8264 assert_eq!(state.pr.as_ref().unwrap().round, 0);
8265 }
8266
8267 #[tokio::test]
8268 async fn the_last_fix_rounds_new_head_gets_its_rerun() {
8269 let mut state = landing_state();
8270 state.config.graph.land_rounds = 1;
8271 let forge = Scripted::new(
8272 vec![
8273 seen("a", Checks::Green, "CLEAN", true),
8274 red("b", Some("7")),
8275 seen("b", Checks::Green, "CLEAN", false),
8276 ],
8277 vec![(true, "")],
8278 );
8279 *forge.fix.lock().unwrap() = Some(Fixed::Committed {
8280 head: "b".to_owned(),
8281 });
8282 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8283 assert_eq!(*forge.reruns.lock().unwrap(), ["7"]);
8284 assert_eq!(forge.calls().iter().filter(|c| **c == "fix").count(), 1);
8285 assert_eq!(state.pr.as_ref().unwrap().round, 1);
8286 assert_eq!(state.status, RunStatus::Merged);
8287 assert_eq!(state.land_reruns.as_ref().unwrap().head, "b");
8288 assert_eq!(
8289 value_of(&forge.argvs()[0], "--match-head-commit"),
8290 Some("b")
8291 );
8292 }
8293
8294 #[tokio::test]
8295 async fn a_failed_rerun_call_falls_through_to_the_fix_in_the_same_lap() {
8296 let mut state = landing_state();
8297 let forge = Scripted::new(vec![red("a", Some("7"))], vec![]);
8298 *forge.rerun_fails.lock().unwrap() = true;
8299 *forge.fix.lock().unwrap() = Some(Fixed::Declined);
8300 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8301 assert_eq!(forge.reruns.lock().unwrap().len(), 1, "never retried");
8302 assert_eq!(
8303 forge.calls().iter().filter(|c| **c == "fix").count(),
8304 1,
8305 "{:?}",
8306 forge.calls()
8307 );
8308 }
8309
8310 #[tokio::test]
8311 async fn a_saved_rerun_record_stops_a_restart_from_rerunning() {
8312 let mut state = landing_state();
8313 let forge = Scripted::new(vec![red("a", Some("7"))], vec![]);
8314 state.land_reruns = Some(reran("a", &[("7", *forge.clock.lock().unwrap())]));
8316 *forge.fix.lock().unwrap() = Some(Fixed::Declined);
8317 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8318 assert!(forge.reruns.lock().unwrap().is_empty());
8319 }
8320
8321 #[tokio::test]
8322 async fn the_switch_off_and_a_missing_run_id_go_straight_to_the_fixer() {
8323 let mut state = landing_state();
8324 state.config.graph.rerun_flaky_checks = false;
8325 let forge = Scripted::new(vec![red("a", Some("7"))], vec![]);
8326 *forge.fix.lock().unwrap() = Some(Fixed::Declined);
8327 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8328 assert!(forge.reruns.lock().unwrap().is_empty());
8329 assert!(forge.calls().contains(&"fix"));
8330
8331 let mut state = landing_state();
8332 let forge = Scripted::new(vec![red("a", None)], vec![]);
8333 *forge.fix.lock().unwrap() = Some(Fixed::Declined);
8334 land_with(&mut state, RERUN_PR, &forge).await.unwrap();
8335 assert!(forge.reruns.lock().unwrap().is_empty());
8336 assert!(forge.calls().contains(&"fix"));
8337 }
8338
8339 fn names(xs: &[&str]) -> BTreeSet<String> {
8340 xs.iter().map(|x| (*x).to_owned()).collect()
8341 }
8342
8343 #[test]
8344 fn a_required_check_the_rollup_never_listed_is_named() {
8345 let req = names(&["build"]);
8346 let why = waiting_on("BLOCKED", &[passing("review")], Some(&req));
8347 assert!(why.contains("never reported: build"), "{why}");
8348 assert!(!why.contains("probably waiting for a review"), "{why}");
8349 }
8350
8351 #[test]
8352 fn an_unreadable_required_list_is_not_read_as_a_review_wait() {
8353 let why = waiting_on("BLOCKED", &[passing("review")], None);
8354 assert!(why.contains("could not be read"), "{why}");
8355 assert!(!why.contains("probably waiting for a review"), "{why}");
8356 }
8357
8358 #[test]
8359 fn all_required_reported_keeps_the_review_guess() {
8360 let req = names(&["build"]);
8361 let why = waiting_on("BLOCKED", &[passing("build")], Some(&req));
8362 assert!(why.contains("probably waiting for a review"), "{why}");
8363 assert!(!why.contains("never reported"), "{why}");
8364 }
8365
8366 #[test]
8367 fn required_names_match_the_rollup_ignoring_case_only() {
8368 let req = names(&["Build"]);
8369 let why = waiting_on("BLOCKED", &[passing("build")], Some(&req));
8370 assert!(!why.contains("never reported"), "{why}");
8371 }
8372
8373 #[test]
8374 fn required_contexts_are_read_from_protection_and_rulesets() {
8375 let classic = r#"{"contexts":["build"],"checks":[{"context":"lint","app_id":1}]}"#;
8376 assert_eq!(
8377 parse_classic_required(classic),
8378 Some(names(&["build", "lint"]))
8379 );
8380 let rules = r#"[{"type":"pull_request","parameters":{}},
8381 {"type":"required_status_checks","parameters":{"required_status_checks":[{"context":"test"}]}}]"#;
8382 assert_eq!(parse_ruleset_required(rules), Some(names(&["test"])));
8383 assert_eq!(parse_ruleset_required("nope"), None);
8384 assert_eq!(encode_path_segment("release/1.x"), "release%2F1.x");
8385 }
8386
8387 #[test]
8388 fn the_direct_merge_guard_needs_the_approved_head_bound_to_its_checks() {
8389 let shown = BTreeSet::new();
8390 let ok = seen("a", Checks::Green, "CLEAN", false);
8391 let guard = |s: Option<&Seen>| direct_merge_is_safe(s, "A", &shown, 0, 4, Duration::ZERO);
8392 assert!(guard(Some(&ok)));
8393 assert!(!guard(None));
8394 assert!(!guard(Some(&seen("b", Checks::Green, "CLEAN", false))));
8395 let mut stale = ok.clone();
8396 stale.rollup_head = "old".to_owned();
8397 assert!(!guard(Some(&stale)));
8398 assert!(!guard(Some(&seen("a", Checks::Pending, "BLOCKED", false))));
8399 assert!(!guard(Some(&merged_view("a"))));
8400 }
8401
8402 #[test]
8403 fn the_rollup_node_carries_whether_each_check_is_required() {
8404 let node = node_json("new", "SUCCESS", false)
8405 .replace(r#""name":"ci","#, r#""name":"ci","isRequired":true,"#);
8406 let s = seen_from(&view_json("new"), Some(&node)).unwrap();
8407 assert_eq!(s.contexts.len(), 1);
8408 assert_eq!(s.contexts[0].required, Some(true));
8409 let s = seen_from(&view_json("new"), Some(&node_json("new", "SUCCESS", false))).unwrap();
8410 assert_eq!(s.contexts[0].required, None);
8411 }
8412
8413 #[tokio::test]
8414 async fn a_resume_that_cannot_disable_a_recorded_arm_stops_and_keeps_the_record() {
8415 let mut state = landing_state();
8416 state.land_armed_head = Some("a".to_owned());
8417 let forge = Scripted::new(
8418 vec![seen("a", Checks::Green, "CLEAN", true)],
8419 vec![(false, "disable exploded")],
8420 );
8421 land_with(&mut state, URL, &forge).await.unwrap();
8422 assert!(!forge.calls().contains(&"fix"));
8423 assert_eq!(state.status, RunStatus::Blocked);
8424 assert_eq!(state.land_armed_head.as_deref(), Some("a"));
8425 let why = state.merge.as_ref().unwrap().detail.clone();
8426 assert!(why.contains("disable exploded"), "{why}");
8427 }
8428}