Skip to content

Commit 26ee898

Browse files
Maxwellimusclaude
andcommitted
fix(desktop): anchor the switch trace at the input event, not handler dispatch
Blind-review round 17 (Fable high) finding: startedAt sampled performance.now() at click-handler dispatch, silently excluding input delay — a click queued behind a long task under-reported by the whole queueing time, unbounded, in exactly the contention regime the tracer exists to expose. begin() now anchors at the dispatching event's timeStamp when one is present (window.event is set only during synchronous dispatch, so stale timestamps cannot leak in from async continuations; min() guards skewed clocks). Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Max Lampert <[email protected]>
1 parent c7c744c commit 26ee898

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎desktop/src/shared/lib/channelSwitchPerf.test.mjs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,32 @@ test("a scheduled route-exit abandon canceled in the same task keeps the trace",
458458
);
459459
});
460460

461+
test("the trace anchors at the input event, not handler dispatch", async () => {
462+
await withSettleHarness(async ({ begin, settle, flush }) => {
463+
// Real-clock gap: earlier tests fired visibilitychange listeners, and a
464+
// back-dated anchor overlapping those timestamps is (correctly) dropped
465+
// by the hidden-window guard. Let them age out first.
466+
await new Promise((resolve) => setTimeout(resolve, 600));
467+
// A click can sit queued behind a long task before its handler runs;
468+
// that input delay is felt switch latency and must be inside totalMs.
469+
// window.event is set only during synchronous dispatch, so this anchor
470+
// can never leak in from async continuations.
471+
globalThis.window.event = { timeStamp: performance.now() - 550 };
472+
begin("aaaa1111aaaa1111");
473+
delete globalThis.window.event;
474+
settle("aaaa1111aaaa1111");
475+
flush();
476+
const measure = performance
477+
.getEntriesByName("buzz:channel-switch:click-to-settled")
478+
.at(-1);
479+
assert.ok(measure, "measure recorded");
480+
assert.ok(
481+
measure.duration >= 550,
482+
`input delay must be inside the measure (got ${measure.duration})`,
483+
);
484+
});
485+
});
486+
461487
test("beginning a switch revokes a pending route-exit abandon for that channel", async () => {
462488
await withSettleHarness(
463489
async ({ begin, scheduleAbandon, settle, flush, measures }) => {

‎desktop/src/shared/lib/channelSwitchPerf.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,21 @@ export function beginChannelSwitchTrace(channelId: string): void {
252252
// cleanup's scheduled abandon pending; it must not kill the fresh trace
253253
// when its microtask drains.
254254
cancelRouteExitAbandon(channelId);
255+
// Anchor at the triggering input event when one is dispatching: a click
256+
// can sit queued behind a long task before its handler runs, and that
257+
// input delay is felt switch latency. window.event is set only during
258+
// synchronous dispatch, so a stale timestamp can never leak in from async
259+
// continuations; min() guards against skewed event clocks.
260+
const now = performance.now();
261+
const dispatchingEvent =
262+
typeof window === "undefined" ? undefined : window.event;
263+
const startedAt =
264+
dispatchingEvent && typeof dispatchingEvent.timeStamp === "number"
265+
? Math.min(dispatchingEvent.timeStamp, now)
266+
: now;
255267
activeTrace = {
256268
channelId,
257-
startedAt: performance.now(),
269+
startedAt,
258270
routeCommitAt: null,
259271
windowFetch: null,
260272
membersFetch: null,

0 commit comments

Comments
 (0)