Skip to content

Commit 7bc9c0f

Browse files
authored
Merge pull request #153 from mimran-khan/fix/scoring-confidence-docs-and-sort
fix(scoring): document confidence scaling, sort by severity within rule bucket
2 parents 1911d82 + 4d50828 commit 7bc9c0f

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎src/skillspector/nodes/report.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,26 @@ def _compute_risk_score(
9797
a quarter. Occurrences beyond the third are ignored for scoring purposes.
9898
This prevents repeated pattern matches from inflating the score unboundedly.
9999
100+
Each finding's contribution is also scaled by its confidence value (clamped
101+
to [0, 1]). Findings with confidence <= 0 are skipped entirely — they do not
102+
contribute to the score but remain in the reported findings list.
103+
104+
Within each rule_id bucket, findings are processed in severity-descending
105+
order so that the highest-severity occurrence always receives the full weight.
106+
100107
Base points per severity: CRITICAL=50, HIGH=25, MEDIUM=10, LOW=5.
101108
Multiplier: 1.3x if has_executable_scripts.
102109
"""
110+
severity_rank = {"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3}
111+
sorted_findings = sorted(
112+
findings,
113+
key=lambda f: (f.rule_id or "UNKNOWN", severity_rank.get((f.severity or "LOW").upper(), 4)),
114+
)
115+
103116
rule_occurrence_count: dict[str, int] = {}
104117
score = 0.0
105118

106-
for f in findings:
119+
for f in sorted_findings:
107120
confidence = max(0.0, min(1.0, f.confidence))
108121
if confidence <= 0.0:
109122
continue

‎tests/nodes/test_report.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,21 @@ def test_same_rule_mixed_severities(self) -> None:
206206
# First TM1: 50*1.0, second TM1: 5*0.5 = 2.5 -> total 52.5 -> 52
207207
assert score == 52
208208

209+
def test_same_rule_low_before_critical_sorted_correctly(self) -> None:
210+
"""LOW before CRITICAL in input order must still score as if CRITICAL came first.
211+
212+
Without severity sorting, LOW gets the full weight (5*1.0=5) and CRITICAL
213+
gets the diminished weight (50*0.5=25), yielding 30. With sorting, CRITICAL
214+
gets full weight (50*1.0=50) and LOW gets diminished (5*0.5=2.5), yielding 52.
215+
"""
216+
findings = [
217+
_finding("TM1", "LOW", confidence=1.0),
218+
_finding("TM1", "CRITICAL", confidence=1.0),
219+
]
220+
score, _, _ = _compute_risk_score(findings, False)
221+
# Sorted: CRITICAL first (50*1.0) + LOW second (5*0.5=2.5) = 52.5 -> 52
222+
assert score == 52
223+
209224
def test_exact_band_boundary_21_is_medium(self) -> None:
210225
findings = [
211226
_finding("R1", "MEDIUM", confidence=1.0),

0 commit comments

Comments
 (0)