Skip to content

The agent that grows with you

fix(shared-metrics): an abandoned connector batch counts its started entry once

Siddharth Balyanbb9458214e26
diff --git a/hermes_cli/observability/relay_shared_metrics.py b/hermes_cli/observability/relay_shared_metrics.py
index 43b9137c9d6..ff5dc742017 100644
--- a/hermes_cli/observability/relay_shared_metrics.py
+++ b/hermes_cli/observability/relay_shared_metrics.py
@@ -518,9 +518,7 @@ class _Runtime:
tool_call = None
if _text(event, "tool_call_id"):
observed_identity = self._tool_call_identity(event)
- if connectors_.is_connector_batch(event) and connectors_.batch_entry_ran(
- task.completed_tool_call_ids, observed_identity):
- task.completed_tool_call_ids.add(observed_identity)
+ if self._settle_connector_batch(session, task, observed_identity, event):
return
if observed_identity in task.completed_tool_call_ids:
return
@@ -553,6 +551,24 @@ class _Runtime:
if chain is not None:
self._mark(session, task, contract.CONNECTOR_CHAIN_MARK, chain)
+ def _settle_connector_batch(
+ self, session: _MetricsSession, task: _TaskRun, wrapper: tuple[str, str, str], event: dict[str, Any],
+ ) -> bool:
+ """A connector batch wrapper's post is not a call of its own once an entry started: entries
+ still open (the executor timed out or abandoned the batch) end with the wrapper's outcome.
+ True when the wrapper was settled this way; a batch refused before any entry counts as itself."""
+ if not connectors_.is_connector_batch(event):
+ return False
+ started = [key for key in session.tool_calls
+ if key[0] == task.task_id and connectors_.is_batch_entry(key[1:], wrapper)]
+ if not started and not connectors_.batch_entry_ran(task.completed_tool_call_ids, wrapper):
+ return False
+ for key in started:
+ task.completed_tool_call_ids.add(key[1:])
+ self._finish_tool_call(task, session.tool_calls.pop(key), event)
+ task.completed_tool_call_ids.add(wrapper)
+ return True
+
def record_skill_lifecycle(self, event: dict[str, Any]) -> None:
"""Emit one allowlisted skill fact without its local identity."""
if _text(event, "action").strip().lower() == "loaded":
diff --git a/hermes_cli/observability/shared_metrics_connectors.py b/hermes_cli/observability/shared_metrics_connectors.py
index 3f966abdbe7..04b1d788967 100644
--- a/hermes_cli/observability/shared_metrics_connectors.py
+++ b/hermes_cli/observability/shared_metrics_connectors.py
@@ -41,11 +41,14 @@ def is_connector_batch(event: dict[str, Any]) -> bool:
return bool(entries) and all(is_connector_name(entry.get("name")) for entry in entries)
+def is_batch_entry(identity: tuple[str, str, str], wrapper: tuple[str, str, str]) -> bool:
+ """Whether ``identity`` is an entry (``<id>#<n>``) of the batch whose wrapper has identity ``wrapper``."""
+ return bool(wrapper[2]) and identity[2].startswith(f"{wrapper[2]}#") and identity[:2] == wrapper[:2]
+
+
def batch_entry_ran(identities: Iterable[tuple[str, str, str]], wrapper: tuple[str, str, str]) -> bool:
"""Whether an entry of the batch whose wrapper has identity ``wrapper`` was already counted."""
- prefix = f"{wrapper[2]}#"
- return bool(wrapper[2]) and any(
- identity[2].startswith(prefix) and identity[:2] == wrapper[:2] for identity in identities)
+ return any(is_batch_entry(identity, wrapper) for identity in identities)
def _described_names(args: Any) -> list[str]:
diff --git a/tests/hermes_cli/test_shared_metrics_connectors.py b/tests/hermes_cli/test_shared_metrics_connectors.py
index 18e996fc3cc..319e5d6eb70 100644
--- a/tests/hermes_cli/test_shared_metrics_connectors.py
+++ b/tests/hermes_cli/test_shared_metrics_connectors.py
@@ -133,6 +133,24 @@ def test_a_batch_rejected_before_any_entry_still_counts_as_tool_call(fake_relay)
assert (end["tool_name"], end["outcome"]) == ("tool_call", "failed")
+def test_a_batch_the_executor_abandons_mid_entry_counts_that_entry_once(fake_relay):
+ """The executor times out during entry 0: the entry never posts, the wrapper posts the timeout.
+ The open entry takes the wrapper's outcome; the wrapper is not a second call."""
+ lifecycle.invoke_hook("pre_llm_call", **_BASE)
+ entry = {**_BASE, "tool_call_id": "tc1#0", "tool_name": "connectors__gmail__SEND_EMAIL", "args": {}}
+ lifecycle.invoke_hook("pre_tool_call", **entry)
+ lifecycle.invoke_hook(
+ "post_tool_call", **_BASE, tool_call_id="tc1", tool_name="tool_call", status="timeout",
+ error_type="tool_timeout", result="timed out",
+ args={"calls": [{"name": "connectors__gmail__SEND_EMAIL", "arguments": {}}]})
+ _end_turn()
+ [end] = [event[2] for event in fake_relay.events if event[0] == "tool.call_end"]
+ assert (end["tool_name"], end["tool_category"], end["outcome"]) == ("connector", "connector", "timed_out")
+ [task_end] = [event[2]["output"] for event in fake_relay.events
+ if event[0] == "scope.pop" and event[1][1] == "hermes.task_run"]
+ assert task_end["tool_call_count_bucket"] == "1"
+
+
def test_chain_row_counts_searches_since_the_last_execute_and_whether_the_tool_was_described(
fake_relay,
):