fix(runtime): provide current delivery evidence after reviewed application
This commit is contained in:
@@ -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');
|
||||
}
|
||||
Reference in New Issue
Block a user