From e8898c75c1751c16169b19bdbfbed896e5e39ae8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 18:30:30 +0000 Subject: [PATCH 1/2] refactor: split the four over-complex functions; SQ-RUN-03 dotted hosts only; SQ-OWN-16 defers to Hypatia Sonar rust:S3776 (cognitive complexity over 30) on main: self_sha_lock_records (41), unpinned_setup_bun (47), code_fix_is_safe (61), triage run_inner (36), plus rust:S1612 (closure replaced by u8::is_ascii_digit). Each is split into small named functions with the same behaviour: - self_sha_lock_records: self_sha_record_key, flag_if_self_ref. - unpinned_setup_bun: setup_bun_uses, step_start, step_end, setup_bun_step_unpinned, bun_version_is_loose. - code_fix_is_safe: a DiffScan with file_header, end_hunk, removed_line, added_line; BANNED_ADDITIONS and LOWERED_THRESHOLD are constants. - triage: parse_args, Findings (scan_log, scan_lock, scan_workflow), build_rows, print_rows. Behaviour check: the CLI built from main and the CLI built from this branch give identical stdout, stderr and exit code on 19 inputs (logs, locks, callers files, workflows, JSON, every argument error, ESTATE_AUTOFIX on). The 202 squabble-core tests pass unchanged apart from one new test. Two small rule changes agreed with the rule reconciliation session: - SQ-RUN-03 matches 'Could not resolve host' and 'Failed to connect to' only for a host name that contains a dot; a service container or localhost is a configuration error a re-run cannot fix. Tests cover both forms. - SQ-OWN-16 says 'as Hypatia WF030 defines it' and no longer restates the guard test, so the definition lives only in Hypatia. fmt, clippy -D warnings and the workspace tests pass. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zmiut2nCcbtEx25HAU --- crates/squabble-cli/src/triage.rs | 134 ++++-- .../squabble-core/data/first-line-rules.tsv | 4 +- crates/squabble-core/src/rules.rs | 397 +++++++++++------- .../0004-first-line-rule-catalogue.adoc | 6 +- 4 files changed, 332 insertions(+), 209 deletions(-) diff --git a/crates/squabble-cli/src/triage.rs b/crates/squabble-cli/src/triage.rs index b5b2935..9a7d2ab 100644 --- a/crates/squabble-cli/src/triage.rs +++ b/crates/squabble-cli/src/triage.rs @@ -46,50 +46,71 @@ pub fn run(args: &[String]) -> ExitCode { } } -fn run_inner(args: &[String]) -> Result<(), String> { - let (mut log, mut step, mut lock, mut callers, mut workflow) = (None, None, None, None, None); - let mut json = false; +/// What the command line asked for. +#[derive(Default)] +struct Args { + log: Option, + step: Option, + lock: Option, + callers: Option, + workflow: Option, + json: bool, +} + +fn parse_args(args: &[String]) -> Result { + let mut a = Args::default(); let mut it = args.iter(); - while let Some(a) = it.next() { + while let Some(arg) = it.next() { let mut val = |name: &str| it.next().cloned().ok_or(format!("{name} needs a value")); - match a.as_str() { - "--log" => log = Some(val("--log")?), - "--step" => step = Some(val("--step")?), - "--lock" => lock = Some(val("--lock")?), - "--callers" => callers = Some(val("--callers")?), - "--workflow" => workflow = Some(val("--workflow")?), - "--json" => json = true, + match arg.as_str() { + "--log" => a.log = Some(val("--log")?), + "--step" => a.step = Some(val("--step")?), + "--lock" => a.lock = Some(val("--lock")?), + "--callers" => a.callers = Some(val("--callers")?), + "--workflow" => a.workflow = Some(val("--workflow")?), + "--json" => a.json = true, other => return Err(format!("unknown argument `{other}`")), } } - if log.is_none() && lock.is_none() && workflow.is_none() { + if a.log.is_none() && a.lock.is_none() && a.workflow.is_none() { return Err("give at least one of --log, --lock, --workflow".into()); } - if step.is_some() && log.is_none() { + if a.step.is_some() && a.log.is_none() { return Err("--step needs --log".into()); } - if callers.is_some() && lock.is_none() { + if a.callers.is_some() && a.lock.is_none() { return Err("--callers needs --lock".into()); } + Ok(a) +} + +/// The rules found, in discovery order, one entry per rule id, each with an +/// optional detail line; and the last lines of the log, for a referral. +#[derive(Default)] +struct Findings { + found: Vec<(&'static str, Option)>, + signature: Option, +} - // (rule id, detail) in discovery order, one row per rule id. - let mut signature: Option = None; - let mut found: Vec<(&'static str, Option)> = Vec::new(); - let mut add = |id: &'static str, detail: Option| { - if !found.iter().any(|(i, _)| *i == id) { - found.push((id, detail)); +impl Findings { + fn add(&mut self, id: &'static str, detail: Option) { + if !self.found.iter().any(|(i, _)| *i == id) { + self.found.push((id, detail)); } - }; - if let Some(p) = &log { - let text = read(p)?; - signature = Some(tail_lines(&text, 3)); - for id in match_log(&text, step.as_deref()) { - add(id, None); + } + + fn scan_log(&mut self, path: &str, step: Option<&str>) -> Result<(), String> { + let text = read(path)?; + self.signature = Some(tail_lines(&text, 3)); + for id in match_log(&text, step) { + self.add(id, None); } + Ok(()) } - if let Some(p) = &lock { - let text = read(p)?; - let callers: Vec = match &callers { + + fn scan_lock(&mut self, path: &str, callers_path: Option<&str>) -> Result<(), String> { + let text = read(path)?; + let callers: Vec = match callers_path { Some(c) => read(c)? .lines() .map(str::trim) @@ -100,23 +121,25 @@ fn run_inner(args: &[String]) -> Result<(), String> { }; let flagged = self_sha_lock_records(&text, &callers); if !flagged.is_empty() { - add( + self.add( "SQ-LOCK-01", Some(format!("self-SHA records: {}", flagged.join(", "))), ); } + Ok(()) } - if let Some(p) = &workflow { - let text = read(p)?; + + fn scan_workflow(&mut self, path: &str) -> Result<(), String> { + let text = read(path)?; if unpinned_setup_bun(&text) { - add( + self.add( "SQ-PIN-03", Some("setup-bun step without an exact bun-version".into()), ); } let jobs = always_after_cancel(&text); if !jobs.is_empty() { - add( + self.add( "SQ-GEN-03", Some(format!( "if: always() after needs, downloading an artifact: {}", @@ -124,32 +147,37 @@ fn run_inner(args: &[String]) -> Result<(), String> { )), ); } + Ok(()) } +} +/// One row per catalogue rule found, with its guard decision and, for a +/// referral, the message Hypatia would receive. +fn build_rows(findings: Findings, step: Option<&str>) -> Vec { let guards = GuardInput { autofix: std::env::var("ESTATE_AUTOFIX").ok(), ..GuardInput::default() }; - let rows: Vec = found + let Findings { found, signature } = findings; + found .into_iter() .filter_map(|(id, detail)| { let rule = rules::rule(id)?; let decision = decide(rule, &guards); - let referral = match (&decision, rule.class) { - (Decision::Refer { .. }, _) => Some(referral_message( + let referral = matches!(decision, Decision::Refer { .. }).then(|| { + referral_message( &Referral { rule_id: rule.id.to_string(), problem: rule.summary.to_string(), fix: Some(rule.fix.to_string()), risk: rule.refer_when.to_string(), - step_name: step.clone(), + step_name: step.map(String::from), signature: signature.clone(), ..Referral::default() }, false, - )), - _ => None, - }; + ) + }); Some(Row { rule: id, class: rule.class, @@ -158,17 +186,19 @@ fn run_inner(args: &[String]) -> Result<(), String> { referral, }) }) - .collect(); + .collect() +} +fn print_rows(rows: &[Row], json: bool) -> Result<(), String> { if json { - let s = serde_json::to_string_pretty(&rows).map_err(|e| e.to_string())?; + let s = serde_json::to_string_pretty(rows).map_err(|e| e.to_string())?; println!("{s}"); return Ok(()); } if rows.is_empty() { println!("no catalogue rule matched"); } - for r in &rows { + for r in rows { println!("{}\t{:?}\t{:?}", r.rule, r.class, r.decision); if let Some(d) = &r.detail { println!(" {d}"); @@ -181,3 +211,19 @@ fn run_inner(args: &[String]) -> Result<(), String> { } Ok(()) } + +fn run_inner(args: &[String]) -> Result<(), String> { + let a = parse_args(args)?; + let mut findings = Findings::default(); + if let Some(p) = &a.log { + findings.scan_log(p, a.step.as_deref())?; + } + if let Some(p) = &a.lock { + findings.scan_lock(p, a.callers.as_deref())?; + } + if let Some(p) = &a.workflow { + findings.scan_workflow(p)?; + } + let rows = build_rows(findings, a.step.as_deref()); + print_rows(&rows, a.json) +} diff --git a/crates/squabble-core/data/first-line-rules.tsv b/crates/squabble-core/data/first-line-rules.tsv index 7be8684..7c65871 100644 --- a/crates/squabble-core/data/first-line-rules.tsv +++ b/crates/squabble-core/data/first-line-rules.tsv @@ -35,7 +35,7 @@ SQ-CANON-01 FIX an instance's canon-lockstep job is an old inline copy and goes SQ-PERM-01 FIX Resource not accessible by integration at get-a-workflow-run in a job that uploads SARIF on a private repository Add actions: read to that job's permissions, with a comment that names the reason The upload step passes on a re-run The permission is already present, or the repository is public SQ-RUN-01 ACT a setup or download step fails with API rate limit exceeded and the same workflow passes on an adjacent commit Re-run the failed jobs once after the rate-limit reset, only if that commit is still the head The re-run passes It fails twice on the same commit, the log has no rate-limit text, or the step is not a setup or download step SQ-RUN-02 ACT a PR is blocked with required checks green under a strict ruleset and its base is behind the default branch Update the branch by merging the base; never rebase a branch the squabbler did not open The required checks pass on the new head Merge conflict, or still blocked on an up-to-date head -SQ-RUN-03 ACT a step fails on a transient host or API error: gh: HTTP 5xx, gh: Server Error (HTTP 5xx), a git remote returning 5xx, Could not resolve host, or Failed to connect Re-run the failed job once, only if that commit is still the head The re-run passes It fails twice on the same commit, or the status is 401 or 403 (a token problem: SQ-OWN-09, or a rate limit: SQ-RUN-01) +SQ-RUN-03 ACT a step fails on a transient host or API error: gh: HTTP 5xx, gh: Server Error (HTTP 5xx), a git remote returning 5xx, or Could not resolve host or Failed to connect to a host name that contains a dot Re-run the failed job once, only if that commit is still the head The re-run passes It fails twice on the same commit, the status is 401 or 403 (a token problem: SQ-OWN-09, or a rate limit: SQ-RUN-01), or the host name has no dot (a service container or localhost is a configuration error a re-run cannot fix) SQ-RUN-04 ACT gitleaks fails with fatal: ambiguous argument ^..: unknown revision or path not in the working tree, because a shallow checkout lacks the range base commit; trufflehog reports commits: 0 for the same range Re-run the failed job once, only if that commit is still the head The re-run passes and the scan covers more than 0 commits It fails twice on the same commit: refer with the suggestion fetch-depth: 0 on the scan checkout SQ-CODE-01 FIX a Rust readiness or integration test cannot find its binary under target/debug/build Use PathBuf::from(env!("CARGO_BIN_EXE_")); a code fix, first-line only when the code-fix guard passes cargo test --test readiness passes The fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head SQ-CODE-02 FIX Validate Hypatia Baseline fails on new code_safety findings Fix the code behind the finding; never touch .hypatia-baseline.json The baseline job passes with the baseline file unchanged The fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head @@ -58,7 +58,7 @@ SQ-OWN-12 OWNER the same scanner (panic-attack) is pinned at more than one commi SQ-OWN-13 OWNER a workflow push is refused by a repository ruleset: gh: Repository rule violations found / Code coverage checks require a pull request. (HTTP 409) (Apodeixis.jl run 43257204071, Publish the lane badges, red on its base too) Report: a ruleset requires a pull request for that push, so the workflow cannot push to the protected branch. The owner changes the workflow to open a PR or adjusts the ruleset. The squabbler never changes a ruleset and never pushes around one (owner) (owner) SQ-OWN-14 OWNER : non-v8 UUID literal () / UUID check failed. Use ESTATE-UUID-V8 (ADR-008) (Apodeixis.jl run 43257204302, ./Project.toml) Report: an identifier needs the owner or the A7 identity process. The squabbler never mints, rewrites or retypes a UUID (owner) (owner) SQ-OWN-15 OWNER SonarQube ERROR Fail to load the list of plugins installed on the server: Not authorized or organization not found, then sonar-scanner failed with exit code 3 (lithoglyph run 43152462261) Report: the Sonar token or organization is missing or wrong; the owner fixes the secret or the Sonar project. Never remove or skip the Sonar step (owner) (owner) -SQ-OWN-16 OWNER a workflow force- or mirror-pushes to a forge, gated on a *_MIRROR_ENABLED variable, with no check of the source repository visibility anywhere in the file (Hypatia WF030, found statically: standards mirror-reusable.yml gates the job at line 93 and runs git push --force gitlab main at line 144; github-backup-mirror-reusable.yml too) Report: the owner decides the guard (refuse, or skip loudly, when the source is private and the forge copy is not confirmed private; escalate when the forge visibility cannot be read). The squabbler never edits a mirror workflow, never pushes to a forge and never changes a visibility setting (owner) (owner) +SQ-OWN-16 OWNER a workflow force- or mirror-pushes to a forge, gated on a *_MIRROR_ENABLED variable, as Hypatia WF030 defines it (found statically, the definition is Hypatia's and not restated here: standards mirror-reusable.yml gates the job at line 93 and runs git push --force gitlab main at line 144; github-backup-mirror-reusable.yml too) Report: the owner decides the guard (refuse, or skip loudly, when the source is private and the forge copy is not confirmed private; escalate when the forge visibility cannot be read). The squabbler never edits a mirror workflow, never pushes to a forge and never changes a visibility setting (owner) (owner) SQ-NOI-01 NOISE Fork governance failed (deed=cancelled) on a head that a newer commit replaced Label superseded; read the newest head The newest head is not deed=cancelled deed=cancelled on the newest head SQ-NOI-02 NOISE couldn't find remote ref refs/pull//merge and the PR closed before the job started None: do not re-run a closed PR The PR API shows the PR closed The PR is still open (conflict or deleted ref) SQ-NOI-03 NOISE Inbox Steward ends startup_failure only on runs triggered by chatgpt-codex-connector[bot] None Count of startup failures by actor Inbox Steward becomes a required check diff --git a/crates/squabble-core/src/rules.rs b/crates/squabble-core/src/rules.rs index a10e76b..f09fc55 100644 --- a/crates/squabble-core/src/rules.rs +++ b/crates/squabble-core/src/rules.rs @@ -151,6 +151,18 @@ impl LogCtx<'_> { self.has("API rate limit exceeded") } + /// A host name that contains a dot right after `marker`, as in + /// `Could not resolve host: git.sr.ht`. A name without a dot (a service + /// container, `localhost`) is a configuration error, not a transient one. + fn dotted_name_after(&self, marker: &str) -> bool { + self.log.match_indices(marker).any(|(at, _)| { + let rest = &self.log[at + marker.len()..]; + let name = rest.split_whitespace().next().unwrap_or(""); + name.trim_end_matches(|c: char| !c.is_ascii_alphanumeric()) + .contains('.') + }) + } + /// A status of 500 to 599 right after `marker`, as in `gh: HTTP 502`. fn http_5xx_after(&self, marker: &str) -> bool { let bytes = self.log.as_bytes(); @@ -190,8 +202,8 @@ static LOG_MATCHERS: &[LogMatcher] = &[ c.http_5xx_after("gh: HTTP ") || c.http_5xx_after("gh: Server Error (HTTP ") || c.http_5xx_after("returned error: ") - || c.has("Could not resolve host") - || c.has("Failed to connect to") + || c.dotted_name_after("Could not resolve host: ") + || c.dotted_name_after("Failed to connect to ") }), ("SQ-RUN-04", |c| { c.has("ambiguous argument") && c.has("unknown revision or path not in the working tree") @@ -291,43 +303,50 @@ pub fn self_sha_lock_records(lock_yaml: &str, job_level_callers: &[String]) -> V continue; } let ind = indent_of(line); - if let Some((_, key_ind)) = ¤t { - if ind <= *key_ind { - current = None; - } - } - if current.is_none() && !t.starts_with('-') { - if let Some(k) = t.strip_suffix(':') { - let k = k.trim().trim_matches(|c| c == '\'' || c == '"'); - if let Some((repo, sha)) = k.split_once('@') { - if repo.contains('/') && is_sha40(sha) { - current = Some(( - format!("{}@{}", repo.to_ascii_lowercase(), sha.to_ascii_lowercase()), - ind, - )); - } - } - } - continue; + if current.as_ref().is_some_and(|(_, key_ind)| ind <= *key_ind) { + current = None; } - if let Some((key, key_ind)) = ¤t { + match ¤t { + None => current = self_sha_record_key(t).map(|key| (key, ind)), // Direct children only: a nested `uses:` list never carries `ref:`. - if ind > *key_ind { - if let Some(v) = t.strip_prefix("ref:") { - let sha = key.split_once('@').map(|(_, s)| s).unwrap_or(""); - if unquote(v).eq_ignore_ascii_case(sha) - && !callers.contains(key) - && !flagged.contains(key) - { - flagged.push(key.clone()); - } - } + Some((key, key_ind)) if ind > *key_ind => { + flag_if_self_ref(key, t, &callers, &mut flagged); } + Some(_) => {} } } flagged } +/// The lowercased `owner/repo@<40-hex>` key of a lock record opened by this +/// line, or None for any other line (a list item, or a key of another shape). +fn self_sha_record_key(trimmed: &str) -> Option { + if trimmed.starts_with('-') { + return None; + } + let key = trimmed + .strip_suffix(':')? + .trim() + .trim_matches(|c| c == '\'' || c == '"'); + let (repo, sha) = key.split_once('@')?; + (repo.contains('/') && is_sha40(sha)) + .then(|| format!("{}@{}", repo.to_ascii_lowercase(), sha.to_ascii_lowercase())) +} + +/// Record KEY in `flagged` when this `ref:` line repeats the SHA in the key +/// and no job-level reusable caller names the key. +fn flag_if_self_ref(key: &str, trimmed: &str, callers: &[String], flagged: &mut Vec) { + let Some(value) = trimmed.strip_prefix("ref:") else { + return; + }; + let sha = key.split_once('@').map_or("", |(_, s)| s); + let is_caller = callers.iter().any(|c| c == key); + let already = flagged.iter().any(|f| f == key); + if unquote(value).eq_ignore_ascii_case(sha) && !is_caller && !already { + flagged.push(key.to_string()); + } +} + /// A job in a workflow file: its id and the lines under it. struct JobBlock<'a> { id: &'a str, @@ -415,72 +434,84 @@ fn runs_after_cancel_and_downloads(job: &JobBlock<'_>) -> bool { /// is missing, `latest`, `canary`, a range, or which uses `bun-version-file`. pub fn unpinned_setup_bun(workflow_yaml: &str) -> bool { let lines: Vec<&str> = workflow_yaml.lines().collect(); - for (i, line) in lines.iter().enumerate() { - let t = line.trim_start(); - if t.starts_with('#') { - continue; - } - let (starts_dash, body) = match t.strip_prefix("- ") { - Some(b) => (true, b.trim_start()), - None => (false, t), + lines.iter().enumerate().any(|(i, line)| { + let Some((key_ind, opens_item)) = setup_bun_uses(line) else { + return false; }; - if !body.starts_with("uses:") || !body.contains("oven-sh/setup-bun@") { - continue; - } - let key_ind = indent_of(line) + if starts_dash { t.len() - body.len() } else { 0 }; - let mut start = i; - if !starts_dash { - while start > 0 { - let p = lines[start - 1]; - if p.trim().is_empty() { - start -= 1; - continue; - } - if indent_of(p) < key_ind { - if p.trim_start().starts_with("- ") { - start -= 1; - } - break; - } + let start = if opens_item { + i + } else { + step_start(&lines, i, key_ind) + }; + let end = step_end(&lines, i, key_ind); + setup_bun_step_unpinned(&lines[start..end]) + }) +} + +/// For a `uses: oven-sh/setup-bun@…` line: the indent of its keys and whether +/// the line opens a `- ` list item. None for any other line. +fn setup_bun_uses(line: &str) -> Option<(usize, bool)> { + let t = line.trim_start(); + if t.starts_with('#') { + return None; + } + let (opens_item, body) = match t.strip_prefix("- ") { + Some(b) => (true, b.trim_start()), + None => (false, t), + }; + if !body.starts_with("uses:") || !body.contains("oven-sh/setup-bun@") { + return None; + } + let key_ind = indent_of(line) + if opens_item { t.len() - body.len() } else { 0 }; + Some((key_ind, opens_item)) +} + +/// First line of the step that holds line `i`, walking up past blank lines +/// and sibling keys to the `- ` that opens it. +fn step_start(lines: &[&str], i: usize, key_ind: usize) -> usize { + let mut start = i; + while start > 0 { + let prev = lines[start - 1]; + if !prev.trim().is_empty() && indent_of(prev) < key_ind { + if prev.trim_start().starts_with("- ") { start -= 1; } + break; } - let mut end = i + 1; - while end < lines.len() { - let n = lines[end]; - if !n.trim().is_empty() && indent_of(n) < key_ind { - break; - } - end += 1; - } - let mut version: Option<&str> = None; - let mut file = false; - for l in &lines[start..end] { - let lt = l.trim_start(); - let lt = lt.strip_prefix("- ").unwrap_or(lt); - if let Some(v) = lt.strip_prefix("bun-version-file:") { - let _ = v; - file = true; - } else if let Some(v) = lt.strip_prefix("bun-version:") { - version = Some(unquote(v)); - } - } - let bad = file - || match version { - None => true, - Some(v) => { - let lv = v.to_ascii_lowercase(); - lv.is_empty() - || lv == "latest" - || lv == "canary" - || lv.contains(['^', '~', '>', '<', '*', 'x']) - } - }; - if bad { - return true; + start -= 1; + } + start +} + +/// One past the last line of the step that holds line `i`. +fn step_end(lines: &[&str], i: usize, key_ind: usize) -> usize { + let mut end = i + 1; + while end < lines.len() && (lines[end].trim().is_empty() || indent_of(lines[end]) >= key_ind) { + end += 1; + } + end +} + +/// True when the step has `bun-version-file`, no `bun-version`, or a +/// `bun-version` that is empty, `latest`, `canary` or a range. +fn setup_bun_step_unpinned(step: &[&str]) -> bool { + let mut version: Option<&str> = None; + let mut uses_file = false; + for l in step { + let lt = l.trim_start(); + let lt = lt.strip_prefix("- ").unwrap_or(lt); + if lt.starts_with("bun-version-file:") { + uses_file = true; + } else if let Some(v) = lt.strip_prefix("bun-version:") { + version = Some(unquote(v)); } } - false + uses_file || version.is_none_or(bun_version_is_loose) +} + +fn bun_version_is_loose(version: &str) -> bool { + let lv = version.to_ascii_lowercase(); + lv.is_empty() || lv == "latest" || lv == "canary" || lv.contains(['^', '~', '>', '<', '*', 'x']) } /// Facts the guards read. The caller gathers them; nothing here fetches. @@ -572,7 +603,7 @@ pub fn decide(rule: &Rule, g: &GuardInput) -> Decision { fn first_number(s: &str) -> Option { let b = s.as_bytes(); - let start = b.iter().position(|c| c.is_ascii_digit())?; + let start = b.iter().position(u8::is_ascii_digit)?; let mut end = start; while end < b.len() && (b[end].is_ascii_digit() || b[end] == b'.') { end += 1; @@ -607,89 +638,120 @@ fn is_threshold_line(l: &str) -> bool { /// suppression or a baseline entry. Conservative by design: a false `Err` /// becomes a referral, a false `Ok` would let a weakened check through. pub fn code_fix_is_safe(diff: &str) -> Result<(), String> { - let mut file = String::new(); - let mut old_file = String::new(); - let mut removed_thr: Vec<(String, f64)> = Vec::new(); - let mut added_thr: Vec<(String, f64)> = Vec::new(); - - fn lowered(removed: &[(String, f64)], added: &[(String, f64)]) -> bool { - added - .iter() - .any(|(k, n)| removed.iter().any(|(rk, rn)| rk == k && n < rn)) + let mut scan = DiffScan::default(); + for line in diff.lines() { + if scan.file_header(line)? { + continue; + } + if line.starts_with("@@") { + scan.end_hunk()?; + } else if let Some(body) = line.strip_prefix('-') { + scan.removed_line(body)?; + } else if let Some(body) = line.strip_prefix('+') { + scan.added_line(body)?; + } } + scan.end_hunk() +} - for line in diff.lines() { +const LOWERED_THRESHOLD: &str = "the diff lowers a threshold, minimum, maximum or coverage value"; + +/// Added lines that weaken a check, with the reason each gives. +const BANNED_ADDITIONS: &[(&str, &str)] = &[ + ("#[ignore", "adds #[ignore]"), + ("skip", "adds a skip"), + ("xit(", "adds xit("), + ("continue-on-error: true", "adds continue-on-error: true"), + ("|| true", "adds `|| true`"), + ("hypatia: allow", "adds a `hypatia: allow` suppression"), + ("nosemgrep", "adds a nosemgrep suppression"), + ("#[allow(", "adds an #[allow(...)] suppression"), + ("#![allow(", "adds an #![allow(...)] suppression"), +]; + +/// What the code-fix guard has seen so far in a unified diff: the file being +/// edited, the file it replaced, and the threshold lines of the current hunk. +#[derive(Default)] +struct DiffScan { + file: String, + old_file: String, + removed_thr: Vec<(String, f64)>, + added_thr: Vec<(String, f64)>, +} + +impl DiffScan { + /// Read a `---` or `+++` file header. `Ok(true)` when the line was one; + /// `Err` when the new side is /dev/null for a test file (a deletion). + fn file_header(&mut self, line: &str) -> Result { if let Some(p) = line.strip_prefix("--- ") { if line.starts_with("--- a/") || line == "--- /dev/null" { - old_file = p.strip_prefix("a/").unwrap_or(p).to_string(); - continue; + self.old_file = p.strip_prefix("a/").unwrap_or(p).to_string(); + return Ok(true); } } if let Some(p) = line.strip_prefix("+++ ") { if line.starts_with("+++ b/") || line == "+++ /dev/null" { - if p == "/dev/null" { - if is_test_path(&old_file) { - return Err(format!("the diff deletes the test file {old_file}")); - } - file.clear(); + if p != "/dev/null" { + self.file = p.strip_prefix("b/").unwrap_or(p).to_string(); + } else if is_test_path(&self.old_file) { + return Err(format!("the diff deletes the test file {}", self.old_file)); } else { - file = p.strip_prefix("b/").unwrap_or(p).to_string(); + self.file.clear(); } - continue; + return Ok(true); } } - if line.starts_with("@@") { - if lowered(&removed_thr, &added_thr) { - return Err( - "the diff lowers a threshold, minimum, maximum or coverage value".into(), - ); - } - removed_thr.clear(); - added_thr.clear(); - continue; + Ok(false) + } + + /// A threshold lowered within the hunk that just ended is unsafe. + fn end_hunk(&mut self) -> Result<(), String> { + let lowered = self + .added_thr + .iter() + .any(|(k, n)| self.removed_thr.iter().any(|(rk, rn)| rk == k && n < rn)); + self.removed_thr.clear(); + self.added_thr.clear(); + if lowered { + Err(LOWERED_THRESHOLD.into()) + } else { + Ok(()) } - if let Some(body) = line.strip_prefix('-') { - if body.contains("assert") { - return Err("the diff removes a line containing `assert`".into()); - } - if is_threshold_line(body) { - if let Some(n) = first_number(body) { - removed_thr.push((without_digits(body), n)); - } - } - } else if let Some(body) = line.strip_prefix('+') { - let base = file.rsplit('/').next().unwrap_or(""); - if base == ".hypatia-baseline.json" || base == ".hypatia-ignore" { - return Err(format!("the diff adds a line to {base}")); - } - let lb = body.to_ascii_lowercase(); - let banned: &[(&str, &str)] = &[ - ("#[ignore", "adds #[ignore]"), - ("skip", "adds a skip"), - ("xit(", "adds xit("), - ("continue-on-error: true", "adds continue-on-error: true"), - ("|| true", "adds `|| true`"), - ("hypatia: allow", "adds a `hypatia: allow` suppression"), - ("nosemgrep", "adds a nosemgrep suppression"), - ("#[allow(", "adds an #[allow(...)] suppression"), - ("#![allow(", "adds an #![allow(...)] suppression"), - ]; - for (pat, why) in banned { - if lb.contains(pat) { - return Err(format!("the diff {why}")); - } - } - if is_threshold_line(body) { - if let Some(n) = first_number(body) { - added_thr.push((without_digits(body), n)); - } - } + } + + fn removed_line(&mut self, body: &str) -> Result<(), String> { + if body.contains("assert") { + return Err("the diff removes a line containing `assert`".into()); + } + if let Some(entry) = threshold_entry(body) { + self.removed_thr.push(entry); } + Ok(()) } - if lowered(&removed_thr, &added_thr) { - return Err("the diff lowers a threshold, minimum, maximum or coverage value".into()); + + fn added_line(&mut self, body: &str) -> Result<(), String> { + let base = self.file.rsplit('/').next().unwrap_or(""); + if base == ".hypatia-baseline.json" || base == ".hypatia-ignore" { + return Err(format!("the diff adds a line to {base}")); + } + let lower = body.to_ascii_lowercase(); + if let Some((_, why)) = BANNED_ADDITIONS.iter().find(|(pat, _)| lower.contains(pat)) { + return Err(format!("the diff {why}")); + } + if let Some(entry) = threshold_entry(body) { + self.added_thr.push(entry); + } + Ok(()) + } +} + +/// The (digit-free text, value) pair of a threshold, minimum, maximum or +/// coverage line, or None for any other line. +fn threshold_entry(body: &str) -> Option<(String, f64)> { + if !is_threshold_line(body) { + return None; } - Ok(()) + first_number(body).map(|n| (without_digits(body), n)) } /// Everything Hypatia needs to reproduce a referred problem. Absent values @@ -922,7 +984,8 @@ mod tests { // standards run 43446385277, Rust coverage (playbook-to-recipe). ("SQ-RUN-03", "gh: Server Error (HTTP 502)"), ("SQ-RUN-03", "fatal: unable to access 'https://git.sr.ht/x': The requested URL returned error: 503"), - ("SQ-RUN-03", "fatal: unable to access 'https://x/': Could not resolve host: x"), + ("SQ-RUN-03", "fatal: unable to access 'https://git.sr.ht/x': Could not resolve host: git.sr.ht"), + ("SQ-RUN-03", "curl: (7) Failed to connect to github.com port 443 after 21 ms"), ( "SQ-RUN-04", "ERR [git] fatal: ambiguous argument '4190136^..bf33aeb': unknown revision or path not in the working tree.", @@ -985,6 +1048,20 @@ mod tests { assert_eq!(ids("gh: HTTP 599", None), vec!["SQ-RUN-03"]); } + #[test] + fn only_dotted_host_names_count_as_transient() { + for log in [ + "Could not resolve host: db", + "curl: (6) Could not resolve host: localhost", + "Failed to connect to localhost port 5432", + "Could not resolve host", + "Could not resolve host: ", + ] { + assert!(!ids(log, None).contains(&"SQ-RUN-03"), "{log}"); + } + assert!(ids("Could not resolve host: api.github.com.", None).contains(&"SQ-RUN-03")); + } + #[test] fn push_refusals_are_told_apart() { // The git form is SQ-OWN-04, the gh API form is SQ-OWN-13; neither claims the other. diff --git a/docs/decisions/0004-first-line-rule-catalogue.adoc b/docs/decisions/0004-first-line-rule-catalogue.adoc index b21ca3e..b3ce01d 100644 --- a/docs/decisions/0004-first-line-rule-catalogue.adoc +++ b/docs/decisions/0004-first-line-rule-catalogue.adoc @@ -173,10 +173,10 @@ holds, or when its check does not pass on the PR head. | Merge conflict, or still blocked on an up-to-date head. | SQ-RUN-03 | ACT -| A step fails on a transient host or API error: `gh: HTTP 5xx` (hypatia run 43358533401: `gh: HTTP 502` on an owner-wide GraphQL query), `gh: Server Error (HTTP 5xx)` (standards run 43446385277: `gh: Server Error (HTTP 502)` while the coverage job read the workflow catalogue), a git remote answering `returned error: 5xx`, `Could not resolve host`, or `Failed to connect to`. +| A step fails on a transient host or API error: `gh: HTTP 5xx` (hypatia run 43358533401: `gh: HTTP 502` on an owner-wide GraphQL query), `gh: Server Error (HTTP 5xx)` (standards run 43446385277: `gh: Server Error (HTTP 502)` while the coverage job read the workflow catalogue), a git remote answering `returned error: 5xx`, or `Could not resolve host` or `Failed to connect to` for a host name that contains a dot. | Re-run the failed job once, only if that commit is still the head. | The re-run passes. -| It fails twice on the same commit, or the status is 401 or 403 (a token problem is SQ-OWN-09; a rate limit is SQ-RUN-01). +| It fails twice on the same commit, the status is 401 or 403 (a token problem is SQ-OWN-09; a rate limit is SQ-RUN-01), or the host name has no dot: a service container or `localhost` is a configuration error that a re-run cannot fix. | SQ-RUN-04 | ACT | gitleaks fails with `fatal: ambiguous argument '^..': unknown revision or path not in the working tree` because a shallow checkout lacks the range's base commit; trufflehog reports `commits: 0` for the same range (hypatia run 43158586027). @@ -311,7 +311,7 @@ holds, or when its check does not pass on the PR head. | (owner) | SQ-OWN-16 | OWNER -| A workflow force- or mirror-pushes to a forge, gated on a `*_MIRROR_ENABLED` variable, with no check of the source repository's visibility anywhere in the file (Hypatia WF030, found statically). Standards `mirror-reusable.yml` gates the job at line 93 and runs `git push --force gitlab main` at line 144, with the same shape per forge (bitbucket 196, codeberg 248); `github-backup-mirror-reusable.yml` too. It fires on nothing in hypatia, gitbot-fleet or this repository. +| A workflow force- or mirror-pushes to a forge, gated on a `*_MIRROR_ENABLED` variable, as Hypatia WF030 defines it (found statically; the definition, including which visibility tests count as a guard, is Hypatia's and is not restated here). Standards `mirror-reusable.yml` gates the job at line 93 and runs `git push --force gitlab main` at line 144, with the same shape per forge (bitbucket 196, codeberg 248); `github-backup-mirror-reusable.yml` too. It fires on nothing in hypatia, gitbot-fleet or this repository. | Report: the owner decides the guard (refuse, or skip loudly, when the source is private and the forge copy is not confirmed private; escalate when the forge visibility cannot be read). The squabbler never edits a mirror workflow, never pushes to a forge and never changes a visibility setting. Hypatia detects and refers it; this row only reports. | (owner) | (owner) From f7f12fff2355d18d62614e2473c6acbbaa7f4058 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 18:38:53 +0000 Subject: [PATCH 2/2] Refer undotted host errors (SQ-RUN-05) and read the host name from its own line CodeRabbit on #161: narrowing SQ-RUN-03 to dotted names left an undotted host error (a service container, localhost) matching no rule, so triage printed 'no catalogue rule matched' and exited 0. That is a silent skip. SQ-RUN-05 (REFER) now catches it. The host name is also read from the error line only, so a path on the next line cannot make a configuration error eligible for a re-run. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zmiut2nCcbtEx25HAU --- .../squabble-core/data/first-line-rules.tsv | 1 + crates/squabble-core/src/rules.rs | 62 +++++++++++++++---- .../0004-first-line-rule-catalogue.adoc | 10 ++- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/crates/squabble-core/data/first-line-rules.tsv b/crates/squabble-core/data/first-line-rules.tsv index 7c65871..1507778 100644 --- a/crates/squabble-core/data/first-line-rules.tsv +++ b/crates/squabble-core/data/first-line-rules.tsv @@ -37,6 +37,7 @@ SQ-RUN-01 ACT a setup or download step fails with API rate limit exceeded and th SQ-RUN-02 ACT a PR is blocked with required checks green under a strict ruleset and its base is behind the default branch Update the branch by merging the base; never rebase a branch the squabbler did not open The required checks pass on the new head Merge conflict, or still blocked on an up-to-date head SQ-RUN-03 ACT a step fails on a transient host or API error: gh: HTTP 5xx, gh: Server Error (HTTP 5xx), a git remote returning 5xx, or Could not resolve host or Failed to connect to a host name that contains a dot Re-run the failed job once, only if that commit is still the head The re-run passes It fails twice on the same commit, the status is 401 or 403 (a token problem: SQ-OWN-09, or a rate limit: SQ-RUN-01), or the host name has no dot (a service container or localhost is a configuration error a re-run cannot fix) SQ-RUN-04 ACT gitleaks fails with fatal: ambiguous argument ^..: unknown revision or path not in the working tree, because a shallow checkout lacks the range base commit; trufflehog reports commits: 0 for the same range Re-run the failed job once, only if that commit is still the head The re-run passes and the scan covers more than 0 commits It fails twice on the same commit: refer with the suggestion fetch-depth: 0 on the scan checkout +SQ-RUN-05 REFER a step fails with Could not resolve host or Failed to connect to a host name that has no dot (a service container, localhost) or no name at all Refer to Hypatia with the log line and the step: this is a configuration error (the service is not running or is named wrongly), so a re-run cannot fix it (owner) Always a referral: the name that does resolve in the workflow is Hypatia's to confirm SQ-CODE-01 FIX a Rust readiness or integration test cannot find its binary under target/debug/build Use PathBuf::from(env!("CARGO_BIN_EXE_")); a code fix, first-line only when the code-fix guard passes cargo test --test readiness passes The fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head SQ-CODE-02 FIX Validate Hypatia Baseline fails on new code_safety findings Fix the code behind the finding; never touch .hypatia-baseline.json The baseline job passes with the baseline file unchanged The fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head SQ-CODE-03 FIX Sonar S7688, S7679 or S131 in shell scripts Make the mechanical edit: for S7688 rewrite [ ] to [[ ]] only in files with a bash shebang (never /bin/sh or POSIX scripts); for S131 add a no-op default case *) ;;; decline S1192 in test files, where repetition is a convention bash -n, shellcheck and the repository's shell tests pass An S7688 test uses -a or -o or relies on word splitting, the fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head diff --git a/crates/squabble-core/src/rules.rs b/crates/squabble-core/src/rules.rs index f09fc55..bd52e81 100644 --- a/crates/squabble-core/src/rules.rs +++ b/crates/squabble-core/src/rules.rs @@ -51,7 +51,7 @@ use RuleClass::{Act, Fix, Noise, Owner, Refer}; /// here. const CATALOGUE_TSV: &str = include_str!("../data/first-line-rules.tsv"); -/// The 45 catalogue rows, in ADR 0004 order. +/// The 46 catalogue rows, in ADR 0004 order. /// /// The data file ships inside the crate, so a malformed row is a defect in /// this crate that a caller cannot repair. The catalogue never loads short: @@ -151,18 +151,31 @@ impl LogCtx<'_> { self.has("API rate limit exceeded") } - /// A host name that contains a dot right after `marker`, as in - /// `Could not resolve host: git.sr.ht`. A name without a dot (a service - /// container, `localhost`) is a configuration error, not a transient one. - fn dotted_name_after(&self, marker: &str) -> bool { - self.log.match_indices(marker).any(|(at, _)| { + /// The host name right after `marker`, taken from the rest of that line + /// only (`Could not resolve host: git.sr.ht`), with trailing punctuation + /// removed. It is empty when the line ends after the marker, so a name on + /// the next log line is never read. + fn host_names_after<'s>(&'s self, marker: &'s str) -> impl Iterator + 's { + self.log.match_indices(marker).map(move |(at, _)| { let rest = &self.log[at + marker.len()..]; - let name = rest.split_whitespace().next().unwrap_or(""); + let line = rest.lines().next().unwrap_or(""); + let name = line.split_whitespace().next().unwrap_or(""); name.trim_end_matches(|c: char| !c.is_ascii_alphanumeric()) - .contains('.') }) } + /// A host name that contains a dot after `marker`. A name without a dot + /// (a service container, `localhost`) is a configuration error, not a + /// transient one. + fn dotted_name_after(&self, marker: &str) -> bool { + self.host_names_after(marker).any(|n| n.contains('.')) + } + + /// A host error after `marker` whose name has no dot, or is missing. + fn undotted_name_after(&self, marker: &str) -> bool { + self.host_names_after(marker).any(|n| !n.contains('.')) + } + /// A status of 500 to 599 right after `marker`, as in `gh: HTTP 502`. fn http_5xx_after(&self, marker: &str) -> bool { let bytes = self.log.as_bytes(); @@ -205,6 +218,12 @@ static LOG_MATCHERS: &[LogMatcher] = &[ || c.dotted_name_after("Could not resolve host: ") || c.dotted_name_after("Failed to connect to ") }), + // A host error whose name has no dot is a referral, never a re-run and + // never silence (no-silent-skip). + ("SQ-RUN-05", |c| { + c.undotted_name_after("Could not resolve host: ") + || c.undotted_name_after("Failed to connect to ") + }), ("SQ-RUN-04", |c| { c.has("ambiguous argument") && c.has("unknown revision or path not in the working tree") }), @@ -919,18 +938,18 @@ mod tests { #[test] fn shipped_catalogue_file_parses() { let parsed = parse_catalogue(CATALOGUE_TSV).expect("the shipped data file is well formed"); - assert_eq!(parsed.len(), 45); + assert_eq!(parsed.len(), 46); } #[test] fn catalogue_shape() { - assert_eq!(CATALOGUE.len(), 45); + assert_eq!(CATALOGUE.len(), 46); let uniq: HashSet<_> = CATALOGUE.iter().map(|r| r.id).collect(); - assert_eq!(uniq.len(), 45); + assert_eq!(uniq.len(), 46); let n = |c| CATALOGUE.iter().filter(|r| r.class == c).count(); assert_eq!(n(Fix), 20); assert_eq!(n(Act), 4); - assert_eq!(n(Refer), 2); + assert_eq!(n(Refer), 3); assert_eq!(n(Owner), 16); assert_eq!(n(Noise), 3); assert!(rule("SQ-OWN-04").is_some()); @@ -949,7 +968,7 @@ mod tests { .filter(|r| r.class == Refer) .map(|r| r.id) .collect(); - assert_eq!(refs, vec!["SQ-REF-06", "SQ-REF-08"]); + assert_eq!(refs, vec!["SQ-RUN-05", "SQ-REF-06", "SQ-REF-08"]); assert!(rule("SQ-XXX-99").is_none()); } @@ -1056,10 +1075,27 @@ mod tests { "Failed to connect to localhost port 5432", "Could not resolve host", "Could not resolve host: ", + // The name must come from the error line, not the next one. + "Could not resolve host: \n./scripts/check.sh: configuration error", + "Failed to connect to\nexample.com says no", ] { assert!(!ids(log, None).contains(&"SQ-RUN-03"), "{log}"); } assert!(ids("Could not resolve host: api.github.com.", None).contains(&"SQ-RUN-03")); + assert!(!ids("Could not resolve host: api.github.com.", None).contains(&"SQ-RUN-05")); + } + + #[test] + fn undotted_host_errors_are_referred_not_silent() { + for log in [ + "Could not resolve host: db", + "curl: (6) Could not resolve host: localhost", + "Failed to connect to localhost port 5432", + "Could not resolve host: \n./scripts/check.sh: configuration error", + ] { + assert_eq!(ids(log, None), vec!["SQ-RUN-05"], "{log}"); + } + assert_eq!(rule("SQ-RUN-05").unwrap().class, Refer); } #[test] diff --git a/docs/decisions/0004-first-line-rule-catalogue.adoc b/docs/decisions/0004-first-line-rule-catalogue.adoc index b3ce01d..c674477 100644 --- a/docs/decisions/0004-first-line-rule-catalogue.adoc +++ b/docs/decisions/0004-first-line-rule-catalogue.adoc @@ -184,6 +184,12 @@ holds, or when its check does not pass on the PR head. | The re-run passes and the scan covers more than 0 commits. | It fails twice on the same commit: refer, with the suggestion `fetch-depth: 0` on the scan checkout. +| SQ-RUN-05 | REFER +| A step fails with `Could not resolve host` or `Failed to connect to` for a host name that has no dot (a service container, `localhost`) or no name on the error line. +| Refer to Hypatia with the log line and the step: this is a configuration error (the service is not running or is named wrongly), so a re-run cannot fix it. +| (owner) +| Always a referral: the name that does resolve in the workflow is Hypatia's to confirm. + | SQ-CODE-01 | FIX | A Rust readiness or integration test cannot find its binary (`failed to run …/target/debug/build/…: No such file`). | Use `PathBuf::from(env!("CARGO_BIN_EXE_"))`. @@ -386,8 +392,8 @@ base). == Consequences -* 24 rules are first-line (20 `FIX`, 4 `ACT`), 2 are referrals, 16 are - owner-only and 3 are noise; 45 in all. Every rule names its check and its +* 24 rules are first-line (20 `FIX`, 4 `ACT`), 3 are referrals, 16 are + owner-only and 3 are noise; 46 in all. Every rule names its check and its referral condition. * A fix that changes code (`SQ-CODE-nn`) is first-line only when the code-fix guard passes on its diff, so a fix that could weaken a check never runs at