paperless-ngx-perf-workflow-queries

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

the Workflows screen is painfully slow on a couple of our
bigger instances. pulling up the workflow list
takes ages once there are a decent number of workflows, and saving a
workflow drags badly whenever the database has built up a
lot of triggers and actions.

look at the BE and make these paths fast again without any other behavioral changes.

## 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
Gemini 3.1 Pro
1/11.001.0x4.03.0
GLM-5.2
1/11.002.0x3.03.0
Kimi K2.6
1/11.001.2x3.03.0
Opus 4.7
1/11.001.1x3.03.0
Opus 4.8
1/11.001.0x3.03.0
Sonnet 4.6
1/11.000.9x3.03.0
Gemini 3.5 Flash
1/11.000.9x3.02.0
GPT-5.4
1/11.002.3x3.03.0
GPT-5.5
1/11.002.1x3.03.0
Sonnet 5
1/11.001.5x3.02.0
No-Op
0/10.50
Agent details

Verifier Tests

Gemini 3.1 Pro1/1

Rubric Criteria

Gemini 3.1 Pro4/4
Fail → Pass
list_endpoint_avoids_nested_m2m_nplus1
prune_uses_bulk_aggregation_not_per_object_loop
action_ordering_preserved_after_optimization
Pass → Pass
query_plan_only_no_behavior_change

Taste Scores

Patch Bloat1.0x
64 agent / 61 oracle SLOC, 2 / 2 files(raw: 5.7x)
Practice Alignment — 4.0/5
4
style consistency
3
pattern adherence
4
library usage
3
abstraction level
5
documentation fit
Relative Taste — 3.0/5
3
minimality
3
approach quality
3
hygiene
3
fluency
3
craftsmanship

Agent Patch