Files
Agent-Workbench/scripts/live-ad/node.mjs
T

121 lines
13 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 { setTimeout as delay } from 'node:timers/promises';
import { gitEnvironment } from '../../src/core/git-process.mjs';
import { root, save, read, client, sha, captureSession, connectionDefinition, connectionInput, compareConfig } from './common.mjs';
const compatibility = read('compatibility/result.json'), frozen = read('current.json');
const priority = frozen.engineeringPriority;
assert.ok(Array.isArray(priority) && priority.length > 0);
const connectionId = priority.find(id => compatibility.results.some(value => value.connectionId === id && value.engineeringAllowed));
assert.equal(fs.existsSync(path.join(root, 'node')), false, 'Never replay a Node sample.');
if (!connectionId) { save('node/result.json', { success: false, executed: false, reason: 'No connection passed the predeclared compatibility gate.', reactPythonExecuted: false, purgeExecuted: false }); process.exit(0); }
const sourceRoot = path.join(root, 'node/expense-project'), allowed = ['src/amount.mjs', 'src/summary.mjs', 'src/report.mjs'];
const { api, boot, close } = await client(); let session, route, original, configured, selectedWritten = false, state, failure, result;
const git = args => execFileSync('git', ['--no-pager', '--no-optional-locks', '-C', sourceRoot, ...args], { encoding: 'utf8', windowsHide: true, env: gitEnvironment() });
let protectedBytes, staged, initialHead; const observations = [], reads = new Set(); let lastNovel = Date.now();
const protect = () => {
if (!protectedBytes) return;
for (const [name, bytes] of Object.entries(protectedBytes)) assert.deepEqual(fs.readFileSync(path.join(sourceRoot, name)), bytes, 'Protected file: ' + name);
assert.equal(git(['show', ':owner-staged.txt']), staged, 'Original owner staging is preserved.');
};
const check = (name, args, prefix) => {
const executed = spawnSync(process.execPath, args, { cwd: sourceRoot, windowsHide: true, encoding: 'utf8', env: gitEnvironment(), maxBuffer: 16 * 1024 * 1024 });
save(prefix + '/' + name + '.txt', (executed.stdout ?? '') + (executed.stderr ?? ''));
const stdout = executed.stdout ?? '', stats = name === 'test' ? Object.fromEntries(['tests','pass','fail','cancelled','skipped','todo'].map(key => [key, Number(stdout.match(new RegExp('^# ' + key + ' (\\d+)$', 'm'))?.[1] ?? NaN)])) : null;
return { status: executed.status, signal: executed.signal, error: executed.error?.message ?? null, stats, stdoutSha256: sha(stdout), stderrSha256: sha(executed.stderr ?? '') };
};
const testArgs = ['--test', '--test-reporter=tap', 'test/expense.test.mjs'];
const settle = async () => {
if (!route) return;
await api(route + '/stop', {}, 60000); // Stop/await only our new Task and its owned tools.
state = await api(route);
assert.ok(!['running','stopping','awaiting_approval'].includes(state.status), 'Owned Task must be quiescent before independent checks.');
};
try {
assert.equal(boot.version, '1.25.8'); const host = await api('/host/status'); assert.equal(host.activity.active, false); assert.equal(host.desktopWindows.count, 0);
assert.equal(boot.config.approvalMode, 'full'); assert.equal(boot.config.capacityMode, 'passthrough');
const center = await api('/model-center'); original = center.connections.find(value => value.id === connectionId);
assert.deepEqual(connectionDefinition(original), connectionDefinition(frozen.connections.find(value => value.id === connectionId)));
assert.deepEqual(center.selected, frozen.center.selected); assert.deepEqual(center.pools, frozen.center.pools); compareConfig(frozen.config, boot.config);
save('node/contract.json', { connectionId, originalConnection: original, originalSelection: center.selected, config: boot.config, allowed,
maxObservationMs: 600000, noNewFactObservationMs: 180000, localTokenOrStepCapsChanged: false,
implementationSupplied: false, supervisorContinuation: false, keysReadOrResaved: false });
fs.cpSync('benchmarks/live-delivery-v1/node', sourceRoot, { recursive: true, errorOnExist: true });
fs.writeFileSync(path.join(sourceRoot, 'README.md'), '# AD independent engineering\nRepair only the three src modules using the original nine tests. Preserve owner files 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 independent AD expense baseline and original tests']); initialHead = git(['rev-parse', 'HEAD']).trim();
fs.writeFileSync(path.join(sourceRoot, 'owner-staged.txt'), 'Owner staged change\n'); git(['add', '--', 'owner-staged.txt']); staged = git(['show', ':owner-staged.txt']);
fs.writeFileSync(path.join(sourceRoot, 'owner-staged.txt'), 'Owner unstaged over 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 protectedNames = ['README.md', '.gitignore', 'package.json', 'test/expense.test.mjs', 'scripts/build.mjs', 'owner-staged.txt', 'owner-unstaged.txt', 'owner-untracked.txt'];
protectedBytes = Object.fromEntries(protectedNames.map(name => [name, fs.readFileSync(path.join(sourceRoot, name))]));
save('node/source-before.json', { initialHead, status: git(['status', '--porcelain=v1']), staged, files: Object.fromEntries([...protectedNames, ...allowed].map(name => [name, sha(fs.readFileSync(path.join(sourceRoot, name)))])) });
const baseline = { test: check('test', testArgs, 'node/baseline'), build: check('build', ['scripts/build.mjs'], 'node/baseline') }; save('node/baseline/result.json', baseline);
assert.notEqual(baseline.test.status, 0); assert.equal(baseline.test.stats.tests, 9); assert.equal(baseline.test.stats.fail, 9); assert.equal(baseline.build.status, 0); protect();
const project = await api('/projects', { root: sourceRoot }); session = await api('/sessions', { projectId: project.id }); route = '/sessions/' + session.id;
save('node/owned.json', { project, session });
configured = await api('/model-center/connections', { ...connectionInput(original), modelIdentityPolicy: 'require-match' });
assert.equal(configured.credentialRevision, original.credentialRevision); assert.equal(configured.hasKey, original.hasKey);
await api('/model-center/select', { type: 'connection', id: connectionId }); selectedWritten = true;
const goal = '请独立修复这个Node费用报表工程,使原九项测试和build通过,并完成本地Git交付。只修改src/amount.mjs、src/summary.mjs、src/report.mjs;按原测试理解金额、币种汇总和报表要求,不修改原测试、package配置、构建脚本、README或owner文件,保留原有暂存/未暂存/未跟踪状态。请自行读取、修改、执行检查并提交,不要只解释方案或口头宣布完成。当前为完全访问权限,可以使用工作台工程工具或终端;不需要等待独立审查,不推送远端。';
save('node/goal.txt', goal); await api(route + '/messages', { content: goal }); const startedAt = Date.now(); let evidenceSignature = '';
while (true) {
state = await api(route); protect();
for (const event of state.events.filter(value => value.kind === 'tool' && value.phase === 'completed' && ['read_file', 'read_artifact', 'search_files', 'query_code', 'list_files'].includes(value.tool))) {
const key = event.tool + ':' + JSON.stringify(event.input) + ':' + sha(event.output ?? ''); if (!reads.has(key)) { reads.add(key); lastNovel = Date.now(); }
}
const signature = JSON.stringify({ files: allowed.map(name => sha(fs.readFileSync(path.join(sourceRoot, name)))), checks: state.execution.checks.map(value => ({ status: value.status, source: value.sourceFingerprint, tests: value.results?.map(row => row.tests ?? row.status) })) });
if (signature !== evidenceSignature) { evidenceSignature = signature; lastNovel = Date.now(); }
const usage = state.execution.task?.usage, observation = { status: state.status, requests: usage?.requests, tokens: usage?.totalTokens, reads: reads.size,
changes: state.changes.length, checks: state.execution.checks.map(value => value.status), lastTool: state.events.filter(value => value.kind === 'tool').at(-1)?.tool };
if (JSON.stringify(observation) !== JSON.stringify(observations.at(-1))) { observations.push(observation); console.log(JSON.stringify(observation)); }
if (!['running', 'stopping', 'awaiting_approval'].includes(state.status)) break;
if (Date.now() - startedAt > 600000 || Date.now() - lastNovel > 180000) { failure = { reason: 'experiment_observation_ended', elapsedMs: Date.now() - startedAt, idleFactMs: Date.now() - lastNovel, productBudgetsChanged: false }; await api(route + '/stop', {}); break; }
await delay(750);
}
await settle(); protect(); const final = { test: check('test', testArgs, 'node/independent-final'), build: check('build', ['scripts/build.mjs'], 'node/independent-final') }; save('node/independent-final/result.json', final);
const commit = git(['rev-parse', 'HEAD']).trim(), paths = git(['diff', '--name-only', initialHead, commit]).trim().split(/\r?\n/).filter(Boolean).sort();
const committedCurrent = allowed.every(name => sha(Buffer.from(git(['show', commit + ':' + name]))) === sha(fs.readFileSync(path.join(sourceRoot, name))));
result = { success: !failure && state.status === 'completed' && final.test.status === 0 && final.test.stats.tests === 9 && final.test.stats.pass === 9 && final.test.stats.fail === 0 && final.build.status === 0 && commit !== initialHead && JSON.stringify(paths) === JSON.stringify(allowed.slice().sort()) && committedCurrent,
connectionId, commit, initialHead, committedPaths: paths, committedCurrent, final, failure: failure ?? null, productStopReason: state.execution.run?.stopReason ?? null,
allOriginalOwnerBytesAndIndexPreserved: true, sourceRoot, executed: true, supervisionContinuations: 0, localTokenOrStepCapsChanged: false };
} catch (error) { failure = { error: error.stack, noRetry: true }; result = { success: false, executed: !!session, failure, connectionId, sourceRoot }; }
finally {
const cleanupErrors = [], restoration = {};
const guard = async (label, fn) => { try { await fn(); } catch (error) { cleanupErrors.push({ label, error: error.stack }); } };
if (route) {
await guard('settle-owned-task', settle);
await guard('capture-owned-task', async () => {
const captured = await captureSession(api, session.id, 'node'); result = { ...result, sessionId: session.id, taskId: captured.state.execution.task?.id, requests: captured.rows.length,
identities: captured.rows.map(value => value.modelIdentity), usage: captured.state.execution.task?.usage, captures: captured.captures };
const identitiesPassed = captured.rows.length > 0 && captured.rows.every(value => value.connectionId === connectionId && value.status === 'completed' && value.modelIdentity?.complete === true && value.modelIdentity.status === 'matched')
&& captured.state.execution.task?.usage.unknownRequests === 0 && captured.state.execution.task.usage.requests === captured.rows.length;
result.success &&= identitiesPassed; result.identitiesPassed = identitiesPassed;
save('node/observations.json', observations); save('node/git-status.txt', git(['status', '--porcelain=v1'])); save('node/git-log.txt', git(['log', '--format=fuller', '--stat', initialHead + '..HEAD']));
});
}
await guard('restore-connection', async () => {
let center = await api('/model-center');
if (configured) {
const current = center.connections.find(value => value.id === connectionId);
if (current.revision === configured.revision) { const restored = await api('/model-center/connections', { ...connectionInput(original), revision: current.revision, modelIdentityPolicy: original.modelIdentityPolicy ?? 'record' }); restoration.connection = restored;
assert.equal(restored.credentialRevision, original.credentialRevision); assert.equal(restored.hasKey, original.hasKey); }
else restoration.externalConnectionChangePreserved = true;
}
});
await guard('restore-selection', async () => {
if (selectedWritten) {
const center = await api('/model-center'); if (center.selected.type === 'connection' && center.selected.id === connectionId) { await api('/model-center/select', { type: frozen.center.selected.type, id: frozen.center.selected.id }); restoration.selection = frozen.center.selected; }
else restoration.externalSelectionChangePreserved = true;
}
});
await guard('verify-config', async () => { restoration.config = (await api('/bootstrap')).config; restoration.configComparison = compareConfig(frozen.config, restoration.config); });
restoration.keysReadOrResaved = false; restoration.cleanupErrors = cleanupErrors; if (cleanupErrors.length) result = { ...result, success: false, cleanupErrors };
save('node/restoration.json', restoration); save('node/result.json', result); await close();
}
console.log(JSON.stringify(result)); if (!result.success) process.exitCode = 1;