bada-terminal / src /services /batch-loop-ledger.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
3.58 kB
/** ๋ฐฐ์น˜ ๋ฃจํ”„์˜ ํšŒ์ฐจ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ์ข…๋ฃŒ ๊ธฐ๋ก. docs/specs/batch-loop-records.md v1.3.0 */
class BatchLoopLedger {
constructor({ db, run, goalId }) {
this.db = db; this.owner = run; this.iteration = null;
this.accountedCost = 0; this.accountedTokens = 0;
this.record = { run_id: 'looprun-' + run.runId, goal_id: goalId, parent_run_id: run.runId,
status: 'running', total_iterations: 0, total_tokens_used: 0, total_cost: 0,
goal_met: 0, started_at: new Date().toISOString(), completed_at: null };
db.saveLoopRun(this.record);
}
totals() {
const cost = this.owner.cumulativeCost ?? 0, tokens = this.owner.tokensUsed ?? 0;
if (!Number.isFinite(cost) || cost < this.accountedCost || !Number.isSafeInteger(tokens) || tokens < this.accountedTokens) throw new Error('Invalid loop usage counters');
return { cost, tokens };
}
delta() { const total = this.totals(); return { cost: total.cost - this.accountedCost, tokens: total.tokens - this.accountedTokens }; }
beginIteration(number, commit) {
if (this.record.status !== 'running' || this.iteration?.status === 'running') throw new Error('Loop iteration owner is not ready');
const next = { id: `iter-${this.record.run_id}-${number}`, loop_run_id: this.record.run_id,
iteration_number: number, status: 'running', started_at: new Date().toISOString(), completed_at: null };
this.db.runInTransaction(() => { this.db.saveLoopIteration(next); commit?.(); }); this.iteration = next;
}
completeIteration({ status, outputs, goalMet = false, error = null, terminal = null }, commit) {
if (!this.iteration || this.iteration.status !== 'running') throw new Error('No active loop iteration');
const total = this.totals(), delta = this.delta(), completed = new Date().toISOString();
const nextIteration = { ...this.iteration, status, step_outputs: outputs, goal_evaluation_result: goalMet ? 1 : 0,
cost: delta.cost, tokens_used: delta.tokens, error_message: error, completed_at: completed };
const nextRun = { ...this.record, total_iterations: this.iteration.iteration_number,
total_cost: total.cost, total_tokens_used: total.tokens,
...(terminal ? { ...terminal, completed_at: completed } : {}) };
// ๋งˆ์ง€๋ง‰ ํšŒ์ฐจ์™€ ์ตœ์ข… ์‹คํ–‰ ์ƒํƒœ๊ฐ€ ์ ˆ๋ฐ˜๋งŒ ์ €์žฅ๋˜์ง€ ์•Š๊ฒŒ ๊ฐ™์€ ํŠธ๋žœ์žญ์…˜์œผ๋กœ ๋ฐ˜์˜ํ•œ๋‹ค.
// committed phase๋„ ๊ฐ™์€ transaction์—์„œ ๊ฒŒ์‹œํ•œ๋‹ค. ์‹คํŒจ ์‹œ ๋ฉ”๋ชจ๋ฆฌ ์ง‘๊ณ„ ์œ„์น˜๋„ ์ „์ง„ํ•˜์ง€ ์•Š๋Š”๋‹ค.
// ์„ค๋ช… ๋ฌธ์„œ: docs/specs/batch-step-continuation.md v0.7.0
this.db.runInTransaction(() => { this.db.saveLoopIteration(nextIteration); this.db.saveLoopRun(nextRun); commit?.(); });
this.iteration = nextIteration; this.record = nextRun;
this.accountedCost = total.cost; this.accountedTokens = total.tokens;
return this.record;
}
finish({ aborted, error, outputs }, commit) {
if (this.record.status !== 'running') return null;
const terminal = { status: aborted ? 'aborted' : 'failed', goal_met: 0,
break_reason: aborted ? 'CANCELLED' : 'EXECUTION_FAILED' };
if (this.iteration?.status === 'running') {
return this.completeIteration({ status: terminal.status, outputs, error, terminal }, commit);
}
const total = this.totals();
const next = { ...this.record, ...terminal, total_cost: total.cost, total_tokens_used: total.tokens, completed_at: new Date().toISOString() };
this.db.runInTransaction(() => { this.db.saveLoopRun(next); commit?.(); }); this.record = next; return next;
}
}
module.exports = { BatchLoopLedger };