add bot patch script
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
# patch_sbx_v2.py — добавляет агенту бота инструмент run_code (E2B-песочница).
|
||||
# Точные якоря + проверка единственности вхождения. Запуск на проде: python3 /tmp/patch_sbx_v2.py
|
||||
import shutil, sys, time
|
||||
|
||||
SRC = "/opt/grok-bot/iishko/legacy.py"
|
||||
BAK = SRC + ".bak-sbx2-" + time.strftime("%Y%m%dT%H%M%S")
|
||||
|
||||
src = open(SRC, encoding="utf-8").read()
|
||||
if "run_code" in src:
|
||||
print("ALREADY PATCHED"); sys.exit(0)
|
||||
|
||||
# --- якорь 1: конец BOT_TOOLS (current_time) ---
|
||||
A1 = '"parameters": {"type": "object", "properties": {}}\n }}\n]\n'
|
||||
assert src.count(A1) == 1, "A1 count=%d" % src.count(A1)
|
||||
|
||||
N1 = ('"parameters": {"type": "object", "properties": {}}\n }},\n'
|
||||
' {"type": "function", "function": {\n'
|
||||
' "name": "run_code",\n'
|
||||
' "description": "Execute Python code in an isolated sandbox (Python 3.11, no network access, no secrets, fresh disposable VM). Use it for exact math, data processing, parsing, algorithms, computed results. Input: a full runnable python script. Returns stdout/stderr and exit code.",\n'
|
||||
' "parameters": {"type": "object", "properties": {\n'
|
||||
' "code": {"type": "string", "description": "Python 3.11 script to run (no network, no persisted files)"},\n'
|
||||
' "timeout_s": {"type": "integer", "description": "optional timeout in seconds, max 60"}\n'
|
||||
' }, "required": ["code"]}\n'
|
||||
' }}\n'
|
||||
']\n')
|
||||
src = src.replace(A1, N1, 1)
|
||||
|
||||
# --- якорь 2: начало _bot_tool_exec ---
|
||||
A2 = 'async def _bot_tool_exec(name, args):\n try:\n'
|
||||
assert src.count(A2) == 1, "A2 count=%d" % src.count(A2)
|
||||
|
||||
BRANCH = (
|
||||
' if name == "run_code":\n'
|
||||
' code = (args or {}).get("code") or ""\n'
|
||||
' try:\n'
|
||||
' timeout_s = int((args or {}).get("timeout_s") or 60)\n'
|
||||
' except Exception:\n'
|
||||
' timeout_s = 60\n'
|
||||
' timeout_s = max(5, min(timeout_s, 60))\n'
|
||||
' if not code.strip():\n'
|
||||
' return "[run_code] empty code"\n'
|
||||
' if len(code) > 16000:\n'
|
||||
' return "[run_code] code too large (max 16000 bytes)"\n'
|
||||
' try:\n'
|
||||
' _tok = open("/opt/grok-bot/sb_token.txt").read().strip()\n'
|
||||
' except Exception:\n'
|
||||
' return "[run_code] sandbox unavailable (no token)"\n'
|
||||
' try:\n'
|
||||
' import urllib.request\n'
|
||||
' _req = urllib.request.Request(\n'
|
||||
' "http://127.0.0.1:48979/v1/exec",\n'
|
||||
' data=json.dumps({"code": code, "timeout_s": timeout_s}).encode(),\n'
|
||||
' headers={"Content-Type": "application/json", "X-Sandbox-Key": _tok},\n'
|
||||
' method="POST")\n'
|
||||
' with urllib.request.urlopen(_req, timeout=timeout_s + 40) as _resp:\n'
|
||||
' out = json.loads(_resp.read().decode("utf-8", "replace"))\n'
|
||||
' except Exception as e:\n'
|
||||
' return "[run_code] sandbox unreachable: %s" % type(e).__name__\n'
|
||||
' if out.get("timeout"):\n'
|
||||
' return "[run_code] execution timed out after %ss (sandbox killed)" % out.get("timeout_s")\n'
|
||||
' if not out.get("ok"):\n'
|
||||
' return "[run_code] error: %s" % str(out.get("error"))[:200]\n'
|
||||
' _parts = []\n'
|
||||
' if out.get("stdout"):\n'
|
||||
' _parts.append("stdout:\\n" + str(out["stdout"])[:3500])\n'
|
||||
' if out.get("stderr"):\n'
|
||||
' _parts.append("stderr:\\n" + str(out["stderr"])[:1500])\n'
|
||||
' _parts.append("exit_code: %s, elapsed: %ss" % (out.get("exit_code"), out.get("elapsed_s")))\n'
|
||||
' return "\\n".join(_parts) or "[run_code] no output"\n'
|
||||
)
|
||||
src = src.replace(A2, A2 + BRANCH, 1)
|
||||
|
||||
shutil.copy2(SRC, BAK)
|
||||
open(SRC, "w", encoding="utf-8").write(src)
|
||||
import py_compile
|
||||
py_compile.compile(SRC, doraise=True)
|
||||
print("PATCHED OK, backup:", BAK)
|
||||
Reference in New Issue
Block a user