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
1 change: 1 addition & 0 deletions docs/incidents.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pipeline still calls it there.
| s. `s_silent_draft` | A new-feature run finished with CI green and CoderHelm's review approving, and its pull request stayed a draft. Four of CoderHelm's own sandbox checks had failed on the head, the mark-ready step returned without a word, and the run page said completed. The same day, mark-ready messages that hit GitHub's rate limit were dropped and five more pull requests stayed drafts. | One decision settles every pull request a finished run leaves, on every path. With CI green and the review approving, CoderHelm's own sandbox failures and checklist items never hold the draft: the pull request is marked ready with them under Open items. A hold that remains names each reason and what to do at the top of the description, at warn in the log, and on the run, which is `needs_input` while a person must act. A mark-ready GitHub refuses is tried again after its reset, a bounded number of times, and the last refusal is recorded on the run. A sweep decides again for finished runs left a draft with no recorded reason, respecting a person's draft and a closed pull request. |
| t. `t_unrun_check_hold` | A check's gate couldn't install its package's dependencies (`pnpm: command not found` in CoderHelm's sandbox). The tooling failure went under Open items as a check the change didn't pass, and the pull request stayed a draft after CI passed. | A check CoderHelm's own tools couldn't run becomes an Open item CI settles, never a hold. A line written the old way stops holding once CI is green on the head and leaves the description when the pull request is marked ready. A check the change really failed still holds. The decision reads the repository's CI whenever such a line is in the description, not only when CoderHelm's own feature checks have findings: without that read, CI counted as not green and the line kept holding on every sweep. |
| u. `u_stacked_plan_check` | A run stacked on an earlier part's open pull request reset to the default branch in every step, rebuilt and force-pushed its branch each time, and measured its plan against the other branch: 13 files that branch already changed read as not done, the coder found nothing to write, and the guard stopped the run after seven continuations. | A later step keeps the base an earlier step stacked it on; a planned file the stacked-on branch already changes is done there, and the plan check's note says so; a fall in the done count is reported on the run page, and its recovery counts as progress a bounded number of times. |
| w. `w_stale_hold_recheck` | A finished run's pull request stayed a draft on an Open items line a wrong rule wrote ("the change makes the staging content its end-to-end tests read out of date"). The rule was fixed, but the line was text in the description and nothing checked it again, so the hold stayed until a person edited it. The same run spent three steps writing nothing while its plan check fell from 37 to 23 and came back each step, and the guard counted each return as progress. | Every blocked line names the version of the code that wrote it. A pull request held on a line other code wrote is checked again on its head in a fresh step of the run, once per run and code version, with the checks whose failure is a hold run again on an unchanged head; the Open items are then written from what they find. A line the running code wrote never starts another check. A fall in the plan check's count and its return count as progress only when the run's coder wrote code in between, and every plan check measures with the stacked base. |

## Adding a case

Expand Down
69 changes: 69 additions & 0 deletions services/worker/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! The version of the code that checks a change, for `passes::checks_version`.
//!
//! A hold CoderHelm's own checks write into a pull request's description is
//! text: nothing ran the check again when the code that wrote it changed, so
//! a pull request held by a rule that was later fixed stayed held. Each such
//! line now carries the version of the code that wrote it, and the ready
//! sweep checks the pull request again when the running code differs.
//!
//! The version is a hash of the worker's and the common crate's sources, so
//! it is the same wherever the same code runs (hosted or a self-hosted
//! install) and changes whenever that code does, with no build step to
//! remember.

use std::path::{Path, PathBuf};

fn files(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for e in entries.flatten() {
let p = e.path();
if p.is_dir() {
files(&p, out);
} else {
out.push(p);
}
}
}

/// FNV-1a, 64 bits: stable across toolchains, unlike the standard hasher.
fn fnv(hash: &mut u64, bytes: &[u8]) {
for b in bytes {
*hash ^= u64::from(*b);
*hash = hash.wrapping_mul(0x0100_0000_01b3);
}
}

fn main() {
let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into()));
let dirs = [root.join("src"), root.join("../common/src")];
let mut all = Vec::new();
for d in &dirs {
println!("cargo:rerun-if-changed={}", d.display());
files(d, &mut all);
}
// The same order everywhere: by path relative to the crate.
let mut named: Vec<(String, PathBuf)> = all
.into_iter()
.map(|p| {
let rel = p
.strip_prefix(&root)
.unwrap_or(&p)
.to_string_lossy()
.replace('\\', "/");
(rel, p)
})
.collect();
named.sort();
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for (rel, p) in &named {
fnv(&mut hash, rel.as_bytes());
fnv(&mut hash, &[0]);
if let Ok(bytes) = std::fs::read(p) {
fnv(&mut hash, &bytes);
}
fnv(&mut hash, &[0]);
}
println!("cargo:rustc-env=CODERHELM_CHECKS_VERSION={hash:016x}");
}
7 changes: 6 additions & 1 deletion services/worker/src/passes/implement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,14 +1043,19 @@ pub async fn run(
}
}

let files = executor
let files: Vec<String> = executor
.write_executor
.files_modified
.lock()
.unwrap()
.iter()
.cloned()
.collect();
// The run's own code moved: what the run guard weighs a recovered plan
// check count against.
if let (Some(run_id), false) = (run_id, files.is_empty()) {
super::run_guard::wrote_code(state, &msg.team_id, run_id).await;
}

// Cut short by the deadline: the loop's own marker, or the pass ended
// inside the wrap-up window (the agent summarized what was left).
Expand Down
1 change: 1 addition & 0 deletions services/worker/src/passes/incidents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ mod s_silent_draft;
mod t_unrun_check_hold;
mod u_stacked_plan_check;
mod v_dropped_field_hold;
mod w_stale_hold_recheck;
3 changes: 3 additions & 0 deletions services/worker/src/passes/incidents/u_stacked_plan_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ fn a_fall_in_the_count_is_news_and_its_recovery_is_progress() {
assert!(!g.apply(&Progress::TasksDone(17)));
assert_eq!(g.task_drops, 2);
g.on_continuation(1.0);
// The run's coder writes the lost work again.
g.on_own_code();
assert!(
g.apply(&Progress::TasksDone(23)),
"lost work done again is progress"
Expand All @@ -138,6 +140,7 @@ fn a_fall_in_the_count_is_news_and_its_recovery_is_progress() {
g.on_continuation(1.0);
g.apply(&Progress::TasksDone(17));
g.on_continuation(1.0);
g.on_own_code();
g.apply(&Progress::TasksDone(23));
}
assert!(g.task_drops > MAX_TASK_DROPS);
Expand Down
250 changes: 250 additions & 0 deletions services/worker/src/passes/incidents/w_stale_hold_recheck.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
//! w. A run finished and its pull request stayed a draft. Under Open items a
//! line marked as a check the change didn't pass said "the change makes the
//! staging content its end-to-end tests read out of date". The rule that
//! wrote it was wrong and was fixed, but the line was text in the
//! description: nothing ran the check again when the code that wrote it
//! changed, so the pull request stayed held on every sweep until a person
//! edited the description. A mention of CoderHelm on the pull request only
//! resumes a fix round's unfinished work, and this one had none.
//!
//! The same run spent three fresh steps doing nothing. Each step started by
//! rebuilding its stacked branch, the plan check at the start of the step
//! measured the branch's own diff (23 of 38 tasks) and the check after the
//! coder measured with the stacked base (37 of 38). The run guard counted
//! each return to 37 as work done again, so it never stopped the steps,
//! though the coder wrote nothing in any of them.
//!
//! Guarantees:
//! - Every blocked line names the version of the code that wrote it. A pull
//! request held on a line other code wrote is checked again on its head by
//! a fresh step of the run, once per run and code version; the checks whose
//! failure is a hold run again even on an unchanged head, and the Open
//! items are written from what they find.
//! - A line the running code wrote never starts another check: no loop.
//! - A fall in the plan check's count and its return count as progress only
//! when the run's coder wrote code in between. Every plan check measures
//! with the stacked base.

use std::collections::HashMap;

use aws_sdk_dynamodb::types::AttributeValue;

use super::fixture::fn_src;
use crate::passes::completeness::{self, Section};
use crate::passes::proof;
use crate::passes::ready::{self, Ci, Decision, Facts};
use crate::passes::recheck;
use crate::passes::run_guard::{GuardState, Ledger, Progress, Trip};

/// The reason the old rule wrote.
const STALE: &str = "the change makes the staging content its end-to-end tests read out of date.";

/// A version of the checks other than the one running now.
const OLD_VERSION: &str = "00000000feedface";

/// The Open items as the run left them: written by older checks, with no
/// version (lines written before lines carried one) or an older one.
fn held_description(version: Option<&str>) -> String {
let section = proof::gaps_section(&[proof::check_item("The new-feature rules", STALE)]);
let now = proof::checked_by(proof::checks_version());
let section = match version {
Some(v) => section.replace(&now, &proof::checked_by(v)),
None => section.replace(&format!(" {now}"), ""),
};
format!("## Summary\nOne Title Size setting for every venue block.\n\n{section}")
}

fn facts(body: &str) -> Facts {
Facts {
draft: true,
title: "Add a Title Size setting to the venue page blocks".into(),
body: body.to_string(),
ci: Ci {
green: true,
why: String::new(),
},
approved: true,
..Default::default()
}
}

/// What the checks write when they run again: the Open items replaced by
/// what they find now (`pr::apply_checks`).
fn rewritten(body: &str, found: &[proof::Finding]) -> String {
let (mut owned, rest) = completeness::take_owned(body);
owned.set(Section::OpenItems, proof::gaps_section(found));
completeness::assemble(&rest, &owned)
}

#[test]
fn a_hold_older_checks_wrote_is_checked_again_and_clears() {
let now = proof::checks_version();
assert!(!now.is_empty());
for body in [held_description(None), held_description(Some(OLD_VERSION))] {
assert!(body.contains(proof::BLOCKED_MARK), "{body}");
let f = facts(&body);
let d = ready::decide(&f);
assert!(matches!(d, Decision::Held { .. }), "{d:?}");
assert!(ready::recheck_due(&f, &d, now), "{body}");
}
// The checks run again with the fixed rule and find nothing: the line
// goes and the pull request is ready.
let after = rewritten(&held_description(None), &[]);
assert!(!after.contains(STALE), "{after}");
assert!(after.contains("One Title Size setting"), "{after}");
let f = facts(&after);
assert_eq!(ready::decide(&f), Decision::Ready { open: vec![] });
}

#[test]
fn a_line_the_running_checks_wrote_never_starts_another_check() {
let now = proof::checks_version();
// The check still fails on the new code: it holds, as a check the change
// didn't pass, and is not checked again.
let body = held_description(Some(now));
assert!(body.contains(&proof::checked_by(now)), "{body}");
assert_eq!(proof::checked_by_of(&body), Some(now));
let f = facts(&body);
let d = ready::decide(&f);
assert!(matches!(d, Decision::Held { .. }));
assert!(!ready::recheck_due(&f, &d, now));

// A person's draft and a closed pull request are never checked again.
let old = held_description(None);
let mine = Facts {
put_back_by: Some("someone".into()),
..facts(&old)
};
assert!(!ready::recheck_due(&mine, &ready::decide(&mine), now));
let closed = Facts {
closed: true,
..facts(&old)
};
assert!(!ready::recheck_due(&closed, &ready::decide(&closed), now));
}

#[test]
fn once_per_run_and_version_recorded_in_the_claim() {
// The claim and its record are one conditional write: a second decider,
// or the next sweep on the same code, finds the version taken.
let src = include_str!("../recheck.rs");
let claim = fn_src(src, "\nasync fn claim(");
assert!(claim.contains("SET recheck_version = :v"));
assert!(claim.contains("recheck_version <> :v"));
assert!(claim.contains("attribute_not_exists(guard_open)"));
// `settle` checks again only when it is due and the claim was won.
let ready_src = include_str!("../ready.rs");
let settle = fn_src(ready_src, "\npub async fn settle(");
let at = settle.find("recheck_due(").expect("settle asks");
assert!(settle[at..].contains("recheck::start("));
// The step resumes at the checks, and their results from other code
// don't stand on an unchanged head.
let m = include_str!("../mod.rs");
let re = fn_src(m, "\npub(crate) async fn recheck_at_checks(");
assert!(re.contains("steps::Gate::Checks"));
let rp = fn_src(m, "\nasync fn run_passes(");
assert!(rp.contains("for_checks_version(proof::checks_version())"));
}

#[test]
fn results_other_code_wrote_run_again_once_and_keep_their_rounds() {
let mut l = Ledger::default();
for r in [&mut l.rules, &mut l.links, &mut l.test_fix, &mut l.proof] {
r.head = "eeeb43e".into();
r.rounds = 2;
}
assert!(l.for_checks_version("v2"));
assert!(!l.rules.unchanged("eeeb43e"));
assert!(!l.links.unchanged("eeeb43e"));
assert!(!l.test_fix.unchanged("eeeb43e"));
assert_eq!(l.rules.rounds, 2, "the rounds a run had still count");
// The proof check's gaps are never a check the change didn't pass.
assert!(l.proof.unchanged("eeeb43e"));
// The same code again: its results stand.
l.rules.head = "eeeb43e".into();
assert!(!l.for_checks_version("v2"));
assert!(l.rules.unchanged("eeeb43e"));
}

#[test]
fn the_step_is_rebuilt_from_the_run_record() {
let s = |v: &str| AttributeValue::S(v.into());
let n = |v: u64| AttributeValue::N(v.to_string());
let item: HashMap<String, AttributeValue> = [
("team_id", s("TEAM#t")),
("run_id", s("R1")),
("repo", s("o/web")),
("branch", s("coderhelm/gh-41")),
("base_branch", s("coderhelm/gh-40")),
("installation_id", n(7)),
("ticket_source", s("jira")),
("ticket_id", s("GH-41")),
("ticket_body", s("Add a Title Size setting.")),
("title", s("Title Size")),
("run_step", n(5)),
("requester_key", s("email:a@example.com")),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect();
let (msg, branch) = recheck::message_of(&item).expect("a step");
assert_eq!(branch, "coderhelm/gh-41");
assert_eq!(
(msg.repo_owner.as_str(), msg.repo_name.as_str()),
("o", "web")
);
assert_eq!(
msg.base_branch, "coderhelm/gh-40",
"the stacked base is kept"
);
assert_eq!(msg.continuation_run_id.as_deref(), Some("R1"));
// The run's last step was 5: the next message is step 6, the same run.
let next = crate::passes::steps::next_message(&msg, "R1", 1);
assert_eq!(crate::passes::steps::step_number(next.continuation), 6);
assert!(next.same_run());

// No branch: nothing to check again.
let mut none = item.clone();
none.remove("branch");
assert!(recheck::message_of(&none).is_none());

// Security: an audit that passed needs no second run; one with items does.
assert!(recheck::carry_of("## Summary\nx").is_some());
assert!(recheck::carry_of(&format!("- x {}", proof::SECURITY_MARK)).is_none());
}

#[test]
fn a_dip_coderhelm_caused_and_its_return_are_not_progress() {
// The incident's counts: 37 at the end of a step, 23 after the rebuild
// at the start of the next, 37 again with nothing written.
let mut g = GuardState::default();
assert!(g.apply(&Progress::TasksDone(37)));
for _ in 0..8 {
g.on_continuation(1.0);
assert!(!g.apply(&Progress::TasksDone(23)));
assert!(
!g.apply(&Progress::TasksDone(37)),
"a return with nothing written is not progress"
);
}
assert_eq!(g.trip(), Some(Trip::Continuations));

// Someone else moved the base and the run's coder redoes the work: that
// is progress, still a bounded number of times.
let mut g = GuardState::default();
assert!(g.apply(&Progress::TasksDone(37)));
g.on_continuation(1.0);
assert!(!g.apply(&Progress::TasksDone(23)));
g.on_own_code();
assert!(g.apply(&Progress::TasksDone(30)));
assert_eq!(g.continuations, 0);

// The coder writing is reported from the one place every round writes,
// and the plan check at a step's start measures with the stacked base.
let imp = include_str!("../implement.rs");
assert!(imp.contains("run_guard::wrote_code("));
let m = include_str!("../mod.rs");
let cf = fn_src(m, "\nasync fn continuation_feedback(");
assert!(cf.contains("plan_coverage::plan_changes("));
assert!(!cf.contains("plan_coverage::coverage("));
}
Loading
Loading