Files
Agent-Workbench/scripts/m4-live/project.mjs
T
2026-11-11 22:58:21 +08:00

43 lines
4.4 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { execFileSync, spawnSync } from 'node:child_process';
import { gitEnvironment } from '../../src/core/git-process.mjs';
import { prepareProject as prepareSecondary, python, specs as secondarySpecs } from '../live-ae/project.mjs';
import { sha } from './common.mjs';
// The old project module supplies only preparation/check helpers. It does not
// load an old claim, connection, session, model result or real-batch entry point.
export { python };
export const specs = { node: { baseline: 'benchmarks/live-delivery-v1/node', allowed: ['src/amount.mjs', 'src/summary.mjs', 'src/report.mjs'], tests: 9 }, ...secondarySpecs };
export function prepareProject(kind, output, save) {
if (kind !== 'node') return prepareSecondary(kind, output, save);
const spec = specs.node, sourceRoot = path.join(output, 'project'); assert.equal(fs.existsSync(sourceRoot), false);
fs.mkdirSync(output, { recursive: true }); fs.cpSync(spec.baseline, sourceRoot, { recursive: true, errorOnExist: true });
const git = args => execFileSync('git', ['--no-pager', '--no-optional-locks', '-C', sourceRoot, ...args], { encoding: 'utf8', windowsHide: true, env: gitEnvironment() });
fs.writeFileSync(path.join(sourceRoot, 'README.md'), '# M4 independent Node baseline\nRepair the three src modules using the original nine tests. Preserve owner bytes and their staging.\n', { flag: 'wx' });
fs.writeFileSync(path.join(sourceRoot, '.gitignore'), 'dist/\n.awb-*\nnode_modules/\n', { flag: 'wx' });
for (const name of ['owner-staged.txt', 'owner-unstaged.txt']) fs.writeFileSync(path.join(sourceRoot, name), 'Owner baseline\n', { flag: 'wx' });
git(['init', '--initial-branch=main']); git(['config', 'core.autocrlf', 'false']); git(['config', 'user.name', 'Workbench QA']); git(['config', 'user.email', 'qa@workbench.invalid']);
git(['add', '--', '.']); git(['commit', '--no-gpg-sign', '-m', 'Seal M4 independent Node original baseline']); const initialHead = git(['rev-parse', 'HEAD']).trim();
fs.writeFileSync(path.join(sourceRoot, 'owner-staged.txt'), 'Owner staged change\n'); git(['add', '--', 'owner-staged.txt']);
fs.writeFileSync(path.join(sourceRoot, 'owner-staged.txt'), 'Owner unstaged over original staging\n');
fs.writeFileSync(path.join(sourceRoot, 'owner-unstaged.txt'), 'Owner unstaged change\n'); fs.writeFileSync(path.join(sourceRoot, 'owner-untracked.txt'), 'Owner untracked change\n', { flag: 'wx' });
const all = git(['ls-files', '-z']).split('\0').filter(Boolean), protectedNames = all.filter(name => !spec.allowed.includes(name)).concat('owner-untracked.txt');
const protectedBytes = Object.fromEntries(protectedNames.map(name => [name, fs.readFileSync(path.join(sourceRoot, name))]));
const index = git(['ls-files', '--stage', '--', ...protectedNames]);
const protect = () => {
for (const [name, bytes] of Object.entries(protectedBytes)) assert.deepEqual(fs.readFileSync(path.join(sourceRoot, name)), bytes, 'Protected file: ' + name);
assert.equal(git(['ls-files', '--stage', '--', ...protectedNames]), index, 'All protected original index entries');
};
const check = (name, args, prefix) => {
const execution = spawnSync(process.execPath, args, { cwd: sourceRoot, windowsHide: true, encoding: 'utf8', env: gitEnvironment(), maxBuffer: 16 * 1024 * 1024 });
save(prefix + '/' + name + '.txt', (execution.stdout ?? '') + (execution.stderr ?? ''));
const stats = name === 'test' ? Object.fromEntries(['tests', 'pass', 'fail', 'cancelled', 'skipped', 'todo'].map(key => [key, Number(execution.stdout?.match(new RegExp('^# ' + key + ' (\\d+)$', 'm'))?.[1] ?? NaN)])) : null;
return { status: execution.status, signal: execution.signal, error: execution.error?.message ?? null, stats, stdoutSha256: sha(execution.stdout ?? ''), stderrSha256: sha(execution.stderr ?? '') };
};
const checks = prefix => { const result = { test: check('test', ['--test', '--test-reporter=tap', 'test/expense.test.mjs'], prefix), build: check('build', ['scripts/build.mjs'], prefix) }; save(prefix + '/result.json', result); protect(); return result; };
save('source-before.json', { initialHead, status: git(['status', '--porcelain=v1']), protectedIndex: index, files: Object.fromEntries([...all, 'owner-untracked.txt'].map(name => [name, sha(fs.readFileSync(path.join(sourceRoot, name)))])) });
return { spec, sourceRoot, git, initialHead, protect, checks };
}