π OpenHands: AI-Driven Development
Allow custom user (#11013)
chuckbutkus37daf068c5d4
diff --git a/enterprise/server/clustered_conversation_manager.py b/enterprise/server/clustered_conversation_manager.pyindex 0ecc062c38..1eae6d19da 100644--- a/enterprise/server/clustered_conversation_manager.py+++ b/enterprise/server/clustered_conversation_manager.py@@ -21,6 +21,7 @@ from openhands.events.event_store_abc import EventStoreABCfrom openhands.events.observation import AgentStateChangedObservationfrom openhands.events.stream import EventStreamSubscriberfrom openhands.llm.llm_registry import LLMRegistry+from openhands.runtime.runtime_status import RuntimeStatusfrom openhands.server.config.server_config import ServerConfigfrom openhands.server.conversation_manager.conversation_manager import (ConversationManager,@@ -686,6 +687,7 @@ class ClusteredConversationManager(StandaloneConversationManager):url=self._get_conversation_url(conversation_id),session_api_key=None,event_store=EventStore(conversation_id, self.file_store, uid),+ runtime_status=RuntimeStatus.READY,))return resultsdiff --git a/enterprise/server/saas_nested_conversation_manager.py b/enterprise/server/saas_nested_conversation_manager.pyindex 1f1af3045b..b2cfad0a46 100644--- a/enterprise/server/saas_nested_conversation_manager.py+++ b/enterprise/server/saas_nested_conversation_manager.py@@ -60,9 +60,14 @@ from openhands.utils.utils import create_registry_and_conversation_statsRUNTIME_URL_PATTERN = os.getenv('RUNTIME_URL_PATTERN', 'https://{runtime_id}.prod-runtime.all-hands.dev')+RUNTIME_ROUTING_MODE = os.getenv('RUNTIME_ROUTING_MODE', 'subdomain').lower()# Pattern for base URL for the runtime-RUNTIME_CONVERSATION_URL = RUNTIME_URL_PATTERN + '/api/conversations/{conversation_id}'+RUNTIME_CONVERSATION_URL = RUNTIME_URL_PATTERN + (+ '/runtime/api/conversations/{conversation_id}'+ if RUNTIME_ROUTING_MODE == 'path'+ else '/api/conversations/{conversation_id}'+)# Time in seconds before a Redis entry is considered expired if not refreshed_REDIS_ENTRY_TIMEOUT_SECONDS = 300diff --git a/openhands/runtime/impl/action_execution/action_execution_client.py b/openhands/runtime/impl/action_execution/action_execution_client.pyindex 93fee37e8b..554a7bfd5b 100644--- a/openhands/runtime/impl/action_execution/action_execution_client.py+++ b/openhands/runtime/impl/action_execution/action_execution_client.py@@ -129,11 +129,15 @@ class ActionExecutionClient(Runtime):return send_request(self.session, method, url, **kwargs)def check_if_alive(self) -> None:+ request_url = f'{self.action_execution_server_url}/alive'+ self.log('debug', f'Sending request to: {request_url}')response = self._send_action_server_request('GET',- f'{self.action_execution_server_url}/alive',+ request_url,timeout=5,)+ self.log('debug', f'Response status code: {response.status_code}')+ self.log('debug', f'Response text: {response.text}')assert response.is_closeddef list_files(self, path: str | None = None) -> list[str]:diff --git a/openhands/runtime/impl/remote/remote_runtime.py b/openhands/runtime/impl/remote/remote_runtime.pyindex b98d99d11f..7cba38a8e2 100644--- a/openhands/runtime/impl/remote/remote_runtime.py+++ b/openhands/runtime/impl/remote/remote_runtime.py@@ -415,11 +415,19 @@ class RemoteRuntime(ActionExecutionClient):def _wait_until_alive_impl(self) -> None:self.log('debug', f'Waiting for runtime to be alive at url: {self.runtime_url}')+ self.log(+ 'debug',+ f'Sending request to: {self.config.sandbox.remote_runtime_api_url}/runtime/{self.runtime_id}',+ )runtime_info_response = self._send_runtime_api_request('GET',f'{self.config.sandbox.remote_runtime_api_url}/runtime/{self.runtime_id}',)runtime_data = runtime_info_response.json()+ self.log(+ 'debug',+ f'received response: {runtime_data}',+ )assert 'runtime_id' in runtime_dataassert runtime_data['runtime_id'] == self.runtime_idassert 'pod_status' in runtime_datadiff --git a/openhands/runtime/plugins/jupyter/__init__.py b/openhands/runtime/plugins/jupyter/__init__.pyindex a5dd28eb87..fde54069f4 100644--- a/openhands/runtime/plugins/jupyter/__init__.py+++ b/openhands/runtime/plugins/jupyter/__init__.py@@ -13,6 +13,15 @@ from openhands.runtime.plugins.requirement import Plugin, PluginRequirementfrom openhands.runtime.utils import find_available_tcp_portfrom openhands.utils.shutdown_listener import should_continue+SU_TO_USER = os.getenv('SU_TO_USER', 'true').lower() in (+ '1',+ 'true',+ 't',+ 'yes',+ 'y',+ 'on',+)+@dataclassclass JupyterRequirement(PluginRequirement):@@ -36,7 +45,7 @@ class JupyterPlugin(Plugin):if not is_local_runtime:# Non-LocalRuntime- prefix = f'su - {username} -s '+ prefix = f'su - {username} -s ' if SU_TO_USER else ''# cd to code repo, setup all env vars and run micromambapoetry_prefix = ('cd /openhands/code\n'diff --git a/openhands/runtime/plugins/vscode/__init__.py b/openhands/runtime/plugins/vscode/__init__.pyindex 2a94876eca..865a69c987 100644--- a/openhands/runtime/plugins/vscode/__init__.py+++ b/openhands/runtime/plugins/vscode/__init__.py@@ -15,6 +15,16 @@ from openhands.runtime.plugins.requirement import Plugin, PluginRequirementfrom openhands.runtime.utils.system import check_port_availablefrom openhands.utils.shutdown_listener import should_continue+RUNTIME_USERNAME = os.getenv('RUNTIME_USERNAME')+SU_TO_USER = os.getenv('SU_TO_USER', 'true').lower() in (+ '1',+ 'true',+ 't',+ 'yes',+ 'y',+ 'on',+)+@dataclassclass VSCodeRequirement(PluginRequirement):@@ -37,7 +47,7 @@ class VSCodePlugin(Plugin):)return- if username not in ['root', 'openhands']:+ if username not in filter(None, [RUNTIME_USERNAME, 'root', 'openhands']):self.vscode_port = Noneself.vscode_connection_token = Nonelogger.warning(@@ -83,13 +93,19 @@ class VSCodePlugin(Plugin):if path_mode:base_path_flag = f' --server-base-path /{runtime_id}/vscode'- cmd = (- f"su - {username} -s /bin/bash << 'EOF'\n"- f'sudo chown -R {username}:{username} /openhands/.openvscode-server\n'- f'cd {workspace_path}\n'- f'exec /openhands/.openvscode-server/bin/openvscode-server --host 0.0.0.0 --connection-token {self.vscode_connection_token} --port {self.vscode_port} --disable-workspace-trust{base_path_flag}\n'- 'EOF'- )+ cmd = (+ (+ f"su - {username} -s /bin/bash << 'EOF'\n"+ if SU_TO_USER+ else "/bin/bash << 'EOF'\n"+ )+ + f'sudo chown -R {username}:{username} /openhands/.openvscode-server\n'+ + f'cd {workspace_path}\n'+ + 'exec /openhands/.openvscode-server/bin/openvscode-server '+ + f'--host 0.0.0.0 --connection-token {self.vscode_connection_token} '+ + f'--port {self.vscode_port} --disable-workspace-trust{base_path_flag}\n'+ + 'EOF'+ )# Using asyncio.create_subprocess_shell instead of subprocess.Popen# to avoid ASYNC101 linting errordiff --git a/openhands/runtime/utils/bash.py b/openhands/runtime/utils/bash.pyindex 7371a829d6..8d929d948f 100644--- a/openhands/runtime/utils/bash.py+++ b/openhands/runtime/utils/bash.py@@ -20,6 +20,16 @@ from openhands.events.observation.commands import (from openhands.runtime.utils.bash_constants import TIMEOUT_MESSAGE_TEMPLATEfrom openhands.utils.shutdown_listener import should_continue+RUNTIME_USERNAME = os.getenv('RUNTIME_USERNAME')+SU_TO_USER = os.getenv('SU_TO_USER', 'true').lower() in (+ '1',+ 'true',+ 't',+ 'yes',+ 'y',+ 'on',+)+def split_bash_commands(commands: str) -> list[str]:if not commands.strip():@@ -193,7 +203,9 @@ class BashSession:def initialize(self) -> None:self.server = libtmux.Server()_shell_command = '/bin/bash'- if self.username in ['root', 'openhands']:+ if SU_TO_USER and self.username in list(+ filter(None, [RUNTIME_USERNAME, 'root', 'openhands'])+ ):# This starts a non-login (new) shell for the given user_shell_command = f'su {self.username} -'diff --git a/openhands/runtime/utils/command.py b/openhands/runtime/utils/command.pyindex 01701b5f1d..affecc2575 100644--- a/openhands/runtime/utils/command.py+++ b/openhands/runtime/utils/command.py@@ -1,3 +1,5 @@+import os+from openhands.core.config import OpenHandsConfigfrom openhands.core.logger import openhands_logger as loggerfrom openhands.runtime.plugins import PluginRequirement@@ -12,6 +14,9 @@ DEFAULT_PYTHON_PREFIX = []DEFAULT_MAIN_MODULE = 'openhands.runtime.action_execution_server'+RUNTIME_USERNAME = os.getenv('RUNTIME_USERNAME')+RUNTIME_UID = os.getenv('RUNTIME_UID')+def get_action_execution_server_startup_command(server_port: int,@@ -26,7 +31,10 @@ def get_action_execution_server_startup_command(sandbox_config = app_config.sandboxlogger.debug(f'app_config {vars(app_config)}')logger.debug(f'sandbox_config {vars(sandbox_config)}')- logger.debug(f'override_user_id {override_user_id}')+ logger.debug(f'RUNTIME_USERNAME {RUNTIME_USERNAME}, RUNTIME_UID {RUNTIME_UID}')+ logger.debug(+ f'override_username {override_username}, override_user_id {override_user_id}'+ )# Plugin argsplugin_args = []@@ -40,10 +48,15 @@ def get_action_execution_server_startup_command('--browsergym-eval-env'] + sandbox_config.browsergym_eval_env.split(' ')- username = override_username or (- 'openhands' if app_config.run_as_openhands else 'root'+ username = (+ override_username+ or RUNTIME_USERNAME+ or ('openhands' if app_config.run_as_openhands else 'root')+ )+ user_id = (+ override_user_id or RUNTIME_UID or (1000 if app_config.run_as_openhands else 0))- user_id = override_user_id or (1000 if app_config.run_as_openhands else 0)+ logger.debug(f'username {username}, user_id {user_id}')base_cmd = [*python_prefix,diff --git a/openhands/utils/http_session.py b/openhands/utils/http_session.pyindex 2244e45fb0..3397b8ce46 100644--- a/openhands/utils/http_session.py+++ b/openhands/utils/http_session.py@@ -27,6 +27,7 @@ class HttpSession:headers = kwargs.get('headers') or {}headers = {**self.headers, **headers}kwargs['headers'] = headers+ logger.debug(f'HttpSession:request called with args {args} and kwargs {kwargs}')return CLIENT.request(*args, **kwargs)def stream(self, *args, **kwargs):