paperless-ngx-perf-document-counts

Based on#11950
SegmentInvestigate-and-fix
Typeperformance
## Task

On larger paperless-ngx instances we're getting timeouts and ~30s
response times on `/api/tags/` and `/api/custom_fields/` whenever a
non-admin user hits them. Lag scales with document corpus size and shared-permission rows.
The numbers each user
sees in `document_count` are correct, the listing is just way too
slow. Similar issue on `/api/correspondents/`,
`/api/document_types/`, and `/api/storage_paths/` (but less bad). Make a backend fix to get those
listings fast again.

## General instructions

- The code repo is at /repo/paperless-ngx.
- You are inside of a Docker container. You may not be able to perform all operations you would normally be able to do on a local machine. Dependencies have not been pre-installed, and you may need to install them yourself.
- You are expected to act autonomously as a software engineer to complete tasks you are given.
- Do not stop until you feel you have completed the task and your code changes can be merged.
- You may need to use software engineering skills like analyzing the codebase, researching technologies, running services, analyzing logs, etc. to complete the task. Not all tasks will be solvable by reading source code alone.

Agent Results

AgentTastefulBasicVerifierValidationRubricBloatPractTasteCheated
Oracle
1/11.001.0x5.04.0
Opus 4.8
1/10.600.8x4.03.0
GPT-5.5
1/10.801.5x3.02.0
Gemini 3.1 Pro
0/10.200.1x3.02.0
Gemini 3.5 Flash
0/10.200.3x3.02.0
GLM-5.2
0/10.400.3x2.02.0
GPT-5.4
0/10.800.5x3.03.0
Kimi K2.6
0/10.600.6x3.03.0
Opus 4.7
0/10.600.5x3.03.0
Sonnet 4.6
0/10.600.5x3.03.0
Sonnet 5
No-Op
0/10.20
Agent details

Verifier Tests

Gemini 3.1 Pro0/1

Rubric Criteria

Gemini 3.1 Pro1/5
Fail → Pass
count_uses_through_table_or_subquery_not_document_join
permitted_document_ids_computed_index_friendly
custom_field_viewset_uses_shared_mixin_and_serializer_updated
tag_hierarchy_descendant_branch_uses_optimized_counting
Pass → Pass
permission_model_semantics_preserved

Taste Scores

Patch Bloat0.1x
19 agent / 155 oracle SLOC, 1 / 3 files(raw: 2.6x)
Practice Alignment — 3.0/5
4
style consistency
2
pattern adherence
3
library usage
2
abstraction level
4
documentation fit
Relative Taste — 2.0/5
3
minimality
2
approach quality
2
hygiene
3
fluency
2
craftsmanship

Agent Patch