File size: 3,583 Bytes
d739b1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** ๋ฐฐ์น ๋ฃจํ์ ํ์ฐจ ์ฌ์ฉ๋๊ณผ ์ข
๋ฃ ๊ธฐ๋ก. 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 };
|