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

99 lines
11 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { setTimeout as delay } from 'node:timers/promises';
import { root, save, read, client, sha, captureSession, connectionDefinition, compareConfig, compareConnectionDefinitions } from './common.mjs';
import { prepareProject, python, specs } from './project.mjs';
import { verifyLiveReactClient } from '../live-react-client.mjs';
const kind = process.argv[2]; assert.ok(Object.hasOwn(specs, kind));
const frozen = read('frozen.json'), compatibility = read('compatibility/result.json');
if (kind !== 'node') assert.equal(read((kind === 'react' ? 'node' : 'react') + '/result.json').success, true, 'The previous engineering baseline must fully pass.');
const connectionId = kind === 'node' ? frozen.engineeringPriority.find(id => compatibility.results.some(value => value.connectionId === id && value.engineeringAllowed)) : read('node/result.json').connectionId;
assert.equal(fs.existsSync(path.join(root, kind)), false, 'Do not replay an engineering sample.');
if (!connectionId) { save(kind + '/result.json', { success: false, executed: false, reason: 'No connection passed the predeclared compatibility gate.', purgeExecuted: false }); process.exit(0); }
const { api, boot, close } = await client(); let session, route, selected = false, original, own, failure, result, state, selectionRevision;
const observations = [], facts = new Set(), output = path.join(root, kind), store = (file, value) => save(kind + '/' + file, value);
const settle = async () => {
if (!route) return; state = await api(route);
if (['running', 'stopping', 'awaiting_approval'].includes(state.status)) { await api(route + '/stop', {}, 60000); state = await api(route); }
assert.ok(!['running', 'stopping', 'awaiting_approval'].includes(state.status), 'Only a quiescent owned task can be independently checked.');
};
try {
const host = await api('/host/status'); assert.equal(host.activity.active, false); assert.equal(host.identity.pid, frozen.host.identity.pid); assert.equal(boot.version, '1.25.13');
compareConfig(frozen.config, boot.config); const center = await api('/model-center'); assert.deepEqual(center.selected, frozen.center.selected); assert.deepEqual(center.pools, frozen.center.pools);
original = center.connections.find(value => value.id === connectionId);
assert.deepEqual(connectionDefinition(original), connectionDefinition(frozen.connections.find(value => value.id === connectionId)));
store('claim.json', { id: randomUUID(), kind, connection: original, config: boot.config, source: specs[kind].baseline, allowed: specs[kind].allowed,
observationMs: 600000, noNewFactMs: 180000, implementationSupplied: false, supervisorContinuation: false, localTokenCaps: false, identityPolicySaved: false });
own = prepareProject(kind, output, store); const baseline = own.checks('baseline'); assert.equal(baseline.test.stats.tests, own.spec.tests); assert.ok(baseline.test.stats.fail > 0); assert.equal(baseline.build.status, 0);
const project = await api('/projects', { root: own.sourceRoot }); session = await api('/sessions', { projectId: project.id }); route = '/sessions/' + session.id; store('owned.json', { project, session });
const selection = await api('/model-center/select', { type: 'connection', id: connectionId }); selected = true; selectionRevision = selection.config.configRevision;
const goals = {
node: '请独立修复这个Node费用报表工程,使原九项测试和build通过,并完成本地Git交付。只修改src/amount.mjs、src/summary.mjs、src/report.mjs;按原测试理解金额、币种汇总和报表要求,不修改原测试、package配置、构建脚本、README或owner文件,保留原有暂存/未暂存/未跟踪状态。请自行读取、修改、执行检查并提交。',
react: '独立完成这个React/TypeScript看板工程README要求,修复状态筛选、去空白搜索、实际统计、按目标任务切换与空态,保留既有样式和布局。只修改src/query.ts、src/summary.ts、src/transition.ts、src/App.tsx。自行读取源码及原九项测试、运行test/typecheck/build并完成仅这四文件的本地Git提交。依赖已按原锁离线准备;不可改原测试、配置、锁、其它源码/样式、README和owner文件,保留原暂存/未暂存/未跟踪状态。',
python: '独立完成这个Python任务报表工程README的规范化、汇总与渲染契约。只修改normalize.py、summary.py、render.py。自行读取源码及原十项测试,运行pytest与源码编译并完成仅这三文件的本地Git提交。现成解释器为 ' + python + ',已装pytest,无需安装依赖。不可改原测试、配置、README和owner文件,保留原暂存/未暂存/未跟踪状态。',
};
const prompt = goals[kind] + ' 当前为完全访问权限,可用工程工具或终端自动执行,无需独立审查,不要只解释方案或口头完成,不推送远端。';
store('goal.txt', prompt); await api(route + '/messages', { content: prompt }); const started = Date.now(); let novel = started, signature = '';
while (true) {
state = await api(route); own.protect();
for (const event of state.events.filter(value => value.kind === 'tool' && value.phase === 'completed' && ['read_file', 'read_artifact', 'list_files', 'query_code', 'search_files'].includes(value.tool))) {
const key = event.tool + JSON.stringify(event.input) + sha(event.output ?? ''); if (!facts.has(key)) { facts.add(key); novel = Date.now(); }
}
const current = JSON.stringify({ files: own.spec.allowed.map(name => sha(fs.readFileSync(path.join(own.sourceRoot, name)))), checks: state.execution.checks.map(value => ({ status: value.status, source: value.sourceFingerprint })) });
if (current !== signature) { signature = current; novel = Date.now(); }
const observation = { kind, status: state.status, requests: state.execution.task?.usage.requests, tokens: state.execution.task?.usage.totalTokens, facts: facts.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() - started > 600000 || Date.now() - novel > 180000) { failure = { reason: 'experiment_observation_ended', elapsedMs: Date.now() - started, noNewFactMs: Date.now() - novel, productBudgetChanged: false }; break; }
await delay(750);
}
await settle(); own.protect(); const final = own.checks('independent-final'); let browser = null;
if (kind === 'react' && Object.values(final).every(value => value.status === 0)) {
try { browser = await verifyLiveReactClient(own.sourceRoot, path.join(output, 'browser')); } catch (error) { browser = { success: false, error: error.stack }; } store('browser/result.json', browser);
}
const commit = own.git(['rev-parse', 'HEAD']).trim(), paths = own.git(['diff', '--name-only', own.initialHead, commit]).trim().split(/\r?\n/).filter(Boolean).sort();
const committedCurrent = own.spec.allowed.every(name => sha(Buffer.from(own.git(['show', commit + ':' + name]))) === sha(fs.readFileSync(path.join(own.sourceRoot, name))));
const otherUntracked = own.git(['ls-files', '--others', '--exclude-standard']).trim().split(/\r?\n/).filter(name => name && name !== 'owner-untracked.txt');
result = { success: !failure && state.status === 'completed' && Object.values(final).every(value => value.status === 0) && final.test.stats.tests === own.spec.tests && final.test.stats.pass === own.spec.tests
&& commit !== own.initialHead && JSON.stringify(paths) === JSON.stringify(own.spec.allowed.slice().sort()) && committedCurrent && !otherUntracked.length && (kind !== 'react' || browser?.success === true),
kind, executed: true, sessionId: session.id, projectId: project.id, connectionId, commit, initialHead: own.initialHead, committedPaths: paths, committedCurrent, otherUntracked,
final, browser, failure: failure ?? null, productStopReason: state.execution.run?.stopReason ?? null, ownerBytesAndIndexPreserved: true, supervisorContinuations: 0, localTokenCapsChanged: false };
} catch (error) { result = { success: false, kind, executed: !!session, failure: { error: error.stack, noRetry: true }, sessionId: session?.id, connectionId }; }
finally {
const errors = [], restoration = {}; const guard = async (label, fn) => { try { await fn(); } catch (error) { errors.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, kind);
const identitiesPassed = captured.total > 0 && captured.rows.every(value => value.connectionId === connectionId && value.status === 'completed' && value.modelIdentity?.complete && value.modelIdentity.status === 'matched')
&& captured.state.execution.task.usage.unknownRequests === 0 && captured.state.execution.task.usage.requests === captured.total;
result = { ...result, success: result.success && identitiesPassed, identitiesPassed, requests: captured.total, usage: captured.state.execution.task.usage, taskId: captured.state.execution.task?.id,
runId: captured.state.execution.run?.id, captures: captured.captures, internalDeliveries: captured.state.execution.deliveries.map(value => ({ id: value.id, status: value.status, commit: value.commit })),
reportArtifacts: captured.state.execution.artifacts.filter(value => /^delivery-report/.test(value.kind)).map(value => ({ id: value.id, kind: value.kind, sha256: value.sha256 })) };
store('git-status.txt', own.git(['status', '--porcelain=v1'])); store('git-log.txt', own.git(['log', '--format=fuller', '--stat', own.initialHead + '..HEAD']));
});
}
await guard('restore-selection', async () => {
if (!selected) return;
const center = await api('/model-center'), now = await api('/bootstrap');
if (center.selected.type === 'connection' && center.selected.id === connectionId && now.config.configRevision === selectionRevision) {
await api('/model-center/select', frozen.center.selected); restoration.selection = frozen.center.selected;
} else restoration.externalSelectionOrConfigChangePreserved = true;
});
await guard('verify-config-and-definitions', async () => {
restoration.config = (await api('/bootstrap')).config;
if (!restoration.externalSelectionOrConfigChangePreserved) restoration.configComparison = compareConfig(frozen.config, restoration.config);
const center = await api('/model-center');
restoration.connectionDefinitions = compareConnectionDefinitions(frozen.center, center, frozen.config, restoration.config);
assert.deepEqual(center.pools, frozen.center.pools); restoration.pools = 'exact';
});
restoration.keysReadOrResaved = false; restoration.errors = errors; if (errors.length) result = { ...result, success: false, cleanupErrors: errors };
store('restoration.json', restoration); store('observations.json', observations); store('result.json', result); await close();
}
console.log(JSON.stringify({ kind, success: result.success, requests: result.requests, usage: result.usage, commit: result.commit, failure: result.failure, cleanupErrors: result.cleanupErrors }));
if (!result.success) process.exitCode = 1;