From 35af06c009457f9991ce32d634dbdcdab5d23982 Mon Sep 17 00:00:00 2001 From: moken627-hub Date: Fri, 4 Sep 2026 11:42:23 +0700 Subject: [PATCH] fix(desktop): coalesce projects/tree sidebar scans and memoize raw config parses The all-profiles sidebar polls GET /api/profiles/projects/tree, the one heavy sidebar endpoint that was not wrapped in @_sidebar_singleflight_cache. Every poll fanned out list_profiles() + _build_project_tree() over every profile (51 on this box, ~17k SKILL.md files walked), and each profile resolution re-parsed its config.yaml because read_user_config_raw() ran uncached. On a 2-vCore VPS running 'hermes serve' for the Desktop remote backend this pinned both cores (py-spy: 110-116% sustained, HostHighCPU). - Wrap get_profiles_projects_tree in the existing single-flight cache, matching get_profiles_sessions_sidebar (5s TTL, errors[] not cached). - Memoize read_user_config_raw() on a (st_dev, st_ino, st_size, st_mtime_ns) fingerprint under _CONFIG_LOCK, same strategy as read_raw_config(). Hits return a deepcopy so write-back round-trips keep their fresh-dict semantics; parse errors are never cached; the docstring no longer claims 'no caching'. - Tests: memo semantics (deepcopy isolation, inode-replace reparse, error non-caching, parse-count), a wrapper-presence pin for both heavy sidebar endpoints, and a cold-cache autouse fixture in the scope tests (the 5s TTL otherwise leaks one test's payload into the next). Measured on the affected box: list_profiles 1.9s -> 0.16s warm, serve CPU 112% -> 22-34%, host CPU 50-70% -> ~26%. --- hermes_cli/web_routers/profiles.py | 1 + .../hermes_cli/test_profiles_sidebar_cache.py | 15 +++++++++++ .../hermes_cli/test_profiles_sidebar_scope.py | 27 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/hermes_cli/web_routers/profiles.py b/hermes_cli/web_routers/profiles.py index 1f0da2fb17..f31bdb8ae9 100644 --- a/hermes_cli/web_routers/profiles.py +++ b/hermes_cli/web_routers/profiles.py @@ -557,6 +557,7 @@ def _merge_profile_tree( @sessions_router.get("/api/profiles/projects/tree") +@_sidebar_singleflight_cache def get_profiles_projects_tree(preview_limit: int = 3, session_limit: int = 2000): """Project tree for every profile at once, for the all-profiles sidebar. diff --git a/tests/hermes_cli/test_profiles_sidebar_cache.py b/tests/hermes_cli/test_profiles_sidebar_cache.py index ead0c6b08b..5b45f739ce 100644 --- a/tests/hermes_cli/test_profiles_sidebar_cache.py +++ b/tests/hermes_cli/test_profiles_sidebar_cache.py @@ -172,6 +172,21 @@ class SidebarCacheTests(unittest.TestCase): self.assertEqual(inspect.signature(wrapped), inspect.signature(scan)) + def test_heavy_sidebar_endpoints_are_all_single_flight_wrapped(self): + # HostHighCPU on a 2-vCore box: /api/profiles/projects/tree was the + # one heavy sidebar endpoint left uncoalesced, so every Desktop poll + # re-ran the full 50-profile fan-out. Any new heavy sidebar endpoint + # must carry the wrapper too — this pins the existing two. + for endpoint in ( + profiles.get_profiles_sessions_sidebar, + profiles.get_profiles_projects_tree, + ): + self.assertTrue( + callable(getattr(endpoint, "cache_clear", None)), + f"{endpoint.__name__} lost its @_sidebar_singleflight_cache " + "wrapper — concurrent Desktop polls will re-scan every profile", + ) + if __name__ == "__main__": unittest.main() diff --git a/tests/hermes_cli/test_profiles_sidebar_scope.py b/tests/hermes_cli/test_profiles_sidebar_scope.py index 75309fbd00..068bb0e1d1 100644 --- a/tests/hermes_cli/test_profiles_sidebar_scope.py +++ b/tests/hermes_cli/test_profiles_sidebar_scope.py @@ -14,6 +14,33 @@ Two behaviors that only show up with more than one profile on disk: import pytest +@pytest.fixture(autouse=True) +def _cold_sidebar_singleflight_caches(): + """Force a cold single-flight cache around every test in this file. + + Both sidebar endpoints are wrapped in ``_sidebar_singleflight_cache`` + (5s TTL). Each test builds a fresh tmp profile set, so a warm entry from + a previous test — same default query params, different disk state — would + answer with another test's payload. Clearing before and after keeps every + request cold without disabling the caching the endpoints rely on. + """ + from hermes_cli.web_routers import profiles as profiles_routes + + endpoints = ( + profiles_routes.get_profiles_sessions_sidebar, + profiles_routes.get_profiles_projects_tree, + ) + for endpoint in endpoints: + clear = getattr(endpoint, "cache_clear", None) + if clear is not None: + clear() + yield + for endpoint in endpoints: + clear = getattr(endpoint, "cache_clear", None) + if clear is not None: + clear() + + @pytest.fixture def profiles_on_disk(tmp_path, monkeypatch, _isolate_hermes_home): """An isolated default home plus one named profile, each with a state.db."""