feat: verify existing file results and preserve honest task delivery
This commit is contained in:
@@ -10,6 +10,7 @@ CORE_KINDS.push('evaluation');
|
||||
CORE_KINDS.push('environment_diagnostic','environment_plan','environment_job');
|
||||
CORE_KINDS.push('preview_plan','preview_job');
|
||||
CORE_KINDS.push('browsing_profile','browsing_tab','browsing_observation','browsing_action');
|
||||
CORE_KINDS.push('file_result');
|
||||
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);
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// This recognizes explicit file actions only; it does not certify business semantics.
|
||||
export const negatedFileAction = prefix => /(?:不要|无需|不需要|不用|禁止|不得|不必|勿|不允许|不能|不再|避免|不)(?:\s|再|去|进行|执行|直接|自动|擅自|任何|做|你)*(?:.{0,24}(?:或|和|以及)\s*)?$|\b(?:do not|don't|without|never|no need to|must not|cannot|can't)\b.{0,40}$|(?:解释|说明|分析|告诉我).{0,24}(?:怎么|如何)\s*$|\bhow\s+to\s*$/i.test(prefix);
|
||||
export function requestedFilePaths(text) {
|
||||
const paths = [];
|
||||
for (const clause of String(text).split(/[,,;;。\n]/)) {
|
||||
const action = /(?:创建|新建|保存|写入|修改|重写|修复|调整|替换)|\b(?:create|save|write|modify|rewrite|fix|change|replace)\b/i.exec(clause);
|
||||
if (!action || negatedFileAction(clause.slice(0, action.index))) continue;
|
||||
const suffix = clause.slice(action.index + action[0].length);
|
||||
if (/\b(?:from|using|based on)\b|(?:根据|参考)/i.test(suffix.split(/(?:[\w\u3400-\u9fff@.-]+\/)*[\w\u3400-\u9fff@-]+\.(?:html?|svg|txt|md|json|[cm]?[jt]sx?|css|py|ya?ml)\b/i)[0])) continue;
|
||||
for (const match of suffix.matchAll(/(?:[\w\u3400-\u9fff@.-]+\/)*[\w\u3400-\u9fff@-]+\.(?:html?|svg|txt|md|json|[cm]?[jt]sx?|css|py|ya?ml)\b/ig)) paths.push(match[0]);
|
||||
}
|
||||
return [...new Set(paths)];
|
||||
}
|
||||
export function changeRequirement(task) {
|
||||
const positive = text => String(text).split(/[,,;;。\n]/).some(clause => {
|
||||
const match = /(?:修改|重写|修复|改写|调整|新增功能|增加功能|删除|替换)|\b(?:modify|rewrite|fix|change|delete|replace)\b/i.exec(clause);
|
||||
return match && !negatedFileAction(clause.slice(0, match.index));
|
||||
});
|
||||
const latest = (task.instructions ?? []).findLast(item => item.source !== 'review-action' && positive(item.content));
|
||||
const goal = task.contract?.goal ?? task.goal;
|
||||
return latest ? { required: true, after: latest.createdAt, text: latest.content, paths: requestedFilePaths(latest.content) }
|
||||
: { required: positive(goal), after: task.createdAt, text: goal, paths: requestedFilePaths(goal) };
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { isId } from './execution.mjs';
|
||||
import { isContinuation, taskContract } from './task-control.mjs';
|
||||
import { planHash, planError, taskRequirements } from './task-plans.mjs';
|
||||
export { requestedFilePaths, changeRequirement } from './file-intent.mjs';
|
||||
|
||||
export function fileResultInput(input, { source = 'agent' } = {}) {
|
||||
if (!input || typeof input !== 'object' || Array.isArray(input) || Object.keys(input).some(key => !['files', 'reason'].includes(key))
|
||||
|| typeof input.reason !== 'string' || !input.reason.trim() || input.reason.length > 600 || input.reason.includes('\0')
|
||||
|| !Array.isArray(input.files) || !input.files.length || input.files.length > 8) throw planError('成果核对需要1–8个文件和采用原因(最多600字)。', 'input', 400);
|
||||
const files = input.files.map(file => {
|
||||
if (!file || typeof file !== 'object' || Object.keys(file).some(key => !['path', 'sha256', 'read_call_ids', 'source_result_id'].includes(key))
|
||||
|| typeof file.path !== 'string' || !file.path || file.path.length > 240 || /[\\:\0]/.test(file.path) || file.path.startsWith('/')
|
||||
|| file.path.split('/').some(part => !part || part === '.' || part === '..') || !/^[a-f0-9]{64}$/.test(file.sha256)
|
||||
|| file.source_result_id !== undefined && !isId(file.source_result_id)) throw planError('需要项目内文件路径、当前SHA256和有效来源ID。', 'input', 400);
|
||||
const calls = file.read_call_ids ?? [];
|
||||
if (!Array.isArray(calls) || calls.length > 128 || calls.some(id => typeof id !== 'string' || !id || id.length > 160) || new Set(calls).size !== calls.length
|
||||
|| source === 'agent' && !calls.length || source === 'user' && calls.length) throw planError('模型需引用本Task实际read_file调用;用户核对由宿主直接读取。', 'input', 400);
|
||||
return { path: file.path, sha256: file.sha256, read_call_ids: calls, ...(file.source_result_id ? { source_result_id: file.source_result_id } : {}) };
|
||||
});
|
||||
if (new Set(files.map(file => file.path.toLowerCase())).size !== files.length) throw planError('核对文件不能重复。', 'input', 400);
|
||||
return { files, reason: input.reason.trim() };
|
||||
}
|
||||
|
||||
export function fileResultRequirements(task) {
|
||||
const value = taskRequirements(task);
|
||||
value.instructions = value.instructions.filter(item => !isContinuation(item.content));
|
||||
return structuredClone({ ...value, contract: taskContract(task) });
|
||||
}
|
||||
export const fileResultRequirementsHash = task => planHash(fileResultRequirements(task));
|
||||
|
||||
export const fileResultToolDefinitions = [{ type: 'function', function: {
|
||||
name: 'verify_existing_files', description: '把已存在且满足当前要求的文本文件登记为当前成果,不改写文件、不代替测试或业务验收。先用本Task的read_file完整分页读取,再传当前SHA与原read_call_ids;跨Task采用须显式source_result_id。文件/要求变化凭据失效。明确修改要求仍需本Task实际修改。返回原成果ID,可用于results类计划步骤;相同文件不重复登记。',
|
||||
parameters: { type: 'object', additionalProperties: false, required: ['files', 'reason'], properties: {
|
||||
reason: { type: 'string', minLength: 1, maxLength: 600 }, files: { type: 'array', minItems: 1, maxItems: 8, items: { type: 'object', additionalProperties: false, required: ['path', 'sha256', 'read_call_ids'], properties: {
|
||||
path: { type: 'string' }, sha256: { type: 'string', pattern: '^[a-f0-9]{64}$' }, read_call_ids: { type: 'array', minItems: 1, maxItems: 128, items: { type: 'string' } }, source_result_id: { type: 'string' },
|
||||
} } },
|
||||
} },
|
||||
} }];
|
||||
@@ -1,4 +1,5 @@
|
||||
// Task intent is retained independently of short follow-ups and system actions.
|
||||
import { changeRequirement, requestedFilePaths, negatedFileAction } from './file-intent.mjs';
|
||||
export const isContinuation = content => /^(?:请)?(?:继续(?:完成|执行|开发|原任务|这个任务)?|接着(?:完成|执行)?|continue(?:\s+(?:the\s+)?(?:task|work))?)[。.!!\s]*$/i.test(String(content).trim());
|
||||
|
||||
export function isDiagnosticQuestion(content) {
|
||||
@@ -29,15 +30,16 @@ function hasFileDeliveryRequest(goal) {
|
||||
// Do not connect a past-tense modifier ("保存的原工件") to a later
|
||||
// prohibition ("不修改文件") and turn a read into a delivery requirement.
|
||||
const clauses = String(goal).split(/[,,;;。\n]|而是|但是|\bbut\b/i);
|
||||
const action = /(?:创建|新建|保存|写入)(?!的|过的|好的|后的|下来的).{0,60}?(?:文件|HTML|[\w-]+\.(?:html?|txt|svg|md)\b)|\bcreate\b.{0,60}?\b(?:file|html|svg)\b/ig;
|
||||
const action = /(?:创建|新建|保存|写入|修改|重写|修复|调整|替换)(?!的|过的|好的|后的|下来的).{0,60}?(?:文件|HTML|[\w-]+\.(?:html?|txt|svg|md)\b)|\b(?:create|modify|rewrite|fix|change|replace)\b.{0,60}?\b(?:file|html|svg)\b/ig;
|
||||
return clauses.some(clause => [...clause.matchAll(action)].some(match => {
|
||||
const prefix = clause.slice(0, match.index);
|
||||
return !/(?:不要|无需|不需要|不用|禁止|不得|不必|勿|不允许|不能|不再|避免|不)(?:\s|再|去|进行|执行|直接|自动|擅自|你|任何)*(?:.{0,24}(?:或|和|以及)\s*)?$|\b(?:do not|don't|without|never|no need to|must not|cannot|can't)\b.{0,40}$/i.test(prefix);
|
||||
return !negatedFileAction(prefix);
|
||||
}));
|
||||
}
|
||||
|
||||
export function updateTaskContract(task, instruction) {
|
||||
const current = taskContract(task);
|
||||
if (hasFileDeliveryRequest(instruction)) current.kind = 'file';
|
||||
// Only an explicit new user instruction changes the check policy.
|
||||
if (/无需(?:任何)?测试|不(?:需要|用|要)(?:进行|做|运行)?测试|\b(?:no tests?|without (?:running )?tests?)\b/i.test(instruction)) current.checks = 'skip';
|
||||
else if (/(?:运行|执行|通过|修复|补充|添加).{0,12}(?:测试|工程检查)|\b(?:run|pass|fix|add)\b.{0,24}\b(?:tests?|checks?)\b/i.test(instruction)) current.checks = 'required';
|
||||
@@ -65,6 +67,14 @@ export function settleAppliedTask(store, session) {
|
||||
if (!applied.length) return;
|
||||
if (!task) { if (!session.taskId) session.status = 'completed'; return; }
|
||||
const contract = taskContract(task, id => store.core.task.get(id));
|
||||
const demand = changeRequirement(task), recent = applied.filter(change => change.taskId === task.id && (!demand.after || change.createdAt >= demand.after && change.appliedAt >= demand.after));
|
||||
const paths = new Set(applied.map(change => change.path));
|
||||
const targets = requestedFilePaths([contract.goal, ...(task.instructions ?? []).filter(item => item.source !== 'review-action').map(item => item.content)].join('\n'));
|
||||
if (targets.some(path => !paths.has(path)) || demand.required && (!recent.length || demand.paths.some(path => !recent.some(change => change.path === path)))) {
|
||||
session.status = 'paused';
|
||||
store.message(session, 'system', '文件已应用,但当前目标的文件范围或追加修改要求尚未满足,请按原目标继续核对。');
|
||||
return;
|
||||
}
|
||||
task.delivery = { status: 'files_applied', paths: applied.map(change => change.path), at: new Date().toISOString(),
|
||||
checks: contract.checks === 'skip' ? 'not_requested' : task.verification?.status ?? 'not_configured' };
|
||||
session.status = contract.checks === 'required' || task.currentTaskPlanId || ['failed', 'stale', 'unknown', 'unverified'].includes(task.verification?.status) && contract.checks !== 'skip'
|
||||
|
||||
@@ -3,9 +3,9 @@ export const taskPlanToolDefinitions = [
|
||||
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 },
|
||||
kind: { type: 'string', enum: ['analysis', 'changes', 'results', '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 },
|
||||
paths: { type: 'array', description: 'changes/results必填,项目相对文件路径。changes仅核验本Task实际应用;results核验verify_existing_files的当前文件成果。', 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'] } },
|
||||
|
||||
@@ -23,15 +23,15 @@ export function taskPlanInput(input) {
|
||||
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);
|
||||
|| typeof value.key !== 'string' || !keyPattern.test(value.key) || !['analysis', 'changes', 'results', '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'];
|
||||
const allowed = value.kind === 'analysis' ? ['tools'] : ['changes', 'results'].includes(value.kind) ? ['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') {
|
||||
} else if (['changes', 'results'].includes(value.kind)) {
|
||||
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') {
|
||||
|
||||
Reference in New Issue
Block a user