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'; const workspace = process.cwd(); const evidence = JSON.parse(fs.readFileSync('.qa/archives-validation.json', 'utf8')); assert.equal(evidence.success, true); assert.ok(evidence.extractionRoot, 'Run python scripts/verify-archives.py --extract-source first.'); const sourceRoot = fs.realpathSync.native(evidence.extractionRoot); const archiveQaRoot = path.resolve(process.env.AGENT_WORKBENCH_ARCHIVE_QA_ROOT || '.qa'); assert.ok(sourceRoot.startsWith(archiveQaRoot + path.sep)); const npmCli = path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js'); assert.ok(fs.existsSync(npmCli), 'Use Node with its bundled npm.'); const files = (root, relative = '') => fs.readdirSync(path.join(root, relative), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)) .flatMap(entry => entry.isDirectory() ? files(root, path.join(relative, entry.name)) : [path.join(relative, entry.name)]); const digest = file => createHash('sha256').update(fs.readFileSync(file)).digest('hex'); const expected = Object.fromEntries(['dist-client', 'dist-server'].flatMap(folder => files(path.join(workspace, folder)).map(relative => [path.join(folder, relative), digest(path.join(workspace, folder, relative))]))); const allowedEnv = /^(path|pathext|systemroot|systemdrive|windir|comspec|temp|tmp|userprofile|homedrive|homepath|home|appdata|localappdata|programfiles(?:\(x86\))?|commonprogramfiles(?:\(x86\))?|lang|lc_all|AGENT_WORKBENCH_TEST_PYTHON|AGENT_WORKBENCH_DEEPSEEK_ROOT)$/i; const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => allowedEnv.test(key))); const inheritedPath = Object.entries(env).find(([key]) => key.toLowerCase() === 'path')?.[1] || ''; for (const key of Object.keys(env)) if (key.toLowerCase() === 'path') delete env[key]; env.PATH = [path.dirname(process.execPath), inheritedPath].filter(Boolean).join(path.delimiter); const directory = path.join(sourceRoot, '.qa', 'package-validation-' + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const inputFolders = ['src', 'tests', 'scripts']; const inputNames = [...inputFolders.flatMap(folder => files(path.join(sourceRoot, folder)).map(relative => path.join(folder, relative))), 'package.json', 'package-lock.json', 'vite.config.ts', 'tsconfig.json', 'index.html'].sort(); const snapshot = () => inputNames.map(relative => ({ path: relative.replaceAll('\\', '/'), bytes: fs.statSync(path.join(sourceRoot, relative)).size, sha256: digest(path.join(sourceRoot, relative)) })); const inputs = snapshot(); fs.writeFileSync(path.join(directory, 'inputs.json'), JSON.stringify(inputs, null, 2)); let stage = 'environment preflight', python = null; const resultFile = path.join(workspace, '.qa/source-package-validation.json'); const logs = []; function save(result) { fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); fs.writeFileSync(resultFile, JSON.stringify(result, null, 2)); } function run(args, logName) { console.log('Source package: ' + stage); return new Promise((resolve, reject) => { const logPath = path.join(directory, logName); const outputFile = fs.createWriteStream(logPath); const child = spawn(process.execPath, args, { cwd: sourceRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }); let tail = ''; const capture = bytes => { outputFile.write(bytes); tail = (tail + bytes.toString('utf8')).slice(-12000); }; child.stdout.on('data', capture); child.stderr.on('data', capture); const timer = setTimeout(() => child.kill(), 600000); child.once('error', error => { clearTimeout(timer); outputFile.end(); reject(error); }); child.once('close', code => { clearTimeout(timer); outputFile.end(() => { logs.push({ stage, path: logPath, sha256: digest(logPath), bytes: fs.statSync(logPath).size }); code === 0 ? resolve(tail) : reject(new Error(stage + ' failed (' + code + '): ' + tail)); }); }); }); } try { assert.equal(JSON.parse(fs.readFileSync(path.join(sourceRoot, 'package.json'), 'utf8')).version, evidence.version); assert.match(process.version, /^v24\./, 'Source package validation requires Node.js 24.'); if (env.AGENT_WORKBENCH_TEST_PYTHON) { assert.ok(path.isAbsolute(env.AGENT_WORKBENCH_TEST_PYTHON) && fs.existsSync(env.AGENT_WORKBENCH_TEST_PYTHON), 'AGENT_WORKBENCH_TEST_PYTHON must name an existing absolute QA interpreter.'); const executable = fs.realpathSync.native(env.AGENT_WORKBENCH_TEST_PYTHON); python = JSON.parse(execFileSync(executable, ['-I', '-c', 'import json,sys,pytest; print(json.dumps({"version":sys.version.split()[0],"pytest":pytest.__version__}))'], { env, windowsHide: true, encoding: 'utf8', timeout: 10000 })); assert.ok(Number(python.version.split('.')[0]) === 3 && Number(python.version.split('.')[1]) >= 10, 'QA Python must be 3.10 or newer.'); assert.equal(python.pytest, '8.4.2', 'Install the fixed QA requirements before validating the source package.'); python.path = executable; env.AGENT_WORKBENCH_TEST_PYTHON = executable; } console.log(JSON.stringify({ sourceRoot, directory, nodeVersion: process.version, python })); stage = 'install'; await run([npmCli, 'ci', '--offline', '--no-audit', '--no-fund'], 'install.log'); stage = 'build'; await run([npmCli, 'run', 'build'], 'build.log'); const actualFiles = ['dist-client', 'dist-server'].flatMap(folder => files(path.join(sourceRoot, folder)).map(relative => path.join(folder, relative))); assert.deepEqual(actualFiles.slice().sort(), Object.keys(expected).sort()); for (const [relative, sha256] of Object.entries(expected)) assert.equal(digest(path.join(sourceRoot, relative)), sha256, relative); let actualRea = null; if (['1.8.0', '1.9.0', '1.10.0', '1.10.1', '1.11.0'].includes(evidence.version)) { stage = 'contained REA'; const reaOutput = await run(['scripts/verify-rea.mjs', '--app-root', sourceRoot, '--contained'], 'rea.log'); actualRea = JSON.parse(reaOutput.trim().split('\n').at(-1)); assert.equal(actualRea.success, true); assert.equal(actualRea.version, evidence.version); assert.equal(actualRea.results.length, 3); assert.ok(actualRea.results.every(item => item.rootExited === true)); } let actualEngine = null; if (['1.9.0', '1.10.0', '1.10.1', '1.11.0'].includes(evidence.version) && process.argv.includes('--native-engines')) { stage = 'native DeepSeek SDK'; const output = await run(['scripts/verify-engines.mjs'], 'engines.log'); actualEngine = JSON.parse(output.trim().split('\n').at(-1)); assert.equal(actualEngine.success, true); assert.equal(actualEngine.version, evidence.version); assert.equal(actualEngine.modelRequests, 0); assert.equal(actualEngine.rootExited, true); assert.equal(actualEngine.nativeMethods.length, 6); } let actualBrowser = null; if (['1.10.0', '1.10.1', '1.11.0'].includes(evidence.version)) { stage = 'contained browser runtime'; const output = await run(['scripts/verify-browser.mjs', '--app-root', sourceRoot, '--contained'], 'browser.log'); actualBrowser = JSON.parse(output.trim().split('\n').at(-1)); assert.equal(actualBrowser.success, true); assert.equal(actualBrowser.version, evidence.version); assert.equal(actualBrowser.rootExited, true); assert.equal(actualBrowser.results.length, 2); assert.ok(actualBrowser.results.every(value => value.status === 'passed' && value.browserExited)); assert.equal(actualBrowser.modelRequests, 0); } stage = 'tests'; const output = await run(['--test', '--test-concurrency=2', '--test-reporter=tap', ...files(path.join(sourceRoot, 'tests')).filter(file => !file.includes(path.sep) && file.endsWith('.test.mjs')).map(file => path.join('tests', file))], 'tests.tap'); const stats = Object.fromEntries([...output.matchAll(/^# (tests|pass|fail|cancelled|skipped|todo) (\d+)\s*$/gm)].map(match => [match[1], Number(match[2])])); assert.ok(stats.tests > 0); assert.equal(stats.pass, stats.tests); for (const key of ['fail', 'cancelled', 'skipped', 'todo']) assert.equal(stats[key], 0); assert.deepEqual(snapshot(), inputs, 'Source package inputs changed during validation.'); const result = { success: true, version: evidence.version, testedAt: new Date().toISOString(), sourceRoot, directory, nodeVersion: process.version, python, offlineInstall: true, inputs, inputFiles: inputs.length, logs, buildMatchedFiles: Object.keys(expected).length, tests: stats, actualRea, actualEngine, actualBrowser, credentialsHandling: 'Only build/runtime environment paths are inherited; no model configuration or credentials are used.' }; save(result); console.log(JSON.stringify({ success: true, version: result.version, sourceRoot, directory, tests: stats, nodeVersion: process.version, python, inputFiles: inputs.length, buildMatchedFiles: result.buildMatchedFiles, rea: !!actualRea?.success, engine: !!actualEngine?.success, browser: !!actualBrowser?.success })); } catch (error) { save({ success: false, version: evidence.version, testedAt: new Date().toISOString(), stage, error: error.message, sourceRoot, directory, nodeVersion: process.version, python, inputs, logs }); throw error; }