refactor: enhance fallback review to include detailed failure reasons from OpenAI API
This commit is contained in:
@@ -166,8 +166,42 @@ def _call_openai_review(settings: Settings, prompt: str) -> dict[str, Any]:
|
||||
raise ReviewError("OpenAI response did not contain JSON output text.")
|
||||
|
||||
|
||||
def _fallback_review(diff_context: dict[str, Any]) -> dict[str, Any]:
|
||||
findings = []
|
||||
def _summarize_openai_failure(exc: Exception) -> str:
|
||||
if isinstance(exc, httpx.HTTPStatusError):
|
||||
status = exc.response.status_code
|
||||
response_text = exc.response.text.strip()
|
||||
if response_text:
|
||||
compact = " ".join(response_text.split())
|
||||
if len(compact) > 400:
|
||||
compact = f"{compact[:400]}..."
|
||||
return f"OpenAI API HTTP {status}: {compact}"
|
||||
return f"OpenAI API HTTP {status}."
|
||||
if isinstance(exc, httpx.TimeoutException):
|
||||
return "OpenAI API request timed out."
|
||||
message = str(exc).strip()
|
||||
if message:
|
||||
return message
|
||||
return f"{exc.__class__.__name__} (no details)"
|
||||
|
||||
|
||||
def _fallback_review(diff_context: dict[str, Any], *, failure_reason: str | None = None) -> dict[str, Any]:
|
||||
findings: list[dict[str, Any]] = []
|
||||
summary = "Fallback analysis was used because OpenAI review was unavailable."
|
||||
|
||||
if failure_reason:
|
||||
summary = f"OpenAI review failed. Error: {failure_reason}"
|
||||
findings.append(
|
||||
{
|
||||
"severity": "high",
|
||||
"file": "unknown",
|
||||
"line_start": 1,
|
||||
"line_end": 1,
|
||||
"title": "OpenAI review request failed",
|
||||
"body": failure_reason,
|
||||
"suggestion": "Fix API/auth/network issues and rerun @codex review.",
|
||||
}
|
||||
)
|
||||
|
||||
if "TODO" in diff_context["diff"]:
|
||||
findings.append(
|
||||
{
|
||||
@@ -183,7 +217,7 @@ def _fallback_review(diff_context: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"verdict": "correct" if not findings else "has_issues",
|
||||
"confidence": 0.4 if not findings else 0.6,
|
||||
"summary": "Fallback analysis was used because OpenAI review was unavailable.",
|
||||
"summary": summary,
|
||||
"findings": findings,
|
||||
}
|
||||
|
||||
@@ -198,8 +232,8 @@ def run_review_for_pr(
|
||||
prompt, diff_context, repo_cfg = prepare_review_prompt(settings, gitea, repo, pr_number, command)
|
||||
try:
|
||||
result = _call_openai_review(settings, prompt)
|
||||
except Exception:
|
||||
result = _fallback_review(diff_context)
|
||||
except Exception as exc:
|
||||
result = _fallback_review(diff_context, failure_reason=_summarize_openai_failure(exc))
|
||||
return normalize_review_result(result), repo_cfg
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user