code_execution_rpc.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. """Host-side RPC servers for execute_code sandboxes.
  2. Two transports share one request pipeline (token check → allow-list → call
  3. budget → dispatch under output silence → log): ``_rpc_server_loop`` serves the
  4. local UDS/TCP socket, ``_rpc_poll_loop`` polls a remote filesystem for request
  5. files via ``env.execute()``.
  6. """
  7. import base64
  8. import json
  9. import logging
  10. import secrets
  11. import shlex
  12. import socket
  13. import threading
  14. import time
  15. from agent.thread_scoped_output import thread_scoped_silence
  16. from tools.registry import tool_error
  17. # Logger name kept as the origin module's so existing log expectations hold.
  18. logger = logging.getLogger("tools.code_execution_tool")
  19. # Terminal parameters that must not be used from ephemeral sandbox scripts.
  20. _TERMINAL_BLOCKED_PARAMS = {"background", "pty", "notify", "notify_on_complete", "watch_patterns", "heartbeat", "persist_on_release"}
  21. def _default_dispatch(task_id):
  22. from model_tools import handle_function_call
  23. return lambda tool_name, tool_args: handle_function_call(tool_name, tool_args, task_id=task_id)
  24. def _private_dirs_cmd(root: str, *subdirs: str) -> str:
  25. """Shell command creating *root* (and optional *subdirs* under it) owner-only
  26. on a shared host. ``umask 077`` makes intermediates and leaves private at
  27. creation (no mkdir-then-chmod window where a co-tenant could open a dir fd);
  28. ``chmod`` then repairs any named dir that already existed with permissive
  29. modes."""
  30. mkdir = " ".join(shlex.quote(d) for d in (subdirs or (root,)))
  31. chmod = " ".join(shlex.quote(d) for d in (root, *subdirs))
  32. return f"umask 077 && mkdir -p {mkdir} && chmod 700 {chmod}"
  33. def _execute_checked(env, cmd: str, what: str, *, timeout: int, **kwargs) -> dict:
  34. """Run *cmd* from ``/`` and raise ``RuntimeError`` on a non-zero exit.
  35. Used where a silent failure would ship secrets or code into a missing,
  36. half-written, or still-permissive remote path. The error carries the
  37. command output only, never the payload."""
  38. result = env.execute(cmd, cwd="/", timeout=timeout, **kwargs)
  39. if result.get("returncode", 1) != 0:
  40. raise RuntimeError(f"{what} failed: {result.get('output')!r}")
  41. return result
  42. def _remote_write(env, remote_path: str, content: str, *, atomic: bool = False,
  43. timeout: int = 30, check: bool = False):
  44. """Write *content* owner-only to *remote_path*; returns the execute() result
  45. (``check=True`` raises on failure via _execute_checked).
  46. The base64 payload always travels as ``stdin_data``: pipe-mode backends
  47. (ssh, docker, local: the real shared-host ones) deliver it on
  48. real stdin, so it never enters argv where a co-tenant can read it via
  49. ``/proc/*/cmdline``; ``BaseEnvironment.execute`` embeds it as a heredoc
  50. for heredoc-mode backends (modal, daytona, vercel), and managed_modal
  51. forwards it as ``stdinData``, the same contract ``_write_to_sandbox``
  52. relies on."""
  53. encoded = base64.b64encode(content.encode("utf-8")).decode("ascii")
  54. target = shlex.quote(remote_path)
  55. write = (f"base64 -d > {target}.tmp && mv -f {target}.tmp {target}"
  56. if atomic else f"base64 -d > {target}")
  57. cmd = f"umask 077 && {write}"
  58. if check:
  59. return _execute_checked(env, cmd, f"remote file ship for {remote_path!r}",
  60. timeout=timeout, stdin_data=encoded)
  61. return env.execute(cmd, cwd="/", timeout=timeout, stdin_data=encoded)
  62. def _rpc_token_ok(request: dict, rpc_token: str) -> bool:
  63. """Constant-time token check; an empty server token fails closed. Compared as bytes:
  64. compare_digest raises TypeError on a non-ASCII str, and the token is script-supplied JSON."""
  65. return bool(rpc_token) and secrets.compare_digest(
  66. str(request.get("token") or "").encode(), rpc_token.encode()
  67. )
  68. def _handle_rpc_request(request: dict, *, allowed_tools: frozenset, tool_call_counter: list,
  69. max_tool_calls: int, dispatch, tool_call_log: list, call_start: float,
  70. where: str) -> str:
  71. """Enforce allow-list + budget, then dispatch one authenticated request. Only a dispatched
  72. call consumes budget and is logged; refusals are free."""
  73. tool_name = request.get("tool", "")
  74. tool_args = request.get("args", {})
  75. if tool_name not in allowed_tools:
  76. return tool_error(f"Tool '{tool_name}' is not available in execute_code. "
  77. f"Available: {', '.join(sorted(allowed_tools))}")
  78. if tool_call_counter[0] >= max_tool_calls:
  79. return tool_error(f"Tool call limit reached ({max_tool_calls}). "
  80. "No more tool calls allowed in this execution.")
  81. if tool_name == "terminal" and isinstance(tool_args, dict):
  82. for param in _TERMINAL_BLOCKED_PARAMS:
  83. tool_args.pop(param, None)
  84. # Silence handler status prints so they don't leak into the CLI spinner.
  85. try:
  86. with thread_scoped_silence():
  87. result = dispatch(tool_name, tool_args)
  88. except Exception as exc:
  89. logger.error("Tool call failed in %s: %s", where, exc, exc_info=True)
  90. result = tool_error(str(exc))
  91. tool_call_counter[0] += 1
  92. entry = {"tool": tool_name, "args_preview": str(tool_args)[:80],
  93. "duration": round(time.monotonic() - call_start, 2)}
  94. error = _result_error(result)
  95. if error:
  96. entry["error"] = error
  97. tool_call_log.append(entry)
  98. return result
  99. def _result_error(result) -> str:
  100. """The ``error`` text of a JSON-object tool result, else ``""``."""
  101. if not isinstance(result, str) or not result.startswith("{") or '"error"' not in result:
  102. return ""
  103. try:
  104. body = json.loads(result)
  105. except ValueError:
  106. return ""
  107. error = body.get("error") if isinstance(body, dict) else None
  108. return str(error)[:300] if error else ""
  109. def tool_errors_since(tool_call_log: list, start: int = 0) -> list:
  110. """Failed in-script tool calls since *start*, for the execute_code result: a script that
  111. ignores a helper's ``{"error": ...}`` return would otherwise report plain success while
  112. the write/patch it relied on never happened."""
  113. return [{"tool": e["tool"], "error": e["error"]} for e in tool_call_log[start:] if e.get("error")][:5]
  114. def _rpc_server_loop(server_sock: socket.socket, task_id: str, tool_call_log: list,
  115. tool_call_counter: list, max_tool_calls: int, allowed_tools: frozenset,
  116. stop_event: threading.Event, rpc_token: str, dispatch=None):
  117. """Accept one client and serve newline-delimited JSON requests until it disconnects, idles
  118. 300s, or the call limit is reached. ``tool_call_counter`` is a mutable ``[int]``. ``dispatch``
  119. overrides how an allowed, budgeted call runs: per-call sandboxes use the default (the thread
  120. carries the cell's context); session kernels rebind each call to the CURRENT cell's authority.
  121. """
  122. if dispatch is None:
  123. dispatch = _default_dispatch(task_id)
  124. conn = None
  125. try:
  126. server_sock.settimeout(0.05)
  127. while not stop_event.is_set():
  128. try:
  129. conn, _ = server_sock.accept()
  130. break
  131. except TimeoutError:
  132. continue
  133. if conn is None:
  134. return
  135. conn.settimeout(300)
  136. buf = b""
  137. while True:
  138. try:
  139. chunk = conn.recv(65536)
  140. except TimeoutError:
  141. break
  142. if not chunk:
  143. break
  144. buf += chunk
  145. while b"\n" in buf:
  146. line, buf = buf.split(b"\n", 1)
  147. line = line.strip()
  148. if not line:
  149. continue
  150. call_start = time.monotonic()
  151. try:
  152. request = json.loads(line.decode())
  153. except (json.JSONDecodeError, UnicodeDecodeError) as exc:
  154. resp = tool_error(f"Invalid RPC request: {exc}")
  155. else:
  156. resp = _handle_rpc_request(
  157. request, allowed_tools=allowed_tools, tool_call_counter=tool_call_counter,
  158. max_tool_calls=max_tool_calls, dispatch=dispatch, tool_call_log=tool_call_log,
  159. call_start=call_start, where="sandbox",
  160. ) if _rpc_token_ok(request, rpc_token) else tool_error("Unauthorized RPC request")
  161. conn.sendall((resp + "\n").encode())
  162. except TimeoutError:
  163. logger.debug("RPC listener socket timeout")
  164. except OSError as e:
  165. logger.debug("RPC listener socket error: %s", e, exc_info=True)
  166. finally:
  167. if conn:
  168. try:
  169. conn.close()
  170. except OSError as e:
  171. logger.debug("RPC conn close error: %s", e)
  172. def _rpc_poll_loop(env, rpc_dir: str, task_id: str, tool_call_log: list, tool_call_counter: list,
  173. max_tool_calls: int, allowed_tools: frozenset, stop_event: threading.Event,
  174. rpc_token: str):
  175. """Poll the remote filesystem for request files and answer them. Background thread; each
  176. ``env.execute()`` is an independent process, so this is safe alongside the script-execution
  177. thread. Malformed or unauthorized requests are removed without a response."""
  178. dispatch = _default_dispatch(task_id)
  179. poll_interval = 0.1
  180. quoted_rpc_dir = shlex.quote(rpc_dir)
  181. while not stop_event.is_set():
  182. try:
  183. ls_result = env.execute(f"ls -1 {quoted_rpc_dir}/req_* 2>/dev/null || true", cwd="/", timeout=10)
  184. output = ls_result.get("output", "").strip()
  185. if not output:
  186. stop_event.wait(poll_interval)
  187. continue
  188. req_files = sorted(f for f in (line.strip() for line in output.split("\n"))
  189. if f and not f.endswith(".tmp") and "/req_" in f)
  190. for req_file in req_files:
  191. if stop_event.is_set():
  192. break
  193. call_start = time.monotonic()
  194. quoted_req_file = shlex.quote(req_file)
  195. read_result = env.execute(f"cat {quoted_req_file}", cwd="/", timeout=10)
  196. try:
  197. request = json.loads(read_result.get("output", ""))
  198. except (json.JSONDecodeError, ValueError):
  199. logger.debug("Malformed RPC request in %s", req_file)
  200. env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)
  201. continue
  202. if not _rpc_token_ok(request, rpc_token):
  203. logger.debug("Unauthorized RPC request in %s", req_file)
  204. env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)
  205. continue
  206. seq = request.get("seq", 0)
  207. if not isinstance(seq, int):
  208. # A non-int seq cannot form the res_NNNNNN name the caller
  209. # polls; formatting it after dispatch would raise, leave the
  210. # request in place, and replay the tool call every cycle.
  211. logger.debug("RPC request with malformed seq in %s", req_file)
  212. env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)
  213. continue
  214. tool_result = _handle_rpc_request(
  215. request, allowed_tools=allowed_tools, tool_call_counter=tool_call_counter,
  216. max_tool_calls=max_tool_calls, dispatch=dispatch, tool_call_log=tool_call_log,
  217. call_start=call_start, where="remote sandbox",
  218. )
  219. # Atomic (tmp + rename) and owner-only; results carry tool output
  220. # on a shared-host backend.
  221. _remote_write(env, f"{rpc_dir}/res_{seq:06d}", tool_result,
  222. atomic=True, timeout=60)
  223. env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)
  224. except Exception as e:
  225. if not stop_event.is_set():
  226. logger.debug("RPC poll error: %s", e, exc_info=True)
  227. if not stop_event.is_set():
  228. stop_event.wait(poll_interval)