Files
iishko-sandbox/patch_sbx_v3.py
T

49 lines
2.2 KiB
Python

#!/usr/bin/env python3
# patch_sbx_v3.py — добавляет в run_code: лог неудач (тип исключения) + 1 ретрай.
import time, shutil
SRC = "/opt/grok-bot/iishko/legacy.py"
A_OLD = ''' except Exception as e:
return "[run_code] sandbox unreachable: %s" % type(e).__name__'''
A_NEW = ''' except Exception as e:
try:
with open("/opt/grok-bot/sbx_calls.log", "a") as _lf:
_lf.write("%s UNREACH1 %s %r\\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), type(e).__name__, str(e)[:300]))
except Exception:
pass
try:
time.sleep(2)
import urllib.request
_req = urllib.request.Request(
"http://127.0.0.1:48979/v1/exec",
data=json.dumps({"code": code, "timeout_s": timeout_s}).encode(),
headers={"Content-Type": "application/json", "X-Sandbox-Key": _tok},
method="POST")
with urllib.request.urlopen(_req, timeout=timeout_s + 40) as _resp:
out = json.loads(_resp.read().decode("utf-8", "replace"))
except Exception as e2:
try:
with open("/opt/grok-bot/sbx_calls.log", "a") as _lf:
_lf.write("%s UNREACH2 %s %r\\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), type(e2).__name__, str(e2)[:300]))
except Exception:
pass
return "[run_code] sandbox unreachable: %s" % type(e2).__name__
if not out.get("ok"):
return "[run_code] error: %s" % str(out.get("error"))[:200]'''
src = open(SRC, encoding="utf-8").read()
if "UNREACH1" in src:
print("ALREADY PATCHED v3")
else:
assert src.count(A_OLD) == 1, "anchor not unique: %d" % src.count(A_OLD)
bak = SRC + ".bak-sbx3-" + time.strftime("%Y%m%dT%H%M%S")
shutil.copy2(SRC, bak)
src = src.replace(A_OLD, A_NEW)
open(SRC, "w", encoding="utf-8").write(src)
print("PATCHED OK, backup:", bak)
import py_compile
py_compile.compile(SRC, doraise=True)
print("compile OK")