feat(tasks): review sourced requirements against current original evidence

This commit is contained in:
Agent Workbench Dev
2026-11-04 10:36:50 +08:00
parent ce3afb2f0d
commit 785dd0c54d
70 changed files with 6103 additions and 7 deletions
+15 -2
View File
@@ -9,12 +9,13 @@ import { verifyWorkspaceProject } from './workspaces/git-workspaces.mjs';
import { verifyPythonCheckEvidence } from './python-runtime.mjs';
import { currentBrowserVerification, verifyBrowserJob } from './browser/browser-manager.mjs';
import { TaskPlanManager } from './tasks/task-plan-manager.mjs';
import { RequirementReview } from './tasks/requirement-review.mjs';
import { FileResults } from './tasks/file-results.mjs';
import { taskContract } from '../shared/contracts/task-control.mjs';
import { modelIdentityLabel } from '../shared/contracts/model-identity.mjs';
const labels = { verified_committed: '检查通过并已提交', verified_uncommitted: '检查通过,等待提交', needs_review: '等待改动或交付审查',
existing_verified: '当前成果已核对,无新增修改', files_delivered: '当前文件已交付,按约定未测试', needs_results: '当前文件成果仍需核对',
existing_verified: '当前成果已核对,无新增修改', needs_requirements: '用户要求仍有待复核事项', files_delivered: '当前文件已交付,按约定未测试', needs_results: '当前文件成果仍需核对',
needs_checks: '需要实际工程检查', needs_browser_checks: '需要实际页面验收', needs_plan_verification: '任务计划仍有未完成项', needs_verification: '执行结果需要核对', in_progress: '任务仍在执行' };
const escapeCell = value => String(value ?? '').replaceAll('|', '\\|').replaceAll('\n', ' ');
@@ -103,6 +104,7 @@ export class DeliveryReporter {
const deliveryReview = pendingDeliveryReview(this.store, session);
const browserVerification = await currentBrowserVerification(this.store, session, inspection);
const planner = new TaskPlanManager(this.store), taskPlan = await planner.inspect(session, { inspection, candidates: false });
const requirements = await new RequirementReview(this.store, { planner }).inspect(session, { inspection });
const taskPlanHistory = [...this.store.core.task_plan.values()].filter(value => value.sessionId === session.id && value.taskId === taskId).map(value => {
let evidenceVerified = false, evidenceError = null;
try { planner.planFor(session, value.id); evidenceVerified = true; } catch (error) { evidenceError = error.message; }
@@ -125,6 +127,7 @@ export class DeliveryReporter {
const status = requiresVerification.length || unresolved.length ? 'needs_verification'
: ['running', 'awaiting_approval', 'stopping'].includes(session.status) ? 'in_progress'
: deliveryReview || changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review'
: requirements.configured && !requirements.all_resolved ? 'needs_requirements'
: taskPlan.configured && !taskPlan.all_verified ? 'needs_plan_verification'
: hasFileScope && !fileDelivery.satisfied ? 'needs_results'
: !check && !(hasFileScope && fileDelivery.satisfied && (contract.checks === 'skip' || contract.checks === 'unspecified' && task.verification.status === 'not_configured')) ? 'needs_checks'
@@ -136,7 +139,8 @@ export class DeliveryReporter {
project: { id: project.id, name: project.name, root: project.root }, workspace: projectWorkspaceIdentity(project),
task: { id: task.id, goal: task.goal, status: task.status, instructions: task.instructions, acceptance: task.acceptance ?? [],
contract, delivery: task.delivery ?? null,
workspaceIdentity: task.workspaceIdentity ?? null, requirementsAcceptance: 'not_recorded', note: '源码和检查证据不代替业务需求验收。' },
workspaceIdentity: task.workspaceIdentity ?? null, requirementsAcceptance: requirements.status, note: '源码和检查证据不代替业务需求验收。' },
requirements,
fileResults, fileDelivery,
checkPolicy: contract.checks === 'skip' ? 'not_requested' : contract.checks,
gitOutcome: { status: !fileDelivery.newChanges && hasFileScope ? 'no_new_changes' : commits.length ? 'committed' : 'uncommitted',
@@ -199,6 +203,15 @@ export function renderDeliveryMarkdown(report) {
}
if (report.gitOutcome) lines.push('', report.gitOutcome.note);
if (report.checkPolicy === 'not_requested') lines.push('', '测试约定:按用户要求未自动测试;不记作测试通过。');
if (report.requirements) {
lines.push('', '## 用户要求与证据', '', report.requirements.note, '', '复核状态:' + report.requirements.status,
'', '| 要求 / 条件 | 用户原文来源 | 修订 | 复核状态 | 原证据 / 下一步 |', '| --- | --- | --- | --- | --- |');
for (const item of report.requirements.items) lines.push('| ' + [item.title + ' / ' + item.criterion,
item.source.kind + ':' + item.source.id + ' [' + item.source.start + ',' + item.source.end + ') ' + item.source.quote,
item.revision, item.status, item.error ?? item.user_note ?? item.proof_artifact_id ?? (item.status === 'not_requested' ? '按约定未执行,非测试通过' : '选择实际证据或完成用户验收')].map(escapeCell).join(' | ') + ' |');
if (report.requirements.unreviewed_sources?.length) lines.push('', '尚未审阅的用户来源:' + report.requirements.unreviewed_sources.join('、'));
if (report.requirements.error) lines.push('', report.requirements.error);
}
if (report.taskPlan?.configured) {
lines.push('', '## 执行计划与实际核验', '', '计划:' + (report.taskPlan.plan_id ?? '无法核对'),
'核验:' + (report.taskPlan.verified_steps ?? 0) + '/' + (report.taskPlan.total_steps ?? 0), '',
+5
View File
@@ -14,6 +14,7 @@ import { verifyWorkspaceProject } from './workspaces/git-workspaces.mjs';
import { currentBrowserVerification } from './browser/browser-manager.mjs';
import { assertBrowserResolved } from '../shared/contracts/browser.mjs';
import { TaskPlanManager } from './tasks/task-plan-manager.mjs';
import { RequirementReview } from './tasks/requirement-review.mjs';
import { assertMcpResolved } from './tools/mcp-contract.mjs';
import { assertWorkersResolved, workerForProject } from '../shared/contracts/workers.mjs';
@@ -227,6 +228,7 @@ export class GitDelivery {
const browser = await currentBrowserVerification(this.store, session, inspection);
if (browser.required && browser.status !== 'passed') throw conflict('本任务已配置页面验收,请先取得当前源码与构建的实际浏览器断言通过记录。');
const taskPlan = await new TaskPlanManager(this.store).assertReady(session, { inspection });
const requirements = await new RequirementReview(this.store).assertReady(session, { inspection });
const files = await this.selectedFiles(session, project, snapshot);
const name = options.authorName ?? snapshot.config['user.name'] ?? 'Agent Workbench';
const email = options.authorEmail ?? snapshot.config['user.email'] ?? 'local@agent-workbench.invalid';
@@ -236,12 +238,14 @@ export class GitDelivery {
if (snapshot.digest !== second.digest || identity.taskId !== session.taskId || identity.runId !== session.runId) throw conflict('准备提交期间工程或任务已变化,请重新预览。');
this.assertIdle(session, idleOptions); this.assertTask(session);
await new TaskPlanManager(this.store).assertReady(session, { expectedBinding: taskPlan.binding_sha256 });
await new RequirementReview(this.store).assertReady(session, { expectedBinding: requirements.binding_sha256 });
const diffArtifact = this.store.artifactStore.write(session, run, files.map(file => file.diff).join('\n'), { kind: 'delivery-diff', mediaType: 'text/plain' });
this.store.core.artifact.set(diffArtifact.id, diffArtifact);
const plan = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: task.id, runId: run.id, status: 'planned', createdAt: at(),
message, author, files, paths: files.map(file => file.path), snapshot, preparedBy: source, fingerprint: inspection.fingerprint, sourceFingerprint: inspection.sourceFingerprint,
checkJobId: check.id, checkSummary: check.results.map(value => ({ name: value.name, command: value.command, exitCode: value.exitCode, tests: value.tests, logArtifactId: value.logArtifactId })),
...(taskPlan.configured ? { taskPlanId: taskPlan.plan_id, taskPlanRevision: taskPlan.revision, taskPlanBindingSha256: taskPlan.binding_sha256 } : {}),
...(requirements.configured ? { requirementBindingSha256: requirements.binding_sha256 } : {}),
...(browser.required ? { browserJobId: browser.jobId, browserPlanId: browser.planId, browserBuildSha256: browser.buildSha256 } : {}),
protectedIndexSha256: protectedIndex(snapshot.stage, snapshot.flags, files.map(file => file.path)), diffArtifactId: diffArtifact.id,
policy: { name: 'local-reviewed-snapshot', hooks: 'disabled', signing: 'unsigned', remote: 'none',
@@ -294,6 +298,7 @@ export class GitDelivery {
}
async verifyBrowserDelivery(session, plan, inspection) {
await new RequirementReview(this.store).assertReady(session, { inspection, expectedBinding: plan.requirementBindingSha256 ?? null });
await new TaskPlanManager(this.store).assertReady(session, { inspection, expectedBinding: plan.taskPlanBindingSha256 ?? null });
assertBrowserResolved(this.store, session.projectId);
const browser = await currentBrowserVerification(this.store, session, inspection);
+190
View File
@@ -0,0 +1,190 @@
import { randomUUID } from 'node:crypto';
import { TaskPlanManager } from './task-plan-manager.mjs';
import { inspectProject } from '../project-inspector.mjs';
import { verifyWorkspaceProject } from '../workspaces/git-workspaces.mjs';
import { taskContract } from '../../shared/contracts/task-control.mjs';
import { taskPlanInput, planHash, planError } from '../../shared/contracts/task-plans.mjs';
import { readText } from '../../server/files.mjs';
import { FileResults } from './file-results.mjs';
const same = (a, b) => planHash(a) === planHash(b);
const now = () => new Date().toISOString();
const checkKeys = (value, keys) => {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).some(key => !keys.includes(key)))
throw planError('要求复核包含未声明字段。', 'input', 400);
};
const requiredText = (value, max) => {
if (typeof value !== 'string' || !value.trim() || value.length > max || value.includes('\0')) throw planError('请填写有效的要求或复核说明。', 'input', 400);
return value.trim();
};
export function requirementSources(task) {
return [{ kind: 'goal', id: task.id, text: task.goal, at: task.createdAt ?? null },
...(task.instructions ?? []).filter(row => row.source !== 'review-action').map(row => ({ kind: 'instruction', id: row.id, text: row.content, at: row.createdAt ?? row.at ?? null }))]
.map(row => ({ ...row, sha256: planHash({ kind: row.kind, id: row.id, text: row.text }) }));
}
function itemInput(value, sources) {
checkKeys(value, ['key', 'title', 'criterion', 'source', 'kind', 'tools', 'paths', 'checks', 'viewport_width', 'actions', 'method']);
checkKeys(value.source, ['kind', 'id', 'start', 'end']);
const original = sources.find(row => row.kind === value.source.kind && row.id === value.source.id);
const { start, end } = value.source;
if (!original || !Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || end <= start || end > original.text.length)
throw planError('要求来源必须是当前Task用户原文的有效范围。', 'input', 400);
const method = value.method ?? 'evidence';
if (!['evidence', 'user', 'not_requested'].includes(method)) throw planError('复核方式无效。', 'input', 400);
const { source, method: ignored, ...fields } = value;
// Reuse the existing bounded evidence scopes; a statement alone never passes.
const step = taskPlanInput({ expected_revision: 0, steps: [{ ...fields, depends_on: [] }] }).steps[0];
if (method === 'not_requested' && step.kind !== 'checks') throw planError('仅检查约定可记录为未执行。', 'input', 400);
return { ...step, method, source: { ...source, quote: original.text.slice(start, end), sourceSha256: original.sha256, at: original.at } };
}
const ledgerContract = row => ({ schemaVersion: 1, kind: 'task-requirements', id: row.id, sessionId: row.sessionId, projectId: row.projectId,
taskId: row.taskId, runId: row.runId, revision: row.revision, parentId: row.parentId, items: row.items, sources: row.sources, createdAt: row.createdAt });
const proofContract = row => ({ schemaVersion: 1, kind: 'requirement-proof', id: row.id, sessionId: row.sessionId, projectId: row.projectId,
taskId: row.taskId, runId: row.runId, key: row.key, requirementSha256: row.requirementSha256, requirementRevision: row.requirementRevision,
evidenceId: row.evidenceId, outcome: row.outcome, sourceFingerprint: row.sourceFingerprint, status: row.status, error: row.error,
note: row.note, createdAt: row.createdAt });
// This ledger lives on the existing Task. It delegates all engineering proof
// checks to TaskPlanManager and grants no tool, command or file permissions.
export class RequirementReview {
constructor(store, { planner = new TaskPlanManager(store), redact = String } = {}) { this.store = store; this.planner = planner; this.redact = redact; }
current(session) { return this.store.owned('task', session.taskId, session.id).requirementRevisions?.at(-1) ?? null; }
verifyRecord(session, row, kind, contract) {
if (row.sessionId !== session.id || row.projectId !== session.projectId || row.taskId !== session.taskId) throw planError('要求记录不属于当前项目和Task。');
const original = this.planner.artifact(session, row.contractArtifactId, kind, row.taskId, row.runId);
if (original.artifact.sha256 !== row.contractSha256 || original.text !== JSON.stringify(contract(row))) throw planError('要求或复核记录与原工件不一致。');
}
write(session, run, row, kind, contract) {
const artifact = this.store.artifactStore.write(session, run, contract(row), { kind });
this.store.core.artifact.set(artifact.id, artifact);
return { ...row, contractArtifactId: artifact.id, contractSha256: artifact.sha256 };
}
async evidence(session, item, id, inspection) {
if (item.kind === 'results') {
const result = new FileResults(this.store).verify(session, id, { current: false });
if (result.taskId !== session.taskId || !item.paths.every(file => result.files.some(row => row.path === file)))
throw planError('成果凭据不属于当前Task或未覆盖要求文件。');
const files = result.files.filter(file => item.paths.includes(file.path));
for (const file of files) if (readText(this.store.project(session.projectId), file.path).sha256 !== file.sha256)
throw planError('当前要求的成果文件SHA已变化。', 'stale');
const artifact = this.store.owned('artifact', result.contractArtifactId, session.id);
return { kind: 'file_result', id: result.id, runId: result.runId, scope: 'current_requirement_files',
files: files.map(({ path, sha256, origin }) => ({ path, sha256, origin })),
artifacts: [{ id: artifact.id, kind: artifact.kind, runId: artifact.runId, sha256: artifact.sha256, bytes: artifact.bytes }] };
}
const outcome = await this.planner.evidence(session, item, id, { inspection });
if (item.kind === 'analysis') {
const record = this.store.owned('tool', id, session.id);
const result = JSON.parse(this.planner.artifact(session, record.resultArtifactId, 'tool-result', record.taskId, record.runId).text);
if (record.tool === 'read_file' && (!result.sha256 || readText(this.store.project(session.projectId), result.path).sha256 !== result.sha256))
throw planError('原读取对应旧文件SHA,请使用当前文件的实际读取。', 'stale');
if (record.tool === 'inspect_project' && result.sourceFingerprint !== inspection.sourceFingerprint)
throw planError('原工程观察对应旧源码输入。', 'stale');
}
return outcome;
}
save(session, input) { return this.planner.mutate(session, 'user', async () => {
checkKeys(input, ['expected_revision', 'requirements']);
const { task, run } = this.planner.task(session), previous = this.current(session), revision = previous?.revision ?? 0;
if (previous) this.verifyRecord(session, previous, 'task-requirements', ledgerContract);
if (input.expected_revision !== revision) throw planError('要求修订已变化,请刷新后保存。', 'stale');
if (revision >= 24 || !Array.isArray(input.requirements) || !input.requirements.length || input.requirements.length > 24)
throw planError('每个Task最多24项要求与24次修订。', 'input', 400);
const sources = requirementSources(task), items = input.requirements.map(value => itemInput(value, sources));
if (new Set(items.map(row => row.key)).size !== items.length || previous?.items.some(row => !items.some(item => item.key === row.key)))
throw planError('要求ID需唯一;原要求不能删除,可修订并保留原记录。', 'input', 400);
if (JSON.stringify(items).length > 24000) throw planError('要求范围过大,请缩小Task。', 'input', 400);
for (const item of items) {
if (item.method === 'not_requested' && taskContract(task).checks !== 'skip') throw planError('当前用户约定没有跳过检查,不能标为按约定未执行。');
const old = previous?.items.find(row => row.key === item.key);
const { revision: oldRevision, ...oldContent } = old ?? {};
item.revision = old && same(oldContent, item) ? oldRevision : (oldRevision ?? 0) + 1;
}
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
if (session.taskId !== task.id || session.runId !== run.id || this.current(session)?.id !== previous?.id || !same(sources, requirementSources(task)))
throw planError('保存期间原任务、来源或要求已变化。', 'stale');
const row = this.write(session, run, { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: task.id, runId: run.id,
revision: revision + 1, parentId: previous?.id ?? null, items, sources, createdAt: now() }, 'task-requirements', ledgerContract);
(task.requirementRevisions ??= []).push(row);
this.store.save(session, { type: 'task.requirements_saved', detail: { id: row.id, revision: row.revision } });
return row;
}); }
async inspect(session, { candidates = false, inspection = null } = {}) {
const { task, run } = this.planner.task(session), ledger = this.current(session), sources = requirementSources(task);
const base = { configured: !!ledger, task_id: task.id, revision: ledger?.revision ?? 0, sources, items: [], all_resolved: false, binding_sha256: null,
note: '证据核对只证明所选范围;模型完成答复不等于业务验收。' };
if (!ledger) return { ...base, status: 'not_recorded', unreviewed_sources: sources.map(row => row.id) };
try { this.verifyRecord(session, ledger, 'task-requirements', ledgerContract); }
catch (error) { return { ...base, status: 'unverified', error: this.redact(error.message) }; }
const unreviewed = sources.filter(row => !ledger.sources.some(old => old.id === row.id && old.sha256 === row.sha256)).map(row => row.id);
const items = [], proofs = task.requirementProofs ?? [];
for (const item of ledger.items) {
const source = sources.find(row => row.id === item.source.id && row.kind === item.source.kind);
const proof = proofs.findLast(row => row.key === item.key && row.requirementSha256 === planHash(item) && row.status === 'verified');
const attempt = proofs.findLast(row => row.key === item.key && row.requirementSha256 === planHash(item));
let status = 'pending', error = null, outcome = null;
try {
if (!source || source.sha256 !== item.source.sourceSha256) throw planError('原用户来源已变化。', 'stale');
if (item.method === 'not_requested') {
if (taskContract(task).checks !== 'skip') throw planError('当前检查约定已变化。', 'stale');
status = 'not_requested';
} else if (proof) {
this.verifyRecord(session, proof, 'requirement-proof', proofContract);
const sourceRead = item.kind === 'analysis' && this.store.core.tool.get(proof.evidenceId)?.tool === 'read_file';
if (item.kind === 'analysis' || item.method === 'user') {
inspection ??= await inspectProject(this.store.project(session.projectId));
if (!inspection.scope.complete || !sourceRead && proof.sourceFingerprint !== inspection.sourceFingerprint) throw planError('复核后源码输入已变化。', 'stale');
}
if (item.method !== 'user') {
outcome = await this.evidence(session, item, proof.evidenceId, inspection);
if (!same(outcome, proof.outcome)) throw planError('实际记录与原复核结果不一致。', 'stale');
}
status = item.method === 'user' ? 'user_confirmed' : 'evidence_verified';
} else if (attempt?.status === 'rejected') status = 'failed';
} catch (cause) { status = cause.code === 'stale' ? 'stale' : 'unverified'; error = this.redact(cause.message); }
items.push({ ...item, status, error, evidence: outcome, proof_id: proof?.id ?? null, proof_artifact_id: proof?.contractArtifactId ?? null,
user_note: status === 'user_confirmed' ? proof.note : null,
last_rejection: attempt?.status === 'rejected' ? { id: attempt.id, message: attempt.error, at: attempt.createdAt } : null,
...(candidates && item.method === 'evidence' ? { candidates: this.planner.candidates(session, item) } : {}) });
}
if (task.id !== session.taskId || run.id !== session.runId || ledger.id !== this.current(session)?.id) throw planError('核对期间Task/Run或要求已变化。', 'stale');
const resolved = !unreviewed.length && items.every(row => ['evidence_verified', 'user_confirmed', 'not_requested'].includes(row.status));
return { ...base, items, unreviewed_sources: unreviewed, status: resolved ? 'resolved' : 'incomplete', all_resolved: resolved,
history: proofs.map(row => ({ id: row.id, key: row.key, revision: row.requirementRevision, status: row.status, error: row.error, at: row.createdAt, artifact_id: row.contractArtifactId })),
contract_artifact_id: ledger.contractArtifactId, contract_sha256: ledger.contractSha256,
binding_sha256: resolved ? planHash({ ledger: ledger.contractSha256, proofs: items.map(row => [row.key, row.proof_id, row.status, row.evidence]) }) : null };
}
verify(session, input) { return this.planner.mutate(session, 'user', async () => {
checkKeys(input, ['expected_revision', 'key', 'evidence_id', 'note']);
const { task, run } = this.planner.task(session), ledger = this.current(session);
if (!ledger || ledger.revision !== input.expected_revision) throw planError('要求修订已变化,请刷新后复核。', 'stale');
this.verifyRecord(session, ledger, 'task-requirements', ledgerContract);
const item = ledger.items.find(row => row.key === input.key);
if (!item || item.method === 'not_requested') throw planError('请选择需要复核的当前要求。');
if ((task.requirementProofs?.length ?? 0) >= 128) throw planError('复核记录已到上限,请缩小任务。');
if (item.method === 'user' ? input.evidence_id !== undefined : input.note !== undefined) throw planError('用户确认和原证据核对不能混用。', 'input', 400);
const note = item.method === 'user' ? requiredText(input.note, 1000) : null;
let outcome = null, inspection = null, error = null;
try {
const source = requirementSources(task).find(row => row.kind === item.source.kind && row.id === item.source.id);
if (!source || source.sha256 !== item.source.sourceSha256) throw planError('用户要求来源已变化。', 'stale');
await verifyWorkspaceProject(this.store, this.store.project(session.projectId));
inspection = await inspectProject(this.store.project(session.projectId));
if (!inspection.scope.complete) throw planError('源码范围不完整,不能复核。');
if (item.method !== 'user') outcome = await this.evidence(session, item, input.evidence_id, inspection);
} catch (cause) { error = this.redact(cause.message); }
if (task.id !== session.taskId || run.id !== session.runId || ledger.id !== this.current(session)?.id) throw planError('复核期间任务或要求已变化。', 'stale');
const proof = this.write(session, run, { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: task.id, runId: run.id,
key: item.key, requirementSha256: planHash(item), requirementRevision: item.revision, evidenceId: input.evidence_id ?? null,
sourceFingerprint: inspection?.sourceFingerprint ?? null, outcome, status: error ? 'rejected' : 'verified', error, note, createdAt: now() }, 'requirement-proof', proofContract);
(task.requirementProofs ??= []).push(proof);
this.store.save(session, { type: error ? 'task.requirement_rejected' : 'task.requirement_verified', detail: { id: proof.id, key: item.key } });
return { status: error ? 'error' : 'completed', proof_id: proof.id, ...(error ? { message: error } : {}) };
}); }
async assertReady(session, { inspection = null, expectedBinding = undefined } = {}) {
const view = await this.inspect(session, { inspection });
if (view.configured && !view.all_resolved) throw planError('用户要求仍有待复核、失效或未审阅来源,请先核对要求与证据。', 'stale');
if (expectedBinding !== undefined && expectedBinding !== view.binding_sha256) throw planError('Git预览后的要求或证据已变化,请重新预览。', 'stale');
return view;
}
}