import assert from 'node:assert/strict'; import {readFileSync, writeFileSync} from 'node:fs'; import {resolve, join} from 'node:path'; import {pathToFileURL} from 'node:url'; // Reuse the frozen capture's service assertions against hash-checked saved // bytes. QMP provenance is checked separately. No guest or debugger operation // is performed by this replay, and replay output never creates guest evidence. export async function createDurationServiceReplay({captureSource, outputDirectory, readArtifact}) { const source = readArtifact(captureSource).toString(); const start = source.indexOf(' const data={};for(const[name,size]of Object.entries(workerSymbols))'); const end = source.indexOf(' const before=gdb.wholeTranscript.length;if(live)'); const headerEnd = source.indexOf('export async function captureWorkers('); assert.ok(headerEnd > 0 && start > headerEnd && end > start); let header = source.slice(0, headerEnd); header = header.replace(/from (['"])\.\/([^'"]+)\1/g, (_, quote, name) => 'from ' + JSON.stringify(pathToFileURL(resolve('scripts/os-acceptance', name)).href)); const code = header + ` export async function replay({capture: original, readArtifact, index, symbols, memoryMiB, bootId, fixturePath, cpuCount, live=false, expectedWindows=3}) { const secondaryCount=cpuCount-1, checkpoint={index}, consumed=[]; async function capture(name,val,size,symbol) { const requested=original.captures[consumed.length]; assert.ok(requested);for(const[k,v]of Object.entries({name,val,size,symbol}))assert.equal(requested[k],v); const bytes=readArtifact(original.dumps[name]);assert.equal(bytes.length,size);consumed.push(name);return bytes; } ` + source.slice(start, end) + ` assert.deepEqual(consumed,original.captures.map(row=>row.name)); return {serviceChecksPassed:true,physicalReadsReplayed:consumed.length}; } `; const path = join(outputDirectory, 'service-replay.mjs'); writeFileSync(path, code); const {replay} = await import(pathToFileURL(resolve(path)).href); assert.equal(typeof replay, 'function'); return {replay, path, captureSource}; }