feat: verify task dependency plans against original execution evidence

This commit is contained in:
Agent Workbench Dev
2026-10-30 10:25:55 +08:00
parent 6bc3ce442d
commit a103ab1b22
55 changed files with 1564 additions and 74 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto';
export const EXECUTION_SCHEMA = 1;
export const ACTIVE_STATES = new Set(['running', 'awaiting_approval', 'stopping']);
export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job', 'delivery_plan', 'delivery_job', 'change_set', 'context_plan', 'workspace', 'research_target', 'research_job', 'engine_job', 'browser_plan', 'browser_job'];
export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job', 'delivery_plan', 'delivery_job', 'change_set', 'context_plan', 'workspace', 'research_target', 'research_job', 'engine_job', 'browser_plan', 'browser_job', 'task_plan', 'task_proof'];
export const timestamp = () => new Date().toISOString();
export const isId = value => typeof value === 'string' && /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(value);
+18
View File
@@ -0,0 +1,18 @@
export const taskPlanToolDefinitions = [
{ type: 'function', function: { name: 'plan_task', description: '为当前复杂任务保存1–12步有依赖的计划。每步固定通过条件及实际证据类别,规划不执行操作。expected_revision首次为0;以后保留全部原步骤并追加,不能删改条件掩盖失败。原用户要求或计划变化后重新封存并核验仍有效的原证据,不重跑已完成工具。', parameters: {
type: 'object', additionalProperties: false, required: ['expected_revision', 'steps'], properties: { expected_revision: { type: 'integer', minimum: 0 }, steps: { type: 'array', minItems: 1, maxItems: 12, items: {
type: 'object', additionalProperties: false, required: ['key', 'title', 'criterion', 'kind'], properties: {
key: { type: 'string', pattern: '^[a-z][a-z0-9_-]{0,39}$' }, title: { type: 'string', maxLength: 100 }, criterion: { type: 'string', maxLength: 300 },
kind: { type: 'string', enum: ['analysis', 'changes', 'checks', 'browser'] }, depends_on: { type: 'array', items: { type: 'string' }, maxItems: 12 },
tools: { type: 'array', description: 'analysis必填,只读工具名称,例如inspect_project/read_file/query_code。', items: { type: 'string' } },
paths: { type: 'array', description: 'changes必填,实际改动的项目相对文件路径。', items: { type: 'string' }, maxItems: 32 },
checks: { type: 'array', description: 'checks必填,实际检查名称,例如test/typecheck/build或python:test。', items: { type: 'string' }, maxItems: 6 },
viewport_width: { type: 'integer', description: 'browser必填,固定桌面/手机视口宽度。', minimum: 320, maximum: 1440 },
actions: { type: 'array', description: 'browser需要的实际操作,始终要求非空通过断言及进程退出。', items: { type: 'string', enum: ['fill', 'select', 'click', 'assert_text', 'assert_count', 'assert_visible'] } },
} } } } } } },
{ type: 'function', function: { name: 'verify_task_step', description: '核验当前计划的一步,前置步骤必须先核验。evidence_id为原tool_invocation_id、已应用change_set_id、实际检查job_id或browser_job_id;类别按原步骤决定。核对原字节/当前输入,失败和失效保留,模型不能直接标完成,不重新执行工具。', parameters: {
type: 'object', additionalProperties: false, required: ['plan_id', 'step_key', 'evidence_id', 'expected_revision'], properties: {
plan_id: { type: 'string' }, step_key: { type: 'string' }, evidence_id: { type: 'string' }, expected_revision: { type: 'integer', minimum: 1 },
} } } },
{ type: 'function', function: { name: 'inspect_task_plan', description: '核对当前Task原计划、步骤依赖及现有证明的有效性。返回ready/blocked/verified/stale/needs_update及未完成项和原工件引用;读取不执行或重放工具。', parameters: { type: 'object', additionalProperties: false, properties: {} } } },
];
+74
View File
@@ -0,0 +1,74 @@
import { createHash } from 'node:crypto';
import { isId } from './execution.mjs';
export const TASK_PLAN_LIMITS = Object.freeze({ steps: 12, text: 6000, revisions: 24 });
export const PLAN_READ_TOOLS = ['inspect_project', 'read_file', 'list_files', 'search_files', 'index_project', 'query_code', 'read_research_evidence', 'inspect_engine_checks', 'read_browser_result'];
export const planHash = value => createHash('sha256').update(typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value)).digest('hex');
export const planError = (message, code = 'invalid', status = 409) => Object.assign(new Error(message), { status, code });
const object = value => value && typeof value === 'object' && !Array.isArray(value);
const keyPattern = /^[a-z][a-z0-9_-]{0,39}$/;
const text = (value, max, label) => {
if (typeof value !== 'string' || !value.trim() || value.length > max || /\0/.test(value)) throw planError(label + '需要1–' + max + '字文本。', 'input', 400);
return value.trim();
};
const unique = (values, label, max = 32) => {
if (!Array.isArray(values) || values.length > max || values.some(value => typeof value !== 'string' || !value || value.length > 160)
|| new Set(values).size !== values.length) throw planError(label + '需要不重复的文本列表。', 'input', 400);
return [...values];
};
export function taskPlanInput(input) {
if (!object(input) || Object.keys(input).some(key => !['expected_revision', 'steps'].includes(key))
|| !Number.isSafeInteger(input.expected_revision) || input.expected_revision < 0) throw planError('任务计划需要expected_revision和steps,不能添加执行命令或完成状态。', 'input', 400);
if (!Array.isArray(input.steps) || !input.steps.length || input.steps.length > TASK_PLAN_LIMITS.steps) throw planError('任务计划需要1–12个步骤。', 'input', 400);
const steps = input.steps.map(value => {
if (!object(value) || Object.keys(value).some(key => !['key', 'title', 'criterion', 'kind', 'depends_on', 'tools', 'paths', 'checks', 'viewport_width', 'actions'].includes(key))
|| typeof value.key !== 'string' || !keyPattern.test(value.key) || !['analysis', 'changes', 'checks', 'browser'].includes(value.kind)) throw planError('步骤需要key、title、criterion和有效证据类别。', 'input', 400);
const step = { key: value.key, title: text(value.title, 100, '步骤标题'), criterion: text(value.criterion, 300, '通过条件'), kind: value.kind,
depends_on: unique(value.depends_on ?? [], '前置依赖', TASK_PLAN_LIMITS.steps).sort() };
const allowed = value.kind === 'analysis' ? ['tools'] : value.kind === 'changes' ? ['paths'] : value.kind === 'checks' ? ['checks'] : ['viewport_width', 'actions'];
if (['tools', 'paths', 'checks', 'viewport_width', 'actions'].some(key => value[key] !== undefined && !allowed.includes(key))) throw planError('步骤证据字段与类别不一致。', 'input', 400);
if (value.kind === 'analysis') {
step.tools = unique(value.tools, '读取工具', PLAN_READ_TOOLS.length).sort();
if (!step.tools.length || step.tools.some(tool => !PLAN_READ_TOOLS.includes(tool))) throw planError('工程读取只能引用已声明的实际只读工具。', 'input', 400);
} else if (value.kind === 'changes') {
step.paths = unique(value.paths, '文件范围').sort();
if (!step.paths.length || step.paths.some(file => /[\\:\0]/.test(file) || file.startsWith('/') || file.split('/').some(part => !part || part === '..' || ['.git', '.hg', '.svn'].includes(part)))) throw planError('源码步骤需要项目内常规相对文件路径。', 'input', 400);
} else if (value.kind === 'checks') {
step.checks = unique(value.checks, '检查名称', 6).sort();
if (!step.checks.length) throw planError('检查步骤必须固定至少一个实际检查名称。', 'input', 400);
} else {
if (!Number.isSafeInteger(value.viewport_width) || value.viewport_width < 320 || value.viewport_width > 1440) throw planError('页面步骤需要320–1440像素的固定视口宽度。', 'input', 400);
step.viewport_width = value.viewport_width; step.actions = unique(value.actions ?? [], '页面必要操作', 6).sort();
if (step.actions.some(action => !['fill', 'select', 'click', 'assert_text', 'assert_count', 'assert_visible'].includes(action))) throw planError('页面必要操作无效。', 'input', 400);
}
return step;
});
const keys = new Set(steps.map(step => step.key));
if (keys.size !== steps.length) throw planError('步骤key必须唯一。', 'input', 400);
for (const step of steps) if (step.depends_on.some(key => !keys.has(key) || key === step.key)) throw planError('前置步骤必须存在,且不能引用自己。', 'input', 400);
const visiting = new Set(), done = new Set();
const walk = key => { if (visiting.has(key)) throw planError('任务步骤依赖不能形成循环。', 'input', 400);
if (done.has(key)) return; visiting.add(key); for (const dependency of steps.find(step => step.key === key).depends_on) walk(dependency); visiting.delete(key); done.add(key); };
for (const step of steps) walk(step.key);
if (steps.reduce((sum, step) => sum + step.title.length + step.criterion.length, 0) > TASK_PLAN_LIMITS.text) throw planError('计划文本超过保留预算,请缩小单个任务范围。', 'input', 400);
return { expected_revision: input.expected_revision, steps };
}
export function taskProofInput(input) {
if (!object(input) || Object.keys(input).some(key => !['plan_id', 'step_key', 'evidence_id', 'expected_revision'].includes(key))
|| !isId(input.plan_id) || typeof input.step_key !== 'string' || !keyPattern.test(input.step_key) || !isId(input.evidence_id)
|| !Number.isSafeInteger(input.expected_revision) || input.expected_revision < 1) throw planError('核验需要当前计划ID、步骤key、原证据ID和expected_revision,不能传入完成结论。', 'input', 400);
return { ...input };
}
export const taskRequirements = task => ({ goal: task.goal, instructions: (task.instructions ?? []).filter(value => value.source !== 'review-action')
.map(value => ({ id: value.id, content: value.content, ...(value.source ? { source: value.source } : {}) })),
constraints: task.constraints ?? [], acceptance: task.acceptance ?? [], workspace: task.workspaceIdentity ?? null });
export const taskPlanContract = plan => ({ schemaVersion: 1, kind: 'task-plan-contract', id: plan.id, sessionId: plan.sessionId, projectId: plan.projectId,
taskId: plan.taskId, runId: plan.runId, revision: plan.revision, parentPlanId: plan.parentPlanId, createdAt: plan.createdAt,
requirements: plan.requirements, requirementsSha256: plan.requirementsSha256, steps: plan.steps });
export const taskProofContract = proof => ({ schemaVersion: 1, kind: 'task-step-proof', id: proof.id, sessionId: proof.sessionId, projectId: proof.projectId,
taskId: proof.taskId, runId: proof.runId, planId: proof.planId, stepKey: proof.stepKey, stepSha256: proof.stepSha256,
planContractSha256: proof.planContractSha256, requirementsSha256: proof.requirementsSha256, status: proof.status,
evidenceId: proof.evidenceId, outcome: proof.outcome, error: proof.error, createdAt: proof.createdAt });