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
231 changes: 231 additions & 0 deletions services/common/src/decision_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,188 @@ pub async fn on_merged(
.await
}

/// A person approved the pull request while CoderHelm's block stood: each
/// open block on it was overridden.
pub async fn on_approved_over_block(
dynamo: &aws_sdk_dynamodb::Client,
table: &str,
team_id: &str,
repo: &str,
pr: u64,
) -> usize {
let subject = Subject::Pr {
repo: repo.to_string(),
number: pr,
};
land(dynamo, table, team_id, &subject, Group::Block, |_| {
Some(Outcome::Overrode)
})
.await
}

/// The pull request heads the open blocks on it were decided on (so a merge
/// can compare each head with the merged one).
pub async fn open_block_heads(
dynamo: &aws_sdk_dynamodb::Client,
table: &str,
team_id: &str,
repo: &str,
pr: u64,
) -> Vec<String> {
let subject = Subject::Pr {
repo: repo.to_string(),
number: pr,
};
let records = for_subject(dynamo, table, team_id, &subject).await;
let mut heads: Vec<String> = pending(&records, Group::Block)
.iter()
.map(|r| r.head_sha.clone())
.filter(|h| !h.is_empty())
.collect();
heads.sort();
heads.dedup();
heads
}

/// A merge's outcome for one block, given the files changed since each
/// block's head (`changed_by_head`, from a compare of that head with the
/// merged one). A block with no compare falls back to [`merge_outcome`]
/// without one.
pub fn merge_outcome_by_head(
rec: &Record,
merge_head: &str,
changed_by_head: &HashMap<String, Vec<String>>,
) -> Outcome {
merge_outcome(
rec,
merge_head,
changed_by_head.get(&rec.head_sha).map(|v| v.as_slice()),
)
}

/// A pull request merged, with the files changed since each open block's
/// head: a block whose flagged file was never touched merged unchanged
/// (it wasn't needed), one whose file changed was fixed.
pub async fn on_merged_by_head(
dynamo: &aws_sdk_dynamodb::Client,
table: &str,
team_id: &str,
repo: &str,
pr: u64,
merge_head: &str,
changed_by_head: &HashMap<String, Vec<String>>,
) -> usize {
let subject = Subject::Pr {
repo: repo.to_string(),
number: pr,
};
land(dynamo, table, team_id, &subject, Group::Block, |r| {
Some(merge_outcome_by_head(r, merge_head, changed_by_head))
})
.await
}

/// The judge's memory of the outcome log: earlier stops or blocks of `kind`
/// that people's acts showed wrong, newest first, the same step first, one
/// line each (`category`: what it said, and what people did).
pub fn overturned_lines(records: &[Record], kind: &str, step: &str) -> Vec<String> {
let mut wrong: Vec<&Record> = records
.iter()
.filter(|r| r.kind == kind && r.confirmed_wrong())
.collect();
wrong.sort_by(|a, b| {
(b.step == step)
.cmp(&(a.step == step))
.then(b.created_at.cmp(&a.created_at))
});
let mut out: Vec<String> = Vec::new();
for r in wrong {
let line = format!(
"{} by the {} step ({}): \"{}\"; people {}",
r.verdict,
r.step,
if r.category.is_empty() {
"no category"
} else {
r.category.as_str()
},
crate::truncate_str(r.detail.trim(), 200),
match r.outcome {
Some(Outcome::AnsweredDefault) => "answered with the default it offered",
Some(Outcome::Retried) => "retried the run as it was",
Some(Outcome::Overrode) => "went ahead over it",
Some(Outcome::DismissedReview) => "dismissed the review",
Some(Outcome::MergedFlaggedUnchanged) => "merged with the flagged lines unchanged",
_ => "overturned it",
}
);
if !out.contains(&line) {
out.push(line);
}
if out.len() >= crate::stop_judge::MAX_OVERTURNED {
break;
}
}
out
}

/// Decisions newer than `since`, without the stored picture and answers
/// (for counts and the judge's memory: a few kilobytes, not megabytes).
pub async fn since_light(
dynamo: &aws_sdk_dynamodb::Client,
table: &str,
team_id: &str,
since: &str,
max_pages: usize,
) -> Vec<Record> {
let mut out = Vec::new();
if table.is_empty() {
return out;
}
let fields = [
"sk",
"kind",
"step",
"verdict",
"category",
"detail",
"outcome",
"outcome_at",
"created_at",
"head_sha",
"location",
"repo",
"pr_number",
"run_id",
];
let names: Vec<String> = (0..fields.len()).map(|i| format!("#f{i}")).collect();
let mut start: Option<HashMap<String, AttributeValue>> = None;
for _ in 0..max_pages.max(1) {
let mut q = dynamo
.query()
.table_name(table)
.key_condition_expression("pk = :pk AND begins_with(sk, :p)")
.filter_expression("#f8 >= :since")
.expression_attribute_values(":pk", AttributeValue::S(team_id.into()))
.expression_attribute_values(":p", AttributeValue::S(SK_PREFIX.into()))
.expression_attribute_values(":since", AttributeValue::S(since.into()))
.projection_expression(names.join(", "))
.set_exclusive_start_key(start.take());
for (alias, name) in names.iter().zip(fields) {
q = q.expression_attribute_names(alias, name);
}
let Ok(page) = q.send().await else {
break;
};
out.extend(page.items().iter().map(Record::from_item));
match page.last_evaluated_key() {
Some(k) if !k.is_empty() => start = Some(k.clone()),
_ => break,
}
}
out
}

/// Decisions newer than `since` (RFC 3339), across the team.
pub async fn since(
dynamo: &aws_sdk_dynamodb::Client,
Expand Down Expand Up @@ -933,6 +1115,55 @@ mod tests {
assert_eq!(merge_outcome(&r, "h2", Some(&touched)), Outcome::Fixed);
}

/// The merge webhook used to pass no compare, so every block on a PR
/// with any later commit read as "fixed" and none as a nuisance.
#[test]
fn a_merge_compares_each_blocks_own_head() {
let (j, p) = judged(Kind::Block, hold());
let pr = Subject::Pr {
repo: "a/b".into(),
number: 1,
};
let r = Record::new(&pr, &j, &p, "h1");
let mut by_head = HashMap::new();
by_head.insert("h1".to_string(), vec!["README.md".to_string()]);
assert_eq!(
merge_outcome_by_head(&r, "h9", &by_head),
Outcome::MergedFlaggedUnchanged
);
by_head.insert("h1".to_string(), vec!["src/a.rs".to_string()]);
assert_eq!(merge_outcome_by_head(&r, "h9", &by_head), Outcome::Fixed);
}

#[test]
fn overturned_decisions_become_the_judges_memory() {
let (j, p) = judged(Kind::Block, hold());
let pr = Subject::Pr {
repo: "a/b".into(),
number: 1,
};
let mut wrong = Record::new(&pr, &j, &p, "h1");
wrong.outcome = Some(Outcome::Overrode);
wrong.created_at = "2026-11-02T00:00:00Z".into();
let mut right = Record::new(&pr, &j, &p, "h1");
right.outcome = Some(Outcome::Fixed);
let open = Record::new(&pr, &j, &p, "h1");
let mut other_step = wrong.clone();
other_step.step = "own_review".into();
other_step.created_at = "2026-11-05T00:00:00Z".into();
other_step.detail = "another".into();
let lines = overturned_lines(&[wrong.clone(), right, open, other_step], "block", "review");
assert_eq!(lines.len(), 2, "{lines:?}");
// The same step first, even when older.
assert!(
lines[0].contains("by the review step (correctness)"),
"{lines:?}"
);
assert!(lines[0].contains("people went ahead over it"));
assert!(lines[1].contains("own_review"));
assert!(overturned_lines(&[wrong], "ask", "review").is_empty());
}

#[test]
fn answers_that_take_the_default() {
assert!(answered_default("Monthly totals", "monthly totals"));
Expand Down
11 changes: 11 additions & 0 deletions services/common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub struct WeeklySummaryTestMessage {
/// Every message on the worker queues. The gateway, the schedules and the
/// worker all read and write this one enum, so a tag one side sends is always
/// one the other reads.
///
/// A message is built once and handed over by value (never kept in bulk),
/// so the ticket variant's size costs nothing; boxing it would only change
/// every match on it.
#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum WorkerMessage {
Expand Down Expand Up @@ -1006,6 +1011,11 @@ pub struct TicketMessage {
/// serialized.
#[serde(skip)]
pub team_instructions: String,
/// Set in-process, once per run: what outcomes taught the coder in this
/// repository (its lessons and the prompt variant the coder's bandit
/// picked), for the coder's system prompt. Capped; never serialized.
#[serde(skip)]
pub coder_learning: String,
/// A person's direction that started this run (a reply on the ticket
/// telling CoderHelm where the fix is and to go ahead), as
/// "author: words". The planner's trace checks then inform, not block.
Expand Down Expand Up @@ -1119,6 +1129,7 @@ impl Default for TicketMessage {
ticket_history_note: None,
plan_cache_key: None,
team_instructions: String::new(),
coder_learning: String::new(),
person_direction: None,
answers: Vec::new(),
new_run_id: None,
Expand Down
47 changes: 44 additions & 3 deletions services/common/src/pr_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ pub fn outcome(pr: &Value) -> Outcome {
Outcome::Open
}

/// Commits on a pull request that a person pushed (not Coderhelm, not a bot).
/// Commits on a pull request that a person pushed (not Coderhelm, not a bot)
/// and that change the work itself. A merge commit (GitHub's "Update
/// branch", or a person merging the base in) brings other people's merged
/// work along; counting it made every updated PR look rewritten by people.
pub fn human_commit_count(commits: &[Value]) -> u64 {
human_edit_commits(commits).len() as u64
}

/// The commits [`human_commit_count`] counts, oldest first.
pub fn human_edit_commits(commits: &[Value]) -> Vec<&Value> {
commits
.iter()
.filter(|c| {
Expand All @@ -48,9 +56,26 @@ pub fn human_commit_count(commits: &[Value]) -> u64 {
.or(c["committer"]["login"].as_str())
.unwrap_or("");
let is_bot = c["author"]["type"].as_str() == Some("Bot") || login.ends_with("[bot]");
!login.contains("coderhelm") && !is_bot && !login.is_empty()
!login.contains("coderhelm") && !is_bot && !login.is_empty() && !is_merge_commit(c)
})
.count() as u64
.collect()
}

/// A merge commit: more than one parent.
pub fn is_merge_commit(c: &Value) -> bool {
c["parents"].as_array().is_some_and(|p| p.len() > 1)
}

/// The first line of a commit's message, as people wrote it.
pub fn commit_subject(c: &Value) -> String {
c["commit"]["message"]
.as_str()
.unwrap_or("")
.lines()
.next()
.unwrap_or("")
.trim()
.to_string()
}

/// `(owner, repo, number)` from a PR's html URL.
Expand Down Expand Up @@ -108,6 +133,22 @@ mod tests {
assert_eq!(human_commit_count(&[]), 0);
}

#[test]
fn a_persons_update_branch_merge_is_not_an_edit() {
let commits = vec![
json!({"author":{"login":"coderhelm[bot]","type":"Bot"},"parents":[{"sha":"a"}]}),
// GitHub's "Update branch": authored by the person who pressed it.
json!({"author":{"login":"alice","type":"User"},"parents":[{"sha":"b"},{"sha":"c"}],
"commit":{"message":"Merge branch 'main' into fix"}}),
json!({"author":{"login":"alice","type":"User"},"parents":[{"sha":"d"}],
"commit":{"message":"Use the shared client\n\nThe new one leaked."}}),
];
assert_eq!(human_commit_count(&commits), 1);
let edits = human_edit_commits(&commits);
assert_eq!(commit_subject(edits[0]), "Use the shared client");
assert!(is_merge_commit(&commits[1]));
}

#[test]
fn parses_pr_urls() {
assert_eq!(
Expand Down
Loading
Loading