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
20 changes: 20 additions & 0 deletions docs/incidents/rv_reviewer_reads_the_merge_base.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# rv. `rv_reviewer_reads_the_merge_base`

**What happened:** The reviewer got four things wrong on a vendor-portal pull request and on a CI script's.

- **The base merge:** after the base branch was merged into the pull request, the re-review read the diff from the commit it had approved. That diff carried a drawer component another pull request had already merged, and the review called it an unrelated change. Against the merge base the pull request changed 8 files, and the drawer wasn't one of them.
- **The passing run:** the preview's end-to-end run exercised the vendor's test ids against the live portal and passed. The review still blocked on "selectors no probe observed", because it only read probe records committed in the repository.
- **The red check:** the end-to-end check was red on the pull request and on the base branch. The review was told only the check's name, and it wrote that an unrelated phone-pricing test was "broken on main". The failing test was the camp-selection test in the spec the pull request changed. It failed because the vendor had opened next year's registration.
- **The missing test:** on the CI script's pull request the review rightly asked for a test, and the run handed it back. A stub of the cloud command on PATH and a fixture of its answer were all the test needed.

**The guarantee:**

- **What a pull request changes:** its merge-base diff. A re-review covers only the files whose own change moved since the reviewed commit. A finding that calls a file outside that diff unrelated or out of scope is dropped.
- **A passing run is a record:** a passing end-to-end run on the head, whose report shows the test ids, counts as a probe record of them.
- **The reviewer probes:** for ids nothing observed, the reviewer runs the probe itself.
- Ids the page has clear the gap.
- Ids the page lacks are a required finding that quotes the probe.
- A probe that couldn't run is a note with the reason, never a block.
- **Red checks, test by test:** a red end-to-end check is read test by test, from each failing test's own error. Tests in specs the change touches come first. A check is never the base branch's by its name.
- **Blame and reverts:** a sentence that blames the base branch for a test, or calls it flaky or pre-existing, is held unless that test fails on the base branch's head too. If it does and its spec is one the change touches, the fix belongs on this branch, and the sentence says so. Asking to revert part of the change is held unless the test passes on the base branch's head.
- **Tests are the coder's work:** on CoderHelm's own pull request, a request for a test always goes to the coder, whatever made it a note. A request about a script comes with how to test it: stub commands on PATH, fixtures, and running the script itself.
67 changes: 67 additions & 0 deletions services/worker/src/passes/e2e_reads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,73 @@ pub async fn read(cx: &Ctx<'_>, head: &str) -> Option<Verdict> {
None
}

/// A passing end-to-end run on the head and the report it published.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PassingRun {
/// How it is known to have passed (check runs, the comment).
pub how: String,
/// The published report, when the end-to-end comment links one.
pub report: Option<String>,
}

/// Whether the head's end-to-end run passed, from its check runs (`checks`)
/// and the pull request's end-to-end comments (`comments`, oldest first):
/// none of its checks failed or is running, and a check or the comment on
/// the head says it passed. The comment's report link comes with it.
pub fn passing_run(checks: &[Value], comments: &[Value], head: &str) -> Option<PassingRun> {
let by_checks = checks_verdict(checks);
if matches!(by_checks, Some(Verdict::Failed(_)) | Some(Verdict::Running)) {
return None;
}
let comment = comments
.iter()
.rev()
.filter_map(|c| comment_verdict(c["body"].as_str().unwrap_or("")))
.find(|c| on_head(c, head));
if comment.as_ref().and_then(|c| c.failed).unwrap_or(0) > 0 {
return None;
}
let report = comment.as_ref().and_then(|c| c.report.clone());
match (by_checks, comment) {
(Some(Verdict::Passed(how)), _) => Some(PassingRun { how, report }),
(None, Some(c)) if c.passed.unwrap_or(0) > 0 => Some(PassingRun {
how: format!(
"{} end-to-end test(s) passed (the end-to-end comment on the pull request)",
c.passed.unwrap_or(0)
),
report,
}),
_ => None,
}
}

/// The test ids (`ids`) a passing run's published report shows: each one
/// the report's text holds was exercised by a test that passed. Read with
/// the page check, so only a host the team's sites allow.
pub async fn report_shows(
state: &crate::WorkerState,
github: &crate::clients::github::GitHubClient,
web: &super::web_check::WebCheck,
report: &str,
ids: &[String],
) -> Vec<String> {
if ids.is_empty() {
return Vec::new();
}
let Ok(ck) = web.verify_find(state, github, report, ids).await else {
return Vec::new();
};
if ck.status() != Some(200) {
return Vec::new();
}
ck.found
.iter()
.filter(|(_, hits)| !hits.is_empty())
.map(|(id, _)| id.clone())
.filter(|id| ids.contains(id))
.collect()
}

/// The published report's verdict, read with the page check (only a host
/// the team's sites allow).
async fn report(cx: &Ctx<'_>, url: &str) -> Option<Verdict> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
//! rv. A vendor-portal pull request, and a CI script's, as the reviewer saw
//! them.
//!
//! - After the base branch was merged into the portal pull request, the
//! review read the diff from the commit it had approved. That diff
//! carried a drawer component another pull request had already merged,
//! and the review called it "an unrelated change". Against the merge
//! base the pull request changed 8 files and the drawer wasn't one.
//! - The preview's end-to-end run exercised the vendor's test ids against
//! the live portal and passed. The review still blocked on "selectors no
//! probe observed", because it only read committed probe records.
//! - The end-to-end check was red on the pull request and on the base
//! branch. The review was told only the check's name, and wrote that an
//! unrelated phone-pricing test was "broken on main". The failing test was
//! the camp-selection test in the spec the pull request changed, red
//! because the vendor had opened next year's registration.
//! - On the CI script's pull request the review asked for a test of the
//! script and the run handed it back, though a stub of the cloud command
//! on PATH and a fixture of its answer were all it needed.
//!
//! Guarantee: a re-review covers only the pull request's own change since
//! the reviewed commit (`review_scope::own_files_moved`, `own_part_of`), and
//! a finding calling a file outside the merge-base diff unrelated is dropped
//! (`drop_outside_claims`). A passing end-to-end run whose report shows the
//! ids is a probe record (`e2e_reads::passing_run`,
//! `review_probe::from_report`), and what is still unobserved the reviewer
//! probes itself and decides from the record (`review_probe::decide`). A red
//! check is read test by test (`review_ci::CiCompare`), and a blame or
//! revert sentence it doesn't back is held (`review_ci::hold`). On
//! CoderHelm's own pull request a request for a test is the coder's work,
//! with how to test a script (`own_review::required`, `test_how`).

use serde_json::{json, Value};

use crate::passes::review_agent::Finding;
use crate::passes::{e2e_reads, own_review, review_ci, review_probe, review_scope};

const PORTAL: &str = "https://portal.example-vendor.io/org/7/portal/42";

fn file(name: &str, patch: &str) -> Value {
json!({"filename": name, "status": "modified", "patch": patch})
}

/// The pull request's own change: the embed, its test and the portal spec.
fn own(embed_patch: &str) -> Vec<Value> {
vec![
file(
"apps/web/src/components/blocks/portal-embed.tsx",
embed_patch,
),
file(
"apps/web/src/test/components/portal-embed.test.tsx",
"@@ -3,1 +3,1 @@\n-expect(link).toBeVisible();\n+expect(link).toBeNull();",
),
file(
"e2e/tests/browse/portal.spec.ts",
"@@ -20,1 +20,1 @@\n-await expect(newTab).toBeVisible();\n+await expect(newTab).toHaveCount(0);",
),
]
}

const EMBED_AT_APPROVAL: &str =
"@@ -10,2 +10,1 @@\n-<a href={url}>{linkLabel}</a>\n-<Frame h=\"svh\" />\n+<Frame />";
const EMBED_NOW: &str = "@@ -10,1 +10,0 @@\n-<a href={url}>{linkLabel}</a>";

#[test]
fn a_re_review_after_a_base_merge_covers_only_the_prs_own_change() {
let at_approval = json!({"status": "ahead", "files": own(EMBED_AT_APPROVAL)});
let now = json!({"status": "ahead", "files": own(EMBED_NOW)});
// From the approved commit to the head: the PR's own fix, and the
// drawer the base branch brought in with the merge.
let since = json!({"status": "ahead", "files": [
file("apps/web/src/components/blocks/portal-embed.tsx", "@@ -10,1 +10,1 @@\n-<Frame h=\"svh\" />\n+<Frame />"),
file("apps/web/src/components/ui/drawer.tsx", "@@ -60,1 +60,2 @@\n <Title>\n+<span className=\"sr-only\">Drawer</span>"),
]});
assert!(!review_scope::same_own_change(&at_approval, &now));
let moved = review_scope::own_files_moved(&at_approval, &now);
assert_eq!(
moved,
vec!["apps/web/src/components/blocks/portal-embed.tsx"]
);
let reviewed = review_scope::own_part_of(&since, &moved);
let paths = review_scope::compare_paths(&reviewed);
assert_eq!(
paths,
vec!["apps/web/src/components/blocks/portal-embed.tsx"]
);
assert!(!paths.iter().any(|p| p.ends_with("drawer.tsx")));
assert_eq!(reviewed["status"], "ahead");

// And a finding that calls the drawer this PR's unrelated change is
// dropped; one about a caller the change breaks is kept.
let pr_files = review_scope::compare_paths(&now);
let mut findings = vec![
Finding {
file: "apps/web/src/components/ui/drawer.tsx".into(),
title: "Unrelated change to the drawer's title".into(),
body: "This file is out of scope for the ticket; revert it.".into(),
severity: "high".into(),
..Default::default()
},
Finding {
file: "apps/web/src/lib/routing/content-renderers.tsx".into(),
title: "Still passes the removed linkLabel prop".into(),
body: "The renderer passes `linkLabel`, which the embed no longer takes.".into(),
severity: "high".into(),
..Default::default()
},
];
let dropped = review_scope::drop_outside_claims(&mut findings, &pr_files);
assert_eq!(dropped.len(), 1);
assert_eq!(findings.len(), 1);
assert!(findings[0].file.ends_with("content-renderers.tsx"));
}

#[test]
fn the_reviewer_wires_the_merge_base_scope_in_both_re_review_paths() {
let src = include_str!("../review_pr.rs");
assert_eq!(
src.matches("review_scope::own_part_of(&since, &moved)")
.count(),
2
);
assert!(src.contains("review_scope::drop_outside_claims(&mut findings, &changed_list)"));
}

const SPEC: &str = "import { test, expect } from '@playwright/test';\nconst PORTAL = 'https://portal.example-vendor.io/org/7/portal/42';\ntest('choosing a camp adds it to the cart', async ({ page }) => {\n const frame = page.frameLocator('iframe');\n await frame.getByTestId('segment-option').first().click();\n await expect(frame.getByTestId('cart-preview-subtotal')).not.toHaveText('$0');\n});\n";

fn gaps() -> Vec<common::e2e::probe::Unprobed> {
let files = vec![(
"e2e/tests/browse/portal.spec.ts".to_string(),
SPEC.to_string(),
SPEC.to_string(),
)];
common::e2e::probe::unprobed(&files, &[], &["acme.example".into()])
}

#[test]
fn a_passing_end_to_end_run_on_the_head_is_a_record_of_the_ids_it_exercised() {
let g = gaps();
assert_eq!(g.len(), 1, "no committed probe: the gate finds the ids");
let head = "eb32df14230678e5c7c1ff2c0929020ae6181dd8";
let checks = vec![
json!({"name": "Lint", "status": "completed", "conclusion": "success"}),
json!({"name": "E2E:camps / Playwright", "status": "completed", "conclusion": "success"}),
];
let comments = vec![
json!({"body": "## Playwright E2E results for eb32df1\n\n12 passed, 0 failed\n\n[Full report](https://reports.acme.example/pr-1/index.html)"}),
];
let run = e2e_reads::passing_run(&checks, &comments, head).expect("a passing run");
assert_eq!(
run.report.as_deref(),
Some("https://reports.acme.example/pr-1/index.html")
);
// The report's text shows both ids (`report_shows`): they are observed.
let found = vec![
"cart-preview-subtotal".to_string(),
"segment-option".to_string(),
];
let record =
review_probe::from_report(&g[0].origin, run.report.as_deref().unwrap(), head, &found);
let files = vec![(
"e2e/tests/browse/portal.spec.ts".to_string(),
SPEC.to_string(),
SPEC.to_string(),
)];
assert!(common::e2e::probe::unprobed(&files, &[record], &["acme.example".into()]).is_empty());
// A red or running run is no record.
let red = vec![
json!({"name": "E2E:camps / Playwright", "status": "completed", "conclusion": "failure"}),
];
assert_eq!(e2e_reads::passing_run(&red, &comments, head), None);
}

#[test]
fn what_is_still_unobserved_the_reviewer_probes_and_decides_from_the_record() {
let g = &gaps()[0];
assert_eq!(
review_probe::page_url(SPEC, &g.origin).as_deref(),
Some(PORTAL)
);
// The page has the ids: no finding.
let seen = common::e2e::probe::ProbeRecord {
origin: g.origin.clone(),
url: PORTAL.into(),
testids: vec!["segment-option".into(), "cart-preview-subtotal".into()],
..Default::default()
};
assert!(review_probe::decide(g, SPEC, SPEC, &review_probe::Probed::Ran(seen)).is_none());
// The page lacks one: a verified, required finding quoting the record.
let partial = common::e2e::probe::ProbeRecord {
origin: g.origin.clone(),
url: PORTAL.into(),
testids: vec!["segment-option".into()],
..Default::default()
};
let f = review_probe::decide(g, SPEC, SPEC, &review_probe::Probed::Ran(partial)).unwrap();
assert!(f.required.is_some() && f.body.contains("`cart-preview-subtotal`"));
// The probe couldn't run: a note, never a block on the reviewer's own
// missing look; on CoderHelm's own PR it is still the coder's work.
let f = review_probe::decide(
g,
SPEC,
SPEC,
&review_probe::Probed::NotRun("the sandbox couldn't run the probe".into()),
)
.unwrap();
assert!(f.required.is_none() && !f.holds_merge());
assert_eq!(own_review::required(&[f], &[]).len(), 1);
}

#[test]
fn the_reviewer_settles_probe_gaps_before_they_are_findings() {
let src = include_str!("../review_pr.rs");
let gate = src.find("async fn probe_gaps(").unwrap();
let end = src.find("async fn smallest_change_gaps(").unwrap();
let body = &src[gate..end];
assert!(body.contains("e2e_reads::passing_run("));
assert!(body.contains("super::web_check::PROBE_TOOL"));
assert!(body.contains("review_probe::decide("));
assert!(!body.contains("review_rules::probe_findings("));
}

const HEAD_NOTES: &str = "\n--- E2E on preview / Playwright e2e (all tests) ---\nfailure: e2e/tests/browse/portal.spec.ts:120: 1) [chromium] › browse/portal.spec.ts:120:7 › Portal › selecting a camp adds it to the cart while registration is open ──\n\n Error: Choosing \"Mini-Camp (AM)\" did not add it to the order summary\n expect(locator).not.toHaveText(expected) failed\n Received: \"Subtotal$0\"\n";

#[test]
fn a_red_check_is_read_test_by_test_and_a_blame_it_doesnt_back_is_held() {
// Red on both heads, the same camp test (the vendor opened registration).
let base_notes = HEAD_NOTES.replace("portal.spec.ts:120", "portal.spec.ts:118");
let base_runs = json!({"check_runs": [
{"name": "E2E on preview / Playwright e2e (all tests)", "status": "completed", "conclusion": "failure"}
]});
let cmp = review_ci::CiCompare::read("eb32df1", "bd414b9", HEAD_NOTES, &base_notes, &base_runs)
.touching(vec!["e2e/tests/browse/portal.spec.ts".into()]);
let t = &cmp.head_failing[0];
assert!(t.error.contains("did not add it to the order summary"));
assert!(cmp.on_base_too(t) && cmp.in_touched_spec(t));
// The check is never the base's by its name: only the test is.
assert_eq!(
cmp.base_failing(),
vec!["Portal › selecting a camp adds it to the cart while registration is open"]
);
let ctx = cmp.context();
assert!(ctx.contains("(a spec this change touches)"));
assert!(ctx.contains("did not add it to the order summary"));

let mut summary =
"The phone pricing test is broken on main, so the red e2e check isn't this change's. \
Revert the portal spec to the last green head to get CI green."
.to_string();
let mut findings = vec![Finding {
file: "e2e/tests/browse/portal.spec.ts".into(),
title: "Camp selection test fails".into(),
body: "selecting a camp adds it to the cart while registration is open fails, and it's pre-existing on main.".into(),
caused_by_diff: Some(false),
..Default::default()
}];
let held = review_ci::hold(&mut findings, &mut summary, &cmp);
assert_eq!(held, 3);
assert!(summary.starts_with(review_ci::BLAME_HELD));
assert!(summary.contains(review_ci::REVERT_HELD));
assert!(!summary.contains("phone pricing"));
// The camp test fails on the base too, but it is in the spec this PR
// changes: its fix belongs on this branch, never "pre-existing".
assert!(findings[0].body.contains(review_ci::TOUCHED_HELD));
assert_eq!(findings[0].caused_by_diff, None);
}

#[test]
fn the_reviewer_reads_red_checks_through_the_comparison() {
let src = include_str!("../review_pr.rs");
assert!(src.contains("review_ci::CiCompare::read("));
assert!(src.contains("let base_failing: Vec<String> = ci_compare.base_failing();"));
assert!(src.contains("review_ci::hold(&mut findings, &mut output.summary, &ci_compare)"));
}

#[test]
fn a_request_for_a_test_of_a_ci_script_is_the_coders_work_with_how() {
let tracked = Finding {
file: ".github/scripts/check-waf-rules.sh".into(),
title: "No test covers the new rule-count check".into(),
body: "The script now fails when describe-rules returns no rules, but no test covers it."
.into(),
category: "tests".into(),
severity: "medium".into(),
tracked: Some(true),
note_only: Some("A note, not a block: the PR already tracks this as a follow-up.".into()),
..Default::default()
};
let items = own_review::required(&[tracked], &[]);
assert_eq!(items.len(), 1, "a tracked test ask still reaches the coder");
assert!(items[0].contains("stub commands first on PATH"));
assert!(items[0].contains("fixture"));
}
Loading
Loading