Skip to content

refactor(backend): per-row lookups in list endpoints — uncached user, owner and access helpers make query counts grow with rows and agents #4672

Open

Description

@vybe

Summary

Technical debt: many list endpoints run a number of queries that grows with the rows or agents they return. The shared cause is that the user, owner and access helpers each open a connection and run their own SELECT on every call, with no caching, so any loop over agents or rows multiplies them.

This is the same class as the asks slowness in #4671 (0.45 s for 63 rows, about 130 extra lookups). A read-only audit found it in the endpoints below. Query counts are read from code; nothing was timed.

Context

Uncached helpers: get_user_by_username, get_user_by_email (src/backend/db/users.py), get_agent_owner, can_user_access_agent, can_user_share_agent. can_user_access_agent costs 1 query for an admin and up to 4 for anyone else; can_user_share_agent costs 1 or 2.

Rank Endpoint What repeats N How hot
1 GET /api/enterprise/client-portal/my-agents get_roster calls may_manage_canvases per agent: get_user_by_email then can_user_share_agent. 2–3 queries per agent, re-reading the same user row roster agents Every Workspace load; the landing also re-reads it 2–3 times
2 GET /api/operator-queue, GET /api/operator-queue/agents/{agent_name} _for_principal calls skill_gate_service.card_view per item: gate request read, a Redis read, _openable_run twice, _person_label per approver. 2–12 queries per gate row gate-approval rows in the page (up to 500) Polled every 15 s
3 GET /api/ops/fleet/status, GET /api/ops/fleet/health One HTTP call per running agent, awaited one after another, plus get_agent_owner per agent all agents /fleet/status polled every 15 s by mobile admin
4 GET /api/agents/{name}/loops _build_status_response calls db.list_loop_runs per loop loops returned (up to 200) Workspace rail, every 12 s per chat participant
5 GET /api/agents/autonomy-status can_user_access_agent, agent_may_reach, is_system_agent per agent. 2–5 queries each every agent with an autonomy row Mobile admin every 15 s; assistant panel
6 GET /api/agents/context-stats HTTP fan-out to every running agent. The cache TTL (15 s) equals the poll interval, and the cache is written only for fleet admins running agents Dashboard poll
7 GET /api/enterprise/client-portal/work, /work/activity get_execution_timeout per agent, get_portal_session per agent, get_execution per running execution chat participants, running executions Polled every 12 s and every few seconds during a turn
8 GET /api/agents/{name}/schedules _can_manage_webhook → agent_viewer_tier per schedule; the answer is the same for every row schedules Schedules tab
9 GET /api/evaluations can_user_share_agent plus agent_may_reach per distinct agent distinct agents in up to 500 rows Evaluations page
10 GET /api/agents/{agent_name}/access-requests _known_as per row: requester history plus a verified-links query access requests Sharing panel
11 GET /api/agents/{agent_name}/public-links, GET /api/public/sessions/{token} usage stats per link; last message per session (capped at 20) links, sessions On open
12 GET /api/ops/auth-report 4–5 queries per agent all agents Low traffic
13 GET /api/systems, GET /api/systems/{system_name} audit-entry paging per tag; three reads per member agent. Not read end to end tags, member agents Unverified

Also in asks (#4671), beyond the roster and user lookups that issue names: _project calls skill_gate_service.card_view per gate row and _linked_request_id twice per row (db.get_operator_queue_item each).

Checked and found already batched: GET /api/agents, executions lists and timeline, activities timeline, notifications, monitoring status, tags, client-portal sessions, chat-state, search and history.

Acceptance Criteria

Shared fix

  • User, owner and access lookups are memoized per request (or batched), so repeated calls for the same user or agent inside one request run one query. Membership rules are not re-implemented in callers.
  • The memo cannot outlive the request or leak one principal's answer to another. A test proves both.

Endpoints

  • For ranks 1–5 and 7, the query count for one call no longer grows with the number of rows or agents. Each has a test that pins the query count on a seeded list of two sizes.
  • /api/ops/fleet/status reads agents concurrently with a bounded timeout, so one slow agent cannot stall the whole response.
  • context-stats: the cache TTL is longer than the poll interval, and a non-admin caller no longer fans out on every poll. Per-caller filtering still applies to cached data.
  • Ranks 8–12 are fixed or each has a recorded reason to leave it.
  • Rank 13 is confirmed or dismissed by reading the loop bodies.
  • Access results are unchanged for every endpoint touched: existing auth and enumeration-safety tests pass, and fail-closed behaviour is kept.

Guard

  • A reusable test helper counts queries for a request, so new list endpoints can pin theirs.

Technical Notes

  • Files: src/backend/client_portal/service.py (get_roster, may_manage_canvases), src/backend/routers/operator_queue.py (_for_principal), src/backend/services/skill_gate_service.py (card_view, _openable_run, _person_label), src/backend/routers/ops.py, src/backend/services/fleet_ops_service.py, src/backend/routers/loops.py, src/backend/services/agent_service/autonomy.py, src/backend/services/agent_service/stats.py, src/backend/client_portal/work/service.py, src/backend/routers/schedules.py, src/backend/routers/evaluations.py, src/backend/services/access_request_service.py, src/backend/routers/public_links.py, src/backend/routers/public.py, src/backend/routers/systems.py.
  • get_accessible_agents (src/backend/services/agent_service/helpers.py) already answers "which agents can this user reach" in 2 queries total; autonomy-status can use it instead of a per-agent check.
  • In-code comments at the get_roster, card_view and loops call sites already acknowledge the per-item cost.
  • Not audited: the sharing, users, MCP keys, subscriptions, skills, reports, sessions, canvas and agent-dashboard routers, shared-folder endpoints, and acting_person_agents. Sweep them with the query-count helper once it exists.
  • Likely more than one PR: the shared memo first, then endpoints in rank order.

Activity

  1. added
    priority-p2
    type-refactor
    theme-reliability
    status-ready
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p2status-readytheme-reliabilitytype-refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions