qualia-framework 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,417 @@
1
+ ---
2
+ name: qualia-optimize
3
+ description: "Deep optimization pass — reads .planning/ AND codebase to find performance, design, UI, backend, and frontend issues. Spawns parallel specialist agents. Use this skill whenever the user says 'optimize', 'optimization pass', 'find issues', 'qualia-optimize', 'deep optimize', 'performance audit', 'design alignment check', 'speed up', 'slow', 'bundle size', or wants a comprehensive quality sweep. Supports --perf, --ui, --backend, --alignment, --fix flags."
4
+ ---
5
+
6
+ # Qualia Optimize — Deep Codebase + Planning Optimization
7
+
8
+ Comprehensive optimization that reads BOTH `.planning/` docs AND the actual codebase. Never analyze one without the other.
9
+
10
+ ## Usage
11
+
12
+ - `/qualia-optimize` — Full optimization (all 6 dimensions)
13
+ - `/qualia-optimize --perf` — Performance only (queries, bundle, render, latency)
14
+ - `/qualia-optimize --ui` — Frontend/design/UI only
15
+ - `/qualia-optimize --backend` — Backend only (RLS, auth, queries, edge functions)
16
+ - `/qualia-optimize --alignment` — Planning-code alignment check only
17
+ - `/qualia-optimize --fix` — Auto-fix LOW/MEDIUM findings from existing OPTIMIZE.md
18
+
19
+ ## Process
20
+
21
+ ### Step 1: Parse Arguments
22
+
23
+ Extract mode from $ARGUMENTS. Default to `full`.
24
+
25
+ Supported modes: `full`, `perf`, `ui`, `backend`, `alignment`, `fix`.
26
+
27
+ If `--fix`: skip to Step 9 (requires existing `.planning/OPTIMIZE.md`).
28
+
29
+ ### Step 2: Load Planning Context (MANDATORY — never skip)
30
+
31
+ Read ALL of these (skip silently if file doesn't exist, but always attempt):
32
+
33
+ ```bash
34
+ cat .planning/PROJECT.md 2>/dev/null || echo "NO_PROJECT"
35
+ ```
36
+
37
+ ```bash
38
+ cat .planning/REQUIREMENTS.md 2>/dev/null || echo "NO_REQUIREMENTS"
39
+ ```
40
+
41
+ ```bash
42
+ cat .planning/ROADMAP.md 2>/dev/null || echo "NO_ROADMAP"
43
+ ```
44
+
45
+ ```bash
46
+ cat .planning/STATE.md 2>/dev/null || echo "NO_STATE"
47
+ ```
48
+
49
+ ```bash
50
+ cat .planning/DESIGN.md 2>/dev/null || echo "NO_DESIGN"
51
+ ```
52
+
53
+ Also read the rules:
54
+ ```bash
55
+ cat ~/.claude/rules/frontend.md 2>/dev/null
56
+ cat ~/.claude/rules/security.md 2>/dev/null
57
+ ```
58
+
59
+ Store all content — you will inline it into agent prompts.
60
+
61
+ **If NO planning docs exist at all**: warn the user but proceed. The optimization still works on raw codebase — it just can't check alignment.
62
+
63
+ ### Step 3: Discover Codebase
64
+
65
+ ```bash
66
+ pwd && node -e "try{const p=require('./package.json');console.log(JSON.stringify({name:p.name,deps:Object.keys(p.dependencies||{}),devDeps:Object.keys(p.devDependencies||{})}))}catch(e){console.log('{}')}" 2>/dev/null
67
+ ```
68
+
69
+ ```bash
70
+ git log --oneline -15 2>/dev/null
71
+ ```
72
+
73
+ ```bash
74
+ git diff --stat HEAD~10..HEAD 2>/dev/null | tail -5
75
+ ```
76
+
77
+ ```bash
78
+ # Project structure
79
+ ls -d app/ src/ pages/ components/ lib/ actions/ hooks/ supabase/ types/ 2>/dev/null
80
+ ```
81
+
82
+ Classify project type: `web` | `voice` | `mobile` | `agent` | `edge-functions` | `unknown`
83
+
84
+ ### Step 4: Spawn Wave 1 Agents (parallel)
85
+
86
+ Based on mode, spawn agents in a **single message** with multiple Agent() calls.
87
+
88
+ | Mode | Agents |
89
+ |------|--------|
90
+ | `full` | frontend-agent + backend-agent + performance-oracle (3 parallel) |
91
+ | `perf` | performance-oracle only |
92
+ | `ui` | frontend-agent only |
93
+ | `backend` | backend-agent only |
94
+ | `alignment` | general-purpose with alignment prompt |
95
+
96
+ **CRITICAL**: Inline ALL planning context into each agent prompt. `@` references don't work across Agent() boundaries.
97
+
98
+ #### Frontend Agent Prompt
99
+
100
+ ```
101
+ Agent(
102
+ prompt="You are optimizing a project's frontend. Read the planning docs and codebase rules below, then analyze the actual code.
103
+
104
+ <planning>
105
+ {PROJECT.md content}
106
+ {REQUIREMENTS.md content}
107
+ {DESIGN.md content}
108
+ </planning>
109
+
110
+ <rules>
111
+ {rules/frontend.md content}
112
+ </rules>
113
+
114
+ <task>
115
+ Analyze the frontend codebase for issues in these categories:
116
+
117
+ 1. **UI Quality**
118
+ - Loading states: every async operation should show a loading indicator
119
+ - Error states: every data-fetching component should handle errors gracefully
120
+ - Empty states: lists/tables should handle zero items with helpful messaging
121
+ - Responsive: check for fixed pixel widths on containers, missing breakpoint handling
122
+ - Accessibility: alt text on images, ARIA labels on interactive elements, keyboard navigation
123
+
124
+ 2. **Design Alignment**
125
+ - Compare actual components against DESIGN.md decisions (colors, typography, spacing)
126
+ - Check rules/frontend.md compliance: distinctive fonts? sharp accents? transitions? No card grids or blue-purple gradients?
127
+ - Consistency: are the same patterns used throughout? (button styles, spacing, color usage)
128
+
129
+ 3. **Frontend Performance**
130
+ - Bundle: large library imports that could be tree-shaken or dynamically imported
131
+ - Images: using next/image? width/height set? lazy loading below fold?
132
+ - Fonts: using next/font? No render-blocking font loads?
133
+ - CSS: unused Tailwind classes? conflicting styles?
134
+ - Rendering: unnecessary re-renders, missing React.memo on list items, heavy computations in render
135
+
136
+ For EVERY finding, output in this exact format:
137
+ - **What**: [description]
138
+ - **Where**: [file:line]
139
+ - **Why**: [impact on users/performance]
140
+ - **Fix**: [concrete fix suggestion]
141
+ - **Severity**: CRITICAL | HIGH | MEDIUM | LOW
142
+ </task>",
143
+ subagent_type="frontend-agent",
144
+ description="Frontend optimization analysis"
145
+ )
146
+ ```
147
+
148
+ #### Backend Agent Prompt
149
+
150
+ ```
151
+ Agent(
152
+ prompt="You are optimizing a project's backend. Read the planning docs and security rules below, then analyze the actual code.
153
+
154
+ <planning>
155
+ {PROJECT.md content}
156
+ {REQUIREMENTS.md content}
157
+ </planning>
158
+
159
+ <rules>
160
+ {rules/security.md content}
161
+ </rules>
162
+
163
+ <task>
164
+ Analyze the backend codebase for issues:
165
+
166
+ 1. **Security**
167
+ - RLS: every Supabase table must have ROW LEVEL SECURITY enabled with policies
168
+ - Service role: grep for service_role key usage in client-side code (app/, components/, src/) — should be ZERO
169
+ - Auth: all mutations use server-side auth check (supabase.auth.getUser())
170
+ - Validation: input validated with Zod before database operations
171
+ - No dangerouslySetInnerHTML or eval()
172
+
173
+ 2. **Data Access Patterns**
174
+ - Server actions vs client mutations: data writes should use 'use server' actions, not direct Supabase client calls
175
+ - Proper error handling: try/catch with meaningful error messages
176
+ - Revalidation: revalidatePath/revalidateTag after mutations
177
+
178
+ 3. **Edge Functions** (if supabase/functions/ exists)
179
+ - Cold start optimization: bundle size, dependency count
180
+ - Error handling and logging
181
+ - CORS configuration
182
+ - Timeout protection (maxDuration)
183
+
184
+ 4. **API Quality**
185
+ - Rate limiting on public endpoints
186
+ - Proper HTTP status codes
187
+ - Consistent error response format
188
+
189
+ For EVERY finding, output:
190
+ - **What**: [description]
191
+ - **Where**: [file:line]
192
+ - **Why**: [impact]
193
+ - **Fix**: [concrete suggestion]
194
+ - **Severity**: CRITICAL | HIGH | MEDIUM | LOW
195
+ </task>",
196
+ subagent_type="backend-agent",
197
+ description="Backend optimization analysis"
198
+ )
199
+ ```
200
+
201
+ #### Performance Oracle Prompt
202
+
203
+ ```
204
+ Agent(
205
+ prompt="You are analyzing cross-cutting performance issues. Read the project context, then analyze the codebase.
206
+
207
+ <planning>
208
+ {PROJECT.md content}
209
+ </planning>
210
+
211
+ <task>
212
+ Analyze for performance issues across the full stack:
213
+
214
+ 1. **Database Queries**
215
+ - N+1 queries: Supabase .from() calls inside loops or .map()
216
+ - Missing indexes: .eq()/.filter()/.order() columns without corresponding indexes in migrations
217
+ - Sequential queries that could be parallel (Promise.all)
218
+ - Over-fetching: SELECT * when only specific columns needed
219
+
220
+ 2. **API Latency**
221
+ - Sequential API calls from client that could be batched
222
+ - Missing caching (SWR/React Query stale times, HTTP cache headers)
223
+ - Large payloads without pagination
224
+
225
+ 3. **Bundle Size**
226
+ - Barrel exports (index.ts re-exporting everything) preventing tree-shaking
227
+ - Large libraries imported for single functions (lodash, moment)
228
+ - Missing dynamic imports for heavy components (charts, editors, maps)
229
+
230
+ 4. **Render Performance**
231
+ - Expensive computations in render path without useMemo
232
+ - Event handlers recreated on every render without useCallback
233
+ - Large lists without virtualization
234
+ - Context providers causing unnecessary re-renders
235
+
236
+ For EVERY finding, output:
237
+ - **What**: [description]
238
+ - **Where**: [file:line]
239
+ - **Why**: [performance impact, quantified if possible]
240
+ - **Fix**: [concrete suggestion]
241
+ - **Severity**: CRITICAL | HIGH | MEDIUM | LOW
242
+ </task>",
243
+ subagent_type="performance-oracle",
244
+ description="Performance optimization analysis"
245
+ )
246
+ ```
247
+
248
+ ### Step 5: Spawn Wave 2 Agent (after Wave 1 completes)
249
+
250
+ After all Wave 1 agents return, spawn the architecture strategist with their combined findings:
251
+
252
+ ```
253
+ Agent(
254
+ prompt="You are synthesizing optimization findings from 3 specialist agents. Look for cross-cutting architectural issues.
255
+
256
+ <wave1_findings>
257
+ {All findings from frontend-agent, backend-agent, performance-oracle}
258
+ </wave1_findings>
259
+
260
+ <planning>
261
+ {PROJECT.md content}
262
+ {REQUIREMENTS.md content}
263
+ </planning>
264
+
265
+ <task>
266
+ 1. Identify patterns across findings — recurring issues that point to a structural problem
267
+ 2. Find coupling issues between frontend and backend
268
+ 3. Check for inconsistent patterns (e.g., some routes use server actions, others use API routes)
269
+ 4. Identify missing abstractions (same pattern repeated 3+ times)
270
+ 5. Check for dead code and unused exports
271
+
272
+ Output:
273
+ - **Structural findings** (architectural issues, not covered by Wave 1)
274
+ - **Pattern consolidation** (where Wave 1 findings share a root cause)
275
+ - Each finding in the same format: What/Where/Why/Fix/Severity
276
+ </task>",
277
+ subagent_type="architecture-strategist",
278
+ description="Architecture synthesis"
279
+ )
280
+ ```
281
+
282
+ **Skip Wave 2 for single-mode runs** (`--perf`, `--ui`, `--backend`). Only run for `full` mode.
283
+
284
+ ### Step 6: Alignment Check (always runs in `full` and `alignment` modes)
285
+
286
+ For `alignment` mode, this is the sole analysis. For `full` mode, run alongside Wave 1.
287
+
288
+ Read REQUIREMENTS.md and ROADMAP.md. For each requirement marked "Complete" or mapped to a completed phase:
289
+
290
+ ```bash
291
+ # Find completed requirements
292
+ grep -E "Complete|✓" .planning/REQUIREMENTS.md 2>/dev/null
293
+ ```
294
+
295
+ For each completed requirement:
296
+ - Grep the codebase for evidence it actually exists (routes, components, API endpoints)
297
+ - If not found: flag as "Claimed complete but not implemented"
298
+
299
+ Then scan for orphan features:
300
+ ```bash
301
+ # Find all routes/pages
302
+ find app -name "page.tsx" -o -name "route.ts" 2>/dev/null
303
+ # Find all API routes
304
+ find app/api -name "route.ts" 2>/dev/null
305
+ ```
306
+
307
+ Cross-reference with REQUIREMENTS.md — any route/feature NOT in requirements is flagged as "Undocumented feature".
308
+
309
+ ### Step 7: Collect and Score Findings
310
+
311
+ After all agents return:
312
+ 1. Deduplicate (same file:line from multiple agents → keep the most detailed one)
313
+ 2. Sort by severity: CRITICAL first
314
+ 3. Group by dimension: Performance, Design Alignment, UI Quality, Backend, Frontend, Planning-Code Alignment, Architecture
315
+
316
+ Count totals per severity.
317
+
318
+ ### Step 8: Write OPTIMIZE.md and Present Results
319
+
320
+ Write to `.planning/OPTIMIZE.md`:
321
+
322
+ ```markdown
323
+ ---
324
+ date: {YYYY-MM-DD HH:MM}
325
+ mode: {full|perf|ui|backend|alignment}
326
+ critical: {N}
327
+ high: {N}
328
+ medium: {N}
329
+ low: {N}
330
+ status: {clean|needs_attention|critical_issues}
331
+ ---
332
+
333
+ # Optimization Report
334
+
335
+ **Project:** {name} | **Mode:** {mode} | **Date:** {date}
336
+
337
+ ## Summary
338
+
339
+ {2-3 sentence overview}
340
+
341
+ {If status is clean: "No critical issues found. Project is in good shape."}
342
+
343
+ ## Critical Issues
344
+
345
+ | # | Dimension | Finding | Location | Fix |
346
+ |---|-----------|---------|----------|-----|
347
+ {findings}
348
+
349
+ ## High Priority
350
+
351
+ | # | Dimension | Finding | Location | Fix |
352
+ |---|-----------|---------|----------|-----|
353
+ {findings}
354
+
355
+ ## Medium Priority
356
+
357
+ | # | Dimension | Finding | Location | Fix |
358
+ |---|-----------|---------|----------|-----|
359
+ {findings}
360
+
361
+ ## Low Priority
362
+
363
+ | # | Dimension | Finding | Location | Fix |
364
+ |---|-----------|---------|----------|-----|
365
+ {findings}
366
+ ```
367
+
368
+ Commit:
369
+ ```bash
370
+ node ~/.claude/qualia-framework/bin/qualia-tools.js commit "docs: optimization report ({mode} mode, {critical} critical)" --files .planning/OPTIMIZE.md
371
+ ```
372
+
373
+ **Present results:**
374
+
375
+ If CRITICAL findings exist:
376
+ ```
377
+ {critical} critical issues found. Options:
378
+
379
+ 1. Create a fix phase in ROADMAP.md for critical + high findings
380
+ 2. Auto-fix LOW/MEDIUM findings: /qualia-optimize --fix
381
+ 3. Review full report: cat .planning/OPTIMIZE.md
382
+ ```
383
+
384
+ If no CRITICAL:
385
+ ```
386
+ Optimization complete. {total} findings ({high} high, {medium} medium, {low} low).
387
+ Report: .planning/OPTIMIZE.md
388
+
389
+ Run /qualia-optimize --fix to auto-fix LOW/MEDIUM findings.
390
+ ```
391
+
392
+ ### Step 9: --fix Mode
393
+
394
+ When `--fix` is provided:
395
+
396
+ 1. Read existing `.planning/OPTIMIZE.md` — if not found, error: "Run /qualia-optimize first"
397
+ 2. Filter to LOW and MEDIUM findings only
398
+ 3. For each finding with a clear, safe fix:
399
+ - Read the target file
400
+ - Apply the fix
401
+ - Verify it doesn't break (npx tsc --noEmit if TypeScript)
402
+ 4. Update OPTIMIZE.md: mark fixed findings, recount severities
403
+ 5. Commit changes
404
+ 6. Report what was fixed and what remains
405
+
406
+ **Never auto-fix CRITICAL or HIGH** — those require human judgment.
407
+
408
+ ### Step 10: Gap Phase Creation (if user selects option 1 from Step 8)
409
+
410
+ If user wants a fix phase for critical issues:
411
+
412
+ 1. Read ROADMAP.md, find current phase number
413
+ 2. Insert a decimal phase: `Phase {N}.1: Optimization Fixes (INSERTED)`
414
+ 3. Map CRITICAL and HIGH findings as success criteria
415
+ 4. Update STATE.md
416
+ 5. Commit
417
+ 6. Suggest: "Fix phase created. Run `/qualia-plan {N}.1`"