UNPKG

gepa-spo

Version:

Genetic-Pareto prompt optimizer to evolve system prompts from a few rollouts with modular support and intelligent crossover

52 lines (51 loc) 1.86 kB
/** * Lightweight budget tracker to enforce and introspect token/call budgets. * * When disabled, all methods are no-ops and `remaining()` always returns the * initial start value provided to the constructor. This allows safe wiring * into existing call sites without changing behavior. */ export class BudgetTracker { start; enabled; remainingInternal; /** * Create a new tracker. * * @param start - Initial remaining budget (non-negative integer) * @param enabled - Whether tracking is active. If false, all operations are no-ops. */ constructor(start, enabled) { const safeStart = Number.isFinite(start) && start >= 0 ? Math.floor(start) : 0; this.start = safeStart; this.enabled = Boolean(enabled); this.remainingInternal = safeStart; } /** Current remaining budget. When disabled, returns the initial `start` value. */ remaining() { return this.enabled ? this.remainingInternal : this.start; } /** * Decrease the budget by `n` (defaults to 1). Negative values are ignored. * When disabled, this is a no-op. * * @param n - Amount to decrement (defaults to 1) * @param _tag - Optional tag for future attribution (ignored in noop scaffold) */ dec(n = 1, _tag) { if (!this.enabled) return; const amt = Number.isFinite(n) && n > 0 ? Math.floor(n) : 0; if (amt === 0) return; this.remainingInternal = Math.max(0, this.remainingInternal - amt); } /** Whether there is at least `n` budget remaining. Always true when disabled. */ canAfford(n) { if (!this.enabled) return true; const amt = Number.isFinite(n) && n > 0 ? Math.floor(n) : 0; return this.remainingInternal >= amt; } } export default BudgetTracker;