feat: add task records and verified engineering check jobs

This commit is contained in:
Agent Workbench Dev
2026-10-29 14:03:50 +08:00
parent 93f4d90ccb
commit 2ea34850c4
26 changed files with 2703 additions and 56 deletions
+192
View File
@@ -0,0 +1,192 @@
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';
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) {
const cli = path.join(path.dirname(process.execPath), 'node_modules', 'npm', 'bin', 'npm-cli.js');
if (!fs.existsSync(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 } = {}) {
this.store = store; this.redact = redact; this.timeoutMs = timeoutMs; this.projectBusy = projectBusy;
this.active = new Map(); this.projects = new Map();
}
isActive(sessionId) { return [...this.active.values()].some(value => value.sessionId === sessionId); }
projectActive(projectId) { return this.projects.has(projectId); }
async plan(session, names) {
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, 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?.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' } = {}) {
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,
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 {
const before = await inspectProject(this.store.project(session.projectId));
signal.throwIfAborted();
if (before.fingerprint !== plan.fingerprint) throw conflict('检查计划创建后工程或Git基线已变化,请重新识别再执行。');
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 });
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.fingerprint !== plan.fingerprint) { job.stale = true; break; }
}
const final = await inspectProject(this.store.project(session.projectId));
job.stale ||= final.fingerprint !== plan.fingerprint;
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, 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); } 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))); }
}
+107
View File
@@ -0,0 +1,107 @@
import fs from 'node:fs';
import path from 'node:path';
import { execFile } from 'node:child_process';
import { createHash } from 'node:crypto';
import { listFiles, safePath, readText } from '../server/files.mjs';
const hash = value => createHash('sha256').update(value).digest('hex');
const languages = { '.js': 'JavaScript', '.mjs': 'JavaScript', '.cjs': 'JavaScript', '.ts': 'TypeScript', '.tsx': 'TypeScript', '.jsx': 'JavaScript', '.py': 'Python', '.go': 'Go', '.rs': 'Rust', '.cs': 'C#', '.vue': 'Vue', '.html': 'HTML', '.css': 'CSS' };
const recognizedScripts = /^(test(?::[a-z0-9_-]+)?|typecheck|check|lint|build)$/i;
function gitCommand(root, args) {
const env = { ...process.env, GIT_OPTIONAL_LOCKS: '0' };
for (const key of Object.keys(env)) if (/^GIT_(DIR|WORK_TREE|INDEX_FILE|CONFIG|COMMON_DIR|OBJECT_DIRECTORY|ALTERNATE_OBJECT_DIRECTORIES)/i.test(key) || key === 'AGENT_WORKBENCH_API_KEY') delete env[key];
return new Promise(resolve => execFile('git', ['--no-optional-locks', '-C', root, ...args], { windowsHide: true, timeout: 5000, maxBuffer: 512000, env, encoding: 'utf8' }, (error, stdout) => resolve({ ok: !error, output: stdout || '', error: error?.code ?? null })));
}
export async function gitBaseline(project) {
const root = safePath(project);
const probe = await gitCommand(root, ['rev-parse', '--is-inside-work-tree']);
if (!probe.ok || probe.output.trim() !== 'true') return { available: false, head: null, branch: null, dirty: false, status: '', complete: true, paths: null };
const top = await gitCommand(root, ['rev-parse', '--show-toplevel']);
const normalize = value => process.platform === 'win32' ? path.resolve(value).toLowerCase() : path.resolve(value);
if (!top.ok || normalize(top.output.trim()) !== normalize(root)) return { available: false, parentRepository: true,
note: '上级目录的Git仓库不作为当前项目独立基线。', head: null, branch: null, dirty: false, status: '', complete: true, paths: null };
const [status, head, branch, files] = await Promise.all([
gitCommand(root, ['status', '--porcelain=v1', '-z', '--untracked-files=all', '--', '.']),
gitCommand(root, ['rev-parse', '--verify', 'HEAD']),
gitCommand(root, ['symbolic-ref', '--quiet', '--short', 'HEAD']),
gitCommand(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', '.']),
]);
return { available: true, head: head.ok ? head.output.trim() : null, branch: branch.ok ? branch.output.trim() : null,
dirty: !!status.output, status: status.output.replaceAll('\0', '\n').slice(0, 8000),
statusSha256: hash(status.output), complete: status.ok && files.ok,
paths: files.ok ? [...new Set(files.output.split('\0').filter(Boolean))] : null };
}
function declaration(project, relative, maximum = 64000) {
const file = safePath(project, relative, true);
if (!fs.existsSync(file)) return null;
if (!fs.statSync(file).isFile() || fs.statSync(file).size > maximum) throw new Error(relative + ' 超过工程声明的读取上限。');
return readText(project, relative);
}
function framework(script) {
if (/^\s*(?:node|node\.exe)\s+(?:--[^\s]+\s+)*--test(?:\s|$)/.test(script)) return 'node';
if (/^\s*(?:npx\s+)?vitest(?:\s|$)/.test(script)) return 'vitest';
if (/^\s*(?:npx\s+)?jest(?:\s|$)/.test(script)) return 'jest';
return null;
}
export async function inspectProject(project) {
const root = safePath(project), git = await gitBaseline(project);
const inventory = git.paths ? null : listFiles(project, '', 8, 1500);
const candidates = (git.paths ?? inventory.files.filter(file => file.kind === 'file').map(file => file.path)).slice().sort();
const fingerprints = [], names = [], excluded = [], detected = new Set();
let bytes = 0, complete = git.complete && !inventory?.truncated && candidates.length <= 1000;
for (const relative of candidates.slice(0, 1000)) {
try {
const file = safePath(project, relative), info = fs.lstatSync(file);
if (!info.isFile() || info.isSymbolicLink()) { excluded.push(relative); continue; }
names.push(relative);
const language = languages[path.extname(relative).toLowerCase()]; if (language) detected.add(language);
if (info.size > 1024 * 1024 || bytes + info.size > 16 * 1024 * 1024) { complete = false; fingerprints.push([relative, info.size, 'outside-read-budget']); continue; }
const contents = fs.readFileSync(file); bytes += contents.length; fingerprints.push([relative, hash(contents)]);
} catch { excluded.push(relative); }
}
const packageFile = declaration(project, 'package.json');
let manifest = null;
if (packageFile) {
try { manifest = JSON.parse(packageFile.content); } catch { throw new Error('项目package.json不是有效JSON,先修复工程声明。'); }
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('项目package.json需要一个JSON对象。');
}
const fileExists = relative => fs.existsSync(safePath(project, relative, true));
const packageManager = (typeof manifest?.packageManager === 'string' ? manifest.packageManager.split('@')[0] : null) || (fileExists('pnpm-lock.yaml') ? 'pnpm' : fileExists('yarn.lock') ? 'yarn' : fileExists('bun.lock') || fileExists('bun.lockb') ? 'bun' : packageFile ? 'npm' : 'unknown');
const scripts = Object.entries(manifest?.scripts ?? {}).filter(([name, script]) => recognizedScripts.test(name) && typeof script === 'string' && script.length <= 6000)
.map(([name, script]) => ({ name, script, kind: /^test(?::|$)/i.test(name) ? 'test' : name === 'build' ? 'build' : 'check', framework: framework(script),
lifecycle: ['pre' + name, 'post' + name].filter(key => typeof manifest.scripts[key] === 'string').map(key => ({ name: key, script: manifest.scripts[key].slice(0, 6000) })) }));
const rules = [];
for (const relative of names.filter(name => /(^|\/)AGENTS\.md$/i.test(name)).slice(0, 8)) {
const file = declaration(project, relative); if (file) rules.push({ path: relative, sha256: file.sha256, excerpt: file.content.slice(0, 1800), interpretation: 'project-data' });
}
const declarations = {};
for (const relative of ['pyproject.toml', 'Cargo.toml', 'go.mod', 'tsconfig.json']) {
const file = declaration(project, relative); if (file) declarations[relative] = { sha256: file.sha256, excerpt: file.content.slice(0, 1000) };
}
const fingerprint = hash(JSON.stringify({ projectId: project.id, root, files: fingerprints, packageSha256: packageFile?.sha256 ?? null,
head: git.head, gitStatus: git.statusSha256 ?? null, complete }));
const { paths, ...visibleGit } = git;
return { projectId: project.id, name: typeof manifest?.name === 'string' ? manifest.name.slice(0, 150) : project.name,
languages: [...detected].sort(), packageManager, scripts, rules, declarations,
entryPoints: [manifest?.main, typeof manifest?.bin === 'string' ? manifest.bin : null, names.includes('src/index.ts') ? 'src/index.ts' : null, names.includes('src/index.mjs') ? 'src/index.mjs' : null].filter(value => typeof value === 'string').slice(0, 5),
git: visibleGit, fingerprint, inspectedAt: new Date().toISOString(),
scope: { complete, scannedFiles: fingerprints.length, bytes, excludedFiles: excluded.length, maximumFiles: 1000, maximumBytes: 16 * 1024 * 1024,
note: '有界快照;Git工程遵循ignore并包含未跟踪源码,跳过依赖、链接和常见凭据路径。' } };
}
export function createCheckList(inspection, names) {
if (names !== undefined && (!Array.isArray(names) || !names.length || names.length > 6 || names.some(name => typeof name !== 'string'))) throw new Error('检查脚本选择需要1到6个已识别名称。');
if (inspection.packageManager !== 'npm') return [];
const preferred = ['test', 'typecheck', 'check', 'lint', 'build'];
const selected = names ?? [...(inspection.scripts.some(script => script.name === 'test') ? ['test'] : inspection.scripts.filter(script => script.kind === 'test').map(script => script.name).slice(0, 2)), ...preferred.slice(1).filter(name => inspection.scripts.some(script => script.name === name))].slice(0, 6);
return [...new Set(selected)].map(name => {
const script = inspection.scripts.find(item => item.name === name);
if (!script) throw new Error('这个脚本不在已识别的检查能力中:' + name);
return { ...script, command: 'npm run ' + name };
});
}
+6 -1
View File
@@ -15,7 +15,12 @@ export class ArtifactStore {
const file = path.join(this.root, relative);
fs.mkdirSync(path.dirname(file), { recursive: true });
if (fs.lstatSync(path.dirname(file)).isSymbolicLink() || fs.existsSync(file) && fs.lstatSync(file).isSymbolicLink()) throw new Error('执行工件不能通过链接写入。');
if (!fs.existsSync(file)) {
if (fs.existsSync(file)) {
const existing = fs.readFileSync(file);
if (existing.length !== bytes.length || createHash('sha256').update(existing).digest('hex') !== sha256) {
throw new Error('已有执行工件校验失败,实际结果需要核对。');
}
} else {
const temporary = file + '.' + randomUUID() + '.tmp';
const handle = fs.openSync(temporary, 'wx', 0o600);
try { fs.writeFileSync(handle, bytes); fs.fsyncSync(handle); } finally { fs.closeSync(handle); }