Skip to content

The agent that grows with you

refactor(config): share the cache-hit predicate between fast and locked paths

kshitijk4poor30ef646eb35c
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index d676a07080b..a007ad63ee9 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -1961,6 +1961,16 @@ def cfg_get(cfg: Optional[Dict[str, Any]], *keys: str, default: Any = None) -> A
return node
+def _raw_config_cache_hit(path_key: str, cache_key: Tuple[Any, ...]) -> Optional[Dict[str, Any]]:
+ """Pure lookup: the cached raw config for ``path_key`` if its signature equals ``cache_key``,
+ else ``None``. Shared by the lock-free fast path and the locked re-check of
+ ``_read_raw_config_impl`` so the predicate cannot drift between them."""
+ cached = _RAW_CONFIG_CACHE.get(path_key)
+ if cached is not None and cached[:len(cache_key)] == cache_key:
+ return cached[len(cache_key)]
+ return None
+
+
def _read_raw_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
# Lock-free fast path for cache hits — same shape as `_load_config_impl`. `_RAW_CONFIG_CACHE`
# publishes each entry as ONE `(*sig, data)` tuple replaced wholesale, so a reader sees either
@@ -1970,9 +1980,9 @@ def _read_raw_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
try:
config_path = get_config_path()
cache_key = file_signature(config_path.stat())
- cached = _RAW_CONFIG_CACHE.get(str(config_path))
- if cached is not None and cached[:len(cache_key)] == cache_key:
- return copy.deepcopy(cached[len(cache_key)]) if want_deepcopy else cached[len(cache_key)]
+ hit = _raw_config_cache_hit(str(config_path), cache_key)
+ if hit is not None:
+ return copy.deepcopy(hit) if want_deepcopy else hit
except Exception:
pass
@@ -1985,9 +1995,9 @@ def _read_raw_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
return {}
path_key = str(config_path)
- cached = _RAW_CONFIG_CACHE.get(path_key)
- if cached is not None and cached[:len(cache_key)] == cache_key:
- return copy.deepcopy(cached[len(cache_key)]) if want_deepcopy else cached[len(cache_key)]
+ hit = _raw_config_cache_hit(path_key, cache_key)
+ if hit is not None:
+ return copy.deepcopy(hit) if want_deepcopy else hit
try:
with open(config_path, encoding="utf-8") as f:
@@ -2298,6 +2308,21 @@ def _merge_managed_overlay(expanded: Dict[str, Any]) -> Tuple[Dict[str, Any], An
return _deep_merge(expanded, _expand_env_vars(managed_normalized)), managed_config
+def _load_config_cache_hit(path_key: str, cache_sig: Any) -> Optional[Dict[str, Any]]:
+ """Pure lookup: the cached expanded config for ``path_key`` if its signature equals
+ ``cache_sig`` AND every ``${VAR}`` it was expanded against still has the same value, else
+ ``None``. Signatures matching is not enough: a load before load_hermes_dotenv() would otherwise
+ pin unexpanded literals (e.g. auxiliary.<task>.api_key) for the process lifetime (#58514).
+ Shared by the lock-free fast path and the locked re-check of ``_load_config_impl``."""
+ cached = _LOAD_CONFIG_CACHE.get(path_key)
+ if cached is None or cache_sig is None or cached[:8] != cache_sig:
+ return None
+ env_snapshot = cached[9] if len(cached) > 9 else {}
+ if all(_env_ref_lookup(k) == v for k, v in env_snapshot.items()):
+ return cached[8]
+ return None
+
+
def _load_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
# Lock-free fast path for cache hits — same publication contract as `_read_raw_config_impl`
# above (whole-tuple replace, `_CONFIG_LOCK` only serializes rebuilds and writers). A hit costs
@@ -2306,13 +2331,11 @@ def _load_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
try:
config_path = get_config_path()
path_key = str(config_path)
- cached = _LOAD_CONFIG_CACHE.get(path_key)
- if cached is not None:
+ if path_key in _LOAD_CONFIG_CACHE:
_, fast_sig = _load_config_cache_sig(config_path)
- if fast_sig is not None and cached[:8] == fast_sig:
- env_snapshot = cached[9] if len(cached) > 9 else {}
- if all(_env_ref_lookup(k) == v for k, v in env_snapshot.items()):
- return copy.deepcopy(cached[8]) if want_deepcopy else cached[8]
+ hit = _load_config_cache_hit(path_key, fast_sig)
+ if hit is not None:
+ return copy.deepcopy(hit) if want_deepcopy else hit
except Exception:
# Any surprise here falls through to the locked path, which is the
# original fully-defensive implementation.
@@ -2325,16 +2348,9 @@ def _load_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]:
user_sig, cache_sig = _load_config_cache_sig(config_path)
- cached = _LOAD_CONFIG_CACHE.get(path_key)
- if cached is not None and cache_sig is not None and cached[:8] == cache_sig:
- # Signatures match, but the cached expansion is only valid if every ${VAR} it was
- # expanded against still has the same value — otherwise a load before
- # load_hermes_dotenv() pins unexpanded literals for the process lifetime.
- # Without this, a load_config() that ran before load_hermes_dotenv() pins unexpanded literals
- # (e.g. auxiliary.<task>.api_key) for the life of the process (#58514).
- env_snapshot = cached[9] if len(cached) > 9 else {}
- if all(_env_ref_lookup(k) == v for k, v in env_snapshot.items()):
- return copy.deepcopy(cached[8]) if want_deepcopy else cached[8]
+ hit = _load_config_cache_hit(path_key, cache_sig)
+ if hit is not None:
+ return copy.deepcopy(hit) if want_deepcopy else hit
config = copy.deepcopy(DEFAULT_CONFIG)