-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexecutor_base.py
More file actions
177 lines (139 loc) · 5.05 KB
/
executor_base.py
File metadata and controls
177 lines (139 loc) · 5.05 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
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Generator, Sequence
from dataclasses import dataclass
from enum import StrEnum
from typing import Literal, Protocol
def wrap_last_line_interactive(code: str) -> str:
"""
Wrap user code to execute in last-line-interactive mode.
This uses Python's 'single' compilation mode for the last expression only,
which automatically prints the value to stdout, mimicking Jupyter notebook behavior.
Only the last line is affected; earlier expressions are not printed.
Args:
code: The Python code to wrap
Returns:
Wrapped Python code that will print the last expression's value if it's a bare expression
"""
# Escape the code string for embedding in Python source
code_escaped = code.replace("\\", "\\\\").replace("'", "\\'")
wrapper = f"""import ast
import sys
# User code
code = '''{code_escaped}'''
# Parse the code
tree = ast.parse(code)
# Execute all statements except the last one normally
if len(tree.body) > 0:
for node in tree.body[:-1]:
code_obj = compile(ast.Module(body=[node], type_ignores=[]), '<stdin>', 'exec')
exec(code_obj)
# For the last statement, check if it's an expression
last_node = tree.body[-1]
if isinstance(last_node, ast.Expr):
# Execute in 'single' mode to print the result
interactive = ast.Interactive(body=[last_node])
ast.fix_missing_locations(interactive)
code_obj = compile(interactive, '<stdin>', 'single')
exec(code_obj)
else:
# Not an expression, execute normally
code_obj = compile(ast.Module(body=[last_node], type_ignores=[]), '<stdin>', 'exec')
exec(code_obj)
"""
return wrapper
@dataclass(frozen=True, slots=True)
class ExecutionResult:
stdout: str
stderr: str
exit_code: int | None
timed_out: bool
duration_ms: int
files: tuple[WorkspaceEntry, ...]
class EntryKind(StrEnum):
FILE = "file"
DIRECTORY = "directory"
@dataclass(frozen=True, slots=True)
class WorkspaceEntry:
path: str
kind: EntryKind
content: bytes | None = None
@dataclass(frozen=True, slots=True)
class StreamChunk:
"""A chunk of output from the execution."""
stream: Literal["stdout", "stderr"]
data: str
@dataclass(frozen=True, slots=True)
class StreamResult:
"""Final execution result emitted at end of stream."""
exit_code: int | None
timed_out: bool
duration_ms: int
files: tuple[WorkspaceEntry, ...]
StreamEvent = StreamChunk | StreamResult
@dataclass(frozen=True, slots=True)
class HealthCheck:
"""Result of an executor health check."""
status: Literal["ok", "error"]
message: str | None = None
class ExecutorProtocol(Protocol):
def execute_python(
self,
*,
code: str,
stdin: str | None,
timeout_ms: int,
max_output_bytes: int,
cpu_time_limit_sec: int | None = None,
memory_limit_mb: int | None = None,
files: Sequence[tuple[str, bytes]] | None = None,
last_line_interactive: bool = True,
) -> ExecutionResult: ...
class BaseExecutor(ABC):
def check_health(self) -> HealthCheck:
"""Check if the executor backend is operational.
Default implementation returns ok. Override for backend-specific checks.
"""
return HealthCheck(status="ok")
@abstractmethod
def execute_python(
self,
*,
code: str,
stdin: str | None,
timeout_ms: int,
max_output_bytes: int,
cpu_time_limit_sec: int | None = None,
memory_limit_mb: int | None = None,
files: Sequence[tuple[str, bytes]] | None = None,
last_line_interactive: bool = True,
) -> ExecutionResult:
"""Execute Python code in an isolated environment.
Args:
last_line_interactive: If True, the last line will print its value to stdout
if it's a bare expression (only the last line is affected).
"""
def execute_python_streaming(
self,
*,
code: str,
stdin: str | None,
timeout_ms: int,
max_output_bytes: int,
cpu_time_limit_sec: int | None = None,
memory_limit_mb: int | None = None,
files: Sequence[tuple[str, bytes]] | None = None,
last_line_interactive: bool = True,
) -> Generator[StreamEvent, None, None]:
"""Execute Python code and yield output chunks as they arrive.
Yields StreamChunk events during execution, then a single StreamResult
at the end. Default implementation raises NotImplementedError.
"""
raise NotImplementedError(f"{type(self).__name__} does not support streaming execution")
@staticmethod
def truncate_output(stream: bytes, max_bytes: int) -> str:
if len(stream) <= max_bytes:
return stream.decode("utf-8", errors="replace")
head = stream[: max(0, max_bytes - 32)]
suffix = b"\n...[truncated]"
return (head + suffix).decode("utf-8", errors="replace")