diff --git a/internal/domain/workflow/prreview/blast.go b/internal/domain/workflow/prreview/blast.go index 944d26228..bd6be92ec 100644 --- a/internal/domain/workflow/prreview/blast.go +++ b/internal/domain/workflow/prreview/blast.go @@ -51,6 +51,58 @@ func BlastRadius(fsys fs.FS, changed []string) ([]string, error) { return radius, nil } +// ExposureCounts returns, for every file the change reaches without touching, +// how many changed files reach it: a fan-in count computed from the same +// import graph BlastRadius walks. It is BlastRadius's detail, not its +// replacement -- the coverage gate needs to tell a file three unrelated +// changed files each reach apart from one only one of them happens to import. +func ExposureCounts(fsys fs.FS, changed []string) (map[string]int, error) { + graph, err := buildImportGraph(fsys) + if err != nil { + return nil, err + } + changedSet := make(map[string]bool, len(changed)) + for _, file := range changed { + changedSet[file] = true + } + + counts := map[string]int{} + for _, file := range changed { + reached := map[string]bool{} + for _, imported := range graph.imports[file] { + reached[imported] = true + } + for _, dependent := range graph.dependents[file] { + reached[dependent] = true + } + for target := range reached { + if changedSet[target] { + continue + } + counts[target]++ + } + } + return counts, nil +} + +// HighExposureThreshold is the fan-in above which a blast-radius file counts +// as high-exposure for the coverage gate: reached by more than one changed +// file, so a regression there is not one dimension's problem to have covered. +const HighExposureThreshold = 1 + +// HighExposureFiles returns the blast-radius files whose exposure count +// exceeds HighExposureThreshold, sorted for a deterministic gap-review order. +func HighExposureFiles(counts map[string]int) []string { + var files []string + for file, count := range counts { + if count > HighExposureThreshold { + files = append(files, file) + } + } + sort.Strings(files) + return files +} + // importGraph is a snapshot's file-level dependency graph, in both directions: // an edge exists when one file's imports resolve to another file. type importGraph struct { diff --git a/internal/domain/workflow/prreview/blast_test.go b/internal/domain/workflow/prreview/blast_test.go index 1f7d76451..75e2d2a16 100644 --- a/internal/domain/workflow/prreview/blast_test.go +++ b/internal/domain/workflow/prreview/blast_test.go @@ -174,3 +174,25 @@ func TestBlastRadiusJavaScriptAndTypeScript(t *testing.T) { }) } } + +func TestExposureCountsAndHighExposureFiles(t *testing.T) { + t.Parallel() + + fsys := fstest.MapFS{ + "go.mod": {Data: []byte("module example.com/m\n\ngo 1.27.0\n")}, + "internal/store/store.go": {Data: []byte("package store\n\nfunc Get() int { return 1 }\n")}, + "internal/app/app.go": {Data: []byte("package app\n\nimport \"example.com/m/internal/store\"\n\nfunc Run() int { return store.Get() }\n")}, + "internal/other/other.go": {Data: []byte("package other\n\nimport \"example.com/m/internal/store\"\n\nfunc Run2() int { return store.Get() }\n")}, + } + + counts, err := ExposureCounts(fsys, []string{"internal/app/app.go", "internal/other/other.go"}) + if err != nil { + t.Fatalf("ExposureCounts() error = %v", err) + } + if counts["internal/store/store.go"] != 2 { + t.Errorf("store.go exposure = %d, want 2 (reached by both changed files)", counts["internal/store/store.go"]) + } + + high := HighExposureFiles(counts) + wantStrings(t, high, []string{"internal/store/store.go"}) +} diff --git a/internal/domain/workflow/prreview/coverage.go b/internal/domain/workflow/prreview/coverage.go new file mode 100644 index 000000000..90f7722ef --- /dev/null +++ b/internal/domain/workflow/prreview/coverage.go @@ -0,0 +1,53 @@ +package prreview + +// CoverageGaps returns the clusters with no finding pointing at any of their +// files, and the high-exposure blast-radius files no finding points at +// either: the two things phase 6's coverage gate checks (docs/prds/ +// pr-review-agent.md, phase 6). +func CoverageGaps(clusters []Cluster, highExposure []string, findings []Finding) (uncoveredClusters []Cluster, uncoveredFiles []string) { + reviewed := make(map[string]bool, len(findings)) + for _, f := range findings { + reviewed[f.File] = true + } + for _, cluster := range clusters { + covered := false + for _, file := range cluster.Files { + if reviewed[file] { + covered = true + break + } + } + if !covered { + uncoveredClusters = append(uncoveredClusters, cluster) + } + } + for _, file := range highExposure { + if !reviewed[file] { + uncoveredFiles = append(uncoveredFiles, file) + } + } + return uncoveredClusters, uncoveredFiles +} + +// ClusterFindings groups findings by the cluster whose files contain them, +// keyed by cluster ID. Phase 5's compound-defect check runs once per cluster +// with more than one confirmed finding, because a compound defect is a +// property of findings that share a place, not of the whole PR. A finding +// whose file belongs to no cluster contributes to none. +func ClusterFindings(clusters []Cluster, findings []Finding) map[string][]Finding { + fileToCluster := make(map[string]string, len(findings)) + for _, cluster := range clusters { + for _, file := range cluster.Files { + fileToCluster[file] = cluster.ID + } + } + grouped := make(map[string][]Finding) + for _, f := range findings { + id, ok := fileToCluster[f.File] + if !ok { + continue + } + grouped[id] = append(grouped[id], f) + } + return grouped +} diff --git a/internal/domain/workflow/prreview/coverage_test.go b/internal/domain/workflow/prreview/coverage_test.go new file mode 100644 index 000000000..f38663801 --- /dev/null +++ b/internal/domain/workflow/prreview/coverage_test.go @@ -0,0 +1,66 @@ +package prreview + +import "testing" + +func TestCoverageGapsFindsUncoveredClustersAndFiles(t *testing.T) { + t.Parallel() + + clusters := []Cluster{ + {ID: "cluster_0", Name: "internal/a", Files: []string{"internal/a/a.go"}}, + {ID: "cluster_1", Name: "internal/b", Files: []string{"internal/b/b.go"}}, + } + highExposure := []string{"internal/c/c.go", "internal/d/d.go"} + findings := []Finding{ + {File: "internal/a/a.go"}, + {File: "internal/d/d.go"}, + } + + uncoveredClusters, uncoveredFiles := CoverageGaps(clusters, highExposure, findings) + + if len(uncoveredClusters) != 1 || uncoveredClusters[0].ID != "cluster_1" { + t.Errorf("uncoveredClusters = %+v, want only cluster_1", uncoveredClusters) + } + if len(uncoveredFiles) != 1 || uncoveredFiles[0] != "internal/c/c.go" { + t.Errorf("uncoveredFiles = %v, want only internal/c/c.go", uncoveredFiles) + } +} + +func TestCoverageGapsReportsNothingWhenFullyCovered(t *testing.T) { + t.Parallel() + + clusters := []Cluster{{ID: "cluster_0", Files: []string{"a.go"}}} + highExposure := []string{"b.go"} + findings := []Finding{{File: "a.go"}, {File: "b.go"}} + + uncoveredClusters, uncoveredFiles := CoverageGaps(clusters, highExposure, findings) + if uncoveredClusters != nil || uncoveredFiles != nil { + t.Errorf("want no gaps, got clusters=%v files=%v", uncoveredClusters, uncoveredFiles) + } +} + +func TestClusterFindingsGroupsByFileMembership(t *testing.T) { + t.Parallel() + + clusters := []Cluster{ + {ID: "cluster_0", Files: []string{"a.go", "a_test.go"}}, + {ID: "cluster_1", Files: []string{"b.go"}}, + } + findings := []Finding{ + {File: "a.go", Title: "f1"}, + {File: "a_test.go", Title: "f2"}, + {File: "b.go", Title: "f3"}, + {File: "unrelated.go", Title: "f4"}, + } + + grouped := ClusterFindings(clusters, findings) + + if len(grouped["cluster_0"]) != 2 { + t.Errorf("cluster_0 findings = %d, want 2", len(grouped["cluster_0"])) + } + if len(grouped["cluster_1"]) != 1 { + t.Errorf("cluster_1 findings = %d, want 1", len(grouped["cluster_1"])) + } + if _, ok := grouped["cluster_2"]; ok { + t.Error("no finding belongs to a cluster_2 that doesn't exist") + } +} diff --git a/internal/domain/workflow/prreview/scoring.go b/internal/domain/workflow/prreview/scoring.go index 528a4f4bc..9f030a0c6 100644 --- a/internal/domain/workflow/prreview/scoring.go +++ b/internal/domain/workflow/prreview/scoring.go @@ -414,6 +414,26 @@ func CapInlineComments(findings []ScoredFinding) []ScoredFinding { return findings } +// IsAIGenerated reports whether intake's machine-written confidence clears +// AIGeneratedThreshold: a coin-flip is not evidence, so it takes strictly +// more than the threshold to count. +func IsAIGenerated(confidence float64) bool { + return confidence > AIGeneratedThreshold +} + +// IsHighPriority reports whether a finding's severity is high enough for +// phase 5's evidence verifier to check it against its evidence package: a +// hallucinated nitpick costs a reader little, a hallucinated critical costs +// them a debugging session chasing a defect that was never there. +func IsHighPriority(f Finding) bool { + switch normalizeSeverity(f.Severity) { + case SeverityCritical, SeverityImportant: + return true + default: + return false + } +} + // ReviewEventFor decides the event a review is submitted with: a blocking // finding requests changes, and anything else -- including a review that found // nothing -- is a comment. A pipeline that found nothing has said nothing about