@@ -5093,6 +5093,26 @@ export function computeDeVerifySampleFractions(k: number): number[] {
50935093 return [ ...Array . from ( { length : k - 1 } , ( _ , i ) => ( i + 1 ) / k ) , 0.95 ] ;
50945094}
50955095
5096+ /** Seeks the page the truth is read from; callbacks run unless suppressed (suppressed, a count-up's text is stale). */
5097+ export async function seekDeVerifyTruth (
5098+ page : Page ,
5099+ t : number ,
5100+ suppressEvents : boolean ,
5101+ ) : Promise < void > {
5102+ await page . evaluate (
5103+ ( tt : number , suppress : boolean ) => {
5104+ const hf = (
5105+ window as unknown as {
5106+ __hf ?: { seek ?: ( x : number , options ?: { suppressEvents ?: boolean } ) => void } ;
5107+ }
5108+ ) . __hf ;
5109+ if ( hf && typeof hf . seek === "function" ) hf . seek ( tt , { suppressEvents : suppress } ) ;
5110+ } ,
5111+ t ,
5112+ suppressEvents ,
5113+ ) ;
5114+ }
5115+
50965116async function captureDeVerificationFrames (
50975117 session : CaptureSession ,
50985118 page : Page ,
@@ -5142,47 +5162,47 @@ async function captureDeVerificationFrames(
51425162 // would share the corruption, PSNR would pass on the damaged output.
51435163 // Seeking 0 → ascending reproduces the render's own seek order.
51445164 const fractions = computeDeVerifySampleFractions ( k ) ;
5145- const seekTo = async ( t : number ) : Promise < void > => {
5146- await page . evaluate ( ( tt : number ) => {
5147- const hf = (
5148- window as unknown as {
5149- __hf ?: { seek ?: ( x : number , options ?: { suppressEvents ?: boolean } ) => void } ;
5150- }
5151- ) . __hf ;
5152- if ( hf && typeof hf . seek === "function" ) hf . seek ( tt , { suppressEvents : true } ) ;
5153- } , t ) ;
5154- } ;
5155- await seekTo ( quantizeTimeToFrame ( 0 , fps ) ) ;
5156- // Force one frame so lazy tween initialization paints at t=0 state.
5157- await pageScreenshotCapture ( page , session . options ) ;
5165+ // A video-free comp reads its truth on a disposable page: callbacks that run there
5166+ // cannot leave the render's own page changed. The video injector is tied to the render page.
5167+ const disposable = session . onBeforeCapture
5168+ ? null
5169+ : await createStaticVerificationPage ( session ) . catch ( ( ) => {
5170+ logInitPhase ( "self-verify truth page unavailable; reading it on the render page" ) ;
5171+ return null ;
5172+ } ) ;
5173+ const isolated = disposable !== null ;
5174+ const truthPage = disposable ?? page ;
5175+ const seekTo = ( t : number ) : Promise < void > => seekDeVerifyTruth ( truthPage , t , ! isolated ) ;
51585176 const frames = new Map < number , Buffer > ( ) ;
5159- for ( const f of fractions ) {
5160- let idx = Math . min ( totalFrames - 1 , Math . max ( 1 , Math . round ( totalFrames * f ) ) ) ;
5161- // Nudge off clip-cut boundaries (±1-frame desync is legitimate there);
5162- // if the nudge saturates on a boundary index, skip the sample entirely.
5163- let guard = 0 ;
5164- while ( boundary . has ( idx ) && guard ++ < 6 ) idx = Math . min ( totalFrames - 1 , idx + 2 ) ;
5165- if ( boundary . has ( idx ) ) continue ;
5166- if ( frames . has ( idx ) ) continue ;
5167- const t = quantizeTimeToFrame ( idx / fps , fps ) ;
5168- await seekTo ( t ) ;
5169- // Video frame injection (same hook the real capture paths run) — without
5170- // it, <video> elements screenshot black and every video comp would
5171- // false-positive into the screenshot fallback.
5172- if ( session . onBeforeCapture ) await session . onBeforeCapture ( page , t ) ;
5173- // Double-capture: the first screenshot forces a frame, which is what runs
5174- // rAF-driven callbacks (count-up text counters land a tick after seek()
5175- // returns — a single immediate screenshot captures stale text and
5176- // false-positives the verify: 3bea8c73 28.7dB vs a truth missing its stat
5177- // values while the DE frame was correct). NOTE: waiting on rAF via
5178- // evaluate instead deadlocks — headless only fires rAF when a frame is
5179- // produced, and nothing produces one until a screenshot asks.
5180- await pageScreenshotCapture ( page , session . options ) ;
5181- frames . set ( idx , await pageScreenshotCapture ( page , session . options ) ) ;
5177+ try {
5178+ await seekTo ( quantizeTimeToFrame ( 0 , fps ) ) ;
5179+ // Force one frame so lazy tween initialization paints at t=0 state.
5180+ await pageScreenshotCapture ( truthPage , session . options ) ;
5181+ for ( const f of fractions ) {
5182+ let idx = Math . min ( totalFrames - 1 , Math . max ( 1 , Math . round ( totalFrames * f ) ) ) ;
5183+ // Nudge off clip-cut boundaries (±1-frame desync is legitimate there);
5184+ // if the nudge saturates on a boundary index, skip the sample entirely.
5185+ let guard = 0 ;
5186+ while ( boundary . has ( idx ) && guard ++ < 6 ) idx = Math . min ( totalFrames - 1 , idx + 2 ) ;
5187+ if ( boundary . has ( idx ) ) continue ;
5188+ if ( frames . has ( idx ) ) continue ;
5189+ const t = quantizeTimeToFrame ( idx / fps , fps ) ;
5190+ await seekTo ( t ) ;
5191+ // Video frame injection (same hook the real capture paths run) — without
5192+ // it, <video> elements screenshot black and every video comp would
5193+ // false-positive into the screenshot fallback.
5194+ if ( session . onBeforeCapture ) await session . onBeforeCapture ( truthPage , t ) ;
5195+ // The first screenshot forces a frame, which runs rAF-driven callbacks (a count-up lands
5196+ // a tick after seek()); waiting on rAF via evaluate deadlocks, headless fires it only
5197+ // when a frame is produced.
5198+ await pageScreenshotCapture ( truthPage , session . options ) ;
5199+ frames . set ( idx , await pageScreenshotCapture ( truthPage , session . options ) ) ;
5200+ }
5201+ // The render's own page goes back to frame 0 so its first seek starts like an unverified render.
5202+ if ( ! isolated ) await seekTo ( quantizeTimeToFrame ( 0 , fps ) ) ;
5203+ } finally {
5204+ if ( isolated ) await truthPage . close ( ) . catch ( ( ) => { } ) ;
51825205 }
5183- // Leave the page at frame 0 so the render's first seek starts from the
5184- // same state as an unverified render.
5185- await seekTo ( quantizeTimeToFrame ( 0 , fps ) ) ;
51865206 session . deVerifyFrames = frames ;
51875207 logInitPhase (
51885208 `drawElement self-verify armed: ${ frames . size } ground-truth frame(s) @ [${ [ ...frames . keys ( ) ] . join ( ", " ) } ] of ${ totalFrames } ` ,
0 commit comments