Repository navigation
Description
Summary
Backend CPU usage reaches ~45% (two Python workers at ~28% each) on a 4-vCPU instance running ~30 agents and 2-3 concurrent browser users. The root cause is a combination of excessive WebSocket connection churn from agent sidecars and aggressive frontend polling from multiple browser tabs. The system becomes sluggish under what should be a manageable load.
Component
Backend / WebSocket Handler / Frontend Polling
Priority
P2
Observed Behavior
# docker stats snapshot
trinity-backend 44.92% 236.1MiB / 15.62GiB
# WebSocket churn: ~1,841 open/close events in 2,000 log lines
INFO: ('172.28.0.x', NNNNN) - "WebSocket /ws" [accepted]
INFO: connection open
INFO: connection closed
# (repeating hundreds of times)
# Frontend polling: single user with agent detail page open generates
# multiple requests per second to /activity, /stats, /git/status endpoints
Root Causes
1. Agent Sidecar WebSocket Reconnection Storm
Each of the ~30 agent containers runs a sidecar (agent-server.py) that maintains a WebSocket connection to the backend. These connections appear to disconnect and reconnect frequently, creating a churn of open/close cycles. With 30+ agents, this creates a constant stream of WebSocket handshakes.
2. Frontend Polling Frequency
The frontend polls multiple endpoints at high frequency when agent detail pages are open:
/api/agents/{name}/activity- polled every ~2-3 seconds/api/agents/{name}/stats- polled every ~2-3 seconds/api/agents/{name}/git/status- polled every ~2-3 seconds
A single user with one agent detail tab generates ~3 requests/second. Multiple tabs or users multiply this linearly. Observed: one user with multiple tabs generating floods of identical requests.
3. No Request Coalescing or Throttling
Each polling endpoint independently queries Docker or the agent sidecar. There's no caching layer, request deduplication, or rate limiting on these high-frequency endpoints.
Reproduction Steps
- Deploy a Trinity instance with 25-30 agents running
- Have 2-3 users open the dashboard and agent detail pages in browsers
- One user opens multiple browser tabs on different agent detail pages
- Monitor backend CPU:
docker stats trinity-backend - Observe CPU climbing to 40-50% on a 4-vCPU machine
Suggested Improvements
Short-term
- Cache agent stats/activity responses for 5-10 seconds — these endpoints query Docker which is expensive
- Increase frontend polling intervals from ~2-3s to 10-15s for non-critical endpoints (git status, activity)
- Use SSE or persistent WebSocket for agent updates instead of polling — the backend already has WebSocket infrastructure
Medium-term
- Investigate sidecar WebSocket stability — why are connections churning? Add reconnection backoff and keepalive
- Add server-side rate limiting on polling endpoints per user session
- Batch agent stats collection — collect all agent stats in one Docker API call instead of per-agent
Long-term
- Replace polling with push model — backend pushes agent state changes over WebSocket to subscribed frontends
- Consider uvicorn worker tuning — currently 2 workers may not be optimal for this I/O-bound workload
- Add request coalescing — if 3 tabs request the same agent stats within 1 second, serve from a single Docker query
Environment
- Trinity version:
f50ee3e - Docker version: 29.2.1
- OS: Ubuntu 22.04.5 LTS
- Instance: 4 vCPU, 16 GB RAM
- Agents running: ~30
- Concurrent browser users: 2-3
Related
src/backend/main.py— WebSocket handlersrc/backend/routers/agents.py— agent stats/activity endpointssrc/agent-base/agent-server.py— sidecar WebSocket clientsrc/frontend/— polling intervals in agent detail components
Activity
vybe commentedon Apr 16, 2026
Additional Finding: N+1 Permissions Fetch
Investigation of 15-second dashboard load times with 12+ agents revealed an unaddressed N+1 problem in permission edge fetching.
Current Behavior
fetchPermissionEdges()instores/network.js:152-219makes N parallel API calls - oneGET /api/agents/{name}/permissionsper agent. Each call:get_agent_container())db.get_permitted_agents())With 12+ agents = 12+ HTTP round trips, even parallelized.
Proposed Fix
Add bulk endpoint
GET /api/agents/permissions-edgesthat returns all permission edges in a single query:Frontend changes:
fetchAgents()- call once on mountImpact
fix(#73): invalidate stats cache at the Docker primitive boundary
docs(#73): clarify stats-cache invalidation chokepoint + raise swallo…
refactor(#73): consolidate per-agent stats cache into a single entry
perf: cache per-agent container stats + bulk /sync-health lookup (#73) (
feat(analytics): per-schedule execution analytics (#868) (#932)
vybe commentedon Jun 11, 2026
Root mechanism identified — delta from the 2026-06-11 repo audit:
The CPU saturation this issue observes has a concrete primary mechanism:
list_all_agents_fast()callscontainers.list()withoutsparse=True(src/backend/services/docker_service.py), which docker-py implements as 1+N synchronous inspects — ~5ms per container, executed directly on the asyncio event loop. At 200 agents that's ~1s of event-loop blockage per call, and it's invoked from the operator-queue 5s tick, the heartbeat tick, monitoring,GET /api/agents, and even the stats cache-HIT path. Verifiers found 8 additional sync Docker call sites inrouters/ops.py.Two fixes, both cheap relative to the payoff:
sparse=True+asyncio.to_threadoffload inlist_all_agents_fastand the other sync Docker sites.--workers 2doubles every sweep (this already caused one documented SQLite-contention incident). Needs leader election (RedisSET NX) or moving fleet sweeps to a single-runner process.AndriiPasternak31 commentedon Jun 22, 2026
/claim
github-actions commentedon Jun 22, 2026
@AndriiPasternak31 This issue is already assigned to AndriiPasternak31. Use
/unclaimfirst if you want to take over.test(brain-orb): close coverage gaps — owner gate, idempotency, hook …
fix(brain-orb): post-voice processing as observable execution + write…
chore(.claude): bump the submodule pointer to main (f7ff3e7 -> 60ddc16)
chore(.claude): bump the submodule pointer to main (f7ff3e7 -> 60ddc1…