Spaces:
Running
Running
feat: resume ragas tasks after restart
Browse files
api.py
CHANGED
|
@@ -21,7 +21,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 21 |
from langgraph.types import Command
|
| 22 |
from pydantic import BaseModel, Field
|
| 23 |
|
| 24 |
-
from app import DB_PATH, _build_and_stream, _normalize_endpoint_name, _ragas_tasks, _ragas_lock
|
| 25 |
from graph import build_graph
|
| 26 |
from observability import TokenCostCallback, flush_langfuse, get_langfuse_handler
|
| 27 |
from state import Finding, Verdict
|
|
@@ -184,11 +184,11 @@ def _run_job(job_id: str, request: ReviewRequest) -> None:
|
|
| 184 |
_jobs[job_id]["status"] = "processing"
|
| 185 |
_save_job(_jobs[job_id])
|
| 186 |
try:
|
| 187 |
-
state, config, cost, ragas = _build_and_stream(request.repo, request.pr_number, _normalize_endpoint_name(request.endpoint), ragas_background=True)
|
| 188 |
verdict: Verdict | None = state.values.get("verdict")
|
| 189 |
findings: list[Finding] = state.values.get("final_findings", [])
|
| 190 |
with _lock:
|
| 191 |
-
_jobs[job_id].update(status="awaiting_approval", config=config, verdict=verdict.model_dump(mode="json") if verdict else None, findings=[item.model_dump(mode="json") for item in findings], cost=cost, ragas_task_id=ragas if isinstance(ragas, str) else None, ragas_scores=ragas if isinstance(ragas, dict) else None)
|
| 192 |
_save_job(_jobs[job_id])
|
| 193 |
except Exception as exc:
|
| 194 |
with _lock:
|
|
@@ -204,6 +204,16 @@ def _hydrate_jobs() -> None:
|
|
| 204 |
continue
|
| 205 |
status = persisted.get("status")
|
| 206 |
_jobs[job_id] = persisted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
if status in {"queued", "processing", "processing_decision"}:
|
| 208 |
# Decision processing cannot be safely replayed without an explicit
|
| 209 |
# user action; review execution can be resumed idempotently.
|
|
|
|
| 21 |
from langgraph.types import Command
|
| 22 |
from pydantic import BaseModel, Field
|
| 23 |
|
| 24 |
+
from app import DB_PATH, _build_and_stream, _normalize_endpoint_name, _ragas_tasks, _ragas_lock, _spawn_ragas_background
|
| 25 |
from graph import build_graph
|
| 26 |
from observability import TokenCostCallback, flush_langfuse, get_langfuse_handler
|
| 27 |
from state import Finding, Verdict
|
|
|
|
| 184 |
_jobs[job_id]["status"] = "processing"
|
| 185 |
_save_job(_jobs[job_id])
|
| 186 |
try:
|
| 187 |
+
state, config, cost, ragas, ragas_inputs = _build_and_stream(request.repo, request.pr_number, _normalize_endpoint_name(request.endpoint), ragas_background=True)
|
| 188 |
verdict: Verdict | None = state.values.get("verdict")
|
| 189 |
findings: list[Finding] = state.values.get("final_findings", [])
|
| 190 |
with _lock:
|
| 191 |
+
_jobs[job_id].update(status="awaiting_approval", config=config, verdict=verdict.model_dump(mode="json") if verdict else None, findings=[item.model_dump(mode="json") for item in findings], cost=cost, ragas_task_id=ragas if isinstance(ragas, str) else None, ragas_scores=ragas if isinstance(ragas, dict) else None, ragas_inputs=ragas_inputs or None)
|
| 192 |
_save_job(_jobs[job_id])
|
| 193 |
except Exception as exc:
|
| 194 |
with _lock:
|
|
|
|
| 204 |
continue
|
| 205 |
status = persisted.get("status")
|
| 206 |
_jobs[job_id] = persisted
|
| 207 |
+
if persisted.get("ragas_inputs") and persisted.get("ragas_task_id") and not persisted.get("ragas_scores"):
|
| 208 |
+
new_task = _spawn_ragas_background(
|
| 209 |
+
persisted["ragas_inputs"],
|
| 210 |
+
persisted.get("repo", ""),
|
| 211 |
+
int(persisted.get("pr_number", 0)),
|
| 212 |
+
None,
|
| 213 |
+
None,
|
| 214 |
+
)
|
| 215 |
+
persisted["ragas_task_id"] = new_task
|
| 216 |
+
_save_job(persisted)
|
| 217 |
if status in {"queued", "processing", "processing_decision"}:
|
| 218 |
# Decision processing cannot be safely replayed without an explicit
|
| 219 |
# user action; review execution can be resumed idempotently.
|