Sdm0 commited on
Commit
559f070
Β·
verified Β·
1 Parent(s): 9d819e1

Deploy 426dc324

Browse files
Files changed (1) hide show
  1. README.md +558 -22
README.md CHANGED
@@ -8,42 +8,578 @@ app_file: app.py
8
  pinned: false
9
  ---
10
 
11
- # OpenCodeReview
12
 
13
- AI-powered PR review with human-in-the-loop approval.
 
 
 
 
 
14
 
15
- Reviews pull requests for **correctness**, **security**, and **test coverage** using LLMs (Gemini/Groq), then presents findings for human approval before posting comments.
 
 
 
 
 
 
16
 
17
- ## Stack
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- - **Frontend**: Gradio web UI
20
- - **Backend**: LangGraph pipeline with structured output
21
- - **LLMs**: Google Gemini (primary) β†’ Groq (fallback)
22
- - **Tracing**: LangSmith & Langfuse
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- ## Usage
25
 
26
- ### Web UI (default)
 
 
 
 
 
 
 
 
 
27
 
28
- Open the Space and enter a GitHub repository + PR number.
29
 
30
- ### CLI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  ```bash
33
- # Smoke test with demo data
34
  python main.py review --smoke
 
35
 
36
- # Review a real PR
37
- python main.py review --repo org/repo --pr 42
 
 
38
  ```
39
 
40
- ### Environment Variables
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  | Variable | Required | Description |
43
  |---|---|---|
44
- | `GEMINI_API_KEY` | Yes (primary) | Google Gemini API key |
45
- | `GROQ_API_KEY` | Yes (fallback) | Groq API key |
46
- | `GITHUB_TOKEN` | Yes | GitHub token for fetching PRs |
47
- | `LANGCHAIN_API_KEY` | No | LangSmith tracing |
48
- | `LANGFUSE_PUBLIC_KEY` | No | Langfuse tracing |
49
- | `LANGFUSE_SECRET_KEY` | No | Langfuse tracing |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.