Files
Agent-Workbench/src/core/evaluation/evaluation-manager.mjs
T

264 lines
28 KiB
JavaScript

import { randomUUID } from 'node:crypto';
import { modelSnapshot } from '../../shared/contracts/execution.mjs';
import { workerUsage, COLLABORATION_CLOSED } from '../../shared/contracts/workers.mjs';
import { evaluationInput, evaluationSeal, evaluationSampleSeal, evaluationHash, evaluationSummary, evaluationConflict } from '../../shared/contracts/evaluations.mjs';
import { evaluationRecord, preparedSample, verifyEvaluationSources } from './provenance.mjs';
import { createCheckList } from '../project-inspector.mjs';
import { nodeEnvironmentBinding } from '../environment/environment-inspector.mjs';
import { verifyNodeBinding } from '../environment/environment-inspector.mjs';
import { verifyEnvironmentPreparation } from '../environment/environment-evidence.mjs';
const at = () => new Date().toISOString();
const identity = source => Object.fromEntries(['root','head','reference','branch','gitDir','commonDir','indexSha256','statusSha256','sourceFingerprint','treeSha256','configuration','filterDrivers'].map(key => [key, source[key]]));
const terminal = new Set(['completed','retained']);
const commandView = checks => checks.map(({ name, kind, script, command, framework, lifecycle, python }) => ({ name, kind, script, command, framework, lifecycle, ...(python ? { python } : {}) }));
export class EvaluationManager {
constructor(store, runtime) { this.store = store; this.runtime = runtime; this.active = new Map(); }
batches(session) { return [...this.store.core.evaluation.values()].filter(batch => batch.sessionId === session.id); }
isActive(sessionId) { return [...this.active.values()].some(value => value.sessionId === sessionId); }
projectActive(projectId, excludeId = null) { return [...this.active.entries()].some(([id,value]) => id !== excludeId && value.projectId === projectId); }
save(session,batch,phase) { batch.updatedAt = at(); return this.store.putRecord('evaluation',session,batch,'evaluation.'+phase); }
own(session,id,fingerprint = null) { return evaluationRecord(this.store,session,id,fingerprint); }
view(session) {
return { batches: this.batches(session).map(batch => ({ ...batch, summary: evaluationSummary(batch), samples: batch.samples.map(sample => ({ ...sample,
workers: sample.groupId ? this.runtime.workerManager().view(session).groups.find(group => group.id === sample.groupId)?.workers ?? [] : [] })) })) };
}
artifact(session,batch,content,kind) {
const artifact = this.store.artifactStore.write(session,this.store.owned('run',batch.runId,session.id),content,{kind});
this.store.core.artifact.set(artifact.id,artifact); return artifact;
}
async plan(session,input) {
this.runtime.workerManager().assertIdle(session);
if (this.batches(session).some(batch => !terminal.has(batch.status)) || [...this.store.core.evaluation.values()].some(batch => batch.projectId === session.projectId && !terminal.has(batch.status))) throw evaluationConflict('工程已有未收尾评测,请先处理原样本或保留现场结束。');
if ([...this.store.core.collaboration.values()].some(group => group.projectId === session.projectId && !COLLABORATION_CLOSED.has(group.status))) throw evaluationConflict('工程已有协作,请先完成原协作。');
const initial = structuredClone(this.store.config), config = this.runtime.provider.prepareConfig?.(initial,this.store) ?? initial;
const definition = evaluationInput(input,config), id = randomUUID();
const active={sessionId:session.id,projectId:session.projectId,controller:new AbortController()};
active.promise=new Promise(resolve=>{active.settle=resolve;});this.active.set(id,active);
try {
const source = await this.runtime.workerManager().workspaces.snapshot(this.store.project(session.projectId));
active.controller.signal.throwIfAborted();
const run = this.store.beginRun(session,definition.title,false,config,{forceNewTask:true});
session.status = 'paused'; this.store.finishRun(session,run,'evaluation_plan');
const batch = {schemaVersion:1,id,projectId:session.projectId,sessionId:session.id,taskId:session.taskId,runId:run.id,
...definition,source:identity(source),model:modelSnapshot(config),samples:definition.samples.map(sample => ({...sample,id:randomUUID(),status:'pending',report:null})),
status:'planned',createdAt:at(),updatedAt:at(),startedAt:null,deadline:null,finishedAt:null,adoption:null,
message:'固定需求、来源、验收条件、检查选择与总预算已封存。尚未创建工作区或请求模型。'};
batch.fingerprint = evaluationHash(evaluationSeal(batch));
const artifact = this.artifact(session,batch,evaluationSeal(batch),'evaluation-plan');
batch.contractArtifactId = artifact.id; batch.contractSha256 = artifact.sha256;
this.store.core.task.get(batch.taskId).origin = 'engineering-evaluation';
this.store.message(session,'user',definition.title); this.save(session,batch,'planned'); return batch;
} finally { this.active.delete(id); active.settle(); }
}
sample(batch,id) { const sample = batch.samples.find(sample => sample.id === id); if (!sample) throw evaluationConflict('样本不属于当前评测。'); return sample; }
group(session,batch,sample) { return this.runtime.workerManager().own(session,sample.groupId,null,{current:false}); }
launch(session,batch,sample,status,execute,{allowCompleted=false}={}) {
if (this.active.has(batch.id) || this.runtime.projectHasActivity(session.projectId)) throw evaluationConflict('工程已有执行,请等待完成。');
if ((terminal.has(batch.status) && !(allowCompleted&&batch.status==='completed')) || batch.adoption) throw evaluationConflict('本批已结束或进入采纳审查,不能再次执行样本。');
const controller = new AbortController(), active = {sessionId:session.id,projectId:session.projectId,controller,promise:null};
this.active.set(batch.id,active); sample.previousStatus=sample.status; batch.status = status; sample.status = status; this.save(session,batch,status);
active.promise = (async () => {
try { return await execute(controller.signal); }
catch (error) {
const group = sample.groupId && this.store.core.collaboration.get(sample.groupId);
sample.status = group?.requiresVerification ? 'unknown' : controller.signal.aborted ? 'stopped' : 'failed';
batch.status = sample.status; sample.error = this.runtime.provider.redact(error.message ?? String(error)); batch.message = sample.error;
return batch;
} finally { this.save(session,batch,batch.status); this.active.delete(batch.id); }
})();
active.promise.catch(()=>{}); return {batch,promise:active.promise};
}
selectParent(session,group) {
if (session.changes.some(change => ['pending','conflict'].includes(change.status) && change.taskId !== group.taskId)) throw evaluationConflict('父工程有其他任务的待审查改动,请先处理。');
session.taskId = group.taskId; session.runId = group.runId; session.status = 'paused';
this.store.save(session,{type:'evaluation.select_task',detail:{groupId:group.id}});
}
prepare(session,id,fingerprint,sampleId) {
const batch = this.own(session,id,fingerprint), sample = this.sample(batch,sampleId), index = batch.samples.indexOf(sample);
if (sample.status !== 'pending' || batch.samples.slice(0,index).some(value => !value.report)) throw evaluationConflict('请按固定队列顺序准备;已尝试的样本不会自动重建。');
return this.launch(session,batch,sample,'preparing',async signal => {
const manager = this.runtime.workerManager(), source = await manager.workspaces.snapshot(this.store.project(session.projectId));
if (JSON.stringify(identity(source)) !== JSON.stringify(batch.source)) throw evaluationConflict('来源工程已变化,请保留原批结果后重新规划。');
const initial = structuredClone(this.store.config), config = this.runtime.provider.prepareConfig?.(initial,this.store) ?? initial;
if (JSON.stringify(modelSnapshot(config)) !== JSON.stringify(batch.model)) throw evaluationConflict('模型连接或池修订已变化,请重新封存评测。');
const part = key => Math.floor(batch.budget[key]/batch.samples.length);
const budget = {maxTokens:Math.min(part('maxTokens'),config.maxRunTokens),maxSteps:part('maxSteps'),maxToolCalls:part('maxToolCalls'),maxDurationMs:batch.budget.maxDurationMs};
const original = {maxTokens:500,maxSteps:1,maxToolCalls:1}, candidate = Object.fromEntries(Object.keys(original).map(key=>[key,budget[key]-original[key]]));
signal.throwIfAborted();
const group = await manager.plan(session,{goal:sample.goal+'\n验收条件:'+sample.criteria,budget,children:[
{label:'原版 · '+sample.label,goal:'只执行原固定检查,不修改工程,不请求模型。',paths:sample.paths,budget:original},
{label:'候选 · '+sample.label,goal:sample.goal+'\n验收条件:'+sample.criteria+'\n必须执行固定检查:'+sample.checks.join('、')+'。不得修改测试或运行声明。',paths:sample.paths,budget:candidate},
]},{evaluation:{batchId:batch.id,sampleId:sample.id}});
sample.groupId = group.id; this.save(session,batch,'workspace_intent'); signal.throwIfAborted();
await manager.prepare(session,group.id,group.fingerprint).promise;
if (group.status !== 'ready') throw evaluationConflict(group.summary);
const inventories = [];
for (const child of group.children) inventories.push(await manager.inventory(this.store.project(child.projectId)));
if (JSON.stringify(inventories[0].files) !== JSON.stringify(inventories[1].files)) throw evaluationConflict('原版与候选检出不是同一字节来源。');
if (sample.paths.some(path=>!inventories[0].files.some(file=>file.path===path))) throw evaluationConflict('允许修改范围必须是来源提交中已有的明确源码文件。');
const commands = inventories.map(value=>commandView(createCheckList(value.inspection,sample.checks)));
if (!commands[0].some(check=>check.kind==='test') || JSON.stringify(commands[0])!==JSON.stringify(commands[1])) throw evaluationConflict('样本必须包含同源的真实测试检查。');
sample.files = inventories[0].files; sample.commands = commands[0]; sample.children = group.children.map(child=>({id:child.id,role:child.evaluation.role,projectId:child.projectId,sessionId:child.sessionId,taskId:child.taskId}));
const environments=group.children.map(child=>nodeEnvironmentBinding(this.store.project(child.projectId),{runtimeDirectory:this.runtime.checkRunner().runtimeDirectory}));
if(environments.some(Boolean)) {
if(!environments[0]||!environments[1]||environments[0].dependencyInputs!==environments[1].dependencyInputs)throw evaluationConflict('原版与候选的依赖声明或Node/npm运行时不同。');
sample.environmentInputs={kind:'node',dependencyInputs:environments[0].dependencyInputs};
}
const artifact = this.artifact(session,batch,evaluationSampleSeal(batch,sample),'evaluation-sample');
sample.contractArtifactId = artifact.id; sample.contractSha256 = artifact.sha256;
preparedSample(this.store,batch,sample); signal.throwIfAborted();
sample.status = 'ready'; batch.status = 'ready'; batch.message = '同源检出、原文件和实际检查命令已封存。请核对后运行原版。'; return batch;
});
}
remaining(batch) {
batch.startedAt ??= at(); batch.deadline ??= Date.parse(batch.startedAt)+batch.budget.maxDurationMs;
const remaining = batch.deadline-Date.now(); if (remaining<=0) throw evaluationConflict('本批总时限已用完,原记录保留;可封存已有结果或重新规划。'); return remaining;
}
baseline(session,id,fingerprint,sampleId) {
const batch=this.own(session,id,fingerprint),sample=this.sample(batch,sampleId);
if (sample.status!=='ready' || sample.baselineAttemptedAt) throw evaluationConflict('原版尚未就绪或已经尝试,不重放检查。');
preparedSample(this.store,batch,sample);
return this.launch(session,batch,sample,'baseline_running',async signal=>{
const remaining=this.remaining(batch), group=this.group(session,batch,sample), child=group.children.find(child=>child.evaluation.role==='original');
sample.baselineAttemptedAt=at();this.save(session,batch,'baseline_intent');
const owner=this.store.session(child.sessionId), runner=this.runtime.checkRunner(), plan=await runner.plan(owner,sample.checks);
const combined=AbortSignal.any([signal,AbortSignal.timeout(remaining)]), started=await runner.start(owner,plan.id,{signal:combined,source:'user'});
sample.baselineJobId=started.job.id;this.save(session,batch,'baseline_started');await started.promise;
const worker=this.store.core.worker.get(child.id);worker.status=started.job.requiresVerification?'unknown':'baseline_complete';worker.finishedAt=at();worker.summary=started.job.summary;worker.requiresVerification=!!started.job.requiresVerification;
this.store.putRecord('worker',owner,worker,'evaluation.original_finished');
sample.status=started.job.requiresVerification?'unknown':'baseline_complete';batch.status=sample.status;batch.message='原版结果已保存:'+started.job.summary;return batch;
});
}
start(session,id,fingerprint,sampleId) {
const batch=this.own(session,id,fingerprint),sample=this.sample(batch,sampleId);
if (!['baseline_complete','awaiting_review','paused','failed','stopped'].includes(sample.status)||!sample.baselineJobId||sample.report) throw evaluationConflict('先完成原版固定检查;已封存样本不重放。');
preparedSample(this.store,batch,sample);
return this.launch(session,batch,sample,'candidate_running',async signal=>{
const remaining=this.remaining(batch),group=this.group(session,batch,sample),child=group.children.find(child=>child.evaluation.role==='candidate');
this.selectParent(session,group);await verifyEvaluationSources(this.store,this.store.session(child.sessionId));signal.throwIfAborted();
if(sample.environmentInputs){
const baseline=this.store.core.check_job.get(sample.baselineJobId),candidate=nodeEnvironmentBinding(this.store.project(child.projectId),{runtimeDirectory:this.runtime.checkRunner().runtimeDirectory});
if(!baseline?.environment||!candidate||baseline.environment.dependencyInputs!==candidate.dependencyInputs||baseline.environment.installed.sha256!==candidate.installed.sha256)throw evaluationConflict('候选与原版检查的实际依赖环境不同。请保留原结果,先核对两侧环境;已尝试原版不重放。');
}
group.startedAt ??= at();group.deadline ??= Date.now()+remaining; group.deadline=Math.min(group.deadline,batch.deadline);
const started=this.runtime.workerManager().start(session,group.id,group.fingerprint,child.id);
const stop=()=>{void this.runtime.workerManager().stopParent(session.id);};signal.addEventListener('abort',stop,{once:true});
try{await started.promise;}finally{signal.removeEventListener('abort',stop);}
const worker=this.store.core.worker.get(child.id);sample.status=worker.requiresVerification?'unknown':signal.aborted?'stopped':worker.status==='completed'?'candidate_complete':worker.status;
batch.status=sample.status;batch.message=worker.summary;return batch;
});
}
roleEvidence(batch,sample,role) {
const child=sample.children?.find(child=>child.role===role);
if (!child) return {outcome:sample.status==='stopped'?'stopped':'unknown',attempts:[],usage:null,reason:sample.error??'尚未完成同源准备。'};
const owner=this.store.session(child.sessionId),worker=this.store.core.worker.get(child.id),task=this.store.core.task.get(child.taskId);
const attempts=[...this.store.core.check_job.values()].filter(job=>job.taskId===child.taskId).map(job=>{
const plan=this.runtime.checkRunner().planFor(owner,job.planId);
if(JSON.stringify(plan.checks.map(check=>check.name))!==JSON.stringify(sample.checks))throw evaluationConflict('历史检查没有执行原完整选择。');
const results=job.results.map(result=>{
const artifact=this.store.owned('artifact',result.logArtifactId,owner.id);this.store.artifactStore.read(artifact);
if(artifact.kind!=='check-log'||artifact.taskId!==child.taskId||artifact.runId!==job.runId)throw evaluationConflict('原检查日志归属不一致。');
const extra={};for(const key of ['metricsArtifactId','inputArtifactId'])if(result[key]){const file=this.store.owned('artifact',result[key],owner.id);this.store.artifactStore.read(file);extra[key]={id:file.id,sha256:file.sha256};}
return {name:result.name,command:result.command,status:result.status,exitCode:result.exitCode,durationMs:result.durationMs,tests:result.tests??null,logArtifactId:artifact.id,logSha256:artifact.sha256,...extra};
});
return {id:job.id,planId:plan.id,status:job.status,stale:!!job.stale,requiresVerification:!!job.requiresVerification,startedAt:job.startedAt,finishedAt:job.finishedAt,sourceFingerprint:job.sourceFingerprint,summary:job.summary,results,...(job.environment?{environment:job.environment}:{})};
});
const latest=attempts.at(-1), raw=role==='original'?latest?.status:worker?.status,lastRun=this.store.core.run.get(worker?.currentRunId);
const outcome=worker?.requiresVerification||latest?.requiresVerification||latest?.stale?'unknown':role==='candidate'&&raw==='completed'?'passed':role==='original'&&raw==='passed'?'passed':raw==='failed'?'failed':['stopped','cancelled'].includes(raw)||role==='candidate'&&lastRun?.stopReason==='cancelled'?'stopped':'unknown';
const usage=workerUsage(this.store,worker);
return {workerId:child.id,projectId:child.projectId,sessionId:child.sessionId,taskId:child.taskId,outcome,workerStatus:worker?.status,reason:worker?.summary??sample.error??null,
startedAt:role==='original'?sample.baselineAttemptedAt:worker?.startedAt,finishedAt:worker?.finishedAt??latest?.finishedAt??null,attempts,
usage:role==='original'?{requests:0,totalTokens:0,reported:true,reason:'原版没有请求模型。'}:{...usage,reported:usage.requests>0&&usage.unknownRequests===0,cost:null,costReason:'上游未报告可核对费用。'},
approvals:owner.approvals.filter(approval=>approval.taskId===task.id||owner.events.some(event=>event.taskId===task.id&&event.approvalId===approval.id)).map(approval=>({id:approval.id,status:approval.status,tool:approval.tool,decidedAt:approval.decidedAt??null})),
changes:owner.changes.filter(change=>change.taskId===child.taskId).map(change=>({id:change.id,path:change.path,status:change.status,sha256:change.sha256,baseSha256:change.baseSha256,changeSetId:change.changeSetId??null})),
requests:[...this.store.core.context_plan.values()].filter(plan=>plan.taskId===child.taskId).map(plan=>({id:plan.id,runId:plan.runId,artifactId:plan.artifactId??null})),
result:worker?.result??null};
}
seal(session,id,fingerprint,sampleId) {
const batch=this.own(session,id,fingerprint),sample=this.sample(batch,sampleId);
if(sample.report||sample.status==='pending')throw evaluationConflict('样本尚未尝试或已封存,不能改写原报告。');
return this.launch(session,batch,sample,'collecting',async()=>{
let inputVerified=false,inputError=null;
try{preparedSample(this.store,batch,sample);for(const child of sample.children)await verifyEvaluationSources(this.store,this.store.session(child.sessionId));inputVerified=true;}catch(error){inputError=this.runtime.provider.redact(error.message);}
const evidence=role=>{try{return this.roleEvidence(batch,sample,role);}catch(error){const child=sample.children?.find(child=>child.role===role);return {outcome:'unknown',evidenceError:this.runtime.provider.redact(error.message),attempts:[...this.store.core.check_job.values()].filter(job=>job.taskId===child?.taskId).map(job=>({id:job.id,status:job.status,results:job.results.map(result=>({name:result.name,logArtifactId:result.logArtifactId}))})),usage:null};}};
const original=evidence('original'),candidate=evidence('candidate');
let environmentComparison=null;
if(sample.environmentInputs){
const left=original.attempts.at(-1)?.environment,right=candidate.attempts.at(-1)?.environment;
const matched=!!left&&!!right&&left.dependencyInputs===sample.environmentInputs.dependencyInputs&&left.dependencyInputs===right.dependencyInputs&&left.installed.sha256===right.installed.sha256;
environmentComparison={dependencyInputs:sample.environmentInputs.dependencyInputs,matched,original:left??null,candidate:right??null};
if(!matched){inputVerified=false;inputError='原版与候选缺少相同实际依赖输入的检查证据。';}
for(const child of sample.children){const owner=this.store.session(child.sessionId),job=[...this.store.core.check_job.values()].findLast(job=>job.taskId===child.taskId);
try{if(job?.environment){verifyNodeBinding(this.store.project(child.projectId),job.environment);verifyEnvironmentPreparation(this.store,child.projectId,job.environment);}}
catch(error){inputVerified=false;inputError=this.runtime.provider.redact(error.message);}
}
}
// Missing comparable check inputs cannot erase an observed cancellation.
// A stopped candidate remains in the denominator and cannot be adopted.
if(!inputVerified){original.outcome='unknown';if(candidate.outcome!=='stopped')candidate.outcome='unknown';}
const report={schemaVersion:1,batchId:batch.id,batchFingerprint:batch.fingerprint,sampleId:sample.id,sampleSha256:sample.contractSha256??null,
label:sample.label,goal:sample.goal,criteria:sample.criteria,source:batch.source,model:batch.model,evidenceMode:batch.evidenceMode,budget:batch.budget,
inputVerified,inputError,commands:sample.commands??[],original,candidate,...(environmentComparison?{environmentComparison}:{}),summary:{original:original.outcome,candidate:candidate.outcome},
inferenceValidated:false,note:batch.evidenceMode==='local-fixture'?'本地固定响应仅证明执行链与真实工程检查,不证明模型自主完成率。':'当前样本结果仅适用于封存需求和工程;不外推整体模型能力。',sealedAt:at()};
const artifact=this.artifact(session,batch,report,'evaluation-report');sample.report={artifactId:artifact.id,sha256:artifact.sha256,summary:report.summary};sample.status='sealed';
const group=sample.groupId&&this.store.core.collaboration.get(sample.groupId);
if(group&&!group.requiresVerification){group.status='resolved_external';group.finishedAt=at();this.runtime.workerManager().save(session,group,'evaluation_sealed');}
batch.status=group?.requiresVerification?'unknown':batch.samples.every(sample=>sample.report)?'completed':'between_samples';
batch.message='原样本分母及全部尝试已封存。'+(inputError??'可查看对照报告,或继续下一个固定样本。');if(batch.status==='completed')batch.finishedAt=at();return batch;
});
}
readReport(session,batch,sample) {
this.own(session,batch.id);if(!sample.report)throw evaluationConflict('样本尚未封存报告。');
const artifact=this.store.owned('artifact',sample.report.artifactId,session.id);
if(artifact.kind!=='evaluation-report'||artifact.taskId!==batch.taskId||artifact.runId!==batch.runId||artifact.sha256!==sample.report.sha256)throw evaluationConflict('报告原工件身份不一致。');
const report=JSON.parse(this.store.artifactStore.read(artifact));
if(report.batchId!==batch.id||report.sampleId!==sample.id||report.batchFingerprint!==batch.fingerprint||JSON.stringify(report.summary)!==JSON.stringify(sample.report.summary))throw evaluationConflict('对照报告与原样本不一致。');return report;
}
adoptionFor(session,group) {
const batch=this.own(session,group.evaluation.batchId),sample=this.sample(batch,group.evaluation.sampleId),report=this.readReport(session,batch,sample);
if(!batch.adoption||batch.adoption.sampleId!==sample.id||batch.adoption.reportSha256!==sample.report.sha256||!report.inputVerified||report.summary.candidate!=='passed'||!['passed','failed'].includes(report.summary.original))throw evaluationConflict('候选采纳与原通过报告不一致。');
return {batch,sample,report};
}
adopt(session,id,fingerprint,sampleId) {
const batch=this.own(session,id,fingerprint),sample=this.sample(batch,sampleId),report=this.readReport(session,batch,sample);
if(this.runtime.projectHasActivity(session.projectId)||batch.adoption||batch.samples.some(sample=>!sample.report)||!report.inputVerified||report.summary.candidate!=='passed'||!['passed','failed'].includes(report.summary.original))throw evaluationConflict('请先封存全部样本;只有原版结果完整、原输入可核对且通过的候选才能采纳。');
const manager=this.runtime.workerManager(),group=this.group(session,batch,sample);
if(group.requiresVerification)throw evaluationConflict('原执行仍待核对。');
return this.launch(session,batch,sample,'adopting',async signal=>{
await manager.sourceFor(group);await manager.verifiedResult(group,group.children.find(child=>child.evaluation.role==='candidate'));signal.throwIfAborted();
this.selectParent(session,group);batch.adoption={sampleId:sample.id,groupId:group.id,reportSha256:sample.report.sha256,status:'preparing'};batch.status='adopting';this.save(session,batch,'adopting');
try{
group.status='ready_to_collect';manager.save(session,group,'evaluation_adoption');
await manager.collect(session,group.id,group.fingerprint,{evaluation:true}).promise;
if(group.status!=='collected')throw evaluationConflict(group.summary);
await manager.prepareMerge(session,group.id,group.fingerprint).promise;
if(group.status!=='merge_prepared')throw evaluationConflict(group.summary);
batch.adoption.status='awaiting_review';batch.status='awaiting_delivery';batch.message='候选已形成父工程待审查变更集。请审查应用、检查父工程并完成本地 Git,再核对交付。';
}catch(error){batch.status='unknown';batch.message=this.runtime.provider.redact(error.message);}
sample.status='sealed';this.save(session,batch,batch.status);return batch;
},{allowCompleted:true});
}
async finalize(session,id,fingerprint) {
const batch=this.own(session,id,fingerprint);if(batch.status!=='awaiting_delivery')throw evaluationConflict('尚未进入父工程交付。');
const group=this.store.core.collaboration.get(batch.adoption.groupId);this.adoptionFor(session,group);
await this.runtime.workerManager().finalize(session,group.id,group.fingerprint);
batch.adoption.status='completed';batch.adoption.delivery=group.delivery;batch.status='completed';batch.finishedAt=at();batch.message='原候选已审查应用、父工程重新检查通过并完成本地 Git。';this.save(session,batch,'delivered');return batch;
}
retain(session,id,fingerprint) {
const batch=this.own(session,id,fingerprint);
if(batch.adoption?.status==='completed')throw evaluationConflict('交付已完成,不能改写原交付状态。');
if(this.runtime.projectHasActivity(session.projectId))throw evaluationConflict('请先停止所属执行。');
for(const sample of batch.samples){const group=sample.groupId&&this.store.core.collaboration.get(sample.groupId);if(group&&!COLLABORATION_CLOSED.has(group.status)){this.selectParent(session,group);this.runtime.workerManager().discard(session,group.id,group.fingerprint);}}
batch.status='retained';batch.finishedAt=at();batch.message='已保留全部工作区、原结果和未执行样本,结束本批;没有声明交付成功。';this.save(session,batch,'retained');return batch;
}
async stopParent(sessionId) {
const active=[...this.active.values()].find(value=>value.sessionId===sessionId);if(!active)return;
active.controller.abort(new Error('用户停止本批执行。'));await this.runtime.workers?.stopParent(sessionId);
await Promise.allSettled([...this.store.core.evaluation.values()].filter(batch=>batch.sessionId===sessionId).flatMap(batch=>batch.samples.flatMap(sample=>sample.children??[])).map(child=>this.runtime.checks?.stop(child.sessionId)));
await active.promise;
}
async stopAll(){await Promise.all([...new Set([...this.active.values()].map(value=>value.sessionId))].map(id=>this.stopParent(id)));}
}