Skip to content

Commit 95c79e1

Browse files
committed
fix: revert unnecessary sandbox backend changes to fix type errors
Align wasm_backend.py, e2b_backend.py, and test_wasm_backend.py with version-sandboxes branch. The changes on this branch introduced type errors by using wrong type ignore codes and adding unnecessary helper functions.
1 parent 279e4e9 commit 95c79e1

3 files changed

Lines changed: 11 additions & 33 deletions

File tree

src/phoenix/server/sandbox/e2b_backend.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ def __init__(
4545
self._sessions: dict[str, Any] = {}
4646

4747
def _get_sandbox_cls(self) -> Any:
48-
try:
49-
from e2b_code_interpreter import AsyncSandbox # type: ignore[import-untyped]
48+
from e2b_code_interpreter import AsyncSandbox # type: ignore[import-not-found]
5049

51-
return AsyncSandbox
52-
except ImportError:
53-
raise
50+
return AsyncSandbox
5451

5552
def _create_kwargs(self) -> dict[str, Any]:
5653
"""Build kwargs for AsyncSandbox.create().

src/phoenix/server/sandbox/wasm_backend.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from pathlib import Path
2121
from typing import Any, Optional
2222

23+
import wasmtime # type: ignore[import-not-found]
24+
2325
from .types import (
2426
BaseNoSessionBackend,
2527
ExecutionResult,
@@ -38,21 +40,11 @@
3840
# Module-level cache: path → (engine, compiled module).
3941
# Engine and module must be paired — a module compiled with one engine
4042
# cannot be used with a store from a different engine.
41-
_MODULE_CACHE: dict[str, tuple[Any, Any]] = {}
42-
43-
44-
def _get_wasmtime() -> Any:
45-
try:
46-
import wasmtime
47-
48-
return wasmtime
49-
except ImportError:
50-
raise
43+
_MODULE_CACHE: dict[str, tuple[wasmtime.Engine, wasmtime.Module]] = {}
5144

5245

53-
def _get_engine_and_module(binary_path: Path) -> tuple[Any, Any]:
46+
def _get_engine_and_module(binary_path: Path) -> tuple[wasmtime.Engine, wasmtime.Module]:
5447
"""Return a cached (engine, module) pair, compiling on first use."""
55-
wasmtime = _get_wasmtime()
5648
cache_key = str(binary_path)
5749
if cache_key not in _MODULE_CACHE:
5850
engine_cfg = wasmtime.Config()
@@ -65,7 +57,6 @@ def _get_engine_and_module(binary_path: Path) -> tuple[Any, Any]:
6557

6658
def _run_wasm(binary_path: Path, code: str, timeout: int) -> ExecutionResult:
6759
"""Execute *code* in a wasmtime WASI context. Runs in a thread."""
68-
wasmtime = _get_wasmtime()
6960
stdout_buf = io.StringIO()
7061
stderr_buf = io.StringIO()
7162
stdin_path: str | None = None

tests/unit/server/api/test_wasm_backend.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,13 @@ class TestEngineCaching:
113113
"""Engine and module are cached together to avoid cross-engine misuse."""
114114

115115
def test_get_engine_and_module_caches_by_path(self) -> None:
116-
from phoenix.server.sandbox.wasm_backend import (
117-
_MODULE_CACHE,
118-
_get_engine_and_module,
119-
_get_wasmtime,
120-
)
116+
from phoenix.server.sandbox.wasm_backend import _MODULE_CACHE, _get_engine_and_module
121117

122118
fake_path = Path("/fake/test-cache.wasm")
123119
cache_key = str(fake_path)
124120
_MODULE_CACHE.pop(cache_key, None)
125121

126-
wasmtime = _get_wasmtime()
122+
import wasmtime # type: ignore[import-not-found]
127123

128124
with patch.object(wasmtime.Module, "from_file", return_value="fake_module"):
129125
engine1, mod1 = _get_engine_and_module(fake_path)
@@ -134,18 +130,14 @@ def test_get_engine_and_module_caches_by_path(self) -> None:
134130
_MODULE_CACHE.pop(cache_key, None)
135131

136132
def test_different_paths_get_different_caches(self) -> None:
137-
from phoenix.server.sandbox.wasm_backend import (
138-
_MODULE_CACHE,
139-
_get_engine_and_module,
140-
_get_wasmtime,
141-
)
133+
from phoenix.server.sandbox.wasm_backend import _MODULE_CACHE, _get_engine_and_module
142134

143135
path_a = Path("/fake/a.wasm")
144136
path_b = Path("/fake/b.wasm")
145137
_MODULE_CACHE.pop(str(path_a), None)
146138
_MODULE_CACHE.pop(str(path_b), None)
147139

148-
wasmtime = _get_wasmtime()
140+
import wasmtime
149141

150142
with patch.object(wasmtime.Module, "from_file", return_value="fake_module"):
151143
engine_a, _ = _get_engine_and_module(path_a)
@@ -166,9 +158,7 @@ def test_stdin_temp_file_is_deleted_after_run(self) -> None:
166158

167159
def test_no_env_inherited_into_sandbox(self) -> None:
168160
"""Verify wasi.inherit_env() is not called — user code gets no server env."""
169-
from phoenix.server.sandbox.wasm_backend import _get_wasmtime
170-
171-
wasmtime = _get_wasmtime()
161+
import wasmtime
172162

173163
with patch.object(wasmtime.Module, "from_file"):
174164
with patch.object(wasmtime.WasiConfig, "inherit_env") as mock_inherit:

0 commit comments

Comments
 (0)