DSPy: The framework for programming—not prompting—language models
Python
Isolate the PythonInterpreter Deno cache (#10206)
Isaac Millerab8a7658d8a8
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.pyindex 2f881d4c0..fe5d1c7ed 100644--- a/dspy/primitives/python_interpreter.py+++ b/dspy/primitives/python_interpreter.py@@ -75,6 +75,11 @@ def _canonicalize_path(path: PathLike | str) -> str:return os.path.realpath(os.path.expanduser(os.fspath(path)))+def _paths_overlap(first: str, second: str) -> bool:+ first, second = (os.path.normcase(path).rstrip(os.sep) for path in (first, second))+ return first == second or first.startswith(second + os.sep) or second.startswith(first + os.sep)++def _find_deno_executable() -> str:"""Prefer the Deno binary managed by the optional Python package."""try:@@ -299,6 +304,9 @@ class PythonInterpreter:# Also allow reading Deno's cache directory so Pyodide can load its filesdeno_dir = self._get_deno_dir(deno_executable)+ protected = [_canonicalize_path(self._get_runner_path()), *([_canonicalize_path(deno_dir)] if deno_dir else [])]+ if any(_paths_overlap(_canonicalize_path(path), item) for path in self.enable_write_paths for item in protected):+ raise CodeInterpreterError("Write paths cannot overlap PythonInterpreter runtime files.")raw_read_paths = [self._get_runner_path(),*([deno_dir] if deno_dir else []),@@ -320,9 +328,10 @@ class PythonInterpreter:args.append(_canonicalize_path(self._get_runner_path()))- # For runner.js to load in env vars- if self._env_arg:- args.append(self._env_arg)+ # For runner.js to load in env vars and revoke cache access after startup+ args.append(self._env_arg)+ if deno_dir:+ args.append(f"--dspy-deno-dir={_canonicalize_path(deno_dir)}")self.deno_command = argsself.deno_process = Nonediff --git a/dspy/primitives/runner.js b/dspy/primitives/runner.jsindex 219a06000..3ab6e842a 100644--- a/dspy/primitives/runner.js+++ b/dspy/primitives/runner.js@@ -141,6 +141,8 @@ globalThis.addEventListener("unhandledrejection", (event) => {});const pyodide = await pyodideModule.loadPyodide();+const denoDir = Deno.args.find((arg) => arg.startsWith("--dspy-deno-dir="))?.slice(16);+if (denoDir) await Deno.permissions.revoke({ name: "read", path: denoDir });// Tool call support: allows Python code to call host-side functions// The stdin reader is shared so tool_call can read responses during executiondiff --git a/tests/primitives/test_python_interpreter.py b/tests/primitives/test_python_interpreter.pyindex 8b0b8d16e..87f9414eb 100644--- a/tests/primitives/test_python_interpreter.py+++ b/tests/primitives/test_python_interpreter.py@@ -269,6 +269,40 @@ except Exception as e:assert secret_content in output+def test_default_runner_cannot_read_shared_deno_cache(monkeypatch, tmp_path):+ shared_cache = tmp_path / "deno"+ shared_cache.mkdir()+ canary = shared_cache / "secret.txt"+ canary.write_text("shared cache secret")+ monkeypatch.setenv("DENO_DIR", str(shared_cache))++ with PythonInterpreter() as interpreter:+ result = interpreter.execute(+ f"""import js+try:+ js.Deno.readTextFileSync({str(canary)!r})+ result = "disclosed"+except Exception as error:+ result = str(error)+result"""+ )++ assert "disclosed" not in result+ assert "read access" in result.lower()+++def test_default_runner_starts_offline_from_warm_shared_cache(monkeypatch, tmp_path):+ shared_cache = tmp_path / "deno"+ runner = str(Path(python_interpreter.__file__).with_name("runner.js"))+ env = {**_deno_subprocess_env(), "DENO_DIR": str(shared_cache)}+ subprocess.run([_find_deno_executable(), "cache", "--no-config", "--no-lock", runner], env=env, check=True)+ monkeypatch.setenv("DENO_DIR", str(shared_cache))++ with PythonInterpreter() as interpreter:+ interpreter.deno_command.insert(interpreter.deno_command.index(os.path.realpath(runner)), "--cached-only")+ assert interpreter.execute("1 + 1") == 2++def test_tools_dict_is_copied():"""Test that tools dict is defensively copied, not stored by reference."""tools = {"my_tool": lambda: "result"}@@ -524,6 +558,23 @@ def test_default_command_uses_managed_deno_for_info_and_run(monkeypatch, tmp_patassert seen_operations == [("info", deno_executable), ("version", deno_executable)]+def test_default_command_revokes_shared_cache_after_startup(monkeypatch, tmp_path):+ shared_cache = tmp_path / "deno"+ monkeypatch.setattr(PythonInterpreter, "_get_deno_dir", staticmethod(lambda executable: str(shared_cache)))+ interpreter = PythonInterpreter()++ assert str(shared_cache) in next(arg for arg in interpreter.deno_command if arg.startswith("--allow-read="))+ assert f"--dspy-deno-dir={shared_cache}" in interpreter.deno_command+++def test_rejects_write_paths_overlapping_runtime_files(monkeypatch, tmp_path):+ cache = tmp_path / "deno"+ monkeypatch.setattr(PythonInterpreter, "_get_deno_dir", staticmethod(lambda executable: str(cache)))++ with pytest.raises(CodeInterpreterError, match="runtime files"):+ PythonInterpreter(enable_write_paths=[cache])++@pytest.mark.parametrize("version", [(2, 0, 0), (2, 4, 5), (2, 9, 5)])def test_accepts_supported_deno_2_versions(monkeypatch, version):monkeypatch.setattr(python_interpreter, "_get_deno_version", lambda executable: version)