Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/incidents/kr_carried_approval_keeps_its_review.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# kr. `kr_carried_approval_keeps_its_review`

**What happened:** CoderHelm approved a pull request on a web app after a full review. A week later a person merged the base branch into it. CoderHelm saw that the PR's own change was the one it had approved, so it did not review again. Instead it posted a new approval on the new head whose whole body was one line: "Approved: the new commits only bring in the base branch; the PR's own change is the same as the approved `dab6dae`." plus the rating footer. Posting it rewrote CoderHelm's earlier reviews to "Outdated: see CoderHelm's latest review", and the tidy between reviews cuts a review of an older commit to one line, so the full review the approval rested on was gone from sight. The pull request's only visible approval had no review in it. A person said: "if this happens we have no review to check".

**The guarantee:** A carried approval stands on its own. Its body gives the verdict and why the earlier review still holds for the new head ("the PR's own change is unchanged since `<sha>`; the new commits only bring in `<base>`"), then that review as it was posted: what was checked, the criteria, the verification, the tests, the notes and findings. The content comes from the stored record of the approved commit's review (the posted body is now kept on every review record); the model is not run again. A carry of a carry keeps the one review and names the commit it was written for, with no nesting. While a carried approval is CoderHelm's latest review, the review it rests on is never rewritten, cut to one line or hidden, its comments stay in sight and its threads stay open; an approval posted the old one-line way is read the same way. A later real review, a merge or a close cuts both as before. With no stored review of the approved commit to carry, or only an old one-line carry with no review in it, the pull request is reviewed in full instead of approved with nothing in sight.

**Where it's enforced:** `worker::passes::review_pr` (`carried_review`, `carried_approval_body`, `carry_approval`, the `posted_body` on `store_review_record`, and the full review when nothing can be carried), `worker::passes::review_sweep::ReviewRecord::posted_body`, and `common::review_supersede` (`carried_from`, `live_carried_sha`, `kept_for_carry`, used by `plan` and `tidy`).
79 changes: 77 additions & 2 deletions services/common/src/review_supersede.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ pub fn finding_key(path: &str, body: &str) -> String {
/// The plan once review `new_id` is posted, raising the findings `raised`
/// ([`finding_key`] of each of its inline comments).
pub fn plan(reviews: &[Review], threads: &[Thread], new_id: u64, raised: &[String]) -> Plan {
// A carried approval rests on the review it carries: that one, its
// comments and its threads stay as they are.
let carried = reviews
.iter()
.find(|r| r.id == new_id)
.and_then(|r| carried_from(&r.body));
let earlier: Vec<&Review> = reviews
.iter()
.filter(|r| r.own && r.id != new_id && r.state != "PENDING")
.filter(|r| !rests_on(r, carried.as_deref()))
.collect();
let earlier_ids: Vec<u64> = earlier.iter().map(|r| r.id).collect();
let mut out = Plan {
Expand All @@ -145,7 +152,12 @@ pub fn plan(reviews: &[Review], threads: &[Thread], new_id: u64, raised: &[Strin
out.minimize.push(c.node_id.clone());
}
}
if !t.resolved && !raised.contains(&finding_key(&t.path, &t.first_body)) {
let kept = t.comments.first().is_some_and(|c| {
reviews
.iter()
.any(|r| r.id == c.review_id && rests_on(r, carried.as_deref()))
});
if !kept && !t.resolved && !raised.contains(&finding_key(&t.path, &t.first_body)) {
out.resolve.push(t.id.clone());
}
}
Expand Down Expand Up @@ -263,6 +275,62 @@ fn is_head(sha: &str, head: &str) -> bool {
sha.len() >= 7 && head.len() >= 7 && (head.starts_with(sha) || sha.starts_with(head))
}

// ── Carried approvals ────────────────────────────────────────────────────
//
// A push that only brings in the base branch is not reviewed again: the
// approval of the earlier commit is posted again on the new head. That
// review's verdict rests on the earlier one, so the earlier one is never cut
// or hidden while the carried approval is CoderHelm's latest word, and the
// carried approval itself carries the earlier review's content.

/// The mark a carried approval starts with, followed by the commit whose
/// review it carries and ` -->`.
pub const CARRIES_MARK: &str = "<!-- coderhelm:carries ";

/// The mark before the carried review's own content in a carried approval,
/// followed by the commit that review was written for and ` -->`.
pub const CARRIED_REVIEW_MARK: &str = "<!-- coderhelm:carried-review ";

/// The commit a carried approval's verdict rests on, when `body` is one:
/// its [`CARRIES_MARK`], or the one-line wording carried approvals had
/// before they carried the review ("the same as the approved `abc1234`").
pub fn carried_from(body: &str) -> Option<String> {
let sha_after = |marker: &str, end: char| -> Option<String> {
let at = body.find(marker)?;
let rest = &body[at + marker.len()..];
let sha = rest[..rest.find(end)?].trim();
(sha.len() >= 7 && sha.chars().all(|c| c.is_ascii_hexdigit())).then(|| sha.to_string())
};
sha_after(CARRIES_MARK, '-').or_else(|| sha_after("the same as the approved `", '`'))
}

/// The commit CoderHelm's latest review on the pull request carries its
/// verdict from, when that review is a carried approval: the review of that
/// commit is the one the standing verdict rests on.
pub fn live_carried_sha(reviews: &[Review]) -> Option<String> {
reviews
.iter()
.filter(|r| r.own && r.state != "PENDING" && !r.body.trim().is_empty())
.max_by_key(|r| r.id)
.and_then(|r| carried_from(&r.body))
}

/// The review a carried approval rests on: CoderHelm's own, for the commit
/// `carried` names.
fn rests_on(r: &Review, carried: Option<&str>) -> bool {
carried.is_some_and(|c| is_head(c, &reviewed_sha(r)))
}

/// The ids of the reviews the latest carried approval rests on.
pub fn kept_for_carry(reviews: &[Review]) -> Vec<u64> {
let live = live_carried_sha(reviews);
reviews
.iter()
.filter(|r| r.own && rests_on(r, live.as_deref()))
.map(|r| r.id)
.collect()
}

/// The verdict a review gave, in two words and an icon.
pub fn verdict_word(r: &Review) -> &'static str {
let b = r.body.as_str();
Expand Down Expand Up @@ -310,8 +378,15 @@ pub fn one_line(r: &Review, pr: &PrNow, why: &str) -> String {
pub fn tidy(reviews: &[Review], threads: &[Thread], pr: &PrNow) -> Tidy {
let mut out = Tidy::default();
let done = pr.state != PrState::Open;
// The review CoderHelm's latest carried approval rests on stays in full
// while the pull request is open: it is what the verdict says was checked.
let live = if done {
None
} else {
live_carried_sha(reviews)
};
for r in reviews.iter().filter(|r| r.own && r.state != "PENDING") {
if r.body.trim().is_empty() || collapsed(&r.body) {
if r.body.trim().is_empty() || collapsed(&r.body) || rests_on(r, live.as_deref()) {
continue;
}
let why = match pr.state {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
//! kr. A pull request on a web app, approved by CoderHelm a week earlier
//! after a full review. A person merged the base branch into it. CoderHelm
//! saw the PR's own change was the one it approved and posted a new approval
//! on the new head whose whole body was one line: "Approved: the new commits
//! only bring in the base branch; the PR's own change is the same as the
//! approved `dab6dae`." plus the rating footer. Posting it rewrote every
//! earlier review of CoderHelm's to "Outdated: see CoderHelm's latest
//! review", and the tidy between reviews cuts any review of an older commit
//! to one line, so the full review the approval rested on was gone from
//! sight. The pull request's only visible approval had no review in it: "if
//! this happens we have no review to check".
//!
//! Guarantee: a carried approval stands on its own. Its body is the verdict,
//! why the earlier review still holds for the new head ("the PR's own change
//! is unchanged since `<sha>`; the new commits only bring in `<base>`"), then
//! that review as it was posted, from its stored record (what was checked,
//! the criteria, the tests, the notes); a carry of a carry keeps the one
//! review, never nests. While a carried approval is CoderHelm's latest word,
//! the review it rests on is never rewritten, cut to one line or hidden, nor
//! its comments hidden or its threads resolved. With no stored review of the
//! approved commit to carry (or only an old one-line carry), the PR is
//! reviewed in full instead of approved with nothing in sight.

use super::super::review_pr::{carried_approval_body, carried_review, CarriedApproval};
use super::super::review_sweep::ReviewRecord;
use common::review_supersede::{self as rs, PrState};
use serde_json::json;

/// The approved commit (the full review) and the head after the person
/// merged the base branch in.
const A: &str = "dab6dae0c1f2e3d4a5b6c7d8e9f0a1b2c3d4e5f6";
const B: &str = "7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f";

/// The full review of `A` as it was posted, without the rating footer.
fn full_review() -> String {
[
"✅ **Approved with notes**: nothing blocks the merge; 1 note below is for a look.",
"_Why approved: nothing blocks (no required rule broken, no failing check)._",
"<details>\n<summary>Details: PR Overview · Risk · Criteria · Verification · Tests</summary>",
"## PR Overview\n| File | What |\n|---|---|\n| `apps/web/src/signup/form.tsx` | Validates the date of birth |",
"#### Risk: low",
"#### Criteria\n- Ages under 18 are refused: proven by `form.test.tsx` \"refuses a minor\"",
"#### Verification\n- typecheck passed\n- unit tests passed",
"#### Tests\n- the under-18 branch is covered; the leap-day branch is not",
"#### Additional findings\n- [low] apps/web/src/signup/form.tsx:41 — The error text is not translated",
"</details>",
]
.join("\n\n")
}

fn footer() -> String {
super::super::review_pr::rating_footer("https://app.example", "acme", "web", 42)
}

/// CoderHelm's stored records, newest first: the full review of `A`.
fn records() -> Vec<ReviewRecord> {
vec![ReviewRecord {
verdict: "APPROVE".into(),
posted_as: "APPROVE".into(),
head_sha: A.into(),
created_at: "2026-11-11T15:00:00Z".into(),
body: "Validates the date of birth.".into(),
posted_body: full_review(),
..Default::default()
}]
}

/// The carried approval posted on `B`, as `carry_approval` builds it.
fn carried_on_b() -> String {
let review = carried_review(&records(), A).expect("the stored review of A");
carried_approval_body(
&CarriedApproval {
approved: A,
only_base_merged: true,
base_branch: "main",
review: &review,
},
B,
)
}

/// GitHub's reviews: the full one on `A`, a person's, then the carried one.
fn reviews(carried: &str) -> Vec<rs::Review> {
super::super::conversation::reviews_of(&json!([
{"id": 11, "state": "APPROVED", "body": format!("{}{}", full_review(), footer()),
"commit_id": A, "user": {"login": "coderhelm[bot]", "type": "Bot"}},
{"id": 12, "state": "COMMENTED", "body": "Thanks, merging main in.", "commit_id": A,
"user": {"login": "sam", "type": "User"}},
{"id": 13, "state": "APPROVED", "body": format!("{carried}{}", footer()),
"commit_id": B, "user": {"login": "coderhelm[bot]", "type": "Bot"}},
]))
}

/// The full review's note, an inline thread of CoderHelm's.
fn note_thread() -> rs::Thread {
rs::Thread {
id: "T1".into(),
path: "apps/web/src/signup/form.tsx".into(),
first_body: "The error text is not translated.".into(),
comments: vec![rs::ThreadComment {
node_id: "C1".into(),
own: true,
review_id: 11,
minimized: false,
}],
..Default::default()
}
}

fn open_pr() -> rs::PrNow {
super::super::review_tidy::pr_now(
"acme",
"web",
42,
&json!({"state": "open", "head": {"sha": B}}),
)
}

/// The latest visible review on `B` carries the full review's content,
/// stated against `B`, and names the commit it rests on.
#[test]
fn the_carried_approval_carries_the_review_it_rests_on() {
let body = carried_on_b();
assert!(
body.contains(
"✅ **Approved with notes** on `7e8f9a0`: the PR's own change is unchanged since \
`dab6dae`; the new commits only bring in `main`."
),
"{body}"
);
for section in [
"_Why approved: nothing blocks",
"## PR Overview",
"#### Criteria\n- Ages under 18 are refused",
"#### Verification\n- typecheck passed",
"#### Tests\n- the under-18 branch is covered",
"The error text is not translated",
] {
assert!(body.contains(section), "{section} missing from {body}");
}
// Not the one line the incident posted.
assert!(body.lines().count() > 10, "{body}");
assert_eq!(rs::carried_from(&body).as_deref(), Some(A));
}

/// Posting the carried approval rewrites nothing it rests on: the review of
/// `A` keeps its body, its note's comment stays in sight and its thread
/// open. A person's review is never touched either.
#[test]
fn posting_the_carry_keeps_the_review_it_rests_on() {
let r = reviews(&carried_on_b());
let p = rs::plan(&r, &[note_thread()], 13, &[]);
assert!(!p.edit.contains(&11), "{p:?}");
assert!(!p.minimize.contains(&"C1".to_string()), "{p:?}");
assert!(!p.resolve.contains(&"T1".to_string()), "{p:?}");
assert!(!p.edit.contains(&12), "{p:?}");
}

/// The tidy between reviews (a push, checks finishing, the sweep) never cuts
/// the review the latest carried approval rests on, nor the carried
/// approval; a second run does nothing.
#[test]
fn the_tidy_never_cuts_the_review_a_carry_rests_on() {
let r = reviews(&carried_on_b());
let t = rs::tidy(&r, &[note_thread()], &open_pr());
assert!(t.collapse.is_empty(), "{t:?}");
assert!(t.minimize.is_empty(), "{t:?}");
assert_eq!(rs::kept_for_carry(&r), vec![11]);
}

/// The approval already posted the old way (one line, no review in it) is
/// still read as resting on `A`: the review of `A` is not cut while it
/// stands.
#[test]
fn an_old_one_line_carry_still_keeps_its_review() {
let one_line = "✅ **Approved**: the new commits only bring in the base branch; the PR's \
own change is the same as the approved `dab6dae`.";
let r = reviews(one_line);
assert!(rs::tidy(&r, &[], &open_pr()).collapse.is_empty());
assert!(rs::plan(&r, &[], 13, &[]).edit.is_empty());
}

/// Once a real review follows (the PR's own change moved), it is the live
/// one: the review of `A` and the carried approval are both cut as before.
/// A merge cuts every body.
#[test]
fn a_later_review_supersedes_both() {
let mut r = reviews(&carried_on_b());
r.push(rs::Review {
id: 14,
own: true,
state: "APPROVED".into(),
body: "✅ **Approved**: the new validation reads well.".into(),
commit_id: "0a1b2c3d4e5f60718293a4b5c6d7e8f901234567".into(),
});
assert_eq!(rs::plan(&r, &[], 14, &[]).edit, vec![11, 13]);
let merged = rs::tidy(
&reviews(&carried_on_b()),
&[],
&rs::PrNow {
state: PrState::Merged,
..open_pr()
},
);
let cut: Vec<u64> = merged.collapse.iter().map(|(id, _)| *id).collect();
assert_eq!(cut, vec![11, 13]);
}

/// A carry of a carry (the base branch merged in again) carries the same
/// review once, from the carried approval's own record.
#[test]
fn a_second_merge_carries_the_same_review_once() {
let first = carried_on_b();
let stored = vec![ReviewRecord {
verdict: "APPROVE".into(),
posted_as: "APPROVE".into(),
head_sha: B.into(),
body: first.clone(),
posted_body: first,
..Default::default()
}];
let review = carried_review(&stored, B).expect("the carried approval's record");
let again = carried_approval_body(
&CarriedApproval {
approved: B,
only_base_merged: true,
base_branch: "main",
review: &review,
},
"c3d4e5f60718293a4b5c6d7e8f90123456789abc",
);
assert!(again.contains("unchanged since `7e8f9a0`"), "{again}");
assert!(again.contains("The review of `dab6dae` below"), "{again}");
assert_eq!(again.matches("#### Criteria").count(), 1, "{again}");
}

/// No stored review to carry, or only a one-line carry with no review in
/// it: nothing to carry, and the pipeline reviews the PR in full instead.
#[test]
fn nothing_to_carry_means_a_real_review() {
assert_eq!(carried_review(&[], A), None);
let one_line = ReviewRecord {
verdict: "APPROVE".into(),
posted_as: "APPROVE".into(),
head_sha: A.into(),
body: "✅ **Approved**: the new commits only bring in the base branch; the PR's own \
change is the same as the approved `1234abc`."
.into(),
..Default::default()
};
assert_eq!(carried_review(&[one_line], A), None);
let mut asked_changes = records();
asked_changes[0].verdict = "REQUEST_CHANGES".into();
assert_eq!(carried_review(&asked_changes, A), None);
// A record older than the posted body still carries its summary and
// findings.
let mut older = records();
older[0].posted_body = String::new();
assert_eq!(
carried_review(&older, A).as_deref(),
Some("Validates the date of birth.")
);
// The pipeline asks for the review before carrying, and reviews in full
// without it.
let src = include_str!("../review_pr.rs");
let carry = src.find("carried_review(&records, &approved)").unwrap();
let full = src[carry..]
.find("if nothing_new && carried.is_none() {")
.unwrap();
assert!(src[carry + full..]
.starts_with("if nothing_new && carried.is_none() {\n warn!"));
assert!(src.contains("let posted_body = full_body.strip_suffix(&footer)"));
}
Loading
Loading