fix(skills): keep every profile's raw config cache entry across a miss

_load_raw_config() cleared the whole keyed cache on every miss, leaving
one live entry at a time. A multiplexed gateway's skill watcher rotates
through up to ~32 published profile/project scopes plus the ambient one
per pass, so each pass re-read and re-parsed every other profile's
unchanged config.yaml (#141610). A miss now inserts only its own key,
with a 128-entry bound retiring the stale signatures a config's own
edits leave behind.
This commit is contained in:
liuhao1024
2026-10-19 23:08:27 +08:00
committed by Teknium
parent ac33a01627
commit 932b20df09
2 changed files with 120 additions and 1 deletions

View File

@@ -192,6 +192,10 @@ def skill_matches_apps(frontmatter: dict[str, Any]) -> bool:
_RAW_CONFIG_CACHE: dict[tuple[str, int, int, int, int], dict[str, Any]] = {}
# A multiplexed watcher rotates through up to ~32 published profile/project scopes plus
# 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
def _raw_config_cache_clear() -> None:
@@ -230,8 +234,9 @@ def _load_raw_config() -> dict[str, Any]:
if not isinstance(parsed, dict):
return {}
if cache_key is not None:
_RAW_CONFIG_CACHE.clear()
_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 parsed

View File

@@ -69,6 +69,120 @@ skills:
assert parse_count == 1
def _write_disabled_config(home, disabled: str) -> None:
home.mkdir(parents=True, exist_ok=True)
(home / "config.yaml").write_text(
f"skills:\n disabled:\n - {disabled}\n", encoding="utf-8"
)
class TestMultiprofileRawConfigCache:
"""#141610: a multiplexed watcher rotates through up to ~32 published profile
scopes plus the ambient one; one profile's cache miss must not evict every
other profile's unchanged entry."""
@staticmethod
def _load_under(home) -> list:
from agent import skill_utils
from hermes_constants import (
reset_hermes_home_override,
set_hermes_home_override,
)
token = set_hermes_home_override(str(home))
try:
return skill_utils._load_raw_config()["skills"]["disabled"]
finally:
reset_hermes_home_override(token)
def test_watcher_rotation_reparses_no_unchanged_config(self, tmp_path, monkeypatch):
from agent import skill_utils
homes = [tmp_path / f"scope-{i:02d}" for i in range(33)]
for i, home in enumerate(homes):
_write_disabled_config(home, f"skill-{i:02d}")
parse_count = 0
real_yaml_load = skill_utils.yaml_load
def counting_yaml_load(text):
nonlocal parse_count
parse_count += 1
return real_yaml_load(text)
monkeypatch.setattr(skill_utils, "yaml_load", counting_yaml_load)
skill_utils._raw_config_cache_clear()
for _ in range(3):
for i, home in enumerate(homes):
assert self._load_under(home) == [f"skill-{i:02d}"]
assert parse_count == 33
def test_editing_one_profile_reparses_only_that_profile(
self, tmp_path, monkeypatch
):
from agent import skill_utils
first, second = tmp_path / "first", tmp_path / "second"
_write_disabled_config(first, "old-disabled")
_write_disabled_config(second, "second-disabled")
parse_count = 0
real_yaml_load = skill_utils.yaml_load
def counting_yaml_load(text):
nonlocal parse_count
parse_count += 1
return real_yaml_load(text)
monkeypatch.setattr(skill_utils, "yaml_load", counting_yaml_load)
skill_utils._raw_config_cache_clear()
assert self._load_under(first) == ["old-disabled"]
assert self._load_under(second) == ["second-disabled"]
# A different length changes st_size, so the signature must change even on
# filesystems whose mtime granularity misses a quick rewrite.
(first / "config.yaml").write_text(
"skills:\n disabled:\n - new-disabled-name\n", encoding="utf-8"
)
assert self._load_under(first) == ["new-disabled-name"]
assert self._load_under(second) == ["second-disabled"]
assert parse_count == 3
def test_one_profile_parse_does_not_block_another_profile(
self, tmp_path, monkeypatch
):
from concurrent.futures import ThreadPoolExecutor
from threading import Event
from agent import skill_utils
first, second = tmp_path / "first", tmp_path / "second"
_write_disabled_config(first, "first-disabled")
_write_disabled_config(second, "second-disabled")
entered, release = Event(), Event()
real_yaml_load = skill_utils.yaml_load
def delayed_yaml_load(text):
if "first-disabled" in text:
entered.set()
assert release.wait(5), "first profile was not released"
return real_yaml_load(text)
monkeypatch.setattr(skill_utils, "yaml_load", delayed_yaml_load)
skill_utils._raw_config_cache_clear()
with ThreadPoolExecutor(max_workers=2) as pool:
first_result = pool.submit(self._load_under, first)
try:
assert entered.wait(5), "first profile parse did not start"
second_result = pool.submit(self._load_under, second)
assert second_result.result(timeout=3) == ["second-disabled"]
finally:
release.set()
assert first_result.result(timeout=5) == ["first-disabled"]
assert self._load_under(first) == ["first-disabled"]
assert self._load_under(second) == ["second-disabled"]
class TestParseConfigStringList:
"""#86661: `hermes config set` and JSON-mode editor saves store lists as
quoted strings (e.g. '["a","b"]'). Treating such a string as a single name