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 | //! A repository's settings for how its pull requests are handled, and the |
| 2 | //! rule about approvals that merging enforces. | |
| 3 | ||
| 4 | use std::collections::HashMap; | |
| 5 | ||
| 6 | use g1t_contracts::time::rfc3339; | |
| 7 | use g1t_contracts::work::*; | |
| 8 | use g1t_contracts::{FailureCode, Outcome}; | |
| 9 | use g1t_kit::now_ms; | |
| 10 | use serde::Deserialize; | |
| 11 | use worker::Result; | |
| 12 | ||
| 13 | use crate::Work; | |
| 14 | use crate::reviews::AGENT_ID; | |
| 15 | ||
| 16 | const MAX_REQUIRED_APPROVALS: u32 = 6; | |
| 17 | const MAX_REVISIONS: u32 = 5; | |
| 18 | ||
| 19 | #[derive(Deserialize)] | |
| 20 | struct SettingsRow { | |
| 21 | auto_merge: u8, | |
| 22 | require_up_to_date: u8, | |
| 23 | required_approvals: u32, | |
| 24 | count_agent_approvals: u8, | |
| 25 | allow_ignoring_checks: u8, | |
| 26 | agent_review: u8, | |
| 27 | max_revisions: u32, | |
| 28 | #[serde(default)] | |
| 29 | merge_queue: u8, | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 30 | /// JSON array of names. |
| 31 | #[serde(default)] | |
| 32 | required_checks: Option<String>, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 33 | updated_by: String, |
| 34 | updated_at: String, | |
| 35 | } | |
| 36 | ||
| 37 | impl From<SettingsRow> for RepoSettings { | |
| 38 | fn from(row: SettingsRow) -> Self { | |
| 39 | RepoSettings { | |
| 40 | auto_merge: row.auto_merge != 0, | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 41 | required_checks: row |
| 42 | .required_checks | |
| 43 | .as_deref() | |
| 44 | .and_then(|names| serde_json::from_str(names).ok()) | |
| 45 | .unwrap_or_default(), | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 46 | require_up_to_date: row.require_up_to_date != 0, |
| 47 | required_approvals: row.required_approvals, | |
| 48 | count_agent_approvals: row.count_agent_approvals != 0, | |
| 49 | allow_ignoring_checks: row.allow_ignoring_checks != 0, | |
| 50 | agent_review: row.agent_review != 0, | |
| 51 | max_revisions: row.max_revisions, | |
| 52 | merge_queue: row.merge_queue != 0, | |
| Thirteen MCP tools and classic token scopes; agents rate their confidence and can be put on an issue in one step | 53 | // Kept in its own table (confidence.rs), read beside this row. |
| 54 | hold_low_confidence: true, | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 55 | updated_by: Some(row.updated_by), |
| 56 | updated_at: Some(row.updated_at), | |
| 57 | } | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | /// What is missing before a pull request has the approvals its repository | |
| 62 | /// asks for, or `None` if nothing is. `verdicts` is each reviewer's id and | |
| 63 | /// their most recent verdict; the author's own does not count. | |
| 64 | pub(crate) fn approvals_missing( | |
| 65 | settings: &RepoSettings, | |
| 66 | author_id: &str, | |
| 67 | verdicts: &[(String, Verdict)], | |
| 68 | ) -> Option<String> { | |
| 69 | if settings.required_approvals == 0 { | |
| 70 | return None; | |
| 71 | } | |
| 72 | let others = || { | |
| 73 | verdicts | |
| 74 | .iter() | |
| 75 | .filter(|(reviewer, _)| reviewer != author_id) | |
| 76 | }; | |
| 77 | if others().any(|(_, verdict)| *verdict == Verdict::RequestChanges) { | |
| 78 | return Some("A reviewer has asked for changes.".to_owned()); | |
| 79 | } | |
| 80 | let approvals = others() | |
| 81 | .filter(|(reviewer, _)| settings.count_agent_approvals || reviewer != AGENT_ID) | |
| 82 | .count() as u32; | |
| 83 | if approvals >= settings.required_approvals { | |
| 84 | return None; | |
| 85 | } | |
| 86 | let needed = settings.required_approvals; | |
| 87 | let from = if settings.count_agent_approvals { | |
| 88 | "" | |
| 89 | } else { | |
| 90 | " from people" | |
| 91 | }; | |
| 92 | Some(format!( | |
| 93 | "This repository requires {needed} approving {}{from} before a pull request merges; this one has {approvals}.", | |
| 94 | if needed == 1 { "review" } else { "reviews" }, | |
| 95 | )) | |
| 96 | } | |
| 97 | ||
| 98 | #[derive(Deserialize)] | |
| 99 | struct VerdictRow { | |
| 100 | author_id: String, | |
| 101 | verdict: Verdict, | |
| 102 | } | |
| 103 | ||
| 104 | impl Work { | |
| 105 | /// The settings of a repository, by its id. Defaults if none were set. | |
| 106 | pub(crate) async fn settings(&self, repo_id: &str) -> Result<RepoSettings> { | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 107 | if let Some(found) = self.prefetched_repo(repo_id) { |
| 108 | let row = found.first::<SettingsRow>(crate::prefetch::Slot::Settings)?; | |
| 109 | let hold = found | |
| 110 | .first::<crate::rows::NumberRow>(crate::prefetch::Slot::Hold)? | |
| 111 | .is_none_or(|row| row.n != 0); | |
| 112 | return Ok(RepoSettings { | |
| 113 | hold_low_confidence: hold, | |
| 114 | ..row.map_or_else(RepoSettings::default, RepoSettings::from) | |
| 115 | }); | |
| 116 | } | |
| Thirteen MCP tools and classic token scopes; agents rate their confidence and can be put on an issue in one step | 117 | let row = async { |
| 118 | self.db | |
| 119 | .prepare("SELECT * FROM repo_settings WHERE repo_id = ?") | |
| 120 | .bind(&[repo_id.into()])? | |
| 121 | .first::<SettingsRow>(None) | |
| 122 | .await | |
| 123 | }; | |
| 124 | let (row, hold) = futures_util::future::try_join(row, self.holds_low_confidence(repo_id)).await?; | |
| 125 | Ok(RepoSettings { | |
| 126 | hold_low_confidence: hold, | |
| 127 | ..row.map_or_else(RepoSettings::default, RepoSettings::from) | |
| 128 | }) | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 129 | } |
| 130 | ||
| 131 | /// What is missing before a pull request has the approvals its | |
| 132 | /// repository asks for, or `None` if nothing is. | |
| 133 | pub(crate) async fn approvals_gap( | |
| 134 | &self, | |
| 135 | settings: &RepoSettings, | |
| 136 | pull: &Pull, | |
| 137 | ) -> Result<Option<String>> { | |
| 138 | if settings.required_approvals == 0 { | |
| 139 | return Ok(None); | |
| 140 | } | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 141 | let rows = match self.prefetched_pull(&pull.id) { |
| 142 | Some(found) => found.rows::<VerdictRow>(crate::prefetch::Slot::Verdicts)?, | |
| 143 | None => self | |
| 144 | .db | |
| 145 | .prepare( | |
| 146 | "SELECT author_id, verdict FROM comments | |
| 147 | WHERE repo_id = ? AND number = ? AND verdict IS NOT NULL ORDER BY id", | |
| 148 | ) | |
| 149 | .bind(&[pull.repo_id.as_str().into(), pull.number.into()])? | |
| 150 | .all() | |
| 151 | .await? | |
| 152 | .results::<VerdictRow>()?, | |
| 153 | }; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 154 | // Each reviewer's latest verdict is the one that stands. |
| 155 | let mut latest: HashMap<String, Verdict> = HashMap::new(); | |
| 156 | for row in rows { | |
| 157 | latest.insert(row.author_id, row.verdict); | |
| 158 | } | |
| 159 | let verdicts: Vec<(String, Verdict)> = latest.into_iter().collect(); | |
| 160 | Ok(approvals_missing(settings, &pull.author.id, &verdicts)) | |
| 161 | } | |
| 162 | ||
| 163 | pub(crate) async fn get_settings(&self, a: ViewArgs) -> Result<Outcome<RepoSettings>> { | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 164 | // Read beside the access check (prefetch.rs), kept only if it passes. |
| 165 | let read = |repo_id: String| async move { self.timing.db(2, self.settings(&repo_id)).await }; | |
| 166 | Ok(match self.repo_then(&a.repo, &a.viewer, read).await? { | |
| 167 | Outcome::Ok((_, settings)) => Outcome::Ok(settings), | |
| 168 | Outcome::Fail(failure) => Outcome::Fail(failure), | |
| 169 | }) | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 170 | } |
| 171 | ||
| 172 | pub(crate) async fn update_settings( | |
| 173 | &self, | |
| 174 | a: UpdateSettingsArgs, | |
| 175 | ) -> Result<Outcome<RepoSettings>> { | |
| 176 | let repo = match self.repo(&a.repo, &Some(a.actor.clone())).await? { | |
| 177 | Outcome::Ok(repo) => repo, | |
| 178 | Outcome::Fail(failure) => return Ok(Outcome::Fail(failure)), | |
| 179 | }; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 180 | if let Outcome::Fail(failure) = crate::retired::writable(&repo) { |
| 181 | return Ok(Outcome::Fail(failure)); | |
| 182 | } | |
| 183 | if !a.actor.verified { | |
| 184 | return Ok(Outcome::fail(FailureCode::Forbidden, crate::UNVERIFIED)); | |
| 185 | } | |
| 186 | if let Outcome::Fail(failure) = | |
| 187 | crate::allowed(Some(&a.actor), &repo, g1t_contracts::access::Capability::ManageSettings) | |
| 188 | { | |
| 189 | return Ok(Outcome::Fail(failure)); | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 190 | } |
| 191 | let settings = RepoSettings { | |
| 192 | required_approvals: a.settings.required_approvals.min(MAX_REQUIRED_APPROVALS), | |
| 193 | max_revisions: a.settings.max_revisions.min(MAX_REVISIONS), | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 194 | required_checks: tidy_required(&a.settings.required_checks), |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 195 | updated_by: Some(a.actor.username), |
| 196 | updated_at: Some(rfc3339(now_ms())), | |
| 197 | ..a.settings | |
| 198 | }; | |
| 199 | self.db | |
| 200 | .prepare( | |
| 201 | "INSERT INTO repo_settings | |
| 202 | (repo_id, auto_merge, require_up_to_date, required_approvals, | |
| 203 | count_agent_approvals, allow_ignoring_checks, agent_review, max_revisions, | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 204 | merge_queue, required_checks, updated_by, updated_at) |
| 205 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 206 | ON CONFLICT (repo_id) DO UPDATE SET |
| 207 | auto_merge = excluded.auto_merge, | |
| 208 | require_up_to_date = excluded.require_up_to_date, | |
| 209 | required_approvals = excluded.required_approvals, | |
| 210 | count_agent_approvals = excluded.count_agent_approvals, | |
| 211 | allow_ignoring_checks = excluded.allow_ignoring_checks, | |
| 212 | agent_review = excluded.agent_review, | |
| 213 | max_revisions = excluded.max_revisions, | |
| 214 | merge_queue = excluded.merge_queue, | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 215 | required_checks = excluded.required_checks, |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 216 | updated_by = excluded.updated_by, |
| 217 | updated_at = excluded.updated_at", | |
| 218 | ) | |
| 219 | .bind(&[ | |
| 220 | repo.id.as_str().into(), | |
| 221 | u32::from(settings.auto_merge).into(), | |
| 222 | u32::from(settings.require_up_to_date).into(), | |
| 223 | settings.required_approvals.into(), | |
| 224 | u32::from(settings.count_agent_approvals).into(), | |
| 225 | u32::from(settings.allow_ignoring_checks).into(), | |
| 226 | u32::from(settings.agent_review).into(), | |
| 227 | settings.max_revisions.into(), | |
| 228 | u32::from(settings.merge_queue).into(), | |
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 229 | serde_json::to_string(&settings.required_checks)?.into(), |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 230 | settings.updated_by.as_deref().unwrap_or_default().into(), |
| 231 | settings.updated_at.as_deref().unwrap_or_default().into(), | |
| 232 | ])? | |
| 233 | .run() | |
| 234 | .await?; | |
| Thirteen MCP tools and classic token scopes; agents rate their confidence and can be put on an issue in one step | 235 | self.set_hold_low_confidence( |
| 236 | &repo.id, | |
| 237 | settings.hold_low_confidence, | |
| 238 | settings.updated_by.as_deref().unwrap_or_default(), | |
| 239 | settings.updated_at.as_deref().unwrap_or_default(), | |
| 240 | ) | |
| 241 | .await?; | |
| Agents as a team: lifecycle, merge queue, billing and a new shell | 242 | Ok(Outcome::Ok(settings)) |
| 243 | } | |
| 244 | } | |
| 245 | ||
| 246 | #[cfg(test)] | |
| 247 | mod tests { | |
| 248 | use super::*; | |
| 249 | ||
| 250 | fn verdicts(list: &[(&str, Verdict)]) -> Vec<(String, Verdict)> { | |
| 251 | list.iter() | |
| 252 | .map(|(reviewer, verdict)| ((*reviewer).to_owned(), *verdict)) | |
| 253 | .collect() | |
| 254 | } | |
| 255 | ||
| 256 | fn requiring(approvals: u32) -> RepoSettings { | |
| 257 | RepoSettings { | |
| 258 | required_approvals: approvals, | |
| 259 | ..RepoSettings::default() | |
| 260 | } | |
| 261 | } | |
| 262 | ||
| 263 | #[test] | |
| 264 | fn nothing_is_required_by_default() { | |
| 265 | assert_eq!( | |
| 266 | approvals_missing(&RepoSettings::default(), "usr_a", &[]), | |
| 267 | None | |
| 268 | ); | |
| 269 | } | |
| 270 | ||
| 271 | #[test] | |
| 272 | fn approvals_are_counted_per_reviewer_and_not_from_the_author() { | |
| 273 | let one = requiring(1); | |
| 274 | assert!(approvals_missing(&one, "usr_a", &[]).is_some()); | |
| 275 | let own = verdicts(&[("usr_a", Verdict::Approve)]); | |
| 276 | assert!(approvals_missing(&one, "usr_a", &own).is_some()); | |
| 277 | let other = verdicts(&[("usr_b", Verdict::Approve)]); | |
| 278 | assert_eq!(approvals_missing(&one, "usr_a", &other), None); | |
| 279 | assert!(approvals_missing(&requiring(2), "usr_a", &other).is_some()); | |
| 280 | } | |
| 281 | ||
| 282 | #[test] | |
| 283 | fn a_request_for_changes_blocks_whatever_else_was_approved() { | |
| 284 | let mixed = verdicts(&[ | |
| 285 | ("usr_b", Verdict::Approve), | |
| 286 | ("usr_c", Verdict::RequestChanges), | |
| 287 | ]); | |
| 288 | assert_eq!( | |
| 289 | approvals_missing(&requiring(1), "usr_a", &mixed).as_deref(), | |
| 290 | Some("A reviewer has asked for changes.") | |
| 291 | ); | |
| 292 | } | |
| 293 | ||
| 294 | #[test] | |
| 295 | fn an_agents_approval_counts_only_where_the_repository_lets_it() { | |
| 296 | let agent = verdicts(&[(AGENT_ID, Verdict::Approve)]); | |
| 297 | assert_eq!(approvals_missing(&requiring(1), "usr_a", &agent), None); | |
| 298 | let people_only = RepoSettings { | |
| 299 | count_agent_approvals: false, | |
| 300 | ..requiring(1) | |
| 301 | }; | |
| 302 | assert!(approvals_missing(&people_only, "usr_a", &agent).is_some()); | |
| 303 | } | |
| 304 | } |