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."""