import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { createHash, randomUUID } from 'node:crypto'; import { spawn, execFileSync } from 'node:child_process'; import { projectEnvironment } from '../src/core/process-environment.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-packaged-build.mjs — extract compile inputs from the latest source ZIP, install offline, rebuild and compare every compiled file to its portable ZIP identity. Set AGENT_WORKBENCH_TEST_PYTHON to an absolute Python path. No model calls or formal-host changes.'); process.exit(0); } assert.equal(process.argv.length, 2); assert.match(process.version, /^v24\./); const release = JSON.parse(fs.readFileSync('releases/latest.json')); const manifest = JSON.parse(fs.readFileSync(path.join(release.portableRoot, 'MANIFEST.json'))); const root = path.resolve('.qa', 'packaged-build-' + randomUUID()), destination = path.join(root, 'source'); fs.mkdirSync(destination, { recursive: true }); const env = projectEnvironment(); const python = process.env.AGENT_WORKBENCH_TEST_PYTHON; assert.ok(python && path.isAbsolute(python) && fs.existsSync(python), 'Select an explicit QA Python.'); const npm = path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js'); assert.ok(fs.existsSync(npm), 'Use Node with its bundled npm.'); const sha = bytes => createHash('sha256').update(bytes).digest('hex'); async function digestFile(file) { const hash = createHash('sha256'); for await (const bytes of fs.createReadStream(file)) hash.update(bytes); return hash.digest('hex'); } const startedAt = new Date().toISOString(); const extract = `import sys,zipfile,pathlib,json,hashlib archive,destination,top=sys.argv[1:] folders={'src','public','scripts','tests','examples','benchmarks','research'} names={'package.json','package-lock.json','vite.config.ts','tsconfig.json','index.html','mobile.html'} output=pathlib.Path(destination).resolve() with zipfile.ZipFile(archive) as bundle: manifest=json.loads(bundle.read(top+'/MANIFEST.json')) selected=[item for item in manifest['files'] if item['path'].split('/')[0] in folders or item['path'] in names] assert manifest['kind']=='source' and selected for item in selected: relative=pathlib.PurePosixPath(item['path']) assert not relative.is_absolute() and '..' not in relative.parts contents=bundle.read(top+'/'+item['path']) assert len(contents)==item['bytes'] and hashlib.sha256(contents).hexdigest()==item['sha256'] target=(output/str(relative)).resolve() assert target.is_relative_to(output) target.parent.mkdir(parents=True,exist_ok=True) target.write_bytes(contents) print(json.dumps({'version':manifest['version'],'inputs':selected}))`; const logs = []; async function run(args, name) { const file = path.join(root, name + '.log'), log = fs.openSync(file, 'wx'); let code; try { code = await new Promise((resolve, reject) => { const child = spawn(process.execPath, [npm, ...args], { cwd: destination, env, windowsHide: true, stdio: ['ignore', log, log] }); child.once('error', reject); child.once('close', resolve); }); } finally { fs.closeSync(log); } logs.push({ name, file, code, sha256: await digestFile(file) }); assert.equal(code, 0, name + ': inspect original log ' + file); console.log('PASS ' + name); } let result, stage = 'source archive identity', inputs = [], compiled = [], mismatches = []; try { assert.equal(await digestFile(release.source.path), release.source.sha256); stage = 'extract fixed compile inputs'; const extraction = JSON.parse(execFileSync(python, ['-I', '-c', extract, release.source.path, destination, path.basename(release.sourceRoot)], { env, encoding: 'utf8', windowsHide: true, maxBuffer: 8 * 1024 * 1024 })); assert.equal(extraction.version, manifest.version); inputs = extraction.inputs; stage = 'offline install'; await run(['ci', '--offline', '--ignore-scripts', '--no-audit', '--no-fund'], 'offline-install'); stage = 'actual source build'; await run(['run', 'build'], 'build'); stage = 'source and compiled bytes'; for (const input of inputs) assert.equal(await digestFile(path.join(destination, input.path)), input.sha256, 'Source input changed: ' + input.path); const folders = ['dist-client', 'dist-server', 'dist-desktop']; const walk = folder => fs.readdirSync(path.join(destination, folder), { withFileTypes: true }) .flatMap(entry => entry.isDirectory() ? walk(path.posix.join(folder, entry.name)) : [path.posix.join(folder, entry.name)]); const expected = manifest.files.filter(item => folders.some(folder => item.path.startsWith(folder + '/'))); assert.deepEqual(folders.flatMap(walk).sort(), expected.map(item => item.path).sort(), 'Exact compiled directory'); for (const item of expected) { const file = path.join(destination, item.path), actual = { bytes: fs.statSync(file).size, sha256: await digestFile(file) }; assert.equal(await digestFile(path.join(release.portableRoot, item.path)), item.sha256, 'Portable input drift: ' + item.path); compiled.push({ path: item.path, expected: { bytes: item.bytes, sha256: item.sha256 }, actual }); if (actual.bytes !== item.bytes || actual.sha256 !== item.sha256) mismatches.push(compiled.at(-1)); } assert.deepEqual(mismatches, [], 'Source ZIP rebuild differs from portable compilation.'); result = { success: true }; } catch (error) { result = { success: false, stage, error: error.stack }; process.exitCode = 1; } result = { ...result, startedAt, finishedAt: new Date().toISOString(), version: manifest.version, sourceCommit: release.documentation.sourceCommit, root, destination, sourceZip: release.source, portableManifestSha256: await digestFile(path.join(release.portableRoot, 'MANIFEST.json')), node: { version: process.version, sha256: await digestFile(process.execPath) }, inputCount: inputs.length, inputs, compiled, mismatches, logs, realModelRequests: 0, scope: 'Independent compile-input extraction from the exact source ZIP, offline dependency installation and actual TypeScript/Vite/mobile/server/desktop rebuild. Every source input retained and every compiled file compared. Historical source tests and full archive checks keep their separate receipts.' }; const output = path.join(root, 'result.json'); fs.writeFileSync(output, JSON.stringify(result, null, 2) + '\n', { flag: 'wx' }); console.log(JSON.stringify({ success: result.success, stage: result.stage, root, output, inputCount: inputs.length, compiledFiles: compiled.length, mismatches: mismatches.length, error: result.error }));