import fs from 'node:fs'; import path from 'node:path'; import { spawn, execFile } from 'node:child_process'; import { randomUUID } from 'node:crypto'; import { StringDecoder } from 'node:string_decoder'; 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'; 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, ''); 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 } = {}) { this.store = store; this.redact = redact; this.timeoutMs = timeoutMs; this.projectBusy = projectBusy; this.active = new Map(); this.projects = new Map(); this.runtimeDirectory = runtimeDirectory; } isActive(sessionId) { return [...this.active.values()].some(value => value.sessionId === sessionId); } projectActive(projectId) { return this.projects.has(projectId); } async plan(session, names) { 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)); 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: createCheckList(inspection, names).map(check => ({ id: randomUUID(), ...check })) }; 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('没有可执行的npm检查脚本。'); return plan; } async start(session, planId, { signal, source = 'user' } = {}) { assertChangeSetsResolved(this.store, session.projectId, { includePrepared: true }); assertWorkspacesResolved(this.store, session.projectId, { includePrepared: true }); const plan = this.planFor(session, planId); 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); try { await verifyWorkspaceProject(this.store, this.store.project(session.projectId)); const before = await inspectProject(this.store.project(session.projectId)); signal.throwIfAborted(); if (before.fingerprint !== plan.fingerprint) throw conflict('检查计划创建后工程或Git基线已变化,请重新识别再执行。'); job.sourceFingerprint = before.sourceFingerprint; 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 result = await this.executeCheck(check, this.store.project(session.projectId), signal); 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, ...record } = result; 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 } }); 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; } } const final = await inspectProject(this.store.project(session.projectId)); job.stale ||= final.sourceFingerprint !== job.sourceFingerprint; if (signal.aborted || job.results.some(result => result.status === 'cancelled')) job.status = 'cancelled'; else if (job.results.some(result => result.status === 'unknown')) { job.status = 'unknown'; job.requiresVerification = true; } 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 { 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; } executeCheck(check, project, signal) { const started = Date.now(); let invocation; try { invocation = npmInvocation(check.name, this.runtimeDirectory); } catch (error) { return Promise.resolve({ status: 'failed', exitCode: null, durationMs: 0, tests: null, stdout: '', stderr: error.message, reason: error.message }); } return new Promise(resolve => { const env = { ...process.env, NO_COLOR: '1', CI: '1' }; delete env.FORCE_COLOR; delete env.AGENT_WORKBENCH_API_KEY; delete env.NODE_TEST_CONTEXT; const priorPath = process.env.PATH || ''; for (const key of Object.keys(env)) if (key.toLowerCase() === 'path') delete env[key]; env.PATH = path.dirname(process.execPath) + path.delimiter + priorPath; const child = spawn(invocation.executable, invocation.args, { cwd: safePath(project), env, windowsHide: true, detached: process.platform !== 'win32', stdio: ['ignore', 'pipe', 'pipe'] }); const streams = { stdout: { head: '', tail: '', characters: 0, decoder: new StringDecoder('utf8') }, stderr: { head: '', tail: '', characters: 0, decoder: new StringDecoder('utf8') } }; let settled = false, timedOut = false, terminating = false, killTimer; const append = (name, text) => { const value = streams[name]; value.characters += text.length; value.head = (value.head + text).slice(0, 240000); value.tail = (value.tail + text).slice(-240000); }; const captured = name => { const value = streams[name]; return value.characters <= 240000 ? value.head : value.characters <= 480000 ? value.head + value.tail.slice(value.head.length - value.characters) : value.head + '\n[中间日志超出保存上限,保留开头和结尾]\n' + value.tail; }; const finish = (code, error, unknown = false) => { if (settled) return; settled = true; clearTimeout(timeout); clearTimeout(killTimer); signal.removeEventListener('abort', terminate); const stdout = captured('stdout'), stderr = captured('stderr') + (error ? '\n' + error.message : ''); const tests = check.kind === 'test' ? parseTestEvidence(streams.stdout.tail, check.framework) : null; const status = unknown ? 'unknown' : signal.aborted ? 'cancelled' : timedOut || error || code !== 0 || tests?.failed > 0 ? 'failed' : check.kind === 'test' && (!tests || tests.total === 0 || tests.passed !== tests.total || tests.cancelled > 0) ? 'unverified' : 'passed'; resolve({ status, exitCode: code, durationMs: Date.now() - started, tests, stdout, stderr, timedOut, truncated: streams.stdout.characters > 480000 || streams.stderr.characters > 480000, reason: unknown ? '进程终止未得到确认,需要核对实际副作用。' : timedOut ? '检查超过时间上限,已请求终止进程树。' : status === 'unverified' ? '测试为空、有跳过项,或没有可验证的测试数量。' : undefined }); }; const terminate = () => { if (settled || terminating) return; terminating = true; if (child.pid) { if (process.platform === 'win32') execFile('taskkill.exe', ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true }, () => {}); else { try { process.kill(-child.pid, 'SIGTERM'); } catch { child.kill('SIGTERM'); } } } killTimer = setTimeout(() => finish(null, null, true), 5000); }; const timeout = setTimeout(() => { timedOut = true; terminate(); }, this.timeoutMs); signal.addEventListener('abort', terminate, { once: true }); for (const name of ['stdout', 'stderr']) { child[name].on('data', bytes => append(name, streams[name].decoder.write(bytes))); child[name].on('end', () => append(name, streams[name].decoder.end())); } child.once('error', error => finish(null, error)); child.once('close', code => finish(code)); if (signal.aborted) terminate(); }); } 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))); } }