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 5e3666aff72..699a02570ef 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 @@ -920,7 +920,7 @@ Preconditions: event file, skips a top-level `acp/` directory, and redacts JSON/JSONL credential fields plus any Fernet token. Each request builds its own temp zip (`F14.trajectory-concurrent`) and reads `conversations_path` from the - live `app.state.config`, so it follows an init-time path + live 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/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/.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 9c8ad043ed2..d2cdfff62bc 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 28378c91d16..d3c57ba421f 100644 --- a/.agents/skills/verify-agent-server/scripts/control_agent_server.py +++ b/.agents/skills/verify-agent-server/scripts/control_agent_server.py @@ -3688,7 +3688,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" @@ -3708,7 +3708,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 8cb86433748..dbd910064e3 100644 --- a/openhands-agent-server/openhands/agent_server/_secrets_exposure.py +++ b/openhands-agent-server/openhands/agent_server/_secrets_exposure.py @@ -25,19 +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 Unavailable("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 83318866b3c..cd93b083de1 100644 --- a/openhands-agent-server/openhands/agent_server/acp_router.py +++ b/openhands-agent-server/openhands/agent_server/acp_router.py @@ -9,13 +9,13 @@ from collections.abc import Awaitable, Callable, Mapping from dataclasses import dataclass, field -from fastapi import APIRouter, Request +from fastapi import APIRouter, Depends, Request 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_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 from openhands.sdk.agent.acp_models import ACPModelDiscovery @@ -131,11 +131,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. @@ -149,7 +148,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. @@ -157,7 +159,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 NotSupported( "ACP model discovery runs only where conversations run in-process" @@ -169,7 +170,9 @@ async def discover_models( raise Unprocessable(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, secrets_store, 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 2fa6e3f622d..49227268d57 100644 --- a/openhands-agent-server/openhands/agent_server/agent_profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/agent_profiles_router.py @@ -15,24 +15,26 @@ import asyncio from typing import Annotated, Any -from fastapi import APIRouter, Path, Request, status +from fastapi import APIRouter, Depends, Path, status from pydantic import BaseModel, ConfigDict, Field, PlainValidator, ValidationError -from openhands.agent_server._secrets_exposure import ( +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.config import Config from openhands.agent_server.launch import ( can_probe_tools, server_launch_stores, target_launch_runtime, ) from openhands.agent_server.persistence import ( + FileSettingsStore, PersistedSettings, - get_agent_profile_store, - get_llm_profile_store, - get_settings_store, + ServerStores, ) from openhands.agent_server.profiles_router import MAX_PROFILES, _has_api_key from openhands.sdk.errors import Conflict, NotFound, OperationFailed, Unprocessable @@ -149,7 +151,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 +183,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 try: llm_store.load(SEED_PROFILE_NAME, cipher=cipher) return SEED_PROFILE_NAME @@ -222,9 +226,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 @@ -233,13 +235,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.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 @@ -253,15 +256,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 @@ -282,7 +286,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 @@ -294,21 +298,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 name = store.name_for_id(settings.active_agent_profile_id) @@ -331,18 +335,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. """ - config = get_config(request) - settings_store = get_settings_store(config) + 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, cipher) settings = settings_store.load() or settings summaries = store.list_summaries() @@ -354,14 +360,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( + name: ProfileName, + store: AgentProfileStore = Depends(get_agent_profile_store), +) -> 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() try: profile = store.load(name) except FileNotFoundError: @@ -394,7 +402,9 @@ def _validate_profile_payload( status_code=status.HTTP_201_CREATED, ) def save_agent_profile( - 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). @@ -405,7 +415,6 @@ def save_agent_profile( """ profile = _validate_profile_payload({**body, "name": name}) - store = get_agent_profile_store() # 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`` @@ -429,7 +438,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). @@ -439,14 +450,13 @@ 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() + store = stores.agent_profiles deleted_id = _summary_id_for_name(store, name) 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: @@ -467,7 +477,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, cipher) logger.info(f"Deleted agent profile '{name}'") return AgentProfileMutationResponse( @@ -479,7 +489,9 @@ def clear_pointer_and_reset_settings( "/{name}/rename", response_model=AgentProfileMutationResponse ) def rename_agent_profile( - name: ProfileName, body: RenameAgentProfileRequest + name: ProfileName, + body: RenameAgentProfileRequest, + store: AgentProfileStore = Depends(get_agent_profile_store), ) -> AgentProfileMutationResponse: """Rename a stored profile atomically. @@ -487,7 +499,6 @@ 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() try: store.rename(name, body.new_name) except FileNotFoundError: @@ -507,7 +518,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. @@ -515,16 +528,12 @@ 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() known_ids = { str(s["id"]) for s in store.list_summaries() if s.get("id") is not None } if profile_id not in known_ids: raise NotFound(f"Agent profile with id '{profile_id}' not found") - config = get_config(request) - settings_store = get_settings_store(config) - def set_pointer(settings: PersistedSettings) -> PersistedSettings: settings.active_agent_profile_id = profile_id return settings @@ -552,9 +561,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. @@ -568,20 +579,19 @@ 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 = stores.agent_profiles try: # Off the event loop: the store lock wait blocks (up to 30 s). profile = await asyncio.to_thread(store.load, name) except FileNotFoundError: raise NotFound(f"Agent profile '{name}' not found") - config = get_config(request) - settings = get_settings_store(config).load() or PersistedSettings() + 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 b115a755011..53180fe0080 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, Mapping, 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,12 +42,9 @@ V1_SESSION_API_KEY_ENV, Config, export_session_api_key, - get_default_config, + load_config, ) from openhands.agent_server.conversation_lease import _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, @@ -86,6 +84,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, @@ -111,10 +114,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.errors import DomainError from openhands.sdk.logger import DEBUG, get_logger @@ -314,28 +316,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 = 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(): @@ -414,29 +419,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( @@ -568,7 +579,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 +628,7 @@ def _find_http_exception( 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 +643,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. @@ -940,17 +951,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 c0bac11d3b1..7551fd564a5 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions/bridge.py @@ -277,7 +277,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 @@ -285,9 +285,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 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", "") @@ -299,15 +297,18 @@ 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 Misdirected("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", "") - config: Config = request.app.state.config if not _allowed_control_origin(config, origin): raise Forbidden("Origin is not allowed") if _origin(origin) == _origin(public_url): @@ -367,14 +368,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) @@ -389,8 +390,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 Unavailable( "Canvas App sessions require HTTPS or a loopback secure context" ) @@ -407,10 +409,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(request.app.state.config) - _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 @@ -421,6 +423,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)}/", @@ -429,21 +432,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(request.app.state.config) - _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(request.app.state.config) - _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) @@ -475,12 +478,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(websocket.app.state.config) - _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 1c4e9a00e00..c2092a23e45 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 from openhands.agent_server.errors import error_responses from openhands.sdk.errors import Conflict, Forbidden, Unauthenticated, Unavailable @@ -48,6 +49,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. @@ -57,7 +59,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( @@ -78,9 +80,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 @@ -95,8 +98,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( @@ -108,16 +112,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}") @@ -125,5 +131,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/canvas_extensions_router.py b/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py index 7a8498dac2c..41b289e1315 100644 --- a/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py +++ b/openhands-agent-server/openhands/agent_server/canvas_extensions_router.py @@ -39,6 +39,7 @@ CanvasExtensionManifest, ) from openhands.agent_server.errors import error_responses +from openhands.agent_server.server_context import server_context_of from openhands.sdk.errors import Conflict, Invalid, NotFound, Unprocessable from openhands.sdk.extensions.fetch import ExtensionFetchError from openhands.sdk.utils.redact import redact_url_credentials @@ -65,7 +66,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 614a13db5c4..ac5f3a255ad 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_router.py +++ b/openhands-agent-server/openhands/agent_server/conversation_router.py @@ -10,7 +10,6 @@ Depends, Header, Query, - Request, Response, status, ) @@ -18,7 +17,6 @@ from openhands.agent_server._secrets_exposure import ( decrypt_incoming_llm_secrets, - get_cipher, ) from openhands.agent_server.conversation_registry import ConversationRegistry from openhands.agent_server.conversation_service import ( @@ -26,7 +24,12 @@ 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_llm_profile_store, +) from openhands.agent_server.errors import error_responses from openhands.agent_server.event_service import CondensationUnavailable from openhands.agent_server.launch import launch_error @@ -38,7 +41,6 @@ ConversationInfo, ConversationPage, ConversationRuntimeInfo, - ConversationRuntimeStatus, ConversationSortOrder, ForkConversationRequest, NavigateConversationRequest, @@ -53,7 +55,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 ( @@ -70,6 +71,7 @@ UpstreamTimeout, ) from openhands.sdk.launch import AgentLaunchError, LaunchStoreError +from openhands.sdk.llm.llm_profile_store import LLMProfileStore from openhands.sdk.marketplace.registry import ( MarketplaceNotFoundError, PluginNotFoundError, @@ -78,6 +80,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"]) @@ -150,11 +153,8 @@ def _request_with_observability_headers( def _with_runtime_info( - request: Request, conversation: ConversationInfo + registry: ConversationRegistry, conversation: ConversationInfo ) -> ConversationInfo: - registry = getattr(request.app.state, "conversation_registry", None) - if not isinstance(registry, ConversationRegistry): - return conversation return conversation.model_copy( update={"runtime_info": registry.runtime_info(conversation.id)} ) @@ -163,7 +163,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"), @@ -182,6 +181,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( @@ -200,7 +200,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 @@ -224,9 +224,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) @@ -234,35 +234,30 @@ async def get_conversation( raise NotFound() 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 NotFound() - 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 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 ) @@ -289,16 +284,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: @@ -624,10 +619,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. @@ -641,7 +637,6 @@ async def switch_conversation_llm( if event_service is None: raise NotFound() 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,7 +645,7 @@ async def switch_conversation_llm( # reference or missing credential surfaces as 422 before the working LLM # is replaced. Inline-key configs without a provider_connection_id are # byte-identical to the old path. - llm = get_llm_profile_store().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/conversation_service.py b/openhands-agent-server/openhands/agent_server/conversation_service.py index 06821b8d84d..40497f73af5 100644 --- a/openhands-agent-server/openhands/agent_server/conversation_service.py +++ b/openhands-agent-server/openhands/agent_server/conversation_service.py @@ -40,6 +40,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, @@ -51,7 +54,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 ( @@ -88,8 +91,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 @@ -588,7 +592,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 @@ -801,14 +808,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, @@ -837,13 +867,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 @@ -1577,26 +1607,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) @@ -1609,7 +1620,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) @@ -2498,18 +2509,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, @@ -2517,8 +2519,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, @@ -2574,7 +2578,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 @@ -2769,6 +2778,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 @@ -2835,16 +2847,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}. " @@ -3139,20 +3146,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 cbc6caaa188..7e7e17f261a 100644 --- a/openhands-agent-server/openhands/agent_server/credential_binding.py +++ b/openhands-agent-server/openhands/agent_server/credential_binding.py @@ -5,9 +5,11 @@ from typing import Final from uuid import UUID -from fastapi import APIRouter, Request, Response, status +from fastapi import APIRouter, Depends, 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 from openhands.sdk.agent.acp_file_credentials import supports_file_credential_binding @@ -113,8 +115,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 request.app.state.conversation_service.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) @@ -126,7 +130,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 NotFound() @@ -136,7 +140,7 @@ async def activate_credential_binding( ) try: await _probe_binding(binding) - await request.app.state.conversation_service.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 f374b8edaa6..ae05cd0f7a7 100644 --- a/openhands-agent-server/openhands/agent_server/dependencies.py +++ b/openhands-agent-server/openhands/agent_server/dependencies.py @@ -1,16 +1,41 @@ +"""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, Request +from fastapi import Depends, 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 ( + 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.errors import NotFound, OperationFailed, Unauthenticated, Unavailable +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 # Cookie name used to authenticate the workspace static-file routes. @@ -25,25 +50,92 @@ ) -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. +# 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 context.config + + +def get_cipher(config: Annotated[Config, Depends(get_config)]) -> Cipher | None: + """The configured cipher (``None`` when ``OH_SECRET_KEY`` is unset).""" + return config.cipher + + +def get_stores(context: _Context) -> ServerStores: + """The persistent stores built for this server's current config. - 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. + For code that works across several stores; a route that uses one store + asks for that store. """ - config: Config = request.app.state.config + 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_agent_profile_store(stores: _Stores) -> AgentProfileStore: + return stores.agent_profiles + + +def get_conversation_registry(context: _Context) -> ConversationRegistry: + return context.conversation_registry + + +def get_vscode_service(context: _Context) -> VSCodeService | None: + """This server's VSCode service (``None`` when VSCode is disabled).""" + return context.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": + return WebSocketException(code=status.WS_1013_TRY_AGAIN_LATER, reason=detail) + return Unavailable(detail) + + +def check_session_api_key( + 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.""" if config.session_api_keys and session_api_key not in config.session_api_keys: raise Unauthenticated() 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. @@ -55,7 +147,6 @@ 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 if not config.session_api_keys: return for candidate in (header_key, cookie_key): @@ -64,14 +155,28 @@ def check_workspace_session( raise Unauthenticated() -def get_conversation_service(request: Request) -> ConversationService: - service = getattr(request.app.state, "conversation_service", None) +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, context: _Context +) -> BashEventService: + """The server-wide bash service (not a conversation runtime's own).""" + service = context.bash_event_service if service is None: - raise Unavailable("Conversation service is not available") + 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 @@ -98,21 +203,19 @@ 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 Unavailable("Bash event service is not available") - return service + # 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 = getattr(request.app.state, "conversation_registry", None) - if isinstance( - registry, ConversationRegistry - ) and registry.serves_persisted_event_reads(conversation_id): + 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 8ffbe276c84..800def79f37 100644 --- a/openhands-agent-server/openhands/agent_server/docker_runtime/registry.py +++ b/openhands-agent-server/openhands/agent_server/docker_runtime/registry.py @@ -30,8 +30,7 @@ ConversationRuntimeInfo, ConversationRuntimeStatus, ) -from openhands.agent_server.persistence import get_settings_store -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 @@ -128,11 +127,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) @@ -622,7 +620,7 @@ async def _evict_idle_runtimes(self, ttl_seconds: float) -> None: def _experimental_features(self) -> dict[str, bool]: """The user's experimental-feature switches, for a new container.""" try: - settings = get_settings_store(self.config).load() + settings = self.stores.settings.load() except (PermissionError, OSError): logger.warning( "Cannot read settings; the runtime starts with features off", 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 dff5df6f87e..491c12b8fda 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,17 @@ from uuid import UUID, uuid4 import httpx -from fastapi import APIRouter, Query, Request, WebSocket +from fastapi import APIRouter, Depends, Query, Request, WebSocket from starlette.responses import JSONResponse, Response, StreamingResponse -from openhands.agent_server.dependencies import get_conversation_service +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, +) from openhands.agent_server.docker_runtime.mediation import materialize_secrets from openhands.agent_server.docker_runtime.proxy import ( bridge_websocket, @@ -40,7 +47,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.errors import ( @@ -67,8 +74,7 @@ logger = get_logger(__name__) -def get_registry(request: Request) -> DockerConversationRegistry: - registry = request.app.state.conversation_registry +def get_registry(registry: ConversationRegistry) -> DockerConversationRegistry: if not isinstance(registry, DockerConversationRegistry): raise Unavailable("Docker conversation runtime is unavailable") return registry @@ -144,6 +150,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()) @@ -153,13 +161,11 @@ async def start_conversation( raise Unprocessable("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) @@ -301,7 +307,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() @@ -314,7 +320,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, ) ) @@ -332,18 +338,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 NotFound("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 NotFound("Conversation not found") identity = registry.provisioning.load(conversation_id) @@ -351,16 +361,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 NotFound("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 UpstreamFailed("Could not release conversation runtime") from exc @@ -371,9 +383,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 @@ -381,8 +394,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 NotFound("Conversation not found") @@ -407,9 +424,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) @@ -420,9 +435,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( @@ -430,7 +450,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", @@ -439,11 +463,11 @@ async def proxy_conversation( "switch_profile", }: raise NotSupported("This operation is unavailable in Docker runtime mode") - registry = get_registry(request) + registry = get_registry(conversation_registry) if tail == "reactions" and request.method == "POST": # The runtime's server has no settings of its own to check: the # user's switch lives here and follows the switch at once. - await require_reactions_enabled(registry.config) + await require_reactions_enabled(registry.stores.settings) container = await _container(registry, conversation_id) upstream_path = _upstream_path( request, @@ -483,9 +507,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 @@ -494,9 +516,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, @@ -518,12 +543,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 = websocket.app.state.conversation_registry if not isinstance(registry, DockerConversationRegistry): await websocket.close(code=1011) return @@ -548,9 +575,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( - conversation_id - ) + await conversation_service.refresh_persisted_conversation(conversation_id) @docker_sockets_router.websocket("/events/{conversation_id}") @@ -558,8 +583,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}") @@ -567,5 +603,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 ff8a2ff3fa7..fc6f241d7cd 100644 --- a/openhands-agent-server/openhands/agent_server/event_router.py +++ b/openhands-agent-server/openhands/agent_server/event_router.py @@ -14,15 +14,15 @@ Depends, Query, ) -from starlette.requests import Request from starlette.responses import JSONResponse -from openhands.agent_server._secrets_exposure import get_config -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_conversation_registry, get_conversation_service, get_event_service, + get_settings_store, ) from openhands.agent_server.errors import error_responses from openhands.agent_server.event_service import EventService @@ -38,7 +38,11 @@ Success, ) from openhands.agent_server.openai.usage import token_usage_delta -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import ( + FileSettingsStore, + PersistedSettings, + SettingsStore, +) from openhands.sdk import Message from openhands.sdk.conversation.state import ConversationExecutionStatus from openhands.sdk.errors import ( @@ -257,12 +261,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 @@ -374,11 +378,11 @@ async def respond_to_confirmation( ) -async def require_reactions_enabled(config: Config) -> None: +async def require_reactions_enabled(settings_store: SettingsStore) -> None: """Refuse with 409 unless the user has conversation reactions on.""" try: settings = await asyncio.to_thread( - lambda: get_settings_store(config).load() or PersistedSettings() + lambda: settings_store.load() or PersistedSettings() ) except (PermissionError, OSError): raise Conflict("Cannot read the settings file to check the feature flag") @@ -398,8 +402,8 @@ async def require_reactions_enabled(config: Config) -> None: ) async def react_to_message( request: ReactionRequest, - http_request: Request, event_service: EventService = Depends(get_event_service), + settings_store: FileSettingsStore = Depends(get_settings_store), ) -> ReactionEvent: """Add or remove the user's emoji reaction on a message. @@ -414,7 +418,7 @@ async def react_to_message( # A Docker runtime's server finds the user's switch on the host, which # checks it before forwarding the request. if host_experimental_features() is None: - await require_reactions_enabled(get_config(http_request)) + await require_reactions_enabled(settings_store) try: return await event_service.react( request.emoji, request.target_event_id, removed=request.removed diff --git a/openhands-agent-server/openhands/agent_server/experimental_features_router.py b/openhands-agent-server/openhands/agent_server/experimental_features_router.py index d6eb44cae1b..537086d5222 100644 --- a/openhands-agent-server/openhands/agent_server/experimental_features_router.py +++ b/openhands-agent-server/openhands/agent_server/experimental_features_router.py @@ -2,9 +2,9 @@ import asyncio -from fastapi import APIRouter, Request +from fastapi import APIRouter, Depends -from openhands.agent_server._secrets_exposure import get_config +from openhands.agent_server.dependencies import get_settings_store from openhands.agent_server.errors import error_responses from openhands.agent_server.experimental_features import ( ExperimentalFeature, @@ -13,7 +13,7 @@ find_experimental_feature, is_enabled, ) -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import FileSettingsStore, PersistedSettings from openhands.sdk.errors import Conflict, NotFound @@ -23,9 +23,10 @@ @experimental_features_router.get("", response_model=list[ExperimentalFeature]) -async def list_experimental_features(request: Request) -> list[ExperimentalFeature]: +async def list_experimental_features( + store: FileSettingsStore = Depends(get_settings_store), +) -> list[ExperimentalFeature]: """List the features the user can turn on or off, and whether each is on.""" - store = get_settings_store(get_config(request)) settings = await asyncio.to_thread(lambda: store.load() or PersistedSettings()) return experimental_feature_flags(settings.experimental_features) @@ -41,7 +42,9 @@ async def list_experimental_features(request: Request) -> list[ExperimentalFeatu ), ) def update_experimental_feature( - request: Request, key: str, payload: ExperimentalFeatureUpdate + key: str, + payload: ExperimentalFeatureUpdate, + store: FileSettingsStore = Depends(get_settings_store), ) -> ExperimentalFeature: """Turn a feature on or off.""" feature = find_experimental_feature(key) @@ -55,7 +58,6 @@ def apply(settings: PersistedSettings) -> PersistedSettings: } return settings - store = get_settings_store(get_config(request)) try: settings = store.update(apply) except RuntimeError: diff --git a/openhands-agent-server/openhands/agent_server/file_router.py b/openhands-agent-server/openhands/agent_server/file_router.py index 00d076b29cb..8985003e3ba 100644 --- a/openhands-agent-server/openhands/agent_server/file_router.py +++ b/openhands-agent-server/openhands/agent_server/file_router.py @@ -19,9 +19,9 @@ from fastapi import ( APIRouter, + Depends, File, Query, - Request, UploadFile, ) from fastapi.responses import FileResponse @@ -34,6 +34,7 @@ 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.errors import ( @@ -933,10 +934,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: Config = request.app.state.config conversation_dir = config.conversations_path / conversation_id.hex if not conversation_dir.is_dir(): 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 1e7cd9178ef..7d61adf6610 100644 --- a/openhands-agent-server/openhands/agent_server/git_provider_service.py +++ b/openhands-agent-server/openhands/agent_server/git_provider_service.py @@ -8,8 +8,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, @@ -75,7 +74,7 @@ def __init__( async def search_provider_repositories( - config: Config, + secrets_store: SecretsStore, provider: GitProvider, *, query: str | None = None, @@ -88,7 +87,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 @@ -111,7 +110,7 @@ async def search_provider_repositories( async def get_provider_pull_request( - config: Config, + secrets_store: SecretsStore, provider: GitProvider, owner: str, repo: str, @@ -122,7 +121,7 @@ async def get_provider_pull_request( raise UnsupportedGitProviderError( f"Pull requests are not supported for provider '{provider.value}'" ) - token = _resolve_provider_token(config, provider) + token = _resolve_provider_token(secrets_store, provider) if token is None: raise MissingGitProviderTokenError(f"No {provider.value} token is configured") @@ -298,10 +297,11 @@ def _badge_api_error(response: httpx.Response) -> GitProviderAPIError: ) -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 13e86a00d9b..e7c16323fb6 100644 --- a/openhands-agent-server/openhands/agent_server/git_router.py +++ b/openhands-agent-server/openhands/agent_server/git_router.py @@ -6,9 +6,9 @@ from pathlib import Path from typing import Annotated -from fastapi import APIRouter, Path as PathParam, Query, Request +from fastapi import APIRouter, Depends, Path as PathParam, Query -from openhands.agent_server._secrets_exposure import get_config +from openhands.agent_server.dependencies import get_secrets_store from openhands.agent_server.errors import error_for_status, error_responses from openhands.agent_server.git_provider_service import ( GitProviderAPIError, @@ -17,6 +17,7 @@ get_provider_pull_request, 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.errors import Conflict, Invalid, RateLimited, Unprocessable from openhands.sdk.git.exceptions import GitError, GitRepositoryError @@ -140,7 +141,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") @@ -151,11 +151,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_config(request), + secrets_store, provider, query=query, limit=limit, @@ -188,20 +189,20 @@ async def git_repositories_search( ), ) async def git_pull_request_status( - request: Request, owner: Annotated[str, PathParam(pattern=_OWNER_REPO_PATTERN)], repo: Annotated[str, PathParam(pattern=_OWNER_REPO_PATTERN)], number: Annotated[int, PathParam(ge=1)], provider: Annotated[ GitProvider, Query(description="Git provider hosting the repository") ], + secrets_store: FileSecretsStore = Depends(get_secrets_store), ) -> GitPullRequestStatus: """Read a pull request's state and the combined CI result of its head commit.""" if owner in _DOT_SEGMENTS or repo in _DOT_SEGMENTS: raise Unprocessable("'owner' and 'repo' must not be '.' or '..'") try: return await get_provider_pull_request( - get_config(request), provider, owner, repo, number + secrets_store, provider, owner, repo, number ) except UnsupportedGitProviderError as e: raise Invalid(str(e)) from e diff --git a/openhands-agent-server/openhands/agent_server/init_router.py b/openhands-agent-server/openhands/agent_server/init_router.py index 3666b464461..905da480cc7 100644 --- a/openhands-agent-server/openhands/agent_server/init_router.py +++ b/openhands-agent-server/openhands/agent_server/init_router.py @@ -14,6 +14,8 @@ import asyncio import os +from contextlib import AsyncExitStack +from dataclasses import dataclass, replace from pathlib import Path from typing import Any, ClassVar, Literal @@ -38,18 +40,22 @@ 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.dependencies import get_config +from openhands.agent_server.persistence import ServerStores, reencrypt_stale_stores +from openhands.agent_server.server_context import ( + ServerContext, + install_server_context, + server_context_of, +) from openhands.agent_server.server_details_router import mark_initialization_complete from openhands.agent_server.telemetry import ( build_telemetry_sink, emit_server_started, shutdown_telemetry_sink, ) -from openhands.agent_server.vscode_service import get_vscode_service from openhands.sdk.errors import ( Invalid, NotFound, @@ -225,12 +231,37 @@ 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. 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. + + 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: @@ -239,15 +270,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 - # ``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, - ) + # What the committed initialisation started, for ``teardown`` to stop. + self._started: AsyncExitStack | None = None @property def state(self) -> InitState: @@ -267,116 +291,129 @@ async def initialize(self, req: InitRequest) -> InitStatus: self._state = "initializing" self._error = None try: - 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 OperationFailed(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() - # 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) + stores = ServerStores.from_config(new_config) + await asyncio.to_thread(reencrypt_stale_stores, stores) + + # 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) + 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 started.enter_async_context(bash_svc) + await started.enter_async_context(service) + started.push_async_callback(conversation_registry.shutdown) 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 - - # 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() + + # 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] ) - 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 OperationFailed(self._error) from exc + 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() + ) + + def _commit(self, prepared: _PreparedInit) -> None: + """Make a prepared initialisation the running server. + + 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. """ - 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 - 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 + started, self._started = self._started, None + if started is not None: + # Registry, then the conversation service, then the bash service, + # then the environment. + await started.aclose() def get_init_service(request: Request) -> InitService: @@ -390,16 +427,15 @@ 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: 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: + 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 Unauthenticated() diff --git a/openhands-agent-server/openhands/agent_server/launch.py b/openhands-agent-server/openhands/agent_server/launch.py index cb0adbc7866..c881932d26a 100644 --- a/openhands-agent-server/openhands/agent_server/launch.py +++ b/openhands-agent-server/openhands/agent_server/launch.py @@ -19,11 +19,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, @@ -68,9 +64,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, @@ -79,7 +75,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 8cfeaa4f386..cc8599cc001 100644 --- a/openhands-agent-server/openhands/agent_server/mcp_router.py +++ b/openhands-agent-server/openhands/agent_server/mcp_router.py @@ -35,15 +35,15 @@ import httpx import mcp.types -from fastapi import APIRouter, Request +from fastapi import APIRouter, Depends 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_secrets_store from openhands.agent_server.mcp_oauth_store import ( InMemoryMCPOAuthTokenStore, ) -from openhands.agent_server.persistence import get_secrets_store +from openhands.agent_server.persistence import FileSecretsStore from openhands.sdk.errors import Conflict, Invalid, NotFound from openhands.sdk.logger import get_logger from openhands.sdk.mcp import create_mcp_tools @@ -822,13 +822,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 ``http_request.app.state``. - cipher = get_cipher(http_request) - secrets_store = get_secrets_store(get_config(http_request)) loop = asyncio.get_running_loop() return await loop.run_in_executor( None, @@ -882,13 +880,13 @@ 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: raise Invalid("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 bcfd5cb0d3b..2c114930cdb 100644 --- a/openhands-agent-server/openhands/agent_server/meta_profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/meta_profiles_router.py @@ -6,14 +6,11 @@ from typing import Annotated -from fastapi import APIRouter, Path, Request, status +from fastapi import APIRouter, Depends, Path, 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_settings_store +from openhands.agent_server.persistence import FileSettingsStore, PersistedSettings from openhands.sdk.errors import ( Conflict, NotFound, @@ -98,10 +95,8 @@ class ActivateMetaProfileResponse(BaseModel): 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: - config = get_config(request) - settings_store = get_settings_store(config) settings = settings_store.load() or PersistedSettings() if settings.active_meta_profile != old_name: return False @@ -125,10 +120,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.""" - config = get_config(request) - settings_store = get_settings_store(config) settings = settings_store.load() or PersistedSettings() store = _get_meta_profile_store() @@ -183,7 +178,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). @@ -192,7 +188,7 @@ def delete_meta_profile( """ store = _get_meta_profile_store() 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( @@ -204,7 +200,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``. @@ -222,8 +219,6 @@ def activate_meta_profile( except FileNotFoundError: raise NotFound(f"Meta-profile '{name}' not found") - config = get_config(request) - settings_store = get_settings_store(config) 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 9a9171dc9c4..4d698dcb973 100644 --- a/openhands-agent-server/openhands/agent_server/openai/router.py +++ b/openhands-agent-server/openhands/agent_server/openai/router.py @@ -4,14 +4,20 @@ from typing import Annotated from uuid import UUID -from fastapi import APIRouter, Depends, Header, Request, Response +from fastapi import APIRouter, Depends, Header, Response 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_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, @@ -26,12 +32,13 @@ run_chat_completion, run_response, ) +from openhands.agent_server.persistence import ServerStores from openhands.sdk.conversation.types import ( ConversationObservabilityMetadata, ConversationObservabilitySpanName, ConversationObservabilityTags, ) -from openhands.sdk.errors import Unauthenticated, Unavailable, Unprocessable +from openhands.sdk.errors import Unauthenticated, Unprocessable openai_router = APIRouter(tags=["OpenAI Compatibility"]) @@ -44,7 +51,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: @@ -57,10 +64,9 @@ 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 if not config.session_api_keys: return bearer_token = authorization.credentials if authorization else None @@ -71,15 +77,9 @@ def check_openai_api_key( raise Unauthenticated() -def _get_config(request: Request) -> Config: - config = getattr(request.app.state, "config", None) - if not isinstance(config, Config): - raise Unavailable("Agent server config is not available") - return config - - -def _get_docker_registry(request: Request) -> DockerConversationRegistry | None: - registry = getattr(request.app.state, "conversation_registry", None) +def _get_docker_registry( + registry: ConversationRegistry, +) -> DockerConversationRegistry | None: return registry if isinstance(registry, DockerConversationRegistry) else None @@ -118,9 +118,10 @@ 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() +async def get_openai_models( + stores: ServerStores = Depends(get_stores), +) -> OpenAIModelListResponse: + return await list_openai_models(stores) @openai_router.post( @@ -130,7 +131,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") @@ -148,13 +148,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), + 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 +191,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,12 +205,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), + 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/openai/service.py b/openhands-agent-server/openhands/agent_server/openai/service.py index d0dd69da292..f5fc7940685 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, @@ -72,6 +68,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 @@ -108,9 +105,11 @@ def _profile_name_from_model(model: str) -> str: raise NotFound(f"Unknown OpenHands model '{model}'. Use GET /v1/models.") -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 NotFound(f"Profile '{profile_name}' not found") except TimeoutError: @@ -248,14 +247,15 @@ 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 Conflict("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. @@ -285,6 +285,7 @@ def _conversation_request( *, request: OpenAIChatCompletionRequest, config: Config, + stores: ServerStores, conversation_id: UUID | None, ) -> StartConversationRequest: user_message = _latest_user_message(request.messages) @@ -293,6 +294,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, ) @@ -302,6 +304,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( @@ -309,6 +312,7 @@ def _response_conversation_request( system_text=_response_system_text(request), user_content=user_content, config=config, + stores=stores, conversation_id=conversation_id, ) @@ -491,9 +495,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 Unavailable("Profile store is busy. Please retry.") data = [ @@ -701,6 +705,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, @@ -713,6 +718,7 @@ async def run_chat_completion( start_request = _conversation_request( request=request, config=config, + stores=stores, conversation_id=reusable_conversation_id, ) if observability_overrides: @@ -755,6 +761,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, @@ -776,6 +783,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 9751e53ccc1..0d6a0ed47b8 100644 --- a/openhands-agent-server/openhands/agent_server/profiles_router.py +++ b/openhands-agent-server/openhands/agent_server/profiles_router.py @@ -3,25 +3,28 @@ import asyncio from typing import Annotated, Any -from fastapi import APIRouter, Path, Request, status +from fastapi import APIRouter, Depends, Path, Request, status from pydantic import BaseModel, Field, SecretStr 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, translate_missing_cipher, ) -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.errors import Conflict, NotFound, OperationFailed from openhands.sdk.llm import LLM, Message, TextContent from openhands.sdk.llm.exceptions import ( @@ -33,6 +36,7 @@ ) from openhands.sdk.llm.llm_profile_store import ( PROFILE_NAME_PATTERN, + LLMProfileStore, ProfileLimitExceeded, canonical_profile_name, ) @@ -42,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 @@ -109,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 @@ -120,21 +128,15 @@ 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}. - connection = get_provider_connections_store(config).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: - config = get_config(request) - settings_store = get_settings_store(config) settings = settings_store.load() or PersistedSettings() active = settings.active_profile # ``foo.json`` addresses profile ``foo`` in the store, so compare store keys. @@ -154,18 +156,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. """ - config = get_config(request) - settings_store = get_settings_store(config) settings = settings_store.load() or PersistedSettings() - store = get_llm_profile_store() summaries = store.list_summaries() return ProfileListResponse( @@ -175,7 +177,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: @@ -185,9 +195,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_llm_profile_store() try: # Runtime reads need current provider credentials. Editor reads # retain the stored reference, including dangling references. @@ -206,7 +214,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), ) @@ -216,9 +226,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. @@ -231,9 +242,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_llm_profile_store() try: store.save( name, @@ -317,9 +326,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,12 +346,11 @@ 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. - llm = get_llm_profile_store().resolve_provider_connection(llm, cipher=cipher) + llm = store.resolve_provider_connection(llm, cipher=cipher) messages = [ Message( @@ -413,19 +422,22 @@ 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_llm_profile_store() - agent_store = get_agent_profile_store() try: delete_llm_profile(agent_store, store, name) except ProfileReferenced as e: raise Conflict(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") @@ -433,9 +445,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. @@ -446,8 +460,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_llm_profile_store() - agent_store = get_agent_profile_store() try: rename_llm_profile(agent_store, store, name, body.new_name) except FileNotFoundError: @@ -456,7 +468,7 @@ def rename_profile( raise Conflict(f"Profile '{body.new_name}' already exists") 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}'") @@ -477,7 +489,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: @@ -493,14 +510,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) - 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() # A dangling provider_connection_id raises ProviderConnectionNotFound (422). try: llm = profile_store.load(name, cipher=cipher, strict=True) @@ -509,7 +522,6 @@ def activate_profile(request: Request, name: ProfileName) -> ActivateProfileResp except FileNotFoundError: raise NotFound(f"Profile '{name}' not found") - settings_store = get_settings_store(config) 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 58571045cbb..1d27b0d9c09 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, Request, status +from fastapi import APIRouter, Depends, status from pydantic import ( BaseModel, ConfigDict, @@ -25,15 +25,15 @@ model_validator, ) -from openhands.agent_server._secrets_exposure import ( +from openhands.agent_server.dependencies import ( get_cipher, - get_config, + get_provider_connections_store, + get_stores, ) from openhands.agent_server.persistence import ( ProviderConnection, - get_llm_profile_store, - get_provider_connections_store, - get_settings_store, + ProviderConnectionStore, + ServerStores, ) from openhands.sdk.errors import Conflict, NotFound, Unprocessable from openhands.sdk.llm.provider_connection_store import ( @@ -42,6 +42,7 @@ ) 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 @@ -119,16 +120,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 +139,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 @@ -162,10 +167,9 @@ def _raise_if_connection_is_referenced(config, connection_id: str) -> None: @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_provider_connections_store(get_config(request)) connections = store.list(cipher=cipher) return [_to_response(c) for c in connections] @@ -174,10 +178,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_provider_connections_store(get_config(request)) now = _now() connection = ProviderConnection( id=uuid.uuid4().hex, @@ -202,14 +206,15 @@ 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: raise Unprocessable("Provide at least one provider connection field to update") - cipher = get_cipher(request) - store = get_provider_connections_store(get_config(request)) connection = store.get(connection_id, cipher=cipher) if connection is None: raise NotFound(f"Provider connection '{connection_id}' not found") @@ -245,15 +250,15 @@ 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: - config = get_config(request) - cipher = get_cipher(request) - store = get_provider_connections_store(config) + store = stores.provider_connections connection = store.get(connection_id, cipher=cipher) if connection is None: raise NotFound(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. try: diff --git a/openhands-agent-server/openhands/agent_server/runtime_router.py b/openhands-agent-server/openhands/agent_server/runtime_router.py index 6b6dc4f9733..a5f7a394276 100644 --- a/openhands-agent-server/openhands/agent_server/runtime_router.py +++ b/openhands-agent-server/openhands/agent_server/runtime_router.py @@ -15,9 +15,13 @@ 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, ) from openhands.agent_server.file_router import file_router from openhands.agent_server.git_router import runtime_git_router @@ -26,15 +30,18 @@ get_vscode_url, vscode_router, ) +from openhands.agent_server.vscode_service import VSCodeService from openhands.sdk.errors import NotFound, Unprocessable 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 NotFound("Conversation not found") @@ -96,9 +103,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 new file mode 100644 index 00000000000..7829793e52b --- /dev/null +++ b/openhands-agent-server/openhands/agent_server/server_context.py @@ -0,0 +1,92 @@ +"""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 +from openhands.agent_server.vscode_service import ( + VSCodeService, + create_vscode_service, +) + + +# 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``.""" + 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: + """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), + vscode_service=create_vscode_service(config), + ) + + +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..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,10 +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 ( + 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 @@ -182,8 +186,10 @@ def build_server_info( @server_details_router.get("/server_info") -async def get_server_info(request: Request) -> ServerInfo: - config = request.app.state.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 7584fe2e9e0..88b38600d6a 100644 --- a/openhands-agent-server/openhands/agent_server/session_socket.py +++ b/openhands-agent-server/openhands/agent_server/session_socket.py @@ -15,13 +15,16 @@ 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 import sockets +from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ( + ConversationService, CredentialBindingActivationRequired, ) +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, @@ -38,7 +41,6 @@ from openhands.agent_server.sockets import ( _accept_authenticated_websocket, _close_subscribed_websocket, - _get_conversation_service, _is_auth_control_message, _is_websocket_connected, _safe_close_websocket, @@ -390,13 +392,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 e863c9810a7..d8f07de8cd3 100644 --- a/openhands-agent-server/openhands/agent_server/settings_router.py +++ b/openhands-agent-server/openhands/agent_server/settings_router.py @@ -2,24 +2,29 @@ from functools import lru_cache from typing import cast -from fastapi import APIRouter, Request, Response, status +from fastapi import APIRouter, Depends, Request, Response, status from pydantic import ValidationError from openhands.agent_server._secrets_exposure import ( build_expose_context, - get_cipher, - get_config, parse_expose_secrets_header, profile_key_mismatch, translate_missing_cipher, ) -from openhands.agent_server.persistence import ( - SECRET_NAME_PATTERN, - PersistedSettings, +from openhands.agent_server.config import Config +from openhands.agent_server.dependencies import ( get_agent_profile_store, - get_llm_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 @@ -31,12 +36,14 @@ Unprocessable, ) 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, @@ -48,6 +55,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, @@ -123,7 +131,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, @@ -155,8 +167,6 @@ async def get_settings(request: Request) -> SettingsResponse: values. """ expose_mode = parse_expose_secrets_header(request) - config = get_config(request) - store = get_settings_store(config) settings = store.load() or PersistedSettings() # Audit log all settings access for security visibility @@ -191,7 +201,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. @@ -248,14 +261,16 @@ def update_settings( "must be provided" ) - _require_pointer_targets(update_data) + _require_pointer_targets(stores, update_data) return _apply_settings_update( request, + config, + stores, 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 @@ -263,7 +278,7 @@ def _require_pointer_targets(update_data: dict) -> None: """ profile_id = update_data.get("active_agent_profile_id") if profile_id is not None: - 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 NotFound(f"Agent profile with id '{profile_id}' not found") @@ -276,7 +291,9 @@ def _require_pointer_targets(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") @@ -285,8 +302,6 @@ def _load_active_profile_llm( if not profile_name or explicit_llm_diff: return None - cipher = get_cipher(request) - profile_store = get_llm_profile_store() # ``load`` resolves any referenced provider connection (read-at-use); a # dangling reference raises ProviderConnectionNotFound (422). A key the # cipher cannot decrypt is refused @@ -325,10 +340,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 @@ -341,8 +360,7 @@ def apply_update(settings: PersistedSettings) -> PersistedSettings: settings.update(applied_update, context=context) return settings - config = get_config(request) - store = get_settings_store(config) + store = stores.settings client_host = request.client.host if request.client else "unknown" try: settings = store.update(apply_update) @@ -419,6 +437,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( @@ -433,6 +453,8 @@ async def create_mcp_server( ) return _apply_settings_update( request, + config, + stores, update_data, lambda settings: _require_mcp_server_absent(settings, settings_key), ) @@ -443,6 +465,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( @@ -456,13 +480,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, @@ -470,6 +501,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), ) @@ -480,16 +513,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.""" - config = get_config(request) - store = get_secrets_store(config) secrets = store.load() allowed_names: set[str] | None = None if agent_profile_id is not None: - profile_store = get_agent_profile_store() # The store raises Invalid for an unloadable profile (400) and # LockTimeout for a busy store (503), as on the agent-profile routes. profile_name = profile_store.name_for_id(agent_profile_id) @@ -519,7 +552,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 @@ -530,8 +567,6 @@ async def get_secret_value(request: Request, name: str) -> Response: """ _validate_secret_name(name) - config = get_config(request) - store = get_secrets_store(config) value = store.get_secret(name) client_host = request.client.host if request.client else "unknown" @@ -553,7 +588,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). @@ -566,9 +603,6 @@ async def create_secret( if not value.strip() or is_redacted_secret(value): raise Unprocessable("Secret value must not be empty or redacted") - config = get_config(request) - store = get_secrets_store(config) - try: store.set_secret( name=secret.name, @@ -599,7 +633,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: @@ -608,9 +646,6 @@ async def delete_secret(request: Request, name: str) -> dict[str, bool]: """ _validate_secret_name(name) - config = get_config(request) - store = get_secrets_store(config) - 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 b7864027871..75fa5311003 100644 --- a/openhands-agent-server/openhands/agent_server/skills_router.py +++ b/openhands-agent-server/openhands/agent_server/skills_router.py @@ -6,9 +6,11 @@ from typing import Annotated, Literal -from fastapi import APIRouter, Path, Request +from fastapi import APIRouter, Depends, 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.errors import error_responses from openhands.agent_server.skills_service import ( ExposedUrlData, @@ -277,7 +279,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 @@ -306,8 +310,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 = 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 5488b099803..62818fe3ccc 100644 --- a/openhands-agent-server/openhands/agent_server/sockets.py +++ b/openhands-agent-server/openhands/agent_server/sockets.py @@ -22,21 +22,23 @@ from fastapi import ( APIRouter, + Depends, Query, WebSocket, WebSocketDisconnect, ) 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.bash_service import BashEventService +from openhands.agent_server.config import 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 +60,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, @@ -139,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. @@ -151,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. @@ -263,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. @@ -283,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: @@ -413,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. @@ -424,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_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..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,10 +8,11 @@ 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 from openhands.sdk.hooks.config import HookConfig @@ -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: Config = http_request.app.state.config 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..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 get_default_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,20 +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() -> ToolPreloadService | None: - """Get the tool preload service instance if preload is enabled.""" - global _tool_preload_service - config = get_default_config() - - 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/tool_router.py b/openhands-agent-server/openhands/agent_server/tool_router.py index ee7c42d9346..49d822a0a09 100644 --- a/openhands-agent-server/openhands/agent_server/tool_router.py +++ b/openhands-agent-server/openhands/agent_server/tool_router.py @@ -2,7 +2,7 @@ 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 @@ -13,6 +13,7 @@ # available to agents this server runs. import openhands.tools.reaction # 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, @@ -51,13 +52,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: Config = request.app.state.config 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 ca6f74ff14e..7aa1578695d 100644 --- a/openhands-agent-server/openhands/agent_server/vscode_router.py +++ b/openhands-agent-server/openhands/agent_server/vscode_router.py @@ -1,9 +1,10 @@ """VSCode router for agent server API endpoints.""" -from fastapi import APIRouter +from fastapi import APIRouter, Depends from pydantic import BaseModel -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.errors import OperationFailed, Unavailable from openhands.sdk.logger import get_logger @@ -21,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", + vscode_service: VSCodeService | None = Depends(get_vscode_service), ) -> VSCodeUrlResponse: """Get the VSCode URL with authentication token. @@ -36,7 +39,6 @@ async def get_vscode_url( Returns: VSCode URL with token if available, None otherwise """ - vscode_service = get_vscode_service() if vscode_service is None: raise Unavailable( "VSCode is disabled in configuration. Set enable_vscode=true to enable." @@ -51,13 +53,14 @@ async def get_vscode_url( @vscode_router.get("/status") -async def get_vscode_status() -> dict[str, bool | str]: +async def get_vscode_status( + 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() 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..6a8b7c58d83 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 @@ -296,34 +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() -> VSCodeService | None: - """Get the global VSCode service instance. +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: - 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 - 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/openhands-agent-server/openhands/agent_server/workspaces_router.py b/openhands-agent-server/openhands/agent_server/workspaces_router.py index 0a9a5493d11..b41fd7b63f3 100644 --- a/openhands-agent-server/openhands/agent_server/workspaces_router.py +++ b/openhands-agent-server/openhands/agent_server/workspaces_router.py @@ -8,15 +8,15 @@ from typing import Annotated -from fastapi import APIRouter, Query, Request +from fastapi import APIRouter, Depends, Query from pydantic import BaseModel, ConfigDict, Field -from openhands.agent_server._secrets_exposure import get_config +from openhands.agent_server.dependencies import get_workspaces_store from openhands.agent_server.persistence import ( + FileWorkspacesStore, PersistedWorkspaces, WorkspaceItem, WorkspaceParentItem, - get_workspaces_store, ) from openhands.sdk.errors import Conflict, NotFound, OperationFailed from openhands.sdk.logger import get_logger @@ -61,10 +61,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.""" - config = get_config(request) - store = get_workspaces_store(config) try: persisted = store.load() except (OSError, PermissionError): @@ -87,11 +87,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``.""" - config = get_config(request) - store = get_workspaces_store(config) def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: # Dedupe against the existing list AND within the incoming payload, @@ -124,12 +123,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.""" - config = get_config(request) - store = get_workspaces_store(config) removed = False @@ -161,11 +158,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``.""" - config = get_config(request) - store = get_workspaces_store(config) def apply(current: PersistedWorkspaces) -> PersistedWorkspaces: # Dedupe against the existing list AND within the incoming payload — @@ -199,12 +195,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.""" - config = get_config(request) - store = get_workspaces_store(config) 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 305e52ec71f..1ac9bd8dfad 100644 --- a/tests/agent_server/canvas_extensions/test_canvas_extension_backend.py +++ b/tests/agent_server/canvas_extensions/test_canvas_extension_backend.py @@ -950,7 +950,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) @@ -1143,7 +1143,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() add_domain_error_handler(app) 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 bd95dc8d6d3..d6f389d1f82 100644 --- a/tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py +++ b/tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py @@ -36,6 +36,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" @@ -206,11 +207,14 @@ async def protocol_endpoint(websocket: WebSocket) -> None: public_url = f"https://127.0.0.1:{bridge_port}" app = FastAPI() add_domain_error_handler(app) - 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 @@ -584,10 +588,13 @@ async def echo() -> JSONResponse: public_url = f"{public_scheme}://127.0.0.1:{bridge_port}" app = FastAPI() add_domain_error_handler(app) - 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/canvas_extensions/test_canvas_extension_bridge_routes.py b/tests/agent_server/canvas_extensions/test_canvas_extension_bridge_routes.py index d45048916b9..f8909a6021a 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 @@ -9,6 +9,8 @@ from openhands.agent_server import canvas_extensions_bridge_router as bridge_router from openhands.agent_server.api import add_domain_error_handler +from openhands.agent_server.config import Config +from tests.agent_server.conftest import install_test_context APP_NAME = "demo-app" @@ -18,12 +20,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) @@ -32,6 +36,7 @@ async def fake_websocket( monkeypatch.setattr(bridge_router, "proxy_app_backend_websocket", fake_websocket) app = FastAPI() add_domain_error_handler(app) + install_test_context(app) app.include_router(bridge_router.app_backend_bridge_router) return TestClient(app), paths 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..6b2899dea64 100644 --- a/tests/agent_server/docker_runtime/test_hybrid.py +++ b/tests/agent_server/docker_runtime/test_hybrid.py @@ -1,17 +1,29 @@ import json -from types import SimpleNamespace +from unittest.mock import AsyncMock from uuid import uuid4 import pytest +from fastapi import FastAPI from fastapi.testclient import TestClient from pydantic import SecretStr 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, ) +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 +39,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 +68,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 +77,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 +108,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 +123,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 +146,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 +159,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 +173,10 @@ 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) + app.dependency_overrides[get_conversation_service] = lambda: AsyncMock() response = TestClient(app).post( "/api/conversations", json={"conversation_id": str(docker_id), "conversation_runtime": "local"}, @@ -180,7 +194,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 89e23009755..98120ab1e6f 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: @@ -195,12 +196,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 f0892ad8cd9..12df5121b98 100644 --- a/tests/agent_server/docker_runtime/test_registry.py +++ b/tests/agent_server/docker_runtime/test_registry.py @@ -20,7 +20,7 @@ DockerConversationRegistry, ) from openhands.agent_server.models import StartConversationRequest -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import PersistedSettings, 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 @@ -33,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) ) @@ -658,7 +659,7 @@ def turn_on(settings: PersistedSettings) -> PersistedSettings: settings.experimental_features = {"conversation_reactions": True} return settings - get_settings_store(runtime.config).update(turn_on) + runtime.stores.settings.update(turn_on) _, command, env = _build_container(runtime, monkeypatch, uuid4()) diff --git a/tests/agent_server/docker_runtime/test_retention.py b/tests/agent_server/docker_runtime/test_retention.py index 718b9302c0a..075a4b828ad 100644 --- a/tests/agent_server/docker_runtime/test_retention.py +++ b/tests/agent_server/docker_runtime/test_retention.py @@ -15,6 +15,7 @@ ) from openhands.agent_server.errors import status_for from openhands.agent_server.models import ConversationRuntimeStatus +from openhands.agent_server.persistence import ServerStores from openhands.sdk.errors import DomainError @@ -25,14 +26,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 59ccb3c2253..628904cf6b0 100644 --- a/tests/agent_server/docker_runtime/test_routes.py +++ b/tests/agent_server/docker_runtime/test_routes.py @@ -28,11 +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 PersistedSettings, get_settings_store +from openhands.agent_server.persistence import PersistedSettings, 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): @@ -115,8 +126,13 @@ def test_root_conversation_proxy_preserves_canonical_path(tmp_path, monkeypatch) ) app = FastAPI() add_domain_error_handler(app) - 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 = {} @@ -159,9 +175,11 @@ def test_worktree_start_forwards_only_mounted_host_paths( ) app = FastAPI() add_domain_error_handler(app) - 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, @@ -216,10 +234,12 @@ async def stop(conversation_id): app = FastAPI() add_domain_error_handler(app) - 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( @@ -233,9 +253,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( @@ -246,7 +264,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) @@ -269,8 +287,10 @@ def test_runtime_release_prunes_cache_but_keeps_conversation_state( app = FastAPI() add_domain_error_handler(app) - 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") @@ -291,7 +311,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) @@ -305,7 +325,7 @@ def test_runtime_info_marks_legacy_local_conversation_non_resumable( app = FastAPI() add_domain_error_handler(app) - 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") @@ -333,7 +353,7 @@ 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( @@ -341,10 +361,10 @@ def test_docker_event_history_reads_persistence_without_starting_a_container( ) app = FastAPI() add_domain_error_handler(app) - 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") @@ -355,10 +375,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() @@ -369,7 +387,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) @@ -380,17 +398,17 @@ def test_delete_stops_runtime_before_removing_outer_owned_state(tmp_path, monkey app = FastAPI() add_domain_error_handler(app) - 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() @@ -415,7 +433,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}) @@ -451,8 +469,10 @@ def test_delete_unregisters_host_worktree_before_removing_runtime( app = FastAPI() add_domain_error_handler(app) - 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}") @@ -488,7 +508,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}) @@ -534,7 +554,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) @@ -550,8 +570,10 @@ def fail_conversation_delete(path): monkeypatch.setattr(routers, "safe_rmtree", fail_conversation_delete) app = FastAPI() add_domain_error_handler(app) - 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: @@ -569,7 +591,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) @@ -582,23 +604,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": SimpleNamespace( - state=SimpleNamespace( - conversation_registry=registry, - conversation_service=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) @@ -617,7 +630,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={ @@ -670,6 +683,7 @@ async def receive(): "more_body": False, } + service = AsyncMock() request = Request( { "type": "http", @@ -677,24 +691,23 @@ 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, ) - 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"} 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( @@ -706,11 +719,13 @@ 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() add_domain_error_handler(app) - 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 @@ -786,9 +801,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): @@ -876,11 +891,15 @@ def apply(settings: PersistedSettings) -> PersistedSettings: settings.experimental_features = {"conversation_reactions": enabled} return settings - get_settings_store(config).update(apply) + stores = ServerStores.from_config(config) + stores.settings.update(apply) app = FastAPI() add_domain_error_handler(app) - app.state.conversation_registry = DockerConversationRegistry(config) - app.state.conversation_service = AsyncMock() + install_test_context( + app, + conversation_registry=DockerConversationRegistry(config, stores), + conversation_service=AsyncMock(), + ) app.include_router(docker_conversation_router, prefix="/api") conversation_id = uuid4() forwarded = [] 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 a409ce9acde..6c209d10c75 100644 --- a/tests/agent_server/stress/conftest.py +++ b/tests/agent_server/stress/conftest.py @@ -32,6 +32,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 @@ -81,14 +82,13 @@ 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() add_domain_error_handler(fastapi_app) - 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 dc74cc999a5..6caf5ed0767 100644 --- a/tests/agent_server/stress/test_slow_webhook.py +++ b/tests/agent_server/stress/test_slow_webhook.py @@ -44,6 +44,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 ( @@ -128,7 +129,7 @@ def app( ) -> FastAPI: fastapi_app = FastAPI() add_domain_error_handler(fastapi_app) - 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") @@ -199,7 +200,7 @@ async def test_slow_webhook_does_not_unbound_growth( # client for it. baseline_app = FastAPI() add_domain_error_handler(baseline_app) - 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] = lambda: ( 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 50dcd312a17..8e6b6b0d770 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, @@ -54,13 +57,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() @@ -85,14 +81,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. @@ -107,10 +103,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) @@ -129,11 +125,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 9589cf00d65..8d16a79037a 100644 --- a/tests/agent_server/test_acp_router.py +++ b/tests/agent_server/test_acp_router.py @@ -17,13 +17,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" @@ -43,12 +48,17 @@ def config(tmp_path: Path) -> Config: def client(config: Config) -> Iterator[TestClient]: app = FastAPI() add_domain_error_handler(app) - 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 @@ -91,10 +101,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}) @@ -154,9 +164,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( @@ -187,7 +197,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 c119abd01e6..2ccad3e8ecc 100644 --- a/tests/agent_server/test_agent_profile_conv_start.py +++ b/tests/agent_server/test_agent_profile_conv_start.py @@ -31,7 +31,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 @@ -56,6 +56,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 @@ -70,8 +71,8 @@ def client(): app = FastAPI() add_domain_error_handler(app) 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) @@ -98,10 +99,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( @@ -140,10 +147,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( @@ -153,7 +161,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"] @@ -428,15 +435,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 bbeacc230e6..8daecb98e2e 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.errors import LockTimeout from openhands.sdk.llm import LLM @@ -31,34 +28,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 @@ -68,9 +58,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") @@ -1161,43 +1151,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 95c048a790a..ed89855e412 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,12 @@ create_app, ) 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, +) from openhands.tools.terminal.terminal.tmux_pane_pool import TmuxPanePool @@ -566,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.""" @@ -603,17 +612,18 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=replace( + build_server_context(Config()), + vscode_service=mock_vscode_service, + ) + ) async with api_lifespan(mock_app): pass @@ -647,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, ), ): @@ -659,7 +665,10 @@ async def slow_stop(): mock_app = AsyncMock() mock_backend_manager = AsyncMock() mock_app.state = SimpleNamespace( - config=Config(), + server_context=replace( + build_server_context(Config()), + vscode_service=mock_vscode_service, + ), canvas_extension_backend_manager=mock_backend_manager, ) @@ -682,21 +691,20 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) # 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 = [] @@ -713,15 +721,13 @@ 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( - config=Config(), conversation_registry=registry + server_context=replace( + _context_without_side_services(), + conversation_registry=cast(ConversationRegistry, registry), + ) ) async with api_lifespan(mock_app): @@ -745,13 +751,11 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(expected_tmux_tmpdir) assert expected_tmux_tmpdir.is_dir() @@ -773,13 +777,11 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(user_tmux_tmpdir) @@ -809,13 +811,11 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) async with api_lifespan(mock_app): assert os.environ[overridden] == "C.UTF-8" @@ -842,13 +842,11 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(substitute) @@ -881,13 +879,11 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) with caplog.at_level("WARNING"): async with api_lifespan(mock_app): pass @@ -912,13 +908,11 @@ 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(config=Config()) + mock_app.state = SimpleNamespace( + server_context=_context_without_side_services() + ) async with api_lifespan(mock_app): assert os.environ["TMUX_TMPDIR"] == str(expected_tmux_tmpdir) @@ -1168,8 +1162,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=[], @@ -1179,6 +1171,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 5395c9dbe12..05635ea30c9 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.errors import status_for from openhands.agent_server.openai.router import _parse_observability_overrides from openhands.sdk.errors import DomainError @@ -124,15 +130,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 @@ -294,6 +292,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 @@ -341,6 +343,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_bash_router.py b/tests/agent_server/test_bash_router.py index eb24386418c..6458d294d46 100644 --- a/tests/agent_server/test_bash_router.py +++ b/tests/agent_server/test_bash_router.py @@ -8,6 +8,7 @@ from openhands.agent_server.api import add_domain_error_handler 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 @@ -15,7 +16,9 @@ async def bash_client(tmp_path): app = FastAPI() add_domain_error_handler(app) 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 ef24d726c69..00e8f95dcbb 100644 --- a/tests/agent_server/test_bash_service.py +++ b/tests/agent_server/test_bash_service.py @@ -26,6 +26,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 @@ -39,8 +40,7 @@ async def bash_service(tmp_path: Path) -> AsyncIterator[BashEventService]: async def client(bash_service: BashEventService) -> AsyncIterator[httpx.AsyncClient]: app = FastAPI() add_domain_error_handler(app) - 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..b1d1063a611 100644 --- a/tests/agent_server/test_blocking_export_extended.py +++ b/tests/agent_server/test_blocking_export_extended.py @@ -35,10 +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: None) - monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda: None - ) monkeypatch.setattr( "openhands.agent_server.api.unseal_tool_catalog", lambda: cleanup.append(True) ) @@ -47,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_conversation_router.py b/tests/agent_server/test_conversation_router.py index 2c772b418b6..3933b067a89 100644 --- a/tests/agent_server/test_conversation_router.py +++ b/tests/agent_server/test_conversation_router.py @@ -42,6 +42,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 @@ -50,10 +51,10 @@ def client(): app = FastAPI() add_domain_error_handler(app) 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) @@ -172,8 +173,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 ) @@ -182,7 +186,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"] == { @@ -2630,10 +2633,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() @@ -2676,10 +2682,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() @@ -2777,54 +2786,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( @@ -2832,66 +2827,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( @@ -2931,46 +2917,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 0ad326bd9bb..66736b9f359 100644 --- a/tests/agent_server/test_conversation_router_skill_trim.py +++ b/tests/agent_server/test_conversation_router_skill_trim.py @@ -39,6 +39,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: @@ -158,8 +159,9 @@ def client(self, heavy_conversation): app = FastAPI() add_domain_error_handler(app) 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 @@ -243,8 +245,9 @@ def test_batch_get_handles_null_items(self): app = FastAPI() add_domain_error_handler(app) 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 fb5f099b518..42ffe96b9e2 100644 --- a/tests/agent_server/test_conversation_tags.py +++ b/tests/agent_server/test_conversation_tags.py @@ -24,12 +24,14 @@ 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() add_domain_error_handler(app) + 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 1287f8953ba..f3b0e4618ec 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 @@ -31,6 +32,7 @@ CustomSecret, FileSecretsStore, Secrets, + ServerStores, ) from openhands.sdk import LLM, Agent, AgentContext from openhands.sdk.agent import ACPAgent @@ -43,6 +45,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 @@ -78,6 +81,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) @@ -201,7 +209,7 @@ def test_activation_route_installs_http_binding( service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() add_domain_error_handler(app) - 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 @@ -232,7 +240,7 @@ def test_activation_route_rejects_unavailable_binding( service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() add_domain_error_handler(app) - 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 @@ -261,7 +269,7 @@ def test_activation_route_retries_transient_binding_failure( service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() add_domain_error_handler(app) - 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 @@ -308,7 +316,7 @@ def test_activation_route_rejects_terminal_binding_failure( service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() add_domain_error_handler(app) - 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 @@ -342,7 +350,7 @@ def test_activation_route_rejects_invalid_binding_url( service = ConversationService(conversations_dir=tmp_path / "conversations") app = FastAPI() add_domain_error_handler(app) - app.state.conversation_service = service + install_test_context(app, conversation_service=service) app.include_router(router, prefix="/api") conversation_id = uuid4() @@ -363,11 +371,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") @@ -387,7 +396,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"), @@ -397,7 +407,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) @@ -592,7 +602,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"], @@ -615,7 +626,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) @@ -938,7 +949,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"), @@ -948,7 +960,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 @@ -1076,11 +1088,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": {}}) @@ -1093,7 +1106,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", @@ -1113,7 +1127,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 @@ -1160,7 +1174,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"), @@ -1168,7 +1183,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 9b6b37e936c..b2161e3faab 100644 --- a/tests/agent_server/test_dependencies.py +++ b/tests/agent_server/test_dependencies.py @@ -9,12 +9,13 @@ from openhands.agent_server.api import add_domain_error_handler 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() add_domain_error_handler(app) - 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(): @@ -54,7 +55,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) @@ -62,7 +63,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 463347ef123..aaef3a539fc 100644 --- a/tests/agent_server/test_event_router.py +++ b/tests/agent_server/test_event_router.py @@ -26,6 +26,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(): @@ -72,6 +73,7 @@ def client(): """Create a test client for the FastAPI app without authentication.""" app = FastAPI() add_domain_error_handler(app) + install_test_context(app) app.include_router(event_router, prefix="/api") return TestClient(app) @@ -99,8 +101,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 c25bd44ad31..388f7c6c506 100644 --- a/tests/agent_server/test_event_router_websocket.py +++ b/tests/agent_server/test_event_router_websocket.py @@ -185,20 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_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 = 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() @@ -212,20 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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) @@ -248,20 +246,18 @@ def side_effect(): mock_websocket.receive_json.side_effect = side_effect - with ( - patch( - "openhands.agent_server.sockets.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) - + 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") @@ -288,20 +284,19 @@ def side_effect(): mock_websocket.receive_json.side_effect = side_effect - with ( - patch( - "openhands.agent_server.sockets.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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 @@ -316,22 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) - + 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() @@ -345,23 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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() @@ -387,23 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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 @@ -431,24 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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 @@ -462,16 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) - + 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( @@ -480,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 @@ -508,24 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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() @@ -558,16 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) - + 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( @@ -575,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 @@ -594,23 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_config") as mock_config, - ): - mock_config.return_value.session_api_keys = None - mock_conv_service.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() @@ -622,16 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) - + 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 @@ -656,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 f6138500c4f..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 @@ -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): @@ -80,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( @@ -96,14 +99,6 @@ async def svc(tmp_path, monkeypatch): ) await service.start() - async def get_event_service(_cid): - return service - - monkeypatch.setattr( - sockets, - "conversation_service", - SimpleNamespace(get_event_service=get_event_service), - ) yield service if service.is_open(): await service.close() @@ -131,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_experimental_features_router.py b/tests/agent_server/test_experimental_features_router.py index bd926249354..258af5be205 100644 --- a/tests/agent_server/test_experimental_features_router.py +++ b/tests/agent_server/test_experimental_features_router.py @@ -9,8 +9,8 @@ from openhands.agent_server import experimental_features from openhands.agent_server.api import add_domain_error_handler -from openhands.agent_server.config import Config from openhands.agent_server.conversation_service import ConversationService +from openhands.agent_server.dependencies import get_settings_store from openhands.agent_server.experimental_features import ( RUNTIME_EXPERIMENTAL_FEATURES_ENV, ExperimentalFeatureDefinition, @@ -21,7 +21,7 @@ experimental_features_router, ) from openhands.agent_server.models import StartConversationRequest -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.sdk import LLM, Agent from openhands.sdk.agent import ACPAgent from openhands.sdk.launch.finalize import LaunchRuntime, finalize @@ -40,24 +40,20 @@ def qa_feature(monkeypatch) -> ExperimentalFeatureDefinition: @pytest.fixture -async def client() -> AsyncIterator[httpx.AsyncClient]: +async def client(server_stores: ServerStores) -> AsyncIterator[httpx.AsyncClient]: app = FastAPI() add_domain_error_handler(app) - app.state.config = Config( - static_files_path=None, session_api_keys=[], secret_key=None - ) + app.dependency_overrides[get_settings_store] = lambda: server_stores.settings app.include_router(experimental_features_router, prefix="/api") transport = httpx.ASGITransport(app=app) async with httpx.AsyncClient(transport=transport, base_url="http://test") as c: yield c -def _settings_path(): - return get_settings_store()._path - - @pytest.mark.asyncio -async def test_listing_needs_no_settings_file_and_creates_none(client, qa_feature): +async def test_listing_needs_no_settings_file_and_creates_none( + client, qa_feature, server_stores +): response = await client.get("/api/settings/experimental") assert response.status_code == 200 @@ -69,11 +65,11 @@ async def test_listing_needs_no_settings_file_and_creates_none(client, qa_featur "enabled": False, } ] - assert not _settings_path().exists() + assert not server_stores.settings._path.exists() @pytest.mark.asyncio -async def test_turning_a_feature_on_and_off_is_saved(client, qa_feature): +async def test_turning_a_feature_on_and_off_is_saved(client, qa_feature, server_stores): on = await client.patch( "/api/settings/experimental/qa_feature", json={"enabled": True} ) @@ -86,30 +82,32 @@ async def test_turning_a_feature_on_and_off_is_saved(client, qa_feature): assert on.json()["enabled"] is True assert listed_on.json()[0]["enabled"] is True assert off.json()["enabled"] is False - stored = get_settings_store().load() + stored = server_stores.settings.load() assert stored is not None assert stored.experimental_features == {"qa_feature": False} @pytest.mark.asyncio -async def test_unknown_features_are_refused(client, qa_feature): +async def test_unknown_features_are_refused(client, qa_feature, server_stores): response = await client.patch( "/api/settings/experimental/time_travel", json={"enabled": True} ) assert response.status_code == 404 - assert not _settings_path().exists() + assert not server_stores.settings._path.exists() @pytest.mark.asyncio -async def test_switching_a_feature_keeps_the_other_settings(client, qa_feature): - get_settings_store().save( +async def test_switching_a_feature_keeps_the_other_settings( + client, qa_feature, server_stores +): + server_stores.settings.save( PersistedSettings(misc_settings={"kept": True}, active_profile="mine") ) await client.patch("/api/settings/experimental/qa_feature", json={"enabled": True}) - stored = get_settings_store().load() + stored = server_stores.settings.load() assert stored is not None assert stored.misc_settings == {"kept": True} assert stored.active_profile == "mine" @@ -117,9 +115,11 @@ async def test_switching_a_feature_keeps_the_other_settings(client, qa_feature): @pytest.mark.asyncio -async def test_a_stored_flag_nobody_knows_is_not_listed_and_is_kept(client, qa_feature): +async def test_a_stored_flag_nobody_knows_is_not_listed_and_is_kept( + client, qa_feature, server_stores +): """A flag saved by another version of the server does no harm.""" - get_settings_store().save( + server_stores.settings.save( PersistedSettings(experimental_features={"from_the_future": True}) ) @@ -127,7 +127,7 @@ async def test_a_stored_flag_nobody_knows_is_not_listed_and_is_kept(client, qa_f await client.patch("/api/settings/experimental/qa_feature", json={"enabled": True}) assert [f["key"] for f in listed.json()] == ["qa_feature"] - stored = get_settings_store().load() + stored = server_stores.settings.load() assert stored is not None assert stored.experimental_features == { "from_the_future": True, @@ -136,8 +136,10 @@ async def test_a_stored_flag_nobody_knows_is_not_listed_and_is_kept(client, qa_f @pytest.mark.asyncio -async def test_a_corrupted_settings_file_is_not_overwritten(client, qa_feature): - path = _settings_path() +async def test_a_corrupted_settings_file_is_not_overwritten( + client, qa_feature, server_stores +): + path = server_stores.settings._path path.parent.mkdir(parents=True, exist_ok=True) path.write_text("{not json") @@ -198,7 +200,9 @@ def test_finalize_adds_feature_tools_to_openhands_agents_only(): @pytest.mark.asyncio -async def test_new_conversations_get_reaction_tools_only_while_on(tmp_path): +async def test_new_conversations_get_reaction_tools_only_while_on( + tmp_path, server_stores +): workspace = tmp_path / "workspace" workspace.mkdir() @@ -209,7 +213,7 @@ def request() -> StartConversationRequest: ) async with ConversationService( - conversations_dir=tmp_path / "conversations" + conversations_dir=tmp_path / "conversations", stores=server_stores ) as service: off, _ = await service.start_conversation(request()) @@ -217,7 +221,7 @@ def turn_on(settings: PersistedSettings) -> PersistedSettings: settings.experimental_features = {"conversation_reactions": True} return settings - get_settings_store().update(turn_on) + server_stores.settings.update(turn_on) on, _ = await service.start_conversation(request()) tool_names = {} diff --git a/tests/agent_server/test_file_router.py b/tests/agent_server/test_file_router.py index c0b2308d5ed..d14af1e7789 100644 --- a/tests/agent_server/test_file_router.py +++ b/tests/agent_server/test_file_router.py @@ -17,20 +17,18 @@ from uuid import UUID, uuid4 import pytest -from fastapi import Request, UploadFile +from fastapi import UploadFile from fastapi.testclient import TestClient from starlette.responses import FileResponse from starlette.types import Message -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 client_context, update_test_context @pytest.fixture @@ -383,12 +381,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), ) @@ -751,10 +750,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() @@ -763,10 +762,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}") @@ -780,12 +775,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( @@ -796,80 +789,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() + }, + ) + 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, - ) + 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 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" -def _app_request(client) -> Request: - """A bare request whose ``app`` carries the client's live config.""" - return Request({"type": "http", "app": client.app}) + # Repeated downloads leave no zip residue behind. + assert list(user_conversations.glob("*.zip")) == [] async def _stream_trajectory( @@ -906,10 +893,10 @@ async def test_download_trajectory_repeat_survives_previous_cleanup( conversations_path = conversation_dir.parent first = await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) second = await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) await _stream_trajectory(first) @@ -929,7 +916,7 @@ async def test_download_trajectory_concurrent_downloads_use_own_files( responses = await asyncio.gather( *( file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) for _ in range(6) ) @@ -961,7 +948,7 @@ def fail(source_dir, output_path): with pytest.raises(OSError, match="disk full"): await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) assert list(conversation_dir.parent.glob("*.zip")) == [] @@ -986,7 +973,9 @@ def blocked_zip(source, destination): monkeypatch.setattr(file_router_module, "_create_zip_from_directory", blocked_zip) task = asyncio.create_task( - file_router_module.download_trajectory(conversation_id, _app_request(client)) + file_router_module.download_trajectory( + conversation_id, client_context(client).config + ) ) try: await asyncio.wait_for(started.wait(), timeout=5) @@ -1010,7 +999,7 @@ async def test_download_trajectory_cleans_up_after_stream_failure( ): conversation_id, conversation_dir = _trajectory_dir(client, tmp_path) response = await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) with pytest.raises(type(error)): await _stream_trajectory(response, error) @@ -1041,7 +1030,7 @@ def tracked_zip(source, destination): responses = await asyncio.gather( *( file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) for _ in range(6) ) @@ -1059,10 +1048,10 @@ async def test_download_trajectory_removes_only_stale_orphaned_temp_zips( two_hours_ago = time.time() - 2 * 60 * 60 # An unstreamed response leaves its temp zip, as a killed server would. stale = await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) recent = await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) os.utime(stale.path, (two_hours_ago, two_hours_ago)) unrelated = conversation_dir.parent / ".backup.zip" @@ -1071,7 +1060,7 @@ async def test_download_trajectory_removes_only_stale_orphaned_temp_zips( await _stream_trajectory( await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) ) @@ -1098,7 +1087,7 @@ def unlink(self, missing_ok=False): monkeypatch.setattr(Path, "unlink", unlink) response = await file_router_module.download_trajectory( - conversation_id, _app_request(client) + conversation_id, client_context(client).config ) with zipfile.ZipFile(response.path) as archive: diff --git a/tests/agent_server/test_git_provider_service.py b/tests/agent_server/test_git_provider_service.py index 3c7f38c7ebf..91ed6f292f2 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,7 +320,7 @@ 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 @@ -369,7 +375,7 @@ def _patch_pr_client( calls: list[tuple[str, Any]] = [] monkeypatch.setattr( "openhands.agent_server.git_provider_service._resolve_provider_token", - lambda config, provider: token, + lambda secrets_store, provider: token, ) monkeypatch.setattr( "openhands.agent_server.git_provider_service.httpx.AsyncClient", @@ -402,7 +408,9 @@ def _pr_routes(pr: dict[str, object], runs: list, statuses: list) -> dict[str, A async def _get_pr(): from openhands.agent_server.git_provider_service import get_provider_pull_request - return await get_provider_pull_request(Config(), GitProvider.GITHUB, "o", "r", 7) + return await get_provider_pull_request( + _secrets_store(), GitProvider.GITHUB, "o", "r", 7 + ) @pytest.mark.asyncio @@ -580,7 +588,7 @@ async def get(self, path, params=None): monkeypatch.setattr( "openhands.agent_server.git_provider_service._resolve_provider_token", - lambda config, provider: "tok", + lambda secrets_store, provider: "tok", ) monkeypatch.setattr( "openhands.agent_server.git_provider_service.httpx.AsyncClient", @@ -588,7 +596,9 @@ async def get(self, path, params=None): ) with pytest.raises(GitProviderAPIError) as excinfo: - await get_provider_pull_request(Config(), GitProvider.GITHUB, "o", "r", 7) + await get_provider_pull_request( + _secrets_store(), GitProvider.GITHUB, "o", "r", 7 + ) await asyncio.sleep(0.2) assert excinfo.value.status_code == 404 diff --git a/tests/agent_server/test_init_router.py b/tests/agent_server/test_init_router.py index 2afeb2ce459..3933a5378bb 100644 --- a/tests/agent_server/test_init_router.py +++ b/tests/agent_server/test_init_router.py @@ -9,14 +9,14 @@ 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 -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 @@ -31,6 +31,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 from openhands.sdk.errors import DomainError @@ -51,60 +56,40 @@ 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. - - The service takes its token from the default config, which is loaded at - import time, before ``_clean_env`` runs, so reset that too. - """ - 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, vscode_service: VSCodeService | None = None +) -> FastAPI: + """A bare app holding the dormant server context, as ``create_app`` leaves it.""" + app = FastAPI() + context = replace(build_server_context(config), vscode_service=vscode_service) + install_server_context(app, context) + 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: @@ -198,8 +183,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( @@ -207,8 +190,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( @@ -221,36 +204,31 @@ 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() - 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( @@ -265,11 +243,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 @@ -287,8 +263,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,15 +278,13 @@ def capture_observability_env(): assert observed_env == "hello" finally: await svc.teardown() - monkeypatch.delenv("DEFERRED_INIT_TEST_VAR", raising=False) - _reset_conversation_singleton() + # 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): - """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( @@ -318,8 +292,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( @@ -329,27 +303,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( @@ -357,11 +327,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( @@ -369,18 +338,14 @@ 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 - ) + vscode = MagicMock(spec=VSCodeService) 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, vscode_service=vscode) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -393,25 +358,20 @@ 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 - ) + vscode = MagicMock(spec=VSCodeService) 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, vscode_service=vscode) + svc = InitService(app, base_config=base) await svc.initialize( InitRequest( @@ -423,14 +383,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", @@ -438,22 +396,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") @@ -462,40 +418,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"), @@ -513,7 +462,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"], @@ -521,10 +469,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( @@ -534,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) @@ -543,7 +488,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", @@ -551,13 +495,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( ( @@ -622,15 +563,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, @@ -648,35 +580,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", @@ -684,43 +612,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", @@ -728,64 +652,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 - assert client.get("/v1/models").status_code == 200 - 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 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", @@ -793,31 +701,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"), @@ -826,41 +730,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 - # GET /api/init does NOT require the key (status polling). - resp = client.get("/api/init") - assert resp.status_code == 200 - finally: - _reset_conversation_singleton() + # 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 + + # 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", @@ -868,45 +768,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() @@ -916,51 +814,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 @@ -995,7 +890,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", @@ -1003,15 +897,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 @@ -1020,17 +911,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( @@ -1039,13 +928,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: @@ -1056,7 +946,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"], @@ -1064,36 +953,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( @@ -1105,44 +989,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 e4052388034..be57959b09b 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.errors import LockTimeout from openhands.sdk.llm.meta_profile_store import MetaProfile, MetaProfileStore from openhands.sdk.settings.model import OpenHandsAgentSettings @@ -35,14 +37,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 @@ -247,7 +247,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) @@ -280,7 +280,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 @@ -326,7 +326,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 @@ -366,7 +366,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") @@ -398,7 +400,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 0d54469f015..ed92f8863ea 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 @@ -37,7 +38,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(), @@ -185,7 +186,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 = [] @@ -444,7 +445,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 80456be1dde..cf72461054b 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 @@ -24,20 +22,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: @@ -46,14 +45,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 @@ -84,10 +87,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.") @@ -102,9 +105,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 817813d6715..168acdceaea 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.errors import Invalid, LockTimeout from openhands.sdk.llm import LLM, Message from openhands.sdk.llm.auth.credentials import OAuthCredentials @@ -28,90 +25,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 @@ -1139,38 +1087,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_reactions_router.py b/tests/agent_server/test_reactions_router.py index a4f16c3e4db..17249d93176 100644 --- a/tests/agent_server/test_reactions_router.py +++ b/tests/agent_server/test_reactions_router.py @@ -8,18 +8,21 @@ from fastapi import FastAPI from openhands.agent_server.api import add_domain_error_handler -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_conversation_service, + get_settings_store, +) from openhands.agent_server.event_router import event_router from openhands.agent_server.experimental_features import ( RUNTIME_EXPERIMENTAL_FEATURES_ENV, ) from openhands.agent_server.models import StartConversationRequest -from openhands.agent_server.persistence import PersistedSettings, get_settings_store +from openhands.agent_server.persistence import PersistedSettings, ServerStores from openhands.sdk import LLM, Agent, Message, TextContent from openhands.sdk.event import MessageEvent, ReactionEvent from openhands.sdk.workspace import LocalWorkspace +from tests.agent_server.conftest import install_test_context @pytest.fixture @@ -30,26 +33,27 @@ async def service(tmp_path) -> AsyncIterator[ConversationService]: yield service -def _set_reactions(enabled: bool) -> None: +def _set_reactions(stores: ServerStores, enabled: bool) -> None: def apply(settings: PersistedSettings) -> PersistedSettings: settings.experimental_features = {"conversation_reactions": enabled} return settings - get_settings_store().update(apply) + stores.settings.update(apply) @pytest.fixture(autouse=True) -def reactions_on(): - _set_reactions(True) +def reactions_on(server_stores: ServerStores): + _set_reactions(server_stores, True) @pytest.fixture -async def client(service) -> AsyncIterator[httpx.AsyncClient]: +async def client( + service, server_stores: ServerStores +) -> AsyncIterator[httpx.AsyncClient]: app = FastAPI() add_domain_error_handler(app) - app.state.config = Config( - static_files_path=None, session_api_keys=[], secret_key=None - ) + install_test_context(app) + app.dependency_overrides[get_settings_store] = lambda: server_stores.settings app.include_router(event_router, prefix="/api") app.dependency_overrides[get_conversation_service] = lambda: service transport = httpx.ASGITransport(app=app) @@ -130,10 +134,10 @@ async def test_bad_reactions_are_refused(client, service, tmp_path): @pytest.mark.asyncio async def test_reactions_are_refused_while_the_feature_is_off( - client, service, tmp_path + client, service, tmp_path, server_stores ): cid, _ = await _conversation_with_answer(service, tmp_path) - _set_reactions(False) + _set_reactions(server_stores, False) response = await client.post( f"/api/conversations/{cid}/reactions", json={"emoji": "+1"} @@ -145,13 +149,13 @@ async def test_reactions_are_refused_while_the_feature_is_off( @pytest.mark.asyncio async def test_a_docker_runtime_leaves_the_switch_to_its_host( - client, service, tmp_path, monkeypatch + client, service, tmp_path, monkeypatch, server_stores ): # In a runtime container the host checks the switch before forwarding; # the container's own settings are empty. monkeypatch.setenv(RUNTIME_EXPERIMENTAL_FEATURES_ENV, "{}") cid, _ = await _conversation_with_answer(service, tmp_path) - _set_reactions(False) + _set_reactions(server_stores, False) response = await client.post( f"/api/conversations/{cid}/reactions", json={"emoji": "+1"} diff --git a/tests/agent_server/test_runtime_router.py b/tests/agent_server/test_runtime_router.py index 01a43a5c5f4..820179cd6e2 100644 --- a/tests/agent_server/test_runtime_router.py +++ b/tests/agent_server/test_runtime_router.py @@ -4,15 +4,24 @@ import httpx import pytest +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 +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) @@ -159,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 @@ -203,7 +225,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..74e1f673dcb --- /dev/null +++ b/tests/agent_server/test_server_context.py @@ -0,0 +1,486 @@ +"""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 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 +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 +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, + get_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 + + +BOOT_KEY = "boot-secret-key" +USER_KEY = "user-secret-key" +SESSION_KEY = "user-session-key" + + +@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 + + +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) + return app + + +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", + 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")}, + **more, + }, + ) + + +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) + 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 + + def recording_get_instance(config, stores): + service = real_get_instance(config, stores) + started.append(service) + return service + + monkeypatch.setattr( + init_router_module.ConversationService, "get_instance", recording_get_instance + ) + + 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" + # 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 + # ... 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 + + vscode.refuses = False + 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_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: + """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_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()) + + +# ── 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", + "vscode_service", + } +) +_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 + + +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): + 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) + 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)", + "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 ...", + ], +) +def test_the_guard_recognises_a_way_around_the_context(source: str) -> None: + assert context_violations(source, "example.py") + + +@pytest.mark.parametrize( + "source", + [ + "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", + "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. 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")) + 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")] + 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) + + +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 = [ + 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) + + +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_session_socket.py b/tests/agent_server/test_session_socket.py index 9be7f6bb56d..dc85d611295 100644 --- a/tests/agent_server/test_session_socket.py +++ b/tests/agent_server/test_session_socket.py @@ -3,12 +3,13 @@ import asyncio import contextlib import json -from unittest.mock import AsyncMock +from unittest.mock import AsyncMock, MagicMock from uuid import uuid4 import pytest from fastapi import WebSocketDisconnect +from openhands.agent_server.config import Config from openhands.agent_server.session_protocol import MAX_FRAME_BYTES from openhands.agent_server.session_socket import ( _ConnectionWriter, @@ -623,19 +624,19 @@ async def test_a_message_sent_while_a_step_runs_is_saved_after_the_client_leaves service = _LockedEventService() ws = _ClientSocket([_user_frame("wait, actually")]) - async def accept(websocket, key): + async def accept(websocket, key, config): return True monkeypatch.setattr(module, "_accept_authenticated_websocket", accept) - monkeypatch.setattr( - module, - "_get_conversation_service", - lambda websocket: type( - "S", (), {"get_event_service": AsyncMock(return_value=service)} - )(), - ) - task = asyncio.ensure_future(module.session_socket(uuid4(), ws)) # type: ignore[arg-type] + task = asyncio.ensure_future( + module.session_socket( + uuid4(), + ws, # type: ignore[arg-type] + config=Config(), + conv_service=MagicMock(get_event_service=AsyncMock(return_value=service)), + ) + ) # The frame is taken while the step still runs; then the client leaves. await asyncio.wait_for(service.sending.wait(), timeout=5) task.cancel() diff --git a/tests/agent_server/test_settings_router.py b/tests/agent_server/test_settings_router.py index a3aa6aab937..1f3d40d6dee 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.errors import LockTimeout @@ -33,6 +31,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 @@ -40,13 +39,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: @@ -644,7 +641,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", @@ -683,9 +682,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( @@ -714,7 +715,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", @@ -736,7 +739,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, @@ -763,7 +766,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, @@ -788,7 +791,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"}} ) @@ -809,8 +814,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( @@ -840,7 +849,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", @@ -2066,7 +2077,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 LockTimeout( diff --git a/tests/agent_server/test_sockets_service_getters.py b/tests/agent_server/test_sockets_service_getters.py index 9a0e255b8c6..54c362bd827 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,48 @@ from uuid import uuid4 import pytest -from fastapi import WebSocketDisconnect +from fastapi import 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.dependencies import ( + get_config, + get_conversation_service, + get_server_bash_event_service, +) +from openhands.agent_server.errors import status_for 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.server_context import ( + ServerContext, + build_server_context, + get_server_context, ) +from openhands.sdk.errors import Unavailable + + +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 +66,71 @@ 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): + context = _context(conversation_service=conv_service) + ws = _make_ws(context) + assert get_conversation_service(ws, context) 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.""" + context = _context() + ws = _make_ws(context) + with pytest.raises(WebSocketException) as excinfo: + get_conversation_service(ws, context) + 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(): + context = _context() + request = _make_ws(context, scope_type="http") + with pytest.raises(Unavailable) as excinfo: + get_conversation_service(request, context) + assert status_for(excinfo.value) == 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): + 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_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(): + context = _context() + ws = _make_ws(context) + with pytest.raises(WebSocketException) as excinfo: + get_server_bash_event_service(ws, context) + 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(): + context = _context() + request = _make_ws(context, scope_type="http") + with pytest.raises(Unavailable) as excinfo: + get_server_bash_event_service(request, context) + assert status_for(excinfo.value) == 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_server_context(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,20 +140,22 @@ 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() + 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()) 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) + 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() @@ -177,29 +163,29 @@ 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() + 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()) 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) + 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() @@ -215,12 +201,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( - { - "conversation_service": service, - "config": Config(session_api_keys=[]), - } - ) + context = _context(conversation_service=service) + ws = _make_ws(context) ws.headers = {} ws.accept = AsyncMock() ws.receive_json = AsyncMock( @@ -230,7 +212,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_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 5b944ed9031..66f0881a844 100644 --- a/tests/agent_server/test_tool_router.py +++ b/tests/agent_server/test_tool_router.py @@ -3,10 +3,12 @@ import importlib from openhands.agent_server.api import add_domain_error_handler +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(): @@ -38,7 +40,7 @@ def test_catalog_offers_the_stock_tools_a_profile_may_pick(): app = FastAPI() add_domain_error_handler(app) - app.state.config = Config() + install_test_context(app, Config()) app.include_router(tool_router, prefix="/api") response = TestClient(app).get("/api/tools/catalog") @@ -83,8 +85,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 @@ -95,10 +95,13 @@ def test_docker_runtime_catalog_does_not_report_host_usability(monkeypatch): ) app = FastAPI() add_domain_error_handler(app) - 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") @@ -108,8 +111,6 @@ 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 @@ -117,10 +118,13 @@ def test_docker_catalog_follows_the_image_browser_setting(monkeypatch): app = FastAPI() add_domain_error_handler(app) - 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") @@ -132,8 +136,6 @@ 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 @@ -141,10 +143,13 @@ def test_catalog_reports_a_disabled_browser_as_unusable(): app = FastAPI() add_domain_error_handler(app) - 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..44b96d40064 100644 --- a/tests/agent_server/test_trace_shutdown.py +++ b/tests/agent_server/test_trace_shutdown.py @@ -25,15 +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: None) - monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda: 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: @@ -59,10 +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: None) - monkeypatch.setattr( - "openhands.agent_server.api.get_tool_preload_service", lambda: None - ) flushed = [] monkeypatch.setattr( TracerProvider, "force_flush", lambda self, *a, **k: flushed.append(1) @@ -72,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 578907f5937..d05938f7afe 100644 --- a/tests/agent_server/test_vscode_router.py +++ b/tests/agent_server/test_vscode_router.py @@ -1,44 +1,53 @@ """Tests for VSCode router.""" -from unittest.mock import patch +from pathlib import Path +from unittest.mock import MagicMock import pytest +from fastapi import FastAPI 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.errors import status_for +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 from openhands.sdk.errors import DomainError @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( @@ -58,7 +67,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") @@ -67,10 +76,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(DomainError) as exc_info: - await get_vscode_url() + await get_vscode_url(vscode_service=mock_vscode_service) assert status_for(exc_info.value) == 500 assert "Failed to get VSCode URL" in str(exc_info.value.detail) @@ -81,7 +90,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() @@ -92,7 +101,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} @@ -103,139 +112,123 @@ async def test_get_vscode_status_error(mock_vscode_service): mock_vscode_service.is_running.side_effect = Exception("Service error") with pytest.raises(DomainError) as exc_info: - await get_vscode_status() + await get_vscode_status(vscode_service=mock_vscode_service) assert status_for(exc_info.value) == 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(DomainError) as exc_info: - await get_vscode_url() + with pytest.raises(DomainError) as exc_info: + await get_vscode_url(vscode_service=None) - assert status_for(exc_info.value) == 503 - assert "VSCode is disabled in configuration" in str(exc_info.value.detail) + assert status_for(exc_info.value) == 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 17390282041..f16fb545163 100644 --- a/tests/agent_server/test_vscode_service.py +++ b/tests/agent_server/test_vscode_service.py @@ -9,9 +9,10 @@ import pytest +from openhands.agent_server.config import Config from openhands.agent_server.vscode_service import ( VSCodeService, - get_vscode_service, + create_vscode_service, ) @@ -584,88 +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.""" - 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() - - 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() - - 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() - - assert service1 is service2 - assert isinstance(service1, VSCodeService) - +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"], + ) -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 = create_vscode_service(config) - service = get_vscode_service() + assert isinstance(service, VSCodeService) + assert service.port == 9001 + assert service.server_base_path == "/runtime-123/vscode" + assert service.connection_token == "first-key" - 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.""" - 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() +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=[]) + ) - 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(): diff --git a/tests/agent_server/test_websocket_first_message_auth.py b/tests/agent_server/test_websocket_first_message_auth.py index fb87993461b..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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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_default_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,16 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) + 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() @@ -307,17 +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.conversation_service" - ) as mock_conv_service, - patch("openhands.agent_server.sockets.get_default_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) - - # 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() @@ -337,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_default_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 @@ -388,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.bash_event_service", - mock_bash_service, - ), - patch("openhands.agent_server.sockets.get_default_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] @@ -427,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.bash_event_service", - mock_bash_service, - ), - patch("openhands.agent_server.sockets.get_default_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() @@ -474,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.bash_event_service", - mock_bash_service, - ), - patch("openhands.agent_server.sockets.get_default_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/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 979b21d23d9..196fc9f58e9 100644 --- a/tests/cross/test_remote_conversation_live_server.py +++ b/tests/cross/test_remote_conversation_live_server.py @@ -18,7 +18,6 @@ from datetime import datetime from pathlib import Path from typing import cast -from unittest.mock import patch from uuid import UUID import httpx @@ -31,11 +30,7 @@ from websockets.exceptions import ConnectionClosed, InvalidStatus from websockets.sync.client import connect -from openhands.agent_server import ( - config as config_module, - conversation_service as service_module, -) -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 @@ -156,7 +151,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() @@ -385,8 +380,6 @@ def on_event(event: Event) -> None: def test_trajectory_downloads_are_independent(tmp_path, monkeypatch): - monkeypatch.setattr(config_module, "_default_config", None) - monkeypatch.setattr(service_module, "_conversation_service", None) with live_server_env(tmp_path, monkeypatch) as server_env: agent = Agent( llm=LLM(model="gpt-4o-mini", api_key=SecretStr("test")), @@ -1155,17 +1148,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", @@ -1173,293 +1162,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 as a default stream.", + "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.", }, ], - 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( @@ -1469,18 +1445,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" @@ -1489,7 +1459,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( @@ -1513,44 +1485,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(