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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
<h1 align="center">π OpenCodeReview</h1>
|
| 12 |
|
| 13 |
+
<p align="center">
|
| 14 |
+
<b>AI-powered pull request review with human-in-the-loop approval.</b><br>
|
| 15 |
+
Three specialist LLM reviewers (correctness Β· security Β· test coverage) analyze every PR,
|
| 16 |
+
a vector store grounds them in your codebase's history, and nothing is posted to GitHub
|
| 17 |
+
until a human approves it.
|
| 18 |
+
</p>
|
| 19 |
|
| 20 |
+
<p align="center">
|
| 21 |
+
<a href="https://huggingface.co/spaces/sdm0/opencodereview"><img src="https://img.shields.io/badge/π€%20Hugging%20Face-Space-blue" alt="Hugging Face Space"></a>
|
| 22 |
+
<a href="#"><img src="https://img.shields.io/badge/Python-3.11%2B-3776AB?logo=python&logoColor=white" alt="Python 3.11+"></a>
|
| 23 |
+
<a href="#"><img src="https://img.shields.io/badge/LangGraph-1.x-1C3C3C?logo=langchain" alt="LangGraph"></a>
|
| 24 |
+
<a href="#"><img src="https://img.shields.io/badge/LLMs-Gemini%20%7C%20Groq-blueviolet" alt="LLMs"></a>
|
| 25 |
+
<a href="#"><img src="https://img.shields.io/badge/License-Unlicensed-lightgrey" alt="License"></a>
|
| 26 |
+
</p>
|
| 27 |
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## π Table of Contents
|
| 31 |
+
|
| 32 |
+
- [β¨ Features](#features)
|
| 33 |
+
- [π How It Works](#how-it-works)
|
| 34 |
+
- [ποΈ Architecture](#architecture)
|
| 35 |
+
- [π§± Tech Stack](#tech-stack)
|
| 36 |
+
- [π Quick Start](#quick-start)
|
| 37 |
+
- [π» CLI Usage](#cli-usage)
|
| 38 |
+
- [π₯οΈ Web UI (Gradio)](#web-ui-gradio)
|
| 39 |
+
- [π Authentication](#authentication)
|
| 40 |
+
- [βοΈ Configuration](#configuration)
|
| 41 |
+
- [π Environment Variables](#environment-variables)
|
| 42 |
+
- [π³ Docker](#docker)
|
| 43 |
+
- [π Evaluation](#evaluation)
|
| 44 |
+
- [π Observability](#observability)
|
| 45 |
+
- [π Alerting](#alerting)
|
| 46 |
+
- [π Project Structure](#project-structure)
|
| 47 |
+
- [π§ͺ Testing](#testing)
|
| 48 |
+
- [π License](#license)
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## β¨ Features
|
| 53 |
|
| 54 |
+
- **Three parallel specialist reviewers** β correctness, security, and test-coverage reviewers
|
| 55 |
+
run concurrently on the same diff, each with its own versioned system prompt.
|
| 56 |
+
- **Retrieval-augmented review** β a persistent [ChromaDB](https://www.trychroma.com/) vector
|
| 57 |
+
store indexes your repository's source code, past merged-PR discussions, and docs, so
|
| 58 |
+
reviewers get relevant context (e.g. *"this function is called from X"*) β not just the diff.
|
| 59 |
+
- **Human-in-the-loop approval** β the pipeline **pauses** before posting anything. A human
|
| 60 |
+
reviews the verdict and findings, then approves, rejects, or quits.
|
| 61 |
+
- **Inline comments** β approved findings are posted as **PR review comments at the exact
|
| 62 |
+
file/line position**, with automatic fallback to issue-level comments when a line has gone stale (422).
|
| 63 |
+
- **Smart aggregation** β heuristic deduplication (file/line proximity + confidence) plus a
|
| 64 |
+
rule-based verdict (with an optional LLM critic pass) that filters noise before humans see it.
|
| 65 |
+
- **LLM fallback chain** β Google Gemini (primary, generous free tier) β Groq (fallback),
|
| 66 |
+
zero code changes required.
|
| 67 |
+
- **Full observability** β LangSmith & Langfuse tracing, per-run token cost & TTFT tracking,
|
| 68 |
+
RAGAS retrieval-quality scoring, and a health-check CLI (`doctor`).
|
| 69 |
+
- **Evaluation harness** β measure precision/recall/F1 against ground-truth human review
|
| 70 |
+
comments, plus RAGAS retrieval metrics.
|
| 71 |
+
- **Web UI & CLI** β a polished Gradio interface and a full-featured Click CLI.
|
| 72 |
+
- **GitHub OAuth (Device Flow)** β authenticate interactively; tokens stored in your OS keyring.
|
| 73 |
+
|
| 74 |
+
---
|
| 75 |
+
|
| 76 |
+
## π How It Works
|
| 77 |
+
|
| 78 |
+
```
|
| 79 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 80 |
+
β GitHub Pull Request β
|
| 81 |
+
ββββββββββββββββββββββββ¬ββββββββββββββββββββββββ
|
| 82 |
+
βΌ
|
| 83 |
+
ββββββββββββββββββββββββββ
|
| 84 |
+
START βββββββββββββββββββββΆβ ingestion β Fetch PR metadata, unified
|
| 85 |
+
β (no-op if diff β diff, and full file contents
|
| 86 |
+
β already present) β from the GitHub API
|
| 87 |
+
βββββββββββββ¬βββββββββββββ
|
| 88 |
+
βΌ
|
| 89 |
+
ββββββββββββββββββββββββββ
|
| 90 |
+
β retrieval β Build/load a ChromaDB vector
|
| 91 |
+
β β store of the repo's source,
|
| 92 |
+
β β past merged PRs & docs; query
|
| 93 |
+
β β it with the diff β context
|
| 94 |
+
βββββββββββββ¬βββββββββββββ
|
| 95 |
+
β
|
| 96 |
+
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
|
| 97 |
+
βΌ βΌ βΌ
|
| 98 |
+
ββββββββββββββββββββββ ββββββββββββββββββββββ ββββββββββββββββββββββ
|
| 99 |
+
β correctness_review β β security_review β β test_coverage_reviewβ
|
| 100 |
+
β (logic, edge cases,β β (injection, auth, β β (are the changes β
|
| 101 |
+
β null checks, ...) β β secrets, ...) β β properly tested?)β
|
| 102 |
+
βββββββββββ¬βββββββββββ βββββββββββ¬βββββββββββ βββββββββββ¬βββββββββββ
|
| 103 |
+
β β β
|
| 104 |
+
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
|
| 105 |
+
βΌ
|
| 106 |
+
ββββββββββββββββββββββββββ
|
| 107 |
+
β aggregator β Deduplicate findings, filter
|
| 108 |
+
β β noise, produce final verdict
|
| 109 |
+
β β (rule-based or LLM critic)
|
| 110 |
+
βββββββββββββ¬βββββββββββββ
|
| 111 |
+
βΌ
|
| 112 |
+
ββββββββββββββββββββββββββ
|
| 113 |
+
β human_approval β βΈ PAUSES here β a human
|
| 114 |
+
β (interrupt() pause) β must approve before posting
|
| 115 |
+
βββββββββββββ¬βββββββββββββ
|
| 116 |
+
βΌ
|
| 117 |
+
ββββββββββββββββββββββββββ
|
| 118 |
+
β executor β Post inline review comments
|
| 119 |
+
β β (422 β issue-comment fallback)
|
| 120 |
+
βββββββββββββ¬βββββββββββββ
|
| 121 |
+
βΌ
|
| 122 |
+
END
|
| 123 |
+
```
|
| 124 |
|
| 125 |
+
### Pipeline steps in detail
|
| 126 |
|
| 127 |
+
| # | Node | What happens |
|
| 128 |
+
|---|------|--------------|
|
| 129 |
+
| 1 | **ingestion** | Fetches PR metadata, the full unified diff, and per-file content (handles fork PRs, pagination, rate limits). Uses the head commit SHA. |
|
| 130 |
+
| 2 | **retrieval** | Builds (once per base-branch SHA, cached) a ChromaDB collection embedding the repo's source files, up to 20 past merged PRs, and docs (README, CONTRIBUTINGβ¦). Queries it with the diff, returns the top 15 context chunks. |
|
| 131 |
+
| 3 | **correctness_review** | LLM scans for logic errors, off-by-one bugs, missing null checks, resource leaks, concurrency hazards. |
|
| 132 |
+
| 4 | **security_review** | LLM scans for injection, secrets, unsafe deserialization, auth flaws, crypto misuse, information disclosure. |
|
| 133 |
+
| 5 | **test_coverage_review** | LLM checks whether the changed logic has adequate test coverage. |
|
| 134 |
+
| 6 | **aggregator** | Merges findings that point at the same file/line (Β±5 lines), keeps the highest-confidence one, drops noise below `0.25` confidence, then produces a verdict β either rule-based (default, ~10 ms, zero LLM cost) or via an optional LLM critic (`AGGREGATOR_USE_LLM_CRITIC=true`). |
|
| 135 |
+
| 7 | **human_approval** | Calls LangGraph's `interrupt()` β the graph **pauses** and returns the verdict + findings to the caller. Nothing is posted until the human resumes with `approve` / `reject`. |
|
| 136 |
+
| 8 | **executor** | Posts each finding as an **inline PR review comment** (right side of the diff, multi-line ranges supported). If GitHub returns `422` (stale line), falls back to a general issue comment. Finally posts the overall verdict as a summary comment. |
|
| 137 |
|
| 138 |
+
### Verdict logic (rule-based default)
|
| 139 |
|
| 140 |
+
| Condition | Recommendation | Approved |
|
| 141 |
+
|---|---|---|
|
| 142 |
+
| β₯ 1 critical finding | `block` | β |
|
| 143 |
+
| β₯ 1 high finding | `request_changes` | β |
|
| 144 |
+
| any medium/low/info finding | `comment` | β
|
|
| 145 |
+
| no findings | `approve` | β
|
|
| 146 |
+
|
| 147 |
+
Score: `10 β 4Β·critical β 2Β·high β 0.5Β·medium` (clamped to 0β10).
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
|
| 151 |
+
## ποΈ Architecture
|
| 152 |
+
|
| 153 |
+
The system is a **LangGraph state machine** with a typed Pydantic state schema and SQLite
|
| 154 |
+
checkpointing (needed for the interrupt/resume round-trip):
|
| 155 |
+
|
| 156 |
+
- **`OpenCodeReviewState`** carries PR metadata (`repo`, `pr_number`, `base_sha`, `diff`,
|
| 157 |
+
`changed_files`), retrieved context (`context_chunks`, accumulated via an `add` reducer),
|
| 158 |
+
findings (`findings` β `final_findings`), the `verdict`, and the `human_approved` flag.
|
| 159 |
+
- **Parallelism** β retrieval fans out to the three reviewers in parallel; LangGraph merges
|
| 160 |
+
their accumulated findings at the aggregator.
|
| 161 |
+
- **Checkpointing** β every run is persisted to SQLite (`SqliteSaver`), so a paused graph can
|
| 162 |
+
be resumed after a page reload or a process restart via the same thread ID.
|
| 163 |
+
|
| 164 |
+
### Tech stack
|
| 165 |
+
|
| 166 |
+
| Layer | Technology |
|
| 167 |
+
|---|---|
|
| 168 |
+
| Orchestration | [LangGraph](https://www.langchain.com/langgraph) (StateGraph + `interrupt()`) |
|
| 169 |
+
| LLMs | Google Gemini `gemini-3.1-flash-lite` (primary) β Groq `llama-3.3-70b-versatile` (fallback) |
|
| 170 |
+
| Structured output | Pydantic v2 schemas via `with_structured_output` |
|
| 171 |
+
| Vector store | ChromaDB (PersistentClient) + ONNX MiniLM-L6-v2 embeddings |
|
| 172 |
+
| GitHub integration | `requests`-based client with token resolution, 401 resilience, 15-min HTTP cache |
|
| 173 |
+
| Web UI | Gradio (dark/light theme, live pipeline status) |
|
| 174 |
+
| CLI | Click (with `OCR_` env-var prefix support) |
|
| 175 |
+
| Tracing | LangSmith (auto) + Langfuse (explicit callback) |
|
| 176 |
+
| Quality | RAGAS retrieval metrics + ground-truth eval harness |
|
| 177 |
+
| Auth | GitHub OAuth Device Flow, tokens stored in OS keyring |
|
| 178 |
+
| Deployment | Docker (multi-stage), Docker Compose, Hugging Face Spaces, GitHub Actions |
|
| 179 |
+
|
| 180 |
+
---
|
| 181 |
+
|
| 182 |
+
## π Quick Start
|
| 183 |
+
|
| 184 |
+
### Prerequisites
|
| 185 |
+
|
| 186 |
+
- Python **3.11+**
|
| 187 |
+
- A **GitHub token** (`GITHUB_TOKEN` or `opencodereview auth login`) β required to fetch PRs and post comments
|
| 188 |
+
- **One LLM key**: `GEMINI_API_KEY` (preferred) or `GROQ_API_KEY` (fallback)
|
| 189 |
+
|
| 190 |
+
### Installation
|
| 191 |
+
|
| 192 |
+
```bash
|
| 193 |
+
# 1. Clone & enter
|
| 194 |
+
git clone https://github.com/sdm0p/opencodereview.git
|
| 195 |
+
cd opencodereview
|
| 196 |
+
|
| 197 |
+
# 2. (Recommended) Create a virtual environment
|
| 198 |
+
python -m venv .venv
|
| 199 |
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 200 |
+
|
| 201 |
+
# 3. Install dependencies
|
| 202 |
+
pip install -r requirements.txt
|
| 203 |
+
|
| 204 |
+
# 4. Set your API keys
|
| 205 |
+
export GITHUB_TOKEN="ghp_..."
|
| 206 |
+
export GEMINI_API_KEY="AIza..." # primary LLM
|
| 207 |
+
# export GROQ_API_KEY="gsk_..." # fallback LLM
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
### Run your first review (smoke test)
|
| 211 |
|
| 212 |
```bash
|
| 213 |
+
# Runs the full pipeline on a synthetic demo PR β no API keys or network needed
|
| 214 |
python main.py review --smoke
|
| 215 |
+
```
|
| 216 |
|
| 217 |
+
### Review a real PR
|
| 218 |
+
|
| 219 |
+
```bash
|
| 220 |
+
python main.py review --repo psf/requests --pr 42
|
| 221 |
```
|
| 222 |
|
| 223 |
+
The pipeline fetches the PR, reviews it, and **pauses** for your decision:
|
| 224 |
+
|
| 225 |
+
```text
|
| 226 |
+
HUMAN-IN-THE-LOOP: Approve this review for posting?
|
| 227 |
+
[a] Approve β post findings as GitHub comments
|
| 228 |
+
[r] Reject β discard results, post nothing
|
| 229 |
+
[q] Quit β leave graph paused
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
---
|
| 233 |
+
|
| 234 |
+
## π» CLI Usage
|
| 235 |
+
|
| 236 |
+
```
|
| 237 |
+
python main.py [COMMAND] [SUBCOMMAND] [OPTIONS]
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
`review` and `doctor` are standalone commands; `auth` and `config` have their own subcommands
|
| 241 |
+
(e.g. `python main.py auth login`, `python main.py config show`).
|
| 242 |
+
|
| 243 |
+
Every option also works as an environment variable with the `OCR_` prefix
|
| 244 |
+
(e.g. `--repo` β `OCR_REPO`, `--pr` β `OCR_PR_NUMBER`).
|
| 245 |
+
|
| 246 |
+
### `review` β run the review pipeline
|
| 247 |
+
|
| 248 |
+
| Option | Default | Description |
|
| 249 |
+
|---|---|---|
|
| 250 |
+
| `--repo OWNER/REPO` | `demo-org/demo-repo` | GitHub repository to review |
|
| 251 |
+
| `--pr N` | `1` | Pull request number |
|
| 252 |
+
| `--smoke` | β | Non-interactive smoke test on synthetic data (skips the HITL prompt) |
|
| 253 |
+
|
| 254 |
+
> **Note:** with the default repo/PR the CLI uses the built-in synthetic payload so you can
|
| 255 |
+
> try the tool instantly. Pass real `--repo`/`--pr` values to review an actual PR.
|
| 256 |
+
|
| 257 |
+
```bash
|
| 258 |
+
python main.py review --smoke # offline pipeline check
|
| 259 |
+
python main.py review --repo psf/requests --pr 42 # review a real PR
|
| 260 |
+
python main.py review --repo private/org --pr 7 # private repos need a token
|
| 261 |
+
```
|
| 262 |
+
|
| 263 |
+
### `doctor` β health checks
|
| 264 |
+
|
| 265 |
+
```bash
|
| 266 |
+
python main.py doctor
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
Checks Python version, API keys, observability backends, and live connectivity to
|
| 270 |
+
Groq / Langfuse.
|
| 271 |
+
|
| 272 |
+
### `auth` β GitHub authentication
|
| 273 |
+
|
| 274 |
+
```bash
|
| 275 |
+
python main.py auth login # OAuth Device Flow (prints a code to enter on GitHub)
|
| 276 |
+
python main.py auth status # show the authenticated user
|
| 277 |
+
python main.py auth logout # remove the stored token
|
| 278 |
+
```
|
| 279 |
+
|
| 280 |
+
### `config` β secrets & settings management
|
| 281 |
+
|
| 282 |
+
```bash
|
| 283 |
+
python main.py config set-key --provider anthropic # store an API key in the keyring
|
| 284 |
+
python main.py config show # masked summary of configured keys
|
| 285 |
+
python main.py config set-observability \
|
| 286 |
+
--langsmith-api-key ls_... \
|
| 287 |
+
--langfuse-public-key pk-lf-... \
|
| 288 |
+
--langfuse-secret-key sk-lf-...
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
+
---
|
| 292 |
+
|
| 293 |
+
## π₯οΈ Web UI (Gradio)
|
| 294 |
+
|
| 295 |
+
A full web interface ships with the project β this is also what runs on
|
| 296 |
+
[Hugging Face Spaces](https://huggingface.co/spaces/sdm0/opencodereview).
|
| 297 |
+
|
| 298 |
+
```bash
|
| 299 |
+
python app.py # serves at http://localhost:7860
|
| 300 |
+
```
|
| 301 |
+
|
| 302 |
+
Features:
|
| 303 |
+
|
| 304 |
+
- **Review a PR** tab β enter `owner/repo` + PR number, watch the pipeline run, then
|
| 305 |
+
**Approve & Post** or **Reject** the results.
|
| 306 |
+
- **Smoke Test** tab β run the pipeline on synthetic demo data (no keys needed).
|
| 307 |
+
- Severity-colored findings table, verdict card, RAGAS retrieval-quality bars,
|
| 308 |
+
per-run cost summary.
|
| 309 |
+
- Dark/light theme toggle (persisted in `localStorage`).
|
| 310 |
+
- **Configured Keys & Health** accordion showing which keys are set.
|
| 311 |
+
|
| 312 |
+
---
|
| 313 |
+
|
| 314 |
+
## π Authentication
|
| 315 |
+
|
| 316 |
+
OpenCodeReview needs GitHub credentials to fetch PR data and (on approval) post comments.
|
| 317 |
+
Tokens are resolved in this priority order: **OS keyring β `GITHUB_TOKEN` env var**.
|
| 318 |
+
|
| 319 |
+
### Option A β OAuth Device Flow (recommended)
|
| 320 |
+
|
| 321 |
+
```bash
|
| 322 |
+
export GITHUB_OAUTH_CLIENT_ID="<your OAuth app client id>"
|
| 323 |
+
python main.py auth login
|
| 324 |
+
```
|
| 325 |
+
|
| 326 |
+
1. Register an OAuth app at `https://github.com/settings/developers` and **enable Device Flow**.
|
| 327 |
+
2. Run `auth login` β you'll get a one-time code and a verification URL.
|
| 328 |
+
3. Open the URL, enter the code, authorize. The token is stored securely in your keyring
|
| 329 |
+
(scope: `repo`).
|
| 330 |
+
|
| 331 |
+
### Option B β Personal access token
|
| 332 |
+
|
| 333 |
+
```bash
|
| 334 |
+
export GITHUB_TOKEN="ghp_..."
|
| 335 |
+
```
|
| 336 |
+
|
| 337 |
+
> π‘ The client automatically detects invalid/revoked tokens (HTTP 401) and raises a
|
| 338 |
+
> helpful error telling you to re-authenticate.
|
| 339 |
+
|
| 340 |
+
---
|
| 341 |
+
|
| 342 |
+
## βοΈ Configuration
|
| 343 |
+
|
| 344 |
+
Configuration is resolved with the priority **environment variable β system keyring β error**:
|
| 345 |
+
|
| 346 |
+
- Provider API keys (e.g. Anthropic via `config set-key`) follow
|
| 347 |
+
`OPENCODEREVIEW_<FIELD>` env naming.
|
| 348 |
+
- GitHub token and observability keys can be stored in the keyring via
|
| 349 |
+
`auth login` / `config set-observability`; env vars always take precedence.
|
| 350 |
+
- The HTTP cache (`requests-cache`, 15-min TTL) and the ChromaDB vector store live under
|
| 351 |
+
`.opencodereview/` in the working directory unless overridden (see env vars below).
|
| 352 |
+
|
| 353 |
+
---
|
| 354 |
+
|
| 355 |
+
## π Environment Variables
|
| 356 |
|
| 357 |
| Variable | Required | Description |
|
| 358 |
|---|---|---|
|
| 359 |
+
| `GEMINI_API_KEY` | Primary LLM | Google Gemini key β used for all reviewer nodes |
|
| 360 |
+
| `GROQ_API_KEY` | Fallback LLM | Groq key β used when Gemini is unavailable |
|
| 361 |
+
| `GITHUB_TOKEN` | YesΒΉ | GitHub token for fetching PRs & posting comments |
|
| 362 |
+
| `GITHUB_OAUTH_CLIENT_ID` | For `auth login` | GitHub OAuth App client ID (Device Flow) |
|
| 363 |
+
| `LANGSMITH_API_KEY` | No | LangSmith tracing (legacy alias: `LANGCHAIN_API_KEY`) |
|
| 364 |
+
| `LANGSMITH_PROJECT` | No | LangSmith project name (default `opencodereview`) |
|
| 365 |
+
| `LANGFUSE_PUBLIC_KEY` | No | Langfuse public key |
|
| 366 |
+
| `LANGFUSE_SECRET_KEY` | No | Langfuse secret key |
|
| 367 |
+
| `LANGFUSE_HOST` | No | Langfuse host (default `https://cloud.langfuse.com`) |
|
| 368 |
+
| `OPENCODEREVIEW_CACHE_DIR` | No | HTTP cache directory (default `.opencodereview/`) |
|
| 369 |
+
| `OPENCODEREVIEW_VECTOR_DIR` | No | ChromaDB vector-store directory (default `.opencodereview/vectors`) |
|
| 370 |
+
| `AGGREGATOR_USE_LLM_CRITIC` | No | `true` to enable the optional LLM aggregator pass (default: rule-based) |
|
| 371 |
+
| `PORT` | No | Gradio server port (default `7860`) |
|
| 372 |
+
| `DISCORD_WEBHOOK` / `SLACK_WEBHOOK` | For alerting | Alerting webhooks (see [Alerting](#alerting)) |
|
| 373 |
+
|
| 374 |
+
ΒΉ No token is required when reviewing the built-in demo payload β a token is only strictly
|
| 375 |
+
required to review real PRs.
|
| 376 |
+
|
| 377 |
+
---
|
| 378 |
+
|
| 379 |
+
## π³ Docker
|
| 380 |
+
|
| 381 |
+
### Build & run
|
| 382 |
+
|
| 383 |
+
```bash
|
| 384 |
+
# Build
|
| 385 |
+
docker build -t opencodereview .
|
| 386 |
+
|
| 387 |
+
# Smoke test
|
| 388 |
+
docker run --rm opencodereview python -m main review --smoke
|
| 389 |
+
|
| 390 |
+
# Review a real PR
|
| 391 |
+
docker run --rm \
|
| 392 |
+
-e GROQ_API_KEY=gsk_... \
|
| 393 |
+
-e GITHUB_TOKEN=ghp_... \
|
| 394 |
+
opencodereview python -m main review --repo org/repo --pr 42
|
| 395 |
+
|
| 396 |
+
# Web UI
|
| 397 |
+
docker run --rm -p 7860:7860 \
|
| 398 |
+
-e GROQ_API_KEY=gsk_... \
|
| 399 |
+
-e GITHUB_TOKEN=ghp_... \
|
| 400 |
+
opencodereview
|
| 401 |
+
```
|
| 402 |
+
|
| 403 |
+
### Docker Compose
|
| 404 |
+
|
| 405 |
+
```bash
|
| 406 |
+
# 1. Put your keys in a .env file
|
| 407 |
+
# GROQ_API_KEY=gsk_...
|
| 408 |
+
# GITHUB_TOKEN=ghp_...
|
| 409 |
+
|
| 410 |
+
# 2. Smoke test
|
| 411 |
+
docker compose run --rm opencodereview review --smoke
|
| 412 |
+
|
| 413 |
+
# 3. Review a real PR
|
| 414 |
+
docker compose run --rm opencodereview review --repo org/repo --pr 42
|
| 415 |
+
|
| 416 |
+
# 4. Run evaluation
|
| 417 |
+
docker compose run --rm opencodereview python eval_data/evaluate.py --max-prs 2
|
| 418 |
+
```
|
| 419 |
+
|
| 420 |
+
The vector store is persisted in a named volume (`opencodereview_vectors`) so embeddings are
|
| 421 |
+
not rebuilt on every run.
|
| 422 |
+
|
| 423 |
+
### Image variants
|
| 424 |
+
|
| 425 |
+
- **`runtime`** (default) β full app; vector-store retrieval uses the lightweight **ONNX**
|
| 426 |
+
MiniLM embedder, so no PyTorch is needed.
|
| 427 |
+
- **`runtime-full`** β adds `sentence-transformers` (PyTorch, ~800 MB) if you need it:
|
| 428 |
+
`docker build --target runtime-full -t opencodereview:full .`
|
| 429 |
+
|
| 430 |
+
### Deployment
|
| 431 |
+
|
| 432 |
+
- **Hugging Face Spaces**: push to `main` triggers
|
| 433 |
+
`.github/workflows/deploy-hf.yml`, which syncs the repo to
|
| 434 |
+
`sdm0/opencodereview` and restarts the Space (Docker SDK, port 7860). Set
|
| 435 |
+
`GROQ_API_KEY` / `GITHUB_TOKEN` as Space secrets.
|
| 436 |
+
|
| 437 |
+
---
|
| 438 |
+
|
| 439 |
+
## π Evaluation
|
| 440 |
+
|
| 441 |
+
Evaluate the agent against **ground-truth human review comments**.
|
| 442 |
+
|
| 443 |
+
### 1. Build the dataset
|
| 444 |
+
|
| 445 |
+
```bash
|
| 446 |
+
python eval_data/fetcher.py # requires GITHUB_TOKEN
|
| 447 |
+
```
|
| 448 |
+
|
| 449 |
+
Produces `eval_data/prs.jsonl` β one JSON object per PR with its diff, changed files, and the
|
| 450 |
+
human review comments used as ground truth.
|
| 451 |
+
|
| 452 |
+
### 2. Run evaluation
|
| 453 |
+
|
| 454 |
+
```bash
|
| 455 |
+
python eval_data/evaluate.py # full run
|
| 456 |
+
python eval_data/evaluate.py --max-prs 2 # quick smoke run
|
| 457 |
+
python eval_data/evaluate.py --verbose # per-finding match details
|
| 458 |
+
python eval_data/evaluate.py --match-keywords # keyword-overlap matching bonus
|
| 459 |
+
python eval_data/evaluate.py --ragas # + RAGAS retrieval metrics
|
| 460 |
+
python eval_data/evaluate.py --log-to-observability # log scores to LangSmith/Langfuse
|
| 461 |
+
```
|
| 462 |
+
|
| 463 |
+
Findings are matched to human comments by **file path + line proximity (Β±3 lines)** (with an
|
| 464 |
+
optional keyword-overlap bonus), then **precision / recall / F1** are computed per PR plus
|
| 465 |
+
micro- and macro-averages. Results are written to `eval_data/eval_results.json`.
|
| 466 |
+
|
| 467 |
+
> β οΈ The evaluator refuses to run while `GITHUB_TOKEN` is set (the executor would post to real
|
| 468 |
+
> PRs). Unset it, or pass `--force` to acknowledge.
|
| 469 |
+
|
| 470 |
+
### RAGAS retrieval metrics
|
| 471 |
+
|
| 472 |
+
`eval_data/ragas_eval.py` scores retrieval & generation quality:
|
| 473 |
+
|
| 474 |
+
| Metric | Question it answers |
|
| 475 |
+
|---|---|
|
| 476 |
+
| `context_precision` | Are the most relevant code chunks ranked first? |
|
| 477 |
+
| `context_recall` | Did we find *all* the relevant code? (needs ground truth) |
|
| 478 |
+
| `faithfulness` | Does the review stick to the retrieved code? |
|
| 479 |
+
| `answer_relevancy` | Does the review address the PR diff? |
|
| 480 |
+
| `mmr` | How early does the first useful chunk appear? |
|
| 481 |
+
|
| 482 |
+
Standalone CLI: `python eval_data/ragas_eval.py --data eval_data/prs.jsonl`
|
| 483 |
+
|
| 484 |
+
---
|
| 485 |
+
|
| 486 |
+
## π Observability
|
| 487 |
+
|
| 488 |
+
Two optional backends β the app runs fine with neither:
|
| 489 |
+
|
| 490 |
+
- **LangSmith** β enabled automatically when `LANGSMITH_API_KEY` (or legacy
|
| 491 |
+
`LANGCHAIN_API_KEY`) is set. Full graph traces out of the box.
|
| 492 |
+
- **Langfuse** β enabled when `LANGFUSE_PUBLIC_KEY` + `LANGFUSE_SECRET_KEY` are set.
|
| 493 |
+
The pipeline creates a trace per review, and logs **scores** linked to that trace:
|
| 494 |
+
- `verdict_score`, `findings_count`
|
| 495 |
+
- `ragas_context_precision`, `ragas_faithfulness`, `ragas_answer_relevancy`, `ragas_mmr`, β¦
|
| 496 |
+
|
| 497 |
+
Every run also tracks **token cost** (USD, via Groq pricing) and **TTFT**, printed at the end
|
| 498 |
+
of each CLI run and shown in the web UI.
|
| 499 |
+
|
| 500 |
+
---
|
| 501 |
+
|
| 502 |
+
## π Alerting
|
| 503 |
+
|
| 504 |
+
`scripts/alert.py` is a continuous health monitor: it runs the smoke-test review, compares the
|
| 505 |
+
resulting score against a stored baseline, and pings a **Discord or Slack webhook** when the
|
| 506 |
+
score deviates by more than `2.5` points or the run fails.
|
| 507 |
+
|
| 508 |
+
```bash
|
| 509 |
+
python scripts/alert.py \
|
| 510 |
+
--baseline-file scripts/alert_baseline.json \
|
| 511 |
+
--discord-webhook "$DISCORD_WEBHOOK"
|
| 512 |
+
|
| 513 |
+
python scripts/alert.py --slack-webhook "$SLACK_WEBHOOK" --update-baseline
|
| 514 |
+
```
|
| 515 |
+
|
| 516 |
+
A GitHub Actions workflow (`.github/workflows/alert.yml`) runs this **every 6 hours**
|
| 517 |
+
(set `GROQ_API_KEY`, `GITHUB_TOKEN`, and the webhook URLs as repository secrets).
|
| 518 |
+
|
| 519 |
+
---
|
| 520 |
+
|
| 521 |
+
## π Project Structure
|
| 522 |
+
|
| 523 |
+
```
|
| 524 |
+
opencodereview/
|
| 525 |
+
βββ app.py # Gradio web UI (entry point for HF Spaces)
|
| 526 |
+
βββ main.py # Click CLI β review / doctor / auth / config
|
| 527 |
+
βββ auth.py # GitHub OAuth Device Flow
|
| 528 |
+
βββ config.py # Settings resolution (env β keyring), config CLI
|
| 529 |
+
βββ graph.py # LangGraph pipeline + SqliteSaver checkpointing
|
| 530 |
+
βββ state.py # Typed Pydantic state schema (Findings, Verdict, β¦)
|
| 531 |
+
βββ llm_factory.py # Gemini β Groq fallback model factory
|
| 532 |
+
βββ github_client.py # GitHub API session, token resolution, HTTP caching
|
| 533 |
+
βββ observability.py # LangSmith/Langfuse, cost & TTFT tracking, health checks
|
| 534 |
+
βββ requirements.txt
|
| 535 |
+
βββ Dockerfile # Multi-stage build (builder β runtime β runtime-full)
|
| 536 |
+
βββ docker-compose.yml
|
| 537 |
+
βββ docker-entrypoint.sh # Fixes vector-store volume perms, drops to appuser
|
| 538 |
+
βββ nodes/ # One module per graph node
|
| 539 |
+
οΏ½οΏ½οΏ½ βββ ingestion.py # Fetch PR data from GitHub
|
| 540 |
+
β βββ retrieval.py # ChromaDB vector store + diff query
|
| 541 |
+
β βββ correctness_reviewer.py # LLM: logic & correctness issues
|
| 542 |
+
β βββ security_reviewer.py # LLM: security vulnerabilities
|
| 543 |
+
β βββ test_coverage_reviewer.py# LLM: test-coverage gaps
|
| 544 |
+
β βββ aggregator.py # Dedupe, filter, verdict
|
| 545 |
+
β βββ human_approval.py # interrupt() β human-in-the-loop gate
|
| 546 |
+
β βββ executor.py # Post inline comments to GitHub
|
| 547 |
+
β βββ passthrough.py # Placeholder/debug node (not wired into the graph)
|
| 548 |
+
βββ prompts/ # Versioned system prompts (correctness/security/β¦_v1.txt)
|
| 549 |
+
βββ eval_data/ # Evaluation harness
|
| 550 |
+
β βββ fetcher.py # Build ground-truth dataset (prs.jsonl)
|
| 551 |
+
β βββ evaluate.py # Precision/recall/F1 vs human comments
|
| 552 |
+
β βββ ragas_eval.py # RAGAS retrieval & generation metrics
|
| 553 |
+
β βββ prs.jsonl # Ground-truth dataset
|
| 554 |
+
β βββ eval_results.json # Latest evaluation results
|
| 555 |
+
βββ scripts/
|
| 556 |
+
β βββ alert.py # Smoke-test alert monitor (Discord/Slack)
|
| 557 |
+
βββ .github/workflows/
|
| 558 |
+
βββ alert.yml # Every-6h smoke-test alert
|
| 559 |
+
βββ deploy-hf.yml # Deploy to Hugging Face Spaces on push to main
|
| 560 |
+
```
|
| 561 |
+
|
| 562 |
+
---
|
| 563 |
+
|
| 564 |
+
## π§ͺ Testing
|
| 565 |
+
|
| 566 |
+
There are no unit-test files yet β the built-in verification paths are:
|
| 567 |
+
|
| 568 |
+
```bash
|
| 569 |
+
# Smoke test: compiles the graph, runs it on synthetic data, and verifies the
|
| 570 |
+
# interrupt/resume round-trip (assertions included)
|
| 571 |
+
python main.py review --smoke
|
| 572 |
+
|
| 573 |
+
# Doctor: checks env, keys, and connectivity
|
| 574 |
+
python main.py doctor
|
| 575 |
+
|
| 576 |
+
# Quick evaluation run on the first 2 PRs
|
| 577 |
+
python eval_data/evaluate.py --max-prs 2
|
| 578 |
+
```
|
| 579 |
+
|
| 580 |
+
---
|
| 581 |
+
|
| 582 |
+
## π License
|
| 583 |
+
|
| 584 |
+
This project is currently **unlicensed** (all rights reserved) β no license file is
|
| 585 |
+
distributed with the repository. Please contact the maintainers before reusing code.
|