Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions openhands-agent-server/openhands/agent_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
conversation_router,
)
from openhands.agent_server.conversation_service import (
ConversationService,
CredentialBindingActivationRequired,
get_default_conversation_service,
)
from openhands.agent_server.credential_binding import (
router as credential_binding_router,
Expand Down Expand Up @@ -379,7 +379,9 @@ async def stop_tool_preload_service():
# A key that came from the config file is visible only in ``config``;
# in-process callers of this server read it from the environment.
export_session_api_key(config)
service = get_default_conversation_service()
# Not get_default_conversation_service(): it rebuilds config from OH_*
# env, ignoring the config passed to create_app().
service = ConversationService.get_instance(config)
mark_initialization_complete()
logger.info("Server initialization complete - ready to serve requests")

Expand Down
47 changes: 38 additions & 9 deletions tests/agent_server/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import pytest
from fastapi.testclient import TestClient

from openhands.agent_server import (
config as config_module,
conversation_service as conversation_service_module,
)
from openhands.agent_server.api import (
_cleanup_stale_tmux_sessions,
_default_server_tmux_tmpdir,
Expand Down Expand Up @@ -595,7 +599,7 @@ async def slow_start():
# Mock the service getters
with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch(
Expand Down Expand Up @@ -639,7 +643,7 @@ async def slow_stop():
# Mock the service getters
with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch(
Expand Down Expand Up @@ -675,7 +679,7 @@ async def test_services_handle_none_values(self):
# Mock all services as None (disabled)
with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -706,7 +710,7 @@ async def test_registry_starts_before_conversation_recovery(self):

with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -738,7 +742,7 @@ async def test_lifespan_defaults_and_restores_tmux_tmpdir(

with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -766,7 +770,7 @@ async def test_lifespan_keeps_user_provided_tmux_tmpdir(

with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -800,7 +804,7 @@ async def test_lifespan_removes_substitute_for_too_deep_tmux_tmpdir(

with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -839,7 +843,7 @@ def fail_rmtree(path):

with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -870,7 +874,7 @@ async def test_lifespan_keeps_preexisting_default_tmux_tmpdir(

with (
patch(
"openhands.agent_server.api.get_default_conversation_service",
"openhands.agent_server.api.ConversationService.get_instance",
return_value=mock_conversation_service,
),
patch("openhands.agent_server.api.get_vscode_service", return_value=None),
Expand Down Expand Up @@ -1102,3 +1106,28 @@ def test_4xx_http_exception_logged_at_info_without_traceback(self, caplog):
info_records = [r for r in api_records if r.levelno == logging.INFO]
assert info_records, "Expected an INFO log line for a 4xx HTTPException"
assert all(r.exc_info is None for r in info_records)


def test_lifespan_conversation_service_uses_app_config_not_env(tmp_path, monkeypatch):
"""create_app(config) must persist and notify per that config, not OH_* env.

Inside an OpenHands sandbox these env vars point at the live conversations
directory and the app server's webhook endpoint, so a test app that fell
back to them would write into, and report to, the real deployment.
"""
monkeypatch.setenv("OH_CONVERSATIONS_PATH", str(tmp_path / "env-conversations"))
monkeypatch.setenv("OH_WEBHOOKS_0_BASE_URL", "http://env-webhook.invalid/hooks")
monkeypatch.setattr(config_module, "_default_config", None)
monkeypatch.setattr(conversation_service_module, "_conversation_service", None)
config = Config(
static_files_path=None,
session_api_keys=[],
conversations_path=tmp_path / "conversations",
bash_events_dir=tmp_path / "bash_events",
)

app = create_app(config)
with TestClient(app):
service = app.state.conversation_service
assert service.conversations_dir == tmp_path / "conversations"
assert service.webhook_specs == []
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Common test fixtures and utilities."""

import os
import re
import uuid
from pathlib import Path
from unittest.mock import MagicMock
Expand All @@ -21,6 +23,20 @@
TOKENIZER_FIXTURES_DIR / "qwen3-4b-instruct-2507-tokenizer_config.json"
)

# Settings that point an agent server at, or authenticate it to, a live
# deployment. A run inside an OpenHands sandbox inherits them from the sandbox.
_LIVE_DEPLOYMENT_ENV = re.compile(
r"SESSION_API_KEY|OH_SESSION_API_KEYS_\d+|OH_WEBHOOKS(_\w+)?"
r"|OH_CONVERSATIONS_PATH|OH_BASH_EVENTS_DIR|OH_INTERNAL_SERVER_URL"
r"|OH_LLM_API_KEY_REFRESH_\w+"
)


def pytest_configure(config: pytest.Config) -> None:
# Before collection: some modules read the agent-server config at import.
for name in [n for n in os.environ if _LIVE_DEPLOYMENT_ENV.fullmatch(n)]:
del os.environ[name]


def pytest_addoption(parser: pytest.Parser) -> None:
group = parser.getgroup("examples")
Expand Down
Loading