Files
Agent-Workbench/scripts/ci-utils.mjs
2026-11-07 22:59:31 +08:00

151 lines
9.7 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import { createHash, randomUUID } from 'node:crypto';
import { spawn } from 'node:child_process';
import { readSourceZip } from './package-source.mjs';
export const digest = bytes => createHash('sha256').update(bytes).digest('hex');
export const fileDigest = file => digest(fs.readFileSync(file));
export const writeNewJson = (file, value) => fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n', { flag: 'wx' });
export function binaryMirror(value) {
if (!value) return null;
const url = new URL(value);
assert.ok(url.protocol === 'https:' && !url.username && !url.password && !url.search && !url.hash && url.pathname.endsWith('/'), 'Binary mirror must be an explicit HTTPS directory without credentials.');
return url.href;
}
export async function loopbackPortStatus(port) {
assert.ok(Number.isSafeInteger(port) && port > 0 && port <= 65535, 'Recorded CI port required');
return new Promise((resolve, reject) => {
const socket = net.createConnection({ host: '127.0.0.1', port });
let settled = false;
const finish = (error, status) => {
if (settled) return; settled = true; clearTimeout(timer); socket.destroy();
if (error) reject(error); else resolve({ host: '127.0.0.1', port, status });
};
const timer = setTimeout(() => finish(new Error('Recorded CI port observation timed out')), 5000);
socket.once('connect', () => finish(null, 'accepting'));
socket.once('error', error => error.code === 'ECONNREFUSED' ? finish(null, 'refused') : finish(error));
});
}
export function createCiRun(outputRoot, profile) {
assert.ok(['contract', 'windows'].includes(profile), 'Unknown CI profile');
const root = path.resolve(outputRoot), run = path.join(root, `run-${profile}-${Date.now().toString(36)}-${randomUUID().slice(0,8)}`);
fs.mkdirSync(root, { recursive: true }); fs.mkdirSync(run);
for (const directory of ['evidence', 'source', 'cache', 'tmp']) fs.mkdirSync(path.join(run, directory));
return { root: run, evidence: path.join(run, 'evidence'), source: path.join(run, 'source'), cache: path.join(run, 'cache'), tmp: path.join(run, 'tmp') };
}
// Read only named OS variables. Never enumerate credential values or forward
// model settings, npm user config, provider tokens or external-engine paths.
export function ciEnvironment(run, input = process.env, node = process.execPath) {
const allowed = ['PATH','PATHEXT','SystemRoot','SystemDrive','WINDIR','ComSpec','TEMP','TMP','USERPROFILE','HOMEDRIVE','HOMEPATH','APPDATA','LOCALAPPDATA','ProgramFiles','ProgramFiles(x86)','CommonProgramFiles','HOME','LANG','LC_ALL'];
const env = {};
for (const key of Object.keys(input)) if (allowed.some(name=>name.toLowerCase() === key.toLowerCase())) env[key] = input[key];
const inherited = Object.keys(env).find(key=>key.toLowerCase()==='path');
const originalPath = inherited ? env[inherited] : '';
if (inherited) delete env[inherited];
env.PATH = [path.dirname(node), originalPath].filter(Boolean).join(path.delimiter);
env.TEMP = run.tmp; env.TMP = run.tmp; env.CI = 'true'; env.PYTEST_DISABLE_PLUGIN_AUTOLOAD = '1';
const userConfig = path.join(run.root, 'npm-user.conf'), globalConfig = path.join(run.root, 'npm-global.conf');
for (const file of [userConfig, globalConfig]) if (!fs.existsSync(file)) fs.writeFileSync(file, '', { flag: 'wx' });
env.npm_config_userconfig = userConfig; env.npm_config_globalconfig = globalConfig;
env.npm_config_registry = 'https://registry.npmjs.org/'; env.npm_config_cache = path.join(run.cache, 'npm');
env.npm_config_audit = 'false'; env.npm_config_fund = 'false'; env.npm_config_update_notifier = 'false';
env.PLAYWRIGHT_BROWSERS_PATH = path.join(run.cache, 'playwright');
env.electron_config_cache = path.join(run.cache, 'electron');
return env;
}
export async function loggedCommand({ id, executable, args = [], cwd, env, evidence, timeoutMs = 0, check = true }) {
assert.match(id, /^[a-z0-9-]+$/);
const stdoutFile = path.join(evidence, `${id}.stdout.log`), stderrFile = path.join(evidence, `${id}.stderr.log`);
let stdoutFd = fs.openSync(stdoutFile, 'wx'), stderrFd = fs.openSync(stderrFile, 'wx');
const startedAt = new Date().toISOString(), started = Date.now();
let child, timer, interrupted = false, spawnError;
const terminate = () => {
interrupted = true;
if (!child?.pid) return;
if (process.platform === 'win32') {
const stop = spawn('taskkill.exe', ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
stop.on('error', () => { try { child.kill(); } catch {} });
} else child.kill('SIGTERM');
};
const onInterrupt = () => terminate();
process.once('SIGINT', onInterrupt); process.once('SIGTERM', onInterrupt);
try {
const result = await new Promise(resolve => {
child = spawn(executable, args, { cwd, env, windowsHide: true, stdio: ['ignore', stdoutFd, stderrFd] });
child.once('spawn', () => writeNewJson(path.join(evidence, `${id}.started.json`), { id, executable, args, cwd, pid: child.pid, parentPid: process.pid, startedAt, environmentNames: Object.keys(env).sort() }));
child.once('error', error => { spawnError = { name: error.name, message: error.message, code: error.code }; });
child.once('close', (exitCode, signal) => resolve({ exitCode, signal }));
if (timeoutMs > 0) { timer = setTimeout(terminate, timeoutMs); timer.unref(); }
});
fs.closeSync(stdoutFd); stdoutFd = -1; fs.closeSync(stderrFd); stderrFd = -1;
const record = { id, executable, args, cwd, pid: child.pid ?? null, startedAt, finishedAt: new Date().toISOString(), durationMs: Date.now() - started, ...result, interrupted, spawnError: spawnError ?? null,
stdout: { file: stdoutFile, bytes: fs.statSync(stdoutFile).size, sha256: fileDigest(stdoutFile) }, stderr: { file: stderrFile, bytes: fs.statSync(stderrFile).size, sha256: fileDigest(stderrFile) } };
writeNewJson(path.join(evidence, `${id}.json`), record);
console.log(JSON.stringify({ command: id, exitCode: record.exitCode, interrupted, durationMs: record.durationMs, evidence }));
if (check && (record.exitCode !== 0 || record.signal || interrupted || spawnError)) throw Object.assign(new Error(`CI command failed: ${id}. Original logs: ${stdoutFile} and ${stderrFile}`), { record });
return record;
} finally {
clearTimeout(timer); process.removeListener('SIGINT', onInterrupt); process.removeListener('SIGTERM', onInterrupt);
// Spawn errors still produce original records; handles are closed above.
for (const fd of [stdoutFd, stderrFd]) if (fd >= 0) try { fs.closeSync(fd); } catch {}
}
}
export function unpackVerifiedSource(archive, destination) {
assert.ok(archive.kind === 'source-code' && /^[a-f0-9]{40}$/.test(archive.sourceCommit));
assert.equal(fileDigest(archive.path), archive.sha256, 'Source ZIP SHA changed');
const entries = readSourceZip(archive.path), prefix = `Agent-Workbench-${archive.version}-source/`;
assert.equal(entries.size, archive.files.length, 'Unexpected source archive entries');
const seen = new Set();
for (const file of archive.files) {
assert.ok(!seen.has(file.path), 'Duplicate source path'); seen.add(file.path);
assert.ok(!file.path.includes('\\') && !file.path.startsWith('/') && !file.path.split('/').some(segment=>segment==='..' || segment.includes(':')), 'Invalid source archive path');
const output = path.resolve(destination, file.path), bytes = entries.get(prefix + file.path);
assert.ok(output.startsWith(path.resolve(destination) + path.sep));
assert.ok(Buffer.isBuffer(bytes), `Missing archived input ${file.path}`);
assert.equal(bytes.length, file.bytes, file.path); assert.equal(digest(bytes), file.sha256, file.path);
fs.mkdirSync(path.dirname(output), { recursive: true }); fs.writeFileSync(output, bytes, { flag: 'wx' });
}
return verifySourceInputs(destination, archive.files);
}
export function verifySourceInputs(root, files) {
for (const file of files) {
const full = path.join(root, file.path); assert.ok(fs.lstatSync(full).isFile() && !fs.lstatSync(full).isSymbolicLink(), file.path);
assert.equal(fileDigest(full), file.sha256, `Source input changed: ${file.path}`);
}
return { passed: true, files: files.length, identity: 'exported ZIP bytes after committed .gitattributes, before and after actual dependency install/build/tests' };
}
export function builtFiles(root) {
const directories = ['dist-client','dist-server','dist-desktop'], files = [];
const walk = (relative) => {
for (const entry of fs.readdirSync(path.join(root, relative), { withFileTypes: true }).sort((a,b)=>a.name.localeCompare(b.name))) {
assert.equal(entry.isSymbolicLink(), false, 'Build output link');
const current = path.join(relative, entry.name);
if (entry.isDirectory()) walk(current);
else { assert.ok(entry.isFile()); const full = path.join(root, current); files.push({ file: current.split(path.sep).join('/'), bytes: fs.statSync(full).size, sha256: fileDigest(full) }); }
}
};
for (const directory of directories) { assert.ok(fs.statSync(path.join(root,directory)).isDirectory(), `Missing ${directory}`); walk(directory); }
return files;
}
export function tapTotals(text) {
const result = {};
for (const match of text.matchAll(/^# (tests|suites|pass|fail|cancelled|skipped|todo|duration_ms) (\d+(?:\.\d+)?)\s*$/gm)) result[match[1]] = Number(match[2]);
assert.ok(result.tests > 0 && Number.isInteger(result.pass), 'Missing full Node TAP totals');
return result;
}
export function requireCompleteTests(totals) {
for (const name of ['fail','cancelled','skipped','todo']) assert.equal(totals[name], 0, `CI ${name} is not a verified pass`);
assert.equal(totals.pass, totals.tests, 'CI not every collected test passed');
}