Skip to content

perf: Bulk permissions endpoint to eliminate N+1 API calls on Dashboard load #359

Closed

Description

@vybe

Problem

Dashboard load time is ~15 seconds with 12+ agents due to N+1 permission fetching. The fetchPermissionEdges() function in stores/network.js:152-219 makes N separate API calls — one GET /api/agents/{name}/permissions per agent.

Each call:

  • Does Docker container lookup (get_agent_container())
  • Queries DB for permissions (db.get_permitted_agents())
  • Returns all accessible agents (redundant data repeated N times)

With 12 agents = 12 HTTP round trips, even when parallelized.

Root Cause

Issue #87 introduced permission edges on the graph dashboard but no bulk endpoint was created. The PERF-269 optimizations (issue #269) addressed context-stats N+1 but not permissions.

Proposed Solution

Backend: Add bulk endpoint

GET /api/agents/permissions-edges — returns all permission edges in a single query:

# db/permissions.py
def get_all_permission_edges(self) -> List[dict]:
    """Get all permission edges for graph visualization."""
    with get_db_connection() as conn:
        cursor = conn.cursor()
        cursor.execute("""
            SELECT source_agent, target_agent FROM agent_permissions
        """)
        return [{"source": r["source_agent"], "target": r["target_agent"]} 
                for r in cursor.fetchall()]
# routers/agents.py (or agent_files.py)
@router.get("/permissions-edges")
async def get_all_permission_edges(current_user: User = Depends(get_current_user)):
    """Get all permission edges for dashboard graph (bulk endpoint)."""
    edges = db.get_all_permission_edges()
    # Filter to only edges where user can access both agents
    accessible = {a['name'] for a in get_accessible_agents(current_user)}
    return {
        "edges": [e for e in edges if e["source"] in accessible and e["target"] in accessible]
    }

Frontend: Replace N fetches with 1

// stores/network.js
async function fetchPermissionEdges() {
  const response = await axios.get('/api/agents/permissions-edges')
  const edges = response.data.edges
  // Build edge map from flat list...
}

Frontend: Decouple from agent refresh

Move fetchPermissionEdges() out of fetchAgents(). Call once on mount, update only when:

  • WebSocket notification of permission change (future)
  • Manual refresh button

Files Affected

File Change
src/backend/db/permissions.py Add get_all_permission_edges()
src/backend/database.py Expose new method
src/backend/routers/agents.py Add /permissions-edges endpoint
src/frontend/src/stores/network.js Replace N fetches with bulk call

Expected Impact

  • API calls: 12+ → 1 (92% reduction)
  • Load time: ~15s → ~3-5s estimated
  • Data transfer: Eliminates redundant available_agents payload

Acceptance Criteria

  • New GET /api/agents/permissions-edges endpoint returns all edges in one query
  • Frontend uses bulk endpoint instead of per-agent fetches
  • Permission edges load in < 500ms with 30+ agents
  • Dashboard initial load < 5 seconds with 12+ agents

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-p1type-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions