Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix: Run setup.sh script in GitHub resolver (#8548)

Graham Neubig5077fea5c736
diff --git a/openhands/resolver/resolve_issue.py b/openhands/resolver/resolve_issue.py
index 7161dc43fb..5446539efe 100644
--- a/openhands/resolver/resolve_issue.py
+++ b/openhands/resolver/resolve_issue.py
@@ -230,7 +230,7 @@ class IssueResolver:
"""Initialize the runtime for the agent.
This function is called before the runtime is used to run the agent.
- Currently it does nothing.
+ It sets up git configuration and runs the setup script if it exists.
"""
logger.info('-' * 30)
logger.info('BEGIN Runtime Completion Fn')
@@ -257,6 +257,14 @@ class IssueResolver:
if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
raise RuntimeError(f'Failed to set git config.\n{obs}')
+ # Run setup script if it exists
+ logger.info('Checking for .openhands/setup.sh script...')
+ runtime.maybe_run_setup_script()
+
+ # Setup git hooks if they exist
+ logger.info('Checking for .openhands/pre-commit.sh script...')
+ runtime.maybe_setup_git_hooks()
+
async def complete_runtime(
self,
runtime: Runtime,
diff --git a/tests/unit/resolver/test_resolve_issue.py b/tests/unit/resolver/test_resolve_issue.py
index 4b17f2564e..f8e03e90ef 100644
--- a/tests/unit/resolver/test_resolve_issue.py
+++ b/tests/unit/resolver/test_resolve_issue.py
@@ -1,17 +1,17 @@
-import os
from unittest import mock
import pytest
from openhands.core.config import SandboxConfig
+from openhands.events.action import CmdRunAction
from openhands.resolver.resolve_issue import IssueResolver
-import openhands
+
def assert_sandbox_config(
config: SandboxConfig,
- base_container_image = SandboxConfig.model_fields["base_container_image"].default,
- runtime_container_image = f'ghcr.io/all-hands-ai/runtime:mock-nikolaik', # Default to mock version
- local_runtime_url = SandboxConfig.model_fields["local_runtime_url"].default,
+ base_container_image=SandboxConfig.model_fields['base_container_image'].default,
+ runtime_container_image='ghcr.io/all-hands-ai/runtime:mock-nikolaik', # Default to mock version
+ local_runtime_url=SandboxConfig.model_fields['local_runtime_url'].default,
):
"""Helper function to assert the properties of the SandboxConfig object."""
assert isinstance(config, SandboxConfig)
@@ -22,6 +22,7 @@ def assert_sandbox_config(
assert config.timeout == 300
assert config.local_runtime_url == local_runtime_url
+
def test_setup_sandbox_config_default():
"""Test default configuration when no images provided and not experimental"""
with mock.patch('openhands.__version__', 'mock'):
@@ -32,23 +33,25 @@ def test_setup_sandbox_config_default():
)
assert_sandbox_config(
- config,
- runtime_container_image='ghcr.io/all-hands-ai/runtime:mock-nikolaik'
+ config, runtime_container_image='ghcr.io/all-hands-ai/runtime:mock-nikolaik'
)
def test_setup_sandbox_config_both_images():
"""Test that providing both container images raises ValueError"""
- with pytest.raises(ValueError, match="Cannot provide both runtime and base container images."):
+ with pytest.raises(
+ ValueError, match='Cannot provide both runtime and base container images.'
+ ):
IssueResolver._setup_sandbox_config(
- base_container_image="base-image",
- runtime_container_image="runtime-image",
+ base_container_image='base-image',
+ runtime_container_image='runtime-image',
is_experimental=False,
)
+
def test_setup_sandbox_config_base_only():
"""Test configuration when only base_container_image is provided"""
- base_image = "custom-base-image"
+ base_image = 'custom-base-image'
config = IssueResolver._setup_sandbox_config(
base_container_image=base_image,
runtime_container_image=None,
@@ -56,25 +59,22 @@ def test_setup_sandbox_config_base_only():
)
assert_sandbox_config(
- config,
- base_container_image=base_image,
- runtime_container_image=None
+ config, base_container_image=base_image, runtime_container_image=None
)
+
def test_setup_sandbox_config_runtime_only():
"""Test configuration when only runtime_container_image is provided"""
- runtime_image = "custom-runtime-image"
+ runtime_image = 'custom-runtime-image'
config = IssueResolver._setup_sandbox_config(
base_container_image=None,
runtime_container_image=runtime_image,
is_experimental=False,
)
- assert_sandbox_config(
- config,
- runtime_container_image=runtime_image
- )
-
+ assert_sandbox_config(config, runtime_container_image=runtime_image)
+
+
def test_setup_sandbox_config_experimental():
"""Test configuration when experimental mode is enabled"""
with mock.patch('openhands.__version__', 'mock'):
@@ -84,40 +84,67 @@ def test_setup_sandbox_config_experimental():
is_experimental=True,
)
- assert_sandbox_config(
- config,
- runtime_container_image=None
- )
+ assert_sandbox_config(config, runtime_container_image=None)
+
[email protected]("openhands.resolver.resolve_issue.os.getuid", return_value=0)
[email protected]("openhands.resolver.resolve_issue.get_unique_uid", return_value=1001)
[email protected]('openhands.resolver.resolve_issue.os.getuid', return_value=0)
[email protected]('openhands.resolver.resolve_issue.get_unique_uid', return_value=1001)
def test_setup_sandbox_config_gitlab_ci(mock_get_unique_uid, mock_getuid):
"""Test GitLab CI specific configuration when running as root"""
with mock.patch('openhands.__version__', 'mock'):
- with mock.patch.object(IssueResolver, "GITLAB_CI", True):
+ with mock.patch.object(IssueResolver, 'GITLAB_CI', True):
config = IssueResolver._setup_sandbox_config(
base_container_image=None,
runtime_container_image=None,
is_experimental=False,
)
-
- assert_sandbox_config(
- config,
- local_runtime_url="http://localhost"
- )
[email protected]("openhands.resolver.resolve_issue.os.getuid", return_value=1000)
+ assert_sandbox_config(config, local_runtime_url='http://localhost')
+
+
[email protected]('openhands.resolver.resolve_issue.os.getuid', return_value=1000)
def test_setup_sandbox_config_gitlab_ci_non_root(mock_getuid):
"""Test GitLab CI configuration when not running as root"""
with mock.patch('openhands.__version__', 'mock'):
- with mock.patch.object(IssueResolver, "GITLAB_CI", True):
+ with mock.patch.object(IssueResolver, 'GITLAB_CI', True):
config = IssueResolver._setup_sandbox_config(
base_container_image=None,
runtime_container_image=None,
is_experimental=False,
)
- assert_sandbox_config(
- config,
- local_runtime_url="http://localhost"
- )
\ No newline at end of file
+ assert_sandbox_config(config, local_runtime_url='http://localhost')
+
+
[email protected]('openhands.events.observation.CmdOutputObservation')
[email protected]('openhands.runtime.base.Runtime')
+def test_initialize_runtime_runs_setup_script_and_git_hooks(
+ mock_runtime, mock_cmd_output
+):
+ """Test that initialize_runtime calls maybe_run_setup_script and maybe_setup_git_hooks"""
+
+ # Create a minimal resolver instance with just the methods we need
+ class MinimalResolver:
+ def initialize_runtime(self, runtime):
+ # This is the method we're testing
+ action = CmdRunAction(command='git config --global core.pager ""')
+ runtime.run_action(action)
+
+ # Run setup script if it exists
+ runtime.maybe_run_setup_script()
+
+ # Setup git hooks if they exist
+ runtime.maybe_setup_git_hooks()
+
+ resolver = MinimalResolver()
+
+ # Mock the runtime's run_action method to return a successful CmdOutputObservation
+ mock_cmd_output.return_value.exit_code = 0
+ mock_runtime.run_action.return_value = mock_cmd_output.return_value
+
+ # Call the method
+ resolver.initialize_runtime(mock_runtime)
+
+ # Verify that both methods were called
+ mock_runtime.maybe_run_setup_script.assert_called_once()
+ mock_runtime.maybe_setup_git_hooks.assert_called_once()