81 lines
6.5 KiB
JavaScript
81 lines
6.5 KiB
JavaScript
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');
|
|
const 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 observations=[...store.core.tool.values()].filter(owned);
|
|
if(!changes.length&&!plans.length&&!jobs.length&&!observations.some(value=>
|
|
value.tool==='inspect_project'&&value.state==='completed'&&!value.requiresVerification))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);
|
|
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;}
|
|
let sourceCurrent=!!latestJob&&inspection.scope.complete&&!latestJob.stale&&!latestJob.requiresVerification
|
|
&&['passed','failed','unverified'].includes(latestJob.status)&&!!latestJob.finishedAt
|
|
&&(latestJob.sourceFingerprint?latestJob.sourceFingerprint===inspection.sourceFingerprint:latestJob.fingerprint===inspection.fingerprint);
|
|
let evidenceError=null;
|
|
if(sourceCurrent)try{
|
|
for(const result of latestJob.results){
|
|
const artifact=store.owned('artifact',result.logArtifactId,session.id);
|
|
if(artifact.kind!=='check-log'||artifact.taskId!==task.id||artifact.runId!==latestJob.runId)throw new Error('检查日志的原任务或运行归属不匹配。');
|
|
store.artifactStore.read(artifact);
|
|
}
|
|
}catch(cause){sourceCurrent=false;evidenceError=cause.message;}
|
|
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:2,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,
|
|
has_applied_changes:changes.length>0,
|
|
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,...(evidenceError?{evidence_error:evidenceError}:{}),
|
|
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(evidenceError)return next('requires_current_evidence_verification');
|
|
if(snapshot.files_truncated||files.some(file=>!file.current))return next('applied_files_need_review');
|
|
// A baseline observation is useful before implementation, but cannot
|
|
// certify an empty delivery or bypass the existing review/Git contracts.
|
|
if(!changes.length){
|
|
if(contract.checks==='skip')return next('observed_without_changes');
|
|
if(sourceCurrent&&latestJob)return next(latestJob.status==='passed'?'baseline_passed_without_changes':'checks_need_repair');
|
|
if(planCurrent)return next('ready_for_checks','run_checks',{plan_id:latestPlan.id});
|
|
return next('observed_without_current_checks');
|
|
}
|
|
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');
|
|
}
|