The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(telemetry): platform.delivery lands in the profile that owns the chat (M6)
teknium1b67d9ed6fec5
diff --git a/hermes_cli/observability/shared_metrics_gateway.py b/hermes_cli/observability/shared_metrics_gateway.pyindex 275fef734b9..3bb3d009d4d 100644--- a/hermes_cli/observability/shared_metrics_gateway.py+++ b/hermes_cli/observability/shared_metrics_gateway.py@@ -230,13 +230,16 @@ def records_delivery(send: Callable[..., Any]) -> Callable[..., Any]:@functools.wraps(send)async def wrapper(self, *args: Any, **kwargs: Any) -> Any:chat_id = kwargs.get("chat_id", args[0] if args else None)+ # The final send runs after the routed profile scope is reset: the row goes to the profile+ # whose turn last started in this chat, not the launch profile.+ home = _chat_home(self, chat_id)try:result = await send(self, *args, **kwargs)except Exception as exc:- _submit(_record_delivery, self, exc=exc, chat_id=chat_id)+ _submit(_in_home, home, _record_delivery, self, exc=exc, chat_id=chat_id)raiseif isinstance(getattr(result, "success", None), bool):- _submit(_record_delivery, self, result, chat_id=chat_id)+ _submit(_in_home, home, _record_delivery, self, result, chat_id=chat_id)return resultreturn wrapper@@ -269,6 +272,9 @@ _REPLY_CLOCK_MAX_AGE = 3600.0# (platform value, chat id) -> (monotonic start, owning Hermes home). Keyed without the profile: the# send side may run outside the turn's scope, so the start side records whose row it is._reply_clocks: OrderedDict[tuple[str, str], tuple[float, str]] = OrderedDict()+# (platform value, chat id) -> the Hermes home whose turn last started there; outlives the clock so+# later sends (busy acks, follow-ups) in the chat are attributed too.+_chat_homes: OrderedDict[tuple[str, str], str] = OrderedDict()_reply_lock = threading.Lock()# One connector adapter fronting several platforms: the turn started under the platform the message# came in on (``discord``), its reply leaves through this adapter.@@ -300,11 +306,28 @@ def start_reply_clock(source: Any, *, internal: bool = False) -> None:returnfrom hermes_constants import get_hermes_home+ home = str(get_hermes_home())with _reply_lock:- _reply_clocks.pop(key, None)- _reply_clocks[key] = (time.monotonic(), str(get_hermes_home()))- while len(_reply_clocks) > _REPLY_CLOCK_MAX:- _reply_clocks.popitem(last=False)+ for table, value in ((_reply_clocks, (time.monotonic(), home)), (_chat_homes, home)):+ table.pop(key, None)+ table[key] = value+ while len(table) > _REPLY_CLOCK_MAX:+ table.popitem(last=False)+++def _chat_home(adapter: Any, chat_id: Any) -> str | None:+ """The home owning ``chat_id``'s latest turn (a fronting adapter matches the one fronted chat),+ else None (the caller's scope)."""+ key = _clock_key(getattr(adapter, "platform", None), chat_id)+ if key is None:+ return None+ with _reply_lock:+ if key in _chat_homes:+ return _chat_homes[key]+ if key[0] in _FRONTING_PLATFORMS:+ fronted = [home for k, home in _chat_homes.items() if k[1] == key[1]]+ return fronted[0] if len(fronted) == 1 else None+ return Nonedef stop_reply_clock(adapter: Any, chat_id: Any, result: Any = None) -> None:diff --git a/tests/gateway/test_platform_shared_metrics.py b/tests/gateway/test_platform_shared_metrics.pyindex a83f031c552..4a53e8368f8 100644--- a/tests/gateway/test_platform_shared_metrics.py+++ b/tests/gateway/test_platform_shared_metrics.py@@ -24,6 +24,8 @@ def rows(monkeypatch):lambda mark, data: got.append((mark, dict(data), str(get_hermes_home()))))monkeypatch.setattr("gateway.platforms.base.random.uniform", lambda *_: 0.0)smg._reply_clocks.clear()+ smg._chat_homes.clear()+def read(metric, *, with_home=False):smg.drain()@@ -125,6 +127,24 @@ def test_a_relay_delivered_turn_stops_the_clock_its_inbound_started(rows):assert not smg._reply_clocks+def test_a_delivery_sent_after_the_routed_scope_resets_lands_in_the_owning_profile(rows, tmp_path):+ # Multiplexed gateway: the turn starts in profile B's scope, the final send runs after it is reset.+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override++ home_b = tmp_path / "profiles" / "b"+ token = set_hermes_home_override(str(home_b))+ try:+ smg.start_reply_clock(SimpleNamespace(platform=Platform.SLACK, chat_id="C9"))+ finally:+ reset_hermes_home_override(token)+ asyncio.run(_Adapter(platform=Platform.SLACK)._send_with_retry("C9", "final answer", base_delay=0))+ asyncio.run(_Adapter()._send_with_retry("unseen", "hi", base_delay=0)) # no turn: the caller's scope+ assert rows("hermes.platform.delivery", with_home=True) == [+ (str(home_b), {"failure_class": "none", "outcome": "sent", "platform": "slack"}),+ (str(get_hermes_home()), {"failure_class": "none", "outcome": "sent", "platform": "telegram"}),+ ]++class _FatalRunner(GatewayAdapterLifecycleMixin):def __init__(self):self.adapters, self._failed_platforms, self._running = {}, {}, True