The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(profiles): serialize cold skill-count scans
Gianpietro Dal Ziofae101eddcfe
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.pyindex 7101ddb732b..1c17c837c5c 100644--- a/hermes_cli/profiles.py+++ b/hermes_cli/profiles.py@@ -724,6 +724,9 @@ _SKILL_COUNT_TTL_SECONDS = 600.0_SKILL_COUNT_RECHECK_SECONDS = 60.0_SKILL_COUNT_NEXT_CHECK: dict[str, float] = {}_SKILL_COUNT_LOCK = threading.Lock()+# One scan lock per skills dir so concurrent cold scans of the SAME profile share one walk+# while different profiles' scans still run in parallel.+_SKILL_COUNT_SCAN_LOCKS: dict[str, threading.Lock] = {}def _skills_dir_signature(skills_dir: Path) -> float:@@ -764,13 +767,21 @@ def _count_skills(profile_dir: Path) -> int:return 0key = str(skills_dir)signature = _skills_dir_signature(skills_dir)- now = time.time()cached = _SKILL_COUNT_CACHE.get(key)- if cached is not None and cached[0] == signature and (now - cached[1]) < _SKILL_COUNT_TTL_SECONDS:+ if cached is not None and cached[0] == signature and (time.time() - cached[1]) < _SKILL_COUNT_TTL_SECONDS:return cached[2]- count = _walk_skill_count(skills_dir)- _SKILL_COUNT_CACHE[key] = (signature, now, count)- return count+ with _SKILL_COUNT_LOCK:+ scan_lock = _SKILL_COUNT_SCAN_LOCKS.setdefault(key, threading.Lock())+ with scan_lock:+ # Re-check under the lock: a concurrent caller may have just finished this walk.+ signature = _skills_dir_signature(skills_dir)+ cached = _SKILL_COUNT_CACHE.get(key)+ if cached is not None and cached[0] == signature and (time.time() - cached[1]) < _SKILL_COUNT_TTL_SECONDS:+ return cached[2]+ count = _walk_skill_count(skills_dir)+ # Stamp AFTER the walk: a scan longer than the TTL must not publish an already-expired entry.+ _SKILL_COUNT_CACHE[key] = (signature, time.time(), count)+ return countdef _cached_skill_count(profile_dir: Path) -> int:diff --git a/tests/hermes_cli/test_profiles.py b/tests/hermes_cli/test_profiles.pyindex 93ab79825e6..33588de1f47 100644--- a/tests/hermes_cli/test_profiles.py+++ b/tests/hermes_cli/test_profiles.py@@ -1906,3 +1906,29 @@ class TestCloneAllExcludesRuntimeTrees:assert not (clone / name).exists(), nameassert (clone / "skills" / "greet" / "SKILL.md").is_file()assert (clone / "config.yaml").is_file()+++def test_count_skills_publishes_timestamp_after_the_walk(tmp_path, monkeypatch):+ """A scan longer than the TTL must not publish an already-expired cache entry (#107151):+ the cached timestamp is taken after _walk_skill_count returns, not before it starts."""+ from hermes_cli import profiles as mod++ skills_dir = tmp_path / "skills"+ skills_dir.mkdir()+ monkeypatch.setattr(mod, "_SKILL_COUNT_CACHE", {})+ monkeypatch.setattr(mod, "_SKILL_COUNT_SCAN_LOCKS", {}, raising=False)+ clock = {"now": 1000.0}+ monkeypatch.setattr(mod.time, "time", lambda: clock["now"])++ def slow_walk(_dir):+ clock["now"] += mod._SKILL_COUNT_TTL_SECONDS + 5 # walk outlives the TTL+ return 3++ monkeypatch.setattr(mod, "_walk_skill_count", slow_walk)+ assert mod._count_skills(tmp_path) == 3+ _sig, stamped, count = mod._SKILL_COUNT_CACHE[str(skills_dir)]+ assert count == 3+ assert stamped >= clock["now"], "published timestamp must be >= scan end"+ # Entry is fresh: a second call within the TTL must not walk again.+ monkeypatch.setattr(mod, "_walk_skill_count", lambda _d: pytest.fail("re-walked a fresh entry"))+ assert mod._count_skills(tmp_path) == 3