Files
weval-l99/fix_api_retry.py
2026-04-20 11:24:29 +02:00

27 lines
1.6 KiB
Python

#!/usr/bin/env python3
# V81: Add 2-attempt retry in api() function for both master + opus
# If first call returns empty response, retry once after 2s wait
for path in ['/var/www/html/api/nonreg-master.php', '/var/www/html/api/nonreg-opus.php']:
with open(path, 'rb') as f:
raw = f.read()
if b"V81: retry on empty" in raw:
print(f"{path}: ALREADY_PATCHED")
continue
# Find pattern: $r=curl_exec($ch);curl_close($ch);return json_decode($r,true)?:[];
old = b"$r=curl_exec($ch);curl_close($ch);return json_decode($r,true)?:[];"
new = b"$r=curl_exec($ch);curl_close($ch);$_d=json_decode($r,true)?:[];\n // V81: retry on empty response (Cerebras flaky)\n if (empty($_d['response'] ?? '')) {\n sleep(2);\n $ch2=curl_init(\"https://127.0.0.1/api/weval-ia-full\");\n curl_setopt_array($ch2,[CURLOPT_POST=>1,CURLOPT_RETURNTRANSFER=>1,CURLOPT_TIMEOUT=>$to+10,CURLOPT_SSL_VERIFYPEER=>false,CURLOPT_SSL_VERIFYHOST=>false,CURLOPT_HTTPHEADER=>['Content-Type: application/json','Host: weval-consulting.com','X-Source: nonreg-retry-v81'],CURLOPT_POSTFIELDS=>json_encode(['message'=>$msg,'mode'=>$mode])]);\n $r2=curl_exec($ch2);curl_close($ch2);$_d2=json_decode($r2,true)?:[];\n if (!empty($_d2['response'] ?? '')) $_d = $_d2;\n }\n return $_d;"
if old in raw:
raw = raw.replace(old, new, 1)
with open(path, 'wb') as f:
f.write(raw)
print(f"{path}: PATCHED size={len(raw)}")
else:
print(f"{path}: PATTERN_NOT_FOUND")
idx = raw.find(b"return json_decode")
if idx > 0:
print(f" context: {raw[idx-30:idx+80]!r}")