Skip to content

Commit d74afc7

Browse files
vanceingallsclaude
andcommitted
feat(engine): measure live DOM size on every render, not just probed ones
The short-comp routing gate can only read a live element count when a probe session exists, and the first v0.7.83 data shows that is far rarer than estimated: 17% of renders (86/503), not the ">=28%" the video-presence proxy suggested. The other 83% fall back to a static source scan, which is exactly blind to the shape that motivated the live count — small markup, thousands of script-created nodes. That leaves the fleet element-count distribution unknowable for most renders, and the observed distribution is already surprising: p99 ~900, max 1,420 against a 2,500 ceiling calibrated on 7k/20k/40k synthetic nodes. Either the ceiling is close to irrelevant, or the large-DOM tail is hiding in the 83% we cannot see. Both readings change what PR B should do, and neither is decidable from probed renders alone (they are a biased sample — they got a probe *because* they carry media or unresolved compositions). So measure it where every render already goes: capture-session init. `collectSessionInitTelemetry` gains a querySelectorAll("*") count beside the tween count it already collects, riding the same channel to `observability_init_element_count`. This is observational only — capture has begun, far too late to route on — and it deliberately does not feed the gate. It answers the distribution question the gate cannot. Coverage for this channel is proven rather than assumed: the tween-count fix that shipped in v0.7.83 took the clamped-parallel bucket from 0/272 renders to 217/217, and 23.1% -> 100% overall. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 5244dde commit d74afc7

8 files changed

Lines changed: 99 additions & 17 deletions

File tree

‎packages/cli/src/telemetry/events.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export interface RenderObservabilityTelemetryPayload {
9898
observabilityExtractCacheMisses?: number;
9999
observabilityInitDurationMs?: number;
100100
observabilityInitTweenCount?: number;
101+
observabilityInitElementCount?: number;
101102
}
102103

103104
function renderObservabilityEventProperties(props: RenderObservabilityTelemetryPayload) {
@@ -157,6 +158,7 @@ function renderObservabilityEventProperties(props: RenderObservabilityTelemetryP
157158
observability_extract_cache_misses: props.observabilityExtractCacheMisses,
158159
observability_init_duration_ms: props.observabilityInitDurationMs,
159160
observability_init_tween_count: props.observabilityInitTweenCount,
161+
observability_init_element_count: props.observabilityInitElementCount,
160162
};
161163
}
162164

‎packages/cli/src/telemetry/renderObservability.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function renderObservabilityTelemetryPayload(
6666
observabilityExtractCacheMisses: extraction?.cacheMisses,
6767
observabilityInitDurationMs: init?.initDurationMs,
6868
observabilityInitTweenCount: init?.tweenCount,
69+
observabilityInitElementCount: init?.elementCount,
6970
};
7071
}
7172

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ export interface CaptureSession {
119119
initTelemetry?: {
120120
initDurationMs: number;
121121
tweenCount: number;
122+
/** Live DOM element count at end of init — observational; see collectSessionInitTelemetry. */
123+
elementCount: number;
122124
};
123125
capturePerf: {
124126
frames: number;
@@ -406,8 +408,24 @@ function appendBrowserDiagnostic(session: CaptureSession, text: string): void {
406408
async function collectSessionInitTelemetry(
407409
page: Page,
408410
initStart: number,
409-
): Promise<{ initDurationMs: number; tweenCount: number }> {
411+
): Promise<{ initDurationMs: number; tweenCount: number; elementCount: number }> {
410412
const initDurationMs = Date.now() - initStart;
413+
// Live DOM size, measured once the init sequence has completed so
414+
// script-generated elements are present. This is the SAME quantity the
415+
// short-comp routing gate wants, but measured here it is observational
416+
// only — capture has already started, so it is far too late to route on.
417+
// Its job is coverage: the routing gate can only read a live count on the
418+
// ~17% of renders that get a probe session, which leaves the fleet
419+
// element-count distribution unknowable for the rest (and hides exactly
420+
// the dangerous shape — small source markup, huge runtime DOM). Every
421+
// render reaches this path, so the distribution becomes readable even
422+
// where the gate stays blind.
423+
let elementCount = 0;
424+
try {
425+
elementCount = await page.evaluate(() => document.querySelectorAll("*").length);
426+
} catch {
427+
elementCount = 0;
428+
}
411429
let tweenCount = 0;
412430
try {
413431
tweenCount = await page.evaluate(() => {
@@ -431,7 +449,7 @@ async function collectSessionInitTelemetry(
431449
} catch {
432450
tweenCount = 0;
433451
}
434-
return { initDurationMs, tweenCount };
452+
return { initDurationMs, tweenCount, elementCount };
435453
}
436454

437455
async function recordSessionInitTelemetry(
@@ -442,7 +460,7 @@ async function recordSessionInitTelemetry(
442460
session.initTelemetry = telemetry;
443461
appendBrowserDiagnostic(
444462
session,
445-
`[FrameCapture:INIT] complete initDurationMs=${telemetry.initDurationMs} tweenCount=${telemetry.tweenCount}`,
463+
`[FrameCapture:INIT] complete initDurationMs=${telemetry.initDurationMs} tweenCount=${telemetry.tweenCount} elementCount=${telemetry.elementCount}`,
446464
);
447465
}
448466

@@ -3788,6 +3806,7 @@ export function getCapturePerfSummary(session: CaptureSession): CapturePerfSumma
37883806
subTimelineWaitOutcome: session.subTimelineWaitOutcome,
37893807
initDurationMs: session.initTelemetry?.initDurationMs,
37903808
initTweenCount: session.initTelemetry?.tweenCount,
3809+
initElementCount: session.initTelemetry?.elementCount,
37913810
warnings: cloneCaptureWarnings(session.warnings),
37923811
staticDedupReused: session.staticDedupCount ?? 0,
37933812
staticDedupEnabled: session.staticDedupEnabled ?? false,

‎packages/engine/src/types.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ export interface CapturePerfSummary {
253253
initDurationMs?: number;
254254
/** GSAP tween count at init — the motion-axis signal for capture routing analysis. */
255255
initTweenCount?: number;
256+
/**
257+
* Live DOM element count at end of init. Observational counterpart to the
258+
* short-comp routing gate's own count: the gate can only measure the ~17%
259+
* of renders that get a probe session, so without this the fleet
260+
* element-count distribution — and any large-runtime-DOM tail — stays
261+
* invisible for the rest.
262+
*/
263+
initElementCount?: number;
256264
/** Correctness warnings observed before or during capture. */
257265
warnings?: CaptureWarning[];
258266
/**

‎packages/producer/src/services/render/observability.test.ts‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,18 +416,33 @@ describe("init observability fallback (parallel workers)", () => {
416416
const summary = makeRecorder().summary({
417417
lastBrowserConsole: ["[FrameCapture:NAV] page.goto start"],
418418
capture: { forceScreenshot: false, captureMode: "screenshot" },
419-
initFallback: { initDurationMs: 850, tweenCount: 1200 },
419+
initFallback: { initDurationMs: 850, tweenCount: 1200, elementCount: 3400 },
420420
});
421-
expect(summary.init).toEqual({ initDurationMs: 850, tweenCount: 1200 });
421+
expect(summary.init).toEqual({ initDurationMs: 850, tweenCount: 1200, elementCount: 3400 });
422422
});
423423

424424
it("max-merges console INIT lines over the fallback, matching multi-session semantics", () => {
425425
const summary = makeRecorder().summary({
426-
lastBrowserConsole: ["[FrameCapture:INIT] complete initDurationMs=1234 tweenCount=42"],
426+
lastBrowserConsole: [
427+
"[FrameCapture:INIT] complete initDurationMs=1234 tweenCount=42 elementCount=5000",
428+
],
429+
capture: { forceScreenshot: false, captureMode: "screenshot" },
430+
initFallback: { initDurationMs: 850, tweenCount: 1200, elementCount: 3400 },
431+
});
432+
expect(summary.init).toEqual({ initDurationMs: 1234, tweenCount: 1200, elementCount: 5000 });
433+
});
434+
435+
// The single-session path has no structured fallback — it parses the console
436+
// line only. This is the path that covers renders the routing gate cannot
437+
// measure, so the element count must survive it.
438+
it("parses elementCount from the console INIT line with no fallback at all", () => {
439+
const summary = makeRecorder().summary({
440+
lastBrowserConsole: [
441+
"[FrameCapture:INIT] complete initDurationMs=90 tweenCount=7 elementCount=1420",
442+
],
427443
capture: { forceScreenshot: false, captureMode: "screenshot" },
428-
initFallback: { initDurationMs: 850, tweenCount: 1200 },
429444
});
430-
expect(summary.init).toEqual({ initDurationMs: 1234, tweenCount: 1200 });
445+
expect(summary.init).toEqual({ initDurationMs: 90, tweenCount: 7, elementCount: 1420 });
431446
});
432447

433448
it("stays undefined when neither source has anything", () => {

‎packages/producer/src/services/render/observability.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ export interface RenderExtractionObservability {
182182
export interface RenderInitObservability {
183183
initDurationMs?: number;
184184
tweenCount?: number;
185+
/**
186+
* Live DOM element count at end of capture-session init. Observational:
187+
* measured after routing has already been decided, so it cannot gate — it
188+
* exists because the routing gate's own count is only available on the
189+
* ~17% of renders that get a probe session, leaving the fleet
190+
* element-count distribution (and any large-runtime-DOM tail) unreadable
191+
* for the rest.
192+
*/
193+
elementCount?: number;
185194
}
186195

187196
export interface RenderObservabilitySummary {
@@ -299,13 +308,17 @@ function summarizeInitObservability(
299308
// let the console parse (same max semantics) refine it.
300309
let initDurationMs: number | undefined = fallback?.initDurationMs;
301310
let tweenCount: number | undefined = fallback?.tweenCount;
311+
let elementCount: number | undefined = fallback?.elementCount;
302312
for (const line of lines) {
303313
if (!line.includes("[FrameCapture:INIT]")) continue;
304314
initDurationMs = maxReading(initDurationMs, readUnsignedIntAfter(line, "initDurationMs="));
305315
tweenCount = maxReading(tweenCount, readUnsignedIntAfter(line, "tweenCount="));
316+
elementCount = maxReading(elementCount, readUnsignedIntAfter(line, "elementCount="));
317+
}
318+
if (initDurationMs === undefined && tweenCount === undefined && elementCount === undefined) {
319+
return undefined;
306320
}
307-
if (initDurationMs === undefined && tweenCount === undefined) return undefined;
308-
return { initDurationMs, tweenCount };
321+
return { initDurationMs, tweenCount, elementCount };
309322
}
310323

311324
// fallow-ignore-next-line complexity

‎packages/producer/src/services/renderOrchestrator.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,17 +1936,25 @@ describe("shouldPreferSingleWorkerDrawElement (DE priority inversion)", () => {
19361936
it("max-merges across workers and ignores workers that reported nothing", () => {
19371937
expect(
19381938
mergeWorkerInitObservability([
1939-
{ initDurationMs: 400, initTweenCount: 900 },
1939+
{ initDurationMs: 400, initTweenCount: 900, initElementCount: 1200 },
19401940
{},
1941-
{ initDurationMs: 1250, initTweenCount: 880 },
1941+
{ initDurationMs: 1250, initTweenCount: 880, initElementCount: 1190 },
19421942
]),
1943-
).toEqual({ initDurationMs: 1250, tweenCount: 900 });
1943+
).toEqual({ initDurationMs: 1250, tweenCount: 900, elementCount: 1200 });
19441944
});
19451945

19461946
it("returns undefined when no worker reported — summary.init must stay absent, not zeroed", () => {
19471947
expect(mergeWorkerInitObservability([])).toBeUndefined();
19481948
expect(mergeWorkerInitObservability([{}, {}])).toBeUndefined();
19491949
});
1950+
1951+
it("surfaces an element count even when a worker reported nothing else", () => {
1952+
expect(mergeWorkerInitObservability([{ initElementCount: 4000 }])).toEqual({
1953+
initDurationMs: undefined,
1954+
tweenCount: undefined,
1955+
elementCount: 4000,
1956+
});
1957+
});
19501958
});
19511959

19521960
describe("countElementTags", () => {

‎packages/producer/src/services/renderOrchestrator.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,15 @@ export async function resolveCompositionElementCount(
13451345
* initializes before the timeline is fully wired, not an expected disagreement.
13461346
*/
13471347
export function mergeWorkerInitObservability(
1348-
perfs: ReadonlyArray<{ initDurationMs?: number; initTweenCount?: number }>,
1349-
): { initDurationMs?: number; tweenCount?: number } | undefined {
1348+
perfs: ReadonlyArray<{
1349+
initDurationMs?: number;
1350+
initTweenCount?: number;
1351+
initElementCount?: number;
1352+
}>,
1353+
): { initDurationMs?: number; tweenCount?: number; elementCount?: number } | undefined {
13501354
let initDurationMs: number | undefined;
13511355
let tweenCount: number | undefined;
1356+
let elementCount: number | undefined;
13521357
for (const perf of perfs) {
13531358
if (perf.initDurationMs !== undefined) {
13541359
initDurationMs =
@@ -1360,9 +1365,20 @@ export function mergeWorkerInitObservability(
13601365
tweenCount =
13611366
tweenCount === undefined ? perf.initTweenCount : Math.max(tweenCount, perf.initTweenCount);
13621367
}
1368+
// Max across workers: every worker loads the same composition, so they
1369+
// should agree — max is defensive against a worker sampled before its
1370+
// init script finished populating the DOM.
1371+
if (perf.initElementCount !== undefined) {
1372+
elementCount =
1373+
elementCount === undefined
1374+
? perf.initElementCount
1375+
: Math.max(elementCount, perf.initElementCount);
1376+
}
1377+
}
1378+
if (initDurationMs === undefined && tweenCount === undefined && elementCount === undefined) {
1379+
return undefined;
13631380
}
1364-
if (initDurationMs === undefined && tweenCount === undefined) return undefined;
1365-
return { initDurationMs, tweenCount };
1381+
return { initDurationMs, tweenCount, elementCount };
13661382
}
13671383

13681384
/**

0 commit comments

Comments
 (0)