278 lines
23 KiB
JavaScript
278 lines
23 KiB
JavaScript
import fs from 'node:fs';
|
||
import path from 'node:path';
|
||
import { runOwnedCommand } from './owned-command.mjs';
|
||
import { nodeEnvironmentBinding, verifyNodeBinding } from './environment/environment-inspector.mjs';
|
||
import { bindEnvironmentPreparation, verifyEnvironmentPreparation } from './environment/environment-evidence.mjs';
|
||
import { createHash, randomUUID } from 'node:crypto';
|
||
import { inspectProject, createCheckList } from './project-inspector.mjs';
|
||
import { safePath } from '../server/files.mjs';
|
||
import { assertChangeSetsResolved } from '../shared/contracts/change-sets.mjs';
|
||
import { assertWorkspacesResolved } from '../shared/contracts/workspaces.mjs';
|
||
import { verifyWorkspaceProject } from './workspaces/git-workspaces.mjs';
|
||
import { assertMcpResolved } from './tools/mcp-contract.mjs';
|
||
import { assertWorkersResolved, workerForSession } from '../shared/contracts/workers.mjs';
|
||
import { bindPythonChecks, preparePythonInvocation, pythonEnvironment, readPythonEvidence, pythonResultBinding } from './python-runtime.mjs';
|
||
import { projectEnvironment } from './process-environment.mjs';
|
||
import { verifyEvaluationSources } from './evaluation/provenance.mjs';
|
||
|
||
const at = () => new Date().toISOString();
|
||
const conflict = message => Object.assign(new Error(message), { status: 409 });
|
||
const removeAnsi = text => text.replace(/\u001b\[[0-9;]*[a-zA-Z]/g, '');
|
||
const checkContract = plan => ({ schemaVersion: 1, kind: 'sealed-check-plan', id: plan.id, sessionId: plan.sessionId,
|
||
projectId: plan.projectId, taskId: plan.taskId, runId: plan.runId, fingerprint: plan.fingerprint,
|
||
sourceFingerprint: plan.sourceFingerprint, checks: plan.checks, ...(plan.environment ? { environment: plan.environment } : {}) });
|
||
|
||
export function parseTestEvidence(output, framework) {
|
||
const text = removeAnsi(output), values = {};
|
||
if (framework === 'node') {
|
||
for (const match of text.matchAll(/^\s*(?:#|ℹ)\s+(tests|pass|fail|cancelled|skipped|todo)\s+(\d+)\s*$/gm)) values[match[1]] = Number(match[2]);
|
||
if (['tests', 'pass', 'fail', 'cancelled', 'skipped'].some(key => !Number.isSafeInteger(values[key]))) return null;
|
||
const result = { total: values.tests, passed: values.pass, failed: values.fail, cancelled: values.cancelled, skipped: values.skipped + (values.todo ?? 0) };
|
||
const namedCases = [...text.matchAll(/^\s*(?:✔|✖|﹣|ok\s+\d+\s+-|not ok\s+\d+\s+-)\s+(.+?)(?:\s+\([\d.]+(?:ms|s)\)|\s+#\s*SKIP|$)/gm)]
|
||
.some(match => !/\.(?:[mc]?js|[mc]?ts|tsx)$/i.test(match[1].trim()));
|
||
// Node counts an empty test file as a successful file-level test. Require
|
||
// explicit case evidence before treating that summary as exercised tests.
|
||
if (result.total > 0 && !namedCases) return null;
|
||
return result.total === result.passed + result.failed + result.cancelled + result.skipped ? result : null;
|
||
}
|
||
const lines = text.split('\n').filter(line => framework === 'vitest' ? /^\s*Tests\s+/.test(line) : framework === 'jest' ? /^\s*Tests:\s+/.test(line) : false);
|
||
const line = lines.at(-1); if (!line) return null;
|
||
const total = framework === 'vitest' ? line.match(/\((\d+)\)\s*$/)?.[1] : line.match(/(\d+) total/)?.[1];
|
||
if (total === undefined) return null;
|
||
const count = name => Number(line.match(new RegExp('(\\d+) ' + name))?.[1] ?? 0);
|
||
const result = { total: Number(total), passed: count('passed'), failed: count('failed'), skipped: count('skipped') + count('todo'), cancelled: 0 };
|
||
return result.total === result.passed + result.failed + result.skipped ? result : null;
|
||
}
|
||
|
||
export function invalidateProjectChecks(store, projectId, reason = '工程文件已变化,需要重新检查。') {
|
||
store.invalidateChecks(projectId, reason);
|
||
}
|
||
|
||
function npmInvocation(name, runtimeDirectory) {
|
||
const cli = [runtimeDirectory, path.dirname(process.execPath)].filter(Boolean).map(directory => path.join(directory, 'node_modules', 'npm', 'bin', 'npm-cli.js')).find(file => fs.existsSync(file));
|
||
if (!cli) throw new Error('执行宿主没有可用的npm CLI,请使用随附Node运行工作台。');
|
||
return { executable: process.execPath, args: [cli, 'run', name] };
|
||
}
|
||
|
||
export class CheckRunner {
|
||
constructor(store, { redact = value => String(value), timeoutMs = 180000, projectBusy = () => false, runtimeDirectory = null,
|
||
pythonExecutable = null, pythonRunnerPath = path.join(process.cwd(), 'scripts', 'python-check-runner.py') } = {}) {
|
||
this.store = store; this.redact = redact; this.timeoutMs = timeoutMs; this.projectBusy = projectBusy;
|
||
this.active = new Map(); this.projects = new Map();
|
||
this.runtimeDirectory = runtimeDirectory;
|
||
this.pythonExecutable = pythonExecutable; this.pythonRunnerPath = pythonRunnerPath;
|
||
}
|
||
isActive(sessionId) { return [...this.active.values()].some(value => value.sessionId === sessionId); }
|
||
projectActive(projectId) { return this.projects.has(projectId); }
|
||
|
||
async plan(session, names) {
|
||
await verifyEvaluationSources(this.store, session, names);
|
||
assertWorkersResolved(this.store, session.projectId);
|
||
if (workerForSession(this.store, session)?.status === 'completed') throw conflict('子成果已经封存,请读取原检查记录;重新执行需要新的受限任务。');
|
||
assertChangeSetsResolved(this.store, session.projectId);
|
||
assertWorkspacesResolved(this.store, session.projectId, { includePrepared: true });
|
||
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
|
||
if (!session.taskId || !session.runId) throw conflict('先发送一个项目任务,再识别本任务的检查计划。');
|
||
const identity = { taskId: session.taskId, runId: session.runId };
|
||
const inspection = await inspectProject(this.store.project(session.projectId));
|
||
const checks = await bindPythonChecks(this.store.project(session.projectId), createCheckList(inspection, names), {
|
||
executable: this.pythonExecutable ?? process.env.AGENT_WORKBENCH_PYTHON_PATH ?? inspection.python?.preferredExecutable,
|
||
runnerPath: this.pythonRunnerPath });
|
||
if (identity.taskId !== session.taskId || identity.runId !== session.runId) throw conflict('识别期间任务已切换,请重新准备当前任务的检查。');
|
||
const run = this.store.owned('run', session.runId, session.id);
|
||
const artifact = this.store.artifactStore.write(session, run, JSON.parse(this.redact(JSON.stringify(inspection))), { kind: 'project-inspection' });
|
||
this.store.core.artifact.set(artifact.id, artifact);
|
||
const plan = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: session.taskId, runId: session.runId,
|
||
status: 'planned', createdAt: at(), fingerprint: inspection.fingerprint, sourceFingerprint: inspection.sourceFingerprint, project: inspection, artifactId: artifact.id,
|
||
checks: checks.map(check => ({ id: randomUUID(), ...check })),
|
||
...(checks.some(check=>check.engine!=='python') ? {environment:bindEnvironmentPreparation(this.store,session.projectId,nodeEnvironmentBinding(this.store.project(session.projectId),{runtimeDirectory:this.runtimeDirectory}))}: {}) };
|
||
const contract = this.store.artifactStore.write(session, run, checkContract(plan), { kind: 'check-plan' });
|
||
const evaluation=await verifyEvaluationSources(this.store,session);
|
||
if(evaluation?.sample.environmentInputs && plan.environment?.dependencyInputs!==evaluation.sample.environmentInputs.dependencyInputs)throw conflict('评测的依赖声明或Node/npm输入与原同源计划不同。');
|
||
this.store.core.artifact.set(contract.id, contract); plan.contractArtifactId = contract.id; plan.contractSha256 = contract.sha256;
|
||
const task = this.store.core.task.get(session.taskId);
|
||
if (task.verification.status === 'passed') {
|
||
const last = this.store.core.check_job.get(task.verification.checkJobId);
|
||
if (last?.sourceFingerprint ? last.sourceFingerprint !== inspection.sourceFingerprint : last?.fingerprint !== inspection.fingerprint) invalidateProjectChecks(this.store, session.projectId);
|
||
}
|
||
return this.store.putRecord('check_plan', session, plan, 'check.plan');
|
||
}
|
||
|
||
planFor(session, planId) {
|
||
const plan = this.store.owned('check_plan', planId, session.id);
|
||
if (plan.taskId !== session.taskId) throw conflict('检查计划属于此前任务,请重新识别当前任务。');
|
||
if (!plan.checks.length) throw conflict('没有可执行的工程检查。');
|
||
if (plan.contractArtifactId) {
|
||
const artifact = this.store.owned('artifact', plan.contractArtifactId, session.id);
|
||
if (artifact.kind !== 'check-plan' || artifact.taskId !== plan.taskId || artifact.sha256 !== plan.contractSha256
|
||
|| this.store.artifactStore.read(artifact) !== JSON.stringify(checkContract(plan))) throw conflict('检查计划与封存的执行范围不一致,请重新识别检查。');
|
||
} else if (plan.checks.some(check => check.engine === 'python')) throw conflict('Python检查缺少封存执行范围,请重新识别检查。');
|
||
return plan;
|
||
}
|
||
|
||
inputIdentity(session, plan, inspection) {
|
||
const describe = file => {
|
||
try { const info = fs.statSync(file); return { path: fs.realpathSync(file), bytes: info.size, modified: info.mtimeMs, changed: info.ctimeMs }; }
|
||
catch { return { path: file, missing: true }; }
|
||
};
|
||
const project = this.store.project(session.projectId);
|
||
const checks = plan.checks.map(({ id, ...check }) => check);
|
||
const npm = checks.some(check => check.engine !== 'python') ? npmInvocation('', this.runtimeDirectory) : null;
|
||
// This detects repeated declared inputs. Reusing an old result never
|
||
// creates a new verification or claims that arbitrary external state was checked.
|
||
return createHash('sha256').update(JSON.stringify({ fingerprint: inspection.fingerprint, checks, ...(plan.environment ? {environment:plan.environment.identity}:{}),
|
||
node: { version: process.version, file: describe(process.execPath) }, npm: npm ? describe(npm.args[0]) : null,
|
||
dependencies: ['node_modules', 'node_modules/.package-lock.json', '.pnp.cjs', '.venv/pyvenv.cfg'].map(relative => describe(path.join(project.root, relative))),
|
||
environment: projectEnvironment() })).digest('hex');
|
||
}
|
||
|
||
async unchangedResult(session, planId) {
|
||
const plan = this.planFor(session, planId), inspection = await inspectProject(this.store.project(session.projectId));
|
||
if (inspection.fingerprint !== plan.fingerprint) throw conflict('检查计划创建后工程或Git基线已变化,请重新识别再执行。');
|
||
if (!inspection.scope.complete) return null;
|
||
verifyNodeBinding(this.store.project(session.projectId),plan.environment);
|
||
const identity = this.inputIdentity(session, plan, inspection);
|
||
const previous = [...this.store.core.check_job.values()].findLast(job => job.taskId === session.taskId && job.inputIdentity === identity
|
||
&& ['passed', 'failed', 'unverified'].includes(job.status) && !job.stale && !job.requiresVerification && job.finishedAt);
|
||
if (!previous || !previous.results.length) return null;
|
||
// Never hand back missing/corrupt logs as usable historical evidence.
|
||
for (const result of previous.results) this.store.artifactStore.read(this.store.owned('artifact', result.logArtifactId, session.id));
|
||
return previous;
|
||
}
|
||
|
||
async start(session, planId, { signal, source = 'user' } = {}) {
|
||
assertWorkersResolved(this.store, session.projectId);
|
||
if (workerForSession(this.store, session)?.status === 'completed') throw conflict('子成果已经封存,不重新运行原检查。');
|
||
assertMcpResolved(this.store, session.projectId);
|
||
assertChangeSetsResolved(this.store, session.projectId, { includePrepared: true });
|
||
assertWorkspacesResolved(this.store, session.projectId, { includePrepared: true });
|
||
const plan = this.planFor(session, planId);
|
||
await verifyEvaluationSources(this.store, session, plan.checks.map(check => check.name));
|
||
if (this.projectActive(session.projectId) || this.projectBusy(session.projectId, source === 'agent' ? session.id : null)) throw conflict('同一项目正在执行任务或检查,请等待结束后再运行。');
|
||
if (session.changes.some(change => ['pending', 'conflict'].includes(change.status))) throw conflict('先应用或撤回本任务的待审查改动,再检查实际工程。');
|
||
const controller = new AbortController(), run = this.store.owned('run', session.runId, session.id);
|
||
const job = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: session.taskId, runId: run.id, planId: plan.id,
|
||
status: 'queued', source, startedAt: at(), finishedAt: null, results: [], fingerprint: plan.fingerprint, sourceFingerprint: plan.sourceFingerprint ?? null,
|
||
baseline: { head: plan.project.git.head, branch: plan.project.git.branch, dirty: plan.project.git.dirty }, originalSessionStatus: session.status };
|
||
const active = { sessionId: session.id, projectId: session.projectId, controller, promise: null };
|
||
this.active.set(job.id, active); this.projects.set(session.projectId, job.id);
|
||
const combined = signal ? AbortSignal.any([signal, controller.signal]) : controller.signal;
|
||
try {
|
||
this.store.putRecord('check_job', session, job, 'check.queued');
|
||
active.promise = this.execute(session, plan, job, run, combined).finally(() => { this.active.delete(job.id); this.projects.delete(session.projectId); });
|
||
// execute() records all terminal failures. Attaching a catch also prevents a
|
||
// rejected storage write from becoming an unhandled process rejection.
|
||
active.promise.catch(() => {});
|
||
return { job, promise: active.promise };
|
||
} catch (error) { this.active.delete(job.id); this.projects.delete(session.projectId); throw error; }
|
||
}
|
||
|
||
async execute(session, plan, job, run, signal) {
|
||
const task = this.store.core.task.get(job.taskId);
|
||
let unrecordedProcess = null;
|
||
try {
|
||
await verifyEvaluationSources(this.store, session, plan.checks.map(check => check.name));
|
||
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
|
||
verifyNodeBinding(this.store.project(session.projectId),plan.environment);
|
||
verifyEnvironmentPreparation(this.store,session.projectId,plan.environment);
|
||
if(plan.environment)job.environment=structuredClone(plan.environment);
|
||
const before = await inspectProject(this.store.project(session.projectId));
|
||
signal.throwIfAborted();
|
||
if (before.fingerprint !== plan.fingerprint) throw conflict('检查计划创建后工程或Git基线已变化,请重新识别再执行。');
|
||
job.sourceFingerprint = before.sourceFingerprint;
|
||
job.inputIdentity = this.inputIdentity(session, plan, before);
|
||
const current = createCheckList(before, plan.checks.map(check => check.name));
|
||
if (current.some((check, index) => check.script !== plan.checks[index].script || check.command !== plan.checks[index].command)) throw conflict('检查脚本与计划不一致,未执行任何命令。');
|
||
job.status = 'running'; session.status = 'running'; task.verification = { status: 'running', checkJobId: job.id };
|
||
this.store.save(session, { type: 'check.started', detail: { jobId: job.id } });
|
||
for (const check of plan.checks) {
|
||
signal.throwIfAborted();
|
||
job.currentCheck = { id: check.id, name: check.name, command: check.command, startedAt: at() };
|
||
this.store.save(session, { type: 'check.command_started', detail: { jobId: job.id, checkId: check.id } });
|
||
const onSpawn = task.workerId ? processInfo => { unrecordedProcess = processInfo; job.currentCheck.process = processInfo; this.store.save(session, { type: 'worker.check_process_owned', detail: { jobId: job.id, pid: processInfo.pid } }); } : null;
|
||
const result = await this.executeCheck(check, this.store.project(session.projectId), signal, onSpawn);
|
||
unrecordedProcess = result.process ?? unrecordedProcess;
|
||
const text = this.redact(result.stdout + (result.stderr ? '\n--- stderr ---\n' + result.stderr : ''));
|
||
const artifact = this.store.artifactStore.write(session, run, text, { kind: 'check-log', mediaType: 'text/plain' });
|
||
this.store.core.artifact.set(artifact.id, artifact);
|
||
const { stdout, stderr, collectedEvidence, checkInput, ...record } = result;
|
||
if (collectedEvidence) {
|
||
const metrics = this.store.artifactStore.write(session, run, collectedEvidence, { kind: 'check-metrics' });
|
||
this.store.core.artifact.set(metrics.id, metrics); record.metricsArtifactId = metrics.id; record.metricsSha256 = metrics.sha256;
|
||
}
|
||
if (checkInput) {
|
||
const input = this.store.artifactStore.write(session, run, checkInput, { kind: 'check-input' });
|
||
this.store.core.artifact.set(input.id, input); record.inputArtifactId = input.id;
|
||
}
|
||
job.results.push({ id: randomUUID(), name: check.name, kind: check.kind, command: check.command, ...record, logArtifactId: artifact.id, logExcerpt: text.slice(-8000) });
|
||
this.store.save(session, { type: 'check.result', detail: { jobId: job.id, checkId: check.id, status: record.status, artifactId: artifact.id } });
|
||
unrecordedProcess = null;
|
||
if (record.status === 'stale') { job.stale = true; break; }
|
||
if (['cancelled', 'unknown'].includes(record.status)) break;
|
||
const after = await inspectProject(this.store.project(session.projectId));
|
||
if (after.sourceFingerprint !== job.sourceFingerprint) { job.stale = true; break; }
|
||
}
|
||
verifyNodeBinding(this.store.project(session.projectId),plan.environment);
|
||
const final = await inspectProject(this.store.project(session.projectId));
|
||
await verifyEvaluationSources(this.store, session, plan.checks.map(check => check.name));
|
||
job.stale ||= final.sourceFingerprint !== job.sourceFingerprint;
|
||
if (job.results.some(result => result.status === 'unknown')) { job.status = 'unknown'; job.requiresVerification = true; }
|
||
else if (signal.aborted || job.results.some(result => result.status === 'cancelled')) job.status = 'cancelled';
|
||
else if (job.stale) job.status = 'stale';
|
||
else if (job.results.some(result => result.status === 'failed')) job.status = 'failed';
|
||
else if (!final.scope.complete || job.results.length !== plan.checks.length || job.results.some(result => result.status !== 'passed') || !job.results.some(result => result.kind === 'test' && result.tests?.passed > 0)) job.status = 'unverified';
|
||
else job.status = 'passed';
|
||
job.summary = job.status === 'passed' ? '所选检查全部通过,测试数量与源码快照已记录。' : job.status === 'stale' ? '检查期间工程已变化,请按新快照重新检查。' : job.status === 'unverified' ? '检查已返回,但测试数量或工程快照未满足验证条件。' : job.status === 'cancelled' ? '检查已停止,已返回的日志和结果保留。' : job.status === 'unknown' ? '进程结果无法确认,需要核对实际状态。' : '有检查失败,请查看退出码和日志后修复。';
|
||
} catch (error) {
|
||
job.status = signal.aborted ? 'cancelled' : error.status === 409 ? 'stale' : 'failed';
|
||
job.stale = error.status === 409; job.summary = this.redact(error.message ?? String(error));
|
||
} finally {
|
||
if (unrecordedProcess) {
|
||
job.unreportedProcesses ??= []; job.unreportedProcesses.push(unrecordedProcess);
|
||
job.status = 'unknown'; job.requiresVerification = true; job.summary = '所属检查进程没有完整持久结果,保留进程归属供原输入核对。';
|
||
}
|
||
job.currentCheck = null;
|
||
job.finishedAt = at();
|
||
task.verification = { status: job.status, checkJobId: job.id, fingerprint: job.fingerprint, sourceFingerprint: job.sourceFingerprint, checkedAt: job.finishedAt };
|
||
if (job.source === 'user') session.status = job.originalSessionStatus;
|
||
this.store.save(session, { type: 'check.finished', detail: { jobId: job.id, status: job.status } });
|
||
}
|
||
return job;
|
||
}
|
||
|
||
async executeCheck(check, project, signal, onSpawn = null) {
|
||
const started = Date.now();
|
||
let invocation;
|
||
const isPython = check.engine === 'python';
|
||
try { invocation = isPython ? await preparePythonInvocation(check, project, { signal }) : npmInvocation(check.name, this.runtimeDirectory); }
|
||
catch (error) { return { status: signal.aborted ? 'cancelled' : error.status === 409 ? 'stale' : 'failed', exitCode: null, durationMs: Date.now() - started, tests: null, stdout: '', stderr: error.message, reason: error.message }; }
|
||
const result = await runOwnedCommand(invocation,{cwd:safePath(project),signal,onSpawn,timeoutMs:this.timeoutMs,
|
||
env:isPython ? pythonEnvironment() : projectEnvironment({NO_COLOR:'1',CI:'1'}),input:isPython?invocation.input:null});
|
||
let collectedEvidence=null,collectedResult=null,evidenceError=null,cleanupError=null;
|
||
const unknown=result.status==='unknown';
|
||
if(isPython && !unknown && result.exitCode!==null) {
|
||
try { const saved=readPythonEvidence(invocation,result.exitCode);collectedEvidence=saved.text;collectedResult=saved.result; }
|
||
catch(error){evidenceError=error.message;}
|
||
}
|
||
const tests=check.kind==='test' ? isPython?collectedResult?.tests??null:parseTestEvidence(result.stdout.slice(-240000),check.framework) : null;
|
||
const status=['unknown','cancelled','failed'].includes(result.status)?result.status
|
||
: evidenceError||tests?.failed>0||collectedResult?.collectionErrors>0?'failed'
|
||
: (check.kind==='test'&&(!tests||tests.total===0||tests.passed!==tests.total||tests.cancelled>0||collectedResult?.collectionSkipped>0)
|
||
||isPython&&collectedResult?.compilation?.total===0)?'unverified':'passed';
|
||
if(isPython&&!unknown){try{invocation.dispose();}catch(error){cleanupError=error.message;}}
|
||
return {...result,status,tests,...(isPython?{engine:'python',python:pythonResultBinding(invocation),collectedEvidence,checkInput:invocation.input.toString('utf8'),
|
||
compilation:collectedResult?.compilation??null,collectionErrors:collectedResult?.collectionErrors??null,collectionSkipped:collectedResult?.collectionSkipped??null,
|
||
evidenceValidated:!!collectedResult,evidenceError,cleanupError,...(unknown?{hostEvidenceDirectory:invocation.directory}:{})}:{}),
|
||
reason:result.reason||evidenceError||collectedResult?.collectorError||(collectedResult?.collectionErrors>0?'Python测试收集或类初始化有错误。':null)
|
||
||(status==='unverified'?'测试为空、有跳过项,或没有可验证的测试数量。':undefined)};
|
||
}
|
||
|
||
async stop(sessionId, jobId) {
|
||
const owned = jobId ? this.store.owned('check_job', jobId, sessionId) : null;
|
||
const entries = [...this.active.entries()].filter(([id, value]) => value.sessionId === sessionId && (!owned || id === owned.id));
|
||
for (const [, active] of entries) active.controller.abort(new Error('用户停止检查'));
|
||
await Promise.all(entries.map(([, active]) => active.promise));
|
||
}
|
||
async stopAll() { await Promise.all([...new Set([...this.active.values()].map(value => value.sessionId))].map(id => this.stop(id))); }
|
||
}
|