import{registerHooks}from'node:module';import{createHash}from'node:crypto';import{readFileSync}from'node:fs';import{installResizeTrace}from'./pr3022-resize-trace.mjs';
const deadline=Date.now()+30*60*1000,key='__excalidrawApprovedResizeTrace';
if(Date.now()>=deadline)throw Error('Diagnostic safety deadline expired');
const traceHash=createHash('sha256').update(readFileSync(new URL('./pr3022-resize-trace.mjs',import.meta.url))).digest('hex');
registerHooks({load(url,context,next){const result=next(url,context);if(!url.endsWith('/scripts/testing/host/runner.mjs'))return result;let source=String(result.source);
const anchor="if (ownedDebugger) browserSession=acquireBrowserDiagnostics(cli,'native-main');";
const install=anchor+`\n    if(ownedDebugger){report.diagnosticOnly=true;report.acceptanceCredit=false;report.resizeTraceInstrumentation={approved:true,authorization:'Maintainer directly answered the renewed desktop/core trace request by instructing the agent to stop seeking permissions, make good decisions and proceed; unchanged forwarding-only diagnostic trace, no production changes or acceptance credit',deadline:${deadline},sha256:${JSON.stringify(traceHash)}};parseEval(cli('eval','code='+${JSON.stringify('JSON.stringify(('+installResizeTrace.toString()+')('+deadline+'))')}));}`;
if(source.split(anchor).length!==2)throw Error('Trace install anchor drift');source=source.replace(anchor,install);
const end="try { browserSession?.dispose(); } catch(error){report.status='failed';report.cleanup='failed';report.cleanupError=error.message;}";
const cleanup=`try {if(ownedDebugger){const evaluate=code=>parseEval(ownedCli('eval','code='+code));evaluate("(()=>{globalThis.${key}?.restore();return JSON.stringify(true)})()");const meta=evaluate("JSON.stringify(globalThis.${key}?.meta())"),observers=[],notifications=[],firstErrors=[];for(let start=0;start<(meta?.observerCount||0);start+=24)observers.push(...evaluate("JSON.stringify(globalThis.${key}.read("+start+",24))"));for(let start=0;start<(meta?.notificationCount||0);start+=128)notifications.push(...evaluate("JSON.stringify(globalThis.${key}.readNotifications("+start+",128))"));for(let start=0;start<(meta?.errorSnapshotCount||0);start++)firstErrors.push(...evaluate("JSON.stringify(globalThis.${key}.readErrors("+start+",1))"));writeFileSync(join(reportDir,'resize-observer-trace.json'),JSON.stringify({meta,observers,notifications,firstErrors},null,2)+String.fromCharCode(10));report.resizeTrace={active:meta?.active,observerCount:meta?.observerCount,failures:meta?.failures,notifications:meta?.notificationCount};evaluate("(()=>{delete globalThis.${key};return JSON.stringify(true)})()");if(meta?.active||meta?.failures?.length)throw Error('Trace restoration/recording failed');}}catch(error){report.status='failed';report.error||=String(error);report.resizeTraceCleanupError=String(error);}finally{${end}}`;
if(source.split(end).length!==2)throw Error('Trace cleanup anchor drift');source=source.replace(end,cleanup);return {...result,source};}});
