-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathsandbox_executor.py
More file actions
229 lines (185 loc) · 6.95 KB
/
sandbox_executor.py
File metadata and controls
229 lines (185 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# /// script
# dependencies = [
# "modal==1.1.0",
# "websockets==15.0.1",
# ]
# ///
# # Stateful Sandbox Code Executor backed by Jupyter
#
# In this example, we demonstrate how to build a Sandbox executor, where the Python
# interpreter state persist. By using a Jupyter kernel, we can distinguish between
# the result of an expression and the output of `stdout` and `stderr`. To run this
# example use: `uv run sandbox_executor.py`.
# ## Defining the `SandboxExecutor`
# First, we define all the dependencies and dataclasses for holding the return
# values from the `SandboxExecutor`.
import json
import re
import secrets
import uuid
from dataclasses import dataclass, field
from http.client import RemoteDisconnected
from time import sleep
from typing import Optional
from urllib.error import HTTPError
from urllib.request import Request, urlopen
import modal
from websockets.sync.client import connect
from websockets.sync.connection import Connection
@dataclass
class ExecutionError:
name: str
value: str
traceback: list[str]
@dataclass
class ExecutionResults:
result: Optional[str] = None
logs: list[str] = field(default_factory=list)
error: Optional[ExecutionError] = None
# The `SandboxExecutor` creates a Modal sandbox and starts a Jupyter Kernel gateway to
# run code in a Jupyter kernel.
@dataclass
class SandboxExecutor:
"""Sandbox executor backed by Modal sandboxes."""
app_name: str
packages: list[str] = field(default_factory=list)
app: modal.App = field(init=False)
sandbox: modal.Sandbox = field(init=False)
ws_url: str = field(init=False)
def __post_init__(self):
image = modal.Image.debian_slim().uv_pip_install(
"jupyter_kernel_gateway", "ipykernel", *self.packages
)
self.app = modal.App.lookup(self.app_name, create_if_missing=True)
jupyter_port = 8888
token = secrets.token_urlsafe(13)
token_secret = modal.Secret.from_dict({"KG_AUTH_TOKEN": token})
entrypoint = [
"jupyter",
"kernelgateway",
"--KernelGatewayApp.ip='0.0.0.0'",
f"--KernelGatewayApp.port={jupyter_port}",
"--KernelGatewayApp.allow_origin='*'",
]
with modal.enable_output():
self.sandbox = modal.Sandbox.create(
*entrypoint,
app=self.app,
image=image,
secrets=[token_secret],
encrypted_ports=[jupyter_port],
timeout=60 * 60,
)
tunnel = self.sandbox.tunnels()[jupyter_port]
self._wait_for_server(tunnel.host, token)
kernel_id = self._start_kernel(tunnel.host, token)
self.ws_url = (
f"wss://{tunnel.host}/api/kernels/{kernel_id}/channels?token={token}"
)
def run_code(self, code: str) -> ExecutionResults:
with connect(self.ws_url) as ws:
return self._run_code(ws, code)
def terminate(self):
self.sandbox.terminate()
@classmethod
def _send_execute_request(cls, ws: Connection, code: str) -> str:
msg_id = str(uuid.uuid4())
execute_request = {
"header": {
"msg_id": msg_id,
"username": "anonymous",
"session": str(uuid.uuid4()),
"msg_type": "execute_request",
"version": "5.0",
},
"parent_header": {},
"metadata": {},
"content": {
"code": code,
"silent": False,
"store_history": True,
"user_expressions": {},
"allow_stdin": False,
},
}
ws.send(json.dumps(execute_request))
return msg_id
@classmethod
def _run_code(cls, ws: Connection, code: str) -> ExecutionResults:
"""Run code on the websocket connection."""
msg_id = cls._send_execute_request(ws, code)
result = None
logs = []
while True:
msg = json.loads(ws.recv())
parent_msg_id = msg.get("parent_header", {}).get("msg_id")
if parent_msg_id != msg_id:
continue
msg_type = msg.get("msg_type", "")
msg_content = msg.get("content", {})
if msg_type == "stream":
logs.append(msg_content["text"])
elif msg_type == "execute_result":
# Only support text for this simple example
result = msg_content["data"].get("text/plain", None)
elif msg_type == "error":
traceback = [
_strip_ansi_colors(line)
for line in msg_content.get("traceback", [])
]
error = ExecutionError(
name=msg_content.get("ename", ""),
value=msg_content.get("evalue", ""),
traceback=traceback,
)
return ExecutionResults(error=error)
elif msg_type == "status" and msg_content["execution_state"] == "idle":
break
return ExecutionResults(result=result, logs=logs)
@classmethod
def _wait_for_server(cls, host: str, token: str):
"""Wait for server to start up."""
counter = 0
req = Request(f"https://{host}/api/kernelspecs?token={token}", method="GET")
while True:
try:
with urlopen(req):
pass
break
except (HTTPError, RemoteDisconnected):
counter += 1
if counter > 100:
raise RuntimeError("Unable to connect to sandbox")
sleep(1.0)
@classmethod
def _start_kernel(cls, host: str, token: str) -> str:
"""Start kernel."""
kernels_url = f"https://{host}/api/kernels?token={token}"
req = Request(kernels_url, method="POST")
with urlopen(req) as response:
body = response.read()
json_resp = json.loads(body)
return json_resp["id"]
ANSI_ESCAPE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
def _strip_ansi_colors(text: str) -> str:
"""Remove ansi colors from text."""
return ANSI_ESCAPE.sub("", text)
# ## Using the API
# We create a `SandboxExecutor` with an `app_name` and run code using the `run_code`
# API.
executor = SandboxExecutor(app_name="jupyter-executor")
# When running `a = 10` there are no outputs because the code does not have a result,
# error, or evaluating an expression.
output = executor.run_code("a = 10")
print(output)
# When running `a`, the output consist of a `results` that is the value of `a`.
output = executor.run_code("a")
print(output.result)
# When running `print(a + 100)`, the output are logs, because the express prints to
# `stdout`.
output = executor.run_code("print(a + 100)")
print(output.logs)
# When running `a / 0`, the output is an error with the error name, value and traceback.
output = executor.run_code("a/0")
print(output.error)
executor.terminate()