Fix and simplify local runtime init (#7997)

This commit is contained in:
Engel Nyst 2025-04-22 00:24:22 +02:00 committed by GitHub
parent 0de50153a0
commit b3bd3924a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 35 deletions

View File

@ -5,6 +5,7 @@ This runtime runs the action_execution_server directly on the local machine with
import os
import shutil
import subprocess
import sys
import tempfile
import threading
from typing import Callable
@ -204,38 +205,21 @@ class LocalRuntime(ActionExecutionClient):
env = os.environ.copy()
# Get the code repo path
code_repo_path = os.path.dirname(os.path.dirname(openhands.__file__))
env['PYTHONPATH'] = f'{code_repo_path}:$PYTHONPATH'
env['PYTHONPATH'] = f'{code_repo_path}{os.pathsep}{env.get("PYTHONPATH", "")}'
env['OPENHANDS_REPO_PATH'] = code_repo_path
env['LOCAL_RUNTIME_MODE'] = '1'
# Get the poetry venv path using 'poetry env info --path'
try:
poetry_venvs_path = subprocess.check_output( # noqa: ASYNC101
['poetry', 'env', 'info', '--path'],
env=env,
cwd=code_repo_path,
text=True,
stderr=subprocess.PIPE,
shell=False,
).strip()
# Verify it's a valid path (basic check)
if not os.path.isdir(poetry_venvs_path):
raise ValueError(f"'{poetry_venvs_path}' is not a valid directory.")
except (subprocess.CalledProcessError, FileNotFoundError, ValueError) as e:
# Attempt to fall back to environment variable if set
poetry_venvs_path = env.get('POETRY_VIRTUALENVS_PATH', '')
if not poetry_venvs_path or not os.path.isdir(poetry_venvs_path):
raise RuntimeError(
'Cannot find poetry venv path using `poetry env info --path` or POETRY_VIRTUALENVS_PATH env var. '
'Please check your poetry installation and ensure a virtual environment exists.'
) from e
logger.warning(
f'Using fallback POETRY_VIRTUALENVS_PATH: {poetry_venvs_path}'
)
env['POETRY_VIRTUALENVS_PATH'] = poetry_venvs_path
logger.debug(f'POETRY_VIRTUALENVS_PATH: {poetry_venvs_path}')
# Derive environment paths using sys.executable
interpreter_path = sys.executable
python_bin_path = os.path.dirname(interpreter_path)
env_root_path = os.path.dirname(python_bin_path)
check_dependencies(code_repo_path, poetry_venvs_path)
# Prepend the interpreter's bin directory to PATH for subprocesses
env['PATH'] = f'{python_bin_path}{os.pathsep}{env.get("PATH", "")}'
logger.debug(f'Updated PATH for subprocesses: {env["PATH"]}')
# Check dependencies using the derived env_root_path
check_dependencies(code_repo_path, env_root_path)
self.server_process = subprocess.Popen( # noqa: ASYNC101
cmd,
stdout=subprocess.PIPE,

View File

@ -48,13 +48,7 @@ class JupyterPlugin(Plugin):
'OPENHANDS_REPO_PATH environment variable is not set. '
'This is required for the jupyter plugin to work with LocalRuntime.'
)
# assert POETRY_VIRTUALENVS_PATH is set
poetry_venvs_path = os.environ.get('POETRY_VIRTUALENVS_PATH')
if not poetry_venvs_path:
raise ValueError(
'POETRY_VIRTUALENVS_PATH environment variable is not set. '
'This is required for the jupyter plugin to work with LocalRuntime.'
)
# The correct environment is ensured by the PATH in LocalRuntime.
poetry_prefix = f'cd {code_repo_path}\n'
jupyter_launch_command = (
f"{prefix}/bin/bash << 'EOF'\n"