The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(relay): shared-metrics task close skips a scope buried under a concurrent turn
teknium1148d1f6da54e
diff --git a/agent/relay_runtime.py b/agent/relay_runtime.pyindex a3732337eaa..6daa2cc01a6 100644--- a/agent/relay_runtime.py+++ b/agent/relay_runtime.py@@ -110,6 +110,22 @@ def pop_relay_scope(relay: Any, handle: Any, *, output: Any = None, metadata: Anreturn pop(handle, **kwargs)+def pop_relay_scope_if_top(relay: Any, handle: Any, *, output: Any = None, metadata: Any = None) -> bool:+ """Pop ``handle`` only while it is the top of the scope stack; return whether it was popped.++ Two concurrent Hermes turns in one session share a physical stack, so the first turn to+ finish may find the sibling's live scope above its own. Popping through it would close the+ sibling's scope and letting the binding raise ("scope handle is not at the top of the+ stack") logs a traceback per overlap (#115471). The skipped scope is reclaimed by the+ session-close drain in ``_close_scope_handle``.+ """+ top = _current_top(relay)+ if top is not None and not _same_handle(top, handle):+ return False+ pop_relay_scope(relay, handle, output=output, metadata=metadata)+ return True++def _current_top(relay: Any) -> Any:"""Return the current top-of-stack scope handle, or None."""# Prefer scope.get_handle(): get_scope_stack() may return a native ScopeStack that scope.pop rejects.diff --git a/hermes_cli/observability/relay_shared_metrics.py b/hermes_cli/observability/relay_shared_metrics.pyindex 61cfe685083..93db37cce78 100644--- a/hermes_cli/observability/relay_shared_metrics.py+++ b/hermes_cli/observability/relay_shared_metrics.py@@ -738,11 +738,13 @@ class _Runtime:retry_count=task.retry_count,)try:- self._guarded(+ popped = self._guarded("Hermes shared-metrics task close failed",- self._run_in_task, task, relay_runtime.pop_relay_scope, self.relay, task.handle,+ self._run_in_task, task, relay_runtime.pop_relay_scope_if_top, self.relay, task.handle,output=fields, metadata=self._event_metadata(),)+ if popped is False:+ logger.debug("Left shared-metrics task scope %s under a concurrent turn's scope; session close drains it", task_id)finally:session.tasks.pop(task_id, None)session.retired_turn_ids.extend(task.turn_ids)diff --git a/tests/hermes_cli/test_relay_shared_metrics_runtime.py b/tests/hermes_cli/test_relay_shared_metrics_runtime.pyindex 13fbdb9e638..1af1455bf34 100644--- a/tests/hermes_cli/test_relay_shared_metrics_runtime.py+++ b/tests/hermes_cli/test_relay_shared_metrics_runtime.py@@ -2638,8 +2638,9 @@ def test_real_binding_concurrent_task_close_skips_pop_under_sibling_scope()assert not [r for r in caplog.records if "task close failed" in r.getMessage()], caplog.text- # Task B's live scope is still the top: the sibling was not popped through.- assert relay_runtime._same_handle(relay_runtime._current_top(runtime.relay), task_b_handle)+ # Task B's live scope is still the top of its context's stack: the sibling was not popped through.+ top = runtime._run_in_task(session.tasks["task-B"], relay_runtime._current_top, runtime.relay)+ assert relay_runtime._same_handle(top, task_b_handle)relay_shared_metrics.finish_task_run(session_id="shared-session", task_id="task-B", platform="cli", result={"ok": True},