fix: monkey-patch litellm acompletion timeout 600s for 9router review diffs

PR-Agent Dynaconf envvar_prefix=False -> ai_timeout=120 default, PR_AGENT_AI_TIMEOUT ignored.
monkey-patch acompletion() to clamp timeout<=120 -> 600 (9router needs ~15min for 15k-token diffs).
Removed ATLAS from fallback (9router rejects provider prefixes; ATLAS needs prefix).
litellm 1.96.2 -> 1.98.0 in /opt venv (fixes 'content' KeyError on 9router Anthropic resp).
Review verified: bot posted full PR Reviewer Guide to GMW PR #1.
This commit is contained in:
asepharyana
2026-08-25 17:10:41 +07:00
parent c26ef2bf71
commit 31f009bb89
+40 -1
View File
@@ -43,11 +43,18 @@ os.environ["OPENAI_API_KEY"] = omni_key
os.environ["CONFIG__MODEL"] = os.environ.get("PR_AGENT_MODEL", "claude-opus-5")
os.environ["CONFIG__FALLBACK_MODELS"] = os.environ.get(
"PR_AGENT_FALLBACK_MODELS",
'["claude-sonnet-5","ATLAS","claude-haiku-4-5-20251001"]',
'["claude-sonnet-5","claude-haiku-4-5-20251001"]',
)
os.environ["CONFIG__CUSTOM_MODEL_MAX_TOKENS"] = os.environ.get(
"PR_AGENT_MAX_TOKENS", "128000"
)
# 9router (omniroute) is latency-tolerant but PR-Agent's default litellm
# timeout (~60-90s) is too short for 15k-token review prompts ->
# AnthropicException Timeout. Raise it so the full context fits.
# PR-Agent uses Dynaconf with prefix `PR_AGENT`; the `ai_timeout` field
# lives under the [config] section, so the env key is `PR_AGENT__AI_TIMEOUT`.
os.environ.setdefault("PR_AGENT__AI_TIMEOUT", "300")
os.environ.setdefault("LITELLM_REQUEST_TIMEOUT", "300")
os.environ["GITHUB_APP__PR_COMMANDS"] = os.environ.get(
"PR_AGENT_PR_COMMANDS",
'["/review --pr_reviewer.require_score_review=true --pr_reviewer.require_security_review=true","/describe","/improve"]',
@@ -64,6 +71,7 @@ DISCORD_ALERT_WEBHOOK_URL = os.environ.get("DISCORD_ALERT_WEBHOOK_URL", "")
sys.path.insert(0, APP_DIR)
from pr_agent.servers.github_app import app as pr_agent_app, router as pr_router
from pr_agent.config_loader import get_settings
from fastapi import FastAPI, Request
import uvicorn
import httpx
@@ -75,6 +83,37 @@ from fastapi.responses import PlainTextResponse, JSONResponse
app = FastAPI(middleware=[Middleware(RawContextMiddleware)])
app.include_router(pr_router)
# ── Override litellm request timeout ──────────────────────────────────────
# PR-Agent's Dynaconf config uses `envvar_prefix=False` (env vars disabled)
# and `.toml` files only, so PR_AGENT__AI_TIMEOUT does NOT work.
# We monkey-patch the loaded settings + litellm global so the long
# 15k-token review diffs via 9router get 300s instead of the default 120s.
try:
_s = get_settings()
_s.config["ai_timeout"] = 300
except Exception:
pass
import litellm as _litellm
# Force a higher request timeout — PR-Agent passes `timeout=120` (from
# configuration.toml `ai_timeout=120`) to litellm.completion, which overrides
# the global `litellm.request_timeout`. 9router/claude-opus-5 needs more
# time for 15k-token review diffs. We wrap acompletion() to clamp the kwarg.
from pr_agent.algo.ai_handlers import litellm_ai_handler as _laih
_orig_acompletion = _laih.acompletion
async def _patched_acompletion(*args, **kwargs):
t = kwargs.get("timeout")
if t is not None and float(t) <= 120:
kwargs["timeout"] = 600
return await _orig_acompletion(*args, **kwargs)
_laih.acompletion = _patched_acompletion
_litellm.request_timeout = 600
# ── Analytics / Metrics ─────────────────────────────────────────────────────
def _read_analytics_logs(max_files: int = 5) -> list: