The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(stream): resolve plugins.stream_reasoning_deltas once per stream, not per token
carlotestor74183e1793a4
diff --git a/agent/plugin_stream_hooks.py b/agent/plugin_stream_hooks.pyindex 78ecd15448b..87aec9d3018 100644--- a/agent/plugin_stream_hooks.py+++ b/agent/plugin_stream_hooks.py@@ -154,10 +154,13 @@ def has_reasoning_stream_observer_hooks() -> bool:def stream_reasoning_deltas_enabled() -> bool:- """Return True only when the user opted plugins into reasoning deltas."""+ """Return True only when the user opted plugins into reasoning deltas.++ Read-only scalar lookup: skips ``load_config()``'s deepcopy. Callers on the token path+ should still cache the result per stream (``_fire_reasoning_delta`` does)."""try:from hermes_cli import config as config_mod- config = config_mod.load_config()+ config = config_mod.load_config_readonly()return bool(config_mod.cfg_get(config, "plugins", "stream_reasoning_deltas", default=False))except Exception:logger.debug("failed to read plugins.stream_reasoning_deltas", exc_info=True)diff --git a/agent/stream_delivery.py b/agent/stream_delivery.pyindex 2f1cfb6ccbc..13a8af8ebae 100644--- a/agent/stream_delivery.py+++ b/agent/stream_delivery.py@@ -52,6 +52,8 @@ class StreamDeliveryMixin:"""think_scrubber = getattr(self, "_stream_think_scrubber", None)ctx_scrubber = getattr(self, "_stream_context_scrubber", None)+ # Next stream re-reads plugins.stream_reasoning_deltas (config edits land per request).+ self._stream_reasoning_hooks_enabled = Nonedef deliver(tail: str) -> None:if tail:@@ -323,13 +325,18 @@ class StreamDeliveryMixin:self._note_dropped_stream_writer("_fire_reasoning_delta")returnself._call_quietly(self.reasoning_callback, text)- try:- from agent.plugin_stream_hooks import stream_reasoning_deltas_enabled-- enabled = stream_reasoning_deltas_enabled()- except Exception:- logger.debug("reasoning on_stream_delta plugin hook enqueue failed", exc_info=True)- return+ # Resolve the opt-in once per stream, not per token: each lookup took _CONFIG_LOCK and+ # serialized every streaming thread in the process behind a config cache hit.+ enabled = getattr(self, "_stream_reasoning_hooks_enabled", None)+ if enabled is None:+ try:+ from agent.plugin_stream_hooks import stream_reasoning_deltas_enabled++ enabled = stream_reasoning_deltas_enabled()+ except Exception:+ logger.debug("reasoning on_stream_delta plugin hook enqueue failed", exc_info=True)+ return+ self._stream_reasoning_hooks_enabled = enabledif enabled:self._enqueue_stream_hook("on_stream_delta", label="reasoning on_stream_delta", delta=text, kind="reasoning")diff --git a/tests/agent/test_plugin_stream_hooks.py b/tests/agent/test_plugin_stream_hooks.pyindex f879f3cf431..81b45fd6a24 100644--- a/tests/agent/test_plugin_stream_hooks.py+++ b/tests/agent/test_plugin_stream_hooks.py@@ -181,6 +181,8 @@ def test_reasoning_stream_delta_plugin_hook_is_opt_in(monkeypatch):assert calls == []+ # The opt-in is resolved once per stream; a new request picks up the flipped flag.+ agent._reset_stream_delivery_tracking()with patch("hermes_cli.config.cfg_get", return_value=True):agent._fire_reasoning_delta("visible reasoning")_wait_for(lambda: calls)@@ -191,6 +193,29 @@ def test_reasoning_stream_delta_plugin_hook_is_opt_in(monkeypatch):assert calls[0][1]["delta"] == "visible reasoning"+def test_reasoning_stream_opt_in_is_read_once_per_stream(monkeypatch):+ from agent.plugin_stream_hooks import shutdown_plugin_stream_hook_dispatcher++ shutdown_plugin_stream_hook_dispatcher()+ monkeypatch.setattr("hermes_cli.plugins.iter_hook_callbacks", _callbacks({"on_stream_delta": [lambda **kw: None]}))+ reads = []++ def _enabled():+ reads.append(1)+ return False++ monkeypatch.setattr("agent.plugin_stream_hooks.stream_reasoning_deltas_enabled", _enabled)+ agent = _agent()+ for token in ("a", "b", "c"):+ agent._fire_reasoning_delta(token)+ assert len(reads) == 1++ agent._reset_stream_delivery_tracking()+ agent._fire_reasoning_delta("d")+ assert len(reads) == 2+ shutdown_plugin_stream_hook_dispatcher()++def test_interim_message_plugin_hook_is_queued(monkeypatch):from agent.plugin_stream_hooks import shutdown_plugin_stream_hook_dispatcher