File size: 12,459 Bytes
4147729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from __future__ import annotations

import logging
import os
from pathlib import Path

from backend.integrations.llm_factory import create_llm
from nodes.finding_quality import calibrate_findings, review_coverage
from pydantic import BaseModel, Field

from backend.state import Finding, OpenCodeReviewState, Severity, Verdict

logger = logging.getLogger(__name__)

# Prompt version β€” tied to the file on disk
PROMPT_VERSION = "v1"
PROMPT_FILE = Path(__file__).resolve().parent.parent / "prompts" / f"aggregator_{PROMPT_VERSION}.txt"

# Findings below this confidence are dropped before the LLM step
MIN_CONFIDENCE = 0.25
# Files/line pairs within this many lines are considered duplicates
LINE_TOLERANCE = 5

# Default to rule-based aggregation (skips expensive LLM critic call).
# Set AGGREGATOR_USE_LLM_CRITIC=true in env to re-enable the LLM pass.
USE_LLM_CRITIC = os.environ.get("AGGREGATOR_USE_LLM_CRITIC", "").lower() in ("1", "true", "yes")


# ─── Load prompt from file ──────────────────────────────────────────────────


def _load_prompt() -> str:
    """Load the system prompt from the versioned prompt file."""
    try:
        return PROMPT_FILE.read_text(encoding="utf-8")
    except FileNotFoundError:
        logger.warning("Prompt file %s not found β€” using fallback", PROMPT_FILE)
        return (
            "You are a senior engineer acting as a critic of a code review. "
            "Filter out noise, deduplicate findings, and produce a verdict. "
            "Return the filtered list of findings plus your verdict."
        )


SYSTEM_PROMPT = _load_prompt()


# ─── LLM output schema ──────────────────────────────────────────────────────


class AggregatedReview(BaseModel):
    """Final, noise-filtered findings and overall verdict."""
    findings: list[Finding] = Field(
        default_factory=list,
        description="Deduplicated, noise-filtered findings worth a senior engineer's time.",
    )
    verdict: Verdict = Field(
        description="Overall PR verdict after considering all findings.",
    )


# ─── Pre-LLM heuristic deduplication ────────────────────────────────────────


def _deduplicate(findings: list[Finding]) -> list[Finding]:
    """Heuristic dedup: group by ``(file_path, line_start)`` with a line
    tolerance, keeping the highest-confidence finding per group."""
    if not findings:
        return []

    # Sort by (file, line_start) so we can iterate and merge neighbours
    sorted_fs = sorted(findings, key=lambda f: (f.file_path, f.line_start))

    merged: list[Finding] = []
    current: Finding = sorted_fs[0]

    for f in sorted_fs[1:]:
        same_file = f.file_path == current.file_path
        close_lines = abs(f.line_start - current.line_start) <= LINE_TOLERANCE
        same_claim = " ".join(f.comment.lower().split()) == " ".join(current.comment.lower().split())
        if same_file and close_lines and f.kind == current.kind and f.category == current.category and same_claim:
            # Keep the higher-confidence finding, merge comments
            if f.confidence > current.confidence:
                current = f
            if current.confidence < 0.9:
                current.confidence = round(
                    (current.confidence + f.confidence) / 2, 2
                )
            # Prefer the specific comment over a generic one
            if len(f.comment) > len(current.comment):
                current.comment = f.comment
            if f.suggested_fix and not current.suggested_fix:
                current.suggested_fix = f.suggested_fix
            # Escalate severity if either finding is more severe
            current.severity = _more_severe(current.severity, f.severity)
        else:
            merged.append(current)
            current = f
    merged.append(current)

    # Drop low-confidence noise
    merged = [f for f in merged if f.confidence >= MIN_CONFIDENCE]

    return merged


_SEVERITY_ORDER = {
    Severity.CRITICAL: 0,
    Severity.HIGH: 1,
    Severity.MEDIUM: 2,
    Severity.LOW: 3,
    Severity.INFO: 4,
}


def _more_severe(a: Severity, b: Severity) -> Severity:
    return a if _SEVERITY_ORDER.get(a, 5) < _SEVERITY_ORDER.get(b, 5) else b


# ─── Rule-based fallback verdict ────────────────────────────────────────────


def _rule_verdict(findings: list[Finding]) -> Verdict:
    """Build a verdict from deduplicated findings without an LLM call."""
    eligible = [f for f in findings if f.kind != "test_suggestion" and f.evidence_status == "source_grounded"]
    suggestions = sum(f.kind == "test_suggestion" for f in findings)
    unverified = sum(f.kind != "test_suggestion" and f.evidence_status != "source_grounded" for f in findings)
    counts = {s: 0 for s in Severity}
    for f in eligible:
        counts[f.severity] = counts.get(f.severity, 0) + 1

    critical = counts[Severity.CRITICAL]
    high = counts[Severity.HIGH]

    if critical > 0:
        recommendation = "block"
        approved = False
    elif high > 0:
        recommendation = "request_changes"
        approved = False
    elif eligible or suggestions or unverified:
        recommendation = "comment"
        approved = True
    else:
        recommendation = "approve"
        approved = True

    # Score: 10 - penalty based on severity
    score = max(0.0, min(10.0, 10.0 - critical * 4.0 - high * 2.0 - counts.get(Severity.MEDIUM, 0) * 0.5))

    severity_counts = {s.value: counts.get(s, 0) for s in Severity}

    return Verdict(
        summary=_rule_summary(recommendation, severity_counts) + (f" {suggestions} test suggestion(s), {unverified} unverified claim(s) are excluded from the score." if suggestions or unverified else ""),
        approved=approved,
        overall_score=round(score, 1),
        critical_count=critical,
        high_count=high,
        recommendation=recommendation,
        score_explanation="Risk estimate from source-grounded findings, not answer correctness or proof of safety. Test suggestions and claims without source grounding do not lower the score.",
        score_breakdown={"starting_score": 10, "critical_penalty": critical * 4, "high_penalty": high * 2,
                         "medium_penalty": counts[Severity.MEDIUM] * .5, "scored_findings": len(eligible),
                         "test_suggestions_excluded": suggestions, "unverified_claims_excluded": unverified},
    )


def _rule_summary(recommendation: str, counts: dict) -> str:
    parts = [f"{v} {k}" for k, v in counts.items() if v > 0]
    if not parts:
        return "No source-grounded defects detected. This does not establish that the PR is safe."
    body = ", ".join(parts)
    if recommendation == "block":
        return f"Blocking: {body} β€” critical issues must be resolved."
    elif recommendation == "request_changes":
        return f"Changes requested: {body} β€” high-severity issues should be addressed."
    elif recommendation == "comment":
        return f"Minor concerns: {body} β€” consider addressing for best practices."
    return f"Approved: {body} β€” no significant issues."


# ─── Prompt builder ─────────────────────────────────────────────────────────


def _build_prompt(findings: list[Finding]) -> str:
    lines = [
        "Below are the raw findings from automated code reviewers "
        "(correctness, security, test-coverage).",
        "",
        "Filter out noise and return only the findings a senior engineer "
        "would actually leave on this PR.",
        "",
        "## Raw findings",
    ]
    for i, f in enumerate(findings, 1):
        lines.append(f"\n--- Finding {i} ---")
        lines.append(f"  File:      {f.file_path}")
        lines.append(f"  Lines:     {f.line_start}–{f.line_end}")
        lines.append(f"  Severity:  {f.severity.value}")
        lines.append(f"  Category:  {f.category}")
        lines.append(f"  Comment:   {f.comment}")
        lines.append(f"  Confidence: {f.confidence}")
        if f.suggested_fix:
            lines.append(f"  Fix:       {f.suggested_fix[:200]}")

    lines.append("\nNow apply the deduplication, noise-filtering, and verdict rules.")
    return "\n".join(lines)


# ─── Node ────────────────────────────────────────────────────────────────────


def aggregator_node(state: OpenCodeReviewState) -> dict:
    """Deduplicate, filter, and aggregate all findings into a final verdict.

    Returns state updates for ``findings`` (overwritten with the filtered
    list) and ``verdict`` (set-once by this node).

    Two-phase approach:
      1. Pre-LLM heuristic deduplication (groups by file/line proximity).
      2. Rule-based verdict by default (fast, ~10ms), or optional LLM critic
         when ``USE_LLM_CRITIC`` is enabled.
    """
    coverage = review_coverage(state)
    calibrated = calibrate_findings(state.findings, state)
    failed = [stage for stage in state.review_stages if stage.get("status") == "failed"]
    if failed:
        names = ", ".join(stage["reviewer"] for stage in failed)
        return {"review_coverage": coverage, "final_findings": _deduplicate(calibrated), "verdict": Verdict(
            summary=f"Review incomplete: {names} failed. Findings are partial; no approval or quality score is available.",
            approved=False, overall_score=None, recommendation="incomplete",
            score_explanation="No overall risk score is available because one or more reviewers failed. Remaining findings are partial.",
        )}
    raw_findings = calibrated
    if not raw_findings:
        logger.info("No findings to aggregate β€” producing empty verdict")
        return {"review_coverage": coverage, "final_findings": [], "verdict": _rule_verdict([])}

    # -- Phase 1: Heuristic dedup --------------------------------------------
    deduped = _deduplicate(raw_findings)
    logger.info(
        "Dedup heuristic: %d raw β†’ %d merged",
        len(raw_findings), len(deduped),
    )

    # -- Phase 2: Rule-based (default, fast) or LLM critic (opt-in) ----------
    if USE_LLM_CRITIC:
        try:
            llm = create_llm(endpoint=state.endpoint or None)
        except ValueError as exc:
            logger.warning("LLM critic unavailable β€” using rule-based: %s", exc)
            llm = None

        if llm is not None:
            logger.info(
                "Invoking aggregator LLM critic (prompt_version=%s) …",
                PROMPT_VERSION,
            )
            try:
                structured_llm = llm.with_structured_output(AggregatedReview)
                messages = [
                    {"role": "system", "content": SYSTEM_PROMPT},
                    {"role": "user", "content": _build_prompt(deduped)},
                ]
                result: AggregatedReview = structured_llm.invoke(messages)  # type: ignore[assignment]
            except Exception as exc:
                logger.warning("LLM critic failed β€” falling back to rule-based: %s", exc)
                result = None

            if result is not None:
                final_findings = calibrate_findings(result.findings, state)
                verdict = _rule_verdict(final_findings)
                logger.info(
                    "LLM aggregator: %d deduped β†’ %d final, recommendation=%s",
                    len(deduped), len(final_findings),
                    verdict.recommendation,
                )
                return {"review_coverage": coverage, "final_findings": final_findings, "verdict": verdict}

    # Rule-based path (default) β€” zero LLM overhead
    logger.info("Using rule-based aggregation (fast path)")
    final_findings = deduped
    verdict = _rule_verdict(deduped)

    return {
        "review_coverage": coverage,
        "final_findings": final_findings,  # plain list, overwrites
        "verdict": verdict,                # set-once, no reducer needed
    }