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%.
This commit is contained in:
moken627-hub
2026-09-04 11:42:23 +07:00
committed by kshitij
parent 0272647ad0
commit 35af06c009
3 changed files with 43 additions and 0 deletions
+1
View File
@@ -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.
@@ -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()
@@ -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."""