8 lines
6.4 KiB
JSON
8 lines
6.4 KiB
JSON
{
|
|
"commit": "2a44f15693516b48ef46a851bba4d1b69b33f844",
|
|
"path": "src/shared/contracts/evaluations.mjs",
|
|
"moduleSha256": "a5c00329eacbad3a87df8dfd21fd770fce06a11f5104d2118ff4ae1d67efb33e",
|
|
"source": "import { createHash } from 'node:crypto';\nimport { workerPath, workerPathKey } from './workers.mjs';\n\nexport const EVALUATION_ACTIVE = new Set(['preparing', 'baseline_running', 'candidate_running', 'collecting', 'adopting']);\nexport const evaluationConflict = message => Object.assign(new Error(message), { status: 409 });\nexport const evaluationHash = value => createHash('sha256').update(typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value)).digest('hex');\nconst object = (value, keys, label) => {\n if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).some(key => !keys.includes(key))) throw evaluationConflict(label + '字段无效。');\n};\nconst text = (value, limit, label) => {\n if (typeof value !== 'string' || !value.trim() || value.length > limit || !value.isWellFormed() || /[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]/.test(value)) throw evaluationConflict(label + '不能为空或超出长度范围。');\n return value.trim();\n};\nconst integer = (value, fallback, min, max, label) => {\n const result = value ?? fallback;\n if (!Number.isSafeInteger(result) || result < min || result > max) throw evaluationConflict(label + '超出允许范围。');\n return result;\n};\nexport function evaluationSourcePath(value) {\n const relative = workerPath(value);\n if (!/\\.(?:[cm]?[jt]sx?|py|go|rs|cs|vue|css|html)$/.test(relative)\n || /(?:^|\\/)(?:tests?|__tests__|fixtures?|scripts?|\\.github|runtime|releases|vendor|node_modules)(?:\\/|$)/i.test(relative)\n || /(?:^|[./_-])(?:test|spec|config|setup|conftest)(?:[./_-]|$)/i.test(relative)) throw evaluationConflict('候选范围仅接受明确的源码文件;测试、运行声明和验收脚本保持原输入。');\n return relative;\n}\nexport function evaluationInput(input, config) {\n object(input, ['title', 'samples', 'budget', 'evidenceMode'], '评测');\n if (!Array.isArray(input.samples) || !input.samples.length || input.samples.length > 3) throw evaluationConflict('每批评测需要1到3个固定样本。');\n const count = input.samples.length;\n const samples = input.samples.map(item => {\n object(item, ['label', 'goal', 'criteria', 'paths', 'checks'], '样本');\n if (!Array.isArray(item.paths) || !item.paths.length || item.paths.length > 16) throw evaluationConflict('样本需要1到16个明确源码文件。');\n const paths = item.paths.map(evaluationSourcePath).sort();\n if (new Set(paths.map(workerPathKey)).size !== paths.length) throw evaluationConflict('源码文件不能重复。');\n if (!Array.isArray(item.checks) || !item.checks.length || item.checks.length > 6 || item.checks.some(name => typeof name !== 'string' || !/^(?:test(?::[a-z0-9_-]+)?|typecheck|check|lint|build|python:(?:test|compile))$/.test(name))\n || new Set(item.checks).size !== item.checks.length) throw evaluationConflict('检查需要1到6个不同的已识别名称。');\n return { label: text(item.label, 64, '样本名称'), goal: text(item.goal, 6000, '需求'), criteria: text(item.criteria, 4000, '验收条件'), paths, checks: [...item.checks] };\n });\n if (new Set(samples.map(item => item.label)).size !== count) throw evaluationConflict('样本名称不能重复。');\n const raw = input.budget ?? {}; object(raw, ['maxTokens', 'maxSteps', 'maxToolCalls', 'maxDurationMs'], '总预算');\n const maximumTokens = Math.min(config.maxTaskTokens ?? 1000000, (config.maxRunTokens ?? 200000) * count);\n const budget = {\n maxTokens: integer(raw.maxTokens, maximumTokens, count * 2000, maximumTokens, '总token预算'),\n maxSteps: integer(raw.maxSteps, Math.min(200, (config.maxSteps ?? 12) * count), count * 2, 200, '总请求步数'),\n maxToolCalls: integer(raw.maxToolCalls, Math.min(512, count * 64), count * 2, 512, '总工具次数'),\n maxDurationMs: integer(raw.maxDurationMs, config.maxRunDurationMs ?? 600000, count * 1000, Math.min(3600000, config.maxRunDurationMs ?? 600000), '总执行时限'),\n };\n const evidenceMode = input.evidenceMode ?? 'configured-model';\n if (!['configured-model', 'local-fixture'].includes(evidenceMode)) throw evaluationConflict('评测来源需要明确为实际配置模型或本地固定响应。');\n return { title: text(input.title, 120, '评测名称'), samples, budget, evidenceMode };\n}\nexport function evaluationSeal(batch) {\n return { schemaVersion: 1, id: batch.id, projectId: batch.projectId, sessionId: batch.sessionId, taskId: batch.taskId, runId: batch.runId,\n title: batch.title, source: batch.source, budget: batch.budget, evidenceMode: batch.evidenceMode, model: batch.model,\n samples: batch.samples.map(({ id, label, goal, criteria, paths, checks }) => ({ id, label, goal, criteria, paths, checks })) };\n}\nexport function evaluationSampleSeal(batch, sample) {\n return { schemaVersion: 1, batchId: batch.id, batchFingerprint: batch.fingerprint, sampleId: sample.id, groupId: sample.groupId,\n baseCommit: batch.source.head, definition: evaluationSeal(batch).samples.find(value => value.id === sample.id),\n files: sample.files, commands: sample.commands, children: sample.children };\n}\nexport function evaluationSummary(batch) {\n const outcomes = batch.samples.map(sample => sample.report?.summary ?? { original: sample.baselineAttemptedAt ? 'unknown' : 'pending', candidate: ['failed','stopped','unknown'].includes(sample.status) ? sample.status : 'pending' });\n const count = (role, status) => outcomes.filter(item => item[role] === status).length;\n return { samples: batch.samples.length, finished: batch.samples.filter(item => !!item.report).length,\n original: Object.fromEntries(['passed', 'failed', 'stopped', 'unknown', 'pending'].map(status => [status, count('original', status)])),\n candidate: Object.fromEntries(['passed', 'failed', 'stopped', 'unknown', 'pending'].map(status => [status, count('candidate', status)])),\n inferenceValidated: false, scope: batch.evidenceMode === 'local-fixture' ? '固定响应仅验证执行链和工程检查,不计模型自主能力。' : '保留当前固定样本的实际结果,不外推任意工程成功率。' };\n}\n",
|
|
"scope": "Exact prior committed Workbench module, retained as a source-bound regression pilot. This is not an injected defect or the current source implementation."
|
|
}
|