The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(skills): lock the raw-config cache's publish, evict and clear
Teknium180ab5e20c8a
diff --git a/agent/skill_utils.py b/agent/skill_utils.pyindex 34147aefb32..e885a5304bc 100644--- a/agent/skill_utils.py+++ b/agent/skill_utils.py@@ -7,6 +7,7 @@ import loggingimport osimport reimport sys+import threadingfrom pathlib import Path, PurePathfrom typing import Any, Callable, Dict, List, Optional, Set, Tuple@@ -196,11 +197,15 @@ _RAW_CONFIG_CACHE: dict[tuple[str, int, int, int, int], dict[str, Any]] = {}# the ambient one (#141610), so a miss must not drop the whole cache — only a stale# signature entry. The bound retires entries a config's own edits leave behind._RAW_CONFIG_CACHE_MAX = 128+# Guards every cache read/publish/evict: concurrent tool threads share it, and an unlocked+# ``pop(next(iter(...)))`` races another thread's insert (RuntimeError) or pop (KeyError).+_RAW_CONFIG_CACHE_LOCK = threading.Lock()def _raw_config_cache_clear() -> None:"""Test hook — drop the shared raw config cache."""- _RAW_CONFIG_CACHE.clear()+ with _RAW_CONFIG_CACHE_LOCK:+ _RAW_CONFIG_CACHE.clear()def _config_cache_key(config_path: Path) -> Optional[tuple[str, int, int, int, int]]:@@ -223,7 +228,8 @@ def _load_raw_config() -> dict[str, Any]:if not config_path.exists():return {}cache_key = _config_cache_key(config_path)- cached = _RAW_CONFIG_CACHE.get(cache_key) if cache_key is not None else None+ with _RAW_CONFIG_CACHE_LOCK:+ cached = _RAW_CONFIG_CACHE.get(cache_key) if cache_key is not None else Noneif cached is not None:return cachedtry:@@ -234,9 +240,10 @@ def _load_raw_config() -> dict[str, Any]:if not isinstance(parsed, dict):return {}if cache_key is not None:- _RAW_CONFIG_CACHE[cache_key] = parsed- while len(_RAW_CONFIG_CACHE) > _RAW_CONFIG_CACHE_MAX:- _RAW_CONFIG_CACHE.pop(next(iter(_RAW_CONFIG_CACHE)))+ with _RAW_CONFIG_CACHE_LOCK:+ _RAW_CONFIG_CACHE[cache_key] = parsed+ while len(_RAW_CONFIG_CACHE) > _RAW_CONFIG_CACHE_MAX:+ _RAW_CONFIG_CACHE.pop(next(iter(_RAW_CONFIG_CACHE)))return parseddiff --git a/tests/agent/test_skill_utils.py b/tests/agent/test_skill_utils.pyindex c670a009558..e8809503d09 100644--- a/tests/agent/test_skill_utils.py+++ b/tests/agent/test_skill_utils.py@@ -182,6 +182,38 @@ class TestMultiprofileRawConfigCache:assert self._load_under(first) == ["first-disabled"]assert self._load_under(second) == ["second-disabled"]+ def test_concurrent_loads_across_evicting_homes_never_raise(+ self, tmp_path, monkeypatch+ ):+ """Tool threads share the cache: two threads evicting the same oldest key must not+ raise KeyError, nor an insert during ``next(iter(...))`` a RuntimeError. A 1 µs+ switch interval makes the unlocked interleaving reliable instead of rare."""+ import sys+ from concurrent.futures import ThreadPoolExecutor++ from agent import skill_utils++ homes = [tmp_path / f"scope-{i:02d}" for i in range(24)]+ for i, home in enumerate(homes):+ _write_disabled_config(home, f"skill-{i:02d}")+ monkeypatch.setattr(skill_utils, "_RAW_CONFIG_CACHE_MAX", 2)+ skill_utils._raw_config_cache_clear()++ def hammer(offset: int) -> None:+ for n in range(150):+ i = (offset + n) % len(homes)+ assert self._load_under(homes[i]) == [f"skill-{i:02d}"]++ old_interval = sys.getswitchinterval()+ sys.setswitchinterval(1e-6)+ try:+ with ThreadPoolExecutor(max_workers=16) as pool:+ futures = [pool.submit(hammer, t) for t in range(16)]+ errors = [f.exception(timeout=60) for f in futures]+ finally:+ sys.setswitchinterval(old_interval)+ assert errors == [None] * 16+class TestParseConfigStringList:"""#86661: `hermes config set` and JSON-mode editor saves store lists as