Skip to content

Commit 5d3bbce

Browse files
committed
fix(interface): resolve the DI container from the request context in system handlers
The provider health, metrics, config, list, and system status/metrics handlers resolved the DI container through the global service locator, so a caller that injected its own container (CLI router and MCP server both attach one to the request args) was ignored. Read the injected container when present and fall back to the global lookup otherwise, matching the machine handlers. Also make three environment-dependent unit tests order-independent by clearing ambient ORB_* vars and asserting the variables the shared test fixture actually sets.
1 parent f506f18 commit 5d3bbce

4 files changed

Lines changed: 33 additions & 14 deletions

File tree

src/orb/interface/system_command_handlers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def handle_provider_health(args) -> dict[str, Any]:
2626
GetProviderHealthOrchestrator,
2727
)
2828

29-
container = get_container()
29+
container = getattr(args, "_container", None) or get_container()
3030
orchestrator = container.get(GetProviderHealthOrchestrator)
3131
result = await orchestrator.execute(
3232
GetProviderHealthInput(
@@ -43,7 +43,7 @@ async def handle_list_providers(args) -> dict[str, Any]:
4343
from orb.application.services.orchestration.dtos import ListProvidersInput
4444
from orb.application.services.orchestration.list_providers import ListProvidersOrchestrator
4545

46-
container = get_container()
46+
container = getattr(args, "_container", None) or get_container()
4747
orchestrator = container.get(ListProvidersOrchestrator)
4848
result = await orchestrator.execute(
4949
ListProvidersInput(
@@ -68,7 +68,7 @@ async def handle_provider_config(args) -> dict[str, Any]:
6868
GetProviderConfigOrchestrator,
6969
)
7070

71-
container = get_container()
71+
container = getattr(args, "_container", None) or get_container()
7272
orchestrator = container.get(GetProviderConfigOrchestrator)
7373
result = await orchestrator.execute(GetProviderConfigInput())
7474
return {"config": result.config, "message": result.message}
@@ -110,7 +110,7 @@ async def handle_provider_metrics(args) -> dict[str, Any]:
110110
GetProviderMetricsOrchestrator,
111111
)
112112

113-
container = get_container()
113+
container = getattr(args, "_container", None) or get_container()
114114
orchestrator = container.get(GetProviderMetricsOrchestrator)
115115
result = await orchestrator.execute(
116116
GetProviderMetricsInput(
@@ -127,7 +127,7 @@ async def handle_reload_provider_config(args) -> InterfaceResponse:
127127
from orb.application.services.provider_registry_service import ProviderRegistryService
128128
from orb.interface.response_formatting_service import ResponseFormattingService
129129

130-
container = get_container()
130+
container = getattr(args, "_container", None) or get_container()
131131
formatter = container.get(ResponseFormattingService)
132132
try:
133133
registry = container.get(ProviderRegistryService)
@@ -145,7 +145,7 @@ async def handle_system_status(args) -> InterfaceResponse:
145145
from orb.infrastructure.di.buses import QueryBus
146146
from orb.interface.response_formatting_service import ResponseFormattingService
147147

148-
container = get_container()
148+
container = getattr(args, "_container", None) or get_container()
149149
query_bus = container.get(QueryBus)
150150
formatter = container.get(ResponseFormattingService)
151151

tests/unit/monitoring/test_health_path_resolution.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
from unittest.mock import patch
66

77

8-
def test_orb_root_dir_flows_through_to_health_check():
8+
def test_orb_root_dir_flows_through_to_health_check(monkeypatch):
99
"""ORB_ROOT_DIR env var should produce root/work/health as health location."""
1010
from orb.config.platform_dirs import get_health_location
1111

12+
# Remove any ambient ORB_WORK_DIR / ORB_HEALTH_DIR that a previous test
13+
# (e.g. one that drives CLI main() through its full setup_environment() path)
14+
# may have left via os.environ.setdefault. Without this guard the
15+
# per-dir overrides would short-circuit the ORB_ROOT_DIR derivation path.
16+
monkeypatch.delenv("ORB_WORK_DIR", raising=False)
17+
monkeypatch.delenv("ORB_HEALTH_DIR", raising=False)
18+
1219
with patch.dict(os.environ, {"ORB_ROOT_DIR": "/myroot"}, clear=False):
1320
result = get_health_location()
1421
assert result == Path("/myroot/work/health")

tests/unit/test_platform_dirs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,15 @@ def test_scripts_dir_env_override(self, monkeypatch):
460460
class TestEnvironmentOverrides:
461461
"""Test environment variable overrides."""
462462

463-
def test_all_env_vars_set(self):
463+
def test_all_env_vars_set(self, monkeypatch):
464464
"""Test all ORB_* environment variables set."""
465+
# Remove any ambient ORB_* vars that a previous test (e.g. one that
466+
# drives CLI main() through its full init path) may have left in the
467+
# environment via os.environ.setdefault, then overlay only what this
468+
# test needs so the assertions are order-independent.
469+
monkeypatch.delenv("ORB_ROOT_DIR", raising=False)
470+
monkeypatch.delenv("ORB_SCRIPTS_DIR", raising=False)
471+
465472
env_vars = {
466473
"ORB_CONFIG_DIR": "/env/config",
467474
"ORB_WORK_DIR": "/env/work",

tests/unit/test_setup.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99

1010
@pytest.mark.unit
1111
def test_environment_setup():
12-
"""Test that test environment is properly set up."""
13-
# Check environment variables - TESTING is set by pytest automatically
12+
"""Test that test environment is properly set up.
13+
14+
Asserts what the root conftest ``setup_test_environment`` fixture actually
15+
sets. Cloud-provider credentials (AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID,
16+
etc.) are deliberately NOT set by the root conftest so that live suites
17+
inherit the operator's real environment untouched.
18+
"""
19+
# PYTEST_CURRENT_TEST is set by pytest for the duration of each test
1420
assert os.environ.get("PYTEST_CURRENT_TEST") is not None
15-
assert os.environ.get("AWS_DEFAULT_REGION") == "us-east-1"
16-
assert os.environ.get("AWS_ACCESS_KEY_ID") == "testing"
17-
# ENVIRONMENT variable is set by conftest.py during test runs
18-
assert os.environ.get("AWS_DEFAULT_REGION") is not None
21+
# These vars are explicitly set by setup_test_environment in conftest.py
22+
assert os.environ.get("TESTING") == "true"
23+
assert os.environ.get("LOG_LEVEL") == "DEBUG"
1924

2025

2126
@pytest.mark.unit

0 commit comments

Comments
 (0)