@babylonjs-toolkit/agent 1.1.68 → 1.1.76

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.
@@ -1,10 +1,10 @@
1
1
  ---
2
2
  name: bt-profile
3
- description: "The Babylon Toolkit Profile Skill finds and fixes what makes a BabylonJS / Babylon Toolkit web game slow. It drives the running game in a real Chrome over the DevTools protocol through a repeatable scenario (a cold load, a level start, a countdown, a boss fight), records a full CPU profile, per-phase allocation profiles, every frame's CPU work and Babylon timers, and maps every sampled millisecond back to the original source line and its owner — your game code, a project-owned script bundle (e.g. the Unity exporter's project bundle), the Babylon Toolkit runtime, BabylonJS, Havok, shader translation, React, the browser. It charges library work to the code of yours that triggered it, splits everything by phase and by second, then diagnoses each bottleneck, fixes it one at a time, re-measures the same scenario to prove the gain (reverting anything that does not help), and writes a code-dashboard report to `.agents/profile/<slug>/dashboard.html` (everything the slug's runs produced sits beside it in `.agents/profile/<slug>/data/`). Use when the user says the game is slow, stutters, crawls at a moment (load, start, many particles), or wants a performance analysis (e.g. `/bt-profile the race start crawls for a minute`, `/bt-profile --analyze`, `/bt-profile http://localhost:5173/play --scenario level-start`)."
3
+ description: "The Babylon Toolkit Profile Skill finds what makes a web app slow and lists the fixes (applying them one at a time with --auto-fix) — built for BabylonJS / Babylon Toolkit games, and just as usable on any front end with no Babylon in it (point it at a URL and the project folder). It drives the running game in a real Chrome over the DevTools protocol through a repeatable scenario (a cold load, a level start, a countdown, a boss fight), records a full CPU profile, per-phase allocation profiles, every frame's CPU work and Babylon timers, and maps every sampled millisecond back to the original source line and its owner — your game code, a project-owned script bundle (e.g. the Unity exporter's project bundle), the Babylon Toolkit runtime, BabylonJS, Havok, shader translation, React, the browser. It charges library work to the code of yours that triggered it, splits everything by phase and by second, then diagnoses each bottleneck and lists the fixes — each with its expected gain and risk, ready to spec one by one or as a group with bt-spec — and, with --auto-fix, applies them one at a time, re-measuring the same scenario to prove each gain (reverting anything that does not help), and writes a code-dashboard report to `.agents/profile/<slug>/dashboard.html` (everything the slug's runs produced sits beside it in `.agents/profile/<slug>/data/`). Use when the user says the game is slow, stutters, crawls at a moment (load, start, many particles), or wants a performance analysis (e.g. `/bt-profile the race start crawls for a minute`, `/bt-profile --auto-fix the race start crawls`, `/bt-profile http://localhost:5173/play --scenario level-start`)."
4
4
  allowed-tools: Read, Grep, Glob, Edit, Write, Bash
5
5
  ---
6
6
 
7
- You are an EXPERT BabylonJS, Babylon Toolkit and browser performance engineer. Always adhere to any rules or requirements set out in the project's agent instructions (AGENTS.md / CLAUDE.md / .github/copilot-instructions.md).
7
+ You are an EXPERT BabylonJS, Babylon Toolkit and browser performance engineer. The skill is built for Babylon Toolkit projects but profiles **any web app**: a page with no Babylon engine gets everything except the engine-only numbers (see *Any web app*). Always adhere to any rules or requirements set out in the project's agent instructions (AGENTS.md / CLAUDE.md / .github/copilot-instructions.md).
8
8
 
9
9
  Use the user's message after the skill name as the `arguments`.
10
10
 
@@ -14,17 +14,23 @@ Use the user's message after the skill name as the `arguments`.
14
14
 
15
15
  ```
16
16
  /bt-profile [<brief: what is slow, when>] [--project:<path>] [--url:<game URL>] [--scenario:<name|path>] [--slug:<name>]
17
- [--analyze] [--snapshots] [--no-warm] [--settle:<seconds>] [--runs:<n>] [--trace]
17
+ [--auto-fix] [--snapshots] [--watch] [--gpu-cold] [--no-warm] [--settle:<seconds>] [--runs:<n>] [--trace]
18
+ [--stop[:<slug>]] [--resume[:<slug>]] [--restart[:<slug>]]
18
19
  [--max-fixes:<n>] [--target-fps:<n>] [--dpr:<n>] [--args:key=value,…]
19
20
  ```
20
21
 
21
22
  - **`<brief>`** — the user's words about the slowdown ("the race start crawls for a minute", "stutters when particles spawn"). It decides the scenario and which phases matter most.
22
23
  - **`--project:`** — the web project to profile: its folder, or any folder or file inside it (absolute, `~/…`, or relative to the working directory). Default: the working directory's project (see **Which project** below).
23
- - **`--url:`** — the page that runs the game. Default: `profile.config.json` → `url`, else the dev server's `/play` (or `/`).
24
+ - **`--url:`** — the page the scenario opens. Default: `profile.config.json` → `url`, else the page a player opens first on the dev server (usually `/`) — the scenario then clicks through to the slow moment the way a player does.
24
25
  - **`--scenario:`** — the slug's `data/scenario.mjs`, or a path to copy into it. Default: the slug's own; with none, write one (Phase 0).
25
26
  - **`--slug:`** — the profiled moment, and its folder: `.agents/profile/<slug>/` holds `dashboard.html` and `data/` (config, scenario, findings, runs, logs). Default: the scenario's name (e.g. `race-start`). A new moment gets a new slug; never mix two scenarios' runs in one slug.
26
- - **`--analyze`** — measure, diagnose and report only. Changes no code.
27
+ - **`--auto-fix`** — also apply the fixes: one at a time, each re-measured and kept only when it wins (Phase 3). **Off by default**: a plain run measures and diagnoses, changes no code, and lists every fix. Either way the report has **The fixes** — every fix with its status, expected gain and risk — so the user can spec them one by one or as a group with `bt-spec` (the dashboard copies the command; `findings.md` beside it carries each fix's cause, evidence, code, proposed change, gain, risk and how to verify it). `--no-fix` / `--analyze` from earlier versions mean the default.
28
+ - **`--stop[:<slug>]`** — stop a running profile: ends its capture and Chrome (`progress.mjs stop`), marks the run stopped on the dashboard, rebuilds the report from what was captured. Without a slug: the one running profile in the project (ask when there are several).
29
+ - **`--resume[:<slug>]`** — continue a stopped profile where it stopped (`progress.mjs resume` prints the stage and the fixes' states): skip what is done, re-measure a fix left half-way, carry on. The dashboard shows it as the next run number.
30
+ - **`--restart[:<slug>]`** — profile the slug again from a fresh baseline with the same config and scenario (after code changes, a stopped run you do not want to continue, or a finished one): `progress.mjs restart` moves the earlier runs, findings and report to `data/archive/<date-time>/` — compare with them in the new report.
27
31
  - **`--snapshots`** — phase screenshots plus a filmstrip (about one small frame per second, lined up under the frame-rate chart), and the before/after look comparison. **Off by default**: a full-size screenshot stalls the game for a moment and would show up in the very profile it explains. Turn it on for the visual verification runs, or whenever the user asks.
32
+ - **`--watch`** — show the Chrome window while the profile runs, so the user can see every capture as it happens (passes `--watch true` to every `profile.mjs` run). Off by default: headless captures are the steadiest. A watched window measures like a real browser (vsync, compositing), so compare watched runs with watched runs; tell the user to keep the window uncovered and not to click or type in it unless the scenario hands them the controls.
33
+ - **`--gpu-cold`** — make every capture a true first visit for the GPU (passes `--gpu-cold true` to `profile.mjs`): each fragment shader gets a never-taken branch unique to the load, so no compiled pipeline from an earlier visit is reused. A fresh Chrome profile alone is only browser-cold — on macOS the compiled Metal pipelines live in a system cache outside it, so without this only the first capture after a shader change is GPU-cold. Use it whenever the complaint is about a first load, a cold start, or a stutter that "only happens the first time". Nothing on disk is touched.
28
34
  - **`--no-warm`** — skip the second, warm load in the same browser (on by default for the baseline: cold vs warm separates first-use costs from steady per-frame work).
29
35
  - **`--settle:`** — seconds recorded after the scenario's main event (passed to the scenario as `--args settle=`).
30
36
  - **`--runs:`** — baseline captures used to measure run-to-run noise (default 2).
@@ -38,35 +44,38 @@ Use the user's message after the skill name as the `arguments`.
38
44
  # What this skill guarantees
39
45
 
40
46
  1. **Measured, never guessed.** Every finding cites the profile: the function, its owner, its ms per frame, the phase, and the code of yours behind it. No fix is made on a hunch.
41
- 2. **One fix at a time, proven.** Each fix is re-measured with the same scenario; it is kept only when its target cost falls and the phase it serves gets faster beyond the measured noise. Otherwise it is reverted, and the report says so.
47
+ 2. **Fixes are listed; applied only with `--auto-fix`, one at a time, proven.** Each applied fix is re-measured with the same scenario; it is kept only when its target cost falls and the phase it serves gets faster beyond the measured noise. Otherwise it is reverted, and the report says so.
42
48
  3. **The user's work is safe.** Uncommitted changes in the working tree are recorded first and never reverted, stashed or committed. Fixes are made as small, reviewable edits; the report carries each diff. Nothing is committed unless the user asks.
43
49
  4. **It still works and looks the same.** After the fixes: typecheck / build, the project's tests, a clean console, and (`--snapshots`) the same phases screenshotted before and after.
44
50
  5. **Libraries are not patched.** BabylonJS, Havok, React and other node_modules code is never edited. When the time is spent inside a library, the fix changes how your code uses it (call it less, on less, configure it, cache, freeze, pool) — or, for a toolkit / engine defect, the report states it with evidence for its owner.
45
51
 
46
52
  # Talk to the user while you work
47
53
 
48
- Long runs lose people. At every phase change, post one short plain-language update: **Done** (with numbers) · **Doing now** · **Next**. When a finding is confirmed, say it in one sentence a non-specialist understands ("Physics copies every body's position back every step — 7 ms of each frame").
54
+ Long runs lose people. At every phase change, post one short plain-language update: **Done** (with numbers) · **Doing now** · **Next**. **In chat too, never go more than about 5 minutes without a message** while working: before starting anything in the background, say what it is and how long it takes; when it finishes, say what it showed before moving on; when the user writes mid-run, answer them first. A silent agent looks stuck. When a finding is confirmed, say it in one sentence a non-specialist understands ("Physics copies every body's position back every step — 7 ms of each frame").
49
55
 
50
56
  # Live progress — the dashboard follows the run
51
57
 
52
- The user watches `dashboard.html` while the profile runs, not only after it. From the run's first minute the page shows **what is happening right now**, the stages (Preflight → Baseline → Diagnose → Fix → Verify → Report), every fix with its status and before → after, and the numbers so far; it checks for changes every 10 s and, when the run ends, reloads into the finished report (where the run folds into a *How this profile ran* summary). All of it comes from `data/progress.js`, which **only `progress.mjs` writes** — never edit it by hand:
58
+ The user watches `dashboard.html` while the profile runs, not only after it. From the run's first minute the page shows **what is happening right now**, the stages (Preflight → Baseline → Diagnose → Fix → Verify → Report), every fix with its status and before → after, and the numbers so far; **while a run is going the page shows its progress only — the findings, charts and tables stay hidden (and Print / Download PDF with them) until it ends**; it checks for changes every 10 s and, when the run ends, reloads into the finished report (where the run folds into a *How this profile ran* summary). All of it comes from `data/progress.js`, which **only `progress.mjs` writes** — never edit it by hand:
53
59
 
54
60
  ```bash
55
- P="node $TOOLS/progress.mjs"; S="$PROF/$SLUG" # S is the slug folder (it holds dashboard.html and data/)
56
- $P start "$S" --title "<Project> — <scenario> performance" --brief "<the user's words>" --project "$PROJ" --url "<url>" [--mode analyze]
57
- $P stage "$S" baseline [--note "2 cold captures + 1 warm, 90 s settle"] # starting a stage finishes the one before
58
- $P now "$S" "Capturing baseline 1 of 2 — about 3 min" # what is happening this minute
59
- $P metric "$S" "Race start FPS" "24" --note "baseline · target 60"
60
- $P fix "$S" F1 --title "Stop forced world-matrix recomputes" --status trying # then measuring → kept | reverted
61
- $P fix "$S" F1 --status kept --before "24 fps" --after "41 fps" --note "−14 ms/frame in the countdown"
62
- $P note "$S" "Noise floor ±1.8 fps — a fix must beat it" # a milestone
63
- $P end "$S" --summary "<the verdict in one sentence>" # or --status stopped|failed with why
61
+ node "$TOOLS/progress.mjs" start "$S" --title "<Project> — <scenario> performance" --brief "<the user's words>" --project "$PROJ" --url "<url>" --open true [--mode auto-fix]
62
+ node "$TOOLS/progress.mjs" set "$S" --url "<url>" # details found after start (the URL, once the code is read)
63
+ node "$TOOLS/progress.mjs" stage "$S" baseline --note "2 cold captures + 1 warm, 90 s settle" # starting a stage finishes the one before
64
+ node "$TOOLS/progress.mjs" now "$S" "Capturing baseline 1 of 2 — about 3 min" # what is happening this minute
65
+ node "$TOOLS/progress.mjs" metric "$S" "Race start FPS" "24" --note "baseline · target 60"
66
+ node "$TOOLS/progress.mjs" fix "$S" F1 --title "Stop forced world-matrix recomputes" --status trying # then measuring → kept | reverted
67
+ node "$TOOLS/progress.mjs" fix "$S" F1 --status kept --before "24 fps" --after "41 fps" --note "−14 ms/frame in the countdown"
68
+ node "$TOOLS/progress.mjs" note "$S" "Noise floor ±1.8 fps — a fix must beat it" # a milestone
69
+ node "$TOOLS/progress.mjs" end "$S" --summary "<the verdict in one sentence>" # or --status stopped|failed with why
64
70
  ```
65
71
 
66
- - **Open the run before anything else** (the first lines of Phase 0): `start`, then the watchdog in the background — `node "$TOOLS/progress.mjs" watch "$S" --watch "$PROJ/src" > "$DATA/logs/watchdog.log" 2>&1 &` (add `--watch` for every other folder the fixes touch: a runtime, a Unity project's scripts). `start` writes a live page when the slug has no dashboard yet, and keeps an older report that has no panel in `data/previous-dashboard.html`.
67
- - **Write before every long command, never after only.** A capture, an analysis, a build or a test run gets a `now` line first that says what it is and roughly how long it takes; the result gets the next one. Never let 10 minutes pass without a write. The watchdog covers a silent stretch — after 5 quiet minutes it posts a heartbeat naming the newest file and the newest capture log's last line, after 30 without any file change it warns of a stall — but it is the backstop, not the narrator.
72
+ - **Write every command out in full** as above — never through a variable that holds a command (`P="node …"; $P now …`): zsh, the macOS default shell, does not split it into words, the command fails, and an `&&` chain behind it silently skips the capture. `$TOOLS`, `$S` and the other names in this skill are shorthand for absolute paths: most agent shells keep no variables between commands, so write the paths out (or set the variables at the top of the same command).
73
+ - **Open the run before anything else** (the first lines of Phase 0): `start --open true` (it opens the dashboard in the browser once; the page reloads itself from then on), then the watchdog in the background — `node "$TOOLS/progress.mjs" watch "$S" --watch "$PROJ/src" > "$DATA/logs/watchdog.log" 2>&1 &` (add `--watch` for every other folder the fixes touch: a runtime, a Unity project's scripts). `start` writes a live page when the slug has no dashboard yet, and keeps an older report that has no panel in `data/previous-dashboard.html`. Without `--auto-fix`, the Fix and Verify stages start out skipped.
74
+ - **Write before every long command, never after only.** A capture, an analysis, a build or a test run gets a `now` line first that says what it is and roughly how long it takes; the result gets the next one. **Never let 5 minutes pass without a write during active work** — reading code, diagnosing, drilling, editing, waiting on a background command and checking its output all count as active work, and each gets its own `now` line. The run is not over until `end`: keep writing between the last capture and the report, and after it until `end`. The watchdog covers a silent stretch — after 5 quiet minutes it posts a heartbeat naming the newest file and the newest capture log's last line, after 30 without any file change it warns of a stall — but it is the backstop, not the narrator.
68
75
  - **`stage`** at every phase change, **`metric`** for the figures the user cares about as soon as they are known (the brief's phases' FPS and CPU ms/frame, the noise floor, the target, the best so far — update the same label as they move), **`fix`** at every step of the fix loop: `planned` when diagnosed, `trying` while editing, `measuring` while re-capturing, then `kept` or `reverted` with before → after.
69
76
  - **Plain language** a non-specialist follows: "Re-measuring the race start with the particle fix", not "runs/fix-3 cold".
77
+ - **Waiting on a background capture** counts as active work: poll its log with a bounded loop (`until grep -qE '"runDir"|failed|Error' "$DATA/logs/<run>.log"; do sleep 5; done`, with a timeout on the command) rather than one long `sleep`, and write a `now` line at least every 5 minutes while it runs. A capture that ends in `scenario failed` or `--seconds` logs its failure evidence (see Phase 0 › Smoke run) — read it before trying again.
78
+ - **Auto-update** is always on. The page's `Auto-update on / off` button (top right of the panel) is built but hidden for now — `SHOW_LIVE_TOGGLE` in `scripts/lib/progress-panel.mjs` shows it again.
70
79
  - **End the run every time** — `end` after the report is written, or `end --status stopped|failed --summary "<why>"` when it cannot finish; a run left `running` keeps the page in live mode (the watchdog exits on its own after 12 hours).
71
80
  - Subagents that capture or fix get these commands and the slug folder, and write their own `now` lines.
72
81
 
@@ -78,7 +87,7 @@ $P end "$S" --summary "<the verdict in one sentence>" #
78
87
 
79
88
  | Tool | Does |
80
89
  |---|---|
81
- | `profile.mjs` | Launches Chrome (GPU on, fresh profile = cold caches), runs the scenario, records the V8 CPU profile, per-phase allocation profiles, the in-page probe (every frame's CPU work, draws, particles, SceneInstrumentation timers, scene counters, compiled shader count, long animation frames, phase marks), and the source + source map of every sampled script. `--warm true` repeats the scenario in the same browser. `--snapshots true` adds screenshots and the filmstrip |
90
+ | `profile.mjs` | Launches Chrome (GPU on, fresh profile = cold HTTP and browser caches), runs the scenario, records the V8 CPU profile, per-phase allocation profiles, the in-page probe (every frame's CPU work, draws, particles, SceneInstrumentation timers, scene counters, compiled shader count, every shader module / pipeline / program the page creates with its time, long animation frames, phase marks), and the source + source map of every sampled script. `--warm true` repeats the scenario in the same browser. `--snapshots true` adds screenshots and the filmstrip. `--gpu-cold true` makes the GPU compile every pipeline fresh. A failed or overrun scenario still ends the capture, with `shots/failed.png` and the page's address, title and console errors in the log |
82
91
  | `analyze.mjs` | (`--drill "Class.fn,…"` breaks a function's time into its direct callees, per phase — use it on every heavy library function before deciding what to change.) Maps every sample to its original file / line / class (source maps, then class-span scan of the script, then the config's `sourceRoots` for bundles without maps), assigns owners, charges library time to the nearest code of yours (the render loop is a frame driver and never takes the blame), and writes `analysis.json`: per phase, per second, per function (self, inclusive, callers, "your code behind it"), allocations, worst frames, hot lines, code excerpts. Prints a summary to read |
83
92
  | `compare.mjs` | Before → after per phase, owner and function, for the fix loop |
84
93
  | `report.mjs` | Builds the dashboard HTML from an analysis, an optional baseline and warm analysis, and the agent's `findings.json`, with the live progress panel at its top |
@@ -92,6 +101,8 @@ The clock of the CPU profile is tied to the page's clock by a named busy-wait ma
92
101
 
93
102
  Read the Babylon Toolkit Agent Reference router and the sub-documents the project's code touches (`node-esm.md` or `classic.md`, `scene-components.md`, `react-framework.md` for a React host, `fusion-networking.md` for networked play) before the first run in a session — unless your prompt already carries those excerpts.
94
103
 
104
+ The names below are shorthand for absolute paths used throughout this skill — write them out in every command (or set them at the top of the same command): most agent shells keep no variables between commands.
105
+
95
106
  ```bash
96
107
  PROJ=<absolute web project root> # holds package.json — resolved as in "Which project" below
97
108
  SKILL_DIR=<absolute path of this skill's folder>
@@ -122,26 +133,28 @@ Run every tool from `$DATA` (`cd "$DATA"`): run paths stay short (`runs/baseline
122
133
 
123
134
  Resolve `$PROJ` before anything else, in this order, and say which project you picked and why in the first update:
124
135
 
125
- 1. **`--project:<path>`** — walk up from the path to the nearest folder with a `package.json`.
136
+ 1. **`--project:<path>`** — the folder (or any folder or file inside it) is the project root: the nearest folder above it with a `package.json`, else the folder itself.
126
137
  2. **A project named in the brief** — a path, or a folder name ("profile BtMarioKartWeb's race start") that matches a project in or next to the working directory.
127
138
  3. **The working directory** — walk up from it to the nearest `package.json`.
128
- 4. **One level down** — when the working directory holds no project (e.g. a folder of games), look in its immediate subfolders; use the one game project found.
139
+ 4. **One level down** — when the working directory holds no project (e.g. a folder of games), look in its immediate subfolders; use the one web project found.
129
140
 
130
- A candidate counts only when it is a **web game**: a `package.json` with a dev/start script and a BabylonJS dependency (`@babylonjs/core`, `babylonjs` or `@babylonjs-toolkit/*`). A `package.json` that is not a game (a tool, a library, the Babylon Toolkit runtime itself) is skipped and the walk goes on. When steps 1–4 find none, or step 4 finds several, **ask the user** with the candidates listed — never guess between projects.
141
+ The project is just **the root folder of the web app**: where its source lives (for code lines and excerpts), where `.agents/profile/` goes, and — when it has a `package.json` with a dev/start script — how to serve it. **Any web app counts**: a Babylon Toolkit game, a plain BabylonJS app, or a front end with no Babylon at all (React, Vue, Svelte, a static site). A `package.json` that is not an app (a tool, a library, the Babylon Toolkit runtime itself) is skipped and the walk goes on. When the user gives `--url` with no `package.json` to serve from — a deployed site, a static folder they already serve — the project is the folder given (or the working directory), used for the output and to map source. When steps 1–4 find none, or step 4 finds several, **ask the user** with the candidates listed — never guess between projects.
131
142
 
132
143
  ## Phase 0 — Preflight
133
144
 
134
145
  ```bash
135
146
  mkdir -p "$TOOLS" "$DATA/runs" "$DATA/logs"
136
147
  cp -R "$SKILL_DIR"/scripts/* "$TOOLS/" # refresh on every skill update
137
- node "$TOOLS/progress.mjs" start "$S" --title "<Project> — <scenario> performance" --brief "<the user's words>" --project "$PROJ" --url "<url>"
148
+ node "$TOOLS/progress.mjs" start "$S" --title "<Project> — <scenario> performance" --brief "<the user's words>" --project "$PROJ" --url "<url, or the dev server's / until known>" --open true
138
149
  node "$TOOLS/progress.mjs" watch "$S" --watch "$PROJ/src" > "$DATA/logs/watchdog.log" 2>&1 & # the watchdog (see Live progress)
139
150
  (cd "$TOOLS" && npm install --silent) # puppeteer-core, source-map-js
140
- printf '.tools/\n*/data/runs/\n*/data/logs/\n*/data/progress.js\n*/data/previous-dashboard.html\n' > "$PROF/.gitignore"
151
+ for line in '.tools/' '*/data/runs/' '*/data/logs/' '*/data/progress.js' '*/data/previous-dashboard.html'; do
152
+ grep -qxF "$line" "$PROF/.gitignore" 2>/dev/null || printf '%s\n' "$line" >> "$PROF/.gitignore" # add what is missing, keep the rest
153
+ done
141
154
  git -C "$PROJ" status --short > "$DATA/worktree-at-start.txt" # the user's own uncommitted work
142
155
  ```
143
156
 
144
- 1. **The game must be served.** Find the dev server (package.json `dev` script, its port) and check it answers; start it in the background if it does not. Content served from elsewhere (a Unity exporter dev server, a CDN) must answer too — read the project's CLAUDE.md / SPEC for how. Profile the **dev server** by default: modules arrive one file each with source maps, so attribution is exact. Mention that React runs in development mode there (its share is inflated); profile a source-mapped production build (`vite build --sourcemap` + `vite preview`) when the React share matters.
157
+ 1. **The app must be served.** With `--url` pointing at a site that already runs (a deployed URL, a server the user started), check it answers and use it. Otherwise: Find the dev server (package.json `dev` script, its port) and check it answers; start it in the background if it does not — with `BROWSER=none` (a Vite `server.open: true` would open a stray tab) and its output in `$DATA/logs/devserver.log`. When the usual port answers but serves **another** project (check the page title or a known path), do not touch that server: start this project's on a free port (Vite: `--port <n>`, or the `PORT` its config reads; a `strictPort` config fails loudly instead of moving), and record the URL with `progress.mjs set --url`. Stop the servers you started when the run ends. Content served from elsewhere (a Unity exporter dev server, a CDN) must answer too — read the project's CLAUDE.md / SPEC for how. Profile the **dev server** by default: modules arrive one file each with source maps, so attribution is exact. Mention that React runs in development mode there (its share is inflated); profile a source-mapped production build (`vite build --sourcemap` + `vite preview`) when the React share matters.
145
158
  2. **`profile.config.json`** in `$DATA` (create it if missing; copy `owners` from a sibling slug's config when there is one):
146
159
  ```json
147
160
  { "projectRoot": "../../../..", "url": "http://localhost:5173/play", "scenario": "scenario.mjs",
@@ -149,10 +162,13 @@ git -C "$PROJ" status --short > "$DATA/worktree-at-start.txt"
149
162
  "sourceRoots": ["/abs/UnityProject/Assets/Scripts"] } ] }
150
163
  ```
151
164
  Add an owner rule for every script **the user owns that ships without a source map** — typically the Unity exporter's project bundle (`scenes/<Product>.js`), compiled from TypeScript under the Unity project's `Assets/`. `sourceRoots` lets analysis find the class's `.ts` file and method line. Owners marked `yours` are charged with the library work they trigger.
152
- 3. **The scenario.** It must play the moment the user says is slow, the way a player would, and mark its phases. Reuse `$DATA/scenario.mjs` when it exists; otherwise copy `templates/scenario.template.mjs` there and adapt it: read the game's code to find what to wait on (a component's phase field, a ready flag) and how to reach it from the page (`window.TOOLKIT.SceneManager.SearchForScriptComponentByName(scene, alias)`, `window.PROJECT.*`). Mark the **reveal** — the moment the loading screen goes away (for a toolkit splash: `#xbabylonjsSplashScreen` gets `opacity: 0`) — as its own phase: games often start their intro behind the splash, and hitches before the reveal are load time, not stutter the player sees. Hold input like a player (keys through `context.keyDown`). If the scenario must keep the game going without a player (a race after the start), hand the player's object to the game's own AI component — never skip or disable work to get there.
153
- 4. **Other sessions.** List the other agent sessions on this machine (`ListAgents`) and check `git status` / `git log` in every repo you may touch. Another session working in the same project or runtime shares the dev server, the runtime build and the commits: tell it which files you change (and ask it to leave them out of its commits), give a one-line heads-up before restarting a dev server or rebuilding a library it serves, and pause captures while it runs timing-sensitive checks. Its edits also move your numbers — compare runs taken on the same code, and say so in the report when you could not.
165
+ 3. **The scenario.** It must play the moment the user says is slow, the way a player would, and mark its phases. **Enter the app the way a player does**: open the page they open (usually the home page) and click through what they click (the menu, the level's button — `context.expectNavigation()` first when the click loads a new page). Use a URL you build yourself (query parameters, a route the UI never links to) only when that is how players really arrive: it can take a path they never take — in one game a `?scene=…gz.gltf` query made the dev server mislabel the page itself as gzip, and the capture recorded a white screen. Reuse `$DATA/scenario.mjs` when it exists; otherwise copy `templates/scenario.template.mjs` there and adapt it: read the game's code to find what to wait on (a component's phase field, a ready flag) and how to reach it from the page (`window.TOOLKIT.SceneManager.SearchForScriptComponentByName(scene, alias)`, `window.PROJECT.*`). Mark the **reveal** — the moment the loading screen goes away (for a toolkit splash: `#xbabylonjsSplashScreen` gets `opacity: 0`) — as its own phase: games often start their intro behind the splash, and hitches before the reveal are load time, not stutter the player sees. Hold input like a player (keys through `context.keyDown`). If the scenario must keep the game going without a player (a race after the start), hand the player's object to the game's own AI component — never skip or disable work to get there. **Size `settle`** (`--args settle=<seconds>`, the seconds recorded after the moment) to the moment: long enough to cover it and a calm stretch after it (a 3-second launch stutter needs ~15–20 s; a frame rate that sinks over a race needs 90), and short of where the scenario stops being realistic (the car reaches a wall, the level ends).
166
+ 4. **Other sessions.** Find the other agent sessions on this machine — the harness's session-listing tool when it has one (Claude Code: `ListAgents`); otherwise the running dev servers and browsers (`ps`, `lsof -iTCP -sTCP:LISTEN`) and recent file times — and check `git status` / `git log` in every repo you may touch. Another session working in the same project or runtime shares the dev server, the runtime build and the commits: tell it which files you change (and ask it to leave them out of its commits), give a one-line heads-up before restarting a dev server or rebuilding a library it serves, and pause captures while it runs timing-sensitive checks. Its edits also move your numbers — compare runs taken on the same code, and say so in the report when you could not.
154
167
  While anyone else edits the project, **never capture on a watching dev server**: one save hot-patches or reloads the game mid-run (the capture logs `WARNING: … restarted` and the report shows a banner). Profile a frozen copy instead: `git archive HEAD | tar -x -C <scratchpad>/snapshot`, copy in your own uncommitted project changes, link `node_modules` to the live project's, and serve it on a spare port with a config that wraps the project's — `mergeConfig(base, { cacheDir: ".vite-profile-cache", server: { watch: null, hmr: false, fs: { strict: false } } })` (its own optimizer cache, since `node_modules` is shared). Warm it once, then capture against that port. Library builds the project links to (a runtime) are still live — keep the heads-up for those.
155
168
  5. **Smoke run** (`node "$TOOLS/profile.mjs" --out runs/smoke --args settle=15 --snapshots true`, from `$DATA`): look at the screenshots and confirm the scenario really did what the user described (the countdown ran, the player moved, the particles fired). Fix the scenario before measuring anything.
169
+ - **When it fails** (`scenario failed` or `--seconds` in the log), the log names the evidence: `shots/failed.png` (what the page showed), the page's address and title, its visible text, and its console errors and failed document / script loads. A white page with a failed document load is a server or routing problem, not the game.
170
+ - **Never read the smoke run for stutter**: each screenshot stalls the page 0.5–2 s — a long frame with no script in it, lined up with a `shot` line in the log. It looks exactly like a hitch.
171
+ - **The first capture of a session may be the only GPU-cold one** (see `--gpu-cold`): if it shows a hitch the later ones do not, that is a first-visit cost, not noise — re-capture with `--gpu-cold true` to reproduce it at will.
156
172
 
157
173
  ## Phase 1 — Measure the baseline
158
174
 
@@ -168,6 +184,8 @@ node "$TOOLS/analyze.mjs" --capture runs/baseline-2/cold --quiet true
168
184
  node "$TOOLS/compare.mjs" --before runs/baseline/cold/analysis.json --after runs/baseline-2/cold/analysis.json # = the noise floor
169
185
  ```
170
186
 
187
+ (With `--gpu-cold`, add `--gpu-cold true` to every capture.) **When the baseline does not show the problem**, close the gap before diagnosing (Troubleshooting › *The profile is smooth*), and keep each attempt as its own run (`runs/repro-<what changed>`). The run that shows it — the **reproduction run** — is what Phase 2 diagnoses and Phase 5 reports.
188
+
171
189
  **A capture right after a dev-server restart is not comparable:** Vite's first load after `--force` re-optimizes dependencies and transforms every module (seconds of `(program)` time in the first phase). Load the page once (a throwaway headless visit) before every capture that follows a restart.
172
190
 
173
191
  Read `summary.txt` in full. Check first: the engine is the one the game uses (WebGPU vs WebGL — a fallback changes everything), the console errors, and that each phase lasted as long as it should. **The noise floor** is the per-phase FPS and CPU ms/frame difference between the two baseline captures; a fix must beat it.
@@ -178,18 +196,20 @@ Read `summary.txt` in full. Check first: the engine is the one the game uses (We
178
196
 
179
197
  Work from the brief's phases outward. For each phase that misses the target:
180
198
 
181
- 1. **Is the main thread the limit?** `busy` near 100% with CPU ms/frame near the frame interval → CPU-bound: find the code. `busy` low with long frame intervals → GPU-bound or waiting: draw calls, fill rate, render scale (the toolkit's `bt-optimize` skill owns scene-side GPU work).
199
+ 1. **Is the main thread the limit?** `busy` near 100% with CPU ms/frame near the frame interval → CPU-bound: find the code. `busy` low with long frame intervals → GPU-bound or waiting: draw calls, fill rate, render scale (the toolkit's `bt-optimize` skill owns scene-side GPU work). The summary's **WORST FRAMES** says which: `cpu` (the frame's own work) or `wait` (a gap the main thread sat through, with its script ms and the pipelines created just before it). A `wait` of hundreds of ms with almost no script, right after new pipelines, is the GPU process compiling shaders — see the hot-spots row *Long frame, almost no script*.
182
200
  2. **Visible or hidden?** Judge stutter only in phases after the reveal mark; a worst frame behind the loading screen costs load time (still worth shortening) but the player never sees it. Check the filmstrip when unsure.
183
201
  3. **Owners.** Which owner grew in this phase compared with the calm phases? A phase that is slow only cold (compare the warm summary) is first-use work: downloads, decoding, shader compilation, pipeline creation, JIT.
184
202
  4. **Subtrees, then functions.** In `inclusiveTop` (frame shells removed) find the subtree that dominates, then its heaviest self-time functions and their `callers` / `enteredFrom` / `yours` columns: those name the code that asks for the work. Re-run `analyze.mjs --drill "<function>"` to split a heavy function into its callees per phase — a cost that **grows** phase by phase while the scene stays the same is accumulating state (more systems switched on, caches invalidated), not load.
185
- 5. **Count, live, when the profile cannot say why.** A function that is cheap per call but runs too often needs a census: copy `templates/census.template.mjs` to the scratchpad (outside the project), adapt how it reaches the moment, and run it with `--args method=TransformNode.prototype.computeWorldMatrix,seconds=1`: it wraps the method for that long and counts calls per object with a sample stack. It names the object and the caller in one run — the Mario Kart run found 16,000 world-matrix recomputes per frame this way.
203
+ 5. **Count, live, when the profile cannot say why.** A function that is cheap per call but runs too often needs a census: copy `templates/census.template.mjs` to the scratchpad (outside the project), adapt how it reaches the moment, and run it with `--args method=TransformNode.prototype.computeWorldMatrix,seconds=1`: it wraps the method for that long and counts calls per object with a sample stack. It names the object and the caller in one run — the Mario Kart run found 16,000 world-matrix recomputes per frame this way. GPU pipelines need no census: every capture records each shader module / pipeline / program with its time (`gpuCompiles` in `analysis.json`, per phase in the summary); line them up with the marks and the scenario's own progress (a distance, a spawn) to name what first drew them.
186
204
  6. **Read the code.** Open the files and lines named (`yoursTop`, `excerpts`, `hotLines`) and the library function the time lands in. Find the mechanism — what runs, how often, on how many objects, why. A finding is not done until you can say *why* it costs what it costs.
187
205
  7. **Worst frames** name the spikes (a spawn, a burst, a compile) that averages hide.
188
206
  8. **Allocations**: phase `heap` lists show who feeds the garbage collector while playing.
189
207
 
190
208
  Write `$DATA/findings.json` (schema in `scripts/report.mjs`): per finding a severity, the phases, the cost in ms/frame and % of the phase, the cause in plain language, the evidence (numbers from the analysis), the code locations, the planned fix. Rank by the time it would win in the phases the user cares about.
191
209
 
192
- ## Phase 3 — Fix, one at a time (skipped with `--analyze`)
210
+ ## Phase 3 — Fix, one at a time (only with `--auto-fix`)
211
+
212
+ Without `--auto-fix`, skip this phase: no code changes. Every finding still carries its proposed `fix`, `gain`, `risk` and `verify`, so **The fixes** and `findings.md` are a complete brief for a later spec.
193
213
 
194
214
  `progress.mjs stage "$S" fix`; each fix moves `trying` → `measuring` → `kept` or `reverted`, with before → after (see *Live progress*).
195
215
 
@@ -203,7 +223,9 @@ For each finding, highest win first:
203
223
 
204
224
  Stop when the target FPS is met in the brief's phases, `--max-fixes` is reached, or what is left is not worth the change (say what it would take).
205
225
 
206
- ## Phase 4 — Verify
226
+ ## Phase 4 — Verify (only with `--auto-fix`)
227
+
228
+ Without `--auto-fix` no code changed: skip it (the stage is already marked skipped) and go to the report.
207
229
 
208
230
  `progress.mjs stage "$S" verify`, with a `now` line for the final captures, the tests and the screenshots.
209
231
 
@@ -225,10 +247,26 @@ node "$TOOLS/report.mjs" --analysis runs/final/cold/analysis.json --baseline run
225
247
  [--snapshots runs/final-shots/cold --snapshots-before runs/baseline-shots/cold] # writes $DASHBOARD (../dashboard.html)
226
248
  ```
227
249
 
228
- (With `--analyze`: `--analysis runs/baseline/cold/analysis.json --warm runs/baseline/warm/analysis.json`.) Then end the run — `node "$TOOLS/progress.mjs" end "$S" --summary "<the verdict in one sentence>"` — so the page leaves live mode and the watchdog exits. Open the report in the browser, screenshot it at desktop and phone width, and check it renders with no console errors. Then tell the user: the verdict in two sentences, each finding with its before → after, what was reverted and why, what remains and what it would take, and the report path.
250
+ (Without `--auto-fix`: `--analysis` is the run that shows the problem — the baseline, or the reproduction run when the baseline did not — and `--warm` its own warm capture when it has one: `--analysis runs/baseline/cold/analysis.json --warm runs/baseline/warm/analysis.json`. Every finding carries `fix` (the proposed change), `gain`, `risk` and `verify`; name the reproduction run and how it differs from a plain capture in the findings' evidence.) Every report lists **The fixes** (each with a copy-able `/bt-spec` command, and one for any group the user ticks) whether they were applied or not. The report also writes `dashboard.pdf` (its **Download PDF** button; `--pdf false` skips it) and `findings.md` (the spec-ready diagnosis). The page has **Print** too. Then end the run — `node "$TOOLS/progress.mjs" end "$S" --summary "<the verdict in one sentence>"` — so the page leaves live mode and the watchdog exits. The dashboard is already open (`start --open true`) and reloads into the report by itself — do not open a second tab; check it renders at desktop and phone width with no console errors (a headless screenshot of the file is enough). Stop every dev server and Chrome the run started. Then tell the user: the verdict in two sentences, each finding with its before → after, what was reverted and why, what remains and what it would take, and the report path.
229
251
 
230
252
  ---
231
253
 
254
+ # Any web app
255
+
256
+ A page with no Babylon engine is profiled the same way — scenario, cold and warm captures, phases, the CPU profile mapped to source lines and owners (`project`, `react`, `vendor`, `program`, `gc`), allocations, long animation frames, screenshots, the dashboard and the fix loop. The differences:
257
+
258
+ - **Frames come from the browser** (`requestAnimationFrame`, `frameSource: "raf"` in the capture): FPS, frame gaps, the worst frames and the per-second timeline are there; the per-frame CPU time, draw calls, particles, compiled shaders and the engine timers are Babylon-only and stay empty. A worst frame's window is the gap the main thread held the next frame back.
259
+ - **The scenario drives the page the way a user does**: clicks (`context.page.click(selector)`, or an `evaluate` that clicks; `context.expectNavigation()` first when it loads a new page), typing, scrolling (`context.evaluate(() => window.scrollBy(0, 800))`), route changes — with a mark per step the user calls slow.
260
+ - **What to look for:**
261
+
262
+ | Hot in the profile | Usually means | Look at |
263
+ |---|---|---|
264
+ | `renderRootSync` / `performWorkOnRoot` / `commitRoot` / `jsxDEV` (React), `patch` (Vue), `flush` (Svelte) | Re-rendering more than the change needs. **During gameplay, any React share at all is a HUD pushing per-frame values through state** — a violation of the Agent Reference's *Game UI Performance — ENFORCED* | Which component re-renders per frame / per keystroke (`yours` names it). A game HUD: the per-frame `setState` / store / EventBus calls in its loop — the fix is the reference's *Live HUD Values* pattern (render once, one frame loop, refs, write on change), and the finding cites it. Elsewhere: memoization, state placed too high, context that changes every render |
265
+ | `(program)` large during play, `Layout` / `Recalculate Style` in a `--trace` | Layout thrashing or style recalculation | Reads of layout (`offsetHeight`, `getBoundingClientRect`) after writes in a loop; animations on layout properties instead of `transform` / `opacity` |
266
+ | `(garbage collector)` | Short-lived allocations per frame or per event | Phase `heap` lists: arrays / objects / closures created in handlers and render functions |
267
+ | One long task at load | Parsing and running a large bundle | Code splitting, deferring what the first view does not need |
268
+ | `JSON.parse` / `stringify`, large `map` / `filter` chains | Big data work on the main thread | Paging, memoizing, moving it to a worker |
269
+
232
270
  # Reading BabylonJS hot spots
233
271
 
234
272
  What a heavy function usually means, and where to look. Always confirm with `callers` / `yours` before acting.
@@ -238,6 +276,7 @@ What a heavy function usually means, and where to look. Always confirm with `cal
238
276
  | `TransformNode.computeWorldMatrix` (deep self-recursion) | **Forced recomputes** (`computeWorldMatrix(true)`): a forced recompute forces every ancestor and bumps its child-update id, which marks every sibling subtree out of date, so the render and the next forced call recompute everything again — a feedback loop. Count recomputes per node live before blaming the scene size | Who calls `computeWorldMatrix(true)` per frame / per physics step (Havok pre-step, sync helpers, animator root motion, particle emitters): one forced call per frame on any node under a level's root invalidates the whole level. Probe the level root's `_childUpdateId` per frame and the forcing stacks. Fix: read the current matrix with `TOOLKIT.Utilities.CurrentWorldMatrix(node)` (recomputes only the changed nodes of the chain); static props never frozen |
239
277
  | `HavokPlugin.setPhysicsBodyTransformation` / `_getTransformInfos` | Havok TELEPORT pre-step: each body whose `disablePreStep` is false is force-synced from its node before every step. Should only be bodies that move by transform (kinematic) | Count bodies by motion type × pre-step live. Static colliders with pre-step on are pure cost (the Babylon Toolkit runtime turned it on for every exported collider before 9.30.x) |
240
278
  | `Node._getDescendants` / `getChildMeshes` / `getChildren` | A hierarchy walk — expensive when done per frame or per object | Per-frame lookups that should be cached at start |
279
+ | **Long frame, almost no script** — a `wait` in WORST FRAMES (LoAF `blockingMs` ≈ 0), right after new pipelines / programs were created (`pipelines near`, `gpu:` per phase) | **The GPU process compiling pipelines** for shaders drawn for the first time. The main thread is idle, so the CPU profile shows nothing; the browser's and the OS's shader caches hide it on later visits, so it is a **first-visit** stutter — every capture after the first may be smooth | Reproduce with `--gpu-cold true`. Which materials / effects are first drawn after the loading screen lifts (the car, the road 10 m in, a first particle burst). Fix: compile them behind the loading screen — wait for the scene and those materials to be ready (`scene.whenReadyAsync()`, `material.forceCompilationAsync(mesh)`, the toolkit's shader warm-up) and render a few frames hidden before revealing, instead of revealing on a fixed timer |
241
280
  | `ProcessIncludes`, `MaterialPluginManager` code injection, `Effect` / `ShaderProcessor`, shader regexes, `glslang` / `twgsl` wasm | A **new shader variant** being built: a material's defines changed, a new material / particle system appeared, a plugin toggled | New materials or defines appearing after load (first particle burst, boost flames, item effects) — pre-warm them at load |
242
281
  | `WebGPUEngine._draw`, `getBindGroups`, `setSubData`, `UniformBuffer.update*`, `Mesh.render`, `SubMesh.render` | Per-draw CPU cost: the frame has too many draws (meshes × passes) | Draw counts per phase, shadow casters, hidden-but-rendered meshes, instancing / merging (`bt-optimize`) |
243
282
  | `ShadowGenerator._renderSubMeshForShadowMap` | Shadow passes re-drawing casters every frame | Casters list, refresh rate, number of shadowed lights |
@@ -246,6 +285,8 @@ What a heavy function usually means, and where to look. Always confirm with `cal
246
285
  | `ParticleSystem.animate` / `_update`, `ShurikenParticles.sys._update` | CPU particle simulation: emit rate × lifetime × systems | Systems started together at an event, capacity, GPU particles |
247
286
  | `(garbage collector)` | Short-lived allocations per frame | Phase `heap` lists: vectors / arrays / closures created in update loops |
248
287
  | `(program)` | Browser-internal work (parse, compile, style, layout, GPU IPC) | Large during load = script parse / compile; during play = DOM / layout churn (HUD) |
288
+ | React (`react` owner) above ~0% during play, `jsxDEV` among the heaviest functions | A HUD re-rendering every frame: speed, timers, needles or bars pushed through React state, props or per-frame EventBus messages | The HUD's frame loop (`yours` names the component). Fix: the Agent Reference's *Live HUD Values — ENFORCED* — render once, one frame loop, write through refs only on change. Budget: ≤ 0.5 ms/frame, `react` near 0% in play phases |
289
+ | `AdvancedDynamicTexture._checkUpdate` / `_render`, `updateDynamicTexture` every frame | A Babylon GUI control assigned every frame: each change re-lays out, redraws and re-uploads the whole GUI texture | Assign a control only when its shown value changes (*Game UI Performance — ENFORCED*) |
249
290
 
250
291
  ---
251
292
 
@@ -271,5 +312,7 @@ What a heavy function usually means, and where to look. Always confirm with `cal
271
312
  | A phase lasts far longer than expected | The scenario waited on the wrong condition — check its log lines and the screenshots |
272
313
  | Functions show `(anonymous)` in a bundle | Minified without names and without a source map: profile a dev build or a `--sourcemap` build |
273
314
  | Capture logs `WARNING: … restarted`, a phase shows 0 FPS, or the compiled-shader count drops mid-run | The dev server reloaded or hot-patched the game because someone saved a file. Discard the run and capture a frozen snapshot (Phase 0 › Other sessions) |
274
- | The profile is smooth but the user feels a stutter | The scenario does not play the way the user does. Close the gap one difference at a time: `--headed true` (a visible window), `--autoplay gesture` (audio stays locked until the first key, as in a normal browser — the unlock can be the hitch), no idle waits where the player acts at once (throttle the moment the loading screen lifts), the same build and URL the user plays (dev server vs a deployed site). If it is still smooth, **let the user drive**: give the scenario a watch-only mode (`--args driver=human`: it loads the moment cold, marks progress such as distance travelled, and sends no keys), run it `--headed true`, and tell the user exactly when it is their turn and for how long |
315
+ | The profile is smooth but the user feels a stutter | The scenario does not play the way the user does. Close the gap one difference at a time, each as its own run: **`--gpu-cold true` first** when the user says "cold", "first time" or "after loading" (later captures reuse compiled GPU pipelines — see the row below), `--headed true` (a visible window), `--autoplay gesture` (audio stays locked until the first key, as in a normal browser — the unlock can be the hitch), no idle waits where the player acts at once (throttle the moment the loading screen lifts), the same build and URL the user plays (dev server vs a deployed site). If it is still smooth, **let the user drive**: give the scenario a watch-only mode (`--args driver=human`: it loads the moment cold, waits for the player's first move, marks progress such as distance travelled, and sends no keys), run it `--headed true` with `--seconds 900` (the default 240 s cap ends the capture before most people start), and tell the user exactly what to do, when, and for how long — in chat and in a `now` line (a subagent has only the `now` line: say so in its brief, and have it give up after a set wait and carry on) |
316
+ | Only the first capture of the session shows the hitch | A **first-visit GPU cost**: the GPU compiled the pipelines in that capture and every later one reused them (macOS keeps compiled Metal pipelines in a system cache that a fresh Chrome profile does not reset). Re-capture with `--gpu-cold true` — never delete the system's shader cache, it belongs to the user's everyday browser |
317
+ | A capture fails: `scenario failed`, `--seconds`, or a white page in the watched window | Read the failure evidence in the log (`shots/failed.png`, the page's address and title, its console errors and failed document / script loads). A white page with a failed document (`ERR_CONTENT_DECODING_FAILED`, 404) is the server or the route: start the app the way a player does (Phase 0 › The scenario) |
275
318
  | Numbers jump between runs | Background load on the machine, a different rival / random seed, or a cold vs warm mix-up: compare cold with cold, and use the noise floor |
@@ -208,7 +208,7 @@ function frameStats(frames, from, to) {
208
208
  const intervals = [];
209
209
  for (let i = 1; i < inside.length; i++) intervals.push(inside[i].at - inside[i - 1].at);
210
210
  const sortedIntervals = [...intervals].sort((first, second) => first - second);
211
- const cpu = inside.map((frame) => frame.cpuMs).sort((first, second) => first - second);
211
+ const cpu = inside.map((frame) => frame.cpuMs).filter((value) => typeof value === "number").sort((first, second) => first - second);
212
212
  const seconds = (inside[inside.length - 1].at - inside[0].at) / 1000;
213
213
  const mean = (key) => {
214
214
  const values = inside.map((frame) => frame[key]).filter((value) => typeof value === "number");
@@ -221,8 +221,8 @@ function frameStats(frames, from, to) {
221
221
  intervalP95Ms: round(percentile(sortedIntervals, 0.95)),
222
222
  worstIntervalMs: round(sortedIntervals[sortedIntervals.length - 1]),
223
223
  slowFrames: intervals.filter((interval) => interval > FRAME_BUDGET_MS * 1.5).length,
224
- cpuMeanMs: round(cpu.reduce((sum, value) => sum + value, 0) / cpu.length, 2),
225
- cpuP95Ms: round(percentile(cpu, 0.95), 2),
224
+ cpuMeanMs: cpu.length ? round(cpu.reduce((sum, value) => sum + value, 0) / cpu.length, 2) : null,
225
+ cpuP95Ms: cpu.length ? round(percentile(cpu, 0.95), 2) : null,
226
226
  draws: round(mean("draws"), 0),
227
227
  activeMeshes: round(mean("activeMeshes"), 0),
228
228
  particles: round(mean("particles"), 0),
@@ -369,15 +369,46 @@ function excerpt(symbolizer, file, line, hot) {
369
369
  return { file, from, to, rows };
370
370
  }
371
371
 
372
- /** The worst frames of the run, each with the functions and owners that filled it. */
373
- function worstFrames(frames, prepared, info, phases, top) {
374
- const candidates = frames.map((frame, index) => ({ ...frame, intervalMs: index > 0 ? frame.at - frames[index - 1].at : null }));
375
- const worst = [...candidates].sort((a, b) => b.cpuMs - a.cpuMs).slice(0, top);
372
+ /** Counts the shader modules and pipelines / programs the page created inside a window. */
373
+ function compilesIn(gpuCompiles, from, to) {
374
+ let pipelines = 0;
375
+ let shaderModules = 0;
376
+ for (const entry of gpuCompiles || []) {
377
+ if (entry.at < from || entry.at >= to) continue;
378
+ if (entry.kind === "shaderModule") shaderModules++;
379
+ else pipelines++;
380
+ }
381
+ return { pipelines, shaderModules };
382
+ }
383
+
384
+ /**
385
+ * The worst frames of the run, each with the functions and owners that filled it. A frame is bad for one of two reasons:
386
+ * its own CPU work (`kind: "cpu"`), or a long gap before it while the main thread mostly waited (`kind: "wait"`) — the GPU
387
+ * process compiling pipelines, a GPU-bound frame, the browser's own work. A wait never shows in CPU time, so both rank.
388
+ */
389
+ function worstFrames(frames, prepared, info, phases, gpuCompiles, top) {
390
+ const candidates = frames.map((frame, index) => {
391
+ const previous = index > 0 ? frames[index - 1] : null;
392
+ const intervalMs = previous ? frame.at - previous.at : null;
393
+ const timed = typeof frame.cpuMs === "number";
394
+ // The gap before this frame, minus the CPU work of the frame that opened it, is time the main thread did not draw.
395
+ const waitMs = intervalMs == null ? 0 : intervalMs - (previous && typeof previous.cpuMs === "number" ? previous.cpuMs : 0);
396
+ const cpuCost = timed ? frame.cpuMs : 0;
397
+ const waits = !timed || waitMs > cpuCost;
398
+ return {
399
+ ...frame, intervalMs, kind: timed ? (waits ? "wait" : "cpu") : "gap", cost: waits ? (intervalMs ?? 0) : cpuCost,
400
+ from: waits ? frame.at - (intervalMs ?? 0) : frame.at, to: waits ? frame.at : frame.at + cpuCost,
401
+ };
402
+ });
403
+ const worst = [...candidates].sort((a, b) => b.cost - a.cost).slice(0, top);
376
404
  return worst.map((frame) => {
377
- const aggregate = aggregateWindow(prepared, info, frame.at, frame.at + frame.cpuMs);
405
+ const aggregate = aggregateWindow(prepared, info, frame.from, frame.to);
378
406
  const phase = phases.find((candidate) => frame.at >= candidate.from && frame.at < candidate.to);
407
+ // Pipelines created just before a wait (up to 2 s) are what the GPU process may still be compiling during it.
408
+ const compiles = compilesIn(gpuCompiles, frame.from - 2000, frame.to);
379
409
  return {
380
- at: frame.at, cpuMs: round(frame.cpuMs), intervalMs: round(frame.intervalMs), phase: phase ? phase.name : null,
410
+ at: frame.at, cpuMs: round(frame.cpuMs), intervalMs: round(frame.intervalMs), kind: frame.kind, phase: phase ? phase.name : null,
411
+ scriptMs: round(aggregate.busyMs), pipelinesNear: compiles.pipelines, shaderModulesNear: compiles.shaderModules,
381
412
  draws: frame.draws, particles: frame.particles,
382
413
  top: [...aggregate.self].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([key, ms]) => ({ name: aggregate.symbols.get(key).name, owner: aggregate.symbols.get(key).owner, ms: round(ms) })),
383
414
  yours: [...aggregate.responsible.values()].sort((a, b) => b.totalMs - a.totalMs).slice(0, 3).map((blame) => ({ name: blame.symbol.name, file: blame.symbol.file, line: blame.symbol.line, ms: round(blame.totalMs) })),
@@ -444,10 +475,18 @@ function signals(analysis) {
444
475
  if (ownerPct("gc") >= 5) notes.push(`Garbage collection takes ${ownerPct("gc")}% of busy time — look at the allocation list (heap.yoursTop) for per-frame garbage.`);
445
476
  if (ownerPct("shader") >= 5) notes.push(`GLSL → WGSL shader translation takes ${ownerPct("shader")}% of busy time — new material / particle shader variants are being compiled on the main thread.`);
446
477
  for (const phase of analysis.phases) {
447
- if (phase.frames.fps != null && phase.frames.fps < 45 && phase.durationSeconds >= 1) {
448
- const lead = phase.lists.inclusiveTop.find((entry) => !entry.shell && entry.owner !== "program");
449
- notes.push(`Phase "${phase.name}" runs at ${phase.frames.fps} FPS (CPU ${phase.frames.cpuMeanMs} ms/frame, main thread ${phase.busyPct}% busy)${phase.lists.yoursTop[0] ? `; your heaviest code: ${phase.lists.yoursTop[0].name} (${phase.lists.yoursTop[0].msPerFrame} ms/frame)` : ""}${lead ? `; heaviest stack entry: ${lead.name}` : ""}.`);
450
- }
478
+ if (phase.beforeFirstFrame || phase.frames.fps == null || phase.frames.fps >= 45 || phase.durationSeconds < 1) continue;
479
+ const lead = phase.lists.inclusiveTop.find((entry) => !entry.shell && entry.owner !== "program");
480
+ const cpu = phase.frames.cpuMeanMs != null ? `CPU ${phase.frames.cpuMeanMs} ms/frame, ` : "";
481
+ const rate = phase.frames.frames < 2 ? `draws ${phase.frames.frames} frame(s) in ${phase.durationSeconds} s` : `runs at ${phase.frames.fps} FPS`;
482
+ notes.push(`Phase "${phase.name}" ${rate} (${cpu}main thread ${phase.busyPct}% busy)${phase.lists.yoursTop[0] ? `; your heaviest code: ${phase.lists.yoursTop[0].name} (${phase.lists.yoursTop[0].msPerFrame != null ? `${phase.lists.yoursTop[0].msPerFrame} ms/frame` : `${phase.lists.yoursTop[0].ms} ms`})` : ""}${lead ? `; heaviest stack entry: ${lead.name}` : ""}.`);
483
+ }
484
+ // The first-visit stutter signature: long frames the main thread spent waiting, right after new pipelines were created.
485
+ const waits = analysis.worstFrames.filter((frame) => frame.kind === "wait" && frame.intervalMs >= 250 && frame.scriptMs < frame.intervalMs * 0.25);
486
+ if (waits.length) {
487
+ const withCompiles = waits.filter((frame) => frame.pipelinesNear > 0);
488
+ notes.push(`${waits.length} frame(s) of ${waits.map((frame) => `${frame.intervalMs} ms at ${frame.t}s`).slice(0, 4).join(", ")} were spent waiting, not running script (main thread ${waits.map((frame) => frame.scriptMs).slice(0, 4).join(" / ")} ms)` +
489
+ (withCompiles.length ? ` — ${withCompiles.length} of them right after new GPU pipelines / programs were created: the GPU process compiling shaders.${analysis.gpuCold ? " This capture was GPU-cold (--gpu-cold), as on a first visit." : " A later visit may hide it (shader caches): confirm with --gpu-cold true."}` : ` — GPU-bound frames or browser work; check draws and the GPU timers.`));
451
490
  }
452
491
  const shaderBuckets = analysis.timeline.filter((bucket) => bucket.newShaders > 0);
453
492
  if (shaderBuckets.length) {
@@ -534,11 +573,19 @@ function printSummary(analysis) {
534
573
  for (const phase of analysis.phases) {
535
574
  line(` ${phase.name.padEnd(14)} ${String(phase.durationSeconds + "s").padStart(6)} ${String(phase.frames.fps ?? "-").padStart(5)} fps p95 ${String(phase.frames.intervalP95Ms ?? "-").padStart(6)} ms cpu ${String(phase.frames.cpuMeanMs ?? "-").padStart(6)} ms/frame busy ${String(phase.busyPct).padStart(3)}% particles ${phase.frames.particles ?? "-"} draws ${phase.frames.draws ?? "-"}`);
536
575
  line(` owners: ${phase.lists.owners.slice(0, 6).map((entry) => `${entry.owner} ${entry.pctBusy}%`).join(" · ")}`);
537
- line(` yours: ${phase.lists.yoursTop.slice(0, 3).map((entry) => `${entry.name} ${entry.msPerFrame}ms/f`).join(" · ") || "-"}`);
576
+ line(` yours: ${phase.lists.yoursTop.slice(0, 3).map((entry) => (entry.msPerFrame != null ? `${entry.name} ${entry.msPerFrame}ms/f` : `${entry.name} ${entry.ms}ms`)).join(" · ") || "-"}`);
577
+ if (phase.gpuCompiles && (phase.gpuCompiles.pipelines || phase.gpuCompiles.shaderModules)) line(` gpu: ${phase.gpuCompiles.pipelines} pipelines / programs, ${phase.gpuCompiles.shaderModules} shader modules created`);
538
578
  if (phase.heap) line(` alloc: ${phase.heap.mbPerSecond} MB/s · ${phase.heap.selfTop.slice(0, 3).map((entry) => `${entry.name} ${entry.mbPerSecond}MB/s`).join(" · ")}`);
539
579
  line(` hot: ${phase.lists.inclusiveTop.filter((entry) => entry.owner !== "program" && !entry.shell).slice(0, 5).map((entry) => `${entry.name} ${entry.pctBusy}%`).join(" · ")}`);
540
580
  }
541
581
  line();
582
+ line("WORST FRAMES — cpu: the frame's own work · wait: a gap the main thread mostly waited through (GPU, browser)");
583
+ for (const frame of analysis.worstFrames.slice(0, 8)) {
584
+ const cost = frame.kind === "cpu" ? `${frame.cpuMs} ms cpu` : `${frame.intervalMs} ms ${frame.kind}`;
585
+ line(` ${String(frame.t + "s").padStart(8)} ${cost.padEnd(16)} ${String(frame.phase || "-").padEnd(14)} script ${frame.scriptMs} ms · ${frame.pipelinesNear} pipelines near · ${frame.top.slice(0, 2).map((entry) => `${entry.name} ${entry.ms}`).join(", ") || "-"}`);
586
+ }
587
+ if (analysis.gpuCold) line(` (--gpu-cold: ${analysis.gpuCold.tagged} fragment shaders made unique, so every pipeline compiled as on a first visit)`);
588
+ line();
542
589
  line("YOUR CODE — time spent, including the library work it triggers (whole run)");
543
590
  for (const entry of analysis.overall.lists.yoursTop.slice(0, 15)) {
544
591
  line(` ${String(entry.ms).padStart(8)} ms ${String(entry.pctBusy).padStart(5)}% ${entry.name} ${shortFile(entry.file)}:${entry.line} [own ${entry.ownCodeMs} ms; ${entry.callees.slice(0, 3).map((callee) => `${callee.name} ${callee.ms}`).join(", ")}]`);
@@ -603,6 +650,9 @@ function main() {
603
650
  startSeconds: round((phase.from - startMs) / 1000, 1),
604
651
  durationSeconds: round((phase.to - phase.from) / 1000, 1),
605
652
  busyPct: round((aggregate.busyMs / Math.max(aggregate.totalMs, 1e-6)) * 100, 0),
653
+ // A phase over before the first frame (the page loading) has no frame rate to judge — it is load time.
654
+ beforeFirstFrame: frames.length === 0 || phase.to <= frames[0].at,
655
+ gpuCompiles: compilesIn(probe.gpuCompiles, phase.from, phase.to),
606
656
  frames: stats,
607
657
  lists: topLists(aggregate, stats.frames, Math.min(top, 15), symbolizer.labels),
608
658
  };
@@ -638,7 +688,9 @@ function main() {
638
688
  phases: phaseResults,
639
689
  timeline: buildTimeline(prepared, info, frames, probe.counters, startMs, endMs),
640
690
  marks: probe.marks.map((mark) => ({ name: mark.name, t: round((mark.at - startMs) / 1000, 2) })),
641
- worstFrames: worstFrames(frames, prepared, info, phases, 15).map((frame) => ({ ...frame, t: round((frame.at - startMs) / 1000, 2) })),
691
+ worstFrames: worstFrames(frames, prepared, info, phases, probe.gpuCompiles, 15).map((frame) => ({ ...frame, t: round((frame.at - startMs) / 1000, 2) })),
692
+ gpuCompiles: (probe.gpuCompiles || []).map((entry) => ({ kind: entry.kind, t: round((entry.at - startMs) / 1000, 2) })),
693
+ gpuCold: capture.gpuCold || null,
642
694
  hotLines: lines,
643
695
  excerpts: [...excerptsFor.values()],
644
696
  heap: analyzeHeap(heapProfiles.map((entry) => entry.profile), symbolizer, (endMs - startMs) / 1000, top),
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Finds the Chrome (or Chromium) executable that bt-profile drives: the capture (profile.mjs) and the report's PDF
3
+ * (report.mjs). CHROME_PATH wins; otherwise the usual install locations on macOS, Linux and Windows.
4
+ */
5
+ import fs from "node:fs";
6
+
7
+ /** @returns {string} the path of a Chrome or Chromium executable */
8
+ export function findChrome() {
9
+ const candidates = [
10
+ process.env.CHROME_PATH,
11
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
12
+ "/Applications/Chromium.app/Contents/MacOS/Chromium",
13
+ "/usr/bin/google-chrome",
14
+ "/usr/bin/chromium",
15
+ "/usr/bin/chromium-browser",
16
+ "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
17
+ "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
18
+ ].filter(Boolean);
19
+ const found = candidates.find((candidate) => fs.existsSync(candidate));
20
+ if (found) return found;
21
+ throw new Error("No Chrome found. Set CHROME_PATH to a Chrome or Chromium executable.");
22
+ }