Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Agents as a team: lifecycle, merge queue, billing and a new shell | 1 | //! Seeing a pull request through. Once a g1t agent has made a change, g1t |
| 2 | //! takes each remaining step itself: the acceptance checks, a review by | |
| 3 | //! another agent, sending the author back to address what either found, | |
| 4 | //! and catching up when the branch it would land on has moved. It stops | |
| 5 | //! when the pull request is ready for a person to merge, or when it has | |
| 6 | //! tried and a person has to decide. | |
| 7 | //! | |
| 8 | //! This service decides what the next step is and claims it. The runner | |
| 9 | //! service asks, on every event that could change the answer, and carries | |
| 10 | //! the step out in a sandbox. | |
| 11 | ||
| 12 | use g1t_contracts::events::PullEvent; | |
| 13 | use g1t_contracts::repos::{GetByIdArgs, Repo, RepoPath}; | |
| 14 | use g1t_contracts::time::rfc3339; | |
| 15 | use g1t_contracts::work::*; | |
| 16 | use g1t_contracts::{FailureCode, Membership, Outcome, Role, User, Viewer}; | |
| 17 | use g1t_kit::now_ms; | |
| 18 | use std::collections::HashMap; | |
| 19 | ||
| 20 | use serde::Deserialize; | |
| 21 | use worker::Result; | |
| 22 | use worker::wasm_bindgen::JsValue; | |
| 23 | ||
| 24 | use crate::Work; | |
| 25 | use crate::reviews::{AGENT_ID, AGENT_NAME}; | |
| GitHub Actions on g1t, part two: running workflows | 26 | use crate::statuses::{self, WorkflowFacts}; |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 27 | use crate::rows::ValueRow; |
| 28 | ||
| 29 | /// How long a claimed step is waited for before it may be taken again. | |
| 30 | const REVIEW_MINUTES: u64 = 20; | |
| 31 | const REVISION_MINUTES: u64 = 60; | |
| 32 | const CATCH_UP_MINUTES: u64 = 30; | |
| 33 | const MERGE_MINUTES: u64 = 2; | |
| 34 | /// Who a merge made by a repository's settings is attributed to. Not an | |
| 35 | /// account: `g1t` cannot be registered. | |
| 36 | const POLICY_ACTOR_ID: &str = "g1t_policy"; | |
| 37 | const POLICY_ACTOR_NAME: &str = "g1t"; | |
| 38 | /// How much of a failed check's output the author is shown. | |
| 39 | const MAX_CHECK_OUTPUT_CHARS: usize = 4_000; | |
| 40 | const MANAGED_PAGE: u32 = 200; | |
| 41 | ||
| 42 | /// The part of a pull request's row that tracks its lifecycle. | |
| 43 | #[derive(Deserialize)] | |
| 44 | struct Progress { | |
| 45 | managed: u8, | |
| 46 | revisions: u32, | |
| 47 | revised_at: Option<String>, | |
| 48 | working_on: Option<String>, | |
| 49 | working_until: Option<String>, | |
| 50 | stalled: Option<String>, | |
| 51 | } | |
| 52 | ||
| 53 | /// A merge asked for while the pull request was behind, as stored. | |
| 54 | #[derive(serde::Serialize, Deserialize)] | |
| 55 | #[serde(rename_all = "camelCase")] | |
| 56 | struct LandRequest { | |
| 57 | actor: User, | |
| 58 | keep_issue_open: bool, | |
| 59 | } | |
| 60 | ||
| 61 | #[derive(Deserialize)] | |
| 62 | struct LandRow { | |
| 63 | land_requested: Option<String>, | |
| 64 | land_requested_at: Option<String>, | |
| 65 | stalled: Option<String>, | |
| 66 | } | |
| 67 | ||
| 68 | #[derive(Deserialize)] | |
| 69 | struct FinishedReview { | |
| 70 | finished_at: String, | |
| 71 | verdict: Option<Verdict>, | |
| 72 | } | |
| 73 | ||
| 74 | #[derive(Deserialize)] | |
| 75 | struct ReviewNote { | |
| 76 | body: String, | |
| 77 | path: Option<String>, | |
| 78 | line: Option<u32>, | |
| 79 | } | |
| 80 | ||
| 81 | /// What the author is being sent back to address. | |
| 82 | pub(crate) enum Feedback { | |
| 83 | FailedChecks, | |
| GitHub Actions on g1t, part two: running workflows | 84 | /// Workflows that failed on its head. |
| 85 | FailedWorkflows, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 86 | /// The review that finished at this time. |
| 87 | Review(String), | |
| 88 | /// What a person who asked for changes wrote since the last revision. | |
| 89 | Person(PersonRequest), | |
| 90 | } | |
| 91 | ||
| 92 | /// A person's request for changes that still stands: their latest verdict, | |
| 93 | /// made after the agent last revised. | |
| 94 | #[derive(Clone, Debug, Deserialize)] | |
| 95 | pub(crate) struct PersonRequest { | |
| 96 | author_id: String, | |
| 97 | author_name: String, | |
| 98 | created_at: String, | |
| 99 | } | |
| 100 | ||
| 101 | /// What should happen next, if it is g1t's turn. | |
| 102 | pub(crate) enum Next { | |
| 103 | /// A step is under way, or it is a person's turn. | |
| 104 | Wait, | |
| 105 | Review, | |
| 106 | Revise(Feedback), | |
| 107 | CatchUp, | |
| 108 | /// Land it, because the repository says ready pull requests land. | |
| 109 | Merge, | |
| 110 | } | |
| 111 | ||
| 112 | /// Whether g1t made this pull request, and so sees it through. | |
| 113 | pub(crate) fn made_by_g1t(pull: &Pull) -> bool { | |
| 114 | pull.runtime == Runtime::Hosted && pull.agent == AGENT_NAME && pull.fork.is_some() | |
| 115 | } | |
| 116 | ||
| 117 | fn at(stage: Stage, detail: impl Into<String>, revisions: u32) -> Lifecycle { | |
| 118 | Lifecycle { | |
| 119 | stage, | |
| 120 | detail: detail.into(), | |
| 121 | revisions, | |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | fn times(count: u32) -> String { | |
| 126 | match count { | |
| 127 | 1 => "once".to_owned(), | |
| 128 | 2 => "twice".to_owned(), | |
| 129 | count => format!("{count} times"), | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | /// Everything the next step depends on. | |
| 134 | struct Facts { | |
| 135 | /// Still being made: not yet marked ready for review. | |
| 136 | draft: bool, | |
| 137 | /// Why g1t stopped, if it has. | |
| 138 | stalled: Option<String>, | |
| 139 | /// The step under way, if one was claimed and is still being waited for. | |
| 140 | working_on: Option<String>, | |
| 141 | check_status: Option<CheckStatus>, | |
| 142 | /// A review someone asked for is being written. | |
| 143 | review_pending: bool, | |
| 144 | /// Whether the issue has acceptance checks at all. | |
| 145 | has_checks: bool, | |
| 146 | revisions: u32, | |
| 147 | /// The latest finished review of the change as it is now. | |
| 148 | review: Option<FinishedReview>, | |
| 149 | /// Whether the branch it would land on has moved without it. | |
| 150 | behind: bool, | |
| 151 | /// Whether the repository lands a ready pull request by itself. | |
| 152 | auto_merge: bool, | |
| 153 | /// Whether the repository refuses to merge one that is behind. | |
| 154 | require_up_to_date: bool, | |
| 155 | /// Whether a second agent reviews it without being asked. | |
| 156 | agent_review: bool, | |
| 157 | /// How many times the author may be sent back. | |
| 158 | max_revisions: u32, | |
| 159 | /// What the repository's approval rule still wants, if anything. | |
| 160 | approvals_missing: Option<String>, | |
| 161 | /// A person asked for changes since the agent last revised. | |
| 162 | person_request: Option<PersonRequest>, | |
| 163 | /// Its place in the merge queue, and what is ahead of it there. | |
| 164 | queued: Option<(QueueState, Vec<u32>)>, | |
| GitHub Actions on g1t, part two: running workflows | 165 | /// What the workflows run on its head say. |
| 166 | workflows: WorkflowFacts, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 167 | } |
| 168 | ||
| 169 | /// Where a pull request stands, and the step to take if it is g1t's turn. | |
| 170 | /// | |
| 171 | /// The order is: nothing while a step is under way; a person asking for | |
| 172 | /// changes is answered first; the checks must pass; then a review must | |
| 173 | /// approve; then it must be up to date. A person's or an agent's request | |
| 174 | /// for changes, or a failed check, sends the author back, a limited number | |
| 175 | /// of times, after which a person is asked. | |
| 176 | fn decide(facts: Facts) -> (Lifecycle, Next) { | |
| 177 | let revisions = facts.revisions; | |
| 178 | let wait = |stage, detail: &str| (at(stage, detail, revisions), Next::Wait); | |
| 179 | let exhausted = revisions >= facts.max_revisions; | |
| 180 | ||
| 181 | if facts.draft { | |
| 182 | return wait(Stage::Working, "A g1t agent is making the change."); | |
| 183 | } | |
| 184 | if let Some(reason) = &facts.stalled { | |
| 185 | return wait(Stage::NeedsYou, reason); | |
| 186 | } | |
| 187 | if let Some((state, ahead)) = &facts.queued { | |
| 188 | let named = ahead.iter().map(|n| format!("#{n}")).collect::<Vec<_>>().join(", "); | |
| 189 | let detail = match (state, ahead.is_empty()) { | |
| 190 | (QueueState::Testing, true) => "In the merge queue: being tested on the default branch as it is.".to_owned(), | |
| 191 | (QueueState::Testing, false) => { | |
| 192 | format!("In the merge queue: being tested together with {named}, ahead of it.") | |
| 193 | } | |
| 194 | (QueueState::Passed, true) => "Passed in the merge queue. Landing.".to_owned(), | |
| 195 | (QueueState::Passed, false) => { | |
| 196 | format!("Passed in the merge queue together with {named}. It lands once they have.") | |
| 197 | } | |
| 198 | _ => "In the merge queue, waiting for its turn to be tested.".to_owned(), | |
| 199 | }; | |
| 200 | return wait(Stage::Queued, &detail); | |
| 201 | } | |
| 202 | match facts.working_on.as_deref() { | |
| 203 | Some("revision") => { | |
| 204 | return wait( | |
| 205 | Stage::Revising, | |
| 206 | "The agent is addressing what the checks or the review found.", | |
| 207 | ); | |
| 208 | } | |
| 209 | Some("catch_up") => { | |
| 210 | return wait( | |
| 211 | Stage::CatchingUp, | |
| 212 | "The agent is merging in the branch this will land on, which has moved.", | |
| 213 | ); | |
| 214 | } | |
| 215 | Some("merge") => return wait(Stage::Ready, "Merging."), | |
| 216 | Some(_) => return wait(Stage::Reviewing, "A g1t agent is reviewing the change."), | |
| 217 | None => {} | |
| 218 | } | |
| 219 | if matches!( | |
| 220 | facts.check_status, | |
| 221 | Some(CheckStatus::Queued | CheckStatus::Running) | |
| 222 | ) { | |
| 223 | return wait(Stage::Checking, "The acceptance checks are running."); | |
| 224 | } | |
| 225 | if facts.review_pending { | |
| 226 | return wait(Stage::Reviewing, "A g1t agent is reviewing the change."); | |
| 227 | } | |
| 228 | // A person asked for changes: the agent makes them, as it would for a | |
| 229 | // review it asked for, before anything else. | |
| 230 | if let Some(request) = &facts.person_request { | |
| 231 | if exhausted { | |
| 232 | return wait( | |
| 233 | Stage::NeedsYou, | |
| 234 | &format!( | |
| 235 | "{} asked for changes, and the agent has already revised {}.", | |
| 236 | request.author_name, | |
| 237 | times(revisions) | |
| 238 | ), | |
| 239 | ); | |
| 240 | } | |
| 241 | return ( | |
| 242 | at( | |
| 243 | Stage::Revising, | |
| 244 | format!( | |
| 245 | "{} asked for changes. The agent is being sent back to make them.", | |
| 246 | request.author_name | |
| 247 | ), | |
| 248 | revisions, | |
| 249 | ), | |
| 250 | Next::Revise(Feedback::Person(request.clone())), | |
| 251 | ); | |
| 252 | } | |
| 253 | ||
| 254 | if facts.has_checks { | |
| 255 | match facts.check_status { | |
| 256 | Some(CheckStatus::Passed) => {} | |
| 257 | Some(CheckStatus::Failed) if exhausted => { | |
| 258 | return wait( | |
| 259 | Stage::NeedsYou, | |
| 260 | &format!( | |
| 261 | "The acceptance checks still fail after the agent revised {}.", | |
| 262 | times(revisions) | |
| 263 | ), | |
| 264 | ); | |
| 265 | } | |
| 266 | Some(CheckStatus::Failed) => { | |
| 267 | return ( | |
| 268 | at( | |
| 269 | Stage::Revising, | |
| 270 | "The acceptance checks failed. The agent is being sent back to fix them.", | |
| 271 | revisions, | |
| 272 | ), | |
| 273 | Next::Revise(Feedback::FailedChecks), | |
| 274 | ); | |
| 275 | } | |
| 276 | Some(CheckStatus::Errored) => { | |
| 277 | return wait(Stage::NeedsYou, "The acceptance checks could not be run."); | |
| 278 | } | |
| 279 | _ => { | |
| 280 | return wait( | |
| 281 | Stage::Checking, | |
| 282 | "Waiting for the acceptance checks to start.", | |
| 283 | ); | |
| 284 | } | |
| 285 | } | |
| 286 | } | |
| 287 | ||
| GitHub Actions on g1t, part two: running workflows | 288 | // Its workflows, like its checks, must pass. |
| 289 | if !facts.workflows.failed.is_empty() { | |
| 290 | let failed = statuses::list(&facts.workflows.failed); | |
| 291 | if exhausted { | |
| 292 | return wait( | |
| 293 | Stage::NeedsYou, | |
| 294 | &format!("{failed} still fails after the agent revised {}.", times(revisions)), | |
| 295 | ); | |
| 296 | } | |
| 297 | return ( | |
| 298 | at( | |
| 299 | Stage::Revising, | |
| 300 | format!("{failed} failed. The agent is being sent back to fix it."), | |
| 301 | revisions, | |
| 302 | ), | |
| 303 | Next::Revise(Feedback::FailedWorkflows), | |
| 304 | ); | |
| 305 | } | |
| 306 | if !facts.workflows.pending.is_empty() { | |
| 307 | return wait( | |
| 308 | Stage::Checking, | |
| 309 | &format!("Waiting for {} to finish.", statuses::list(&facts.workflows.pending)), | |
| 310 | ); | |
| 311 | } | |
| 312 | ||
| Agents as a team: lifecycle, merge queue, billing and a new shell | 313 | // A second agent reviews it, unless the repository leaves review to people. |
| 314 | if facts.agent_review { | |
| 315 | match facts.review { | |
| 316 | None => { | |
| 317 | return ( | |
| 318 | at( | |
| 319 | Stage::Reviewing, | |
| 320 | "A g1t agent is about to review the change.", | |
| 321 | revisions, | |
| 322 | ), | |
| 323 | Next::Review, | |
| 324 | ); | |
| 325 | } | |
| 326 | Some(FinishedReview { verdict: None, .. }) => { | |
| 327 | return wait(Stage::NeedsYou, "The review could not be completed."); | |
| 328 | } | |
| 329 | Some(FinishedReview { | |
| 330 | verdict: Some(Verdict::RequestChanges), | |
| 331 | .. | |
| 332 | }) if exhausted => { | |
| 333 | return wait( | |
| 334 | Stage::NeedsYou, | |
| 335 | &format!( | |
| 336 | "The review still asks for changes after the agent revised {}.", | |
| 337 | times(revisions) | |
| 338 | ), | |
| 339 | ); | |
| 340 | } | |
| 341 | Some(FinishedReview { | |
| 342 | verdict: Some(Verdict::RequestChanges), | |
| 343 | finished_at, | |
| 344 | }) => { | |
| 345 | return ( | |
| 346 | at( | |
| 347 | Stage::Revising, | |
| 348 | "The review asked for changes. The agent is being sent back to make them.", | |
| 349 | revisions, | |
| 350 | ), | |
| 351 | Next::Revise(Feedback::Review(finished_at)), | |
| 352 | ); | |
| 353 | } | |
| 354 | Some(FinishedReview { | |
| 355 | verdict: Some(Verdict::Approve), | |
| 356 | .. | |
| 357 | }) => {} | |
| 358 | } | |
| 359 | } | |
| 360 | ||
| 361 | // Only where the repository insists is catching up a step of its own, | |
| 362 | // followed by the checks again. Elsewhere it happens as part of merging. | |
| 363 | if facts.behind && facts.require_up_to_date { | |
| 364 | return ( | |
| 365 | at( | |
| 366 | Stage::CatchingUp, | |
| 367 | "The branch it will land on has moved. The agent is catching up.", | |
| 368 | revisions, | |
| 369 | ), | |
| 370 | Next::CatchUp, | |
| 371 | ); | |
| 372 | } | |
| 373 | // The repository wants approvals this does not have yet: people's turn, | |
| 374 | // so it is shown as needing someone, not as g1t still working. | |
| 375 | if let Some(missing) = &facts.approvals_missing { | |
| 376 | return wait(Stage::NeedsYou, missing); | |
| 377 | } | |
| 378 | if facts.auto_merge { | |
| 379 | return ( | |
| 380 | at( | |
| 381 | Stage::Ready, | |
| 382 | "Everything this repository asks for is met. Merging, as its settings say.", | |
| 383 | revisions, | |
| 384 | ), | |
| 385 | Next::Merge, | |
| 386 | ); | |
| 387 | } | |
| 388 | wait( | |
| 389 | Stage::Ready, | |
| 390 | if facts.behind { | |
| 391 | "Ready to merge. Merging brings it up to date with the default branch first." | |
| 392 | } else { | |
| 393 | "Everything this repository asks for is met. Ready to merge." | |
| 394 | }, | |
| 395 | ) | |
| 396 | } | |
| 397 | ||
| 398 | impl Work { | |
| 399 | /// Where a pull request stands and what g1t does next, remembered so | |
| 400 | /// lists can show it without working it out again. `None` for one g1t | |
| 401 | /// is not seeing through. | |
| 402 | pub(crate) async fn assess( | |
| 403 | &self, | |
| 404 | pull: &Pull, | |
| 405 | issue: &Option<Issue>, | |
| 406 | behind: bool, | |
| 407 | ) -> Result<Option<(Lifecycle, Next)>> { | |
| 408 | let assessed = self.assess_now(pull, issue, behind).await?; | |
| 409 | if let Some((lifecycle, _)) = &assessed { | |
| 410 | self.remember(&pull.id, lifecycle).await?; | |
| 411 | } | |
| 412 | Ok(assessed) | |
| 413 | } | |
| 414 | ||
| 415 | /// Saves where a pull request stands, for [`Self::remembered`]. | |
| 416 | pub(crate) async fn remember(&self, pull_id: &str, lifecycle: &Lifecycle) -> Result<()> { | |
| 417 | let stage = serde_json::to_value(lifecycle.stage)?; | |
| 418 | self.db | |
| 419 | .prepare("UPDATE pulls SET stage = ?, stage_detail = ? WHERE id = ?") | |
| 420 | .bind(&[ | |
| 421 | stage.as_str().unwrap_or_default().into(), | |
| 422 | lifecycle.detail.as_str().into(), | |
| 423 | pull_id.into(), | |
| 424 | ])? | |
| 425 | .run() | |
| 426 | .await?; | |
| 427 | Ok(()) | |
| 428 | } | |
| 429 | ||
| 430 | async fn assess_now( | |
| 431 | &self, | |
| 432 | pull: &Pull, | |
| 433 | issue: &Option<Issue>, | |
| 434 | behind: bool, | |
| 435 | ) -> Result<Option<(Lifecycle, Next)>> { | |
| 436 | if !pull.status.is_active() { | |
| 437 | return Ok(None); | |
| 438 | } | |
| 439 | let Some(progress) = self | |
| 440 | .db | |
| 441 | .prepare( | |
| 442 | "SELECT managed, revisions, revised_at, working_on, working_until, stalled | |
| 443 | FROM pulls WHERE id = ?", | |
| 444 | ) | |
| 445 | .bind(&[pull.id.as_str().into()])? | |
| 446 | .first::<Progress>(None) | |
| 447 | .await? | |
| 448 | .filter(|progress| progress.managed != 0) | |
| 449 | else { | |
| 450 | return Ok(None); | |
| 451 | }; | |
| 452 | let now = rfc3339(now_ms()); | |
| 453 | let working_on = progress | |
| 454 | .working_until | |
| 455 | .as_deref() | |
| 456 | .is_some_and(|until| until > now.as_str()) | |
| 457 | .then(|| progress.working_on.clone().unwrap_or_default()); | |
| 458 | let review = self | |
| 459 | .db | |
| 460 | .prepare( | |
| 461 | "SELECT finished_at, verdict FROM review_runs | |
| 462 | WHERE pull_id = ? AND finished_at IS NOT NULL ORDER BY id DESC LIMIT 1", | |
| 463 | ) | |
| 464 | .bind(&[pull.id.as_str().into()])? | |
| 465 | .first::<FinishedReview>(None) | |
| 466 | .await? | |
| 467 | // A review of what the change was before its last revision says | |
| 468 | // nothing about what it is now. | |
| 469 | .filter(|review| { | |
| 470 | progress | |
| 471 | .revised_at | |
| 472 | .as_deref() | |
| 473 | .is_none_or(|revised| review.finished_at.as_str() >= revised) | |
| 474 | }); | |
| 475 | let settings = self.settings(&pull.repo_id).await?; | |
| 476 | Ok(Some(decide(Facts { | |
| 477 | draft: pull.status == PullStatus::Draft, | |
| 478 | stalled: progress.stalled, | |
| 479 | working_on, | |
| 480 | check_status: pull.check_status, | |
| 481 | review_pending: self.review_pending(&pull.id).await?, | |
| 482 | has_checks: issue.as_ref().is_some_and(|issue| !issue.checks.is_empty()), | |
| 483 | revisions: progress.revisions, | |
| 484 | review, | |
| 485 | behind, | |
| 486 | auto_merge: settings.auto_merge, | |
| 487 | require_up_to_date: settings.require_up_to_date, | |
| 488 | agent_review: settings.agent_review, | |
| 489 | max_revisions: settings.max_revisions, | |
| 490 | approvals_missing: self.approvals_gap(&settings, pull).await?, | |
| 491 | person_request: self | |
| 492 | .person_request(pull, progress.revised_at.as_deref()) | |
| 493 | .await?, | |
| 494 | queued: self.queued_entry(&pull.id).await?, | |
| GitHub Actions on g1t, part two: running workflows | 495 | workflows: WorkflowFacts::of(&self.statuses(&pull.repo_id, pull.head_commit.as_deref()).await?), |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 496 | }))) |
| 497 | } | |
| 498 | ||
| 499 | /// The latest request for changes by a person other than the author, | |
| 500 | /// if it is that person's latest verdict and came after the last | |
| 501 | /// revision. | |
| 502 | async fn person_request( | |
| 503 | &self, | |
| 504 | pull: &Pull, | |
| 505 | revised_at: Option<&str>, | |
| 506 | ) -> Result<Option<PersonRequest>> { | |
| 507 | #[derive(Deserialize)] | |
| 508 | struct Verdicts { | |
| 509 | author_id: String, | |
| 510 | author_name: String, | |
| 511 | verdict: Verdict, | |
| 512 | created_at: String, | |
| 513 | } | |
| 514 | let rows = self | |
| 515 | .db | |
| 516 | .prepare( | |
| 517 | "SELECT author_id, author_name, verdict, created_at FROM comments | |
| 518 | WHERE repo_id = ? AND number = ? AND verdict IS NOT NULL | |
| 519 | AND author_id != ? AND author_id != ? | |
| 520 | ORDER BY id", | |
| 521 | ) | |
| 522 | .bind(&[ | |
| 523 | pull.repo_id.as_str().into(), | |
| 524 | pull.number.into(), | |
| 525 | pull.author.id.as_str().into(), | |
| 526 | AGENT_ID.into(), | |
| 527 | ])? | |
| 528 | .all() | |
| 529 | .await? | |
| 530 | .results::<Verdicts>()?; | |
| 531 | // Each person's latest verdict is the one that stands. | |
| 532 | let mut latest: HashMap<String, Verdicts> = HashMap::new(); | |
| 533 | for row in rows { | |
| 534 | latest.insert(row.author_id.clone(), row); | |
| 535 | } | |
| 536 | Ok(latest | |
| 537 | .into_values() | |
| 538 | .filter(|row| row.verdict == Verdict::RequestChanges) | |
| 539 | .filter(|row| revised_at.is_none_or(|revised| row.created_at.as_str() > revised)) | |
| 540 | .max_by(|a, b| a.created_at.cmp(&b.created_at)) | |
| 541 | .map(|row| PersonRequest { | |
| 542 | author_id: row.author_id, | |
| 543 | author_name: row.author_name, | |
| 544 | created_at: row.created_at, | |
| 545 | })) | |
| 546 | } | |
| 547 | ||
| 548 | /// Marks a pull request a g1t agent has just opened as one g1t sees | |
| 549 | /// through. | |
| 550 | pub(crate) async fn manage(&self, pull: &Pull) -> Result<()> { | |
| 551 | if !made_by_g1t(pull) { | |
| 552 | return Ok(()); | |
| 553 | } | |
| 554 | self.db | |
| 555 | .prepare("UPDATE pulls SET managed = 1 WHERE id = ?") | |
| 556 | .bind(&[pull.id.as_str().into()])? | |
| 557 | .run() | |
| 558 | .await?; | |
| 559 | Ok(()) | |
| 560 | } | |
| 561 | ||
| 562 | /// Takes a step for a pull request, if nobody else has. One statement, | |
| 563 | /// so that two callers cannot both take it. | |
| 564 | async fn claim(&self, pull_id: &str, step: &str, minutes: u64, revising: bool) -> Result<bool> { | |
| 565 | let now = now_ms(); | |
| 566 | let revision = if revising { | |
| 567 | ", revisions = revisions + 1, revised_at = ?1" | |
| 568 | } else { | |
| 569 | "" | |
| 570 | }; | |
| 571 | Ok(self | |
| 572 | .db | |
| 573 | .prepare(format!( | |
| 574 | "UPDATE pulls SET working_on = ?2, working_until = ?3{revision} | |
| 575 | WHERE id = ?4 AND status = 'open' AND stalled IS NULL | |
| 576 | AND (working_until IS NULL OR working_until < ?1) | |
| 577 | RETURNING id AS value" | |
| 578 | )) | |
| 579 | .bind(&[ | |
| 580 | rfc3339(now).into(), | |
| 581 | step.into(), | |
| 582 | rfc3339(now + minutes * 60 * 1000).into(), | |
| 583 | pull_id.into(), | |
| 584 | ])? | |
| 585 | .first::<ValueRow>(None) | |
| 586 | .await? | |
| 587 | .is_some()) | |
| 588 | } | |
| 589 | ||
| 590 | /// What the author is told when sent back: the checks that failed and | |
| 591 | /// what they printed, or the review and its comments on lines. | |
| 592 | async fn feedback(&self, pull: &Pull, feedback: &Feedback) -> Result<String> { | |
| 593 | match feedback { | |
| 594 | Feedback::FailedChecks => { | |
| 595 | let failed: Vec<String> = self | |
| 596 | .latest_checks(&pull.id) | |
| 597 | .await? | |
| 598 | .map(|run| run.results) | |
| 599 | .unwrap_or_default() | |
| 600 | .into_iter() | |
| 601 | .filter(|result| !result.passed) | |
| 602 | .map(|result| { | |
| 603 | let length = result.output.chars().count(); | |
| 604 | let output: String = result | |
| 605 | .output | |
| 606 | .chars() | |
| 607 | .skip(length.saturating_sub(MAX_CHECK_OUTPUT_CHARS)) | |
| 608 | .collect(); | |
| 609 | let exit = result | |
| 610 | .exit_code | |
| 611 | .map_or("it was stopped for taking too long".to_owned(), |code| { | |
| 612 | format!("exit code {code}") | |
| 613 | }); | |
| 614 | format!("`{}` failed ({exit}):\n\n{}", result.command, output.trim()) | |
| 615 | }) | |
| 616 | .collect(); | |
| 617 | Ok(format!( | |
| 618 | "These acceptance checks were run against your change in a clean sandbox and failed.\n\n{}", | |
| 619 | failed.join("\n\n") | |
| 620 | )) | |
| 621 | } | |
| GitHub Actions on g1t, part two: running workflows | 622 | Feedback::FailedWorkflows => { |
| 623 | let statuses = self.statuses(&pull.repo_id, pull.head_commit.as_deref()).await?; | |
| 624 | let failed: Vec<String> = statuses | |
| 625 | .iter() | |
| 626 | .filter(|s| s.state == "failure" || s.state == "error") | |
| 627 | .map(|s| { | |
| 628 | let run = s.target_url.as_deref().and_then(|url| url.rsplit('/').next()).unwrap_or_default(); | |
| 629 | format!( | |
| 630 | "- {} ({}): run `{run}`", | |
| 631 | s.context, | |
| 632 | s.description.as_deref().unwrap_or("failed") | |
| 633 | ) | |
| 634 | }) | |
| 635 | .collect(); | |
| 636 | Ok(format!( | |
| 637 | "These GitHub Actions workflows failed on your latest commit:\n\n{}\n\n\ | |
| 638 | Read why with the `get_workflow_run` tool (this repository, and the run's id), \ | |
| 639 | then `get_job_logs` for the job that failed. Fix the cause in the code, not the workflow, \ | |
| 640 | unless the workflow itself is wrong.", | |
| 641 | failed.join("\n") | |
| 642 | )) | |
| 643 | } | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 644 | Feedback::Review(finished_at) => { |
| 645 | // Everything a review says is recorded at the moment it finished. | |
| 646 | let notes = self | |
| 647 | .db | |
| 648 | .prepare( | |
| 649 | "SELECT body, path, line FROM comments | |
| 650 | WHERE repo_id = ? AND number = ? AND author_id = ? AND created_at = ? | |
| 651 | ORDER BY id", | |
| 652 | ) | |
| 653 | .bind(&[ | |
| 654 | pull.repo_id.as_str().into(), | |
| 655 | pull.number.into(), | |
| 656 | AGENT_ID.into(), | |
| 657 | finished_at.as_str().into(), | |
| 658 | ])? | |
| 659 | .all() | |
| 660 | .await? | |
| 661 | .results::<ReviewNote>()?; | |
| 662 | let mut on_lines = Vec::new(); | |
| 663 | let mut summary = String::new(); | |
| 664 | for note in notes { | |
| 665 | match (note.path, note.line) { | |
| 666 | (Some(path), Some(line)) => { | |
| 667 | on_lines.push(format!("- `{path}` line {line}: {}", note.body)); | |
| 668 | } | |
| 669 | (Some(path), None) => on_lines.push(format!("- `{path}`: {}", note.body)), | |
| 670 | (None, _) => summary = note.body, | |
| 671 | } | |
| 672 | } | |
| 673 | let mut text = format!( | |
| 674 | "Another agent reviewed your change and asked for changes.\n\n{summary}" | |
| 675 | ); | |
| 676 | if !on_lines.is_empty() { | |
| 677 | text.push_str("\n\nIts comments on lines:\n"); | |
| 678 | text.push_str(&on_lines.join("\n")); | |
| 679 | } | |
| 680 | Ok(text) | |
| 681 | } | |
| 682 | Feedback::Person(request) => { | |
| 683 | // What they wrote since the agent last revised, which their | |
| 684 | // request for changes closes. | |
| 685 | let revised: Option<String> = self | |
| 686 | .db | |
| 687 | .prepare("SELECT revised_at AS value FROM pulls WHERE id = ?") | |
| 688 | .bind(&[pull.id.as_str().into()])? | |
| 689 | .first::<Option<String>>(Some("value")) | |
| 690 | .await? | |
| 691 | .flatten(); | |
| 692 | let notes = self | |
| 693 | .db | |
| 694 | .prepare( | |
| 695 | "SELECT body, path, line FROM comments | |
| 696 | WHERE repo_id = ? AND number = ? AND author_id = ? | |
| 697 | AND created_at > ? AND created_at <= ? | |
| 698 | ORDER BY id", | |
| 699 | ) | |
| 700 | .bind(&[ | |
| 701 | pull.repo_id.as_str().into(), | |
| 702 | pull.number.into(), | |
| 703 | request.author_id.as_str().into(), | |
| 704 | revised.unwrap_or_default().into(), | |
| 705 | request.created_at.as_str().into(), | |
| 706 | ])? | |
| 707 | .all() | |
| 708 | .await? | |
| 709 | .results::<ReviewNote>()?; | |
| 710 | let mut on_lines = Vec::new(); | |
| 711 | let mut said = Vec::new(); | |
| 712 | for note in notes { | |
| 713 | match (note.path, note.line) { | |
| 714 | (Some(path), Some(line)) => { | |
| 715 | on_lines.push(format!("- `{path}` line {line}: {}", note.body)); | |
| 716 | } | |
| 717 | (Some(path), None) => on_lines.push(format!("- `{path}`: {}", note.body)), | |
| 718 | (None, _) => said.push(note.body), | |
| 719 | } | |
| 720 | } | |
| 721 | let mut text = format!( | |
| 722 | "{} reviewed your change and asked for changes.\n\n{}", | |
| 723 | request.author_name, | |
| 724 | said.join("\n\n") | |
| 725 | ); | |
| 726 | if !on_lines.is_empty() { | |
| 727 | text.push_str("\n\nTheir comments on lines:\n"); | |
| 728 | text.push_str(&on_lines.join("\n")); | |
| 729 | } | |
| 730 | Ok(text) | |
| 731 | } | |
| 732 | } | |
| 733 | } | |
| 734 | ||
| 735 | pub(crate) async fn advance(&self, a: AdvanceArgs) -> Result<Advance> { | |
| 736 | let Some(pull) = self.pull_by_id(&a.pull_id).await? else { | |
| 737 | return Ok(Advance::None); | |
| 738 | }; | |
| 739 | if pull.status != PullStatus::Open { | |
| 740 | return Ok(Advance::None); | |
| 741 | } | |
| Agents move along on private repositories too | 742 | // As a member: a private repository would look missing otherwise, |
| 743 | // and the pull request would never move. | |
| 744 | let viewer: Viewer = self.author_viewer(&pull).await?; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 745 | let repo: Outcome<Repo> = g1t_kit::call( |
| 746 | &self.repos, | |
| 747 | "get_by_id", | |
| 748 | &GetByIdArgs { | |
| 749 | id: pull.repo_id.clone(), | |
| 750 | viewer, | |
| 751 | }, | |
| 752 | ) | |
| 753 | .await?; | |
| 754 | let (Outcome::Ok(repo), Some(source)) = (repo, pull.fork.clone()) else { | |
| 755 | return Ok(Advance::None); | |
| 756 | }; | |
| 757 | let issue = match pull.issue { | |
| 758 | Some(number) => self.issue(&pull.repo_id, number).await?, | |
| 759 | None => None, | |
| 760 | }; | |
| 761 | let behind = self.is_behind(&repo.id, &pull).await?; | |
| 762 | let Some((lifecycle, next)) = self.assess(&pull, &issue, behind).await? else { | |
| 763 | return Ok(Advance::None); | |
| 764 | }; | |
| 765 | ||
| 766 | if matches!(next, Next::Merge) { | |
| 767 | self.merge_by_policy(&repo, &pull).await?; | |
| 768 | return Ok(Advance::None); | |
| 769 | } | |
| 770 | let (step, minutes) = match &next { | |
| 771 | Next::Wait | Next::Merge => return Ok(Advance::None), | |
| 772 | Next::Review => ("review", REVIEW_MINUTES), | |
| 773 | Next::Revise(_) => ("revision", REVISION_MINUTES), | |
| 774 | Next::CatchUp => ("catch_up", CATCH_UP_MINUTES), | |
| 775 | }; | |
| 776 | let feedback = match &next { | |
| 777 | Next::Revise(feedback) => self.feedback(&pull, feedback).await?, | |
| 778 | _ => String::new(), | |
| 779 | }; | |
| 780 | if !self | |
| 781 | .claim(&pull.id, step, minutes, matches!(next, Next::Revise(_))) | |
| 782 | .await? | |
| 783 | { | |
| 784 | return Ok(Advance::None); | |
| 785 | } | |
| 786 | // Said in the conversation, so nobody has to wonder why a review or | |
| 787 | // a new commit appeared. | |
| 788 | let told = match &next { | |
| 789 | Next::Review => { | |
| 790 | self.db | |
| 791 | .prepare( | |
| 792 | "UPDATE pulls SET reviewers = json_insert(reviewers, '$[#]', ?1) | |
| 793 | WHERE id = ?2 AND NOT EXISTS ( | |
| 794 | SELECT 1 FROM json_each(pulls.reviewers) WHERE json_each.value = ?1)", | |
| 795 | ) | |
| 796 | .bind(&[AGENT_NAME.into(), pull.id.as_str().into()])? | |
| 797 | .run() | |
| 798 | .await?; | |
| 799 | "requested a review from g1t-agent".to_owned() | |
| 800 | } | |
| 801 | Next::Revise(Feedback::FailedChecks) => { | |
| 802 | "sent g1t-agent back to fix the failed checks".to_owned() | |
| 803 | } | |
| GitHub Actions on g1t, part two: running workflows | 804 | Next::Revise(Feedback::FailedWorkflows) => { |
| 805 | "sent g1t-agent back to fix the failed workflows".to_owned() | |
| 806 | } | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 807 | Next::Revise(Feedback::Review(_)) => { |
| 808 | "sent g1t-agent back to address the review".to_owned() | |
| 809 | } | |
| 810 | _ => format!( | |
| 811 | "asked g1t-agent to bring this up to date with {}", | |
| 812 | repo.default_branch | |
| 813 | ), | |
| 814 | }; | |
| 815 | self.note( | |
| 816 | &pull.repo_id, | |
| 817 | pull.number, | |
| 818 | (POLICY_ACTOR_ID, POLICY_ACTOR_NAME), | |
| 819 | &told, | |
| 820 | ) | |
| 821 | .await?; | |
| 822 | let job = LifecycleJob { | |
| 823 | pull_id: pull.id, | |
| 824 | repo: RepoPath { | |
| 825 | namespace: repo.namespace, | |
| 826 | name: repo.name, | |
| 827 | }, | |
| 828 | number: pull.number, | |
| 829 | author: pull.author, | |
| 830 | source, | |
| 831 | branch: None, | |
| 832 | default_branch: repo.default_branch, | |
| 833 | title: pull.title, | |
| 834 | description: pull.body.unwrap_or_default(), | |
| 835 | issue, | |
| 836 | feedback, | |
| 837 | round: lifecycle.revisions + 1, | |
| 838 | }; | |
| 839 | Ok(match next { | |
| 840 | Next::Review => Advance::Review { job }, | |
| 841 | Next::Revise(_) => Advance::Revise { job }, | |
| 842 | Next::CatchUp => Advance::CatchUp { job }, | |
| 843 | Next::Wait | Next::Merge => Advance::None, | |
| 844 | }) | |
| 845 | } | |
| 846 | ||
| 847 | /// Lands a pull request that is ready, on the authority of the | |
| 848 | /// repository's settings instead of a person's click. | |
| 849 | async fn merge_by_policy(&self, repo: &Repo, pull: &Pull) -> Result<()> { | |
| 850 | if !self.claim(&pull.id, "merge", MERGE_MINUTES, false).await? { | |
| 851 | return Ok(()); | |
| 852 | } | |
| 853 | // g1t acts for the workspace whose members turned this on. | |
| 854 | let actor = User { | |
| 855 | id: POLICY_ACTOR_ID.to_owned(), | |
| 856 | username: POLICY_ACTOR_NAME.to_owned(), | |
| 857 | verified: true, | |
| 858 | workspaces: vec![Membership { | |
| 859 | slug: repo.namespace.clone(), | |
| 860 | role: Role::Member, | |
| 861 | }], | |
| 862 | ..User::default() | |
| 863 | }; | |
| 864 | let merged = self | |
| 865 | .merge_pull(PullActionArgs { | |
| 866 | actor, | |
| 867 | repo: RepoPath { | |
| 868 | namespace: repo.namespace.clone(), | |
| 869 | name: repo.name.clone(), | |
| 870 | }, | |
| 871 | number: pull.number, | |
| 872 | summary: String::new(), | |
| 873 | keep_issue_open: false, | |
| 874 | ignore_checks: false, | |
| 875 | }) | |
| 876 | .await?; | |
| 877 | match merged { | |
| 878 | Outcome::Ok(_) => Ok(()), | |
| 879 | // Most likely the branch moved in the moment between: let go, and | |
| 880 | // the next look at it will catch up and try again. | |
| 881 | Outcome::Fail(failure) if failure.code == FailureCode::Conflict => { | |
| 882 | self.db | |
| 883 | .prepare( | |
| 884 | "UPDATE pulls SET working_on = NULL, working_until = NULL | |
| 885 | WHERE id = ? AND working_on = 'merge'", | |
| 886 | ) | |
| 887 | .bind(&[pull.id.as_str().into()])? | |
| 888 | .run() | |
| 889 | .await?; | |
| 890 | Ok(()) | |
| 891 | } | |
| 892 | Outcome::Fail(failure) => { | |
| 893 | self.stall(StallArgs { | |
| 894 | pull_id: pull.id.clone(), | |
| 895 | reason: format!("g1t could not merge this: {}", failure.message), | |
| 896 | }) | |
| 897 | .await?; | |
| 898 | Ok(()) | |
| 899 | } | |
| 900 | } | |
| 901 | } | |
| 902 | ||
| 903 | /// Records that a merge was asked for while the pull request was | |
| 904 | /// behind, and announces it so that the runner brings it up to date. | |
| 905 | pub(crate) async fn request_landing( | |
| 906 | &self, | |
| 907 | pull: &Pull, | |
| 908 | actor: &User, | |
| 909 | keep_issue_open: bool, | |
| 910 | ) -> Result<()> { | |
| 911 | let now = now_ms(); | |
| 912 | let request = serde_json::to_string(&LandRequest { | |
| 913 | actor: actor.clone(), | |
| 914 | keep_issue_open, | |
| 915 | })?; | |
| 916 | self.db | |
| 917 | .prepare( | |
| 918 | "UPDATE pulls | |
| 919 | SET land_requested = ?, land_requested_at = ?, stalled = NULL, | |
| 920 | working_on = 'catch_up', working_until = ? | |
| 921 | WHERE id = ?", | |
| 922 | ) | |
| 923 | .bind(&[ | |
| 924 | request.into(), | |
| 925 | rfc3339(now).into(), | |
| 926 | rfc3339(now + CATCH_UP_MINUTES * 60 * 1000).into(), | |
| 927 | pull.id.as_str().into(), | |
| 928 | ])? | |
| 929 | .run() | |
| 930 | .await?; | |
| 931 | self.publish( | |
| 932 | "pull.merge_requested", | |
| 933 | &pull.repo_id, | |
| 934 | actor, | |
| 935 | PullEvent { | |
| 936 | pull_id: pull.id.clone(), | |
| 937 | repo_id: pull.repo_id.clone(), | |
| 938 | number: pull.number, | |
| 939 | issue: pull.issue, | |
| 940 | ..PullEvent::default() | |
| 941 | }, | |
| 942 | ) | |
| 943 | .await | |
| 944 | } | |
| 945 | ||
| 946 | /// The merge waiting on a pull request, if one was asked for recently | |
| 947 | /// enough to still stand. | |
| 948 | async fn land_request(&self, pull_id: &str) -> Result<Option<LandRequest>> { | |
| 949 | let row = self | |
| 950 | .db | |
| 951 | .prepare("SELECT land_requested, land_requested_at, stalled FROM pulls WHERE id = ?") | |
| 952 | .bind(&[pull_id.into()])? | |
| 953 | .first::<LandRow>(None) | |
| 954 | .await?; | |
| 955 | let oldest = rfc3339(now_ms().saturating_sub(CATCH_UP_MINUTES * 60 * 1000)); | |
| 956 | Ok(row | |
| 957 | .filter(|row| { | |
| 958 | row.land_requested_at | |
| 959 | .as_deref() | |
| 960 | .is_some_and(|at| at >= oldest.as_str()) | |
| 961 | }) | |
| 962 | .and_then(|row| row.land_requested) | |
| 963 | .and_then(|request| serde_json::from_str(&request).ok())) | |
| 964 | } | |
| 965 | ||
| 966 | /// Whether a merge is waiting on a pull request, and why g1t stopped | |
| 967 | /// working on it if it did. | |
| 968 | pub(crate) async fn landing_state(&self, pull_id: &str) -> Result<(bool, Option<String>)> { | |
| 969 | let stalled = self | |
| 970 | .db | |
| 971 | .prepare("SELECT land_requested, land_requested_at, stalled FROM pulls WHERE id = ?") | |
| 972 | .bind(&[pull_id.into()])? | |
| 973 | .first::<LandRow>(None) | |
| 974 | .await? | |
| 975 | .and_then(|row| row.stalled); | |
| 976 | Ok((self.land_request(pull_id).await?.is_some(), stalled)) | |
| 977 | } | |
| 978 | ||
| 979 | async fn forget_landing(&self, pull_id: &str) -> Result<()> { | |
| 980 | self.db | |
| 981 | .prepare( | |
| 982 | "UPDATE pulls SET land_requested = NULL, land_requested_at = NULL WHERE id = ?", | |
| 983 | ) | |
| 984 | .bind(&[pull_id.into()])? | |
| 985 | .run() | |
| 986 | .await?; | |
| 987 | Ok(()) | |
| 988 | } | |
| 989 | ||
| 990 | /// Lands a pull request whose head has just moved, if a merge of it was | |
| 991 | /// waiting for exactly that. What it was caught up to was already | |
| 992 | /// checked and reviewed apart from the merge, so the checks are not | |
| 993 | /// waited for again; a repository that wants them rerun turns on | |
| 994 | /// "require up to date", and then nothing is landed this way. | |
| 995 | pub(crate) async fn land_if_requested(&self, pull_id: &str) -> Result<()> { | |
| 996 | let Some(request) = self.land_request(pull_id).await? else { | |
| 997 | return Ok(()); | |
| 998 | }; | |
| 999 | self.forget_landing(pull_id).await?; | |
| 1000 | let Some(pull) = self.pull_by_id(pull_id).await? else { | |
| 1001 | return Ok(()); | |
| 1002 | }; | |
| 1003 | let repo: Outcome<Repo> = g1t_kit::call( | |
| 1004 | &self.repos, | |
| 1005 | "get_by_id", | |
| 1006 | &GetByIdArgs { | |
| 1007 | id: pull.repo_id.clone(), | |
| 1008 | viewer: Some(request.actor.clone()), | |
| 1009 | }, | |
| 1010 | ) | |
| 1011 | .await?; | |
| 1012 | let Outcome::Ok(repo) = repo else { | |
| 1013 | return Ok(()); | |
| 1014 | }; | |
| 1015 | let merged = self | |
| 1016 | .merge_pull(PullActionArgs { | |
| 1017 | actor: request.actor, | |
| 1018 | repo: RepoPath { | |
| 1019 | namespace: repo.namespace, | |
| 1020 | name: repo.name, | |
| 1021 | }, | |
| 1022 | number: pull.number, | |
| 1023 | summary: String::new(), | |
| 1024 | keep_issue_open: request.keep_issue_open, | |
| 1025 | ignore_checks: true, | |
| 1026 | }) | |
| 1027 | .await?; | |
| 1028 | if let Outcome::Fail(failure) = merged { | |
| 1029 | self.stall(StallArgs { | |
| 1030 | pull_id: pull.id, | |
| 1031 | reason: format!( | |
| 1032 | "It was brought up to date but could not be merged: {}", | |
| 1033 | failure.message | |
| 1034 | ), | |
| 1035 | }) | |
| 1036 | .await?; | |
| 1037 | } | |
| 1038 | Ok(()) | |
| 1039 | } | |
| 1040 | ||
| 1041 | /// What the runner needs to bring a pull request up to date for a merge | |
| 1042 | /// that is waiting on it. | |
| 1043 | pub(crate) async fn catch_up_job(&self, a: CatchUpJobArgs) -> Result<Option<LifecycleJob>> { | |
| 1044 | if self.land_request(&a.pull_id).await?.is_none() { | |
| 1045 | return Ok(None); | |
| 1046 | } | |
| 1047 | let Some(pull) = self.pull_by_id(&a.pull_id).await? else { | |
| 1048 | return Ok(None); | |
| 1049 | }; | |
| 1050 | // Its author can read both the repository and the pull request's source. | |
| 1051 | let repo: Outcome<Repo> = g1t_kit::call( | |
| 1052 | &self.repos, | |
| 1053 | "get_by_id", | |
| 1054 | &GetByIdArgs { | |
| 1055 | id: pull.repo_id.clone(), | |
| Agents move along on private repositories too | 1056 | viewer: self.author_viewer(&pull).await?, |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 1057 | }, |
| 1058 | ) | |
| 1059 | .await?; | |
| 1060 | let Outcome::Ok(repo) = repo else { | |
| 1061 | return Ok(None); | |
| 1062 | }; | |
| 1063 | let path = RepoPath { | |
| 1064 | namespace: repo.namespace, | |
| 1065 | name: repo.name, | |
| 1066 | }; | |
| 1067 | let issue = match pull.issue { | |
| 1068 | Some(number) => self.issue(&pull.repo_id, number).await?, | |
| 1069 | None => None, | |
| 1070 | }; | |
| 1071 | Ok(Some(LifecycleJob { | |
| 1072 | pull_id: pull.id, | |
| 1073 | source: pull.fork.unwrap_or_else(|| path.clone()), | |
| 1074 | repo: path, | |
| 1075 | number: pull.number, | |
| 1076 | author: pull.author, | |
| 1077 | branch: pull.branch, | |
| 1078 | default_branch: repo.default_branch, | |
| 1079 | title: pull.title, | |
| 1080 | description: pull.body.unwrap_or_default(), | |
| 1081 | issue, | |
| 1082 | feedback: String::new(), | |
| 1083 | round: 0, | |
| 1084 | })) | |
| 1085 | } | |
| 1086 | ||
| 1087 | pub(crate) async fn stall(&self, a: StallArgs) -> Result<bool> { | |
| 1088 | self.db | |
| 1089 | .prepare( | |
| 1090 | "UPDATE pulls | |
| 1091 | SET stalled = ?, working_on = NULL, working_until = NULL, | |
| 1092 | land_requested = NULL, land_requested_at = NULL | |
| 1093 | WHERE id = ? AND status = 'open'", | |
| 1094 | ) | |
| 1095 | .bind(&[a.reason.trim().into(), a.pull_id.as_str().into()])? | |
| 1096 | .run() | |
| 1097 | .await?; | |
| 1098 | self.db | |
| 1099 | .prepare("UPDATE pulls SET stage = 'needs_you', stage_detail = ? WHERE id = ?") | |
| 1100 | .bind(&[a.reason.trim().into(), a.pull_id.into()])? | |
| 1101 | .run() | |
| 1102 | .await?; | |
| 1103 | Ok(true) | |
| 1104 | } | |
| 1105 | ||
| 1106 | pub(crate) async fn managed_pulls(&self, a: ManagedPullsArgs) -> Result<Vec<String>> { | |
| 1107 | let rows = self | |
| 1108 | .db | |
| 1109 | .prepare( | |
| 1110 | "SELECT id AS value FROM pulls | |
| 1111 | WHERE status = 'open' AND managed = 1 AND stalled IS NULL | |
| 1112 | AND (?1 IS NULL OR repo_id = ?1) | |
| 1113 | ORDER BY updated_at DESC LIMIT ?2", | |
| 1114 | ) | |
| 1115 | .bind(&[ | |
| 1116 | a.repo_id.map_or(JsValue::NULL, JsValue::from), | |
| 1117 | MANAGED_PAGE.into(), | |
| 1118 | ])? | |
| 1119 | .all() | |
| 1120 | .await? | |
| 1121 | .results::<ValueRow>()?; | |
| 1122 | Ok(rows.into_iter().map(|row| row.value).collect()) | |
| 1123 | } | |
| 1124 | } | |
| 1125 | ||
| 1126 | #[cfg(test)] | |
| 1127 | mod tests { | |
| 1128 | use super::*; | |
| 1129 | ||
| 1130 | const MAX_REVISIONS: u32 = 2; | |
| 1131 | ||
| 1132 | #[test] | |
| 1133 | fn approvals_the_repository_wants_are_waited_for() { | |
| 1134 | let short = || Facts { | |
| 1135 | review: reviewed(Some(Verdict::Approve)), | |
| 1136 | approvals_missing: Some("This repository requires 1 approving review.".to_owned()), | |
| 1137 | ..facts() | |
| 1138 | }; | |
| 1139 | let (lifecycle, next) = decide(short()); | |
| 1140 | assert_eq!(lifecycle.stage, Stage::NeedsYou); | |
| 1141 | assert_eq!( | |
| 1142 | lifecycle.detail, | |
| 1143 | "This repository requires 1 approving review." | |
| 1144 | ); | |
| 1145 | assert!(matches!(next, Next::Wait)); | |
| 1146 | // Not even a repository that merges by itself merges without them. | |
| 1147 | let automatic = Facts { | |
| 1148 | auto_merge: true, | |
| 1149 | ..short() | |
| 1150 | }; | |
| 1151 | assert_eq!(outcome(automatic), (Stage::NeedsYou, "wait")); | |
| 1152 | } | |
| 1153 | ||
| 1154 | #[test] | |
| 1155 | fn a_repository_can_leave_review_to_people() { | |
| 1156 | let unreviewed = Facts { | |
| 1157 | agent_review: false, | |
| 1158 | ..facts() | |
| 1159 | }; | |
| 1160 | assert_eq!(outcome(unreviewed), (Stage::Ready, "wait")); | |
| 1161 | let failing = Facts { | |
| 1162 | agent_review: false, | |
| 1163 | check_status: Some(CheckStatus::Failed), | |
| 1164 | ..facts() | |
| 1165 | }; | |
| 1166 | assert_eq!(outcome(failing), (Stage::Revising, "revise for checks")); | |
| 1167 | } | |
| 1168 | ||
| 1169 | #[test] | |
| 1170 | fn a_repository_sets_how_often_the_author_is_sent_back() { | |
| 1171 | let never = Facts { | |
| 1172 | max_revisions: 0, | |
| 1173 | check_status: Some(CheckStatus::Failed), | |
| 1174 | ..facts() | |
| 1175 | }; | |
| 1176 | assert_eq!(outcome(never), (Stage::NeedsYou, "wait")); | |
| 1177 | } | |
| 1178 | ||
| 1179 | /// A pull request that is ready for review, with checks that passed | |
| 1180 | /// and nothing else yet. | |
| 1181 | fn facts() -> Facts { | |
| 1182 | Facts { | |
| 1183 | draft: false, | |
| 1184 | stalled: None, | |
| 1185 | working_on: None, | |
| 1186 | check_status: Some(CheckStatus::Passed), | |
| 1187 | review_pending: false, | |
| 1188 | has_checks: true, | |
| 1189 | revisions: 0, | |
| 1190 | review: None, | |
| 1191 | behind: false, | |
| 1192 | auto_merge: false, | |
| 1193 | require_up_to_date: false, | |
| 1194 | agent_review: true, | |
| 1195 | max_revisions: MAX_REVISIONS, | |
| 1196 | approvals_missing: None, | |
| 1197 | person_request: None, | |
| 1198 | queued: None, | |
| GitHub Actions on g1t, part two: running workflows | 1199 | workflows: WorkflowFacts::default(), |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 1200 | } |
| 1201 | } | |
| 1202 | ||
| 1203 | #[test] | |
| GitHub Actions on g1t, part two: running workflows | 1204 | fn failed_workflows_send_the_agent_back_and_running_ones_wait() { |
| 1205 | let failed = Facts { | |
| 1206 | workflows: WorkflowFacts { pending: vec![], failed: vec!["CI / pull_request".into()] }, | |
| 1207 | ..facts() | |
| 1208 | }; | |
| 1209 | let (lifecycle, next) = decide(failed); | |
| 1210 | assert!(matches!(next, Next::Revise(Feedback::FailedWorkflows))); | |
| 1211 | assert!(lifecycle.detail.contains("CI / pull_request failed")); | |
| 1212 | let running = Facts { | |
| 1213 | workflows: WorkflowFacts { pending: vec!["CI / pull_request".into()], failed: vec![] }, | |
| 1214 | ..facts() | |
| 1215 | }; | |
| 1216 | let (lifecycle, next) = decide(running); | |
| 1217 | assert!(matches!(next, Next::Wait)); | |
| 1218 | assert!(lifecycle.detail.contains("Waiting for CI / pull_request")); | |
| 1219 | } | |
| 1220 | ||
| 1221 | #[test] | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 1222 | fn a_queued_pull_request_waits_in_the_queue() { |
| 1223 | let queued = Facts { | |
| 1224 | review: reviewed(Some(Verdict::Approve)), | |
| 1225 | queued: Some((QueueState::Testing, vec![12, 14])), | |
| 1226 | auto_merge: true, | |
| 1227 | ..facts() | |
| 1228 | }; | |
| 1229 | let (lifecycle, next) = decide(queued); | |
| 1230 | assert_eq!(lifecycle.stage, Stage::Queued); | |
| 1231 | assert!(lifecycle.detail.contains("#12, #14")); | |
| 1232 | assert!(matches!(next, Next::Wait)); | |
| 1233 | } | |
| 1234 | ||
| 1235 | fn asked_by_a_person() -> Option<PersonRequest> { | |
| 1236 | Some(PersonRequest { | |
| 1237 | author_id: "usr_reviewer".to_owned(), | |
| 1238 | author_name: "g1t-reviewer".to_owned(), | |
| 1239 | created_at: "2026-10-02T11:00:00.000Z".to_owned(), | |
| 1240 | }) | |
| 1241 | } | |
| 1242 | ||
| 1243 | #[test] | |
| 1244 | fn a_person_asking_for_changes_sends_the_agent_back() { | |
| 1245 | let asked = Facts { | |
| 1246 | review: reviewed(Some(Verdict::Approve)), | |
| 1247 | approvals_missing: Some("A reviewer has asked for changes.".to_owned()), | |
| 1248 | person_request: asked_by_a_person(), | |
| 1249 | ..facts() | |
| 1250 | }; | |
| 1251 | let (lifecycle, _) = decide(Facts { | |
| 1252 | person_request: asked_by_a_person(), | |
| 1253 | ..facts() | |
| 1254 | }); | |
| 1255 | assert!(lifecycle.detail.starts_with("g1t-reviewer asked for changes")); | |
| 1256 | assert_eq!(outcome(asked), (Stage::Revising, "revise for a person")); | |
| 1257 | } | |
| 1258 | ||
| 1259 | #[test] | |
| 1260 | fn a_person_is_asked_once_the_revisions_run_out() { | |
| 1261 | let exhausted = Facts { | |
| 1262 | person_request: asked_by_a_person(), | |
| 1263 | revisions: MAX_REVISIONS, | |
| 1264 | ..facts() | |
| 1265 | }; | |
| 1266 | assert_eq!(outcome(exhausted), (Stage::NeedsYou, "wait")); | |
| 1267 | } | |
| 1268 | ||
| 1269 | fn reviewed(verdict: Option<Verdict>) -> Option<FinishedReview> { | |
| 1270 | Some(FinishedReview { | |
| 1271 | finished_at: "2026-10-02T10:00:00.000Z".to_owned(), | |
| 1272 | verdict, | |
| 1273 | }) | |
| 1274 | } | |
| 1275 | ||
| 1276 | /// The stage, and a word for the step to take. | |
| 1277 | fn outcome(facts: Facts) -> (Stage, &'static str) { | |
| 1278 | let (lifecycle, next) = decide(facts); | |
| 1279 | let step = match next { | |
| 1280 | Next::Wait => "wait", | |
| 1281 | Next::Review => "review", | |
| 1282 | Next::Revise(Feedback::FailedChecks) => "revise for checks", | |
| GitHub Actions on g1t, part two: running workflows | 1283 | Next::Revise(Feedback::FailedWorkflows) => "revise for workflows", |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 1284 | Next::Revise(Feedback::Review(_)) => "revise for review", |
| 1285 | Next::Revise(Feedback::Person(_)) => "revise for a person", | |
| 1286 | Next::CatchUp => "catch up", | |
| 1287 | Next::Merge => "merge", | |
| 1288 | }; | |
| 1289 | (lifecycle.stage, step) | |
| 1290 | } | |
| 1291 | ||
| 1292 | #[test] | |
| 1293 | fn nothing_is_started_while_the_agent_is_still_working() { | |
| 1294 | let draft = Facts { | |
| 1295 | draft: true, | |
| 1296 | check_status: None, | |
| 1297 | ..facts() | |
| 1298 | }; | |
| 1299 | assert_eq!(outcome(draft), (Stage::Working, "wait")); | |
| 1300 | } | |
| 1301 | ||
| 1302 | #[test] | |
| 1303 | fn checks_come_before_review() { | |
| 1304 | let unchecked = Facts { | |
| 1305 | check_status: None, | |
| 1306 | ..facts() | |
| 1307 | }; | |
| 1308 | assert_eq!(outcome(unchecked), (Stage::Checking, "wait")); | |
| 1309 | let running = Facts { | |
| 1310 | check_status: Some(CheckStatus::Running), | |
| 1311 | ..facts() | |
| 1312 | }; | |
| 1313 | assert_eq!(outcome(running), (Stage::Checking, "wait")); | |
| 1314 | assert_eq!(outcome(facts()), (Stage::Reviewing, "review")); | |
| 1315 | } | |
| 1316 | ||
| 1317 | #[test] | |
| 1318 | fn an_issue_without_checks_goes_straight_to_review() { | |
| 1319 | let unchecked = Facts { | |
| 1320 | has_checks: false, | |
| 1321 | check_status: None, | |
| 1322 | ..facts() | |
| 1323 | }; | |
| 1324 | assert_eq!(outcome(unchecked), (Stage::Reviewing, "review")); | |
| 1325 | } | |
| 1326 | ||
| 1327 | #[test] | |
| 1328 | fn failed_checks_send_the_author_back() { | |
| 1329 | let failed = Facts { | |
| 1330 | check_status: Some(CheckStatus::Failed), | |
| 1331 | ..facts() | |
| 1332 | }; | |
| 1333 | assert_eq!(outcome(failed), (Stage::Revising, "revise for checks")); | |
| 1334 | } | |
| 1335 | ||
| 1336 | #[test] | |
| 1337 | fn checks_that_could_not_run_are_a_persons_problem() { | |
| 1338 | let errored = Facts { | |
| 1339 | check_status: Some(CheckStatus::Errored), | |
| 1340 | ..facts() | |
| 1341 | }; | |
| 1342 | assert_eq!(outcome(errored), (Stage::NeedsYou, "wait")); | |
| 1343 | } | |
| 1344 | ||
| 1345 | #[test] | |
| 1346 | fn a_review_asking_for_changes_sends_the_author_back() { | |
| 1347 | let changes = Facts { | |
| 1348 | review: reviewed(Some(Verdict::RequestChanges)), | |
| 1349 | ..facts() | |
| 1350 | }; | |
| 1351 | assert_eq!(outcome(changes), (Stage::Revising, "revise for review")); | |
| 1352 | } | |
| 1353 | ||
| 1354 | #[test] | |
| 1355 | fn the_author_is_sent_back_only_so_many_times() { | |
| 1356 | let failing = Facts { | |
| 1357 | check_status: Some(CheckStatus::Failed), | |
| 1358 | revisions: MAX_REVISIONS, | |
| 1359 | ..facts() | |
| 1360 | }; | |
| 1361 | assert_eq!(outcome(failing), (Stage::NeedsYou, "wait")); | |
| 1362 | let unconvinced = Facts { | |
| 1363 | review: reviewed(Some(Verdict::RequestChanges)), | |
| 1364 | revisions: MAX_REVISIONS, | |
| 1365 | ..facts() | |
| 1366 | }; | |
| 1367 | assert_eq!(outcome(unconvinced), (Stage::NeedsYou, "wait")); | |
| 1368 | // One short of the limit still gets another go. | |
| 1369 | let once = Facts { | |
| 1370 | check_status: Some(CheckStatus::Failed), | |
| 1371 | revisions: MAX_REVISIONS - 1, | |
| 1372 | ..facts() | |
| 1373 | }; | |
| 1374 | assert_eq!(outcome(once), (Stage::Revising, "revise for checks")); | |
| 1375 | } | |
| 1376 | ||
| 1377 | #[test] | |
| 1378 | fn a_review_that_could_not_be_written_is_not_retried() { | |
| 1379 | let broken = Facts { | |
| 1380 | review: reviewed(None), | |
| 1381 | ..facts() | |
| 1382 | }; | |
| 1383 | assert_eq!(outcome(broken), (Stage::NeedsYou, "wait")); | |
| 1384 | } | |
| 1385 | ||
| 1386 | #[test] | |
| 1387 | fn being_behind_only_holds_a_change_up_where_the_repository_says_so() { | |
| 1388 | let behind = || Facts { | |
| 1389 | review: reviewed(Some(Verdict::Approve)), | |
| 1390 | behind: true, | |
| 1391 | ..facts() | |
| 1392 | }; | |
| 1393 | // By default it is ready as it is; merging brings it up to date. | |
| 1394 | assert_eq!(outcome(behind()), (Stage::Ready, "wait")); | |
| 1395 | let strict = Facts { | |
| 1396 | require_up_to_date: true, | |
| 1397 | ..behind() | |
| 1398 | }; | |
| 1399 | assert_eq!(outcome(strict), (Stage::CatchingUp, "catch up")); | |
| 1400 | let current = Facts { | |
| 1401 | review: reviewed(Some(Verdict::Approve)), | |
| 1402 | ..facts() | |
| 1403 | }; | |
| 1404 | assert_eq!(outcome(current), (Stage::Ready, "wait")); | |
| 1405 | } | |
| 1406 | ||
| 1407 | #[test] | |
| 1408 | fn a_ready_change_lands_by_itself_only_where_the_repository_says_so() { | |
| 1409 | let ready = || Facts { | |
| 1410 | review: reviewed(Some(Verdict::Approve)), | |
| 1411 | ..facts() | |
| 1412 | }; | |
| 1413 | assert_eq!(outcome(ready()), (Stage::Ready, "wait")); | |
| 1414 | let automatic = Facts { | |
| 1415 | auto_merge: true, | |
| 1416 | ..ready() | |
| 1417 | }; | |
| 1418 | assert_eq!(outcome(automatic), (Stage::Ready, "merge")); | |
| 1419 | // One that is behind is merged too: merging brings it up to date. | |
| 1420 | let behind = Facts { | |
| 1421 | auto_merge: true, | |
| 1422 | behind: true, | |
| 1423 | ..ready() | |
| 1424 | }; | |
| 1425 | assert_eq!(outcome(behind), (Stage::Ready, "merge")); | |
| 1426 | // Unless the repository wants it caught up and checked again first. | |
| 1427 | let strict = Facts { | |
| 1428 | auto_merge: true, | |
| 1429 | behind: true, | |
| 1430 | require_up_to_date: true, | |
| 1431 | ..ready() | |
| 1432 | }; | |
| 1433 | assert_eq!(outcome(strict), (Stage::CatchingUp, "catch up")); | |
| 1434 | // Nothing short of approved is merged, whatever the setting. | |
| 1435 | let failing = Facts { | |
| 1436 | auto_merge: true, | |
| 1437 | check_status: Some(CheckStatus::Failed), | |
| 1438 | ..ready() | |
| 1439 | }; | |
| 1440 | assert_eq!(outcome(failing), (Stage::Revising, "revise for checks")); | |
| 1441 | let unreviewed = Facts { | |
| 1442 | auto_merge: true, | |
| 1443 | ..facts() | |
| 1444 | }; | |
| 1445 | assert_eq!(outcome(unreviewed), (Stage::Reviewing, "review")); | |
| 1446 | } | |
| 1447 | ||
| 1448 | #[test] | |
| 1449 | fn catching_up_reruns_the_checks_but_not_the_review() { | |
| 1450 | // The merge moved the head, so the checks are waited for again. | |
| 1451 | let merged_in = Facts { | |
| 1452 | review: reviewed(Some(Verdict::Approve)), | |
| 1453 | check_status: None, | |
| 1454 | ..facts() | |
| 1455 | }; | |
| 1456 | assert_eq!(outcome(merged_in), (Stage::Checking, "wait")); | |
| 1457 | } | |
| 1458 | ||
| 1459 | #[test] | |
| 1460 | fn a_step_under_way_is_not_started_again() { | |
| 1461 | for (step, stage) in [ | |
| 1462 | ("review", Stage::Reviewing), | |
| 1463 | ("revision", Stage::Revising), | |
| 1464 | ("catch_up", Stage::CatchingUp), | |
| 1465 | ] { | |
| 1466 | let busy = Facts { | |
| 1467 | working_on: Some(step.to_owned()), | |
| 1468 | // Whatever else is true, the step in hand comes first. | |
| 1469 | check_status: Some(CheckStatus::Failed), | |
| 1470 | ..facts() | |
| 1471 | }; | |
| 1472 | assert_eq!(outcome(busy), (stage, "wait")); | |
| 1473 | } | |
| 1474 | let asked = Facts { | |
| 1475 | review_pending: true, | |
| 1476 | ..facts() | |
| 1477 | }; | |
| 1478 | assert_eq!(outcome(asked), (Stage::Reviewing, "wait")); | |
| 1479 | } | |
| 1480 | ||
| 1481 | #[test] | |
| 1482 | fn once_stopped_it_stays_stopped() { | |
| 1483 | let (lifecycle, next) = decide(Facts { | |
| 1484 | stalled: Some("The agent could not catch up.".to_owned()), | |
| 1485 | review: reviewed(Some(Verdict::Approve)), | |
| 1486 | behind: true, | |
| 1487 | ..facts() | |
| 1488 | }); | |
| 1489 | assert_eq!(lifecycle.stage, Stage::NeedsYou); | |
| 1490 | assert_eq!(lifecycle.detail, "The agent could not catch up."); | |
| 1491 | assert!(matches!(next, Next::Wait)); | |
| 1492 | } | |
| 1493 | } |