bada-terminal / src /utils /batch-definition.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
2.54 kB
// ๋ฐฐ์น˜ ์‹คํ–‰ ์„ค์ •๊ณผ ํ…Œ์ŠคํŠธ ์ฆ๊ฑฐ์˜ ๊ณตํ†ต ๊ณ„์•ฝ. 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 };