The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(plugins): key the hook-timeout memo per config path
kshitijk4poorc6750513c03b
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.pyindex bb46b5b6dc0..bece644de2f 100644--- a/hermes_cli/plugins.py+++ b/hermes_cli/plugins.py@@ -1131,18 +1131,15 @@ for _name, _method in list(vars(PluginContext).items()):del _name, _method-# Sentinel distinct from every real signature AND from None (which-# _load_config_cache_sig returns when there is no config file to key on).-_UNRESOLVED_HOOK_TIMEOUT_SIG: Any = object()-# Published as ONE ``(sig, value)`` tuple swapped atomically (a module-global rebind is a single-# STORE under the GIL), so a lock-free reader can never observe a new sig paired with an old value.-_HOOK_TIMEOUT_CACHE: Tuple[Any, Optional[float]] = (_UNRESOLVED_HOOK_TIMEOUT_SIG, None)+# Keyed by the scope-resolved config path (like `_LOAD_CONFIG_CACHE`) so multiplexed profiles do not+# evict each other. Each value is ONE `(sig, value)` tuple stored with a single dict item assignment+# (atomic under the GIL), so a lock-free reader can never observe a new sig paired with an old value.+_HOOK_TIMEOUT_CACHE: Dict[str, Tuple[Any, Optional[float]]] = {}def _reset_hook_callback_timeout_cache() -> None:- """Drop the memoized hook-callback timeout. For tests and config reloads."""- global _HOOK_TIMEOUT_CACHE- _HOOK_TIMEOUT_CACHE = (_UNRESOLVED_HOOK_TIMEOUT_SIG, None)+ """Drop the memoized hook-callback timeouts. For tests and config reloads."""+ _HOOK_TIMEOUT_CACHE.clear()def _resolve_hook_callback_timeout() -> float:@@ -1157,18 +1154,19 @@ def _resolve_hook_callback_timeout() -> float:"""try:from hermes_cli.config import _load_config_cache_sig, get_config_path- _, sig = _load_config_cache_sig(get_config_path())+ config_path = get_config_path()+ path_key = str(config_path)+ _, sig = _load_config_cache_sig(config_path)except Exception:- sig = None+ path_key, sig = "", None- global _HOOK_TIMEOUT_CACHE- cached_sig, cached_value = _HOOK_TIMEOUT_CACHE # one read of the published tuple- if sig is not None and cached_sig == sig and isinstance(cached_value, float):- return cached_value+ cached = _HOOK_TIMEOUT_CACHE.get(path_key) # one read of the published tuple+ if cached is not None and sig is not None and cached[0] == sig and isinstance(cached[1], float):+ return cached[1]resolved = _resolve_hook_callback_timeout_uncached()if sig is not None:- _HOOK_TIMEOUT_CACHE = (sig, resolved)+ _HOOK_TIMEOUT_CACHE[path_key] = (sig, resolved)return resolveddiff --git a/tests/hermes_cli/test_config_lock_free_cache_hit.py b/tests/hermes_cli/test_config_lock_free_cache_hit.pyindex 682c0f7a148..94989824a54 100644--- a/tests/hermes_cli/test_config_lock_free_cache_hit.py+++ b/tests/hermes_cli/test_config_lock_free_cache_hit.py@@ -156,8 +156,8 @@ def test_hook_timeout_memo_never_pairs_a_new_sig_with_an_old_value(config_home):t = threading.Thread(target=writer, daemon=True); t.start()try:for _ in range(2000):- sig, value = pluginsmod._HOOK_TIMEOUT_CACHE- if sig in sig_of:- assert sig_of[sig] == value, "memo published a new sig with a stale value"+ for sig, value in list(pluginsmod._HOOK_TIMEOUT_CACHE.values()):+ if sig in sig_of:+ assert sig_of[sig] == value, "memo published a new sig with a stale value"finally:stop.set(); t.join(timeout=5)