From a1733b69acd10139df2e0510c6a4e515923626c2 Mon Sep 17 00:00:00 2001 From: Shoyu Date: Tue, 17 Nov 2026 15:00:07 -0500 Subject: [PATCH 1/6] Stop restarting a ticket whose last 3 runs failed while it is unchanged A failed latest run never blocked a ticket event, so every field or status change on a ticket whose runs kept failing started the same failing run again. There was no per-ticket cap. After 3 runs in a row ended failed, an event that leaves the ticket unchanged now starts no further run. The cap counts only runs that ended failed: a run a person retried (status `retried`, whatever it ended as) neither counts nor ends the streak. Changing the ticket, reopening it, or a person's Retry or command still starts a run; explicit triggers never reach the cap. Split out of #706 at the reviewer's request. --- services/gateway/src/routes/trigger_gate.rs | 112 +++++++++++++++++++- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/services/gateway/src/routes/trigger_gate.rs b/services/gateway/src/routes/trigger_gate.rs index 4840306b1..466767467 100644 --- a/services/gateway/src/routes/trigger_gate.rs +++ b/services/gateway/src/routes/trigger_gate.rs @@ -33,7 +33,13 @@ //! run, and the closed run stays as history //! - a ticket a person marked done on the ticket itself ("fixed in the //! booking system") stays done until its content changes -//! - a failed / retried latest run never blocks +//! - a failed / retried latest run doesn't block, until +//! [`MAX_FAILED_RUNS_IN_A_ROW`] runs in a row ended failed (a run a +//! person retried is passed over: it neither counts nor ends the streak): +//! then an event that leaves the ticket unchanged starts nothing (a field +//! or status change used to start the same failing run every time). A +//! content change, a reopen, a person's Retry or command still start one; +//! Retry and commands are never blocked by the cap //! - explicit human triggers (slash commands, comment retries) skip the //! content check but still respect in-flight blocking @@ -75,6 +81,9 @@ struct LatestRun<'a> { context_hash: Option<&'a str>, /// The hash of the newest edit already sent to the run's open PR. seen_hash: Option<&'a str>, + /// Runs in a row, newest first, that ended failed; runs a person + /// retried are passed over (they neither count nor end the streak). + failed_in_a_row: usize, } impl LatestRun<'_> { @@ -122,9 +131,22 @@ enum Decision { ToOpenPr, } +/// Failed runs in a row on one ticket after which an event that leaves the +/// ticket unchanged starts no further run. +pub const MAX_FAILED_RUNS_IN_A_ROW: usize = 3; + fn decide(latest: &LatestRun, new_hash: Option<&str>, reopened: bool) -> Decision { if !handled(latest.status) { - // failed / retried: allow retry + // failed / retried: allow a retry, up to the cap while unchanged + if latest.failed_in_a_row >= MAX_FAILED_RUNS_IN_A_ROW + && !reopened + && !latest.changed(new_hash) + { + return Decision::Skip( + "the ticket's last runs failed and it is unchanged; changing the ticket, \ + reopening it, or a person's explicit Retry or command starts another", + ); + } return Decision::Enqueue; } if latest.pr_open() { @@ -314,6 +336,15 @@ pub async fn gate_ticket_trigger( pr_outcome: s("pr_outcome").unwrap_or(""), context_hash: s("context_hash"), seen_hash: s(SEEN_HASH_FIELD), + failed_in_a_row: failed_in_a_row( + items + .iter() + .map(|i| { + let id = i.get("run_id").and_then(|v| v.as_s().ok()).cloned(); + (id.unwrap_or_default(), status_of(i)) + }) + .collect(), + ), }; match decide(&run, new_context_hash, reopened) { Decision::Enqueue => { @@ -382,6 +413,18 @@ pub async fn gate_ticket_trigger( } } +/// How many of the ticket's runs in a row, newest first, ended failed, +/// from `(run_id, status)` in any order. A run a person retried (`retried`, +/// whatever it ended as) is passed over: it neither counts nor ends the +/// streak. Run ids are ULIDs, which sort by creation time. +fn failed_in_a_row(mut runs: Vec<(String, String)>) -> usize { + runs.sort_by(|a, b| b.0.cmp(&a.0)); + runs.iter() + .filter(|(_, status)| status != "retried") + .take_while(|(_, status)| status == "failed") + .count() +} + /// The run record's field holding the hash of the newest ticket edit sent /// to its open PR. const SEEN_HASH_FIELD: &str = "seen_context_hash"; @@ -458,7 +501,10 @@ pub fn expected_attachment_keys( #[cfg(test)] mod tests { - use super::{decide, edit_note, handled, jira_reopened, Decision, LatestRun, OpenPr}; + use super::{ + decide, edit_note, failed_in_a_row, handled, jira_reopened, Decision, LatestRun, OpenPr, + MAX_FAILED_RUNS_IN_A_ROW, + }; fn run<'a>(status: &'a str, pr: u64, outcome: &'a str, hash: &'a str) -> LatestRun<'a> { LatestRun { @@ -467,9 +513,69 @@ mod tests { pr_outcome: outcome, context_hash: Some(hash), seen_hash: None, + failed_in_a_row: 0, } } + /// The same failing run used to start on every field or status change. + #[test] + fn failed_runs_in_a_row_stop_restarting_on_an_unchanged_ticket() { + let below = LatestRun { + failed_in_a_row: MAX_FAILED_RUNS_IN_A_ROW - 1, + ..run("failed", 0, "", "h1") + }; + assert_eq!(decide(&below, Some("h1"), false), Decision::Enqueue); + let capped = LatestRun { + failed_in_a_row: MAX_FAILED_RUNS_IN_A_ROW, + ..run("failed", 0, "", "h1") + }; + assert!(matches!( + decide(&capped, Some("h1"), false), + Decision::Skip(_) + )); + assert!(matches!(decide(&capped, None, false), Decision::Skip(_))); + // New content, or a reopen, starts one. + assert_eq!(decide(&capped, Some("h2"), false), Decision::Enqueue); + assert_eq!(decide(&capped, Some("h1"), true), Decision::Enqueue); + } + + #[test] + fn failures_in_a_row_count_from_the_newest_run() { + let r = |id: &str, s: &str| (id.to_string(), s.to_string()); + // Out of order on purpose: ids sort by creation time. + assert_eq!( + failed_in_a_row(vec![ + r("01C", "failed"), + r("01A", "failed"), + r("01D", "failed"), + r("01B", "completed"), + ]), + 2 + ); + // Two runs that asked a question and a person retried, then one + // failure: one failed run, not three. + assert_eq!( + failed_in_a_row(vec![ + r("01A", "retried"), + r("01B", "retried"), + r("01C", "failed"), + ]), + 1 + ); + // A retried run between failures doesn't end the streak. + assert_eq!( + failed_in_a_row(vec![ + r("01A", "failed"), + r("01B", "retried"), + r("01C", "failed"), + r("01D", "failed"), + ]), + 3 + ); + assert_eq!(failed_in_a_row(vec![r("01A", "completed")]), 0); + assert_eq!(failed_in_a_row(vec![]), 0); + } + /// The prod duplicate: a Jira edit one second before the merge, while /// auto-merge was armed, started a whole new run. #[test] From cd3029848d298029481730e9b90ca3bf80d413c9 Mon Sep 17 00:00:00 2001 From: Shoyu Date: Wed, 18 Nov 2026 00:23:15 -0500 Subject: [PATCH 2/6] no-mistakes(review): Exempt GitHub trigger-label re-adds from failed-run cap --- services/gateway/src/routes/alerts.rs | 1 + services/gateway/src/routes/github_webhook.rs | 16 +++++++++++--- services/gateway/src/routes/jira_webhook.rs | 1 + services/gateway/src/routes/run_start.rs | 4 +++- services/gateway/src/routes/trigger_gate.rs | 22 ++++++++++++++++--- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/services/gateway/src/routes/alerts.rs b/services/gateway/src/routes/alerts.rs index c3d05d27c..7014b9ee8 100644 --- a/services/gateway/src/routes/alerts.rs +++ b/services/gateway/src/routes/alerts.rs @@ -1534,6 +1534,7 @@ async fn start_run( Some(&context_hash), false, false, + false, ) .await { diff --git a/services/gateway/src/routes/github_webhook.rs b/services/gateway/src/routes/github_webhook.rs index fcc0ba57c..137120bb4 100644 --- a/services/gateway/src/routes/github_webhook.rs +++ b/services/gateway/src/routes/github_webhook.rs @@ -575,7 +575,8 @@ async fn handle_issue_event( // run, re-trigger only if the issue content changed (re-labeling an // edited issue reworks it; re-labeling an unchanged one is a no-op). // Failed/needs_input runs no longer block — the old any-run-exists check - // made GitHub tickets unretriable by re-label. + // made GitHub tickets unretriable by re-label. Re-labeling still retries + // a failed issue, even past the failed-run cap. let ticket_id_str = format!("GH-{}", issue["number"].as_u64().unwrap_or(0)); let context_hash = common::ticket_context_hash( issue["title"].as_str().unwrap_or(""), @@ -589,6 +590,7 @@ async fn handle_issue_event( Some(&context_hash), false, false, + is_labeled, ) .await { @@ -1645,8 +1647,16 @@ async fn handle_issue_comment( // enqueue a duplicate run with no dedup at all. let ticket_id_str = format!("GH-{}", issue["number"].as_u64().unwrap_or(0)); if matches!( - super::trigger_gate::gate_ticket_trigger(state, team_id, &ticket_id_str, None, true, false) - .await, + super::trigger_gate::gate_ticket_trigger( + state, + team_id, + &ticket_id_str, + None, + true, + false, + false, + ) + .await, super::trigger_gate::TicketGate::SkipInFlight ) { // No second run: the run in flight gets the comment (kept, diff --git a/services/gateway/src/routes/jira_webhook.rs b/services/gateway/src/routes/jira_webhook.rs index 038cbe52e..fc6516dcf 100644 --- a/services/gateway/src/routes/jira_webhook.rs +++ b/services/gateway/src/routes/jira_webhook.rs @@ -615,6 +615,7 @@ async fn process_jira_payload( Some(&context_hash), false, super::trigger_gate::jira_reopened(payload), + false, ) .await { diff --git a/services/gateway/src/routes/run_start.rs b/services/gateway/src/routes/run_start.rs index e42c250b5..148bea7d1 100644 --- a/services/gateway/src/routes/run_start.rs +++ b/services/gateway/src/routes/run_start.rs @@ -198,6 +198,7 @@ async fn github_ticket( Some(&hash), true, false, + false, ) .await; if let Some(refused) = gate_refusal(&gate) { @@ -261,7 +262,8 @@ async fn jira_ticket( .item .ok_or_else(|| fail(StatusCode::NOT_FOUND, "That ticket's last run is gone."))?; let gate = - trigger_gate::gate_ticket_trigger(state, &claims.team_id, key, None, true, false).await; + trigger_gate::gate_ticket_trigger(state, &claims.team_id, key, None, true, false, false) + .await; if let Some(refused) = gate_refusal(&gate) { return Err(refused); } diff --git a/services/gateway/src/routes/trigger_gate.rs b/services/gateway/src/routes/trigger_gate.rs index 466767467..956e48d1e 100644 --- a/services/gateway/src/routes/trigger_gate.rs +++ b/services/gateway/src/routes/trigger_gate.rs @@ -38,8 +38,9 @@ //! person retried is passed over: it neither counts nor ends the streak): //! then an event that leaves the ticket unchanged starts nothing (a field //! or status change used to start the same failing run every time). A -//! content change, a reopen, a person's Retry or command still start one; -//! Retry and commands are never blocked by the cap +//! content change, a reopen, a person's Retry or command, or a person +//! adding the trigger label to a GitHub issue still start one; Retry, +//! commands and the label are never blocked by the cap //! - explicit human triggers (slash commands, comment retries) skip the //! content check but still respect in-flight blocking @@ -84,6 +85,9 @@ struct LatestRun<'a> { /// Runs in a row, newest first, that ended failed; runs a person /// retried are passed over (they neither count nor end the streak). failed_in_a_row: usize, + /// The event is a person adding the trigger label, which the cap never + /// blocks. + label_added: bool, } impl LatestRun<'_> { @@ -139,6 +143,7 @@ fn decide(latest: &LatestRun, new_hash: Option<&str>, reopened: bool) -> Decisio if !handled(latest.status) { // failed / retried: allow a retry, up to the cap while unchanged if latest.failed_in_a_row >= MAX_FAILED_RUNS_IN_A_ROW + && !latest.label_added && !reopened && !latest.changed(new_hash) { @@ -203,7 +208,8 @@ fn attr_s(v: &str) -> AttributeValue { /// content; pass `None` when the caller cannot compute it (treated as /// unchanged for completed tickets). `explicit` marks direct human commands, /// which bypass the content check. `reopened`: the event moved the ticket -/// back to an open status ([`jira_reopened`]). +/// back to an open status ([`jira_reopened`]). `label_added`: a person added +/// the trigger label, which the failed-run cap never blocks. /// /// Fails open on query errors — the worker's own active-run guard is the /// backstop, and dropping webhooks during a DynamoDB blip is worse than a @@ -215,6 +221,7 @@ pub async fn gate_ticket_trigger( new_context_hash: Option<&str>, explicit: bool, reopened: bool, + label_added: bool, ) -> TicketGate { let result = state .dynamo @@ -345,6 +352,7 @@ pub async fn gate_ticket_trigger( }) .collect(), ), + label_added, }; match decide(&run, new_context_hash, reopened) { Decision::Enqueue => { @@ -514,6 +522,7 @@ mod tests { context_hash: Some(hash), seen_hash: None, failed_in_a_row: 0, + label_added: false, } } @@ -537,6 +546,13 @@ mod tests { // New content, or a reopen, starts one. assert_eq!(decide(&capped, Some("h2"), false), Decision::Enqueue); assert_eq!(decide(&capped, Some("h1"), true), Decision::Enqueue); + // A person adding the trigger label retries anyway. + let labeled = LatestRun { + label_added: true, + ..capped + }; + assert_eq!(decide(&labeled, Some("h1"), false), Decision::Enqueue); + assert_eq!(decide(&labeled, None, false), Decision::Enqueue); } #[test] From d7cbb42ae8a067d5a2ff5cfd28f4154e0f850af1 Mon Sep 17 00:00:00 2001 From: Shoyu Date: Thu, 19 Nov 2026 00:40:44 -0500 Subject: [PATCH 3/6] Person's retry gestures on GitHub and Jira never reach the failed-run cap (review on #773) --- services/gateway/src/routes/github_webhook.rs | 7 +- services/gateway/src/routes/jira_webhook.rs | 2 +- services/gateway/src/routes/trigger_gate.rs | 129 ++++++++++++++++-- 3 files changed, 122 insertions(+), 16 deletions(-) diff --git a/services/gateway/src/routes/github_webhook.rs b/services/gateway/src/routes/github_webhook.rs index 137120bb4..f560e8210 100644 --- a/services/gateway/src/routes/github_webhook.rs +++ b/services/gateway/src/routes/github_webhook.rs @@ -575,8 +575,9 @@ async fn handle_issue_event( // run, re-trigger only if the issue content changed (re-labeling an // edited issue reworks it; re-labeling an unchanged one is a no-op). // Failed/needs_input runs no longer block — the old any-run-exists check - // made GitHub tickets unretriable by re-label. Re-labeling still retries - // a failed issue, even past the failed-run cap. + // made GitHub tickets unretriable by re-label. Every issue event that + // gets here is a person's retry gesture (the label added, or CoderHelm + // assigned), so it retries a failed issue even past the failed-run cap. let ticket_id_str = format!("GH-{}", issue["number"].as_u64().unwrap_or(0)); let context_hash = common::ticket_context_hash( issue["title"].as_str().unwrap_or(""), @@ -590,7 +591,7 @@ async fn handle_issue_event( Some(&context_hash), false, false, - is_labeled, + super::trigger_gate::github_retry_gesture(payload), ) .await { diff --git a/services/gateway/src/routes/jira_webhook.rs b/services/gateway/src/routes/jira_webhook.rs index fc6516dcf..804ba1f9e 100644 --- a/services/gateway/src/routes/jira_webhook.rs +++ b/services/gateway/src/routes/jira_webhook.rs @@ -615,7 +615,7 @@ async fn process_jira_payload( Some(&context_hash), false, super::trigger_gate::jira_reopened(payload), - false, + super::trigger_gate::jira_retry_gesture(payload, &trigger_label), ) .await { diff --git a/services/gateway/src/routes/trigger_gate.rs b/services/gateway/src/routes/trigger_gate.rs index 956e48d1e..dc3506c5d 100644 --- a/services/gateway/src/routes/trigger_gate.rs +++ b/services/gateway/src/routes/trigger_gate.rs @@ -87,7 +87,7 @@ struct LatestRun<'a> { failed_in_a_row: usize, /// The event is a person adding the trigger label, which the cap never /// blocks. - label_added: bool, + retry_gesture: bool, } impl LatestRun<'_> { @@ -143,7 +143,7 @@ fn decide(latest: &LatestRun, new_hash: Option<&str>, reopened: bool) -> Decisio if !handled(latest.status) { // failed / retried: allow a retry, up to the cap while unchanged if latest.failed_in_a_row >= MAX_FAILED_RUNS_IN_A_ROW - && !latest.label_added + && !latest.retry_gesture && !reopened && !latest.changed(new_hash) { @@ -178,6 +178,48 @@ fn decide(latest: &LatestRun, new_hash: Option<&str>, reopened: bool) -> Decisio } } +/// A person's retry gesture on a GitHub issue: adding the `coderhelm` label, +/// or assigning the issue to CoderHelm. These are the only issue events that +/// start runs, so the failed-run cap never applies to one. +pub fn github_retry_gesture(payload: &serde_json::Value) -> bool { + let action = payload["action"].as_str().unwrap_or(""); + let labeled = action == "labeled" + && payload["label"]["name"] + .as_str() + .is_some_and(|l| l.eq_ignore_ascii_case("coderhelm")); + let assigned = action == "assigned" + && payload["assignee"]["login"] + .as_str() + .is_some_and(|l| l.contains("coderhelm")); + labeled || assigned +} + +/// A person's retry gesture on a Jira ticket, read from the event's +/// changelog like [`jira_reopened`]: the team's trigger label added (in the +/// new labels, not the old), or the ticket assigned to CoderHelm (from +/// someone else). Other field and status changes stay under the failed-run +/// cap. +pub fn jira_retry_gesture(payload: &serde_json::Value, trigger_label: &str) -> bool { + let has_label = |list: Option<&str>| { + list.unwrap_or("") + .split_whitespace() + .any(|l| l.eq_ignore_ascii_case(trigger_label.trim())) + }; + let names_bot = |v: Option<&str>| v.unwrap_or("").to_ascii_lowercase().contains("coderhelm"); + payload + .pointer("/changelog/items") + .and_then(|v| v.as_array()) + .is_some_and(|items| { + items.iter().any(|i| { + let field = i.get("field").and_then(|f| f.as_str()).unwrap_or(""); + let from = i.get("fromString").and_then(|f| f.as_str()); + let to = i.get("toString").and_then(|f| f.as_str()); + (field.eq_ignore_ascii_case("labels") && has_label(to) && !has_label(from)) + || (field.eq_ignore_ascii_case("assignee") && names_bot(to) && !names_bot(from)) + }) + }) +} + /// A Jira update that moved the ticket back to an open status: its changelog /// changed the status, and the status is now outside the "done" category. pub fn jira_reopened(payload: &serde_json::Value) -> bool { @@ -208,8 +250,10 @@ fn attr_s(v: &str) -> AttributeValue { /// content; pass `None` when the caller cannot compute it (treated as /// unchanged for completed tickets). `explicit` marks direct human commands, /// which bypass the content check. `reopened`: the event moved the ticket -/// back to an open status ([`jira_reopened`]). `label_added`: a person added -/// the trigger label, which the failed-run cap never blocks. +/// back to an open status ([`jira_reopened`]). `retry_gesture`: a person's +/// deliberate retry ([`github_retry_gesture`], [`jira_retry_gesture`]: the +/// trigger label added, or the ticket assigned to CoderHelm), which the +/// failed-run cap never blocks. /// /// Fails open on query errors — the worker's own active-run guard is the /// backstop, and dropping webhooks during a DynamoDB blip is worse than a @@ -221,7 +265,7 @@ pub async fn gate_ticket_trigger( new_context_hash: Option<&str>, explicit: bool, reopened: bool, - label_added: bool, + retry_gesture: bool, ) -> TicketGate { let result = state .dynamo @@ -352,7 +396,7 @@ pub async fn gate_ticket_trigger( }) .collect(), ), - label_added, + retry_gesture, }; match decide(&run, new_context_hash, reopened) { Decision::Enqueue => { @@ -428,8 +472,8 @@ pub async fn gate_ticket_trigger( fn failed_in_a_row(mut runs: Vec<(String, String)>) -> usize { runs.sort_by(|a, b| b.0.cmp(&a.0)); runs.iter() - .filter(|(_, status)| status != "retried") - .take_while(|(_, status)| status == "failed") + .filter(|(_, status)| status != common::run_status::RETRIED) + .take_while(|(_, status)| status == common::run_status::FAILED) .count() } @@ -510,8 +554,8 @@ pub fn expected_attachment_keys( #[cfg(test)] mod tests { use super::{ - decide, edit_note, failed_in_a_row, handled, jira_reopened, Decision, LatestRun, OpenPr, - MAX_FAILED_RUNS_IN_A_ROW, + decide, edit_note, failed_in_a_row, github_retry_gesture, handled, jira_reopened, + jira_retry_gesture, Decision, LatestRun, OpenPr, MAX_FAILED_RUNS_IN_A_ROW, }; fn run<'a>(status: &'a str, pr: u64, outcome: &'a str, hash: &'a str) -> LatestRun<'a> { @@ -522,7 +566,7 @@ mod tests { context_hash: Some(hash), seen_hash: None, failed_in_a_row: 0, - label_added: false, + retry_gesture: false, } } @@ -548,7 +592,7 @@ mod tests { assert_eq!(decide(&capped, Some("h1"), true), Decision::Enqueue); // A person adding the trigger label retries anyway. let labeled = LatestRun { - label_added: true, + retry_gesture: true, ..capped }; assert_eq!(decide(&labeled, Some("h1"), false), Decision::Enqueue); @@ -684,6 +728,67 @@ mod tests { ); } + /// Every GitHub issue event that starts a run is a person's retry + /// gesture, so the failed-run cap never blocks one. + #[test] + fn github_label_adds_and_bot_assigns_are_retry_gestures() { + let ev = |action: &str, label: &str, assignee: &str| { + serde_json::json!({ + "action": action, + "label": {"name": label}, + "assignee": {"login": assignee} + }) + }; + assert!(github_retry_gesture(&ev("labeled", "CoderHelm", ""))); + assert!(github_retry_gesture(&ev("assigned", "", "coderhelm[bot]"))); + assert!(!github_retry_gesture(&ev("labeled", "bug", ""))); + assert!(!github_retry_gesture(&ev("assigned", "", "pat"))); + assert!(!github_retry_gesture(&ev("edited", "coderhelm", ""))); + } + + /// On Jira, re-adding the trigger label or re-assigning to CoderHelm is + /// a retry; a field or status change isn't. + #[test] + fn jira_relabels_and_reassigns_are_retry_gestures() { + let ev = |field: &str, from: &str, to: &str| { + serde_json::json!({"changelog": {"items": [ + {"field": field, "fromString": from, "toString": to} + ]}}) + }; + assert!(jira_retry_gesture( + &ev("labels", "backend", "backend coderhelm"), + "coderhelm" + )); + assert!(jira_retry_gesture(&ev("labels", "", "AI"), "ai")); + assert!(jira_retry_gesture( + &ev("assignee", "Pat Doe", "CoderHelm"), + "coderhelm" + )); + // Still labelled, or still assigned: no new gesture. + assert!(!jira_retry_gesture( + &ev("labels", "coderhelm", "coderhelm backend"), + "coderhelm" + )); + assert!(!jira_retry_gesture( + &ev("assignee", "CoderHelm", "CoderHelm"), + "coderhelm" + )); + // Label removed, another field, a status move: capped. + assert!(!jira_retry_gesture( + &ev("labels", "coderhelm", ""), + "coderhelm" + )); + assert!(!jira_retry_gesture( + &ev("priority", "Low", "High"), + "coderhelm" + )); + assert!(!jira_retry_gesture( + &ev("status", "To Do", "In Progress"), + "coderhelm" + )); + assert!(!jira_retry_gesture(&serde_json::json!({}), "coderhelm")); + } + #[test] fn a_reopen_is_a_status_change_out_of_done() { let ev = |cat: &str, field: &str| { From 40f5c9c7faf17e214f33acb01c006bb7cfbd5131 Mon Sep 17 00:00:00 2001 From: Shoyu Date: Thu, 19 Nov 2026 01:21:36 -0500 Subject: [PATCH 4/6] One trigger-label rule for Jira, one retry-gesture rule for GitHub --- services/gateway/src/routes/github_webhook.rs | 23 +++-------- services/gateway/src/routes/jira_webhook.rs | 7 ++-- services/gateway/src/routes/trigger_gate.rs | 40 +++++++++++++++---- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/services/gateway/src/routes/github_webhook.rs b/services/gateway/src/routes/github_webhook.rs index f560e8210..c3a1739d8 100644 --- a/services/gateway/src/routes/github_webhook.rs +++ b/services/gateway/src/routes/github_webhook.rs @@ -519,22 +519,11 @@ async fn handle_issue_event( installation_id: u64, team_id: &str, ) -> Result { - let action = payload["action"].as_str().unwrap_or(""); - - // Trigger on: issue assigned to coderhelm[bot], or labeled "coderhelm" - let is_assigned_to_bot = action == "assigned" - && payload["assignee"]["login"] - .as_str() - .map(|l| l.contains("coderhelm")) - .unwrap_or(false); - - let is_labeled = action == "labeled" - && payload["label"]["name"] - .as_str() - .map(|l| l.eq_ignore_ascii_case("coderhelm")) - .unwrap_or(false); - - if !is_assigned_to_bot && !is_labeled { + // Trigger on: issue assigned to coderhelm[bot], or labeled "coderhelm". + // One rule for the trigger and for the failed-run cap, which never + // blocks a person's retry gesture. + let retry_gesture = super::trigger_gate::github_retry_gesture(payload); + if !retry_gesture { return Ok(StatusCode::OK); } let issue = &payload["issue"]; @@ -591,7 +580,7 @@ async fn handle_issue_event( Some(&context_hash), false, false, - super::trigger_gate::github_retry_gesture(payload), + retry_gesture, ) .await { diff --git a/services/gateway/src/routes/jira_webhook.rs b/services/gateway/src/routes/jira_webhook.rs index 804ba1f9e..c03aed786 100644 --- a/services/gateway/src/routes/jira_webhook.rs +++ b/services/gateway/src/routes/jira_webhook.rs @@ -356,10 +356,9 @@ fn assignee_of(issue: &Value) -> &str { /// CoderHelm). Either one starts a run, and either one is what lets a /// comment written before the run be kept for it. pub(crate) fn ticket_triggers(issue: &Value, trigger_label: &str) -> (bool, bool) { - let label = trigger_label.to_ascii_lowercase(); - let has_label = labels_of(issue).iter().any(|l| { - l.eq_ignore_ascii_case(&label) || l.to_ascii_lowercase().starts_with(&format!("{label}:")) - }); + let has_label = labels_of(issue) + .iter() + .any(|l| super::trigger_gate::is_trigger_label(l, trigger_label)); let is_assigned = assignee_of(issue) .to_ascii_lowercase() .contains("coderhelm"); diff --git a/services/gateway/src/routes/trigger_gate.rs b/services/gateway/src/routes/trigger_gate.rs index dc3506c5d..9f2dd744d 100644 --- a/services/gateway/src/routes/trigger_gate.rs +++ b/services/gateway/src/routes/trigger_gate.rs @@ -38,9 +38,11 @@ //! person retried is passed over: it neither counts nor ends the streak): //! then an event that leaves the ticket unchanged starts nothing (a field //! or status change used to start the same failing run every time). A -//! content change, a reopen, a person's Retry or command, or a person -//! adding the trigger label to a GitHub issue still start one; Retry, -//! commands and the label are never blocked by the cap +//! content change, a reopen, a person's Retry or command, or a person's +//! retry gesture still start one: on GitHub adding the trigger label or +//! assigning CoderHelm, on Jira re-adding the trigger label or +//! re-assigning to CoderHelm. Retry, commands and those gestures are +//! never blocked by the cap //! - explicit human triggers (slash commands, comment retries) skip the //! content check but still respect in-flight blocking @@ -85,8 +87,8 @@ struct LatestRun<'a> { /// Runs in a row, newest first, that ended failed; runs a person /// retried are passed over (they neither count nor end the streak). failed_in_a_row: usize, - /// The event is a person adding the trigger label, which the cap never - /// blocks. + /// The event is a person's retry gesture ([`github_retry_gesture`], + /// [`jira_retry_gesture`]), which the cap never blocks. retry_gesture: bool, } @@ -194,6 +196,16 @@ pub fn github_retry_gesture(payload: &serde_json::Value) -> bool { labeled || assigned } +/// Whether `label` is the team's trigger label: the label itself (case +/// ignored), or the label with a repository pinned after it +/// (`coderhelm:acme/web`). One rule for the Jira trigger and for +/// [`jira_retry_gesture`]. +pub fn is_trigger_label(label: &str, trigger_label: &str) -> bool { + let trigger = trigger_label.trim().to_ascii_lowercase(); + let label = label.trim().to_ascii_lowercase(); + !trigger.is_empty() && (label == trigger || label.starts_with(&format!("{trigger}:"))) +} + /// A person's retry gesture on a Jira ticket, read from the event's /// changelog like [`jira_reopened`]: the team's trigger label added (in the /// new labels, not the old), or the ticket assigned to CoderHelm (from @@ -203,7 +215,7 @@ pub fn jira_retry_gesture(payload: &serde_json::Value, trigger_label: &str) -> b let has_label = |list: Option<&str>| { list.unwrap_or("") .split_whitespace() - .any(|l| l.eq_ignore_ascii_case(trigger_label.trim())) + .any(|l| is_trigger_label(l, trigger_label)) }; let names_bot = |v: Option<&str>| v.unwrap_or("").to_ascii_lowercase().contains("coderhelm"); payload @@ -554,8 +566,8 @@ pub fn expected_attachment_keys( #[cfg(test)] mod tests { use super::{ - decide, edit_note, failed_in_a_row, github_retry_gesture, handled, jira_reopened, - jira_retry_gesture, Decision, LatestRun, OpenPr, MAX_FAILED_RUNS_IN_A_ROW, + decide, edit_note, failed_in_a_row, github_retry_gesture, handled, is_trigger_label, + jira_reopened, jira_retry_gesture, Decision, LatestRun, OpenPr, MAX_FAILED_RUNS_IN_A_ROW, }; fn run<'a>(status: &'a str, pr: u64, outcome: &'a str, hash: &'a str) -> LatestRun<'a> { @@ -764,6 +776,18 @@ mod tests { &ev("assignee", "Pat Doe", "CoderHelm"), "coderhelm" )); + // The repo-pinned form of the trigger label counts too. + assert!(jira_retry_gesture( + &ev("labels", "backend", "backend coderhelm:acme/web"), + "coderhelm" + )); + assert!(!jira_retry_gesture( + &ev("labels", "coderhelm:acme/web", "coderhelm:acme/web backend"), + "coderhelm" + )); + assert!(is_trigger_label("CoderHelm", "coderhelm")); + assert!(is_trigger_label("coderhelm:acme/web", "coderhelm")); + assert!(!is_trigger_label("coderhelmish", "coderhelm")); // Still labelled, or still assigned: no new gesture. assert!(!jira_retry_gesture( &ev("labels", "coderhelm", "coderhelm backend"), From ab7713abd2abae22706703a39992f1b802942995 Mon Sep 17 00:00:00 2001 From: Shoyu Date: Thu, 19 Nov 2026 09:38:01 -0500 Subject: [PATCH 5/6] no-mistakes(test): Stop Jira status moves from skipping the failed-run cap --- services/gateway/src/routes/trigger_gate.rs | 26 +++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/services/gateway/src/routes/trigger_gate.rs b/services/gateway/src/routes/trigger_gate.rs index 9f2dd744d..310566d42 100644 --- a/services/gateway/src/routes/trigger_gate.rs +++ b/services/gateway/src/routes/trigger_gate.rs @@ -37,9 +37,9 @@ //! [`MAX_FAILED_RUNS_IN_A_ROW`] runs in a row ended failed (a run a //! person retried is passed over: it neither counts nor ends the streak): //! then an event that leaves the ticket unchanged starts nothing (a field -//! or status change used to start the same failing run every time). A -//! content change, a reopen, a person's Retry or command, or a person's -//! retry gesture still start one: on GitHub adding the trigger label or +//! or status change used to start the same failing run every time; no +//! status change skips the cap, a reopen included). A content change, a +//! person's Retry or command, or a person's retry gesture still start one: on GitHub adding the trigger label or //! assigning CoderHelm, on Jira re-adding the trigger label or //! re-assigning to CoderHelm. Retry, commands and those gestures are //! never blocked by the cap @@ -146,12 +146,11 @@ fn decide(latest: &LatestRun, new_hash: Option<&str>, reopened: bool) -> Decisio // failed / retried: allow a retry, up to the cap while unchanged if latest.failed_in_a_row >= MAX_FAILED_RUNS_IN_A_ROW && !latest.retry_gesture - && !reopened && !latest.changed(new_hash) { return Decision::Skip( "the ticket's last runs failed and it is unchanged; changing the ticket, \ - reopening it, or a person's explicit Retry or command starts another", + or a person's explicit Retry or command starts another", ); } return Decision::Enqueue; @@ -599,9 +598,22 @@ mod tests { Decision::Skip(_) )); assert!(matches!(decide(&capped, None, false), Decision::Skip(_))); - // New content, or a reopen, starts one. + // New content starts one. assert_eq!(decide(&capped, Some("h2"), false), Decision::Enqueue); - assert_eq!(decide(&capped, Some("h1"), true), Decision::Enqueue); + assert_eq!(decide(&capped, Some("h2"), true), Decision::Enqueue); + // No status move skips the cap: To Do -> In Progress, or Done -> To Do. + let status_move = |cat: &str| { + jira_reopened(&serde_json::json!({ + "changelog": {"items": [{"field": "status"}]}, + "issue": {"fields": {"status": {"statusCategory": {"key": cat}}}} + })) + }; + for cat in ["indeterminate", "new"] { + assert!(matches!( + decide(&capped, Some("h1"), status_move(cat)), + Decision::Skip(_) + )); + } // A person adding the trigger label retries anyway. let labeled = LatestRun { retry_gesture: true, From 79f8bf653d77b7d31f919361d1643d542757e0e8 Mon Sep 17 00:00:00 2001 From: Shoyu Date: Thu, 19 Nov 2026 09:52:31 -0500 Subject: [PATCH 6/6] no-mistakes(document): Reflow failed-run cap note in trigger gate doc --- services/gateway/src/routes/trigger_gate.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/gateway/src/routes/trigger_gate.rs b/services/gateway/src/routes/trigger_gate.rs index 310566d42..0c011a709 100644 --- a/services/gateway/src/routes/trigger_gate.rs +++ b/services/gateway/src/routes/trigger_gate.rs @@ -39,9 +39,9 @@ //! then an event that leaves the ticket unchanged starts nothing (a field //! or status change used to start the same failing run every time; no //! status change skips the cap, a reopen included). A content change, a -//! person's Retry or command, or a person's retry gesture still start one: on GitHub adding the trigger label or -//! assigning CoderHelm, on Jira re-adding the trigger label or -//! re-assigning to CoderHelm. Retry, commands and those gestures are +//! person's Retry or command, or a person's retry gesture still starts +//! one: on GitHub adding the trigger label or assigning CoderHelm, on +//! Jira re-adding the trigger label or re-assigning to CoderHelm. Retry, commands and those gestures are //! never blocked by the cap //! - explicit human triggers (slash commands, comment retries) skip the //! content check but still respect in-flight blocking