File size: 17,709 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# OpenCodeReview β€” Project Description

> **Spec file** Β· Audience: future maintainers & contributors Β· Goal: balanced
> overview (features + architecture + how to run) Β· Accuracy: **as-is**, i.e. the
> working tree is the source of truth, including uncommitted changes. No
> recommendations section β€” this document describes only.
>
> Written: 2026-08-12 Β· Based on code reading (no commands executed).

---

## 1. What the project is

**OpenCodeReview** is an AI-powered pull-request reviewer with a
**human-in-the-loop** approval gate. It runs a LangGraph pipeline in which three
specialist LLM reviewers β€” **correctness**, **security**, and **test coverage** β€”
analyze a PR in parallel, grounded by a ChromaDB vector store over the repo's
codebase history. Nothing is posted to GitHub until a human reviews the verdict
and explicitly approves it. Findings are posted as inline PR review comments with
an automatic fallback to issue-level comments when lines go stale.

It ships as both a **Click CLI** (`main.py`) and a **Gradio web UI** (`app.py`,
the entry point for Hugging Face Spaces), and includes an evaluation harness
(ground-truth precision/recall/F1 + RAGAS retrieval metrics), optional
observability (LangSmith + Langfuse), cost/TTFT tracking, a health-check CLI
(`doctor`), and a continuous alerting script.

**One-paragraph elevator summary:** "Enter `owner/repo` + PR number (or run the
CLI), the pipeline fetches the PR, indexes/retrieves relevant code context,
runs three parallel LLM reviews, dedupes and scores the findings, pauses for a
human decision, and only then posts comments back to GitHub."

---

## 2. Feature summary

- **Three parallel specialist reviewers** β€” correctness, security, and
  test-coverage nodes run concurrently on the same diff, each with its own
  versioned system prompt under `prompts/`.
- **Retrieval-augmented review** β€” a persistent ChromaDB store embeds the repo's
  source, up to 20 past merged PRs, and docs (README, CONTRIBUTING…); the diff is
  used as a query and the top ~15 chunks are injected as reviewer context.
- **Human-in-the-loop approval** β€” the graph calls LangGraph's `interrupt()` and
  pauses; the caller (CLI prompt or UI buttons) resumes with `approve` / `reject`.
- **Inline comments** β€” approved findings are posted at the exact file/line
  position; a GitHub `422` (stale line) triggers an issue-comment fallback.
- **Smart aggregation** β€” heuristic deduplication (file/line proximity Β±5 lines +
  confidence) and noise filtering (< 0.25 confidence dropped), plus a rule-based
  verdict with an optional LLM-critic pass.
- **LLM flexibility** β€” Gemini primary β†’ Groq fallback, plus **custom endpoints**
  (BYO keys) for any OpenAI-compatible API, Anthropic, or Google, configured via
  `OCR_ENDPOINT_*` env vars or added at runtime in the UI.
- **Evaluation & observability** β€” ground-truth eval harness, RAGAS retrieval
  metrics, LangSmith/Langfuse tracing, per-run token cost & TTFT.
- **CLI + Web UI** β€” identical pipeline driven from either interface.
- **GitHub OAuth Device Flow** β€” interactive login, tokens stored in the OS
  keyring (priority: keyring β†’ `GITHUB_TOKEN` env).

---

## 3. System architecture (pipeline-focused)

The system is a **LangGraph state machine** (`graph.py`) with a typed Pydantic
state schema and SQLite checkpointing. Topology:

```
START β†’ ingestion β†’ retrieval ──→ correctness_review ──┐
                              β”œβ”€β”€β†’ security_review    β”œβ”€β”€β†’ aggregator β†’ human_approval (interrupt ⏸) β†’ executor β†’ END
                              └──→ test_coverage_review β”€β”€β”˜
```

### 3.1 Pipeline nodes

| # | Node | Module | What it does |
|---|------|--------|--------------|
| 1 | `ingestion` | `nodes/ingestion.py` | Fetches PR metadata, the full unified diff, and per-file contents from the GitHub API (handles fork PRs, pagination, rate limits). No-op when a diff is already present in state (e.g. synthetic payloads). |
| 2 | `retrieval` | `nodes/retrieval.py` | Builds/loads a ChromaDB collection embedding the repo source, past merged PRs, and docs; queries it with the diff; returns top-15 context chunks. Cached per base-branch SHA so embeddings aren't rebuilt per run. |
| 3–5 | `correctness_review`, `security_review`, `test_coverage_review` | `nodes/{correctness,security,test_coverage}_reviewer.py` | Three parallel LLM reviewers. Each emits Pydantic-validated findings (category, severity, confidence, line range, suggested fix) via `with_structured_output`. Prompts live in `prompts/*_v1.txt`. |
| 6 | `aggregator` | `nodes/aggregator.py` | Merges findings pointing at the same file/line (Β±5 lines, highest confidence wins), drops noise below 0.25, then produces a verdict. Default is a rule-based verdict (~10 ms, zero LLM cost); `AGGREGATOR_USE_LLM_CRITIC=true` enables an optional LLM critic pass. |
| 7 | `human_approval` | `nodes/human_approval.py` | Calls `interrupt()` β€” the graph **pauses** and returns verdict + findings to the caller. Nothing is posted until resumed with `approve` / `reject`. |
| 8 | `executor` | `nodes/executor.py` | Posts each finding as an inline PR review comment (multi-line ranges supported). On GitHub `422` (stale line), falls back to a general issue comment. Posts the overall verdict as a summary comment. |

**Verdict rules (rule-based default):**

| Condition | Recommendation | Approved |
|---|---|---|
| β‰₯ 1 critical finding | `block` | ❌ |
| β‰₯ 1 high finding | `request_changes` | ❌ |
| any medium/low/info finding | `comment` | βœ… |
| no findings | `approve` | βœ… |

Score = `10 βˆ’ 4Β·critical βˆ’ 2Β·high βˆ’ 0.5Β·medium`, clamped to 0–10.

### 3.2 State schema (minimal)

`OpenCodeReviewState` (`state.py`) is a Pydantic `BaseModel`. Key fields:

- PR metadata: `repo`, `pr_number`, `base_sha`, `diff`, `changed_files`
- `endpoint` β€” chosen custom LLM endpoint name (empty = built-in default)
- Accumulated lists (`Annotated[list, add]` reducers so parallel branches merge):
  `context_chunks`, `findings`
- Aggregated output: `final_findings`, `verdict` (`Verdict` model), `human_approved`

Sub-models: `ChangedFile`, `ContextChunk`, `Finding`, `Verdict`, and a `Severity`
enum (`critical`/`high`/`medium`/`low`/`info`). See `state.py` for field-level
detail.

### 3.3 Checkpointing & resume

- Every run is persisted via **`SqliteSaver`** on a SQLite DB
  (`graph.py`, `checkpoints.db` by default; `:memory:` for ephemeral runs).
- A `JsonPlusSerializer` with an allowlist of the Pydantic models handles state
  (de)serialization.
- The compiled graph exposes `_opencodereview_conn` (informal handle) so callers
  close the SQLite connection when a run finishes β€” prevents file-handle leaks in
  the long-running UI. The DB file is cleaned up after runs (`_cleanup_db()`).
- Because checkpoints persist to disk, a paused graph can be resumed after a page
  reload or process restart via the same `thread_id`.

---

## 4. Supporting layers

### 4.1 LLM layer (`llm_factory.py`)

- **Default chain:** Google Gemini (`gemini-3.1-flash-lite`, primary) β†’ Groq
  (`llama-3.3-70b-versatile`, fallback); `temperature=0`. `ValueError` if no key.
- **Custom endpoints** (`endpoints.py` + `llm_factory.create_endpoint_llm`):
  - OpenAI-compatible (`langchain-openai`) β€” DeepSeek, OpenRouter, vLLM, Ollama,
    Together, local proxies…
  - Anthropic (`langchain-anthropic`), Google (`langchain-google-genai`).
- **Endpoint registry** (`endpoints.py`):
  - Env convention: `OCR_ENDPOINT_<n>_NAME/TYPE/API_KEY/BASE_URL/MODEL`
    (`TYPE`: `openai` | `anthropic` | `google`; default `openai`).
  - Built-in entries: `Gemini` and `Grok` are always selectable as endpoints.
  - UI-added endpoints live in an in-memory, thread-locked session registry
    (never written to disk; vanish on process restart).
  - Name precedence on collision: **session (UI) > env-var > built-in**.
  - Keys are only read from the environment and masked for display.

### 4.2 GitHub integration (`github_client.py`, `auth.py`)

- `requests`-based client; token resolution priority **keyring β†’ `GITHUB_TOKEN`
  env**; 401 detection with a helpful re-auth error.
- HTTP caching via `requests-cache` (15-min TTL) under `.opencodereview/`.
- `auth.py`: GitHub OAuth **Device Flow** β€” `auth login` prints a one-time code;
  token stored in the OS keyring (scope `repo`).

### 4.3 Aggregation & verdict (see Β§3.1 node 6)

### 4.4 Configuration (`config.py`)

- `Settings` (pydantic-settings) resolves env var β†’ system keyring β†’ error.
- Provider registry currently covers `anthropic` (`OPENCODEREVIEW_ANTHROPIC_KEY`).
- CLI: `config set-key`, `config show` (masked), `config set-observability`.

---

## 5. Interfaces & configuration

### 5.1 CLI (`main.py`)

| Command | Purpose |
|---|---|
| `review [--smoke] [--repo OWNER/REPO] [--pr N] [--endpoint NAME]` | Run the pipeline. Default repo/pr uses a built-in synthetic payload (no keys/network needed). Interactive HITL prompt (`a`/`r`/`q`). |
| `doctor` | Health checks: Python version, API keys, LLM endpoints, Groq/Langfuse connectivity. |
| `auth login/status/logout` | GitHub OAuth Device Flow management. |
| `config …` | `set-key`, `show`, `set-observability` (see Β§4.4). |

Every option also works as an env var with the `OCR_` prefix
(e.g. `--repo` β†’ `OCR_REPO`, `--pr` β†’ `OCR_PR_NUMBER`).

### 5.2 Gradio web UI (`app.py`)

- **Review a PR** tab (owner/repo + PR number, live pipeline status, then
  **Approve & Post** / **Reject**).
- **Smoke Test** tab (synthetic demo, no keys).
- Severity-colored findings table, verdict card, RAGAS retrieval-quality bars,
  per-run cost summary.
- **Configured Keys & Health** accordion; dark/light theme toggle (persisted).
- **LLM Endpoint** dropdown with a "Built-in" optgroup (Gemini, Grok) and flat
  custom-endpoint entries; an add-any-custom-endpoint form (BYO name/type/base
  URL/key/model); endpoint test buttons that probe connectivity
  (`_probe_endpoint` β€” classifies 401/403 as "server rejected the key", falls
  back to a `chat/completions` probe when `GET /models` is blocked/missing).

### 5.3 Environment variables (key ones; full list in README)

| Variable | Purpose |
|---|---|
| `GEMINI_API_KEY` / `GROQ_API_KEY` | Primary / fallback LLM keys |
| `GITHUB_TOKEN` | GitHub token (keyring preferred) |
| `OCR_ENDPOINT_<n>_*` | Custom endpoint configs (name/type/key/base_url/model) |
| `AGGREGATOR_USE_LLM_CRITIC` | `true` enables LLM-critic aggregation pass |
| `LANGSMITH_API_KEY`, `LANGFUSE_*` | Observability backends |
| `PORT` | Gradio port (default 7860) |
| `OCR_RAGAS_INTERVAL` | RAGAS throttle interval in seconds (default 3.0; 0 disables) |

---

## 6. Evaluation & observability

### 6.1 Ground-truth evaluation (`eval_data/`)

- `fetcher.py` builds `prs.jsonl` β€” one JSON per PR with diff, changed files, and
  human review comments as ground truth (requires `GITHUB_TOKEN`).
- `evaluate.py` matches findings to human comments by **file path + line
  proximity (Β±3 lines)** (optional keyword-overlap bonus), computes
  **precision / recall / F1** per PR plus micro/macro averages β†’
  `eval_data/eval_results.json`. Refuses to run while `GITHUB_TOKEN` is set
  (executor would post to real PRs) unless `--force`.
- `ragas_eval.py` computes RAGAS retrieval metrics: `context_precision`,
  `context_recall`, `faithfulness`, `answer_relevancy`, `mmr`.

### 6.2 Observability (`observability.py`)

- **LangSmith** β€” enabled automatically when `LANGSMITH_API_KEY` (or legacy
  `LANGCHAIN_API_KEY`) is set.
- **Langfuse** β€” enabled when public+secret keys are set; a trace per review;
  scores logged: `verdict_score`, `findings_count`, `ragas_*` metrics.
- **Cost & TTFT** β€” `TokenCostCallback` tracks per-run USD cost (via a
  `MODEL_PRICING` table covering Groq models and Gemini) and time-to-first-token;
  printed by CLI and shown in the UI.
- **Health checks** β€” `HealthStatus`, `check_groq_connectivity`,
  `check_langfuse_connectivity` (used by `doctor`).

### 6.3 RAGAS details worth knowing

- RAGAS scoring is routed **Groq-first** (avoids Gemini's ~15 req/min quota),
  then Gemini. Requires `GROQ_API_KEY` or `GEMINI_API_KEY`.
- Uncommitted work in the working tree throttles RAGAS LLM calls through a
  `_ThrottledChatModel` wrapper (`OCR_RAGAS_INTERVAL`, default 3 s between calls)
  to stay under free-tier per-minute rate limits, and failed metrics are logged
  as `None` (not fake `0.0`), with consumers guarded against `None`.

### 6.4 Alerting (`scripts/alert.py`)

Runs the smoke-test review, compares the score to a stored baseline, and pings a
**Discord or Slack webhook** when the score deviates by > 2.5 points or the run
fails. `alert.yml` runs it every 6 hours.

---

## 7. Deployment & CI

- **Dockerfile** β€” multi-stage: `builder` β†’ `runtime` (default; ONNX MiniLM
  embedder, no PyTorch) β†’ `runtime-full` (adds sentence-transformers/PyTorch).
- **docker-compose.yml** β€” one service, named volume for the vector store
  (`opencodereview_vectors`) so embeddings persist across runs;
  `docker-entrypoint.sh` fixes volume permissions and drops to a non-root user.
- **Hugging Face Spaces** β€” `.github/workflows/deploy-hf.yml` syncs the repo to
  the Space and restarts it (Docker SDK, port 7860) on push to `main`.
- **GitHub Actions** β€” `alert.yml` (every-6h smoke-test alert).

---

## 8. Project structure (file map)

```
β”œβ”€β”€ app.py                 # Gradio web UI (HF Spaces entry point)
β”œβ”€β”€ main.py                # Click CLI β€” review / doctor / auth / config
β”œβ”€β”€ graph.py               # LangGraph pipeline + SqliteSaver checkpointing
β”œβ”€β”€ state.py               # Typed Pydantic state schema
β”œβ”€β”€ llm_factory.py         # Gemini β†’ Groq fallback + custom-endpoint factory
β”œβ”€β”€ endpoints.py           # Custom LLM endpoint registry (env + UI session)
β”œβ”€β”€ github_client.py       # GitHub API client, token resolution, HTTP cache
β”œβ”€β”€ auth.py                # GitHub OAuth Device Flow
β”œβ”€β”€ config.py              # Settings (env β†’ keyring β†’ error) + config CLI
β”œβ”€β”€ observability.py       # LangSmith/Langfuse, cost & TTFT, health checks
β”œβ”€β”€ nodes/                 # One module per graph node (8 nodes)
β”œβ”€β”€ prompts/               # Versioned system prompts (…_v1.txt)
β”œβ”€β”€ eval_data/             # fetcher.py Β· evaluate.py Β· ragas_eval.py Β· prs.jsonl Β· eval_results.json
β”œβ”€β”€ scripts/alert.py       # Continuous health monitor (Discord/Slack)
β”œβ”€β”€ .github/workflows/     # alert.yml Β· deploy-hf.yml
└── Dockerfile Β· docker-compose.yml Β· docker-entrypoint.sh
```

---

## 9. Current state β€” known gaps & observations

These are the honest as-is observations a maintainer should know (no
recommendations here β€” just description):

- **No automated tests.** There is no `tests/` directory; verification relies on
  `python main.py review --smoke`, `python main.py doctor`, and the eval harness.
  The README acknowledges this.
- **Breadth-first exception swallowing.** Many layers catch `Exception` and only
  warn or silently degrade: Langfuse score logging, reviewer nodes (LLM failure β†’
  empty review), the aggregator's optional LLM critic, and RAGAS metric failures.
  This historically hid real failures (documented in `FIXES.md`).
- **FIXES.md backlog** β€” 14 documented fixes, most already applied to the code
  (verified: `import requests` in `nodes/executor.py`, the `_opencodereview_conn`
  connection-close pattern in `graph.py`/`main.py`/`app.py`, deleted dead
  `passthrough`/`post_results` nodes, and a `MODEL_PRICING` table incl. Gemini).
  The master checklist at the end of `FIXES.md` is unchecked β€” treat it as a
  record of intent rather than current status.
- **Uncommitted working-tree changes** (described as current state above):
  - `app.py` β€” improved endpoint probing (401/403 classified as key-rejected,
    chat/completions fallback probe for servers without `GET /models`, clearer
    status text in cards and the test form).
  - `eval_data/ragas_eval.py` β€” `_ThrottledChatModel` rate-limit throttle,
    Groq-first with `max_retries=5`, failed metrics β†’ `None`.
- **Minor artifacts:** a stray `nodes__init__.py` file sits at the repo root
  (an accidental duplicate of `nodes/__init__.py`); `.opencodereview/vectors/`
  contains a large checked-in ChromaDB sqlite file.
- **Naming inconsistencies:** the UI/CLI refer to the built-in Groq provider as
  "Grok" (`builtin_endpoints()`), which differs from the actual provider name.
- **`nodes/aggregator.py`** (unlike the reviewers) imports a shared
  `GROQ_MODEL`-style constant rather than a centralised `constants.py` β€” there is
  no `constants.py`; constants are still duplicated across modules.

---

## 10. How to run it (quick reference)

```bash
pip install -r requirements.txt
export GITHUB_TOKEN=ghp_...        # real PRs only; keyring preferred
export GEMINI_API_KEY=AIza...      # or GROQ_API_KEY

python main.py review --smoke                       # offline pipeline check
python main.py review --repo psf/requests --pr 42   # real PR
python main.py review --endpoint DeepSeek ...       # custom endpoint
python main.py doctor                               # health checks
python app.py                                       # web UI at :7860

python eval_data/evaluate.py --max-prs 2            # quick eval run
python eval_data/ragas_eval.py --data eval_data/prs.jsonl   # RAGAS metrics
```

---

*End of project description. Source of truth: the working tree at commit
`bd08fdb` + uncommitted changes, verified by code reading on 2026-08-12.*