Skip to content

πŸ™Œ OpenHands: AI-Driven Development

feat: Auto-forward LMNR_* environment variables to agent-server (#14123)

Juan Michelini2a6f5c8976e9
diff --git a/openhands/app_server/sandbox/sandbox_spec_service.py b/openhands/app_server/sandbox/sandbox_spec_service.py
index 223f4b01fc..5976907595 100644
--- a/openhands/app_server/sandbox/sandbox_spec_service.py
+++ b/openhands/app_server/sandbox/sandbox_spec_service.py
@@ -71,7 +71,7 @@ def get_agent_server_image() -> str:
# Prefixes for environment variables that should be auto-forwarded to agent-server
# These are typically configuration variables that affect the agent's behavior
-AUTO_FORWARD_PREFIXES = ('LLM_',)
+AUTO_FORWARD_PREFIXES = ('LLM_', 'LMNR_')
def get_agent_server_env() -> dict[str, str]:
@@ -80,9 +80,10 @@ def get_agent_server_env() -> dict[str, str]:
This function combines two sources of environment variables:
1. **Auto-forwarded variables**: Environment variables with certain prefixes
- (e.g., LLM_*) are automatically forwarded to the agent-server container.
+ (e.g., LLM_*, LMNR_*) are automatically forwarded to the agent-server container.
This ensures that LLM configuration like timeouts and retry settings
- work correctly in the two-container V1 architecture.
+ work correctly in the two-container V1 architecture, as well as
+ Laminar monitoring/analytics configuration.
2. **Explicit overrides via OH_AGENT_SERVER_ENV**: A JSON string that allows
setting arbitrary environment variables in the agent-server container.
@@ -90,6 +91,7 @@ def get_agent_server_env() -> dict[str, str]:
Auto-forwarded prefixes:
- LLM_* : LLM configuration (timeout, retries, model settings, etc.)
+ - LMNR_* : Laminar monitoring/analytics configuration
Usage:
# Auto-forwarding (no action needed):
@@ -97,6 +99,11 @@ def get_agent_server_env() -> dict[str, str]:
export LLM_NUM_RETRIES=10
# These will automatically be available in the agent-server
+ # Auto-forwarding for Laminar:
+ export LMNR_PROJECT_API_KEY=your-api-key
+ export LMNR_BASE_URL=https://app.lmnr.ai
+ # These will automatically be available in the agent-server
+
# Explicit override via JSON:
OH_AGENT_SERVER_ENV='{"DEBUG": "true", "CUSTOM_VAR": "value"}'
diff --git a/tests/unit/app_server/test_agent_server_env_override.py b/tests/unit/app_server/test_agent_server_env_override.py
index 5c1c1ea208..9b05b33a54 100644
--- a/tests/unit/app_server/test_agent_server_env_override.py
+++ b/tests/unit/app_server/test_agent_server_env_override.py
@@ -295,6 +295,68 @@ class TestLLMAutoForwarding:
assert 'Llm_Timeout' not in result
+class TestLMNRAutoForwarding:
+ """Test cases for automatic forwarding of LMNR_* environment variables."""
+
+ def test_auto_forward_prefixes_contains_lmnr(self):
+ """Test that LMNR_ is in the auto-forward prefixes."""
+ assert 'LMNR_' in AUTO_FORWARD_PREFIXES
+
+ def test_lmnr_project_api_key_auto_forwarded(self):
+ """Test that LMNR_PROJECT_API_KEY is automatically forwarded."""
+ env_vars = {
+ 'LMNR_PROJECT_API_KEY': 'sk-test-key-12345',
+ 'OTHER_VAR': 'should_not_be_included',
+ }
+
+ with patch.dict(os.environ, env_vars, clear=True):
+ result = get_agent_server_env()
+ assert 'LMNR_PROJECT_API_KEY' in result
+ assert result['LMNR_PROJECT_API_KEY'] == 'sk-test-key-12345'
+ assert 'OTHER_VAR' not in result
+
+ def test_lmnr_base_url_auto_forwarded(self):
+ """Test that LMNR_BASE_URL is automatically forwarded."""
+ env_vars = {
+ 'LMNR_BASE_URL': 'https://app.lmnr.ai',
+ }
+
+ with patch.dict(os.environ, env_vars, clear=True):
+ result = get_agent_server_env()
+ assert 'LMNR_BASE_URL' in result
+ assert result['LMNR_BASE_URL'] == 'https://app.lmnr.ai'
+
+ def test_multiple_lmnr_vars_auto_forwarded(self):
+ """Test that multiple LMNR_* variables are automatically forwarded."""
+ env_vars = {
+ 'LMNR_PROJECT_API_KEY': 'sk-test-key-12345',
+ 'LMNR_BASE_URL': 'https://app.lmnr.ai',
+ 'LMNR_VERBOSITY': 'debug',
+ }
+
+ with patch.dict(os.environ, env_vars, clear=True):
+ result = get_agent_server_env()
+ assert result['LMNR_PROJECT_API_KEY'] == 'sk-test-key-12345'
+ assert result['LMNR_BASE_URL'] == 'https://app.lmnr.ai'
+ assert result['LMNR_VERBOSITY'] == 'debug'
+
+ def test_lmnr_prefix_is_case_sensitive(self):
+ """Test that LMNR_ prefix matching is case-sensitive."""
+ env_vars = {
+ 'LMNR_PROJECT_API_KEY': 'sk-test-key', # Should be included
+ 'lmnr_project_api_key': 'lowercase', # Should NOT be included
+ 'Lmnr_Project_Api_Key': 'mixed', # Should NOT be included
+ }
+
+ with patch.dict(os.environ, env_vars, clear=True):
+ result = get_agent_server_env()
+ assert 'LMNR_PROJECT_API_KEY' in result
+ assert result['LMNR_PROJECT_API_KEY'] == 'sk-test-key'
+ # Lowercase variants should not be included
+ assert 'lmnr_project_api_key' not in result
+ assert 'Lmnr_Project_Api_Key' not in result
+
+
class TestDockerSandboxSpecEnvironmentOverride:
"""Test environment variable override integration in Docker sandbox specs."""