--- name: web-performance description: >- Use when the user reports jank, dropped frames, janky scrolling, slow click or typing response, poor INP, slow LCP, layout shifts (CLS), unresponsive pages, or asks to debug rendering, animation, or interaction performance in a browser app. --- # Web Performance ## Overview Browser performance debugging via `PerformanceObserver`, with **LoAF (`long-animation-frame`) as the primary signal**. LoAF is the only entry type that, in one record, attributes a slow frame to a specific `sourceURL` + `sourceFunctionName` + `sourceCharPosition` + `invokerType`, with per-script `forcedStyleAndLayoutDuration` (sync reflow), `pauseDuration` (sync XHR / `alert`), and `blockingDuration`. Code inspection and `performance.now()` cannot reach this. **Start with LoAFs, conclude from LoAFs.** ## When to use Symptoms: - Jank, dropped frames, janky scroll/swipe, complaints about frame rate - Slow click / keypress / touch response, "unresponsive" complaints, poor INP - Slow LCP, layout shifts (CLS) - Animation stutter, transition jank, expensive renders during interaction **Do NOT use for:** - Backend / non-browser perf — use raw NDJSON file appends from your server runtime - Memory leaks, bundle-size regressions — heap snapshots / bundle analyzers - Logic bugs unrelated to timing — use raw fetch instrumentation ## Core pattern **Before — manual `performance.now()` (wrong):** ```js const t0 = performance.now(); drawSeries(data); console.log("drawSeries took", performance.now() - t0); ``` Tells you a number. Doesn't tell you the function caused a long frame, what scheduled it, or whether it forced sync layout. Requires you to _already suspect_ `drawSeries`. **After — LoAF observer (right):** ```js new PerformanceObserver((list) => { for (const loaf of list.getEntries()) send(loaf); }).observe({ type: "long-animation-frame", buffered: true }); ``` Reports every frame `> 50ms` across the whole page, with `scripts[].sourceURL` + `sourceFunctionName` + `sourceCharPosition` + `invokerType` + `forcedStyleAndLayoutDuration` for each script that ran in the frame. **You don't need to know where the bug is in advance.** ## Workflow 1. Generate 3-5 hypotheses about what's slow and where. 2. Start the logging server (Implementation → STEP 0). 3. Inject the LoAF observer as the first script in `
` or top of SPA entry. 4. Reproduce — automate via Playwright/Puppeteer if possible; otherwise give numbered steps and ask the user to confirm in their UI (do NOT ask them to type "done"). 5. Clear the log file before each run via the deletion tool (NOT `rm`). 6. Analyze LoAFs first; consult secondary signals only if LoAF is silent. Mark hypotheses CONFIRMED / REJECTED / INCONCLUSIVE with cited entries. 7. Fix only with 100% confidence. Keep instrumentation in place; tag post-fix runs with `runId="post-fix"`. 8. Verify by re-running and comparing before/after LoAFs with cited lines. If failed, revert rejected-hypothesis code (keep instrumentation), generate new hypotheses, iterate. 9. Cleanup — remove the `#region debug log` block only after verified success + explicit user confirmation. ## Implementation ### STEP 0: Start the logging server (background-only) ```bash npx debug-agent@latest --json --daemon ``` `--daemon` forks the server into a detached process and exits immediately (your shell unblocks instantly — no need for `&`/`nohup`). `--json` makes the parent emit one machine-readable JSON line (without it you get a colored spinner). It prints one JSON line on startup: ```json { "sessionId": "a1b2c3", "endpoint": "http://127.0.0.1:54321/ingest/a1b2c3", "logPath": "/tmp/debug-agent/debug-a1b2c3.log" } ``` Capture `endpoint` (POST traces here), `logPath` (NDJSON written here on macOS at `/var/folders/.../T/debug-agent/debug-