fix(prreview): order the confidence floor before dedup and merge overlapping findings
Score recorded a finding's duplicate identity before dropping it under its severity's confidence floor, so a below-floor finding shadowed a later above-floor duplicate of the same defect. The floor now runs first, and the merge keeps the higher-scoring of two duplicates, so the result depends on which findings were given and not on the order they were given in. The duplicate rule was byte-identical line ranges. The PRD's rule is overlapping lines: two same-file, same-severity findings whose ranges share a line are now one finding. FileStatus said "removed" for a deleted file, a second vocabulary for the status the repository persists as "deleted". The value says "deleted" now.
This commit is contained in:
1 parent
8c7ee60270
commit
2657b18be7
4 files changed
+156
-24
No files matched your search
@@ -18,14 +18,15 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FileStatus is what a change did to one file. The values are git's own
|
||||
// vocabulary for a change, which is also what a forge reports.
|
||||
// FileStatus is what a change did to one file. The values are the vocabulary
|
||||
// the repository already persists a change's status in, so a status read off a
|
||||
// diff and a status read off a capture are spelled the same word.
|
||||
type FileStatus string
|
||||
|
||||
const (
|
||||
FileAdded FileStatus = "added"
|
||||
FileModified FileStatus = "modified"
|
||||
FileRemoved FileStatus = "removed"
|
||||
FileRemoved FileStatus = "deleted"
|
||||
FileRenamed FileStatus = "renamed"
|
||||
)
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestParseDiffDescribesEachFile(t *testing.T) {
|
||||
{
|
||||
name: "a deleted file keeps the path it had",
|
||||
diff: "diff --git a/pkg/gone.py b/pkg/gone.py\ndeleted file mode 100644\n--- a/pkg/gone.py\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-import os\n-x = 1\n",
|
||||
want: []fileSummary{{Path: "pkg/gone.py", Status: "removed", Language: "python", Removed: 2, Hunks: 1}},
|
||||
want: []fileSummary{{Path: "pkg/gone.py", Status: "deleted", Language: "python", Removed: 2, Hunks: 1}},
|
||||
},
|
||||
{
|
||||
name: "a rename with changes",
|
||||
|
||||
@@ -130,24 +130,23 @@ type ScoreInputs struct {
|
||||
BlastRadiusFiles int
|
||||
}
|
||||
|
||||
// Score deduplicates exact duplicates, scores what is left, drops the findings
|
||||
// under their severity's confidence floor and ranks the rest by score. The same
|
||||
// findings always produce the same scores and the same order.
|
||||
// Score drops the findings under their severity's confidence floor, scores
|
||||
// what is left, merges the duplicates among the survivors and ranks the rest by
|
||||
// score. The same findings always produce the same scores and the same order.
|
||||
//
|
||||
// The floor comes before the merge and the merge keeps the higher-scoring
|
||||
// duplicate, so which findings were handed in decides the result and the order
|
||||
// they were handed in does not: a discarded finding must not be able to shadow
|
||||
// a kept one.
|
||||
func Score(findings []Finding, inputs ScoreInputs) []ScoredFinding {
|
||||
scored := make([]ScoredFinding, 0, len(findings))
|
||||
seen := map[dedupKey]bool{}
|
||||
for _, finding := range findings {
|
||||
severity := normalizeSeverity(finding.Severity)
|
||||
key := dedupKey{file: finding.File, lineStart: finding.LineStart, lineEnd: finding.LineEnd, severity: severity}
|
||||
if seen[key] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
if finding.Confidence < confidenceFloor(severity) {
|
||||
continue
|
||||
}
|
||||
score, applied := scoreOf(finding, severity, inputs)
|
||||
scored = append(scored, ScoredFinding{
|
||||
candidate := ScoredFinding{
|
||||
Dimension: finding.Dimension,
|
||||
File: finding.File,
|
||||
LineStart: finding.LineStart,
|
||||
@@ -162,22 +161,40 @@ func Score(findings []Finding, inputs ScoreInputs) []ScoredFinding {
|
||||
Score: score,
|
||||
Multipliers: applied,
|
||||
Blocking: finding.Blocking,
|
||||
})
|
||||
}
|
||||
if duplicate := indexOfDuplicate(scored, candidate); duplicate >= 0 {
|
||||
if candidate.Score > scored[duplicate].Score {
|
||||
scored[duplicate] = candidate
|
||||
}
|
||||
continue
|
||||
}
|
||||
scored = append(scored, candidate)
|
||||
}
|
||||
|
||||
slices.SortStableFunc(scored, func(a, b ScoredFinding) int { return cmp.Compare(b.Score, a.Score) })
|
||||
return scored
|
||||
}
|
||||
|
||||
// dedupKey is what makes two findings the same finding: one place, one
|
||||
// severity. The wording is not part of it -- two reviewers describing one
|
||||
// defect are one comment -- and a different severity at the same place is a
|
||||
// different claim about it.
|
||||
type dedupKey struct {
|
||||
file string
|
||||
lineStart int
|
||||
lineEnd int
|
||||
severity Severity
|
||||
// indexOfDuplicate returns the position of the scoring already kept that
|
||||
// candidate duplicates, or -1 when it is a finding of its own. Two findings are
|
||||
// one finding when they are the same claim about one place: the same severity,
|
||||
// in the same file, over lines that overlap. The wording is not part of it --
|
||||
// two reviewers describing one defect are one comment -- and a different
|
||||
// severity over the same lines is a different claim about them.
|
||||
func indexOfDuplicate(scored []ScoredFinding, candidate ScoredFinding) int {
|
||||
for index, kept := range scored {
|
||||
if kept.File == candidate.File && kept.Severity == candidate.Severity && linesOverlap(kept, candidate) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// linesOverlap reports whether two findings' line ranges share a line. A range
|
||||
// that merely ends where the next begins does not overlap it: line 13 and the
|
||||
// range 10-12 are two places.
|
||||
func linesOverlap(a, b ScoredFinding) bool {
|
||||
return a.LineStart <= b.LineEnd && b.LineStart <= a.LineEnd
|
||||
}
|
||||
|
||||
// scoreOf applies the rubric: the severity's weight times the reviewer's
|
||||
|
||||
@@ -199,21 +199,135 @@ func TestScoreMergesExactDuplicates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreAppliesTheConfidenceFloorBeforeDeduplication(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Both findings are the same defect in the same place, so the duplicate
|
||||
// rule reads them as one finding: one is under important's confidence
|
||||
// floor and one is over it.
|
||||
below := scored(SeverityImportant, 0.29)
|
||||
below.Title = "below floor"
|
||||
above := scored(SeverityImportant, 0.8)
|
||||
above.Title = "above floor"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
findings []Finding
|
||||
}{
|
||||
{name: "a below-floor finding first does not suppress the above-floor one", findings: []Finding{below, above}},
|
||||
{name: "an above-floor finding first survives the below-floor one", findings: []Finding{above, below}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := Score(test.findings, ScoreInputs{})
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("Score() returned %d findings, want the one above the confidence floor", len(got))
|
||||
}
|
||||
if got[0].Title != "above floor" {
|
||||
t.Errorf("Score() kept %q, want %q", got[0].Title, "above floor")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreMergesFindingsWhoseLinesOverlap(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The same defect reported at 10-12 and at 10-13 by two reviewers, the
|
||||
// second less sure of it than the first.
|
||||
higher := scored(SeverityImportant, 0.8)
|
||||
higher.LineStart = 10
|
||||
higher.LineEnd = 12
|
||||
higher.Title = "higher"
|
||||
lower := scored(SeverityImportant, 0.6)
|
||||
lower.LineStart = 10
|
||||
lower.LineEnd = 13
|
||||
lower.Title = "lower"
|
||||
|
||||
// 12 and 13 do not share a line, so these are two findings.
|
||||
before := scored(SeverityImportant, 0.8)
|
||||
before.LineStart = 12
|
||||
before.LineEnd = 12
|
||||
before.Title = "before the gap"
|
||||
after := scored(SeverityImportant, 0.8)
|
||||
after.LineStart = 13
|
||||
after.LineEnd = 13
|
||||
after.Title = "after the gap"
|
||||
|
||||
// The same lines carrying another severity are another claim about them.
|
||||
other := scored(SeverityCritical, 0.8)
|
||||
other.LineStart = 10
|
||||
other.LineEnd = 13
|
||||
other.Title = "another severity"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
findings []Finding
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "an overlapping range collapses to the higher-scoring finding",
|
||||
findings: []Finding{higher, lower},
|
||||
want: []string{"higher"},
|
||||
},
|
||||
{
|
||||
name: "input order does not decide which overlapping finding survives",
|
||||
findings: []Finding{lower, higher},
|
||||
want: []string{"higher"},
|
||||
},
|
||||
{
|
||||
name: "ranges that touch but do not overlap are two findings",
|
||||
findings: []Finding{before, after},
|
||||
want: []string{"after the gap", "before the gap"},
|
||||
},
|
||||
{
|
||||
name: "an overlapping range of another severity is another finding",
|
||||
findings: []Finding{higher, other},
|
||||
want: []string{"another severity", "higher"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := Score(test.findings, ScoreInputs{})
|
||||
titles := make([]string, 0, len(got))
|
||||
for _, finding := range got {
|
||||
titles = append(titles, finding.Title)
|
||||
}
|
||||
// The ranking is asserted elsewhere; here the question is which
|
||||
// findings survived, so the titles are compared as a set.
|
||||
slices.Sort(titles)
|
||||
wantStrings(t, titles, test.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreRanksByScoreAndKeepsInputOrderOnTies(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Every finding here covers a line of its own: the ties are two findings,
|
||||
// not one reported twice.
|
||||
low := scored(SeveritySuggestion, 0.5)
|
||||
low.Title = "low"
|
||||
low.LineStart = 1
|
||||
low.LineEnd = 1
|
||||
high := scored(SeverityCritical, 0.9)
|
||||
high.Title = "high"
|
||||
high.LineStart = 2
|
||||
high.LineEnd = 2
|
||||
firstTie := scored(SeverityImportant, 0.5)
|
||||
firstTie.Title = "tie-first"
|
||||
firstTie.LineStart = 3
|
||||
firstTie.LineEnd = 3
|
||||
secondTie := scored(SeverityImportant, 0.5)
|
||||
secondTie.Title = "tie-second"
|
||||
secondTie.LineStart = 4
|
||||
secondTie.LineEnd = 4
|
||||
|
||||
findings := []Finding{low, high, firstTie, secondTie}
|
||||
|
||||
|
||||
Reference in new issue
Block a user