Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fix(interpreter): reset tools/mounts when Deno subprocess restarts (#9351)

Isaac Miller295e2b355ad2
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.py
index bee88643a..b97d50c2d 100644
--- a/dspy/primitives/python_interpreter.py
+++ b/dspy/primitives/python_interpreter.py
@@ -315,6 +315,9 @@ class PythonInterpreter:
def _ensure_deno_process(self) -> None:
if self.deno_process is None or self.deno_process.poll() is not None:
+ # Process identity changed (or process missing), so replay setup.
+ self._tools_registered = False
+ self._mounted_files = False
try:
self.deno_process = subprocess.Popen(
self.deno_command,
@@ -502,8 +505,6 @@ class PythonInterpreter:
self.deno_process.stdin.flush()
except BrokenPipeError:
# If the process died, restart and try again once
- self._tools_registered = False
- self._mounted_files = False
self._ensure_deno_process()
self._mount_files()
self._register_tools()
diff --git a/tests/primitives/test_python_interpreter.py b/tests/primitives/test_python_interpreter.py
index 41c45556f..e112db7c3 100644
--- a/tests/primitives/test_python_interpreter.py
+++ b/tests/primitives/test_python_interpreter.py
@@ -297,6 +297,51 @@ def test_tool_default_args():
assert result == "Hi, World!"
+def test_tools_re_register_after_process_restart():
+ """Tools should remain callable after Deno subprocess restart."""
+ def echo(message: str = "") -> str:
+ return f"Echo: {message}"
+
+ with PythonInterpreter(tools={"echo": echo}) as interpreter:
+ first = interpreter.execute('print(echo(message="one"))')
+ assert "Echo: one" in first
+
+ first_pid = interpreter.deno_process.pid
+ interpreter.deno_process.kill()
+ interpreter.deno_process.wait()
+
+ second = interpreter.execute('print(echo(message="two"))')
+ assert "Echo: two" in second
+ assert interpreter.deno_process.pid != first_pid
+
+
+def test_mounts_replay_after_process_restart(tmp_path):
+ """Mounted files should still be accessible after subprocess restart."""
+ host_file = tmp_path / "mount_restart.txt"
+ host_file.write_text("restarted-ok")
+ virtual_path = f"/sandbox/{host_file.name}"
+
+ with PythonInterpreter(enable_read_paths=[str(host_file)]) as interpreter:
+ first = interpreter.execute(
+ f"with open({virtual_path!r}, 'r') as f:\n"
+ f" data = f.read()\n"
+ f"data"
+ )
+ assert first == "restarted-ok"
+
+ first_pid = interpreter.deno_process.pid
+ interpreter.deno_process.kill()
+ interpreter.deno_process.wait()
+
+ second = interpreter.execute(
+ f"with open({virtual_path!r}, 'r') as f:\n"
+ f" data = f.read()\n"
+ f"data"
+ )
+ assert second == "restarted-ok"
+ assert interpreter.deno_process.pid != first_pid
+
+
def test_tool_all_positional_args():
"""Test that tools work when all arguments are passed positionally."""