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

Author: angelusbrCreated Oct 1, 2026Updated Oct 1, 2026

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:

  1. switch_profile() clears _SKILLS_STATS_CACHE for all profiles on every switch (api/profiles.py:1729 on master @ dd54519036). The next list_profiles_api() after the 4 s row cache (_LIST_PROFILES_CACHE_TTL) expires re-reads and YAML-parses every SKILL.md of every profile. In practice the 300 s TTL rarely applies to anyone who switches profiles.
  2. _skill_tree_max_mtime_ns() runs on every call and stats every entry under every profile's skills/ tree. Cheap on a local SSD, expensive wherever stat() is not free (sandboxed runtimes, network filesystems — the NAS case from #4783).
  3. get_cli_sessions(all_profiles=True) → _all_profiles_cli_contexts() calls list_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 on master @ 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 plain runc on the same host are included to separate "slow stat" from "CPU work".
  • Real instance: 3 profiles, ~306 SKILL.md per 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/switch 5.1–9.7 s together with GET /api/profiles 5.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 stage list_profiles_api.
  • Another user, one profile switch under load: GET /api/profiles 24.2 s (stage list_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 stage get_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

  1. Create 5+ profiles cloned from the default (bundled skills).
  2. Switch profiles waiting > 4 s between switches; watch /api/profile/switch and /api/profiles in the access log and the list_profiles_api stage in Slow WebUI request.
  3. With "All profiles" on in the sidebar, watch GET /api/sessions?...all_profiles=1 → stage get_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).