spec-lite 1.1.0 → 1.1.2

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,350 @@
1
+ ---
2
+ name: performance-optimization
3
+ description: Optimizes application performance. Use when performance requirements exist, when you suspect performance regressions, or when Core Web Vitals or load times need improvement. Use when profiling reveals bottlenecks that need fixing.
4
+ ---
5
+
6
+ # Performance Optimization
7
+
8
+ ## Overview
9
+
10
+ Measure before optimizing. Performance work without measurement is guessing — and guessing leads to premature optimization that adds complexity without improving what matters. Profile first, identify the actual bottleneck, fix it, measure again. Optimize only what measurements prove matters.
11
+
12
+ ## When to Use
13
+
14
+ - Performance requirements exist in the spec (load time budgets, response time SLAs)
15
+ - Users or monitoring report slow behavior
16
+ - Core Web Vitals scores are below thresholds
17
+ - You suspect a change introduced a regression
18
+ - Building features that handle large datasets or high traffic
19
+
20
+ **When NOT to use:** Don't optimize before you have evidence of a problem. Premature optimization adds complexity that costs more than the performance it gains.
21
+
22
+ ## Core Web Vitals Targets
23
+
24
+ | Metric | Good | Needs Improvement | Poor |
25
+ |--------|------|-------------------|------|
26
+ | **LCP** (Largest Contentful Paint) | ≤ 2.5s | ≤ 4.0s | > 4.0s |
27
+ | **INP** (Interaction to Next Paint) | ≤ 200ms | ≤ 500ms | > 500ms |
28
+ | **CLS** (Cumulative Layout Shift) | ≤ 0.1 | ≤ 0.25 | > 0.25 |
29
+
30
+ ## The Optimization Workflow
31
+
32
+ ```
33
+ 1. MEASURE → Establish baseline with real data
34
+ 2. IDENTIFY → Find the actual bottleneck (not assumed)
35
+ 3. FIX → Address the specific bottleneck
36
+ 4. VERIFY → Measure again, confirm improvement
37
+ 5. GUARD → Add monitoring or tests to prevent regression
38
+ ```
39
+
40
+ ### Step 1: Measure
41
+
42
+ Two complementary approaches — use both:
43
+
44
+ - **Synthetic (Lighthouse, DevTools Performance tab):** Controlled conditions, reproducible. Best for CI regression detection and isolating specific issues.
45
+ - **RUM (web-vitals library, CrUX):** Real user data in real conditions. Required to validate that a fix actually improved user experience.
46
+
47
+ **Frontend:**
48
+ ```bash
49
+ # Synthetic: Lighthouse in Chrome DevTools (or CI)
50
+ # Chrome DevTools → Performance tab → Record
51
+ # Chrome DevTools MCP → Performance trace
52
+
53
+ # RUM: Web Vitals library in code
54
+ import { onLCP, onINP, onCLS } from 'web-vitals';
55
+
56
+ onLCP(console.log);
57
+ onINP(console.log);
58
+ onCLS(console.log);
59
+ ```
60
+
61
+ **Backend:**
62
+ ```bash
63
+ # Response time logging
64
+ # Application Performance Monitoring (APM)
65
+ # Database query logging with timing
66
+
67
+ # Simple timing
68
+ console.time('db-query');
69
+ const result = await db.query(...);
70
+ console.timeEnd('db-query');
71
+ ```
72
+
73
+ ### Where to Start Measuring
74
+
75
+ Use the symptom to decide what to measure first:
76
+
77
+ ```
78
+ What is slow?
79
+ ├── First page load
80
+ │ ├── Large bundle? --> Measure bundle size, check code splitting
81
+ │ ├── Slow server response? --> Measure TTFB in DevTools Network waterfall
82
+ │ │ ├── DNS long? --> Add dns-prefetch / preconnect for known origins
83
+ │ │ ├── TCP/TLS long? --> Enable HTTP/2, check edge deployment, keep-alive
84
+ │ │ └── Waiting (server) long? --> Profile backend, check queries and caching
85
+ │ └── Render-blocking resources? --> Check network waterfall for CSS/JS blocking
86
+ ├── Interaction feels sluggish
87
+ │ ├── UI freezes on click? --> Profile main thread, look for long tasks (>50ms)
88
+ │ ├── Form input lag? --> Check re-renders, controlled component overhead
89
+ │ └── Animation jank? --> Check layout thrashing, forced reflows
90
+ ├── Page after navigation
91
+ │ ├── Data loading? --> Measure API response times, check for waterfalls
92
+ │ └── Client rendering? --> Profile component render time, check for N+1 fetches
93
+ └── Backend / API
94
+ ├── Single endpoint slow? --> Profile database queries, check indexes
95
+ ├── All endpoints slow? --> Check connection pool, memory, CPU
96
+ └── Intermittent slowness? --> Check for lock contention, GC pauses, external deps
97
+ ```
98
+
99
+ ### Step 2: Identify the Bottleneck
100
+
101
+ Common bottlenecks by category:
102
+
103
+ **Frontend:**
104
+
105
+ | Symptom | Likely Cause | Investigation |
106
+ |---------|-------------|---------------|
107
+ | Slow LCP | Large images, render-blocking resources, slow server | Check network waterfall, image sizes |
108
+ | High CLS | Images without dimensions, late-loading content, font shifts | Check layout shift attribution |
109
+ | Poor INP | Heavy JavaScript on main thread, large DOM updates | Check long tasks in Performance trace |
110
+ | Slow initial load | Large bundle, many network requests | Check bundle size, code splitting |
111
+
112
+ **Backend:**
113
+
114
+ | Symptom | Likely Cause | Investigation |
115
+ |---------|-------------|---------------|
116
+ | Slow API responses | N+1 queries, missing indexes, unoptimized queries | Check database query log |
117
+ | Memory growth | Leaked references, unbounded caches, large payloads | Heap snapshot analysis |
118
+ | CPU spikes | Synchronous heavy computation, regex backtracking | CPU profiling |
119
+ | High latency | Missing caching, redundant computation, network hops | Trace requests through the stack |
120
+
121
+ ### Step 3: Fix Common Anti-Patterns
122
+
123
+ #### N+1 Queries (Backend)
124
+
125
+ ```typescript
126
+ // BAD: N+1 — one query per task for the owner
127
+ const tasks = await db.tasks.findMany();
128
+ for (const task of tasks) {
129
+ task.owner = await db.users.findUnique({ where: { id: task.ownerId } });
130
+ }
131
+
132
+ // GOOD: Single query with join/include
133
+ const tasks = await db.tasks.findMany({
134
+ include: { owner: true },
135
+ });
136
+ ```
137
+
138
+ #### Unbounded Data Fetching
139
+
140
+ ```typescript
141
+ // BAD: Fetching all records
142
+ const allTasks = await db.tasks.findMany();
143
+
144
+ // GOOD: Paginated with limits
145
+ const tasks = await db.tasks.findMany({
146
+ take: 20,
147
+ skip: (page - 1) * 20,
148
+ orderBy: { createdAt: 'desc' },
149
+ });
150
+ ```
151
+
152
+ #### Missing Image Optimization (Frontend)
153
+
154
+ ```html
155
+ <!-- BAD: No dimensions, no format optimization -->
156
+ <img src="/hero.jpg" />
157
+
158
+ <!-- GOOD: Hero / LCP image — art direction + resolution switching, high priority -->
159
+ <!--
160
+ Two techniques combined:
161
+ - Art direction (media): different crop/composition per breakpoint
162
+ - Resolution switching (srcset + sizes): right file size per screen density
163
+ -->
164
+ <picture>
165
+ <!-- Mobile: portrait crop (8:10) -->
166
+ <source
167
+ media="(max-width: 767px)"
168
+ srcset="/hero-mobile-400.avif 400w, /hero-mobile-800.avif 800w"
169
+ sizes="100vw"
170
+ width="800"
171
+ height="1000"
172
+ type="image/avif"
173
+ />
174
+ <source
175
+ media="(max-width: 767px)"
176
+ srcset="/hero-mobile-400.webp 400w, /hero-mobile-800.webp 800w"
177
+ sizes="100vw"
178
+ width="800"
179
+ height="1000"
180
+ type="image/webp"
181
+ />
182
+ <!-- Desktop: landscape crop (2:1) -->
183
+ <source
184
+ srcset="/hero-800.avif 800w, /hero-1200.avif 1200w, /hero-1600.avif 1600w"
185
+ sizes="(max-width: 1200px) 100vw, 1200px"
186
+ width="1200"
187
+ height="600"
188
+ type="image/avif"
189
+ />
190
+ <source
191
+ srcset="/hero-800.webp 800w, /hero-1200.webp 1200w, /hero-1600.webp 1600w"
192
+ sizes="(max-width: 1200px) 100vw, 1200px"
193
+ width="1200"
194
+ height="600"
195
+ type="image/webp"
196
+ />
197
+ <img
198
+ src="/hero-desktop.jpg"
199
+ width="1200"
200
+ height="600"
201
+ fetchpriority="high"
202
+ alt="Hero image description"
203
+ />
204
+ </picture>
205
+
206
+ <!-- GOOD: Below-the-fold image — lazy loaded + async decoding -->
207
+ <img
208
+ src="/content.webp"
209
+ width="800"
210
+ height="400"
211
+ loading="lazy"
212
+ decoding="async"
213
+ alt="Content image description"
214
+ />
215
+ ```
216
+
217
+ #### Unnecessary Re-renders (React)
218
+
219
+ ```tsx
220
+ // BAD: Creates new object on every render, causing children to re-render
221
+ function TaskList() {
222
+ return <TaskFilters options={{ sortBy: 'date', order: 'desc' }} />;
223
+ }
224
+
225
+ // GOOD: Stable reference
226
+ const DEFAULT_OPTIONS = { sortBy: 'date', order: 'desc' } as const;
227
+ function TaskList() {
228
+ return <TaskFilters options={DEFAULT_OPTIONS} />;
229
+ }
230
+
231
+ // Use React.memo for expensive components
232
+ const TaskItem = React.memo(function TaskItem({ task }: Props) {
233
+ return <div>{/* expensive render */}</div>;
234
+ });
235
+
236
+ // Use useMemo for expensive computations
237
+ function TaskStats({ tasks }: Props) {
238
+ const stats = useMemo(() => calculateStats(tasks), [tasks]);
239
+ return <div>{stats.completed} / {stats.total}</div>;
240
+ }
241
+ ```
242
+
243
+ #### Large Bundle Size
244
+
245
+ ```typescript
246
+ // Modern bundlers (Vite, webpack 5+) handle named imports with tree-shaking automatically,
247
+ // provided the dependency ships ESM and is marked `sideEffects: false` in package.json.
248
+ // Profile before changing import styles — the real gains come from splitting and lazy loading.
249
+
250
+ // GOOD: Dynamic import for heavy, rarely-used features
251
+ const ChartLibrary = lazy(() => import('./ChartLibrary'));
252
+
253
+ // GOOD: Route-level code splitting wrapped in Suspense
254
+ const SettingsPage = lazy(() => import('./pages/Settings'));
255
+
256
+ function App() {
257
+ return (
258
+ <Suspense fallback={<Spinner />}>
259
+ <SettingsPage />
260
+ </Suspense>
261
+ );
262
+ }
263
+ ```
264
+
265
+ #### Missing Caching (Backend)
266
+
267
+ ```typescript
268
+ // Cache frequently-read, rarely-changed data
269
+ const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
270
+ let cachedConfig: AppConfig | null = null;
271
+ let cacheExpiry = 0;
272
+
273
+ async function getAppConfig(): Promise<AppConfig> {
274
+ if (cachedConfig && Date.now() < cacheExpiry) {
275
+ return cachedConfig;
276
+ }
277
+ cachedConfig = await db.config.findFirst();
278
+ cacheExpiry = Date.now() + CACHE_TTL;
279
+ return cachedConfig;
280
+ }
281
+
282
+ // HTTP caching headers for static assets
283
+ app.use('/static', express.static('public', {
284
+ maxAge: '1y', // Cache for 1 year
285
+ immutable: true, // Never revalidate (use content hashing in filenames)
286
+ }));
287
+
288
+ // Cache-Control for API responses
289
+ res.set('Cache-Control', 'public, max-age=300'); // 5 minutes
290
+ ```
291
+
292
+ ## Performance Budget
293
+
294
+ Set budgets and enforce them:
295
+
296
+ ```
297
+ JavaScript bundle: < 200KB gzipped (initial load)
298
+ CSS: < 50KB gzipped
299
+ Images: < 200KB per image (above the fold)
300
+ Fonts: < 100KB total
301
+ API response time: < 200ms (p95)
302
+ Time to Interactive: < 3.5s on 4G
303
+ Lighthouse Performance score: ≥ 90
304
+ ```
305
+
306
+ **Enforce in CI:**
307
+ ```bash
308
+ # Bundle size check
309
+ npx bundlesize --config bundlesize.config.json
310
+
311
+ # Lighthouse CI
312
+ npx lhci autorun
313
+ ```
314
+
315
+ ## See Also
316
+
317
+ For detailed performance checklists, optimization commands, and anti-pattern reference, see `references/performance-checklist.md`.
318
+
319
+
320
+ ## Common Rationalizations
321
+
322
+ | Rationalization | Reality |
323
+ |---|---|
324
+ | "We'll optimize later" | Performance debt compounds. Fix obvious anti-patterns now, defer micro-optimizations. |
325
+ | "It's fast on my machine" | Your machine isn't the user's. Profile on representative hardware and networks. |
326
+ | "This optimization is obvious" | If you didn't measure, you don't know. Profile first. |
327
+ | "Users won't notice 100ms" | Research shows 100ms delays impact conversion rates. Users notice more than you think. |
328
+ | "The framework handles performance" | Frameworks prevent some issues but can't fix N+1 queries or oversized bundles. |
329
+
330
+ ## Red Flags
331
+
332
+ - Optimization without profiling data to justify it
333
+ - N+1 query patterns in data fetching
334
+ - List endpoints without pagination
335
+ - Images without dimensions, lazy loading, or responsive sizes
336
+ - Bundle size growing without review
337
+ - No performance monitoring in production
338
+ - `React.memo` and `useMemo` everywhere (overusing is as bad as underusing)
339
+
340
+ ## Verification
341
+
342
+ After any performance-related change:
343
+
344
+ - [ ] Before and after measurements exist (specific numbers)
345
+ - [ ] The specific bottleneck is identified and addressed
346
+ - [ ] Core Web Vitals are within "Good" thresholds
347
+ - [ ] Bundle size hasn't increased significantly
348
+ - [ ] No N+1 queries in new data fetching code
349
+ - [ ] Performance budget passes in CI (if configured)
350
+ - [ ] Existing tests still pass (optimization didn't break behavior)
@@ -0,0 +1,30 @@
1
+ ---
2
+ name: review
3
+ description: Review code sau bước /build theo năm trục — correctness, readability, architecture, security, performance. Dùng sau khi hoàn thành một hoặc nhiều task trong todo.md. Đối chiếu implementation với spec.md và tech.md để xác nhận đúng yêu cầu.
4
+ ---
5
+
6
+ # Review
7
+
8
+ ## Tổng quan
9
+
10
+ Invoke `code-review-and-quality` để review toàn bộ thay đổi sau `/build`.
11
+
12
+ Trước khi bắt đầu review, load context từ working integration:
13
+
14
+ 1. `specs/integrations/{slug}/spec.md` — yêu cầu nghiệp vụ, acceptance criteria
15
+ 2. `specs/integrations/{slug}/tech.md` — thiết kế kỹ thuật, API contract, data model
16
+ 3. `specs/integrations/{slug}/plan.md` — task breakdown và acceptance criteria chi tiết
17
+ 4. `specs/integrations/{slug}/todo.md` — task nào đã được implement (`[x]`)
18
+
19
+ Chạy `git diff main...HEAD` để lấy thay đổi cần review.
20
+
21
+ ## Xác định integration
22
+
23
+ **Nếu có ARGUMENT:** Parse số thứ tự (`2`, `002`) hoặc slug (`002-implement-todo`). Quét `specs/integrations/*/todo.md`, dùng integration khớp.
24
+
25
+ **Nếu không có ARGUMENT:** Liệt kê integrations có task đã done, cho user chọn.
26
+
27
+ ## Sau khi review
28
+
29
+ - **Có Critical/Important:** Dừng, không tiếp tục `/build`. Sửa và re-review.
30
+ - **Chỉ Suggestion hoặc không có finding:** Approve, tiếp tục `/build` task tiếp theo.