// 배치 실행 설정과 테스트 증거의 공통 계약. docs/specs/batch-certification.md v1.0.0 const { createHash } = require('crypto'); function ordered(value) { if (Array.isArray(value)) return value.map(ordered); if (value && typeof value === 'object') return Object.fromEntries(Object.keys(value).sort().map(key => [key, ordered(value[key])])); return value; } function execution(task) { const steps = task.steps_json ? JSON.parse(task.steps_json) : task.steps || []; if (!Array.isArray(steps)) throw new Error('Batch steps must be an array'); return JSON.stringify(ordered({ project: task.project_id, steps, legacy: steps.length ? null : [task.prompt || 'hello', task.max_turns || 30], loop: require('../config/loop-configuration').normalize(task.loop_config, { requireGoals: false }) })); } const fingerprint = task => createHash('sha256').update(execution(task)).digest('hex'); function certified(task) { try { return task?.is_tested_success === 1 && task.tested_fingerprint === fingerprint(task); } catch { return false; } } function forSave(previous, task) { let valid = false; try { valid = certified(previous) && previous.tested_fingerprint === fingerprint(task); } catch {} return { is_tested_success: valid ? 1 : 0, tested_fingerprint: valid ? previous.tested_fingerprint : null }; } function assertCertified(task) { if (!certified(task)) throw new Error('현재 실행 설정으로 구동 테스트를 성공시킨 뒤 스케줄을 활성화하세요.'); } function testStatus(task, status, testedFingerprint) { if (!status) return { is_tested_success: 0, tested_fingerprint: null }; if (!task || testedFingerprint !== fingerprint(task)) throw new Error('Tested batch fingerprint does not match the saved definition'); return { is_tested_success: 1, tested_fingerprint: testedFingerprint }; } // 체크포인트 정의와 원장의 자기 정책을 비교한다. 부모 상속 금액은 더 클 수 있다. function assertRequestReservation(definition, record) { const { normalize } = require('../services/runtime-request-reservation'); const own = normalize(definition.loop?.request_reserve_usd), effective = normalize(record?.requestReserveUsd); if (own !== normalize(record?.requestReserveOwnUsd) || own !== null && (effective === null || effective < own)) throw new Error('Batch request reservation differs from the saved definition'); } module.exports = { ordered, execution, fingerprint, certified, forSave, assertCertified, testStatus, assertRequestReservation };