fix(runtime): provide current delivery evidence after reviewed application

This commit is contained in:
Agent Workbench Dev
2026-11-03 11:57:42 +08:00
parent 3659f5e858
commit 10dbd8f500
4 changed files with 102 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
import {readText} from '../../server/files.mjs';
import {inspectProject} from '../project-inspector.mjs';
import {taskContract} from '../../shared/contracts/task-control.mjs';
import {pendingDeliveryReview} from '../../shared/contracts/delivery-review.mjs';
// A fresh, bounded observation for the next request. It grants no permission:
// checks, proofs and Git still validate their original contracts at execution.
export async function taskDeliveryProgress(store,session,run,{checks,worker=false,taskPlan=null}={}){
if(run.purpose==='diagnostic')return null;
const task=store.owned('task',run.taskId,session.id),project=store.project(session.projectId);
const changes=session.changes.filter(change=>change.taskId===task.id&&change.status==='applied');
if(!changes.length)return null;
const latest=[...new Map(changes.map(change=>[change.path,change])).values()];
const files=latest.slice(-24).map(change=>{
let currentSha256=null,error=null;try{currentSha256=readText(project,change.path).sha256;}catch(cause){error=cause.message;}
return {path:change.path,change_id:change.id,change_set_id:change.changeSetId??null,applied_sha256:change.sha256,
current_sha256:currentSha256,current:currentSha256===change.sha256,...(error?{error}:{})};
});
const inspection=await inspectProject(project),owned=value=>value.taskId===task.id&&value.sessionId===session.id&&value.projectId===session.projectId;
const plans=[...store.core.check_plan.values()].filter(owned),jobs=[...store.core.check_job.values()].filter(owned);
const latestPlan=plans.at(-1),latestJob=jobs.at(-1);
let planCurrent=false,planError=null;
if(latestPlan)try{checks.planFor(session,latestPlan.id);planCurrent=latestPlan.fingerprint===inspection.fingerprint;}
catch(cause){planError=cause.message;}
const sourceCurrent=!!latestJob&&!latestJob.stale&&!latestJob.requiresVerification
&&(latestJob.sourceFingerprint?latestJob.sourceFingerprint===inspection.sourceFingerprint:latestJob.fingerprint===inspection.fingerprint);
const unknown=[...store.core.tool.values()].filter(value=>owned(value)&&value.requiresVerification).map(value=>value.id);
const pending=session.changes.filter(change=>['pending','conflict'].includes(change.status)).map(change=>({id:change.id,path:change.path,status:change.status}));
const review=pendingDeliveryReview(store,session),contract=taskContract(task);
const snapshot={schemaVersion:1,kind:'task-delivery-progress',task_id:task.id,run_id:run.id,source_fingerprint:inspection.sourceFingerprint,
semantics:'current_observations_not_authorization_or_delivery_proof',files,total_files:latest.length,files_truncated:latest.length>files.length,
checks_policy:contract.checks,check_plan:latestPlan?{id:latestPlan.id,current:planCurrent,checks:latestPlan.checks.map(value=>value.name),error:planError}:null,
latest_check:latestJob?{id:latestJob.id,plan_id:latestJob.planId,status:latestJob.status,current_source:sourceCurrent,
stale:!!latestJob.stale||!sourceCurrent,results:latestJob.results.map(value=>({name:value.name,status:value.status,tests:value.tests,
log_artifact_id:value.logArtifactId,log_sha256:store.core.artifact.get(value.logArtifactId)?.sha256??null}))}:null,
remaining_plan_steps:taskPlan?.remaining_steps??[],plan_steps:taskPlan?.steps.map(step=>({key:step.key,kind:step.kind,status:step.status,blockers:step.blockers,proof_id:step.proof_id}))??[],
pending_changes:pending,unknown_tool_ids:unknown,
stage:null,next_action:null,execution_note:'建议只依据当前事实;实际操作仍核对原计划和权限,执行检查等待审批,Git只准备预览并交还审查。'};
const next=(stage,tool,args={})=>{snapshot.stage=stage;snapshot.next_action=tool?{tool,arguments:args}:null;return snapshot;};
if(pending.length||unknown.length||review)return next('awaiting_review_or_verification');
if(snapshot.files_truncated||files.some(file=>!file.current))return next('applied_files_need_review');
if(contract.checks==='skip')return next('checks_not_requested',worker?null:'delivery_report');
if(!sourceCurrent||!latestJob){
if(planCurrent)return next('ready_for_checks','run_checks',{plan_id:latestPlan.id});
return next('needs_current_check_plan','plan_checks',latestPlan?.checks.length?{names:latestPlan.checks.map(value=>value.name)}:{});
}
if(latestJob.status!=='passed')return next('checks_need_repair');
if(taskPlan?.configured&&!taskPlan.all_verified){
for(const step of taskPlan.steps.filter(value=>value.status!=='verified'&&!value.blockers.length)){
const evidence=step.kind==='checks'?latestJob:step.kind==='changes'?[...store.core.change_set.values()].findLast(value=>owned(value)
&&value.direction==='apply'&&value.status==='completed'&&step.paths.every(path=>value.files.some(file=>file.path===path&&file.state==='after'))):null;
if(evidence)return next('needs_plan_evidence','verify_task_step',{plan_id:taskPlan.plan_id,expected_revision:taskPlan.revision,step_key:step.key,evidence_id:evidence.id});
}
return next('needs_plan_evidence');
}
if(worker)return next('ready_for_parent_review');
return next('ready_for_git_preview','plan_delivery');
}
+3
View File
@@ -28,6 +28,7 @@ import { ENGINE_SEED_RESERVATION } from '../shared/contracts/engine-context.mjs'
import { InputAttachments } from '../core/context/input-attachments.mjs';
import { BudgetResume } from '../core/context/budget-resume.mjs';
import { artifactPage, filePage, readMessageBudget, recordArtifactRead, recordBrowserRead, recordFileRead, recordUnchangedCheck, clearReadStreak, recordSessionInspection, readMetadataReserve, BROWSER_READ_HINT } from '../core/context/read-budget.mjs';
import { taskDeliveryProgress } from '../core/context/delivery-progress.mjs';
import { ChangeSetManager } from '../core/workspaces/change-sets.mjs';
import { assertChangeSetsResolved } from '../shared/contracts/change-sets.mjs';
import { pendingDeliveryReview } from '../shared/contracts/delivery-review.mjs';
@@ -327,6 +328,7 @@ export class Runtime {
config.inputImages=this.inputAttachments().images(session,task);
if(config.inputImages.length&&config.mode==='demo')throw new Error('固定演示执行器不会读取图片;请连接并声明支持视觉的模型。');
const taskPlan = task.currentTaskPlanId ? await this.taskPlanner().inspect(session, { candidates: false }) : null;
const deliveryProgress = await taskDeliveryProgress(this.store, session, run.record, { checks: this.checkRunner(), worker: !!run.worker, taskPlan });
const state = session.changes.filter(change => change.taskId === task.id || ['pending', 'conflict'].includes(change.status))
.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)
@@ -341,6 +343,7 @@ 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)
+ (deliveryProgress ? '\n已应用修改后的当前交付依据(每次请求重新核对,非操作授权):\n' + JSON.stringify(deliveryProgress) : '')
+ (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),