Spaces:
Running
Running
Deploy 40060d4f
Browse files- ARCHITECTURE.md +32 -0
- api.py +39 -1
- app.py +11 -2
- nodes/retrieval.py +7 -0
ARCHITECTURE.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OpenCodeReview architecture
|
| 2 |
+
|
| 3 |
+
The deployed path is intentionally remote-inference only. The API and Gradio
|
| 4 |
+
process orchestrate GitHub retrieval and LangGraph; Gemini, Groq, or another
|
| 5 |
+
configured OpenAI-compatible provider performs model inference. No model is
|
| 6 |
+
downloaded or executed by the hosted API by default.
|
| 7 |
+
|
| 8 |
+
## Runtime boundaries
|
| 9 |
+
|
| 10 |
+
- `api.py`: authenticated HTTP job lifecycle, persistence, approval, and
|
| 11 |
+
posting receipts.
|
| 12 |
+
- `app.py`: Gradio entry point and review orchestration adapter.
|
| 13 |
+
- `graph.py`, `nodes/`, `state.py`: LangGraph workflow and typed state.
|
| 14 |
+
- `github_client.py`: GitHub reads, caching, and write operations.
|
| 15 |
+
- `llm_factory.py`, `endpoints.py`: remote provider selection and fallback.
|
| 16 |
+
- `frontend/`: Vercel React client; it talks only to the API boundary.
|
| 17 |
+
- `eval_data/`: offline benchmark scorer and RAGAS evaluation utilities.
|
| 18 |
+
- `prompts/`: review prompts kept outside orchestration code.
|
| 19 |
+
|
| 20 |
+
## Local model policy
|
| 21 |
+
|
| 22 |
+
Local embeddings are disabled unless `OPENCODEREVIEW_ALLOW_LOCAL_EMBEDDINGS=true`
|
| 23 |
+
is explicitly set. This keeps laptops and hosted containers from pulling
|
| 24 |
+
PyTorch or embedding models. Retrieval gracefully returns no extra context and
|
| 25 |
+
the review continues with the PR diff when the flag is disabled.
|
| 26 |
+
|
| 27 |
+
## Persistence boundary
|
| 28 |
+
|
| 29 |
+
The API persists jobs in SQLite so a process restart can recover queued and
|
| 30 |
+
awaiting-approval jobs. SQLite on an ephemeral Space filesystem is not a
|
| 31 |
+
replacement for a managed database or mounted volume; production deployment
|
| 32 |
+
should point `DB_PATH` at durable storage before horizontal scaling.
|
api.py
CHANGED
|
@@ -53,6 +53,11 @@ def _load_job(job_id: str) -> dict[str, Any] | None:
|
|
| 53 |
row = conn.execute("SELECT payload FROM review_jobs WHERE id = ?", (job_id,)).fetchone()
|
| 54 |
return json.loads(row[0]) if row else None
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
class ReviewRequest(BaseModel):
|
| 58 |
repo: str = Field(pattern=r"^[\w.-]+/[\w.-]+$")
|
|
@@ -85,7 +90,11 @@ def _with_ragas(job: dict[str, Any]) -> dict[str, Any]:
|
|
| 85 |
elif task.get("error"):
|
| 86 |
result["ragas"] = {"status": "failed", "error": task.get("error")}
|
| 87 |
else:
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
return result
|
| 90 |
|
| 91 |
|
|
@@ -107,6 +116,35 @@ def _run_job(job_id: str, request: ReviewRequest) -> None:
|
|
| 107 |
_save_job(_jobs[job_id])
|
| 108 |
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
@app.get("/api/health")
|
| 111 |
def health() -> dict[str, str]:
|
| 112 |
return {"status": "ok"}
|
|
|
|
| 53 |
row = conn.execute("SELECT payload FROM review_jobs WHERE id = ?", (job_id,)).fetchone()
|
| 54 |
return json.loads(row[0]) if row else None
|
| 55 |
|
| 56 |
+
def _load_jobs() -> list[dict[str, Any]]:
|
| 57 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 58 |
+
rows = conn.execute("SELECT payload FROM review_jobs").fetchall()
|
| 59 |
+
return [json.loads(row[0]) for row in rows]
|
| 60 |
+
|
| 61 |
|
| 62 |
class ReviewRequest(BaseModel):
|
| 63 |
repo: str = Field(pattern=r"^[\w.-]+/[\w.-]+$")
|
|
|
|
| 90 |
elif task.get("error"):
|
| 91 |
result["ragas"] = {"status": "failed", "error": task.get("error")}
|
| 92 |
else:
|
| 93 |
+
scores = task.get("scores") or {}
|
| 94 |
+
if scores:
|
| 95 |
+
result["ragas"] = {"status": "completed", "scores": scores}
|
| 96 |
+
else:
|
| 97 |
+
result["ragas"] = {"status": "completed_no_scores", "scores": {}, "error": "RAGAS completed without metric values"}
|
| 98 |
return result
|
| 99 |
|
| 100 |
|
|
|
|
| 116 |
_save_job(_jobs[job_id])
|
| 117 |
|
| 118 |
|
| 119 |
+
def _hydrate_jobs() -> None:
|
| 120 |
+
"""Restore persisted jobs after a process restart and resume interrupted work."""
|
| 121 |
+
for persisted in _load_jobs():
|
| 122 |
+
job_id = persisted.get("id")
|
| 123 |
+
if not job_id:
|
| 124 |
+
continue
|
| 125 |
+
status = persisted.get("status")
|
| 126 |
+
_jobs[job_id] = persisted
|
| 127 |
+
if status in {"queued", "processing", "processing_decision"}:
|
| 128 |
+
# Decision processing cannot be safely replayed without an explicit
|
| 129 |
+
# user action; review execution can be resumed idempotently.
|
| 130 |
+
if status == "processing_decision":
|
| 131 |
+
_jobs[job_id]["status"] = "awaiting_approval"
|
| 132 |
+
_save_job(_jobs[job_id])
|
| 133 |
+
continue
|
| 134 |
+
try:
|
| 135 |
+
request = ReviewRequest(repo=persisted["repo"], pr_number=int(persisted["pr_number"]), endpoint=persisted.get("endpoint"))
|
| 136 |
+
except Exception:
|
| 137 |
+
_jobs[job_id].update(status="failed", error="Persisted job request is invalid")
|
| 138 |
+
_save_job(_jobs[job_id])
|
| 139 |
+
continue
|
| 140 |
+
_jobs[job_id]["status"] = "queued"
|
| 141 |
+
_save_job(_jobs[job_id])
|
| 142 |
+
_jobs[job_id]["future"] = _executor.submit(_run_job, job_id, request)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
_hydrate_jobs()
|
| 146 |
+
|
| 147 |
+
|
| 148 |
@app.get("/api/health")
|
| 149 |
def health() -> dict[str, str]:
|
| 150 |
return {"status": "ok"}
|
app.py
CHANGED
|
@@ -506,11 +506,20 @@ def _ragas_worker(
|
|
| 506 |
handler,
|
| 507 |
) -> None:
|
| 508 |
"""Daemon thread body: compute scores, stash them for the Timer poll."""
|
| 509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
with _ragas_lock:
|
| 511 |
entry = _ragas_tasks.get(task_id)
|
| 512 |
if entry is not None:
|
| 513 |
-
entry["scores"] = scores
|
| 514 |
entry["done"] = True
|
| 515 |
# Ship the Langfuse scores logged from this thread.
|
| 516 |
flush_langfuse()
|
|
|
|
| 506 |
handler,
|
| 507 |
) -> None:
|
| 508 |
"""Daemon thread body: compute scores, stash them for the Timer poll."""
|
| 509 |
+
try:
|
| 510 |
+
scores = _compute_and_log_ragas(inputs, repo, pr_number, run_trace_id, handler)
|
| 511 |
+
except Exception as exc:
|
| 512 |
+
logger.warning("RAGAS worker failed: %s", exc)
|
| 513 |
+
with _ragas_lock:
|
| 514 |
+
entry = _ragas_tasks.get(task_id)
|
| 515 |
+
if entry is not None:
|
| 516 |
+
entry["error"] = str(exc)[:500]
|
| 517 |
+
entry["done"] = True
|
| 518 |
+
return
|
| 519 |
with _ragas_lock:
|
| 520 |
entry = _ragas_tasks.get(task_id)
|
| 521 |
if entry is not None:
|
| 522 |
+
entry["scores"] = scores or {}
|
| 523 |
entry["done"] = True
|
| 524 |
# Ship the Langfuse scores logged from this thread.
|
| 525 |
flush_langfuse()
|
nodes/retrieval.py
CHANGED
|
@@ -484,6 +484,13 @@ def retrieval_node(state: OpenCodeReviewState) -> dict:
|
|
| 484 |
logger.info("No diff available — skipping retrieval")
|
| 485 |
return {}
|
| 486 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
# -- Parse identifiers ----------------------------------------------------
|
| 488 |
parts = state.repo.split("/")
|
| 489 |
if len(parts) != 2:
|
|
|
|
| 484 |
logger.info("No diff available — skipping retrieval")
|
| 485 |
return {}
|
| 486 |
|
| 487 |
+
# Hosted deployments must not download or execute embedding models on the
|
| 488 |
+
# application machine. Enable this only on a machine intentionally sized
|
| 489 |
+
# for local retrieval; the review itself always uses a remote LLM provider.
|
| 490 |
+
if os.getenv("OPENCODEREVIEW_ALLOW_LOCAL_EMBEDDINGS", "false").lower() not in {"1", "true", "yes"}:
|
| 491 |
+
logger.info("Local embeddings disabled; skipping vector retrieval")
|
| 492 |
+
return {}
|
| 493 |
+
|
| 494 |
# -- Parse identifiers ----------------------------------------------------
|
| 495 |
parts = state.repo.split("/")
|
| 496 |
if len(parts) != 2:
|