The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): keep api_server.py and _handle_runs under their health caps
Teknium75c99d8fb5a4
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.pyindex a9b7a0b6917..8dd719b47a3 100644--- a/gateway/platforms/api_server.py+++ b/gateway/platforms/api_server.py@@ -1096,13 +1096,6 @@ def _run_route_delegate(name: str):return _handler--def _delivered_session_id(admitted: tuple[Path, dict[str, Any]], requested: str) -> str:- """The session a live Bot Chat delivery actually landed in: the requested id, unless the owner moved- an API-held \"Bot Chat\" title aside and minted the local chat (MSG-21), whose id every answer names."""- return str(admitted[1].get("session_id") or requested)--class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):"""aiohttp server routing OpenAI-format requests through hermes-agent's AIAgent."""@@ -3465,7 +3458,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):admitted = await self._admit_to_live_bot_chat(ctx["session_id"], ctx["user_message"], ctx["run_kwargs"].get("turn_author"))if admitted is None:return None- session_id = _delivered_session_id(admitted, ctx["session_id"])+ session_id = admitted[1].get("session_id") or ctx["session_id"] # a moved-aside API row: the local chat (MSG-21)record = await self._await_live_bot_chat_receipt(*admitted)delivery_id = record["delivery_id"]headers = self._session_headers(session_id, ctx["gateway_session_key"])@@ -3506,7 +3499,7 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):admitted = await self._admit_to_live_bot_chat(ctx["session_id"], ctx["user_message"], ctx["run_kwargs"].get("turn_author"))if admitted is None:return None- session_id = _delivered_session_id(admitted, ctx["session_id"])+ session_id = admitted[1].get("session_id") or ctx["session_id"] # a moved-aside API row: the local chat (MSG-21)events = _SessionEventQueue(session_id, f"run_{uuid.uuid4().hex}")response = await self._prepare_sse_response(request, session_id, ctx["gateway_session_key"])diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.pyindex 6d44c938c6b..c7296d95cc0 100644--- a/gateway/platforms/api_server_runs.py+++ b/gateway/platforms/api_server_runs.py@@ -835,11 +835,7 @@ async def _handle_runs(self, request: web.Request, *, _api_server) -> web.Respon# Under session authority the same call admits the turn to the Bot Chat's own FIFO, which is# why it is decided BEFORE ``admit_api_turn`` would bind the chat as an API conversation.admitted = await self._admit_to_live_bot_chat(session_id, user_message, turn_author) if selected_session_id else None- if admitted is not None and (delivered := admitted[1].get("session_id")) and delivered != session_id:- # The owner moved an API-held "Bot Chat" title aside and the turn landed in the local chat it- # minted (MSG-21): the run record (and its idempotency replay) names that chat, not the old row.- session_id = launch.session_id = delivered- self._set_run_status(run_id, "queued", session_id=session_id)+ _retarget_to_delivered_chat(self, launch, admitted)if admitted is None and getattr(self.gateway_runner, 'session_authority', None) is not None:refused = await _admit_run_to_authority(self, launch, run_id, session_history_delivery, _declared_selected,@@ -859,6 +855,15 @@ async def _handle_runs(self, request: web.Request, *, _api_server) -> web.Responreturn _accepted_response(run_id, "started", gateway_session_key, replayed=False)+def _retarget_to_delivered_chat(self, launch: _RunLaunch, admitted) -> None:+ """The owner moved an API-held "Bot Chat" title aside and the turn landed in the local chat it+ minted (MSG-21): the run record (and its idempotency replay) names that chat, not the old row."""+ delivered = admitted[1].get("session_id") if admitted is not None else None+ if delivered and delivered != launch.session_id:+ launch.session_id = delivered+ self._set_run_status(launch.run_id, "queued", session_id=delivered)++def _run_usage(agent) -> dict[str, int]:"""Terminal ``usage`` payload from the agent's session counters; a missing or non-numericcounter (test doubles, agents without cache accounting) reads as ``0``."""