45 lines
4.1 KiB
JavaScript
45 lines
4.1 KiB
JavaScript
import fs from 'node:fs';
|
|
import { workerForSession, workerHash } from '../../shared/contracts/workers.mjs';
|
|
import { evaluationSeal, evaluationSampleSeal, evaluationHash, evaluationConflict } from '../../shared/contracts/evaluations.mjs';
|
|
import { inspectProject, verifyIndexSnapshot } from '../project-inspector.mjs';
|
|
import { safePath } from '../../server/files.mjs';
|
|
|
|
export function evaluationRecord(store, session, id, fingerprint = null) {
|
|
const batch = store.owned('evaluation', id, session.id);
|
|
if (batch.projectId !== session.projectId || evaluationHash(evaluationSeal(batch)) !== batch.fingerprint || fingerprint !== null && fingerprint !== batch.fingerprint) throw evaluationConflict('评测来源、样本或预算与原封存契约不一致。');
|
|
const artifact = store.owned('artifact', batch.contractArtifactId, session.id);
|
|
if (artifact.kind !== 'evaluation-plan' || artifact.taskId !== batch.taskId || artifact.runId !== batch.runId || artifact.sha256 !== batch.contractSha256
|
|
|| store.artifactStore.read(artifact) !== JSON.stringify(evaluationSeal(batch))) throw evaluationConflict('评测计划原工件无法核对。');
|
|
return batch;
|
|
}
|
|
export function preparedSample(store, batch, sample) {
|
|
if (!sample.contractArtifactId) throw evaluationConflict('尚未封存同源检出与实际检查命令,不能执行。');
|
|
const artifact = store.owned('artifact', sample.contractArtifactId, batch.sessionId);
|
|
const expected = evaluationSampleSeal(batch, sample);
|
|
if (artifact.kind !== 'evaluation-sample' || artifact.taskId !== batch.taskId || artifact.runId !== batch.runId || artifact.sha256 !== sample.contractSha256 || store.artifactStore.read(artifact) !== JSON.stringify(expected)) throw evaluationConflict('样本来源、固定检查或原检出字节已变化。');
|
|
return expected;
|
|
}
|
|
export async function verifyEvaluationSources(store, session, names = null) {
|
|
const worker = workerForSession(store, session);
|
|
if (!worker?.evaluation) return null;
|
|
const owner = store.session(worker.parentSessionId), batch = evaluationRecord(store, owner, worker.evaluation.batchId);
|
|
const sample = batch.samples.find(value => value.id === worker.evaluation.sampleId);
|
|
if (!sample || sample.groupId !== worker.collaborationId || !sample.children?.some(value => value.id === worker.id && value.role === worker.evaluation.role)) throw evaluationConflict('子工程不属于原评测样本。');
|
|
preparedSample(store, batch, sample);
|
|
if (names !== null && (sample.report || ['completed','retained'].includes(batch.status))) throw evaluationConflict('样本已经封存,不能重新执行检查。');
|
|
if (names !== null && JSON.stringify(names) !== JSON.stringify(sample.checks)) throw evaluationConflict('评测必须执行原封存的完整检查选择,不能缩小测试集。');
|
|
const project = store.project(session.projectId), inspection = await inspectProject(project, { includeFiles: true, strictSnapshot: true });
|
|
if (!inspection.scope.complete) throw evaluationConflict('评测工程来源范围不完整。');
|
|
const paths = new Set(sample.paths.map(value => value.toLowerCase()));
|
|
const protectedFiles = sample.files.filter(file => worker.evaluation.role === 'original' || !paths.has(file.path.toLowerCase()));
|
|
const current = inspection.files.filter(file => worker.evaluation.role === 'original' || !paths.has(file.path.toLowerCase()));
|
|
const view = files => files.map(file => ({ path: file.path, sha256: file.sha256, bytes: file.bytes })).sort((a,b)=>a.path.localeCompare(b.path));
|
|
if (JSON.stringify(view(current)) !== JSON.stringify(view(protectedFiles))) throw evaluationConflict('原测试、运行声明或非候选文件已变化;保留失败,不能据此比较。');
|
|
for (const file of protectedFiles) {
|
|
const target = safePath(project,file.path), stat = fs.lstatSync(target);
|
|
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || stat.size !== file.bytes || workerHash(fs.readFileSync(target)) !== file.sha256) throw evaluationConflict('固定评测输入无法核对:'+file.path);
|
|
}
|
|
await verifyIndexSnapshot(project,inspection);
|
|
return { batch, sample, worker, inspection };
|
|
}
|