From 5b04a3a4cf5ef38c329574d439abe375770314b5 Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Mon, 9 Nov 2026 17:15:48 +0100 Subject: [PATCH 1/4] refactor(agent-server): one ServerContext per (re)initialisation A server's config, its six persistent stores, its conversation registry and its conversation and bash services now live together in one frozen ServerContext. It is built when the app is created, completed by the lifespan once the services are started, and rebuilt whole by POST /api/init, which installs it with a single assignment after everything is up. A failed init stops what it started and leaves the dormant context untouched. Request handlers reach the context through the dependencies in dependencies.py (get_config, get_cipher, get_stores, get_conversation_registry, get_conversation_service, get_server_bash_event_service). Code that runs outside a request is handed the store or service it needs: ConversationService, the conversation registries, the MCP OAuth token store, the telemetry sink, the local secret resolver, the git provider lookup, the OpenAI gateway and the launch stores all take them as arguments. Removed, with no replacement kept beside the new path: - config.get_default_config and the cached default config - conversation_service.get_default_conversation_service and its singleton - bash_service.get_default_bash_event_service and its singleton - persistence.get_settings_store, get_secrets_store, get_provider_connections_store, get_workspaces_store, get_llm_profile_store, get_agent_profile_store, reset_stores and the six module singletons - the module-level conversation_service / bash_event_service in sockets.py and the fallbacks to them - _secrets_exposure.get_config / get_cipher, openai.router._get_config and every direct read of app.state.config, .conversation_service, .bash_event_service and .conversation_registry ServerStores resolves the cipher and the persistence directories once, when it is built, and creates each store on first use, so building a context touches no file. The lifespan's bash service is now built from the config given to create_app (it used to come from the environment) and is closed on shutdown. A test in tests/agent_server/test_server_context.py fails if a module other than server_context.py reads config-derived app.state, or defines a get_default_* function. The REST surface is unchanged: the OpenAPI document is byte-identical before and after. Part of #6456 (step 5, ServerContext half). --- .../references/feature-map/F14-files.md | 2 +- .../feature-map/F17-workspaces-registry.md | 8 +- .../scripts/control_agent_server.py | 4 +- .../agent_server/_secrets_exposure.py | 16 - .../openhands/agent_server/acp_router.py | 11 +- .../agent_server/agent_profiles_router.py | 92 +- .../openhands/agent_server/api.py | 80 +- .../openhands/agent_server/bash_service.py | 18 - .../agent_server/canvas_extensions/backend.py | 12 +- .../agent_server/canvas_extensions/bridge.py | 16 +- .../agent_server/canvas_extensions_router.py | 6 +- .../openhands/agent_server/config.py | 12 - .../agent_server/conversation_registry.py | 14 +- .../agent_server/conversation_router.py | 26 +- .../agent_server/conversation_service.py | 124 +- .../agent_server/credential_binding.py | 5 +- .../openhands/agent_server/dependencies.py | 79 +- .../agent_server/docker_runtime/hybrid.py | 7 +- .../agent_server/docker_runtime/registry.py | 9 +- .../agent_server/docker_runtime/routers.py | 17 +- .../openhands/agent_server/file_router.py | 4 +- .../agent_server/git_provider_service.py | 14 +- .../openhands/agent_server/git_router.py | 4 +- .../openhands/agent_server/init_router.py | 118 +- .../openhands/agent_server/launch.py | 12 +- .../agent_server/local_secret_resolver.py | 18 +- .../openhands/agent_server/mcp_oauth_store.py | 19 +- .../openhands/agent_server/mcp_router.py | 7 +- .../agent_server/meta_profiles_router.py | 16 +- .../openhands/agent_server/openai/router.py | 35 +- .../openhands/agent_server/openai/service.py | 32 +- .../agent_server/persistence/__init__.py | 16 +- .../agent_server/persistence/store.py | 302 ++--- .../openhands/agent_server/profiles_router.py | 43 +- .../provider_connections_router.py | 44 +- .../openhands/agent_server/server_context.py | 83 ++ .../agent_server/server_details_router.py | 3 +- .../openhands/agent_server/session_socket.py | 4 +- .../openhands/agent_server/settings_router.py | 34 +- .../openhands/agent_server/skills_router.py | 4 +- .../openhands/agent_server/sockets.py | 65 +- .../agent_server/sub_agents_router.py | 4 +- .../agent_server/telemetry/service.py | 31 +- .../agent_server/tool_preload_service.py | 8 +- .../openhands/agent_server/tool_router.py | 4 +- .../openhands/agent_server/vscode_router.py | 16 +- .../openhands/agent_server/vscode_service.py | 14 +- .../agent_server/workspaces_router.py | 18 +- scripts/build_config_template.py | 4 +- .../test_canvas_extension_backend.py | 4 +- .../test_canvas_extension_bridge.py | 25 +- tests/agent_server/conftest.py | 108 +- .../docker_runtime/test_disk_budget.py | 23 +- .../docker_runtime/test_hybrid.py | 39 +- .../docker_runtime/test_mediation.py | 37 +- .../agent_server/docker_runtime/test_proxy.py | 10 +- .../docker_runtime/test_registry.py | 16 +- .../docker_runtime/test_retention.py | 14 +- .../docker_runtime/test_routes.py | 136 ++- .../docker_runtime/test_storage_scenario.py | 17 +- .../docker_runtime/test_worktree_live.py | 5 +- tests/agent_server/stress/conftest.py | 10 +- .../agent_server/stress/test_slow_webhook.py | 5 +- tests/agent_server/telemetry/conftest.py | 35 +- .../test_telemetry_disabled_by_default.py | 46 +- .../telemetry/test_telemetry_failure_paths.py | 10 +- .../test_telemetry_init_lifecycle.py | 27 +- .../telemetry/test_telemetry_settings_path.py | 14 +- tests/agent_server/test_acp_router.py | 28 +- .../test_agent_launch_additions.py | 6 +- .../test_agent_profile_conv_start.py | 27 +- .../test_agent_profiles_router.py | 79 +- tests/agent_server/test_api.py | 58 +- tests/agent_server/test_api_authentication.py | 10 +- tests/agent_server/test_bash_router.py | 5 +- tests/agent_server/test_bash_service.py | 4 +- .../test_blocking_export_extended.py | 6 +- .../agent_server/test_conversation_router.py | 279 ++--- .../test_conversation_router_skill_trim.py | 11 +- .../agent_server/test_conversation_service.py | 78 +- tests/agent_server/test_conversation_tags.py | 2 + tests/agent_server/test_credential_binding.py | 53 +- tests/agent_server/test_dependencies.py | 7 +- tests/agent_server/test_event_router.py | 8 +- .../test_event_router_websocket.py | 104 +- tests/agent_server/test_event_socket_races.py | 15 +- tests/agent_server/test_file_router.py | 164 ++- .../agent_server/test_git_provider_service.py | 24 +- tests/agent_server/test_init_router.py | 787 ++++++------ tests/agent_server/test_launch_parity.py | 37 +- .../test_local_secret_resolver.py | 8 +- tests/agent_server/test_local_storage.py | 7 - tests/agent_server/test_mcp_oauth_store.py | 1057 ++++++++--------- tests/agent_server/test_mcp_router.py | 28 +- .../agent_server/test_meta_profiles_router.py | 18 +- .../test_openai_gateway_docker.py | 7 +- .../test_openai_gateway_launch.py | 35 +- .../test_profile_cipher_mismatch.py | 45 +- .../test_profile_resume_secret_scope.py | 37 +- .../test_profile_store_lock_responsiveness.py | 3 - .../test_profile_store_persistence_dir.py | 94 +- tests/agent_server/test_profiles_router.py | 122 +- tests/agent_server/test_runtime_router.py | 22 +- tests/agent_server/test_server_context.py | 331 ++++++ tests/agent_server/test_settings_router.py | 43 +- .../test_sockets_service_getters.py | 195 ++- .../test_store_cipher_mismatch.py | 7 +- tests/agent_server/test_tool_router.py | 43 +- tests/agent_server/test_trace_shutdown.py | 12 +- tests/agent_server/test_vscode_service.py | 69 +- .../test_websocket_first_message_auth.py | 66 +- tests/agent_server/test_workspaces_router.py | 17 +- tests/cross/conftest.py | 6 +- .../test_remote_conversation_live_server.py | 616 +++++----- 114 files changed, 3446 insertions(+), 3349 deletions(-) create mode 100644 openhands-agent-server/openhands/agent_server/server_context.py create mode 100644 tests/agent_server/test_server_context.py diff --git a/.agents/skills/verify-agent-server/references/feature-map/F14-files.md b/.agents/skills/verify-agent-server/references/feature-map/F14-files.md index cc9a77db187..65d30d0b506 100644 --- a/.agents/skills/verify-agent-server/references/feature-map/F14-files.md +++ b/.agents/skills/verify-agent-server/references/feature-map/F14-files.md @@ -922,7 +922,7 @@ Preconditions: event file, skips a top-level `acp/` directory, and redacts JSON/JSONL credential fields plus any Fernet token. It writes a fixed temp name (`F14.trajectory-concurrent`) and reads `conversations_path` from the live - `app.state.config`, so it follows an init-time path + server context's config, so it follows an init-time path (`F14.trajectory-init-path`). - No file route emits conversation events or WebSocket frames; their second views are downloads, listings, archives and the disk. diff --git a/.agents/skills/verify-agent-server/references/feature-map/F17-workspaces-registry.md b/.agents/skills/verify-agent-server/references/feature-map/F17-workspaces-registry.md index 3b6af1313a0..8b196f2b115 100644 --- a/.agents/skills/verify-agent-server/references/feature-map/F17-workspaces-registry.md +++ b/.agents/skills/verify-agent-server/references/feature-map/F17-workspaces-registry.md @@ -683,9 +683,11 @@ Preconditions: ## Gotchas -- The workspaces store is a process-wide singleton created on the first - request (`get_workspaces_store`), so the file location is fixed for the - life of the process: changing `OH_PERSISTENCE_DIR` needs a restart. +- The workspaces store belongs to the server context + (`ServerStores.workspaces`) and its location is resolved when that context + is built, so it is fixed for the life of the process: changing + `OH_PERSISTENCE_DIR` needs a restart (a deferred-init server resolves it + again at `POST /api/init`). Without it, the location follows `conversations_path`, which for the default relative `workspace/conversations` is `./workspace/.openhands` under the server's working directory. The contents are not cached: every diff --git a/.agents/skills/verify-agent-server/scripts/control_agent_server.py b/.agents/skills/verify-agent-server/scripts/control_agent_server.py index 8e24fc05e3e..74492a086c6 100644 --- a/.agents/skills/verify-agent-server/scripts/control_agent_server.py +++ b/.agents/skills/verify-agent-server/scripts/control_agent_server.py @@ -3678,7 +3678,7 @@ def route_templates(checkout: Path | None = None) -> list[dict[str, Any]]: "from fastapi.routing import APIRoute, APIWebSocketRoute\n" "from starlette.routing import Route\n" "from openhands.agent_server.api import create_app\n" - "from openhands.agent_server.config import get_default_config\n" + "from openhands.agent_server.config import load_config\n" "def table(app):\n" " out = []\n" " for r in app.routes:\n" @@ -3698,7 +3698,7 @@ def route_templates(checkout: Path | None = None) -> list[dict[str, Any]]: " return out\n" "out = table(create_app())\n" "seen = {(r['method'], r['path']) for r in out}\n" - "docker = get_default_config().model_copy(update={'conversation_runtime': 'docker'})\n" + "docker = load_config().model_copy(update={'conversation_runtime': 'docker'})\n" "for r in table(create_app(docker)):\n" " if (r['method'], r['path']) not in seen:\n" " out.append({**r, 'config': 'conversation_runtime=docker'})\n" diff --git a/openhands-agent-server/openhands/agent_server/_secrets_exposure.py b/openhands-agent-server/openhands/agent_server/_secrets_exposure.py index 3f080e782bd..6e5130bd6e9 100644 --- a/openhands-agent-server/openhands/agent_server/_secrets_exposure.py +++ b/openhands-agent-server/openhands/agent_server/_secrets_exposure.py @@ -25,22 +25,6 @@ ExposeSecretsMode = Literal["encrypted", "plaintext"] -def get_config(request: Request): - """Get config from app state, raising 503 if uninitialized.""" - config = getattr(request.app.state, "config", None) - if config is None: - raise HTTPException( - status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail="Server not fully initialized", - ) - return config - - -def get_cipher(request: Request) -> Cipher | None: - """Get the configured cipher (``None`` when ``OH_SECRET_KEY`` is unset).""" - return get_config(request).cipher - - def parse_expose_secrets_header(request: Request) -> ExposeSecretsMode | None: """Parse the ``X-Expose-Secrets`` header. diff --git a/openhands-agent-server/openhands/agent_server/acp_router.py b/openhands-agent-server/openhands/agent_server/acp_router.py index 46110521cc3..5a3abea9498 100644 --- a/openhands-agent-server/openhands/agent_server/acp_router.py +++ b/openhands-agent-server/openhands/agent_server/acp_router.py @@ -12,10 +12,10 @@ from fastapi import APIRouter, HTTPException, Request, status from pydantic import BaseModel, Field -from openhands.agent_server._secrets_exposure import get_config from openhands.agent_server.config import Config from openhands.agent_server.credential_binding import LocalVersionedCredentialBinding -from openhands.agent_server.persistence import get_secrets_store +from openhands.agent_server.dependencies import get_config, get_stores +from openhands.agent_server.persistence import FileSecretsStore from openhands.sdk.agent.acp_file_credentials import CODEX_AUTH_SECRET_NAME from openhands.sdk.agent.acp_model_discovery import discover_acp_models from openhands.sdk.agent.acp_models import ACPModelDiscovery @@ -130,11 +130,10 @@ def _cache_key( def _codex_bindings( - settings: ACPAgentSettings, config: Config, secrets: dict[str, str] + settings: ACPAgentSettings, store: FileSecretsStore, secrets: dict[str, str] ) -> dict[str, VersionedCredentialBinding]: if settings.acp_server != "codex": return {} - store = get_secrets_store(config) if store.get_secret(CODEX_AUTH_SECRET_NAME) is None: return {} # A stored ChatGPT login refreshes through the store, as in a conversation. @@ -169,7 +168,9 @@ async def discover_models( raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc secrets = await asyncio.to_thread(_resolve_secrets, body.secrets) - bindings = await asyncio.to_thread(_codex_bindings, settings, config, secrets) + bindings = await asyncio.to_thread( + _codex_bindings, settings, get_stores(request).secrets, secrets + ) key = _cache_key(settings, command, secrets) async def run() -> ACPModelDiscovery: diff --git a/openhands-agent-server/openhands/agent_server/agent_profiles_router.py b/openhands-agent-server/openhands/agent_server/agent_profiles_router.py index 03ca50f4af9..55c48ff8758 100644 --- a/openhands-agent-server/openhands/agent_server/agent_profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/agent_profiles_router.py @@ -18,23 +18,14 @@ from fastapi import APIRouter, HTTPException, Path, Request, status from pydantic import BaseModel, ConfigDict, Field, PlainValidator, ValidationError -from openhands.agent_server._secrets_exposure import ( - get_cipher, - get_config, - store_errors, -) -from openhands.agent_server.config import Config +from openhands.agent_server._secrets_exposure import store_errors +from openhands.agent_server.dependencies import get_cipher, get_config, get_stores from openhands.agent_server.launch import ( can_probe_tools, server_launch_stores, target_launch_runtime, ) -from openhands.agent_server.persistence import ( - PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_settings_store, -) +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.agent_server.profiles_router import MAX_PROFILES, _has_api_key from openhands.sdk.launch import preview_launch from openhands.sdk.llm import LLM @@ -149,7 +140,9 @@ def _llm_has_real_config(llm: LLM) -> bool: return False -def _seed_default_llm_profile(llm: LLM, cipher: Cipher | None) -> str: +def _seed_default_llm_profile( + llm: LLM, stores: ServerStores, cipher: Cipher | None +) -> str: """Mirror the live LLM config into a ``SEED_PROFILE_NAME`` LLM profile. ``build_seed_profile`` falls back to the literal name ``SEED_PROFILE_NAME`` @@ -179,7 +172,7 @@ def _seed_default_llm_profile(llm: LLM, cipher: Cipher | None) -> str: ``list()`` membership check — that mismatch would otherwise let ``save()`` silently clobber it. """ - llm_store = get_llm_profile_store() + llm_store = stores.llm_profiles with store_errors(): try: llm_store.load(SEED_PROFILE_NAME, cipher=cipher) @@ -223,9 +216,7 @@ def _seed_default_llm_profile(llm: LLM, cipher: Cipher | None) -> str: return SEED_PROFILE_NAME -def _ensure_active_profile( - store: AgentProfileStore, config: Config, cipher: Cipher | None -) -> None: +def _ensure_active_profile(stores: ServerStores, cipher: Cipher | None) -> None: """Keep ``active_agent_profile_id`` on a stored profile. A missing or dangling pointer moves to ``default``, which is seeded from the @@ -234,13 +225,14 @@ def _ensure_active_profile( other profile is activated instead. The lock spans the check, the seed and the pointer write, so concurrent callers seed once and agree on the pointer. """ - settings_store = get_settings_store(config) + store = stores.agent_profiles + settings_store = stores.settings with store_errors(), store.lock(): settings = settings_store.load() or PersistedSettings() active_id = settings.active_agent_profile_id if active_id is not None and store.name_for_id(active_id) is not None: return - target = _profile_to_activate(store, settings, cipher) + target = _profile_to_activate(stores, settings, cipher) if target is None: return name, profile_id = target @@ -254,15 +246,16 @@ def set_pointer(s: PersistedSettings) -> PersistedSettings: def _profile_to_activate( - store: AgentProfileStore, settings: PersistedSettings, cipher: Cipher | None + stores: ServerStores, settings: PersistedSettings, cipher: Cipher | None ) -> tuple[str, str] | None: """Return the name and id of ``default``, seeding it if the store has room.""" + store = stores.agent_profiles try: profile = store.load(SEED_PROFILE_NAME) return profile.name, str(profile.id) except FileNotFoundError: try: - profile = _seed_default_profile(store, settings, cipher) + profile = _seed_default_profile(stores, settings, cipher) return profile.name, str(profile.id) except ProfileLimitExceeded: pass @@ -283,7 +276,7 @@ def _profile_to_activate( def _seed_default_profile( - store: AgentProfileStore, settings: PersistedSettings, cipher: Cipher | None + stores: ServerStores, settings: PersistedSettings, cipher: Cipher | None ) -> OpenHandsAgentProfile | ACPAgentProfile: active_llm_profile = settings.active_profile # Falsy check (not `is None`): mirrors build_seed_profile's own @@ -295,21 +288,21 @@ def _seed_default_profile( # on the same condition or the exact #3933 dangling ref reappears. if not active_llm_profile and settings.agent_settings.agent_kind != "acp": active_llm_profile = _seed_default_llm_profile( - settings.agent_settings.llm, cipher + settings.agent_settings.llm, stores, cipher ) profile = build_seed_profile(settings.agent_settings, active_llm_profile) - store.save(profile, max_profiles=MAX_AGENT_PROFILES) + stores.agent_profiles.save(profile, max_profiles=MAX_AGENT_PROFILES) logger.info(f"Seeded default agent profile '{profile.name}' (id={profile.id})") return profile def active_agent_profile( - config: Config, cipher: Cipher | None + stores: ServerStores, cipher: Cipher | None ) -> OpenHandsAgentProfile | ACPAgentProfile | None: """Return the active Agent Profile, activating ``default`` when none is.""" - store = get_agent_profile_store() - _ensure_active_profile(store, config, cipher) - settings = get_settings_store(config).load() or PersistedSettings() + store = stores.agent_profiles + _ensure_active_profile(stores, cipher) + settings = stores.settings.load() or PersistedSettings() if settings.active_agent_profile_id is None: return None with store_errors(): @@ -340,12 +333,12 @@ def list_agent_profiles(request: Request) -> AgentProfileListResponse: Some profile is always active: with no valid pointer, ``default`` is activated, and seeded from the current ``agent_settings`` when absent. """ - config = get_config(request) - settings_store = get_settings_store(config) + stores = get_stores(request) + settings_store = stores.settings settings = settings_store.load() or PersistedSettings() - store = get_agent_profile_store() - _ensure_active_profile(store, config, get_cipher(request)) + store = stores.agent_profiles + _ensure_active_profile(stores, get_cipher(request)) settings = settings_store.load() or settings with store_errors(): @@ -358,14 +351,16 @@ def list_agent_profiles(request: Request) -> AgentProfileListResponse: @agent_profiles_router.get("/{name}", response_model=AgentProfileDetailResponse) -def get_agent_profile(name: ProfileName) -> AgentProfileDetailResponse: +def get_agent_profile( + request: Request, name: ProfileName +) -> AgentProfileDetailResponse: """Get a stored profile. A profile is secret-free at rest (#4017), so there is nothing to mask or expose — unlike the LLM ``/api/profiles`` router, this endpoint has no ``X-Expose-Secrets`` behavior. """ - store = get_agent_profile_store() + store = get_stores(request).agent_profiles try: with store_errors(): profile = store.load(name) @@ -408,7 +403,7 @@ def _validate_profile_payload( status_code=status.HTTP_201_CREATED, ) def save_agent_profile( - name: ProfileName, body: dict[str, Any] + request: Request, name: ProfileName, body: dict[str, Any] ) -> AgentProfileMutationResponse: """Save an ``AgentProfile`` under ``name`` (overwriting a namesake). @@ -419,7 +414,7 @@ def save_agent_profile( """ profile = _validate_profile_payload({**body, "name": name}) - store = get_agent_profile_store() + store = get_stores(request).agent_profiles # The id is server-managed (the active pointer is keyed on it): overwrite # keeps the namesake's id and bumps revision; create mints a fresh id, # ignoring any client-supplied one. ``save_profile_preserving_identity`` @@ -457,15 +452,15 @@ def delete_agent_profile( reset to default OpenHands agent settings so stale ACP configuration does not outlive the profile it came from; server-wide settings are carried over. """ - store = get_agent_profile_store() + stores = get_stores(request) + store = stores.agent_profiles deleted_id = _summary_id_for_name(store, name) with store_errors(): store.delete(name) if deleted_id is not None: - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = stores.settings settings = settings_store.load() or PersistedSettings() if settings.active_agent_profile_id == deleted_id: @@ -486,7 +481,7 @@ def clear_pointer_and_reset_settings( settings_store.update(clear_pointer_and_reset_settings) logger.info(f"Cleared active pointer for deleted profile '{name}'") - _ensure_active_profile(store, config, get_cipher(request)) + _ensure_active_profile(stores, get_cipher(request)) logger.info(f"Deleted agent profile '{name}'") return AgentProfileMutationResponse( @@ -498,7 +493,7 @@ def clear_pointer_and_reset_settings( "/{name}/rename", response_model=AgentProfileMutationResponse ) def rename_agent_profile( - name: ProfileName, body: RenameAgentProfileRequest + request: Request, name: ProfileName, body: RenameAgentProfileRequest ) -> AgentProfileMutationResponse: """Rename a stored profile atomically. @@ -506,7 +501,7 @@ def rename_agent_profile( survives the rename untouched. Returns 404 if the source is missing, 409 if ``new_name`` is taken. """ - store = get_agent_profile_store() + store = get_stores(request).agent_profiles try: with store_errors(): store.rename(name, body.new_name) @@ -541,7 +536,7 @@ def activate_agent_profile( ``/activate``, this does **not** write ``agent_settings`` (the creation-time-only contract). Returns 404 if no stored profile has that id. """ - store = get_agent_profile_store() + store = get_stores(request).agent_profiles with store_errors(): known_ids = { str(s["id"]) for s in store.list_summaries() if s.get("id") is not None @@ -552,8 +547,7 @@ def activate_agent_profile( detail=f"Agent profile with id '{profile_id}' not found", ) - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings def set_pointer(settings: PersistedSettings) -> PersistedSettings: settings.active_agent_profile_id = profile_id @@ -601,7 +595,7 @@ async def materialize_agent_profile( if body is not None and body.profile is not None: profile = _validate_profile_payload({**body.profile, "name": name}) else: - store = get_agent_profile_store() + store = get_stores(request).agent_profiles try: with store_errors(): # Off the event loop: the store lock wait blocks (up to 30 s). @@ -613,12 +607,14 @@ async def materialize_agent_profile( ) config = get_config(request) - settings = get_settings_store(config).load() or PersistedSettings() + stores = get_stores(request) + cipher = get_cipher(request) + settings = stores.settings.load() or PersistedSettings() context = settings.agent_settings.agent_context return await asyncio.to_thread( lambda: preview_launch( profile, - server_launch_stores(settings, get_cipher(request)), + server_launch_stores(settings, stores, cipher), target_launch_runtime(config), load_memory=context is not None and context.load_memory, check_usable=can_probe_tools(config), diff --git a/openhands-agent-server/openhands/agent_server/api.py b/openhands-agent-server/openhands/agent_server/api.py index 759fb11f81c..bbb7f947995 100644 --- a/openhands-agent-server/openhands/agent_server/api.py +++ b/openhands-agent-server/openhands/agent_server/api.py @@ -9,6 +9,7 @@ from collections.abc import AsyncIterator, Sequence from concurrent.futures import Future from contextlib import asynccontextmanager, suppress +from dataclasses import replace from importlib.metadata import version from pathlib import Path from typing import Any @@ -28,7 +29,7 @@ from openhands.agent_server.agent_profiles_router import agent_profiles_router from openhands.agent_server.auth_router import auth_router from openhands.agent_server.bash_router import bash_router -from openhands.agent_server.bash_service import get_default_bash_event_service +from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.canvas_extensions.backend import ( CanvasExtensionBackendManager, ) @@ -41,15 +42,12 @@ V1_SESSION_API_KEY_ENV, Config, export_session_api_key, - get_default_config, + load_config, ) from openhands.agent_server.conversation_lease import ( ConversationOwnershipLostError, _is_pid_alive, ) -from openhands.agent_server.conversation_registry import ( - create_conversation_registry, -) from openhands.agent_server.conversation_router import ( conversation_catalog_router, conversation_router, @@ -89,6 +87,11 @@ from openhands.agent_server.provider_connections_router import ( provider_connections_router, ) +from openhands.agent_server.server_context import ( + build_server_context, + install_server_context, + server_context_of, +) from openhands.agent_server.server_details_router import ( get_server_info, mark_initialization_complete, @@ -316,28 +319,31 @@ async def api_lifespan(api: FastAPI) -> AsyncIterator[None]: if backend_manager is not None: await backend_manager.reap_orphans() - config: Config = api.state.config + context = server_context_of(api) + config = context.config deferred = config.deferred_init - conversation_registry = getattr( - api.state, "conversation_registry", None - ) or create_conversation_registry(config) - api.state.conversation_registry = conversation_registry + conversation_registry = context.conversation_registry # Answer our own LookupSecret URLs in-process; a loopback fetch made - # from the event loop cannot be served by the loop blocked on it. - secret_resolution = local_secret_resolution(config) + # from the event loop cannot be served by the loop blocked on it. The + # store is looked up per resolution: POST /api/init replaces it. + secret_resolution = local_secret_resolution( + lambda: server_context_of(api).stores.secrets + ) secret_resolution.__enter__() # Deferred pods boot with telemetry disabled and are rebuilt by # InitService, so they emit `server_started` there instead. - api.state.telemetry_sink = await build_telemetry_sink(config) + api.state.telemetry_sink = await build_telemetry_sink( + config, context.stores.settings + ) if not deferred: emit_server_started() seal_tool_catalog() - vscode_service = get_vscode_service() - tool_preload_service = get_tool_preload_service() + vscode_service = get_vscode_service(config) + tool_preload_service = get_tool_preload_service(config) # Define async functions for starting each service async def start_vscode_service(): @@ -416,29 +422,35 @@ async def stop_tool_preload_service(): # Non-deferred (legacy) path: build and enter the conversation # service as part of the lifespan, exactly as before. - await asyncio.to_thread(reencrypt_stale_stores, config) + await asyncio.to_thread(reencrypt_stale_stores, context.stores) # # 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) - # Not get_default_conversation_service(): it rebuilds config from OH_* - # env, ignoring the config passed to create_app(). - service = ConversationService.get_instance(config) + service = ConversationService.get_instance(config, context.stores) mark_initialization_complete() logger.info("Server initialization complete - ready to serve requests") - bash_svc = get_default_bash_event_service() - api.state.bash_event_service = bash_svc + bash_svc = BashEventService( + bash_events_dir=config.bash_events_dir, cipher=config.cipher + ) conversation_registry.configure_service(service) # Runtime cleanup must precede external-catalog recovery so stale # runtime owners cannot lose their expired leases to the outer service. await conversation_registry.start() - async with service: - api.state.conversation_service = service + async with bash_svc, service: + # The same config, stores and registry, now with their services. + install_server_context( + api, + replace( + context, + conversation_service=service, + bash_event_service=bash_svc, + ), + ) - config = api.state.config retention_task: asyncio.Task | None = None if config.bash_events_retention_seconds is not None: retention_task = asyncio.create_task( @@ -570,7 +582,7 @@ class AgentServerApp(FastAPI): def build_middleware_stack(self) -> ASGIApp: # ServerErrorMiddleware creates unhandled 500 responses outside user # middleware, so CORS must wrap the complete stack to decorate them. - config = self.state.config + config = server_context_of(self).config return CORSDispatcher( super().build_middleware_stack(), allow_origins=config.allow_cors_origins, @@ -617,7 +629,7 @@ def _find_http_exception(exc: BaseExceptionGroup) -> HTTPException | None: def _add_api_routes(app: FastAPI) -> None: """Add all API routes to the FastAPI application.""" - conversation_registry = app.state.conversation_registry + conversation_registry = server_context_of(app).conversation_registry app.include_router(server_details_router) # The /api/init endpoint bypasses both the session-key auth and the @@ -632,8 +644,8 @@ def _add_api_routes(app: FastAPI) -> None: # Header-only auth: applied to every /api/* route EXCEPT the workspace # static-file routes (handled separately below). Cookies are NOT honored # here so that we don't expand the CSRF surface across the whole API. - # check_session_api_key reads config from request.app.state at request time, - # so keys delivered via POST /api/init are honoured without re-registering routes. + # check_session_api_key reads the server context at request time, so keys + # delivered via POST /api/init are honoured without re-registering routes. dependencies = [ # Dormant gate: 503s every /api/* route until POST /api/init completes. # No-op for non-deferred deployments. @@ -937,17 +949,19 @@ def create_app(config: Config | None = None) -> FastAPI: """Create and configure the FastAPI application. Args: - config: Configuration object. If None, uses default config. + config: Configuration object. If None, it is loaded from the config + file and the ``OH_*`` environment variables. Returns: Configured FastAPI application. """ if config is None: - config = get_default_config() + config = load_config() app = _create_fastapi_instance(config) - app.state.config = config - app.state.conversation_registry = create_conversation_registry(config) - app.state.canvas_extension_backend_manager = CanvasExtensionBackendManager() + install_server_context(app, build_server_context(config)) + app.state.canvas_extension_backend_manager = CanvasExtensionBackendManager( + secrets_store=lambda: server_context_of(app).stores.secrets + ) app.state.app_backend_session_store = AppBackendSessionStore() _add_api_routes(app) diff --git a/openhands-agent-server/openhands/agent_server/bash_service.py b/openhands-agent-server/openhands/agent_server/bash_service.py index 3104e6e4ebb..6f020e8cbc1 100644 --- a/openhands-agent-server/openhands/agent_server/bash_service.py +++ b/openhands-agent-server/openhands/agent_server/bash_service.py @@ -627,21 +627,3 @@ async def __aenter__(self): async def __aexit__(self, exc_type, exc_value, traceback): """Finish using this task service""" await self.close() - - -_bash_event_service: BashEventService | None = None - - -def get_default_bash_event_service() -> BashEventService: - """Get the default bash event service instance.""" - global _bash_event_service - if _bash_event_service: - return _bash_event_service - - from openhands.agent_server.config import get_default_config - - config = get_default_config() - _bash_event_service = BashEventService( - bash_events_dir=config.bash_events_dir, cipher=config.cipher - ) - return _bash_event_service diff --git a/openhands-agent-server/openhands/agent_server/canvas_extensions/backend.py b/openhands-agent-server/openhands/agent_server/canvas_extensions/backend.py index a387fe0b1a2..7c7019abc91 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions/backend.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions/backend.py @@ -17,6 +17,7 @@ import urllib.request import zlib from collections import deque +from collections.abc import Callable from pathlib import Path, PurePosixPath from typing import Final, Literal from urllib.parse import urljoin, urlsplit @@ -34,7 +35,7 @@ CanvasExtensionBackendArtifact, CanvasExtensionManifest, ) -from openhands.agent_server.persistence.store import SecretsStore, get_secrets_store +from openhands.agent_server.persistence.store import SecretsStore from openhands.sdk.conversation.secret_registry import StreamOutputMask from openhands.sdk.utils.path import get_user_persistence_dir from openhands.sdk.utils.pydantic_secrets import REDACTED_SECRET_VALUE @@ -150,8 +151,11 @@ def __init__( *, allow_insecure_artifact_urls: bool = False, max_artifact_bytes: int = _MAX_ARTIFACT_BYTES, - secrets_store: SecretsStore | None = None, + secrets_store: Callable[[], SecretsStore] | None = None, ) -> None: + """``secrets_store`` returns the store a backend's ``secret_refs`` are + read from. It is called at each start, so a manager that outlives a + server (re)initialisation reads the store of the current one.""" self.installed_dir = installed_dir or get_installed_canvas_extensions_dir() root = ( state_dir or get_user_persistence_dir() / "canvas-extensions" / "backends" @@ -807,7 +811,9 @@ async def start(self, name: str, revision: str) -> BackendStatus: await self._terminate_runtime(runtime) secret_environment: dict[str, str] = {} if backend.secret_refs: - store = self.secrets_store or get_secrets_store() + if self.secrets_store is None: + raise ValueError("Backend secret store is not available") + store = self.secrets_store() for secret_name in backend.secret_refs: try: value = await asyncio.to_thread( diff --git a/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py b/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py index 2b8d658493b..a5d9c0de7b0 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py @@ -23,6 +23,7 @@ _append_partitioned_to_last_set_cookie, ) from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_config from openhands.agent_server.docker_runtime.proxy import ( bridge_websocket, proxy_http, @@ -280,9 +281,7 @@ def _request_origin(request: Request | WebSocket) -> str | None: proxy terminates TLS in front of this server. Otherwise the request's own scheme and ``Host`` decide — the same inputs a spoofing client cannot fake. """ - config: Config | None = getattr(request.app.state, "config", None) - trust_forwarded = bool(config and config.trust_forwarded_headers) - if trust_forwarded: + if get_config(request).trust_forwarded_headers: forwarded_proto = request.headers.get("x-forwarded-proto", "") scheme = forwarded_proto.split(",")[0].strip() or request.url.scheme forwarded_host = request.headers.get("x-forwarded-host", "") @@ -305,8 +304,7 @@ def _require_ingress_host(request: Request | WebSocket, public_url: str) -> None def _require_bootstrap_origin(request: Request, public_url: str) -> None: _require_ingress_host(request, public_url) origin = request.headers.get("origin", "") - config: Config = request.app.state.config - if not _allowed_control_origin(config, origin): + if not _allowed_control_origin(get_config(request), origin): raise HTTPException(status.HTTP_403_FORBIDDEN, "Origin is not allowed") if _origin(origin) == _origin(public_url): raise HTTPException( @@ -417,7 +415,7 @@ def _set_session_cookie( async def create_app_backend_session( request: Request, response: Response, extension_name: str ) -> AppBackendSessionResponse: - public_url = _public_url(request.app.state.config) + public_url = _public_url(get_config(request)) _require_bootstrap_origin(request, public_url) endpoint = _ready_endpoint(request, extension_name) token, session = await get_app_backend_session_store(request).create( @@ -439,7 +437,7 @@ async def create_app_backend_session( async def delete_app_backend_session( request: Request, response: Response, extension_name: str ) -> None: - public_url = _public_url(request.app.state.config) + public_url = _public_url(get_config(request)) _require_bootstrap_origin(request, public_url) token = request.cookies.get(APP_BACKEND_SESSION_COOKIE_NAME) await get_app_backend_session_store(request).revoke_token(token) @@ -450,7 +448,7 @@ async def proxy_app_backend_http( request: Request, extension_name: str, path: str ) -> Response: _validate_proxy_path(path) - public_url = _public_url(request.app.state.config) + public_url = _public_url(get_config(request)) _require_ingress_host(request, public_url) if request.method not in _SAFE_METHODS: _require_app_origin(request.headers.get("origin"), public_url) @@ -489,7 +487,7 @@ async def proxy_app_backend_websocket( ) -> None: try: _validate_proxy_path(path) - public_url = _public_url(websocket.app.state.config) + public_url = _public_url(get_config(websocket)) _require_ingress_host(websocket, public_url) _require_app_origin(websocket.headers.get("origin"), public_url) endpoint = _ready_endpoint(websocket, extension_name) diff --git a/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py b/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py index ed4068ee5c8..7c6152276d0 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py @@ -38,6 +38,7 @@ MANIFEST_FILENAME, CanvasExtensionManifest, ) +from openhands.agent_server.server_context import server_context_of from openhands.sdk.extensions.fetch import ExtensionFetchError from openhands.sdk.utils.redact import redact_url_credentials @@ -63,7 +64,10 @@ def _backend_manager(request: Request) -> CanvasExtensionBackendManager: manager = getattr(request.app.state, "canvas_extension_backend_manager", None) if manager is None: - manager = CanvasExtensionBackendManager() + app = request.app + manager = CanvasExtensionBackendManager( + secrets_store=lambda: server_context_of(app).stores.secrets + ) request.app.state.canvas_extension_backend_manager = manager return manager diff --git a/openhands-agent-server/openhands/agent_server/config.py b/openhands-agent-server/openhands/agent_server/config.py index 31c060f260d..bdf4522fa64 100644 --- a/openhands-agent-server/openhands/agent_server/config.py +++ b/openhands-agent-server/openhands/agent_server/config.py @@ -590,9 +590,6 @@ def cipher(self) -> Cipher | None: return cipher -_default_config: Config | None = None - - def _read_config_file(path: Path) -> dict[str, Any]: if not path.exists(): return {} @@ -641,12 +638,3 @@ def load_config(config_path: Path | None = None) -> Config: if not data: return Config() return Config.model_validate(data) - - -def get_default_config() -> Config: - """Get the default local server config shared across the server""" - global _default_config - if _default_config is None: - _default_config = load_config() - assert _default_config is not None - return _default_config diff --git a/openhands-agent-server/openhands/agent_server/conversation_registry.py b/openhands-agent-server/openhands/agent_server/conversation_registry.py index 1c05ce8d872..546ccb6c25e 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_registry.py +++ b/openhands-agent-server/openhands/agent_server/conversation_registry.py @@ -12,6 +12,7 @@ ConversationRuntimeInfo, ConversationRuntimeStatus, ) +from openhands.agent_server.persistence import ServerStores if TYPE_CHECKING: @@ -21,8 +22,9 @@ class ConversationRegistry: """Route and lifecycle adapter for host-local conversations.""" - def __init__(self, config: Config) -> None: + def __init__(self, config: Config, stores: ServerStores) -> None: self.config = config + self.stores = stores def configure_service(self, service: ConversationService) -> None: """Connect runtime-specific persistence to the shared catalog.""" @@ -80,17 +82,19 @@ def sockets_router(self) -> APIRouter: return router -def create_conversation_registry(config: Config) -> ConversationRegistry: +def create_conversation_registry( + config: Config, stores: ServerStores +) -> ConversationRegistry: if config.conversation_runtime_selectable: from openhands.agent_server.docker_runtime.hybrid import ( HybridConversationRegistry, ) - return HybridConversationRegistry(config) + return HybridConversationRegistry(config, stores) if config.conversation_runtime == "docker": from openhands.agent_server.docker_runtime.registry import ( DockerConversationRegistry, ) - return DockerConversationRegistry(config) - return ConversationRegistry(config) + return DockerConversationRegistry(config, stores) + return ConversationRegistry(config, stores) diff --git a/openhands-agent-server/openhands/agent_server/conversation_router.py b/openhands-agent-server/openhands/agent_server/conversation_router.py index dc1cfeb2bab..3496051cf91 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_router.py +++ b/openhands-agent-server/openhands/agent_server/conversation_router.py @@ -19,16 +19,19 @@ from openhands.agent_server._secrets_exposure import ( decrypt_incoming_llm_secrets, - get_cipher, store_errors, ) -from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ( ConversationService, InvalidParentConversation, UnavailableAgentTool, ) -from openhands.agent_server.dependencies import get_conversation_service +from openhands.agent_server.dependencies import ( + get_cipher, + get_conversation_registry, + get_conversation_service, + get_stores, +) from openhands.agent_server.event_service import CondensationUnavailable from openhands.agent_server.launch import launch_http_exception from openhands.agent_server.models import ( @@ -39,7 +42,6 @@ ConversationInfo, ConversationPage, ConversationRuntimeInfo, - ConversationRuntimeStatus, ConversationSortOrder, ForkConversationRequest, NavigateConversationRequest, @@ -54,7 +56,6 @@ UpdateSecretsRequest, trim_conversation_response_skills, ) -from openhands.agent_server.persistence import get_llm_profile_store from openhands.sdk import LLM from openhands.sdk.conversation.state import ConversationExecutionStatus from openhands.sdk.conversation.types import ( @@ -149,9 +150,7 @@ def _request_with_observability_headers( def _with_runtime_info( request: Request, conversation: ConversationInfo ) -> ConversationInfo: - registry = getattr(request.app.state, "conversation_registry", None) - if not isinstance(registry, ConversationRegistry): - return conversation + registry = get_conversation_registry(request) return conversation.model_copy( update={"runtime_info": registry.runtime_info(conversation.id)} ) @@ -243,12 +242,7 @@ async def get_local_conversation_runtime( """Inspect the always-available in-process runtime.""" if await conversation_service.get_conversation(conversation_id) is None: raise HTTPException(status.HTTP_404_NOT_FOUND) - registry = getattr(request.app.state, "conversation_registry", None) - if isinstance(registry, ConversationRegistry): - return registry.runtime_info(conversation_id) - return ConversationRuntimeInfo( - runtime_status=ConversationRuntimeStatus.AVAILABLE, can_resume=True - ) + return get_conversation_registry(request).runtime_info(conversation_id) @conversation_router.post("/{conversation_id}/runtime/reprovision") @@ -656,7 +650,9 @@ async def switch_conversation_llm( # is replaced. Inline-key configs without a provider_connection_id are # byte-identical to the old path. with store_errors(): - llm = get_llm_profile_store().resolve_provider_connection(llm, cipher=cipher) + llm = get_stores(request).llm_profiles.resolve_provider_connection( + llm, cipher=cipher + ) try: conversation.switch_llm(llm) except ValueError as e: diff --git a/openhands-agent-server/openhands/agent_server/conversation_service.py b/openhands-agent-server/openhands/agent_server/conversation_service.py index 27143b53c2f..09260424382 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_service.py +++ b/openhands-agent-server/openhands/agent_server/conversation_service.py @@ -36,6 +36,9 @@ conversation_worktree_dir, remove_conversation_worktree, ) +from openhands.agent_server.mcp_oauth_store import ( + create_settings_backed_mcp_tool_provider, +) from openhands.agent_server.models import ( ConversationInfo, ConversationPage, @@ -47,7 +50,7 @@ StoredConversation, UpdateConversationRequest, ) -from openhands.agent_server.persistence import FileSecretsStore +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.agent_server.pub_sub import Subscriber from openhands.agent_server.server_details_router import update_last_execution_time from openhands.agent_server.telemetry import ( @@ -83,8 +86,9 @@ from openhands.sdk.event.conversation_state import ConversationStateUpdateEvent from openhands.sdk.git.exceptions import GitCommandError, GitRepositoryError from openhands.sdk.git.utils import run_git_command, validate_git_repository -from openhands.sdk.launch import LaunchedAgent, finalize +from openhands.sdk.launch import LaunchedAgent, LaunchStoreError, LaunchStores, finalize from openhands.sdk.llm.call_context import LLMCallContext +from openhands.sdk.llm.llm_profile_store import LLMProfileStore from openhands.sdk.mcp.utils import MCPToolProvider from openhands.sdk.observability import OPERATION_METADATA_KEY, observe from openhands.sdk.tool.client_tool import register_client_tools @@ -560,7 +564,10 @@ class ConversationService: cipher: Cipher | None = None runtime_cipher_resolver: Callable[[UUID], Cipher] | None = None mcp_tool_provider: MCPToolProvider | None = None - secrets_store: FileSecretsStore | None = None + # The persistent stores of the server this service belongs to. ``None`` + # is a service without any: it starts conversations with default settings + # and cannot launch from a profile. + stores: ServerStores | None = None owner_instance_id: str = field(default_factory=lambda: uuid4().hex) max_concurrent_runs: int = 10 lease_ttl_seconds: float = DEFAULT_LEASE_TTL_SECONDS @@ -773,14 +780,37 @@ def _is_codex_agent(agent: AgentBase | None) -> bool: return isinstance(agent, ACPAgent) and agent.acp_server == "codex" async def _has_local_codex_credential(self) -> bool: - if self.secrets_store is None: + if self.stores is None: return False value = await asyncio.to_thread( - self.secrets_store.get_secret, + self.stores.secrets.get_secret, CODEX_AUTH_SECRET_NAME, ) return value is not None + async def _load_settings(self) -> PersistedSettings: + """The stored agent preferences, or the defaults when there are none.""" + stores = self.stores + if stores is None: + return PersistedSettings() + # FileSettingsStore.load re-raises PermissionError/OSError by design; + # every launch reads it, so a bad file mode must not take down request + # shapes that need nothing from settings. + try: + settings = await asyncio.to_thread(stores.settings.load) + except (PermissionError, OSError): + logger.warning( + "Cannot read settings; starting without the stored agent preferences", + exc_info=True, + ) + return PersistedSettings() + return settings or PersistedSettings() + + def _launch_stores(self, settings: PersistedSettings) -> LaunchStores: + if self.stores is None: + raise LaunchStoreError("This server has no profile stores") + return server_launch_stores(settings, self.stores, self.cipher) + async def _resolve_credential_bindings( self, stored: StoredConversation, @@ -809,13 +839,13 @@ async def _resolve_credential_bindings( and self._is_codex_agent(agent) and await self._has_local_codex_credential() ): - assert self.secrets_store is not None + assert self.stores is not None from openhands.agent_server.credential_binding import ( LocalVersionedCredentialBinding, ) bindings[CODEX_AUTH_SECRET_NAME] = LocalVersionedCredentialBinding( - self.secrets_store, + self.stores.secrets, CODEX_AUTH_SECRET_NAME, ) return bindings @@ -1545,26 +1575,7 @@ async def _create_conversation( else None ) - from openhands.agent_server.persistence import ( - PersistedSettings, - get_settings_store, - ) - - # get_settings_store() is safe here: get_instance() initialises the - # singleton with the server cipher before any conversation can start. - # FileSettingsStore.load re-raises PermissionError/OSError by design; - # now that every launch reads it, a bad file mode must not take down - # request shapes that need nothing from settings. - try: - settings = await asyncio.to_thread( - lambda: get_settings_store().load() or PersistedSettings() - ) - except (PermissionError, OSError): - logger.warning( - "Cannot read settings; starting without the stored agent preferences", - exc_info=True, - ) - settings = PersistedSettings() + settings = await self._load_settings() # ``ACPAgentSettings.agent_context`` is nullable, hence the guard. stored_context = settings.agent_settings.agent_context load_memory = bool(stored_context and stored_context.load_memory) @@ -1572,7 +1583,7 @@ async def _create_conversation( source = await asyncio.to_thread( launch_source, request, - lambda: server_launch_stores(settings, self.cipher), + lambda: self._launch_stores(settings), self.cipher, ) secrets = scoped_secrets(request.secrets, source) @@ -2445,18 +2456,9 @@ async def __aexit__(self, exc_type, exc_value, traceback): raise credential_failure or failures[0] @classmethod - def get_instance(cls, config: Config) -> "ConversationService": - # Initialise the settings-store singleton with the server cipher before - # any conversation handler can call get_settings_store() without config. - from openhands.agent_server.mcp_oauth_store import ( - create_settings_backed_mcp_tool_provider, - ) - from openhands.agent_server.persistence import ( - get_secrets_store, - get_settings_store, - ) - - get_settings_store(config) + def get_instance( + cls, config: Config, stores: ServerStores + ) -> "ConversationService": return ConversationService( conversations_dir=config.conversations_path, webhook_specs=config.webhooks, @@ -2464,8 +2466,10 @@ def get_instance(cls, config: Config) -> "ConversationService": config.session_api_keys[0] if config.session_api_keys else None ), cipher=config.cipher, - mcp_tool_provider=create_settings_backed_mcp_tool_provider(config), - secrets_store=get_secrets_store(config), + mcp_tool_provider=create_settings_backed_mcp_tool_provider( + config, stores.settings + ), + stores=stores, max_concurrent_runs=config.max_concurrent_runs, lease_ttl_seconds=config.lease_ttl_seconds, conversation_idle_ttl_seconds=config.conversation_idle_ttl_seconds, @@ -2521,7 +2525,12 @@ async def _start_event_service( ) if stored.autotitle and stored.title is None: await event_service.subscribe_to_events( - AutoTitleSubscriber(service=event_service) + AutoTitleSubscriber( + service=event_service, + llm_profile_store=( + self.stores.llm_profiles if self.stores else None + ), + ) ) await self._maybe_subscribe_telemetry( event_service, stored, is_new_conversation=is_new_conversation @@ -2716,6 +2725,9 @@ def _generate_title_traced( @dataclass class AutoTitleSubscriber(Subscriber): service: EventService + # Where ``title_llm_profile`` is looked up; ``None`` leaves titling to the + # agent's own LLM. + llm_profile_store: LLMProfileStore | None = None async def __call__(self, event: Event) -> None: # Only act on incoming user messages @@ -2782,16 +2794,11 @@ def _load_title_llm(self) -> LLM | None: falls back to the agent's LLM (and then to message truncation). """ profile_name = self.service.stored.title_llm_profile - if not profile_name: + if not profile_name or self.llm_profile_store is None: return None try: - from openhands.agent_server.persistence.store import ( - get_llm_profile_store, - ) - - profile_store = get_llm_profile_store() - return profile_store.load(profile_name, cipher=self.service.cipher) + return self.llm_profile_store.load(profile_name, cipher=self.service.cipher) except (FileNotFoundError, ValueError) as e: logger.warning( f"Failed to load title LLM profile '{profile_name}': {e}. " @@ -3086,20 +3093,3 @@ async def post_conversation_info(self, conversation_info: BaseModel): f"{conversations_url} after {self.spec.num_retries + 1} " f"attempts. Response: {response_content}" ) - - -_conversation_service: ConversationService | None = None - - -def get_default_conversation_service() -> ConversationService: - global _conversation_service - if _conversation_service: - return _conversation_service - - from openhands.agent_server.config import ( - get_default_config, - ) - - config = get_default_config() - _conversation_service = ConversationService.get_instance(config) - return _conversation_service diff --git a/openhands-agent-server/openhands/agent_server/credential_binding.py b/openhands-agent-server/openhands/agent_server/credential_binding.py index 3674e52b3eb..6e2187ed868 100644 --- a/openhands-agent-server/openhands/agent_server/credential_binding.py +++ b/openhands-agent-server/openhands/agent_server/credential_binding.py @@ -8,6 +8,7 @@ from fastapi import APIRouter, HTTPException, Request, Response, status from pydantic import BaseModel, ConfigDict, Field, field_validator +from openhands.agent_server.dependencies import get_conversation_service from openhands.agent_server.event_service import CredentialBindingActivationTooLate from openhands.agent_server.persistence import FileSecretsStore from openhands.sdk.agent.acp_file_credentials import supports_file_credential_binding @@ -106,7 +107,7 @@ async def _probe_binding(binding: HttpVersionedCredentialBinding) -> None: @router.post("/prepare-for-sandbox-pause", include_in_schema=False) async def prepare_for_sandbox_pause(request: Request) -> Response: - await request.app.state.conversation_service.prepare_for_sandbox_pause() + await get_conversation_service(request).prepare_for_sandbox_pause() return Response(status_code=status.HTTP_204_NO_CONTENT) @@ -128,7 +129,7 @@ async def activate_credential_binding( ) try: await _probe_binding(binding) - await request.app.state.conversation_service.activate_credential_binding( + await get_conversation_service(request).activate_credential_binding( conversation_id, secret_name, binding, diff --git a/openhands-agent-server/openhands/agent_server/dependencies.py b/openhands-agent-server/openhands/agent_server/dependencies.py index d2a69c2474d..46020a4840c 100644 --- a/openhands-agent-server/openhands/agent_server/dependencies.py +++ b/openhands-agent-server/openhands/agent_server/dependencies.py @@ -1,15 +1,19 @@ from uuid import UUID -from fastapi import Depends, HTTPException, Request, status +from fastapi import Depends, HTTPException, Request, WebSocketException, status from fastapi.exceptions import RequestValidationError from fastapi.security import APIKeyCookie, APIKeyHeader from pydantic import TypeAdapter, ValidationError +from starlette.requests import HTTPConnection from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.config import Config from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.event_service import EventService +from openhands.agent_server.persistence import ServerStores +from openhands.agent_server.server_context import get_server_context +from openhands.sdk.utils.cipher import Cipher # Cookie name used to authenticate the workspace static-file routes. @@ -24,17 +28,43 @@ ) +def get_config(connection: HTTPConnection) -> Config: + """The config this server currently runs with. + + Read from the server context at request time, so the config delivered via + ``POST /api/init`` takes effect immediately without restarting the server + or re-registering routes. + """ + return get_server_context(connection).config + + +def get_cipher(connection: HTTPConnection) -> Cipher | None: + """The configured cipher (``None`` when ``OH_SECRET_KEY`` is unset).""" + return get_server_context(connection).config.cipher + + +def get_stores(connection: HTTPConnection) -> ServerStores: + """The persistent stores built for this server's current config.""" + return get_server_context(connection).stores + + +def get_conversation_registry(connection: HTTPConnection) -> ConversationRegistry: + return get_server_context(connection).conversation_registry + + +def _unavailable(connection: HTTPConnection, detail: str) -> Exception: + """A started-service lookup that found none: 503, or 1013 on a WebSocket.""" + if connection.scope["type"] == "websocket": + return WebSocketException(code=status.WS_1013_TRY_AGAIN_LATER, reason=detail) + return HTTPException(status.HTTP_503_SERVICE_UNAVAILABLE, detail) + + def check_session_api_key( request: Request, session_api_key: str | None = Depends(_SESSION_API_KEY_HEADER), ) -> None: - """Reject the request if the supplied key is not in the current session keys. - - Reads ``session_api_keys`` from ``request.app.state.config`` at request time - so that keys delivered via ``POST /api/init`` take effect immediately without - restarting the server or re-registering routes. - """ - config: Config = request.app.state.config + """Reject the request if the supplied key is not in the current session keys.""" + config = get_config(request) if config.session_api_keys and session_api_key not in config.session_api_keys: raise HTTPException(status.HTTP_401_UNAUTHORIZED) @@ -54,7 +84,7 @@ def check_workspace_session( frontend embeds workspace artifacts. The cookie is deliberately scoped to this router only; no other endpoint honors it. """ - config: Config = request.app.state.config + config = get_config(request) if not config.session_api_keys: return for candidate in (header_key, cookie_key): @@ -63,13 +93,18 @@ def check_workspace_session( raise HTTPException(status.HTTP_401_UNAUTHORIZED) -def get_conversation_service(request: Request) -> ConversationService: - service = getattr(request.app.state, "conversation_service", None) +def get_conversation_service(connection: HTTPConnection) -> ConversationService: + service = get_server_context(connection).conversation_service if service is None: - raise HTTPException( - status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail="Conversation service is not available", - ) + raise _unavailable(connection, "Conversation service is not available") + return service + + +def get_server_bash_event_service(connection: HTTPConnection) -> BashEventService: + """The server-wide bash service (not a conversation runtime's own).""" + service = get_server_context(connection).bash_event_service + if service is None: + raise _unavailable(connection, "Bash event service is not available") return service @@ -103,13 +138,7 @@ async def get_bash_event_service(request: Request) -> BashEventService: cipher=event_service.cipher, ) return event_service.bash_event_service - service = getattr(request.app.state, "bash_event_service", None) - if service is None: - raise HTTPException( - status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail="Bash event service is not available", - ) - return service + return get_server_bash_event_service(request) async def get_event_service( @@ -117,10 +146,8 @@ async def get_event_service( request: Request, conversation_service: ConversationService = Depends(get_conversation_service), ) -> EventService: - registry = getattr(request.app.state, "conversation_registry", None) - if isinstance( - registry, ConversationRegistry - ) and registry.serves_persisted_event_reads(conversation_id): + registry = get_conversation_registry(request) + if registry.serves_persisted_event_reads(conversation_id): event_service = await conversation_service.get_persisted_event_service( conversation_id ) diff --git a/openhands-agent-server/openhands/agent_server/docker_runtime/hybrid.py b/openhands-agent-server/openhands/agent_server/docker_runtime/hybrid.py index 1c2b0b1158e..a40db0edbed 100644 --- a/openhands-agent-server/openhands/agent_server/docker_runtime/hybrid.py +++ b/openhands-agent-server/openhands/agent_server/docker_runtime/hybrid.py @@ -17,6 +17,7 @@ from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry from openhands.agent_server.models import ConversationRuntimeInfo +from openhands.agent_server.server_context import server_context_of DOCKER_ROUTE_PREFIX: Final[str] = "/_docker" @@ -93,7 +94,11 @@ def __init__(self, app: ASGIApp) -> None: self.app = app async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: - registry = scope["app"].state.conversation_registry if "app" in scope else None + registry = ( + server_context_of(scope["app"]).conversation_registry + if "app" in scope + else None + ) if scope["type"] not in ("http", "websocket") or not isinstance( registry, HybridConversationRegistry ): diff --git a/openhands-agent-server/openhands/agent_server/docker_runtime/registry.py b/openhands-agent-server/openhands/agent_server/docker_runtime/registry.py index 912b62040d9..f4515dbe83b 100644 --- a/openhands-agent-server/openhands/agent_server/docker_runtime/registry.py +++ b/openhands-agent-server/openhands/agent_server/docker_runtime/registry.py @@ -26,7 +26,7 @@ ConversationRuntimeInfo, ConversationRuntimeStatus, ) -from openhands.agent_server.persistence.store import _get_persistence_dir +from openhands.agent_server.persistence import ServerStores from openhands.agent_server.storage import Reclaimer from openhands.sdk.conversation.request import ConversationContainerOptions from openhands.sdk.logger import get_logger @@ -123,11 +123,10 @@ async def __aexit__( class DockerConversationRegistry(ConversationRegistry): - def __init__(self, config: Config) -> None: - super().__init__(config) + def __init__(self, config: Config, stores: ServerStores) -> None: + super().__init__(config, stores) paths = ( - f"{config.conversations_path.resolve()}\0" - f"{_get_persistence_dir(config).resolve()}" + f"{config.conversations_path.resolve()}\0{stores.workspace_dir.resolve()}" ) self.owner = hashlib.sha256(paths.encode()).hexdigest()[:24] self.provisioning = RuntimeProvisioningStore(config) diff --git a/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py b/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py index 1bc30c29358..8cfe5268f63 100644 --- a/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py +++ b/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py @@ -14,7 +14,10 @@ from fastapi import APIRouter, HTTPException, Query, Request, WebSocket from starlette.responses import JSONResponse, Response, StreamingResponse -from openhands.agent_server.dependencies import get_conversation_service +from openhands.agent_server.dependencies import ( + get_conversation_registry, + get_conversation_service, +) from openhands.agent_server.docker_runtime.mediation import materialize_secrets from openhands.agent_server.docker_runtime.proxy import ( bridge_websocket, @@ -39,7 +42,7 @@ StartConversationRequest, UpdateSecretsRequest, ) -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import PersistedSettings from openhands.agent_server.utils import safe_rmtree from openhands.sdk.conversation.request import ConversationContainerOptions from openhands.sdk.git.exceptions import GitCommandError, GitRepositoryError @@ -56,7 +59,7 @@ def get_registry(request: Request) -> DockerConversationRegistry: - registry = request.app.state.conversation_registry + registry = get_conversation_registry(request) if not isinstance(registry, DockerConversationRegistry): raise HTTPException(503, "Docker conversation runtime is unavailable") return registry @@ -289,7 +292,7 @@ async def _prepare_forward( context={"cipher": config.cipher} if body.get("secrets_encrypted") else None, ) try: - settings = await asyncio.to_thread(get_settings_store(config).load) + settings = await asyncio.to_thread(registry.stores.settings.load) except (OSError, PermissionError): settings = None settings = settings or PersistedSettings() @@ -302,7 +305,7 @@ async def _prepare_forward( else await asyncio.to_thread( launch_source, start, - lambda: server_launch_stores(settings, config.cipher), + lambda: server_launch_stores(settings, registry.stores, config.cipher), config.cipher, ) ) @@ -507,7 +510,7 @@ async def _proxy_socket( if not await _accept_authenticated_websocket(websocket, session_api_key): return - registry = websocket.app.state.conversation_registry + registry = get_conversation_registry(websocket) if not isinstance(registry, DockerConversationRegistry): await websocket.close(code=1011) return @@ -532,7 +535,7 @@ async def _proxy_socket( if release is not None: release() registry.detach_session(conversation_id) - await websocket.app.state.conversation_service.refresh_persisted_conversation( + await get_conversation_service(websocket).refresh_persisted_conversation( conversation_id ) diff --git a/openhands-agent-server/openhands/agent_server/file_router.py b/openhands-agent-server/openhands/agent_server/file_router.py index bf98bd94817..abd28f10259 100644 --- a/openhands-agent-server/openhands/agent_server/file_router.py +++ b/openhands-agent-server/openhands/agent_server/file_router.py @@ -31,7 +31,7 @@ redacted_json_bytes, should_redact, ) -from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_config from openhands.agent_server.models import FileUploadResult, Success from openhands.agent_server.server_details_router import update_last_execution_time from openhands.sdk.git.exceptions import GitCommandError, GitRepositoryError @@ -944,7 +944,7 @@ async def download_trajectory( request: Request, ) -> FileResponse: """Download a zip archive of a conversation trajectory.""" - config: Config = request.app.state.config + config = get_config(request) temp_file = config.conversations_path / f"{conversation_id.hex}.zip" conversation_dir = config.conversations_path / conversation_id.hex diff --git a/openhands-agent-server/openhands/agent_server/git_provider_service.py b/openhands-agent-server/openhands/agent_server/git_provider_service.py index 5a3395d1f6e..60f52d7c1ea 100644 --- a/openhands-agent-server/openhands/agent_server/git_provider_service.py +++ b/openhands-agent-server/openhands/agent_server/git_provider_service.py @@ -6,8 +6,7 @@ import httpx -from openhands.agent_server.config import Config -from openhands.agent_server.persistence import get_secrets_store +from openhands.agent_server.persistence import SecretsStore from openhands.sdk.git.models import GitProviderRepository, GitProviderRepositoryPage from openhands.sdk.logger import get_logger from openhands.sdk.workspace.repo import PROVIDER_TOKEN_NAMES, GitProvider @@ -52,7 +51,7 @@ def __init__(self, message: str, *, status_code: int = 502) -> None: async def search_provider_repositories( - config: Config, + secrets_store: SecretsStore, provider: GitProvider, *, query: str | None = None, @@ -65,7 +64,7 @@ async def search_provider_repositories( f"Repository discovery is not supported for provider '{provider.value}'" ) - token = _resolve_provider_token(config, provider) + token = _resolve_provider_token(secrets_store, provider) if token is None: return GitProviderRepositoryPage( items=[], next_page_id=None, missing_token=True @@ -79,10 +78,11 @@ async def search_provider_repositories( ) -def _resolve_provider_token(config: Config, provider: GitProvider) -> str | None: - store = get_secrets_store(config) +def _resolve_provider_token( + secrets_store: SecretsStore, provider: GitProvider +) -> str | None: for name in _PROVIDER_TOKEN_CANDIDATES.get(provider, ()): # pragma: no branch - if value := store.get_secret(name): + if value := secrets_store.get_secret(name): return value return None diff --git a/openhands-agent-server/openhands/agent_server/git_router.py b/openhands-agent-server/openhands/agent_server/git_router.py index bd31b39d5d3..0e301e58777 100644 --- a/openhands-agent-server/openhands/agent_server/git_router.py +++ b/openhands-agent-server/openhands/agent_server/git_router.py @@ -8,7 +8,7 @@ from fastapi import APIRouter, HTTPException, Path as PathParam, Query, Request -from openhands.agent_server._secrets_exposure import get_config +from openhands.agent_server.dependencies import get_stores from openhands.agent_server.git_provider_service import ( GitProviderAPIError, UnsupportedGitProviderError, @@ -150,7 +150,7 @@ async def git_repositories_search( """List repositories accessible to a configured git provider token.""" try: return await search_provider_repositories( - get_config(request), + get_stores(request).secrets, provider, query=query, limit=limit, diff --git a/openhands-agent-server/openhands/agent_server/init_router.py b/openhands-agent-server/openhands/agent_server/init_router.py index e8636233e1b..4241ea55d4d 100644 --- a/openhands-agent-server/openhands/agent_server/init_router.py +++ b/openhands-agent-server/openhands/agent_server/init_router.py @@ -14,6 +14,7 @@ import asyncio import os +from contextlib import AsyncExitStack from pathlib import Path from typing import Any, ClassVar, Literal @@ -39,11 +40,15 @@ export_session_api_key, ) from openhands.agent_server.conversation_registry import ( - ConversationRegistry, create_conversation_registry, ) from openhands.agent_server.conversation_service import ConversationService -from openhands.agent_server.persistence import reencrypt_stale_stores, reset_stores +from openhands.agent_server.persistence import ServerStores, reencrypt_stale_stores +from openhands.agent_server.server_context import ( + ServerContext, + get_server_context, + install_server_context, +) from openhands.agent_server.server_details_router import mark_initialization_complete from openhands.agent_server.telemetry import ( build_telemetry_sink, @@ -224,6 +229,11 @@ class InitService: A single ``asyncio.Lock`` makes concurrent /api/init posts safe; the second one sees ``state != "dormant"`` and gets a 400. On failure mid-init the state rolls back to ``dormant`` so the orchestrator can retry. + + The stores and services of the delivered config are built and started + beside the dormant :class:`ServerContext` and replace it in one assignment + once all of them are up. A failed attempt stops what it started and leaves + the dormant context in place. """ def __init__(self, app: FastAPI, base_config: Config) -> None: @@ -232,9 +242,8 @@ def __init__(self, app: FastAPI, base_config: Config) -> None: self._state: InitState = "dormant" self._error: str | None = None self._lock = asyncio.Lock() - self._entered_service: ConversationService | None = None - self._entered_conversation_registry: ConversationRegistry | None = None - self._entered_bash_service: BashEventService | None = None + # The services ``initialize`` started, to be stopped by ``teardown``. + self._started: AsyncExitStack | None = None # ``initialize`` exports the delivered session key to the environment; # ``(None, None)`` means nothing to put back. self._session_api_key_env_before_init: tuple[bool, str | None] = ( @@ -285,55 +294,65 @@ async def initialize(self, req: InitRequest) -> InitStatus: export_session_api_key(new_config) maybe_init_laminar() + # Built after ``req.env`` is applied, so the stores resolve their + # directories (``OH_PERSISTENCE_DIR``) from the delivered + # environment and encrypt with the delivered key. + stores = ServerStores.from_config(new_config) + # Must precede get_instance(), which captures the sink. The # matching emit_server_started() is deferred until the ``ready`` # transition below: emitting here would produce a server_started # for an init that later fails and rolls back to dormant, leaving # an unpaired start and letting a retry emit a second one. await shutdown_telemetry_sink() - reset_stores() - await asyncio.to_thread(reencrypt_stale_stores, new_config) - self._app.state.telemetry_sink = await build_telemetry_sink(new_config) - - # Reset the module-level singleton so other call sites that go - # through ``get_default_conversation_service`` see the new - # instance built from the merged config. - from openhands.agent_server import conversation_service as cs_mod - - service = ConversationService.get_instance(new_config) - cs_mod._conversation_service = service - conversation_registry = create_conversation_registry(new_config) - conversation_registry.configure_service(service) + await asyncio.to_thread(reencrypt_stale_stores, stores) + self._app.state.telemetry_sink = await build_telemetry_sink( + new_config, stores.settings + ) + service = ConversationService.get_instance(new_config, stores) + conversation_registry = create_conversation_registry(new_config, stores) + conversation_registry.configure_service(service) bash_svc = BashEventService( bash_events_dir=new_config.bash_events_dir, cipher=new_config.cipher ) - await bash_svc.__aenter__() - self._entered_bash_service = bash_svc - - await service.__aenter__() - self._entered_service = service - await conversation_registry.start() - self._entered_conversation_registry = conversation_registry - self._app.state.config = new_config - self._app.state.conversation_service = service - self._app.state.conversation_registry = conversation_registry - self._app.state.bash_event_service = bash_svc + + # Unwinds in reverse on a failure below, so a failed attempt keeps + # no half-started service; on success ``teardown`` unwinds it. + async with AsyncExitStack() as started: + await started.enter_async_context(bash_svc) + await started.enter_async_context(service) + started.push_async_callback(conversation_registry.shutdown) + await conversation_registry.start() + + # VSCode's token is the first session key, as on a normal boot. + # A dormant server that booted without one gave VSCode a random + # token, so switch it to the key delivered here. + vscode_service = get_vscode_service(new_config) + if vscode_service is not None and new_config.session_api_keys: + await vscode_service.set_connection_token( + new_config.session_api_keys[0] + ) + + # Nothing below can fail: from this single assignment on, every + # request sees the delivered config with its own stores and + # services, and none of the dormant ones. + install_server_context( + self._app, + ServerContext( + config=new_config, + stores=stores, + conversation_registry=conversation_registry, + conversation_service=service, + bash_event_service=bash_svc, + ), + ) + self._started = started.pop_all() # Re-derive root_path from the merged config so Doc URLS are valid from openhands.agent_server.api import _get_root_path - new_root_path = _get_root_path(new_config) - self._app.root_path = new_root_path - - # VSCode's token is the first session key, as on a normal boot. A - # dormant server that booted without one gave VSCode a random - # token, so switch it to the key delivered here. - vscode_service = get_vscode_service() - if vscode_service is not None and new_config.session_api_keys: - await vscode_service.set_connection_token( - new_config.session_api_keys[0] - ) + self._app.root_path = _get_root_path(new_config) mark_initialization_complete() self._state = "ready" @@ -358,15 +377,10 @@ async def teardown(self) -> None: Called from the FastAPI lifespan's finally clause so dormant pods that were never initialized don't need any cleanup. """ - if self._entered_service is not None: - if self._entered_conversation_registry is not None: - await self._entered_conversation_registry.shutdown() - self._entered_conversation_registry = None - await self._entered_service.__aexit__(None, None, None) - self._entered_service = None - if self._entered_bash_service is not None: - await self._entered_bash_service.__aexit__(None, None, None) - self._entered_bash_service = None + started, self._started = self._started, None + if started is not None: + # Registry, then the conversation service, then the bash service. + await started.aclose() self._restore_session_api_key_env() def _restore_session_api_key_env(self) -> None: @@ -402,9 +416,9 @@ def check_init_api_key( bootstrap credential — the orchestrator already holds it because it is required for encryption. The key is replaced when /api/init delivers the per-user runtime config.""" - config: Config | None = getattr(request.app.state, "config", None) - if config is None or config.secret_key is None: - if config is not None and config.secret_key_was_blank: + config = get_server_context(request).config + if config.secret_key is None: + if config.secret_key_was_blank: # A blank key was configured but no usable key remains → fail # closed, since no client can present a blank key. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) diff --git a/openhands-agent-server/openhands/agent_server/launch.py b/openhands-agent-server/openhands/agent_server/launch.py index 1e54c8794e1..e6a35ef787b 100644 --- a/openhands-agent-server/openhands/agent_server/launch.py +++ b/openhands-agent-server/openhands/agent_server/launch.py @@ -20,11 +20,7 @@ Config, ) from openhands.agent_server.docker_runtime.mediation import materialize_secrets -from openhands.agent_server.persistence import ( - PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, -) +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.agent_server.skills_service import ( discover_profile_skills, discover_public_skill_names, @@ -62,9 +58,9 @@ def server_launch_stores( - settings: PersistedSettings, cipher: Cipher | None + settings: PersistedSettings, stores: ServerStores, cipher: Cipher | None ) -> LaunchStores: - llm_store = get_llm_profile_store() + llm_store = stores.llm_profiles verification = getattr(settings.agent_settings, "verification", None) return LaunchStores( llm_profiles=llm_store, @@ -73,7 +69,7 @@ def server_launch_stores( skills=discover_profile_skills, public_skill_names=discover_public_skill_names, critic_api_key=verification.critic_api_key if verification else None, - agent_profiles=get_agent_profile_store(), + agent_profiles=stores.agent_profiles, meta_profiles=MetaProfileStore(base_dir=default_meta_profile_dir()), cipher=cipher, ) diff --git a/openhands-agent-server/openhands/agent_server/local_secret_resolver.py b/openhands-agent-server/openhands/agent_server/local_secret_resolver.py index 11523c0a5ce..dab7808217b 100644 --- a/openhands-agent-server/openhands/agent_server/local_secret_resolver.py +++ b/openhands-agent-server/openhands/agent_server/local_secret_resolver.py @@ -9,10 +9,10 @@ """ import os +from collections.abc import Callable from urllib.parse import urlsplit -from openhands.agent_server.config import Config -from openhands.agent_server.persistence import get_secrets_store +from openhands.agent_server.persistence import SecretsStore from openhands.sdk.logger import get_logger from openhands.sdk.secret import ( register_local_secret_resolver, @@ -57,14 +57,18 @@ def _secret_name_if_local(url: str) -> str | None: return name -def build_local_secret_resolver(config: Config): - """Build a resolver that answers this server's own secret URLs.""" +def build_local_secret_resolver(secrets_store: Callable[[], SecretsStore]): + """Build a resolver that answers this server's own secret URLs. + + ``secrets_store`` is called per resolution, so the resolver follows the + server to the store of its current context. + """ def resolve(url: str) -> str | None: name = _secret_name_if_local(url) if name is None: return None - value = get_secrets_store(config).get_secret(name) + value = secrets_store().get_secret(name) if value is None: # Fall through to HTTP so the caller still sees the server's 404. return None @@ -77,8 +81,8 @@ def resolve(url: str) -> str | None: class local_secret_resolution: """Context manager registering the in-process resolver for a server.""" - def __init__(self, config: Config) -> None: - self._resolver = build_local_secret_resolver(config) + def __init__(self, secrets_store: Callable[[], SecretsStore]) -> None: + self._resolver = build_local_secret_resolver(secrets_store) def __enter__(self) -> None: register_local_secret_resolver(self._resolver) diff --git a/openhands-agent-server/openhands/agent_server/mcp_oauth_store.py b/openhands-agent-server/openhands/agent_server/mcp_oauth_store.py index e8a033479d5..f463c84b970 100644 --- a/openhands-agent-server/openhands/agent_server/mcp_oauth_store.py +++ b/openhands-agent-server/openhands/agent_server/mcp_oauth_store.py @@ -18,7 +18,7 @@ import httpx from openhands.agent_server.config import Config, WebhookSpec -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import PersistedSettings, SettingsStore from openhands.sdk.logger import get_logger from openhands.sdk.mcp.client import MCPClient from openhands.sdk.mcp.config import ( @@ -115,12 +115,14 @@ class MCPSettingsOAuthTokenStore: def __init__( self, + settings_store: SettingsStore, *, seed_mcp_config: Mapping[str, MCPServer] | None = None, cipher: Cipher | None = None, write_back_webhooks: Sequence[WebhookSpec] = (), session_api_key: str | None = None, ): + self._settings_store = settings_store self._seeded: dict[str, MCPOAuthState] = {} self._seeded_client_ids: dict[str, str | None] = {} self._seeded_lock = threading.Lock() @@ -151,8 +153,7 @@ def _get_entry_sync( if field is None: return None, None - store = get_settings_store() - settings = store.load() + settings = self._settings_store.load() mcp_config = settings.agent_settings.mcp_config if settings else {} match = _find_matching_oauth_server(mcp_config, key) if match is None: @@ -226,7 +227,7 @@ def apply_update(settings: PersistedSettings) -> PersistedSettings: ) return settings - get_settings_store().update(apply_update) + self._settings_store.update(apply_update) for server_url, state in seeded_updates: self._write_back_seeded_state(server_url, state) @@ -330,7 +331,7 @@ def apply_update(settings: PersistedSettings) -> PersistedSettings: ) return settings - get_settings_store().update(apply_update) + self._settings_store.update(apply_update) return deleted async def delete(self, key: str, *, collection: str | None = None) -> bool: @@ -461,6 +462,7 @@ class SettingsBackedMCPToolProvider: the user. """ + settings_store: SettingsStore cipher: Cipher | None = None webhooks: tuple[WebhookSpec, ...] = () session_api_key: str | None = None @@ -477,6 +479,7 @@ def create_tools( mcp_config, timeout, mcp_oauth_token_storage=MCPSettingsOAuthTokenStore( + self.settings_store, seed_mcp_config=mcp_config, cipher=self.cipher, write_back_webhooks=self.webhooks, @@ -489,10 +492,9 @@ def create_tools( def create_settings_backed_mcp_tool_provider( - config: Config, + config: Config, settings_store: SettingsStore ) -> SettingsBackedMCPToolProvider: - """Initialize settings storage and return the agent-server MCP provider.""" - get_settings_store(config) + """Return the agent-server MCP provider that persists to ``settings_store``.""" if config.secret_key is None: logger.warning( "Saving MCP OAuth state without encryption " @@ -500,6 +502,7 @@ def create_settings_backed_mcp_tool_provider( "production deployments." ) return SettingsBackedMCPToolProvider( + settings_store=settings_store, cipher=config.cipher, webhooks=tuple(config.webhooks), session_api_key=( diff --git a/openhands-agent-server/openhands/agent_server/mcp_router.py b/openhands-agent-server/openhands/agent_server/mcp_router.py index ac5195361de..6a1b03f5a17 100644 --- a/openhands-agent-server/openhands/agent_server/mcp_router.py +++ b/openhands-agent-server/openhands/agent_server/mcp_router.py @@ -39,11 +39,10 @@ from fastmcp.client.auth.oauth import ClientNotFoundError, OAuth from pydantic import BaseModel, Field, ValidationError, model_validator -from openhands.agent_server._secrets_exposure import get_cipher, get_config +from openhands.agent_server.dependencies import get_cipher, get_stores from openhands.agent_server.mcp_oauth_store import ( InMemoryMCPOAuthTokenStore, ) -from openhands.agent_server.persistence import get_secrets_store from openhands.sdk.logger import get_logger from openhands.sdk.mcp import create_mcp_tools from openhands.sdk.mcp.client import MCPClient @@ -825,9 +824,9 @@ async def test_mcp_server( ) -> MCPTestResponse: """Probe a single MCP server config and report whether it works.""" # Resolve the cipher and store here: the threadpool function below must - # not reach back into ``http_request.app.state``. + # not reach back into the request's server context. cipher = get_cipher(http_request) - secrets_store = get_secrets_store(get_config(http_request)) + secrets_store = get_stores(http_request).secrets loop = asyncio.get_running_loop() return await loop.run_in_executor( None, diff --git a/openhands-agent-server/openhands/agent_server/meta_profiles_router.py b/openhands-agent-server/openhands/agent_server/meta_profiles_router.py index 25d35910a55..f7fb905f8e7 100644 --- a/openhands-agent-server/openhands/agent_server/meta_profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/meta_profiles_router.py @@ -11,11 +11,8 @@ from fastapi import APIRouter, HTTPException, Path, Request, status from pydantic import AfterValidator, BaseModel -from openhands.agent_server._secrets_exposure import get_config -from openhands.agent_server.persistence import ( - PersistedSettings, - get_settings_store, -) +from openhands.agent_server.dependencies import get_stores +from openhands.agent_server.persistence import PersistedSettings from openhands.sdk.llm.llm_profile_store import PROFILE_NAME_PATTERN from openhands.sdk.llm.meta_profile_store import ( MetaProfile, @@ -117,8 +114,7 @@ def _store_errors() -> Iterator[None]: def _set_active_meta_profile_if_matches( request: Request, old_name: str, new_name: str | None ) -> bool: - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() if settings.active_meta_profile != old_name: return False @@ -144,8 +140,7 @@ def update_active(settings: PersistedSettings) -> PersistedSettings: @meta_profiles_router.get("", response_model=MetaProfileListResponse) def list_meta_profiles(request: Request) -> MetaProfileListResponse: """List all saved meta-profiles and the currently active one.""" - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() store = _get_meta_profile_store() @@ -253,8 +248,7 @@ def activate_meta_profile( detail=f"Meta-profile '{name}' not found", ) - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings applied = False def apply_active(settings: PersistedSettings) -> PersistedSettings: diff --git a/openhands-agent-server/openhands/agent_server/openai/router.py b/openhands-agent-server/openhands/agent_server/openai/router.py index e8f5c046024..de7347bba76 100644 --- a/openhands-agent-server/openhands/agent_server/openai/router.py +++ b/openhands-agent-server/openhands/agent_server/openai/router.py @@ -9,9 +9,13 @@ from fastapi.security import APIKeyHeader, HTTPAuthorizationCredentials, HTTPBearer from pydantic import TypeAdapter, ValidationError -from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService -from openhands.agent_server.dependencies import get_conversation_service +from openhands.agent_server.dependencies import ( + get_config, + get_conversation_registry, + get_conversation_service, + get_stores, +) from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry from openhands.agent_server.openai.models import ( OpenAIChatCompletionRequest, @@ -56,10 +60,10 @@ def check_openai_api_key( system. When no session keys are configured, the local server remains unauthenticated like the existing agent-server API. - Reads config from ``request.app.state`` at request time so that keys - delivered via ``POST /api/init`` take effect immediately. + Reads the config at request time so that keys delivered via + ``POST /api/init`` take effect immediately. """ - config: Config = request.app.state.config + config = get_config(request) if not config.session_api_keys: return bearer_token = authorization.credentials if authorization else None @@ -70,18 +74,8 @@ def check_openai_api_key( raise HTTPException(status.HTTP_401_UNAUTHORIZED) -def _get_config(request: Request) -> Config: - config = getattr(request.app.state, "config", None) - if not isinstance(config, Config): - raise HTTPException( - status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail="Agent server config is not available", - ) - return config - - def _get_docker_registry(request: Request) -> DockerConversationRegistry | None: - registry = getattr(request.app.state, "conversation_registry", None) + registry = get_conversation_registry(request) return registry if isinstance(registry, DockerConversationRegistry) else None @@ -125,8 +119,7 @@ def _parse_observability_overrides( @openai_router.get("/v1/models", response_model=OpenAIModelListResponse) async def get_openai_models(request: Request) -> OpenAIModelListResponse: - _get_config(request) - return await list_openai_models() + return await list_openai_models(get_stores(request)) @openai_router.post( @@ -157,7 +150,8 @@ async def create_chat_completion( ) -> OpenAIChatCompletionResponse | StreamingResponse: result = await run_chat_completion( request=body.model_copy(update={"stream": False}) if body.stream else body, - config=_get_config(request), + config=get_config(request), + stores=get_stores(request), conversation_service=conversation_service, reusable_conversation_id=x_openhands_server_conversation_id, registry=_get_docker_registry(request), @@ -211,7 +205,8 @@ async def create_response( ) -> OpenAIResponse: result = await run_response( request=body, - config=_get_config(request), + config=get_config(request), + stores=get_stores(request), conversation_service=conversation_service, registry=_get_docker_registry(request), observability_overrides=_parse_observability_overrides( diff --git a/openhands-agent-server/openhands/agent_server/openai/service.py b/openhands-agent-server/openhands/agent_server/openai/service.py index 23aea5eb71d..bd26c34dfdf 100644 --- a/openhands-agent-server/openhands/agent_server/openai/service.py +++ b/openhands-agent-server/openhands/agent_server/openai/service.py @@ -43,11 +43,7 @@ OpenAIResponseUsage, OpenAIUsage, ) -from openhands.agent_server.persistence import ( - PersistedSettings, - get_llm_profile_store, - get_settings_store, -) +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.sdk import Message from openhands.sdk.conversation.request import ( AgentLaunchAdditions, @@ -63,6 +59,7 @@ from openhands.sdk.llm.utils.metrics import TokenUsage from openhands.sdk.profiles import OpenHandsAgentProfile from openhands.sdk.profiles.resolver import ProfileNotFound +from openhands.sdk.utils.cipher import Cipher from openhands.sdk.workspace import LocalWorkspace @@ -102,9 +99,11 @@ def _profile_name_from_model(model: str) -> str: ) -def _require_llm_profile(profile_name: str, config: Config) -> None: +def _require_llm_profile( + profile_name: str, stores: ServerStores, cipher: Cipher | None +) -> None: try: - get_llm_profile_store().load(profile_name, cipher=config.cipher) + stores.llm_profiles.load(profile_name, cipher=cipher) except FileNotFoundError: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -269,17 +268,18 @@ def _create_conversation_request( system_text: str, user_content: list[TextContent | ImageContent], config: Config, + stores: ServerStores, conversation_id: UUID | None, ) -> StartConversationRequest: llm_profile = _profile_name_from_model(model) - _require_llm_profile(llm_profile, config) - profile = active_agent_profile(config, config.cipher) + _require_llm_profile(llm_profile, stores, config.cipher) + profile = active_agent_profile(stores, config.cipher) if profile is None: raise HTTPException( status_code=status.HTTP_409_CONFLICT, detail="No active Agent Profile; activate one to use the gateway", ) - settings = get_settings_store(config).load() or PersistedSettings() + settings = stores.settings.load() or PersistedSettings() try: additions = AgentLaunchAdditions( # An ACP server runs its own model. @@ -312,6 +312,7 @@ def _conversation_request( *, request: OpenAIChatCompletionRequest, config: Config, + stores: ServerStores, conversation_id: UUID | None, ) -> StartConversationRequest: user_message = _latest_user_message(request.messages) @@ -320,6 +321,7 @@ def _conversation_request( system_text=_system_text(request.messages), user_content=_content_to_sdk_parts(user_message), config=config, + stores=stores, conversation_id=conversation_id, ) @@ -329,6 +331,7 @@ def _response_conversation_request( request: OpenAIResponseRequest, user_content: list[TextContent | ImageContent], config: Config, + stores: ServerStores, conversation_id: UUID | None, ) -> StartConversationRequest: return _create_conversation_request( @@ -336,6 +339,7 @@ def _response_conversation_request( system_text=_response_system_text(request), user_content=user_content, config=config, + stores=stores, conversation_id=conversation_id, ) @@ -528,9 +532,9 @@ def iter_openai_chat_completion_sse( yield "data: [DONE]\n\n" -async def list_openai_models() -> OpenAIModelListResponse: +async def list_openai_models(stores: ServerStores) -> OpenAIModelListResponse: try: - profiles = await asyncio.to_thread(get_llm_profile_store().list_summaries) + profiles = await asyncio.to_thread(stores.llm_profiles.list_summaries) except TimeoutError: raise HTTPException( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, @@ -747,6 +751,7 @@ async def run_chat_completion( *, request: OpenAIChatCompletionRequest, config: Config, + stores: ServerStores, conversation_service: ConversationService, reusable_conversation_id: UUID | None, observability_overrides: dict[str, object] | None = None, @@ -762,6 +767,7 @@ async def run_chat_completion( start_request = _conversation_request( request=request, config=config, + stores=stores, conversation_id=reusable_conversation_id, ) if observability_overrides: @@ -804,6 +810,7 @@ async def run_response( *, request: OpenAIResponseRequest, config: Config, + stores: ServerStores, conversation_service: ConversationService, observability_overrides: dict[str, object] | None = None, registry: DockerConversationRegistry | None = None, @@ -832,6 +839,7 @@ async def run_response( request=request, user_content=user_content, config=config, + stores=stores, conversation_id=None, ) if observability_overrides: diff --git a/openhands-agent-server/openhands/agent_server/persistence/__init__.py b/openhands-agent-server/openhands/agent_server/persistence/__init__.py index 30805245cd9..aaaa11b9c6f 100644 --- a/openhands-agent-server/openhands/agent_server/persistence/__init__.py +++ b/openhands-agent-server/openhands/agent_server/persistence/__init__.py @@ -23,16 +23,10 @@ FileSettingsStore, FileWorkspacesStore, SecretsStore, + ServerStores, SettingsStore, WorkspacesStore, - get_agent_profile_store, - get_llm_profile_store, - get_provider_connections_store, - get_secrets_store, - get_settings_store, - get_workspaces_store, reencrypt_stale_stores, - reset_stores, ) from openhands.sdk.llm.provider_connection_store import ( PersistedProviderConnections, @@ -62,14 +56,8 @@ "FileWorkspacesStore", "ProviderConnectionStore", "SecretsStore", + "ServerStores", "SettingsStore", "WorkspacesStore", - "get_agent_profile_store", - "get_llm_profile_store", - "get_provider_connections_store", - "get_secrets_store", - "get_settings_store", - "get_workspaces_store", "reencrypt_stale_stores", - "reset_stores", ] diff --git a/openhands-agent-server/openhands/agent_server/persistence/store.py b/openhands-agent-server/openhands/agent_server/persistence/store.py index 7788a69b0c8..bf3efa39623 100644 --- a/openhands-agent-server/openhands/agent_server/persistence/store.py +++ b/openhands-agent-server/openhands/agent_server/persistence/store.py @@ -17,8 +17,9 @@ from abc import ABC, abstractmethod from collections.abc import Callable, Iterator from contextlib import contextmanager +from dataclasses import dataclass, field from pathlib import Path -from typing import TYPE_CHECKING, Any, Final +from typing import TYPE_CHECKING, Any, Final, cast from pydantic import SecretStr @@ -241,10 +242,6 @@ def _atomic_write_json(path: Path, data: dict) -> None: raise -# Default storage directory (relative to working directory) -DEFAULT_PERSISTENCE_DIR = Path("workspace/.openhands") - - # Appended to the "different key" refusals of read-modify-write paths. _KEY_HINT: Final[str] = ( "restore the key it was saved under as OH_SECRET_KEY, or add it to " @@ -886,32 +883,19 @@ def update( return updated -# ── Global Store Access ────────────────────────────────────────────────── - -_settings_store: FileSettingsStore | None = None -_secrets_store: FileSecretsStore | None = None -_provider_connections_store: ProviderConnectionStore | None = None -_workspaces_store: FileWorkspacesStore | None = None -_llm_profile_store: LLMProfileStore | None = None -_agent_profile_store: AgentProfileStore | None = None -_store_lock = threading.Lock() +# ── Per-server stores ──────────────────────────────────────────────────── -def _get_persistence_dir(config: Config | None = None) -> Path: - """Get the persistence directory from config or default.""" +def _get_persistence_dir(config: Config) -> Path: + """Get the workspace-relative persistence directory for ``config``.""" # Absent OH_PERSISTENCE_DIR, fall back to config's conversations_path parent. - default = ( - config.conversations_path.parent / ".openhands" - if config is not None - else DEFAULT_PERSISTENCE_DIR - ) - return get_user_persistence_dir(default) + return get_user_persistence_dir(config.conversations_path.parent / ".openhands") def _get_profile_persistence_dir() -> Path: - """Get the base dir for LLM/agent profile stores. + """Get the base dir for the settings, secrets and profile stores. - Profiles can hold credentials (LLM API keys), so absent + These can hold credentials (LLM API keys), so absent ``OH_PERSISTENCE_DIR`` they fall back to the user's ``~/.openhands`` rather than the workspace-relative agent-server default, keeping bare local profile secrets in the expected user config directory. @@ -919,191 +903,129 @@ def _get_profile_persistence_dir() -> Path: return get_user_persistence_dir() -def _get_cipher(config: Config | None = None) -> Cipher | None: - """Get cipher from config for encrypting secrets.""" - if config is not None: - return config.cipher - return None - - -def get_settings_store(config: Config | None = None) -> FileSettingsStore: - """Get the global settings store instance (thread-safe). - - Note: - The config parameter is only used on first initialization. - Subsequent calls return the existing instance regardless of config. - - Warning: - The cipher key (OH_SECRET_KEY) must NOT change during runtime. - The store singleton caches the cipher from first initialization. - If the cipher key changes: - - New data may be encrypted with a stale key - - Existing data may fail to decrypt - - This could trigger data loss protection in update operations - - To use a new cipher key, restart the server process. - For testing, use :func:`reset_stores` to clear the singletons. - """ - global _settings_store - if _settings_store is not None: - return _settings_store - - with _store_lock: - # Double-check after acquiring lock - if _settings_store is None: - _settings_store = FileSettingsStore( - persistence_dir=_get_profile_persistence_dir(), - cipher=_get_cipher(config), - ) - return _settings_store - +@dataclass(frozen=True) +class ServerStores: + """The persistent stores of one server (re)initialisation. -def get_secrets_store(config: Config | None = None) -> FileSecretsStore: - """Get the global secrets store instance (thread-safe). + A server builds one instance per :class:`Config` it runs with (at start-up + and again on ``POST /api/init``) and reaches every store through it, so all + of them agree on the cipher and the directories resolved when it was built. + There is no process-wide store: code that needs one is handed the instance + (or the store) it should use. - Note: - The config parameter is only used on first initialization. - Subsequent calls return the existing instance regardless of config. + Building the instance touches no file. Each store is created on first use + and then reused; several of their constructors create their directory. Warning: - The cipher key (OH_SECRET_KEY) must NOT change during runtime. - The store singleton caches the cipher from first initialization. - If the cipher key changes: - - New data may be encrypted with a stale key - - Existing data may fail to decrypt - - This could trigger data loss protection in update operations - - To use a new cipher key, restart the server process. - For testing, use :func:`reset_stores` to clear the singletons. + The cipher key is fixed for the lifetime of the instance. To use a new + key, build a new instance from the new config. """ - global _secrets_store - if _secrets_store is not None: - return _secrets_store - - with _store_lock: - # Double-check after acquiring lock - if _secrets_store is None: - _secrets_store = FileSecretsStore( - persistence_dir=_get_profile_persistence_dir(), - cipher=_get_cipher(config), - ) - return _secrets_store + cipher: Cipher | None + """Encrypts the settings and secrets stores at rest.""" + profile_dir: Path + """Base of the settings, secrets, provider-connection and profile stores: + ``OH_PERSISTENCE_DIR`` when set, else ``~/.openhands``.""" + workspace_dir: Path + """Base of the workspaces store: ``OH_PERSISTENCE_DIR`` when set, else + ``.openhands`` next to the config's conversations directory.""" + _lock: threading.RLock = field( + default_factory=threading.RLock, init=False, repr=False, compare=False + ) + _built: dict[str, object] = field( + default_factory=dict, init=False, repr=False, compare=False + ) -def reencrypt_stale_stores(config: Config) -> None: + @classmethod + def from_config(cls, config: Config) -> ServerStores: + """Resolve the cipher and directories ``config`` selects.""" + return cls( + cipher=config.cipher, + profile_dir=_get_profile_persistence_dir(), + workspace_dir=_get_persistence_dir(config), + ) + + def _once[StoreT](self, name: str, build: Callable[[], StoreT]) -> StoreT: + with self._lock: + if name not in self._built: + self._built[name] = build() + return cast(StoreT, self._built[name]) + + @property + def settings(self) -> FileSettingsStore: + return self._once( + "settings", + lambda: FileSettingsStore( + persistence_dir=self.profile_dir, cipher=self.cipher + ), + ) + + @property + def secrets(self) -> FileSecretsStore: + return self._once( + "secrets", + lambda: FileSecretsStore( + persistence_dir=self.profile_dir, cipher=self.cipher + ), + ) + + @property + def provider_connections(self) -> ProviderConnectionStore: + """Stored at ``/provider-connections``. + + The store itself is cipher-agnostic; callers pass the cipher per call + so keys are encrypted at rest. + """ + return self._once( + "provider_connections", + lambda: ProviderConnectionStore( + base_dir=self.profile_dir / "provider-connections" + ), + ) + + @property + def workspaces(self) -> FileWorkspacesStore: + return self._once( + "workspaces", + lambda: FileWorkspacesStore(persistence_dir=self.workspace_dir), + ) + + @property + def llm_profiles(self) -> LLMProfileStore: + """Stored at ``/profiles``, never workspace-relative.""" + return self._once( + "llm_profiles", + lambda: LLMProfileStore( + base_dir=self.profile_dir / "profiles", + provider_store=self.provider_connections, + ), + ) + + @property + def agent_profiles(self) -> AgentProfileStore: + """Stored at ``/agent-profiles``.""" + return self._once( + "agent_profiles", + lambda: AgentProfileStore(base_dir=self.profile_dir / "agent-profiles"), + ) + + +def reencrypt_stale_stores(stores: ServerStores) -> None: """Re-encrypt the settings and secrets stores under the current key. Runs at startup when ``previous_secret_keys`` is configured, so a rotated key does not wait for the next write. Failures are logged, never raised: the stores stay readable through the previous keys either way. """ - cipher = config.cipher + cipher = stores.cipher if cipher is None or not cipher.previous_keys: return for name, store in ( - ("settings", get_settings_store(config)), - ("secrets", get_secrets_store(config)), + ("settings", stores.settings), + ("secrets", stores.secrets), ): try: if store.reencrypt(): logger.info(f"Re-encrypted the {name} store under the current key") except Exception: logger.error(f"Could not re-encrypt the {name} store", exc_info=True) - - -def get_provider_connections_store( - config: Config | None = None, # noqa: ARG001 -) -> ProviderConnectionStore: - """Get the global provider connections store instance (thread-safe). - - Stored at ``/provider-connections`` alongside ``profiles`` / - ``agent-profiles``. The store itself is cipher-agnostic; callers pass the - request cipher per call so keys are encrypted at rest. - """ - global _provider_connections_store - if _provider_connections_store is not None: - return _provider_connections_store - - with _store_lock: - if _provider_connections_store is None: - _provider_connections_store = ProviderConnectionStore( - base_dir=_get_profile_persistence_dir() / "provider-connections", - ) - return _provider_connections_store - - -def get_workspaces_store(config: Config | None = None) -> FileWorkspacesStore: - """Get the global workspaces store instance (thread-safe). - - Note: - The config parameter is only used on first initialization. - Subsequent calls return the existing instance regardless of config. - """ - global _workspaces_store - if _workspaces_store is not None: - return _workspaces_store - - with _store_lock: - if _workspaces_store is None: - _workspaces_store = FileWorkspacesStore( - persistence_dir=_get_persistence_dir(config), - ) - return _workspaces_store - - -def get_llm_profile_store() -> LLMProfileStore: - """Get the global ``LLMProfileStore`` instance (thread-safe). - - Stored at ``/profiles`` where ```` is ``OH_PERSISTENCE_DIR`` - when set, else ``~/.openhands``. This honors isolated agent-server - instances while keeping bare local profile secrets in the user's - config directory (never workspace-relative). See - :func:`_get_profile_persistence_dir`. - """ - global _llm_profile_store - if _llm_profile_store is not None: - return _llm_profile_store - - # Resolve the provider store first: it takes ``_store_lock`` itself, and - # ``_store_lock`` is non-reentrant, so calling it while already holding the - # lock below would deadlock. - provider_store = get_provider_connections_store() - with _store_lock: - if _llm_profile_store is None: - _llm_profile_store = LLMProfileStore( - base_dir=_get_profile_persistence_dir() / "profiles", - provider_store=provider_store, - ) - return _llm_profile_store - - -def get_agent_profile_store() -> AgentProfileStore: - """Get the global ``AgentProfileStore`` instance (thread-safe). - - Stored at ``/agent-profiles`` where ```` is resolved by - :func:`_get_profile_persistence_dir`, for the same reason as - :func:`get_llm_profile_store`. - """ - global _agent_profile_store - if _agent_profile_store is not None: - return _agent_profile_store - - with _store_lock: - if _agent_profile_store is None: - _agent_profile_store = AgentProfileStore( - base_dir=_get_profile_persistence_dir() / "agent-profiles", - ) - return _agent_profile_store - - -def reset_stores() -> None: - """Reset global store instances (for testing).""" - global _settings_store, _secrets_store, _provider_connections_store - global _workspaces_store, _llm_profile_store, _agent_profile_store - with _store_lock: - _settings_store = None - _secrets_store = None - _provider_connections_store = None - _workspaces_store = None - _llm_profile_store = None - _agent_profile_store = None diff --git a/openhands-agent-server/openhands/agent_server/profiles_router.py b/openhands-agent-server/openhands/agent_server/profiles_router.py index 290da67fe44..3d8059af981 100644 --- a/openhands-agent-server/openhands/agent_server/profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/profiles_router.py @@ -9,20 +9,13 @@ from openhands.agent_server._secrets_exposure import ( build_expose_context, decrypt_incoming_llm_secrets, - get_cipher, - get_config, parse_expose_secrets_header, profile_key_mismatch, store_errors, translate_missing_cipher, ) -from openhands.agent_server.persistence import ( - PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_provider_connections_store, - get_settings_store, -) +from openhands.agent_server.dependencies import get_cipher, get_stores +from openhands.agent_server.persistence import PersistedSettings from openhands.sdk.llm import LLM, Message, TextContent from openhands.sdk.llm.exceptions import ( LLMError, @@ -120,12 +113,11 @@ def _profile_api_key_set(request: Request, llm: LLM) -> bool: connection_id = llm.provider_connection_id if not connection_id: return False - config = get_config(request) cipher = get_cipher(request) # The provider store read can raise on a corrupted file; map it instead of # letting it surface as an unhandled 500 on GET /profiles/{name}. with store_errors(): - connection = get_provider_connections_store(config).get( + connection = get_stores(request).provider_connections.get( connection_id, cipher=cipher ) return connection is not None and connection.api_key_value() is not None @@ -134,8 +126,7 @@ def _profile_api_key_set(request: Request, llm: LLM) -> bool: def _set_active_profile_if_matches( request: Request, old_name: str, new_name: str | None ) -> bool: - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() active = settings.active_profile # ``foo.json`` addresses profile ``foo`` in the store, so compare store keys. @@ -162,11 +153,10 @@ def list_profiles(request: Request) -> ProfileListResponse: if one has been activated. The active_profile tracks which LLM profile configuration is currently in use. """ - config = get_config(request) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() - store = get_llm_profile_store() + store = get_stores(request).llm_profiles with store_errors(): summaries = store.list_summaries() @@ -189,7 +179,7 @@ def get_profile(request: Request, name: ProfileName) -> ProfileDetailResponse: expose_mode = parse_expose_secrets_header(request) cipher = get_cipher(request) - store = get_llm_profile_store() + store = get_stores(request).llm_profiles try: with store_errors(): # Runtime reads need current provider credentials. Editor reads @@ -239,7 +229,7 @@ def save_profile( """ cipher = get_cipher(request) llm = decrypt_incoming_llm_secrets(body.llm, cipher) if cipher else body.llm - store = get_llm_profile_store() + store = get_stores(request).llm_profiles try: with store_errors(): store.save( @@ -352,7 +342,9 @@ async def validate_profile( # credentials, as activation and ``switch_llm`` run it. No-op without # ``provider_connection_id``; a dangling link is a 422 naming it. with store_errors(): - llm = get_llm_profile_store().resolve_provider_connection(llm, cipher=cipher) + llm = get_stores(request).llm_profiles.resolve_provider_connection( + llm, cipher=cipher + ) messages = [ Message( @@ -430,8 +422,8 @@ def delete_profile(request: Request, name: ProfileName) -> ProfileMutationRespon Guarded by the agent-profile FK: returns 409 naming the referrers if any ``AgentProfile`` still cites this LLM profile via ``llm_profile_ref``. """ - store = get_llm_profile_store() - agent_store = get_agent_profile_store() + store = get_stores(request).llm_profiles + agent_store = get_stores(request).agent_profiles try: with store_errors(): delete_llm_profile(agent_store, store, name) @@ -458,8 +450,8 @@ def rename_profile( setting is updated to the new name. Any ``AgentProfile.llm_profile_ref`` citing the old name is cascaded to the new name in lock-step. """ - store = get_llm_profile_store() - agent_store = get_agent_profile_store() + store = get_stores(request).llm_profiles + agent_store = get_stores(request).agent_profiles try: with store_errors(): rename_llm_profile(agent_store, store, name, body.new_name) @@ -513,13 +505,12 @@ def activate_profile(request: Request, name: ProfileName) -> ActivateProfileResp the ``active_profile`` field. """ cipher = get_cipher(request) - config = get_config(request) # Load the profile. ``load`` resolves any referenced provider connection # (read-at-use), so the LLM applied to settings already carries the shared # api_key / base_url; ``provider_connection_id`` is retained so a later # rotation re-resolves on the next activation or launch. - profile_store = get_llm_profile_store() + profile_store = get_stores(request).llm_profiles # A dangling provider_connection_id raises ProviderConnectionNotFound, which # store_errors() maps to 422. try: @@ -533,7 +524,7 @@ def activate_profile(request: Request, name: ProfileName) -> ActivateProfileResp detail=f"Profile '{name}' not found", ) - settings_store = get_settings_store(config) + settings_store = get_stores(request).settings llm_applied = False def apply_profile(settings: PersistedSettings) -> PersistedSettings: diff --git a/openhands-agent-server/openhands/agent_server/provider_connections_router.py b/openhands-agent-server/openhands/agent_server/provider_connections_router.py index f376480d2b1..9e87be5d936 100644 --- a/openhands-agent-server/openhands/agent_server/provider_connections_router.py +++ b/openhands-agent-server/openhands/agent_server/provider_connections_router.py @@ -25,17 +25,9 @@ model_validator, ) -from openhands.agent_server._secrets_exposure import ( - get_cipher, - get_config, - store_errors, -) -from openhands.agent_server.persistence import ( - ProviderConnection, - get_llm_profile_store, - get_provider_connections_store, - get_settings_store, -) +from openhands.agent_server._secrets_exposure import store_errors +from openhands.agent_server.dependencies import get_cipher, get_stores +from openhands.agent_server.persistence import ProviderConnection, ServerStores from openhands.sdk.llm.provider_connection_store import ( ProviderConnectionLimitExceeded, ProviderConnectionNotFound, @@ -119,16 +111,18 @@ def _to_response(connection: ProviderConnection) -> ProviderConnectionResponse: ) -def _linked_profile_names(connection_id: str) -> list[str]: +def _linked_profile_names(stores: ServerStores, connection_id: str) -> list[str]: return sorted( str(summary["name"]) - for summary in get_llm_profile_store().list_summaries() + for summary in stores.llm_profiles.list_summaries() if summary.get("provider_connection_id") == connection_id ) -def _active_settings_references_connection(config, connection_id: str) -> bool: - settings = get_settings_store(config).load() +def _active_settings_references_connection( + stores: ServerStores, connection_id: str +) -> bool: + settings = stores.settings.load() if settings is None or not isinstance( settings.agent_settings, OpenHandsAgentSettings ): @@ -136,15 +130,17 @@ def _active_settings_references_connection(config, connection_id: str) -> bool: return settings.agent_settings.llm.provider_connection_id == connection_id -def _raise_if_connection_is_referenced(config, connection_id: str) -> None: +def _raise_if_connection_is_referenced( + stores: ServerStores, connection_id: str +) -> None: """Block deletion while any profile or the active settings still point here. Deleting a referenced connection would leave those references dangling; a linked profile with no inline key raises on its next load. So the delete is rejected until the references are removed first. """ - profile_names = _linked_profile_names(connection_id) - active_reference = _active_settings_references_connection(config, connection_id) + profile_names = _linked_profile_names(stores, connection_id) + active_reference = _active_settings_references_connection(stores, connection_id) if not profile_names and not active_reference: return @@ -168,7 +164,7 @@ def list_provider_connections( request: Request, ) -> list[ProviderConnectionResponse]: cipher = get_cipher(request) - store = get_provider_connections_store(get_config(request)) + store = get_stores(request).provider_connections with store_errors(): connections = store.list(cipher=cipher) return [_to_response(c) for c in connections] @@ -181,7 +177,7 @@ def create_provider_connection( request: Request, body: ProviderConnectionCreateRequest ) -> ProviderConnectionResponse: cipher = get_cipher(request) - store = get_provider_connections_store(get_config(request)) + store = get_stores(request).provider_connections now = _now() connection = ProviderConnection( id=uuid.uuid4().hex, @@ -220,7 +216,7 @@ def update_provider_connection( ) cipher = get_cipher(request) - store = get_provider_connections_store(get_config(request)) + store = get_stores(request).provider_connections with store_errors(): connection = store.get(connection_id, cipher=cipher) if connection is None: @@ -269,9 +265,9 @@ def update_provider_connection( def delete_provider_connection( request: Request, connection_id: str ) -> ProviderConnectionResponse: - config = get_config(request) cipher = get_cipher(request) - store = get_provider_connections_store(config) + stores = get_stores(request) + store = stores.provider_connections with store_errors(): connection = store.get(connection_id, cipher=cipher) if connection is None: @@ -279,7 +275,7 @@ def delete_provider_connection( status_code=status.HTTP_404_NOT_FOUND, detail=f"Provider connection '{connection_id}' not found", ) - _raise_if_connection_is_referenced(config, connection_id) + _raise_if_connection_is_referenced(stores, connection_id) # See update handler: keep the delete-race as 404, map infra errors. with store_errors(): diff --git a/openhands-agent-server/openhands/agent_server/server_context.py b/openhands-agent-server/openhands/agent_server/server_context.py new file mode 100644 index 00000000000..1053c81faf9 --- /dev/null +++ b/openhands-agent-server/openhands/agent_server/server_context.py @@ -0,0 +1,83 @@ +"""Everything a running agent server derives from its configuration. + +A server has exactly one :class:`ServerContext` at a time. It is built once per +(re)initialisation -- when the app is created, when its lifespan starts the +services, and again when ``POST /api/init`` delivers the runtime configuration +of a deferred-init server -- and installed on the app with a single assignment, +so a request sees either the previous context or the next one, never a mix of +the two. + +Request handlers reach it through the dependencies in +:mod:`openhands.agent_server.dependencies`. Code that runs outside a request is +handed the config, store or service it needs by whoever built the context; +there is no process-wide default to fall back to. +""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from starlette.applications import Starlette +from starlette.requests import HTTPConnection + +from openhands.agent_server.config import Config +from openhands.agent_server.conversation_registry import ( + ConversationRegistry, + create_conversation_registry, +) +from openhands.agent_server.persistence import ServerStores + + +# The services import modules that read the context, so they are named here +# for typing only. +if TYPE_CHECKING: + from openhands.agent_server.bash_service import BashEventService + from openhands.agent_server.conversation_service import ConversationService + + +@dataclass(frozen=True, slots=True) +class ServerContext: + """The config of one server (re)initialisation and what was built from it.""" + + config: Config + stores: ServerStores + conversation_registry: ConversationRegistry + conversation_service: ConversationService | None = None + """``None`` until the services are started: before the lifespan runs, and + for as long as a deferred-init server is dormant.""" + bash_event_service: BashEventService | None = None + """``None`` under the same conditions as ``conversation_service``.""" + + +def build_server_context(config: Config) -> ServerContext: + """Build the context for ``config`` with no service started yet.""" + stores = ServerStores.from_config(config) + return ServerContext( + config=config, + stores=stores, + conversation_registry=create_conversation_registry(config, stores), + ) + + +def install_server_context(app: Starlette, context: ServerContext) -> None: + """Make ``context`` the one every later request to ``app`` sees.""" + app.state.server_context = context + + +def server_context_of(app: Starlette) -> ServerContext: + """Return the context ``app`` currently runs with.""" + try: + context = app.state.server_context + except AttributeError: + raise RuntimeError( + "This app has no ServerContext; build it with create_app() or " + "install one with install_server_context()" + ) from None + assert isinstance(context, ServerContext) + return context + + +def get_server_context(connection: HTTPConnection) -> ServerContext: + """Return the context of the app serving this request or WebSocket.""" + return server_context_of(connection.app) diff --git a/openhands-agent-server/openhands/agent_server/server_details_router.py b/openhands-agent-server/openhands/agent_server/server_details_router.py index c293a4b8db0..3fbe008be6e 100644 --- a/openhands-agent-server/openhands/agent_server/server_details_router.py +++ b/openhands-agent-server/openhands/agent_server/server_details_router.py @@ -9,6 +9,7 @@ from pydantic import BaseModel, Field from openhands.agent_server.launch import configured_browser_available +from openhands.agent_server.server_context import get_server_context from openhands.sdk.settings.model import ConversationRuntime from openhands.sdk.tool import BROWSER_TOOL_NAME from openhands.sdk.tool.registry import list_registered_tools, list_usable_tools @@ -183,7 +184,7 @@ def build_server_info( @server_details_router.get("/server_info") async def get_server_info(request: Request) -> ServerInfo: - config = request.app.state.config + config = get_server_context(request).config return build_server_info( config.conversation_runtime, app_backend_ingress_url=config.app_backend_public_url, diff --git a/openhands-agent-server/openhands/agent_server/session_socket.py b/openhands-agent-server/openhands/agent_server/session_socket.py index e0173825ceb..5a51f4adfb5 100644 --- a/openhands-agent-server/openhands/agent_server/session_socket.py +++ b/openhands-agent-server/openhands/agent_server/session_socket.py @@ -21,6 +21,7 @@ from openhands.agent_server.conversation_service import ( CredentialBindingActivationRequired, ) +from openhands.agent_server.dependencies import get_conversation_service from openhands.agent_server.pub_sub import MaxSubscribersError, Subscriber from openhands.agent_server.session_protocol import ( MAX_FRAME_BYTES, @@ -36,7 +37,6 @@ ) from openhands.agent_server.sockets import ( _accept_authenticated_websocket, - _get_conversation_service, _is_auth_control_message, _is_websocket_connected, _safe_close_websocket, @@ -372,7 +372,7 @@ async def session_socket( return logger.info("session_socket_connected: %s after_seq=%s", conversation_id, after_seq) - conv_service = _get_conversation_service(websocket) + conv_service = get_conversation_service(websocket) try: event_service = await conv_service.get_event_service(conversation_id) except CredentialBindingActivationRequired: diff --git a/openhands-agent-server/openhands/agent_server/settings_router.py b/openhands-agent-server/openhands/agent_server/settings_router.py index aa7a0b6245c..8040e1733f8 100644 --- a/openhands-agent-server/openhands/agent_server/settings_router.py +++ b/openhands-agent-server/openhands/agent_server/settings_router.py @@ -7,20 +7,16 @@ from openhands.agent_server._secrets_exposure import ( build_expose_context, - get_cipher, - get_config, parse_expose_secrets_header, profile_key_mismatch, store_errors, translate_missing_cipher, ) +from openhands.agent_server.dependencies import get_cipher, get_config, get_stores from openhands.agent_server.persistence import ( SECRET_NAME_PATTERN, PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_secrets_store, - get_settings_store, + ServerStores, ) from openhands.agent_server.persistence.models import SettingsUpdatePayload from openhands.agent_server.telemetry import notify_misc_settings_changed @@ -153,7 +149,7 @@ async def get_settings(request: Request) -> SettingsResponse: """ expose_mode = parse_expose_secrets_header(request) config = get_config(request) - store = get_settings_store(config) + store = get_stores(request).settings settings = store.load() or PersistedSettings() # Audit log all settings access for security visibility @@ -248,14 +244,14 @@ def update_settings( ), ) - _require_pointer_targets(update_data) + _require_pointer_targets(get_stores(request), update_data) return _apply_settings_update( request, cast(SettingsUpdatePayload, update_data), ) -def _require_pointer_targets(update_data: dict) -> None: +def _require_pointer_targets(stores: ServerStores, update_data: dict) -> None: """Refuse agent-profile / meta-profile pointers that name nothing. Mirrors ``POST /api/agent-profiles/{id}/activate`` and @@ -264,7 +260,7 @@ def _require_pointer_targets(update_data: dict) -> None: profile_id = update_data.get("active_agent_profile_id") if profile_id is not None: with store_errors(): - known = get_agent_profile_store().name_for_id(profile_id) is not None + known = stores.agent_profiles.name_for_id(profile_id) is not None if not known: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -294,7 +290,7 @@ def _load_active_profile_llm( return None cipher = get_cipher(request) - profile_store = get_llm_profile_store() + profile_store = get_stores(request).llm_profiles # ``load`` resolves any referenced provider connection (read-at-use); a # dangling reference raises ProviderConnectionNotFound, which # store_errors() maps to 422. A key the cipher cannot decrypt is refused @@ -354,7 +350,7 @@ def apply_update(settings: PersistedSettings) -> PersistedSettings: return settings config = get_config(request) - store = get_settings_store(config) + store = get_stores(request).settings client_host = request.client.host if request.client else "unknown" try: settings = store.update(apply_update) @@ -507,13 +503,12 @@ def list_secrets( request: Request, agent_profile_id: str | None = None ) -> SecretsListResponse: """List available secret names, optionally scoped by an agent profile.""" - config = get_config(request) - store = get_secrets_store(config) + store = get_stores(request).secrets secrets = store.load() allowed_names: set[str] | None = None if agent_profile_id is not None: - profile_store = get_agent_profile_store() + profile_store = get_stores(request).agent_profiles # store_errors() answers an unloadable profile (400) and a busy store # (503) the way the agent-profile routes do. with store_errors(): @@ -555,8 +550,7 @@ async def get_secret_value(request: Request, name: str) -> Response: """ _validate_secret_name(name) - config = get_config(request) - store = get_secrets_store(config) + store = get_stores(request).secrets value = store.get_secret(name) client_host = request.client.host if request.client else "unknown" @@ -594,8 +588,7 @@ async def create_secret( detail="Secret value must not be empty or redacted", ) - config = get_config(request) - store = get_secrets_store(config) + store = get_stores(request).secrets try: store.set_secret( @@ -639,8 +632,7 @@ async def delete_secret(request: Request, name: str) -> dict[str, bool]: """ _validate_secret_name(name) - config = get_config(request) - store = get_secrets_store(config) + store = get_stores(request).secrets client_host = request.client.host if request.client else "unknown" try: diff --git a/openhands-agent-server/openhands/agent_server/skills_router.py b/openhands-agent-server/openhands/agent_server/skills_router.py index b54abd9e3c8..ce2961c4b32 100644 --- a/openhands-agent-server/openhands/agent_server/skills_router.py +++ b/openhands-agent-server/openhands/agent_server/skills_router.py @@ -9,6 +9,7 @@ from fastapi import APIRouter, HTTPException, Path, Request from pydantic import BaseModel, Field +from openhands.agent_server.dependencies import get_config from openhands.agent_server.skills_service import ( ExposedUrlData, MarketplaceSkillInfo, @@ -304,8 +305,7 @@ def get_skills(request: SkillsRequest, http_request: Request) -> SkillsResponse: if request.org_configs: org_repos = [(c.org_repo_url, c.org_name) for c in request.org_configs] - config = getattr(http_request.app.state, "config", None) - server_registrations = config.registered_marketplaces if config is not None else [] + server_registrations = get_config(http_request).registered_marketplaces registered_marketplaces = _merge_registered_marketplaces( server_registrations, request.registered_marketplaces, diff --git a/openhands-agent-server/openhands/agent_server/sockets.py b/openhands-agent-server/openhands/agent_server/sockets.py index 375a44df1af..8b3deac63d5 100644 --- a/openhands-agent-server/openhands/agent_server/sockets.py +++ b/openhands-agent-server/openhands/agent_server/sockets.py @@ -28,15 +28,13 @@ ) from starlette.websockets import WebSocketState -from openhands.agent_server.bash_service import ( - BashEventService, - get_default_bash_event_service, -) -from openhands.agent_server.config import Config, get_default_config from openhands.agent_server.conversation_service import ( - ConversationService, CredentialBindingActivationRequired, - get_default_conversation_service, +) +from openhands.agent_server.dependencies import ( + get_config, + get_conversation_service, + get_server_bash_event_service, ) from openhands.agent_server.event_router import normalize_datetime_to_server_timezone from openhands.agent_server.models import ( @@ -58,56 +56,9 @@ conversation_sockets_router = APIRouter(prefix="/sockets", tags=["WebSockets"]) bash_sockets_router = APIRouter(prefix="/sockets", tags=["WebSockets"]) -conversation_service = get_default_conversation_service() -bash_event_service = get_default_bash_event_service() logger = logging.getLogger(__name__) -def _get_config(websocket: WebSocket) -> Config: - """Return the Config associated with this FastAPI app instance. - - This ensures WebSocket auth follows the same configuration as the REST API - when the agent server is used as a library (e.g., tests or when mounted into - another FastAPI app), rather than always reading environment defaults. - """ - config = getattr(websocket.app.state, "config", None) - if isinstance(config, Config): - return config - return get_default_config() - - -def _get_conversation_service(websocket: WebSocket) -> ConversationService: - """Return the ConversationService for this FastAPI app instance. - - Looks up ``app.state.conversation_service`` at request time so that the - service delivered via ``POST /api/init`` (deferred-init / dormant mode) - is used instead of the module-level default captured at import. When - ``app.state`` is not configured (e.g. when sockets.py is imported as a - library without a lifespan), falls back to the module-level singleton, - which keeps the behaviour of existing tests that patch the module-level - variable. - """ - service = getattr(websocket.app.state, "conversation_service", None) - if isinstance(service, ConversationService): - return service - return conversation_service - - -def _get_bash_event_service(websocket: WebSocket) -> BashEventService: - """Return the BashEventService for this FastAPI app instance. - - Looks up ``app.state.bash_event_service`` at request time so that the - service delivered via ``POST /api/init`` (deferred-init / dormant mode) - is used instead of the module-level default captured at import. When - ``app.state`` is not configured (e.g. when sockets.py is imported as a - library without a lifespan), falls back to the module-level singleton. - """ - service = getattr(websocket.app.state, "bash_event_service", None) - if isinstance(service, BashEventService): - return service - return bash_event_service - - def _resolve_websocket_session_api_key( websocket: WebSocket, session_api_key: str | None, @@ -151,7 +102,7 @@ async def _accept_authenticated_websocket( The WebSocket is always *accepted* before first-message auth is attempted because raw WebSocket requires ``accept()`` before any frames can be read. """ - config = _get_config(websocket) + config = get_config(websocket) resolved_key = _resolve_websocket_session_api_key(websocket, session_api_key) # No auth configured — accept unconditionally. @@ -287,7 +238,7 @@ async def events_socket( return logger.info(f"Event Websocket Connected: {conversation_id}") - conv_service = _get_conversation_service(websocket) + conv_service = get_conversation_service(websocket) try: event_service = await conv_service.get_event_service(conversation_id) except CredentialBindingActivationRequired: @@ -427,7 +378,7 @@ async def bash_events_socket( if not await _accept_authenticated_websocket(websocket, session_api_key): return - bash_service = _get_bash_event_service(websocket) + bash_service = get_server_bash_event_service(websocket) logger.info("Bash Websocket Connected") try: subscriber_id = await bash_service.subscribe_to_events( diff --git a/openhands-agent-server/openhands/agent_server/sub_agents_router.py b/openhands-agent-server/openhands/agent_server/sub_agents_router.py index da5c09b9e52..280b977a4a1 100644 --- a/openhands-agent-server/openhands/agent_server/sub_agents_router.py +++ b/openhands-agent-server/openhands/agent_server/sub_agents_router.py @@ -11,7 +11,7 @@ from fastapi import APIRouter, Request from pydantic import BaseModel, Field -from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_config from openhands.agent_server.launch import configured_browser_available from openhands.sdk.context.condenser import CondenserBase from openhands.sdk.hooks.config import HookConfig @@ -137,7 +137,7 @@ def get_sub_agents( include_project=request.load_project, include_user=request.load_user, ) - config: Config = http_request.app.state.config + config = get_config(http_request) builtins = ( discover_builtin_agents( enable_browser=configured_browser_available(config) is not False diff --git a/openhands-agent-server/openhands/agent_server/telemetry/service.py b/openhands-agent-server/openhands/agent_server/telemetry/service.py index a085a9c12c3..91eabc3fc5e 100644 --- a/openhands-agent-server/openhands/agent_server/telemetry/service.py +++ b/openhands-agent-server/openhands/agent_server/telemetry/service.py @@ -1,7 +1,7 @@ """Construction and lifecycle of the process-wide telemetry sink. -Mirrors the singleton pattern used by ``persistence/store.py`` so tests can -reset process state between cases. +The sink is process state; ``reset_telemetry_sink`` lets tests drop it between +cases. Everything here degrades rather than fails: a missing optional dependency, an unconfigured exporter, or a settings-store read error all resolve to the no-op @@ -13,6 +13,7 @@ from typing import Any from openhands.agent_server.config import Config +from openhands.agent_server.persistence.store import SettingsStore from openhands.agent_server.telemetry.factory import ( DiagnosticEventFactory, build_runtime_properties, @@ -38,18 +39,14 @@ _server_started_emitted = False -def _read_misc_settings_sync(config: Config) -> dict[str, Any]: - from openhands.agent_server.persistence.store import get_settings_store - - # Must pass config: the store singleton is fixed by the first call, and a - # no-arg call here would disable the cipher process-wide. - settings = get_settings_store(config).load() +def _read_misc_settings_sync(settings_store: SettingsStore) -> dict[str, Any]: + settings = settings_store.load() return dict(settings.misc_settings) if settings is not None else {} -async def _read_decision(config: Config) -> TelemetryDecision: +async def _read_decision(settings_store: SettingsStore) -> TelemetryDecision: """Resolve consent from persisted settings, off the event loop.""" - misc = await asyncio.to_thread(_read_misc_settings_sync, config) + misc = await asyncio.to_thread(_read_misc_settings_sync, settings_store) return resolve(misc) @@ -98,8 +95,14 @@ def _build_exporter(config: Config) -> TelemetryExporter | None: return None -async def build_telemetry_sink(config: Config) -> TelemetrySink: - """Build the sink for ``config``, or a no-op if telemetry cannot run.""" +async def build_telemetry_sink( + config: Config, settings_store: SettingsStore +) -> TelemetrySink: + """Build the sink for ``config``, or a no-op if telemetry cannot run. + + Consent is read from ``settings_store``, the settings store of the server + context ``config`` belongs to. + """ global _telemetry_sink, _event_factory spec = config.telemetry @@ -133,7 +136,7 @@ async def build_telemetry_sink(config: Config) -> TelemetrySink: return _telemetry_sink try: - decision = await _read_decision(config) + decision = await _read_decision(settings_store) except Exception as exc: # Unreadable settings fail closed. logger.debug("Could not read telemetry consent: %s", type(exc).__name__) @@ -142,7 +145,7 @@ async def build_telemetry_sink(config: Config) -> TelemetrySink: sink = BufferedTelemetrySink( exporter, decision=decision, - decision_reader=partial(_read_decision, config), + decision_reader=partial(_read_decision, settings_store), max_queue_size=spec.max_queue_size, event_buffer_size=spec.event_buffer_size, flush_delay=spec.flush_delay, diff --git a/openhands-agent-server/openhands/agent_server/tool_preload_service.py b/openhands-agent-server/openhands/agent_server/tool_preload_service.py index a8fe99d0331..7ec62a8f039 100644 --- a/openhands-agent-server/openhands/agent_server/tool_preload_service.py +++ b/openhands-agent-server/openhands/agent_server/tool_preload_service.py @@ -2,7 +2,7 @@ from __future__ import annotations -from openhands.agent_server.config import get_default_config +from openhands.agent_server.config import Config from openhands.sdk.logger import get_logger from openhands.sdk.tool.schema import Action from openhands.sdk.tool.tool import create_action_type_with_risk @@ -55,11 +55,9 @@ def is_running(self) -> bool: _tool_preload_service: ToolPreloadService | None = None -def get_tool_preload_service() -> ToolPreloadService | None: - """Get the tool preload service instance if preload is enabled.""" +def get_tool_preload_service(config: Config) -> ToolPreloadService | None: + """Get the tool preload service instance if ``config`` enables preload.""" global _tool_preload_service - config = get_default_config() - if not config.preload_tools: _logger.info("Tool preload is disabled in configuration") return None diff --git a/openhands-agent-server/openhands/agent_server/tool_router.py b/openhands-agent-server/openhands/agent_server/tool_router.py index e2df6e2f6c5..59c23fbbf07 100644 --- a/openhands-agent-server/openhands/agent_server/tool_router.py +++ b/openhands-agent-server/openhands/agent_server/tool_router.py @@ -8,7 +8,7 @@ # Registered only in the agent-server: the tool needs a server to create the # child on, so it is deliberately absent from get_default_tools(). import openhands.tools.child_conversation # noqa: E402, F401 -from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_config from openhands.agent_server.launch import ( can_probe_tools, configured_browser_available, @@ -53,7 +53,7 @@ async def get_tool_catalog(request: Request) -> ToolCatalogResponse: Clients offer the ``user_selectable`` entries; ``usable`` says whether the runtime conversations run in can run the tool. """ - config: Config = request.app.state.config + config = get_config(request) entries = await asyncio.to_thread( list_tool_catalog, check_usable=can_probe_tools(config) ) diff --git a/openhands-agent-server/openhands/agent_server/vscode_router.py b/openhands-agent-server/openhands/agent_server/vscode_router.py index 8bfc7b26fe0..b32c14fb987 100644 --- a/openhands-agent-server/openhands/agent_server/vscode_router.py +++ b/openhands-agent-server/openhands/agent_server/vscode_router.py @@ -1,8 +1,10 @@ """VSCode router for agent server API endpoints.""" -from fastapi import APIRouter, HTTPException +from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel +from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_config from openhands.agent_server.vscode_service import get_vscode_service from openhands.sdk.logger import get_logger @@ -20,7 +22,9 @@ class VSCodeUrlResponse(BaseModel): @vscode_router.get("/url", response_model=VSCodeUrlResponse) async def get_vscode_url( - base_url: str | None = None, workspace_dir: str = "workspace" + base_url: str | None = None, + workspace_dir: str = "workspace", + config: Config = Depends(get_config), ) -> VSCodeUrlResponse: """Get the VSCode URL with authentication token. @@ -35,7 +39,7 @@ async def get_vscode_url( Returns: VSCode URL with token if available, None otherwise """ - vscode_service = get_vscode_service() + vscode_service = get_vscode_service(config) if vscode_service is None: raise HTTPException( status_code=503, @@ -53,13 +57,15 @@ async def get_vscode_url( @vscode_router.get("/status") -async def get_vscode_status() -> dict[str, bool | str]: +async def get_vscode_status( + config: Config = Depends(get_config), +) -> dict[str, bool | str]: """Get the VSCode server status. Returns: Dictionary with running status and enabled status """ - vscode_service = get_vscode_service() + vscode_service = get_vscode_service(config) if vscode_service is None: return { "running": False, diff --git a/openhands-agent-server/openhands/agent_server/vscode_service.py b/openhands-agent-server/openhands/agent_server/vscode_service.py index e454655e013..66fb3caae3a 100644 --- a/openhands-agent-server/openhands/agent_server/vscode_service.py +++ b/openhands-agent-server/openhands/agent_server/vscode_service.py @@ -8,6 +8,7 @@ from pathlib import Path from urllib.parse import quote, urlencode +from openhands.agent_server.config import Config from openhands.sdk.logger import get_logger from openhands.sdk.utils import sanitized_env @@ -300,20 +301,17 @@ async def _wait_for_startup(self) -> None: _vscode_service: VSCodeService | None = None -def get_vscode_service() -> VSCodeService | None: - """Get the global VSCode service instance. +def get_vscode_service(config: Config) -> VSCodeService | None: + """Get the global VSCode service instance, creating it from ``config``. + + The service outlives a server (re)initialisation, so ``config`` only + decides how it is created the first time. Returns: VSCode service instance if enabled, None if disabled """ global _vscode_service if _vscode_service is None: - from openhands.agent_server.config import ( - get_default_config, - ) - - config = get_default_config() - if not config.enable_vscode: logger.info("VSCode is disabled in configuration") return None diff --git a/openhands-agent-server/openhands/agent_server/workspaces_router.py b/openhands-agent-server/openhands/agent_server/workspaces_router.py index 5ee50edc587..8ffbc9285d3 100644 --- a/openhands-agent-server/openhands/agent_server/workspaces_router.py +++ b/openhands-agent-server/openhands/agent_server/workspaces_router.py @@ -11,12 +11,11 @@ from fastapi import APIRouter, HTTPException, Query, Request, status from pydantic import BaseModel, ConfigDict, Field -from openhands.agent_server._secrets_exposure import get_config +from openhands.agent_server.dependencies import get_stores from openhands.agent_server.persistence import ( PersistedWorkspaces, WorkspaceItem, WorkspaceParentItem, - get_workspaces_store, ) from openhands.sdk.logger import get_logger @@ -62,8 +61,7 @@ def _to_response(persisted: PersistedWorkspaces) -> WorkspacesListResponse: ) async def list_workspaces(request: Request) -> WorkspacesListResponse: """Return the saved workspaces and workspace parents.""" - config = get_config(request) - store = get_workspaces_store(config) + store = get_stores(request).workspaces try: persisted = store.load() except (OSError, PermissionError): @@ -92,8 +90,7 @@ async def add_workspaces( request: Request, payload: AddWorkspacesRequest ) -> WorkspacesListResponse: """Append workspaces to the saved list. Idempotent — dedupes by ``path``.""" - config = get_config(request) - store = get_workspaces_store(config) + store = get_stores(request).workspaces def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: # Dedupe against the existing list AND within the incoming payload, @@ -133,8 +130,7 @@ async def delete_workspace( path: Annotated[str, Query(description="Absolute workspace path to remove")], ) -> DeleteResponse: """Remove a workspace by its absolute path.""" - config = get_config(request) - store = get_workspaces_store(config) + store = get_stores(request).workspaces removed = False @@ -172,8 +168,7 @@ async def add_workspace_parents( request: Request, payload: AddWorkspaceParentsRequest ) -> WorkspacesListResponse: """Append workspace parents. Idempotent — dedupes by ``path``.""" - config = get_config(request) - store = get_workspaces_store(config) + store = get_stores(request).workspaces def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: # Dedupe against the existing list AND within the incoming payload — @@ -214,8 +209,7 @@ async def delete_workspace_parent( path: Annotated[str, Query(description="Absolute workspace parent path to remove")], ) -> DeleteResponse: """Remove a workspace parent by its absolute path.""" - config = get_config(request) - store = get_workspaces_store(config) + store = get_stores(request).workspaces removed = False diff --git a/scripts/build_config_template.py b/scripts/build_config_template.py index 4e2a6e99605..beaf4742b8c 100644 --- a/scripts/build_config_template.py +++ b/scripts/build_config_template.py @@ -5,7 +5,7 @@ import argparse -from openhands.agent_server.config import get_default_config +from openhands.agent_server.config import load_config from openhands.agent_server.env_parser import to_env @@ -17,5 +17,5 @@ args = parser.parse_args() print(f"🛠️ Building: {args.file}") with open(args.file, "w") as f: - content = to_env(get_default_config(), "OH") + content = to_env(load_config(), "OH") f.write(content) diff --git a/tests/agent_server/canvas_extensions/test_canvas_extension_backend.py b/tests/agent_server/canvas_extensions/test_canvas_extension_backend.py index 523a83d9354..b5e82d57516 100644 --- a/tests/agent_server/canvas_extensions/test_canvas_extension_backend.py +++ b/tests/agent_server/canvas_extensions/test_canvas_extension_backend.py @@ -947,7 +947,7 @@ async def test_revision_bound_store_secret_lifecycle(tmp_path: Path, monkeypatch install_canvas_extension(str(source), installed_dir=installed) store = FileSecretsStore(tmp_path / "secrets") manager = CanvasExtensionBackendManager( - installed, tmp_path / "state", secrets_store=store + installed, tmp_path / "state", secrets_store=lambda: store ) name = "my-extension" revision = manager.revision(name) @@ -1138,7 +1138,7 @@ async def test_enable_orchestrates_backend_and_retains_revision_consent( install_canvas_extension(str(source), installed_dir=installed) store = FileSecretsStore(tmp_path / "secrets") manager = CanvasExtensionBackendManager( - installed, tmp_path / "state", secrets_store=store + installed, tmp_path / "state", secrets_store=lambda: store ) app = FastAPI() app.state.canvas_extension_backend_manager = manager diff --git a/tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py b/tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py index ebf1ca10700..61ece65eb0b 100644 --- a/tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py +++ b/tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py @@ -35,6 +35,7 @@ app_backend_bridge_router, ) from openhands.agent_server.config import Config +from tests.agent_server.conftest import install_test_context CONTROL_KEY = "control-key" @@ -203,11 +204,14 @@ async def protocol_endpoint(websocket: WebSocket) -> None: bridge_port = _free_port() public_url = f"https://127.0.0.1:{bridge_port}" app = FastAPI() - app.state.config = Config( - session_api_keys=[CONTROL_KEY], - allow_cors_origins=[CANVAS_ORIGIN], - app_backend_public_url=public_url, - trust_forwarded_headers=trust_forwarded_headers, + install_test_context( + app, + Config( + session_api_keys=[CONTROL_KEY], + allow_cors_origins=[CANVAS_ORIGIN], + app_backend_public_url=public_url, + trust_forwarded_headers=trust_forwarded_headers, + ), ) app.state.app_backend_session_store = AppBackendSessionStore( ttl_seconds=ttl_seconds @@ -579,10 +583,13 @@ async def echo() -> JSONResponse: bridge_port = _free_port() public_url = f"{public_scheme}://127.0.0.1:{bridge_port}" app = FastAPI() - app.state.config = Config( - session_api_keys=[CONTROL_KEY], - allow_cors_origins=[CANVAS_ORIGIN], - app_backend_public_url=public_url, + install_test_context( + app, + Config( + session_api_keys=[CONTROL_KEY], + allow_cors_origins=[CANVAS_ORIGIN], + app_backend_public_url=public_url, + ), ) app.state.app_backend_session_store = AppBackendSessionStore() app.state.canvas_extension_backend_manager = _ReadyBackendManager( diff --git a/tests/agent_server/conftest.py b/tests/agent_server/conftest.py index 686c2b00541..dda00dac08e 100644 --- a/tests/agent_server/conftest.py +++ b/tests/agent_server/conftest.py @@ -1,15 +1,27 @@ import os from collections.abc import Callable +from dataclasses import replace from types import SimpleNamespace from typing import Any import pytest +from starlette.applications import Starlette +from starlette.testclient import TestClient +from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.config import ( V0_SESSION_API_KEY_ENV, V1_SESSION_API_KEY_ENV, + Config, +) +from openhands.agent_server.conversation_registry import ConversationRegistry +from openhands.agent_server.conversation_service import ConversationService +from openhands.agent_server.persistence import ServerStores +from openhands.agent_server.server_context import ( + ServerContext, + install_server_context, + server_context_of, ) -from openhands.agent_server.persistence import reset_stores from openhands.sdk.mcp.config import MCPServer @@ -81,19 +93,17 @@ def isolate_session_api_key_env(): def isolate_persistence_dir(tmp_path, monkeypatch): """Keep the developer's real ``~/.openhands`` out of every test. - ``_start_conversation`` reads the settings store on every launch, not just - the ``agent_profile_id`` one, and ``get_settings_store`` falls back to - ``~/.openhands`` when ``OH_PERSISTENCE_DIR`` is unset. Tests that build a - ``ConversationService`` directly never initialise the singleton, so without - this they would pick up whatever settings.json happens to be on the host and - pass or fail depending on the machine. + A server's stores (``ServerStores``) fall back to ``~/.openhands`` when + ``OH_PERSISTENCE_DIR`` is unset, so without this an app built by a test + would pick up whatever settings.json happens to be on the host and pass or + fail depending on the machine. """ monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / ".openhands")) - # The singleton getters resolve ``OH_PERSISTENCE_DIR`` per call, but three - # SDK defaults are frozen into module constants at import time and therefore - # ignore the env var above once the module is already imported (which it - # always is here). Pin them too, or a store constructed with no explicit - # ``base_dir`` still lands in the developer's real ``~/.openhands``. + # ``ServerStores`` resolves ``OH_PERSISTENCE_DIR`` when it is built, but + # three SDK defaults are frozen into module constants at import time and + # therefore ignore the env var above once the module is already imported + # (which it always is here). Pin them too, or a store constructed with no + # explicit ``base_dir`` still lands in the developer's real ``~/.openhands``. from openhands.sdk.llm import llm_profile_store from openhands.sdk.profiles import agent_profile_store from openhands.sdk.skills import installed as installed_skills @@ -110,8 +120,72 @@ def isolate_persistence_dir(tmp_path, monkeypatch): "DEFAULT_INSTALLED_SKILLS_DIR", persistence_dir / "skills" / "installed", ) - reset_stores() - try: - yield - finally: - reset_stores() + + +@pytest.fixture +def server_stores() -> ServerStores: + """The stores of a server with the default config, in the test's own dir. + + For tests that build a ``ConversationService`` (or call a helper) directly + rather than through an app: pass these in, and read or seed them, instead + of expecting a process-wide store. + """ + return ServerStores.from_config(Config()) + + +def client_context(client: TestClient) -> ServerContext: + """The server context of the app behind a ``TestClient``. + + Read it after the request (or the ``with`` block) whose effect a test + checks: the lifespan and ``POST /api/init`` each install a new one. + """ + app = client.app + assert isinstance(app, Starlette) + return server_context_of(app) + + +def update_test_context(app: Starlette, **changes: Any) -> ServerContext: + """Rebind part of the context ``app`` already has, keeping the rest. + + For an app from ``create_app`` whose test swaps in a config or a service: + ``update_test_context(app, config=other)`` leaves its stores, registry and + services where they are. + """ + context = replace(server_context_of(app), **changes) + install_server_context(app, context) + return context + + +def install_test_context( + app: Starlette, + config: Config | None = None, + *, + conversation_registry: ConversationRegistry | None = None, + conversation_service: ConversationService | None = None, + bash_event_service: BashEventService | None = None, +) -> ServerContext: + """Give a hand-built app (a bare ``FastAPI`` plus routers) a server context. + + ``create_app`` installs one itself; this is for tests that mount a router + on their own app and still need what its handlers read from the context. + Services passed here stand in for the ones the lifespan would start, and a + registry passed here brings its own config and stores. Without one the app + gets the plain host-local registry whatever runtime ``config`` names, so a + router test can describe a Docker deployment without a Docker registry. + """ + if conversation_registry is not None: + config = config if config is not None else conversation_registry.config + stores = conversation_registry.stores + else: + config = config if config is not None else Config() + stores = ServerStores.from_config(config) + conversation_registry = ConversationRegistry(config, stores) + context = ServerContext( + config=config, + stores=stores, + conversation_registry=conversation_registry, + conversation_service=conversation_service, + bash_event_service=bash_event_service, + ) + install_server_context(app, context) + return context diff --git a/tests/agent_server/docker_runtime/test_disk_budget.py b/tests/agent_server/docker_runtime/test_disk_budget.py index bee7a4157a4..02c59d776be 100644 --- a/tests/agent_server/docker_runtime/test_disk_budget.py +++ b/tests/agent_server/docker_runtime/test_disk_budget.py @@ -12,19 +12,19 @@ ConversationContainer, DockerConversationRegistry, ) +from openhands.agent_server.persistence import ServerStores from openhands.agent_server.storage import reclaimer as reclaimer_module, selectors def registry(tmp_path, monkeypatch) -> DockerConversationRegistry: monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "persistence")) - return DockerConversationRegistry( - Config( - conversations_path=tmp_path / "conversations", - workspace_path=tmp_path / "workspaces", - secret_key=SecretStr("outer-key"), - conversation_storage_disk_budget=0.8, - ) + config = Config( + conversations_path=tmp_path / "conversations", + workspace_path=tmp_path / "workspaces", + secret_key=SecretStr("outer-key"), + conversation_storage_disk_budget=0.8, ) + return DockerConversationRegistry(config, ServerStores.from_config(config)) def git(repo: Path, *args: str) -> None: @@ -227,12 +227,11 @@ async def test_disk_budget_loop_runs_only_when_configured(tmp_path, monkeypatch) assert runtime.reclaimer._maintenance is None monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "persistence")) - unset = DockerConversationRegistry( - Config( - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("outer-key"), - ) + config = Config( + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("outer-key"), ) + unset = DockerConversationRegistry(config, ServerStores.from_config(config)) monkeypatch.setattr(unset, "cleanup_stale_containers", lambda: None) await unset.start() assert unset.reclaimer._maintenance is None diff --git a/tests/agent_server/docker_runtime/test_hybrid.py b/tests/agent_server/docker_runtime/test_hybrid.py index 72ffe3ab9ff..1e41031170b 100644 --- a/tests/agent_server/docker_runtime/test_hybrid.py +++ b/tests/agent_server/docker_runtime/test_hybrid.py @@ -1,8 +1,8 @@ import json -from types import SimpleNamespace from uuid import uuid4 import pytest +from fastapi import FastAPI from fastapi.testclient import TestClient from pydantic import SecretStr @@ -12,6 +12,16 @@ HybridConversationRegistry, RuntimeDispatchMiddleware, ) +from openhands.agent_server.persistence import ServerStores +from openhands.agent_server.server_context import server_context_of +from tests.agent_server.conftest import install_test_context + + +def _app_with(registry: HybridConversationRegistry) -> FastAPI: + """An app whose server context holds ``registry``.""" + app = FastAPI() + install_test_context(app, conversation_registry=registry) + return app @pytest.fixture @@ -27,7 +37,8 @@ def config(tmp_path, monkeypatch): def test_selectable_mode_serves_both_runtimes(config): app = create_app(config) - assert isinstance(app.state.conversation_registry, HybridConversationRegistry) + registry = server_context_of(app).conversation_registry + assert isinstance(registry, HybridConversationRegistry) paths = [getattr(route, "path", "") for route in app.routes] assert any( p.startswith("/api/conversations/{runtime_conversation_id}/") for p in paths @@ -55,7 +66,7 @@ async def receive(): scope = { "root_path": "", - "app": SimpleNamespace(state=SimpleNamespace(conversation_registry=registry)), + "app": _app_with(registry), **scope, } await RuntimeDispatchMiddleware(app)(scope, receive, None) # type: ignore[arg-type] @@ -64,7 +75,7 @@ async def receive(): @pytest.mark.asyncio async def test_dispatch_routes_conversations_by_runtime(config): - registry = HybridConversationRegistry(config) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) docker_id, local_id = uuid4(), uuid4() registry.provisioning.create(docker_id) @@ -95,7 +106,7 @@ def http(method, path): ], ) async def test_dispatch_routes_fastapi_uuid_forms(config, uuid_format): - registry = HybridConversationRegistry(config) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) docker_id = uuid4() registry.provisioning.create(docker_id) path_id = uuid_format.format(docker_id) @@ -110,7 +121,7 @@ async def test_dispatch_routes_fastapi_uuid_forms(config, uuid_format): @pytest.mark.asyncio async def test_dispatch_leaves_invalid_uuid_for_route_validation(config): - registry = HybridConversationRegistry(config) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) path = "/api/conversations/not-a-uuid/run" seen = await _dispatch(registry, {"type": "http", "method": "POST", "path": path}) assert seen["path"] == path @@ -133,9 +144,8 @@ async def test_dispatch_leaves_invalid_uuid_for_route_validation(config): async def test_dispatch_start_uses_requested_runtime( config, default, requested, expected ): - registry = HybridConversationRegistry( - config.model_copy(update={"conversation_runtime": default}) - ) + config = config.model_copy(update={"conversation_runtime": default}) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) body = json.dumps( {"conversation_runtime": requested} if requested else {"x": 1} ).encode() @@ -147,9 +157,8 @@ async def test_dispatch_start_uses_requested_runtime( @pytest.mark.asyncio async def test_dispatch_start_resumes_existing_docker_conversation(config): - registry = HybridConversationRegistry( - config.model_copy(update={"conversation_runtime": "local"}) - ) + config = config.model_copy(update={"conversation_runtime": "local"}) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) docker_id = uuid4() registry.provisioning.create(docker_id) body = json.dumps({"conversation_id": str(docker_id)}).encode() @@ -162,7 +171,9 @@ async def test_dispatch_start_resumes_existing_docker_conversation(config): def test_start_existing_docker_conversation_as_local_is_rejected(config): app = create_app(config.model_copy(update={"conversation_runtime": "local"})) docker_id = uuid4() - app.state.conversation_registry.provisioning.create(docker_id) + registry = server_context_of(app).conversation_registry + assert isinstance(registry, HybridConversationRegistry) + registry.provisioning.create(docker_id) response = TestClient(app).post( "/api/conversations", json={"conversation_id": str(docker_id), "conversation_runtime": "local"}, @@ -180,7 +191,7 @@ def test_start_existing_docker_conversation_as_local_is_rejected(config): ], ) async def test_dispatch_routes_docker_sockets(config, uuid_format): - registry = HybridConversationRegistry(config) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) docker_id = uuid4() registry.provisioning.create(docker_id) path_id = uuid_format.format(docker_id) diff --git a/tests/agent_server/docker_runtime/test_mediation.py b/tests/agent_server/docker_runtime/test_mediation.py index 23d575e3d37..6a4e16b57bd 100644 --- a/tests/agent_server/docker_runtime/test_mediation.py +++ b/tests/agent_server/docker_runtime/test_mediation.py @@ -14,12 +14,7 @@ from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry from openhands.agent_server.docker_runtime.routers import _prepare_forward from openhands.agent_server.launch import launch_source, target_launch_runtime -from openhands.agent_server.persistence import ( - PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_settings_store, -) +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.sdk import LLM, Agent from openhands.sdk.context import AgentContext from openhands.sdk.conversation.request import ( @@ -47,10 +42,20 @@ def config(tmp_path, monkeypatch) -> Config: ) +def _registry(runtime_config: Config) -> DockerConversationRegistry: + """The part of a registry ``_prepare_forward`` reads: config and stores.""" + return cast( + DockerConversationRegistry, + SimpleNamespace( + config=runtime_config, stores=ServerStores.from_config(runtime_config) + ), + ) + + async def _forward( request: StartConversationRequest, runtime_config: Config, *, existing=False ) -> tuple[dict[str, Any], Any, RuntimeIdentity]: - registry = cast(DockerConversationRegistry, SimpleNamespace(config=runtime_config)) + registry = _registry(runtime_config) body = request.model_dump(mode="json", context={"expose_secrets": True}) payload, launched, _, _ = await _prepare_forward(body, registry, existing=existing) provisioning = RuntimeProvisioningStore(runtime_config) @@ -62,7 +67,7 @@ async def _forward( @pytest.mark.parametrize("worktree", [False, True]) async def test_forward_preserves_worktree_flag(tmp_path, monkeypatch, worktree): runtime_config = config(tmp_path, monkeypatch) - registry = cast(DockerConversationRegistry, SimpleNamespace(config=runtime_config)) + registry = _registry(runtime_config) request = StartConversationRequest( workspace=LocalWorkspace(working_dir="/workspace"), agent=Agent(llm=LLM(model="test", api_key=SecretStr("model-key"))), @@ -166,7 +171,7 @@ async def test_every_encrypted_field_reaches_the_container_decryptable( secrets_encrypted=True, ) body = request.model_dump(mode="json", context={"cipher": runtime_config.cipher}) - registry = cast(DockerConversationRegistry, SimpleNamespace(config=runtime_config)) + registry = _registry(runtime_config) payload, _, _, _ = await _prepare_forward(body, registry, existing=False) identity = RuntimeProvisioningStore(runtime_config).create(uuid4()) @@ -184,7 +189,8 @@ async def test_the_host_resolves_a_profile_and_the_container_finalizes_it( tmp_path, monkeypatch ): runtime_config = config(tmp_path, monkeypatch) - get_llm_profile_store().save( + stores = ServerStores.from_config(runtime_config) + stores.llm_profiles.save( "docker-test-model", LLM(model="test", api_key=SecretStr("model-key")), include_secrets=True, @@ -196,7 +202,7 @@ async def test_the_host_resolves_a_profile_and_the_container_finalizes_it( mcp_server_refs=[], secret_refs=["ALLOWED"], ) - get_agent_profile_store().save(profile) + stores.agent_profiles.save(profile) monkeypatch.setattr( "openhands.agent_server.launch.discover_profile_skills", lambda: [] ) @@ -246,17 +252,18 @@ async def test_the_host_forwards_its_memory_preference_and_no_llm_override( tmp_path, monkeypatch ): runtime_config = config(tmp_path, monkeypatch) - get_settings_store(runtime_config).save( + stores = ServerStores.from_config(runtime_config) + stores.settings.save( PersistedSettings( agent_settings=OpenHandsAgentSettings( agent_context=AgentContext(load_memory=True) ) ) ) - get_llm_profile_store().save("fast", LLM(model="fast-model")) - get_llm_profile_store().save("slow", LLM(model="slow-model")) + stores.llm_profiles.save("fast", LLM(model="fast-model")) + stores.llm_profiles.save("slow", LLM(model="slow-model")) profile = OpenHandsAgentProfile(name="p", llm_profile_ref="slow") - get_agent_profile_store().save(profile) + stores.agent_profiles.save(profile) monkeypatch.setattr( "openhands.agent_server.launch.discover_profile_skills", lambda: [] ) diff --git a/tests/agent_server/docker_runtime/test_proxy.py b/tests/agent_server/docker_runtime/test_proxy.py index 8692e5801a4..5b45d1200c1 100644 --- a/tests/agent_server/docker_runtime/test_proxy.py +++ b/tests/agent_server/docker_runtime/test_proxy.py @@ -5,6 +5,7 @@ from starlette.websockets import WebSocket from openhands.agent_server.docker_runtime.proxy import _bridge_websocket_loop +from openhands.agent_server.persistence import ServerStores class Upstream: @@ -197,12 +198,11 @@ async def __aiter__(self): ) monkeypatch.setattr(proxy.httpx, "AsyncClient", lambda **kwargs: client) monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "persistence")) - registry = DockerConversationRegistry( - Config( - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("outer-key"), - ) + config = Config( + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("outer-key"), ) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) request, target = request_and_target() conversation_id = uuid4() diff --git a/tests/agent_server/docker_runtime/test_registry.py b/tests/agent_server/docker_runtime/test_registry.py index 0d095dbd860..af19a776de1 100644 --- a/tests/agent_server/docker_runtime/test_registry.py +++ b/tests/agent_server/docker_runtime/test_registry.py @@ -20,6 +20,7 @@ DockerConversationRegistry, ) from openhands.agent_server.models import StartConversationRequest +from openhands.agent_server.persistence import ServerStores from openhands.agent_server.storage import trash as trash_module from openhands.sdk import LLM, Agent, Message, TextContent from openhands.sdk.conversation.request import ConversationContainerOptions @@ -32,14 +33,15 @@ def registry( tmp_path, monkeypatch, idle_ttl: float | None = 1200, **config ) -> DockerConversationRegistry: monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "persistence")) + server_config = Config( + conversations_path=tmp_path / "conversations", + workspace_path=tmp_path / "workspaces", + secret_key=SecretStr("outer-key"), + conversation_idle_ttl_seconds=idle_ttl, + **config, + ) return DockerConversationRegistry( - Config( - conversations_path=tmp_path / "conversations", - workspace_path=tmp_path / "workspaces", - secret_key=SecretStr("outer-key"), - conversation_idle_ttl_seconds=idle_ttl, - **config, - ) + server_config, ServerStores.from_config(server_config) ) diff --git a/tests/agent_server/docker_runtime/test_retention.py b/tests/agent_server/docker_runtime/test_retention.py index 901345412cc..eff6e4e1ef8 100644 --- a/tests/agent_server/docker_runtime/test_retention.py +++ b/tests/agent_server/docker_runtime/test_retention.py @@ -15,6 +15,7 @@ RuntimeArchivedError, ) from openhands.agent_server.models import ConversationRuntimeStatus +from openhands.agent_server.persistence import ServerStores DAY = 86400 @@ -24,14 +25,13 @@ def registry( tmp_path, monkeypatch, days: float | None = 7 ) -> DockerConversationRegistry: monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "persistence")) - return DockerConversationRegistry( - Config( - conversations_path=tmp_path / "conversations", - workspace_path=tmp_path / "workspaces", - secret_key=SecretStr("outer-key"), - conversation_storage_retention_days=days, - ) + config = Config( + conversations_path=tmp_path / "conversations", + workspace_path=tmp_path / "workspaces", + secret_key=SecretStr("outer-key"), + conversation_storage_retention_days=days, ) + return DockerConversationRegistry(config, ServerStores.from_config(config)) def provision( diff --git a/tests/agent_server/docker_runtime/test_routes.py b/tests/agent_server/docker_runtime/test_routes.py index f207401cb2c..4d3021ed052 100644 --- a/tests/agent_server/docker_runtime/test_routes.py +++ b/tests/agent_server/docker_runtime/test_routes.py @@ -28,10 +28,22 @@ ) from openhands.agent_server.event_router import event_read_router from openhands.agent_server.models import UpdateSecretsRequest +from openhands.agent_server.persistence import ServerStores +from openhands.agent_server.server_context import server_context_of from openhands.sdk.conversation.request import ConversationContainerOptions from openhands.sdk.git.utils import run_git_command from openhands.sdk.profiles.agent_profile import LaunchedAgentProfile from openhands.sdk.secret import LookupSecret +from tests.agent_server.conftest import install_test_context + + +def _app_with(registry: DockerConversationRegistry, service: AsyncMock) -> FastAPI: + """An app whose server context holds ``registry`` and ``service``.""" + app = FastAPI() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) + return app def test_docker_mode_replaces_local_conversation_execution_routes(tmp_path): @@ -113,8 +125,13 @@ def test_root_conversation_proxy_preserves_canonical_path(tmp_path, monkeypatch) secret_key=SecretStr("outer-key"), ) app = FastAPI() - app.state.conversation_registry = DockerConversationRegistry(config) - app.state.conversation_service = AsyncMock() + install_test_context( + app, + conversation_registry=DockerConversationRegistry( + config, ServerStores.from_config(config) + ), + conversation_service=AsyncMock(), + ) app.include_router(docker_conversation_router, prefix="/api") conversation_id = uuid4() captured = {} @@ -156,9 +173,11 @@ def test_worktree_start_forwards_only_mounted_host_paths( secret_key=SecretStr("outer-key"), ) app = FastAPI() - registry = DockerConversationRegistry(config) - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") monkeypatch.setattr( registry, @@ -212,10 +231,12 @@ async def stop(conversation_id): stopped.append(conversation_id) app = FastAPI() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) registry.stop = stop - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") with TestClient(app) as client: response = client.post( @@ -229,9 +250,7 @@ async def stop(conversation_id): == 204 ) assert stopped == [conversation_id] - app.state.conversation_service.refresh_persisted_conversation.assert_awaited_once_with( - conversation_id - ) + service.refresh_persisted_conversation.assert_awaited_once_with(conversation_id) def test_runtime_release_prunes_cache_but_keeps_conversation_state( @@ -242,7 +261,7 @@ def test_runtime_release_prunes_cache_but_keeps_conversation_state( conversations_path=tmp_path / "conversations", secret_key=SecretStr("outer-key"), ) - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) conversation_id = uuid4() registry.provisioning.create(conversation_id) conversation_dir = registry.conversation_dir(conversation_id) @@ -264,8 +283,10 @@ def test_runtime_release_prunes_cache_but_keeps_conversation_state( ) app = FastAPI() - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") with TestClient(app) as client: response = client.delete(f"/api/conversations/{conversation_id}/runtime") @@ -286,7 +307,7 @@ def test_runtime_info_marks_legacy_local_conversation_non_resumable( conversations_path=tmp_path / "conversations", secret_key=SecretStr("outer-key"), ) - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) legacy_id = uuid4() legacy_dir = registry.conversation_dir(legacy_id) legacy_dir.mkdir(parents=True) @@ -299,7 +320,7 @@ def test_runtime_info_marks_legacy_local_conversation_non_resumable( (docker_dir / "meta.json").write_text("{}") app = FastAPI() - app.state.conversation_registry = registry + install_test_context(app, conversation_registry=registry) app.include_router(docker_conversation_router, prefix="/api") with TestClient(app) as client: legacy = client.get(f"/api/conversations/{legacy_id}/runtime") @@ -327,17 +348,17 @@ def test_docker_event_history_reads_persistence_without_starting_a_container( secret_key=SecretStr("outer-key"), ) conversation_id = uuid4() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) registry.provisioning.create(conversation_id) registry.get_or_create = AsyncMock() event_service = SimpleNamespace( search_events=AsyncMock(return_value={"items": [], "next_page_id": None}) ) app = FastAPI() - app.state.conversation_registry = registry - app.state.conversation_service = SimpleNamespace( - get_persisted_event_service=AsyncMock(return_value=event_service), - get_event_service=AsyncMock(), + service = AsyncMock() + service.get_persisted_event_service.return_value = event_service + install_test_context( + app, conversation_registry=registry, conversation_service=service ) app.include_router(event_read_router, prefix="/api") @@ -348,10 +369,8 @@ def test_docker_event_history_reads_persistence_without_starting_a_container( assert response.status_code == 200 assert response.json() == {"items": [], "next_page_id": None} - app.state.conversation_service.get_persisted_event_service.assert_awaited_once_with( - conversation_id - ) - app.state.conversation_service.get_event_service.assert_not_awaited() + service.get_persisted_event_service.assert_awaited_once_with(conversation_id) + service.get_event_service.assert_not_awaited() registry.get_or_create.assert_not_awaited() @@ -362,7 +381,7 @@ def test_delete_stops_runtime_before_removing_outer_owned_state(tmp_path, monkey secret_key=SecretStr("outer-key"), ) conversation_id = uuid4() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) registry.provisioning.create(conversation_id) conversation_dir = registry.conversation_dir(conversation_id) conversation_dir.mkdir(parents=True) @@ -372,17 +391,17 @@ def test_delete_stops_runtime_before_removing_outer_owned_state(tmp_path, monkey registry.stop = AsyncMock() app = FastAPI() - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") with TestClient(app) as client: response = client.delete(f"/api/conversations/{conversation_id}") assert response.status_code == 200 registry.stop.assert_awaited_once_with(conversation_id) - app.state.conversation_service.refresh_persisted_conversation.assert_awaited_once_with( - conversation_id - ) + service.refresh_persisted_conversation.assert_awaited_once_with(conversation_id) assert not conversation_dir.exists() assert not runtime_dir.exists() @@ -407,7 +426,7 @@ def test_delete_unregisters_host_worktree_before_removing_runtime( secret_key=SecretStr("outer-key"), ) conversation_id = uuid4() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) identity = registry.provisioning.create( conversation_id, workspace_path=repository ).model_copy(update={"worktree_repository_path": repository}) @@ -442,8 +461,10 @@ def test_delete_unregisters_host_worktree_before_removing_runtime( registry.stop = AsyncMock() app = FastAPI() - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") with TestClient(app) as client: response = client.delete(f"/api/conversations/{conversation_id}") @@ -479,7 +500,7 @@ def test_cleanup_worktree_is_idempotent(tmp_path, monkeypatch): secret_key=SecretStr("outer-key"), ) conversation_id = uuid4() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) identity = registry.provisioning.create( conversation_id, workspace_path=repository ).model_copy(update={"worktree_repository_path": repository}) @@ -525,7 +546,7 @@ def test_failed_delete_keeps_docker_ownership_until_state_is_removed( secret_key=SecretStr("outer-key"), ) conversation_id = uuid4() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) registry.provisioning.create(conversation_id) conversation_dir = registry.conversation_dir(conversation_id) conversation_dir.mkdir(parents=True) @@ -540,8 +561,10 @@ def fail_conversation_delete(path): monkeypatch.setattr(routers, "safe_rmtree", fail_conversation_delete) app = FastAPI() - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") with TestClient(app, raise_server_exceptions=False) as client: @@ -559,7 +582,7 @@ async def test_delete_blocks_runtime_restart_while_container_stops(tmp_path): secret_key=SecretStr("outer-key"), ) conversation_id = uuid4() - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) registry.provisioning.create(conversation_id) conversation_dir = registry.conversation_dir(conversation_id) conversation_dir.mkdir(parents=True) @@ -579,12 +602,7 @@ async def stop(conversation_id): "path": f"/api/conversations/{conversation_id}", "query_string": b"", "headers": [], - "app": SimpleNamespace( - state=SimpleNamespace( - conversation_registry=registry, - conversation_service=AsyncMock(), - ) - ), + "app": _app_with(registry, AsyncMock()), } ) @@ -607,7 +625,7 @@ async def test_secret_updates_are_materialized_and_profile_scoped( conversations_path=tmp_path / "conversations", secret_key=SecretStr("outer-key"), ) - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) conversation_id = uuid4() identity = registry.provisioning.create(conversation_id).model_copy( update={ @@ -660,6 +678,7 @@ async def receive(): "more_body": False, } + service = AsyncMock() request = Request( { "type": "http", @@ -667,12 +686,7 @@ async def receive(): "path": f"/api/conversations/{conversation_id}/secrets", "query_string": b"", "headers": [(b"content-type", b"application/json")], - "app": SimpleNamespace( - state=SimpleNamespace( - conversation_registry=registry, - conversation_service=AsyncMock(), - ) - ), + "app": _app_with(registry, service), }, receive, ) @@ -682,9 +696,7 @@ async def receive(): assert set(forwarded.secrets) == {"ALLOWED"} assert forwarded.secrets["ALLOWED"].get_value() == "resolved-ALLOWED" assert looked_up == ["http://outer/api/settings/secrets/ALLOWED"] - request.app.state.conversation_service.refresh_persisted_conversation.assert_awaited_once_with( - conversation_id - ) + service.refresh_persisted_conversation.assert_awaited_once_with(conversation_id) def _docker_start_app( @@ -696,10 +708,12 @@ def _docker_start_app( workspace_path=tmp_path / "workspaces", secret_key=SecretStr("outer-key"), ) - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) app = FastAPI() - app.state.conversation_registry = registry - app.state.conversation_service = AsyncMock() + service = AsyncMock() + install_test_context( + app, conversation_registry=registry, conversation_service=service + ) app.include_router(docker_conversation_router, prefix="/api") return app, registry @@ -775,9 +789,9 @@ async def post(self, url, **kwargs): assert response.status_code == 201 assert response.json()["id"] == str(conversation_id) assert not registry.has_active_leases(conversation_id) - app.state.conversation_service.refresh_persisted_conversation.assert_awaited_once_with( - conversation_id - ) + service = server_context_of(app).conversation_service + assert isinstance(service, AsyncMock) + service.refresh_persisted_conversation.assert_awaited_once_with(conversation_id) def test_a_symlinked_conversation_dir_is_rejected(tmp_path, monkeypatch): diff --git a/tests/agent_server/docker_runtime/test_storage_scenario.py b/tests/agent_server/docker_runtime/test_storage_scenario.py index 0d6e9bbef9e..e666892d97b 100644 --- a/tests/agent_server/docker_runtime/test_storage_scenario.py +++ b/tests/agent_server/docker_runtime/test_storage_scenario.py @@ -16,12 +16,10 @@ import pytest from fastapi.testclient import TestClient -from openhands.agent_server import ( - config as config_module, - conversation_service as service_module, -) from openhands.agent_server.api import create_app from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry +from openhands.agent_server.server_context import server_context_of +from tests.agent_server.conftest import client_context pytestmark = pytest.mark.docker_live @@ -103,21 +101,22 @@ def start(**env: str) -> Iterator[TestClient]: **env, }.items(): monkeypatch.setenv(name, value) - # Both are process-wide and would keep another test's paths. - monkeypatch.setattr(config_module, "_default_config", None) - monkeypatch.setattr(service_module, "_conversation_service", None) app = create_app() + registry = server_context_of(app).conversation_registry + assert isinstance(registry, DockerConversationRegistry) try: with TestClient(app) as client: yield client finally: - app.state.conversation_registry.cleanup_stale_containers() + registry.cleanup_stale_containers() return start def _registry(client: TestClient) -> DockerConversationRegistry: - return client.app.state.conversation_registry # type: ignore[attr-defined] + registry = client_context(client).conversation_registry + assert isinstance(registry, DockerConversationRegistry) + return registry def _start(client: TestClient, working_dir: Path | None = None) -> UUID: diff --git a/tests/agent_server/docker_runtime/test_worktree_live.py b/tests/agent_server/docker_runtime/test_worktree_live.py index fc2715cd62d..18c7fd0dee4 100644 --- a/tests/agent_server/docker_runtime/test_worktree_live.py +++ b/tests/agent_server/docker_runtime/test_worktree_live.py @@ -15,6 +15,8 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config +from openhands.agent_server.docker_runtime.registry import DockerConversationRegistry +from openhands.agent_server.server_context import server_context_of def test_docker_worktrees_remain_host_resolvable_after_runtime_lifecycle( @@ -56,7 +58,8 @@ def git(*args, cwd=repository): first, second = uuid4(), uuid4() with TestClient(app) as client: - registry = app.state.conversation_registry + registry = server_context_of(app).conversation_registry + assert isinstance(registry, DockerConversationRegistry) try: for cid in (first, second): response = client.post( diff --git a/tests/agent_server/stress/conftest.py b/tests/agent_server/stress/conftest.py index 37ab1f7bd5b..ec6db13639d 100644 --- a/tests/agent_server/stress/conftest.py +++ b/tests/agent_server/stress/conftest.py @@ -31,6 +31,7 @@ mark_initialization_complete, server_details_router, ) +from tests.agent_server.conftest import install_test_context from tests.agent_server.stress.budgets import CONCURRENT_CONVERSATIONS from tests.agent_server.stress.probe import ResourceProbe @@ -80,13 +81,12 @@ def app( that need websocket coverage assert against pub_sub internals (white-box) rather than performing real WS handshakes through ASGITransport. - ``app.state.config`` is set so any code that reads it (e.g. middleware) - finds something. ``mark_initialization_complete`` is called so /ready - returns 200 in the responsiveness canary. + A server context is installed so any code that reads it (config, stores, + the bash service) finds one. ``mark_initialization_complete`` is called + so /ready returns 200 in the responsiveness canary. """ fastapi_app = FastAPI() - fastapi_app.state.config = Config() - fastapi_app.state.bash_event_service = bash_service + install_test_context(fastapi_app, Config(), bash_event_service=bash_service) fastapi_app.include_router(server_details_router) fastapi_app.include_router(conversation_router, prefix="/api") fastapi_app.include_router(event_router, prefix="/api") diff --git a/tests/agent_server/stress/test_slow_webhook.py b/tests/agent_server/stress/test_slow_webhook.py index 23d3024e6be..7a3a2fb7162 100644 --- a/tests/agent_server/stress/test_slow_webhook.py +++ b/tests/agent_server/stress/test_slow_webhook.py @@ -43,6 +43,7 @@ from openhands.agent_server.event_router import event_router from openhands.sdk.conversation.state import ConversationExecutionStatus from openhands.sdk.event.conversation_state import ConversationStateUpdateEvent +from tests.agent_server.conftest import install_test_context from tests.agent_server.stress.budgets import SLOW_WEBHOOK from tests.agent_server.stress.probe import ResourceProbe from tests.agent_server.stress.scripts import ( @@ -126,7 +127,7 @@ def app( conversation_service: ConversationService, bash_service: BashEventService ) -> FastAPI: fastapi_app = FastAPI() - fastapi_app.state.config = Config() + install_test_context(fastapi_app, Config()) fastapi_app.include_router(conversation_router, prefix="/api") fastapi_app.include_router(event_router, prefix="/api") fastapi_app.include_router(bash_router_module.bash_router, prefix="/api") @@ -196,7 +197,7 @@ async def test_slow_webhook_does_not_unbound_growth( # but with a webhook-free ConversationService. We need a separate ASGI # client for it. baseline_app = FastAPI() - baseline_app.state.config = Config() + install_test_context(baseline_app, Config()) baseline_app.include_router(conversation_router, prefix="/api") baseline_app.include_router(event_router, prefix="/api") baseline_app.dependency_overrides[get_conversation_service] = ( diff --git a/tests/agent_server/telemetry/conftest.py b/tests/agent_server/telemetry/conftest.py index 2a7efc84092..42990d9afe9 100644 --- a/tests/agent_server/telemetry/conftest.py +++ b/tests/agent_server/telemetry/conftest.py @@ -5,18 +5,19 @@ process state. """ -import os -import tempfile from base64 import urlsafe_b64encode -from pathlib import Path from typing import Any import pytest from pydantic import SecretStr from openhands.agent_server.config import Config, TelemetryExporterKind, TelemetrySpec -from openhands.agent_server.persistence.store import reset_stores -from openhands.agent_server.telemetry import reset_telemetry_sink +from openhands.agent_server.persistence import ServerStores +from openhands.agent_server.telemetry import ( + TelemetrySink, + build_telemetry_sink, + reset_telemetry_sink, +) from openhands.agent_server.telemetry.policy import ( CONSENT_ENV, CONSENT_MODE_ENV, @@ -41,17 +42,19 @@ def _reset_sink(): @pytest.fixture -def temp_persistence_dir(): - with tempfile.TemporaryDirectory() as tmpdir: - reset_stores() - old_val = os.environ.get("OH_PERSISTENCE_DIR") - os.environ["OH_PERSISTENCE_DIR"] = tmpdir - yield Path(tmpdir) - reset_stores() - if old_val is not None: - os.environ["OH_PERSISTENCE_DIR"] = old_val - else: - os.environ.pop("OH_PERSISTENCE_DIR", None) +def temp_persistence_dir(tmp_path, monkeypatch): + persistence_dir = tmp_path / "persistence" + persistence_dir.mkdir() + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(persistence_dir)) + return persistence_dir + + +async def build_sink(config: Config) -> TelemetrySink: + """Build the sink as a server running ``config`` does. + + Consent is read from that server's own settings store. + """ + return await build_telemetry_sink(config, ServerStores.from_config(config).settings) @pytest.fixture diff --git a/tests/agent_server/telemetry/test_telemetry_disabled_by_default.py b/tests/agent_server/telemetry/test_telemetry_disabled_by_default.py index 3952104ca6a..6e9ca860461 100644 --- a/tests/agent_server/telemetry/test_telemetry_disabled_by_default.py +++ b/tests/agent_server/telemetry/test_telemetry_disabled_by_default.py @@ -8,6 +8,7 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config, TelemetrySpec +from openhands.agent_server.persistence import FileSettingsStore, ServerStores from openhands.agent_server.telemetry import ( NoOpTelemetrySink, build_telemetry_sink, @@ -15,6 +16,7 @@ get_telemetry_sink, ) from openhands.agent_server.telemetry.sanitizer import pseudonymize +from tests.agent_server.telemetry.conftest import build_sink def test_default_config_yields_a_noop_sink(temp_persistence_dir): @@ -23,7 +25,7 @@ def test_default_config_yields_a_noop_sink(temp_persistence_dir): async def test_building_from_a_default_config_stays_a_noop(temp_persistence_dir): - sink = await build_telemetry_sink(Config(static_files_path=None)) + sink = await build_sink(Config(static_files_path=None)) assert isinstance(sink, NoOpTelemetrySink) assert sink.enabled is False @@ -31,7 +33,7 @@ async def test_building_from_a_default_config_stays_a_noop(temp_persistence_dir) async def test_building_sink_carries_configured_deployment_kind( temp_persistence_dir, ): - sink = await build_telemetry_sink( + sink = await build_sink( Config( static_files_path=None, telemetry=TelemetrySpec(deployment_kind="remote") ) @@ -53,7 +55,7 @@ async def test_salt_falls_back_to_the_secret_key_as_documented( assert "linkable across restarts" in description conversation_id = uuid4() - sink = await build_telemetry_sink(config_factory()) + sink = await build_sink(config_factory()) try: factory = get_event_factory() assert factory is not None @@ -66,15 +68,13 @@ async def test_salt_falls_back_to_the_secret_key_as_documented( async def test_opt_in_without_an_api_key_stays_inactive(config_factory): """No key means no exporter.""" - sink = await build_telemetry_sink(config_factory("posthog")) + sink = await build_sink(config_factory("posthog")) assert isinstance(sink, NoOpTelemetrySink) async def test_kill_switch_short_circuits_delivery(config_factory, monkeypatch): monkeypatch.setenv("DO_NOT_TRACK", "1") - sink = await build_telemetry_sink( - config_factory("posthog", posthog_api_key="phc_test") - ) + sink = await build_sink(config_factory("posthog", posthog_api_key="phc_test")) assert sink.enabled is False @@ -131,7 +131,7 @@ def __init__(self, *a, **k): monkeypatched = getattr(service_mod, "PostHogExporter", None) assert monkeypatched is None, "exporter must not be a module-level attribute" - await build_telemetry_sink(Config(static_files_path=None)) + await build_sink(Config(static_files_path=None)) assert built == [] @@ -302,14 +302,14 @@ async def aclose(self): assert emitted == [], f"deferred pod emitted unpaired lifecycle events: {emitted}" -async def test_telemetry_init_does_not_hijack_the_settings_store_singleton( +async def test_telemetry_reads_consent_through_the_server_settings_store( temp_persistence_dir, monkeypatch ): - """Regression: telemetry must prime the settings store WITH the config. + """Telemetry reads consent through the store the server hands it. - ``get_settings_store`` is a singleton whose cipher is fixed by the first - call. Telemetry initialises during lifespan startup, so a no-arg call here - would leave the whole process writing secrets with encryption disabled. + Telemetry initialises during lifespan startup. It must use the server's + settings store, which carries the config's cipher and directory, and not + build a keyless one of its own. """ from base64 import urlsafe_b64encode @@ -317,7 +317,6 @@ async def test_telemetry_init_does_not_hijack_the_settings_store_singleton( import openhands.agent_server.telemetry.posthog_exporter as pe from openhands.agent_server.config import Config, TelemetrySpec - from openhands.agent_server.persistence.store import get_settings_store class _FakeExporter: async def send(self, events): @@ -336,13 +335,20 @@ async def aclose(self): ), ) - sink = await build_telemetry_sink(config) + store = ServerStores.from_config(config).settings + loads: list[FileSettingsStore] = [] + real_load = FileSettingsStore.load + + def recording_load(self, **kwargs): + loads.append(self) + return real_load(self, **kwargs) + + monkeypatch.setattr(FileSettingsStore, "load", recording_load) + + sink = await build_telemetry_sink(config, store) try: - store = get_settings_store(config) - assert store.cipher is not None, ( - "telemetry primed the settings store without a cipher; secrets " - "would be persisted unencrypted process-wide" - ) + assert loads == [store], "consent must be read through the given store" + assert store.cipher is not None assert store.persistence_dir == temp_persistence_dir finally: await sink.aclose() diff --git a/tests/agent_server/telemetry/test_telemetry_failure_paths.py b/tests/agent_server/telemetry/test_telemetry_failure_paths.py index 2d7481d3772..72511e680d5 100644 --- a/tests/agent_server/telemetry/test_telemetry_failure_paths.py +++ b/tests/agent_server/telemetry/test_telemetry_failure_paths.py @@ -17,11 +17,11 @@ from openhands.agent_server.config import Config, TelemetrySpec from openhands.agent_server.telemetry import ( NoOpTelemetrySink, - build_telemetry_sink, get_telemetry_sink, notify_misc_settings_changed, shutdown_telemetry_sink, ) +from tests.agent_server.telemetry.conftest import build_sink SETTINGS_URL = "/api/settings" @@ -54,7 +54,7 @@ def _no_posthog(name, *args, **kwargs): sys.modules, "openhands.agent_server.telemetry.posthog_exporter", raising=False ) - sink = await build_telemetry_sink(Config(static_files_path=None, telemetry=_spec())) + sink = await build_sink(Config(static_files_path=None, telemetry=_spec())) assert isinstance(sink, NoOpTelemetrySink) assert sink.enabled is False @@ -71,7 +71,7 @@ def _boom(**kwargs): module.Posthog = _boom # type: ignore[attr-defined] monkeypatch.setitem(sys.modules, "posthog", module) - sink = await build_telemetry_sink(Config(static_files_path=None, telemetry=_spec())) + sink = await build_sink(Config(static_files_path=None, telemetry=_spec())) assert isinstance(sink, NoOpTelemetrySink) @@ -90,7 +90,7 @@ def _boom(_config): monkeypatch.setattr(service_mod, "_read_misc_settings_sync", _boom) - sink = await build_telemetry_sink(Config(static_files_path=None, telemetry=_spec())) + sink = await build_sink(Config(static_files_path=None, telemetry=_spec())) try: assert sink.enabled is False, "unreadable consent must not enable telemetry" finally: @@ -104,7 +104,7 @@ async def test_exporter_aclose_raising_is_swallowed(temp_persistence_dir, monkey module.Posthog = lambda **kw: client # type: ignore[attr-defined] monkeypatch.setitem(sys.modules, "posthog", module) - await build_telemetry_sink(Config(static_files_path=None, telemetry=_spec())) + await build_sink(Config(static_files_path=None, telemetry=_spec())) # Must not propagate. await shutdown_telemetry_sink() diff --git a/tests/agent_server/telemetry/test_telemetry_init_lifecycle.py b/tests/agent_server/telemetry/test_telemetry_init_lifecycle.py index b39811a174b..2ef0217d0ed 100644 --- a/tests/agent_server/telemetry/test_telemetry_init_lifecycle.py +++ b/tests/agent_server/telemetry/test_telemetry_init_lifecycle.py @@ -8,13 +8,16 @@ unpaired second start. """ -from types import SimpleNamespace - import pytest +from fastapi import FastAPI import openhands.agent_server.telemetry.service as service_mod from openhands.agent_server.config import Config from openhands.agent_server.init_router import InitRequest, InitService +from openhands.agent_server.server_context import ( + build_server_context, + install_server_context, +) from openhands.agent_server.telemetry import models as m from openhands.agent_server.telemetry.factory import ( DiagnosticEventFactory, @@ -53,13 +56,6 @@ def recording(monkeypatch): service_mod.reset_telemetry_sink() -def _reset_singletons(): - from openhands.agent_server import bash_service, conversation_service - - conversation_service._conversation_service = None - bash_service._bash_event_service = None - - async def test_emit_server_started_is_idempotent(recording): service_mod.emit_server_started() service_mod.emit_server_started() @@ -84,14 +80,14 @@ async def test_failed_init_emits_no_start_and_retry_emits_exactly_one( server_started there, leaving an unpaired start and letting the retry emit a second one. """ - _reset_singletons() base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = FastAPI() + install_server_context(app, build_server_context(base)) + svc = InitService(app, base_config=base) # The sink is (re)built inside initialize(); keep our recording sink in # place so the emit path is observable regardless. @@ -106,10 +102,10 @@ async def test_failed_init_emits_no_start_and_retry_emits_exactly_one( boom = {"raise": True} real_get_instance = init_mod.ConversationService.get_instance - def _maybe_boom(config): + def _maybe_boom(config, stores): if boom["raise"]: raise RuntimeError("get_instance failed") - return real_get_instance(config) + return real_get_instance(config, stores) monkeypatch.setattr(init_mod.ConversationService, "get_instance", _maybe_boom) @@ -130,11 +126,10 @@ def _maybe_boom(config): ) finally: await svc.teardown() - _reset_singletons() def _keep_sink(sink): - async def _build(_config): + async def _build(_config, _settings_store): service_mod._telemetry_sink = sink return sink diff --git a/tests/agent_server/telemetry/test_telemetry_settings_path.py b/tests/agent_server/telemetry/test_telemetry_settings_path.py index 60d4fb56158..2cc40223b27 100644 --- a/tests/agent_server/telemetry/test_telemetry_settings_path.py +++ b/tests/agent_server/telemetry/test_telemetry_settings_path.py @@ -9,7 +9,7 @@ from fastapi.testclient import TestClient from openhands.agent_server.api import create_app -from openhands.agent_server.persistence.store import get_settings_store +from openhands.agent_server.persistence import ServerStores from openhands.agent_server.telemetry.policy import TelemetryDecision, resolve @@ -60,19 +60,19 @@ def test_consent_is_written_and_read_through_misc_settings(client, config_factor assert body["misc_settings"]["telemetry"]["consent"] == "granted" - settings = get_settings_store(config_factory()).load() + settings = ServerStores.from_config(config_factory()).settings.load() assert settings is not None assert resolve(settings.misc_settings, env={}).enabled is True -def test_consent_survives_a_store_reset(client, config_factory, temp_persistence_dir): - from openhands.agent_server.persistence.store import reset_stores - +def test_consent_survives_a_server_restart( + client, config_factory, temp_persistence_dir +): with client as c: c.patch(SETTINGS_URL, json=diff("granted")) - reset_stores() - settings = get_settings_store(config_factory()).load() + # Freshly built stores, as the next server process would have. + settings = ServerStores.from_config(config_factory()).settings.load() assert settings is not None assert resolve(settings.misc_settings, env={}).enabled is True diff --git a/tests/agent_server/test_acp_router.py b/tests/agent_server/test_acp_router.py index b1f366c0f31..8c36ff9e1c6 100644 --- a/tests/agent_server/test_acp_router.py +++ b/tests/agent_server/test_acp_router.py @@ -16,13 +16,18 @@ from openhands.agent_server.config import Config from openhands.agent_server.credential_binding import LocalVersionedCredentialBinding from openhands.agent_server.local_secret_resolver import local_secret_resolution -from openhands.agent_server.persistence import get_secrets_store +from openhands.agent_server.persistence import ServerStores from openhands.sdk.agent.acp_file_credentials import CODEX_AUTH_SECRET_NAME from openhands.sdk.agent.acp_models import ( ACPModelDiscovery, ACPModelDiscoveryError, ACPModelInfo, ) +from tests.agent_server.conftest import ( + client_context, + install_test_context, + update_test_context, +) FAKE_SERVER = Path(__file__).parents[1] / "sdk" / "agent" / "fake_acp_models_server.py" @@ -41,12 +46,17 @@ def config(tmp_path: Path) -> Config: @pytest.fixture def client(config: Config) -> Iterator[TestClient]: app = FastAPI() - app.state.config = config + context = install_test_context(app, config) app.include_router(acp_router, prefix="/api") - with local_secret_resolution(config): + with local_secret_resolution(lambda: context.stores.secrets): yield TestClient(app) +@pytest.fixture +def stores(client: TestClient) -> ServerStores: + return client_context(client).stores + + class _FakeDiscovery: def __init__(self, result: ACPModelDiscovery) -> None: self.result = result @@ -89,10 +99,10 @@ def test_reports_a_live_servers_models( def test_resolves_stored_secrets_by_reference( - client: TestClient, config: Config, monkeypatch: pytest.MonkeyPatch + client: TestClient, stores: ServerStores, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.delenv("FAKE_ACP_KEY", raising=False) - get_secrets_store(config).set_secret("FAKE_ACP_KEY", "stored-key") + stores.secrets.set_secret("FAKE_ACP_KEY", "stored-key") lookup = {"kind": "LookupSecret", "url": "/api/settings/secrets/FAKE_ACP_KEY"} response = _discover(client, secrets={"FAKE_ACP_KEY": lookup}) @@ -152,9 +162,9 @@ def test_does_not_reuse_a_failure( def test_uses_the_stored_codex_login_like_a_conversation( - client: TestClient, config: Config, fake_discovery: _FakeDiscovery + client: TestClient, stores: ServerStores, fake_discovery: _FakeDiscovery ) -> None: - get_secrets_store(config).set_secret(CODEX_AUTH_SECRET_NAME, '{"tokens": {}}') + stores.secrets.set_secret(CODEX_AUTH_SECRET_NAME, '{"tokens": {}}') sent = {"kind": "StaticSecret", "value": "pasted"} client.post( @@ -185,7 +195,9 @@ def test_is_unavailable_when_conversations_run_in_containers( ) -> None: app = client.app assert isinstance(app, FastAPI) - app.state.config = config.model_copy(update={"conversation_runtime": "docker"}) + update_test_context( + app, config=config.model_copy(update={"conversation_runtime": "docker"}) + ) response = _discover(client) diff --git a/tests/agent_server/test_agent_launch_additions.py b/tests/agent_server/test_agent_launch_additions.py index bfb75bef2c4..e19ff10325b 100644 --- a/tests/agent_server/test_agent_launch_additions.py +++ b/tests/agent_server/test_agent_launch_additions.py @@ -99,7 +99,9 @@ def test_launch_addition_uses_existing_acp_prompt_path(): @pytest.mark.parametrize("profile_launch", [False, True]) @pytest.mark.asyncio -async def test_launch_additions_apply_after_agent_resolution(profile_launch, tmp_path): +async def test_launch_additions_apply_after_agent_resolution( + profile_launch, tmp_path, server_stores +): profile = OpenHandsAgentProfile( name="p", llm_profile_ref="default", @@ -132,7 +134,7 @@ async def test_launch_additions_apply_after_agent_resolution(profile_launch, tmp execution_status=ConversationExecutionStatus.IDLE, ) captured: dict[str, Any] = {} - service = ConversationService(conversations_dir=tmp_path) + service = ConversationService(conversations_dir=tmp_path, stores=server_stores) service._event_services = {} async def capture_start(stored, **kwargs): diff --git a/tests/agent_server/test_agent_profile_conv_start.py b/tests/agent_server/test_agent_profile_conv_start.py index 430f8eb3ace..cb57f8d172c 100644 --- a/tests/agent_server/test_agent_profile_conv_start.py +++ b/tests/agent_server/test_agent_profile_conv_start.py @@ -30,7 +30,7 @@ StartConversationRequest, StoredConversation, ) -from openhands.agent_server.persistence import PersistedSettings +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.sdk import LLM, Agent, AgentBase, AgentContext from openhands.sdk.agent.acp_agent import ACPAgent from openhands.sdk.conversation.request import AgentLaunchAdditions @@ -55,6 +55,7 @@ from openhands.sdk.skills import Skill from openhands.sdk.tool import Tool from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import install_test_context from tests.sdk.launch import fakes @@ -68,8 +69,8 @@ def client(): """TestClient with no auth — conversations router only.""" app = FastAPI() app.include_router(conversation_router, prefix="/api") - app.state.config = Config( - static_files_path=None, session_api_keys=[], secret_key=None + install_test_context( + app, Config(static_files_path=None, session_api_keys=[], secret_key=None) ) return TestClient(app) @@ -96,10 +97,16 @@ def _make_agent() -> Agent: _STORES_PATH = "openhands.agent_server.conversation_service.server_launch_stores" -_SETTINGS_STORE_PATH = "openhands.agent_server.persistence.get_settings_store" _BROWSER_PATH = "openhands.agent_server.launch.is_tool_usable" +def _server_stores(persisted: PersistedSettings | None = None) -> ServerStores: + """The service's stores, holding ``persisted`` as the saved settings.""" + stores = ServerStores.from_config(Config()) + stores.settings.save(persisted or PersistedSettings()) + return stores + + def _event_service_for(stored: StoredConversation, agent: AgentBase) -> AsyncMock: event_service = AsyncMock(spec=EventService) event_service.get_state.return_value = ConversationState( @@ -138,10 +145,11 @@ async def capture_start(stored, **kwargs): captured["launched"] = kwargs["launched"] return _event_service_for(stored, kwargs["launched"].agent) - service = ConversationService(conversations_dir=tmp_path / "conversations") + service = ConversationService( + conversations_dir=tmp_path / "conversations", stores=_server_stores(persisted) + ) service._event_services = {} with ( - patch(_SETTINGS_STORE_PATH) as settings_store, patch(_STORES_PATH, return_value=stores or fakes.stores()), patch(_BROWSER_PATH, return_value=browser), patch.object( @@ -151,7 +159,6 @@ async def capture_start(stored, **kwargs): side_effect=capture_start, ), ): - settings_store.return_value.load.return_value = persisted or PersistedSettings() await service.start_conversation(request) return captured["stored"], captured["launched"] @@ -426,15 +433,15 @@ async def capture_start(stored, **kwargs): return _event_service_for(stored, kwargs["launched"].agent) service = ConversationService( - conversations_dir=tmp_path / "conversations", acp_skill_sourcing=sourcing + conversations_dir=tmp_path / "conversations", + acp_skill_sourcing=sourcing, + stores=_server_stores(), ) service._event_services = {} with ( - patch(_SETTINGS_STORE_PATH) as settings_store, patch(_STORES_PATH, return_value=stores), patch.object(service, "_start_event_service", side_effect=capture_start), ): - settings_store.return_value.load.return_value = PersistedSettings() await service.start_conversation(request) context = captured["agent"].agent_context diff --git a/tests/agent_server/test_agent_profiles_router.py b/tests/agent_server/test_agent_profiles_router.py index 9421d36b578..110f3acd0e1 100644 --- a/tests/agent_server/test_agent_profiles_router.py +++ b/tests/agent_server/test_agent_profiles_router.py @@ -7,8 +7,6 @@ import concurrent.futures import json -import tempfile -from pathlib import Path from unittest.mock import patch import pytest @@ -18,7 +16,6 @@ from openhands.agent_server import agent_profiles_router as router_module from openhands.agent_server.api import create_app from openhands.agent_server.config import Config -from openhands.agent_server.persistence import reset_stores from openhands.agent_server.profiles_router import MAX_PROFILES from openhands.sdk.llm import LLM from openhands.sdk.llm.llm_profile_store import LLMProfileStore @@ -30,34 +27,27 @@ @pytest.fixture -def temp_agent_profiles_dir(): - with tempfile.TemporaryDirectory() as tmpdir: - agent_dir = Path(tmpdir) / "agent-profiles" - agent_dir.mkdir(parents=True, exist_ok=True) - yield agent_dir +def temp_settings_dir(tmp_path, monkeypatch): + """The server's persistence directory; ``settings.json`` lives in it.""" + settings_dir = tmp_path / "settings" + settings_dir.mkdir() + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(settings_dir)) + return settings_dir @pytest.fixture -def temp_settings_dir(): - with tempfile.TemporaryDirectory() as tmpdir: - settings_dir = Path(tmpdir) / "settings" - settings_dir.mkdir(parents=True, exist_ok=True) - yield settings_dir +def temp_agent_profiles_dir(temp_settings_dir): + """Where the server's stores keep agent profiles.""" + agent_dir = temp_settings_dir / "agent-profiles" + agent_dir.mkdir() + return agent_dir @pytest.fixture -def client(temp_agent_profiles_dir, temp_settings_dir, monkeypatch): +def client(temp_agent_profiles_dir, temp_settings_dir): """Test client with isolated agent-profile/settings dirs, no cipher.""" - reset_stores() - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(temp_settings_dir)) config = Config(static_files_path=None, session_api_keys=[], secret_key=None) - app = create_app(config) - with patch( - "openhands.agent_server.agent_profiles_router.get_agent_profile_store", - lambda: AgentProfileStore(base_dir=temp_agent_profiles_dir), - ): - yield TestClient(app) - reset_stores() + return TestClient(create_app(config)) @pytest.fixture @@ -67,9 +57,9 @@ def store(temp_agent_profiles_dir): @pytest.fixture def default_llm_profile_store(temp_settings_dir): - """The real (unpatched) LLM profile store the ``client`` fixture's - ``get_llm_profile_store()`` resolves to, given ``OH_PERSISTENCE_DIR`` — - see ``_get_profile_persistence_dir`` (``/profiles``).""" + """The LLM profile store of the ``client`` fixture's server, given + ``OH_PERSISTENCE_DIR`` — see ``ServerStores.llm_profiles`` + (``/profiles``).""" return LLMProfileStore(base_dir=temp_settings_dir / "profiles") @@ -1160,43 +1150,20 @@ def test_save_at_limit_returns_409(client, store, monkeypatch): @pytest.fixture -def temp_llm_profiles_dir(): - with tempfile.TemporaryDirectory() as tmpdir: - llm_dir = Path(tmpdir) / "llm-profiles" - llm_dir.mkdir(parents=True, exist_ok=True) - yield llm_dir +def temp_llm_profiles_dir(temp_settings_dir): + """Where the server's stores keep LLM profiles.""" + llm_dir = temp_settings_dir / "profiles" + llm_dir.mkdir(exist_ok=True) + return llm_dir @pytest.fixture def client_with_llm_store( - temp_agent_profiles_dir, temp_settings_dir, temp_llm_profiles_dir, monkeypatch + temp_agent_profiles_dir, temp_settings_dir, temp_llm_profiles_dir ): """Test client with isolated agent-profile/settings/llm-profile dirs, no cipher.""" - reset_stores() - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(temp_settings_dir)) config = Config(static_files_path=None, session_api_keys=[], secret_key=None) - app = create_app(config) - - def agent_profiles() -> AgentProfileStore: - return AgentProfileStore(base_dir=temp_agent_profiles_dir) - - def llm_profiles() -> LLMProfileStore: - return LLMProfileStore(base_dir=temp_llm_profiles_dir) - - with ( - patch( - "openhands.agent_server.agent_profiles_router.get_agent_profile_store", - agent_profiles, - ), - patch( - "openhands.agent_server.agent_profiles_router.get_llm_profile_store", - llm_profiles, - ), - patch("openhands.agent_server.launch.get_agent_profile_store", agent_profiles), - patch("openhands.agent_server.launch.get_llm_profile_store", llm_profiles), - ): - yield TestClient(app) - reset_stores() + return TestClient(create_app(config)) @pytest.fixture diff --git a/tests/agent_server/test_api.py b/tests/agent_server/test_api.py index 159e569b86b..cdb9dcd9b53 100644 --- a/tests/agent_server/test_api.py +++ b/tests/agent_server/test_api.py @@ -7,18 +7,16 @@ import tempfile import threading import time +from dataclasses import replace from pathlib import Path from types import SimpleNamespace +from typing import cast from unittest.mock import AsyncMock, patch import libtmux 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, @@ -28,6 +26,11 @@ create_app, ) from openhands.agent_server.config import Config +from openhands.agent_server.conversation_registry import ConversationRegistry +from openhands.agent_server.server_context import ( + build_server_context, + server_context_of, +) from openhands.tools.terminal.terminal.tmux_pane_pool import TmuxPanePool @@ -613,7 +616,9 @@ async def slow_start(): ): # Create a mock FastAPI app mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) async with api_lifespan(mock_app): pass @@ -659,7 +664,7 @@ async def slow_stop(): mock_app = AsyncMock() mock_backend_manager = AsyncMock() mock_app.state = SimpleNamespace( - config=Config(), + server_context=build_server_context(Config()), canvas_extension_backend_manager=mock_backend_manager, ) @@ -689,14 +694,17 @@ async def test_services_handle_none_values(self): ): # Create a mock FastAPI app mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) # This should not raise any exceptions async with api_lifespan(mock_app): pass # Verify conversation service was set up - assert mock_app.state.conversation_service == mock_conversation_service + context = mock_app.state.server_context + assert context.conversation_service == mock_conversation_service async def test_registry_starts_before_conversation_recovery(self): events = [] @@ -721,7 +729,10 @@ async def test_registry_starts_before_conversation_recovery(self): ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - config=Config(), conversation_registry=registry + server_context=replace( + build_server_context(Config()), + conversation_registry=cast(ConversationRegistry, registry), + ) ) async with api_lifespan(mock_app): @@ -751,7 +762,9 @@ async def test_lifespan_defaults_and_restores_tmux_tmpdir( ), ): mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(expected_tmux_tmpdir) assert expected_tmux_tmpdir.is_dir() @@ -779,7 +792,9 @@ async def test_lifespan_keeps_user_provided_tmux_tmpdir( ), ): mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(user_tmux_tmpdir) @@ -815,7 +830,9 @@ async def test_lifespan_sets_and_restores_utf8_locale( ), ): mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) async with api_lifespan(mock_app): assert os.environ[overridden] == "C.UTF-8" @@ -848,7 +865,9 @@ async def test_lifespan_removes_substitute_for_too_deep_tmux_tmpdir( ), ): mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(substitute) @@ -887,7 +906,9 @@ def fail_rmtree(path): ), ): mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) with caplog.at_level("WARNING"): async with api_lifespan(mock_app): pass @@ -918,7 +939,9 @@ async def test_lifespan_keeps_preexisting_default_tmux_tmpdir( ), ): mock_app = AsyncMock() - mock_app.state = SimpleNamespace(config=Config()) + mock_app.state = SimpleNamespace( + server_context=build_server_context(Config()) + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(expected_tmux_tmpdir) @@ -1147,8 +1170,6 @@ def test_lifespan_conversation_service_uses_app_config_not_env(tmp_path, monkeyp """ 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=[], @@ -1158,6 +1179,7 @@ def test_lifespan_conversation_service_uses_app_config_not_env(tmp_path, monkeyp app = create_app(config) with TestClient(app): - service = app.state.conversation_service + service = server_context_of(app).conversation_service + assert service is not None assert service.conversations_dir == tmp_path / "conversations" assert service.webhook_specs == [] diff --git a/tests/agent_server/test_api_authentication.py b/tests/agent_server/test_api_authentication.py index 7c56cc64da1..88b96f717ec 100644 --- a/tests/agent_server/test_api_authentication.py +++ b/tests/agent_server/test_api_authentication.py @@ -122,15 +122,7 @@ def test_api_server_details_no_auth_required(client_with_auth): assert response.status_code != 401 -def test_openai_routes_accept_bearer_session_key(client_with_auth, monkeypatch): - class EmptyProfileStore: - def list_summaries(self) -> list[dict[str, object]]: - return [] - - monkeypatch.setattr( - "openhands.agent_server.openai.service.get_llm_profile_store", EmptyProfileStore - ) - +def test_openai_routes_accept_bearer_session_key(client_with_auth): response = client_with_auth.get("/v1/models") assert response.status_code == 401 diff --git a/tests/agent_server/test_bash_router.py b/tests/agent_server/test_bash_router.py index 6c2224a7e88..2428ced6629 100644 --- a/tests/agent_server/test_bash_router.py +++ b/tests/agent_server/test_bash_router.py @@ -7,13 +7,16 @@ import openhands.agent_server.bash_service as bash_service_module from openhands.agent_server.bash_router import bash_router from openhands.agent_server.bash_service import BashEventService +from tests.agent_server.conftest import install_test_context @pytest.fixture async def bash_client(tmp_path): app = FastAPI() app.include_router(bash_router, prefix="/api") - app.state.bash_event_service = BashEventService(tmp_path / "bash_events") + install_test_context( + app, bash_event_service=BashEventService(tmp_path / "bash_events") + ) async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://test" ) as client: diff --git a/tests/agent_server/test_bash_service.py b/tests/agent_server/test_bash_service.py index fce91eab092..2fa840a4576 100644 --- a/tests/agent_server/test_bash_service.py +++ b/tests/agent_server/test_bash_service.py @@ -25,6 +25,7 @@ server_details_router, ) from openhands.sdk.utils.cipher import FERNET_TOKEN_PREFIX, Cipher +from tests.agent_server.conftest import install_test_context @pytest_asyncio.fixture @@ -37,8 +38,7 @@ async def bash_service(tmp_path: Path) -> AsyncIterator[BashEventService]: @pytest_asyncio.fixture async def client(bash_service: BashEventService) -> AsyncIterator[httpx.AsyncClient]: app = FastAPI() - app.state.config = Config() - app.state.bash_event_service = bash_service + install_test_context(app, Config(), bash_event_service=bash_service) app.include_router(server_details_router) app.include_router(bash_router_module.bash_router, prefix="/api") mark_initialization_complete() diff --git a/tests/agent_server/test_blocking_export_extended.py b/tests/agent_server/test_blocking_export_extended.py index dc6f1ab3f48..e89091c048e 100644 --- a/tests/agent_server/test_blocking_export_extended.py +++ b/tests/agent_server/test_blocking_export_extended.py @@ -35,9 +35,11 @@ def blocked_export(spans): monkeypatch.setattr( "openhands.agent_server.api._TRACE_FLUSH_TIMEOUT_SECONDS", 0.05, raising=False ) - monkeypatch.setattr("openhands.agent_server.api.get_vscode_service", lambda: None) monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda: None + "openhands.agent_server.api.get_vscode_service", lambda _config: None + ) + monkeypatch.setattr( + "openhands.agent_server.api.get_tool_preload_service", lambda _config: None ) monkeypatch.setattr( "openhands.agent_server.api.unseal_tool_catalog", lambda: cleanup.append(True) diff --git a/tests/agent_server/test_conversation_router.py b/tests/agent_server/test_conversation_router.py index 87efdf068f8..997f1d2a14e 100644 --- a/tests/agent_server/test_conversation_router.py +++ b/tests/agent_server/test_conversation_router.py @@ -41,6 +41,7 @@ from openhands.sdk.security.llm_analyzer import LLMSecurityAnalyzer from openhands.sdk.settings import AGENT_SETTINGS_SCHEMA_VERSION from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import client_context, install_test_context @pytest.fixture @@ -48,10 +49,10 @@ def client(): """Create a test client for the FastAPI app without authentication.""" app = FastAPI() app.include_router(conversation_router, prefix="/api") - # switch_llm reads request.app.state.config to get the optional cipher; - # populate it with a no-cipher config so unrelated tests don't 503. - app.state.config = Config( - static_files_path=None, session_api_keys=[], secret_key=None + # switch_llm reads the server context's config to get the optional cipher; + # install a no-cipher one. + install_test_context( + app, Config(static_files_path=None, session_api_keys=[], secret_key=None) ) return TestClient(app) @@ -170,8 +171,11 @@ def runtime_info(self, conversation_id: UUID) -> ConversationRuntimeInfo: can_resume=False, ) - registry = MissingRuntimeRegistry(client.app.state.config) - client.app.state.conversation_registry = registry + context = client_context(client) + install_test_context( + client.app, + conversation_registry=MissingRuntimeRegistry(context.config, context.stores), + ) client.app.dependency_overrides[get_conversation_service] = lambda: ( mock_conversation_service ) @@ -180,7 +184,6 @@ def runtime_info(self, conversation_id: UUID) -> ConversationRuntimeInfo: response = client.get("/api/conversations/search") finally: client.app.dependency_overrides.clear() - del client.app.state.conversation_registry assert response.status_code == 200 assert response.json()["items"][0]["runtime_info"] == { @@ -2599,10 +2602,13 @@ def test_switch_conversation_llm_decrypts_encrypted_api_key( assert encrypted_api_key is not None # Install a cipher-enabled config on the test app for this test. - client.app.state.config = Config( - static_files_path=None, - session_api_keys=[], - secret_key=SecretStr(secret_key), + install_test_context( + client.app, + Config( + static_files_path=None, + session_api_keys=[], + secret_key=SecretStr(secret_key), + ), ) mock_conversation = MagicMock() @@ -2645,10 +2651,13 @@ def test_switch_conversation_llm_plaintext_with_cipher_passes_through( from base64 import urlsafe_b64encode secret_key = urlsafe_b64encode(b"a" * 32).decode("ascii") - client.app.state.config = Config( - static_files_path=None, - session_api_keys=[], - secret_key=SecretStr(secret_key), + install_test_context( + client.app, + Config( + static_files_path=None, + session_api_keys=[], + secret_key=SecretStr(secret_key), + ), ) mock_conversation = MagicMock() @@ -2746,54 +2755,40 @@ def test_switch_conversation_llm_resolves_provider_connection( """A provider_connection_id is resolved into the connection's api_key / base_url before the LLM is installed — regression for OpenHands/OpenHands#17206 (switching to a linked OpenRouter profile lost credentials).""" - import tempfile - from pathlib import Path - - from openhands.agent_server.persistence import store as store_module - from openhands.sdk.llm.provider_connection_store import ProviderConnectionStore - - with tempfile.TemporaryDirectory() as tmpdir: - base = Path(tmpdir) - provider_store = ProviderConnectionStore(base_dir=base / "connections") - provider_store.create( - _provider_connection("or-conn", api_key="or-secret", base_url=None), - cipher=None, - ) - profile_store = LLMProfileStore( - base_dir=base / "profiles", provider_store=provider_store - ) - - mock_conversation = MagicMock() - mock_conversation_service.get_event_service.return_value = mock_event_service - mock_event_service.get_conversation.return_value = mock_conversation - client.app.dependency_overrides[get_conversation_service] = lambda: ( - mock_conversation_service - ) - - old_store = store_module._llm_profile_store - store_module._llm_profile_store = profile_store - try: - response = client.post( - f"/api/conversations/{sample_conversation_id}/switch_llm", - json={ - "llm": { - "model": "openrouter/z-ai/glm-5.3-flash", - "provider_connection_id": "or-conn", - "usage_id": "caller-supplied-id", - } - }, - ) + # The server's own connection store; its profile store resolves from it. + client_context(client).stores.provider_connections.create( + _provider_connection("or-conn", api_key="or-secret", base_url=None), + cipher=None, + ) - assert response.status_code == 200 - mock_conversation.switch_llm.assert_called_once() - forwarded = mock_conversation.switch_llm.call_args.args[0] - assert isinstance(forwarded, LLM) - assert forwarded.provider_connection_id == "or-conn" - assert isinstance(forwarded.api_key, SecretStr) - assert forwarded.api_key.get_secret_value() == "or-secret" - finally: - store_module._llm_profile_store = old_store - client.app.dependency_overrides.clear() + mock_conversation = MagicMock() + mock_conversation_service.get_event_service.return_value = mock_event_service + mock_event_service.get_conversation.return_value = mock_conversation + client.app.dependency_overrides[get_conversation_service] = lambda: ( + mock_conversation_service + ) + + try: + response = client.post( + f"/api/conversations/{sample_conversation_id}/switch_llm", + json={ + "llm": { + "model": "openrouter/z-ai/glm-5.3-flash", + "provider_connection_id": "or-conn", + "usage_id": "caller-supplied-id", + } + }, + ) + + assert response.status_code == 200 + mock_conversation.switch_llm.assert_called_once() + forwarded = mock_conversation.switch_llm.call_args.args[0] + assert isinstance(forwarded, LLM) + assert forwarded.provider_connection_id == "or-conn" + assert isinstance(forwarded.api_key, SecretStr) + assert forwarded.api_key.get_secret_value() == "or-secret" + finally: + client.app.dependency_overrides.clear() def test_switch_conversation_llm_resolves_encrypted_provider_connection( @@ -2801,66 +2796,57 @@ def test_switch_conversation_llm_resolves_encrypted_provider_connection( ): """Encrypted-at-rest provider credentials are decrypted with the server's cipher before the LLM is installed.""" - import tempfile from base64 import urlsafe_b64encode - from pathlib import Path - from openhands.agent_server.persistence import store as store_module - from openhands.sdk.llm.provider_connection_store import ProviderConnectionStore from openhands.sdk.utils.cipher import Cipher secret_key = urlsafe_b64encode(b"a" * 32).decode("ascii") cipher = Cipher(secret_key) - client.app.state.config = Config( - static_files_path=None, - session_api_keys=[], - secret_key=SecretStr(secret_key), - ) - - with tempfile.TemporaryDirectory() as tmpdir: - base = Path(tmpdir) - provider_store = ProviderConnectionStore(base_dir=base / "connections") - provider_store.create( - _provider_connection( - "or-conn", - api_key="plaintext-or-secret", - base_url="https://openrouter.ai/api/v1", - ), - cipher=cipher, - ) - profile_store = LLMProfileStore( - base_dir=base / "profiles", provider_store=provider_store - ) - - mock_conversation = MagicMock() - mock_conversation_service.get_event_service.return_value = mock_event_service - mock_event_service.get_conversation.return_value = mock_conversation - client.app.dependency_overrides[get_conversation_service] = lambda: ( - mock_conversation_service - ) - - old_store = store_module._llm_profile_store - store_module._llm_profile_store = profile_store - try: - response = client.post( - f"/api/conversations/{sample_conversation_id}/switch_llm", - json={ - "llm": { - "model": "openrouter/z-ai/glm-5.3-flash", - "provider_connection_id": "or-conn", - "usage_id": "caller-supplied-id", - } - }, - ) + context = install_test_context( + client.app, + Config( + static_files_path=None, + session_api_keys=[], + secret_key=SecretStr(secret_key), + ), + ) + + # The server's own connection store; its profile store resolves from it. + context.stores.provider_connections.create( + _provider_connection( + "or-conn", + api_key="plaintext-or-secret", + base_url="https://openrouter.ai/api/v1", + ), + cipher=cipher, + ) + + mock_conversation = MagicMock() + mock_conversation_service.get_event_service.return_value = mock_event_service + mock_event_service.get_conversation.return_value = mock_conversation + client.app.dependency_overrides[get_conversation_service] = lambda: ( + mock_conversation_service + ) + + try: + response = client.post( + f"/api/conversations/{sample_conversation_id}/switch_llm", + json={ + "llm": { + "model": "openrouter/z-ai/glm-5.3-flash", + "provider_connection_id": "or-conn", + "usage_id": "caller-supplied-id", + } + }, + ) - assert response.status_code == 200 - forwarded = mock_conversation.switch_llm.call_args.args[0] - assert isinstance(forwarded.api_key, SecretStr) - assert forwarded.api_key.get_secret_value() == "plaintext-or-secret" - assert forwarded.base_url == "https://openrouter.ai/api/v1" - finally: - store_module._llm_profile_store = old_store - client.app.dependency_overrides.clear() + assert response.status_code == 200 + forwarded = mock_conversation.switch_llm.call_args.args[0] + assert isinstance(forwarded.api_key, SecretStr) + assert forwarded.api_key.get_secret_value() == "plaintext-or-secret" + assert forwarded.base_url == "https://openrouter.ai/api/v1" + finally: + client.app.dependency_overrides.clear() def test_switch_conversation_llm_inline_key_without_provider_connection( @@ -2900,46 +2886,31 @@ def test_switch_conversation_llm_missing_provider_connection_rejects_before_swit ): """A dangling provider_connection_id (and no inline key) surfaces 422 before the working LLM is replaced.""" - import tempfile - from pathlib import Path - - from openhands.agent_server.persistence import store as store_module - from openhands.sdk.llm.provider_connection_store import ProviderConnectionStore - - with tempfile.TemporaryDirectory() as tmpdir: - base = Path(tmpdir) - provider_store = ProviderConnectionStore(base_dir=base / "connections") - profile_store = LLMProfileStore( - base_dir=base / "profiles", provider_store=provider_store - ) - - mock_conversation = MagicMock() - mock_conversation_service.get_event_service.return_value = mock_event_service - mock_event_service.get_conversation.return_value = mock_conversation - client.app.dependency_overrides[get_conversation_service] = lambda: ( - mock_conversation_service - ) - - old_store = store_module._llm_profile_store - store_module._llm_profile_store = profile_store - try: - response = client.post( - f"/api/conversations/{sample_conversation_id}/switch_llm", - json={ - "llm": { - "model": "openrouter/z-ai/glm-5.3-flash", - "provider_connection_id": "ghost-conn", - "usage_id": "caller-supplied-id", - } - }, - ) + # The server's connection store is empty: nothing named ``ghost-conn``. + mock_conversation = MagicMock() + mock_conversation_service.get_event_service.return_value = mock_event_service + mock_event_service.get_conversation.return_value = mock_conversation + client.app.dependency_overrides[get_conversation_service] = lambda: ( + mock_conversation_service + ) - assert response.status_code == 422 - assert "ghost-conn" in response.json()["detail"] - mock_conversation.switch_llm.assert_not_called() - finally: - store_module._llm_profile_store = old_store - client.app.dependency_overrides.clear() + try: + response = client.post( + f"/api/conversations/{sample_conversation_id}/switch_llm", + json={ + "llm": { + "model": "openrouter/z-ai/glm-5.3-flash", + "provider_connection_id": "ghost-conn", + "usage_id": "caller-supplied-id", + } + }, + ) + + assert response.status_code == 422 + assert "ghost-conn" in response.json()["detail"] + mock_conversation.switch_llm.assert_not_called() + finally: + client.app.dependency_overrides.clear() def _provider_connection(connection_id: str, *, api_key: str, base_url: str | None): diff --git a/tests/agent_server/test_conversation_router_skill_trim.py b/tests/agent_server/test_conversation_router_skill_trim.py index b910fb1a4d9..c1ce0f7f578 100644 --- a/tests/agent_server/test_conversation_router_skill_trim.py +++ b/tests/agent_server/test_conversation_router_skill_trim.py @@ -38,6 +38,7 @@ from openhands.sdk.conversation.state import ConversationExecutionStatus from openhands.sdk.skills import Skill from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import install_test_context def _make_skill(name: str, content: str = "skill body bytes") -> Skill: @@ -156,8 +157,9 @@ def client(self, heavy_conversation): app = FastAPI() app.include_router(conversation_router, prefix="/api") - app.state.config = Config( - static_files_path=None, session_api_keys=[], secret_key=None + install_test_context( + app, + Config(static_files_path=None, session_api_keys=[], secret_key=None), ) app.dependency_overrides[get_conversation_service] = lambda: service return TestClient(app), heavy_conversation @@ -240,8 +242,9 @@ def test_batch_get_handles_null_items(self): service.batch_get_conversations.return_value = [None] app = FastAPI() app.include_router(conversation_router, prefix="/api") - app.state.config = Config( - static_files_path=None, session_api_keys=[], secret_key=None + install_test_context( + app, + Config(static_files_path=None, session_api_keys=[], secret_key=None), ) app.dependency_overrides[get_conversation_service] = lambda: service c = TestClient(app) diff --git a/tests/agent_server/test_conversation_service.py b/tests/agent_server/test_conversation_service.py index a8475a8eff8..cdfd6afff51 100644 --- a/tests/agent_server/test_conversation_service.py +++ b/tests/agent_server/test_conversation_service.py @@ -3832,22 +3832,22 @@ async def test_autotitle_uses_llm_profile_when_configured(self): service = self._make_service(title_llm_profile="cheap-model") mock_llm = LLM(model="gpt-3.5-turbo", usage_id="title-llm") + profile_store = MagicMock(spec=LLMProfileStore) + with ( - patch( - "openhands.agent_server.persistence.store.get_llm_profile_store" - ) as MockStore, patch( self._GENERATE_TITLE_PATH, return_value="✨ Profile LLM Title" ) as mock_generate_title, ): - mock_store_instance = MockStore.return_value + mock_store_instance = profile_store mock_store_instance.load.return_value = mock_llm - subscriber = AutoTitleSubscriber(service=service) + subscriber = AutoTitleSubscriber( + service=service, llm_profile_store=profile_store + ) await subscriber(self._user_message_event()) await self._drain_title_task(lambda: service.stored.title is not None) - MockStore.assert_called_once_with() mock_store_instance.load.assert_called_once_with( "cheap-model", cipher=service.cipher ) @@ -3864,20 +3864,21 @@ async def test_autotitle_falls_back_to_agent_llm_when_profile_not_found(self): service = self._make_service(title_llm_profile="nonexistent-profile") agent_llm = service._conversation.agent.llm + profile_store = MagicMock(spec=LLMProfileStore) + with ( - patch( - "openhands.agent_server.persistence.store.get_llm_profile_store" - ) as MockStore, patch( self._GENERATE_TITLE_PATH, return_value="✨ Agent LLM Title" ) as mock_generate_title, ): - mock_store_instance = MockStore.return_value + mock_store_instance = profile_store mock_store_instance.load.side_effect = FileNotFoundError( "Profile 'nonexistent-profile' not found" ) - subscriber = AutoTitleSubscriber(service=service) + subscriber = AutoTitleSubscriber( + service=service, llm_profile_store=profile_store + ) await subscriber(self._user_message_event()) await self._drain_title_task(lambda: service.stored.title is not None) @@ -3914,18 +3915,19 @@ async def test_autotitle_handles_profile_load_value_error(self): service = self._make_service(title_llm_profile="corrupted-profile") agent_llm = service._conversation.agent.llm + profile_store = MagicMock(spec=LLMProfileStore) + with ( - patch( - "openhands.agent_server.persistence.store.get_llm_profile_store" - ) as MockStore, patch( self._GENERATE_TITLE_PATH, return_value="✨ Agent LLM Title" ) as mock_generate_title, ): - mock_store_instance = MockStore.return_value + mock_store_instance = profile_store mock_store_instance.load.side_effect = ValueError("Invalid profile format") - subscriber = AutoTitleSubscriber(service=service) + subscriber = AutoTitleSubscriber( + service=service, llm_profile_store=profile_store + ) await subscriber(self._user_message_event()) await self._drain_title_task(lambda: service.stored.title is not None) @@ -3967,16 +3969,17 @@ async def test_autotitle_uses_title_profile_for_settings_built_acp_agent(self): service._conversation.agent.llm = ACPAgentSettings().create_agent().llm profile_llm = LLM(model="gpt-3.5-turbo", usage_id="title-llm") + profile_store = MagicMock(spec=LLMProfileStore) + with ( - patch( - "openhands.agent_server.persistence.store.get_llm_profile_store" - ) as MockStore, patch( self._TITLE_WITH_LLM_PATH, return_value="Profile Title" ) as mock_llm_title, ): - MockStore.return_value.load.return_value = profile_llm - subscriber = AutoTitleSubscriber(service=service) + profile_store.load.return_value = profile_llm + subscriber = AutoTitleSubscriber( + service=service, llm_profile_store=profile_store + ) await subscriber(self._user_message_event()) await self._drain_title_task(lambda: service.stored.title is not None) @@ -4044,23 +4047,17 @@ def fake_generate(self_llm, _messages, **_kwargs): raw_response=raw, ) - # Point the agent-server profile store singleton at our tmp dir via - # OH_PERSISTENCE_DIR so the real _load_title_llm code path finds our - # on-disk profile under `{tmp_path}/profiles`. - from openhands.agent_server.persistence import reset_stores - - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path)) - reset_stores() - # Clear the singleton at teardown even if an assertion raises, so the - # stale store (pointing at the soon-deleted tmp_path) can't leak. - request.addfinalizer(reset_stores) - with patch( "openhands.sdk.llm.llm.LLM.generate", autospec=True, side_effect=fake_generate, ): - subscriber = AutoTitleSubscriber(service=service) + # The real _load_title_llm code path, reading the on-disk + # profile under `{tmp_path}/profiles`. + subscriber = AutoTitleSubscriber( + service=service, + llm_profile_store=LLMProfileStore(base_dir=profile_dir), + ) await subscriber(self._user_message_event("Fix the login bug")) # Wait for the background executor task to complete. The production # code uses run_in_executor, so sleep(0) is not enough. @@ -4141,20 +4138,17 @@ def fake_generate(self_llm, _messages, **_kwargs): raw_response=raw, ) - from openhands.agent_server.persistence import reset_stores - - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path)) - reset_stores() - # Clear the singleton at teardown even if an assertion raises, so the - # stale store (pointing at the soon-deleted tmp_path) can't leak. - request.addfinalizer(reset_stores) - with patch( "openhands.sdk.llm.llm.LLM.generate", autospec=True, side_effect=fake_generate, ): - subscriber = AutoTitleSubscriber(service=service) + # The real _load_title_llm code path, reading the on-disk + # profile under `{tmp_path}/profiles`. + subscriber = AutoTitleSubscriber( + service=service, + llm_profile_store=LLMProfileStore(base_dir=profile_dir), + ) await subscriber(self._user_message_event("Fix the login bug")) for _ in range(50): await asyncio.sleep(0.02) diff --git a/tests/agent_server/test_conversation_tags.py b/tests/agent_server/test_conversation_tags.py index f76a762f5de..ad89f568e6b 100644 --- a/tests/agent_server/test_conversation_tags.py +++ b/tests/agent_server/test_conversation_tags.py @@ -23,11 +23,13 @@ from openhands.sdk.conversation.state import ConversationExecutionStatus from openhands.sdk.security.confirmation_policy import NeverConfirm from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import install_test_context @pytest.fixture def client(): app = FastAPI() + install_test_context(app) app.include_router(conversation_router, prefix="/api") return TestClient(app) diff --git a/tests/agent_server/test_credential_binding.py b/tests/agent_server/test_credential_binding.py index d49cfe45545..4b94012c39f 100644 --- a/tests/agent_server/test_credential_binding.py +++ b/tests/agent_server/test_credential_binding.py @@ -3,6 +3,7 @@ from collections.abc import Generator from dataclasses import dataclass, field from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path from unittest.mock import AsyncMock, call, patch from uuid import uuid4 @@ -30,6 +31,7 @@ CustomSecret, FileSecretsStore, Secrets, + ServerStores, ) from openhands.sdk import LLM, Agent, AgentContext from openhands.sdk.agent import ACPAgent @@ -42,6 +44,7 @@ from openhands.sdk.secret import StaticSecret from openhands.sdk.utils.cipher import Cipher from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import install_test_context @dataclass @@ -77,6 +80,11 @@ def log_message(self, format: str, *args: object) -> None: thread.join() +def _server_stores(directory: Path) -> ServerStores: + """The stores of a keyless server that persists under ``directory``.""" + return ServerStores(cipher=None, profile_dir=directory, workspace_dir=directory) + + @pytest.mark.asyncio async def test_local_binding_versions_and_conflicts(tmp_path) -> None: store = FileSecretsStore(tmp_path) @@ -199,7 +207,7 @@ def test_activation_route_installs_http_binding( ) -> None: service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() - app.state.conversation_service = service + install_test_context(app, conversation_service=service) app.include_router(router, prefix="/api") conversation_id = uuid4() callback_url, callback = credential_callback @@ -229,7 +237,7 @@ def test_activation_route_rejects_unavailable_binding( ) -> None: service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() - app.state.conversation_service = service + install_test_context(app, conversation_service=service) app.include_router(router, prefix="/api") conversation_id = uuid4() callback_url, callback = credential_callback @@ -257,7 +265,7 @@ def test_activation_route_retries_transient_binding_failure( ) -> None: service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() - app.state.conversation_service = service + install_test_context(app, conversation_service=service) app.include_router(router, prefix="/api") conversation_id = uuid4() callback_url, callback = credential_callback @@ -303,7 +311,7 @@ def test_activation_route_rejects_terminal_binding_failure( ) -> None: service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() - app.state.conversation_service = service + install_test_context(app, conversation_service=service) app.include_router(router, prefix="/api") conversation_id = uuid4() callback_url, callback = credential_callback @@ -336,7 +344,7 @@ def test_activation_route_rejects_invalid_binding_url( ) -> None: service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() - app.state.conversation_service = service + install_test_context(app, conversation_service=service) app.include_router(router, prefix="/api") conversation_id = uuid4() @@ -357,11 +365,12 @@ def test_activation_route_rejects_invalid_binding_url( @pytest.mark.asyncio async def test_direct_conversations_share_rotated_canonical_value(tmp_path) -> None: - store = FileSecretsStore(tmp_path / "settings") + stores = _server_stores(tmp_path / "settings") + store = stores.secrets store.set_secret("CODEX_AUTH_JSON", "r0") service = ConversationService( conversations_dir=tmp_path / "conversations", - secrets_store=store, + stores=stores, ) agent = ACPAgent(acp_command=["codex-acp"], acp_server="codex") workspace = LocalWorkspace(working_dir=tmp_path / "workspace") @@ -381,7 +390,8 @@ async def test_direct_conversations_share_rotated_canonical_value(tmp_path) -> N @pytest.mark.asyncio async def test_direct_start_strips_reserved_conversation_secret(tmp_path) -> None: - store = FileSecretsStore(tmp_path / "settings") + stores = _server_stores(tmp_path / "settings") + store = stores.secrets store.set_secret("CODEX_AUTH_JSON", "canonical") request = StartConversationRequest( agent=ACPAgent(acp_command=["codex-acp"], acp_server="codex"), @@ -391,7 +401,7 @@ async def test_direct_start_strips_reserved_conversation_secret(tmp_path) -> Non async with ConversationService( conversations_dir=tmp_path / "conversations", - secrets_store=store, + stores=stores, ) as service: info, _ = await service.start_conversation(request) event_service = await service.get_event_service(info.id) @@ -586,7 +596,8 @@ async def test_one_off_credential_survives_cold_restart_without_binding( @pytest.mark.asyncio async def test_managed_start_scrubs_all_durable_credential_copies(tmp_path) -> None: - store = FileSecretsStore(tmp_path / "settings") + stores = _server_stores(tmp_path / "settings") + store = stores.secrets store.set_secret("CODEX_AUTH_JSON", "canonical") agent = ACPAgent( acp_command=["codex-acp"], @@ -609,7 +620,7 @@ async def test_managed_start_scrubs_all_durable_credential_copies(tmp_path) -> N async with ConversationService( conversations_dir=tmp_path / "conversations", - secrets_store=store, + stores=stores, ) as service: info, _ = await service.start_conversation(request) event_service = await service.get_event_service(info.id) @@ -932,7 +943,8 @@ def restart_while_locked(agent, secret_names) -> None: @pytest.mark.asyncio async def test_resume_uses_plaintext_fallback_without_binding(tmp_path) -> None: - store = FileSecretsStore(tmp_path / "settings") + stores = _server_stores(tmp_path / "settings") + store = stores.secrets store.set_secret("CODEX_AUTH_JSON", "canonical") request = StartConversationRequest( agent=ACPAgent(acp_command=["codex-acp"], acp_server="codex"), @@ -942,7 +954,7 @@ async def test_resume_uses_plaintext_fallback_without_binding(tmp_path) -> None: async with ConversationService( conversations_dir=tmp_path / "conversations", - secrets_store=store, + stores=stores, ) as service: info, _ = await service.start_conversation(request) assert service._event_services is not None @@ -1070,11 +1082,12 @@ async def test_non_owner_does_not_scrub_persisted_credential(tmp_path) -> None: before_meta = (conversation_dir / "meta.json").read_bytes() before_state = (conversation_dir / "base_state.json").read_bytes() - canonical = FileSecretsStore(tmp_path / "settings") + canonical_stores = _server_stores(tmp_path / "settings") + canonical = canonical_stores.secrets canonical.set_secret("CODEX_AUTH_JSON", "canonical") async with ConversationService( conversations_dir=conversations_dir, - secrets_store=canonical, + stores=canonical_stores, ) as non_owner: _, started = await non_owner.start_conversation( request.model_copy(update={"conversation_id": info.id, "secrets": {}}) @@ -1087,7 +1100,8 @@ async def test_non_owner_does_not_scrub_persisted_credential(tmp_path) -> None: @pytest.mark.asyncio async def test_resume_removes_legacy_persisted_credential(tmp_path) -> None: - store = FileSecretsStore(tmp_path / "settings") + stores = _server_stores(tmp_path / "settings") + store = stores.secrets agent = ACPAgent( acp_command=["codex-acp"], acp_server="codex", @@ -1107,7 +1121,7 @@ async def test_resume_removes_legacy_persisted_credential(tmp_path) -> None: async with ConversationService( conversations_dir=tmp_path / "conversations", - secrets_store=store, + stores=stores, ) as service: info, _ = await service.start_conversation(request) assert service._event_services is not None @@ -1154,7 +1168,8 @@ async def test_resume_removes_legacy_persisted_credential(tmp_path) -> None: @pytest.mark.asyncio async def test_failed_canonical_preflight_preserves_legacy_auth_file(tmp_path) -> None: - store = FileSecretsStore(tmp_path / "settings") + stores = _server_stores(tmp_path / "settings") + store = stores.secrets request = StartConversationRequest( agent=ACPAgent(acp_command=["codex-acp"], acp_server="codex"), workspace=LocalWorkspace(working_dir=tmp_path / "workspace"), @@ -1162,7 +1177,7 @@ async def test_failed_canonical_preflight_preserves_legacy_auth_file(tmp_path) - async with ConversationService( conversations_dir=tmp_path / "conversations", - secrets_store=store, + stores=stores, ) as service: info, _ = await service.start_conversation(request) assert service._event_services is not None diff --git a/tests/agent_server/test_dependencies.py b/tests/agent_server/test_dependencies.py index b3c6571befc..d39022654e9 100644 --- a/tests/agent_server/test_dependencies.py +++ b/tests/agent_server/test_dependencies.py @@ -8,11 +8,12 @@ from openhands.agent_server.config import Config from openhands.agent_server.dependencies import check_session_api_key +from tests.agent_server.conftest import install_test_context def _make_app(session_api_keys: list[str]) -> FastAPI: app = FastAPI() - app.state.config = Config(session_api_keys=session_api_keys) + install_test_context(app, Config(session_api_keys=session_api_keys)) @app.get("/test", dependencies=[Depends(check_session_api_key)]) async def test_endpoint(): @@ -52,7 +53,7 @@ def test_check_session_api_key_no_keys_configured(): def test_check_session_api_key_reflects_config_update(): - """Updating app.state.config is reflected immediately; no route re-registration needed.""" # noqa: E501 + """A new server context is reflected immediately; no route re-registration.""" app = _make_app(["old-key"]) client = TestClient(app, raise_server_exceptions=False) @@ -60,7 +61,7 @@ def test_check_session_api_key_reflects_config_update(): client.get("/test", headers={"X-Session-API-Key": "old-key"}).status_code == 200 ) - app.state.config = Config(session_api_keys=["new-key"]) + install_test_context(app, Config(session_api_keys=["new-key"])) assert ( client.get("/test", headers={"X-Session-API-Key": "new-key"}).status_code == 200 diff --git a/tests/agent_server/test_event_router.py b/tests/agent_server/test_event_router.py index 05a1b89adac..4b97b57f073 100644 --- a/tests/agent_server/test_event_router.py +++ b/tests/agent_server/test_event_router.py @@ -25,6 +25,7 @@ from openhands.sdk.event.llm_convertible.message import MessageEvent from openhands.sdk.io.memory import InMemoryFileStore from openhands.sdk.llm.message import ImageContent, TextContent +from tests.agent_server.conftest import install_test_context, update_test_context def test_normalize_datetime_naive_passthrough(): @@ -70,6 +71,7 @@ def test_normalize_datetime_fixed_offset_timezone(): def client(): """Create a test client for the FastAPI app without authentication.""" app = FastAPI() + install_test_context(app) app.include_router(event_router, prefix="/api") return TestClient(app) @@ -97,8 +99,10 @@ async def slow_load(conversation_id): await asyncio.sleep(0.04) return event_service - client.app.state.conversation_service = SimpleNamespace(get_event_service=slow_load) - client.app.state.conversation_registry = None + update_test_context( + client.app, + conversation_service=SimpleNamespace(get_event_service=slow_load), + ) response = client.post( f"/api/conversations/{uuid4()}/events/_gateway/run", diff --git a/tests/agent_server/test_event_router_websocket.py b/tests/agent_server/test_event_router_websocket.py index 6ba707dd914..662a046dd98 100644 --- a/tests/agent_server/test_event_router_websocket.py +++ b/tests/agent_server/test_event_router_websocket.py @@ -187,12 +187,14 @@ async def test_websocket_disconnect_breaks_loop( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -214,12 +216,14 @@ async def test_websocket_no_double_unsubscription( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -250,13 +254,15 @@ def side_effect(): with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, patch("openhands.agent_server.sockets.logger.exception") as log_exception, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -290,12 +296,14 @@ def side_effect(): with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -318,13 +326,15 @@ async def test_disconnect_and_unsubscribe_when_send_error_fails( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, patch("openhands.agent_server.sockets.logger.debug") as log_debug, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -347,12 +357,14 @@ async def test_resend_mode_none_no_resend( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -389,12 +401,14 @@ async def test_resend_mode_all_resends_events( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -433,12 +447,14 @@ async def test_resend_mode_since_with_timestamp( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -464,13 +480,15 @@ async def test_resend_mode_since_without_timestamp_logs_warning( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, patch("openhands.agent_server.sockets.logger") as mock_logger, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -510,12 +528,14 @@ async def test_resend_mode_since_timezone_aware_is_normalized( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -560,13 +580,15 @@ async def test_deprecated_resend_all_true_still_works( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, patch("openhands.agent_server.sockets.logger") as mock_logger, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -596,12 +618,14 @@ async def test_deprecated_resend_all_false_no_resend( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket @@ -624,13 +648,15 @@ async def test_resend_mode_takes_precedence_over_resend_all( with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, patch("openhands.agent_server.sockets.logger") as mock_logger, ): mock_config.return_value.session_api_keys = None - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) from openhands.agent_server.sockets import events_socket diff --git a/tests/agent_server/test_event_socket_races.py b/tests/agent_server/test_event_socket_races.py index f6138500c4f..2dd5954779f 100644 --- a/tests/agent_server/test_event_socket_races.py +++ b/tests/agent_server/test_event_socket_races.py @@ -16,6 +16,7 @@ from openhands.agent_server.event_service import EventService from openhands.agent_server.models import StoredConversation from openhands.agent_server.pub_sub import PubSub, Subscriber +from openhands.agent_server.server_context import build_server_context from openhands.sdk import Message, TextContent from openhands.sdk.agent import Agent from openhands.sdk.conversation.state import ConversationExecutionStatus @@ -40,7 +41,9 @@ def __init__(self) -> None: self.client_state = WebSocketState.CONNECTED self.headers: dict = {} self.app = SimpleNamespace( - state=SimpleNamespace(config=Config(session_api_keys=[])) + state=SimpleNamespace( + server_context=build_server_context(Config(session_api_keys=[])) + ) ) async def accept(self): @@ -99,11 +102,11 @@ async def svc(tmp_path, monkeypatch): async def get_event_service(_cid): return service - monkeypatch.setattr( - sockets, - "conversation_service", - SimpleNamespace(get_event_service=get_event_service), - ) + conversation_service = SimpleNamespace(get_event_service=get_event_service) + for module in (sockets, session_socket): + monkeypatch.setattr( + module, "get_conversation_service", lambda _ws: conversation_service + ) yield service if service.is_open(): await service.close() diff --git a/tests/agent_server/test_file_router.py b/tests/agent_server/test_file_router.py index 52735f82167..06364cdff80 100644 --- a/tests/agent_server/test_file_router.py +++ b/tests/agent_server/test_file_router.py @@ -19,15 +19,13 @@ from fastapi import UploadFile from fastapi.testclient import TestClient -from openhands.agent_server import ( - bash_service as bash_mod, - conversation_service as cs_mod, - file_router as file_router_module, -) +from openhands.agent_server import file_router as file_router_module from openhands.agent_server.api import create_app from openhands.agent_server.config import Config from openhands.agent_server.file_router import ARCHIVE_MANIFEST_NAME, _upload_file +from openhands.agent_server.server_context import server_context_of from openhands.sdk.utils.pydantic_secrets import REDACTED_SECRET_VALUE +from tests.agent_server.conftest import update_test_context @pytest.fixture @@ -380,12 +378,13 @@ def test_upload_file_with_special_characters_in_path(client, tmp_path): def _use_conversations_path(client, conversations_path: Path) -> None: """Point the app's request-time config at ``conversations_path``. - Trajectory downloads resolve the conversations directory from - ``request.app.state.config`` — the same config ``POST /api/init`` + Trajectory downloads resolve the conversations directory from the + config of the server context — the same config ``POST /api/init`` replaces on a deferred-init server. """ - client.app.state.config = Config( - session_api_keys=[], conversations_path=conversations_path + update_test_context( + client.app, + config=Config(session_api_keys=[], conversations_path=conversations_path), ) @@ -748,10 +747,10 @@ def read_then_save(self, *args, **kwargs): def test_download_trajectory_resolves_dir_from_request_config( client, monkeypatch, tmp_path ): - """The served directory comes from ``request.app.state.config``. + """The served directory comes from the server context's config. - ``POST /api/init`` on a deferred-init server only replaces that config; - the launch-time config lookup must not decide where the route looks. + ``POST /api/init`` on a deferred-init server replaces that context; the + config the app was created with must not decide where the route looks. """ request_path = tmp_path / "request-conversations" conversation_id = uuid4() @@ -760,10 +759,6 @@ def test_download_trajectory_resolves_dir_from_request_config( (conversation_dir / "meta.json").write_text("{}") (conversation_dir / "events" / "event-00000.json").write_text('{"id": "e0"}') - monkeypatch.setattr( - "openhands.agent_server.config.get_default_config", - lambda: Config(session_api_keys=[], conversations_path=tmp_path / "elsewhere"), - ) _use_conversations_path(client, request_path) response = client.get(f"/api/file/download-trajectory/{conversation_id}") @@ -777,12 +772,10 @@ def test_deferred_init_trajectory_downloads_follow_init_conversations_path(tmp_p """Both trajectory routes work after ``/api/init`` moves storage. Regression test for a deferred-init (warm-pool) server: ``/api/init`` - installs the per-user ``conversations_path`` on ``app.state.config``, so + installs the per-user ``conversations_path`` in the server context, so both the flat and the conversation-scoped download route must serve the conversation from that directory instead of the launch-time default. """ - cs_mod._conversation_service = None - bash_mod._bash_event_service = None boot_conversations = tmp_path / "boot-conversations" user_conversations = tmp_path / "user" / "conversations" app = create_app( @@ -793,75 +786,74 @@ def test_deferred_init_trajectory_downloads_follow_init_conversations_path(tmp_p ) ) headers = {"X-Session-API-Key": "init-key"} - try: - with TestClient(app) as client: - response = client.post( - "/api/init", - json={ - "session_api_keys": ["init-key"], - "conversations_path": str(user_conversations), - "bash_events_dir": str(tmp_path / "user" / "bash_events"), - }, - ) - assert response.status_code == 200, response.text - - response = client.post( - "/api/conversations", - headers=headers, - json={ - "workspace": {"working_dir": str(tmp_path / "workspace")}, - "agent": { - "kind": "Agent", - "llm": {"model": "openai/test", "usage_id": "test"}, - }, + with TestClient(app) as client: + response = client.post( + "/api/init", + json={ + "session_api_keys": ["init-key"], + "conversations_path": str(user_conversations), + "bash_events_dir": str(tmp_path / "user" / "bash_events"), + }, + ) + assert response.status_code == 200, response.text + + response = client.post( + "/api/conversations", + headers=headers, + json={ + "workspace": {"working_dir": str(tmp_path / "workspace")}, + "agent": { + "kind": "Agent", + "llm": {"model": "openai/test", "usage_id": "test"}, }, - ) - assert response.status_code == 201, response.text - conversation_id = UUID(response.json()["id"]) - assert (user_conversations / conversation_id.hex).is_dir() - assert not (boot_conversations / conversation_id.hex).exists() - - flat = client.get( - f"/api/file/download-trajectory/{conversation_id}", headers=headers - ) - scoped = client.get( - f"/api/conversations/{conversation_id}/file/download-trajectory/" - f"{conversation_id}", - headers=headers, - ) - - assert flat.status_code == 200, flat.text - assert scoped.status_code == 200, scoped.text - - root = conversation_id.hex - with zipfile.ZipFile(io.BytesIO(flat.content)) as archive: - flat_names = set(archive.namelist()) - with zipfile.ZipFile(io.BytesIO(scoped.content)) as archive: - scoped_names = set(archive.namelist()) - assert flat_names == scoped_names - assert f"{root}/meta.json" in flat_names - assert f"{root}/base_state.json" in flat_names - - # No conversation directory under the resolved config → 404 on - # both routes. Rebinding the config stands in for a conversation - # whose storage moved away from the initialized path. - app.state.config = app.state.config.model_copy( + }, + ) + assert response.status_code == 201, response.text + conversation_id = UUID(response.json()["id"]) + assert (user_conversations / conversation_id.hex).is_dir() + assert not (boot_conversations / conversation_id.hex).exists() + + flat = client.get( + f"/api/file/download-trajectory/{conversation_id}", headers=headers + ) + scoped = client.get( + f"/api/conversations/{conversation_id}/file/download-trajectory/" + f"{conversation_id}", + headers=headers, + ) + + assert flat.status_code == 200, flat.text + assert scoped.status_code == 200, scoped.text + + root = conversation_id.hex + with zipfile.ZipFile(io.BytesIO(flat.content)) as archive: + flat_names = set(archive.namelist()) + with zipfile.ZipFile(io.BytesIO(scoped.content)) as archive: + scoped_names = set(archive.namelist()) + assert flat_names == scoped_names + assert f"{root}/meta.json" in flat_names + assert f"{root}/base_state.json" in flat_names + + # No conversation directory under the resolved config → 404 on + # both routes. Rebinding the config stands in for a conversation + # whose storage moved away from the initialized path. + update_test_context( + app, + config=server_context_of(app).config.model_copy( update={"conversations_path": tmp_path / "elsewhere"} - ) - for url in ( - f"/api/file/download-trajectory/{conversation_id}", - f"/api/conversations/{conversation_id}/file/download-trajectory/" - f"{conversation_id}", - ): - missing = client.get(url, headers=headers) - assert missing.status_code == 404, missing.text - assert missing.json()["detail"] == "Conversation not found" - - # Repeated downloads leave no zip residue behind. - assert list(user_conversations.glob("*.zip")) == [] - finally: - cs_mod._conversation_service = None - bash_mod._bash_event_service = None + ), + ) + for url in ( + f"/api/file/download-trajectory/{conversation_id}", + f"/api/conversations/{conversation_id}/file/download-trajectory/" + f"{conversation_id}", + ): + missing = client.get(url, headers=headers) + assert missing.status_code == 404, missing.text + assert missing.json()["detail"] == "Conversation not found" + + # Repeated downloads leave no zip residue behind. + assert list(user_conversations.glob("*.zip")) == [] def test_download_file_with_special_characters_in_path(client, tmp_path): diff --git a/tests/agent_server/test_git_provider_service.py b/tests/agent_server/test_git_provider_service.py index c1da0ef65b1..654ae61fd57 100644 --- a/tests/agent_server/test_git_provider_service.py +++ b/tests/agent_server/test_git_provider_service.py @@ -10,9 +10,15 @@ GitProviderAPIError, search_provider_repositories, ) +from openhands.agent_server.persistence import SecretsStore, ServerStores from openhands.sdk.workspace.repo import GitProvider +def _secrets_store() -> SecretsStore: + """The secrets store of a server with the default config.""" + return ServerStores.from_config(Config()).secrets + + class FakeResponse: def __init__( self, @@ -95,7 +101,7 @@ def __init__(self, **_kwargs): async def test_search_provider_repositories_reports_missing_token(monkeypatch): _patch_provider_token(monkeypatch, None) - result = await search_provider_repositories(Config(), GitProvider.GITHUB) + result = await search_provider_repositories(_secrets_store(), GitProvider.GITHUB) assert result.items == [] assert result.next_page_id is None @@ -123,7 +129,7 @@ async def test_search_provider_repositories_maps_github_repositories(monkeypatch ) result = await search_provider_repositories( - Config(), + _secrets_store(), GitProvider.GITHUB, limit=30, page_id="2", @@ -171,7 +177,7 @@ async def test_search_provider_repositories_filters_across_pages(monkeypatch): ) result = await search_provider_repositories( - Config(), GitProvider.GITHUB, query="software", limit=2 + _secrets_store(), GitProvider.GITHUB, query="software", limit=2 ) assert captured["calls"] == [ @@ -218,7 +224,7 @@ async def test_search_provider_repositories_continues_from_offset_cursor(monkeyp ) result = await search_provider_repositories( - Config(), GitProvider.GITHUB, query="software", limit=1, page_id="2:1" + _secrets_store(), GitProvider.GITHUB, query="software", limit=1, page_id="2:1" ) assert [item.full_name for item in result.items] == ["OpenHands/software-agents"] @@ -231,7 +237,7 @@ async def test_search_provider_repositories_rejects_invalid_page_id(monkeypatch) with pytest.raises(GitProviderAPIError) as exc_info: await search_provider_repositories( - Config(), GitProvider.GITHUB, page_id="not-a-page" + _secrets_store(), GitProvider.GITHUB, page_id="not-a-page" ) assert exc_info.value.status_code == 400 @@ -249,10 +255,10 @@ async def test_search_provider_repositories_cursor_survives_limit_changes( ) first = await search_provider_repositories( - Config(), GitProvider.GITHUB, query="OpenHands/r", limit=4 + _secrets_store(), GitProvider.GITHUB, query="OpenHands/r", limit=4 ) second = await search_provider_repositories( - Config(), + _secrets_store(), GitProvider.GITHUB, query="OpenHands/r", limit=2, @@ -286,7 +292,7 @@ async def test_search_provider_repositories_maps_provider_status( _patch_github_client(monkeypatch, [FakeResponse(status_code=provider_status)]) with pytest.raises(GitProviderAPIError) as exc_info: - await search_provider_repositories(Config(), GitProvider.GITHUB) + await search_provider_repositories(_secrets_store(), GitProvider.GITHUB) assert exc_info.value.status_code == expected_status @@ -314,6 +320,6 @@ async def get(self, _path, params): ) with pytest.raises(GitProviderAPIError) as exc_info: - await search_provider_repositories(Config(), GitProvider.GITHUB) + await search_provider_repositories(_secrets_store(), GitProvider.GITHUB) assert exc_info.value.status_code == 504 diff --git a/tests/agent_server/test_init_router.py b/tests/agent_server/test_init_router.py index c3a353d31ed..4cccb983383 100644 --- a/tests/agent_server/test_init_router.py +++ b/tests/agent_server/test_init_router.py @@ -16,7 +16,7 @@ from uuid import UUID, uuid4 import pytest -from fastapi import Request, Response, status +from fastapi import FastAPI, Request, Response, status from fastapi.testclient import TestClient from fastapi.websockets import WebSocketDisconnect from pydantic import SecretStr @@ -30,6 +30,11 @@ _build_initialized_config, require_initialized, ) +from openhands.agent_server.server_context import ( + build_server_context, + install_server_context, + server_context_of, +) from openhands.agent_server.vscode_service import VSCodeService @@ -51,58 +56,41 @@ def _clean_env(monkeypatch): @pytest.fixture(autouse=True) def _fresh_vscode_service(monkeypatch): - """/api/init updates the VSCode service singleton; give each test its own. - - The service takes its token from the default config, which is loaded at - import time, before ``_clean_env`` runs, so reset that too. - """ + """/api/init updates the VSCode service singleton; give each test its own.""" monkeypatch.setattr("openhands.agent_server.vscode_service._vscode_service", None) - monkeypatch.setattr("openhands.agent_server.config._default_config", None) - - -def _reset_conversation_singleton(): - """Some tests build their own ConversationService; reset the module-level - cache so unrelated tests don't see leftover state.""" - from openhands.agent_server import conversation_service as cs_mod - - cs_mod._conversation_service = None - -def _reset_bash_singleton(): - """Reset the module-level BashEventService cache so each test starts fresh.""" - from openhands.agent_server import bash_service as bash_mod - bash_mod._bash_event_service = None +def _dormant_app(config: Config) -> FastAPI: + """A bare app holding the dormant server context, as ``create_app`` leaves it.""" + app = FastAPI() + install_server_context(app, build_server_context(config)) + return app def _vscode_url_after_init(tmp_path: Path) -> str | None: """Boot a dormant app, deliver a session key through /api/init, and read /api/vscode/url with that key.""" - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - resp = client.post( - "/api/init", - json={ - "session_api_keys": ["user-session-key"], - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 + resp = client.post( + "/api/init", + json={ + "session_api_keys": ["user-session-key"], + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 - resp = client.get( - "/api/vscode/url", headers={"X-Session-API-Key": "user-session-key"} - ) - assert resp.status_code == 200 - return resp.json()["url"] - finally: - _reset_conversation_singleton() + resp = client.get( + "/api/vscode/url", headers={"X-Session-API-Key": "user-session-key"} + ) + assert resp.status_code == 200 + return resp.json()["url"] class TestConfigDefaults: @@ -196,8 +184,6 @@ def test_init_get_404_without_deferred_mode(self): class TestInitServiceTransitions: @pytest.mark.asyncio async def test_init_transitions_dormant_to_ready(self, tmp_path): - _reset_conversation_singleton() - _reset_bash_singleton() from openhands.agent_server.bash_service import BashEventService base = Config( @@ -205,8 +191,8 @@ async def test_init_transitions_dormant_to_ready(self, tmp_path): conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) assert svc.state == "dormant" result = await svc.initialize( @@ -219,28 +205,24 @@ async def test_init_transitions_dormant_to_ready(self, tmp_path): try: assert result.state == "ready" assert svc.state == "ready" - # New config landed on app.state with deferred_init cleared. - assert app.state.config.deferred_init is False - assert app.state.config.session_api_keys == ["user-key"] - assert app.state.conversation_service is not None - # BashEventService is registered on app.state so the bash - # websocket handler picks up the per-user bash_events_dir - # rather than the import-time default. - assert isinstance( - getattr(app.state, "bash_event_service", None), BashEventService - ) + # The new context carries the delivered config with + # deferred_init cleared. + context = server_context_of(app) + assert context.config.deferred_init is False + assert context.config.session_api_keys == ["user-key"] + assert context.conversation_service is not None + # Its BashEventService is the one the bash websocket handler + # picks up, so it uses the per-user bash_events_dir rather than + # the dormant one. + assert isinstance(context.bash_event_service, BashEventService) assert ( - app.state.bash_event_service.bash_events_dir - == tmp_path / "user" / "bash" + context.bash_event_service.bash_events_dir == tmp_path / "user" / "bash" ) finally: await svc.teardown() - _reset_conversation_singleton() - _reset_bash_singleton() @pytest.mark.asyncio async def test_second_init_rejected_with_400(self, tmp_path): - _reset_conversation_singleton() from fastapi import HTTPException base = Config( @@ -248,8 +230,8 @@ async def test_second_init_rejected_with_400(self, tmp_path): conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -264,11 +246,9 @@ async def test_second_init_rejected_with_400(self, tmp_path): assert "already in state" in str(excinfo.value.detail) finally: await svc.teardown() - _reset_conversation_singleton() @pytest.mark.asyncio async def test_init_applies_env_vars(self, tmp_path, monkeypatch): - _reset_conversation_singleton() # Pre-clean so the env var truly comes from /api/init. monkeypatch.delenv("DEFERRED_INIT_TEST_VAR", raising=False) observed_env = None @@ -286,8 +266,8 @@ def capture_observability_env(): conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -302,14 +282,11 @@ def capture_observability_env(): finally: await svc.teardown() monkeypatch.delenv("DEFERRED_INIT_TEST_VAR", raising=False) - _reset_conversation_singleton() @pytest.mark.asyncio async def test_init_bash_service_uses_user_supplied_dir(self, tmp_path): - """After /api/init, app.state.bash_event_service uses the + """After /api/init, the context's bash service uses the bash_events_dir supplied in the InitRequest.""" - _reset_conversation_singleton() - _reset_bash_singleton() from openhands.agent_server.bash_service import BashEventService base = Config( @@ -317,8 +294,8 @@ async def test_init_bash_service_uses_user_supplied_dir(self, tmp_path): conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "boot" / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) user_dir = tmp_path / "user" / "bash" await svc.initialize( @@ -328,27 +305,23 @@ async def test_init_bash_service_uses_user_supplied_dir(self, tmp_path): ) ) try: - bash_svc = app.state.bash_event_service + bash_svc = server_context_of(app).bash_event_service assert isinstance(bash_svc, BashEventService) assert bash_svc.bash_events_dir == user_dir finally: await svc.teardown() - _reset_conversation_singleton() - _reset_bash_singleton() @pytest.mark.asyncio async def test_init_teardown_releases_bash_service(self, tmp_path): """When /api/init is followed by teardown, the bash service must also be exited so its background tasks are released.""" - _reset_conversation_singleton() - _reset_bash_singleton() base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -356,11 +329,10 @@ async def test_init_teardown_releases_bash_service(self, tmp_path): bash_events_dir=tmp_path / "u" / "bash", ) ) - assert svc._entered_bash_service is not None + bash_svc = server_context_of(app).bash_event_service + assert bash_svc is not None and not bash_svc._closed await svc.teardown() - assert svc._entered_bash_service is None - _reset_conversation_singleton() - _reset_bash_singleton() + assert bash_svc._closed @pytest.mark.asyncio async def test_init_switches_vscode_to_first_session_key( @@ -368,18 +340,18 @@ async def test_init_switches_vscode_to_first_session_key( ): """VSCode booted with a random token; init switches it to the first session key, which a server that booted with the key would use.""" - _reset_conversation_singleton() vscode = SimpleNamespace(set_connection_token=AsyncMock()) monkeypatch.setattr( - "openhands.agent_server.init_router.get_vscode_service", lambda: vscode + "openhands.agent_server.init_router.get_vscode_service", + lambda _config: vscode, ) base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -392,25 +364,24 @@ async def test_init_switches_vscode_to_first_session_key( vscode.set_connection_token.assert_awaited_once_with("user-key") finally: await svc.teardown() - _reset_conversation_singleton() @pytest.mark.asyncio async def test_init_without_session_keys_keeps_vscode_token( self, tmp_path, monkeypatch ): """With no session key to switch to, VSCode keeps its boot token.""" - _reset_conversation_singleton() vscode = SimpleNamespace(set_connection_token=AsyncMock()) monkeypatch.setattr( - "openhands.agent_server.init_router.get_vscode_service", lambda: vscode + "openhands.agent_server.init_router.get_vscode_service", + lambda _config: vscode, ) base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -422,14 +393,12 @@ async def test_init_without_session_keys_keeps_vscode_token( vscode.set_connection_token.assert_not_awaited() finally: await svc.teardown() - _reset_conversation_singleton() class TestEndToEndOverLifespan: """Drive the whole flow through the FastAPI lifespan + TestClient.""" def test_blank_init_secret_falls_back_to_session_key(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", @@ -437,22 +406,20 @@ def test_blank_init_secret_falls_back_to_session_key(self, tmp_path): ) app = create_app(cfg) with TestClient(app) as client: - try: - response = client.post( - "/api/init", - json={ - "secret_key": "", - "session_api_keys": ["user-session-key"], - }, - ) - assert response.status_code == 200 - assert app.state.config.cipher.secret_key == "user-session-key" - finally: - _reset_conversation_singleton() + response = client.post( + "/api/init", + json={ + "secret_key": "", + "session_api_keys": ["user-session-key"], + }, + ) + assert response.status_code == 200 + cipher = server_context_of(app).config.cipher + assert cipher is not None + assert cipher.secret_key == "user-session-key" @pytest.mark.parametrize("blank", ["", " "]) def test_blank_secret_key_env_keeps_init_closed(self, tmp_path, monkeypatch, blank): - _reset_conversation_singleton() monkeypatch.setenv(CONFIG_PATH_ENV, str(tmp_path / "missing.json")) monkeypatch.setenv("OH_SECRET_KEY", blank) monkeypatch.setenv("OH_DEFERRED_INIT", "true") @@ -461,40 +428,33 @@ def test_blank_secret_key_env_keeps_init_closed(self, tmp_path, monkeypatch, bla cfg = load_config() assert cfg.deferred_init and cfg.secret_key is None with TestClient(create_app(cfg)) as client: - try: - assert client.post("/api/init", json={}).status_code == 401 - assert client.get("/api/init").json()["state"] == "dormant" - finally: - _reset_conversation_singleton() + assert client.post("/api/init", json={}).status_code == 401 + assert client.get("/api/init").json()["state"] == "dormant" @pytest.mark.parametrize("session_api_keys", [[""], ["", "user-session-key"]]) def test_init_rejects_empty_session_keys(self, tmp_path, session_api_keys): - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - response = client.post( - "/api/init", json={"session_api_keys": session_api_keys} - ) - assert response.status_code == 422 - assert "user-session-key" not in response.text - assert client.get("/api/init").json()["state"] == "dormant" + response = client.post( + "/api/init", json={"session_api_keys": session_api_keys} + ) + assert response.status_code == 422 + assert "user-session-key" not in response.text + assert client.get("/api/init").json()["state"] == "dormant" - response = client.post( - "/api/init", json={"session_api_keys": ["user-session-key"]} - ) - assert response.status_code == 200 - response = client.get( - "/api/conversations/count", - headers={"X-Session-API-Key": "user-session-key"}, - ) - assert response.status_code == 200 - finally: - _reset_conversation_singleton() + response = client.post( + "/api/init", json={"session_api_keys": ["user-session-key"]} + ) + assert response.status_code == 200 + response = client.get( + "/api/conversations/count", + headers={"X-Session-API-Key": "user-session-key"}, + ) + assert response.status_code == 200 @pytest.mark.parametrize( ("method", "path"), @@ -512,7 +472,6 @@ def test_init_rejects_empty_session_keys(self, tmp_path, session_api_keys): def test_dormant_readiness_precedes_authentication( self, tmp_path: Path, method: str, path: str ) -> None: - _reset_conversation_singleton() cfg = Config( deferred_init=True, session_api_keys=["dormant-key"], @@ -520,10 +479,7 @@ def test_dormant_readiness_precedes_authentication( bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - assert client.request(method, path).status_code == 503 - finally: - _reset_conversation_singleton() + assert client.request(method, path).status_code == 503 @pytest.mark.parametrize("init_state", ("dormant", "initializing")) def test_app_backend_http_requires_ready_state( @@ -542,7 +498,6 @@ async def accept_app_backend_http( "proxy_app_backend_http", accept_app_backend_http, ) - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", @@ -550,13 +505,10 @@ async def accept_app_backend_http( ) app = create_app(cfg) with TestClient(app) as client: - try: - init_service: InitService = app.state.init_service - init_service._state = init_state - assert client.get("/api/init").json()["state"] == init_state - assert client.get("/app-backends/test-extension").status_code == 503 - finally: - _reset_conversation_singleton() + init_service: InitService = app.state.init_service + init_service._state = init_state + assert client.get("/api/init").json()["state"] == init_state + assert client.get("/app-backends/test-extension").status_code == 503 @pytest.mark.parametrize( ( @@ -621,15 +573,6 @@ def test_request_crossing_init_uses_ready_credentials( expected_status: int, ready_headers: dict[str, str], ) -> None: - class EmptyProfileStore: - def list_summaries(self) -> list[dict[str, object]]: - return [] - - monkeypatch.setattr( - "openhands.agent_server.openai.service.get_llm_profile_store", - EmptyProfileStore, - ) - _reset_conversation_singleton() cfg = Config( deferred_init=True, session_api_keys=dormant_keys, @@ -647,35 +590,31 @@ def pause_at_readiness(request: Request) -> None: app.dependency_overrides[require_initialized] = pause_at_readiness with TestClient(app) as client: - try: - with ThreadPoolExecutor(max_workers=1) as executor: - pending = executor.submit( - client.get, - path, - headers=request_headers, + with ThreadPoolExecutor(max_workers=1) as executor: + pending = executor.submit( + client.get, + path, + headers=request_headers, + ) + try: + assert readiness_reached.wait(timeout=5) + initialized = client.post( + "/api/init", + json={ + "session_api_keys": ["ready-key"], + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, ) - try: - assert readiness_reached.wait(timeout=5) - initialized = client.post( - "/api/init", - json={ - "session_api_keys": ["ready-key"], - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert initialized.status_code == 200 - finally: - continue_request.set() - crossed = pending.result(timeout=5) - - assert crossed.status_code == expected_status - assert client.get(path, headers=ready_headers).status_code == 200 - finally: - _reset_conversation_singleton() + assert initialized.status_code == 200 + finally: + continue_request.set() + crossed = pending.result(timeout=5) + + assert crossed.status_code == expected_status + assert client.get(path, headers=ready_headers).status_code == 200 def test_dormant_503s_api_routes_until_init(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", @@ -683,43 +622,39 @@ def test_dormant_503s_api_routes_until_init(self, tmp_path): ) app = create_app(cfg) with TestClient(app) as client: - try: - # Health/ready/server_info are not gated. - assert client.get("/alive").status_code == 200 - assert client.get("/ready").status_code == 200 - - # Sample /api/* route — should be 503. The agent-server's - # 5xx exception handler replaces ``detail`` with a generic - # "Internal Server Error" message, so we only assert on the - # status code here — that's what the warm-pool orchestrator - # actually inspects. - resp = client.get("/api/conversations/count") - assert resp.status_code == 503 - - # Init status reports dormant. - resp = client.get("/api/init") - assert resp.status_code == 200 - assert resp.json()["state"] == "dormant" - - # Run /api/init. - resp = client.post( - "/api/init", - json={ - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 - assert resp.json()["state"] == "ready" + # Health/ready/server_info are not gated. + assert client.get("/alive").status_code == 200 + assert client.get("/ready").status_code == 200 + + # Sample /api/* route — should be 503. The agent-server's + # 5xx exception handler replaces ``detail`` with a generic + # "Internal Server Error" message, so we only assert on the + # status code here — that's what the warm-pool orchestrator + # actually inspects. + resp = client.get("/api/conversations/count") + assert resp.status_code == 503 + + # Init status reports dormant. + resp = client.get("/api/init") + assert resp.status_code == 200 + assert resp.json()["state"] == "dormant" + + # Run /api/init. + resp = client.post( + "/api/init", + json={ + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 + assert resp.json()["state"] == "ready" - # /api/* now works (200, not 503). - resp = client.get("/api/conversations/count") - assert resp.status_code == 200 - finally: - _reset_conversation_singleton() + # /api/* now works (200, not 503). + resp = client.get("/api/conversations/count") + assert resp.status_code == 200 def test_dormant_refuses_bash_socket_until_init(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", @@ -727,64 +662,48 @@ def test_dormant_refuses_bash_socket_until_init(self, tmp_path): ) marker = tmp_path / "marker" with TestClient(create_app(cfg)) as client: - try: - with pytest.raises(WebSocketDisconnect) as excinfo: - with client.websocket_connect("/sockets/bash-events") as ws: - ws.send_json({"command": f"touch {marker}"}) - assert excinfo.value.code == status.WS_1013_TRY_AGAIN_LATER - assert not marker.exists() - - resp = client.post( - "/api/init", - json={ - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 + with pytest.raises(WebSocketDisconnect) as excinfo: + with client.websocket_connect("/sockets/bash-events") as ws: + ws.send_json({"command": f"touch {marker}"}) + assert excinfo.value.code == status.WS_1013_TRY_AGAIN_LATER + assert not marker.exists() - with client.websocket_connect("/sockets/bash-events"): - pass - finally: - _reset_conversation_singleton() + resp = client.post( + "/api/init", + json={ + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 - def test_dormant_503s_openai_routes_until_init(self, tmp_path, monkeypatch): - class EmptyProfileStore: - def list_summaries(self) -> list[dict[str, object]]: - return [] + with client.websocket_connect("/sockets/bash-events"): + pass - monkeypatch.setattr( - "openhands.agent_server.openai.service.get_llm_profile_store", - EmptyProfileStore, - ) - _reset_conversation_singleton() + def test_dormant_503s_openai_routes_until_init(self, tmp_path): cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - assert client.get("/v1/models").status_code == 503 - - resp = client.post( - "/api/init", - json={ - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 + assert client.get("/v1/models").status_code == 503 + + resp = client.post( + "/api/init", + json={ + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 - assert client.get("/v1/models").status_code == 200 - finally: - _reset_conversation_singleton() + assert client.get("/v1/models").status_code == 200 def test_root_path_updates_from_init_web_url(self, tmp_path): """When /api/init delivers a ``web_url``, the FastAPI ``root_path`` must be re-derived from it so OpenAPI/Swagger/ReDoc URLs reflect the external mount path (e.g. behind a reverse proxy).""" - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", @@ -792,31 +711,27 @@ def test_root_path_updates_from_init_web_url(self, tmp_path): ) app = create_app(cfg) with TestClient(app) as client: - try: - # Dormant server has no web_url → empty root_path. - assert app.root_path == "" - - resp = client.post( - "/api/init", - json={ - "web_url": "https://example.com/agent-server-123/agent-server/", - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 + # Dormant server has no web_url → empty root_path. + assert app.root_path == "" - # The FastAPI app's root_path must now match the prefix from - # the per-user web_url so OpenAPI doc URLs and Swagger asset - # links are correct under the reverse-proxy mount path. - assert app.root_path == "/agent-server-123/agent-server", ( - f"root_path was not updated after /api/init: {app.root_path!r}" - ) - finally: - _reset_conversation_singleton() + resp = client.post( + "/api/init", + json={ + "web_url": "https://example.com/agent-server-123/agent-server/", + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 + + # The FastAPI app's root_path must now match the prefix from + # the per-user web_url so OpenAPI doc URLs and Swagger asset + # links are correct under the reverse-proxy mount path. + assert app.root_path == "/agent-server-123/agent-server", ( + f"root_path was not updated after /api/init: {app.root_path!r}" + ) def test_init_api_key_required_when_configured(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=True, secret_key=SecretStr("pool-key"), @@ -825,41 +740,37 @@ def test_init_api_key_required_when_configured(self, tmp_path): ) app = create_app(cfg) with TestClient(app) as client: - try: - # Wrong key → 401. - resp = client.post( - "/api/init", - headers={"X-Init-API-Key": "wrong"}, - json={ - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 401 - - # No key → 401. - resp = client.post("/api/init", json={}) - assert resp.status_code == 401 - - # Right key → 200. - resp = client.post( - "/api/init", - headers={"X-Init-API-Key": "pool-key"}, - json={ - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 + # Wrong key → 401. + resp = client.post( + "/api/init", + headers={"X-Init-API-Key": "wrong"}, + json={ + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 401 + + # No key → 401. + resp = client.post("/api/init", json={}) + assert resp.status_code == 401 - # GET /api/init does NOT require the key (status polling). - resp = client.get("/api/init") - assert resp.status_code == 200 - finally: - _reset_conversation_singleton() + # Right key → 200. + resp = client.post( + "/api/init", + headers={"X-Init-API-Key": "pool-key"}, + json={ + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 + + # GET /api/init does NOT require the key (status polling). + resp = client.get("/api/init") + assert resp.status_code == 200 def test_session_api_key_set_at_init_protects_api(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", @@ -867,45 +778,43 @@ def test_session_api_key_set_at_init_protects_api(self, tmp_path): ) app = create_app(cfg) with TestClient(app) as client: - try: - # Before /api/init, no session key required at startup config - # level — but the dormant gate 503s anyway. - assert client.get("/api/conversations/count").status_code == 503 - - # Init delivers the session key. - resp = client.post( - "/api/init", - json={ - "session_api_keys": ["user-session-key"], - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 - - assert app.state.config.session_api_keys == ["user-session-key"] - assert client.get("/api/conversations/count").status_code == 401 - assert ( - client.get( - "/api/conversations/count", - headers={"X-Session-API-Key": "wrong-key"}, - ).status_code - == 401 - ) - assert ( - client.get( - "/api/conversations/count", - headers={"X-Session-API-Key": "user-session-key"}, - ).status_code - == 200 - ) - finally: - _reset_conversation_singleton() + # Before /api/init, no session key required at startup config + # level — but the dormant gate 503s anyway. + assert client.get("/api/conversations/count").status_code == 503 + + # Init delivers the session key. + resp = client.post( + "/api/init", + json={ + "session_api_keys": ["user-session-key"], + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 + + assert server_context_of(app).config.session_api_keys == [ + "user-session-key" + ] + assert client.get("/api/conversations/count").status_code == 401 + assert ( + client.get( + "/api/conversations/count", + headers={"X-Session-API-Key": "wrong-key"}, + ).status_code + == 401 + ) + assert ( + client.get( + "/api/conversations/count", + headers={"X-Session-API-Key": "user-session-key"}, + ).status_code + == 200 + ) def test_download_trajectory_uses_init_conversations_path(self, tmp_path): """Both trajectory routes serve conversations stored under the ``conversations_path`` delivered by /api/init, not the launch path.""" - _reset_conversation_singleton() init_convs = tmp_path / "u" / "convs" workspace = tmp_path / "ws" workspace.mkdir() @@ -915,51 +824,48 @@ def test_download_trajectory_uses_init_conversations_path(self, tmp_path): bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - resp = client.post( - "/api/init", - json={ - "conversations_path": str(init_convs), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 - - resp = client.post( - "/api/conversations", - json={ - "agent": { - "kind": "Agent", - "llm": {"model": "test", "api_key": "sk-init-path"}, - "tools": [], - }, - "workspace": {"working_dir": str(workspace)}, + resp = client.post( + "/api/init", + json={ + "conversations_path": str(init_convs), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 + + resp = client.post( + "/api/conversations", + json={ + "agent": { + "kind": "Agent", + "llm": {"model": "test", "api_key": "sk-init-path"}, + "tools": [], }, - ) - assert resp.status_code in (200, 201), resp.text - conversation_id = UUID(resp.json()["id"]) - assert (init_convs / conversation_id.hex / "base_state.json").exists() - - for route in ( - f"/api/file/download-trajectory/{conversation_id}", - f"/api/conversations/{conversation_id}" - f"/file/download-trajectory/{conversation_id}", - ): - resp = client.get(route) - assert resp.status_code == 200, (route, resp.text) - with zipfile.ZipFile(io.BytesIO(resp.content)) as archive: - names = archive.namelist() - blob = b"".join(archive.read(n) for n in names) - assert f"{conversation_id.hex}/base_state.json" in names - assert b"sk-init-path" not in blob - assert not list(init_convs.glob("*.zip")) - - missing = uuid4() - resp = client.get(f"/api/file/download-trajectory/{missing}") - assert resp.status_code == 404 - assert resp.json()["detail"] == "Conversation not found" - finally: - _reset_conversation_singleton() + "workspace": {"working_dir": str(workspace)}, + }, + ) + assert resp.status_code in (200, 201), resp.text + conversation_id = UUID(resp.json()["id"]) + assert (init_convs / conversation_id.hex / "base_state.json").exists() + + for route in ( + f"/api/file/download-trajectory/{conversation_id}", + f"/api/conversations/{conversation_id}" + f"/file/download-trajectory/{conversation_id}", + ): + resp = client.get(route) + assert resp.status_code == 200, (route, resp.text) + with zipfile.ZipFile(io.BytesIO(resp.content)) as archive: + names = archive.namelist() + blob = b"".join(archive.read(n) for n in names) + assert f"{conversation_id.hex}/base_state.json" in names + assert b"sk-init-path" not in blob + assert not list(init_convs.glob("*.zip")) + + missing = uuid4() + resp = client.get(f"/api/file/download-trajectory/{missing}") + assert resp.status_code == 404 + assert resp.json()["detail"] == "Conversation not found" def test_vscode_url_uses_session_key_after_init(self, tmp_path, monkeypatch): """After /api/init the VSCode URL's token is the first session key, so @@ -994,7 +900,6 @@ class TestNonDeferredPathUnchanged: """Regression: deferred_init=False must behave exactly like before.""" def test_non_deferred_does_not_create_init_service(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=False, conversations_path=tmp_path / "convs", @@ -1002,15 +907,12 @@ def test_non_deferred_does_not_create_init_service(self, tmp_path): ) app = create_app(cfg) with TestClient(app) as client: - try: - # No init_service in non-deferred mode. - assert getattr(app.state, "init_service", None) is None - # /api/* should be live (200) — the dormant gate is a no-op. - assert client.get("/api/conversations/count").status_code == 200 - # /api/init returns 404 because no InitService is attached. - assert client.get("/api/init").status_code == 404 - finally: - _reset_conversation_singleton() + # No init_service in non-deferred mode. + assert getattr(app.state, "init_service", None) is None + # /api/* should be live (200) — the dormant gate is a no-op. + assert client.get("/api/conversations/count").status_code == 200 + # /api/init returns 404 because no InitService is attached. + assert client.get("/api/init").status_code == 404 @pytest.mark.asyncio @@ -1019,17 +921,15 @@ async def test_lifespan_teardown_releases_conversation_service_after_init( ): """If /api/init succeeds, the lifespan finally clause must release the conversation service. If /api/init never runs, teardown is a no-op.""" - _reset_conversation_singleton() - _reset_bash_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - # Build a fake FastAPI app — api_lifespan only touches `.state`. - fake_app = SimpleNamespace(state=SimpleNamespace(config=cfg)) - async with api_lifespan(fake_app): # type: ignore[arg-type] - init_svc = fake_app.state.init_service + # A bare app is enough: api_lifespan only reads its server context. + app = _dormant_app(cfg) + async with api_lifespan(app): + init_svc = app.state.init_service assert init_svc.state == "dormant" await init_svc.initialize( InitRequest( @@ -1038,13 +938,14 @@ async def test_lifespan_teardown_releases_conversation_service_after_init( ) ) assert init_svc.state == "ready" + context = server_context_of(app) + assert context.conversation_service is not None + assert context.bash_event_service is not None # After lifespan exit the conversation service should have been torn - # down — i.e. _entered_service is cleared. - assert init_svc._entered_service is None - # Same for the bash service: it must be torn down on lifespan exit. - assert init_svc._entered_bash_service is None - _reset_conversation_singleton() - _reset_bash_singleton() + # down, and the bash service with it. + assert init_svc._started is None + assert context.conversation_service._event_services is None + assert context.bash_event_service._closed class TestSessionKeyExport: @@ -1055,7 +956,6 @@ class TestSessionKeyExport: def test_init_exports_delivered_key_over_boot_key(self, tmp_path, monkeypatch): """A dormant pod boots holding a shared key; /api/init replaces it.""" monkeypatch.setenv("OH_SESSION_API_KEYS_0", "boot-key") - _reset_conversation_singleton() cfg = Config( deferred_init=True, session_api_keys=["boot-key"], @@ -1063,36 +963,31 @@ def test_init_exports_delivered_key_over_boot_key(self, tmp_path, monkeypatch): bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - resp = client.post( - "/api/init", - json={ - "session_api_keys": ["user-session-key"], - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - # The bootstrap credential is the dormant secret key, which - # defaults to the first session key. - headers={"X-Init-API-Key": "boot-key"}, - ) - assert resp.status_code == 200 - assert os.environ["OH_SESSION_API_KEYS_0"] == "user-session-key" - finally: - _reset_conversation_singleton() + resp = client.post( + "/api/init", + json={ + "session_api_keys": ["user-session-key"], + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + # The bootstrap credential is the dormant secret key, which + # defaults to the first session key. + headers={"X-Init-API-Key": "boot-key"}, + ) + assert resp.status_code == 200 + assert os.environ["OH_SESSION_API_KEYS_0"] == "user-session-key" @pytest.mark.asyncio async def test_teardown_restores_the_env_after_a_direct_init(self, tmp_path): """Driving InitService without the lifespan (as other tests do) must not leak the delivered key into the process environment.""" - _reset_conversation_singleton() - _reset_bash_singleton() base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = SimpleNamespace(state=SimpleNamespace(config=base)) - svc = InitService(app, base_config=base) # type: ignore[arg-type] + app = _dormant_app(base) + svc = InitService(app, base_config=base) try: await svc.initialize( InitRequest( @@ -1104,44 +999,34 @@ async def test_teardown_restores_the_env_after_a_direct_init(self, tmp_path): assert os.environ["OH_SESSION_API_KEYS_0"] == "user-key" finally: await svc.teardown() - _reset_conversation_singleton() - _reset_bash_singleton() assert "OH_SESSION_API_KEYS_0" not in os.environ def test_init_without_keys_leaves_env_untouched(self, tmp_path): - _reset_conversation_singleton() cfg = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) with TestClient(create_app(cfg)) as client: - try: - resp = client.post( - "/api/init", - json={ - "conversations_path": str(tmp_path / "u" / "convs"), - "bash_events_dir": str(tmp_path / "u" / "bash"), - }, - ) - assert resp.status_code == 200 - assert "OH_SESSION_API_KEYS_0" not in os.environ - finally: - _reset_conversation_singleton() + resp = client.post( + "/api/init", + json={ + "conversations_path": str(tmp_path / "u" / "convs"), + "bash_events_dir": str(tmp_path / "u" / "bash"), + }, + ) + assert resp.status_code == 200 + assert "OH_SESSION_API_KEYS_0" not in os.environ def test_startup_exports_config_key_and_restores_env_on_shutdown(self, tmp_path): """A key read from the config file is exported for the server's lifetime only.""" - _reset_conversation_singleton() cfg = Config( deferred_init=False, session_api_keys=["file-key"], conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - try: - with TestClient(create_app(cfg)): - assert os.environ["OH_SESSION_API_KEYS_0"] == "file-key" - assert "OH_SESSION_API_KEYS_0" not in os.environ - finally: - _reset_conversation_singleton() + with TestClient(create_app(cfg)): + assert os.environ["OH_SESSION_API_KEYS_0"] == "file-key" + assert "OH_SESSION_API_KEYS_0" not in os.environ diff --git a/tests/agent_server/test_launch_parity.py b/tests/agent_server/test_launch_parity.py index a97e817ee9d..8e68dd80b91 100644 --- a/tests/agent_server/test_launch_parity.py +++ b/tests/agent_server/test_launch_parity.py @@ -22,10 +22,7 @@ ) from openhands.agent_server.persistence import ( PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_settings_store, - reset_stores, + ServerStores, ) from openhands.sdk import LLM from openhands.sdk.conversation.request import StartConversationRequest @@ -41,6 +38,7 @@ from openhands.sdk.skills import Skill from openhands.sdk.utils.cipher import Cipher from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import client_context CATALOG = [ @@ -52,7 +50,6 @@ @pytest.fixture def server(tmp_path, monkeypatch) -> Iterator[TestClient]: monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "persistence")) - reset_stores() config = Config( static_files_path=None, session_api_keys=[], @@ -61,7 +58,7 @@ def server(tmp_path, monkeypatch) -> Iterator[TestClient]: workspace_path=tmp_path / "workspace", ) (tmp_path / "workspace").mkdir() - get_settings_store(config).save( + ServerStores.from_config(config).settings.save( PersistedSettings( agent_settings=OpenHandsAgentSettings( mcp_config={ @@ -71,7 +68,7 @@ def server(tmp_path, monkeypatch) -> Iterator[TestClient]: ) ) ) - get_llm_profile_store().save( + ServerStores.from_config(Config()).llm_profiles.save( "default", LLM(model="parity-model", api_key=SecretStr("sk-parity")), include_secrets=True, @@ -82,11 +79,10 @@ def server(tmp_path, monkeypatch) -> Iterator[TestClient]: TestClient(create_app(config)) as client, ): yield client - reset_stores() def _save(profile: OpenHandsAgentProfile) -> OpenHandsAgentProfile: - get_agent_profile_store().save(profile) + ServerStores.from_config(Config()).agent_profiles.save(profile) return profile @@ -270,8 +266,9 @@ def launched_skills(profile: OpenHandsAgentProfile) -> list[str]: def test_the_global_critic_key_reaches_profile_launches_but_no_response( server, tmp_path ): - config = server.app.state.config - store = get_settings_store(config) + context = client_context(server) + config = context.config + store = context.stores.settings stored = store.load() assert stored is not None store.save( @@ -296,7 +293,7 @@ def test_the_global_critic_key_reaches_profile_launches_but_no_response( ) settings = store.load() assert settings is not None - stores = server_launch_stores(settings, config.cipher) + stores = server_launch_stores(settings, context.stores, config.cipher) request = StartConversationRequest( agent_profile_id=profile.id, workspace=LocalWorkspace(working_dir="/workspace") ) @@ -328,10 +325,13 @@ def test_inline_stored_and_agent_settings_sources_launch_the_same_agent( ): stored = _save(_profile("named")) workspace = str(tmp_path / "workspace") - config = server.app.state.config - settings = get_settings_store(config).load() + context = client_context(server) + config = context.config + settings = context.stores.settings.load() assert settings is not None - resolved = resolve(stored, server_launch_stores(settings, config.cipher)) + resolved = resolve( + stored, server_launch_stores(settings, context.stores, config.cipher) + ) agent_settings = resolved.settings.model_dump( mode="json", context={"expose_secrets": True} ) @@ -349,15 +349,16 @@ def test_a_docker_host_preview_matches_what_the_container_launches( server, tmp_path, image_has_browser ): profile = _save(_profile("named")) - host = server.app.state.config.model_copy( + context = client_context(server) + host = context.config.model_copy( update={ "conversation_runtime": "docker", "conversation_image_has_browser": image_has_browser, } ) - settings = get_settings_store(host).load() + settings = context.stores.settings.load() assert settings is not None - stores = server_launch_stores(settings, host.cipher) + stores = server_launch_stores(settings, context.stores, host.cipher) preview = preview_launch(profile, stores, target_launch_runtime(host)) diff --git a/tests/agent_server/test_local_secret_resolver.py b/tests/agent_server/test_local_secret_resolver.py index ddd7f657728..74fb79db1e5 100644 --- a/tests/agent_server/test_local_secret_resolver.py +++ b/tests/agent_server/test_local_secret_resolver.py @@ -10,7 +10,7 @@ _secret_name_if_local, build_local_secret_resolver, ) -from openhands.agent_server.persistence import get_secrets_store +from openhands.agent_server.persistence import ServerStores from openhands.sdk.secret import ( LookupSecret, register_local_secret_resolver, @@ -73,9 +73,9 @@ def test_ignores_other_routes_and_malformed_names(monkeypatch): def test_foreign_loopback_port_uses_its_own_server(monkeypatch): monkeypatch.setenv("OH_INTERNAL_SERVER_URL", "http://127.0.0.1:18000") - config = Config() - get_secrets_store(config).set_secret("TOKEN", "local-value") - resolver = build_local_secret_resolver(config) + stores = ServerStores.from_config(Config()) + stores.secrets.set_secret("TOKEN", "local-value") + resolver = build_local_secret_resolver(lambda: stores.secrets) register_local_secret_resolver(resolver) try: with _foreign_secret_server() as base_url: diff --git a/tests/agent_server/test_local_storage.py b/tests/agent_server/test_local_storage.py index 3c931287558..7a0978b417c 100644 --- a/tests/agent_server/test_local_storage.py +++ b/tests/agent_server/test_local_storage.py @@ -8,10 +8,6 @@ import pytest from fastapi.testclient import TestClient -from openhands.agent_server import ( - config as config_module, - conversation_service as service_module, -) from openhands.agent_server.api import create_app @@ -53,9 +49,6 @@ def client(tmp_path, monkeypatch) -> Iterator[TestClient]: "OH_SECRET_KEY": "key", }.items(): monkeypatch.setenv(name, value) - # Both are process-wide and would keep another test's paths. - monkeypatch.setattr(config_module, "_default_config", None) - monkeypatch.setattr(service_module, "_conversation_service", None) with TestClient(create_app()) as client: yield client diff --git a/tests/agent_server/test_mcp_oauth_store.py b/tests/agent_server/test_mcp_oauth_store.py index c09e1872c8a..7db4e2d365c 100644 --- a/tests/agent_server/test_mcp_oauth_store.py +++ b/tests/agent_server/test_mcp_oauth_store.py @@ -27,9 +27,9 @@ create_settings_backed_mcp_tool_provider, ) from openhands.agent_server.persistence import ( + FileSettingsStore, PersistedSettings, - get_settings_store, - reset_stores, + ServerStores, ) from openhands.sdk.mcp.config import MCPServer, coerce_mcp_config, dump_mcp_config from openhands.sdk.mcp.exceptions import MCPError, MCPTimeoutError @@ -37,6 +37,15 @@ from openhands.sdk.mcp.utils import create_mcp_tools +def _settings_store(config: Config) -> FileSettingsStore: + """The settings store of a server running with ``config``. + + Each call builds one over the same file with the same cipher, as a server + (re)initialisation does. + """ + return ServerStores.from_config(config).settings + + def _find_free_port() -> int: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.bind(("", 0)) @@ -131,107 +140,101 @@ def read_subject(subject: str) -> str: async def test_mcp_oauth_token_store_persists_values_in_settings( tmp_path: Path, ): - reset_stores() - try: - config = Config( - session_api_keys=[], - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("mcp-oauth-test-key"), - ) - settings = PersistedSettings() - settings.agent_settings = settings.agent_settings.model_copy( - update={ - "mcp_config": coerce_mcp_config( - { - "superhuman": { - "url": "https://mcp.example.com/mcp", - "auth": { - "strategy": "oauth2", - "authentication": { - "type": "oauth", - "client_auth_method": "none", - }, + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("mcp-oauth-test-key"), + ) + settings = PersistedSettings() + settings.agent_settings = settings.agent_settings.model_copy( + update={ + "mcp_config": coerce_mcp_config( + { + "superhuman": { + "url": "https://mcp.example.com/mcp", + "auth": { + "strategy": "oauth2", + "authentication": { + "type": "oauth", + "client_auth_method": "none", }, - } + }, } - ) - } - ) - settings_store = get_settings_store(config) - settings_store.save(settings) - create_settings_backed_mcp_tool_provider(config) - - key = "https://mcp.example.com/mcp/tokens" - client_info_key = "https://mcp.example.com/mcp/client_info" - token_expiry_key = "https://mcp.example.com/mcp/token_expiry" - value = { - "access_token": "super-secret-token", - "refresh_token": "refresh-token", - } - client_info = { - "redirect_uris": ["http://127.0.0.1:64801/callback"], - "client_id": "superhuman-client", - "client_secret": "superhuman-client-secret", + } + ) } - token_expiry = {"expires_at": 12345.0} + ) + settings_store = _settings_store(config) + settings_store.save(settings) + create_settings_backed_mcp_tool_provider(config, _settings_store(config)) + + key = "https://mcp.example.com/mcp/tokens" + client_info_key = "https://mcp.example.com/mcp/client_info" + token_expiry_key = "https://mcp.example.com/mcp/token_expiry" + value = { + "access_token": "super-secret-token", + "refresh_token": "refresh-token", + } + client_info = { + "redirect_uris": ["http://127.0.0.1:64801/callback"], + "client_id": "superhuman-client", + "client_secret": "superhuman-client-secret", + } + token_expiry = {"expires_at": 12345.0} + + store = MCPSettingsOAuthTokenStore(_settings_store(config)) + await store.put(key=key, value=value, collection="mcp-oauth-token") + await store.put( + key=client_info_key, + value=client_info, + collection="mcp-oauth-client-info", + ) + await store.put( + key=token_expiry_key, + value=token_expiry, + collection="mcp-oauth-token-expiry", + ) - store = MCPSettingsOAuthTokenStore() - await store.put(key=key, value=value, collection="mcp-oauth-token") - await store.put( + reloaded_store = MCPSettingsOAuthTokenStore(_settings_store(config)) + assert (await reloaded_store.get(key=key, collection="mcp-oauth-token")) == value + assert ( + await reloaded_store.get( key=client_info_key, - value=client_info, collection="mcp-oauth-client-info", ) - await store.put( + ) == client_info + assert ( + await reloaded_store.get( key=token_expiry_key, - value=token_expiry, collection="mcp-oauth-token-expiry", ) - - reloaded_store = MCPSettingsOAuthTokenStore() - assert ( - await reloaded_store.get(key=key, collection="mcp-oauth-token") - ) == value - assert ( - await reloaded_store.get( - key=client_info_key, - collection="mcp-oauth-client-info", - ) - ) == client_info - assert ( - await reloaded_store.get( - key=token_expiry_key, - collection="mcp-oauth-token-expiry", - ) - ) == token_expiry - - on_disk_text = (settings_store.persistence_dir / "settings.json").read_text() - assert "super-secret-token" not in on_disk_text - assert "refresh-token" not in on_disk_text - assert "superhuman-client-secret" not in on_disk_text - - on_disk = json.loads(on_disk_text) - stored_state = on_disk["agent_settings"]["mcp_config"]["superhuman"]["auth"][ - "state" - ] - stored_value = stored_state["tokens"] - assert stored_value["access_token"].startswith("gAAAA") - assert stored_value["refresh_token"].startswith("gAAAA") - assert stored_state["client_info"]["client_secret"].startswith("gAAAA") - assert stored_state["token_expires_at"] == 12345.0 - - loaded = settings_store.load() - assert loaded is not None - server = dump_mcp_config(loaded.agent_settings.mcp_config)["superhuman"] - auth = server["auth"] - assert isinstance(auth, dict) - assert auth["state"] == { - "tokens": value, - "client_info": client_info, - "token_expires_at": 12345.0, - } - finally: - reset_stores() + ) == token_expiry + + on_disk_text = (settings_store.persistence_dir / "settings.json").read_text() + assert "super-secret-token" not in on_disk_text + assert "refresh-token" not in on_disk_text + assert "superhuman-client-secret" not in on_disk_text + + on_disk = json.loads(on_disk_text) + stored_state = on_disk["agent_settings"]["mcp_config"]["superhuman"]["auth"][ + "state" + ] + stored_value = stored_state["tokens"] + assert stored_value["access_token"].startswith("gAAAA") + assert stored_value["refresh_token"].startswith("gAAAA") + assert stored_state["client_info"]["client_secret"].startswith("gAAAA") + assert stored_state["token_expires_at"] == 12345.0 + + loaded = settings_store.load() + assert loaded is not None + server = dump_mcp_config(loaded.agent_settings.mcp_config)["superhuman"] + auth = server["auth"] + assert isinstance(auth, dict) + assert auth["state"] == { + "tokens": value, + "client_info": client_info, + "token_expires_at": 12345.0, + } def test_oauth_mcp_connection_persists_and_reuses_settings_state( @@ -247,127 +250,121 @@ def test_oauth_mcp_connection_persists_and_reuses_settings_state( reuses the persisted state. """ - reset_stores() _HeadlessOAuth.redirect_count = 0 - try: - config = Config( - session_api_keys=[], - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("mcp-oauth-e2e-test-key"), - ) - mcp_config = coerce_mcp_config( - { - "mail": { - "url": protected_oauth_mcp_server, - "transport": "http", - "auth": { - "strategy": "oauth2", - "authentication": { - "type": "oauth", - "client_auth_method": "none", - "scopes": ["mail.read"], - }, + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("mcp-oauth-e2e-test-key"), + ) + mcp_config = coerce_mcp_config( + { + "mail": { + "url": protected_oauth_mcp_server, + "transport": "http", + "auth": { + "strategy": "oauth2", + "authentication": { + "type": "oauth", + "client_auth_method": "none", + "scopes": ["mail.read"], }, - } + }, } - ) - settings = PersistedSettings() - settings.agent_settings = settings.agent_settings.model_copy( - update={"mcp_config": mcp_config} - ) - settings_store = get_settings_store(config) - settings_store.save(settings) - tool_provider = create_settings_backed_mcp_tool_provider(config) - - with create_mcp_tools( - mcp_config, - timeout=10.0, - mcp_oauth_token_storage=MCPSettingsOAuthTokenStore(), - mcp_oauth_class=_HeadlessOAuth, - ) as client: - tool = next(tool for tool in client.tools if tool.name == "read_subject") - assert tool.executor is not None - observation = tool.executor( - tool.action_from_arguments({"subject": "Quarterly Plan"}) - ) - assert "OAuth mail subject: Quarterly Plan" in observation.text - - assert _HeadlessOAuth.redirect_count == 1 - on_disk_text = (settings_store.persistence_dir / "settings.json").read_text() - assert "test_access_token_" not in on_disk_text - assert "test_refresh_token_" not in on_disk_text - - reloaded = settings_store.load() - assert reloaded is not None - persisted_mcp_config = reloaded.agent_settings.mcp_config - server = dump_mcp_config(persisted_mcp_config)["mail"] - auth = server["auth"] - assert isinstance(auth, dict) - state = auth["state"] - assert isinstance(state, dict) - tokens = state["tokens"] - assert isinstance(tokens, dict) - access_token = tokens["access_token"] - refresh_token = tokens["refresh_token"] - assert isinstance(access_token, str) - assert isinstance(refresh_token, str) - assert access_token.startswith("test_access_token_") - assert refresh_token.startswith("test_refresh_token_") - client_info = state["client_info"] - assert isinstance(client_info, dict) - assert client_info["client_id"] - - with tool_provider.create_tools( - persisted_mcp_config, - timeout=10.0, - ) as client: - tool = next(tool for tool in client.tools if tool.name == "read_subject") - assert tool.executor is not None - observation = tool.executor( - tool.action_from_arguments({"subject": "Follow-up"}) - ) - assert "OAuth mail subject: Follow-up" in observation.text - finally: - reset_stores() + } + ) + settings = PersistedSettings() + settings.agent_settings = settings.agent_settings.model_copy( + update={"mcp_config": mcp_config} + ) + settings_store = _settings_store(config) + settings_store.save(settings) + tool_provider = create_settings_backed_mcp_tool_provider( + config, _settings_store(config) + ) + + with create_mcp_tools( + mcp_config, + timeout=10.0, + mcp_oauth_token_storage=MCPSettingsOAuthTokenStore(_settings_store(config)), + mcp_oauth_class=_HeadlessOAuth, + ) as client: + tool = next(tool for tool in client.tools if tool.name == "read_subject") + assert tool.executor is not None + observation = tool.executor( + tool.action_from_arguments({"subject": "Quarterly Plan"}) + ) + assert "OAuth mail subject: Quarterly Plan" in observation.text + + assert _HeadlessOAuth.redirect_count == 1 + on_disk_text = (settings_store.persistence_dir / "settings.json").read_text() + assert "test_access_token_" not in on_disk_text + assert "test_refresh_token_" not in on_disk_text + + reloaded = settings_store.load() + assert reloaded is not None + persisted_mcp_config = reloaded.agent_settings.mcp_config + server = dump_mcp_config(persisted_mcp_config)["mail"] + auth = server["auth"] + assert isinstance(auth, dict) + state = auth["state"] + assert isinstance(state, dict) + tokens = state["tokens"] + assert isinstance(tokens, dict) + access_token = tokens["access_token"] + refresh_token = tokens["refresh_token"] + assert isinstance(access_token, str) + assert isinstance(refresh_token, str) + assert access_token.startswith("test_access_token_") + assert refresh_token.startswith("test_refresh_token_") + client_info = state["client_info"] + assert isinstance(client_info, dict) + assert client_info["client_id"] + + with tool_provider.create_tools( + persisted_mcp_config, + timeout=10.0, + ) as client: + tool = next(tool for tool in client.tools if tool.name == "read_subject") + assert tool.executor is not None + observation = tool.executor( + tool.action_from_arguments({"subject": "Follow-up"}) + ) + assert "OAuth mail subject: Follow-up" in observation.text @pytest.mark.asyncio async def test_mcp_oauth_token_storage_does_not_attach_to_non_oauth_server( tmp_path: Path, ): - reset_stores() - try: - config = Config( - session_api_keys=[], - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("mcp-oauth-test-key"), - ) - settings = PersistedSettings() - settings.agent_settings = settings.agent_settings.model_copy( - update={ - "mcp_config": coerce_mcp_config( - {"plain": {"url": "https://mcp.example.com/mcp"}} - ) - } - ) - settings_store = get_settings_store(config) - settings_store.save(settings) + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("mcp-oauth-test-key"), + ) + settings = PersistedSettings() + settings.agent_settings = settings.agent_settings.model_copy( + update={ + "mcp_config": coerce_mcp_config( + {"plain": {"url": "https://mcp.example.com/mcp"}} + ) + } + ) + settings_store = _settings_store(config) + settings_store.save(settings) - create_settings_backed_mcp_tool_provider(config) - store = MCPSettingsOAuthTokenStore() + create_settings_backed_mcp_tool_provider(config, _settings_store(config)) + store = MCPSettingsOAuthTokenStore(_settings_store(config)) - await store.put( - key="https://mcp.example.com/mcp/tokens", - value={"access_token": "super-secret-token"}, - collection="mcp-oauth-token", - ) + await store.put( + key="https://mcp.example.com/mcp/tokens", + value={"access_token": "super-secret-token"}, + collection="mcp-oauth-token", + ) - loaded = settings_store.load() - assert loaded is not None - server = dump_mcp_config(loaded.agent_settings.mcp_config)["plain"] - assert "auth" not in server - finally: - reset_stores() + loaded = settings_store.load() + assert loaded is not None + server = dump_mcp_config(loaded.agent_settings.mcp_config)["plain"] + assert "auth" not in server def test_settings_backed_provider_forwards_on_tools_reconciled(): @@ -377,7 +374,7 @@ def test_settings_backed_provider_forwards_on_tools_reconciled(): to attach on_tools_reconciled to the returned client after the fact -- missing any notification that arrived during the initial connect. """ - provider = SettingsBackedMCPToolProvider() + provider = SettingsBackedMCPToolProvider(settings_store=_settings_store(Config())) config = coerce_mcp_config({"fake": {"command": "true"}}) def callback(client, tools): @@ -411,107 +408,97 @@ def _inline_oauth_server(access_token: str): async def test_mcp_oauth_token_store_serves_inline_state_for_servers_absent_from_settings( # noqa: E501 tmp_path: Path, ): - reset_stores() - try: - # Arrange: the sandbox settings store knows no MCP servers at all. - config = Config( - session_api_keys=[], - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("mcp-oauth-test-key"), - ) - settings_store = get_settings_store(config) - settings_store.save(PersistedSettings()) - store = MCPSettingsOAuthTokenStore( - seed_mcp_config=_inline_oauth_server("agent-access-token"), - cipher=config.cipher, - ) - key = "https://mcp.example.com/mcp/tokens" - - # Act - initial = await store.get(key=key, collection="mcp-oauth-token") - await store.put( - key=key, - value={"access_token": "refreshed-access-token"}, - collection="mcp-oauth-token", - ) - refreshed = await store.get(key=key, collection="mcp-oauth-token") - deleted = await store.delete(key=key, collection="mcp-oauth-token") - - # Assert: served from the inline state, refreshed in memory, never - # written into settings. - assert initial == {"access_token": "agent-access-token"} - assert refreshed == {"access_token": "refreshed-access-token"} - assert deleted is True - assert await store.get(key=key, collection="mcp-oauth-token") is None - loaded = settings_store.load() - assert loaded is not None - assert not loaded.agent_settings.mcp_config - finally: - reset_stores() + # Arrange: the sandbox settings store knows no MCP servers at all. + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("mcp-oauth-test-key"), + ) + settings_store = _settings_store(config) + settings_store.save(PersistedSettings()) + store = MCPSettingsOAuthTokenStore( + _settings_store(config), + seed_mcp_config=_inline_oauth_server("agent-access-token"), + cipher=config.cipher, + ) + key = "https://mcp.example.com/mcp/tokens" + + # Act + initial = await store.get(key=key, collection="mcp-oauth-token") + await store.put( + key=key, + value={"access_token": "refreshed-access-token"}, + collection="mcp-oauth-token", + ) + refreshed = await store.get(key=key, collection="mcp-oauth-token") + deleted = await store.delete(key=key, collection="mcp-oauth-token") + + # Assert: served from the inline state, refreshed in memory, never + # written into settings. + assert initial == {"access_token": "agent-access-token"} + assert refreshed == {"access_token": "refreshed-access-token"} + assert deleted is True + assert await store.get(key=key, collection="mcp-oauth-token") is None + loaded = settings_store.load() + assert loaded is not None + assert not loaded.agent_settings.mcp_config @pytest.mark.asyncio async def test_mcp_oauth_token_store_prefers_settings_over_inline_state( tmp_path: Path, ): - reset_stores() - try: - # Arrange: the same server exists in settings with its own tokens. - config = Config( - session_api_keys=[], - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("mcp-oauth-test-key"), - ) - settings = PersistedSettings() - settings.agent_settings = settings.agent_settings.model_copy( - update={"mcp_config": _inline_oauth_server("settings-access-token")} - ) - get_settings_store(config).save(settings) - store = MCPSettingsOAuthTokenStore( - seed_mcp_config=_inline_oauth_server("agent-access-token"), - cipher=config.cipher, - ) + # Arrange: the same server exists in settings with its own tokens. + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("mcp-oauth-test-key"), + ) + settings = PersistedSettings() + settings.agent_settings = settings.agent_settings.model_copy( + update={"mcp_config": _inline_oauth_server("settings-access-token")} + ) + _settings_store(config).save(settings) + store = MCPSettingsOAuthTokenStore( + _settings_store(config), + seed_mcp_config=_inline_oauth_server("agent-access-token"), + cipher=config.cipher, + ) - # Act - value = await store.get( - key="https://mcp.example.com/mcp/tokens", collection="mcp-oauth-token" - ) + # Act + value = await store.get( + key="https://mcp.example.com/mcp/tokens", collection="mcp-oauth-token" + ) - # Assert - assert value == {"access_token": "settings-access-token"} - finally: - reset_stores() + # Assert + assert value == {"access_token": "settings-access-token"} @pytest.mark.asyncio async def test_settings_backed_provider_seeds_token_store_from_agent_mcp_config( tmp_path: Path, ): - reset_stores() - try: - # Arrange - config = Config( - session_api_keys=[], - conversations_path=tmp_path / "conversations", - secret_key=SecretStr("mcp-oauth-test-key"), - ) - get_settings_store(config).save(PersistedSettings()) - provider = create_settings_backed_mcp_tool_provider(config) - mcp_config = _inline_oauth_server("agent-access-token") + # Arrange + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + secret_key=SecretStr("mcp-oauth-test-key"), + ) + _settings_store(config).save(PersistedSettings()) + provider = create_settings_backed_mcp_tool_provider(config, _settings_store(config)) + mcp_config = _inline_oauth_server("agent-access-token") - # Act - with patch( - "openhands.agent_server.mcp_oauth_store.create_mcp_tools" - ) as mock_create: - provider.create_tools(mcp_config) + # Act + with patch( + "openhands.agent_server.mcp_oauth_store.create_mcp_tools" + ) as mock_create: + provider.create_tools(mcp_config) - # Assert: the store handed to FastMCP already knows the agent's tokens. - storage = mock_create.call_args.kwargs["mcp_oauth_token_storage"] - assert await storage.get( - key="https://mcp.example.com/mcp/tokens", collection="mcp-oauth-token" - ) == {"access_token": "agent-access-token"} - finally: - reset_stores() + # Assert: the store handed to FastMCP already knows the agent's tokens. + storage = mock_create.call_args.kwargs["mcp_oauth_token_storage"] + assert await storage.get( + key="https://mcp.example.com/mcp/tokens", collection="mcp-oauth-token" + ) == {"access_token": "agent-access-token"} def _hosted_config(tmp_path: Path) -> Config: @@ -534,140 +521,128 @@ def _hosted_config(tmp_path: Path) -> Config: async def test_settings_backed_provider_writes_refreshed_inline_state_back_to_the_app_server( # noqa: E501 tmp_path: Path, monkeypatch: pytest.MonkeyPatch ): - reset_stores() - try: - # Arrange: the server is inline only, as a hosted conversation passes it. - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - posted: list[dict] = [] - - def fake_post(url, *, json, headers, timeout): - posted.append({"url": url, "json": json, "headers": headers}) - return httpx.Response(200, request=httpx.Request("POST", url)) - - monkeypatch.setattr(httpx, "post", fake_post) - provider = create_settings_backed_mcp_tool_provider(config) - with patch( - "openhands.agent_server.mcp_oauth_store.create_mcp_tools" - ) as mock_create: - provider.create_tools(_inline_oauth_server("agent-access-token")) - storage = mock_create.call_args.kwargs["mcp_oauth_token_storage"] - - # Act: FastMCP stores a refreshed token and its expiry. - await storage.put( - key="https://mcp.example.com/mcp/tokens", - value={ + # Arrange: the server is inline only, as a hosted conversation passes it. + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + posted: list[dict] = [] + + def fake_post(url, *, json, headers, timeout): + posted.append({"url": url, "json": json, "headers": headers}) + return httpx.Response(200, request=httpx.Request("POST", url)) + + monkeypatch.setattr(httpx, "post", fake_post) + provider = create_settings_backed_mcp_tool_provider(config, _settings_store(config)) + with patch( + "openhands.agent_server.mcp_oauth_store.create_mcp_tools" + ) as mock_create: + provider.create_tools(_inline_oauth_server("agent-access-token")) + storage = mock_create.call_args.kwargs["mcp_oauth_token_storage"] + + # Act: FastMCP stores a refreshed token and its expiry. + await storage.put( + key="https://mcp.example.com/mcp/tokens", + value={ + "access_token": "refreshed-access-token", + "refresh_token": "rotated-refresh-token", + }, + collection="mcp-oauth-token", + ) + await storage.put( + key="https://mcp.example.com/mcp/token_expiry", + value={"expires_at": 1234.0}, + collection="mcp-oauth-token-expiry", + ) + + # Assert: each write posts the whole state to the app server. + assert [entry["url"] for entry in posted] == [ + "https://app.example/api/v1/webhooks/mcp-oauth-state" + ] * 2 + assert posted[-1]["headers"] == { + "X-Trace": "on", + "X-Session-API-Key": "sandbox-session-key", + } + assert posted[-1]["json"] == { + "server_url": "https://mcp.example.com/mcp", + "oauth_state": { + "tokens": { "access_token": "refreshed-access-token", "refresh_token": "rotated-refresh-token", }, - collection="mcp-oauth-token", - ) - await storage.put( - key="https://mcp.example.com/mcp/token_expiry", - value={"expires_at": 1234.0}, - collection="mcp-oauth-token-expiry", - ) - - # Assert: each write posts the whole state to the app server. - assert [entry["url"] for entry in posted] == [ - "https://app.example/api/v1/webhooks/mcp-oauth-state" - ] * 2 - assert posted[-1]["headers"] == { - "X-Trace": "on", - "X-Session-API-Key": "sandbox-session-key", - } - assert posted[-1]["json"] == { - "server_url": "https://mcp.example.com/mcp", - "oauth_state": { - "tokens": { - "access_token": "refreshed-access-token", - "refresh_token": "rotated-refresh-token", - }, - "token_expires_at": 1234.0, - }, - } - finally: - reset_stores() + "token_expires_at": 1234.0, + }, + } @pytest.mark.asyncio async def test_mcp_oauth_token_store_does_not_write_back_settings_backed_state( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ): - reset_stores() - try: - # Arrange: the same server lives in this agent server's own settings. - config = _hosted_config(tmp_path) - settings = PersistedSettings() - settings.agent_settings = settings.agent_settings.model_copy( - update={"mcp_config": _inline_oauth_server("settings-access-token")} - ) - get_settings_store(config).save(settings) - posted: list[str] = [] - monkeypatch.setattr( - httpx, "post", lambda url, **kwargs: posted.append(url) or None - ) - store = MCPSettingsOAuthTokenStore( - seed_mcp_config=_inline_oauth_server("agent-access-token"), - cipher=config.cipher, - write_back_webhooks=config.webhooks, - session_api_key="sandbox-session-key", - ) - key = "https://mcp.example.com/mcp/tokens" + # Arrange: the same server lives in this agent server's own settings. + config = _hosted_config(tmp_path) + settings = PersistedSettings() + settings.agent_settings = settings.agent_settings.model_copy( + update={"mcp_config": _inline_oauth_server("settings-access-token")} + ) + _settings_store(config).save(settings) + posted: list[str] = [] + monkeypatch.setattr(httpx, "post", lambda url, **kwargs: posted.append(url) or None) + store = MCPSettingsOAuthTokenStore( + _settings_store(config), + seed_mcp_config=_inline_oauth_server("agent-access-token"), + cipher=config.cipher, + write_back_webhooks=config.webhooks, + session_api_key="sandbox-session-key", + ) + key = "https://mcp.example.com/mcp/tokens" - # Act - await store.put( - key=key, - value={"access_token": "refreshed-access-token"}, - collection="mcp-oauth-token", - ) + # Act + await store.put( + key=key, + value={"access_token": "refreshed-access-token"}, + collection="mcp-oauth-token", + ) - # Assert: persisted locally, nothing sent to the app server. - assert posted == [] - assert await store.get(key=key, collection="mcp-oauth-token") == { - "access_token": "refreshed-access-token" - } - finally: - reset_stores() + # Assert: persisted locally, nothing sent to the app server. + assert posted == [] + assert await store.get(key=key, collection="mcp-oauth-token") == { + "access_token": "refreshed-access-token" + } @pytest.mark.asyncio async def test_mcp_oauth_token_store_keeps_refreshed_inline_state_when_write_back_fails( # noqa: E501 tmp_path: Path, monkeypatch: pytest.MonkeyPatch ): - reset_stores() - try: - # Arrange: the app server cannot be reached. - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - - def failing_post(url, **kwargs): - raise httpx.ConnectError( - "app server unreachable", request=httpx.Request("POST", url) - ) - - monkeypatch.setattr(httpx, "post", failing_post) - store = MCPSettingsOAuthTokenStore( - seed_mcp_config=_inline_oauth_server("agent-access-token"), - cipher=config.cipher, - write_back_webhooks=config.webhooks, - session_api_key="sandbox-session-key", - ) - key = "https://mcp.example.com/mcp/tokens" + # Arrange: the app server cannot be reached. + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + + def failing_post(url, **kwargs): + raise httpx.ConnectError( + "app server unreachable", request=httpx.Request("POST", url) + ) + + monkeypatch.setattr(httpx, "post", failing_post) + store = MCPSettingsOAuthTokenStore( + _settings_store(config), + seed_mcp_config=_inline_oauth_server("agent-access-token"), + cipher=config.cipher, + write_back_webhooks=config.webhooks, + session_api_key="sandbox-session-key", + ) + key = "https://mcp.example.com/mcp/tokens" - # Act - await store.put( - key=key, - value={"access_token": "refreshed-access-token"}, - collection="mcp-oauth-token", - ) + # Act + await store.put( + key=key, + value={"access_token": "refreshed-access-token"}, + collection="mcp-oauth-token", + ) - # Assert: the conversation keeps using the refreshed token. - assert await store.get(key=key, collection="mcp-oauth-token") == { - "access_token": "refreshed-access-token" - } - finally: - reset_stores() + # Assert: the conversation keeps using the refreshed token. + assert await store.get(key=key, collection="mcp-oauth-token") == { + "access_token": "refreshed-access-token" + } @pytest.fixture @@ -687,67 +662,61 @@ def fake_post(url, *, json, headers, timeout): async def test_mcp_oauth_token_store_does_not_write_back_state_without_tokens( tmp_path: Path, write_backs: list[dict] ): - reset_stores() - try: - # Arrange - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - store = MCPSettingsOAuthTokenStore( - seed_mcp_config=_inline_oauth_server("agent-access-token"), - cipher=config.cipher, - write_back_webhooks=config.webhooks, - session_api_key="sandbox-session-key", - ) + # Arrange + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + store = MCPSettingsOAuthTokenStore( + _settings_store(config), + seed_mcp_config=_inline_oauth_server("agent-access-token"), + cipher=config.cipher, + write_back_webhooks=config.webhooks, + session_api_key="sandbox-session-key", + ) - # Act: FastMCP drops the stored tokens and registers a new client. - await store.delete( - key="https://mcp.example.com/mcp/tokens", collection="mcp-oauth-token" - ) - await store.put( - key="https://mcp.example.com/mcp/client_info", - value={ - "client_id": "loopback-client", - "redirect_uris": ["http://localhost:4321/callback"], - }, - collection="mcp-oauth-client-info", - ) + # Act: FastMCP drops the stored tokens and registers a new client. + await store.delete( + key="https://mcp.example.com/mcp/tokens", collection="mcp-oauth-token" + ) + await store.put( + key="https://mcp.example.com/mcp/client_info", + value={ + "client_id": "loopback-client", + "redirect_uris": ["http://localhost:4321/callback"], + }, + collection="mcp-oauth-client-info", + ) - # Assert: nothing replaces the credentials the app server holds. - assert write_backs == [] - finally: - reset_stores() + # Assert: nothing replaces the credentials the app server holds. + assert write_backs == [] @pytest.mark.asyncio async def test_mcp_oauth_token_store_does_not_write_back_a_client_it_registered( tmp_path: Path, write_backs: list[dict] ): - reset_stores() - try: - # Arrange: the inline state has tokens but no client registration. - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - store = MCPSettingsOAuthTokenStore( - seed_mcp_config=_inline_oauth_server("agent-access-token"), - cipher=config.cipher, - write_back_webhooks=config.webhooks, - session_api_key="sandbox-session-key", - ) + # Arrange: the inline state has tokens but no client registration. + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + store = MCPSettingsOAuthTokenStore( + _settings_store(config), + seed_mcp_config=_inline_oauth_server("agent-access-token"), + cipher=config.cipher, + write_back_webhooks=config.webhooks, + session_api_key="sandbox-session-key", + ) - # Act: FastMCP registers a loopback client while keeping the tokens. - await store.put( - key="https://mcp.example.com/mcp/client_info", - value={ - "client_id": "loopback-client", - "redirect_uris": ["http://localhost:4321/callback"], - }, - collection="mcp-oauth-client-info", - ) + # Act: FastMCP registers a loopback client while keeping the tokens. + await store.put( + key="https://mcp.example.com/mcp/client_info", + value={ + "client_id": "loopback-client", + "redirect_uris": ["http://localhost:4321/callback"], + }, + collection="mcp-oauth-client-info", + ) - # Assert - assert write_backs == [] - finally: - reset_stores() + # Assert + assert write_backs == [] def _installed_client(oauth_provider: InMemoryOAuthProvider) -> dict[str, Any]: @@ -815,31 +784,27 @@ def test_settings_backed_provider_keeps_stored_state_when_refresh_fails( installed client never registered, and on the 400 drops the stored state, registers a new client and opens a browser. """ - reset_stores() opened: list[str] = [] monkeypatch.setattr(webbrowser, "open", lambda url, *_, **__: opened.append(url)) - try: - # Arrange: the server no longer accepts the stored refresh token. - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - client_info = _installed_client(oauth_provider) - mcp_config = _expired_inline_oauth_server( - protected_oauth_mcp_server, client_info, "revoked-refresh-token" - ) - provider = create_settings_backed_mcp_tool_provider(config) + # Arrange: the server no longer accepts the stored refresh token. + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + client_info = _installed_client(oauth_provider) + mcp_config = _expired_inline_oauth_server( + protected_oauth_mcp_server, client_info, "revoked-refresh-token" + ) + provider = create_settings_backed_mcp_tool_provider(config, _settings_store(config)) - # Act - with pytest.raises(MCPError) as exc_info: - provider.create_tools(mcp_config, timeout=5.0) + # Act + with pytest.raises(MCPError) as exc_info: + provider.create_tools(mcp_config, timeout=5.0) - # Assert: no browser or callback wait, no new client, and nothing - # posted over the credentials the app server holds. - assert not isinstance(exc_info.value, MCPTimeoutError) - assert opened == [] - assert list(oauth_provider.clients) == ["installed-client"] - assert write_backs == [] - finally: - reset_stores() + # Assert: no browser or callback wait, no new client, and nothing + # posted over the credentials the app server holds. + assert not isinstance(exc_info.value, MCPTimeoutError) + assert opened == [] + assert list(oauth_provider.clients) == ["installed-client"] + assert write_backs == [] def test_settings_backed_provider_does_not_write_back_a_client_it_registers( @@ -853,28 +818,24 @@ def test_settings_backed_provider_does_not_write_back_a_client_it_registers( FastMCP registers a loopback client before it asks for authorization; that client must not reach the app server. """ - reset_stores() opened: list[str] = [] monkeypatch.setattr(webbrowser, "open", lambda url, *_, **__: opened.append(url)) - try: - # Arrange - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - mcp_config = _expired_inline_oauth_server( - protected_oauth_mcp_server, None, "stored-refresh-token" - ) - provider = create_settings_backed_mcp_tool_provider(config) + # Arrange + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + mcp_config = _expired_inline_oauth_server( + protected_oauth_mcp_server, None, "stored-refresh-token" + ) + provider = create_settings_backed_mcp_tool_provider(config, _settings_store(config)) - # Act - with pytest.raises(MCPError) as exc_info: - provider.create_tools(mcp_config, timeout=5.0) + # Act + with pytest.raises(MCPError) as exc_info: + provider.create_tools(mcp_config, timeout=5.0) - # Assert - assert not isinstance(exc_info.value, MCPTimeoutError) - assert opened == [] - assert write_backs == [] - finally: - reset_stores() + # Assert + assert not isinstance(exc_info.value, MCPTimeoutError) + assert opened == [] + assert write_backs == [] def test_settings_backed_provider_writes_back_tokens_refreshed_on_connect( @@ -883,36 +844,32 @@ def test_settings_backed_provider_writes_back_tokens_refreshed_on_connect( protected_oauth_mcp_server: str, write_backs: list[dict], ): - reset_stores() - try: - # Arrange: an expired access token and a refresh token the server issued. - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - client_info = _installed_client(oauth_provider) - oauth_provider.refresh_tokens["issued-refresh-token"] = RefreshToken( - token="issued-refresh-token", - client_id="installed-client", - scopes=["mail.read"], - ) - mcp_config = _expired_inline_oauth_server( - protected_oauth_mcp_server, client_info, "issued-refresh-token" - ) - provider = create_settings_backed_mcp_tool_provider(config) + # Arrange: an expired access token and a refresh token the server issued. + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + client_info = _installed_client(oauth_provider) + oauth_provider.refresh_tokens["issued-refresh-token"] = RefreshToken( + token="issued-refresh-token", + client_id="installed-client", + scopes=["mail.read"], + ) + mcp_config = _expired_inline_oauth_server( + protected_oauth_mcp_server, client_info, "issued-refresh-token" + ) + provider = create_settings_backed_mcp_tool_provider(config, _settings_store(config)) - # Act - with provider.create_tools(mcp_config, timeout=10.0) as client: - tool_names = [tool.name for tool in client.tools] + # Act + with provider.create_tools(mcp_config, timeout=10.0) as client: + tool_names = [tool.name for tool in client.tools] - # Assert: connected with the rotated tokens, which went back to the - # app server along with the client and the new expiry. - assert "read_subject" in tool_names - state = write_backs[-1]["oauth_state"] - assert state["tokens"]["refresh_token"] in oauth_provider.refresh_tokens - assert state["tokens"]["refresh_token"] != "issued-refresh-token" - assert state["client_info"]["client_id"] == "installed-client" - assert state["token_expires_at"] > time.time() - finally: - reset_stores() + # Assert: connected with the rotated tokens, which went back to the + # app server along with the client and the new expiry. + assert "read_subject" in tool_names + state = write_backs[-1]["oauth_state"] + assert state["tokens"]["refresh_token"] in oauth_provider.refresh_tokens + assert state["tokens"]["refresh_token"] != "issued-refresh-token" + assert state["client_info"]["client_id"] == "installed-client" + assert state["token_expires_at"] > time.time() @pytest.mark.parametrize("stored_client", [True, False]) @@ -925,34 +882,30 @@ def test_settings_backed_provider_writes_back_tokens_refreshed_for_a_static_clie ): """FastMCP rewrites a static client's registration with a loopback redirect URI on every connect; its refreshed tokens still go back.""" - reset_stores() - try: - # Arrange - config = _hosted_config(tmp_path) - get_settings_store(config).save(PersistedSettings()) - client_info = _installed_client(oauth_provider) - oauth_provider.refresh_tokens["issued-refresh-token"] = RefreshToken( - token="issued-refresh-token", - client_id="installed-client", - scopes=["mail.read"], - ) - mcp_config = _expired_inline_oauth_server( - protected_oauth_mcp_server, - client_info if stored_client else None, - "issued-refresh-token", - static_client_id="installed-client", - ) - provider = create_settings_backed_mcp_tool_provider(config) - - # Act - with provider.create_tools(mcp_config, timeout=10.0) as client: - tool_names = [tool.name for tool in client.tools] - - # Assert - assert "read_subject" in tool_names - state = write_backs[-1]["oauth_state"] - assert state["tokens"]["refresh_token"] in oauth_provider.refresh_tokens - assert state["tokens"]["refresh_token"] != "issued-refresh-token" - assert state["client_info"]["client_id"] == "installed-client" - finally: - reset_stores() + # Arrange + config = _hosted_config(tmp_path) + _settings_store(config).save(PersistedSettings()) + client_info = _installed_client(oauth_provider) + oauth_provider.refresh_tokens["issued-refresh-token"] = RefreshToken( + token="issued-refresh-token", + client_id="installed-client", + scopes=["mail.read"], + ) + mcp_config = _expired_inline_oauth_server( + protected_oauth_mcp_server, + client_info if stored_client else None, + "issued-refresh-token", + static_client_id="installed-client", + ) + provider = create_settings_backed_mcp_tool_provider(config, _settings_store(config)) + + # Act + with provider.create_tools(mcp_config, timeout=10.0) as client: + tool_names = [tool.name for tool in client.tools] + + # Assert + assert "read_subject" in tool_names + state = write_backs[-1]["oauth_state"] + assert state["tokens"]["refresh_token"] in oauth_provider.refresh_tokens + assert state["tokens"]["refresh_token"] != "issued-refresh-token" + assert state["client_info"]["client_id"] == "installed-client" diff --git a/tests/agent_server/test_mcp_router.py b/tests/agent_server/test_mcp_router.py index cb497622cc5..842838cb129 100644 --- a/tests/agent_server/test_mcp_router.py +++ b/tests/agent_server/test_mcp_router.py @@ -30,7 +30,9 @@ _oauth_probe_jobs_lock, _register_oauth_job, ) -from openhands.agent_server.persistence import get_secrets_store, reset_stores +from openhands.agent_server.persistence import ( + ServerStores, +) from openhands.agent_server.persistence.store import FileSecretsStore from openhands.sdk.mcp.config import to_fastmcp_mcp_config from tests.agent_server.conftest import FakeMCPTools @@ -48,11 +50,9 @@ @pytest.fixture def client() -> Generator[TestClient]: - reset_stores() config = Config(session_api_keys=[]) # Disable authentication. with TestClient(create_app(config), raise_server_exceptions=False) as test_client: yield test_client - reset_stores() @pytest.fixture @@ -357,7 +357,9 @@ def test_mcp_test_expands_remote_secret_reference_from_saved_secrets( """A remote server saved with ``Bearer ${NAME}`` connects with the saved secret, as a conversation would, instead of sending the placeholder.""" config = Config(session_api_keys=[]) - get_secrets_store(config).set_secret("SONAR_TOKEN", "sonar-saved-token") + ServerStores.from_config(config).secrets.set_secret( + "SONAR_TOKEN", "sonar-saved-token" + ) client = TestClient(create_app(config), raise_server_exceptions=False) response = client.post( @@ -388,7 +390,9 @@ def test_mcp_test_expands_references_like_a_conversation( secret is masked in the returned tool output; the short environment value is not.""" config = Config(session_api_keys=[]) - get_secrets_store(config).set_secret("HF_BOT_TOKEN", "xoxb-saved-secret") + ServerStores.from_config(config).secrets.set_secret( + "HF_BOT_TOKEN", "xoxb-saved-secret" + ) monkeypatch.setenv("HF_TEAM_ID", "T0123") monkeypatch.delenv("HF_CHANNEL", raising=False) client = TestClient(create_app(config), raise_server_exceptions=False) @@ -465,7 +469,7 @@ def test_mcp_test_masks_substituted_secrets_in_errors_and_logs( url: str, ): config = Config(session_api_keys=[]) - get_secrets_store(config).set_secret("URL_TOKEN", secret) + ServerStores.from_config(config).secrets.set_secret("URL_TOKEN", secret) client = TestClient(create_app(config), raise_server_exceptions=False) _fail_with_expanded_url(fake_mcp_tools) @@ -552,7 +556,7 @@ def test_mcp_test_masks_an_expanded_basic_credential( """Basic auth goes out as ``base64(user:password)``; that form is masked too, not only the substituted password.""" config = Config(session_api_keys=[]) - get_secrets_store(config).set_secret("HF_DB_PASS", "s3cret-pw") + ServerStores.from_config(config).secrets.set_secret("HF_DB_PASS", "s3cret-pw") client = TestClient(create_app(config), raise_server_exceptions=False) token = base64.b64encode(b"user:s3cret-pw").decode() @@ -647,7 +651,7 @@ def test_mcp_test_keeps_brace_text_that_expansion_leaves_literal( """``${...}`` text from a secret's value or a default is not a reference the user wrote: like a conversation, pass it on instead of failing.""" config = Config(session_api_keys=[]) - store = get_secrets_store(config) + store = ServerStores.from_config(config).secrets store.set_secret("PASSWORD", "a-${OTHER}-b") store.set_secret("ENDPOINT", "https://h/${path}/mcp") store.set_secret("B", "set") @@ -713,7 +717,7 @@ def test_mcp_test_expands_an_oauth_server_but_sends_its_auth_as_written( """The OAuth client copies a configured client secret into the state the caller saves, so ``auth`` goes out as written while the rest expands.""" config = Config(session_api_keys=[]) - store = get_secrets_store(config) + store = ServerStores.from_config(config).secrets store.set_secret("HF_TEAM", "team-42") store.set_secret("HF_CLIENT_SECRET", "oauth-client-secret") client = TestClient(create_app(config), raise_server_exceptions=False) @@ -810,7 +814,9 @@ def test_mcp_test_keeps_the_secret_out_of_a_post_expansion_validation_error( """A substituted header name of ``Authorization`` next to ``auth`` fails validation; pydantic's own message would echo the expanded config.""" config = Config(session_api_keys=[]) - get_secrets_store(config).set_secret("HF_TOKEN", "REAL-SECRET-VALUE-123") + ServerStores.from_config(config).secrets.set_secret( + "HF_TOKEN", "REAL-SECRET-VALUE-123" + ) monkeypatch.setenv("HF_HEADER", "Authorization") client = TestClient(create_app(config), raise_server_exceptions=False) @@ -867,7 +873,7 @@ def test_mcp_test_reads_the_secrets_file_once_per_name( monkeypatch: pytest.MonkeyPatch, fake_mcp_tools: FakeMCPTools ): config = Config(session_api_keys=[]) - get_secrets_store(config).set_secret("HF_TOKEN", "tok-value") + ServerStores.from_config(config).secrets.set_secret("HF_TOKEN", "tok-value") client = TestClient(create_app(config), raise_server_exceptions=False) loads: list[str] = [] real_load = FileSecretsStore.load diff --git a/tests/agent_server/test_meta_profiles_router.py b/tests/agent_server/test_meta_profiles_router.py index ea187bfa51f..a18c981f6e6 100644 --- a/tests/agent_server/test_meta_profiles_router.py +++ b/tests/agent_server/test_meta_profiles_router.py @@ -9,7 +9,9 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config -from openhands.agent_server.persistence import get_settings_store, reset_stores +from openhands.agent_server.persistence import ( + ServerStores, +) from openhands.sdk.llm.meta_profile_store import MetaProfile, MetaProfileStore from openhands.sdk.settings.model import OpenHandsAgentSettings @@ -34,14 +36,12 @@ def temp_meta_profiles_dir(temp_settings_dir): @pytest.fixture def client(temp_meta_profiles_dir, temp_settings_dir, monkeypatch): - reset_stores() monkeypatch.setenv("OH_PERSISTENCE_DIR", str(temp_settings_dir)) config = Config(static_files_path=None, session_api_keys=[], secret_key=None) app = create_app(config) # No MetaProfileStore patch needed: the router now honors OH_PERSISTENCE_DIR # and resolves the same dir the store fixture seeds. yield TestClient(app) - reset_stores() @pytest.fixture @@ -246,7 +246,7 @@ def test_delete_active_clears_nested_agent_settings(client, store): client.delete("/api/meta-profiles/p") - persisted = get_settings_store().load() + persisted = ServerStores.from_config(Config()).settings.load() assert persisted is not None agent = persisted.agent_settings assert isinstance(agent, OpenHandsAgentSettings) @@ -279,7 +279,7 @@ def test_activate_propagates_into_agent_settings(client, store): response = client.post("/api/meta-profiles/p/activate") assert response.status_code == 200 - persisted = get_settings_store().load() + persisted = ServerStores.from_config(Config()).settings.load() assert persisted is not None assert persisted.active_meta_profile == "p" agent = persisted.agent_settings @@ -325,7 +325,7 @@ def test_activate_on_acp_settings_reports_not_applied(client, store): } assert client.get("/api/meta-profiles").json()["active_meta_profile"] is None - persisted = get_settings_store().load() + persisted = ServerStores.from_config(Config()).settings.load() assert persisted is not None assert persisted.active_meta_profile is None @@ -365,7 +365,9 @@ def test_activate_settings_io_error_returns_500(client, store, monkeypatch): def fail(self, update_fn): raise OSError("disk full") - monkeypatch.setattr(type(get_settings_store()), "update", fail) + monkeypatch.setattr( + type(ServerStores.from_config(Config()).settings), "update", fail + ) response = client.post("/api/meta-profiles/p/activate") @@ -397,7 +399,7 @@ def test_concurrent_delete_and_activate_keep_settings_consistent(client, store): for future in futures: assert future.result().status_code == 200 - persisted = get_settings_store().load() + persisted = ServerStores.from_config(Config()).settings.load() assert persisted is not None active = persisted.active_meta_profile # "p" was deleted, so it must never remain active; outcome is "q" or None diff --git a/tests/agent_server/test_openai_gateway_docker.py b/tests/agent_server/test_openai_gateway_docker.py index 0d1208df9b9..83632826556 100644 --- a/tests/agent_server/test_openai_gateway_docker.py +++ b/tests/agent_server/test_openai_gateway_docker.py @@ -20,6 +20,7 @@ from openhands.agent_server.event_router import run_gateway_message from openhands.agent_server.event_service import ConversationRunLimitExceeded from openhands.agent_server.openai import service as gateway +from openhands.agent_server.persistence import ServerStores from openhands.sdk.conversation.conversation_stats import ConversationStats from openhands.sdk.conversation.message_request import SendMessageRequest from openhands.sdk.conversation.request import StartConversationRequest @@ -36,7 +37,7 @@ def _setup(tmp_path): workspace_path=tmp_path / "outer-workspace", secret_key=SecretStr("outer-key"), ) - registry = DockerConversationRegistry(config) + registry = DockerConversationRegistry(config, ServerStores.from_config(config)) outer_service = AsyncMock() request = StartConversationRequest( agent_profile_id=uuid4(), @@ -184,7 +185,7 @@ async def test_f55r_resume_gateway_run_stays_in_existing_docker_container( "conversation_runtime_selectable": True, } ) - registry = HybridConversationRegistry(config) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) conversation_id = uuid4() registry.provisioning.create(conversation_id) requests = [] @@ -443,7 +444,7 @@ async def test_docker_gateway_rejects_id_owned_by_local_conversation(tmp_path): async def test_hybrid_gateway_keeps_a_local_owned_conversation_local(tmp_path): config, _, outer_service, start_request = _setup(tmp_path) config = config.model_copy(update={"conversation_runtime_selectable": True}) - registry = HybridConversationRegistry(config) + registry = HybridConversationRegistry(config, ServerStores.from_config(config)) conversation_id = uuid4() conversation_dir = registry.conversation_dir(conversation_id) conversation_dir.mkdir(parents=True) diff --git a/tests/agent_server/test_openai_gateway_launch.py b/tests/agent_server/test_openai_gateway_launch.py index a71e4b9c538..f85727027b5 100644 --- a/tests/agent_server/test_openai_gateway_launch.py +++ b/tests/agent_server/test_openai_gateway_launch.py @@ -6,9 +6,7 @@ from openhands.agent_server.openai.service import _create_conversation_request from openhands.agent_server.persistence import ( PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_settings_store, + ServerStores, ) from openhands.sdk import LLM from openhands.sdk.context.agent_context import AgentContext @@ -23,20 +21,21 @@ def config(tmp_path, monkeypatch) -> Config: monkeypatch.setattr( "openhands.agent_server.launch.discover_profile_skills", lambda: [] ) - get_llm_profile_store().save("fast", LLM(model="fast-model")) - get_llm_profile_store().save("slow", LLM(model="slow-model")) - return Config( + config = Config( conversations_path=tmp_path / "conversations", workspace_path=tmp_path / "workspace", session_api_keys=[], ) + stores = ServerStores.from_config(config) + stores.llm_profiles.save("fast", LLM(model="fast-model")) + stores.llm_profiles.save("slow", LLM(model="slow-model")) + return config def _activate(config: Config, profile: OpenHandsAgentProfile | ACPAgentProfile): - get_agent_profile_store().save(profile) - get_settings_store(config).save( - PersistedSettings(active_agent_profile_id=str(profile.id)) - ) + stores = ServerStores.from_config(config) + stores.agent_profiles.save(profile) + stores.settings.save(PersistedSettings(active_agent_profile_id=str(profile.id))) def _gateway_request(config: Config, model: str) -> StartConversationRequest: @@ -45,14 +44,18 @@ def _gateway_request(config: Config, model: str) -> StartConversationRequest: system_text="Answer briefly.", user_content=[], config=config, + stores=ServerStores.from_config(config), conversation_id=None, ) def _launch(config: Config, request: StartConversationRequest) -> ResolvedLaunch: - settings = get_settings_store(config).load() or PersistedSettings() + stores = ServerStores.from_config(config) + settings = stores.settings.load() or PersistedSettings() source = launch_source( - request, lambda: server_launch_stores(settings, config.cipher), config.cipher + request, + lambda: server_launch_stores(settings, stores, config.cipher), + config.cipher, ) assert isinstance(source, ResolvedLaunch) return source @@ -83,10 +86,10 @@ def test_the_gateway_launches_an_acp_profile_with_its_own_model(config): def test_without_an_active_profile_the_gateway_activates_default(config): - get_agent_profile_store().save( + ServerStores.from_config(Config()).agent_profiles.save( OpenHandsAgentProfile(name="inactive", llm_profile_ref="slow") ) - get_settings_store(config).save( + ServerStores.from_config(config).settings.save( PersistedSettings( agent_settings=OpenHandsAgentSettings( agent_context=AgentContext(system_message_suffix="Global suffix.") @@ -101,9 +104,9 @@ def test_without_an_active_profile_the_gateway_activates_default(config): assert launched.settings.agent_context.system_message_suffix == "Global suffix." assert launched.profile is not None assert launched.profile.llm_profile_ref == "fast" - default = get_agent_profile_store().load("default") + default = ServerStores.from_config(Config()).agent_profiles.load("default") assert launched.profile.agent_profile_id == default.id - settings = get_settings_store(config).load() + settings = ServerStores.from_config(config).settings.load() assert settings is not None assert settings.active_agent_profile_id == str(default.id) diff --git a/tests/agent_server/test_profile_cipher_mismatch.py b/tests/agent_server/test_profile_cipher_mismatch.py index fe2b6000282..790a51f3b9d 100644 --- a/tests/agent_server/test_profile_cipher_mismatch.py +++ b/tests/agent_server/test_profile_cipher_mismatch.py @@ -5,11 +5,8 @@ """ import json -import os -import tempfile from contextlib import contextmanager from pathlib import Path -from unittest.mock import patch import pytest from fastapi.testclient import TestClient @@ -17,10 +14,8 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config -from openhands.agent_server.persistence import reset_stores from openhands.sdk.llm import LLM from openhands.sdk.llm.llm_profile_store import LLMProfileStore -from openhands.sdk.profiles import AgentProfileStore from openhands.sdk.utils.cipher import Cipher @@ -30,18 +25,13 @@ @pytest.fixture -def dirs(): - with tempfile.TemporaryDirectory() as tmpdir: - root = Path(tmpdir) - reset_stores() - old_val = os.environ.get("OH_PERSISTENCE_DIR") - os.environ["OH_PERSISTENCE_DIR"] = str(root / "settings") - yield root - reset_stores() - if old_val is not None: - os.environ["OH_PERSISTENCE_DIR"] = old_val - else: - os.environ.pop("OH_PERSISTENCE_DIR", None) +def dirs(tmp_path, monkeypatch): + """The server's persistence directory: ``settings.json``, ``profiles/`` and + ``agent-profiles/`` all live in it.""" + root = tmp_path / "persistence" + root.mkdir() + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(root)) + return root @contextmanager @@ -52,21 +42,7 @@ def _client(dirs: Path, previous_keys: tuple[str, ...] = ()): secret_key=SecretStr(KEY_B), previous_secret_keys=[SecretStr(k) for k in previous_keys], ) - with ( - patch( - "openhands.agent_server.profiles_router.get_llm_profile_store", - lambda: LLMProfileStore(base_dir=dirs / "profiles"), - ), - patch( - "openhands.agent_server.settings_router.get_llm_profile_store", - lambda: LLMProfileStore(base_dir=dirs / "profiles"), - ), - patch( - "openhands.agent_server.profiles_router.get_agent_profile_store", - lambda: AgentProfileStore(base_dir=dirs / "agent-profiles"), - ), - ): - yield TestClient(create_app(config)) + yield TestClient(create_app(config)) @pytest.fixture @@ -94,7 +70,7 @@ def _profile_key_under_key_a(dirs: Path) -> str | None: def test_activate_undecryptable_profile_refused(client_b: TestClient, dirs: Path): before = _seed_profile_under_key_a(dirs) - settings_path = dirs / "settings" / "settings.json" + settings_path = dirs / "settings.json" assert not settings_path.exists() response = client_b.post(f"/api/profiles/{PROFILE}/activate") @@ -122,7 +98,7 @@ def test_patch_active_undecryptable_profile_refused(client_b: TestClient, dirs: f"Profile '{PROFILE}' is encrypted with a different key" ) assert "OH_PREVIOUS_SECRET_KEYS" in response.json()["detail"] - assert not (dirs / "settings" / "settings.json").exists() + assert not (dirs / "settings.json").exists() def test_resave_undecryptable_profile_refused(client_b: TestClient, dirs: Path): @@ -185,7 +161,6 @@ def test_previous_key_lets_a_profile_activate_and_resave(dirs: Path): stored = json.loads((dirs / "profiles" / f"{PROFILE}.json").read_text()) assert Cipher(KEY_B).try_decrypt_str(stored["api_key"]) == "sk-a" - reset_stores() # The previous key is no longer needed for the profile or the settings. with _client(dirs) as client: diff --git a/tests/agent_server/test_profile_resume_secret_scope.py b/tests/agent_server/test_profile_resume_secret_scope.py index a30fef3af98..82246b9ac99 100644 --- a/tests/agent_server/test_profile_resume_secret_scope.py +++ b/tests/agent_server/test_profile_resume_secret_scope.py @@ -8,11 +8,7 @@ from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.credential_binding import LocalVersionedCredentialBinding from openhands.agent_server.models import StartConversationRequest -from openhands.agent_server.persistence import ( - FileSecretsStore, - get_agent_profile_store, - reset_stores, -) +from openhands.agent_server.persistence import ServerStores from openhands.sdk.credential import CredentialAuthorizationRejected from openhands.sdk.profiles.agent_profile import ACPAgentProfile from openhands.sdk.secret import StaticSecret @@ -21,12 +17,14 @@ @pytest.fixture -def scoped_credentials(tmp_path, monkeypatch): - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "settings")) - reset_stores() +def scoped_credentials(tmp_path): + """A server's stores under ``tmp_path / "settings"``, and their cipher.""" cipher = Cipher("synthetic-profile-scope-key") - yield FileSecretsStore(tmp_path / "settings", cipher=cipher), cipher - reset_stores() + settings_dir = tmp_path / "settings" + stores = ServerStores( + cipher=cipher, profile_dir=settings_dir, workspace_dir=settings_dir + ) + return stores, cipher @pytest.mark.asyncio @@ -34,7 +32,8 @@ def scoped_credentials(tmp_path, monkeypatch): async def test_profile_scope_controls_saved_codex_binding_after_restart( tmp_path, scoped_credentials, secret_refs ): - store, cipher = scoped_credentials + stores, cipher = scoped_credentials + store = stores.secrets store.set_secret( "CODEX_AUTH_JSON", json.dumps({"auth_mode": "chatgpt", "tokens": {"refresh_token": "dummy"}}), @@ -42,14 +41,14 @@ async def test_profile_scope_controls_saved_codex_binding_after_restart( profile = ACPAgentProfile( name="codex-scope", acp_server="codex", secret_refs=secret_refs ) - get_agent_profile_store().save(profile) + stores.agent_profiles.save(profile) request = StartConversationRequest( agent_profile_id=profile.id, workspace=LocalWorkspace(working_dir=tmp_path / "workspace"), ) expected = {"CODEX_AUTH_JSON"} if secret_refs != [] else set() async with ConversationService( - conversations_dir=tmp_path / "conversations", secrets_store=store, cipher=cipher + conversations_dir=tmp_path / "conversations", stores=stores, cipher=cipher ) as service: info, _ = await service.start_conversation(request) event_service = await service.get_event_service(info.id) @@ -62,9 +61,9 @@ async def test_profile_scope_controls_saved_codex_binding_after_restart( "CODEX_AUTH_JSON", LocalVersionedCredentialBinding(store, "CODEX_AUTH_JSON"), ) - get_agent_profile_store().save(profile.model_copy(update={"secret_refs": None})) + stores.agent_profiles.save(profile.model_copy(update={"secret_refs": None})) async with ConversationService( - conversations_dir=tmp_path / "conversations", secrets_store=store, cipher=cipher + conversations_dir=tmp_path / "conversations", stores=stores, cipher=cipher ) as service: event_service = await service.get_event_service(info.id) assert event_service is not None @@ -79,22 +78,22 @@ async def test_profile_scope_controls_saved_codex_binding_after_restart( async def test_profile_scope_filters_resume_supplied_codex_secret( tmp_path, scoped_credentials, secret_refs, cold_resume ): - store, cipher = scoped_credentials + stores, cipher = scoped_credentials profile = ACPAgentProfile( name="codex-scope", acp_server="codex", secret_refs=secret_refs ) - get_agent_profile_store().save(profile) + stores.agent_profiles.save(profile) request = StartConversationRequest( agent_profile_id=profile.id, workspace=LocalWorkspace(working_dir=tmp_path / "workspace"), ) async with ConversationService( - conversations_dir=tmp_path / "conversations", secrets_store=store, cipher=cipher + conversations_dir=tmp_path / "conversations", stores=stores, cipher=cipher ) as service: info, _ = await service.start_conversation(request) if cold_resume: await service.prepare_for_sandbox_pause() - get_agent_profile_store().save(profile.model_copy(update={"secret_refs": None})) + stores.agent_profiles.save(profile.model_copy(update={"secret_refs": None})) resume = request.model_copy( update={ "conversation_id": info.id, diff --git a/tests/agent_server/test_profile_store_lock_responsiveness.py b/tests/agent_server/test_profile_store_lock_responsiveness.py index 94384039a30..ffef832c82d 100644 --- a/tests/agent_server/test_profile_store_lock_responsiveness.py +++ b/tests/agent_server/test_profile_store_lock_responsiveness.py @@ -17,7 +17,6 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config -from openhands.agent_server.persistence import reset_stores # Upper bound for the held lock: if the handler blocks the event loop, nothing @@ -84,7 +83,6 @@ async def test_alive_answers_while_store_request_waits_for_lock( expected_status: int, ) -> None: monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path)) - reset_stores() app = create_app( Config(static_files_path=None, session_api_keys=[], secret_key=None) ) @@ -126,4 +124,3 @@ async def test_alive_answers_while_store_request_waits_for_lock( safety.cancel() if held.is_locked: held.release(force=True) - reset_stores() diff --git a/tests/agent_server/test_profile_store_persistence_dir.py b/tests/agent_server/test_profile_store_persistence_dir.py index 9e74d3cb56b..1a8ce63b6e1 100644 --- a/tests/agent_server/test_profile_store_persistence_dir.py +++ b/tests/agent_server/test_profile_store_persistence_dir.py @@ -2,43 +2,25 @@ from __future__ import annotations -import os -import tempfile -from collections.abc import Iterator from pathlib import Path import pytest +from pydantic import SecretStr from openhands.agent_server.config import Config -from openhands.agent_server.persistence import ( - PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_secrets_store, - get_settings_store, - reset_stores, -) +from openhands.agent_server.persistence import PersistedSettings, ServerStores @pytest.fixture -def isolated_persistence_dir() -> Iterator[Path]: - """``OH_PERSISTENCE_DIR`` pointed at a clean tempdir, stores reset.""" - with tempfile.TemporaryDirectory() as tmpdir: - reset_stores() - old_val = os.environ.get("OH_PERSISTENCE_DIR") - os.environ["OH_PERSISTENCE_DIR"] = tmpdir - try: - yield Path(tmpdir) - finally: - reset_stores() - if old_val is not None: - os.environ["OH_PERSISTENCE_DIR"] = old_val - else: - os.environ.pop("OH_PERSISTENCE_DIR", None) +def isolated_persistence_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: + """``OH_PERSISTENCE_DIR`` pointed at a clean directory.""" + persistence_dir = tmp_path / "persistence" + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(persistence_dir)) + return persistence_dir def test_llm_profile_store_uses_persistence_dir(isolated_persistence_dir: Path) -> None: - store = get_llm_profile_store() + store = ServerStores.from_config(Config()).llm_profiles assert store.base_dir == isolated_persistence_dir / "profiles" assert store.list() == [] @@ -46,7 +28,7 @@ def test_llm_profile_store_uses_persistence_dir(isolated_persistence_dir: Path) def test_agent_profile_store_uses_persistence_dir( isolated_persistence_dir: Path, ) -> None: - store = get_agent_profile_store() + store = ServerStores.from_config(Config()).agent_profiles assert store.base_dir == isolated_persistence_dir / "agent-profiles" assert store.list() == [] @@ -54,30 +36,26 @@ def test_agent_profile_store_uses_persistence_dir( @pytest.fixture def home_without_persistence_env( monkeypatch: pytest.MonkeyPatch, tmp_path: Path -) -> Iterator[Path]: +) -> Path: """No ``OH_PERSISTENCE_DIR``; ``Path.home()`` redirected to a tempdir.""" - reset_stores() monkeypatch.delenv("OH_PERSISTENCE_DIR", raising=False) fake_home = tmp_path / "home" fake_home.mkdir() monkeypatch.setattr(Path, "home", lambda: fake_home) - try: - yield fake_home - finally: - reset_stores() + return fake_home def test_llm_profile_store_falls_back_to_home( home_without_persistence_env: Path, ) -> None: - store = get_llm_profile_store() + store = ServerStores.from_config(Config()).llm_profiles assert store.base_dir == home_without_persistence_env / ".openhands" / "profiles" def test_agent_profile_store_falls_back_to_home( home_without_persistence_env: Path, ) -> None: - store = get_agent_profile_store() + store = ServerStores.from_config(Config()).agent_profiles assert ( store.base_dir == home_without_persistence_env / ".openhands" / "agent-profiles" ) @@ -92,10 +70,11 @@ def test_settings_and_secrets_stores_fall_back_to_home( repo.mkdir() monkeypatch.chdir(repo) config = Config(conversations_path=Path("workspace/conversations")) + stores = ServerStores.from_config(config) - settings_store = get_settings_store(config) + settings_store = stores.settings settings_store.save(PersistedSettings()) - secrets_store = get_secrets_store(config) + secrets_store = stores.secrets secrets_store.set_secret("OPENAI_API_KEY", "sk-test") expected_dir = home_without_persistence_env / ".openhands" @@ -111,8 +90,9 @@ def test_profile_stores_do_not_read_home_directory( isolated_persistence_dir: Path, ) -> None: """The host user's ``~/.openhands/profiles/*.json`` must not appear.""" - llm = get_llm_profile_store() - agent = get_agent_profile_store() + stores = ServerStores.from_config(Config()) + llm = stores.llm_profiles + agent = stores.agent_profiles # Both stores must materialize their own directory *inside* the # persistence dir, not anywhere else. @@ -125,3 +105,39 @@ def test_profile_stores_do_not_read_home_directory( # And the new dir should contain nothing the host happens to have. visible_names = set(llm.list()) | set(agent.list()) assert visible_names == set() + + +def test_stores_keep_the_directory_and_cipher_they_were_built_with( + isolated_persistence_dir: Path, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """One server (re)initialisation, one location and key for all its stores. + + The environment is read when the stores are built. Changing it afterwards + moves the stores of the next build, never half of the current ones. + """ + config = Config(secret_key=SecretStr("first-key")) + stores = ServerStores.from_config(config) + settings_before = stores.settings + + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "elsewhere")) + + assert stores.settings is settings_before + assert stores.secrets.persistence_dir == isolated_persistence_dir + assert stores.llm_profiles.base_dir == isolated_persistence_dir / "profiles" + assert stores.secrets.cipher is stores.settings.cipher is config.cipher + + rebuilt = ServerStores.from_config(Config(secret_key=SecretStr("second-key"))) + assert rebuilt.settings.persistence_dir == tmp_path / "elsewhere" + assert rebuilt.settings.cipher is not config.cipher + + +def test_building_stores_touches_no_file(isolated_persistence_dir: Path) -> None: + """A dormant server builds its stores before its volume may be writable.""" + stores = ServerStores.from_config(Config()) + + assert not isolated_persistence_dir.exists() + + stores.llm_profiles + assert (isolated_persistence_dir / "profiles").is_dir() diff --git a/tests/agent_server/test_profiles_router.py b/tests/agent_server/test_profiles_router.py index 5ee47646230..064c91d7050 100644 --- a/tests/agent_server/test_profiles_router.py +++ b/tests/agent_server/test_profiles_router.py @@ -1,9 +1,7 @@ """Tests for profiles_router endpoints.""" -import tempfile import time import warnings -from pathlib import Path from typing import Any from unittest.mock import patch @@ -14,7 +12,6 @@ from openhands.agent_server import profiles_router as profiles_router_module from openhands.agent_server.api import create_app from openhands.agent_server.config import Config -from openhands.agent_server.persistence import reset_stores from openhands.sdk.llm import LLM, Message from openhands.sdk.llm.auth.credentials import OAuthCredentials from openhands.sdk.llm.llm_profile_store import LLMProfileStore @@ -27,90 +24,41 @@ @pytest.fixture -def temp_profiles_dir(): - """Create a temporary directory for profiles.""" - with tempfile.TemporaryDirectory() as tmpdir: - profiles_dir = Path(tmpdir) / "profiles" - profiles_dir.mkdir(parents=True, exist_ok=True) - yield profiles_dir +def temp_settings_dir(tmp_path, monkeypatch): + """The server's persistence directory; ``settings.json`` lives in it.""" + settings_dir = tmp_path / "settings" + settings_dir.mkdir() + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(settings_dir)) + return settings_dir @pytest.fixture -def temp_agent_profiles_dir(): - """Create a temporary directory for agent profiles (FK store).""" - with tempfile.TemporaryDirectory() as tmpdir: - agent_dir = Path(tmpdir) / "agent-profiles" - agent_dir.mkdir(parents=True, exist_ok=True) - yield agent_dir +def temp_profiles_dir(temp_settings_dir): + """Where the server's stores keep LLM profiles.""" + profiles_dir = temp_settings_dir / "profiles" + profiles_dir.mkdir() + return profiles_dir @pytest.fixture -def temp_settings_dir(): - """Create a temporary directory for settings.""" - with tempfile.TemporaryDirectory() as tmpdir: - settings_dir = Path(tmpdir) / "settings" - settings_dir.mkdir(parents=True, exist_ok=True) - yield settings_dir +def temp_agent_profiles_dir(temp_settings_dir): + """Where the server's stores keep agent profiles (FK store).""" + agent_dir = temp_settings_dir / "agent-profiles" + agent_dir.mkdir() + return agent_dir @pytest.fixture -def client(temp_profiles_dir, temp_agent_profiles_dir, temp_settings_dir, monkeypatch): - """Create test client with isolated profiles/settings directories, no cipher.""" - # Reset store singletons to ensure clean state - reset_stores() - - # Set environment variable for persistence directory - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(temp_settings_dir)) +def client(temp_profiles_dir, temp_agent_profiles_dir, temp_settings_dir): + """Create test client with isolated profiles/settings directories, no cipher. + The app's own stores live in those directories (its provider connections in + ``provider-connections`` beside the profiles), so the routes and the tests + read and write the same files. + """ # Explicitly disable cipher by setting secret_key to None config = Config(static_files_path=None, session_api_keys=[], secret_key=None) - app = create_app(config) - - # Patch stores to use temp directories (AgentProfileStore is hit by the - # FK guard on delete/rename). The LLM profile store is wired to a shared - # provider-connection store so ``load`` resolves provider references, and - # the router's own ``get_provider_connections_store`` returns the same - # instance so CRUD and resolution see one file. - provider_store = ProviderConnectionStore( - base_dir=temp_profiles_dir.parent / "provider-connections" - ) - - def make_llm_store(): - return LLMProfileStore( - base_dir=temp_profiles_dir, provider_store=provider_store - ) - - with ( - patch( - "openhands.agent_server.profiles_router.get_llm_profile_store", - make_llm_store, - ), - patch( - "openhands.agent_server.profiles_router.get_agent_profile_store", - lambda: AgentProfileStore(base_dir=temp_agent_profiles_dir), - ), - patch( - "openhands.agent_server.profiles_router.get_provider_connections_store", - lambda config=None: provider_store, - ), - patch( - "openhands.agent_server.settings_router.get_llm_profile_store", - make_llm_store, - ), - patch( - "openhands.agent_server.provider_connections_router.get_llm_profile_store", - make_llm_store, - ), - patch( - "openhands.agent_server.provider_connections_router." - "get_provider_connections_store", - lambda config=None: provider_store, - ), - ): - yield TestClient(app) - - # Reset stores after test - reset_stores() + return TestClient(create_app(config)) @pytest.fixture @@ -1138,38 +1086,16 @@ def client_with_cipher( temp_agent_profiles_dir, temp_settings_dir, secret_key, - monkeypatch, ): """Create test client with cipher configured.""" from pydantic import SecretStr - # Reset store singletons to ensure clean state - reset_stores() - - # Set environment variable for persistence directory - monkeypatch.setenv("OH_PERSISTENCE_DIR", str(temp_settings_dir)) - config = Config( static_files_path=None, session_api_keys=[], secret_key=SecretStr(secret_key), ) - app = create_app(config) - - with ( - patch( - "openhands.agent_server.profiles_router.get_llm_profile_store", - lambda: LLMProfileStore(base_dir=temp_profiles_dir), - ), - patch( - "openhands.agent_server.profiles_router.get_agent_profile_store", - lambda: AgentProfileStore(base_dir=temp_agent_profiles_dir), - ), - ): - yield TestClient(app) - - # Reset stores after test - reset_stores() + return TestClient(create_app(config)) @pytest.fixture diff --git a/tests/agent_server/test_runtime_router.py b/tests/agent_server/test_runtime_router.py index 01a43a5c5f4..c235e295efa 100644 --- a/tests/agent_server/test_runtime_router.py +++ b/tests/agent_server/test_runtime_router.py @@ -4,6 +4,7 @@ import httpx import pytest +from fastapi import FastAPI from fastapi.routing import APIRoute import openhands.agent_server.vscode_service as vscode_service_module @@ -12,7 +13,15 @@ from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.runtime_router import create_runtime_router +from openhands.agent_server.server_context import server_context_of from openhands.agent_server.vscode_service import VSCodeService +from tests.agent_server.conftest import update_test_context + + +def _conversation_service(app: FastAPI) -> ConversationService: + service = server_context_of(app).conversation_service + assert service is not None + return service @pytest.fixture @@ -25,8 +34,11 @@ async def runtime_client(tmp_path): async with ConversationService( conversations_dir=config.conversations_path ) as service: - app.state.conversation_service = service - app.state.bash_event_service = BashEventService(tmp_path / "global-bash") + update_test_context( + app, + conversation_service=service, + bash_event_service=BashEventService(tmp_path / "global-bash"), + ) async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://test", @@ -149,7 +161,8 @@ async def test_conversation_close_stops_runtime_terminal(runtime_client, tmp_pat while not (root / "pid").exists(): await asyncio.sleep(0.01) pid = int((root / "pid").read_text()) - service = await app.state.conversation_service.get_event_service(UUID(cid)) + service = await _conversation_service(app).get_event_service(UUID(cid)) + assert service is not None await service.close() with pytest.raises(ProcessLookupError): os.kill(pid, 0) @@ -203,7 +216,8 @@ def create_service(*args, **kwargs): ) ) assert all(response.status_code == 200 for response in responses) - event_service = await app.state.conversation_service.get_event_service(UUID(cid)) + event_service = await _conversation_service(app).get_event_service(UUID(cid)) + assert event_service is not None assert instances == [event_service.bash_event_service] diff --git a/tests/agent_server/test_server_context.py b/tests/agent_server/test_server_context.py new file mode 100644 index 00000000000..d41a7e91fa1 --- /dev/null +++ b/tests/agent_server/test_server_context.py @@ -0,0 +1,331 @@ +"""One ``ServerContext`` per (re)initialisation, installed in one piece. + +A server derives its stores, registry and services from one config and keeps +them together in a ``ServerContext``. ``POST /api/init`` builds the context of +the delivered config beside the dormant one and swaps it in with a single +assignment, so no request can see the new config with the old stores, or the +old key with the new services; a failed init leaves the dormant context as it +was. Nothing in the server reads a process-wide default instead (see the guard +at the end of this file). +""" + +from __future__ import annotations + +import ast +import dataclasses +import json +from collections.abc import Iterator +from pathlib import Path + +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient +from pydantic import SecretStr + +import openhands.agent_server +from openhands.agent_server import init_router as init_router_module +from openhands.agent_server.api import create_app +from openhands.agent_server.config import Config +from openhands.agent_server.conversation_service import ConversationService +from openhands.agent_server.server_context import ServerContext, server_context_of +from openhands.sdk.utils.cipher import Cipher + + +BOOT_KEY = "boot-secret-key" +USER_KEY = "user-secret-key" +SESSION_KEY = "user-session-key" + + +@pytest.fixture(autouse=True) +def _fresh_vscode_service(monkeypatch: pytest.MonkeyPatch) -> None: + """/api/init touches the VSCode service singleton; give each test its own.""" + monkeypatch.setattr("openhands.agent_server.vscode_service._vscode_service", None) + + +@pytest.fixture +def dormant(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[TestClient]: + """A deferred-init server that booted with the pool's key and directory.""" + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "boot" / "persistence")) + config = Config( + deferred_init=True, + secret_key=SecretStr(BOOT_KEY), + conversations_path=tmp_path / "boot" / "conversations", + bash_events_dir=tmp_path / "boot" / "bash", + enable_vscode=False, + preload_tools=False, + ) + with TestClient(create_app(config)) as client: + yield client + + +def _app(client: TestClient) -> FastAPI: + app = client.app + assert isinstance(app, FastAPI) + return app + + +def _init(client: TestClient, tmp_path: Path): + """Deliver the user's key, session key and directories through /api/init.""" + return client.post( + "/api/init", + headers={"X-Init-API-Key": BOOT_KEY}, + json={ + "secret_key": USER_KEY, + "session_api_keys": [SESSION_KEY], + "conversations_path": str(tmp_path / "user" / "conversations"), + "bash_events_dir": str(tmp_path / "user" / "bash"), + "env": {"OH_PERSISTENCE_DIR": str(tmp_path / "user" / "persistence")}, + }, + ) + + +def test_init_swaps_config_stores_and_services_together( + dormant: TestClient, tmp_path: Path +) -> None: + app = _app(dormant) + before = server_context_of(app) + assert before.conversation_service is None + assert before.bash_event_service is None + + assert _init(dormant, tmp_path).status_code == 200 + + after = server_context_of(app) + assert after is not before + # Everything in the new context was built from the delivered config ... + assert after.config.deferred_init is False + assert after.config.session_api_keys == [SESSION_KEY] + assert after.stores is not before.stores + assert after.stores.cipher is after.config.cipher + assert after.stores.profile_dir == tmp_path / "user" / "persistence" + assert after.conversation_registry is not before.conversation_registry + assert after.conversation_registry.config is after.config + assert after.conversation_registry.stores is after.stores + assert after.conversation_service is not None + assert after.conversation_service.stores is after.stores + assert after.conversation_service.cipher is after.config.cipher + assert after.bash_event_service is not None + assert after.bash_event_service.bash_events_dir == tmp_path / "user" / "bash" + # ... and the dormant one was replaced, not edited. + assert before.config.deferred_init is True + assert before.conversation_service is None + assert before.stores.profile_dir == tmp_path / "boot" / "persistence" + + +def test_requests_after_init_use_the_delivered_key_and_directory( + dormant: TestClient, tmp_path: Path +) -> None: + """No reset step: the next request already runs on the new stores.""" + assert _init(dormant, tmp_path).status_code == 200 + + saved = dormant.put( + "/api/settings/secrets", + headers={"X-Session-API-Key": SESSION_KEY}, + json={"name": "API_TOKEN", "value": "token-value"}, + ) + assert saved.status_code == 200 + + secrets_file = tmp_path / "user" / "persistence" / "secrets.json" + stored = json.loads(secrets_file.read_text())["custom_secrets"]["API_TOKEN"] + assert Cipher(USER_KEY).try_decrypt_str(stored["secret"]) == "token-value" + assert Cipher(BOOT_KEY).try_decrypt_str(stored["secret"]) is None + assert not (tmp_path / "boot" / "persistence" / "secrets.json").exists() + + +def test_a_failed_init_leaves_the_dormant_context_in_place( + dormant: TestClient, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + app = _app(dormant) + before = server_context_of(app) + started: list[ConversationService] = [] + real_get_instance = ConversationService.get_instance + + def recording_get_instance(config, stores): + service = real_get_instance(config, stores) + started.append(service) + return service + + class _FailingVSCode: + async def set_connection_token(self, _token: str) -> None: + raise RuntimeError("vscode refused the token") + + monkeypatch.setattr( + init_router_module.ConversationService, "get_instance", recording_get_instance + ) + # Fails after the new stores, registry and services are all up. + monkeypatch.setattr( + init_router_module, "get_vscode_service", lambda _config: _FailingVSCode() + ) + + failed = _init(dormant, tmp_path) + + assert failed.status_code == 500 + assert dormant.get("/api/init").json()["state"] == "dormant" + # The very same context object, with nothing of the attempt in it ... + assert server_context_of(app) is before + assert before.conversation_service is None + assert before.config.deferred_init is True + # ... and the service the attempt started was stopped, not left running. + (attempted,) = started + assert attempted._event_services is None + # The dormant key still guards /api/init, so the orchestrator can retry. + assert dormant.post("/api/init", json={}).status_code == 401 + + monkeypatch.setattr(init_router_module, "get_vscode_service", lambda _config: None) + assert _init(dormant, tmp_path).status_code == 200 + retried = server_context_of(app) + assert retried is not before + assert retried.conversation_service is started[-1] + + +def test_the_lifespan_adds_services_to_the_context_it_was_created_with( + tmp_path: Path, +) -> None: + """A normal boot keeps its config, stores and registry; only services come.""" + config = Config( + session_api_keys=[], + conversations_path=tmp_path / "conversations", + bash_events_dir=tmp_path / "bash", + enable_vscode=False, + preload_tools=False, + ) + app = create_app(config) + created = server_context_of(app) + assert created.conversation_service is None + + with TestClient(app): + running = server_context_of(app) + assert running.config is created.config + assert running.stores is created.stores + assert running.conversation_registry is created.conversation_registry + assert running.conversation_service is not None + assert running.conversation_service.stores is created.stores + assert running.bash_event_service is not None + # Built from the config given to create_app, not from the environment. + assert running.bash_event_service.bash_events_dir == tmp_path / "bash" + + +def test_an_app_without_a_context_says_so() -> None: + with pytest.raises(RuntimeError, match="no ServerContext"): + server_context_of(FastAPI()) + + +# ── Guard: the context is the only way in ─────────────────────────────────── + +# What a server derives from its config. Only ``server_context.py`` may keep +# any of it on ``app.state``; everything else asks the context. +_CONTEXT_STATE = frozenset( + { + "server_context", + "config", + "conversation_service", + "bash_event_service", + "conversation_registry", + } +) +_CONTEXT_MODULE = "server_context.py" +_PACKAGE_DIR = Path(openhands.agent_server.__file__).parent + + +def _is_app_state(node: ast.expr) -> bool: + """``.state`` -- an app's (or a stand-in's) state bag.""" + return isinstance(node, ast.Attribute) and node.attr == "state" + + +def context_violations(source: str, filename: str) -> list[str]: + """Ways ``source`` reaches server context state without going through it.""" + found: list[str] = [] + for node in ast.walk(ast.parse(source, filename=filename)): + if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef): + if node.name.startswith("get_default_"): + found.append(f"{filename}:{node.lineno}: defines {node.name}()") + elif isinstance(node, ast.Attribute): + if node.attr in _CONTEXT_STATE and _is_app_state(node.value): + found.append(f"{filename}:{node.lineno}: reads .state.{node.attr}") + elif isinstance(node, ast.Call): + # getattr/setattr/hasattr(.state, "config", ...) + if ( + isinstance(node.func, ast.Name) + and node.func.id in {"getattr", "setattr", "hasattr", "delattr"} + and len(node.args) >= 2 + and _is_app_state(node.args[0]) + and isinstance(node.args[1], ast.Constant) + and node.args[1].value in _CONTEXT_STATE + ): + found.append( + f"{filename}:{node.lineno}: {node.func.id}(.state, " + f"{node.args[1].value!r})" + ) + return found + + +@pytest.mark.parametrize( + "source", + [ + "config = request.app.state.config", + "service = websocket.app.state.conversation_service", + "app.state.bash_event_service = service", + "registry = scope['app'].state.conversation_registry", + "context = api.state.server_context", + "config = getattr(request.app.state, 'config', None)", + "def get_default_config():\n return load_config()", + "async def get_default_conversation_service():\n ...", + ], +) +def test_the_guard_recognises_a_way_around_the_context(source: str) -> None: + assert context_violations(source, "example.py") + + +@pytest.mark.parametrize( + "source", + [ + "config = get_config(request)", + "stores = get_server_context(request).stores", + "manager = request.app.state.canvas_extension_backend_manager", + "event_service = getattr(request.state, 'runtime_event_service', None)", + "status = conversation.state.execution_status", + "def _get_default_parsers():\n return {}", + ], +) +def test_the_guard_accepts_the_context_and_unrelated_state(source: str) -> None: + assert context_violations(source, "example.py") == [] + + +def test_only_the_server_context_module_touches_context_state() -> None: + """No module reads config-derived ``app.state`` or defines ``get_default_*``. + + A reader of ``app.state.config`` (or of a service kept there) sees whatever + was assigned last, which during ``POST /api/init`` can be half of the old + setup and half of the new one; a ``get_default_*`` function builds a second + copy from the environment. Take the config, a store or a service from the + request's ``ServerContext`` (``openhands.agent_server.dependencies``), or + have it passed in by the code that built the context. + """ + violations: list[str] = [] + modules = sorted(_PACKAGE_DIR.rglob("*.py")) + assert len(modules) > 50, "the agent-server package was not found" + for path in modules: + relative = path.relative_to(_PACKAGE_DIR).as_posix() + found = context_violations(path.read_text(), relative) + if relative == _CONTEXT_MODULE: + # The one place the context itself is kept and read. + found = [v for v in found if not v.endswith(".state.server_context")] + violations.extend(found) + + assert violations == [], "\n".join(violations) + + +def test_the_context_module_keeps_the_context_on_app_state() -> None: + """The guard's exception is real: that module is where the context lives.""" + source = (_PACKAGE_DIR / _CONTEXT_MODULE).read_text() + kept = context_violations(source, _CONTEXT_MODULE) + assert kept and all(v.endswith(".state.server_context") for v in kept) + + +def test_a_context_cannot_be_edited_in_place(tmp_path: Path) -> None: + """Changing a server means installing another context, never mutating one.""" + context = server_context_of( + create_app(Config(conversations_path=tmp_path / "conversations")) + ) + assert isinstance(context, ServerContext) + with pytest.raises(dataclasses.FrozenInstanceError): + setattr(context, "config", Config()) diff --git a/tests/agent_server/test_settings_router.py b/tests/agent_server/test_settings_router.py index 7bdbfce8417..5bc8af7cc23 100644 --- a/tests/agent_server/test_settings_router.py +++ b/tests/agent_server/test_settings_router.py @@ -16,9 +16,7 @@ PERSISTED_SETTINGS_SCHEMA_VERSION, FileSettingsStore, PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - reset_stores, + ServerStores, ) from openhands.agent_server.persistence.models import _deep_merge from openhands.sdk.llm import LLM @@ -32,6 +30,7 @@ ) from openhands.sdk.utils.cipher import Cipher from openhands.sdk.utils.pydantic_secrets import REDACTED_SECRET_VALUE +from tests.agent_server.conftest import client_context @pytest.fixture @@ -39,13 +38,11 @@ def temp_persistence_dir(): """Create a temporary directory for persistence files and reset stores.""" with tempfile.TemporaryDirectory() as tmpdir: # Reset global store singletons before test - reset_stores() # Set environment variable for persistence directory old_val = os.environ.get("OH_PERSISTENCE_DIR") os.environ["OH_PERSISTENCE_DIR"] = tmpdir yield Path(tmpdir) # Cleanup: reset stores and restore environment - reset_stores() if old_val is not None: os.environ["OH_PERSISTENCE_DIR"] = old_val else: @@ -643,7 +640,9 @@ def test_patch_settings_updates_llm_config(client_with_settings): def test_patch_settings_updates_active_profile(client_with_settings): """PATCH /api/settings can update and clear the active LLM profile.""" - get_llm_profile_store().save("fast-profile", LLM(model="gpt-4o-mini")) + ServerStores.from_config(Config()).llm_profiles.save( + "fast-profile", LLM(model="gpt-4o-mini") + ) response = client_with_settings.patch( "/api/settings", @@ -682,9 +681,11 @@ def test_patch_settings_rejects_invalid_active_profile(client_with_settings): def test_patch_settings_active_agent_profile_id_independent(client_with_settings): """active_agent_profile_id sets/clears independently of active_profile.""" - get_llm_profile_store().save("fast-profile", LLM(model="gpt-4o-mini")) + ServerStores.from_config(Config()).llm_profiles.save( + "fast-profile", LLM(model="gpt-4o-mini") + ) profile = OpenHandsAgentProfile(name="mine", llm_profile_ref="fast-profile") - get_agent_profile_store().save(profile) + ServerStores.from_config(Config()).agent_profiles.save(profile) agent_id = str(profile.id) set_response = client_with_settings.patch( @@ -713,7 +714,9 @@ def test_patch_settings_active_agent_profile_id_independent(client_with_settings def test_patch_settings_active_profile_applies_llm(client_with_settings): """PATCH active_profile applies that profile's LLM (#4314).""" - get_llm_profile_store().save("fast-profile", LLM(model="claude-haiku")) + ServerStores.from_config(Config()).llm_profiles.save( + "fast-profile", LLM(model="claude-haiku") + ) response = client_with_settings.patch( "/api/settings", @@ -735,7 +738,7 @@ def test_patch_settings_active_profile_applies_encrypted_api_key( ): """Applying a profile carries its at-rest-encrypted api_key through PATCH.""" cipher = Cipher(secret_key) - get_llm_profile_store().save( + ServerStores.from_config(Config()).llm_profiles.save( "secure-profile", LLM(model="claude-haiku", api_key=SecretStr("sk-secret")), include_secrets=True, @@ -762,7 +765,7 @@ def test_patch_settings_active_profile_leaves_acp_settings_without_llm( monkeypatch.setattr( "openhands.sdk.utils.deprecation._current_version", lambda: "1.51.0" ) - get_llm_profile_store().save( + ServerStores.from_config(Config()).llm_profiles.save( "fast-profile", LLM(model="claude-haiku", api_key=SecretStr("sk-secret")), include_secrets=True, @@ -787,7 +790,9 @@ def test_patch_settings_active_profile_leaves_acp_settings_without_llm( def test_patch_settings_active_profile_applies_llm_when_switching_to_openhands( client_with_settings, ): - get_llm_profile_store().save("fast-profile", LLM(model="claude-haiku")) + ServerStores.from_config(Config()).llm_profiles.save( + "fast-profile", LLM(model="claude-haiku") + ) client_with_settings.patch( "/api/settings", json={"agent_settings_diff": {"agent_kind": "acp"}} ) @@ -808,8 +813,12 @@ def test_patch_settings_active_profile_applies_llm_when_switching_to_openhands( def test_patch_settings_switching_active_profile_updates_llm(client_with_settings): """Switching active_profile re-applies the new profile's LLM.""" - get_llm_profile_store().save("profile-a", LLM(model="model-a")) - get_llm_profile_store().save("profile-b", LLM(model="model-b")) + ServerStores.from_config(Config()).llm_profiles.save( + "profile-a", LLM(model="model-a") + ) + ServerStores.from_config(Config()).llm_profiles.save( + "profile-b", LLM(model="model-b") + ) client_with_settings.patch("/api/settings", json={"active_profile": "profile-a"}) response = client_with_settings.patch( @@ -839,7 +848,9 @@ def test_patch_settings_explicit_llm_diff_overrides_profile_autoload( client_with_settings, ): """An explicit agent_settings_diff.llm overrides profile autoload.""" - get_llm_profile_store().save("fast-profile", LLM(model="claude-haiku")) + ServerStores.from_config(Config()).llm_profiles.save( + "fast-profile", LLM(model="claude-haiku") + ) response = client_with_settings.patch( "/api/settings", @@ -2065,7 +2076,7 @@ def test_list_secrets_busy_agent_profile_store_is_503( client_with_settings, monkeypatch ): """A lock timeout on the agent-profile store answers 503, not 500.""" - store = get_agent_profile_store() + store = client_context(client_with_settings).stores.agent_profiles def _timeout(*_args, **_kwargs): raise TimeoutError("Agent profile store lock acquisition timed out") diff --git a/tests/agent_server/test_sockets_service_getters.py b/tests/agent_server/test_sockets_service_getters.py index 9a0e255b8c6..52a9c87aa08 100644 --- a/tests/agent_server/test_sockets_service_getters.py +++ b/tests/agent_server/test_sockets_service_getters.py @@ -1,12 +1,9 @@ -"""Tests for the per-app service getters in sockets.py. - -The websocket handlers must read ``ConversationService`` and -``BashEventService`` from ``app.state`` (set by the lifespan or by -``POST /api/init`` in deferred-init mode) rather than the module-level -singletons captured at import time. These helpers encapsulate that lookup -and gracefully fall back to the module-level default when ``app.state`` -is not configured (e.g. when sockets.py is imported as a library without -a lifespan). +"""The websocket handlers take their services from the server context. + +``ConversationService`` and ``BashEventService`` belong to the context of the +app a socket is connected to (installed by the lifespan, or by +``POST /api/init`` in deferred-init mode). There is no process-wide service to +fall back to: a context whose services have not started refuses the socket. """ from types import SimpleNamespace @@ -14,25 +11,41 @@ from uuid import uuid4 import pytest -from fastapi import WebSocketDisconnect +from fastapi import HTTPException, WebSocketDisconnect, WebSocketException, status import openhands.agent_server.sockets as sockets_mod from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService -from openhands.agent_server.event_service import EventService -from openhands.agent_server.sockets import ( - _get_bash_event_service, - _get_conversation_service, +from openhands.agent_server.dependencies import ( + get_conversation_service, + get_server_bash_event_service, ) +from openhands.agent_server.event_service import EventService +from openhands.agent_server.server_context import ServerContext, build_server_context + + +def _context( + config: Config | None = None, + *, + conversation_service: ConversationService | None = None, + bash_event_service: BashEventService | None = None, +) -> ServerContext: + base = build_server_context(config or Config(session_api_keys=[])) + return ServerContext( + config=base.config, + stores=base.stores, + conversation_registry=base.conversation_registry, + conversation_service=conversation_service, + bash_event_service=bash_event_service, + ) -def _make_ws(state: dict[str, object] | None = None) -> MagicMock: - """Build a mock WebSocket whose ``app.state`` exposes ``state``.""" +def _make_ws(context: ServerContext, scope_type: str = "websocket") -> MagicMock: + """Build a mock connection whose app runs with ``context``.""" ws = MagicMock() - ws.app.state = SimpleNamespace() - if state is not None: - ws.app.state = SimpleNamespace(**state) + ws.app.state = SimpleNamespace(server_context=context) + ws.scope = {"type": scope_type} return ws @@ -46,107 +59,65 @@ def bash_service(): return MagicMock(spec=BashEventService) -# -- _get_conversation_service -- +# -- get_conversation_service -- -def test_get_conversation_service_prefers_app_state(conv_service): - """When the app registers a service, that one is returned (not the - module-level singleton).""" - ws = _make_ws({"conversation_service": conv_service}) - assert _get_conversation_service(ws) is conv_service +def test_get_conversation_service_returns_the_context_service(conv_service): + ws = _make_ws(_context(conversation_service=conv_service)) + assert get_conversation_service(ws) is conv_service -def test_get_conversation_service_falls_back_to_module_singleton(): - """When ``app.state`` has no conversation service, the module-level - default is returned (preserves the behaviour of tests that patch the - module-level singleton).""" - ws = _make_ws() - assert _get_conversation_service(ws) is sockets_mod.conversation_service +def test_get_conversation_service_refuses_a_socket_without_a_started_service(): + """A dormant context has no service, and nothing stands in for it.""" + ws = _make_ws(_context()) + with pytest.raises(WebSocketException) as excinfo: + get_conversation_service(ws) + assert excinfo.value.code == status.WS_1013_TRY_AGAIN_LATER -def test_get_conversation_service_ignores_wrong_type(): - """If ``app.state.conversation_service`` is set to something that is not - a ConversationService (e.g. None or some other object), fall back to the - module-level default rather than blow up.""" - for bogus in (None, "not a service", 42): - ws = _make_ws({"conversation_service": bogus}) - # Must not raise — just return the module-level default. - result = _get_conversation_service(ws) - assert isinstance(result, ConversationService) +def test_get_conversation_service_answers_503_to_a_request_without_a_service(): + request = _make_ws(_context(), scope_type="http") + with pytest.raises(HTTPException) as excinfo: + get_conversation_service(request) + assert excinfo.value.status_code == status.HTTP_503_SERVICE_UNAVAILABLE -# -- _get_bash_event_service -- +# -- get_server_bash_event_service -- -def test_get_bash_event_service_prefers_app_state(bash_service): - ws = _make_ws({"bash_event_service": bash_service}) - assert _get_bash_event_service(ws) is bash_service +def test_get_bash_event_service_returns_the_context_service(bash_service): + ws = _make_ws(_context(bash_event_service=bash_service)) + assert get_server_bash_event_service(ws) is bash_service -def test_get_bash_event_service_falls_back_to_module_singleton(): - ws = _make_ws() - assert _get_bash_event_service(ws) is sockets_mod.bash_event_service +def test_get_bash_event_service_refuses_a_socket_without_a_started_service(): + ws = _make_ws(_context()) + with pytest.raises(WebSocketException) as excinfo: + get_server_bash_event_service(ws) + assert excinfo.value.code == status.WS_1013_TRY_AGAIN_LATER -def test_get_bash_event_service_ignores_wrong_type(): - for bogus in (None, "not a service", 42): - ws = _make_ws({"bash_event_service": bogus}) - result = _get_bash_event_service(ws) - assert isinstance(result, BashEventService) +def test_get_bash_event_service_answers_503_to_a_request_without_a_service(): + request = _make_ws(_context(), scope_type="http") + with pytest.raises(HTTPException) as excinfo: + get_server_bash_event_service(request) + assert excinfo.value.status_code == status.HTTP_503_SERVICE_UNAVAILABLE -# -- Integration with Config -- - - -def test_get_conversation_service_handles_real_config(tmp_path): - """Sanity check: the helper returns a real ConversationService built - from a Config on ``app.state`` rather than the import-time default.""" - from openhands.agent_server import conversation_service as cs_mod - - original_default = sockets_mod.conversation_service - cs_mod._conversation_service = None - try: - new_service = ConversationService( - conversations_dir=tmp_path / "convs", - ) - cfg = Config( - conversations_path=tmp_path / "convs", - bash_events_dir=tmp_path / "bash", - ) - ws = _make_ws({"conversation_service": new_service, "config": cfg}) - assert _get_conversation_service(ws) is new_service - assert _get_conversation_service(ws) is not original_default - finally: - cs_mod._conversation_service = None - - -def test_get_bash_event_service_handles_real_config(tmp_path): - """Sanity check: the helper returns a real BashEventService built - from a Config on ``app.state`` rather than the import-time default.""" - from openhands.agent_server import bash_service as bash_mod - - original_default = sockets_mod.bash_event_service - bash_mod._bash_event_service = None - try: - new_service = BashEventService(bash_events_dir=tmp_path / "user" / "bash") - cfg = Config( - conversations_path=tmp_path / "convs", - bash_events_dir=tmp_path / "user" / "bash", - ) - ws = _make_ws({"bash_event_service": new_service, "config": cfg}) - assert _get_bash_event_service(ws) is new_service - assert _get_bash_event_service(ws) is not original_default - finally: - bash_mod._bash_event_service = None +def test_an_app_without_a_context_is_a_programming_error(): + """Nothing is built behind the caller's back when no context is installed.""" + ws = MagicMock() + ws.app.state = SimpleNamespace() + with pytest.raises(RuntimeError, match="no ServerContext"): + get_conversation_service(ws) -# -- End-to-end: WebSocket handlers use per-app services after /api/init -- +# -- End-to-end: WebSocket handlers use the context's services -- @pytest.mark.asyncio -async def test_events_socket_uses_app_state_conversation_service(): - """events_socket must use app.state.conversation_service (set by /api/init) - rather than the module-level singleton captured at import time.""" +async def test_events_socket_uses_the_context_conversation_service(): + """events_socket must use the service of the context (set by /api/init).""" from openhands.agent_server.sockets import events_socket mock_event_svc = MagicMock(spec=EventService) @@ -156,18 +127,13 @@ async def test_events_socket_uses_app_state_conversation_service(): per_app_conv_svc = MagicMock(spec=ConversationService) per_app_conv_svc.get_event_service = AsyncMock(return_value=mock_event_svc) - ws = MagicMock() + ws = _make_ws(_context(conversation_service=per_app_conv_svc)) ws.accept = AsyncMock() ws.receive_text = AsyncMock() ws.receive_json = AsyncMock(side_effect=WebSocketDisconnect()) ws.send_json = AsyncMock() ws.close = AsyncMock() ws.headers = {} - # Simulate app.state after /api/init delivers a per-user service. - ws.app.state = SimpleNamespace( - conversation_service=per_app_conv_svc, - config=Config(session_api_keys=[]), - ) await events_socket(uuid4(), ws, session_api_key=None) @@ -177,27 +143,21 @@ async def test_events_socket_uses_app_state_conversation_service(): @pytest.mark.asyncio -async def test_bash_events_socket_uses_app_state_bash_event_service(): - """bash_events_socket must use app.state.bash_event_service (set by /api/init) - rather than the module-level singleton captured at import time.""" +async def test_bash_events_socket_uses_the_context_bash_event_service(): + """bash_events_socket must use the service of the context (set by /api/init).""" from openhands.agent_server.sockets import bash_events_socket per_app_bash_svc = MagicMock(spec=BashEventService) per_app_bash_svc.subscribe_to_events = AsyncMock(return_value=uuid4()) per_app_bash_svc.unsubscribe_from_events = AsyncMock(return_value=True) - ws = MagicMock() + ws = _make_ws(_context(bash_event_service=per_app_bash_svc)) ws.accept = AsyncMock() ws.receive_text = AsyncMock() ws.receive_json = AsyncMock(side_effect=WebSocketDisconnect()) ws.send_json = AsyncMock() ws.close = AsyncMock() ws.headers = {} - # Simulate app.state after /api/init delivers a per-user service. - ws.app.state = SimpleNamespace( - bash_event_service=per_app_bash_svc, - config=Config(session_api_keys=[]), - ) await bash_events_socket(ws, session_api_key=None) @@ -215,12 +175,7 @@ async def test_events_socket_preserves_diagnostics_without_extra_stack( event_service.unsubscribe_from_events = AsyncMock() service = MagicMock(spec=ConversationService) service.get_event_service = AsyncMock(return_value=event_service) - ws = _make_ws( - { - "conversation_service": service, - "config": Config(session_api_keys=[]), - } - ) + ws = _make_ws(_context(conversation_service=service)) ws.headers = {} ws.accept = AsyncMock() ws.receive_json = AsyncMock( diff --git a/tests/agent_server/test_store_cipher_mismatch.py b/tests/agent_server/test_store_cipher_mismatch.py index daaa937306a..7b89ad028b2 100644 --- a/tests/agent_server/test_store_cipher_mismatch.py +++ b/tests/agent_server/test_store_cipher_mismatch.py @@ -18,8 +18,8 @@ FileSecretsStore, FileSettingsStore, PersistedSettings, + ServerStores, reencrypt_stale_stores, - reset_stores, ) from openhands.agent_server.persistence.models import SettingsUpdatePayload from openhands.sdk.llm import LLM @@ -36,11 +36,9 @@ @pytest.fixture def persistence_dir(): with tempfile.TemporaryDirectory() as tmpdir: - reset_stores() old_val = os.environ.get("OH_PERSISTENCE_DIR") os.environ["OH_PERSISTENCE_DIR"] = tmpdir yield Path(tmpdir) - reset_stores() if old_val is not None: os.environ["OH_PERSISTENCE_DIR"] = old_val else: @@ -244,7 +242,7 @@ def test_reencrypt_stale_stores_at_startup(persistence_dir): secret_key=SecretStr(KEY_B), previous_secret_keys=[SecretStr(KEY_A)], ) - reencrypt_stale_stores(config) + reencrypt_stale_stores(ServerStores.from_config(config)) _assert_readable_with(persistence_dir, Cipher(KEY_B)) @@ -270,7 +268,6 @@ def test_api_writes_succeed_after_rotation_with_previous_key(persistence_dir): assert delete.status_code == 200 # The writes re-encrypted both files: the new key alone now reads them. - reset_stores() client = TestClient( create_app( Config( diff --git a/tests/agent_server/test_tool_router.py b/tests/agent_server/test_tool_router.py index bff19a80d7f..56e381e8ce7 100644 --- a/tests/agent_server/test_tool_router.py +++ b/tests/agent_server/test_tool_router.py @@ -2,10 +2,12 @@ import importlib +from openhands.agent_server.config import Config from openhands.sdk.subagent.registry import ( _reset_registry_for_tests, get_agent_factory, ) +from tests.agent_server.conftest import install_test_context def test_builtin_agents_registered_on_tool_router_import(): @@ -36,7 +38,7 @@ def test_catalog_offers_the_stock_tools_a_profile_may_pick(): from openhands.agent_server.tool_router import tool_router app = FastAPI() - app.state.config = Config() + install_test_context(app, Config()) app.include_router(tool_router, prefix="/api") response = TestClient(app).get("/api/tools/catalog") @@ -81,8 +83,6 @@ def test_catalog_offers_the_stock_tools_a_profile_may_pick(): def test_docker_runtime_catalog_does_not_report_host_usability(monkeypatch): - from types import SimpleNamespace - from fastapi import FastAPI from fastapi.testclient import TestClient @@ -92,10 +92,13 @@ def test_docker_runtime_catalog_does_not_report_host_usability(monkeypatch): "openhands.sdk.tool.registry._check_tool_usable", lambda name, checker: False ) app = FastAPI() - app.state.config = SimpleNamespace( - conversation_runtime="docker", - conversation_image_has_browser=True, - enable_browser=True, + install_test_context( + app, + Config( + conversation_runtime="docker", + conversation_image_has_browser=True, + enable_browser=True, + ), ) app.include_router(tool_router, prefix="/api") @@ -105,18 +108,19 @@ def test_docker_runtime_catalog_does_not_report_host_usability(monkeypatch): def test_docker_catalog_follows_the_image_browser_setting(monkeypatch): - from types import SimpleNamespace - from fastapi import FastAPI from fastapi.testclient import TestClient from openhands.agent_server.tool_router import tool_router app = FastAPI() - app.state.config = SimpleNamespace( - conversation_runtime="docker", - conversation_image_has_browser=False, - enable_browser=True, + install_test_context( + app, + Config( + conversation_runtime="docker", + conversation_image_has_browser=False, + enable_browser=True, + ), ) app.include_router(tool_router, prefix="/api") @@ -128,18 +132,19 @@ def test_docker_catalog_follows_the_image_browser_setting(monkeypatch): def test_catalog_reports_a_disabled_browser_as_unusable(): - from types import SimpleNamespace - from fastapi import FastAPI from fastapi.testclient import TestClient from openhands.agent_server.tool_router import tool_router app = FastAPI() - app.state.config = SimpleNamespace( - conversation_runtime="local", - conversation_image_has_browser=True, - enable_browser=False, + install_test_context( + app, + Config( + conversation_runtime="local", + conversation_image_has_browser=True, + enable_browser=False, + ), ) app.include_router(tool_router, prefix="/api") diff --git a/tests/agent_server/test_trace_shutdown.py b/tests/agent_server/test_trace_shutdown.py index 6f0c81ddd2b..8410dad9327 100644 --- a/tests/agent_server/test_trace_shutdown.py +++ b/tests/agent_server/test_trace_shutdown.py @@ -25,9 +25,11 @@ def flush(timeout_millis): monkeypatch.setattr(provider, "force_flush", flush) monkeypatch.setattr("opentelemetry.trace.get_tracer_provider", lambda: provider) - monkeypatch.setattr("openhands.agent_server.api.get_vscode_service", lambda: None) monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda: None + "openhands.agent_server.api.get_vscode_service", lambda _config: None + ) + monkeypatch.setattr( + "openhands.agent_server.api.get_tool_preload_service", lambda _config: None ) app = create_app( Config( @@ -59,9 +61,11 @@ async def test_no_flush_when_tracing_is_not_configured(monkeypatch, tmp_path): monkeypatch.setattr( "opentelemetry.trace.get_tracer_provider", lambda: ProxyTracerProvider() ) - monkeypatch.setattr("openhands.agent_server.api.get_vscode_service", lambda: None) monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda: None + "openhands.agent_server.api.get_vscode_service", lambda _config: None + ) + monkeypatch.setattr( + "openhands.agent_server.api.get_tool_preload_service", lambda _config: None ) flushed = [] monkeypatch.setattr( diff --git a/tests/agent_server/test_vscode_service.py b/tests/agent_server/test_vscode_service.py index 17390282041..38c5808b9b6 100644 --- a/tests/agent_server/test_vscode_service.py +++ b/tests/agent_server/test_vscode_service.py @@ -9,6 +9,7 @@ import pytest +from openhands.agent_server.config import Config from openhands.agent_server.vscode_service import ( VSCodeService, get_vscode_service, @@ -589,46 +590,27 @@ async def test_wait_for_startup_timeout(vscode_service): def test_get_vscode_service_enabled(tmp_path): """Test get_vscode_service returns VSCodeService when enabled.""" - with ( - patch("openhands.agent_server.config.get_default_config") as mock_config, - patch("openhands.agent_server.vscode_service._vscode_service", None), - ): - mock_config.return_value.enable_vscode = True - mock_config.return_value.vscode_port = 8001 - mock_config.return_value.vscode_base_path = None - mock_config.return_value.session_api_keys = [] - - service = get_vscode_service() + config = Config(enable_vscode=True, vscode_port=8001, session_api_keys=[]) + with patch("openhands.agent_server.vscode_service._vscode_service", None): + service = get_vscode_service(config) assert isinstance(service, VSCodeService) def test_get_vscode_service_disabled(): """Test get_vscode_service returns None when disabled.""" - with ( - patch("openhands.agent_server.config.get_default_config") as mock_config, - patch("openhands.agent_server.vscode_service._vscode_service", None), - ): - mock_config.return_value.enable_vscode = False - - service = get_vscode_service() + with patch("openhands.agent_server.vscode_service._vscode_service", None): + service = get_vscode_service(Config(enable_vscode=False)) assert service is None def test_get_vscode_service_singleton(): """Test get_vscode_service returns the same instance on multiple calls.""" - with ( - patch("openhands.agent_server.config.get_default_config") as mock_config, - patch("openhands.agent_server.vscode_service._vscode_service", None), - ): - mock_config.return_value.enable_vscode = True - mock_config.return_value.vscode_port = 8001 - mock_config.return_value.vscode_base_path = None - mock_config.return_value.session_api_keys = [] - - service1 = get_vscode_service() - service2 = get_vscode_service() + config = Config(enable_vscode=True, vscode_port=8001, session_api_keys=[]) + with patch("openhands.agent_server.vscode_service._vscode_service", None): + service1 = get_vscode_service(config) + service2 = get_vscode_service(config) assert service1 is service2 assert isinstance(service1, VSCodeService) @@ -636,16 +618,9 @@ def test_get_vscode_service_singleton(): def test_get_vscode_service_with_custom_port(): """Test get_vscode_service uses the configured port.""" - with ( - patch("openhands.agent_server.config.get_default_config") as mock_config, - patch("openhands.agent_server.vscode_service._vscode_service", None), - ): - mock_config.return_value.enable_vscode = True - mock_config.return_value.vscode_port = 9001 - mock_config.return_value.vscode_base_path = None - mock_config.return_value.session_api_keys = [] - - service = get_vscode_service() + config = Config(enable_vscode=True, vscode_port=9001, session_api_keys=[]) + with patch("openhands.agent_server.vscode_service._vscode_service", None): + service = get_vscode_service(config) assert isinstance(service, VSCodeService) assert service.port == 9001 @@ -653,16 +628,14 @@ def test_get_vscode_service_with_custom_port(): def test_get_vscode_service_with_base_path(): """Test get_vscode_service passes vscode_base_path from config.""" - with ( - patch("openhands.agent_server.config.get_default_config") as mock_config, - patch("openhands.agent_server.vscode_service._vscode_service", None), - ): - mock_config.return_value.enable_vscode = True - mock_config.return_value.vscode_port = 8001 - mock_config.return_value.vscode_base_path = "/runtime-123/vscode" - mock_config.return_value.session_api_keys = [] - - service = get_vscode_service() + config = Config( + enable_vscode=True, + vscode_port=8001, + vscode_base_path="/runtime-123/vscode", + session_api_keys=[], + ) + with patch("openhands.agent_server.vscode_service._vscode_service", None): + service = get_vscode_service(config) assert isinstance(service, VSCodeService) assert service.server_base_path == "/runtime-123/vscode" diff --git a/tests/agent_server/test_websocket_first_message_auth.py b/tests/agent_server/test_websocket_first_message_auth.py index fb87993461b..969a46e2de7 100644 --- a/tests/agent_server/test_websocket_first_message_auth.py +++ b/tests/agent_server/test_websocket_first_message_auth.py @@ -33,7 +33,7 @@ def _make_mock_websocket(*, headers=None): @pytest.mark.asyncio async def test_no_auth_configured_accepts_immediately(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = [] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -48,7 +48,7 @@ async def test_no_auth_configured_accepts_immediately(): @pytest.mark.asyncio async def test_legacy_query_param_valid_key(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket( ws, session_api_key="sk-oh-valid" @@ -62,7 +62,7 @@ async def test_legacy_query_param_valid_key(): @pytest.mark.asyncio async def test_legacy_query_param_invalid_key(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket( ws, session_api_key="sk-oh-wrong" @@ -80,7 +80,7 @@ async def test_legacy_query_param_takes_precedence_over_first_message(): ws.receive_text.return_value = json.dumps( {"type": "auth", "session_api_key": "sk-oh-different"} ) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket( ws, session_api_key="sk-oh-valid" @@ -98,7 +98,7 @@ async def test_legacy_query_param_takes_precedence_over_first_message(): @pytest.mark.asyncio async def test_legacy_header_valid_key(): ws = _make_mock_websocket(headers={"x-session-api-key": "sk-oh-valid"}) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -109,7 +109,7 @@ async def test_legacy_header_valid_key(): @pytest.mark.asyncio async def test_legacy_header_invalid_key(): ws = _make_mock_websocket(headers={"x-session-api-key": "sk-oh-wrong"}) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -126,7 +126,7 @@ async def test_first_message_auth_valid_key(): ws.receive_text.return_value = json.dumps( {"type": "auth", "session_api_key": "sk-oh-valid"} ) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -141,7 +141,7 @@ async def test_first_message_auth_invalid_key(): ws.receive_text.return_value = json.dumps( {"type": "auth", "session_api_key": "sk-oh-wrong"} ) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -156,7 +156,7 @@ async def test_first_message_auth_wrong_type_field(): ws.receive_text.return_value = json.dumps( {"type": "message", "session_api_key": "sk-oh-valid"} ) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -167,7 +167,7 @@ async def test_first_message_auth_wrong_type_field(): async def test_first_message_auth_missing_key_field(): ws = _make_mock_websocket() ws.receive_text.return_value = json.dumps({"type": "auth"}) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -177,7 +177,7 @@ async def test_first_message_auth_missing_key_field(): @pytest.mark.asyncio async def test_legacy_empty_key_rejected_even_if_listed(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid", ""] result = await _accept_authenticated_websocket(ws, session_api_key="") @@ -190,7 +190,7 @@ async def test_legacy_empty_key_rejected_even_if_listed(): async def test_first_message_auth_empty_key_rejected_even_if_listed(): ws = _make_mock_websocket() ws.receive_text.return_value = json.dumps({"type": "auth", "session_api_key": ""}) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid", ""] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -202,7 +202,7 @@ async def test_first_message_auth_empty_key_rejected_even_if_listed(): async def test_first_message_auth_malformed_json(): ws = _make_mock_websocket() ws.receive_text.return_value = "not json at all" - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -214,7 +214,7 @@ async def test_first_message_auth_malformed_json(): async def test_first_message_auth_client_disconnects(): ws = _make_mock_websocket() ws.receive_text.side_effect = WebSocketDisconnect() - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] result = await _accept_authenticated_websocket(ws, session_api_key=None) @@ -231,7 +231,7 @@ async def slow_receive(): ws.receive_text.side_effect = slow_receive with ( - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, patch( "openhands.agent_server.sockets._FIRST_MESSAGE_AUTH_TIMEOUT_SECONDS", 0.05 ), @@ -265,12 +265,14 @@ async def test_events_socket_first_message_auth_e2e(): with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = ["sk-oh-valid"] - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) await events_socket(uuid4(), ws, session_api_key=None) @@ -309,12 +311,14 @@ async def test_events_socket_ignores_redundant_auth_control_frame(): with ( patch( - "openhands.agent_server.sockets.conversation_service" + "openhands.agent_server.sockets.get_conversation_service" ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = ["sk-oh-valid"] - mock_conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + mock_conv_service.return_value.get_event_service = AsyncMock( + return_value=mock_event_service + ) # Authenticate via legacy query param so receive_text is never called. await events_socket(uuid4(), ws, session_api_key="sk-oh-valid") @@ -337,7 +341,7 @@ async def test_events_socket_first_message_auth_rejected(): {"type": "auth", "session_api_key": "sk-oh-wrong"} ) - with patch("openhands.agent_server.sockets.get_default_config") as mock_config: + with patch("openhands.agent_server.sockets.get_config") as mock_config: mock_config.return_value.session_api_keys = ["sk-oh-valid"] await events_socket(uuid4(), ws, session_api_key=None) @@ -390,10 +394,10 @@ async def test_bash_socket_ignores_redundant_auth_before_command( with ( patch( - "openhands.agent_server.sockets.bash_event_service", - mock_bash_service, + "openhands.agent_server.sockets.get_server_bash_event_service", + return_value=mock_bash_service, ), - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = session_api_keys await bash_events_socket( @@ -429,10 +433,10 @@ async def test_bash_socket_validation_error_log_omits_payload(caplog): with ( patch( - "openhands.agent_server.sockets.bash_event_service", - mock_bash_service, + "openhands.agent_server.sockets.get_server_bash_event_service", + return_value=mock_bash_service, ), - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = [] await bash_events_socket( @@ -476,10 +480,10 @@ async def test_bash_socket_error_send_failure_log_omits_exceptions(caplog): with ( patch( - "openhands.agent_server.sockets.bash_event_service", - mock_bash_service, + "openhands.agent_server.sockets.get_server_bash_event_service", + return_value=mock_bash_service, ), - patch("openhands.agent_server.sockets.get_default_config") as mock_config, + patch("openhands.agent_server.sockets.get_config") as mock_config, ): mock_config.return_value.session_api_keys = [] await bash_events_socket( diff --git a/tests/agent_server/test_workspaces_router.py b/tests/agent_server/test_workspaces_router.py index 24c754a8b4f..714556f50b0 100644 --- a/tests/agent_server/test_workspaces_router.py +++ b/tests/agent_server/test_workspaces_router.py @@ -16,18 +16,15 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config -from openhands.agent_server.persistence import reset_stores @pytest.fixture def client(monkeypatch): with tempfile.TemporaryDirectory() as tmpdir: - reset_stores() monkeypatch.setenv("OH_PERSISTENCE_DIR", str(Path(tmpdir) / "persist")) config = Config(static_files_path=None, session_api_keys=[], secret_key=None) app = create_app(config) yield TestClient(app) - reset_stores() @pytest.fixture @@ -37,7 +34,6 @@ def client_with_auth(monkeypatch): workspaces router inherits the same auth as the rest of ``/api``. """ with tempfile.TemporaryDirectory() as tmpdir: - reset_stores() monkeypatch.setenv("OH_PERSISTENCE_DIR", str(Path(tmpdir) / "persist")) config = Config( static_files_path=None, @@ -46,7 +42,6 @@ def client_with_auth(monkeypatch): ) app = create_app(config) yield TestClient(app, raise_server_exceptions=False) - reset_stores() def test_list_workspaces_empty_when_no_file(client): @@ -145,13 +140,15 @@ def test_workspaces_survive_across_requests_via_disk_persistence(client): json={"workspaces": [{"id": "/keep", "name": "keep", "path": "/keep"}]}, ) - # Act: confirm it's on disk, then reset the in-memory singleton (simulating - # a server restart) and re-read. + # Act: confirm it's on disk, then re-read through a new server on the same + # directory (a restart: its stores are built from scratch). persist_dir = Path(os.environ["OH_PERSISTENCE_DIR"]) assert (persist_dir / "workspaces.json").exists() - reset_stores() - listed_again = client.get("/api/workspaces").json() + restarted = TestClient( + create_app(Config(static_files_path=None, session_api_keys=[], secret_key=None)) + ) + listed_again = restarted.get("/api/workspaces").json() # Assert assert [w["path"] for w in listed_again["workspaces"]] == ["/keep"] @@ -292,8 +289,6 @@ def test_list_workspaces_returns_409_when_persisted_file_is_corrupted(client): ) persist_dir = Path(os.environ["OH_PERSISTENCE_DIR"]) (persist_dir / "workspaces.json").write_text("{not valid json", encoding="utf-8") - # Force the next request to re-read from disk rather than the in-memory store. - reset_stores() # Act response = client.get("/api/workspaces") diff --git a/tests/cross/conftest.py b/tests/cross/conftest.py index 20f213eaa9b..6c026b55f59 100644 --- a/tests/cross/conftest.py +++ b/tests/cross/conftest.py @@ -159,9 +159,8 @@ def live_server_env( # Build app after env is set from openhands.agent_server.api import create_app from openhands.agent_server.config import Config - from openhands.agent_server.persistence import reset_stores + from openhands.agent_server.server_context import server_context_of - reset_stores() cfg_obj = Config.model_validate_json(cfg_file.read_text()) app = create_app(cfg_obj) @@ -198,7 +197,7 @@ def live_server_env( yield { "app": app, "conversation_service": ( - None if deferred_init else app.state.conversation_service + None if deferred_init else server_context_of(app).conversation_service ), "host": f"http://127.0.0.1:{port}", "workspace_path": workspace_path, @@ -212,4 +211,3 @@ def live_server_env( cwd_conversations = Path("workspace/conversations") if cwd_conversations.exists(): shutil.rmtree(cwd_conversations) - reset_stores() diff --git a/tests/cross/test_remote_conversation_live_server.py b/tests/cross/test_remote_conversation_live_server.py index e3ebcfb8213..ec9519badc1 100644 --- a/tests/cross/test_remote_conversation_live_server.py +++ b/tests/cross/test_remote_conversation_live_server.py @@ -16,7 +16,6 @@ from datetime import datetime from pathlib import Path from typing import cast -from unittest.mock import patch from uuid import UUID import httpx @@ -1000,17 +999,13 @@ def test_remote_conversation_created_from_agent_settings(server_env): def test_openai_chat_completions_gateway_over_real_server( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, patched_llm ): - from openhands.agent_server import ( - config as config_module, - conversation_service as service_module, - ) from openhands.sdk.llm.llm_profile_store import LLMProfileStore - monkeypatch.setattr(config_module, "_default_config", None) - monkeypatch.setattr(service_module, "_conversation_service", None) monkeypatch.delenv("OH_WEBHOOKS_0_BASE_URL", raising=False) - profiles_dir = tmp_path / "profiles" + # Where the live server's own stores keep LLM profiles (live_server_env + # points OH_PERSISTENCE_DIR at ``tmp_path / ".openhands"``). + profiles_dir = tmp_path / ".openhands" / "profiles" store = LLMProfileStore(base_dir=profiles_dir) store.save( "smoke", @@ -1018,293 +1013,280 @@ def test_openai_chat_completions_gateway_over_real_server( include_secrets=True, ) - def llm_profiles() -> LLMProfileStore: - return LLMProfileStore(base_dir=profiles_dir) - - with ( - patch( - "openhands.agent_server.openai.service.get_llm_profile_store", llm_profiles - ), - patch("openhands.agent_server.launch.get_llm_profile_store", llm_profiles), - ): - with live_server_env(tmp_path, monkeypatch) as env: - with httpx.Client() as client: - models_response = client.get(f"{env['host']}/v1/models", timeout=2.0) - assert models_response.status_code == 200 - assert models_response.json()["data"] == [ - { - "id": "openhands_smoke", - "object": "model", - "created": 0, - "owned_by": "openhands", - } - ] - - response = client.post( - f"{env['host']}/v1/chat/completions", - json={ - "model": "openhands_smoke", - "messages": [ - {"role": "system", "content": "Answer briefly."}, - {"role": "user", "content": "Say hello."}, - ], - }, - timeout=10.0, - ) - assert response.status_code == 200 - body = response.json() - assert body["object"] == "chat.completion" - assert body["model"] == "openhands_smoke" - assert body["choices"][0]["message"] == { - "role": "assistant", - "content": "Hello from patched LLM", - } - assert body["usage"] == { - "prompt_tokens": 7, - "completion_tokens": 5, - "total_tokens": 12, + with live_server_env(tmp_path, monkeypatch) as env: + with httpx.Client() as client: + models_response = client.get(f"{env['host']}/v1/models", timeout=2.0) + assert models_response.status_code == 200 + assert models_response.json()["data"] == [ + { + "id": "openhands_smoke", + "object": "model", + "created": 0, + "owned_by": "openhands", } - conversation_id = response.headers["X-OpenHands-ServerConversation-ID"] - UUID(conversation_id) - persisted_response = client.get( - f"{env['host']}/api/conversations/{conversation_id}", timeout=2.0 - ) - assert persisted_response.status_code == 200 - assert persisted_response.json()["workspace"]["working_dir"] == str( - env["workspace_path"] - ) + ] - reused_response = client.post( - f"{env['host']}/v1/chat/completions", - headers={"X-OpenHands-ServerConversation-ID": conversation_id}, - json={ - "model": "openhands_smoke", - "messages": [ - {"role": "user", "content": "Say hello again."}, - ], - }, - timeout=10.0, - ) - assert reused_response.status_code == 200 - assert ( - reused_response.headers["X-OpenHands-ServerConversation-ID"] - == conversation_id - ) - assert reused_response.json()["choices"][0]["message"] == { - "role": "assistant", - "content": "Hello from patched LLM", - } - # A continued turn reports only its own model call, not the - # conversation's running total (7/5 per call, two calls so far). - assert reused_response.json()["usage"] == { - "prompt_tokens": 7, - "completion_tokens": 5, - "total_tokens": 12, - } + response = client.post( + f"{env['host']}/v1/chat/completions", + json={ + "model": "openhands_smoke", + "messages": [ + {"role": "system", "content": "Answer briefly."}, + {"role": "user", "content": "Say hello."}, + ], + }, + timeout=10.0, + ) + assert response.status_code == 200 + body = response.json() + assert body["object"] == "chat.completion" + assert body["model"] == "openhands_smoke" + assert body["choices"][0]["message"] == { + "role": "assistant", + "content": "Hello from patched LLM", + } + assert body["usage"] == { + "prompt_tokens": 7, + "completion_tokens": 5, + "total_tokens": 12, + } + conversation_id = response.headers["X-OpenHands-ServerConversation-ID"] + UUID(conversation_id) + persisted_response = client.get( + f"{env['host']}/api/conversations/{conversation_id}", timeout=2.0 + ) + assert persisted_response.status_code == 200 + assert persisted_response.json()["workspace"]["working_dir"] == str( + env["workspace_path"] + ) - from openai import BadRequestError, OpenAI + reused_response = client.post( + f"{env['host']}/v1/chat/completions", + headers={"X-OpenHands-ServerConversation-ID": conversation_id}, + json={ + "model": "openhands_smoke", + "messages": [ + {"role": "user", "content": "Say hello again."}, + ], + }, + timeout=10.0, + ) + assert reused_response.status_code == 200 + assert ( + reused_response.headers["X-OpenHands-ServerConversation-ID"] + == conversation_id + ) + assert reused_response.json()["choices"][0]["message"] == { + "role": "assistant", + "content": "Hello from patched LLM", + } + # A continued turn reports only its own model call, not the + # conversation's running total (7/5 per call, two calls so far). + assert reused_response.json()["usage"] == { + "prompt_tokens": 7, + "completion_tokens": 5, + "total_tokens": 12, + } - openai_client = OpenAI( - api_key="unused", - base_url=f"{env['host']}/v1", - timeout=10, - ) - reused_stream = openai_client.chat.completions.create( - model="openhands_smoke", - messages=[{"role": "user", "content": "Say hello a third time."}], - stream=True, - stream_options={"include_usage": True}, - extra_headers={ - "X-OpenHands-ServerConversation-ID": conversation_id + from openai import BadRequestError, OpenAI + + openai_client = OpenAI( + api_key="unused", + base_url=f"{env['host']}/v1", + timeout=10, + ) + reused_stream = openai_client.chat.completions.create( + model="openhands_smoke", + messages=[{"role": "user", "content": "Say hello a third time."}], + stream=True, + stream_options={"include_usage": True}, + extra_headers={"X-OpenHands-ServerConversation-ID": conversation_id}, + ) + reused_usage = [chunk.usage for chunk in reused_stream if chunk.usage] + assert len(reused_usage) == 1 + assert reused_usage[0].prompt_tokens == 7 + assert reused_usage[0].completion_tokens == 5 + assert reused_usage[0].total_tokens == 12 + + stream = openai_client.chat.completions.create( + model="openhands_smoke", + messages=[ + {"role": "developer", "content": "Answer tersely."}, + {"role": "user", "content": "Say hello as a stream."}, + ], + stream=True, + stream_options={"include_usage": True}, + user="compat-test-user", + ) + chunks = list(stream) + streamed_text = "".join( + chunk.choices[0].delta.content or "" + for chunk in chunks + if chunk.choices + ) + usage_chunks = [chunk.usage for chunk in chunks if chunk.usage] + assert streamed_text == "Hello from patched LLM" + assert usage_chunks[-1].prompt_tokens == 7 + assert usage_chunks[-1].completion_tokens == 5 + assert usage_chunks[-1].total_tokens == 12 + + stream = openai_client.chat.completions.create( + model="openhands_smoke", + messages=[ + { + "role": "user", + "content": "Say hello as a default stream.", }, - ) - reused_usage = [chunk.usage for chunk in reused_stream if chunk.usage] - assert len(reused_usage) == 1 - assert reused_usage[0].prompt_tokens == 7 - assert reused_usage[0].completion_tokens == 5 - assert reused_usage[0].total_tokens == 12 + ], + stream=True, + ) + chunks = list(stream) + streamed_text = "".join( + chunk.choices[0].delta.content or "" + for chunk in chunks + if chunk.choices + ) + usage_chunks = [chunk.usage for chunk in chunks if chunk.usage] + assert streamed_text == "Hello from patched LLM" + assert usage_chunks == [] + + raw_response = openai_client.responses.with_raw_response.create( + model="openhands_smoke", + instructions="Answer briefly.", + input="Say hello through Responses.", + store=False, + ) + responses_result = raw_response.parse() + assert responses_result.object == "response" + assert responses_result.status == "completed" + assert responses_result.model == "openhands_smoke" + assert responses_result.output_text == "Hello from patched LLM" + assert responses_result.previous_response_id is None + assert responses_result.usage is not None + assert responses_result.usage.input_tokens == 7 + assert responses_result.usage.output_tokens == 5 + assert responses_result.usage.total_tokens == 12 + assert responses_result.id.startswith("resp_") + UUID(hex=responses_result.id.removeprefix("resp_")) + response_system_text = "\n".join( + part.text + for message in patched_llm[-1] + if message.role == "system" + for part in message.content + if isinstance(part, TextContent) + ) + response_user_text = "\n".join( + part.text + for message in patched_llm[-1] + if message.role == "user" + for part in message.content + if isinstance(part, TextContent) + ) + assert "Answer briefly." in response_system_text + assert "Answer briefly." not in response_user_text + assert "Say hello through Responses." in response_user_text + + responses_conversation_id = raw_response.headers[ + "X-OpenHands-ServerConversation-ID" + ] + UUID(responses_conversation_id) - stream = openai_client.chat.completions.create( + llm_calls_before_rejected_continuation = len(patched_llm) + with pytest.raises(BadRequestError) as exc_info: + openai_client.responses.create( model="openhands_smoke", - messages=[ - {"role": "developer", "content": "Answer tersely."}, - {"role": "user", "content": "Say hello as a stream."}, - ], - stream=True, - stream_options={"include_usage": True}, - user="compat-test-user", - ) - chunks = list(stream) - streamed_text = "".join( - chunk.choices[0].delta.content or "" - for chunk in chunks - if chunk.choices + input="This must not continue server-side.", + previous_response_id=responses_result.id, + store=False, ) - usage_chunks = [chunk.usage for chunk in chunks if chunk.usage] - assert streamed_text == "Hello from patched LLM" - assert usage_chunks[-1].prompt_tokens == 7 - assert usage_chunks[-1].completion_tokens == 5 - assert usage_chunks[-1].total_tokens == 12 + assert exc_info.value.status_code == 400 + assert exc_info.value.response.json()["detail"] == ( + "previous_response_id is not supported; replay input items instead" + ) + assert len(patched_llm) == llm_calls_before_rejected_continuation - stream = openai_client.chat.completions.create( + second_stateless_response = ( + openai_client.responses.with_raw_response.create( model="openhands_smoke", - messages=[ + input=[ + { + "role": "developer", + "content": "Use replayed context.", + }, + { + "role": "user", + "content": "Say hello through Responses.", + }, + *[ + cast( + ResponseInputItemParam, + item.model_dump(mode="json", exclude_none=True), + ) + for item in responses_result.output + ], { "role": "user", - "content": "Say hello as a default stream.", + "content": "Follow up using the prior answer.", }, ], - stream=True, - ) - chunks = list(stream) - streamed_text = "".join( - chunk.choices[0].delta.content or "" - for chunk in chunks - if chunk.choices - ) - usage_chunks = [chunk.usage for chunk in chunks if chunk.usage] - assert streamed_text == "Hello from patched LLM" - assert usage_chunks == [] - - raw_response = openai_client.responses.with_raw_response.create( - model="openhands_smoke", - instructions="Answer briefly.", - input="Say hello through Responses.", store=False, ) - responses_result = raw_response.parse() - assert responses_result.object == "response" - assert responses_result.status == "completed" - assert responses_result.model == "openhands_smoke" - assert responses_result.output_text == "Hello from patched LLM" - assert responses_result.previous_response_id is None - assert responses_result.usage is not None - assert responses_result.usage.input_tokens == 7 - assert responses_result.usage.output_tokens == 5 - assert responses_result.usage.total_tokens == 12 - assert responses_result.id.startswith("resp_") - UUID(hex=responses_result.id.removeprefix("resp_")) - response_system_text = "\n".join( - part.text - for message in patched_llm[-1] - if message.role == "system" - for part in message.content - if isinstance(part, TextContent) - ) - response_user_text = "\n".join( - part.text - for message in patched_llm[-1] - if message.role == "user" - for part in message.content - if isinstance(part, TextContent) - ) - assert "Answer briefly." in response_system_text - assert "Answer briefly." not in response_user_text - assert "Say hello through Responses." in response_user_text - - responses_conversation_id = raw_response.headers[ - "X-OpenHands-ServerConversation-ID" - ] - UUID(responses_conversation_id) - - llm_calls_before_rejected_continuation = len(patched_llm) - with pytest.raises(BadRequestError) as exc_info: - openai_client.responses.create( - model="openhands_smoke", - input="This must not continue server-side.", - previous_response_id=responses_result.id, - store=False, - ) - assert exc_info.value.status_code == 400 - assert exc_info.value.response.json()["detail"] == ( - "previous_response_id is not supported; replay input items instead" - ) - assert len(patched_llm) == llm_calls_before_rejected_continuation - - second_stateless_response = ( - openai_client.responses.with_raw_response.create( - model="openhands_smoke", - input=[ - { - "role": "developer", - "content": "Use replayed context.", - }, - { - "role": "user", - "content": "Say hello through Responses.", - }, - *[ - cast( - ResponseInputItemParam, - item.model_dump(mode="json", exclude_none=True), - ) - for item in responses_result.output - ], - { - "role": "user", - "content": "Follow up using the prior answer.", - }, - ], - store=False, - ) - ) - second_stateless_result = second_stateless_response.parse() - assert second_stateless_result.output_text == "Hello from patched LLM" - assert ( - second_stateless_response.headers[ - "X-OpenHands-ServerConversation-ID" - ] - != responses_conversation_id - ) - stateless_system_text = "\n".join( - part.text - for message in patched_llm[-1] - if message.role == "system" - for part in message.content - if isinstance(part, TextContent) - ) - stateless_history_text = "\n".join( - part.text - for message in patched_llm[-1] - if message.role == "user" - for part in message.content - if isinstance(part, TextContent) - ) - assert "Use replayed context." in stateless_system_text - assert "Use replayed context." not in stateless_history_text - assert '' in stateless_history_text - assert '' in stateless_history_text - assert "Say hello through Responses." in stateless_history_text - assert "Hello from patched LLM" in stateless_history_text - assert "Follow up using the prior answer." in stateless_history_text - - streaming_response = client.post( - f"{env['host']}/v1/responses", - json={ - "model": "openhands_smoke", - "input": "This must not run as a buffered stream.", - "stream": True, - }, - timeout=2.0, - ) - assert streaming_response.status_code == 400 - assert streaming_response.json()["detail"] == ( - "Streaming responses are not supported yet" - ) + ) + second_stateless_result = second_stateless_response.parse() + assert second_stateless_result.output_text == "Hello from patched LLM" + assert ( + second_stateless_response.headers["X-OpenHands-ServerConversation-ID"] + != responses_conversation_id + ) + stateless_system_text = "\n".join( + part.text + for message in patched_llm[-1] + if message.role == "system" + for part in message.content + if isinstance(part, TextContent) + ) + stateless_history_text = "\n".join( + part.text + for message in patched_llm[-1] + if message.role == "user" + for part in message.content + if isinstance(part, TextContent) + ) + assert "Use replayed context." in stateless_system_text + assert "Use replayed context." not in stateless_history_text + assert '' in stateless_history_text + assert '' in stateless_history_text + assert "Say hello through Responses." in stateless_history_text + assert "Hello from patched LLM" in stateless_history_text + assert "Follow up using the prior answer." in stateless_history_text + + streaming_response = client.post( + f"{env['host']}/v1/responses", + json={ + "model": "openhands_smoke", + "input": "This must not run as a buffered stream.", + "stream": True, + }, + timeout=2.0, + ) + assert streaming_response.status_code == 400 + assert streaming_response.json()["detail"] == ( + "Streaming responses are not supported yet" + ) - store_response = client.post( - f"{env['host']}/v1/responses", - json={ - "model": "openhands_smoke", - "input": "This must not run as a stored response.", - "store": True, - }, - timeout=2.0, - ) - assert store_response.status_code == 400 - assert store_response.json()["detail"] == ( - "Persistent response storage (store=True) is not supported yet" - ) + store_response = client.post( + f"{env['host']}/v1/responses", + json={ + "model": "openhands_smoke", + "input": "This must not run as a stored response.", + "store": True, + }, + timeout=2.0, + ) + assert store_response.status_code == 400 + assert store_response.json()["detail"] == ( + "Persistent response storage (store=True) is not supported yet" + ) def test_openai_gateway_replays_frozen_llm_fixtures( @@ -1314,18 +1296,12 @@ def test_openai_gateway_replays_frozen_llm_fixtures( from openai import OpenAI - from openhands.agent_server import ( - config as config_module, - conversation_service as service_module, - ) from openhands.agent_server.models import StartConversationRequest from openhands.sdk import Message, TextContent from openhands.sdk.llm.llm_profile_store import LLMProfileStore from openhands.sdk.testing import TestLLM from openhands.sdk.workspace import LocalWorkspace - monkeypatch.setattr(config_module, "_default_config", None) - monkeypatch.setattr(service_module, "_conversation_service", None) monkeypatch.delenv("OH_WEBHOOKS_0_BASE_URL", raising=False) fixtures_dir = Path(__file__).parents[1] / "fixtures" / "openai_gateway" @@ -1334,7 +1310,9 @@ def test_openai_gateway_replays_frozen_llm_fixtures( json.loads((fixtures_dir / "litellm_haiku_completion.json").read_text()), ] - profiles_dir = tmp_path / "profiles" + # Where the live server's own stores keep LLM profiles (live_server_env + # points OH_PERSISTENCE_DIR at ``tmp_path / ".openhands"``). + profiles_dir = tmp_path / ".openhands" / "profiles" store = LLMProfileStore(base_dir=profiles_dir) for fixture in fixtures: store.save( @@ -1358,44 +1336,38 @@ async def start_conversation_with_test_llm(conversation_service, llm: TestLLM): event_service.get_conversation().switch_llm(llm) return info.id - with patch( - "openhands.agent_server.openai.service.get_llm_profile_store", - lambda: LLMProfileStore(base_dir=profiles_dir), - ): - with live_server_env(tmp_path, monkeypatch) as env: - for fixture in fixtures: - expected_content = fixture["response"]["choices"][0]["message"][ - "content" - ] - llm = TestLLM.from_messages( - [ - Message( - role="assistant", - content=[TextContent(text=expected_content)], - ) - ], - model=fixture["backing_model"], - usage_id=f"frozen-{fixture['profile_name']}", - ) - conversation_id = asyncio.run( - start_conversation_with_test_llm(env["conversation_service"], llm) - ) - client = OpenAI( - api_key="unused", - base_url=f"{env['host']}/v1", - default_headers={ - "X-OpenHands-ServerConversation-ID": str(conversation_id) - }, - timeout=10, - ) - completion = client.chat.completions.create( - model=fixture["gateway_model"], - messages=fixture["messages"], - ) + with live_server_env(tmp_path, monkeypatch) as env: + for fixture in fixtures: + expected_content = fixture["response"]["choices"][0]["message"]["content"] + llm = TestLLM.from_messages( + [ + Message( + role="assistant", + content=[TextContent(text=expected_content)], + ) + ], + model=fixture["backing_model"], + usage_id=f"frozen-{fixture['profile_name']}", + ) + conversation_id = asyncio.run( + start_conversation_with_test_llm(env["conversation_service"], llm) + ) + client = OpenAI( + api_key="unused", + base_url=f"{env['host']}/v1", + default_headers={ + "X-OpenHands-ServerConversation-ID": str(conversation_id) + }, + timeout=10, + ) + completion = client.chat.completions.create( + model=fixture["gateway_model"], + messages=fixture["messages"], + ) - assert completion.model == fixture["gateway_model"] - assert completion.choices[0].message.content == expected_content - assert llm.call_count == 1 + assert completion.model == fixture["gateway_model"] + assert completion.choices[0].message.content == expected_content + assert llm.call_count == 1 @pytest.mark.skipif( From 45121b365d78d08bc60be45b288d512a5bb628fb Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Thu, 12 Nov 2026 16:25:38 +0100 Subject: [PATCH 2/4] refactor(agent-server): the app owns its VSCode and tool-preload services The VSCode service was a module singleton built from whichever config asked first. A second app in the same process got the first app's editor: created with enable_vscode=False it still reported "enabled": true, and a different vscode_port was ignored. build_server_context() now builds the service of its own config and keeps it in ServerContext.vscode_service. The lifespan starts and stops it, POST /api/init carries it into the next context and only switches its token, and routes receive it through Depends(get_vscode_service). The tool-preload service is built by the lifespan that starts it. get_vscode_service(config), get_tool_preload_service(config) and both module singletons are gone. This also fixes GET /api/conversations/{id}/vscode/url on a server with VSCode disabled. The runtime router calls the get_vscode_url handler as a plain function, so its config parameter was the Depends default and the request failed with a 500 instead of the 503 the plain route gives. --- .../F16-workspace-serving-and-vscode.md | 2 +- .../openhands/agent_server/api.py | 7 +- .../openhands/agent_server/dependencies.py | 6 + .../openhands/agent_server/init_router.py | 10 +- .../openhands/agent_server/runtime_router.py | 4 + .../openhands/agent_server/server_context.py | 9 + .../agent_server/tool_preload_service.py | 16 -- .../openhands/agent_server/vscode_router.py | 11 +- .../openhands/agent_server/vscode_service.py | 37 +-- tests/agent_server/test_api.py | 77 ++---- .../test_blocking_export_extended.py | 8 +- tests/agent_server/test_init_router.py | 33 +-- tests/agent_server/test_runtime_router.py | 15 +- tests/agent_server/test_server_context.py | 39 +-- tests/agent_server/test_trace_shutdown.py | 16 +- tests/agent_server/test_vscode_router.py | 236 +++++++++--------- tests/agent_server/test_vscode_service.py | 74 +++--- 17 files changed, 265 insertions(+), 335 deletions(-) diff --git a/.agents/skills/verify-agent-server/references/feature-map/F16-workspace-serving-and-vscode.md b/.agents/skills/verify-agent-server/references/feature-map/F16-workspace-serving-and-vscode.md index 583f5a3340c..19186f3b80f 100644 --- a/.agents/skills/verify-agent-server/references/feature-map/F16-workspace-serving-and-vscode.md +++ b/.agents/skills/verify-agent-server/references/feature-map/F16-workspace-serving-and-vscode.md @@ -433,7 +433,7 @@ Preconditions: `running` is false, so the body is exactly `{"url": null}` on both routes, as a keyless server answers (`F16.vscode-url-no-key`) and as a server that receives the very same key through `POST /api/init` answers - (`F16.vscode-url-after-init`). `get_vscode_service()` seeds the token from + (`F16.vscode-url-after-init`). `create_vscode_service()` seeds the token from `session_api_keys[0]` before `start()` runs, but `get_vscode_url` returns a URL only while the server is running, so no dead "open VS Code" link (and no session key) is handed out for a server that is not there. diff --git a/openhands-agent-server/openhands/agent_server/api.py b/openhands-agent-server/openhands/agent_server/api.py index bbb7f947995..3e7840820c0 100644 --- a/openhands-agent-server/openhands/agent_server/api.py +++ b/openhands-agent-server/openhands/agent_server/api.py @@ -117,10 +117,9 @@ RequestFailedProperties, ) from openhands.agent_server.telemetry.sanitizer import normalize_exception -from openhands.agent_server.tool_preload_service import get_tool_preload_service +from openhands.agent_server.tool_preload_service import ToolPreloadService from openhands.agent_server.tool_router import tool_router from openhands.agent_server.vscode_router import vscode_router -from openhands.agent_server.vscode_service import get_vscode_service from openhands.agent_server.workspaces_router import workspaces_router from openhands.sdk.logger import DEBUG, get_logger from openhands.sdk.tool.registry import seal_tool_catalog, unseal_tool_catalog @@ -342,8 +341,8 @@ async def api_lifespan(api: FastAPI) -> AsyncIterator[None]: seal_tool_catalog() - vscode_service = get_vscode_service(config) - tool_preload_service = get_tool_preload_service(config) + vscode_service = context.vscode_service + tool_preload_service = ToolPreloadService() if config.preload_tools else None # Define async functions for starting each service async def start_vscode_service(): diff --git a/openhands-agent-server/openhands/agent_server/dependencies.py b/openhands-agent-server/openhands/agent_server/dependencies.py index 46020a4840c..c55cd19d44f 100644 --- a/openhands-agent-server/openhands/agent_server/dependencies.py +++ b/openhands-agent-server/openhands/agent_server/dependencies.py @@ -13,6 +13,7 @@ from openhands.agent_server.event_service import EventService from openhands.agent_server.persistence import ServerStores from openhands.agent_server.server_context import get_server_context +from openhands.agent_server.vscode_service import VSCodeService from openhands.sdk.utils.cipher import Cipher @@ -52,6 +53,11 @@ def get_conversation_registry(connection: HTTPConnection) -> ConversationRegistr return get_server_context(connection).conversation_registry +def get_vscode_service(connection: HTTPConnection) -> VSCodeService | None: + """This server's VSCode service (``None`` when VSCode is disabled).""" + return get_server_context(connection).vscode_service + + def _unavailable(connection: HTTPConnection, detail: str) -> Exception: """A started-service lookup that found none: 503, or 1013 on a WebSocket.""" if connection.scope["type"] == "websocket": diff --git a/openhands-agent-server/openhands/agent_server/init_router.py b/openhands-agent-server/openhands/agent_server/init_router.py index 4241ea55d4d..ee4dd40a23e 100644 --- a/openhands-agent-server/openhands/agent_server/init_router.py +++ b/openhands-agent-server/openhands/agent_server/init_router.py @@ -15,6 +15,7 @@ import asyncio import os from contextlib import AsyncExitStack +from dataclasses import replace from pathlib import Path from typing import Any, ClassVar, Literal @@ -45,9 +46,9 @@ from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.persistence import ServerStores, reencrypt_stale_stores from openhands.agent_server.server_context import ( - ServerContext, get_server_context, install_server_context, + server_context_of, ) from openhands.agent_server.server_details_router import mark_initialization_complete from openhands.agent_server.telemetry import ( @@ -55,7 +56,6 @@ emit_server_started, shutdown_telemetry_sink, ) -from openhands.agent_server.vscode_service import get_vscode_service from openhands.sdk.logger import get_logger from openhands.sdk.observability import maybe_init_laminar @@ -275,6 +275,7 @@ async def initialize(self, req: InitRequest) -> InitStatus: self._state = "initializing" self._error = None try: + dormant = server_context_of(self._app) new_config = _build_initialized_config(self._base_config, req) if req.env: # Setting env vars before services boot lets things like @@ -328,7 +329,7 @@ async def initialize(self, req: InitRequest) -> InitStatus: # VSCode's token is the first session key, as on a normal boot. # A dormant server that booted without one gave VSCode a random # token, so switch it to the key delivered here. - vscode_service = get_vscode_service(new_config) + vscode_service = dormant.vscode_service if vscode_service is not None and new_config.session_api_keys: await vscode_service.set_connection_token( new_config.session_api_keys[0] @@ -339,7 +340,8 @@ async def initialize(self, req: InitRequest) -> InitStatus: # services, and none of the dormant ones. install_server_context( self._app, - ServerContext( + replace( + dormant, config=new_config, stores=stores, conversation_registry=conversation_registry, diff --git a/openhands-agent-server/openhands/agent_server/runtime_router.py b/openhands-agent-server/openhands/agent_server/runtime_router.py index fddc306f933..37e5d9153d9 100644 --- a/openhands-agent-server/openhands/agent_server/runtime_router.py +++ b/openhands-agent-server/openhands/agent_server/runtime_router.py @@ -18,6 +18,7 @@ from openhands.agent_server.dependencies import ( get_conversation_service, get_event_service, + get_vscode_service, ) from openhands.agent_server.file_router import file_router from openhands.agent_server.git_router import runtime_git_router @@ -26,6 +27,7 @@ get_vscode_url, vscode_router, ) +from openhands.agent_server.vscode_service import VSCodeService async def bind_local_conversation_runtime( @@ -99,9 +101,11 @@ async def get_runtime_vscode_url( request: Request, base_url: str | None = None, workspace_dir: str | None = None, + vscode_service: VSCodeService | None = Depends(get_vscode_service), ) -> VSCodeUrlResponse: event_service = request.state.runtime_event_service return await get_vscode_url( base_url, workspace_dir or event_service.get_conversation().workspace.working_dir, + vscode_service, ) diff --git a/openhands-agent-server/openhands/agent_server/server_context.py b/openhands-agent-server/openhands/agent_server/server_context.py index 1053c81faf9..7829793e52b 100644 --- a/openhands-agent-server/openhands/agent_server/server_context.py +++ b/openhands-agent-server/openhands/agent_server/server_context.py @@ -27,6 +27,10 @@ create_conversation_registry, ) from openhands.agent_server.persistence import ServerStores +from openhands.agent_server.vscode_service import ( + VSCodeService, + create_vscode_service, +) # The services import modules that read the context, so they are named here @@ -48,6 +52,10 @@ class ServerContext: for as long as a deferred-init server is dormant.""" bash_event_service: BashEventService | None = None """``None`` under the same conditions as ``conversation_service``.""" + vscode_service: VSCodeService | None = None + """``None`` when VSCode is disabled. The editor belongs to the app rather + than to one initialisation: ``POST /api/init`` keeps it and only switches + its token.""" def build_server_context(config: Config) -> ServerContext: @@ -57,6 +65,7 @@ def build_server_context(config: Config) -> ServerContext: config=config, stores=stores, conversation_registry=create_conversation_registry(config, stores), + vscode_service=create_vscode_service(config), ) diff --git a/openhands-agent-server/openhands/agent_server/tool_preload_service.py b/openhands-agent-server/openhands/agent_server/tool_preload_service.py index 7ec62a8f039..d262c7451c7 100644 --- a/openhands-agent-server/openhands/agent_server/tool_preload_service.py +++ b/openhands-agent-server/openhands/agent_server/tool_preload_service.py @@ -2,7 +2,6 @@ from __future__ import annotations -from openhands.agent_server.config import Config from openhands.sdk.logger import get_logger from openhands.sdk.tool.schema import Action from openhands.sdk.tool.tool import create_action_type_with_risk @@ -50,18 +49,3 @@ async def stop(self) -> None: def is_running(self) -> bool: """Check if tool preload is running.""" return self.running - - -_tool_preload_service: ToolPreloadService | None = None - - -def get_tool_preload_service(config: Config) -> ToolPreloadService | None: - """Get the tool preload service instance if ``config`` enables preload.""" - global _tool_preload_service - if not config.preload_tools: - _logger.info("Tool preload is disabled in configuration") - return None - - if _tool_preload_service is None: - _tool_preload_service = ToolPreloadService() - return _tool_preload_service diff --git a/openhands-agent-server/openhands/agent_server/vscode_router.py b/openhands-agent-server/openhands/agent_server/vscode_router.py index b32c14fb987..a5e55b3ced1 100644 --- a/openhands-agent-server/openhands/agent_server/vscode_router.py +++ b/openhands-agent-server/openhands/agent_server/vscode_router.py @@ -3,9 +3,8 @@ from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel -from openhands.agent_server.config import Config -from openhands.agent_server.dependencies import get_config -from openhands.agent_server.vscode_service import get_vscode_service +from openhands.agent_server.dependencies import get_vscode_service +from openhands.agent_server.vscode_service import VSCodeService from openhands.sdk.logger import get_logger @@ -24,7 +23,7 @@ class VSCodeUrlResponse(BaseModel): async def get_vscode_url( base_url: str | None = None, workspace_dir: str = "workspace", - config: Config = Depends(get_config), + vscode_service: VSCodeService | None = Depends(get_vscode_service), ) -> VSCodeUrlResponse: """Get the VSCode URL with authentication token. @@ -39,7 +38,6 @@ async def get_vscode_url( Returns: VSCode URL with token if available, None otherwise """ - vscode_service = get_vscode_service(config) if vscode_service is None: raise HTTPException( status_code=503, @@ -58,14 +56,13 @@ async def get_vscode_url( @vscode_router.get("/status") async def get_vscode_status( - config: Config = Depends(get_config), + vscode_service: VSCodeService | None = Depends(get_vscode_service), ) -> dict[str, bool | str]: """Get the VSCode server status. Returns: Dictionary with running status and enabled status """ - vscode_service = get_vscode_service(config) if vscode_service is None: return { "running": False, diff --git a/openhands-agent-server/openhands/agent_server/vscode_service.py b/openhands-agent-server/openhands/agent_server/vscode_service.py index 66fb3caae3a..6a8b7c58d83 100644 --- a/openhands-agent-server/openhands/agent_server/vscode_service.py +++ b/openhands-agent-server/openhands/agent_server/vscode_service.py @@ -297,31 +297,20 @@ async def _wait_for_startup(self) -> None: raise RuntimeError("code-server exited or timed out before startup") -# Global VSCode service instance -_vscode_service: VSCodeService | None = None - - -def get_vscode_service(config: Config) -> VSCodeService | None: - """Get the global VSCode service instance, creating it from ``config``. - - The service outlives a server (re)initialisation, so ``config`` only - decides how it is created the first time. +def create_vscode_service(config: Config) -> VSCodeService | None: + """Build the VSCode service ``config`` asks for, without starting it. Returns: VSCode service instance if enabled, None if disabled """ - global _vscode_service - if _vscode_service is None: - if not config.enable_vscode: - logger.info("VSCode is disabled in configuration") - return None - else: - connection_token = None - if config.session_api_keys: - connection_token = config.session_api_keys[0] - _vscode_service = VSCodeService( - port=config.vscode_port, - connection_token=connection_token, - server_base_path=config.vscode_base_path, - ) - return _vscode_service + if not config.enable_vscode: + logger.info("VSCode is disabled in configuration") + return None + connection_token = None + if config.session_api_keys: + connection_token = config.session_api_keys[0] + return VSCodeService( + port=config.vscode_port, + connection_token=connection_token, + server_base_path=config.vscode_base_path, + ) diff --git a/tests/agent_server/test_api.py b/tests/agent_server/test_api.py index cdb9dcd9b53..31a4f6c219a 100644 --- a/tests/agent_server/test_api.py +++ b/tests/agent_server/test_api.py @@ -28,6 +28,7 @@ from openhands.agent_server.config import Config from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.server_context import ( + ServerContext, build_server_context, server_context_of, ) @@ -569,6 +570,11 @@ def test_no_root_redirect_when_static_directory_missing(self): assert response.status_code == 200 +def _context_without_side_services() -> ServerContext: + """A context whose lifespan starts neither VSCode nor tool preload.""" + return build_server_context(Config(enable_vscode=False, preload_tools=False)) + + class TestServiceParallelization: """Test that services are started and stopped in parallel.""" @@ -606,18 +612,17 @@ async def slow_start(): return_value=mock_conversation_service, ), patch( - "openhands.agent_server.api.get_vscode_service", - return_value=mock_vscode_service, - ), - patch( - "openhands.agent_server.api.get_tool_preload_service", + "openhands.agent_server.api.ToolPreloadService", return_value=mock_tool_preload_service, ), ): # Create a mock FastAPI app mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=replace( + build_server_context(Config()), + vscode_service=mock_vscode_service, + ) ) async with api_lifespan(mock_app): @@ -652,11 +657,7 @@ async def slow_stop(): return_value=mock_conversation_service, ), patch( - "openhands.agent_server.api.get_vscode_service", - return_value=mock_vscode_service, - ), - patch( - "openhands.agent_server.api.get_tool_preload_service", + "openhands.agent_server.api.ToolPreloadService", return_value=mock_tool_preload_service, ), ): @@ -664,7 +665,10 @@ async def slow_stop(): mock_app = AsyncMock() mock_backend_manager = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()), + server_context=replace( + build_server_context(Config()), + vscode_service=mock_vscode_service, + ), canvas_extension_backend_manager=mock_backend_manager, ) @@ -687,15 +691,11 @@ async def test_services_handle_none_values(self): "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): # Create a mock FastAPI app mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) # This should not raise any exceptions @@ -721,16 +721,11 @@ async def test_registry_starts_before_conversation_recovery(self): "openhands.agent_server.api.ConversationService.get_instance", return_value=service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", - return_value=None, - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( server_context=replace( - build_server_context(Config()), + _context_without_side_services(), conversation_registry=cast(ConversationRegistry, registry), ) ) @@ -756,14 +751,10 @@ async def test_lifespan_defaults_and_restores_tmux_tmpdir( "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(expected_tmux_tmpdir) @@ -786,14 +777,10 @@ async def test_lifespan_keeps_user_provided_tmux_tmpdir( "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(user_tmux_tmpdir) @@ -824,14 +811,10 @@ async def test_lifespan_sets_and_restores_utf8_locale( "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) async with api_lifespan(mock_app): assert os.environ[overridden] == "C.UTF-8" @@ -859,14 +842,10 @@ async def test_lifespan_removes_substitute_for_too_deep_tmux_tmpdir( "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(substitute) @@ -900,14 +879,10 @@ def fail_rmtree(path): "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) with caplog.at_level("WARNING"): async with api_lifespan(mock_app): @@ -933,14 +908,10 @@ async def test_lifespan_keeps_preexisting_default_tmux_tmpdir( "openhands.agent_server.api.ConversationService.get_instance", return_value=mock_conversation_service, ), - patch("openhands.agent_server.api.get_vscode_service", return_value=None), - patch( - "openhands.agent_server.api.get_tool_preload_service", return_value=None - ), ): mock_app = AsyncMock() mock_app.state = SimpleNamespace( - server_context=build_server_context(Config()) + server_context=_context_without_side_services() ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(expected_tmux_tmpdir) diff --git a/tests/agent_server/test_blocking_export_extended.py b/tests/agent_server/test_blocking_export_extended.py index e89091c048e..b1d1063a611 100644 --- a/tests/agent_server/test_blocking_export_extended.py +++ b/tests/agent_server/test_blocking_export_extended.py @@ -35,12 +35,6 @@ def blocked_export(spans): monkeypatch.setattr( "openhands.agent_server.api._TRACE_FLUSH_TIMEOUT_SECONDS", 0.05, raising=False ) - monkeypatch.setattr( - "openhands.agent_server.api.get_vscode_service", lambda _config: None - ) - monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda _config: None - ) monkeypatch.setattr( "openhands.agent_server.api.unseal_tool_catalog", lambda: cleanup.append(True) ) @@ -49,6 +43,8 @@ def blocked_export(spans): deferred_init=True, conversations_path=tmp_path / "conversations", bash_events_dir=tmp_path / "bash", + enable_vscode=False, + preload_tools=False, ) ) monkeypatch.setattr( diff --git a/tests/agent_server/test_init_router.py b/tests/agent_server/test_init_router.py index 4cccb983383..85a70021a9d 100644 --- a/tests/agent_server/test_init_router.py +++ b/tests/agent_server/test_init_router.py @@ -9,10 +9,10 @@ import os import zipfile from concurrent.futures import ThreadPoolExecutor +from dataclasses import replace from pathlib import Path from threading import Event -from types import SimpleNamespace -from unittest.mock import AsyncMock +from unittest.mock import AsyncMock, MagicMock from uuid import UUID, uuid4 import pytest @@ -54,16 +54,13 @@ def _clean_env(monkeypatch): monkeypatch.delenv(key, raising=False) -@pytest.fixture(autouse=True) -def _fresh_vscode_service(monkeypatch): - """/api/init updates the VSCode service singleton; give each test its own.""" - monkeypatch.setattr("openhands.agent_server.vscode_service._vscode_service", None) - - -def _dormant_app(config: Config) -> FastAPI: +def _dormant_app( + config: Config, vscode_service: VSCodeService | None = None +) -> FastAPI: """A bare app holding the dormant server context, as ``create_app`` leaves it.""" app = FastAPI() - install_server_context(app, build_server_context(config)) + context = replace(build_server_context(config), vscode_service=vscode_service) + install_server_context(app, context) return app @@ -340,17 +337,13 @@ async def test_init_switches_vscode_to_first_session_key( ): """VSCode booted with a random token; init switches it to the first session key, which a server that booted with the key would use.""" - vscode = SimpleNamespace(set_connection_token=AsyncMock()) - monkeypatch.setattr( - "openhands.agent_server.init_router.get_vscode_service", - lambda _config: vscode, - ) + vscode = MagicMock(spec=VSCodeService) base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = _dormant_app(base) + app = _dormant_app(base, vscode_service=vscode) svc = InitService(app, base_config=base) await svc.initialize( @@ -370,17 +363,13 @@ async def test_init_without_session_keys_keeps_vscode_token( self, tmp_path, monkeypatch ): """With no session key to switch to, VSCode keeps its boot token.""" - vscode = SimpleNamespace(set_connection_token=AsyncMock()) - monkeypatch.setattr( - "openhands.agent_server.init_router.get_vscode_service", - lambda _config: vscode, - ) + vscode = MagicMock(spec=VSCodeService) base = Config( deferred_init=True, conversations_path=tmp_path / "convs", bash_events_dir=tmp_path / "bash", ) - app = _dormant_app(base) + app = _dormant_app(base, vscode_service=vscode) svc = InitService(app, base_config=base) await svc.initialize( diff --git a/tests/agent_server/test_runtime_router.py b/tests/agent_server/test_runtime_router.py index c235e295efa..820179cd6e2 100644 --- a/tests/agent_server/test_runtime_router.py +++ b/tests/agent_server/test_runtime_router.py @@ -7,11 +7,11 @@ from fastapi import FastAPI from fastapi.routing import APIRoute -import openhands.agent_server.vscode_service as vscode_service_module from openhands.agent_server.api import create_app from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService +from openhands.agent_server.dependencies import get_vscode_service from openhands.agent_server.runtime_router import create_runtime_router from openhands.agent_server.server_context import server_context_of from openhands.agent_server.vscode_service import VSCodeService @@ -172,16 +172,25 @@ async def test_conversation_close_stops_runtime_terminal(runtime_client, tmp_pat async def test_scoped_vscode_defaults_to_conversation_workspace( runtime_client, tmp_path, monkeypatch ): - client, _ = runtime_client + client, app = runtime_client root = tmp_path / "workspace" cid = await _create(client, root) service = VSCodeService(port=18765) - monkeypatch.setattr(vscode_service_module, "_vscode_service", service) + app.dependency_overrides[get_vscode_service] = lambda: service response = await client.get(f"/api/conversations/{cid}/vscode/url") assert response.status_code == 200, response.text assert response.json()["url"] == service.get_vscode_url(workspace_dir=str(root)) +@pytest.mark.asyncio +async def test_scoped_vscode_reports_a_disabled_editor(runtime_client, tmp_path): + client, app = runtime_client + cid = await _create(client, tmp_path / "workspace") + app.dependency_overrides[get_vscode_service] = lambda: None + response = await client.get(f"/api/conversations/{cid}/vscode/url") + assert response.status_code == 503, response.text + + def test_runtime_routes_are_registered_with_dispatch_adapter(): class CustomRuntimeRoute(APIRoute): pass diff --git a/tests/agent_server/test_server_context.py b/tests/agent_server/test_server_context.py index d41a7e91fa1..197b4b6adb4 100644 --- a/tests/agent_server/test_server_context.py +++ b/tests/agent_server/test_server_context.py @@ -27,7 +27,12 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService -from openhands.agent_server.server_context import ServerContext, server_context_of +from openhands.agent_server.server_context import ( + ServerContext, + install_server_context, + server_context_of, +) +from openhands.agent_server.vscode_service import VSCodeService from openhands.sdk.utils.cipher import Cipher @@ -36,12 +41,6 @@ SESSION_KEY = "user-session-key" -@pytest.fixture(autouse=True) -def _fresh_vscode_service(monkeypatch: pytest.MonkeyPatch) -> None: - """/api/init touches the VSCode service singleton; give each test its own.""" - monkeypatch.setattr("openhands.agent_server.vscode_service._vscode_service", None) - - @pytest.fixture def dormant(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[TestClient]: """A deferred-init server that booted with the pool's key and directory.""" @@ -58,6 +57,17 @@ def dormant(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[TestCli yield client +class _RefusingVSCode(VSCodeService): + """An editor whose token switch fails: the last step of /api/init, after the + new stores, registry and services are all up.""" + + refuses = True + + async def set_connection_token(self, connection_token: str) -> None: + if self.refuses: + raise RuntimeError("vscode refused the token") + + def _app(client: TestClient) -> FastAPI: app = client.app assert isinstance(app, FastAPI) @@ -135,6 +145,10 @@ def test_a_failed_init_leaves_the_dormant_context_in_place( dormant: TestClient, tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: app = _app(dormant) + vscode = _RefusingVSCode() + install_server_context( + app, dataclasses.replace(server_context_of(app), vscode_service=vscode) + ) before = server_context_of(app) started: list[ConversationService] = [] real_get_instance = ConversationService.get_instance @@ -144,17 +158,9 @@ def recording_get_instance(config, stores): started.append(service) return service - class _FailingVSCode: - async def set_connection_token(self, _token: str) -> None: - raise RuntimeError("vscode refused the token") - monkeypatch.setattr( init_router_module.ConversationService, "get_instance", recording_get_instance ) - # Fails after the new stores, registry and services are all up. - monkeypatch.setattr( - init_router_module, "get_vscode_service", lambda _config: _FailingVSCode() - ) failed = _init(dormant, tmp_path) @@ -170,7 +176,7 @@ async def set_connection_token(self, _token: str) -> None: # The dormant key still guards /api/init, so the orchestrator can retry. assert dormant.post("/api/init", json={}).status_code == 401 - monkeypatch.setattr(init_router_module, "get_vscode_service", lambda _config: None) + vscode.refuses = False assert _init(dormant, tmp_path).status_code == 200 retried = server_context_of(app) assert retried is not before @@ -220,6 +226,7 @@ def test_an_app_without_a_context_says_so() -> None: "conversation_service", "bash_event_service", "conversation_registry", + "vscode_service", } ) _CONTEXT_MODULE = "server_context.py" diff --git a/tests/agent_server/test_trace_shutdown.py b/tests/agent_server/test_trace_shutdown.py index 8410dad9327..44b96d40064 100644 --- a/tests/agent_server/test_trace_shutdown.py +++ b/tests/agent_server/test_trace_shutdown.py @@ -25,17 +25,13 @@ def flush(timeout_millis): monkeypatch.setattr(provider, "force_flush", flush) monkeypatch.setattr("opentelemetry.trace.get_tracer_provider", lambda: provider) - monkeypatch.setattr( - "openhands.agent_server.api.get_vscode_service", lambda _config: None - ) - monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda _config: None - ) app = create_app( Config( deferred_init=True, conversations_path=tmp_path / "conversations", bash_events_dir=tmp_path / "bash", + enable_vscode=False, + preload_tools=False, ) ) try: @@ -61,12 +57,6 @@ async def test_no_flush_when_tracing_is_not_configured(monkeypatch, tmp_path): monkeypatch.setattr( "opentelemetry.trace.get_tracer_provider", lambda: ProxyTracerProvider() ) - monkeypatch.setattr( - "openhands.agent_server.api.get_vscode_service", lambda _config: None - ) - monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda _config: None - ) flushed = [] monkeypatch.setattr( TracerProvider, "force_flush", lambda self, *a, **k: flushed.append(1) @@ -76,6 +66,8 @@ async def test_no_flush_when_tracing_is_not_configured(monkeypatch, tmp_path): deferred_init=True, conversations_path=tmp_path / "conversations", bash_events_dir=tmp_path / "bash", + enable_vscode=False, + preload_tools=False, ) ) async with api_lifespan(app): diff --git a/tests/agent_server/test_vscode_router.py b/tests/agent_server/test_vscode_router.py index 0288ed37299..0a49c41690f 100644 --- a/tests/agent_server/test_vscode_router.py +++ b/tests/agent_server/test_vscode_router.py @@ -1,43 +1,51 @@ """Tests for VSCode router.""" -from unittest.mock import patch +from pathlib import Path +from unittest.mock import MagicMock import pytest -from fastapi import HTTPException +from fastapi import FastAPI, HTTPException from fastapi.testclient import TestClient from openhands.agent_server.api import create_app from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_vscode_service +from openhands.agent_server.server_context import server_context_of from openhands.agent_server.vscode_router import ( get_vscode_status, get_vscode_url, ) +from openhands.agent_server.vscode_service import VSCodeService @pytest.fixture -def client(): +def app() -> FastAPI: + """An app with authentication disabled.""" + return create_app(Config(session_api_keys=[])) + + +@pytest.fixture +def client(app: FastAPI) -> TestClient: """Create a test client.""" - config = Config(session_api_keys=[]) # Disable authentication for tests - app = create_app(config) return TestClient(app) @pytest.fixture -def mock_vscode_service(): +def mock_vscode_service() -> MagicMock: """Mock VSCode service for testing.""" - with patch("openhands.agent_server.vscode_router.get_vscode_service") as mock: - yield mock.return_value + return MagicMock(spec=VSCodeService) @pytest.mark.asyncio async def test_get_vscode_url_success(mock_vscode_service): """Test getting VSCode URL successfully.""" - mock_vscode_service.get_connection_token.return_value = "test-token" mock_vscode_service.get_vscode_url.return_value = ( "http://localhost:8001/?tkn=test-token&folder=/workspace" ) - response = await get_vscode_url("http://localhost") + response = await get_vscode_url( + "http://localhost", vscode_service=mock_vscode_service + ) assert response.url == "http://localhost:8001/?tkn=test-token&folder=/workspace" mock_vscode_service.get_vscode_url.assert_called_once_with( @@ -57,7 +65,7 @@ async def test_get_vscode_url_default_uses_configured_port(mock_vscode_service): "http://localhost:19000/?tkn=test-token&folder=workspace" ) - response = await get_vscode_url() + response = await get_vscode_url(vscode_service=mock_vscode_service) assert response.url == "http://localhost:19000/?tkn=test-token&folder=workspace" mock_vscode_service.get_vscode_url.assert_called_once_with(None, "workspace") @@ -66,10 +74,10 @@ async def test_get_vscode_url_default_uses_configured_port(mock_vscode_service): @pytest.mark.asyncio async def test_get_vscode_url_error(mock_vscode_service): """Test getting VSCode URL with service error.""" - mock_vscode_service.get_connection_token.side_effect = Exception("Service error") + mock_vscode_service.get_vscode_url.side_effect = Exception("Service error") with pytest.raises(HTTPException) as exc_info: - await get_vscode_url() + await get_vscode_url(vscode_service=mock_vscode_service) assert exc_info.value.status_code == 500 assert "Failed to get VSCode URL" in str(exc_info.value.detail) @@ -80,7 +88,7 @@ async def test_get_vscode_status_running(mock_vscode_service): """Test getting VSCode status when running.""" mock_vscode_service.is_running.return_value = True - response = await get_vscode_status() + response = await get_vscode_status(vscode_service=mock_vscode_service) assert response == {"running": True, "enabled": True} mock_vscode_service.is_running.assert_called_once() @@ -91,7 +99,7 @@ async def test_get_vscode_status_not_running(mock_vscode_service): """Test getting VSCode status when not running.""" mock_vscode_service.is_running.return_value = False - response = await get_vscode_status() + response = await get_vscode_status(vscode_service=mock_vscode_service) assert response == {"running": False, "enabled": True} @@ -102,139 +110,123 @@ async def test_get_vscode_status_error(mock_vscode_service): mock_vscode_service.is_running.side_effect = Exception("Service error") with pytest.raises(HTTPException) as exc_info: - await get_vscode_status() + await get_vscode_status(vscode_service=mock_vscode_service) assert exc_info.value.status_code == 500 assert "Failed to get VSCode status" in str(exc_info.value.detail) -def test_vscode_router_endpoints_integration(client): +def test_vscode_router_endpoints_integration(app, client, mock_vscode_service): """Test VSCode router endpoints through the API.""" - # Patch both the router import and the service module - with ( - patch( - "openhands.agent_server.vscode_router.get_vscode_service" - ) as mock_service_getter, - patch("openhands.agent_server.api.get_vscode_service") as mock_api_service, - ): - mock_service = mock_service_getter.return_value - mock_service.get_vscode_url.return_value = ( - "http://localhost:8001/?tkn=integration-token" - ) - mock_service.is_running.return_value = True - - # Mock the API service to avoid startup - mock_api_service.return_value.start.return_value = True - mock_api_service.return_value.stop.return_value = None + app.dependency_overrides[get_vscode_service] = lambda: mock_vscode_service + mock_vscode_service.get_vscode_url.return_value = ( + "http://localhost:8001/?tkn=integration-token" + ) + mock_vscode_service.is_running.return_value = True - # Test URL endpoint - response = client.get("/api/vscode/url") - assert response.status_code == 200 - data = response.json() - assert data["url"] == "http://localhost:8001/?tkn=integration-token" + # Test URL endpoint + response = client.get("/api/vscode/url") + assert response.status_code == 200 + data = response.json() + assert data["url"] == "http://localhost:8001/?tkn=integration-token" - # Test URL endpoint with custom base URL - response = client.get("/api/vscode/url?base_url=http://example.com") - assert response.status_code == 200 + # Test URL endpoint with custom base URL + response = client.get("/api/vscode/url?base_url=http://example.com") + assert response.status_code == 200 - # Test status endpoint - response = client.get("/api/vscode/status") - assert response.status_code == 200 - data = response.json() - assert data["running"] is True + # Test status endpoint + response = client.get("/api/vscode/status") + assert response.status_code == 200 + data = response.json() + assert data["running"] is True -def test_vscode_router_endpoints_with_errors(client): +def test_vscode_router_endpoints_with_errors(app, client, mock_vscode_service): """Test VSCode router endpoints with service errors.""" - # Patch both the router import and the service module - with ( - patch( - "openhands.agent_server.vscode_router.get_vscode_service" - ) as mock_service_getter, - patch("openhands.agent_server.api.get_vscode_service") as mock_api_service, - ): - mock_service = mock_service_getter.return_value - mock_service.is_running.side_effect = Exception("Service down") - - # Mock the API service to avoid startup - mock_api_service.return_value.start.return_value = True - mock_api_service.return_value.stop.return_value = None - - # Test URL endpoint error - response = client.get("/api/vscode/url") - assert response.status_code == 500 - data = response.json() - assert data["detail"] == "Internal Server Error" - - # Test status endpoint error - response = client.get("/api/vscode/status") - assert response.status_code == 500 - data = response.json() - assert data["detail"] == "Internal Server Error" + app.dependency_overrides[get_vscode_service] = lambda: mock_vscode_service + mock_vscode_service.get_vscode_url.side_effect = Exception("Service down") + mock_vscode_service.is_running.side_effect = Exception("Service down") + + # Test URL endpoint error + response = client.get("/api/vscode/url") + assert response.status_code == 500 + data = response.json() + assert data["detail"] == "Internal Server Error" + + # Test status endpoint error + response = client.get("/api/vscode/status") + assert response.status_code == 500 + data = response.json() + assert data["detail"] == "Internal Server Error" @pytest.mark.asyncio async def test_get_vscode_url_disabled(): """Test getting VSCode URL when VSCode is disabled.""" - with patch( - "openhands.agent_server.vscode_router.get_vscode_service" - ) as mock_service: - mock_service.return_value = None - - with pytest.raises(HTTPException) as exc_info: - await get_vscode_url() + with pytest.raises(HTTPException) as exc_info: + await get_vscode_url(vscode_service=None) - assert exc_info.value.status_code == 503 - assert "VSCode is disabled in configuration" in str(exc_info.value.detail) + assert exc_info.value.status_code == 503 + assert "VSCode is disabled in configuration" in str(exc_info.value.detail) @pytest.mark.asyncio async def test_get_vscode_status_disabled(): """Test getting VSCode status when VSCode is disabled.""" - with patch( - "openhands.agent_server.vscode_router.get_vscode_service" - ) as mock_service: - mock_service.return_value = None + response = await get_vscode_status(vscode_service=None) - response = await get_vscode_status() + assert response == { + "running": False, + "enabled": False, + "message": "VSCode is disabled in configuration", + } - assert response == { - "running": False, - "enabled": False, - "message": "VSCode is disabled in configuration", - } - -def test_vscode_router_disabled_integration(client): +def test_vscode_router_disabled_integration(): """Test VSCode router endpoints when VSCode is disabled.""" - with ( - patch( - "openhands.agent_server.vscode_router.get_vscode_service" - ) as mock_router_service, - patch("openhands.agent_server.api.get_vscode_service") as mock_api_service, - ): - # Configure VSCode as disabled - mock_router_service.return_value = None - - # Mock the API service to avoid startup - mock_api_service.return_value = None - - # Test URL endpoint returns 503 when disabled - response = client.get("/api/vscode/url") - assert response.status_code == 503 - data = response.json() - # The error message might be in different fields depending on FastAPI error - # handling - error_message = data.get("detail", data.get("message", "")) - assert ( - "VSCode is disabled" in error_message - or "Internal Server Error" in error_message + client = TestClient(create_app(Config(session_api_keys=[], enable_vscode=False))) + + # Test URL endpoint returns 503 when disabled + response = client.get("/api/vscode/url") + assert response.status_code == 503 + + # Test status endpoint returns disabled status + response = client.get("/api/vscode/status") + assert response.status_code == 200 + data = response.json() + assert data["running"] is False + assert data["enabled"] is False + assert "VSCode is disabled in configuration" in data["message"] + + +def test_each_app_has_the_vscode_service_of_its_own_config(tmp_path: Path): + """One app's editor settings must not decide another app's.""" + + def client_for(name: str, *, enable_vscode: bool, vscode_port: int) -> TestClient: + config = Config( + session_api_keys=[], + conversations_path=tmp_path / name / "conversations", + bash_events_dir=tmp_path / name / "bash", + preload_tools=False, + enable_vscode=enable_vscode, + vscode_port=vscode_port, ) + return TestClient(create_app(config)) + + def service_of(client: TestClient) -> VSCodeService | None: + app = client.app + assert isinstance(app, FastAPI) + return server_context_of(app).vscode_service + + enabled = client_for("a", enable_vscode=True, vscode_port=18001) + disabled = client_for("b", enable_vscode=False, vscode_port=18001) + other_port = client_for("c", enable_vscode=True, vscode_port=18002) + + assert enabled.get("/api/vscode/status").json()["enabled"] is True + assert disabled.get("/api/vscode/status").json()["enabled"] is False + assert disabled.get("/api/vscode/url").status_code == 503 - # Test status endpoint returns disabled status - response = client.get("/api/vscode/status") - assert response.status_code == 200 - data = response.json() - assert data["running"] is False - assert data["enabled"] is False - assert "VSCode is disabled in configuration" in data["message"] + first, second = service_of(enabled), service_of(other_port) + assert first is not None and second is not None + assert (first.port, second.port) == (18001, 18002) + assert service_of(disabled) is None diff --git a/tests/agent_server/test_vscode_service.py b/tests/agent_server/test_vscode_service.py index 38c5808b9b6..f16fb545163 100644 --- a/tests/agent_server/test_vscode_service.py +++ b/tests/agent_server/test_vscode_service.py @@ -12,7 +12,7 @@ from openhands.agent_server.config import Config from openhands.agent_server.vscode_service import ( VSCodeService, - get_vscode_service, + create_vscode_service, ) @@ -585,60 +585,44 @@ async def test_wait_for_startup_timeout(vscode_service): await vscode_service._wait_for_startup() -# Tests for get_vscode_service with enable_vscode configuration +# Tests for create_vscode_service with enable_vscode configuration -def test_get_vscode_service_enabled(tmp_path): - """Test get_vscode_service returns VSCodeService when enabled.""" - config = Config(enable_vscode=True, vscode_port=8001, session_api_keys=[]) - with patch("openhands.agent_server.vscode_service._vscode_service", None): - service = get_vscode_service(config) - - assert isinstance(service, VSCodeService) - - -def test_get_vscode_service_disabled(): - """Test get_vscode_service returns None when disabled.""" - with patch("openhands.agent_server.vscode_service._vscode_service", None): - service = get_vscode_service(Config(enable_vscode=False)) - - assert service is None - - -def test_get_vscode_service_singleton(): - """Test get_vscode_service returns the same instance on multiple calls.""" - config = Config(enable_vscode=True, vscode_port=8001, session_api_keys=[]) - with patch("openhands.agent_server.vscode_service._vscode_service", None): - service1 = get_vscode_service(config) - service2 = get_vscode_service(config) +def test_create_vscode_service_enabled(): + """An enabled config gets a service built from its port and base path.""" + config = Config( + enable_vscode=True, + vscode_port=9001, + vscode_base_path="/runtime-123/vscode", + session_api_keys=["first-key", "second-key"], + ) - assert service1 is service2 - assert isinstance(service1, VSCodeService) + service = create_vscode_service(config) + assert isinstance(service, VSCodeService) + assert service.port == 9001 + assert service.server_base_path == "/runtime-123/vscode" + assert service.connection_token == "first-key" -def test_get_vscode_service_with_custom_port(): - """Test get_vscode_service uses the configured port.""" - config = Config(enable_vscode=True, vscode_port=9001, session_api_keys=[]) - with patch("openhands.agent_server.vscode_service._vscode_service", None): - service = get_vscode_service(config) - assert isinstance(service, VSCodeService) - assert service.port == 9001 +def test_create_vscode_service_disabled(): + """A config with VSCode disabled gets no service.""" + assert create_vscode_service(Config(enable_vscode=False)) is None -def test_get_vscode_service_with_base_path(): - """Test get_vscode_service passes vscode_base_path from config.""" - config = Config( - enable_vscode=True, - vscode_port=8001, - vscode_base_path="/runtime-123/vscode", - session_api_keys=[], +def test_create_vscode_service_keeps_no_process_state(): + """A later config is never answered with an earlier config's service.""" + first = create_vscode_service( + Config(enable_vscode=True, vscode_port=8001, session_api_keys=[]) + ) + second = create_vscode_service( + Config(enable_vscode=True, vscode_port=9001, session_api_keys=[]) ) - with patch("openhands.agent_server.vscode_service._vscode_service", None): - service = get_vscode_service(config) - assert isinstance(service, VSCodeService) - assert service.server_base_path == "/runtime-123/vscode" + assert first is not None and second is not None + assert first is not second + assert (first.port, second.port) == (8001, 9001) + assert create_vscode_service(Config(enable_vscode=False)) is None def test_vscode_service_with_different_ports(): From 4afd12d5f05a101499bb8bb2d144ad4262d5f3f8 Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Thu, 12 Nov 2026 16:51:47 +0100 Subject: [PATCH 3/4] fix(agent-server): make POST /api/init all-or-nothing A failed /api/init reported "dormant" while keeping parts of the attempt. With a web_url that cannot be parsed, the root path was derived after the new context was installed: the request failed with a 500, but the delivered key already guarded /api/init (a retry with the bootstrap key got 401) and the conversation service kept running. Any failed attempt also left the variables from "env" in the process and the delivered telemetry sink in place of the dormant one, and a cancelled attempt left the server in "initializing" for good. InitService.initialize now has two phases: - _prepare does every step that can fail and publishes nothing. The root path is derived first. The services and the environment go on one exit stack, the telemetry switch on a second one that only runs when the attempt fails, so a failure or a cancellation undoes all of it. - _commit publishes the result. It is synchronous and made of assignments, so it cannot fail or be interrupted half-way. teardown() unwinds the same stack, so it now also restores the variables set through "env", not only the exported session key. --- .../openhands/agent_server/init_router.py | 233 ++++++++++-------- tests/agent_server/test_init_router.py | 3 +- tests/agent_server/test_server_context.py | 98 +++++++- 3 files changed, 224 insertions(+), 110 deletions(-) diff --git a/openhands-agent-server/openhands/agent_server/init_router.py b/openhands-agent-server/openhands/agent_server/init_router.py index ee4dd40a23e..454710aeeb0 100644 --- a/openhands-agent-server/openhands/agent_server/init_router.py +++ b/openhands-agent-server/openhands/agent_server/init_router.py @@ -15,7 +15,7 @@ import asyncio import os from contextlib import AsyncExitStack -from dataclasses import replace +from dataclasses import dataclass, replace from pathlib import Path from typing import Any, ClassVar, Literal @@ -46,6 +46,7 @@ from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.persistence import ServerStores, reencrypt_stale_stores from openhands.agent_server.server_context import ( + ServerContext, get_server_context, install_server_context, server_context_of, @@ -223,6 +224,25 @@ def _build_initialized_config(base: Config, req: InitRequest) -> Config: return base.model_copy(update=updates) +def _restore_environ(previous: dict[str, str | None]) -> None: + """Put variables back to the recorded values; ``None`` means it was unset.""" + for key, value in previous.items(): + if value is None: + os.environ.pop(key, None) + else: + os.environ[key] = value + + +@dataclass(frozen=True, slots=True) +class _PreparedInit: + """An initialisation with every fallible step done and nothing published.""" + + context: ServerContext + root_path: str + started: AsyncExitStack + """Stops the services of ``context`` and restores the environment.""" + + class InitService: """Tracks dormant→ready transition and serialises /api/init calls. @@ -230,10 +250,11 @@ class InitService: one sees ``state != "dormant"`` and gets a 400. On failure mid-init the state rolls back to ``dormant`` so the orchestrator can retry. - The stores and services of the delivered config are built and started - beside the dormant :class:`ServerContext` and replace it in one assignment - once all of them are up. A failed attempt stops what it started and leaves - the dormant context in place. + An initialisation has two phases. ``_prepare`` does everything that can + fail beside the dormant :class:`ServerContext` and undoes all of it if it + does fail; ``_commit`` then publishes the result with assignments alone. + So an attempt either becomes the running server in one piece or leaves + nothing behind. """ def __init__(self, app: FastAPI, base_config: Config) -> None: @@ -242,14 +263,8 @@ def __init__(self, app: FastAPI, base_config: Config) -> None: self._state: InitState = "dormant" self._error: str | None = None self._lock = asyncio.Lock() - # The services ``initialize`` started, to be stopped by ``teardown``. + # What the committed initialisation started, for ``teardown`` to stop. self._started: AsyncExitStack | None = None - # ``initialize`` exports the delivered session key to the environment; - # ``(None, None)`` means nothing to put back. - self._session_api_key_env_before_init: tuple[bool, str | None] = ( - False, - None, - ) @property def state(self) -> InitState: @@ -275,41 +290,64 @@ async def initialize(self, req: InitRequest) -> InitStatus: self._state = "initializing" self._error = None try: - dormant = server_context_of(self._app) - new_config = _build_initialized_config(self._base_config, req) - if req.env: - # Setting env vars before services boot lets things like - # the cipher pick up OH_SECRET_KEY-style overrides, and - # tools pick up credentials. - for key, value in req.env.items(): - os.environ[key] = value - # The key this server now authenticates with must also reach the + prepared = await self._prepare(req) + except Exception as exc: # pragma: no cover - logged + re-raised + logger.exception("deferred_init: /api/init failed; rolling back to dormant") + self._error = f"{type(exc).__name__}: {exc}" + self._state = "dormant" + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail=self._error, + ) from exc + except BaseException: + # Cancelled: the attempt is unwound like a failed one, so it can + # be retried rather than leaving the server "initializing". + self._state = "dormant" + raise + self._commit(prepared) + logger.info("deferred_init: server transitioned to ready") + return self.snapshot() + + async def _prepare(self, req: InitRequest) -> _PreparedInit: + """Do every step of an initialisation that can fail; publish nothing. + + If a step fails, the two exit stacks undo the earlier ones: the + services are stopped, the environment and the telemetry sink are the + dormant server's again, and the dormant context was never replaced. + """ + # Circular: api.py builds the app this router is mounted on. + from openhands.agent_server.api import _get_root_path + + dormant = server_context_of(self._app) + new_config = _build_initialized_config(self._base_config, req) + root_path = _get_root_path(new_config) + + # ``started`` is undone on failure and otherwise kept for ``teardown``; + # ``on_failure`` only if the attempt fails, after ``started`` has + # stopped the services. + async with AsyncExitStack() as on_failure, AsyncExitStack() as started: + # ``req.env`` lets the stores resolve ``OH_PERSISTENCE_DIR`` and + # tools find credentials. The session key must also reach the # in-process callers that read it from the environment (the - # start_child_conversation tool, the managed LLM key refresh); it - # replaces whatever key the dormant process booted with, and - # ``teardown`` puts the previous value back. - self._session_api_key_env_before_init = ( - True, - os.environ.get(V1_SESSION_API_KEY_ENV), + # start_child_conversation tool, the managed LLM key refresh). + exported = {*(req.env or {}), V1_SESSION_API_KEY_ENV} + started.callback( + _restore_environ, {key: os.environ.get(key) for key in exported} ) + os.environ.update(req.env or {}) export_session_api_key(new_config) maybe_init_laminar() - # Built after ``req.env`` is applied, so the stores resolve their - # directories (``OH_PERSISTENCE_DIR``) from the delivered - # environment and encrypt with the delivered key. stores = ServerStores.from_config(new_config) - - # Must precede get_instance(), which captures the sink. The - # matching emit_server_started() is deferred until the ``ready`` - # transition below: emitting here would produce a server_started - # for an init that later fails and rolls back to dormant, leaving - # an unpaired start and letting a retry emit a second one. - await shutdown_telemetry_sink() await asyncio.to_thread(reencrypt_stale_stores, stores) - self._app.state.telemetry_sink = await build_telemetry_sink( - new_config, stores.settings + + # The conversation service reads the sink while it starts, so the + # delivered one has to be in place first. ``server_started`` is + # emitted by ``_commit``: an attempt that fails never produces one. + on_failure.push_async_callback( + self._use_telemetry_of, dormant.config, dormant.stores ) + await self._use_telemetry_of(new_config, stores) service = ConversationService.get_instance(new_config, stores) conversation_registry = create_conversation_registry(new_config, stores) @@ -317,84 +355,67 @@ async def initialize(self, req: InitRequest) -> InitStatus: bash_svc = BashEventService( bash_events_dir=new_config.bash_events_dir, cipher=new_config.cipher ) - - # Unwinds in reverse on a failure below, so a failed attempt keeps - # no half-started service; on success ``teardown`` unwinds it. - async with AsyncExitStack() as started: - await started.enter_async_context(bash_svc) - await started.enter_async_context(service) - started.push_async_callback(conversation_registry.shutdown) - await conversation_registry.start() - - # VSCode's token is the first session key, as on a normal boot. - # A dormant server that booted without one gave VSCode a random - # token, so switch it to the key delivered here. - vscode_service = dormant.vscode_service - if vscode_service is not None and new_config.session_api_keys: - await vscode_service.set_connection_token( - new_config.session_api_keys[0] - ) - - # Nothing below can fail: from this single assignment on, every - # request sees the delivered config with its own stores and - # services, and none of the dormant ones. - install_server_context( - self._app, - replace( - dormant, - config=new_config, - stores=stores, - conversation_registry=conversation_registry, - conversation_service=service, - bash_event_service=bash_svc, - ), + await started.enter_async_context(bash_svc) + await started.enter_async_context(service) + started.push_async_callback(conversation_registry.shutdown) + await conversation_registry.start() + + # VSCode's token is the first session key, as on a normal boot. + # A dormant server that booted without one gave VSCode a random + # token, so switch it to the key delivered here. Last, because a + # restarted editor cannot be put back if a later step failed. + vscode_service = dormant.vscode_service + if vscode_service is not None and new_config.session_api_keys: + await vscode_service.set_connection_token( + new_config.session_api_keys[0] ) - self._started = started.pop_all() - # Re-derive root_path from the merged config so Doc URLS are valid - from openhands.agent_server.api import _get_root_path + context = replace( + dormant, + config=new_config, + stores=stores, + conversation_registry=conversation_registry, + conversation_service=service, + bash_event_service=bash_svc, + ) + on_failure.pop_all() + return _PreparedInit( + context=context, root_path=root_path, started=started.pop_all() + ) - self._app.root_path = _get_root_path(new_config) + def _commit(self, prepared: _PreparedInit) -> None: + """Make a prepared initialisation the running server. - mark_initialization_complete() - self._state = "ready" - # Emitted only now that init has actually succeeded, so a failed - # attempt never produces a start and a retry cannot double-emit. - emit_server_started() - logger.info("deferred_init: server transitioned to ready") - return self.snapshot() - except Exception as exc: # pragma: no cover - logged + re-raised - logger.exception("deferred_init: /api/init failed; rolling back to dormant") - self._restore_session_api_key_env() - self._error = f"{type(exc).__name__}: {exc}" - self._state = "dormant" - raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=self._error, - ) from exc + Assignments only, with no ``await`` between them: it cannot fail or be + interrupted half-way, so every request sees the delivered config with + its own stores and services, or none of them. + """ + self._started = prepared.started + install_server_context(self._app, prepared.context) + # Re-derived from the merged config so Doc URLs are valid. + self._app.root_path = prepared.root_path + mark_initialization_complete() + self._state = "ready" + emit_server_started() + + async def _use_telemetry_of(self, config: Config, stores: ServerStores) -> None: + """Replace the telemetry sink with the one ``config`` asks for.""" + await shutdown_telemetry_sink() + self._app.state.telemetry_sink = await build_telemetry_sink( + config, stores.settings + ) async def teardown(self) -> None: - """Tear down the conversation service if /api/init succeeded. + """Stop what /api/init started and restore the environment it changed. - Called from the FastAPI lifespan's finally clause so dormant pods - that were never initialized don't need any cleanup. + Called from the FastAPI lifespan's finally clause; a dormant pod that + was never initialized has nothing to undo. """ started, self._started = self._started, None if started is not None: - # Registry, then the conversation service, then the bash service. + # Registry, then the conversation service, then the bash service, + # then the environment. await started.aclose() - self._restore_session_api_key_env() - - def _restore_session_api_key_env(self) -> None: - """Undo the export ``initialize`` made, if it made one.""" - exported, previous = self._session_api_key_env_before_init - if not exported: - return - self._session_api_key_env_before_init = (False, None) - if previous is None: - os.environ.pop(V1_SESSION_API_KEY_ENV, None) - else: - os.environ[V1_SESSION_API_KEY_ENV] = previous def get_init_service(request: Request) -> InitService: diff --git a/tests/agent_server/test_init_router.py b/tests/agent_server/test_init_router.py index 85a70021a9d..9e6b39bb9b5 100644 --- a/tests/agent_server/test_init_router.py +++ b/tests/agent_server/test_init_router.py @@ -278,7 +278,8 @@ def capture_observability_env(): assert observed_env == "hello" finally: await svc.teardown() - monkeypatch.delenv("DEFERRED_INIT_TEST_VAR", raising=False) + # The server that set it is gone, and so is the variable. + assert "DEFERRED_INIT_TEST_VAR" not in os.environ @pytest.mark.asyncio async def test_init_bash_service_uses_user_supplied_dir(self, tmp_path): diff --git a/tests/agent_server/test_server_context.py b/tests/agent_server/test_server_context.py index 197b4b6adb4..279eb33265d 100644 --- a/tests/agent_server/test_server_context.py +++ b/tests/agent_server/test_server_context.py @@ -12,10 +12,13 @@ from __future__ import annotations import ast +import asyncio import dataclasses import json +import os from collections.abc import Iterator from pathlib import Path +from typing import Any import pytest from fastapi import FastAPI @@ -27,11 +30,14 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService +from openhands.agent_server.init_router import InitRequest, InitService from openhands.agent_server.server_context import ( ServerContext, + build_server_context, install_server_context, server_context_of, ) +from openhands.agent_server.telemetry import NoOpTelemetrySink, get_telemetry_sink from openhands.agent_server.vscode_service import VSCodeService from openhands.sdk.utils.cipher import Cipher @@ -74,7 +80,7 @@ def _app(client: TestClient) -> FastAPI: return app -def _init(client: TestClient, tmp_path: Path): +def _init(client: TestClient, tmp_path: Path, **more: Any): """Deliver the user's key, session key and directories through /api/init.""" return client.post( "/api/init", @@ -85,6 +91,7 @@ def _init(client: TestClient, tmp_path: Path): "conversations_path": str(tmp_path / "user" / "conversations"), "bash_events_dir": str(tmp_path / "user" / "bash"), "env": {"OH_PERSISTENCE_DIR": str(tmp_path / "user" / "persistence")}, + **more, }, ) @@ -162,7 +169,13 @@ def recording_get_instance(config, stores): init_router_module.ConversationService, "get_instance", recording_get_instance ) - failed = _init(dormant, tmp_path) + monkeypatch.delenv("OH_SESSION_API_KEYS_0", raising=False) + delivered_telemetry = { + "exporter": "http", + "http_endpoint": "http://127.0.0.1:9/telemetry", + } + + failed = _init(dormant, tmp_path, telemetry=delivered_telemetry) assert failed.status_code == 500 assert dormant.get("/api/init").json()["state"] == "dormant" @@ -170,9 +183,15 @@ def recording_get_instance(config, stores): assert server_context_of(app) is before assert before.conversation_service is None assert before.config.deferred_init is True - # ... and the service the attempt started was stopped, not left running. + # ... the service the attempt started was stopped, not left running ... (attempted,) = started assert attempted._event_services is None + # ... and the process is the dormant server's again: its directory, no + # exported session key, and its telemetry sink rather than the delivered + # exporter's. + assert os.environ["OH_PERSISTENCE_DIR"] == str(tmp_path / "boot" / "persistence") + assert "OH_SESSION_API_KEYS_0" not in os.environ + assert isinstance(get_telemetry_sink(), NoOpTelemetrySink) # The dormant key still guards /api/init, so the orchestrator can retry. assert dormant.post("/api/init", json={}).status_code == 401 @@ -183,6 +202,79 @@ def recording_get_instance(config, stores): assert retried.conversation_service is started[-1] +def test_an_init_that_cannot_derive_its_root_path_changes_nothing( + dormant: TestClient, tmp_path: Path +) -> None: + """The root path is derived before anything is started or published.""" + app = _app(dormant) + before = server_context_of(app) + + failed = _init(dormant, tmp_path, web_url="http://[") + + assert failed.status_code == 500 + assert dormant.get("/api/init").json()["state"] == "dormant" + assert server_context_of(app) is before + assert os.environ["OH_PERSISTENCE_DIR"] == str(tmp_path / "boot" / "persistence") + # The bootstrap key was not replaced by the delivered one, so a corrected + # request is accepted. + assert _init(dormant, tmp_path).status_code == 200 + assert server_context_of(app).config.session_api_keys == [SESSION_KEY] + + +async def test_a_cancelled_init_can_be_retried( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("OH_PERSISTENCE_DIR", str(tmp_path / "boot" / "persistence")) + monkeypatch.delenv("CANCELLED_INIT_VAR", raising=False) + config = Config( + deferred_init=True, + conversations_path=tmp_path / "boot" / "conversations", + bash_events_dir=tmp_path / "boot" / "bash", + enable_vscode=False, + preload_tools=False, + ) + app = FastAPI() + install_server_context(app, build_server_context(config)) + before = server_context_of(app) + init_service = InitService(app, base_config=config) + request = InitRequest( + conversations_path=tmp_path / "user" / "conversations", + bash_events_dir=tmp_path / "user" / "bash", + env={"CANCELLED_INIT_VAR": "delivered"}, + ) + + hangs = True + starting = asyncio.Event() + real_start = ConversationService.__aenter__ + + async def start(self: ConversationService) -> ConversationService: + if hangs: + starting.set() + await asyncio.Event().wait() + return await real_start(self) + + monkeypatch.setattr(ConversationService, "__aenter__", start) + + attempt = asyncio.create_task(init_service.initialize(request)) + await starting.wait() + assert os.environ["CANCELLED_INIT_VAR"] == "delivered" + attempt.cancel() + with pytest.raises(asyncio.CancelledError): + await attempt + + assert init_service.state == "dormant" + assert server_context_of(app) is before + assert "CANCELLED_INIT_VAR" not in os.environ + + hangs = False + try: + assert (await init_service.initialize(request)).state == "ready" + assert server_context_of(app).conversation_service is not None + finally: + await init_service.teardown() + assert "CANCELLED_INIT_VAR" not in os.environ + + def test_the_lifespan_adds_services_to_the_context_it_was_created_with( tmp_path: Path, ) -> None: From 9cd4ffd0830a499e199b6bcbe4ae6a93b0aabed6 Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Thu, 12 Nov 2026 16:57:43 +0100 Subject: [PATCH 4/4] refactor(agent-server): routes declare their dependencies The providers in dependencies.py took a Request and were called by hand inside handler bodies and helpers (get_stores(request) in 48 places, get_cipher(request) in 16, get_config(request) in 15), so what a route depended on was not in its signature and every call read app.state again. Every provider now resolves from Depends(get_server_context), which FastAPI calls once per request or WebSocket. A route names what it needs (Depends(get_settings_store), Depends(get_cipher), ...), helpers receive the objects instead of the request, and all the dependencies of one request, including the router-level session-key check, come from the same ServerContext. There is one provider per store next to get_stores for code that needs the whole bundle. The providers declare their own inputs with Annotated, which makes them required arguments: calling one by hand with only a request is now a type error instead of leaving a Depends object where the context should be. The guard test also rejects fetching the whole context by hand outside the modules that build or swap it. No route, parameter, status code or response changes; the OpenAPI schema is byte-identical. A declared dependency is resolved before the handler body, so where a route used to look the conversation or bash service up late, "service not running" is now answered first: the event, bash and session WebSockets close with 1013 before the handshake, like the dormant gate already does, and the docker runtime and credential-binding routes return 503 before doing any work. --- .../openhands/agent_server/acp_router.py | 12 +- .../agent_server/agent_profiles_router.py | 62 ++-- .../agent_server/canvas_extensions/bridge.py | 53 +-- .../canvas_extensions_bridge_router.py | 21 +- .../agent_server/conversation_router.py | 37 +- .../agent_server/credential_binding.py | 13 +- .../openhands/agent_server/dependencies.py | 122 +++++-- .../agent_server/docker_runtime/routers.py | 122 ++++--- .../openhands/agent_server/event_router.py | 7 +- .../openhands/agent_server/file_router.py | 6 +- .../openhands/agent_server/git_router.py | 9 +- .../openhands/agent_server/init_router.py | 5 +- .../openhands/agent_server/mcp_router.py | 17 +- .../agent_server/meta_profiles_router.py | 23 +- .../openhands/agent_server/openai/router.py | 39 ++- .../openhands/agent_server/profiles_router.py | 97 ++++-- .../provider_connections_router.py | 39 ++- .../openhands/agent_server/runtime_router.py | 11 +- .../agent_server/server_details_router.py | 13 +- .../openhands/agent_server/session_socket.py | 11 +- .../openhands/agent_server/settings_router.py | 93 +++-- .../openhands/agent_server/skills_router.py | 9 +- .../openhands/agent_server/sockets.py | 16 +- .../agent_server/sub_agents_router.py | 6 +- .../openhands/agent_server/tool_router.py | 8 +- .../agent_server/workspaces_router.py | 24 +- .../test_canvas_extension_bridge_routes.py | 9 +- .../docker_runtime/test_hybrid.py | 3 + .../docker_runtime/test_routes.py | 26 +- tests/agent_server/test_api_authentication.py | 16 +- .../test_event_router_websocket.py | 326 +++++++----------- tests/agent_server/test_event_socket_races.py | 21 +- tests/agent_server/test_init_router.py | 2 +- tests/agent_server/test_server_context.py | 68 +++- .../test_sockets_service_getters.py | 67 ++-- .../test_websocket_first_message_auth.py | 233 ++++++------- .../test_remote_conversation_live_server.py | 4 +- 37 files changed, 962 insertions(+), 688 deletions(-) diff --git a/openhands-agent-server/openhands/agent_server/acp_router.py b/openhands-agent-server/openhands/agent_server/acp_router.py index 5a3abea9498..53d2c2a3069 100644 --- a/openhands-agent-server/openhands/agent_server/acp_router.py +++ b/openhands-agent-server/openhands/agent_server/acp_router.py @@ -9,12 +9,12 @@ from collections.abc import Awaitable, Callable, Mapping from dataclasses import dataclass, field -from fastapi import APIRouter, HTTPException, Request, status +from fastapi import APIRouter, Depends, HTTPException, Request, status from pydantic import BaseModel, Field from openhands.agent_server.config import Config from openhands.agent_server.credential_binding import LocalVersionedCredentialBinding -from openhands.agent_server.dependencies import get_config, get_stores +from openhands.agent_server.dependencies import get_config, get_secrets_store from openhands.agent_server.persistence import FileSecretsStore from openhands.sdk.agent.acp_file_credentials import CODEX_AUTH_SECRET_NAME from openhands.sdk.agent.acp_model_discovery import discover_acp_models @@ -147,7 +147,10 @@ def _codex_bindings( @acp_router.post("/models", response_model=ACPModelDiscovery) async def discover_models( - body: ACPModelDiscoveryRequest, request: Request + body: ACPModelDiscoveryRequest, + request: Request, + config: Config = Depends(get_config), + secrets_store: FileSecretsStore = Depends(get_secrets_store), ) -> ACPModelDiscovery: """Start the ACP server in a throwaway session and report its models. @@ -155,7 +158,6 @@ async def discover_models( given credentials; a launch or authentication failure comes back in ``error`` rather than as an HTTP error. """ - config: Config = get_config(request) if config.conversation_runtime == "docker": raise HTTPException( status.HTTP_501_NOT_IMPLEMENTED, @@ -169,7 +171,7 @@ async def discover_models( secrets = await asyncio.to_thread(_resolve_secrets, body.secrets) bindings = await asyncio.to_thread( - _codex_bindings, settings, get_stores(request).secrets, secrets + _codex_bindings, settings, secrets_store, secrets ) key = _cache_key(settings, command, secrets) diff --git a/openhands-agent-server/openhands/agent_server/agent_profiles_router.py b/openhands-agent-server/openhands/agent_server/agent_profiles_router.py index 55c48ff8758..0b223a64e3b 100644 --- a/openhands-agent-server/openhands/agent_server/agent_profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/agent_profiles_router.py @@ -15,17 +15,28 @@ import asyncio from typing import Annotated, Any -from fastapi import APIRouter, HTTPException, Path, Request, status +from fastapi import APIRouter, Depends, HTTPException, Path, status from pydantic import BaseModel, ConfigDict, Field, PlainValidator, ValidationError from openhands.agent_server._secrets_exposure import store_errors -from openhands.agent_server.dependencies import get_cipher, get_config, get_stores +from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import ( + get_agent_profile_store, + get_cipher, + get_config, + get_settings_store, + get_stores, +) from openhands.agent_server.launch import ( can_probe_tools, server_launch_stores, target_launch_runtime, ) -from openhands.agent_server.persistence import PersistedSettings, ServerStores +from openhands.agent_server.persistence import ( + FileSettingsStore, + PersistedSettings, + ServerStores, +) from openhands.agent_server.profiles_router import MAX_PROFILES, _has_api_key from openhands.sdk.launch import preview_launch from openhands.sdk.llm import LLM @@ -327,18 +338,20 @@ def _summary_id_for_name(store: AgentProfileStore, name: str) -> str | None: @agent_profiles_router.get("", response_model=AgentProfileListResponse) -def list_agent_profiles(request: Request) -> AgentProfileListResponse: +def list_agent_profiles( + stores: ServerStores = Depends(get_stores), + cipher: Cipher | None = Depends(get_cipher), +) -> AgentProfileListResponse: """List all stored agent profiles and the active pointer. Some profile is always active: with no valid pointer, ``default`` is activated, and seeded from the current ``agent_settings`` when absent. """ - stores = get_stores(request) settings_store = stores.settings settings = settings_store.load() or PersistedSettings() store = stores.agent_profiles - _ensure_active_profile(stores, get_cipher(request)) + _ensure_active_profile(stores, cipher) settings = settings_store.load() or settings with store_errors(): @@ -352,7 +365,8 @@ def list_agent_profiles(request: Request) -> AgentProfileListResponse: @agent_profiles_router.get("/{name}", response_model=AgentProfileDetailResponse) def get_agent_profile( - request: Request, name: ProfileName + name: ProfileName, + store: AgentProfileStore = Depends(get_agent_profile_store), ) -> AgentProfileDetailResponse: """Get a stored profile. @@ -360,7 +374,6 @@ def get_agent_profile( expose — unlike the LLM ``/api/profiles`` router, this endpoint has no ``X-Expose-Secrets`` behavior. """ - store = get_stores(request).agent_profiles try: with store_errors(): profile = store.load(name) @@ -403,7 +416,9 @@ def _validate_profile_payload( status_code=status.HTTP_201_CREATED, ) def save_agent_profile( - request: Request, name: ProfileName, body: dict[str, Any] + name: ProfileName, + body: dict[str, Any], + store: AgentProfileStore = Depends(get_agent_profile_store), ) -> AgentProfileMutationResponse: """Save an ``AgentProfile`` under ``name`` (overwriting a namesake). @@ -414,7 +429,6 @@ def save_agent_profile( """ profile = _validate_profile_payload({**body, "name": name}) - store = get_stores(request).agent_profiles # The id is server-managed (the active pointer is keyed on it): overwrite # keeps the namesake's id and bumps revision; create mints a fresh id, # ignoring any client-supplied one. ``save_profile_preserving_identity`` @@ -442,7 +456,9 @@ def save_agent_profile( @agent_profiles_router.delete("/{name}", response_model=AgentProfileMutationResponse) def delete_agent_profile( - request: Request, name: ProfileName + name: ProfileName, + stores: ServerStores = Depends(get_stores), + cipher: Cipher | None = Depends(get_cipher), ) -> AgentProfileMutationResponse: """Delete a stored profile (idempotent). @@ -452,7 +468,6 @@ def delete_agent_profile( reset to default OpenHands agent settings so stale ACP configuration does not outlive the profile it came from; server-wide settings are carried over. """ - stores = get_stores(request) store = stores.agent_profiles deleted_id = _summary_id_for_name(store, name) @@ -481,7 +496,7 @@ def clear_pointer_and_reset_settings( settings_store.update(clear_pointer_and_reset_settings) logger.info(f"Cleared active pointer for deleted profile '{name}'") - _ensure_active_profile(stores, get_cipher(request)) + _ensure_active_profile(stores, cipher) logger.info(f"Deleted agent profile '{name}'") return AgentProfileMutationResponse( @@ -493,7 +508,9 @@ def clear_pointer_and_reset_settings( "/{name}/rename", response_model=AgentProfileMutationResponse ) def rename_agent_profile( - request: Request, name: ProfileName, body: RenameAgentProfileRequest + name: ProfileName, + body: RenameAgentProfileRequest, + store: AgentProfileStore = Depends(get_agent_profile_store), ) -> AgentProfileMutationResponse: """Rename a stored profile atomically. @@ -501,7 +518,6 @@ def rename_agent_profile( survives the rename untouched. Returns 404 if the source is missing, 409 if ``new_name`` is taken. """ - store = get_stores(request).agent_profiles try: with store_errors(): store.rename(name, body.new_name) @@ -528,7 +544,9 @@ def rename_agent_profile( "/{profile_id}/activate", response_model=ActivateAgentProfileResponse ) def activate_agent_profile( - request: Request, profile_id: ProfileId + profile_id: ProfileId, + store: AgentProfileStore = Depends(get_agent_profile_store), + settings_store: FileSettingsStore = Depends(get_settings_store), ) -> ActivateAgentProfileResponse: """Activate a profile by its stable ``id`` — pointer only. @@ -536,7 +554,6 @@ def activate_agent_profile( ``/activate``, this does **not** write ``agent_settings`` (the creation-time-only contract). Returns 404 if no stored profile has that id. """ - store = get_stores(request).agent_profiles with store_errors(): known_ids = { str(s["id"]) for s in store.list_summaries() if s.get("id") is not None @@ -547,8 +564,6 @@ def activate_agent_profile( detail=f"Agent profile with id '{profile_id}' not found", ) - settings_store = get_stores(request).settings - def set_pointer(settings: PersistedSettings) -> PersistedSettings: settings.active_agent_profile_id = profile_id return settings @@ -579,9 +594,11 @@ def set_pointer(settings: PersistedSettings) -> PersistedSettings: response_model=AgentProfileDiagnostics, ) async def materialize_agent_profile( - request: Request, name: ProfileName, body: MaterializeAgentProfileRequest | None = None, + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), + cipher: Cipher | None = Depends(get_cipher), ) -> AgentProfileDiagnostics: """Preview the launch of a profile; return a diagnostics report. @@ -595,7 +612,7 @@ async def materialize_agent_profile( if body is not None and body.profile is not None: profile = _validate_profile_payload({**body.profile, "name": name}) else: - store = get_stores(request).agent_profiles + store = stores.agent_profiles try: with store_errors(): # Off the event loop: the store lock wait blocks (up to 30 s). @@ -606,9 +623,6 @@ async def materialize_agent_profile( detail=f"Agent profile '{name}' not found", ) - config = get_config(request) - stores = get_stores(request) - cipher = get_cipher(request) settings = stores.settings.load() or PersistedSettings() context = settings.agent_settings.agent_context return await asyncio.to_thread( diff --git a/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py b/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py index a5d9c0de7b0..69a3672bf5f 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py @@ -23,7 +23,6 @@ _append_partitioned_to_last_set_cookie, ) from openhands.agent_server.config import Config -from openhands.agent_server.dependencies import get_config from openhands.agent_server.docker_runtime.proxy import ( bridge_websocket, proxy_http, @@ -273,7 +272,7 @@ def _allowed_control_origin(config: Config, origin: str) -> bool: ) -def _request_origin(request: Request | WebSocket) -> str | None: +def _request_origin(request: Request | WebSocket, config: Config) -> str | None: """Derive the request origin from the connection the server actually saw. ``X-Forwarded-Proto``/``X-Forwarded-Host`` are client-controllable, so they @@ -281,7 +280,7 @@ def _request_origin(request: Request | WebSocket) -> str | None: proxy terminates TLS in front of this server. Otherwise the request's own scheme and ``Host`` decide — the same inputs a spoofing client cannot fake. """ - if get_config(request).trust_forwarded_headers: + if config.trust_forwarded_headers: forwarded_proto = request.headers.get("x-forwarded-proto", "") scheme = forwarded_proto.split(",")[0].strip() or request.url.scheme forwarded_host = request.headers.get("x-forwarded-host", "") @@ -293,18 +292,22 @@ def _request_origin(request: Request | WebSocket) -> str | None: return _origin(f"{scheme}://{host}") -def _require_ingress_host(request: Request | WebSocket, public_url: str) -> None: - if _request_origin(request) != _origin(public_url): +def _require_ingress_host( + request: Request | WebSocket, public_url: str, config: Config +) -> None: + if _request_origin(request, config) != _origin(public_url): raise HTTPException( status.HTTP_421_MISDIRECTED_REQUEST, "Canvas App request must use the configured ingress origin", ) -def _require_bootstrap_origin(request: Request, public_url: str) -> None: - _require_ingress_host(request, public_url) +def _require_bootstrap_origin( + request: Request, public_url: str, config: Config +) -> None: + _require_ingress_host(request, public_url, config) origin = request.headers.get("origin", "") - if not _allowed_control_origin(get_config(request), origin): + if not _allowed_control_origin(config, origin): raise HTTPException(status.HTTP_403_FORBIDDEN, "Origin is not allowed") if _origin(origin) == _origin(public_url): raise HTTPException( @@ -372,14 +375,14 @@ def _cookie_path(extension_name: str) -> str: return f"{_APP_BACKEND_PATH}/{extension_name}" -def _session_cookie_is_secure(request: Request) -> bool: +def _session_cookie_is_secure(request: Request, config: Config) -> bool: """Whether a ``Secure`` app-session cookie can be issued for this request. Uses :func:`_request_origin`, so it makes the same non-spoofable trusted-proxy decision as the ingress checks rather than reading ``X-Forwarded-*`` unconditionally. """ - origin = _request_origin(request) + origin = _request_origin(request, config) if origin is None: return False parsed = urlparse(origin) @@ -394,8 +397,9 @@ def _set_session_cookie( extension_name: str, token: str, max_age: int, + config: Config, ) -> None: - if not _session_cookie_is_secure(request): + if not _session_cookie_is_secure(request, config): raise HTTPException( status.HTTP_503_SERVICE_UNAVAILABLE, "Canvas App sessions require HTTPS or a loopback secure context", @@ -413,10 +417,10 @@ def _set_session_cookie( async def create_app_backend_session( - request: Request, response: Response, extension_name: str + request: Request, response: Response, extension_name: str, config: Config ) -> AppBackendSessionResponse: - public_url = _public_url(get_config(request)) - _require_bootstrap_origin(request, public_url) + public_url = _public_url(config) + _require_bootstrap_origin(request, public_url, config) endpoint = _ready_endpoint(request, extension_name) token, session = await get_app_backend_session_store(request).create( extension_name, endpoint @@ -427,6 +431,7 @@ async def create_app_backend_session( extension_name, token, APP_BACKEND_SESSION_TTL_SECONDS, + config, ) return AppBackendSessionResponse( ingress_url=f"{public_url}{_cookie_path(extension_name)}/", @@ -435,21 +440,21 @@ async def create_app_backend_session( async def delete_app_backend_session( - request: Request, response: Response, extension_name: str + request: Request, response: Response, extension_name: str, config: Config ) -> None: - public_url = _public_url(get_config(request)) - _require_bootstrap_origin(request, public_url) + public_url = _public_url(config) + _require_bootstrap_origin(request, public_url, config) token = request.cookies.get(APP_BACKEND_SESSION_COOKIE_NAME) await get_app_backend_session_store(request).revoke_token(token) - _set_session_cookie(response, request, extension_name, "", 0) + _set_session_cookie(response, request, extension_name, "", 0, config) async def proxy_app_backend_http( - request: Request, extension_name: str, path: str + request: Request, extension_name: str, path: str, config: Config ) -> Response: _validate_proxy_path(path) - public_url = _public_url(get_config(request)) - _require_ingress_host(request, public_url) + public_url = _public_url(config) + _require_ingress_host(request, public_url, config) if request.method not in _SAFE_METHODS: _require_app_origin(request.headers.get("origin"), public_url) endpoint = _ready_endpoint(request, extension_name) @@ -483,12 +488,12 @@ async def proxy_app_backend_http( async def proxy_app_backend_websocket( - websocket: WebSocket, extension_name: str, path: str + websocket: WebSocket, extension_name: str, path: str, config: Config ) -> None: try: _validate_proxy_path(path) - public_url = _public_url(get_config(websocket)) - _require_ingress_host(websocket, public_url) + public_url = _public_url(config) + _require_ingress_host(websocket, public_url, config) _require_app_origin(websocket.headers.get("origin"), public_url) endpoint = _ready_endpoint(websocket, extension_name) token = websocket.cookies.get(APP_BACKEND_SESSION_COOKIE_NAME) diff --git a/openhands-agent-server/openhands/agent_server/canvas_extensions_bridge_router.py b/openhands-agent-server/openhands/agent_server/canvas_extensions_bridge_router.py index 2b16348eb9c..78de9ada0f5 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions_bridge_router.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions_bridge_router.py @@ -14,7 +14,8 @@ from openhands.agent_server.canvas_extensions_router import ( CANVAS_EXTENSION_NAME_PATTERN, ) -from openhands.agent_server.dependencies import check_session_api_key +from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import check_session_api_key, get_config app_backend_bridge_router = APIRouter(tags=["Canvas App Backends"]) @@ -44,6 +45,7 @@ async def create_app_backend_session_endpoint( extension_name: AppBackendNamePath, request: Request, response: Response, + config: Config = Depends(get_config), ) -> AppBackendSessionResponse: """Issue a short-lived HttpOnly session for one ready app backend. @@ -53,7 +55,7 @@ async def create_app_backend_session_endpoint( (``credentials: include``); third-party iframe deployments require HTTPS and browser support for partitioned cookies. """ - return await create_app_backend_session(request, response, extension_name) + return await create_app_backend_session(request, response, extension_name, config) @app_backend_bridge_router.delete( @@ -70,9 +72,10 @@ async def delete_app_backend_session_endpoint( extension_name: AppBackendNamePath, request: Request, response: Response, + config: Config = Depends(get_config), ) -> Response: """Revoke the current app-scoped browser session.""" - await delete_app_backend_session(request, response, extension_name) + await delete_app_backend_session(request, response, extension_name, config) response.status_code = 204 return response @@ -87,8 +90,9 @@ async def delete_app_backend_session_endpoint( async def proxy_app_backend_http_root_endpoint( extension_name: AppBackendNamePath, request: Request, + config: Config = Depends(get_config), ) -> Response: - return await proxy_app_backend_http(request, extension_name, "") + return await proxy_app_backend_http(request, extension_name, "", config) @app_backend_bridge_router.api_route( @@ -100,16 +104,18 @@ async def proxy_app_backend_http_endpoint( extension_name: AppBackendNamePath, request: Request, path: str, + config: Config = Depends(get_config), ) -> Response: - return await proxy_app_backend_http(request, extension_name, path) + return await proxy_app_backend_http(request, extension_name, path, config) @app_backend_bridge_router.websocket("/app-backends/{extension_name}") async def proxy_app_backend_websocket_root_endpoint( extension_name: AppBackendNamePath, websocket: WebSocket, + config: Config = Depends(get_config), ) -> None: - await proxy_app_backend_websocket(websocket, extension_name, "") + await proxy_app_backend_websocket(websocket, extension_name, "", config) @app_backend_bridge_router.websocket("/app-backends/{extension_name}/{path:path}") @@ -117,5 +123,6 @@ async def proxy_app_backend_websocket_endpoint( extension_name: AppBackendNamePath, websocket: WebSocket, path: str, + config: Config = Depends(get_config), ) -> None: - await proxy_app_backend_websocket(websocket, extension_name, path) + await proxy_app_backend_websocket(websocket, extension_name, path, config) diff --git a/openhands-agent-server/openhands/agent_server/conversation_router.py b/openhands-agent-server/openhands/agent_server/conversation_router.py index 3496051cf91..79bce44d361 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_router.py +++ b/openhands-agent-server/openhands/agent_server/conversation_router.py @@ -11,7 +11,6 @@ Header, HTTPException, Query, - Request, Response, status, ) @@ -21,6 +20,7 @@ decrypt_incoming_llm_secrets, store_errors, ) +from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ( ConversationService, InvalidParentConversation, @@ -30,7 +30,7 @@ get_cipher, get_conversation_registry, get_conversation_service, - get_stores, + get_llm_profile_store, ) from openhands.agent_server.event_service import CondensationUnavailable from openhands.agent_server.launch import launch_http_exception @@ -64,6 +64,7 @@ ConversationObservabilityTags, ) from openhands.sdk.launch import AgentLaunchError, LaunchStoreError +from openhands.sdk.llm.llm_profile_store import LLMProfileStore from openhands.sdk.marketplace.registry import ( MarketplaceNotFoundError, PluginNotFoundError, @@ -72,6 +73,7 @@ from openhands.sdk.plugin import PluginFetchError from openhands.sdk.profiles.resolver import ProfileNotFound from openhands.sdk.tool.client_tool import ClientToolRegistrationError +from openhands.sdk.utils.cipher import Cipher conversation_catalog_router = APIRouter(prefix="/conversations", tags=["Conversations"]) @@ -148,9 +150,8 @@ def _request_with_observability_headers( def _with_runtime_info( - request: Request, conversation: ConversationInfo + registry: ConversationRegistry, conversation: ConversationInfo ) -> ConversationInfo: - registry = get_conversation_registry(request) return conversation.model_copy( update={"runtime_info": registry.runtime_info(conversation.id)} ) @@ -159,7 +160,6 @@ def _with_runtime_info( @conversation_catalog_router.get("/search", include_in_schema=False) @conversation_router.get("/search") async def search_conversations( - request: Request, page_id: Annotated[ str | None, Query(title="Optional next_page_id from the previously returned page"), @@ -178,6 +178,7 @@ async def search_conversations( ] = ConversationSortOrder.CREATED_AT_DESC, include_skills: Annotated[bool, Query(title=INCLUDE_SKILLS_PARAM_TITLE)] = False, conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> ConversationPage: """Search / List conversations""" page = await conversation_service.search_conversations( @@ -196,7 +197,7 @@ async def search_conversations( } ) page = page.model_copy( - update={"items": [_with_runtime_info(request, item) for item in page.items]} + update={"items": [_with_runtime_info(registry, item) for item in page.items]} ) return page @@ -220,9 +221,9 @@ async def count_conversations( ) async def get_conversation( conversation_id: UUID, - request: Request, include_skills: Annotated[bool, Query(title=INCLUDE_SKILLS_PARAM_TITLE)] = False, conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> ConversationInfo: """Given an id, get a conversation""" conversation = await conversation_service.get_conversation(conversation_id) @@ -230,30 +231,30 @@ async def get_conversation( raise HTTPException(status.HTTP_404_NOT_FOUND) if not include_skills: conversation = trim_conversation_response_skills(conversation) - return _with_runtime_info(request, conversation) + return _with_runtime_info(registry, conversation) @conversation_router.get("/{conversation_id}/runtime") async def get_local_conversation_runtime( conversation_id: UUID, - request: Request, conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> ConversationRuntimeInfo: """Inspect the always-available in-process runtime.""" if await conversation_service.get_conversation(conversation_id) is None: raise HTTPException(status.HTTP_404_NOT_FOUND) - return get_conversation_registry(request).runtime_info(conversation_id) + return registry.runtime_info(conversation_id) @conversation_router.post("/{conversation_id}/runtime/reprovision") async def reprovision_local_conversation_runtime( conversation_id: UUID, - request: Request, conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> ConversationRuntimeInfo: """Return local runtime state; local mode has no infrastructure to provision.""" return await get_local_conversation_runtime( - conversation_id, request, conversation_service + conversation_id, conversation_service, registry ) @@ -280,16 +281,16 @@ async def get_conversation_agent_final_response( @conversation_router.get("") async def batch_get_conversations( - request: Request, ids: Annotated[list[UUID], Query(max_length=99)], include_skills: Annotated[bool, Query(title=INCLUDE_SKILLS_PARAM_TITLE)] = False, conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> list[ConversationInfo | None]: """Get a batch of up to 99 conversations given their ids, returning null for any missing item""" conversations = await conversation_service.batch_get_conversations(ids) conversations = [ - _with_runtime_info(request, conversation) if conversation is not None else None + _with_runtime_info(registry, conversation) if conversation is not None else None for conversation in conversations ] if not include_skills: @@ -623,10 +624,11 @@ async def switch_conversation_profile( }, ) async def switch_conversation_llm( - request: Request, conversation_id: UUID, llm: LLM = Body(..., embed=True), # noqa: B008 conversation_service: ConversationService = Depends(get_conversation_service), + cipher: Cipher | None = Depends(get_cipher), + llm_profile_store: LLMProfileStore = Depends(get_llm_profile_store), ) -> Success: """Swap the conversation's LLM to a caller-supplied object. @@ -640,7 +642,6 @@ async def switch_conversation_llm( if event_service is None: raise HTTPException(status.HTTP_404_NOT_FOUND) conversation = event_service.get_conversation() - cipher = get_cipher(request) if cipher is not None: llm = decrypt_incoming_llm_secrets(llm, cipher) # Resolve a referenced provider connection before installing the LLM, so a @@ -650,9 +651,7 @@ async def switch_conversation_llm( # is replaced. Inline-key configs without a provider_connection_id are # byte-identical to the old path. with store_errors(): - llm = get_stores(request).llm_profiles.resolve_provider_connection( - llm, cipher=cipher - ) + llm = llm_profile_store.resolve_provider_connection(llm, cipher=cipher) try: conversation.switch_llm(llm) except ValueError as e: diff --git a/openhands-agent-server/openhands/agent_server/credential_binding.py b/openhands-agent-server/openhands/agent_server/credential_binding.py index 6e2187ed868..9ff2546120b 100644 --- a/openhands-agent-server/openhands/agent_server/credential_binding.py +++ b/openhands-agent-server/openhands/agent_server/credential_binding.py @@ -5,9 +5,10 @@ from typing import Final from uuid import UUID -from fastapi import APIRouter, HTTPException, Request, Response, status +from fastapi import APIRouter, Depends, HTTPException, Response, status from pydantic import BaseModel, ConfigDict, Field, field_validator +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import get_conversation_service from openhands.agent_server.event_service import CredentialBindingActivationTooLate from openhands.agent_server.persistence import FileSecretsStore @@ -106,8 +107,10 @@ async def _probe_binding(binding: HttpVersionedCredentialBinding) -> None: @router.post("/prepare-for-sandbox-pause", include_in_schema=False) -async def prepare_for_sandbox_pause(request: Request) -> Response: - await get_conversation_service(request).prepare_for_sandbox_pause() +async def prepare_for_sandbox_pause( + conversation_service: ConversationService = Depends(get_conversation_service), +) -> Response: + await conversation_service.prepare_for_sandbox_pause() return Response(status_code=status.HTTP_204_NO_CONTENT) @@ -119,7 +122,7 @@ async def activate_credential_binding( conversation_id: UUID, secret_name: str, activation: CredentialBindingActivation, - request: Request, + conversation_service: ConversationService = Depends(get_conversation_service), ) -> Response: if not supports_file_credential_binding(secret_name): raise HTTPException(status.HTTP_404_NOT_FOUND) @@ -129,7 +132,7 @@ async def activate_credential_binding( ) try: await _probe_binding(binding) - await get_conversation_service(request).activate_credential_binding( + await conversation_service.activate_credential_binding( conversation_id, secret_name, binding, diff --git a/openhands-agent-server/openhands/agent_server/dependencies.py b/openhands-agent-server/openhands/agent_server/dependencies.py index c55cd19d44f..0090427d2bf 100644 --- a/openhands-agent-server/openhands/agent_server/dependencies.py +++ b/openhands-agent-server/openhands/agent_server/dependencies.py @@ -1,3 +1,15 @@ +"""What a route can ask the server for. + +A route names what it needs in its signature, ``Depends(get_settings_store)`` +for example, and FastAPI hands it over. Every provider here resolves from +``get_server_context``, which FastAPI calls once per request or WebSocket, so +all the dependencies of one request come from the same +:class:`~openhands.agent_server.server_context.ServerContext`, including the +router-level ones such as ``check_session_api_key``. A test replaces any of +them through ``app.dependency_overrides``. +""" + +from typing import Annotated from uuid import UUID from fastapi import Depends, HTTPException, Request, WebSocketException, status @@ -11,9 +23,17 @@ from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.event_service import EventService -from openhands.agent_server.persistence import ServerStores -from openhands.agent_server.server_context import get_server_context +from openhands.agent_server.persistence import ( + FileSecretsStore, + FileSettingsStore, + FileWorkspacesStore, + ProviderConnectionStore, + ServerStores, +) +from openhands.agent_server.server_context import ServerContext, get_server_context from openhands.agent_server.vscode_service import VSCodeService +from openhands.sdk.llm.llm_profile_store import LLMProfileStore +from openhands.sdk.profiles.agent_profile_store import AgentProfileStore from openhands.sdk.utils.cipher import Cipher @@ -29,33 +49,70 @@ ) -def get_config(connection: HTTPConnection) -> Config: +# Declared with ``Annotated`` so that they are required arguments: calling a +# provider by hand with only a request does not type-check, instead of leaving +# a ``Depends`` object where the context should be. +_Context = Annotated[ServerContext, Depends(get_server_context)] + + +def get_config(context: _Context) -> Config: """The config this server currently runs with. Read from the server context at request time, so the config delivered via ``POST /api/init`` takes effect immediately without restarting the server or re-registering routes. """ - return get_server_context(connection).config + return context.config -def get_cipher(connection: HTTPConnection) -> Cipher | None: +def get_cipher(config: Annotated[Config, Depends(get_config)]) -> Cipher | None: """The configured cipher (``None`` when ``OH_SECRET_KEY`` is unset).""" - return get_server_context(connection).config.cipher + return config.cipher + + +def get_stores(context: _Context) -> ServerStores: + """The persistent stores built for this server's current config. + + For code that works across several stores; a route that uses one store + asks for that store. + """ + return context.stores + + +_Stores = Annotated[ServerStores, Depends(get_stores)] + + +def get_settings_store(stores: _Stores) -> FileSettingsStore: + return stores.settings + + +def get_secrets_store(stores: _Stores) -> FileSecretsStore: + return stores.secrets + + +def get_provider_connections_store(stores: _Stores) -> ProviderConnectionStore: + return stores.provider_connections + + +def get_workspaces_store(stores: _Stores) -> FileWorkspacesStore: + return stores.workspaces + + +def get_llm_profile_store(stores: _Stores) -> LLMProfileStore: + return stores.llm_profiles -def get_stores(connection: HTTPConnection) -> ServerStores: - """The persistent stores built for this server's current config.""" - return get_server_context(connection).stores +def get_agent_profile_store(stores: _Stores) -> AgentProfileStore: + return stores.agent_profiles -def get_conversation_registry(connection: HTTPConnection) -> ConversationRegistry: - return get_server_context(connection).conversation_registry +def get_conversation_registry(context: _Context) -> ConversationRegistry: + return context.conversation_registry -def get_vscode_service(connection: HTTPConnection) -> VSCodeService | None: +def get_vscode_service(context: _Context) -> VSCodeService | None: """This server's VSCode service (``None`` when VSCode is disabled).""" - return get_server_context(connection).vscode_service + return context.vscode_service def _unavailable(connection: HTTPConnection, detail: str) -> Exception: @@ -66,19 +123,18 @@ def _unavailable(connection: HTTPConnection, detail: str) -> Exception: def check_session_api_key( - request: Request, - session_api_key: str | None = Depends(_SESSION_API_KEY_HEADER), + config: Annotated[Config, Depends(get_config)], + session_api_key: Annotated[str | None, Depends(_SESSION_API_KEY_HEADER)], ) -> None: """Reject the request if the supplied key is not in the current session keys.""" - config = get_config(request) if config.session_api_keys and session_api_key not in config.session_api_keys: raise HTTPException(status.HTTP_401_UNAUTHORIZED) def check_workspace_session( - request: Request, - header_key: str | None = Depends(_SESSION_API_KEY_HEADER), - cookie_key: str | None = Depends(_WORKSPACE_SESSION_COOKIE), + config: Annotated[Config, Depends(get_config)], + header_key: Annotated[str | None, Depends(_SESSION_API_KEY_HEADER)], + cookie_key: Annotated[str | None, Depends(_WORKSPACE_SESSION_COOKIE)], ) -> None: """Auth dependency for the workspace static-file routes. @@ -90,7 +146,6 @@ def check_workspace_session( frontend embeds workspace artifacts. The cookie is deliberately scoped to this router only; no other endpoint honors it. """ - config = get_config(request) if not config.session_api_keys: return for candidate in (header_key, cookie_key): @@ -99,22 +154,28 @@ def check_workspace_session( raise HTTPException(status.HTTP_401_UNAUTHORIZED) -def get_conversation_service(connection: HTTPConnection) -> ConversationService: - service = get_server_context(connection).conversation_service +def get_conversation_service( + connection: HTTPConnection, context: _Context +) -> ConversationService: + service = context.conversation_service if service is None: raise _unavailable(connection, "Conversation service is not available") return service -def get_server_bash_event_service(connection: HTTPConnection) -> BashEventService: +def get_server_bash_event_service( + connection: HTTPConnection, context: _Context +) -> BashEventService: """The server-wide bash service (not a conversation runtime's own).""" - service = get_server_context(connection).bash_event_service + service = context.bash_event_service if service is None: raise _unavailable(connection, "Bash event service is not available") return service -async def get_bash_event_service(request: Request) -> BashEventService: +async def get_bash_event_service( + request: Request, context: _Context +) -> BashEventService: if "runtime_conversation_id" in request.path_params: event_service: EventService | None = getattr( request.state, "runtime_event_service", None @@ -144,15 +205,18 @@ async def get_bash_event_service(request: Request) -> BashEventService: cipher=event_service.cipher, ) return event_service.bash_event_service - return get_server_bash_event_service(request) + # Looked up only here: a runtime's own bash service does not need the + # server-wide one to be running. + return get_server_bash_event_service(request, context) async def get_event_service( conversation_id: UUID, - request: Request, - conversation_service: ConversationService = Depends(get_conversation_service), + conversation_service: Annotated[ + ConversationService, Depends(get_conversation_service) + ], + registry: Annotated[ConversationRegistry, Depends(get_conversation_registry)], ) -> EventService: - registry = get_conversation_registry(request) if registry.serves_persisted_event_reads(conversation_id): event_service = await conversation_service.get_persisted_event_service( conversation_id diff --git a/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py b/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py index 8cfe5268f63..16ac5cafdc5 100644 --- a/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py +++ b/openhands-agent-server/openhands/agent_server/docker_runtime/routers.py @@ -11,10 +11,14 @@ from uuid import UUID, uuid4 import httpx -from fastapi import APIRouter, HTTPException, Query, Request, WebSocket +from fastapi import APIRouter, Depends, HTTPException, Query, Request, WebSocket from starlette.responses import JSONResponse, Response, StreamingResponse +from openhands.agent_server.config import Config +from openhands.agent_server.conversation_registry import ConversationRegistry +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( + get_config, get_conversation_registry, get_conversation_service, ) @@ -58,8 +62,7 @@ logger = get_logger(__name__) -def get_registry(request: Request) -> DockerConversationRegistry: - registry = get_conversation_registry(request) +def get_registry(registry: ConversationRegistry) -> DockerConversationRegistry: if not isinstance(registry, DockerConversationRegistry): raise HTTPException(503, "Docker conversation runtime is unavailable") return registry @@ -135,6 +138,8 @@ async def detach() -> None: async def start_conversation( request: Request, include_skills: Annotated[bool, Query()] = False, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), ) -> JSONResponse: try: body = json.loads(await request.body()) @@ -144,13 +149,11 @@ async def start_conversation( raise HTTPException(422, "Expected a JSON object") status_code, content, conversation_id = await start_docker_conversation( body, - get_registry(request), + get_registry(conversation_registry), include_skills=include_skills, ) if status_code < 400: - await get_conversation_service(request).refresh_persisted_conversation( - conversation_id - ) + await conversation_service.refresh_persisted_conversation(conversation_id) return JSONResponse(content=content, status_code=status_code) @@ -323,18 +326,22 @@ def payload(cipher: Cipher) -> dict[str, Any]: "/{conversation_id}/runtime", response_model=ConversationRuntimeInfo ) async def runtime_info( - conversation_id: UUID, request: Request + conversation_id: UUID, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), ) -> ConversationRuntimeInfo: - registry = get_registry(request) + registry = get_registry(conversation_registry) if not registry.conversation_dir(conversation_id).joinpath("meta.json").is_file(): raise HTTPException(404, "Conversation not found") return registry.runtime_info(conversation_id) @docker_conversation_router.post("/{conversation_id}/runtime/credentials") -async def runtime_credentials(conversation_id: UUID, request: Request) -> JSONResponse: +async def runtime_credentials( + conversation_id: UUID, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), +) -> JSONResponse: """Return the key for an authenticated client attached to this runtime.""" - registry = get_registry(request) + registry = get_registry(conversation_registry) if not registry.conversation_dir(conversation_id).joinpath("meta.json").is_file(): raise HTTPException(404, "Conversation not found") identity = registry.provisioning.load(conversation_id) @@ -342,16 +349,18 @@ async def runtime_credentials(conversation_id: UUID, request: Request) -> JSONRe @docker_conversation_router.delete("/{conversation_id}/runtime", status_code=204) -async def release_runtime(conversation_id: UUID, request: Request) -> Response: +async def release_runtime( + conversation_id: UUID, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), +) -> Response: """Stop the container while retaining conversation and workspace state.""" - registry = get_registry(request) + registry = get_registry(conversation_registry) if not registry.conversation_dir(conversation_id).joinpath("meta.json").is_file(): raise HTTPException(404, "Conversation not found") try: await registry.stop(conversation_id) - await get_conversation_service(request).refresh_persisted_conversation( - conversation_id - ) + await conversation_service.refresh_persisted_conversation(conversation_id) except Exception as exc: logger.exception("Could not release conversation runtime %s", conversation_id) raise HTTPException(502, "Could not release conversation runtime") from exc @@ -362,9 +371,10 @@ async def release_runtime(conversation_id: UUID, request: Request) -> Response: "/{conversation_id}/runtime/reprovision", response_model=ConversationRuntimeInfo ) async def reprovision_runtime( - conversation_id: UUID, request: Request + conversation_id: UUID, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), ) -> ConversationRuntimeInfo: - registry = get_registry(request) + registry = get_registry(conversation_registry) await _container(registry, conversation_id) return ConversationRuntimeInfo( runtime_status=ConversationRuntimeStatus.AVAILABLE, can_resume=True @@ -372,8 +382,12 @@ async def reprovision_runtime( @docker_conversation_router.delete("/{conversation_id}") -async def delete_conversation(conversation_id: UUID, request: Request) -> Response: - registry = get_registry(request) +async def delete_conversation( + conversation_id: UUID, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), +) -> Response: + registry = get_registry(conversation_registry) if not registry.provisioning.manifest_path(conversation_id).is_file(): raise HTTPException(404, "Conversation not found") @@ -398,9 +412,7 @@ async def delete_conversation(conversation_id: UUID, request: Request) -> Respon await asyncio.to_thread(safe_rmtree, registry.conversation_dir(conversation_id)) registry.provisioning.manifest_path(conversation_id).unlink(missing_ok=True) registry.archived_marker(conversation_id).unlink(missing_ok=True) - await get_conversation_service(request).refresh_persisted_conversation( - conversation_id - ) + await conversation_service.refresh_persisted_conversation(conversation_id) finally: await registry.finish_delete(conversation_id) return Response(status_code=200) @@ -411,9 +423,14 @@ async def delete_conversation(conversation_id: UUID, request: Request) -> Respon methods=["POST", "PUT", "PATCH", "OPTIONS"], ) async def proxy_conversation_root( - conversation_id: UUID, request: Request + conversation_id: UUID, + request: Request, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), ) -> StreamingResponse: - return await proxy_conversation(conversation_id, "", request) + return await proxy_conversation( + conversation_id, "", request, conversation_registry, conversation_service + ) @docker_conversation_router.api_route( @@ -421,7 +438,11 @@ async def proxy_conversation_root( methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"], ) async def proxy_conversation( - conversation_id: UUID, tail: str, request: Request + conversation_id: UUID, + tail: str, + request: Request, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), ) -> StreamingResponse: if tail.split("/", 1)[0] in { "credential-bindings", @@ -430,7 +451,7 @@ async def proxy_conversation( "switch_profile", }: raise HTTPException(501, "This operation is unavailable in Docker runtime mode") - registry = get_registry(request) + registry = get_registry(conversation_registry) container = await _container(registry, conversation_id) upstream_path = _upstream_path( request, @@ -470,9 +491,7 @@ async def proxy_conversation( upstream_path=upstream_path, ) if request.method not in {"GET", "HEAD", "OPTIONS"} and response.status_code < 400: - await get_conversation_service(request).refresh_persisted_conversation( - conversation_id - ) + await conversation_service.refresh_persisted_conversation(conversation_id) return response @@ -481,9 +500,12 @@ async def proxy_conversation( @docker_workspace_router.get("/{conversation_id}/workspace/{file_path:path}") async def proxy_workspace_file( - conversation_id: UUID, file_path: str, request: Request + conversation_id: UUID, + file_path: str, + request: Request, + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), ) -> StreamingResponse: - registry = get_registry(request) + registry = get_registry(conversation_registry) container = await _container(registry, conversation_id) return await _proxy_with_session( registry, @@ -505,12 +527,14 @@ async def _proxy_socket( conversation_id: UUID, session_api_key: str | None, socket_name: str, + config: Config, + registry: ConversationRegistry, + conversation_service: ConversationService, ) -> None: from openhands.agent_server.sockets import _accept_authenticated_websocket - if not await _accept_authenticated_websocket(websocket, session_api_key): + if not await _accept_authenticated_websocket(websocket, session_api_key, config): return - registry = get_conversation_registry(websocket) if not isinstance(registry, DockerConversationRegistry): await websocket.close(code=1011) return @@ -535,9 +559,7 @@ async def _proxy_socket( if release is not None: release() registry.detach_session(conversation_id) - await get_conversation_service(websocket).refresh_persisted_conversation( - conversation_id - ) + await conversation_service.refresh_persisted_conversation(conversation_id) @docker_sockets_router.websocket("/events/{conversation_id}") @@ -545,8 +567,19 @@ async def proxy_events( websocket: WebSocket, conversation_id: UUID, session_api_key: Annotated[str | None, Query(alias="session_api_key")] = None, + config: Config = Depends(get_config), + registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), ) -> None: - await _proxy_socket(websocket, conversation_id, session_api_key, "events") + await _proxy_socket( + websocket, + conversation_id, + session_api_key, + "events", + config, + registry, + conversation_service, + ) @docker_session_sockets_router.websocket("/session/{conversation_id}") @@ -554,5 +587,16 @@ async def proxy_session( websocket: WebSocket, conversation_id: UUID, session_api_key: Annotated[str | None, Query(alias="session_api_key")] = None, + config: Config = Depends(get_config), + registry: ConversationRegistry = Depends(get_conversation_registry), + conversation_service: ConversationService = Depends(get_conversation_service), ) -> None: - await _proxy_socket(websocket, conversation_id, session_api_key, "session") + await _proxy_socket( + websocket, + conversation_id, + session_api_key, + "session", + config, + registry, + conversation_service, + ) diff --git a/openhands-agent-server/openhands/agent_server/event_router.py b/openhands-agent-server/openhands/agent_server/event_router.py index 6f1070fc03b..8eb213295a0 100644 --- a/openhands-agent-server/openhands/agent_server/event_router.py +++ b/openhands-agent-server/openhands/agent_server/event_router.py @@ -16,11 +16,12 @@ Query, status, ) -from starlette.requests import Request from starlette.responses import JSONResponse +from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( + get_conversation_registry, get_conversation_service, get_event_service, ) @@ -241,12 +242,12 @@ async def send_message( async def _gateway_context( conversation_id: UUID, - http_request: Request, conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> tuple[EventService, float]: deadline = time.monotonic() + _GATEWAY_RUN_TIMEOUT_SECONDS event_service = await get_event_service( - conversation_id, http_request, conversation_service + conversation_id, conversation_service, registry ) return event_service, deadline diff --git a/openhands-agent-server/openhands/agent_server/file_router.py b/openhands-agent-server/openhands/agent_server/file_router.py index abd28f10259..219883f1a06 100644 --- a/openhands-agent-server/openhands/agent_server/file_router.py +++ b/openhands-agent-server/openhands/agent_server/file_router.py @@ -16,10 +16,10 @@ from fastapi import ( APIRouter, + Depends, File, HTTPException, Query, - Request, UploadFile, status, ) @@ -31,6 +31,7 @@ redacted_json_bytes, should_redact, ) +from openhands.agent_server.config import Config from openhands.agent_server.dependencies import get_config from openhands.agent_server.models import FileUploadResult, Success from openhands.agent_server.server_details_router import update_last_execution_time @@ -941,10 +942,9 @@ async def search_subdirs( ) async def download_trajectory( conversation_id: UUID, - request: Request, + config: Config = Depends(get_config), ) -> FileResponse: """Download a zip archive of a conversation trajectory.""" - config = get_config(request) temp_file = config.conversations_path / f"{conversation_id.hex}.zip" conversation_dir = config.conversations_path / conversation_id.hex diff --git a/openhands-agent-server/openhands/agent_server/git_router.py b/openhands-agent-server/openhands/agent_server/git_router.py index 0e301e58777..427d00100e5 100644 --- a/openhands-agent-server/openhands/agent_server/git_router.py +++ b/openhands-agent-server/openhands/agent_server/git_router.py @@ -6,14 +6,15 @@ from pathlib import Path from typing import Annotated -from fastapi import APIRouter, HTTPException, Path as PathParam, Query, Request +from fastapi import APIRouter, Depends, HTTPException, Path as PathParam, Query -from openhands.agent_server.dependencies import get_stores +from openhands.agent_server.dependencies import get_secrets_store from openhands.agent_server.git_provider_service import ( GitProviderAPIError, UnsupportedGitProviderError, search_provider_repositories, ) +from openhands.agent_server.persistence import FileSecretsStore from openhands.agent_server.server_details_router import update_last_execution_time from openhands.sdk.git.exceptions import GitError, GitRepositoryError from openhands.sdk.git.git_changes import get_git_changes @@ -135,7 +136,6 @@ async def _get_commit_file_diff(path: str, commit: str) -> GitDiff: @git_router.get("/repositories/search", response_model=GitProviderRepositoryPage) async def git_repositories_search( - request: Request, provider: Annotated[GitProvider, Query(description="Git provider to search")], query: Annotated[ str | None, Query(description="Optional repository name filter") @@ -146,11 +146,12 @@ async def git_repositories_search( page_id: Annotated[ str | None, Query(description="Provider-specific page cursor") ] = None, + secrets_store: FileSecretsStore = Depends(get_secrets_store), ) -> GitProviderRepositoryPage: """List repositories accessible to a configured git provider token.""" try: return await search_provider_repositories( - get_stores(request).secrets, + secrets_store, provider, query=query, limit=limit, diff --git a/openhands-agent-server/openhands/agent_server/init_router.py b/openhands-agent-server/openhands/agent_server/init_router.py index 454710aeeb0..f73b44ce90f 100644 --- a/openhands-agent-server/openhands/agent_server/init_router.py +++ b/openhands-agent-server/openhands/agent_server/init_router.py @@ -44,10 +44,10 @@ create_conversation_registry, ) from openhands.agent_server.conversation_service import ConversationService +from openhands.agent_server.dependencies import get_config from openhands.agent_server.persistence import ServerStores, reencrypt_stale_stores from openhands.agent_server.server_context import ( ServerContext, - get_server_context, install_server_context, server_context_of, ) @@ -432,14 +432,13 @@ def get_init_service(request: Request) -> InitService: def check_init_api_key( - request: Request, + config: Config = Depends(get_config), init_api_key: str | None = Depends(_INIT_API_KEY_HEADER), ) -> None: """Auth gate for /api/init. Uses the dormant server's ``secret_key`` as the bootstrap credential — the orchestrator already holds it because it is required for encryption. The key is replaced when /api/init delivers the per-user runtime config.""" - config = get_server_context(request).config if config.secret_key is None: if config.secret_key_was_blank: # A blank key was configured but no usable key remains → fail diff --git a/openhands-agent-server/openhands/agent_server/mcp_router.py b/openhands-agent-server/openhands/agent_server/mcp_router.py index 6a1b03f5a17..a06e8f392ca 100644 --- a/openhands-agent-server/openhands/agent_server/mcp_router.py +++ b/openhands-agent-server/openhands/agent_server/mcp_router.py @@ -35,14 +35,15 @@ import httpx import mcp.types -from fastapi import APIRouter, HTTPException, Request +from fastapi import APIRouter, Depends, HTTPException from fastmcp.client.auth.oauth import ClientNotFoundError, OAuth from pydantic import BaseModel, Field, ValidationError, model_validator -from openhands.agent_server.dependencies import get_cipher, get_stores +from openhands.agent_server.dependencies import get_cipher, get_secrets_store from openhands.agent_server.mcp_oauth_store import ( InMemoryMCPOAuthTokenStore, ) +from openhands.agent_server.persistence import FileSecretsStore from openhands.sdk.logger import get_logger from openhands.sdk.mcp import create_mcp_tools from openhands.sdk.mcp.client import MCPClient @@ -820,13 +821,11 @@ def _probe_with_secret_references( ), ) async def test_mcp_server( - request: MCPTestRequest, http_request: Request + request: MCPTestRequest, + cipher: Cipher | None = Depends(get_cipher), + secrets_store: FileSecretsStore = Depends(get_secrets_store), ) -> MCPTestResponse: """Probe a single MCP server config and report whether it works.""" - # Resolve the cipher and store here: the threadpool function below must - # not reach back into the request's server context. - cipher = get_cipher(http_request) - secrets_store = get_stores(http_request).secrets loop = asyncio.get_running_loop() return await loop.run_in_executor( None, @@ -880,7 +879,7 @@ def _get_oauth_job(job_id: str) -> _MCPOAuthProbeJob: summary="Start an MCP OAuth install probe", ) async def start_mcp_oauth( - request: MCPTestRequest, http_request: Request + request: MCPTestRequest, cipher: Cipher | None = Depends(get_cipher) ) -> MCPOAuthStartResponse: """Start OAuth for a candidate MCP server and return the authorization URL.""" if request.resolved_server.oauth_auth is None: @@ -889,7 +888,7 @@ async def start_mcp_oauth( detail="MCP OAuth start requires auth.strategy='oauth2'", ) - job = _MCPOAuthProbeJob(request=request, cipher=get_cipher(http_request)) + job = _MCPOAuthProbeJob(request=request, cipher=cipher) _register_oauth_job(job) thread = threading.Thread( target=_run_oauth_probe_job, diff --git a/openhands-agent-server/openhands/agent_server/meta_profiles_router.py b/openhands-agent-server/openhands/agent_server/meta_profiles_router.py index f7fb905f8e7..1e037d0e856 100644 --- a/openhands-agent-server/openhands/agent_server/meta_profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/meta_profiles_router.py @@ -8,11 +8,11 @@ from contextlib import contextmanager from typing import Annotated -from fastapi import APIRouter, HTTPException, Path, Request, status +from fastapi import APIRouter, Depends, HTTPException, Path, status from pydantic import AfterValidator, BaseModel -from openhands.agent_server.dependencies import get_stores -from openhands.agent_server.persistence import PersistedSettings +from openhands.agent_server.dependencies import get_settings_store +from openhands.agent_server.persistence import FileSettingsStore, PersistedSettings from openhands.sdk.llm.llm_profile_store import PROFILE_NAME_PATTERN from openhands.sdk.llm.meta_profile_store import ( MetaProfile, @@ -112,9 +112,8 @@ def _store_errors() -> Iterator[None]: def _set_active_meta_profile_if_matches( - request: Request, old_name: str, new_name: str | None + settings_store: FileSettingsStore, old_name: str, new_name: str | None ) -> bool: - settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() if settings.active_meta_profile != old_name: return False @@ -138,9 +137,10 @@ def update_active(settings: PersistedSettings) -> PersistedSettings: @meta_profiles_router.get("", response_model=MetaProfileListResponse) -def list_meta_profiles(request: Request) -> MetaProfileListResponse: +def list_meta_profiles( + settings_store: FileSettingsStore = Depends(get_settings_store), +) -> MetaProfileListResponse: """List all saved meta-profiles and the currently active one.""" - settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() store = _get_meta_profile_store() @@ -204,7 +204,8 @@ def save_meta_profile( @meta_profiles_router.delete("/{name}", response_model=MetaProfileMutationResponse) def delete_meta_profile( - request: Request, name: MetaProfileName + name: MetaProfileName, + settings_store: FileSettingsStore = Depends(get_settings_store), ) -> MetaProfileMutationResponse: """Delete a meta-profile (idempotent). @@ -214,7 +215,7 @@ def delete_meta_profile( store = _get_meta_profile_store() with _store_errors(): store.delete(name) - if _set_active_meta_profile_if_matches(request, name, None): + if _set_active_meta_profile_if_matches(settings_store, name, None): logger.info(f"Cleared active_meta_profile for deleted meta-profile '{name}'") logger.info(f"Deleted meta-profile '{name}'") return MetaProfileMutationResponse( @@ -226,7 +227,8 @@ def delete_meta_profile( "/{name}/activate", response_model=ActivateMetaProfileResponse ) def activate_meta_profile( - request: Request, name: MetaProfileName + name: MetaProfileName, + settings_store: FileSettingsStore = Depends(get_settings_store), ) -> ActivateMetaProfileResponse: """Activate a meta-profile by recording it as ``active_meta_profile``. @@ -248,7 +250,6 @@ def activate_meta_profile( detail=f"Meta-profile '{name}' not found", ) - settings_store = get_stores(request).settings applied = False def apply_active(settings: PersistedSettings) -> PersistedSettings: diff --git a/openhands-agent-server/openhands/agent_server/openai/router.py b/openhands-agent-server/openhands/agent_server/openai/router.py index de7347bba76..027a9cf56b0 100644 --- a/openhands-agent-server/openhands/agent_server/openai/router.py +++ b/openhands-agent-server/openhands/agent_server/openai/router.py @@ -4,11 +4,13 @@ from typing import Annotated from uuid import UUID -from fastapi import APIRouter, Depends, Header, HTTPException, Request, Response, status +from fastapi import APIRouter, Depends, Header, HTTPException, Response, status from fastapi.responses import StreamingResponse from fastapi.security import APIKeyHeader, HTTPAuthorizationCredentials, HTTPBearer from pydantic import TypeAdapter, ValidationError +from openhands.agent_server.config import Config +from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( get_config, @@ -30,6 +32,7 @@ run_chat_completion, run_response, ) +from openhands.agent_server.persistence import ServerStores from openhands.sdk.conversation.types import ( ConversationObservabilityMetadata, ConversationObservabilitySpanName, @@ -47,7 +50,7 @@ def check_openai_api_key( - request: Request, + config: Config = Depends(get_config), session_api_key: str | None = Depends(_SESSION_API_KEY_HEADER), authorization: HTTPAuthorizationCredentials | None = Depends(_AUTHORIZATION_HEADER), ) -> None: @@ -63,7 +66,6 @@ def check_openai_api_key( Reads the config at request time so that keys delivered via ``POST /api/init`` take effect immediately. """ - config = get_config(request) if not config.session_api_keys: return bearer_token = authorization.credentials if authorization else None @@ -74,8 +76,9 @@ def check_openai_api_key( raise HTTPException(status.HTTP_401_UNAUTHORIZED) -def _get_docker_registry(request: Request) -> DockerConversationRegistry | None: - registry = get_conversation_registry(request) +def _get_docker_registry( + registry: ConversationRegistry, +) -> DockerConversationRegistry | None: return registry if isinstance(registry, DockerConversationRegistry) else None @@ -118,8 +121,10 @@ def _parse_observability_overrides( @openai_router.get("/v1/models", response_model=OpenAIModelListResponse) -async def get_openai_models(request: Request) -> OpenAIModelListResponse: - return await list_openai_models(get_stores(request)) +async def get_openai_models( + stores: ServerStores = Depends(get_stores), +) -> OpenAIModelListResponse: + return await list_openai_models(stores) @openai_router.post( @@ -129,7 +134,6 @@ async def get_openai_models(request: Request) -> OpenAIModelListResponse: ) async def create_chat_completion( body: OpenAIChatCompletionRequest, - request: Request, response: Response, x_openhands_server_conversation_id: Annotated[ UUID | None, Header(alias="X-OpenHands-ServerConversation-ID") @@ -147,14 +151,17 @@ async def create_chat_completion( str | None, Header(alias="X-OpenHands-Observability-Parent-Span-Context") ] = None, conversation_service: ConversationService = Depends(get_conversation_service), + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), ) -> OpenAIChatCompletionResponse | StreamingResponse: result = await run_chat_completion( request=body.model_copy(update={"stream": False}) if body.stream else body, - config=get_config(request), - stores=get_stores(request), + config=config, + stores=stores, conversation_service=conversation_service, reusable_conversation_id=x_openhands_server_conversation_id, - registry=_get_docker_registry(request), + registry=_get_docker_registry(conversation_registry), observability_overrides=_parse_observability_overrides( span_name=x_openhands_observability_span_name, tags=x_openhands_observability_tags, @@ -187,7 +194,6 @@ async def create_chat_completion( ) async def create_response( body: OpenAIResponseRequest, - request: Request, response: Response, x_openhands_observability_span_name: Annotated[ str | None, Header(alias="X-OpenHands-Observability-Span-Name") @@ -202,13 +208,16 @@ async def create_response( str | None, Header(alias="X-OpenHands-Observability-Parent-Span-Context") ] = None, conversation_service: ConversationService = Depends(get_conversation_service), + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), + conversation_registry: ConversationRegistry = Depends(get_conversation_registry), ) -> OpenAIResponse: result = await run_response( request=body, - config=get_config(request), - stores=get_stores(request), + config=config, + stores=stores, conversation_service=conversation_service, - registry=_get_docker_registry(request), + registry=_get_docker_registry(conversation_registry), observability_overrides=_parse_observability_overrides( span_name=x_openhands_observability_span_name, tags=x_openhands_observability_tags, diff --git a/openhands-agent-server/openhands/agent_server/profiles_router.py b/openhands-agent-server/openhands/agent_server/profiles_router.py index 3d8059af981..715c5743c91 100644 --- a/openhands-agent-server/openhands/agent_server/profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/profiles_router.py @@ -3,7 +3,7 @@ import asyncio from typing import Annotated, Any -from fastapi import APIRouter, HTTPException, Path, Request, status +from fastapi import APIRouter, Depends, HTTPException, Path, Request, status from pydantic import BaseModel, Field, SecretStr from openhands.agent_server._secrets_exposure import ( @@ -14,8 +14,18 @@ store_errors, translate_missing_cipher, ) -from openhands.agent_server.dependencies import get_cipher, get_stores -from openhands.agent_server.persistence import PersistedSettings +from openhands.agent_server.dependencies import ( + get_agent_profile_store, + get_cipher, + get_llm_profile_store, + get_provider_connections_store, + get_settings_store, +) +from openhands.agent_server.persistence import ( + FileSettingsStore, + PersistedSettings, + ProviderConnectionStore, +) from openhands.sdk.llm import LLM, Message, TextContent from openhands.sdk.llm.exceptions import ( LLMError, @@ -26,6 +36,7 @@ ) from openhands.sdk.llm.llm_profile_store import ( PROFILE_NAME_PATTERN, + LLMProfileStore, ProfileLimitExceeded, canonical_profile_name, ) @@ -35,7 +46,9 @@ delete_llm_profile, rename_llm_profile, ) +from openhands.sdk.profiles.agent_profile_store import AgentProfileStore from openhands.sdk.settings import OpenHandsAgentSettings +from openhands.sdk.utils.cipher import Cipher from openhands.sdk.utils.pydantic_secrets import SecretDecryptionError from openhands.sdk.utils.redact import redact_text_secrets @@ -102,7 +115,9 @@ def _has_api_key(llm: LLM) -> bool: return bool(llm.api_key.get_secret_value().strip()) -def _profile_api_key_set(request: Request, llm: LLM) -> bool: +def _profile_api_key_set( + provider_connections: ProviderConnectionStore, cipher: Cipher | None, llm: LLM +) -> bool: """Effective key presence: the profile's own key, or its provider's. A profile linked to a provider connection carries no inline key (cleared on @@ -113,20 +128,16 @@ def _profile_api_key_set(request: Request, llm: LLM) -> bool: connection_id = llm.provider_connection_id if not connection_id: return False - cipher = get_cipher(request) # The provider store read can raise on a corrupted file; map it instead of # letting it surface as an unhandled 500 on GET /profiles/{name}. with store_errors(): - connection = get_stores(request).provider_connections.get( - connection_id, cipher=cipher - ) + connection = provider_connections.get(connection_id, cipher=cipher) return connection is not None and connection.api_key_value() is not None def _set_active_profile_if_matches( - request: Request, old_name: str, new_name: str | None + settings_store: FileSettingsStore, old_name: str, new_name: str | None ) -> bool: - settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() active = settings.active_profile # ``foo.json`` addresses profile ``foo`` in the store, so compare store keys. @@ -146,17 +157,18 @@ def update_active(settings: PersistedSettings) -> PersistedSettings: @profiles_router.get("", response_model=ProfileListResponse) -def list_profiles(request: Request) -> ProfileListResponse: +def list_profiles( + settings_store: FileSettingsStore = Depends(get_settings_store), + store: LLMProfileStore = Depends(get_llm_profile_store), +) -> ProfileListResponse: """List all saved LLM profiles. Returns the list of profiles along with the currently active profile name, if one has been activated. The active_profile tracks which LLM profile configuration is currently in use. """ - settings_store = get_stores(request).settings settings = settings_store.load() or PersistedSettings() - store = get_stores(request).llm_profiles with store_errors(): summaries = store.list_summaries() @@ -167,7 +179,15 @@ def list_profiles(request: Request) -> ProfileListResponse: @profiles_router.get("/{name}", response_model=ProfileDetailResponse) -def get_profile(request: Request, name: ProfileName) -> ProfileDetailResponse: +def get_profile( + request: Request, + name: ProfileName, + store: LLMProfileStore = Depends(get_llm_profile_store), + provider_connections: ProviderConnectionStore = Depends( + get_provider_connections_store + ), + cipher: Cipher | None = Depends(get_cipher), +) -> ProfileDetailResponse: """Get a profile's configuration. Use the ``X-Expose-Secrets`` header to control secret exposure: @@ -177,9 +197,7 @@ def get_profile(request: Request, name: ProfileName) -> ProfileDetailResponse: - (absent): Returns nulled ``api_key`` with ``api_key_set`` indicator """ expose_mode = parse_expose_secrets_header(request) - cipher = get_cipher(request) - store = get_stores(request).llm_profiles try: with store_errors(): # Runtime reads need current provider credentials. Editor reads @@ -202,7 +220,9 @@ def get_profile(request: Request, name: ProfileName) -> ProfileDetailResponse: config["api_key"] = None return ProfileDetailResponse( - name=name, config=config, api_key_set=_profile_api_key_set(request, llm) + name=name, + config=config, + api_key_set=_profile_api_key_set(provider_connections, cipher, llm), ) @@ -212,9 +232,10 @@ def get_profile(request: Request, name: ProfileName) -> ProfileDetailResponse: status_code=status.HTTP_201_CREATED, ) def save_profile( - request: Request, name: ProfileName, body: SaveProfileRequest, + store: LLMProfileStore = Depends(get_llm_profile_store), + cipher: Cipher | None = Depends(get_cipher), ) -> ProfileMutationResponse: """Save an LLM configuration as a named profile. @@ -227,9 +248,7 @@ def save_profile( Clients can submit cipher-encrypted secrets which will be decrypted server-side before re-encrypting with the storage cipher. """ - cipher = get_cipher(request) llm = decrypt_incoming_llm_secrets(body.llm, cipher) if cipher else body.llm - store = get_stores(request).llm_profiles try: with store_errors(): store.save( @@ -317,9 +336,10 @@ def _invalid(name: str, exc: LLMError) -> ValidateProfileResponse: response_model=ValidateProfileResponse, ) async def validate_profile( - request: Request, name: ProfileName, body: ValidateProfileRequest, + store: LLMProfileStore = Depends(get_llm_profile_store), + cipher: Cipher | None = Depends(get_cipher), ) -> ValidateProfileResponse: """Pre-flight check: fire a minimal LLM completion to catch misconfigurations. @@ -336,15 +356,12 @@ async def validate_profile( misconfigured profile. A 429 that reports an exhausted budget or quota is not transient and fails the validation. """ - cipher = get_cipher(request) llm = decrypt_incoming_llm_secrets(body.llm, cipher) if cipher else body.llm # Check a draft linked to a provider connection with the connection's # credentials, as activation and ``switch_llm`` run it. No-op without # ``provider_connection_id``; a dangling link is a 422 naming it. with store_errors(): - llm = get_stores(request).llm_profiles.resolve_provider_connection( - llm, cipher=cipher - ) + llm = store.resolve_provider_connection(llm, cipher=cipher) messages = [ Message( @@ -416,20 +433,23 @@ async def validate_profile( @profiles_router.delete("/{name}", response_model=ProfileMutationResponse) -def delete_profile(request: Request, name: ProfileName) -> ProfileMutationResponse: +def delete_profile( + name: ProfileName, + store: LLMProfileStore = Depends(get_llm_profile_store), + agent_store: AgentProfileStore = Depends(get_agent_profile_store), + settings_store: FileSettingsStore = Depends(get_settings_store), +) -> ProfileMutationResponse: """Delete a saved profile (idempotent). Guarded by the agent-profile FK: returns 409 naming the referrers if any ``AgentProfile`` still cites this LLM profile via ``llm_profile_ref``. """ - store = get_stores(request).llm_profiles - agent_store = get_stores(request).agent_profiles try: with store_errors(): delete_llm_profile(agent_store, store, name) except ProfileReferenced as e: raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(e)) - if _set_active_profile_if_matches(request, name, None): + if _set_active_profile_if_matches(settings_store, name, None): logger.info(f"Cleared active_profile for deleted profile '{name}'") logger.info(f"Deleted profile '{name}'") return ProfileMutationResponse(name=name, message=f"Profile '{name}' deleted") @@ -437,9 +457,11 @@ def delete_profile(request: Request, name: ProfileName) -> ProfileMutationRespon @profiles_router.post("/{name}/rename", response_model=ProfileMutationResponse) def rename_profile( - request: Request, name: ProfileName, body: RenameProfileRequest, + store: LLMProfileStore = Depends(get_llm_profile_store), + agent_store: AgentProfileStore = Depends(get_agent_profile_store), + settings_store: FileSettingsStore = Depends(get_settings_store), ) -> ProfileMutationResponse: """Rename a saved profile atomically. @@ -450,8 +472,6 @@ def rename_profile( setting is updated to the new name. Any ``AgentProfile.llm_profile_ref`` citing the old name is cascaded to the new name in lock-step. """ - store = get_stores(request).llm_profiles - agent_store = get_stores(request).agent_profiles try: with store_errors(): rename_llm_profile(agent_store, store, name, body.new_name) @@ -467,7 +487,7 @@ def rename_profile( ) if name != body.new_name and _set_active_profile_if_matches( - request, name, body.new_name + settings_store, name, body.new_name ): logger.info(f"Updated active_profile from '{name}' to '{body.new_name}'") @@ -488,7 +508,12 @@ class ActivateProfileResponse(BaseModel): @profiles_router.post("/{name}/activate", response_model=ActivateProfileResponse) -def activate_profile(request: Request, name: ProfileName) -> ActivateProfileResponse: +def activate_profile( + name: ProfileName, + profile_store: LLMProfileStore = Depends(get_llm_profile_store), + settings_store: FileSettingsStore = Depends(get_settings_store), + cipher: Cipher | None = Depends(get_cipher), +) -> ActivateProfileResponse: """Activate a saved LLM profile. This endpoint: @@ -504,13 +529,10 @@ def activate_profile(request: Request, name: ProfileName) -> ActivateProfileResp Use ``GET /api/profiles`` to see which profile is currently active via the ``active_profile`` field. """ - cipher = get_cipher(request) - # Load the profile. ``load`` resolves any referenced provider connection # (read-at-use), so the LLM applied to settings already carries the shared # api_key / base_url; ``provider_connection_id`` is retained so a later # rotation re-resolves on the next activation or launch. - profile_store = get_stores(request).llm_profiles # A dangling provider_connection_id raises ProviderConnectionNotFound, which # store_errors() maps to 422. try: @@ -524,7 +546,6 @@ def activate_profile(request: Request, name: ProfileName) -> ActivateProfileResp detail=f"Profile '{name}' not found", ) - settings_store = get_stores(request).settings llm_applied = False def apply_profile(settings: PersistedSettings) -> PersistedSettings: diff --git a/openhands-agent-server/openhands/agent_server/provider_connections_router.py b/openhands-agent-server/openhands/agent_server/provider_connections_router.py index 9e87be5d936..067a10a6afc 100644 --- a/openhands-agent-server/openhands/agent_server/provider_connections_router.py +++ b/openhands-agent-server/openhands/agent_server/provider_connections_router.py @@ -15,7 +15,7 @@ import uuid from typing import Any -from fastapi import APIRouter, HTTPException, Request, status +from fastapi import APIRouter, Depends, HTTPException, status from pydantic import ( BaseModel, ConfigDict, @@ -26,14 +26,23 @@ ) from openhands.agent_server._secrets_exposure import store_errors -from openhands.agent_server.dependencies import get_cipher, get_stores -from openhands.agent_server.persistence import ProviderConnection, ServerStores +from openhands.agent_server.dependencies import ( + get_cipher, + get_provider_connections_store, + get_stores, +) +from openhands.agent_server.persistence import ( + ProviderConnection, + ProviderConnectionStore, + ServerStores, +) from openhands.sdk.llm.provider_connection_store import ( ProviderConnectionLimitExceeded, ProviderConnectionNotFound, ) from openhands.sdk.logger import get_logger from openhands.sdk.settings import OpenHandsAgentSettings +from openhands.sdk.utils.cipher import Cipher from openhands.sdk.utils.pydantic_secrets import is_redacted_secret @@ -161,10 +170,9 @@ def _raise_if_connection_is_referenced( @provider_connections_router.get("", response_model=list[ProviderConnectionResponse]) def list_provider_connections( - request: Request, + store: ProviderConnectionStore = Depends(get_provider_connections_store), + cipher: Cipher | None = Depends(get_cipher), ) -> list[ProviderConnectionResponse]: - cipher = get_cipher(request) - store = get_stores(request).provider_connections with store_errors(): connections = store.list(cipher=cipher) return [_to_response(c) for c in connections] @@ -174,10 +182,10 @@ def list_provider_connections( "", response_model=ProviderConnectionResponse, status_code=status.HTTP_201_CREATED ) def create_provider_connection( - request: Request, body: ProviderConnectionCreateRequest + body: ProviderConnectionCreateRequest, + store: ProviderConnectionStore = Depends(get_provider_connections_store), + cipher: Cipher | None = Depends(get_cipher), ) -> ProviderConnectionResponse: - cipher = get_cipher(request) - store = get_stores(request).provider_connections now = _now() connection = ProviderConnection( id=uuid.uuid4().hex, @@ -206,7 +214,10 @@ def create_provider_connection( "/{connection_id}", response_model=ProviderConnectionResponse ) def update_provider_connection( - request: Request, connection_id: str, body: ProviderConnectionUpdateRequest + connection_id: str, + body: ProviderConnectionUpdateRequest, + store: ProviderConnectionStore = Depends(get_provider_connections_store), + cipher: Cipher | None = Depends(get_cipher), ) -> ProviderConnectionResponse: fields = body.model_fields_set if not fields: @@ -215,8 +226,6 @@ def update_provider_connection( detail="Provide at least one provider connection field to update", ) - cipher = get_cipher(request) - store = get_stores(request).provider_connections with store_errors(): connection = store.get(connection_id, cipher=cipher) if connection is None: @@ -263,10 +272,10 @@ def update_provider_connection( "/{connection_id}", response_model=ProviderConnectionResponse ) def delete_provider_connection( - request: Request, connection_id: str + connection_id: str, + stores: ServerStores = Depends(get_stores), + cipher: Cipher | None = Depends(get_cipher), ) -> ProviderConnectionResponse: - cipher = get_cipher(request) - stores = get_stores(request) store = stores.provider_connections with store_errors(): connection = store.get(connection_id, cipher=cipher) diff --git a/openhands-agent-server/openhands/agent_server/runtime_router.py b/openhands-agent-server/openhands/agent_server/runtime_router.py index 37e5d9153d9..cf49fc43301 100644 --- a/openhands-agent-server/openhands/agent_server/runtime_router.py +++ b/openhands-agent-server/openhands/agent_server/runtime_router.py @@ -15,7 +15,10 @@ from fastapi.routing import APIRoute from openhands.agent_server.bash_router import bash_router +from openhands.agent_server.conversation_registry import ConversationRegistry +from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( + get_conversation_registry, get_conversation_service, get_event_service, get_vscode_service, @@ -31,11 +34,13 @@ async def bind_local_conversation_runtime( - runtime_conversation_id: UUID, request: Request + runtime_conversation_id: UUID, + request: Request, + conversation_service: ConversationService = Depends(get_conversation_service), + registry: ConversationRegistry = Depends(get_conversation_registry), ) -> None: - conversation_service = get_conversation_service(request) event_service = await get_event_service( - runtime_conversation_id, request, conversation_service + runtime_conversation_id, conversation_service, registry ) if event_service is None: raise HTTPException(404, "Conversation not found") diff --git a/openhands-agent-server/openhands/agent_server/server_details_router.py b/openhands-agent-server/openhands/agent_server/server_details_router.py index 3fbe008be6e..6a61ec56915 100644 --- a/openhands-agent-server/openhands/agent_server/server_details_router.py +++ b/openhands-agent-server/openhands/agent_server/server_details_router.py @@ -5,11 +5,14 @@ from importlib.metadata import version from typing import Literal -from fastapi import APIRouter, Request, Response +from fastapi import APIRouter, Depends, Response from pydantic import BaseModel, Field from openhands.agent_server.launch import configured_browser_available -from openhands.agent_server.server_context import get_server_context +from openhands.agent_server.server_context import ( + ServerContext, + get_server_context, +) from openhands.sdk.settings.model import ConversationRuntime from openhands.sdk.tool import BROWSER_TOOL_NAME from openhands.sdk.tool.registry import list_registered_tools, list_usable_tools @@ -183,8 +186,10 @@ def build_server_info( @server_details_router.get("/server_info") -async def get_server_info(request: Request) -> ServerInfo: - config = get_server_context(request).config +async def get_server_info( + context: ServerContext = Depends(get_server_context), +) -> ServerInfo: + config = context.config return build_server_info( config.conversation_runtime, app_backend_ingress_url=config.app_backend_public_url, diff --git a/openhands-agent-server/openhands/agent_server/session_socket.py b/openhands-agent-server/openhands/agent_server/session_socket.py index 5a51f4adfb5..46197872fbb 100644 --- a/openhands-agent-server/openhands/agent_server/session_socket.py +++ b/openhands-agent-server/openhands/agent_server/session_socket.py @@ -15,13 +15,15 @@ from typing import Annotated, Final from uuid import UUID -from fastapi import APIRouter, Query, WebSocket, WebSocketDisconnect +from fastapi import APIRouter, Depends, Query, WebSocket, WebSocketDisconnect from pydantic import ValidationError +from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ( + ConversationService, CredentialBindingActivationRequired, ) -from openhands.agent_server.dependencies import get_conversation_service +from openhands.agent_server.dependencies import get_config, get_conversation_service from openhands.agent_server.pub_sub import MaxSubscribersError, Subscriber from openhands.agent_server.session_protocol import ( MAX_FRAME_BYTES, @@ -366,13 +368,14 @@ async def session_socket( ) ), ] = None, + config: Config = Depends(get_config), + conv_service: ConversationService = Depends(get_conversation_service), ): """Session socket: durable events in an envelope, on one ordered channel.""" - if not await _accept_authenticated_websocket(websocket, session_api_key): + if not await _accept_authenticated_websocket(websocket, session_api_key, config): return logger.info("session_socket_connected: %s after_seq=%s", conversation_id, after_seq) - conv_service = get_conversation_service(websocket) try: event_service = await conv_service.get_event_service(conversation_id) except CredentialBindingActivationRequired: diff --git a/openhands-agent-server/openhands/agent_server/settings_router.py b/openhands-agent-server/openhands/agent_server/settings_router.py index 8040e1733f8..b203a49e317 100644 --- a/openhands-agent-server/openhands/agent_server/settings_router.py +++ b/openhands-agent-server/openhands/agent_server/settings_router.py @@ -2,7 +2,7 @@ from functools import lru_cache from typing import cast -from fastapi import APIRouter, HTTPException, Request, Response, status +from fastapi import APIRouter, Depends, HTTPException, Request, Response, status from pydantic import ValidationError from openhands.agent_server._secrets_exposure import ( @@ -12,21 +12,32 @@ store_errors, translate_missing_cipher, ) -from openhands.agent_server.dependencies import get_cipher, get_config, get_stores +from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import ( + get_agent_profile_store, + get_config, + get_secrets_store, + get_settings_store, + get_stores, +) from openhands.agent_server.persistence import ( SECRET_NAME_PATTERN, + FileSecretsStore, + FileSettingsStore, PersistedSettings, ServerStores, ) from openhands.agent_server.persistence.models import SettingsUpdatePayload from openhands.agent_server.telemetry import notify_misc_settings_changed from openhands.sdk.llm import LLM +from openhands.sdk.llm.llm_profile_store import LLMProfileStore from openhands.sdk.llm.meta_profile_store import ( MetaProfileStore, default_meta_profile_dir, ) from openhands.sdk.logger import get_logger from openhands.sdk.mcp.config import MCPServer +from openhands.sdk.profiles.agent_profile_store import AgentProfileStore from openhands.sdk.settings import ( ConversationSettings, SecretCreateRequest, @@ -38,6 +49,7 @@ export_agent_settings_schema, ) from openhands.sdk.settings.api_models import MCPServerPatch +from openhands.sdk.utils.cipher import Cipher from openhands.sdk.utils.pydantic_secrets import ( SecretDecryptionError, is_redacted_secret, @@ -116,7 +128,11 @@ def _validate_secret_name(name: str) -> None: @settings_router.get(SETTINGS_PATH, response_model=SettingsResponse) -async def get_settings(request: Request) -> SettingsResponse: +async def get_settings( + request: Request, + config: Config = Depends(get_config), + store: FileSettingsStore = Depends(get_settings_store), +) -> SettingsResponse: """Get current settings. Returns the persisted settings including agent configuration, @@ -148,8 +164,6 @@ async def get_settings(request: Request) -> SettingsResponse: values. """ expose_mode = parse_expose_secrets_header(request) - config = get_config(request) - store = get_stores(request).settings settings = store.load() or PersistedSettings() # Audit log all settings access for security visibility @@ -184,7 +198,10 @@ async def get_settings(request: Request) -> SettingsResponse: @settings_router.patch(SETTINGS_PATH, response_model=SettingsResponse) def update_settings( - request: Request, payload: SettingsUpdateRequest + request: Request, + payload: SettingsUpdateRequest, + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), ) -> SettingsResponse: """Update settings with partial changes. @@ -244,9 +261,11 @@ def update_settings( ), ) - _require_pointer_targets(get_stores(request), update_data) + _require_pointer_targets(stores, update_data) return _apply_settings_update( request, + config, + stores, cast(SettingsUpdatePayload, update_data), ) @@ -280,7 +299,9 @@ def _require_pointer_targets(stores: ServerStores, update_data: dict) -> None: def _load_active_profile_llm( - request: Request, update_data: SettingsUpdatePayload + cipher: Cipher | None, + profile_store: LLMProfileStore, + update_data: SettingsUpdatePayload, ) -> LLM | None: """Load the named profile's LLM unless the caller already gave one.""" profile_name = update_data.get("active_profile") @@ -289,8 +310,6 @@ def _load_active_profile_llm( if not profile_name or explicit_llm_diff: return None - cipher = get_cipher(request) - profile_store = get_stores(request).llm_profiles # ``load`` resolves any referenced provider connection (read-at-use); a # dangling reference raises ProviderConnectionNotFound, which # store_errors() maps to 422. A key the cipher cannot decrypt is refused @@ -333,10 +352,14 @@ def _with_profile_llm( def _apply_settings_update( request: Request, + config: Config, + stores: ServerStores, update_data: SettingsUpdatePayload, before_update: Callable[[PersistedSettings], None] | None = None, ) -> SettingsResponse: - profile_llm = _load_active_profile_llm(request, update_data) + profile_llm = _load_active_profile_llm( + config.cipher, stores.llm_profiles, update_data + ) applied_update = update_data # Apply updates atomically with file locking @@ -349,8 +372,7 @@ def apply_update(settings: PersistedSettings) -> PersistedSettings: settings.update(applied_update, context=context) return settings - config = get_config(request) - store = get_stores(request).settings + store = stores.settings client_host = request.client.host if request.client else "unknown" try: settings = store.update(apply_update) @@ -439,6 +461,8 @@ async def create_mcp_server( request: Request, settings_key: str, server: MCPServer, + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), ) -> SettingsResponse: """Create one named MCP server without replacing an existing map entry.""" server_data = server.model_dump( @@ -453,6 +477,8 @@ async def create_mcp_server( ) return _apply_settings_update( request, + config, + stores, update_data, lambda settings: _require_mcp_server_absent(settings, settings_key), ) @@ -463,6 +489,8 @@ async def patch_mcp_server( request: Request, settings_key: str, patch: MCPServerPatch, + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), ) -> SettingsResponse: """Sparsely update one existing named MCP server.""" patch_data = patch.model_dump( @@ -476,13 +504,20 @@ async def patch_mcp_server( ) return _apply_settings_update( request, + config, + stores, update_data, lambda settings: _require_mcp_server_present(settings, settings_key), ) @settings_router.delete(MCP_SERVER_PATH, response_model=SettingsResponse) -async def delete_mcp_server(request: Request, settings_key: str) -> SettingsResponse: +async def delete_mcp_server( + request: Request, + settings_key: str, + config: Config = Depends(get_config), + stores: ServerStores = Depends(get_stores), +) -> SettingsResponse: """Delete one existing named MCP server without altering sibling entries.""" update_data = cast( SettingsUpdatePayload, @@ -490,6 +525,8 @@ async def delete_mcp_server(request: Request, settings_key: str) -> SettingsResp ) return _apply_settings_update( request, + config, + stores, update_data, lambda settings: _require_mcp_server_present(settings, settings_key), ) @@ -500,15 +537,16 @@ async def delete_mcp_server(request: Request, settings_key: str) -> SettingsResp @settings_router.get(SECRETS_PATH, response_model=SecretsListResponse) def list_secrets( - request: Request, agent_profile_id: str | None = None + request: Request, + agent_profile_id: str | None = None, + store: FileSecretsStore = Depends(get_secrets_store), + profile_store: AgentProfileStore = Depends(get_agent_profile_store), ) -> SecretsListResponse: """List available secret names, optionally scoped by an agent profile.""" - store = get_stores(request).secrets secrets = store.load() allowed_names: set[str] | None = None if agent_profile_id is not None: - profile_store = get_stores(request).agent_profiles # store_errors() answers an unloadable profile (400) and a busy store # (503) the way the agent-profile routes do. with store_errors(): @@ -539,7 +577,11 @@ def list_secrets( @settings_router.get(SECRET_VALUE_PATH) -async def get_secret_value(request: Request, name: str) -> Response: +async def get_secret_value( + request: Request, + name: str, + store: FileSecretsStore = Depends(get_secrets_store), +) -> Response: """Get a single secret value by name. Returns the raw secret value as plain text. This endpoint is designed @@ -550,7 +592,6 @@ async def get_secret_value(request: Request, name: str) -> Response: """ _validate_secret_name(name) - store = get_stores(request).secrets value = store.get_secret(name) client_host = request.client.host if request.client else "unknown" @@ -572,7 +613,9 @@ async def get_secret_value(request: Request, name: str) -> Response: @settings_router.put(SECRETS_PATH, response_model=SecretItemResponse) async def create_secret( - request: Request, secret: SecretCreateRequest + request: Request, + secret: SecretCreateRequest, + store: FileSecretsStore = Depends(get_secrets_store), ) -> SecretItemResponse: """Create or update a custom secret (upsert). @@ -588,8 +631,6 @@ async def create_secret( detail="Secret value must not be empty or redacted", ) - store = get_stores(request).secrets - try: store.set_secret( name=secret.name, @@ -623,7 +664,11 @@ async def create_secret( @settings_router.delete(SECRET_VALUE_PATH) -async def delete_secret(request: Request, name: str) -> dict[str, bool]: +async def delete_secret( + request: Request, + name: str, + store: FileSecretsStore = Depends(get_secrets_store), +) -> dict[str, bool]: """Delete a custom secret by name. Raises: @@ -632,8 +677,6 @@ async def delete_secret(request: Request, name: str) -> dict[str, bool]: """ _validate_secret_name(name) - store = get_stores(request).secrets - client_host = request.client.host if request.client else "unknown" try: deleted = store.delete_secret(name) diff --git a/openhands-agent-server/openhands/agent_server/skills_router.py b/openhands-agent-server/openhands/agent_server/skills_router.py index ce2961c4b32..bc0a4a1c674 100644 --- a/openhands-agent-server/openhands/agent_server/skills_router.py +++ b/openhands-agent-server/openhands/agent_server/skills_router.py @@ -6,9 +6,10 @@ from typing import Annotated, Literal -from fastapi import APIRouter, HTTPException, Path, Request +from fastapi import APIRouter, Depends, HTTPException, Path from pydantic import BaseModel, Field +from openhands.agent_server.config import Config from openhands.agent_server.dependencies import get_config from openhands.agent_server.skills_service import ( ExposedUrlData, @@ -276,7 +277,9 @@ def _merge_registered_marketplaces( @skills_router.post("", response_model=SkillsResponse) -def get_skills(request: SkillsRequest, http_request: Request) -> SkillsResponse: +def get_skills( + request: SkillsRequest, config: Config = Depends(get_config) +) -> SkillsResponse: """Load and merge skills from all configured sources. Skills are loaded from multiple sources and merged with the following @@ -305,7 +308,7 @@ def get_skills(request: SkillsRequest, http_request: Request) -> SkillsResponse: if request.org_configs: org_repos = [(c.org_repo_url, c.org_name) for c in request.org_configs] - server_registrations = get_config(http_request).registered_marketplaces + server_registrations = config.registered_marketplaces registered_marketplaces = _merge_registered_marketplaces( server_registrations, request.registered_marketplaces, diff --git a/openhands-agent-server/openhands/agent_server/sockets.py b/openhands-agent-server/openhands/agent_server/sockets.py index 8b3deac63d5..c5fea63495e 100644 --- a/openhands-agent-server/openhands/agent_server/sockets.py +++ b/openhands-agent-server/openhands/agent_server/sockets.py @@ -22,13 +22,17 @@ from fastapi import ( APIRouter, + Depends, Query, WebSocket, WebSocketDisconnect, ) from starlette.websockets import WebSocketState +from openhands.agent_server.bash_service import BashEventService +from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ( + ConversationService, CredentialBindingActivationRequired, ) from openhands.agent_server.dependencies import ( @@ -90,6 +94,7 @@ def _resolve_websocket_session_api_key( async def _accept_authenticated_websocket( websocket: WebSocket, session_api_key: str | None, + config: Config, ) -> bool: """Authenticate and accept the socket, or close with an auth error. @@ -102,7 +107,6 @@ async def _accept_authenticated_websocket( The WebSocket is always *accepted* before first-message auth is attempted because raw WebSocket requires ``accept()`` before any frames can be read. """ - config = get_config(websocket) resolved_key = _resolve_websocket_session_api_key(websocket, session_api_key) # No auth configured — accept unconditionally. @@ -214,6 +218,8 @@ async def events_socket( deprecated=True, ), ] = False, + config: Config = Depends(get_config), + conv_service: ConversationService = Depends(get_conversation_service), ): """WebSocket endpoint for conversation events. @@ -234,11 +240,10 @@ async def events_socket( backward compatibility - if True and resend_mode is None, behaves as resend_mode='all'. """ - if not await _accept_authenticated_websocket(websocket, session_api_key): + if not await _accept_authenticated_websocket(websocket, session_api_key, config): return logger.info(f"Event Websocket Connected: {conversation_id}") - conv_service = get_conversation_service(websocket) try: event_service = await conv_service.get_event_service(conversation_id) except CredentialBindingActivationRequired: @@ -364,6 +369,8 @@ async def bash_events_socket( deprecated=True, ), ] = False, + config: Config = Depends(get_config), + bash_service: BashEventService = Depends(get_server_bash_event_service), ): """WebSocket endpoint for bash events. @@ -375,10 +382,9 @@ async def bash_events_socket( - None: Don't resend, just subscribe to new events resend_all: DEPRECATED. Use resend_mode='all' instead. """ - if not await _accept_authenticated_websocket(websocket, session_api_key): + if not await _accept_authenticated_websocket(websocket, session_api_key, config): return - bash_service = get_server_bash_event_service(websocket) logger.info("Bash Websocket Connected") try: subscriber_id = await bash_service.subscribe_to_events( diff --git a/openhands-agent-server/openhands/agent_server/sub_agents_router.py b/openhands-agent-server/openhands/agent_server/sub_agents_router.py index 280b977a4a1..471e52ad021 100644 --- a/openhands-agent-server/openhands/agent_server/sub_agents_router.py +++ b/openhands-agent-server/openhands/agent_server/sub_agents_router.py @@ -8,9 +8,10 @@ from typing import Any -from fastapi import APIRouter, Request +from fastapi import APIRouter, Depends from pydantic import BaseModel, Field +from openhands.agent_server.config import Config from openhands.agent_server.dependencies import get_config from openhands.agent_server.launch import configured_browser_available from openhands.sdk.context.condenser import CondenserBase @@ -124,7 +125,7 @@ class SubAgentsResponse(BaseModel): @sub_agents_router.post("", response_model=SubAgentsResponse) def get_sub_agents( - request: SubAgentsRequest, http_request: Request + request: SubAgentsRequest, config: Config = Depends(get_config) ) -> SubAgentsResponse: """List file-based and built-in sub-agents for the workspace. @@ -137,7 +138,6 @@ def get_sub_agents( include_project=request.load_project, include_user=request.load_user, ) - config = get_config(http_request) builtins = ( discover_builtin_agents( enable_browser=configured_browser_available(config) is not False diff --git a/openhands-agent-server/openhands/agent_server/tool_router.py b/openhands-agent-server/openhands/agent_server/tool_router.py index 59c23fbbf07..72f88a7db7b 100644 --- a/openhands-agent-server/openhands/agent_server/tool_router.py +++ b/openhands-agent-server/openhands/agent_server/tool_router.py @@ -2,12 +2,13 @@ import asyncio -from fastapi import APIRouter, Request +from fastapi import APIRouter, Depends from pydantic import BaseModel # Registered only in the agent-server: the tool needs a server to create the # child on, so it is deliberately absent from get_default_tools(). import openhands.tools.child_conversation # noqa: E402, F401 +from openhands.agent_server.config import Config from openhands.agent_server.dependencies import get_config from openhands.agent_server.launch import ( can_probe_tools, @@ -47,13 +48,14 @@ class ToolCatalogResponse(BaseModel): @tool_router.get("/catalog") -async def get_tool_catalog(request: Request) -> ToolCatalogResponse: +async def get_tool_catalog( + config: Config = Depends(get_config), +) -> ToolCatalogResponse: """List the tools this server offers for configuring an agent. Clients offer the ``user_selectable`` entries; ``usable`` says whether the runtime conversations run in can run the tool. """ - config = get_config(request) entries = await asyncio.to_thread( list_tool_catalog, check_usable=can_probe_tools(config) ) diff --git a/openhands-agent-server/openhands/agent_server/workspaces_router.py b/openhands-agent-server/openhands/agent_server/workspaces_router.py index 8ffbc9285d3..c2c7a928659 100644 --- a/openhands-agent-server/openhands/agent_server/workspaces_router.py +++ b/openhands-agent-server/openhands/agent_server/workspaces_router.py @@ -8,11 +8,12 @@ from typing import Annotated -from fastapi import APIRouter, HTTPException, Query, Request, status +from fastapi import APIRouter, Depends, HTTPException, Query, status from pydantic import BaseModel, ConfigDict, Field -from openhands.agent_server.dependencies import get_stores +from openhands.agent_server.dependencies import get_workspaces_store from openhands.agent_server.persistence import ( + FileWorkspacesStore, PersistedWorkspaces, WorkspaceItem, WorkspaceParentItem, @@ -59,9 +60,10 @@ def _to_response(persisted: PersistedWorkspaces) -> WorkspacesListResponse: response_model=WorkspacesListResponse, response_model_exclude_none=True, ) -async def list_workspaces(request: Request) -> WorkspacesListResponse: +async def list_workspaces( + store: FileWorkspacesStore = Depends(get_workspaces_store), +) -> WorkspacesListResponse: """Return the saved workspaces and workspace parents.""" - store = get_stores(request).workspaces try: persisted = store.load() except (OSError, PermissionError): @@ -87,10 +89,10 @@ async def list_workspaces(request: Request) -> WorkspacesListResponse: response_model_exclude_none=True, ) async def add_workspaces( - request: Request, payload: AddWorkspacesRequest + payload: AddWorkspacesRequest, + store: FileWorkspacesStore = Depends(get_workspaces_store), ) -> WorkspacesListResponse: """Append workspaces to the saved list. Idempotent — dedupes by ``path``.""" - store = get_stores(request).workspaces def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: # Dedupe against the existing list AND within the incoming payload, @@ -126,11 +128,10 @@ def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: @workspaces_router.delete("", response_model=DeleteResponse) async def delete_workspace( - request: Request, path: Annotated[str, Query(description="Absolute workspace path to remove")], + store: FileWorkspacesStore = Depends(get_workspaces_store), ) -> DeleteResponse: """Remove a workspace by its absolute path.""" - store = get_stores(request).workspaces removed = False @@ -165,10 +166,10 @@ def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: response_model_exclude_none=True, ) async def add_workspace_parents( - request: Request, payload: AddWorkspaceParentsRequest + payload: AddWorkspaceParentsRequest, + store: FileWorkspacesStore = Depends(get_workspaces_store), ) -> WorkspacesListResponse: """Append workspace parents. Idempotent — dedupes by ``path``.""" - store = get_stores(request).workspaces def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: # Dedupe against the existing list AND within the incoming payload — @@ -205,11 +206,10 @@ def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: @workspaces_router.delete("/parents", response_model=DeleteResponse) async def delete_workspace_parent( - request: Request, path: Annotated[str, Query(description="Absolute workspace parent path to remove")], + store: FileWorkspacesStore = Depends(get_workspaces_store), ) -> DeleteResponse: """Remove a workspace parent by its absolute path.""" - store = get_stores(request).workspaces removed = False diff --git a/tests/agent_server/canvas_extensions/test_canvas_extension_bridge_routes.py b/tests/agent_server/canvas_extensions/test_canvas_extension_bridge_routes.py index fb82713bbbe..df8c06d71b1 100644 --- a/tests/agent_server/canvas_extensions/test_canvas_extension_bridge_routes.py +++ b/tests/agent_server/canvas_extensions/test_canvas_extension_bridge_routes.py @@ -8,6 +8,8 @@ from starlette.websockets import WebSocketDisconnect from openhands.agent_server import canvas_extensions_bridge_router as bridge_router +from openhands.agent_server.config import Config +from tests.agent_server.conftest import install_test_context APP_NAME = "demo-app" @@ -17,12 +19,14 @@ def proxied(monkeypatch: pytest.MonkeyPatch) -> tuple[TestClient, list[str]]: paths: list[str] = [] - async def fake_http(request: Request, extension_name: str, path: str) -> Response: + async def fake_http( + request: Request, extension_name: str, path: str, config: Config + ) -> Response: paths.append(path) return Response(status_code=204) async def fake_websocket( - websocket: WebSocket, extension_name: str, path: str + websocket: WebSocket, extension_name: str, path: str, config: Config ) -> None: paths.append(path) await websocket.close(code=1011) @@ -30,6 +34,7 @@ async def fake_websocket( monkeypatch.setattr(bridge_router, "proxy_app_backend_http", fake_http) monkeypatch.setattr(bridge_router, "proxy_app_backend_websocket", fake_websocket) app = FastAPI() + install_test_context(app) app.include_router(bridge_router.app_backend_bridge_router) return TestClient(app), paths diff --git a/tests/agent_server/docker_runtime/test_hybrid.py b/tests/agent_server/docker_runtime/test_hybrid.py index 1e41031170b..6b2899dea64 100644 --- a/tests/agent_server/docker_runtime/test_hybrid.py +++ b/tests/agent_server/docker_runtime/test_hybrid.py @@ -1,4 +1,5 @@ import json +from unittest.mock import AsyncMock from uuid import uuid4 import pytest @@ -8,6 +9,7 @@ from openhands.agent_server.api import create_app from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import get_conversation_service from openhands.agent_server.docker_runtime.hybrid import ( HybridConversationRegistry, RuntimeDispatchMiddleware, @@ -174,6 +176,7 @@ def test_start_existing_docker_conversation_as_local_is_rejected(config): registry = server_context_of(app).conversation_registry assert isinstance(registry, HybridConversationRegistry) registry.provisioning.create(docker_id) + app.dependency_overrides[get_conversation_service] = lambda: AsyncMock() response = TestClient(app).post( "/api/conversations", json={"conversation_id": str(docker_id), "conversation_runtime": "local"}, diff --git a/tests/agent_server/docker_runtime/test_routes.py b/tests/agent_server/docker_runtime/test_routes.py index 4d3021ed052..f8d22ac7865 100644 --- a/tests/agent_server/docker_runtime/test_routes.py +++ b/tests/agent_server/docker_runtime/test_routes.py @@ -595,18 +595,14 @@ async def stop(conversation_id): await allow_stop.wait() registry.stop = stop - request = Request( - { - "type": "http", - "method": "DELETE", - "path": f"/api/conversations/{conversation_id}", - "query_string": b"", - "headers": [], - "app": _app_with(registry, AsyncMock()), - } - ) - deletion = asyncio.create_task(delete_conversation(conversation_id, request)) + deletion = asyncio.create_task( + delete_conversation( + conversation_id, + conversation_registry=registry, + conversation_service=AsyncMock(), + ) + ) await stop_started.wait() with pytest.raises(RuntimeError, match="Conversation is being deleted"): await registry.get_or_create(conversation_id) @@ -690,7 +686,13 @@ async def receive(): }, receive, ) - await proxy_conversation(conversation_id, "secrets", request) + await proxy_conversation( + conversation_id, + "secrets", + request, + conversation_registry=registry, + conversation_service=service, + ) forwarded = UpdateSecretsRequest.model_validate_json(captured["body"]) assert set(forwarded.secrets) == {"ALLOWED"} diff --git a/tests/agent_server/test_api_authentication.py b/tests/agent_server/test_api_authentication.py index 88b96f717ec..bcc3322ba98 100644 --- a/tests/agent_server/test_api_authentication.py +++ b/tests/agent_server/test_api_authentication.py @@ -4,14 +4,20 @@ """ import json +from unittest.mock import AsyncMock, MagicMock +from uuid import uuid4 import pytest from fastapi import HTTPException from fastapi.testclient import TestClient from openhands.agent_server.api import _find_http_exception, create_app +from openhands.agent_server.bash_service import BashEventService from openhands.agent_server.config import Config -from openhands.agent_server.dependencies import get_conversation_service +from openhands.agent_server.dependencies import ( + get_conversation_service, + get_server_bash_event_service, +) from openhands.agent_server.openai.router import _parse_observability_overrides @@ -284,6 +290,10 @@ def test_api_websocket_authentication(): """Test that WebSocket connections also respect authentication.""" config = Config(session_api_keys=["test-key"]) app = create_app(config) + bash_service = MagicMock(spec=BashEventService) + bash_service.subscribe_to_events = AsyncMock(return_value=uuid4()) + bash_service.unsubscribe_from_events = AsyncMock(return_value=True) + app.dependency_overrides[get_server_bash_event_service] = lambda: bash_service client = TestClient(app) # Without authentication -> should fail @@ -331,6 +341,10 @@ def test_api_websocket_no_auth_required(): """Test that WebSocket connections work when auth is disabled.""" config = Config(session_api_keys=[]) app = create_app(config) + bash_service = MagicMock(spec=BashEventService) + bash_service.subscribe_to_events = AsyncMock(return_value=uuid4()) + bash_service.unsubscribe_from_events = AsyncMock(return_value=True) + app.dependency_overrides[get_server_bash_event_service] = lambda: bash_service client = TestClient(app) with client.websocket_connect("/sockets/bash-events"): diff --git a/tests/agent_server/test_event_router_websocket.py b/tests/agent_server/test_event_router_websocket.py index 662a046dd98..01931cec002 100644 --- a/tests/agent_server/test_event_router_websocket.py +++ b/tests/agent_server/test_event_router_websocket.py @@ -185,22 +185,19 @@ async def test_websocket_disconnect_breaks_loop( """Test that WebSocketDisconnect exception breaks the loop.""" mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - - from openhands.agent_server.sockets import events_socket - - await events_socket( - sample_conversation_id, mock_websocket, session_api_key=None - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + + from openhands.agent_server.sockets import events_socket + + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + config=config, + conv_service=conv_service, + ) mock_event_service.unsubscribe_from_events.assert_called() @@ -214,22 +211,19 @@ async def test_websocket_no_double_unsubscription( mock_event_service.subscribe_to_events.return_value = subscriber_id mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, mock_websocket, session_api_key=None - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + config=config, + conv_service=conv_service, + ) assert mock_event_service.unsubscribe_from_events.call_count == 1 mock_event_service.unsubscribe_from_events.assert_called_with(subscriber_id) @@ -252,22 +246,18 @@ def side_effect(): mock_websocket.receive_json.side_effect = side_effect - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - patch("openhands.agent_server.sockets.logger.exception") as log_exception, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + with patch("openhands.agent_server.sockets.logger.exception") as log_exception: from openhands.agent_server.sockets import events_socket await events_socket( - sample_conversation_id, mock_websocket, session_api_key=None + sample_conversation_id, + mock_websocket, + session_api_key=None, + config=config, + conv_service=conv_service, ) log_exception.assert_called_once_with("error_in_subscription") @@ -294,22 +284,19 @@ def side_effect(): mock_websocket.receive_json.side_effect = side_effect - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, mock_websocket, session_api_key=None - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + config=config, + conv_service=conv_service, + ) mock_event_service.send_message.assert_called_once() assert mock_websocket.receive_json.call_count == 2 @@ -324,24 +311,20 @@ async def test_disconnect_and_unsubscribe_when_send_error_fails( mock_websocket.receive_json.side_effect = RuntimeError("Connection broken") mock_websocket.send_json.side_effect = RuntimeError("Connection broken") - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - patch("openhands.agent_server.sockets.logger.debug") as log_debug, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + with patch("openhands.agent_server.sockets.logger.debug") as log_debug: from openhands.agent_server.sockets import events_socket # RuntimeError is caught gracefully (like WebSocketDisconnect) # and the function returns normally await events_socket( - sample_conversation_id, mock_websocket, session_api_key=None + sample_conversation_id, + mock_websocket, + session_api_key=None, + config=config, + conv_service=conv_service, ) log_debug.assert_called_once() @@ -355,25 +338,20 @@ async def test_resend_mode_none_no_resend( """Test that resend_mode=None doesn't trigger event resend.""" mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, - mock_websocket, - session_api_key=None, - resend_mode=None, - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + resend_mode=None, + config=config, + conv_service=conv_service, + ) mock_event_service.search_events.assert_not_called() @@ -399,25 +377,20 @@ async def test_resend_mode_all_resends_events( mock_event_service.search_events = AsyncMock(return_value=mock_event_page) mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, - mock_websocket, - session_api_key=None, - resend_mode="all", - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + resend_mode="all", + config=config, + conv_service=conv_service, + ) mock_event_service.search_events.assert_called_once_with(page_id=None) assert mock_websocket.send_json.call_count == 2 @@ -445,26 +418,21 @@ async def test_resend_mode_since_with_timestamp( # Use a naive timestamp test_timestamp = datetime(2024, 1, 15, 10, 30, 0) - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, - mock_websocket, - session_api_key=None, - resend_mode="since", - after_timestamp=test_timestamp, - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + resend_mode="since", + after_timestamp=test_timestamp, + config=config, + conv_service=conv_service, + ) mock_event_service.search_events.assert_called_once_with( page_id=None, timestamp__gte=test_timestamp @@ -478,18 +446,10 @@ async def test_resend_mode_since_without_timestamp_logs_warning( """Test that resend_mode='since' without after_timestamp logs warning.""" mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - patch("openhands.agent_server.sockets.logger") as mock_logger, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + with patch("openhands.agent_server.sockets.logger") as mock_logger: from openhands.agent_server.sockets import events_socket await events_socket( @@ -498,6 +458,8 @@ async def test_resend_mode_since_without_timestamp_logs_warning( session_api_key=None, resend_mode="since", after_timestamp=None, + config=config, + conv_service=conv_service, ) # Should log a warning and not call search_events @@ -526,26 +488,21 @@ async def test_resend_mode_since_timezone_aware_is_normalized( # Use a timezone-aware timestamp (UTC) test_timestamp = datetime(2024, 1, 15, 10, 30, 0, tzinfo=UTC) - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, - mock_websocket, - session_api_key=None, - resend_mode="since", - after_timestamp=test_timestamp, - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + resend_mode="since", + after_timestamp=test_timestamp, + config=config, + conv_service=conv_service, + ) # search_events should be called with the normalized timestamp mock_event_service.search_events.assert_called_once() @@ -578,18 +535,10 @@ async def test_deprecated_resend_all_true_still_works( mock_event_service.search_events = AsyncMock(return_value=mock_event_page) mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - patch("openhands.agent_server.sockets.logger") as mock_logger, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + with patch("openhands.agent_server.sockets.logger") as mock_logger: from openhands.agent_server.sockets import events_socket await events_socket( @@ -597,6 +546,8 @@ async def test_deprecated_resend_all_true_still_works( mock_websocket, session_api_key=None, resend_all=True, + config=config, + conv_service=conv_service, ) # Should log deprecation warning @@ -616,25 +567,20 @@ async def test_deprecated_resend_all_false_no_resend( """Test backward compatibility: resend_all=False doesn't trigger event resend.""" mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - from openhands.agent_server.sockets import events_socket + from openhands.agent_server.sockets import events_socket - await events_socket( - sample_conversation_id, - mock_websocket, - session_api_key=None, - resend_all=False, - ) + await events_socket( + sample_conversation_id, + mock_websocket, + session_api_key=None, + resend_all=False, + config=config, + conv_service=conv_service, + ) mock_event_service.search_events.assert_not_called() @@ -646,18 +592,10 @@ async def test_resend_mode_takes_precedence_over_resend_all( """Test that resend_mode takes precedence over deprecated resend_all.""" mock_websocket.receive_json.side_effect = WebSocketDisconnect() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - patch("openhands.agent_server.sockets.logger") as mock_logger, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - + config = MagicMock(session_api_keys=None) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + with patch("openhands.agent_server.sockets.logger") as mock_logger: from openhands.agent_server.sockets import events_socket # If resend_mode is explicitly None and resend_all=True, it should @@ -682,6 +620,8 @@ async def test_resend_mode_takes_precedence_over_resend_all( session_api_key=None, resend_mode="all", resend_all=False, # This should be ignored since resend_mode is set + config=config, + conv_service=conv_service, ) # resend_mode="all" should trigger resend, not the resend_all=False diff --git a/tests/agent_server/test_event_socket_races.py b/tests/agent_server/test_event_socket_races.py index 2dd5954779f..519fda5427e 100644 --- a/tests/agent_server/test_event_socket_races.py +++ b/tests/agent_server/test_event_socket_races.py @@ -3,7 +3,7 @@ import asyncio import json from types import SimpleNamespace -from unittest.mock import MagicMock, patch +from unittest.mock import AsyncMock, MagicMock, patch from uuid import uuid4 import pytest @@ -83,7 +83,7 @@ async def _wait_until(predicate): @pytest.fixture -async def svc(tmp_path, monkeypatch): +async def svc(tmp_path): with patch("openhands.sdk.llm.utils.model_info.httpx.get") as mock_get: mock_get.return_value = MagicMock(json=lambda: {"data": []}) service = EventService( @@ -99,14 +99,6 @@ async def svc(tmp_path, monkeypatch): ) await service.start() - async def get_event_service(_cid): - return service - - conversation_service = SimpleNamespace(get_event_service=get_event_service) - for module in (sockets, session_socket): - monkeypatch.setattr( - module, "get_conversation_service", lambda _ws: conversation_service - ) yield service if service.is_open(): await service.close() @@ -134,12 +126,19 @@ def _legacy(ws, svc, resend_mode=None): resend_mode=resend_mode, after_timestamp=None, resend_all=False, + config=ws.app.state.server_context.config, + conv_service=MagicMock(get_event_service=AsyncMock(return_value=svc)), ) def _session(ws, svc, after_seq=None): return session_socket.session_socket( - svc.stored.id, ws, session_api_key=None, after_seq=after_seq + svc.stored.id, + ws, + session_api_key=None, + after_seq=after_seq, + config=ws.app.state.server_context.config, + conv_service=MagicMock(get_event_service=AsyncMock(return_value=svc)), ) diff --git a/tests/agent_server/test_init_router.py b/tests/agent_server/test_init_router.py index 9e6b39bb9b5..4213eeec6cd 100644 --- a/tests/agent_server/test_init_router.py +++ b/tests/agent_server/test_init_router.py @@ -479,7 +479,7 @@ def test_app_backend_http_requires_ready_state( init_state: InitState, ) -> None: async def accept_app_backend_http( - _request: Request, _extension_name: str, _path: str + _request: Request, _extension_name: str, _path: str, _config: Config ) -> Response: return Response(status_code=200) diff --git a/tests/agent_server/test_server_context.py b/tests/agent_server/test_server_context.py index 279eb33265d..74e1f673dcb 100644 --- a/tests/agent_server/test_server_context.py +++ b/tests/agent_server/test_server_context.py @@ -24,6 +24,7 @@ from fastapi import FastAPI from fastapi.testclient import TestClient from pydantic import SecretStr +from starlette.requests import HTTPConnection import openhands.agent_server from openhands.agent_server import init_router as init_router_module @@ -34,6 +35,7 @@ from openhands.agent_server.server_context import ( ServerContext, build_server_context, + get_server_context, install_server_context, server_context_of, ) @@ -302,6 +304,38 @@ def test_the_lifespan_adds_services_to_the_context_it_was_created_with( assert running.bash_event_service.bash_events_dir == tmp_path / "bash" +def test_a_request_takes_everything_from_one_context(tmp_path: Path) -> None: + """The auth check, the config and the store of a request share a context. + + ``GET /api/settings`` depends on the session-key check (router level), the + config and the settings store. They all resolve from one context lookup, + so a context swapped while the request runs cannot give it a mix. + """ + app = create_app( + Config( + session_api_keys=[SESSION_KEY], + conversations_path=tmp_path / "conversations", + bash_events_dir=tmp_path / "bash", + enable_vscode=False, + preload_tools=False, + ) + ) + lookups: list[ServerContext] = [] + + def counting_lookup(connection: HTTPConnection) -> ServerContext: + lookups.append(server_context_of(connection.app)) + return lookups[-1] + + app.dependency_overrides[get_server_context] = counting_lookup + with TestClient(app) as client: + response = client.get( + "/api/settings", headers={"X-Session-API-Key": SESSION_KEY} + ) + + assert response.status_code == 200 + assert len(lookups) == 1 + + def test_an_app_without_a_context_says_so() -> None: with pytest.raises(RuntimeError, match="no ServerContext"): server_context_of(FastAPI()) @@ -322,6 +356,19 @@ def test_an_app_without_a_context_says_so() -> None: } ) _CONTEXT_MODULE = "server_context.py" +# Fetching the whole context by hand. A route declares what it needs instead +# (``Depends(get_settings_store)``), so only code that builds or swaps the +# context, or runs outside a route, may do this. +_CONTEXT_LOOKUPS = frozenset({"get_server_context", "server_context_of"}) +_CONTEXT_LOOKUP_MODULES = frozenset( + { + _CONTEXT_MODULE, + "api.py", # builds the app, its lifespan and its middleware stack + "init_router.py", # swaps the context in POST /api/init + "docker_runtime/hybrid.py", # ASGI middleware + "canvas_extensions_router.py", # a manager that outlives /api/init + } +) _PACKAGE_DIR = Path(openhands.agent_server.__file__).parent @@ -341,6 +388,8 @@ def context_violations(source: str, filename: str) -> list[str]: if node.attr in _CONTEXT_STATE and _is_app_state(node.value): found.append(f"{filename}:{node.lineno}: reads .state.{node.attr}") elif isinstance(node, ast.Call): + if isinstance(node.func, ast.Name) and node.func.id in _CONTEXT_LOOKUPS: + found.append(f"{filename}:{node.lineno}: calls {node.func.id}()") # getattr/setattr/hasattr(.state, "config", ...) if ( isinstance(node.func, ast.Name) @@ -366,6 +415,8 @@ def context_violations(source: str, filename: str) -> list[str]: "registry = scope['app'].state.conversation_registry", "context = api.state.server_context", "config = getattr(request.app.state, 'config', None)", + "stores = get_server_context(request).stores", + "config = server_context_of(request.app).config", "def get_default_config():\n return load_config()", "async def get_default_conversation_service():\n ...", ], @@ -377,8 +428,8 @@ def test_the_guard_recognises_a_way_around_the_context(source: str) -> None: @pytest.mark.parametrize( "source", [ - "config = get_config(request)", - "stores = get_server_context(request).stores", + "async def route(config: Config = Depends(get_config)): ...", + "def provider(context: ServerContext = Depends(get_server_context)): ...", "manager = request.app.state.canvas_extension_backend_manager", "event_service = getattr(request.state, 'runtime_event_service', None)", "status = conversation.state.execution_status", @@ -395,9 +446,10 @@ def test_only_the_server_context_module_touches_context_state() -> None: A reader of ``app.state.config`` (or of a service kept there) sees whatever was assigned last, which during ``POST /api/init`` can be half of the old setup and half of the new one; a ``get_default_*`` function builds a second - copy from the environment. Take the config, a store or a service from the - request's ``ServerContext`` (``openhands.agent_server.dependencies``), or - have it passed in by the code that built the context. + copy from the environment. A route declares the config, store or service it + needs with ``Depends`` (``openhands.agent_server.dependencies``) rather than + fetching the context; other code has it passed in by whoever built the + context. """ violations: list[str] = [] modules = sorted(_PACKAGE_DIR.rglob("*.py")) @@ -408,6 +460,8 @@ def test_only_the_server_context_module_touches_context_state() -> None: if relative == _CONTEXT_MODULE: # The one place the context itself is kept and read. found = [v for v in found if not v.endswith(".state.server_context")] + if relative in _CONTEXT_LOOKUP_MODULES: + found = [v for v in found if ": calls " not in v] violations.extend(found) assert violations == [], "\n".join(violations) @@ -416,7 +470,9 @@ def test_only_the_server_context_module_touches_context_state() -> None: def test_the_context_module_keeps_the_context_on_app_state() -> None: """The guard's exception is real: that module is where the context lives.""" source = (_PACKAGE_DIR / _CONTEXT_MODULE).read_text() - kept = context_violations(source, _CONTEXT_MODULE) + kept = [ + v for v in context_violations(source, _CONTEXT_MODULE) if ": calls " not in v + ] assert kept and all(v.endswith(".state.server_context") for v in kept) diff --git a/tests/agent_server/test_sockets_service_getters.py b/tests/agent_server/test_sockets_service_getters.py index 52a9c87aa08..83e073a7dac 100644 --- a/tests/agent_server/test_sockets_service_getters.py +++ b/tests/agent_server/test_sockets_service_getters.py @@ -18,11 +18,16 @@ from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService from openhands.agent_server.dependencies import ( + get_config, get_conversation_service, get_server_bash_event_service, ) from openhands.agent_server.event_service import EventService -from openhands.agent_server.server_context import ServerContext, build_server_context +from openhands.agent_server.server_context import ( + ServerContext, + build_server_context, + get_server_context, +) def _context( @@ -63,22 +68,25 @@ def bash_service(): def test_get_conversation_service_returns_the_context_service(conv_service): - ws = _make_ws(_context(conversation_service=conv_service)) - assert get_conversation_service(ws) is conv_service + context = _context(conversation_service=conv_service) + ws = _make_ws(context) + assert get_conversation_service(ws, context) is conv_service def test_get_conversation_service_refuses_a_socket_without_a_started_service(): """A dormant context has no service, and nothing stands in for it.""" - ws = _make_ws(_context()) + context = _context() + ws = _make_ws(context) with pytest.raises(WebSocketException) as excinfo: - get_conversation_service(ws) + get_conversation_service(ws, context) assert excinfo.value.code == status.WS_1013_TRY_AGAIN_LATER def test_get_conversation_service_answers_503_to_a_request_without_a_service(): - request = _make_ws(_context(), scope_type="http") + context = _context() + request = _make_ws(context, scope_type="http") with pytest.raises(HTTPException) as excinfo: - get_conversation_service(request) + get_conversation_service(request, context) assert excinfo.value.status_code == status.HTTP_503_SERVICE_UNAVAILABLE @@ -86,21 +94,24 @@ def test_get_conversation_service_answers_503_to_a_request_without_a_service(): def test_get_bash_event_service_returns_the_context_service(bash_service): - ws = _make_ws(_context(bash_event_service=bash_service)) - assert get_server_bash_event_service(ws) is bash_service + context = _context(bash_event_service=bash_service) + ws = _make_ws(context) + assert get_server_bash_event_service(ws, context) is bash_service def test_get_bash_event_service_refuses_a_socket_without_a_started_service(): - ws = _make_ws(_context()) + context = _context() + ws = _make_ws(context) with pytest.raises(WebSocketException) as excinfo: - get_server_bash_event_service(ws) + get_server_bash_event_service(ws, context) assert excinfo.value.code == status.WS_1013_TRY_AGAIN_LATER def test_get_bash_event_service_answers_503_to_a_request_without_a_service(): - request = _make_ws(_context(), scope_type="http") + context = _context() + request = _make_ws(context, scope_type="http") with pytest.raises(HTTPException) as excinfo: - get_server_bash_event_service(request) + get_server_bash_event_service(request, context) assert excinfo.value.status_code == status.HTTP_503_SERVICE_UNAVAILABLE @@ -109,7 +120,7 @@ def test_an_app_without_a_context_is_a_programming_error(): ws = MagicMock() ws.app.state = SimpleNamespace() with pytest.raises(RuntimeError, match="no ServerContext"): - get_conversation_service(ws) + get_server_context(ws) # -- End-to-end: WebSocket handlers use the context's services -- @@ -127,7 +138,8 @@ async def test_events_socket_uses_the_context_conversation_service(): per_app_conv_svc = MagicMock(spec=ConversationService) per_app_conv_svc.get_event_service = AsyncMock(return_value=mock_event_svc) - ws = _make_ws(_context(conversation_service=per_app_conv_svc)) + context = _context(conversation_service=per_app_conv_svc) + ws = _make_ws(context) ws.accept = AsyncMock() ws.receive_text = AsyncMock() ws.receive_json = AsyncMock(side_effect=WebSocketDisconnect()) @@ -135,7 +147,13 @@ async def test_events_socket_uses_the_context_conversation_service(): ws.close = AsyncMock() ws.headers = {} - await events_socket(uuid4(), ws, session_api_key=None) + await events_socket( + uuid4(), + ws, + session_api_key=None, + config=get_config(context), + conv_service=get_conversation_service(ws, context), + ) per_app_conv_svc.get_event_service.assert_called_once() mock_event_svc.subscribe_to_events.assert_called_once() @@ -151,7 +169,8 @@ async def test_bash_events_socket_uses_the_context_bash_event_service(): per_app_bash_svc.subscribe_to_events = AsyncMock(return_value=uuid4()) per_app_bash_svc.unsubscribe_from_events = AsyncMock(return_value=True) - ws = _make_ws(_context(bash_event_service=per_app_bash_svc)) + context = _context(bash_event_service=per_app_bash_svc) + ws = _make_ws(context) ws.accept = AsyncMock() ws.receive_text = AsyncMock() ws.receive_json = AsyncMock(side_effect=WebSocketDisconnect()) @@ -159,7 +178,12 @@ async def test_bash_events_socket_uses_the_context_bash_event_service(): ws.close = AsyncMock() ws.headers = {} - await bash_events_socket(ws, session_api_key=None) + await bash_events_socket( + ws, + session_api_key=None, + config=get_config(context), + bash_service=get_server_bash_event_service(ws, context), + ) per_app_bash_svc.subscribe_to_events.assert_called_once() per_app_bash_svc.unsubscribe_from_events.assert_called_once() @@ -175,7 +199,8 @@ async def test_events_socket_preserves_diagnostics_without_extra_stack( event_service.unsubscribe_from_events = AsyncMock() service = MagicMock(spec=ConversationService) service.get_event_service = AsyncMock(return_value=event_service) - ws = _make_ws(_context(conversation_service=service)) + context = _context(conversation_service=service) + ws = _make_ws(context) ws.headers = {} ws.accept = AsyncMock() ws.receive_json = AsyncMock( @@ -185,7 +210,9 @@ async def test_events_socket_preserves_diagnostics_without_extra_stack( ws.close = AsyncMock() caplog.set_level("DEBUG", logger=sockets_mod.__name__) - await sockets_mod.events_socket(uuid4(), ws) + await sockets_mod.events_socket( + uuid4(), ws, config=context.config, conv_service=service + ) payload = ws.send_json.call_args.args[0] assert payload["code"] == "ValueError" diff --git a/tests/agent_server/test_websocket_first_message_auth.py b/tests/agent_server/test_websocket_first_message_auth.py index 969a46e2de7..93f0fb63ed8 100644 --- a/tests/agent_server/test_websocket_first_message_auth.py +++ b/tests/agent_server/test_websocket_first_message_auth.py @@ -33,9 +33,10 @@ def _make_mock_websocket(*, headers=None): @pytest.mark.asyncio async def test_no_auth_configured_accepts_immediately(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = [] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=[]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is True ws.accept.assert_called_once() @@ -48,11 +49,10 @@ async def test_no_auth_configured_accepts_immediately(): @pytest.mark.asyncio async def test_legacy_query_param_valid_key(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket( - ws, session_api_key="sk-oh-valid" - ) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key="sk-oh-valid", config=config + ) assert result is True ws.accept.assert_called_once() @@ -62,11 +62,10 @@ async def test_legacy_query_param_valid_key(): @pytest.mark.asyncio async def test_legacy_query_param_invalid_key(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket( - ws, session_api_key="sk-oh-wrong" - ) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key="sk-oh-wrong", config=config + ) assert result is False ws.close.assert_called_once_with(code=4001, reason="Authentication failed") @@ -80,11 +79,10 @@ async def test_legacy_query_param_takes_precedence_over_first_message(): ws.receive_text.return_value = json.dumps( {"type": "auth", "session_api_key": "sk-oh-different"} ) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket( - ws, session_api_key="sk-oh-valid" - ) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key="sk-oh-valid", config=config + ) assert result is True ws.accept.assert_called_once() @@ -98,9 +96,10 @@ async def test_legacy_query_param_takes_precedence_over_first_message(): @pytest.mark.asyncio async def test_legacy_header_valid_key(): ws = _make_mock_websocket(headers={"x-session-api-key": "sk-oh-valid"}) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is True ws.accept.assert_called_once() @@ -109,9 +108,10 @@ async def test_legacy_header_valid_key(): @pytest.mark.asyncio async def test_legacy_header_invalid_key(): ws = _make_mock_websocket(headers={"x-session-api-key": "sk-oh-wrong"}) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False ws.close.assert_called_once_with(code=4001, reason="Authentication failed") @@ -126,9 +126,10 @@ async def test_first_message_auth_valid_key(): ws.receive_text.return_value = json.dumps( {"type": "auth", "session_api_key": "sk-oh-valid"} ) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is True ws.accept.assert_called_once() @@ -141,9 +142,10 @@ async def test_first_message_auth_invalid_key(): ws.receive_text.return_value = json.dumps( {"type": "auth", "session_api_key": "sk-oh-wrong"} ) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False ws.accept.assert_called_once() # accepted before reading first message @@ -156,9 +158,10 @@ async def test_first_message_auth_wrong_type_field(): ws.receive_text.return_value = json.dumps( {"type": "message", "session_api_key": "sk-oh-valid"} ) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False @@ -167,9 +170,10 @@ async def test_first_message_auth_wrong_type_field(): async def test_first_message_auth_missing_key_field(): ws = _make_mock_websocket() ws.receive_text.return_value = json.dumps({"type": "auth"}) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False @@ -177,9 +181,10 @@ async def test_first_message_auth_missing_key_field(): @pytest.mark.asyncio async def test_legacy_empty_key_rejected_even_if_listed(): ws = _make_mock_websocket() - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid", ""] - result = await _accept_authenticated_websocket(ws, session_api_key="") + config = MagicMock(session_api_keys=["sk-oh-valid", ""]) + result = await _accept_authenticated_websocket( + ws, session_api_key="", config=config + ) assert result is False ws.close.assert_called_once_with(code=4001, reason="Authentication failed") @@ -190,9 +195,10 @@ async def test_legacy_empty_key_rejected_even_if_listed(): async def test_first_message_auth_empty_key_rejected_even_if_listed(): ws = _make_mock_websocket() ws.receive_text.return_value = json.dumps({"type": "auth", "session_api_key": ""}) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid", ""] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid", ""]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False ws.close.assert_called_once_with(code=4001, reason="Authentication failed") @@ -202,9 +208,10 @@ async def test_first_message_auth_empty_key_rejected_even_if_listed(): async def test_first_message_auth_malformed_json(): ws = _make_mock_websocket() ws.receive_text.return_value = "not json at all" - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False ws.close.assert_called_once_with(code=4001, reason="Authentication failed") @@ -214,9 +221,10 @@ async def test_first_message_auth_malformed_json(): async def test_first_message_auth_client_disconnects(): ws = _make_mock_websocket() ws.receive_text.side_effect = WebSocketDisconnect() - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False @@ -230,14 +238,13 @@ async def slow_receive(): ws.receive_text.side_effect = slow_receive - with ( - patch("openhands.agent_server.sockets.get_config") as mock_config, - patch( - "openhands.agent_server.sockets._FIRST_MESSAGE_AUTH_TIMEOUT_SECONDS", 0.05 - ), + config = MagicMock(session_api_keys=["sk-oh-valid"]) + with patch( + "openhands.agent_server.sockets._FIRST_MESSAGE_AUTH_TIMEOUT_SECONDS", 0.05 ): - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - result = await _accept_authenticated_websocket(ws, session_api_key=None) + result = await _accept_authenticated_websocket( + ws, session_api_key=None, config=config + ) assert result is False ws.close.assert_called_once_with(code=4001, reason="Authentication failed") @@ -263,18 +270,13 @@ async def test_events_socket_first_message_auth_e2e(): mock_event_service.subscribe_to_events = AsyncMock(return_value=uuid4()) mock_event_service.unsubscribe_from_events = AsyncMock(return_value=True) - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) - await events_socket(uuid4(), ws, session_api_key=None) + await events_socket( + uuid4(), ws, session_api_key=None, config=config, conv_service=conv_service + ) ws.accept.assert_called_once() mock_event_service.subscribe_to_events.assert_called_once() @@ -309,19 +311,18 @@ async def test_events_socket_ignores_redundant_auth_control_frame(): mock_event_service.unsubscribe_from_events = AsyncMock(return_value=True) mock_event_service.send_message = AsyncMock() - with ( - patch( - "openhands.agent_server.sockets.get_conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - mock_conv_service.return_value.get_event_service = AsyncMock( - return_value=mock_event_service - ) - - # Authenticate via legacy query param so receive_text is never called. - await events_socket(uuid4(), ws, session_api_key="sk-oh-valid") + config = MagicMock(session_api_keys=["sk-oh-valid"]) + conv_service = MagicMock() + conv_service.get_event_service = AsyncMock(return_value=mock_event_service) + + # Authenticate via legacy query param so receive_text is never called. + await events_socket( + uuid4(), + ws, + session_api_key="sk-oh-valid", + config=config, + conv_service=conv_service, + ) # No ServerErrorEvent should be emitted for the auth control frame. ws.send_json.assert_not_called() @@ -341,10 +342,10 @@ async def test_events_socket_first_message_auth_rejected(): {"type": "auth", "session_api_key": "sk-oh-wrong"} ) - with patch("openhands.agent_server.sockets.get_config") as mock_config: - mock_config.return_value.session_api_keys = ["sk-oh-valid"] - - await events_socket(uuid4(), ws, session_api_key=None) + config = MagicMock(session_api_keys=["sk-oh-valid"]) + await events_socket( + uuid4(), ws, session_api_key=None, config=config, conv_service=MagicMock() + ) ws.accept.assert_called_once() # Should not proceed to subscribe @@ -392,20 +393,14 @@ async def test_bash_socket_ignores_redundant_auth_before_command( mock_bash_service.unsubscribe_from_events = AsyncMock(return_value=True) mock_bash_service.start_bash_command = AsyncMock() - with ( - patch( - "openhands.agent_server.sockets.get_server_bash_event_service", - return_value=mock_bash_service, - ), - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = session_api_keys - await bash_events_socket( - ws, - session_api_key=session_api_key, - resend_mode=None, - resend_all=False, - ) + await bash_events_socket( + ws, + session_api_key=session_api_key, + resend_mode=None, + resend_all=False, + config=MagicMock(session_api_keys=session_api_keys), + bash_service=mock_bash_service, + ) mock_bash_service.start_bash_command.assert_awaited_once() request = mock_bash_service.start_bash_command.await_args.args[0] @@ -431,20 +426,14 @@ async def test_bash_socket_validation_error_log_omits_payload(caplog): mock_bash_service.unsubscribe_from_events = AsyncMock(return_value=True) mock_bash_service.start_bash_command = AsyncMock() - with ( - patch( - "openhands.agent_server.sockets.get_server_bash_event_service", - return_value=mock_bash_service, - ), - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = [] - await bash_events_socket( - ws, - session_api_key=None, - resend_mode=None, - resend_all=False, - ) + await bash_events_socket( + ws, + session_api_key=None, + resend_mode=None, + resend_all=False, + config=MagicMock(session_api_keys=[]), + bash_service=mock_bash_service, + ) mock_bash_service.start_bash_command.assert_not_awaited() mock_bash_service.unsubscribe_from_events.assert_awaited_once() @@ -478,20 +467,14 @@ async def test_bash_socket_error_send_failure_log_omits_exceptions(caplog): mock_bash_service.unsubscribe_from_events = AsyncMock(return_value=True) mock_bash_service.start_bash_command = AsyncMock() - with ( - patch( - "openhands.agent_server.sockets.get_server_bash_event_service", - return_value=mock_bash_service, - ), - patch("openhands.agent_server.sockets.get_config") as mock_config, - ): - mock_config.return_value.session_api_keys = [] - await bash_events_socket( - ws, - session_api_key=None, - resend_mode=None, - resend_all=False, - ) + await bash_events_socket( + ws, + session_api_key=None, + resend_mode=None, + resend_all=False, + config=MagicMock(session_api_keys=[]), + bash_service=mock_bash_service, + ) mock_bash_service.start_bash_command.assert_not_awaited() mock_bash_service.unsubscribe_from_events.assert_awaited_once() diff --git a/tests/cross/test_remote_conversation_live_server.py b/tests/cross/test_remote_conversation_live_server.py index ec9519badc1..ea45ef01a82 100644 --- a/tests/cross/test_remote_conversation_live_server.py +++ b/tests/cross/test_remote_conversation_live_server.py @@ -28,7 +28,7 @@ from websockets.exceptions import ConnectionClosed, InvalidStatus from websockets.sync.client import connect -from openhands.agent_server.config import load_config +from openhands.agent_server.config import Config, load_config from openhands.sdk import LLM, Agent, AgentContext, Conversation, Message, TextContent from openhands.sdk.conversation import RemoteConversation from openhands.sdk.conversation.event_store import EventLog @@ -148,7 +148,7 @@ def test_deferred_websocket_initialization_lifecycle( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ): async def accept_app_backend_websocket( - websocket: WebSocket, _extension_name: str, _path: str + websocket: WebSocket, _extension_name: str, _path: str, _config: Config ) -> None: await websocket.accept() await websocket.close()