Skip to content

perf(soc-dashboard): cache workflow manifest/node-count parsing to fix 100% CPU under polling - #808

Open
Erliangg wants to merge 1 commit into
AgentFlocks:mainfrom
Erliangg:perf/soc-dashboard-task-center-cache
Open

Erliangg wants to merge 1 commit into
AgentFlocks:mainfrom
Erliangg:perf/soc-dashboard-task-center-cache

Conversation

@Erliangg

Copy link
Copy Markdown

Summary

The SOC dashboard polls /api/task-center and /api/activity every 1-3 seconds
while open. Each poll re-parsed every workflow definition on disk (up to
163KB each) and re-scanned all plugin manifests, pinning one core at 100%+ in
the serve process. Session runners (agents) share that process and were
starved — agent turns stalled mid-stream with the UI stuck on "generating".

Root cause (py-spy, 108 samples in production)

100% of hot samples landed in the polling handler chain:

  • _workflow_manifest_name_map() — rebuilt the whole manifest map per request
    (Path.home() ×3, read_text + json.loads per plugin manifest)
  • _workflow_node_count(workflow_id) — re-read and re-parsed each
    workflow.json (up to 163KB) for every workflow row, every poll
  • Path.home() / Path.resolve() / _load_parts appearing throughout the stack

Fix

Module-level caches invalidated by file mtime_ns (copy-then-replace, GIL-atomic,
no locks):

  • _WORKFLOW_ROOTS constant — roots built once
  • _MANIFEST_MAP_CACHE — keyed by a fingerprint of the workflow roots dir mtimes
  • _NODE_COUNT_CACHE — keyed by each workflow.json's mtime_ns

Definitions edited on disk refresh automatically on the next request; no TTL,
no stale window.

Verification (production, v2026.11.09)

Metric Before After
serve CPU (dashboard open) 104% 16-18%
py-spy hot samples in handler chain 100% <5%, no concentrated hotspot
/api/task-center response — byte-identical (diffed before/after)
definition edit refresh — automatic on next request (mtime_ns)

Reproduction

  1. Open the SOC dashboard page in a browser
  2. top -p $(pgrep -f "flocks serve") → CPU ~100%
  3. py-spy record --pid <pid> -d 12 → all samples in _task_center_workflow_rows
  4. Apply this patch → CPU drops to <20%

The soc-dashboard page polls /api/task-center and /api/activity every
1-3 seconds while open. Each poll re-parsed every workflow definition
on disk (up to 163KB each) and re-scanned all plugin manifests,
pinning one core at 100%+ in the serve process. Session runners share
that process and were starved (agent turns stalled on the same event
loop).

Add module-level caches invalidated by file mtime_ns:
- _WORKFLOW_ROOTS constant (was rebuilt per call, Path.home() hit
  3x per poll)
- _MANIFEST_MAP_CACHE (keyed by workflow roots dir mtimes fingerprint)
- _NODE_COUNT_CACHE (keyed by workflow.json mtime_ns, per workflow)

Cache writes are copy-then-replace (GIL-atomic), so no locks are
needed. API responses are byte-identical; edited definitions refresh
on the next request.

Measured in production (soc-dashboard left open): serve CPU 104% -> 16-18%.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant