feat: verify existing file results and preserve honest task delivery
This commit is contained in:
+59
-4
@@ -37,6 +37,10 @@ import { EngineRunner } from '../core/engines/engine-runner.mjs';
|
||||
import { BrowserManager, publicBrowserPlan } from '../core/browser/browser-manager.mjs';
|
||||
import { browserPlanInput } from '../shared/contracts/browser.mjs';
|
||||
import { TaskPlanManager } from '../core/tasks/task-plan-manager.mjs';
|
||||
import { FileResults } from '../core/tasks/file-results.mjs';
|
||||
import { fileResultInput, fileResultToolDefinitions } from '../shared/contracts/file-results.mjs';
|
||||
import { verifiedCheck } from '../core/delivery-report.mjs';
|
||||
import { currentBrowserVerification } from '../core/browser/browser-manager.mjs';
|
||||
import { taskPlanInput, taskProofInput, PLAN_READ_TOOLS } from '../shared/contracts/task-plans.mjs';
|
||||
import { taskPlanToolDefinitions } from '../shared/contracts/task-plan-tools.mjs';
|
||||
import { ProjectMemoryManager } from '../core/memory/project-memory.mjs';
|
||||
@@ -66,6 +70,7 @@ labels.act_browser_tab = '执行原页面固定动作';
|
||||
labels.plan_task = '保存任务依赖计划';
|
||||
labels.verify_task_step = '核验任务步骤原证据';
|
||||
labels.inspect_task_plan = '核对任务计划与未完成项';
|
||||
labels.verify_existing_files = '核对当前已有成果';
|
||||
labels.read_registered_skill = '读取已登记技能修订';
|
||||
labels.call_registered_mcp = '请求调用已登记MCP工具';
|
||||
|
||||
@@ -75,6 +80,7 @@ export function validateToolArgs(tool, args) {
|
||||
if (tool === 'read_registered_skill') { skillReadInput(args); return; }
|
||||
if (tool === 'call_registered_mcp') { mcpCallInput(args); return; }
|
||||
if (tool === 'plan_task') { taskPlanInput(args); return; }
|
||||
if (tool === 'verify_existing_files') { fileResultInput(args); return; }
|
||||
if (tool === 'verify_task_step') { taskProofInput(args); return; }
|
||||
if (tool === 'inspect_task_plan') { if (Object.keys(args).length) throw new Error('任务计划查询只读取当前Task,不接受执行命令。'); return; }
|
||||
if (tool === 'inspect_browser_tabs') { if (Object.keys(args).length) throw new Error('标签页查询不接受附加参数。'); return; }
|
||||
@@ -128,6 +134,39 @@ export class Runtime {
|
||||
evaluationManager() { return this.evaluations ??= new EvaluationManager(this.store, this); }
|
||||
workerManager() { return this.workers ??= new WorkerManager(this.store, this); }
|
||||
projectIndexer() { return this.codeIndex ??= new ProjectIndex({ cacheDirectory: path.join(this.store.directory, 'cache', 'project-index') }); }
|
||||
fileResults() { return this.results ??= new FileResults(this.store); }
|
||||
async fileDelivery(session) {
|
||||
const task = this.store.owned('task', session.taskId, session.id), delivery = this.fileResults().completion(session);
|
||||
if (!delivery.satisfied) return { ...delivery, stopReason: 'delivery_incomplete', message: delivery.requiresChange
|
||||
? '当前要求包含修改,尚无对应追加要求之后的实际文件改动。已有成果核对不能代替修改。'
|
||||
: '要求的文件尚无当前有效成果或已应用改动。' + (delivery.missingPaths.length ? '缺少:' + delivery.missingPaths.join('、') + '。' : '') + '请完整读取并核对已有成果,或生成可审查改动。' };
|
||||
const policy = taskContract(task).checks;
|
||||
if (policy !== 'skip' && (policy === 'required' || task.verification?.status !== 'not_configured')) {
|
||||
const inspection = await inspectProject(this.store.project(session.projectId));
|
||||
if (!verifiedCheck(this.store, session, inspection)) return { ...delivery, satisfied: false, stopReason: 'verification_incomplete', message: '当前工程检查尚无对应当前输入的通过证据,已有成果不能代替检查。' };
|
||||
}
|
||||
const browser = await currentBrowserVerification(this.store, session);
|
||||
if (browser.required && browser.status !== 'passed') return { ...delivery, satisfied: false, stopReason: 'verification_incomplete', message: '当前页面验收尚未通过,已有成果不能代替页面检查。' };
|
||||
return delivery;
|
||||
}
|
||||
async completeFileResults(session) {
|
||||
if (this.projectHasActivity(session.projectId)) throw Object.assign(new Error('工程仍有活动,请停止或等待原操作结束后核对成果。'), { status: 409 });
|
||||
const taskId = session.taskId, runId = session.runId;
|
||||
if (workerForSession(this.store, session)) throw Object.assign(new Error('受限子任务不能独立结束父项目成果。'), { status: 409 });
|
||||
if (session.changes.some(change => ['pending', 'conflict'].includes(change.status))
|
||||
|| [...this.store.core.tool.values()].some(tool => tool.taskId === taskId && tool.requiresVerification)) throw Object.assign(new Error('仍有待审改动或未知结果,请先核对原操作。'), { status: 409 });
|
||||
assertWorkspacesResolved(this.store, session.projectId, { includePrepared: true }); assertWorkersResolved(this.store, session.projectId); assertMcpResolved(this.store, session.projectId); assertChangeSetsResolved(this.store, session.projectId);
|
||||
const delivery = await this.fileDelivery(session);
|
||||
if (!delivery.satisfied) throw Object.assign(new Error(delivery.message), { status: 409 });
|
||||
const plan = await this.taskPlanner().inspect(session, { candidates: false });
|
||||
if (plan.configured && !plan.all_verified) throw Object.assign(new Error('原执行计划仍有未核验步骤,请先核对。'), { status: 409 });
|
||||
if (session.taskId !== taskId || session.runId !== runId || this.projectHasActivity(session.projectId) || !this.fileResults().completion(session).satisfied) throw Object.assign(new Error('核对期间任务、文件或活动已变化,请重新核对。'), { status: 409 });
|
||||
const task = this.store.owned('task', taskId, session.id);
|
||||
task.delivery = { ...delivery, at: now(), checks: taskContract(task).checks === 'skip' ? 'not_requested' : task.verification.status };
|
||||
session.status = 'completed';
|
||||
this.store.message(session, 'system', '已按当前成果结束任务;' + (delivery.newChanges ? '实际修改另列。' : '无新增修改。') + '原运行、检查与Git记录保留,未请求模型。');
|
||||
this.store.save(session, { type: 'file_result.task_completed' });
|
||||
}
|
||||
taskPlanner() { return this.taskPlans ??= new TaskPlanManager(this.store, { redact: value => this.provider.redact(value),
|
||||
projectBusy: (projectId, exceptId) => this.projectHasActivity(projectId, exceptId) }); }
|
||||
browserManager() { return this.browser ??= new BrowserManager(this.store, { appDirectory: this.appDirectory,
|
||||
@@ -290,6 +329,7 @@ export class Runtime {
|
||||
.map(change => ({ path: change.path, status: change.status, id: change.id, changeSetId: change.changeSetId ?? null }));
|
||||
const unknown = [...this.store.core.tool.values()].filter(tool => tool.taskId === task.id && tool.requiresVerification)
|
||||
.map(tool => ({ invocation_id: tool.id, call_id: tool.callId, tool: tool.tool, status: tool.state, requires_verification: true }));
|
||||
const fileResults = this.fileResults().inspect(session);
|
||||
const context = this.provider.redact(projectContext(run.project) + (run.worker ? '\n受限子任务执行契约(宿主权限,不可扩展):' + JSON.stringify({
|
||||
parent_task_id: run.worker.parentTaskId, worker_id: run.worker.id, write_paths: run.worker.paths, budget: run.worker.budget,
|
||||
rule: '只有范围内提案、审查应用和当前工程检查构成完成;不得请求任意终端、独立Git提交、外部工具或嵌套协作。',
|
||||
@@ -299,6 +339,10 @@ export class Runtime {
|
||||
goal: taskContract(task).goal, instructions: task.instructions.filter(instruction => instruction.source !== 'review-action'), constraints: task.constraints ?? [], acceptance: task.acceptance ?? [], contract: taskContract(task),
|
||||
}) + (run.record.purpose === 'diagnostic' ? '\n本次仅回答运行诊断问题,允许读取所属结果;不得修改文件、执行命令或将解释视为原任务交付。诊断问题:' + run.record.question : '')
|
||||
+ '\n结果未知的调用,不得假定成功或重放:' + JSON.stringify(unknown)
|
||||
+ (fileResults.results.length || fileResults.sources.length ? '\n当前成果与可显式采用的旧来源(任务数据,原文件不等于新写入):' + JSON.stringify({
|
||||
current: fileResults.results.slice(0, 8).map(row => ({ id: row.id, taskId: row.taskId, runId: row.runId, verification: row.verification, error: row.error, reason: row.reason, files: row.files.map(({ path, sha256, origin }) => ({ path, sha256, origin })) })),
|
||||
sources: fileResults.sources.slice(0, 4),
|
||||
}) : '')
|
||||
+ '\n' + sessionContext(session, run.project, config, value => this.provider.redact(value))
|
||||
+ (task.readProgress?.warning ? '\n当前读取进展:' + task.readProgress.warning : '')
|
||||
+ (taskPlan ? '\n当前Task依赖计划与尚未核验的原步骤(任务数据,不授权额外操作):\n' + JSON.stringify({
|
||||
@@ -308,7 +352,7 @@ export class Runtime {
|
||||
}) : ''));
|
||||
const skillCatalog = run.worker ? null : this.skillRegistry().catalog(session.projectId);
|
||||
const mcpCatalog = run.worker ? null : this.mcpRegistry().catalog(session.projectId);
|
||||
const availableTools = [...toolDefinitions, ...taskPlanToolDefinitions, ...([...this.store.core.browsing_tab.values()].some(tab=>tab.sessionId===session.id&&tab.taskId===run.taskId&&tab.status==='open')?browsingToolDefinitions:[]), ...(skillCatalog?.entries.some(value => value.status === 'current') ? skillToolDefinitions : []),
|
||||
const availableTools = [...toolDefinitions, ...taskPlanToolDefinitions, ...fileResultToolDefinitions, ...([...this.store.core.browsing_tab.values()].some(tab=>tab.sessionId===session.id&&tab.taskId===run.taskId&&tab.status==='open')?browsingToolDefinitions:[]), ...(skillCatalog?.entries.some(value => value.status === 'current') ? skillToolDefinitions : []),
|
||||
...(mcpCatalog?.entries.length ? mcpToolDefinitions : []),
|
||||
...([...this.store.core.research_target.values()].some(target => target.sessionId === session.id && target.taskId === run.taskId) ? researchToolDefinitions : []),
|
||||
...([...this.store.core.engine_job.values()].some(job => job.sessionId === session.id && job.taskId === run.taskId) ? engineToolDefinitions : []),
|
||||
@@ -413,9 +457,12 @@ export class Runtime {
|
||||
this.store.finishStep(session, stepRecord);
|
||||
session.status = session.changes.some(change => ['pending', 'conflict'].includes(change.status)) ? 'needs_review' : 'completed';
|
||||
if (run.record.purpose === 'diagnostic') { session.status = 'completed'; run.stopReason = 'diagnostic_answered'; }
|
||||
else if (session.status === 'completed' && taskContract(task).kind === 'file' && !session.changes.some(change => change.taskId === task.id && change.status === 'applied')) {
|
||||
session.status = 'paused'; run.stopReason = 'delivery_incomplete';
|
||||
this.store.message(session, 'system', '答复已收到,但原目标要求的文件尚无已应用记录,不能视为文件交付完成。请生成可审查的文件改动。');
|
||||
else if (session.status === 'completed' && taskContract(task).kind === 'file') {
|
||||
const delivery = await this.fileDelivery(session);
|
||||
if (!delivery.satisfied) {
|
||||
session.status = 'paused'; run.stopReason = delivery.stopReason;
|
||||
this.store.message(session, 'system', '答复已收到,但' + delivery.message);
|
||||
} else task.delivery = { ...delivery, at: now(), checks: taskContract(task).checks === 'skip' ? 'not_requested' : task.verification?.status ?? 'not_configured' };
|
||||
}
|
||||
else if (session.status === 'completed' && taskContract(task).checks !== 'skip' && (['failed', 'stale', 'unknown', 'unverified', 'running'].includes(task.verification?.status)
|
||||
|| taskContract(task).checks === 'required' && task.verification?.status !== 'passed')) {
|
||||
@@ -563,6 +610,14 @@ export class Runtime {
|
||||
result = { status: 'planned', plan_id: plan.id, revision: plan.revision, steps: plan.steps, contract_artifact_id: plan.contractArtifactId,
|
||||
note: '计划已保存,步骤尚未核验;保留原目标和通过条件,按原工具结果核验,不直接标记完成。' }; }
|
||||
else if (tool === 'inspect_task_plan') result = await this.taskPlanner().inspect(session, { candidates: false });
|
||||
else if (tool === 'verify_existing_files') {
|
||||
result = this.fileResults().record(session, args);
|
||||
const task = this.store.core.task.get(run.taskId);
|
||||
if (!result.reused) clearReadStreak(task);
|
||||
const progress = task.readProgress ??= { schemaVersion: 1, reads: [], repeated: 0 };
|
||||
progress.resultRepeats = result.reused ? (progress.resultRepeats ?? 0) + 1 : 0;
|
||||
if (progress.resultRepeats >= 3) { progress.pause = true; progress.warning = '连续核对同一成果没有推进任务,请使用原成果凭据继续交付。'; }
|
||||
}
|
||||
else if (tool === 'verify_task_step') result = await this.taskPlanner().verify(session, args, { source: 'agent' });
|
||||
else if (tool === 'inspect_engine_checks') result = { status: 'completed', engine_checks: this.engineManager().jobs(session)
|
||||
.filter(job => job.taskId === run.taskId).map(job => this.engineManager().summary(session, job)), note: '已保存的引擎连接核验;完整原记录使用report_artifact_id读取,查询不会启动引擎。' };
|
||||
|
||||
Reference in New Issue
Block a user