File size: 3,751 Bytes
5246c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import enum
from operator import add
from typing import Annotated, Literal, Optional

from pydantic import BaseModel, Field


# ─── Sub-models ──────────────────────────────────────────────────────────────


class ChangedFile(BaseModel):
    """A single file modified in the PR."""
    path: str
    status: str  # "added" | "modified" | "removed" | "renamed"
    content: str  # Full file content after the PR
    diff_hunk: str  # The unified-diff hunk for this file


class ContextChunk(BaseModel):
    """A chunk of relevant context retrieved from the repo's codebase or past PRs."""
    source: str  # "codebase" | "past_pr"
    file_path: str  # Where the chunk came from
    content: str  # The relevant code/text
    relevance_score: float = Field(default=0.0, ge=0.0, le=1.0)


class Severity(str, enum.Enum):
    CRITICAL = "critical"
    HIGH = "high"
    MEDIUM = "medium"
    LOW = "low"
    INFO = "info"


class Finding(BaseModel):
    """A single issue found during review."""
    file_path: str
    line_start: int
    line_end: int
    severity: Severity
    category: str  # "logic" | "security" | "style" | "performance" | "correctness"
    comment: str  # Human-readable explanation
    confidence: float = Field(default=0.5, ge=0.0, le=1.0)
    suggested_fix: Optional[str] = None
    kind: Literal["bug", "security_concern", "test_suggestion"] = "bug"
    evidence_status: Literal["source_grounded", "needs_verification", "outside_diff"] = "needs_verification"
    evidence_excerpt: str = ""
    calibration_note: str = ""


class Verdict(BaseModel):
    """Aggregated final verdict across all findings."""
    summary: str
    approved: bool
    overall_score: Optional[float] = Field(default=None, ge=0.0, le=10.0)
    critical_count: int = 0
    high_count: int = 0
    recommendation: str  # "approve" | "request_changes" | "block"
    score_explanation: str = ""
    score_breakdown: dict = Field(default_factory=dict)


# ─── Graph State ─────────────────────────────────────────────────────────────


class OpenCodeReviewState(BaseModel):
    """Shared state for the OpenCodeReview LangGraph agent."""

    # PR metadata: set-once, single values
    repo: str = ""
    pr_number: int = 0
    head_sha: str = ""
    incremental_base_sha: str = ""
    base_sha: str = ""  # SHA of the base branch (used as vector-store cache key)
    diff: Optional[str] = None
    changed_files: list[ChangedFile] = Field(default_factory=list)

    # Custom LLM endpoint chosen for this run (name from OCR_ENDPOINT_* env
    # configs).  Empty string means "use the built-in default provider".
    endpoint: str = ""
    review_mode: str = "thorough"

    # Retrieved context: accumulated across parallel retrieval nodes
    context_chunks: Annotated[list[ContextChunk], add] = Field(default_factory=list)

    # Review findings: accumulated across parallel review agents
    findings: Annotated[list[Finding], add] = Field(default_factory=list)

    review_stages: Annotated[list[dict], add] = Field(default_factory=list)
    review_coverage: dict = Field(default_factory=dict)

    # Aggregated (deduped, noise-filtered) findings: set-once by aggregator
    final_findings: list[Finding] = Field(default_factory=list)

    # Final verdict: set-once by the aggregation node
    verdict: Optional[Verdict] = None

    # Human-in-the-loop: set by the human_approval node
    human_approved: bool = False
    posting_result: Optional[dict] = None