The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(observability): forward Hermes session id on approval hooks
Victor Kyriazakos15959d8259e6
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.pyindex ba4cfd8f516..ded7ed4260c 100644--- a/hermes_cli/plugins.py+++ b/hermes_cli/plugins.py@@ -5511,6 +5511,7 @@ def resolve_pre_tool_block(approval_tokens = set_current_observability_context(turn_id=turn_id,tool_call_id=tool_call_id,+ session_id=session_id,)except Exception:passdiff --git a/model_tools.py b/model_tools.pyindex c5c1256c907..8fe4ffd34c1 100644--- a/model_tools.py+++ b/model_tools.py@@ -1461,6 +1461,7 @@ def handle_function_call(_approval_tokens = set_current_observability_context(turn_id=turn_id or "",tool_call_id=tool_call_id or "",+ session_id=session_id or "",)except Exception:reset_current_observability_context = Nonediff --git a/tests/tools/test_approval_hook_session_id.py b/tests/tools/test_approval_hook_session_id.pynew file mode 100644index 00000000000..b079c4d5be9--- /dev/null+++ b/tests/tools/test_approval_hook_session_id.py@@ -0,0 +1,92 @@+"""Approval hooks must carry the Hermes session id to observer plugins.++Staging defect 2026-08-10: approval marks were emitted under a synthetic+"default" relay session because the approval hook payload carried only+turn_id/tool_call_id — the observability plugin's ``_session_id()`` fell+back to "default", parented the marks to a session scope that never+closes, and close-time exporters never shipped them. The audit board's+approval tables stayed empty while approvals were demonstrably firing.++Contract: when the dispatch layer binds an observability context with a+session id, every approval hook payload carries that session id; when no+context is bound, the payload omits it (legacy behavior preserved).+"""++from __future__ import annotations++from unittest.mock import patch++from tools import approval as approval_mod+++def _capture_hook(captured):+ def _invoke(hook_name, **kwargs):+ captured.append((hook_name, kwargs))+ return _invoke+++class TestApprovalHookSessionId:+ def test_session_id_forwarded_when_bound(self):+ captured = []+ tokens = approval_mod.set_current_observability_context(+ turn_id="turn-1",+ tool_call_id="call-1",+ session_id="20260810_test_session",+ )+ try:+ with patch(+ "hermes_cli.lifecycle.invoke_hook",+ side_effect=_capture_hook(captured),+ ):+ approval_mod._fire_approval_hook(+ "pre_approval_request",+ command="rm -rf /etc/hosts",+ description="dangerous",+ surface="gateway",+ )+ finally:+ approval_mod.reset_current_observability_context(tokens)++ assert captured, "hook must dispatch"+ _, kwargs = captured[0]+ assert kwargs.get("session_id") == "20260810_test_session"+ assert kwargs.get("turn_id") == "turn-1"+ assert kwargs.get("tool_call_id") == "call-1"++ def test_explicit_session_id_not_clobbered(self):+ captured = []+ tokens = approval_mod.set_current_observability_context(+ session_id="context-session",+ )+ try:+ with patch(+ "hermes_cli.lifecycle.invoke_hook",+ side_effect=_capture_hook(captured),+ ):+ approval_mod._fire_approval_hook(+ "post_approval_response",+ session_id="explicit-session",+ choice="approved",+ )+ finally:+ approval_mod.reset_current_observability_context(tokens)++ _, kwargs = captured[0]+ assert kwargs.get("session_id") == "explicit-session"++ def test_absent_when_unbound(self):+ captured = []+ with patch(+ "hermes_cli.lifecycle.invoke_hook",+ side_effect=_capture_hook(captured),+ ):+ approval_mod._fire_approval_hook(+ "pre_approval_request",+ command="x",+ description="y",+ )+ _, kwargs = captured[0]+ assert "session_id" not in kwargs, (+ "no synthetic session id when none is bound — the observer's "+ "own fallback owns that decision"+ )diff --git a/tools/approval.py b/tools/approval.pyindex 0509907a1bd..194bbd636fa 100644--- a/tools/approval.py+++ b/tools/approval.py@@ -51,6 +51,16 @@ _approval_tool_call_id: contextvars.ContextVar[str] = contextvars.ContextVar("approval_tool_call_id",default="",)+# Hermes session id (observability identity, distinct from the gateway+# routing session_key above). Approval hooks forward it so observer+# plugins can attach approval marks to the REAL session scope — without+# it they fall back to a synthetic "default" session whose scope never+# closes, and close-time exporters never ship the marks (staging defect+# 2026-08-10: approvals invisible on the audit board).+_approval_session_id: contextvars.ContextVar[str] = contextvars.ContextVar(+ "approval_session_id",+ default="",+)# Interactive-CLI flag. Concurrent ACP sessions run on a shared# ThreadPoolExecutor (acp_adapter/server.py), so mutating the process-global@@ -113,6 +123,12 @@ def _fire_approval_hook(hook_name: str, **kwargs) -> None:try:kwargs.setdefault("turn_id", _approval_turn_id.get())kwargs.setdefault("tool_call_id", _approval_tool_call_id.get())+ # Forward the Hermes session id so observer plugins parent approval+ # marks to the real session scope instead of a synthetic "default"+ # session (whose scope never closes → marks never export).+ _session_id = _approval_session_id.get()+ if _session_id:+ kwargs.setdefault("session_id", _session_id)invoke_hook(hook_name, **kwargs)except Exception as exc:# invoke_hook() already swallows per-callback errors, so reaching here@@ -183,19 +199,26 @@ def set_current_observability_context(*,turn_id: str = "",tool_call_id: str = "",-) -> tuple[contextvars.Token[str], contextvars.Token[str]]:+ session_id: str = "",+) -> tuple[+ contextvars.Token[str], contextvars.Token[str], contextvars.Token[str]+]:"""Bind active tool correlation IDs to approval hooks."""return (_approval_turn_id.set(turn_id or ""),_approval_tool_call_id.set(tool_call_id or ""),+ _approval_session_id.set(session_id or ""),)def reset_current_observability_context(- tokens: tuple[contextvars.Token[str], contextvars.Token[str]],+ tokens: tuple[+ contextvars.Token[str], contextvars.Token[str], contextvars.Token[str]+ ],) -> None:"""Restore prior approval hook correlation IDs."""- turn_token, tool_token = tokens+ turn_token, tool_token, session_token = tokens+ _approval_session_id.reset(session_token)_approval_tool_call_id.reset(tool_token)_approval_turn_id.reset(turn_token)