perf(profiles): profile skill stats still dominate `/api/profiles`, profile switch and `/api/sessions?all_profiles=1` — cache is cleared on every switch and the mtime probe walks every skill file
Disclosure upfront: the investigation and this write-up were done with AI assistance (Anthropic Claude, claude-opus-5-5, with tool use: shell, cProfile, log analysis). All numbers below come from our own instances and were reviewed by a human before posting.
Summary
Follow-up to #4783 / #5364 / #7305. The two-tier mtime cache that closed #4783 helps a steady-state single profile, but in multi-profile use the skill-count work is still on the hot path of three user-facing requests:
switch_profile()clears_SKILLS_STATS_CACHEfor all profiles on every switch (api/profiles.py:1729onmaster@dd54519036). The nextlist_profiles_api()after the 4 s row cache (_LIST_PROFILES_CACHE_TTL) expires re-reads and YAML-parses everySKILL.mdof every profile. In practice the 300 s TTL rarely applies to anyone who switches profiles._skill_tree_max_mtime_ns()runs on every call and stats every entry under every profile'sskills/tree. Cheap on a local SSD, expensive whereverstat()is not free (sandboxed runtimes, network filesystems — the NAS case from #4783).get_cli_sessions(all_profiles=True)→_all_profiles_cli_contexts()callslist_profiles_api()(api/models.py:8258) only to enumerate profile names, so the sidebar session list inherits the whole skill-stats cost. A few lines later the same function already does_profiles_root().iterdir().
Note: the skill count does not depend on which profile is active (_compute_profile_skills_stats reads only the profile's own config.yaml and frontmatter), and unlike the create/delete clears (#1612) the clear in switch_profile has no comment explaining it.
Environment
- hermes-webui at upstream
c296673ebf(2026-09-28); the relevant code is unchanged onmaster@dd54519036(2026-10-01). - Linux, Docker, Python 3.14. Each user runs in their own container; production uses gVisor (
runsc, systrap, no KVM), so every file syscall is expensive. Numbers with plainruncon the same host are included to separate "slow stat" from "CPU work". - Real instance: 3 profiles, ~306
SKILL.mdper profile, ~2,330 entries walked by the mtime probe, 45 WebUI sessions.
Measurements
From the WebUI's own Slow WebUI request stage breakdown, 2026-09-30:
- Profile switch on an instance with 6 profiles:
POST /api/profile/switch5.1–9.7 s together withGET /api/profiles5.9–10.7 s, alternating with 30–90 ms switches that land inside the 4 s row cache. 100% of the slow time is in stagelist_profiles_api. - Another user, one profile switch under load:
GET /api/profiles24.2 s (stagelist_profiles_api). - Sidebar:
GET /api/sessions?sidebar_source=webui&all_profiles=1— 202 calls in one day for one user, 111 above 1 s, worst 12.2 s; dominant stageget_cli_sessions.
cProfile of get_cli_sessions(all_profiles=True), cold, same data copied into two containers on the same host (profiler overhead inflates totals; the ratios are the point):
| total | stat/scandir/open/read | YAML (ruamel) | SQL | |
|---|---|---|---|---|
| gVisor, 2nd run | 8.6 s | 2.6 s | 2.9 s | — |
| runc, 2nd run | 4.7 s | 0.24 s | 2.7 s | — |
gVisor, skill stats short-circuited to (0, 0) |
3.0–4.5 s | 1.2–2.1 s | 0.2 s | |
| runc, skill stats short-circuited | 1.3 s | 0.08 s | 0.2 s |
On the real gVisor instance: 8.7 s of a 10.5 s cold get_cli_sessions call is _all_profiles_cli_contexts → list_profiles_api → _get_profile_skills_stats; the SQLite queries total 0.65 s (largest state.db: 6,577 messages), so the database is not the bottleneck here. The YAML parse is CPU-bound (~2.7 s with or without gVisor), so the per-switch recompute costs seconds even on native disks.
Steps to reproduce
- Create 5+ profiles cloned from the default (bundled skills).
- Switch profiles waiting > 4 s between switches; watch
/api/profile/switchand/api/profilesin the access log and thelist_profiles_apistage inSlow WebUI request. - With "All profiles" on in the sidebar, watch
GET /api/sessions?...all_profiles=1→ stageget_cli_sessions.
Suggested direction (small, independent pieces)
a. Drop _SKILLS_STATS_CACHE.clear() from switch_profile() (or justify it in a comment); the mtime probe already catches real changes.
b. Cheaper change detection: the agent already does this for its own count — _skills_dir_signature (root + category dirs, one scandir) plus _cached_skill_count (recount off the request thread) in hermes_cli/profiles.py. Parity would also fix (2). We know the existing comment about nested out-of-band deletes; a generation counter bumped by the WebUI's own skill mutations plus the coarse signature may be enough.
c. Stale-while-revalidate: serve the cached counts and recompute in the background.
d. In _all_profiles_cli_contexts(), enumerate profiles without list_profiles_api() (default + active + _profiles_root().iterdir(), which is already there).
e. On TTL expiry with an unchanged mtime, just extend expiry instead of recomputing.
f. Optionally, an env opt-out (e.g. HERMES_WEBUI_PROFILE_SKILL_STATS=0) for slow-storage deployments; the UI already hides the line when total_skills is 0 (static/panels.js).
Related
#4783 (closed by the two-tier cache), #5364 (thundering herd), #7305, #7566 (withdrawn by its author; same get_cli_sessions path, different cause — cold state.db and cache-key churn).
Source: nesquena/hermes-webui