--------|----------------|-------|
| 1 | Database & Queries | references/database-queries.md | N+1 queries, SELECT *, missing indexes, ORM misuse, connection pooling |
| 2 | Memory & Resources | references/memory-resources.md | Memory leaks, unclosed resources, large allocations, string concat in loops |
| 3 | Algorithmic Complexity | references/algorithmic-complexity.md | O(n^2) patterns, unnecessary iterations, wrong data structures for lookups |
| 4 | Concurrency & Async | references/concurrency-async.md | Sequential awaits, blocking in async, race conditions, unbounded concurrency |
| 5 | Bundle & Dependencies | references/bundle-dependencies.md | Heavy imports, unused deps, duplicate libs, missing lazy loading |
| 6 | Dead Code & Redundancy | references/dead-code-redundancy.md | Unused exports, commented code, dead branches, duplicate logic |
| 7 | I/O & Network | references/io-network.md | Sequential requests, missing batching, no dedup, missing compression |
| 8 | Rendering & UI | references/rendering-ui.md | Re-renders, missing virtualization, layout thrashing, animation perf |
| 9 | Data Structures | references/data-structures.md | Wrong structures, unnecessary copies, inefficient serialization |
| 10 | Error & Resilience | references/error-resilience.md | Missing timeouts, swallowed errors, no retries, no circuit breakers |
| 11 | Caching & Memoization | references/caching-memoization.md | Missing memoization, cache without invalidation, redundant API calls |
| 12 | Build & Compilation | references/build-compilation.md | Dev code in prod, missing optimization flags, slow tests, Docker issues |
| 13 | Security-Performance | references/security-performance.md | Crypto misuse, missing rate limiting, ReDoS, SQL injection vectors |
Optional agents (spawn if relevant to detected stack):
- Logging & Observability (
references/logging-observability.md) — if logging framework detected
- Config & Infrastructure (
references/config-infra.md) — if Docker/deployment config detected
Agent Prompt Template
Each agent MUST receive this prompt structure:
You are a {DOMAIN_NAME} optimization specialist. Your job is to find performance
anti-patterns in the codebase at {PROJECT_ROOT}.
CRITICAL RULES:
1. DO NOT read source code files before searching. This avoids anchoring bias.
2. First, read your reference file: {SKILL_DIR}/references/{REFERENCE_FILE}
3. Use Grep and Glob to search for the patterns described in the reference file.
4. Only read 5-10 lines of context around each finding to confirm it's a real issue.
5. Skip patterns that don't match the project's stack: {DETECTED_STACK}
Tech stack detected: {DETECTED_STACK}
Project root: {PROJECT_ROOT}
For each finding, report:
- **File**: path:line_number
- **Pattern**: what anti-pattern was detected
- **Severity**: CRITICAL / HIGH / MEDIUM / LOW
- **Current code**: the problematic snippet (keep short)
- **Why it's slow**: brief explanation of the performance impact
- **Optimal fix**: the recommended solution (code snippet or approach)
- **Estimated impact**: qualitative improvement expected (e.g., "10x faster for large lists")
If you find 0 issues in your domain, report "No issues found" — this is a valid outcome.
Sort findings by severity (CRITICAL first).
Step 3: Consolidate Report
After all agents complete, consolidate their findings into a single prioritized report:
- Collect all findings from all agents
- Deduplicate (different agents may flag the same code for different reasons)
- Sort by severity: CRITICAL > HIGH > MEDIUM > LOW
- Group by file (so the user can fix file-by-file)
- Present the final report with:
- Executive summary: total findings by severity, top 3 most impactful
- Detailed findings table grouped by file
- Improvement plan: ordered list of fixes from highest to lowest impact
Report Format
# Code Optimization Audit Report
## Executive Summary
- **X** critical issues, **Y** high, **Z** medium, **W** low
- Top 3 highest-impact fixes:
1. [brief description] — [estimated impact]
2. [brief description] — [estimated impact]
3. [brief description] — [estimated impact]
## Findings by File
### `path/to/file.ts`
| # | Severity | Domain | Pattern | Fix | Impact |
|---|----------|--------|---------|-----|--------|
| 1 | CRITICAL | Database | N+1 query in loop | Use prefetch_related | 50x fewer queries |
| 2 | HIGH | Async | Sequential awaits | Use Promise.all | 3x faster |
[... for each file with findings ...]
## Improvement Plan
Priority-ordered steps to implement the fixes:
1. **[CRITICAL] Fix N+1 queries in `api/users.py`**
- Current: loop queries user.posts for each user
- Fix: add prefetch_related('posts') to queryset
- Impact: reduces N+1 to 2 queries
2. **[HIGH] Parallelize API calls in `services/sync.ts`**
- Current: 5 sequential await fetch() calls
- Fix: Promise.all([fetch1, fetch2, ...])
- Impact: ~5x faster sync operation
[... continue for all findings ...]
1---2name: code-optimizer3description: Deep code optimization audit using parallel specialist agents. Each agent hunts for performance anti-patterns, inefficiencies, and suboptimal code using pattern-based detection (Grep/Glob) WITHOUT reading the full source code first — avoiding anchoring bias on existing implementations. Covers ALL optimization domains: database queries, memory leaks, algorithmic complexity, concurrency, bundle size, dead code, I/O & network, rendering/UI, data structures, error handling, caching, build config, security-performance, logging, and infrastructure. Use when asked to: "optimize my code", "find performance issues", "audit code quality", "speed up my app", "find bottlenecks", "code review for performance", "find anti-patterns", "improve code efficiency", "reduce latency", "optimize performance", "code smell detection", "find slow code", "optimize this project", "performance audit", "code optimization". Also triggers on: "optimizar codigo", "encontrar cuellos de botella", "mejorar rendimiento".4---5--------|----------------|-------|6| 1 | Database & Queries | `references/database-queries.md` | N+1 queries, SELECT *, missing indexes, ORM misuse, connection pooling |7| 2 | Memory & Resources | `references/memory-resources.md` | Memory leaks, unclosed resources, large allocations, string concat in loops |8| 3 | Algorithmic Complexity | `references/algorithmic-complexity.md` | O(n^2) patterns, unnecessary iterations, wrong data structures for lookups |9| 4 | Concurrency & Async | `references/concurrency-async.md` | Sequential awaits, blocking in async, race conditions, unbounded concurrency |10| 5 | Bundle & Dependencies | `references/bundle-dependencies.md` | Heavy imports, unused deps, duplicate libs, missing lazy loading |11| 6 | Dead Code & Redundancy | `references/dead-code-redundancy.md` | Unused exports, commented code, dead branches, duplicate logic |12| 7 | I/O & Network | `references/io-network.md` | Sequential requests, missing batching, no dedup, missing compression |13| 8 | Rendering & UI | `references/rendering-ui.md` | Re-renders, missing virtualization, layout thrashing, animation perf |14| 9 | Data Structures | `references/data-structures.md` | Wrong structures, unnecessary copies, inefficient serialization |15| 10 | Error & Resilience | `references/error-resilience.md` | Missing timeouts, swallowed errors, no retries, no circuit breakers |16| 11 | Caching & Memoization | `references/caching-memoization.md` | Missing memoization, cache without invalidation, redundant API calls |17| 12 | Build & Compilation | `references/build-compilation.md` | Dev code in prod, missing optimization flags, slow tests, Docker issues |18| 13 | Security-Performance | `references/security-performance.md` | Crypto misuse, missing rate limiting, ReDoS, SQL injection vectors |1920**Optional agents** (spawn if relevant to detected stack):21- Logging & Observability (`references/logging-observability.md`) — if logging framework detected22- Config & Infrastructure (`references/config-infra.md`) — if Docker/deployment config detected2324### Agent Prompt Template2526Each agent MUST receive this prompt structure:2728```29You are a {DOMAIN_NAME} optimization specialist. Your job is to find performance30anti-patterns in the codebase at {PROJECT_ROOT}.3132CRITICAL RULES:331. DO NOT read source code files before searching. This avoids anchoring bias.342. First, read your reference file: {SKILL_DIR}/references/{REFERENCE_FILE}353. Use Grep and Glob to search for the patterns described in the reference file.364. Only read 5-10 lines of context around each finding to confirm it's a real issue.375. Skip patterns that don't match the project's stack: {DETECTED_STACK}3839Tech stack detected: {DETECTED_STACK}40Project root: {PROJECT_ROOT}4142For each finding, report:43- **File**: path:line_number44- **Pattern**: what anti-pattern was detected45- **Severity**: CRITICAL / HIGH / MEDIUM / LOW46- **Current code**: the problematic snippet (keep short)47- **Why it's slow**: brief explanation of the performance impact48- **Optimal fix**: the recommended solution (code snippet or approach)49- **Estimated impact**: qualitative improvement expected (e.g., "10x faster for large lists")5051If you find 0 issues in your domain, report "No issues found" — this is a valid outcome.52Sort findings by severity (CRITICAL first).53```5455### Step 3: Consolidate Report5657After all agents complete, consolidate their findings into a single prioritized report:58591. Collect all findings from all agents602. Deduplicate (different agents may flag the same code for different reasons)613. Sort by severity: CRITICAL > HIGH > MEDIUM > LOW624. Group by file (so the user can fix file-by-file)635. Present the final report with:64 - Executive summary: total findings by severity, top 3 most impactful65 - Detailed findings table grouped by file66 - Improvement plan: ordered list of fixes from highest to lowest impact6768### Report Format6970```markdown71# Code Optimization Audit Report7273## Executive Summary74- **X** critical issues, **Y** high, **Z** medium, **W** low75- Top 3 highest-impact fixes:76 1. [brief description] — [estimated impact]77 2. [brief description] — [estimated impact]78 3. [brief description] — [estimated impact]7980## Findings by File8182### `path/to/file.ts`8384| # | Severity | Domain | Pattern | Fix | Impact |85|---|----------|--------|---------|-----|--------|86| 1 | CRITICAL | Database | N+1 query in loop | Use prefetch_related | 50x fewer queries |87| 2 | HIGH | Async | Sequential awaits | Use Promise.all | 3x faster |8889[... for each file with findings ...]9091## Improvement Plan9293Priority-ordered steps to implement the fixes:94951. **[CRITICAL] Fix N+1 queries in `api/users.py`**96 - Current: loop queries user.posts for each user97 - Fix: add prefetch_related('posts') to queryset98 - Impact: reduces N+1 to 2 queries991002. **[HIGH] Parallelize API calls in `services/sync.ts`**101 - Current: 5 sequential await fetch() calls102 - Fix: Promise.all([fetch1, fetch2, ...])103 - Impact: ~5x faster sync operation104105[... continue for all findings ...]106```