diff --git a/services/common/src/e2e/attribution.rs b/services/common/src/e2e/attribution.rs index 202dbcf6a..f0ca90285 100644 --- a/services/common/src/e2e/attribution.rs +++ b/services/common/src/e2e/attribution.rs @@ -9,7 +9,10 @@ //! page objects it imports), found in app code, and the requests the //! trace recorded, found where the app builds or serves them. The diff //! touches the failing path only when it changes one of those app files; -//! a spec or helper the change shares with the test is not evidence. +//! a helper the change shares with other tests is not evidence. But a +//! change that adds or edits the failing test itself (its spec or a page +//! object it imports) owns the failure ([`test_changed`]): the fix goes +//! on that pull request, never a separate one from the default branch. //! 3. **Data dependence**: when the page behaved correctly for its data (a //! registration window not open yet, a full class, content moved on //! staging), the test is wrong, not the app. @@ -238,6 +241,32 @@ pub fn touched(diff: &[String], path: &[String]) -> Vec { .collect() } +/// The failing test's own files (its spec and the page objects it imports) +/// the change adds or edits. The test then runs this change's expectation: +/// a separate pull request from the default branch can't fix it (the spec +/// may not even be there), so the failure is the change's to fix. +pub fn test_changed(diff: &[String], test_files: &[String]) -> Vec { + let norm = |p: &str| p.trim_start_matches("./").to_string(); + let files: Vec = test_files.iter().map(|p| norm(p)).collect(); + diff.iter() + .filter(|f| files.contains(&norm(f))) + .cloned() + .collect() +} + +/// Why a failure in a test the change wrote or edited is the change's. +pub fn test_changed_reason(files: &[String]) -> String { + format!( + "This pull request adds or edits the failing test itself ({}), so the fix belongs on \ + this pull request, not in a separate one from the default branch.", + files + .iter() + .map(|f| format!("`{f}`")) + .collect::>() + .join(", ") + ) +} + /// The verdict, from what the diff touches and whether the page was right /// for its data. pub fn decide(touched: &[String], page_valid_for_data: bool) -> Verdict { @@ -448,6 +477,26 @@ mod tests { /// registration isn't open yet. pub const NOT_OPEN_YET: &str = "- main:\n - heading \"Beginner swim, Tuesdays\" [level=1]\n - paragraph: Registration opens on March 3.\n - button \"Register\" [disabled]\n"; + /// A test the pull request adds (its spec and page object are only on + /// its branch) is the pull request's to fix; a spec it doesn't touch + /// is not. + #[test] + fn a_test_the_change_adds_or_edits_is_the_changes_failure() { + let diff = vec![ + "e2e/pages/venue-subnav.ts".to_string(), + "e2e/tests/site/venue-subnav-more-menu.spec.ts".to_string(), + "web/src/components/subnav.tsx".to_string(), + ]; + let test_files = vec![ + "./e2e/tests/site/venue-subnav-more-menu.spec.ts".to_string(), + "e2e/pages/venue-subnav.ts".to_string(), + ]; + let changed = test_changed(&diff, &test_files); + assert_eq!(changed.len(), 2); + assert!(test_changed_reason(&changed).contains("`e2e/pages/venue-subnav.ts`")); + assert!(test_changed(&diff, &["e2e/tests/site/home.spec.ts".to_string()]).is_empty()); + } + #[test] fn hooks_come_from_locators_in_steps_and_page_objects() { let step = "Click getByTestId('class-card').first()"; diff --git a/services/worker/src/passes/e2e_attribution.rs b/services/worker/src/passes/e2e_attribution.rs index 681ef0e4e..ad65565de 100644 --- a/services/worker/src/passes/e2e_attribution.rs +++ b/services/worker/src/passes/e2e_attribution.rs @@ -306,6 +306,8 @@ pub async fn attribute( .cloned(); let mut texts: Vec = vec![f.failed_step.clone(), f.snippet.clone(), f.error.clone()]; + // The test's own files: its spec and the page objects it imports. + let mut test_files = vec![spec.clone().unwrap_or_else(|| f.file.clone())]; if let Some(spec) = &spec { if let Ok(src) = gh.read_file(owner, repo, spec, &msg.head_sha).await { for import in common::e2e::test_package::relative_imports(spec, &src, &tree) @@ -315,15 +317,17 @@ pub async fn attribute( if let Ok(po) = gh.read_file(owner, repo, &import, &msg.head_sha).await { texts.push(po); } + test_files.push(import); } } } + let test_changed = attribution::test_changed(&diff, &test_files); let refs: Vec<&str> = texts.iter().map(String::as_str).collect(); let mut hooks = attribution::hooks(&refs); hooks.extend(requests.into_iter().map(Hook::Request)); let found = find_hooks(gh, owner, repo, &msg.head_sha, &hooks).await; let (path, path_evidence) = attribution::failing_path(&found); - let touched = attribution::touched(&diff, &path); + let mut touched = attribution::touched(&diff, &path); let page = read_page( state, &provider, @@ -336,7 +340,19 @@ pub async fn attribute( ) .await .unwrap_or_default(); - let verdict = attribution::decide(&touched, page.valid_for_data); + let mut verdict = attribution::decide(&touched, page.valid_for_data); + let mut reason = page.reason; + // The change wrote or edited this test: its fix is the change's, + // on its own pull request (the test may not exist anywhere else). + if !test_changed.is_empty() { + verdict = Verdict::Related; + reason = attribution::test_changed_reason(&test_changed); + for t in test_changed { + if !touched.contains(&t) { + touched.push(t); + } + } + } out.push(Attribution { test: title, file: spec.unwrap_or_else(|| f.file.clone()), @@ -351,7 +367,7 @@ pub async fn attribute( touched, diff: diff.clone(), valid_for_data: page.valid_for_data, - reason: page.reason, + reason, verdict, fix_pr: None, source: f.source.clone(), diff --git a/services/worker/src/passes/mod.rs b/services/worker/src/passes/mod.rs index 9b427932c..129fbf1a6 100644 --- a/services/worker/src/passes/mod.rs +++ b/services/worker/src/passes/mod.rs @@ -15,6 +15,11 @@ const MIN_TIME_FOR_REMEDIATION_SECS: u64 = 240; // 4 minutes /// left (conflict resolution and the description are model calls); with /// less, the run continues in a fresh step at the pull request. const MIN_TIME_FOR_PR_SECS: u64 = 150; +/// Least time left in a step for the coder to start in it (it stops 150s +/// before the end): less, and it starts in a fresh step instead. +const MIN_TIME_FOR_IMPLEMENT_SECS: u64 = 480; +// Past the PR buffer and the coder's wrap-up window, with minutes to build. +const _: () = assert!(MIN_TIME_FOR_IMPLEMENT_SECS >= 150 + 120 + 180); /// The pass the checks on the open pull request run under. pub(crate) const PASS_CHECKS: &str = "review"; @@ -680,6 +685,29 @@ mod continuation_tests { assert!(h.contains("couldn't continue in a")); } + /// A run whose planning used 11 of its 15 minutes started the coder with + /// 80 seconds; it wrote nothing and the run ended as "made no write". + /// The coder starts in a fresh step when the step's time is short, and + /// a coder that ran out of time before writing continues, never stops. + #[test] + fn the_coder_never_starts_or_ends_on_a_spent_step() { + let rp = run_passes_src(); + let check = rp + .find("if remaining_secs(start) < MIN_TIME_FOR_IMPLEMENT_SECS {") + .expect("a time check before the coder starts"); + let first_run = rp.find("let mut result = implement::run(").unwrap(); + assert!(check < first_run); + assert!(rp[check..first_run].contains("try_auto_continue(")); + let judged = rp.find("let mut verdict = run_end::judge(").unwrap(); + let timed = rp[judged..] + .find("if result.timed_out") + .expect("a timed-out coder continues") + + judged; + let again = rp.find("let again = implement::run(").unwrap(); + assert!(timed < again); + assert!(rp[timed..again].contains("try_auto_continue(")); + } + /// A continuation resumes at the gate the last step handed off at: the /// hand-off's checkpoint ("implement") is one a step resumes from /// without implementing again, and the gate decides which stages run. @@ -4710,6 +4738,26 @@ async fn run_passes( usage, ) .await; + // Planning (and a re-plan) can use most of a step: the coder never + // starts with a few seconds left, writes nothing and ends the run as + // "made no write". It starts in a fresh step with its full time. + if remaining_secs(start) < MIN_TIME_FOR_IMPLEMENT_SECS { + let resume_from = Some(("implement_started".to_string(), branch_name.clone(), 0u8)); + let handoff = try_auto_continue( + state, + msg, + run_id, + &resume_from, + &["implement_started"], + false, + "Planning used most of this step; the coder starts in a fresh one", + ) + .await; + if handoff.continued() { + close_memory(state, agent_memory.take()).await; + return Ok(()); + } + } // A continuation picks up the planned tasks the branch doesn't do yet. let still_to_do = if msg.continuation > 0 { continuation_feedback(state, run_id, &github, msg, &plan_result, &branch_name).await @@ -4958,6 +5006,30 @@ async fn run_passes( &result.writes, outside, ); + // The coder ran out of this step's time before writing: that is + // never "made no write" or a question. It builds in a fresh step. + if result.timed_out + && matches!( + verdict, + run_end::Empty::Unbuilt(_) | run_end::Empty::Ask(_) | run_end::Empty::Generic + ) + { + let resume_from = Some(("implement_started".to_string(), branch_name.clone(), 0u8)); + let handoff = try_auto_continue( + state, + msg, + run_id, + &resume_from, + &["implement_started"], + false, + "The coder ran out of this step's time before writing a file", + ) + .await; + if handoff.continued() { + close_memory(state, agent_memory.take()).await; + return Ok(()); + } + } // Nothing on the branch, and the plan says what to build: one // more build, told every task, before anything is asked. if matches!(verdict, run_end::Empty::Unbuilt(_))