@kal-elsam/kairo-runtime 0.11.0 → 0.13.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.
- package/global-template/components/agent-skills/LICENSE +21 -0
- package/global-template/components/agent-skills/PROVENANCE.md +26 -0
- package/global-template/components/agent-skills/skills/context-engineering/SKILL.md +289 -0
- package/global-template/components/agent-skills/skills/frontend-ui-engineering/SKILL.md +328 -0
- package/global-template/components/agent-skills/skills/observability-and-instrumentation/SKILL.md +203 -0
- package/global-template/components/agent-skills/skills/performance-optimization/SKILL.md +396 -0
- package/global-template/components/agent-skills/skills/source-driven-development/SKILL.md +194 -0
- package/global-template/components/catalog.json +29 -0
- package/package.json +5 -2
- package/scripts/cockpit-smoke.mjs +2 -1
- package/src/cli.js +136 -8
- package/src/global/component-builders.js +3 -1
- package/src/global/components/agent-skills.js +27 -0
- package/src/global/ink/cockpit-control-center.js +107 -4
- package/src/global/ink/cockpit-scan.js +20 -2
- package/src/global/ink/ecosystem-updates-display.js +37 -0
- package/src/global/ink/launch-input.js +32 -1
- package/src/global/ink/obsidian-vault-display.js +37 -0
- package/src/global/ink/orchestrator-app.js +2 -1
- package/src/global/ink/orchestrator-state.js +17 -2
- package/src/global/ink/system-resources-display.js +109 -0
- package/src/global/ink/use-orchestrator-data.js +40 -4
- package/src/global/ink/ux/live-overview.js +11 -1
- package/src/global/mcp/kairo-mcp.js +230 -0
- package/src/global/observability/build-companion-snapshot.js +302 -0
- package/src/global/observability/build-observability-snapshot.js +24 -0
- package/src/global/observability/ecosystem-updates.js +224 -0
- package/src/global/observability/gentle-bundle-export.js +71 -0
- package/src/global/observability/gentle-bundle-import.js +122 -0
- package/src/global/observability/gentle-probe.js +155 -0
- package/src/global/observability/graphify-ops.js +133 -0
- package/src/global/observability/graphify-parse-cache.js +90 -0
- package/src/global/observability/graphify-probe.js +185 -0
- package/src/global/observability/hermes-activity.js +163 -0
- package/src/global/observability/hermes-probe.js +171 -0
- package/src/global/observability/index.js +124 -0
- package/src/global/observability/obsidian-knowledge-preview.js +214 -0
- package/src/global/observability/obsidian-knowledge-views.js +227 -0
- package/src/global/observability/obsidian-publisher.js +181 -0
- package/src/global/observability/obsidian-status.js +76 -0
- package/src/global/observability/obsidian-vault.js +259 -0
- package/src/global/observability/passive-snapshot-flight.js +93 -0
- package/src/global/observability/probe-contract.js +38 -0
- package/src/global/observability/probe-registry.js +30 -0
- package/src/global/observability/resource-advisor.js +71 -0
- package/src/global/observability/system-resources.js +171 -0
- package/src/global/runtime/alerts/alert-cli.js +31 -0
- package/src/global/runtime/alerts/alert-store.js +29 -6
- package/src/global/runtime/alerts/alert-validate.js +25 -1
- package/src/global/runtime/alerts/controlled-alert-actions.js +56 -0
- package/src/global/runtime/execution-adapters/claude.js +2 -1
- package/src/global/runtime/execution-adapters/codex.js +2 -1
- package/src/global/runtime/execution-adapters/create-execution-adapter.js +3 -14
- package/src/global/runtime/execution-adapters/cursor.js +2 -1
- package/src/global/runtime/execution-adapters/opencode.js +2 -1
- package/src/global/runtime/execution-adapters/pi.js +2 -1
- package/src/global/runtime/review/index.js +1 -1
- package/src/global/runtime/review/review-cli.js +113 -3
- package/src/global/runtime/review/review-git.js +142 -11
- package/src/global/runtime/review/review-patch.js +2 -0
- package/src/global/runtime/review/review-receipts.js +12 -7
- package/src/global/runtime/review/review-runner.js +2 -2
- package/src/global/runtime/review/review-types.js +8 -5
- package/src/global/runtime/review/review-validate.js +5 -1
- package/src/global/runtime/run-cli.js +2 -0
- package/src/global/runtime/run-manager.js +39 -18
- package/src/global/runtime/run-permissions.js +231 -0
- package/src/global/runtime/run-profile.js +2 -0
- package/src/global/runtime/run-supervisor.js +77 -37
- package/src/global/runtime/run-types.js +2 -0
- package/src/global/updates-cli.js +41 -0
package/global-template/components/agent-skills/skills/observability-and-instrumentation/SKILL.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: observability-and-instrumentation
|
|
3
|
+
description: Instruments code so production behavior is visible and diagnosable. Use when adding logging, metrics, tracing, or alerting. Use when shipping any feature that runs in production and you need evidence it works. Use when production issues are reported but you can't tell what happened from the available data.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Observability and Instrumentation
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
Code you can't observe is code you can't operate. Observability is the ability to answer "what is the system doing and why?" from the outside, using the telemetry the code emits. Instrumentation is not a post-launch add-on — it's written alongside the feature, the same way tests are. If a feature ships without telemetry, the first user-reported bug becomes archaeology instead of a query.
|
|
11
|
+
|
|
12
|
+
## When to Use
|
|
13
|
+
|
|
14
|
+
- Building any feature that will run in production
|
|
15
|
+
- Adding a new service, endpoint, background job, or external integration
|
|
16
|
+
- A production incident took too long to diagnose ("we couldn't tell what happened")
|
|
17
|
+
- Setting up or reviewing alerting rules
|
|
18
|
+
- Reviewing a PR that adds I/O, retries, queues, or cross-service calls
|
|
19
|
+
|
|
20
|
+
**NOT for:**
|
|
21
|
+
- Diagnosing a failure happening right now — use the `debugging-and-error-recovery` skill (observability is what makes that skill fast next time)
|
|
22
|
+
- Profiling and optimizing measured slowness — use the `performance-optimization` skill
|
|
23
|
+
- Launch-day monitoring checklists and rollback triggers — see the `shipping-and-launch` skill; this skill covers the instrumentation that feeds them
|
|
24
|
+
|
|
25
|
+
## Process
|
|
26
|
+
|
|
27
|
+
### 1. Define "working" before instrumenting
|
|
28
|
+
|
|
29
|
+
Telemetry without a question is noise. Before adding any instrumentation, write down 2–4 questions an on-call engineer will ask about this feature:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
FEATURE: checkout payment retry
|
|
33
|
+
QUESTIONS ON-CALL WILL ASK:
|
|
34
|
+
1. What fraction of payments succeed on first attempt vs after retry?
|
|
35
|
+
2. When a payment fails permanently, why? (provider error? timeout? validation?)
|
|
36
|
+
3. Is the payment provider slower than usual?
|
|
37
|
+
→ Every signal below must help answer one of these.
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
If you can't name the questions, you're not ready to instrument — you'll log everything and learn nothing.
|
|
41
|
+
|
|
42
|
+
### 2. Pick the right signal for each question
|
|
43
|
+
|
|
44
|
+
| Signal | Answers | Cost profile | Example |
|
|
45
|
+
|---|---|---|---|
|
|
46
|
+
| **Structured log** | "What happened in this specific case?" | Per-event; grows with traffic | `payment_failed` with provider error code |
|
|
47
|
+
| **Metric** | "How often / how fast, in aggregate?" | Fixed per series; cheap to query | p99 latency of provider calls |
|
|
48
|
+
| **Trace** | "Where did time go across services?" | Per-request; usually sampled | One slow checkout, broken down by hop |
|
|
49
|
+
|
|
50
|
+
Rule of thumb: metrics tell you **that** something is wrong, traces tell you **where**, logs tell you **why**.
|
|
51
|
+
|
|
52
|
+
### 3. Structured logging
|
|
53
|
+
|
|
54
|
+
Log events, not prose. Every log line is a JSON object with a stable event name and machine-readable fields:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// BAD: string interpolation — unqueryable, inconsistent
|
|
58
|
+
logger.info(`Payment ${id} failed for user ${userId} after ${n} retries`);
|
|
59
|
+
|
|
60
|
+
// GOOD: stable event name + structured fields
|
|
61
|
+
logger.warn({
|
|
62
|
+
event: 'payment_failed',
|
|
63
|
+
paymentId: id,
|
|
64
|
+
provider: 'stripe',
|
|
65
|
+
errorCode: err.code,
|
|
66
|
+
attempt: n,
|
|
67
|
+
}, 'payment failed');
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Log levels — use them consistently:**
|
|
71
|
+
|
|
72
|
+
| Level | Meaning | On-call action |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `error` | Invariant broken; someone may need to act | Investigate |
|
|
75
|
+
| `warn` | Degraded but handled (retry succeeded, fallback used) | Watch for trends |
|
|
76
|
+
| `info` | Significant business event (order placed, job finished) | None |
|
|
77
|
+
| `debug` | Diagnostic detail | Off in production by default |
|
|
78
|
+
|
|
79
|
+
**Correlation IDs are mandatory.** Generate (or accept) a request ID at the system boundary and attach it to every log line, span, and outbound call. Without it, you cannot reconstruct a single request from interleaved logs:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// Express: child logger per request, ID propagated downstream
|
|
83
|
+
app.use((req, res, next) => {
|
|
84
|
+
req.id = req.headers['x-request-id'] ?? crypto.randomUUID();
|
|
85
|
+
req.log = logger.child({ requestId: req.id });
|
|
86
|
+
res.setHeader('x-request-id', req.id);
|
|
87
|
+
next();
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Never log secrets, tokens, passwords, or full PII.** This is a hard rule from the `security-and-hardening` skill — telemetry pipelines are a classic data-leak path. Allowlist fields; don't log whole request bodies.
|
|
92
|
+
|
|
93
|
+
### 4. Metrics
|
|
94
|
+
|
|
95
|
+
For request-driven services, instrument **RED** on every endpoint and every external dependency: **R**ate (requests/sec), **E**rrors (failure rate), **D**uration (latency histogram, not average). For resources (queues, pools, hosts), use **USE**: **U**tilization, **S**aturation, **E**rrors.
|
|
96
|
+
|
|
97
|
+
As with tracing, the vendor-neutral path is the OpenTelemetry metrics API (same SDK and context as step 5). The example below uses Prometheus' `prom-client` — one common backend choice, not the only one; the RED/USE and cardinality rules are identical either way.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { Histogram } from 'prom-client';
|
|
101
|
+
|
|
102
|
+
const httpDuration = new Histogram({
|
|
103
|
+
name: 'http_request_duration_seconds',
|
|
104
|
+
help: 'HTTP request duration',
|
|
105
|
+
labelNames: ['method', 'route', 'status_class'], // '2xx', not '200'
|
|
106
|
+
buckets: [0.05, 0.1, 0.25, 0.5, 1, 2.5, 5],
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Cardinality is the failure mode.** Every unique label combination is a separate time series. Labels must come from small, fixed sets (route template, status class, provider name). Never use user IDs, raw URLs, error messages, or other unbounded values as labels — that belongs in logs and traces.
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
OK as label: route="/api/tasks/:id" status_class="5xx" provider="stripe"
|
|
114
|
+
NEVER a label: user_id, email, request_id, full URL, error message text
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Track averages never, percentiles always: an average hides the 1% of users having a terrible time. Use histograms and read p50/p95/p99.
|
|
118
|
+
|
|
119
|
+
### 5. Distributed tracing
|
|
120
|
+
|
|
121
|
+
Use OpenTelemetry — it's the vendor-neutral standard, and auto-instrumentation covers HTTP, gRPC, and common DB clients with near-zero code:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// tracing.ts — must be imported before anything else
|
|
125
|
+
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
126
|
+
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
|
|
127
|
+
|
|
128
|
+
const sdk = new NodeSDK({
|
|
129
|
+
serviceName: 'checkout-service',
|
|
130
|
+
instrumentations: [getNodeAutoInstrumentations()],
|
|
131
|
+
});
|
|
132
|
+
sdk.start();
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Add manual spans only around meaningful internal units of work (e.g., `applyDiscounts`, `chargeProvider`) and attach the attributes on-call will filter by. Propagate context across every async boundary — HTTP headers, queue message metadata — or the trace dies at the gap. Sample head-based at a low rate by default; keep 100% of errors if your backend supports tail sampling.
|
|
136
|
+
|
|
137
|
+
### 6. Alerting
|
|
138
|
+
|
|
139
|
+
Alert on **symptoms users feel**, not on causes:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
SYMPTOM (page-worthy): CAUSE (dashboard, not a page):
|
|
143
|
+
error rate > 1% for 5 min CPU at 85%
|
|
144
|
+
p99 latency > 2s one pod restarted
|
|
145
|
+
queue age > 10 min disk at 70%
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Cause-based alerts fire when nothing is wrong and miss failures you didn't predict. Symptom-based alerts fire exactly when users are hurt, regardless of the cause.
|
|
149
|
+
|
|
150
|
+
Rules for every alert you create:
|
|
151
|
+
|
|
152
|
+
1. **It must be actionable.** If the response is "ignore it, it self-heals", delete the alert.
|
|
153
|
+
2. **It links to a runbook** — even three lines: what it means, first query to run, escalation path.
|
|
154
|
+
3. **It has a threshold and duration** justified by the SLO or by historical data, not by a guess.
|
|
155
|
+
4. Use two severities only: **page** (user-facing, act now) and **ticket** (degradation, act this week). A third tier becomes noise that trains people to ignore everything.
|
|
156
|
+
|
|
157
|
+
### 7. Verify the telemetry itself
|
|
158
|
+
|
|
159
|
+
Instrumentation is code; it can be wrong. Before calling the work done, trigger the paths and look at the actual output:
|
|
160
|
+
|
|
161
|
+
- Force an error in staging → find it in the logs by `requestId`, confirm fields are structured (not `[object Object]`)
|
|
162
|
+
- Send test traffic → confirm metric series appear with the expected labels and sane values
|
|
163
|
+
- Follow one request across services in the tracing UI → no broken spans
|
|
164
|
+
- Fire each new alert once (lower the threshold temporarily) → confirm it reaches the right channel and the runbook link works
|
|
165
|
+
|
|
166
|
+
## Common Rationalizations
|
|
167
|
+
|
|
168
|
+
| Rationalization | Reality |
|
|
169
|
+
|---|---|
|
|
170
|
+
| "I'll add logging after it works" | "After" becomes "after the first incident", which is the most expensive moment to discover you're blind. Instrument as you build. |
|
|
171
|
+
| "More logs = more observability" | Unstructured noise makes incidents slower, not faster. Three queryable events beat three hundred prose lines. |
|
|
172
|
+
| "console.log is fine for now" | Unstructured output can't be filtered, correlated, or alerted on. The structured logger costs five extra minutes once. |
|
|
173
|
+
| "We can just look at the dashboards when something breaks" | Dashboards built without defined questions show you everything except the answer. Start from on-call questions. |
|
|
174
|
+
| "Alert on everything important, we'll tune later" | A noisy pager trains people to ignore it. The tuning never happens; the missed real page does. |
|
|
175
|
+
| "User ID as a metric label makes debugging easier" | It also makes your metrics backend fall over. High-cardinality lookups belong in logs and traces. |
|
|
176
|
+
| "Tracing is overkill for our two services" | Two services already means cross-service latency questions logs can't answer. Auto-instrumentation makes the cost trivial. |
|
|
177
|
+
|
|
178
|
+
## Red Flags
|
|
179
|
+
|
|
180
|
+
- A feature PR with retries, queues, or external calls and zero new telemetry
|
|
181
|
+
- Log lines built by string interpolation instead of structured fields
|
|
182
|
+
- No correlation/request ID — each log line is an orphan
|
|
183
|
+
- Metrics labeled with user IDs, raw URLs, or error message text (cardinality bomb)
|
|
184
|
+
- Latency tracked as an average with no percentiles
|
|
185
|
+
- Alerts that fire daily and get acknowledged without action
|
|
186
|
+
- Alerts on causes (CPU, memory) paging humans while user-facing error rate is unmonitored
|
|
187
|
+
- Secrets, tokens, or full request bodies appearing in logs
|
|
188
|
+
- "It works on my machine" as the only evidence a production feature is healthy
|
|
189
|
+
|
|
190
|
+
## Verification
|
|
191
|
+
|
|
192
|
+
After instrumenting a feature, confirm:
|
|
193
|
+
|
|
194
|
+
- [ ] The on-call questions for this feature are written down, and each signal maps to one
|
|
195
|
+
- [ ] All log output is structured (JSON), with stable event names and a correlation ID on every line
|
|
196
|
+
- [ ] No secrets, tokens, or unredacted PII in any log line (spot-check actual output)
|
|
197
|
+
- [ ] RED metrics exist for every new endpoint and every external dependency, with bounded label sets
|
|
198
|
+
- [ ] Latency is a histogram; p95/p99 are queryable
|
|
199
|
+
- [ ] A single request can be followed end-to-end in the tracing UI without broken spans
|
|
200
|
+
- [ ] Every new alert is symptom-based, has a runbook link, and was test-fired once
|
|
201
|
+
- [ ] An induced failure in staging was located via telemetry alone, without reading the source
|
|
202
|
+
|
|
203
|
+
For the at-a-glance version of this list, including the pre-launch instrumentation gate, see `references/observability-checklist.md`.
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: performance-optimization
|
|
3
|
+
description: Optimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.
|
|
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; keep or revert
|
|
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
|
+
### Step 4: Verify (Keep or Revert)
|
|
293
|
+
|
|
294
|
+
A fix is a hypothesis until you re-measure. This step decides whether it survives.
|
|
295
|
+
|
|
296
|
+
**Re-measure the way you measured the baseline:** same command, same conditions, same fixed budget (wall-clock, sample count, or request count). A baseline taken on a cold cache against a result taken on a warm one measures the cache, not your change.
|
|
297
|
+
|
|
298
|
+
**Change one thing at a time.** Three optimizations landed together produce one number, and you cannot attribute it. If they must ship together, measure each in isolation first.
|
|
299
|
+
|
|
300
|
+
**Beat the noise, not just the mean.** Repeat the measurement and compare the delta against run-to-run variance. A 3% gain inside ±5% variance is not a gain; it is a different sample.
|
|
301
|
+
|
|
302
|
+
Then decide, strictly:
|
|
303
|
+
|
|
304
|
+
| Result vs. baseline | Action |
|
|
305
|
+
|---|---|
|
|
306
|
+
| Past the threshold, tests green | **Keep.** Commit with the before/after numbers in the message. |
|
|
307
|
+
| Within noise (no measurable change) | **Revert.** |
|
|
308
|
+
| Worse | **Revert.** |
|
|
309
|
+
| Improved, but a test went red | **Revert.** A regression wearing a win's clothing. |
|
|
310
|
+
|
|
311
|
+
**"Neutral" is a revert, not a keep.** This is the step teams skip: the change is already written, throwing it away feels wasteful, so it lands unmeasured, and the codebase accretes complexity that never bought anything. Code you keep, you maintain forever. Make it pay for itself.
|
|
312
|
+
|
|
313
|
+
**Correctness gates the metric.** The suite stays green *and* the number moves. An "optimization" that wins by dropping work the product needed (skipping a validation, caching something that must be fresh, removing an `await` that was load-bearing) is a regression, not a win.
|
|
314
|
+
|
|
315
|
+
#### Log every attempt, including the reverted ones
|
|
316
|
+
|
|
317
|
+
Reverted work leaves no trace in git history, which is exactly why the same dead idea gets tried again next quarter. Keep a short ledger so a discarded idea stays discarded:
|
|
318
|
+
|
|
319
|
+
| Idea | Baseline → Result | Verdict | Why |
|
|
320
|
+
|---|---|---|---|
|
|
321
|
+
| Memoize the row component | INP 240ms → 235ms | reverted | Inside noise (±15ms). Rows weren't the bottleneck. |
|
|
322
|
+
| Virtualize the list | INP 240ms → 90ms | kept | Long tasks gone from the trace. |
|
|
323
|
+
| Preconnect to the API origin | LCP 2.8s → 2.8s | reverted | Already same-origin. |
|
|
324
|
+
|
|
325
|
+
A section in the PR description or a `PERF.md` in the repo both work. What matters is that the next person (or the next agent) reads it before proposing an experiment, and doesn't re-run one that already failed.
|
|
326
|
+
|
|
327
|
+
## Performance Budget
|
|
328
|
+
|
|
329
|
+
Set budgets and enforce them:
|
|
330
|
+
|
|
331
|
+
```
|
|
332
|
+
JavaScript bundle: < 200KB gzipped (initial load)
|
|
333
|
+
CSS: < 50KB gzipped
|
|
334
|
+
Images: < 200KB per image (above the fold)
|
|
335
|
+
Fonts: < 100KB total
|
|
336
|
+
API response time: < 200ms (p95)
|
|
337
|
+
Time to Interactive: < 3.5s on 4G
|
|
338
|
+
Lighthouse Performance score: ≥ 90
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Enforce in CI:**
|
|
342
|
+
```bash
|
|
343
|
+
# Bundle size check
|
|
344
|
+
npx bundlesize --config bundlesize.config.json
|
|
345
|
+
|
|
346
|
+
# Lighthouse CI
|
|
347
|
+
npx lhci autorun
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## See Also
|
|
351
|
+
|
|
352
|
+
For detailed performance checklists, optimization commands, and anti-pattern reference, see `references/performance-checklist.md`.
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
## Common Rationalizations
|
|
356
|
+
|
|
357
|
+
| Rationalization | Reality |
|
|
358
|
+
|---|---|
|
|
359
|
+
| "We'll optimize later" | Performance debt compounds. Fix obvious anti-patterns now, defer micro-optimizations. |
|
|
360
|
+
| "It's fast on my machine" | Your machine isn't the user's. Profile on representative hardware and networks. |
|
|
361
|
+
| "This optimization is obvious" | If you didn't measure, you don't know. Profile first. |
|
|
362
|
+
| "Users won't notice 100ms" | Research shows 100ms delays impact conversion rates. Users notice more than you think. |
|
|
363
|
+
| "The framework handles performance" | Frameworks prevent some issues but can't fix N+1 queries or oversized bundles. |
|
|
364
|
+
| "It didn't help much, but it doesn't hurt" | Neutral changes are a revert. You pay maintenance on them forever and got nothing back. |
|
|
365
|
+
| "We already wrote it, may as well keep it" | Sunk cost. The measurement doesn't care how long the change took to write. |
|
|
366
|
+
| "The improvement is obvious, no need to re-measure" | Then re-measuring is cheap and proves it. Unmeasured wins are how neutral complexity lands. |
|
|
367
|
+
|
|
368
|
+
## Red Flags
|
|
369
|
+
|
|
370
|
+
- Optimization without profiling data to justify it
|
|
371
|
+
- N+1 query patterns in data fetching
|
|
372
|
+
- List endpoints without pagination
|
|
373
|
+
- Images without dimensions, lazy loading, or responsive sizes
|
|
374
|
+
- Bundle size growing without review
|
|
375
|
+
- No performance monitoring in production
|
|
376
|
+
- `React.memo` and `useMemo` everywhere (overusing is as bad as underusing)
|
|
377
|
+
- Optimizations kept without a re-measurement that justifies them
|
|
378
|
+
- Several optimizations bundled into one measurement, so no single change can be attributed
|
|
379
|
+
- A "win" that required a test to be changed, skipped, or deleted
|
|
380
|
+
- The same failed optimization attempted more than once because nobody recorded the first attempt
|
|
381
|
+
|
|
382
|
+
## Verification
|
|
383
|
+
|
|
384
|
+
After any performance-related change:
|
|
385
|
+
|
|
386
|
+
- [ ] Before and after measurements exist (specific numbers)
|
|
387
|
+
- [ ] The result was re-measured the same way as the baseline (same command, same conditions)
|
|
388
|
+
- [ ] The improvement exceeds run-to-run variance, not just the mean
|
|
389
|
+
- [ ] Changes that didn't beat the baseline were reverted, not kept as neutral
|
|
390
|
+
- [ ] Attempts are logged, kept and reverted alike, so a dead idea isn't re-run
|
|
391
|
+
- [ ] The specific bottleneck is identified and addressed
|
|
392
|
+
- [ ] Core Web Vitals are within "Good" thresholds
|
|
393
|
+
- [ ] Bundle size hasn't increased significantly
|
|
394
|
+
- [ ] No N+1 queries in new data fetching code
|
|
395
|
+
- [ ] Performance budget passes in CI (if configured)
|
|
396
|
+
- [ ] Existing tests still pass (optimization didn't break behavior)
|