Эх сурвалжийг харах

fix runtime_startup_env_vars not being used (#4250)

Xingyao Wang 1 жил өмнө
parent
commit
9c07370559

+ 3 - 0
openhands/runtime/remote/runtime.py

@@ -202,6 +202,9 @@ class RemoteRuntime(Runtime):
         ), 'Runtime URL is not set. This should never happen.'
         self.send_status_message(' ')
 
+        self._wait_until_alive()
+        self.setup_initial_env()
+
     @retry(
         stop=stop_after_attempt(10) | stop_if_should_exit(),
         wait=wait_exponential(multiplier=1, min=4, max=60),

+ 2 - 0
openhands/runtime/runtime.py

@@ -77,6 +77,8 @@ class Runtime:
     def setup_initial_env(self) -> None:
         logger.debug(f'Adding env vars: {self.initial_env_vars}')
         self.add_env_vars(self.initial_env_vars)
+        if self.config.sandbox.runtime_startup_env_vars:
+            self.add_env_vars(self.config.sandbox.runtime_startup_env_vars)
 
     def close(self) -> None:
         pass

+ 3 - 0
tests/runtime/conftest.py

@@ -209,6 +209,7 @@ def _load_runtime(
     browsergym_eval_env: str | None = None,
     use_workspace: bool | None = None,
     force_rebuild_runtime: bool = False,
+    runtime_startup_env_vars: dict[str, str] | None = None,
 ) -> Runtime:
     sid = 'rt_' + str(random.randint(100000, 999999))
 
@@ -241,6 +242,8 @@ def _load_runtime(
 
     config.sandbox.browsergym_eval_env = browsergym_eval_env
     config.sandbox.enable_auto_lint = enable_auto_lint
+    if runtime_startup_env_vars is not None:
+        config.sandbox.runtime_startup_env_vars = runtime_startup_env_vars
 
     if base_container_image is not None:
         config.sandbox.base_container_image = base_container_image

+ 16 - 0
tests/runtime/test_env_vars.py

@@ -65,3 +65,19 @@ def test_env_vars_runtime_operations(temp_dir, box_class):
     )
 
     _close_test_runtime(runtime)
+
+
+def test_env_vars_added_by_config(temp_dir, box_class):
+    runtime = _load_runtime(
+        temp_dir,
+        box_class,
+        runtime_startup_env_vars={'ADDED_ENV_VAR': 'added_value'},
+    )
+
+    # Test adding single env var
+    obs = runtime.run_action(CmdRunAction(command='echo $ADDED_ENV_VAR'))
+    assert (
+        obs.exit_code == 0
+        and obs.content.strip().split('\r\n')[0].strip() == 'added_value'
+    )
+    _close_test_runtime(runtime)