116 lines
12 KiB
JavaScript
116 lines
12 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { createRequire } from 'node:module';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
import { packageSource } from './package-source.mjs';
|
|
import { verifyOfficialContracts } from './official-contract-check.mjs';
|
|
import { createCiRun, ciEnvironment, loggedCommand, unpackVerifiedSource, verifySourceInputs, builtFiles, tapTotals, requireCompleteTests, fileDigest, writeNewJson } from './ci-utils.mjs';
|
|
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const args = process.argv.slice(2), options = { profile: 'contract', outputRoot: path.join(root, '.qa/ci'), python: process.env.AGENT_WORKBENCH_TEST_PYTHON };
|
|
if (args.includes('--help')) {
|
|
console.log('node scripts/ci.mjs [--profile contract|windows] [--python ABSOLUTE_BASE_PYTHON] [--output-root DIRECTORY]\nExports committed Git source to a new directory; performs a real npm ci, official contract/build/test checks and Windows smoke. No current node_modules, data, keys or engine installation are reused. Each invocation preserves its own logs and failures.');
|
|
process.exit(0);
|
|
}
|
|
for (let i=0;i<args.length;i++) {
|
|
const name = { '--profile':'profile', '--python':'python', '--output-root':'outputRoot' }[args[i]];
|
|
if (!name || !args[i+1]) throw new Error('Unsupported CI argument; use --help');
|
|
options[name] = args[++i];
|
|
}
|
|
assert.ok(['contract','windows'].includes(options.profile));
|
|
const run = createCiRun(options.outputRoot, options.profile), env = ciEnvironment(run);
|
|
const report = { schemaVersion: 1, profile: options.profile, startedAt: new Date().toISOString(), passed: false, root: run.root, evidence: run.evidence,
|
|
commands: [], scope: { realModelRequests: 0, realKeysRead: 0, formalDataOrConfigWrites: 0, deployment: false, remoteRunClaim: false },
|
|
limitations: ['Pinned source and controlled local checks do not prove live-provider compatibility, model delivery or Purge/cache gains.', 'GitAtlas runner and remote results must be independently confirmed.', 'Formal schema2 migration and post-activation rollback remain M3 acceptance.'] };
|
|
let stage = 'preflight';
|
|
const command = async (id, executable, argv, cwd = run.source, extraEnv = {}) => {
|
|
try { const result = await loggedCommand({ id, executable, args: argv, cwd, env: { ...env, ...extraEnv }, evidence: run.evidence }); report.commands.push(result); return result; }
|
|
catch(error) { if (error.record) report.commands.push(error.record); throw error; }
|
|
};
|
|
const nodeCommand = (id, argv, cwd, extraEnv) => command(id, process.execPath, argv, cwd, extraEnv);
|
|
const readOutput = id => fs.readFileSync(path.join(run.evidence, `${id}.stdout.log`), 'utf8').trim();
|
|
try {
|
|
const git = argv => execFileSync('git', argv, { cwd: root, encoding: 'utf8', windowsHide: true, env }).trim();
|
|
const inputPaths = ['src','public','scripts','tests','.gitea','package.json','package-lock.json','vite.config.ts','tsconfig.json','index.html','mobile.html','.gitattributes'];
|
|
report.source = { commit: git(['rev-parse','HEAD']), tree: git(['rev-parse','HEAD^{tree}']), branch: git(['branch','--show-current']), worktreeStatus: git(['status','--porcelain=v1']) };
|
|
assert.equal(git(['diff','--name-only','HEAD','--',...inputPaths]), '', 'Commit program and test changes before CI; it verifies committed source only.');
|
|
assert.equal(git(['ls-files','--others','--exclude-standard','--',...inputPaths]), '', 'Untracked program or test inputs are not part of committed CI.');
|
|
const lock = JSON.parse(fs.readFileSync(path.join(root, 'scripts/ci-environment.json')));
|
|
assert.equal(lock.schemaVersion, 1); assert.equal(process.version, 'v'+lock.node, 'Use the pinned Node runtime');
|
|
if (options.profile === 'windows') assert.ok(process.platform === 'win32' && process.arch === 'x64', 'Windows profile requires actual Windows x64');
|
|
const npmCli = path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js');
|
|
assert.ok(fs.statSync(npmCli).isFile(), 'Use Node with its bundled npm');
|
|
await nodeCommand('npm-version', [npmCli,'--version'], run.tmp); assert.equal(readOutput('npm-version'), lock.npm, 'Pinned npm version required');
|
|
report.environment = { node: { version: process.version, executable: process.execPath, sha256: fileDigest(process.execPath) }, npm: { version: lock.npm, cli: npmCli, sha256: fileDigest(npmCli) }, platform: process.platform, arch: process.arch, ciLockSha256: fileDigest(path.join(root,'scripts/ci-environment.json')) };
|
|
if (options.profile === 'windows') {
|
|
assert.ok(options.python && path.isAbsolute(options.python) && fs.statSync(options.python).isFile(), '--python or AGENT_WORKBENCH_TEST_PYTHON must name the absolute base QA Python.');
|
|
await command('python-base', options.python, ['-I','-c','import sys,json; print(json.dumps({"version":sys.version.split()[0],"executable":sys.executable,"baseExecutable":sys._base_executable,"basePrefix":sys.base_prefix}))'], run.tmp);
|
|
const python = JSON.parse(readOutput('python-base')); assert.equal(python.version, lock.python, 'Pinned base Python version required');
|
|
report.environment.pythonBase = { ...python, sha256: fileDigest(options.python) };
|
|
}
|
|
stage = 'committed-source-export';
|
|
const archive = packageSource({ root, outputRoot: run.evidence });
|
|
writeNewJson(path.join(run.evidence,'source-archive.json'), archive);
|
|
report.source = { ...report.source, archiveSha256: archive.sha256, archiveFile: archive.path, packageLockSha256: archive.files.find(file=>file.path==='package-lock.json').sha256, files: archive.files.length };
|
|
report.sourceBefore = unpackVerifiedSource(archive, run.source);
|
|
assert.equal(fs.existsSync(path.join(run.source,'node_modules')), false, 'Fresh source must not have dependencies');
|
|
writeNewJson(path.join(run.evidence,'source-before.json'), report.sourceBefore);
|
|
stage = 'official-contracts';
|
|
await nodeCommand('official-registry', ['scripts/verify-official-model-registry.mjs',path.join(run.evidence,'official-registry-result.json')]);
|
|
await nodeCommand('official-contracts', ['scripts/verify-official-contracts.mjs','--output',path.join(run.evidence,'official-contracts-result.json')]);
|
|
const contracts = verifyOfficialContracts(run.source); report.contracts = { snapshotSha256: contracts.snapshotSha256, registryVersion: contracts.registryVersion, requestDefaultsVersion: contracts.requestDefaultsVersion, sources: contracts.sourceInventory.reduce((sum,item)=>sum+item.sources.length,0), records: contracts.recordsCount };
|
|
stage = 'fresh-npm-ci';
|
|
await nodeCommand('npm-ci', [npmCli,'ci','--no-audit','--no-fund']);
|
|
assert.equal(fs.lstatSync(path.join(run.source,'node_modules')).isSymbolicLink(), false, 'No node_modules junction reuse');
|
|
const packageLock = JSON.parse(fs.readFileSync(path.join(run.source,'package-lock.json'))), installed = {};
|
|
for (const name of ['playwright','playwright-core','electron','typescript','vite','esbuild','yaml']) {
|
|
const file = path.join(run.source,'node_modules',name,'package.json'), actual = JSON.parse(fs.readFileSync(file));
|
|
assert.equal(actual.version, packageLock.packages['node_modules/'+name].version, name+' differs from lock');
|
|
installed[name] = { version: actual.version, sha256: fileDigest(file) };
|
|
}
|
|
assert.equal(installed.playwright.version,lock.playwright); assert.equal(installed.electron.version,lock.electron);
|
|
report.environment.packages = installed;
|
|
await nodeCommand('dependency-tree', [npmCli,'ls','--all','--json']);
|
|
stage = 'workflow-contract';
|
|
await nodeCommand('workflow-check', ['scripts/verify-ci-workflow.mjs',path.join(run.evidence,'workflow.json')]);
|
|
if (options.profile === 'windows') {
|
|
stage = 'fresh-python-qa';
|
|
const venv = path.join(run.root,'python-qa');
|
|
await command('python-venv',options.python,['-I','-m','venv',venv],run.tmp);
|
|
const python = path.join(venv,'Scripts/python.exe'); env.AGENT_WORKBENCH_TEST_PYTHON = python;
|
|
await command('python-install',python,['-I','-m','pip','--isolated','install','--disable-pip-version-check','--no-input','--require-hashes','--only-binary=:all:','--index-url','https://pypi.org/simple','--cache-dir',path.join(run.cache,'pip'),'-r',path.join(run.source,'scripts/ci-python-requirements.txt')],run.tmp);
|
|
await command('python-packages',python,['-I','-c','import sys,pytest,pip,json; print(json.dumps({"python":sys.version.split()[0],"pytest":pytest.__version__,"pip":pip.__version__,"executable":sys.executable,"prefix":sys.prefix,"basePrefix":sys.base_prefix}))'],run.tmp);
|
|
const pythonInfo = JSON.parse(readOutput('python-packages')); assert.equal(pythonInfo.python,lock.python); assert.equal(pythonInfo.pytest,lock.pytest); assert.equal(pythonInfo.pip,lock.pip); assert.notEqual(pythonInfo.prefix,pythonInfo.basePrefix);
|
|
report.environment.pythonQa = { ...pythonInfo, sha256: fileDigest(python), requirementsSha256: fileDigest(path.join(run.source,'scripts/ci-python-requirements.txt')) };
|
|
await command('python-pip-check',python,['-I','-m','pip','--isolated','check'],run.tmp);
|
|
stage = 'explicit-electron-install';
|
|
await nodeCommand('electron-install',['node_modules/electron/install.js']);
|
|
const require = createRequire(path.join(run.source,'package.json'));
|
|
const electron = require('electron'); assert.ok(fs.statSync(electron).isFile(), 'Actual Electron binary missing');
|
|
report.environment.electron = { executable: electron, sha256: fileDigest(electron) };
|
|
const { browserInstallation } = await import(pathToFileURL(path.join(run.source,'src/core/browser/browser-driver.mjs')).href);
|
|
const edge = browserInstallation(run.source); assert.equal(edge.status,'available','Actual Microsoft Edge is required; do not skip browser tests.'); report.environment.edge = edge;
|
|
}
|
|
stage = 'build'; await nodeCommand('build',[npmCli,'run','build']);
|
|
report.build = builtFiles(run.source); writeNewJson(path.join(run.evidence,'build-inputs.json'),report.build);
|
|
stage = 'tests';
|
|
const tests = options.profile === 'windows' ? fs.readdirSync(path.join(run.source,'tests')).filter(name=>name.endsWith('.test.mjs')).sort().map(name=>'tests/'+name) : [...contracts.testFiles,'tests/official-contracts-ci.test.mjs','tests/ci-pipeline.test.mjs','tests/source-package.test.mjs'];
|
|
const testFiles = [...new Set(tests)].sort();
|
|
writeNewJson(path.join(run.evidence,'test-inputs.json'),testFiles.map(file=>({file,sha256:fileDigest(path.join(run.source,file))})));
|
|
const testResult = await loggedCommand({id:'source-tests',executable:process.execPath,args:['--test','--test-concurrency=2','--test-reporter=tap',...testFiles],cwd:run.source,env,evidence:run.evidence,check:false}); report.commands.push(testResult);
|
|
report.tests = { files: testFiles.length, totals: tapTotals(readOutput('source-tests')), exitCode:testResult.exitCode, interrupted:testResult.interrupted };
|
|
assert.equal(testResult.exitCode,0,'Original source tests failed; preserve this run and fix the specific failure.'); assert.equal(testResult.interrupted,false); requireCompleteTests(report.tests.totals);
|
|
if (options.profile === 'windows') { stage='actual-browser-desktop-smoke'; await nodeCommand('desktop-smoke',['scripts/ci-desktop-smoke.mjs','--evidence',path.join(run.evidence,'desktop-smoke')]); report.desktopSmoke = JSON.parse(fs.readFileSync(path.join(run.evidence,'desktop-smoke/result.json'))); assert.equal(report.desktopSmoke.passed,true); }
|
|
stage='final-identity'; report.sourceAfter=verifySourceInputs(run.source,archive.files); assert.deepEqual(builtFiles(run.source),report.build,'Build artifacts changed during tests'); writeNewJson(path.join(run.evidence,'source-after.json'),report.sourceAfter);
|
|
report.passed=true;
|
|
} catch(error) {
|
|
report.failure={stage,name:error.name,message:error.message,stack:error.stack}; process.exitCode=1;
|
|
} finally {
|
|
report.finishedAt=new Date().toISOString(); writeNewJson(path.join(run.evidence,'report.json'),report);
|
|
const logFiles = fs.readdirSync(run.evidence).filter(name=>fs.statSync(path.join(run.evidence,name)).isFile()).sort();
|
|
writeNewJson(path.join(run.evidence,'MANIFEST.json'),{schemaVersion:1,files:logFiles.map(file=>({file,bytes:fs.statSync(path.join(run.evidence,file)).size,sha256:fileDigest(path.join(run.evidence,file))})),scope:'Top-level immutable command logs and reports; source/dependencies are separate owned execution inputs.'});
|
|
console.log(JSON.stringify({passed:report.passed,profile:options.profile,source:report.source?.commit,tests:report.tests,stage:report.failure?.stage,error:report.failure?.message,report:path.join(run.evidence,'report.json')}));
|
|
}
|