The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway/platforms): api_server — compact docstrings/comments (WHY kept), generated run/room-grant delegators
Teknium1f93b1a84f94
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.pyindex e2cd49f5dbe..cc839344d5a 100644--- a/gateway/platforms/api_server.py+++ b/gateway/platforms/api_server.py@@ -29,18 +29,14 @@ import uuidfrom pathlib import Pathfrom typing import Any, Dict, List, Optional-# Sentinel returned by _resolve_request_profile when a /p/<profile>/ prefix-# names a profile this gateway does not serve (→ 404). Distinct from None-# (no prefix / multiplexing off → handle as the default profile).+# _resolve_request_profile result for a /p/<profile>/ prefix this gateway does not serve (-> 404);+# distinct from None (no prefix / multiplexing off -> default profile)._PROFILE_REJECTED = object()def _prefix_names_served_profile(profile: str) -> bool:- """True when a /p/<profile>/ prefix names the profile this gateway serves.-- Single-profile gateways must not answer /p/<x>/ from their own config (that- served the owner's toolsets under another profile's URL). Fail closed.- """+ """True when a /p/<profile>/ prefix names the profile this gateway serves. Fail closed: a+ single-profile gateway answering /p/<x>/ served the owner's toolsets under another URL."""try:from hermes_cli.profiles import profile_matches_homereturn profile_matches_home(profile)@@ -48,8 +44,7 @@ def _prefix_names_served_profile(profile: str) -> bool:return False-# Profile selected by the /p/<profile>/ URL prefix for the current request.-# Set by the profile-prefix middleware; read by handlers / _run_agent.+# Per-request /p/<profile>/ selection: set by the profile-prefix middleware, read by handlers._api_request_profile: ContextVar[Optional[str]] = ContextVar("api_server_request_profile", default=None)_api_request_browser_control_principal: ContextVar[str] = ContextVar(@@ -154,9 +149,8 @@ logger = logging.getLogger(__name__)def _browser_controller_ws_sender(ws, loop, *, wait_timeout: float = 10.0):"""Return a loop-aware broker sender for one aiohttp controller socket.- A wait timeout means the coroutine is still in flight on a live loop, not- that the frame was rejected. Keep the broker command pending and let its- own deadline/cancel path decide; a real send exception still propagates.+ A wait timeout means the coroutine is still in flight, not that the frame was rejected:+ keep the broker command pending (its own deadline decides); a real send error propagates."""def send(frame: dict) -> None:@@ -349,11 +343,10 @@ def _request_agent_overrides() -> Dict[str, Any]:"""Extract per-request model/provider/options for _run_agent.- The advertised virtual model (``hermes-agent``) means "gateway default". A bare- ``model`` without ``provider`` is honored only when ``allow_bare_model`` (generic- OpenAI clients hardcode "gpt-4o" and rely on the default; OpenAI-compatible- handlers pass the ``direct_model_requests`` opt-in, Hermes-native endpoints- always allow it). An explicit ``provider`` is always honored.+ The virtual model (``hermes-agent``) means "gateway default". A bare ``model`` without+ ``provider`` is honored only when ``allow_bare_model`` (generic clients hardcode "gpt-4o";+ OpenAI-compatible handlers pass the ``direct_model_requests`` opt-in, Hermes-native+ endpoints always allow it). An explicit ``provider`` is always honored."""if not isinstance(body, dict):return {}@@ -429,11 +422,8 @@ def _cap_list(items: list) -> list:def _normalize_chat_content(content: Any, *, _max_depth: int = 10, _depth: int = 0) -> str:- """Flatten OpenAI chat content (string or typed-part array) into one plain string.-- Non-text parts (image_url, ...) are skipped. Recursion depth, list size and- output length are bounded.- """+ """Flatten OpenAI chat content (string or typed-part array) into one plain string; non-text+ parts are skipped, recursion depth / list size / output length are bounded."""if _depth > _max_depth or content is None:return ""if isinstance(content, str):@@ -504,13 +494,11 @@ def _normalize_image_part(part: Dict[str, Any]) -> Dict[str, Any]:def _normalize_multimodal_content(content: Any) -> Any:- """Validate multimodal content: a plain string when text-only, else a list of canonical- ``text``/``image_url`` parts (native OpenAI vision format; Anthropic conversion happens- downstream).+ """Validate multimodal content: a plain string when text-only, else canonical ``text`` /+ ``image_url`` parts (native OpenAI vision shape; Anthropic conversion happens downstream).- Raises ``ValueError("<code>:<message>")`` — codes ``unsupported_content_type`` (file- parts, non-image data URLs, unknown part types), ``invalid_image_url``,- ``invalid_content_part``. Callers translate it into a 400.+ Raises ``ValueError("<code>:<message>")`` with codes ``unsupported_content_type`` (file+ parts, non-image data URLs, unknown types), ``invalid_image_url``, ``invalid_content_part``."""if content is None:return ""@@ -920,12 +908,9 @@ _api_agent_request_reservation: ContextVar[Optional[dict[str, bool]]] = ContextVdef _admit_api_agent_request(handler):- """Reserve an authenticated API turn before its handler first awaits.-- Drain check + reservation happen in one non-awaiting block so a request admitted- just before shutdown can't become invisible while parsing its body. The mutable- reservation is shared with child tasks so the slot is released exactly once.- """+ """Reserve an authenticated API turn before its handler first awaits: drain check ++ reservation in one non-awaiting block so a request admitted just before shutdown can't go+ invisible while parsing its body. The mutable reservation releases the slot exactly once."""@wraps(handler)async def _wrapped(self, request, *args, **kwargs):auth_err = (@@ -1071,17 +1056,13 @@ except Exception: # pragma: no cover - scanner is optional hardeningclass _ProviderAuthResolutionError(RuntimeError):- """Provider credential resolution failed (the sole RuntimeError raiser in _create_agent's- call graph). A typed subclass so callers never mislabel other RuntimeErrors from- run_conversation() (e.g. "Failed to recreate closed OpenAI client") as auth failures."""+ """Provider credential resolution failed. Typed so callers never mislabel other+ RuntimeErrors from run_conversation() (e.g. a closed OpenAI client) as auth failures."""class _SessionEventQueue:- """Ordered SSE event queue for one /api/sessions/{id}/chat/stream run.-- ``payload`` stamps session_id/run_id/seq/ts; ``enqueue`` is safe from the agent's- executor thread (hops onto the owning loop) and swallows a closed-loop RuntimeError.- """+ """Ordered SSE event queue for one /api/sessions/{id}/chat/stream run. ``payload`` stamps+ session_id/run_id/seq/ts; ``enqueue`` is executor-thread safe (hops onto the owning loop)."""def __init__(self, session_id: str, run_id: str):self.loop = asyncio.get_running_loop()@@ -1111,11 +1092,29 @@ class _SessionEventQueue:self.loop.call_soon_threadsafe(self.queue.put_nowait, event)+def _room_grant_delegate(name: str):+ """Adapter method forwarding to ``api_server_room_grants.<name>`` (looked up at call time so+ test patches on that module take effect) with this module's error/profile bindings."""+ async def _handler(self, request: "web.Request") -> "web.Response":+ return await getattr(_room_grants, name)(+ self, request, _openai_error=_openai_error, _api_request_profile=_api_request_profile)+ _handler.__name__ = name+ return _handler+++def _run_route_delegate(name: str):+ """Adapter method forwarding to ``api_server_runs.<name>`` (call-time lookup) with this+ module's namespace as ``_api_server``."""+ async def _handler(self, request: "web.Request") -> "web.StreamResponse":+ return await getattr(_api_runs, name)(self, request, _api_server=sys.modules[__name__])+ _handler.__name__ = name+ return _handler++class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):"""aiohttp server routing OpenAI-format requests through hermes-agent's AIAgent."""- # Stateless request/response: every route tears down its channel when the turn- # ends and ``send()`` is a stub, so async-delivery tools must not promise+ # Stateless request/response (``send()`` is a stub): async-delivery tools must not promise# delivery here, and a resumed turn completes the work rather than asking.supports_async_delivery: bool = Falseinteractive_resume: bool = False@@ -1137,11 +1136,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):extra.get("cors_origins", os.getenv("API_SERVER_CORS_ORIGINS", "")))self._model_name: str = self._resolve_model_name(extra.get("model_name", os.getenv("API_SERVER_MODEL_NAME", "")))- # model_routes: alias (what the client sends as "model") -> {model, provider?, api_key?- # (UPSTREAM key, never logged), base_url?}. See _parse_model_routes.+ # alias (client "model") -> {model, provider?, api_key? (UPSTREAM, never logged), base_url?}self._model_routes: Dict[str, Dict[str, Any]] = self._parse_model_routes(extra.get("model_routes"))- # Opt-in passthrough for a bare ``model`` (no provider) on the OpenAI-compatible- # surfaces; off by default because generic clients hardcode "gpt-4o" etc.+ # Opt-in bare ``model`` passthrough on OpenAI-compatible surfaces (generic clients+ # hardcode "gpt-4o" etc., hence off by default).self._direct_model_requests: bool = _coerce_request_bool(extra.get("direct_model_requests"), default=False)self._app: Optional["web.Application"] = None@@ -1153,15 +1151,15 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):self._session_dbs: Dict[str, Any] = {} # per-profile-home SessionDB cacheself._session_db_cache_lock = threading.Lock()self._session_db_cache_closed = False- # Last-known-good model per gateway_session_key ("*" = process-wide); never keyed by- # session_id (per request -> unbounded). Recovers a transient empty model resolution.+ # Last-known-good model per gateway_session_key ("*" = process-wide; never session_id,+ # which is per request -> unbounded). Recovers a transient empty model resolution.self._last_resolved_model: Dict[str, str] = {}self._session_db_lock: Optional[asyncio.Lock] = None # single-flight for lazy initself._max_concurrent_runs: int = self._resolve_max_concurrent_runs() # 0 disables# In-flight _run_agent() turns (/v1/runs tracks its own via _active_run_tasks).self._inflight_agent_runs: int = 0- # Every agent inside _run_agent() for shutdown interrupt (NOT the run_id-keyed- # _active_run_agents). Keyed by id(); the strong ref keeps the id() from recycling.+ # Every agent inside _run_agent() for shutdown interrupt, keyed by id() (the strong ref+ # keeps the id() from recycling); distinct from the run_id-keyed _active_run_agents.self._shutdown_interruptible_agents: Dict[int, Any] = {}self.gateway_runner: Optional[Any] = None # set by gateway/run.py# Admitted requests not yet in agent bookkeeping, so shutdown drain counts them.@@ -1355,12 +1353,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):status=401)def _check_auth(self, request: "web.Request") -> Optional["web.Response"]:- """Validate the Bearer token; None when OK, else a 401.-- connect() refuses to start without API_SERVER_KEY, so the no-key branch only exists- for tests/manual wiring — and only on the default listener; named profiles fail closed- rather than inherit the listener owner's key.- """+ """Validate the Bearer token; None when OK, else a 401. The no-key branch (connect()+ refuses to start without API_SERVER_KEY) exists for tests/manual wiring on the default+ listener only; named profiles fail closed rather than inherit the owner's key."""profile = _api_request_profile.get()expected_key = self._expected_api_key()if not expected_key:@@ -1450,13 +1445,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):# -- Multi-profile multiplexing (/p/<profile>/...) --------------------------------def _resolve_request_profile(self, request: "web.Request"):- """Resolve + validate the /p/<profile>/ prefix.-- Returns ``None`` (no prefix, or multiplexing off and the prefix names this- gateway's own profile), the profile name (multiplexing on, profile served), or- ``_PROFILE_REJECTED`` (unknown/unserved profile -> 404). Fail closed: ignoring a- foreign prefix served the owner's toolsets under another profile's URL.- """+ """Resolve + validate the /p/<profile>/ prefix: ``None`` (no prefix, or multiplexing+ off and the prefix names this gateway's own profile), the served profile name, or+ ``_PROFILE_REJECTED`` (-> 404). Fail closed: a foreign prefix must never be ignored."""profile = (request.match_info.get("profile") or "").strip()if not profile:return None@@ -1474,12 +1465,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):@staticmethoddef _profile_scope(profile: Optional[str]):- """Enter the multiplex profile runtime scope, or a no-op when unset.-- No prefix AND multiplexing active enters the DEFAULT profile's scope instead of a- no-op: with fail-closed ``get_secret`` an unscoped run raises UnscopedSecretError- on its first credential read. Single-profile gateways keep the no-op.- """+ """Enter the multiplex profile runtime scope, or a no-op when unset. No prefix AND+ multiplexing active enters the DEFAULT profile's scope (an unscoped run would raise+ UnscopedSecretError on its first credential read); single-profile gateways no-op."""if not profile:with suppress(Exception):from agent.secret_scope import is_multiplex_active@@ -1577,13 +1565,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):def _declared_conversation_session(self, gateway_session_key: Optional[str]) -> Optional[str]:- """Resolve the live session a client declared with ``X-Hermes-Session-Key``.-- The key names the conversation; ``session_id`` its current transcript. Same- reset-fenced recovery as ``SessionStore._recover_session_for_peer`` (rows ended at- a conversation boundary are fenced out). Two concurrent first requests converge- (same key + source -> later row wins). None when undeclared, no live row, or DB error.- """+ """Resolve the live session a client declared with ``X-Hermes-Session-Key`` (the key+ names the conversation, ``session_id`` its current transcript). Same reset-fenced+ recovery as ``SessionStore._recover_session_for_peer``; concurrent first requests+ converge (later row wins). None when undeclared, no live row, or DB error."""key = (gateway_session_key or "").strip()db = self._ensure_session_db() if key else Noneif db is None:@@ -1598,9 +1583,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):def _bind_declared_conversation(self, session_id: Optional[str], gateway_session_key: Optional[str]) -> None:- """Record the declared conversation key on the session row (AIAgent writes it unkeyed).- ``include_compression_ancestors`` shares the key across a mid-turn compression rotation- only. UPDATE semantics: a no-op if the turn failed before row creation."""+ """Record the declared conversation key on the session row (AIAgent writes it unkeyed);+ ``include_compression_ancestors`` covers a mid-turn rotation. UPDATE: no-op w/o a row."""key = (gateway_session_key or "").strip()sid = str(session_id or "").strip()db = self._ensure_session_db() if key and sid else None@@ -1625,9 +1609,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):def _parse_session_key_header(self, request: "web.Request") -> tuple[Optional[str], Optional["web.Response"]]:- """Validate ``X-Hermes-Session-Key`` (stable per-channel memory scope, independent of- ``X-Hermes-Session-Id``). Returns ``(key_or_None, None)`` or ``(None, error)``.- Requires API-key auth so an unauthenticated client can't guess another memory scope."""+ """Validate ``X-Hermes-Session-Key`` (per-channel memory scope) -> ``(key_or_None, None)``+ or ``(None, error)``. Requires API-key auth so a client can't guess another scope."""raw = request.headers.get("X-Hermes-Session-Key", "").strip()if not raw:return None, None@@ -1647,9 +1630,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):# -- Session DB -------------------------------------------------------------------def _open_and_cache_session_db(self, home) -> Optional[Any]:- """Sync core shared by both ``_ensure_session_db*`` entry points: the cached SessionDB- for ``home``, opened once. Never writes ``self._session_db`` (explicit override only),- so the first profile served can't pin later requests to its DB."""+ """Cached SessionDB for ``home`` (shared by both ``_ensure_session_db*``). Never writes+ ``self._session_db`` (explicit override only), so no profile pins later requests."""from hermes_state import SessionDBkey = str(home)with self._session_db_cache_lock:@@ -1677,9 +1659,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):logger.debug("Failed to close API-server SessionDB", exc_info=True)def _ensure_session_db(self):- """SessionDB for the active profile home (the multiplex runtime scope redirects- ``get_hermes_home()``, so each profile gets its own DB). Sync, for ``_create_agent``;- request handlers use ``_ensure_session_db_async``."""+ """SessionDB for the active profile home (the runtime scope redirects ``get_hermes_home()``+ per profile). Sync, for ``_create_agent``; handlers use ``_ensure_session_db_async``."""if self._session_db is not None:return self._session_dbtry:@@ -1718,11 +1699,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):@staticmethoddef _parse_model_routes(raw: Any) -> Dict[str, Dict[str, Any]]:- """Validate ``model_routes``: ``alias -> {model, provider?, api_key?, base_url?}``.-- Invalid shapes are dropped, never raised. Route ``api_key`` values are UPSTREAM- provider credentials, not caller auth, and must never be logged.- """+ """Validate ``model_routes`` (``alias -> {model, provider?, api_key?, base_url?}``); invalid+ shapes are dropped, never raised. Route ``api_key`` is an UPSTREAM credential: never log."""if not isinstance(raw, dict):if raw:logger.warning(@@ -1986,11 +1964,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):def _resolve_provider_runtime(provider: Optional[str], *, target_model: Optional[str], required: bool,) -> Optional[Dict[str, Any]]:- """Runtime kwargs for ``provider``; falls back to the gateway's provider resolver.-- ``required`` raises the typed ``_ProviderAuthResolutionError`` (so callers return- the controlled response shape, not a raw 500) instead of returning None.- """+ """Runtime kwargs for ``provider``, falling back to the gateway's resolver; ``required``+ raises ``_ProviderAuthResolutionError`` (controlled response, not a raw 500), not None."""provider_name = _clean_request_string(provider)if not provider_name:return None@@ -2041,14 +2016,11 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):self, runtime_kwargs: Dict[str, Any], model: str, *, requested_model: Optional[str],requested_provider: Optional[str], route: Optional[Dict[str, Any]], session_model: Optional[str],confirmed_runtime_lock: bool, gateway_session_key: Optional[str], session_id: Optional[str]) -> tuple:- """Apply the model/provider precedence chain for one agent (mutates ``runtime_kwargs``).-- Precedence: confirmed Browser model lock > session ``/model`` override >- session-persisted model > model_routes alias > per-request provider/model >- global defaults. A confirmed lock bypasses the session override and fails closed- if its provider cannot be resolved. model_options stay request-scoped whichever- wins. Returns ``(model, session_override, request_model, request_provider)``.- """+ """Apply the model/provider precedence chain for one agent (mutates ``runtime_kwargs``):+ confirmed Browser lock > session ``/model`` override > session-persisted model >+ model_routes alias > per-request provider/model > global defaults. A confirmed lock+ bypasses the override and fails closed if its provider cannot be resolved.+ Returns ``(model, session_override, request_model, request_provider)``."""request_model = _clean_request_string(requested_model)request_provider = _clean_request_string(requested_provider)route_cfg = route if isinstance(route, dict) else {}@@ -2124,25 +2096,22 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):room_dispatch: Optional[Dict[str, Any]] = None,room_execution_policy: Optional[Dict[str, Any]] = None) -> Any:"""Create an AIAgent from the gateway runtime config + platform toolsets.-- ``gateway_session_key`` persists across transcripts (memory scope), unlike- ``session_id``. ``route`` and ``session_model`` are mutually exclusive;- ``confirmed_runtime_lock`` beats the session ``/model`` override, disables the- fallback chain and fails closed (see ``_select_agent_runtime``).- """+ ``gateway_session_key`` persists across transcripts (memory scope), unlike ``session_id``;+ ``route`` / ``session_model`` are mutually exclusive; ``confirmed_runtime_lock`` beats the+ session ``/model`` override, disables the fallback chain and fails closed."""from run_agent import AIAgentfrom gateway.run import (_checkpoint_agent_kwargs, _current_max_iterations, _resolve_runtime_agent_kwargs,_resolve_gateway_model, _load_gateway_config, GatewayRunner)from hermes_cli.tools_config import _get_platform_tools- # RuntimeError is caught ONLY around this call: it is the sole raiser for provider- # auth failure, and the typed subclass keeps run_conversation() errors distinct.+ # RuntimeError is caught ONLY here (sole provider-auth raiser); the typed subclass keeps+ # run_conversation() errors distinct.try:runtime_kwargs = _resolve_runtime_agent_kwargs()except RuntimeError as exc:raise _ProviderAuthResolutionError(str(exc)) from exc- # A fallback-provider runtime carries its own ``model``; pop it so it overrides the- # config model instead of colliding with the ``**runtime_kwargs`` spread.+ # A fallback-provider runtime carries its own ``model``: pop it (overrides config, and+ # must not collide with the ``**runtime_kwargs`` spread).model = runtime_kwargs.pop("model", None) or _resolve_gateway_model()request_reasoning_config = _request_reasoning_config(model_options)request_service_tier = _request_service_tier(model_options)@@ -2159,8 +2128,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):policy = RoomExecutionPolicy.from_mapping(room_execution_policy or {})enabled_toolsets = list(policy.enabled_toolsets)max_iterations = policy.max_iterations- # Reasoning resolves against the model that will actually run (per-model overrides key- # off it), so only after the precedence chain settles; an explicit request wins.+ # Reasoning resolves against the model that actually runs (per-model overrides), so only+ # after the precedence chain settles; an explicit request wins.if request_reasoning_config is None:request_reasoning_config = GatewayRunner._load_reasoning_config(model)agent_kwargs = {@@ -2307,11 +2276,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):async def _handle_browser_control_register(self, request: "web.Request") -> "web.Response":"""POST /v1/browser-control/register — mint a short-lived single-use controller ticket.- Identity is NOT taken from the body: the scope principal is a server-derived digest of- the authenticated key/profile and capabilities are filtered to the shared allowlist,- so a spoofed ``principal_id`` or inflated capability list is ignored. The named- session must already exist in the profile's SessionDB. Status ladder: 404 feature- disabled, 403 no API key configured, 401 bad Bearer, 201 success.+ Identity is NOT taken from the body: the principal is a server-derived digest of the+ authenticated key/profile and capabilities are filtered to the allowlist (a spoofed+ ``principal_id`` / inflated list is ignored); the session must exist in the profile's+ SessionDB. Ladder: 404 disabled, 403 no API key, 401 bad Bearer, 201 success."""if not self._browser_control_enabled():return _error_response(@@ -2387,10 +2355,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):async def _handle_browser_control_ws(self, request: "web.Request") -> "web.WebSocketResponse":"""GET /v1/browser-control/ws — controller WebSocket (one-shot ticket).- The ticket rides in ``Sec-WebSocket-Protocol`` (never the query string: request targets- land in access logs) and is exchanged exactly once for the registration scope; bad,- consumed or expired tickets 401 before upgrade. The socket attaches to the broker under- that scope; owner-aware teardown cannot detach a newer controller generation.+ The ticket rides in ``Sec-WebSocket-Protocol`` (never the query string: targets land in+ access logs) and is exchanged once for the registration scope; bad/consumed/expired+ tickets 401 before upgrade. Owner-aware teardown cannot detach a newer generation."""# Re-checked at upgrade so disabling the feature closes the gate immediately.if not self._browser_control_enabled():@@ -2419,8 +2386,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):loop = asyncio.get_running_loop()_send = _browser_controller_ws_sender(ws, loop)- # attach/disconnect take the controller send_lock, which a worker-thread dispatch- # may hold while blocking on THIS loop; offload so the race parks a worker, not the loop.+ # attach/disconnect take the controller send_lock, which a worker-thread dispatch may+ # hold while blocking on THIS loop: offload so the race parks a worker, not the loop.await asyncio.to_thread(self._browser_control_broker.attach, scope, _send, owner=ws)try:async for msg in ws:@@ -2507,12 +2474,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):# -- One-shot artifact transport --------------------------------------------------def _artifact_store_for(self, profile: str) -> ArtifactStore:- """Profile-scoped artifact store, created lazily under the profile's data dir.-- Cached BY RESOLVED PROFILE: on a multiplex listener, profile A touching the route- first must never pin profile B to A's physical root. TTL cleanup runs on every- store/load/prune.- """+ """Profile-scoped artifact store, lazily created under the profile's data dir and cached+ BY RESOLVED PROFILE (on a multiplex listener profile A must never pin B to A's root)."""profile_key = str(profile or "default")store = self._browser_control_artifacts.get(profile_key)if store is not None:@@ -2558,11 +2521,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):self._browser_control_artifact_limiter = limiterdef _artifact_route_prelude(self, request: "web.Request", action: str, *, check_enabled: bool = True) -> tuple:- """Shared upload/download gate: feature flag → API key → Bearer → per-principal rate limit.-- Returns ``((profile, principal), None)`` or ``(None, error_response)``.- ``action`` is ``"upload"``/``"download"`` (limiter bucket + error text).- """+ """Shared upload/download gate (feature flag -> API key -> Bearer -> per-principal rate+ limit) -> ``((profile, principal), None)`` or ``(None, error_response)``."""if check_enabled and not self._browser_control_enabled():return None, _error_response("Browser control is not enabled on this server.", 404, code="browser_control_disabled")@@ -2582,14 +2542,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):return (profile, principal), Noneasync def _handle_artifact_upload(self, request: "web.Request") -> "web.Response":- """POST /v1/artifacts/upload — one-shot bounded upload.-- Raw body with an exact size cap; ``Content-Type`` must be an allowed MIME type,- ``X-Artifact-Filename`` is display-only. Returns a provenance receipt (server-minted- id, SHA-256, size, TTL, download path — never a filesystem path). Status ladder: 404- disabled, 403 no API key, 401 bad Bearer, 429 rate limited, 413 too large, 415 MIME- rejected, 400 missing filename/scope, 201 success.- """+ """POST /v1/artifacts/upload — one-shot bounded raw upload; ``Content-Type`` must be an+ allowed MIME type, ``X-Artifact-Filename`` is display-only. Returns a provenance receipt+ (never a filesystem path). Ladder: 404 disabled, 403 no API key, 401 bad Bearer, 429+ rate limited, 413 too large, 415 MIME rejected, 400 missing filename/scope, 201."""ctx, err = self._artifact_route_prelude(request, "upload")if err is not None:return err@@ -2625,10 +2581,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):receipt.to_dict(download_path=f"/v1/artifacts/download/{receipt.artifact_id}"), status=201)async def _handle_artifact_download(self, request: "web.Request") -> "web.Response":- """GET /v1/artifacts/download/{artifact_id} — one-shot download (consumed on success;- a second download 404s). Streams the verified bytes with ``X-Artifact-Sha256``. Status- ladder: 404 disabled/unknown, 403 no API key, 401 bad Bearer, 429 rate limited, 410- expired, 400 invalid id/scope mismatch, 200 success."""+ """GET /v1/artifacts/download/{artifact_id} — one-shot download (a second one 404s) with+ ``X-Artifact-Sha256``. Ladder: 404 disabled/unknown, 403 no API key, 401 bad Bearer, 429+ rate limited, 410 expired, 400 invalid id/scope mismatch, 200."""if not self._browser_control_enabled():raise web.HTTPNotFound()ctx, err = self._artifact_route_prelude(request, "download", check_enabled=False)@@ -2807,11 +2762,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):@_require_authasync def _handle_create_session(self, request: "web.Request") -> "web.Response":- """POST /api/sessions -- create an empty Hermes session row.-- Existence check, insert, title handling and invalid-title rollback run as ONE- off-loop write so concurrent same-id creates cannot both pass the check and 201.- """+ """POST /api/sessions -- create an empty Hermes session row. Existence check, insert and+ title handling run as ONE off-loop write so concurrent same-id creates can't both 201."""body, err = await self._read_json_body(request)if err:return err@@ -3007,13 +2959,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):async def _prepare_session_chat(self, request: "web.Request") -> tuple:"""Shared prelude for /api/sessions/{id}/chat[/stream]: header/body validation, then- runtime selection. A Browser model lock (body ``require_model_lock`` or a confirmed- persisted lock) wins; otherwise the session-persisted model routes via model_routes- when it is an alias or threads through as ``session_model`` when raw, then body values.-- Returns ``(ctx_dict, None)`` or ``(None, error_response)``; ``ctx["run_kwargs"]`` is- the ``_run_agent`` argument set shared by the sync and stream handlers.- """+ runtime selection — a Browser model lock (body ``require_model_lock`` or a confirmed+ persisted lock) wins; else the session-persisted model routes via model_routes when an+ alias or threads through as ``session_model`` when raw, then body values.+ Returns ``(ctx, None)`` or ``(None, error)``; ``ctx["run_kwargs"]`` feeds ``_run_agent``."""gateway_session_key, key_err = self._parse_session_key_header(request)if key_err is not None:return None, key_err@@ -3470,13 +3419,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):lambda jid: _cron_trigger(jid, extra_prompt=extra_prompt), job_id, notify=False)async def _handle_cron_fire(self, request: "web.Request") -> "web.Response":- """POST /api/cron/fire — Chronos managed-cron fire webhook (NAS -> agent).-- Authenticated by a NAS-minted JWT via the pluggable fire-verifier, NOT- API_SERVER_KEY (the only inbound that can trigger remote job execution). 202 +- background run so a long turn never trips NAS's HTTP timeout; the store CAS claim- guards against double-fire on retry.- """+ """POST /api/cron/fire — Chronos fire webhook (NAS -> agent), authenticated by a+ NAS-minted JWT via the pluggable verifier, NOT API_SERVER_KEY. 202 + background run so+ a long turn never trips NAS's timeout; the store CAS claim guards double-fire on retry."""from hermes_cli.config import cfg_get, load_configfrom plugins.cron_providers.chronos.verify import get_fire_verifierauth = request.headers.get("Authorization", "")@@ -3557,11 +3502,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):task.add_done_callback(self._background_tasks.discard)def _concurrency_limited_response(self) -> Optional["web.Response"]:- """429 when the concurrent-run cap is reached (0 disables), else None.-- Uses the same adapter-owned work count as shutdown draining (admitted requests- included). Stream queues are transport state and must not define run concurrency.- """+ """429 when the concurrent-run cap is reached (0 disables), else None. Uses the same+ adapter-owned work count as shutdown draining (admitted requests included)."""limit = self._max_concurrent_runsif limit <= 0:return None@@ -3580,12 +3522,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):def _bind_api_server_session(*, chat_id: str = "", session_key: str = "", session_id: str = "",browser_control_principal: str = "", browser_control_transport_family: str = "") -> list:- """Bind session contextvars for an API-server agent run — the SINGLE chokepoint every- agent-entry path uses. Hardwires ``platform="api_server"`` and ``async_delivery=False``- (the stateless HTTP path can never wake the agent after the turn), so no new route can- reintroduce the silent-no-op bug. Returns reset tokens for ``clear_session_vars`` in a- ``finally``: the binding is request-scoped; a later resume on a delivering interface- re-binds fresh."""+ """Bind session contextvars for an API-server agent run — the SINGLE chokepoint for every+ agent-entry path. Hardwires ``platform="api_server"`` + ``async_delivery=False`` (HTTP+ can never wake the agent after the turn) so no route reintroduces the silent no-op bug.+ Returns reset tokens for ``clear_session_vars`` in a ``finally`` (request-scoped)."""from gateway.session_context import set_session_varsreturn set_session_vars(platform="api_server", chat_id=chat_id, session_key=session_key, session_id=session_id,@@ -3667,12 +3607,9 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):requested_runtime: Optional[Dict[str, Any]] = None, route_source: str = "global",confirmed_runtime_lock: bool = False, bind_declared_conversation: bool = False) -> tuple:"""Create an agent and run one turn in a thread executor -> ``(result, usage)``.-- ``agent_ref[0]`` receives the agent before the turn so SSE writers can interrupt- it; ``active_run_id`` registers it in ``_active_run_agents`` for run control. Under- a confirmed model lock the actual provider/model must match or the turn fails, and- ``runtime`` metadata (actual vs requested) is attached to result and usage.- """+ ``agent_ref[0]`` receives the agent so SSE writers can interrupt it; ``active_run_id``+ registers it in ``_active_run_agents``. Under a confirmed model lock the actual+ provider/model must match or the turn fails; ``runtime`` metadata is attached."""loop = asyncio.get_running_loop()# ContextVars do not follow run_in_executor threads: capture here, re-enter in _run().request_profile = _api_request_profile.get()@@ -3741,9 +3678,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):finally:self._inflight_agent_runs -= 1- # -- /v1/runs, room grants, room dispatch: thin delegators ------------------------- # Kept as real methods (tests assert __dict__ membership and patch the module-level- # implementations); ``_api_server=`` hands the implementation this module's namespace.+ # -- /v1/runs, room grants, room dispatch: thin delegators (real methods: tests assert+ # __dict__ membership and patch the module-level implementations) ---------------------_RUN_STREAM_TTL = 300 # seconds before orphaned runs are swept_RUN_STATUS_TTL = 3600 # seconds to retain terminal run status for polling@@ -3776,21 +3712,10 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):async def _normalize_room_dispatch(self, request: "web.Request", body: Any) -> tuple[Any, "web.Response | None"]:return await _room_dispatch._normalize_room_dispatch(self, request, body, _api_server=sys.modules[__name__])- async def _handle_room_member_invitation(self, request: "web.Request") -> "web.Response":- return await _room_grants._handle_room_member_invitation(- self, request, _openai_error=_openai_error, _api_request_profile=_api_request_profile)-- async def _handle_room_member_capabilities(self, request: "web.Request") -> "web.Response":- return await _room_grants._handle_room_member_capabilities(- self, request, _openai_error=_openai_error, _api_request_profile=_api_request_profile)-- async def _handle_room_member_grant_refresh(self, request: "web.Request") -> "web.Response":- return await _room_grants._handle_room_member_grant_refresh(- self, request, _openai_error=_openai_error, _api_request_profile=_api_request_profile)-- async def _handle_room_member_grant_revoke(self, request: "web.Request") -> "web.Response":- return await _room_grants._handle_room_member_grant_revoke(- self, request, _openai_error=_openai_error, _api_request_profile=_api_request_profile)+ _handle_room_member_invitation = _room_grant_delegate("_handle_room_member_invitation")+ _handle_room_member_capabilities = _room_grant_delegate("_handle_room_member_capabilities")+ _handle_room_member_grant_refresh = _room_grant_delegate("_handle_room_member_grant_refresh")+ _handle_room_member_grant_revoke = _room_grant_delegate("_handle_room_member_grant_revoke")def _durable_run_status(self, request: "web.Request", run_id: str) -> Dict[str, Any] | None:return _api_runs._durable_run_status(self, request, run_id)@@ -3805,20 +3730,11 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):def _release_run_owner_if_forgotten(self, run_id: str) -> None:_api_runs._release_run_owner_if_forgotten(self, run_id)- async def _handle_get_run(self, request: "web.Request") -> "web.Response":- return await _api_runs._handle_get_run(self, request, _api_server=sys.modules[__name__])-- async def _handle_run_events(self, request: "web.Request") -> "web.StreamResponse":- return await _api_runs._handle_run_events(self, request, _api_server=sys.modules[__name__])-- async def _handle_run_approval(self, request: "web.Request") -> "web.Response":- return await _api_runs._handle_run_approval(self, request, _api_server=sys.modules[__name__])-- async def _handle_steer_run(self, request: "web.Request") -> "web.Response":- return await _api_runs._handle_steer_run(self, request, _api_server=sys.modules[__name__])-- async def _handle_stop_run(self, request: "web.Request") -> "web.Response":- return await _api_runs._handle_stop_run(self, request, _api_server=sys.modules[__name__])+ _handle_get_run = _run_route_delegate("_handle_get_run")+ _handle_run_events = _run_route_delegate("_handle_run_events")+ _handle_run_approval = _run_route_delegate("_handle_run_approval")+ _handle_steer_run = _run_route_delegate("_handle_steer_run")+ _handle_stop_run = _run_route_delegate("_handle_stop_run")async def _sweep_orphaned_runs(self) -> None:return await _api_runs._sweep_orphaned_runs(self)@@ -3867,8 +3783,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):with self._session_db_cache_lock:self._session_db_cache_closed = Falseif not self._api_key_passes_startup_guard():- # A config error, not transient: a bare ``return False`` would make the reconnect- # watcher re-instantiate the adapter (+ sqlite connection) until EMFILE.+ # Config error, not transient: a bare ``return False`` would make the reconnect watcher+ # re-instantiate the adapter (+ sqlite connection) until EMFILE.self._set_fatal_error("api_server_key_invalid","API_SERVER_KEY was rejected by the startup guard (missing, "@@ -3884,19 +3800,19 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):security_headers_middleware) if mw is not None]self._app = web.Application(middlewares=mws, client_max_size=MAX_REQUEST_BYTES)assert self._app is not None- # Native routes + multiplex /p/<profile>/ mirrors (same handlers; the prefix- # middleware validates and scopes config/credentials when multiplexing is on).+ # Native routes + multiplex /p/<profile>/ mirrors (the prefix middleware validates and+ # scopes config/credentials when multiplexing is on).for method, path, handler in self._http_route_table():self._app.router.add_route(method, path, handler)self._app.router.add_route(method, f"/p/{{profile}}{path}", handler)- # Set after native routes: Relay bootstrap shims feature-detect on this key and- # must no-op rather than shadow the native session-control handlers.+ # After native routes: Relay bootstrap shims feature-detect on this key and must+ # no-op rather than shadow the native session-control handlers.self._app["api_server_adapter"] = selfif self.gateway_runner is not None:self._app["gateway_runner"] = self.gateway_runnerself._track_background_task(asyncio.create_task(self._sweep_orphaned_runs()))- # Network-accessible + unsandboxed local terminal backend = host-user RCE surface.- # Warn, don't refuse: the operator may have a firewall / strong key.+ # Network-accessible + unsandboxed local terminal backend = host-user RCE surface;+ # warn, don't refuse (the operator may have a firewall / strong key).if is_network_accessible(self._host):try:from hermes_cli.config import load_config as _load_cfg@@ -3917,9 +3833,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):self._wire_plugin_handlers(self._app)self._runner = web.AppRunner(self._app)await self._runner.setup()- # Bind directly (a pre-probe raced the real bind and misreported TIME_WAIT as "in- # use"). SO_REUSEADDR off on macOS (BSD semantics can split traffic between two- # listeners), default on Linux (only permits TIME_WAIT rebind).+ # Bind directly (a pre-probe raced the bind, misreporting TIME_WAIT as "in use").+ # SO_REUSEADDR off on macOS (BSD can split traffic between two listeners).self._site = web.TCPSite(self._runner, self._host, self._port, reuse_address=False if sys.platform == "darwin" else None)try:@@ -3929,8 +3844,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):self._runner = Noneself._site = Noneif getattr(exc, "errno", None) == errno.EADDRINUSE:- # A config error: non-retryable, or the reconnect watcher would retry- # forever leaking ResponseStore fds each time.+ # Config error: non-retryable, or the reconnect watcher leaks fds forever.self._set_fatal_error("api_server_port_in_use",f"Port {self._port} already in use. Set "@@ -3952,9 +3866,8 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):return Falseasync def disconnect(self) -> None:- """Stop the aiohttp server and release all owned resources, including the ResponseStore- SQLite connection: the reconnect loop builds a fresh adapter per retry, so leaking its- 2 fds (db + WAL) exhausted the fd limit after ~12h of failed reconnects."""+ """Stop the aiohttp server and release every owned resource, including the ResponseStore+ connection (the reconnect loop builds a fresh adapter per retry; leaked fds hit EMFILE)."""self._mark_disconnected()if self._response_store is not None:try: