diff --git a/internal/domain/workflow/prreview/dimension.go b/internal/domain/workflow/prreview/dimension.go new file mode 100644 index 000000000..c627b638d --- /dev/null +++ b/internal/domain/workflow/prreview/dimension.go @@ -0,0 +1,69 @@ +package prreview + +import ( + "cmp" + "slices" +) + +// Dimension is one review dimension a lens proposed: a name, a reviewer +// prompt written for this PR, the files the reviewer must read, and a +// priority phase 3 ranks dimensions by when the depth's cap forces a cut. +type Dimension struct { + Name string + Prompt string + TargetFiles []string + ContextFiles []string + Priority float64 +} + +// MergeDimensions drops duplicate dimension names across the lenses' outputs +// (keeping the higher-priority copy), sorts by priority, and cuts the tail at +// cap. Order is stable for equal priority: the input order of the first lens +// that produced the name wins, so re-running the same lenses in the same +// order always keeps the same dimensions. +func MergeDimensions(lensOutputs [][]Dimension, limit int) []Dimension { + byName := make(map[string]Dimension) + order := make([]string, 0) + for _, lens := range lensOutputs { + for _, d := range lens { + existing, ok := byName[d.Name] + if !ok { + order = append(order, d.Name) + byName[d.Name] = d + continue + } + if d.Priority > existing.Priority { + byName[d.Name] = d + } + } + } + merged := make([]Dimension, 0, len(order)) + for _, name := range order { + merged = append(merged, byName[name]) + } + slices.SortStableFunc(merged, func(a, b Dimension) int { + return cmp.Compare(b.Priority, a.Priority) + }) + if len(merged) > limit { + merged = merged[:limit] + } + return merged +} + +// HallucinationDimension is the extra review dimension phase 3 adds for a PR +// intake scores as likely machine-written: claims, citations and behaviour +// the diff does not actually implement. Returns nil at or below +// AIGeneratedThreshold -- a coin-flip is not evidence, so it is not added. +func HallucinationDimension(aiGenerated float64) *Dimension { + if aiGenerated <= AIGeneratedThreshold { + return nil + } + return &Dimension{ + Name: "hallucination-check", + Prompt: "This PR's description or commit messages read as likely machine-written. " + + "Check every claim the PR makes -- what it says it does, any cited behaviour, API, or test result -- " + + "against what the diff actually implements. Report a finding for any claim, citation, or described " + + "behaviour that the diff does not actually carry out.", + Priority: 1, + } +} diff --git a/internal/domain/workflow/prreview/dimension_test.go b/internal/domain/workflow/prreview/dimension_test.go new file mode 100644 index 000000000..55a68ac02 --- /dev/null +++ b/internal/domain/workflow/prreview/dimension_test.go @@ -0,0 +1,68 @@ +package prreview + +import "testing" + +func TestMergeDimensionsDropsDuplicatesByName(t *testing.T) { + t.Parallel() + + lenses := [][]Dimension{ + {{Name: "security", Priority: 0.9}, {Name: "types", Priority: 0.4}}, + {{Name: "security", Priority: 0.5}, {Name: "docs", Priority: 0.2}}, + } + got := MergeDimensions(lenses, 10) + if len(got) != 3 { + t.Fatalf("MergeDimensions() returned %d dimensions, want 3 (security, types, docs merged from duplicates)", len(got)) + } + for _, d := range got { + if d.Name == "security" && d.Priority != 0.9 { + t.Errorf("duplicate %q kept priority %v, want the higher-priority lens's 0.9", d.Name, d.Priority) + } + } +} + +func TestMergeDimensionsCapsAtTheDepthLimit(t *testing.T) { + t.Parallel() + + lenses := [][]Dimension{ + { + {Name: "a", Priority: 0.9}, + {Name: "b", Priority: 0.8}, + {Name: "c", Priority: 0.7}, + {Name: "d", Priority: 0.1}, + }, + } + got := MergeDimensions(lenses, 2) + if len(got) != 2 { + t.Fatalf("MergeDimensions() with cap 2 returned %d dimensions, want 2", len(got)) + } + if got[0].Name != "a" || got[1].Name != "b" { + t.Errorf("MergeDimensions() with cap 2 kept %+v, want the two highest-priority dimensions (a, b)", got) + } +} + +func TestMergeDimensionsIsDeterministicForTheSameInput(t *testing.T) { + t.Parallel() + + lenses := [][]Dimension{{{Name: "x", Priority: 0.5}, {Name: "y", Priority: 0.5}}} + + got1 := MergeDimensions(lenses, 10) + got2 := MergeDimensions(lenses, 10) + if got1[0].Name != got2[0].Name || got1[1].Name != got2[1].Name { + t.Errorf("MergeDimensions() on the same input produced different orders across calls: %+v vs %+v", got1, got2) + } +} + +func TestHallucinationDimensionOnlyAddedAboveTheAIGeneratedThreshold(t *testing.T) { + t.Parallel() + + if got := HallucinationDimension(AIGeneratedThreshold); got != nil { + t.Errorf("HallucinationDimension() at the threshold = %+v, want nil (the PRD says above, not at)", got) + } + got := HallucinationDimension(AIGeneratedThreshold + 0.01) + if got == nil { + t.Fatal("HallucinationDimension() above the threshold = nil, want a dimension") + } + if got.Name == "" || got.Prompt == "" { + t.Errorf("HallucinationDimension() = %+v, want a named dimension with a reviewer prompt", got) + } +} diff --git a/internal/domain/workflow/prreview/intake.go b/internal/domain/workflow/prreview/intake.go new file mode 100644 index 000000000..22b5ddada --- /dev/null +++ b/internal/domain/workflow/prreview/intake.go @@ -0,0 +1,62 @@ +package prreview + +// Depth is how many review dimensions a run affords, driven by the size of +// the change (docs/prds/pr-review-agent.md, phase 1). +type Depth string + +const ( + DepthQuick Depth = "quick" + DepthStandard Depth = "standard" + DepthDeep Depth = "deep" +) + +// The line-count boundaries phase 1 classifies depth from: under 100 is +// quick, under 500 is standard, otherwise deep. +const ( + quickLinesCeiling = 100 + standardLinesCeiling = 500 +) + +// The dimension caps each depth affords. +const ( + maxDimensionsQuick = 3 + maxDimensionsStandard = 6 + maxDimensionsDeep = 12 +) + +// ClassifyDepth derives a review depth from the number of changed lines +// (additions plus deletions). +func ClassifyDepth(changedLines int) Depth { + switch { + case changedLines < quickLinesCeiling: + return DepthQuick + case changedLines < standardLinesCeiling: + return DepthStandard + default: + return DepthDeep + } +} + +// MaxDimensionsFor returns how many review dimensions a depth affords. An +// unrecognised depth gets the cheapest cap, never the most expensive: a typo +// or a stale value must not silently buy a deep run's budget. +func MaxDimensionsFor(depth Depth) int { + switch depth { + case DepthStandard: + return maxDimensionsStandard + case DepthDeep: + return maxDimensionsDeep + default: + return maxDimensionsQuick + } +} + +// ResolveDepth applies the PRD's override rule: an operator-set depth wins +// over the line-count classification. An empty explicit depth means no +// override was given. +func ResolveDepth(changedLines int, explicit Depth) Depth { + if explicit != "" { + return explicit + } + return ClassifyDepth(changedLines) +} diff --git a/internal/domain/workflow/prreview/intake_test.go b/internal/domain/workflow/prreview/intake_test.go new file mode 100644 index 000000000..e1ffac7c6 --- /dev/null +++ b/internal/domain/workflow/prreview/intake_test.go @@ -0,0 +1,61 @@ +package prreview + +import "testing" + +func TestClassifyDepth(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + changedLines int + want Depth + }{ + {"zero lines is quick", 0, DepthQuick}, + {"just under the quick boundary", 99, DepthQuick}, + {"the quick boundary itself is standard", 100, DepthStandard}, + {"just under the standard boundary", 499, DepthStandard}, + {"the standard boundary itself is deep", 500, DepthDeep}, + {"far above the standard boundary is deep", 5000, DepthDeep}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + if got := ClassifyDepth(tt.changedLines); got != tt.want { + t.Errorf("ClassifyDepth(%d) = %q, want %q", tt.changedLines, got, tt.want) + } + }) + } +} + +func TestMaxDimensionsFor(t *testing.T) { + t.Parallel() + + tests := []struct { + depth Depth + want int + }{ + {DepthQuick, 3}, + {DepthStandard, 6}, + {DepthDeep, 12}, + {Depth("unknown"), 3}, // an unrecognised depth is treated as the cheapest, never the most expensive + } + for _, tt := range tests { + if got := MaxDimensionsFor(tt.depth); got != tt.want { + t.Errorf("MaxDimensionsFor(%q) = %d, want %d", tt.depth, got, tt.want) + } + } +} + +func TestExplicitDepthOverridesClassification(t *testing.T) { + t.Parallel() + + got := ResolveDepth(5000, DepthQuick) + if got != DepthQuick { + t.Errorf("ResolveDepth with an explicit operator depth = %q, want %q (the explicit value must win over the line count)", got, DepthQuick) + } + + got = ResolveDepth(5000, "") + if got != DepthDeep { + t.Errorf("ResolveDepth with no explicit depth = %q, want %q (falls back to classification)", got, DepthDeep) + } +}