/**
 * BudgetManager - Tracks token usage, turn count, and wall-clock time
 * to enforce resource limits on the agent RunLoop.
 *
 * Note: Budget is a soft limit — the last LLM response may push usage
 * over the configured maximum by up to one response size. The check runs
 * before each LLM call, not after.
 */
export class BudgetManager {
    totalInputTokens = 0;
    totalOutputTokens = 0;
    turnCount = 0;
    startTime;
    config;
    constructor(config) {
        this.config = config;
        this.startTime = Date.now();
    }
    recordUsage(inputTokens, outputTokens) {
        this.totalInputTokens += inputTokens;
        this.totalOutputTokens += outputTokens;
        this.turnCount++;
    }
    isExhausted() {
        if (this.config.maxTokens !== undefined &&
            this.totalTokens >= this.config.maxTokens) {
            return true;
        }
        if (this.config.maxTurns !== undefined &&
            this.turnCount >= this.config.maxTurns) {
            return true;
        }
        if (this.config.maxWallClockMs !== undefined &&
            this.elapsedMs >= this.config.maxWallClockMs) {
            return true;
        }
        return false;
    }
    get totalTokens() {
        return this.totalInputTokens + this.totalOutputTokens;
    }
    get elapsedMs() {
        return Date.now() - this.startTime;
    }
    get turns() {
        return this.turnCount;
    }
    getStats() {
        return {
            totalInputTokens: this.totalInputTokens,
            totalOutputTokens: this.totalOutputTokens,
            totalTokens: this.totalTokens,
            turns: this.turnCount,
            elapsedMs: this.elapsedMs,
        };
    }
}
