Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Make sure that tools are added back after deno subprocess dies

isaacbmillere594f7507f57
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.py
index 76b685ad5..569510ca4 100644
--- a/dspy/primitives/python_interpreter.py
+++ b/dspy/primitives/python_interpreter.py
@@ -79,6 +79,10 @@ def _jsonrpc_error(code: int, message: str, id: int | str, data: dict | None = N
return json.dumps({"jsonrpc": "2.0", "error": err, "id": id})
+class _SubprocessDied(Exception):
+ """Internal: raised when the Deno subprocess dies during execution."""
+
+
class PythonInterpreter:
"""Local interpreter for secure Python execution using Deno and Pyodide.
@@ -196,6 +200,40 @@ class PythonInterpreter:
"Create a separate interpreter instance for each thread."
)
+ def _kill_process(self) -> None:
+ """Terminate the subprocess and clear the handle to allow restart."""
+ if self.deno_process is not None:
+ try:
+ self.deno_process.kill()
+ except OSError:
+ pass
+ try:
+ self.deno_process.wait(timeout=5)
+ except Exception:
+ pass
+ self.deno_process = None
+
+ def _write_msg(self, msg: str, context: str) -> None:
+ """Write a JSON-RPC message to the subprocess. Raises _SubprocessDied on failure."""
+ try:
+ self.deno_process.stdin.write(msg + "\n")
+ self.deno_process.stdin.flush()
+ except BrokenPipeError:
+ exit_code = self.deno_process.poll()
+ raise _SubprocessDied(f"Deno subprocess died during {context} (exit code: {exit_code})")
+
+ def _read_line(self, context: str) -> str:
+ """Read a line from the subprocess. Raises _SubprocessDied on EOF."""
+ line = self.deno_process.stdout.readline().strip()
+ if not line:
+ exit_code = self.deno_process.poll()
+ stderr = self.deno_process.stderr.read() if self.deno_process.stderr else ""
+ parts = [f"Deno subprocess produced no output during {context} (exit code: {exit_code})"]
+ if stderr.strip():
+ parts.append(f"Stderr: {stderr.strip()[:500]}")
+ raise _SubprocessDied(". ".join(parts))
+ return line
+
@staticmethod
@functools.lru_cache(maxsize=1)
def _get_deno_dir() -> str | None:
@@ -318,11 +356,12 @@ class PythonInterpreter:
error_code = JSONRPC_APP_ERRORS.get(error_type, JSONRPC_APP_ERRORS["Unknown"])
response = _jsonrpc_error(error_code, str(e), request_id, {"type": error_type})
- self.deno_process.stdin.write(response + "\n")
- self.deno_process.stdin.flush()
+ self._write_msg(response, "tool call response")
def _ensure_deno_process(self) -> None:
if self.deno_process is None or self.deno_process.poll() is not None:
+ self._tools_registered = False
+ self._mounted_files = False
try:
self.deno_process = subprocess.Popen(
self.deno_command,
@@ -349,19 +388,8 @@ class PythonInterpreter:
"""Send a JSON-RPC request and return the parsed response."""
self._request_id += 1
request_id = self._request_id
- msg = _jsonrpc_request(method, params, request_id)
- self.deno_process.stdin.write(msg + "\n")
- self.deno_process.stdin.flush()
-
- response_line = self.deno_process.stdout.readline().strip()
- if not response_line:
- exit_code = self.deno_process.poll()
- if exit_code is not None:
- stderr = self.deno_process.stderr.read() if self.deno_process.stderr else ""
- raise CodeInterpreterError(f"Deno exited (code {exit_code}) {context}: {stderr}")
- raise CodeInterpreterError(f"No response {context}")
-
- response = json.loads(response_line)
+ self._write_msg(_jsonrpc_request(method, params, request_id), context)
+ response = json.loads(self._read_line(context))
if response.get("id") != request_id:
raise CodeInterpreterError(f"Response ID mismatch {context}: expected {request_id}, got {response.get('id')}")
if "error" in response:
@@ -463,6 +491,19 @@ class PythonInterpreter:
self._check_thread_ownership()
variables = variables or {}
code = self._inject_variables(code, variables)
+
+ try:
+ return self._execute_inner(code)
+ except _SubprocessDied:
+ self._kill_process()
+
+ try:
+ return self._execute_inner(code)
+ except _SubprocessDied as e:
+ raise CodeInterpreterError(f"Deno subprocess failed after automatic restart: {e}")
+
+ def _execute_inner(self, code: str) -> Any:
+ """Run code in the subprocess. Raises _SubprocessDied on process-death conditions."""
self._ensure_deno_process()
self._mount_files()
self._register_tools()
@@ -473,30 +514,12 @@ class PythonInterpreter:
# Send the code as JSON-RPC request
self._request_id += 1
execute_request_id = self._request_id
- input_data = _jsonrpc_request("execute", {"code": code}, execute_request_id)
- try:
- self.deno_process.stdin.write(input_data + "\n")
- 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()
- for name, value in self._pending_large_vars.items():
- self._inject_large_var(name, value)
- self.deno_process.stdin.write(input_data + "\n")
- self.deno_process.stdin.flush()
+ self._write_msg(_jsonrpc_request("execute", {"code": code}, execute_request_id), "execute")
# Read and handle messages until we get the final output.
# Loop is needed because tool calls require back-and-forth communication.
while True:
- output_line = self.deno_process.stdout.readline().strip()
- if not output_line:
- # Possibly the subprocess died or gave no output
- err_output = self.deno_process.stderr.read()
- raise CodeInterpreterError(f"No output from Deno subprocess. Stderr: {err_output}")
+ output_line = self._read_line("execute")
# Skip non-JSON lines (e.g., Pyodide package loading messages)
if not output_line.startswith("{"):
diff --git a/tests/predict/test_rlm.py b/tests/predict/test_rlm.py
index 6fe70615e..2a457fa0b 100644
--- a/tests/predict/test_rlm.py
+++ b/tests/predict/test_rlm.py
@@ -1156,6 +1156,43 @@ class TestRLMWithDummyLM:
assert result.total == 15
+# ============================================================================
+# Integration Tests: RLM Subprocess Restart Resilience
+# ============================================================================
+
+
+class TestRLMSubprocessRestart:
+ """Tests that RLM recovers when the interpreter subprocess dies."""
+
+ def test_tool_works_after_interpreter_restart(self):
+ """RLM with a host tool succeeds after forced interpreter restart between calls."""
+ def lookup(key: str) -> str:
+ return {"color": "red"}.get(key, "unknown")
+
+ interp = PythonInterpreter()
+ rlm = RLM("query -> answer: str", max_iterations=3, tools=[lookup], interpreter=interp)
+
+ with dummy_lm_context([
+ {"reasoning": "Look up color", "code": 'result = lookup(key="color")\nSUBMIT(result)'},
+ ]):
+ result = rlm.forward(query="what color?")
+ assert result.answer == "red"
+
+ # Kill the interpreter subprocess to simulate crash
+ interp.deno_process.kill()
+ interp.deno_process.wait()
+
+ # Second forward should auto-recover: no "Unknown tool" error
+ with dummy_lm_context([
+ {"reasoning": "Look up color again", "code": 'result = lookup(key="color")\nSUBMIT(result)'},
+ ]):
+ result = rlm.forward(query="what color?")
+ assert result.answer == "red"
+
+ interp.shutdown()
+
+
# ============================================================================
# Integration Tests: RLM with real LM (require API key and Deno)
# ============================================================================
diff --git a/tests/primitives/test_python_interpreter.py b/tests/primitives/test_python_interpreter.py
index 4a34b7f68..9ce0bb68b 100644
--- a/tests/primitives/test_python_interpreter.py
+++ b/tests/primitives/test_python_interpreter.py
@@ -553,3 +553,112 @@ def test_enable_read_paths_multiple_files(tmp_path):
assert contents["test1.txt"] == "Content 1"
assert contents["test2.txt"] == "Content 2"
assert contents["test3.txt"] == "Content 3"
+
+
+# =============================================================================
+# Subprocess Restart Resilience Tests
+# =============================================================================
+
+
+def test_tool_works_after_subprocess_kill():
+ """Tool calls succeed after the Deno subprocess is forcefully killed."""
+
+ def greet(name: str = "world") -> str:
+ return f"Hello, {name}!"
+
+ with PythonInterpreter(tools={"greet": greet}) as interp:
+ result = interp.execute('print(greet(name="Alice"))')
+ assert "Hello, Alice!" in result
+
+ # Kill the subprocess to simulate crash
+ interp.deno_process.kill()
+ interp.deno_process.wait()
+
+ # Tools should work after automatic restart and re-registration
+ result = interp.execute('print(greet(name="Bob"))')
+ assert "Hello, Bob!" in result
+
+
+def test_restart_clears_registration_state():
+ """_ensure_deno_process resets _tools_registered when creating a new process."""
+
+ def my_tool(x: str = "") -> str:
+ return f"result: {x}"
+
+ interp = PythonInterpreter(tools={"my_tool": my_tool})
+ interp._ensure_deno_process()
+ interp._register_tools()
+ assert interp._tools_registered is True
+
+ # Kill the process so next _ensure_deno_process creates a new one
+ interp.deno_process.kill()
+ interp.deno_process.wait()
+
+ interp._ensure_deno_process()
+ assert interp._tools_registered is False, "_tools_registered should be cleared after subprocess restart"
+ interp.shutdown()
+
+
+def test_execute_retries_once_on_subprocess_death():
+ """execute() retries exactly once when the subprocess dies."""
+ from unittest.mock import patch
+
+ from dspy.primitives.python_interpreter import _SubprocessDied
+
+ interp = PythonInterpreter()
+ attempts = []
+
+ def mock_inner(code):
+ attempts.append(1)
+ if len(attempts) == 1:
+ raise _SubprocessDied("simulated death")
+ return "recovered"
+
+ with patch.object(interp, '_execute_inner', side_effect=mock_inner):
+ with patch.object(interp, '_kill_process'):
+ result = interp.execute("print(1)")
+
+ assert result == "recovered"
+ assert len(attempts) == 2
+
+
+def test_execute_raises_after_second_failure():
+ """execute() raises CodeInterpreterError when both attempts fail."""
+ from unittest.mock import patch
+
+ from dspy.primitives.python_interpreter import _SubprocessDied
+
+ interp = PythonInterpreter()
+
+ def always_fail(code):
+ raise _SubprocessDied("process died again")
+
+ with patch.object(interp, '_execute_inner', side_effect=always_fail):
+ with patch.object(interp, '_kill_process'):
+ with pytest.raises(CodeInterpreterError, match="failed after automatic restart"):
+ interp.execute("print(1)")
+
+
+def test_error_message_includes_exit_code():
+ """Subprocess death errors include exit code, never blank."""
+ from unittest.mock import patch
+
+ from dspy.primitives.python_interpreter import _SubprocessDied
+
+ interp = PythonInterpreter()
+ interp._ensure_deno_process()
+ interp._tools_registered = True
+
+ # Kill the process
+ interp.deno_process.kill()
+ interp.deno_process.wait()
+
+ # Prevent auto-restart to observe the raw _SubprocessDied error
+ with patch.object(interp, '_ensure_deno_process'):
+ with pytest.raises(_SubprocessDied) as exc_info:
+ interp._execute_inner("print(1)")
+
+ error_msg = str(exc_info.value)
+ assert "exit code" in error_msg
+ assert len(error_msg) > 10
+ interp._kill_process()