@snap/valdi 1.1.5 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.metadata/.bazelrc.template +9 -3
- package/.metadata/MODULE.bazel.template +112 -3
- package/.metadata/bzl/patches/rules_python/1.9.0/BUILD.bazel.template +1 -0
- package/.metadata/bzl/patches/rules_python/1.9.0/remove_setuptools_spaces.patch.template +96 -0
- package/README.md +35 -1
- package/bundled-skills/registry.json +2 -1
- package/bundled-skills/skills/valdi-animations/skill.md +49 -0
- package/bundled-skills/skills/valdi-async/skill.md +3 -2
- package/bundled-skills/skills/valdi-bazel/skill.md +2 -6
- package/bundled-skills/skills/valdi-compiler/skill.md +4 -4
- package/bundled-skills/skills/valdi-cpp-runtime/skill.md +18 -0
- package/bundled-skills/skills/valdi-error-handling/skill.md +1 -1
- package/bundled-skills/skills/valdi-gestures/skill.md +3 -3
- package/bundled-skills/skills/valdi-migrate/skill.md +2 -1
- package/bundled-skills/skills/valdi-mock-breakdown/skill.md +4 -4
- package/bundled-skills/skills/valdi-mock-to-module/skill.md +9 -6
- package/bundled-skills/skills/valdi-overview/skill.md +2 -1
- package/bundled-skills/skills/valdi-perf/skill.md +121 -5
- package/bundled-skills/skills/valdi-pr-review/skill.md +189 -0
- package/bundled-skills/skills/valdi-profile/skill.md +5 -3
- package/bundled-skills/skills/valdi-setup/skill.md +3 -6
- package/bundled-skills/skills/valdi-testing/skill.md +1 -1
- package/bundled-skills/skills/valdi-tsx/skill.md +47 -11
- package/debugger/README.md +356 -0
- package/debugger/debugger-actions.js +373 -0
- package/debugger/debugger-api.js +225 -0
- package/debugger/debugger-bootstrap.js +295 -0
- package/debugger/debugger-model.js +790 -0
- package/debugger/debugger-performance.js +573 -0
- package/debugger/debugger-preview-html.js +676 -0
- package/debugger/debugger-providers.js +389 -0
- package/debugger/debugger-render.js +529 -0
- package/debugger/debugger-runtime.js +387 -0
- package/debugger/debugger-session.js +188 -0
- package/debugger/debugger-settings.js +209 -0
- package/debugger/debugger-state.js +221 -0
- package/debugger/debugger-tree-model.js +539 -0
- package/debugger/debugger.css +1865 -0
- package/debugger/devtools-panel.css +1318 -0
- package/debugger/devtools-panel.html +273 -0
- package/debugger/devtools-panel.js +3329 -0
- package/debugger/index.html +279 -0
- package/dist/commands/bootstrap.js +6 -5
- package/dist/commands/build.js +6 -0
- package/dist/commands/debugger.js +163 -0
- package/dist/commands/doctor.js +5 -125
- package/dist/commands/inspect.js +5 -2
- package/dist/commands/inspect_commands/input.js +176 -0
- package/dist/commands/inspect_commands/snapshot.js +1 -1
- package/dist/commands/inspect_commands/tree.js +1 -1
- package/dist/commands/install.js +6 -0
- package/dist/commands/projectsync.js +15 -0
- package/dist/commands/webHotreload.js +31 -0
- package/dist/core/constants.js +26 -7
- package/dist/debugger/chromiumConsole.js +279 -0
- package/dist/debugger/inputClient.js +473 -0
- package/dist/debugger/server.js +3801 -0
- package/dist/debugger/targetRegistry.js +436 -0
- package/dist/debugger/webPreviewPerformance.js +864 -0
- package/dist/index.js +0 -0
- package/dist/setup/DevSetupHelper.js +0 -3
- package/dist/setup/linuxSetup.js +0 -14
- package/dist/setup/macOSSetup.js +1 -2
- package/dist/utils/BazelClient.js +34 -5
- package/dist/utils/applicationUtils.js +18 -0
- package/dist/utils/chromiumDevToolsClient.js +350 -0
- package/dist/utils/daemonClient.js +364 -44
- package/dist/utils/hermesClient.js +82 -176
- package/dist/utils/linuxDistro.js +0 -21
- package/dist/utils/loopbackHost.js +14 -0
- package/dist/utils/owlCdpClient.js +282 -0
- package/dist/utils/owlDevToolsExtension.js +142 -0
- package/dist/utils/skillsAdapters.js +40 -14
- package/dist/utils/stringUtils.js +13 -1
- package/package.json +4 -2
- package/dist/commands/inspect_commands/snapshot.spec.js +0 -105
- package/dist/core/packageFiles.spec.js +0 -29
- package/dist/preview/previewGenerator.spec.js +0 -203
- package/dist/preview/viewModelParser.spec.js +0 -171
- package/dist/utils/linuxDistro.spec.js +0 -170
- package/dist/utils/skillsAdapters.spec.js +0 -60
- package/dist/utils/stringUtils.spec.js +0 -81
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
// Renderer trace and Hermes CPU profile UI behavior.
|
|
2
|
+
function readSecondsInput(input, maximumSeconds) {
|
|
3
|
+
const value = Number.parseFloat(input.value);
|
|
4
|
+
if (!Number.isFinite(value)) return 5;
|
|
5
|
+
return Math.max(0.1, Math.min(maximumSeconds, value));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function formatMs(value) {
|
|
9
|
+
if (!Number.isFinite(value)) return 'n/a';
|
|
10
|
+
return `${value.toFixed(value >= 100 ? 0 : 1)} ms`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function traceSummaryText(result) {
|
|
14
|
+
const summary = result?.summary;
|
|
15
|
+
if (!summary || !result.traceCount) return 'No renderer trace captured.';
|
|
16
|
+
const topComponent = summary.topComponents?.[0];
|
|
17
|
+
const topTrigger = summary.topViewModelTriggers?.[0];
|
|
18
|
+
const parts = [
|
|
19
|
+
`<strong>${result.traceCount}</strong> events`,
|
|
20
|
+
`<strong>${summary.durationTraceCount || 0}</strong> durations`,
|
|
21
|
+
'process-wide capture',
|
|
22
|
+
];
|
|
23
|
+
if (result.captureTarget?.contextId) {
|
|
24
|
+
parts.push(`requested from ${escapeHtml(String(result.captureTarget.contextId))}`);
|
|
25
|
+
}
|
|
26
|
+
if (result.droppedTraceEventCount) parts.push(`${result.droppedTraceEventCount} dropped`);
|
|
27
|
+
if (result.traceEventLimitReached) parts.push('native trace limit reached');
|
|
28
|
+
if (result.elapsedMs !== undefined) parts.push(`window ${formatMs(result.elapsedMs)}`);
|
|
29
|
+
if (topComponent) parts.push(`top render ${escapeHtml(topComponent.name)} ${formatMs(topComponent.durationMs)}`);
|
|
30
|
+
if (topTrigger) parts.push(`top trigger ${escapeHtml(topTrigger.name)} x${topTrigger.count}`);
|
|
31
|
+
return parts.join(' · ');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function traceEventsHtml(result) {
|
|
35
|
+
const traces = Array.isArray(result?.traces) ? result.traces : [];
|
|
36
|
+
if (!traces.length) return '';
|
|
37
|
+
const baseMicros = traces.reduce((minimum, trace) => Math.min(minimum, trace.startMicros), traces[0].startMicros);
|
|
38
|
+
const visibleTraces = traces.slice(0, 80);
|
|
39
|
+
const eventLimitText =
|
|
40
|
+
visibleTraces.length === traces.length ? '' : ` (first ${visibleTraces.length} of ${traces.length})`;
|
|
41
|
+
const rows = visibleTraces.map(trace => {
|
|
42
|
+
const durationMs = Math.max(0, trace.endMicros - trace.startMicros) / 1000;
|
|
43
|
+
const details = JSON.stringify(
|
|
44
|
+
{
|
|
45
|
+
name: trace.trace,
|
|
46
|
+
threadId: trace.threadId,
|
|
47
|
+
startMs: (trace.startMicros - baseMicros) / 1000,
|
|
48
|
+
durationMs,
|
|
49
|
+
},
|
|
50
|
+
null,
|
|
51
|
+
2,
|
|
52
|
+
);
|
|
53
|
+
return `
|
|
54
|
+
<details class="trace-event-row">
|
|
55
|
+
<summary class="trace-event-summary" title="${escapeHtml(trace.trace)}">
|
|
56
|
+
<div class="trace-event-name">${escapeHtml(trace.trace)}</div>
|
|
57
|
+
<div class="trace-event-meta">${escapeHtml(((trace.startMicros - baseMicros) / 1000).toFixed(2))} ms</div>
|
|
58
|
+
<div class="trace-event-meta">${escapeHtml(formatMs(durationMs))}</div>
|
|
59
|
+
</summary>
|
|
60
|
+
<pre class="trace-event-details">${escapeHtml(details)}</pre>
|
|
61
|
+
</details>
|
|
62
|
+
`;
|
|
63
|
+
});
|
|
64
|
+
return `
|
|
65
|
+
<div class="trace-events-header">
|
|
66
|
+
<div>Events${eventLimitText}</div>
|
|
67
|
+
<div>Start</div>
|
|
68
|
+
<div>Duration</div>
|
|
69
|
+
</div>
|
|
70
|
+
${rows.join('')}
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function buildPerformanceTracePerfettoPayload(result) {
|
|
75
|
+
const traces = Array.isArray(result?.traces) ? result.traces : [];
|
|
76
|
+
const minimumStartMicros = traces.reduce(
|
|
77
|
+
(minimum, trace) => Math.min(minimum, trace.startMicros),
|
|
78
|
+
traces[0]?.startMicros || 0,
|
|
79
|
+
);
|
|
80
|
+
const threadIds = Array.from(new Set(traces.map(trace => trace.threadId)))
|
|
81
|
+
.sort((left, right) => left - right)
|
|
82
|
+
.slice(0, 256);
|
|
83
|
+
const traceEvents = [
|
|
84
|
+
{
|
|
85
|
+
name: 'process_name',
|
|
86
|
+
ph: 'M',
|
|
87
|
+
pid: 1,
|
|
88
|
+
args: { name: 'Valdi' },
|
|
89
|
+
},
|
|
90
|
+
...threadIds.map(threadId => ({
|
|
91
|
+
name: 'thread_name',
|
|
92
|
+
ph: 'M',
|
|
93
|
+
pid: 1,
|
|
94
|
+
tid: threadId,
|
|
95
|
+
args: { name: `Valdi thread ${threadId}` },
|
|
96
|
+
})),
|
|
97
|
+
];
|
|
98
|
+
for (const trace of traces) {
|
|
99
|
+
const instant = /(?:^|\.)Renderer\.viewModelChange\.[^.]+\..+$/.test(trace.trace);
|
|
100
|
+
const event = {
|
|
101
|
+
name: trace.trace,
|
|
102
|
+
cat: 'valdi',
|
|
103
|
+
ph: instant ? 'i' : 'X',
|
|
104
|
+
pid: 1,
|
|
105
|
+
tid: trace.threadId,
|
|
106
|
+
ts: trace.startMicros - minimumStartMicros,
|
|
107
|
+
};
|
|
108
|
+
if (instant) event.s = 't';
|
|
109
|
+
else event.dur = Math.max(0, trace.endMicros - trace.startMicros);
|
|
110
|
+
traceEvents.push(event);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
displayTimeUnit: 'ms',
|
|
114
|
+
metadata: result?.perfettoMetadata || {},
|
|
115
|
+
traceEvents,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function profileSummaryText(result) {
|
|
120
|
+
const summary = result?.summary;
|
|
121
|
+
if (!summary || !result.profile) return 'No CPU profile captured.';
|
|
122
|
+
const topFunction = summary.topFunctions?.[0];
|
|
123
|
+
const parts = [
|
|
124
|
+
`<strong>${summary.sampleCount || 0}</strong> samples`,
|
|
125
|
+
`<strong>${summary.nodeCount || 0}</strong> nodes`,
|
|
126
|
+
];
|
|
127
|
+
if (result.elapsedMs !== undefined) parts.push(`window ${formatMs(result.elapsedMs)}`);
|
|
128
|
+
if (topFunction) parts.push(`top function ${escapeHtml(topFunction.name)} x${topFunction.sampleCount}`);
|
|
129
|
+
return parts.join(' · ');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function normalizePerformanceTraceTarget(target) {
|
|
133
|
+
const port = Number(target?.port);
|
|
134
|
+
if (!Number.isFinite(port) || target?.clientId === undefined || target?.contextId === undefined) return null;
|
|
135
|
+
return {
|
|
136
|
+
port,
|
|
137
|
+
clientId: String(target.clientId),
|
|
138
|
+
contextId: String(target.contextId),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function performanceTraceTargetsMatch(left, right) {
|
|
143
|
+
const normalizedLeft = normalizePerformanceTraceTarget(left);
|
|
144
|
+
const normalizedRight = normalizePerformanceTraceTarget(right);
|
|
145
|
+
return Boolean(
|
|
146
|
+
normalizedLeft &&
|
|
147
|
+
normalizedRight &&
|
|
148
|
+
normalizedLeft.port === normalizedRight.port &&
|
|
149
|
+
normalizedLeft.clientId === normalizedRight.clientId &&
|
|
150
|
+
normalizedLeft.contextId === normalizedRight.contextId,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function syncActiveTraceState(result, fallbackTarget) {
|
|
155
|
+
state.performance.traceStateUnknown = false;
|
|
156
|
+
state.performance.traceActive = Boolean(result?.recording);
|
|
157
|
+
state.performance.traceResultPending = Boolean(result?.completedRecordingAvailable);
|
|
158
|
+
const resultContextId = result?.recording ? result.contextId : result?.completedContextId;
|
|
159
|
+
const resultTarget =
|
|
160
|
+
normalizePerformanceTraceTarget(result?.captureTarget) || normalizePerformanceTraceTarget(fallbackTarget);
|
|
161
|
+
if ((result?.recording || result?.completedRecordingAvailable) && resultTarget) {
|
|
162
|
+
state.performance.activeTraceTarget = {
|
|
163
|
+
...resultTarget,
|
|
164
|
+
contextId: resultContextId === undefined ? resultTarget.contextId : String(resultContextId),
|
|
165
|
+
};
|
|
166
|
+
} else if (!state.performance.traceCapturePending) {
|
|
167
|
+
state.performance.activeTraceTarget = null;
|
|
168
|
+
}
|
|
169
|
+
persistPerformanceTraceState();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function persistPerformanceTraceState() {
|
|
173
|
+
if (typeof persistDebuggerSessionState === 'function') persistDebuggerSessionState();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function markPerformanceTraceStateUnknown(target) {
|
|
177
|
+
const normalizedTarget = normalizePerformanceTraceTarget(target);
|
|
178
|
+
state.performance.traceStateUnknown = true;
|
|
179
|
+
if (normalizedTarget) state.performance.activeTraceTarget = normalizedTarget;
|
|
180
|
+
persistPerformanceTraceState();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function syncActiveProfileState(activeProfile) {
|
|
184
|
+
const contextId = activeProfile?.contextId;
|
|
185
|
+
state.performance.profileActive = Boolean(activeProfile?.profiling);
|
|
186
|
+
state.performance.activeProfileContextId =
|
|
187
|
+
state.performance.profileActive && contextId !== undefined && contextId !== null ? String(contextId) : null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function renderPerformance() {
|
|
191
|
+
const perf = state.performance;
|
|
192
|
+
const traceTargetAvailable = hasSelectedLiveTarget();
|
|
193
|
+
const traceSupported = perf.traceSupported !== false;
|
|
194
|
+
const traceBusy = perf.traceActive || perf.traceCapturePending || perf.traceResultPending || perf.traceStateUnknown;
|
|
195
|
+
elements.traceStatusPill.textContent = perf.traceCapturePending
|
|
196
|
+
? 'Capturing'
|
|
197
|
+
: perf.traceStateUnknown
|
|
198
|
+
? 'Unknown'
|
|
199
|
+
: perf.traceActive
|
|
200
|
+
? 'Recording'
|
|
201
|
+
: perf.traceResultPending
|
|
202
|
+
? 'Ready'
|
|
203
|
+
: 'Idle';
|
|
204
|
+
elements.traceStatusPill.className = `source-pill ${traceBusy ? 'live' : ''}`;
|
|
205
|
+
elements.traceStartButton.disabled = !traceTargetAvailable || !traceSupported || traceBusy;
|
|
206
|
+
elements.traceStopButton.disabled =
|
|
207
|
+
!traceTargetAvailable ||
|
|
208
|
+
(!perf.traceActive && !perf.traceResultPending && !perf.traceStateUnknown);
|
|
209
|
+
elements.traceCaptureButton.disabled = !traceTargetAvailable || !traceSupported || perf.traceCapturePending;
|
|
210
|
+
elements.traceCaptureButton.textContent =
|
|
211
|
+
perf.traceActive || perf.traceResultPending || perf.traceStateUnknown ? 'Stop & Capture' : 'Capture';
|
|
212
|
+
elements.rendererTracingToggle.disabled = traceBusy;
|
|
213
|
+
elements.traceDurationInput.disabled = traceBusy;
|
|
214
|
+
elements.traceExportButton.disabled = !Array.isArray(perf.lastTrace?.traces);
|
|
215
|
+
elements.traceSummary.innerHTML = traceSummaryText(perf.lastTrace);
|
|
216
|
+
elements.traceEvents.innerHTML = traceEventsHtml(perf.lastTrace);
|
|
217
|
+
|
|
218
|
+
elements.profileStatusPill.textContent = perf.profileActive ? 'Recording' : 'Idle';
|
|
219
|
+
elements.profileStatusPill.className = `source-pill ${perf.profileActive ? 'live' : ''}`;
|
|
220
|
+
elements.profileStartButton.disabled = perf.profileActive;
|
|
221
|
+
elements.profileStopButton.disabled = !perf.profileActive;
|
|
222
|
+
elements.profileCaptureButton.disabled = perf.profileActive;
|
|
223
|
+
elements.profileExportButton.disabled = !perf.lastProfile?.profile;
|
|
224
|
+
elements.profileSummary.innerHTML = profileSummaryText(perf.lastProfile);
|
|
225
|
+
|
|
226
|
+
elements.profileContextSelect.disabled = perf.profileActive;
|
|
227
|
+
const selectedContext = perf.activeProfileContextId || elements.profileContextSelect.value;
|
|
228
|
+
elements.profileContextSelect.innerHTML = [
|
|
229
|
+
`<option value="">Auto context</option>`,
|
|
230
|
+
...perf.profileContexts.map(context => {
|
|
231
|
+
const selected = context.id === selectedContext ? ' selected' : '';
|
|
232
|
+
return `<option value="${escapeHtml(context.id)}"${selected}>${escapeHtml(context.title || context.id)}</option>`;
|
|
233
|
+
}),
|
|
234
|
+
].join('');
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function refreshPerformanceTraceStatus(options) {
|
|
238
|
+
const targetParams =
|
|
239
|
+
normalizePerformanceTraceTarget(options.target) ||
|
|
240
|
+
normalizePerformanceTraceTarget(state.performance.activeTraceTarget) ||
|
|
241
|
+
normalizePerformanceTraceTarget(getSelectedTargetParams());
|
|
242
|
+
if (!targetParams) {
|
|
243
|
+
syncActiveTraceState(null);
|
|
244
|
+
state.performance.traceSupported = true;
|
|
245
|
+
if (options.render !== false) renderPerformance();
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
const result = await apiGet('/api/performance/trace/status', targetParams, {
|
|
251
|
+
timeoutMs: options.timeoutMs || 10000,
|
|
252
|
+
});
|
|
253
|
+
syncActiveTraceState(result, targetParams);
|
|
254
|
+
state.performance.traceSupported = result.tracingSupported === true;
|
|
255
|
+
return result;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (!options.silent) addLog('warn', 'trace', error.message);
|
|
258
|
+
return null;
|
|
259
|
+
} finally {
|
|
260
|
+
if (options.render !== false) renderPerformance();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function startPerformanceTrace() {
|
|
265
|
+
const targetParams = normalizePerformanceTraceTarget(getSelectedTargetParams());
|
|
266
|
+
if (!targetParams) return;
|
|
267
|
+
const wasUnknown = state.performance.traceStateUnknown === true;
|
|
268
|
+
try {
|
|
269
|
+
const status = await refreshPerformanceTraceStatus({ silent: true, render: false, target: targetParams });
|
|
270
|
+
if (!status) {
|
|
271
|
+
addLog('warn', 'trace', 'Could not verify renderer trace status; start was not sent.');
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (wasUnknown) {
|
|
275
|
+
addLog('info', 'trace', 'Recovering the uncertain renderer trace before starting another.');
|
|
276
|
+
await stopPerformanceTrace({ target: targetParams, render: false });
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (status?.recording || status?.completedRecordingAvailable) {
|
|
280
|
+
syncActiveTraceState(status, targetParams);
|
|
281
|
+
addLog('warn', 'trace', 'A renderer trace is active or waiting to be retrieved. Use Stop first.');
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (status.tracingSupported !== true) {
|
|
285
|
+
addLog('warn', 'trace', 'This Valdi runtime does not support renderer trace capture.');
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
markPerformanceTraceStateUnknown(targetParams);
|
|
289
|
+
const result = await apiPost('/api/performance/trace/start', targetParams, {
|
|
290
|
+
rendererTracing: elements.rendererTracingToggle.checked,
|
|
291
|
+
});
|
|
292
|
+
syncActiveTraceState(result, targetParams);
|
|
293
|
+
state.performance.traceSupported = result.tracingSupported !== false;
|
|
294
|
+
addLog('info', 'trace', `Started process-wide renderer trace from context ${targetParams.contextId}.`);
|
|
295
|
+
} catch (error) {
|
|
296
|
+
addLog('error', 'trace', error.message);
|
|
297
|
+
const status = await refreshPerformanceTraceStatus({ silent: true, render: false, target: targetParams });
|
|
298
|
+
if (!status) markPerformanceTraceStateUnknown(targetParams);
|
|
299
|
+
} finally {
|
|
300
|
+
renderPerformance();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async function stopPerformanceTrace(options) {
|
|
305
|
+
const targetParams =
|
|
306
|
+
normalizePerformanceTraceTarget(options.target) ||
|
|
307
|
+
normalizePerformanceTraceTarget(state.performance.activeTraceTarget) ||
|
|
308
|
+
normalizePerformanceTraceTarget(getSelectedTargetParams());
|
|
309
|
+
if (!targetParams) return false;
|
|
310
|
+
markPerformanceTraceStateUnknown(targetParams);
|
|
311
|
+
const applyResult = result => {
|
|
312
|
+
syncActiveTraceState(result, targetParams);
|
|
313
|
+
state.performance.traceSupported = result.tracingSupported === true;
|
|
314
|
+
state.performance.lastTrace = result;
|
|
315
|
+
addLog('info', 'trace', `Captured ${result.traceCount || 0} Valdi trace event(s).`);
|
|
316
|
+
if (result.completionError) addLog('warn', 'trace', result.completionError);
|
|
317
|
+
return true;
|
|
318
|
+
};
|
|
319
|
+
try {
|
|
320
|
+
const result = await apiPost('/api/performance/trace/stop', targetParams, {}, { timeoutMs: 30000 });
|
|
321
|
+
return applyResult(result);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
addLog('error', 'trace', error.message);
|
|
324
|
+
const status = await refreshPerformanceTraceStatus({ silent: true, render: false, target: targetParams });
|
|
325
|
+
if (!status) {
|
|
326
|
+
markPerformanceTraceStateUnknown(targetParams);
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
try {
|
|
330
|
+
const replayedResult = await apiPost('/api/performance/trace/stop', targetParams, {}, { timeoutMs: 30000 });
|
|
331
|
+
return applyResult(replayedResult);
|
|
332
|
+
} catch (replayError) {
|
|
333
|
+
addLog('error', 'trace', `Could not retrieve the retained renderer trace: ${replayError.message}`);
|
|
334
|
+
markPerformanceTraceStateUnknown(targetParams);
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
} finally {
|
|
338
|
+
if (options.render !== false) renderPerformance();
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async function preparePerformanceTraceTargetSwitch(nextTarget) {
|
|
343
|
+
const activeTarget = normalizePerformanceTraceTarget(state.performance.activeTraceTarget);
|
|
344
|
+
if (!activeTarget) {
|
|
345
|
+
if (state.performance.traceActive || state.performance.traceResultPending || state.performance.traceStateUnknown) {
|
|
346
|
+
addLog('warn', 'trace', 'Recover the uncertain renderer trace before switching targets.');
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
if (performanceTraceTargetsMatch(activeTarget, nextTarget)) return true;
|
|
352
|
+
if (state.performance.traceCapturePending) {
|
|
353
|
+
addLog('warn', 'trace', 'Wait for the process-wide renderer trace capture to finish before switching targets.');
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
if (!state.performance.traceActive && !state.performance.traceResultPending && !state.performance.traceStateUnknown) {
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
addLog('info', 'trace', 'Stopping the active process-wide renderer trace before switching targets.');
|
|
360
|
+
if (await stopPerformanceTrace({ target: activeTarget, render: false })) return true;
|
|
361
|
+
if (!(await isPerformanceTraceTargetVerifiedMissing(activeTarget))) return false;
|
|
362
|
+
|
|
363
|
+
state.performance.traceActive = false;
|
|
364
|
+
state.performance.traceResultPending = false;
|
|
365
|
+
state.performance.traceStateUnknown = false;
|
|
366
|
+
state.performance.activeTraceTarget = null;
|
|
367
|
+
state.performance.traceSupported = true;
|
|
368
|
+
persistPerformanceTraceState();
|
|
369
|
+
addLog('warn', 'trace', 'The previous trace target disconnected; discarded its unrecoverable trace state.');
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async function isPerformanceTraceTargetVerifiedMissing(target) {
|
|
374
|
+
let status;
|
|
375
|
+
try {
|
|
376
|
+
status = await apiGet('/api/status', { port: target.port }, { timeoutMs: 5000 });
|
|
377
|
+
} catch {
|
|
378
|
+
// A transport failure does not prove that the prior context disappeared.
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const portStatus = status?.ports?.find(candidate => Number(candidate.port) === target.port);
|
|
383
|
+
if (!portStatus) return false;
|
|
384
|
+
// inspectPort reports connected:false for transient connection and inventory failures too.
|
|
385
|
+
// Only a successfully connected inventory can prove that the prior target disappeared.
|
|
386
|
+
if (portStatus.connected !== true || !Array.isArray(portStatus.clients)) return false;
|
|
387
|
+
|
|
388
|
+
const client = portStatus.clients.find(candidate => String(candidate.client_id) === target.clientId);
|
|
389
|
+
if (!client) return true;
|
|
390
|
+
if (client.contextError !== null && client.contextError !== undefined) return false;
|
|
391
|
+
if (!Array.isArray(client.contexts)) return false;
|
|
392
|
+
return !client.contexts.some(context => String(context.id) === target.contextId);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async function recoverPerformanceTraceState() {
|
|
396
|
+
const target =
|
|
397
|
+
normalizePerformanceTraceTarget(state.performance.activeTraceTarget) ||
|
|
398
|
+
normalizePerformanceTraceTarget(getSelectedTargetParams());
|
|
399
|
+
if (!target) return;
|
|
400
|
+
const wasUnknown = state.performance.traceStateUnknown === true;
|
|
401
|
+
const status = await refreshPerformanceTraceStatus({ silent: true, render: false, target });
|
|
402
|
+
if (!status) {
|
|
403
|
+
if (wasUnknown) markPerformanceTraceStateUnknown(target);
|
|
404
|
+
renderPerformance();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (status.recording || status.completedRecordingAvailable || wasUnknown) {
|
|
408
|
+
const completedTarget = {
|
|
409
|
+
...target,
|
|
410
|
+
contextId: String(status.completedContextId || target.contextId),
|
|
411
|
+
};
|
|
412
|
+
const recovered = await stopPerformanceTrace({ target: completedTarget, render: false });
|
|
413
|
+
if (recovered) {
|
|
414
|
+
addLog('info', 'trace', 'Recovered the completed process-wide renderer trace from the runtime.');
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
renderPerformance();
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
async function capturePerformanceTrace() {
|
|
421
|
+
const durationMs = Math.round(readSecondsInput(elements.traceDurationInput, 15) * 1000);
|
|
422
|
+
const targetParams = normalizePerformanceTraceTarget(getSelectedTargetParams());
|
|
423
|
+
if (!targetParams) return;
|
|
424
|
+
const wasUnknown = state.performance.traceStateUnknown === true;
|
|
425
|
+
try {
|
|
426
|
+
const status = await refreshPerformanceTraceStatus({ silent: true, render: false, target: targetParams });
|
|
427
|
+
if (!status) {
|
|
428
|
+
addLog('warn', 'trace', 'Could not verify renderer trace status; capture was not sent.');
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (status.recording || status.completedRecordingAvailable || state.performance.traceActive || wasUnknown) {
|
|
432
|
+
addLog('info', 'trace', 'Stopping active renderer trace.');
|
|
433
|
+
await stopPerformanceTrace({ target: state.performance.activeTraceTarget || targetParams });
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (status.tracingSupported !== true) {
|
|
437
|
+
addLog('warn', 'trace', 'This Valdi runtime does not support renderer trace capture.');
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
state.performance.traceCapturePending = true;
|
|
441
|
+
markPerformanceTraceStateUnknown(targetParams);
|
|
442
|
+
renderPerformance();
|
|
443
|
+
addLog('info', 'trace', `Capturing renderer trace for ${(durationMs / 1000).toFixed(1)}s.`);
|
|
444
|
+
const result = await apiPost(
|
|
445
|
+
'/api/performance/trace/capture',
|
|
446
|
+
targetParams,
|
|
447
|
+
{
|
|
448
|
+
rendererTracing: elements.rendererTracingToggle.checked,
|
|
449
|
+
durationMs,
|
|
450
|
+
},
|
|
451
|
+
{ timeoutMs: durationMs + 15000 },
|
|
452
|
+
);
|
|
453
|
+
syncActiveTraceState(result, targetParams);
|
|
454
|
+
state.performance.traceSupported = result.tracingSupported === true;
|
|
455
|
+
state.performance.lastTrace = result;
|
|
456
|
+
addLog('info', 'trace', `Captured ${result.traceCount || 0} Valdi trace event(s).`);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
addLog('error', 'trace', error.message);
|
|
459
|
+
const status = await refreshPerformanceTraceStatus({ silent: true, render: false, target: targetParams });
|
|
460
|
+
if (status) {
|
|
461
|
+
await stopPerformanceTrace({ target: targetParams, render: false });
|
|
462
|
+
} else {
|
|
463
|
+
markPerformanceTraceStateUnknown(targetParams);
|
|
464
|
+
}
|
|
465
|
+
} finally {
|
|
466
|
+
state.performance.traceCapturePending = false;
|
|
467
|
+
if (
|
|
468
|
+
!state.performance.traceActive &&
|
|
469
|
+
!state.performance.traceResultPending &&
|
|
470
|
+
!state.performance.traceStateUnknown
|
|
471
|
+
) {
|
|
472
|
+
state.performance.activeTraceTarget = null;
|
|
473
|
+
}
|
|
474
|
+
renderPerformance();
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function exportPerformanceTrace() {
|
|
479
|
+
const result = state.performance.lastTrace;
|
|
480
|
+
if (!Array.isArray(result?.traces)) return;
|
|
481
|
+
downloadJson(
|
|
482
|
+
buildPerformanceTracePerfettoPayload(result),
|
|
483
|
+
`${targetFilePrefix()}-valdi-trace.json`,
|
|
484
|
+
'Exported Valdi trace JSON for Perfetto.',
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async function refreshProfileContexts(options) {
|
|
489
|
+
try {
|
|
490
|
+
const result = await apiGet('/api/performance/profile/contexts', {}, { timeoutMs: 5000 });
|
|
491
|
+
state.performance.profileContexts = result.contexts || [];
|
|
492
|
+
syncActiveProfileState(result.active);
|
|
493
|
+
if (!options.silent) {
|
|
494
|
+
addLog('info', 'profile', `Found ${state.performance.profileContexts.length} Hermes context(s).`);
|
|
495
|
+
}
|
|
496
|
+
} catch (error) {
|
|
497
|
+
if (!options.silent) addLog('error', 'profile', error.message);
|
|
498
|
+
} finally {
|
|
499
|
+
renderPerformance();
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function selectedProfileContextId() {
|
|
504
|
+
return elements.profileContextSelect.value || undefined;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
async function startCpuProfile() {
|
|
508
|
+
try {
|
|
509
|
+
const result = await apiPost(
|
|
510
|
+
'/api/performance/profile/start',
|
|
511
|
+
{},
|
|
512
|
+
{
|
|
513
|
+
contextId: selectedProfileContextId(),
|
|
514
|
+
},
|
|
515
|
+
);
|
|
516
|
+
syncActiveProfileState(result);
|
|
517
|
+
addLog(
|
|
518
|
+
'info',
|
|
519
|
+
'profile',
|
|
520
|
+
`Started CPU profile for ${result.contextTitle || result.contextId || 'Hermes context'}.`,
|
|
521
|
+
);
|
|
522
|
+
} catch (error) {
|
|
523
|
+
addLog('error', 'profile', error.message);
|
|
524
|
+
} finally {
|
|
525
|
+
renderPerformance();
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
async function stopCpuProfile() {
|
|
530
|
+
try {
|
|
531
|
+
const result = await apiPost('/api/performance/profile/stop', {}, {}, { timeoutMs: 65000 });
|
|
532
|
+
syncActiveProfileState(null);
|
|
533
|
+
state.performance.lastProfile = result;
|
|
534
|
+
addLog('info', 'profile', `Captured CPU profile with ${result.summary?.sampleCount || 0} sample(s).`);
|
|
535
|
+
} catch (error) {
|
|
536
|
+
addLog('error', 'profile', error.message);
|
|
537
|
+
} finally {
|
|
538
|
+
renderPerformance();
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
async function captureCpuProfile() {
|
|
543
|
+
const durationMs = Math.round(readSecondsInput(elements.profileDurationInput, 60) * 1000);
|
|
544
|
+
const contextId = selectedProfileContextId();
|
|
545
|
+
syncActiveProfileState({ profiling: true, contextId });
|
|
546
|
+
renderPerformance();
|
|
547
|
+
try {
|
|
548
|
+
addLog('info', 'profile', `Capturing CPU profile for ${(durationMs / 1000).toFixed(1)}s.`);
|
|
549
|
+
const result = await apiPost(
|
|
550
|
+
'/api/performance/profile/capture',
|
|
551
|
+
{},
|
|
552
|
+
{
|
|
553
|
+
contextId,
|
|
554
|
+
durationMs,
|
|
555
|
+
},
|
|
556
|
+
{ timeoutMs: durationMs + 15000 },
|
|
557
|
+
);
|
|
558
|
+
syncActiveProfileState(null);
|
|
559
|
+
state.performance.lastProfile = result;
|
|
560
|
+
addLog('info', 'profile', `Captured CPU profile with ${result.summary?.sampleCount || 0} sample(s).`);
|
|
561
|
+
} catch (error) {
|
|
562
|
+
addLog('error', 'profile', error.message);
|
|
563
|
+
await refreshProfileContexts({ silent: true });
|
|
564
|
+
} finally {
|
|
565
|
+
renderPerformance();
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function exportCpuProfile() {
|
|
570
|
+
const result = state.performance.lastProfile;
|
|
571
|
+
if (!result?.profile) return;
|
|
572
|
+
downloadJson(result.profile, `${targetFilePrefix()}-hermes.cpuprofile`, 'Exported Hermes CPU profile.');
|
|
573
|
+
}
|