fix(engine): one finding per blocking row, message starts with the blocked row, list every blocked row

This commit is contained in:
Ivan Miletic
2026-10-21 14:40:46 +02:00
parent 7069bdfe91
commit 41b8bf5b4f
3 changed files with 127 additions and 117 deletions
+22 -24
View File
@@ -210,10 +210,10 @@ impl VerifyTable<'_> {
example,
same_conditions,
redundant,
more,
others,
} => {
let rows = Self::row_list(&covered_by);
let subject = match more {
let subject = match others.len() {
0 => format!("row {} never fires", row + 1),
1 => format!("row {} and 1 more row below never fire", row + 1),
n => format!("row {} and {n} more rows below never fire", row + 1),
@@ -222,33 +222,24 @@ impl VerifyTable<'_> {
[only] if self.catch_all_row(*only) => Some(*only),
_ => None,
};
let matches = if more > 0 { "they match" } else { "it matches" };
let matches = if others.is_empty() {
"it matches"
} else {
"they match"
};
let head = match covered_by.len() {
_ if catch_all.is_some_and(|only| self.blank_outputs(only)) => format!(
"row {rows} is empty and not the last row, so it matches every input first and returns nothing: {subject}"
"{subject}: row {rows} is empty and not the last row, so it matches every input first and returns nothing"
),
_ if catch_all.is_some() => format!(
"row {rows} has no conditions and is not the last row, so it matches every input first: {subject}"
"{subject}: row {rows} has no conditions and is not the last row, so it matches every input first"
),
1 if same_conditions => format!(
"row {} has the same conditions as row {rows} but a different result; only row {rows} is used",
row + 1
),
1 if redundant => format!("{subject}: row {rows} already matches every input {matches}"),
1 => format!(
"{subject}: row {rows} already matches every input {matches} and returns a different result"
),
_ if redundant => {
format!("{subject}: rows {rows} together already match every input {matches}")
}
_ => format!(
"{subject}: rows {rows} together already match every input {matches}, with a different result"
),
};
let tail = match (redundant, more) {
(false, _) => "",
(true, 0) => "; removing it changes nothing",
(true, _) => "; removing them changes nothing",
1 => format!("{subject}: row {rows} already matches every input {matches}"),
_ => format!("{subject}: rows {rows} together already match every input {matches}"),
};
let mut args = row_args(row);
args.insert("coveredBy", rows);
@@ -269,17 +260,24 @@ impl VerifyTable<'_> {
} else if catch_all.is_some() {
args.insert("coverCatchAll", "true".to_string());
}
if more > 0 {
args.insert("moreBlocked", more.to_string());
if !others.is_empty() {
args.insert("moreBlocked", others.len().to_string());
args.insert(
"blockedIds",
std::iter::once(row)
.chain(others.iter().copied())
.map(|r| row_key(r).to_string())
.collect::<Vec<_>>()
.join(","),
);
}
let mut message = head;
if let Some(example) = example {
if !redundant {
if !redundant && others.is_empty() {
message.push_str(&format!(", for example {example}"));
}
args.insert("example", example.to_string());
}
message.push_str(tail);
let mut diagnostic = (if redundant {
Diagnostic::hint
} else {
+12 -8
View File
@@ -66,7 +66,7 @@ pub(crate) enum Finding {
example: Option<Value>,
same_conditions: bool,
redundant: bool,
more: usize,
others: Vec<usize>,
},
DuplicateRule {
row: usize,
@@ -242,7 +242,7 @@ impl<'a> VerifyTable<'a> {
let mut reported: Vec<bool> = satisfiable.iter().map(|s| !s).collect();
let mut warned: Vec<bool> = vec![false; self.rules.len()];
let mut seen: HashMap<(Vec<CellConstraint>, Vec<String>), usize> = HashMap::default();
let mut blocked_by: HashMap<(usize, bool), usize> = HashMap::default();
let mut blocked_by: HashMap<usize, usize> = HashMap::default();
for row in 0..self.rules.len() {
if let Some(index) = index.as_mut().filter(|_| row > 0) {
if satisfiable[row - 1] && !reported[row - 1] && !self.can_fail(row - 1) {
@@ -287,14 +287,18 @@ impl<'a> VerifyTable<'a> {
&& self.row_signature(&cells[row], row).0
== self.row_signature(&cells[covered_by[0]], covered_by[0]).0;
let blocker = match covered_by.as_slice() {
[only] if !same_conditions || self.catch_all_row(*only) => {
Some((*only, redundant))
}
[only] if !same_conditions || self.catch_all_row(*only) => Some(*only),
_ => None,
};
if let Some(at) = blocker.and_then(|b| blocked_by.get(&b)) {
if let Some(Finding::UnreachableRule { more, .. }) = findings.get_mut(*at) {
*more += 1;
if let Some(Finding::UnreachableRule {
others,
redundant: group_redundant,
..
}) = findings.get_mut(*at)
{
others.push(row);
*group_redundant &= redundant;
}
continue;
}
@@ -307,7 +311,7 @@ impl<'a> VerifyTable<'a> {
covered_by,
same_conditions,
redundant,
more: 0,
others: Vec::new(),
});
}
Some(None) => {}
+93 -85
View File
@@ -1088,34 +1088,22 @@ fn catch_all_with_a_result_in_the_middle_is_named_as_the_cause() {
],
};
let diagnostics = table.graph_diagnostics();
let find = |id: &str| {
diagnostics
.iter()
.find(|d| {
d.code == DiagnosticCode::UnreachableRule
&& d.args.get("rowId").map(String::as_str) == Some(id)
})
.expect("unreachable")
};
let blocked = find("r2");
assert_eq!(blocked.severity, Severity::Warning);
assert!(
blocked.message.contains("row 1 has no conditions"),
"{}",
blocked.message
);
let found = unreachable_of(&diagnostics);
assert_eq!(found.len(), 1, "{found:#?}");
let d = found[0];
assert_eq!(d.severity, Severity::Warning);
assert_eq!(d.args.get("rowId").map(String::as_str), Some("r2"));
assert_eq!(d.args.get("blockedIds").map(String::as_str), Some("r2,r3"));
assert_eq!(
blocked.args.get("coverCatchAll").map(String::as_str),
d.args.get("coverCatchAll").map(String::as_str),
Some("true")
);
let redundant = find("r3");
assert_eq!(redundant.severity, Severity::Hint);
assert!(
redundant.message.contains("changes nothing"),
d.message
.contains("row 2 and 1 more row below never fire: row 1 has no conditions"),
"{}",
redundant.message
d.message
);
assert!(redundant.args.get("coverCatchAll").is_none());
}
#[test]
@@ -1222,47 +1210,6 @@ fn rows_blocked_by_one_row_are_reported_once() {
}
}
#[test]
fn redundant_rows_behind_one_row_are_reported_once() {
let table = Table {
hit: "first",
inputs: &["applicant.age"],
outputs: &["applicant.discount"],
rows: &[
("r1", &["> 0"], &["1"]),
("r2", &["> 10"], &["1"]),
("r3", &["> 20"], &["2"]),
("r4", &["> 30"], &["1"]),
("r5", &["< 0"], &["3"]),
],
};
for diagnostics in [table.graph_diagnostics(), table.policy_diagnostics()] {
let unreachable: Vec<_> = diagnostics
.iter()
.filter(|d| d.code == DiagnosticCode::UnreachableRule)
.collect();
assert_eq!(unreachable.len(), 2, "{unreachable:?}");
let hint = unreachable
.iter()
.find(|d| d.severity == Severity::Hint)
.expect("hint");
assert_eq!(hint.args.get("rowId").map(String::as_str), Some("r2"));
assert_eq!(hint.args.get("moreBlocked").map(String::as_str), Some("1"));
assert!(
hint.message
.contains("row 2 and 1 more row below never fire"),
"{}",
hint.message
);
let warning = unreachable
.iter()
.find(|d| d.severity == Severity::Warning)
.expect("warning");
assert_eq!(warning.args.get("rowId").map(String::as_str), Some("r3"));
assert!(warning.args.get("moreBlocked").is_none());
}
}
struct Expect {
row: &'static str,
severity: Severity,
@@ -1314,6 +1261,50 @@ fn assert_unreachable(
}
}
#[test]
fn rows_behind_one_row_form_one_finding_whatever_their_results() {
let mixed = Table {
hit: "first",
inputs: &["applicant.age"],
outputs: &["applicant.discount"],
rows: &[
("r1", &["> 0"], &["1"]),
("r2", &["> 10"], &["1"]),
("r3", &["> 20"], &["2"]),
("r4", &["> 30"], &["1"]),
("r5", &["< 0"], &["3"]),
],
};
let same = Table {
hit: "first",
inputs: &["applicant.age"],
outputs: &["applicant.discount"],
rows: &[
("r1", &["> 0"], &["1"]),
("r2", &["> 10"], &["1"]),
("r3", &["> 20"], &[""]),
("r4", &["< 0"], &["3"]),
],
};
for (table, severity, ids) in [
(mixed, Severity::Warning, "r2,r3,r4"),
(same, Severity::Hint, "r2,r3"),
] {
let diagnostics = table.graph_diagnostics();
let found = unreachable_of(&diagnostics);
assert_eq!(found.len(), 1, "{found:#?}");
let d = found[0];
assert_eq!(d.severity, severity, "{}", d.message);
assert_eq!(d.args.get("blockedIds").map(String::as_str), Some(ids));
assert!(
d.message
.contains("row 1 already matches every input they match"),
"{}",
d.message
);
}
}
#[test]
fn unreachable_rows_matrix() {
let age = &["applicant.age"];
@@ -1322,37 +1313,37 @@ fn unreachable_rows_matrix() {
(
"1 catch-all with result blocks rows with other results",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 100"], &["1"]), ("r2", &[""], &["0"]), ("r3", &["< 10"], &["2"]), ("r4", &["[20..30]"], &["3"])] },
vec![Expect { row: "r3", severity: Severity::Warning, more: Some("1"), catch_all: true, text: "row 2 has no conditions and is not the last row, so it matches every input first: row 3 and 1 more row below never fire" }],
vec![Expect { row: "r3", severity: Severity::Warning, more: Some("1"), catch_all: true, text: "row 3 and 1 more row below never fire: row 2 has no conditions and is not the last row, so it matches every input first" }],
),
(
"2 catch-all with result blocks rows that lose nothing",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &[""], &["0"]), ("r2", &["< 10"], &["0"]), ("r3", &["> 5"], &[""])] },
vec![Expect { row: "r2", severity: Severity::Hint, more: Some("1"), catch_all: false, text: "row 1 has no conditions and is not the last row, so it matches every input first: row 2 and 1 more row below never fire; removing them changes nothing" }],
vec![Expect { row: "r2", severity: Severity::Hint, more: Some("1"), catch_all: false, text: "row 2 and 1 more row below never fire: row 1 has no conditions and is not the last row, so it matches every input first" }],
),
(
"3 empty catch-all blocks rows with results",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 500"], &["0"]), ("r2", &[""], &[""]), ("r3", &["> 200"], &["1"]), ("r4", &["< 150"], &["1"])] },
vec![Expect { row: "r3", severity: Severity::Warning, more: Some("1"), catch_all: true, text: "row 2 is empty and not the last row, so it matches every input first and returns nothing: row 3 and 1 more row below never fire" }],
vec![Expect { row: "r3", severity: Severity::Warning, more: Some("1"), catch_all: true, text: "row 3 and 1 more row below never fire: row 2 is empty and not the last row, so it matches every input first and returns nothing" }],
),
(
"4 empty catch-all blocks a row without a result",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 5"], &["1"]), ("r2", &[""], &[""]), ("r3", &["< 3"], &[""])] },
vec![Expect { row: "r3", severity: Severity::Hint, more: None, catch_all: false, text: "row 2 is empty and not the last row, so it matches every input first and returns nothing: row 3 never fires; removing it changes nothing" }],
vec![Expect { row: "r3", severity: Severity::Hint, more: None, catch_all: false, text: "row 3 never fires: row 2 is empty and not the last row, so it matches every input first and returns nothing" }],
),
(
"5 normal row blocks rows with other results",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 100"], &["0"]), ("r2", &["> 200"], &["1"]), ("r3", &["> 300"], &["2"]), ("r4", &["< 10"], &["3"])] },
vec![Expect { row: "r2", severity: Severity::Warning, more: Some("1"), catch_all: false, text: "row 2 and 1 more row below never fire: row 1 already matches every input they match and returns a different result" }],
vec![Expect { row: "r2", severity: Severity::Warning, more: Some("1"), catch_all: false, text: "row 2 and 1 more row below never fire: row 1 already matches every input they match" }],
),
(
"5b single row blocked by a normal row",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 100"], &["0"]), ("r2", &["> 200"], &["1"]), ("r3", &["< 10"], &["3"])] },
vec![Expect { row: "r2", severity: Severity::Warning, more: None, catch_all: false, text: "row 2 never fires: row 1 already matches every input it matches and returns a different result" }],
vec![Expect { row: "r2", severity: Severity::Warning, more: None, catch_all: false, text: "row 2 never fires: row 1 already matches every input it matches" }],
),
(
"6 normal row blocks rows that lose nothing",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 0"], &["1"]), ("r2", &["> 10"], &["1"]), ("r3", &["> 20"], &[""]), ("r4", &["< 0"], &["2"])] },
vec![Expect { row: "r2", severity: Severity::Hint, more: Some("1"), catch_all: false, text: "row 2 and 1 more row below never fire: row 1 already matches every input they match; removing them changes nothing" }],
vec![Expect { row: "r2", severity: Severity::Hint, more: Some("1"), catch_all: false, text: "row 2 and 1 more row below never fire: row 1 already matches every input they match" }],
),
(
"7 identical conditions with a different result stay per pair",
@@ -1365,7 +1356,7 @@ fn unreachable_rows_matrix() {
(
"8 several rows together",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["< 50"], &["1"]), ("r2", &[">= 50"], &["2"]), ("r3", &["[40..60]"], &["3"])] },
vec![Expect { row: "r3", severity: Severity::Warning, more: None, catch_all: false, text: "row 3 never fires: rows 1, 2 together already match every input it matches, with a different result" }],
vec![Expect { row: "r3", severity: Severity::Warning, more: None, catch_all: false, text: "row 3 never fires: rows 1, 2 together already match every input it matches" }],
),
(
"10 catch-all as the last row",
@@ -1376,8 +1367,8 @@ fn unreachable_rows_matrix() {
"image 73",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 500"], &["false"]), ("r2", &["> 200"], &["true"]), ("r3", &["> 250"], &["true"]), ("r4", &[""], &["true"]), ("r5", &["< 300"], &["true"]), ("r6", &["< 300"], &["true"]), ("r7", &[""], &[""])] },
vec![
Expect { row: "r3", severity: Severity::Hint, more: None, catch_all: false, text: "row 3 never fires: row 2 already matches every input it matches; removing it changes nothing" },
Expect { row: "r5", severity: Severity::Hint, more: Some("1"), catch_all: false, text: "row 4 has no conditions and is not the last row, so it matches every input first: row 5 and 1 more row below never fire; removing them changes nothing" },
Expect { row: "r3", severity: Severity::Hint, more: None, catch_all: false, text: "row 3 never fires: row 2 already matches every input it matches" },
Expect { row: "r5", severity: Severity::Hint, more: Some("1"), catch_all: false, text: "row 5 and 1 more row below never fire: row 4 has no conditions and is not the last row, so it matches every input first" },
],
),
];
@@ -1427,22 +1418,22 @@ fn unreachable_rows_matrix() {
(
"13 empty in one of two inputs is not a catch-all",
Table { hit: "first", inputs: tier_age, outputs: out, rows: &[("r1", &["\"gold\"", ""], &["1"]), ("r2", &["\"gold\"", "> 10"], &["2"]), ("r3", &["\"silver\"", ""], &["3"])] },
vec![Expect { row: "r2", severity: Severity::Warning, more: None, catch_all: false, text: "row 2 never fires: row 1 already matches every input it matches and returns a different result" }],
vec![Expect { row: "r2", severity: Severity::Warning, more: None, catch_all: false, text: "row 2 never fires: row 1 already matches every input it matches" }],
),
(
"14 empty in every input is a catch-all",
Table { hit: "first", inputs: tier_age, outputs: out, rows: &[("r1", &["\"gold\"", "> 10"], &["1"]), ("r2", &["", ""], &["0"]), ("r3", &["\"silver\"", "< 5"], &["2"])] },
vec![Expect { row: "r3", severity: Severity::Warning, more: None, catch_all: true, text: "row 2 has no conditions and is not the last row, so it matches every input first: row 3 never fires" }],
vec![Expect { row: "r3", severity: Severity::Warning, more: None, catch_all: true, text: "row 3 never fires: row 2 has no conditions and is not the last row, so it matches every input first" }],
),
(
"15 one of two outputs differs",
Table { hit: "first", inputs: age, outputs: two_out, rows: &[("r1", &["> 10"], &["1", "\"a\""]), ("r2", &["> 20"], &["1", "\"b\""]), ("r3", &["<= 10"], &["0", "\"c\""])] },
vec![Expect { row: "r2", severity: Severity::Warning, more: None, catch_all: false, text: "returns a different result" }],
vec![Expect { row: "r2", severity: Severity::Warning, more: None, catch_all: false, text: "row 1 already matches every input it matches" }],
),
(
"16 both outputs equal",
Table { hit: "first", inputs: age, outputs: two_out, rows: &[("r1", &["> 10"], &["1", "\"a\""]), ("r2", &["> 20"], &["1", "\"a\""]), ("r3", &["<= 10"], &["0", "\"c\""])] },
vec![Expect { row: "r2", severity: Severity::Hint, more: None, catch_all: false, text: "row 2 never fires: row 1 already matches every input it matches; removing it changes nothing" }],
vec![Expect { row: "r2", severity: Severity::Hint, more: None, catch_all: false, text: "row 2 never fires: row 1 already matches every input it matches" }],
),
(
"17 partial overlap is not flagged",
@@ -1452,10 +1443,13 @@ fn unreachable_rows_matrix() {
(
"18 blocked by a catch-all and an earlier normal row",
Table { hit: "first", inputs: age, outputs: out, rows: &[("r1", &["> 100"], &["1"]), ("r2", &[""], &["0"]), ("r3", &["> 200"], &["1"]), ("r4", &["> 300"], &["5"])] },
vec![
Expect { row: "r3", severity: Severity::Hint, more: None, catch_all: false, text: "removing it changes nothing" },
Expect { row: "r4", severity: Severity::Warning, more: None, catch_all: false, text: "row 1 already matches" },
],
vec![Expect {
row: "r3",
severity: Severity::Warning,
more: Some("1"),
catch_all: false,
text: "row 3 and 1 more row below never fire: row 1 already matches every input they match",
}],
),
(
"19 duplicated empty rows in the middle",
@@ -2196,7 +2190,11 @@ fn covered_rows_with_the_same_result_are_redundant_hints() {
assert_eq!(d.args.get("redundant").map(String::as_str), Some("true"));
assert_eq!(d.args.get("rowId").map(String::as_str), Some("r2"));
assert!(d.message.contains("never fires"), "{}", d.message);
assert!(d.message.contains("changes nothing"), "{}", d.message);
assert!(
d.message.contains("already matches every input it matches"),
"{}",
d.message
);
}
}
@@ -2673,16 +2671,26 @@ fn fuzz_flagged_rows_and_compression_never_change_results() {
let flagged = d.code == DiagnosticCode::UnreachableRule
|| (d.code == DiagnosticCode::DuplicateRule && hit != "collect");
if flagged {
let id = d.args.get("rowId").expect("rowId").clone();
let ids: Vec<String> = d
.args
.get("blockedIds")
.or_else(|| d.args.get("rowId"))
.expect("rowId")
.split(',')
.map(str::to_string)
.collect();
let mut without = content.clone();
without["rules"]
.as_array_mut()
.expect("rules")
.retain(|rule| rule["_id"] != Value::String(id.clone()));
.retain(|rule| {
!ids.iter()
.any(|id| rule["_id"] == Value::String(id.clone()))
});
assert_eq!(
pick(&fuzz_run(&table, &without, &inputs)),
expected,
"{mode}: removing flagged {id} changed results ({}) in {content}",
"{mode}: removing flagged {ids:?} changed results ({}) in {content}",
d.message
);
checked.0 += 1;