From aa0eb8b04f56f04fbee38af09eebe7b5f1f2a56b Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 23:49:08 -0700 Subject: [PATCH] =?UTF-8?q?refactor(hermes=5Fcli):=20relay=5Fshared=5Fmetr?= =?UTF-8?q?ics=20=E2=80=94=20shared=20=5Frun=5Ftask=5Fhook=20for=20the=20t?= =?UTF-8?q?wo=20boundary=20entrypoints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../observability/relay_shared_metrics.py | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/hermes_cli/observability/relay_shared_metrics.py b/hermes_cli/observability/relay_shared_metrics.py index 61675811ea..77a85218ca 100644 --- a/hermes_cli/observability/relay_shared_metrics.py +++ b/hermes_cli/observability/relay_shared_metrics.py @@ -150,16 +150,16 @@ class _Runtime: raise RuntimeError("Hermes core Relay runtime is unavailable") self.host: relay_runtime.RelayRuntime = resolved_host self.relay = self.host.relay - self._sessions_lock = threading.RLock() self._active = True self._sessions: dict[str, _MetricsSession] = {} + self._task_sessions: dict[tuple[str, str], _MetricsSession] = {} + self._turn_sessions: dict[tuple[str, str], _MetricsSession] = {} + self._sessions_lock = threading.RLock() self._task_creation_lock = threading.RLock() self._task_sessions_lock = threading.RLock() # Guards the opt-in send pass: at most one in flight per process. self._send_lock = threading.RLock() self._send_thread: threading.Thread | None = None - self._task_sessions: dict[tuple[str, str], _MetricsSession] = {} - self._turn_sessions: dict[tuple[str, str], _MetricsSession] = {} self._subscriber_name = f"{SUBSCRIBER_NAME}.{self.host.runtime_id}" self.subscriber = SharedMetricsSubscriber( SharedMetricsStore(), __version__, runtime_id=self.host.runtime_id @@ -958,17 +958,10 @@ def start_task_run( *, session_id: str, task_id: str, platform: str, parent_session_id: str = "" ) -> None: """Start task metrics at the outer Hermes execution boundary.""" - if not enabled(): - return - runtime = _get_runtime(retry_failed=True) - if runtime is not None: - runtime._safe( - runtime.start_task, - { - "session_id": session_id, "task_id": task_id, "platform": platform, - "parent_session_id": parent_session_id, - }, - ) + _run_task_hook( + "start_task", retry_failed=True, session_id=session_id, task_id=task_id, + platform=platform, parent_session_id=parent_session_id, + ) def finish_task_run( @@ -976,19 +969,18 @@ def finish_task_run( result: dict[str, Any] | None = None, error: BaseException | None = None, ) -> None: """Finish task metrics for every return or exception path.""" + _run_task_hook( + "finish_task", session_id=session_id, task_id=task_id, platform=platform, + **_terminal_flags(result, error), + ) + + +def _run_task_hook(method: str, *, retry_failed: bool = False, **event: Any) -> None: if not enabled(): return - runtime = _get_runtime() - if runtime is None: - return - - runtime._safe( - runtime.finish_task, - { - "session_id": session_id, "task_id": task_id, "platform": platform, - **_terminal_flags(result, error), - }, - ) + runtime = _get_runtime(retry_failed=retry_failed) + if runtime is not None: + runtime._safe(getattr(runtime, method), event) def _terminal_flags(result: dict[str, Any] | None, error: BaseException | None) -> dict[str, Any]: