import { buildLimitConfig, checkLimitsDetailed } from '../../run/LimitChecker.js';
export class GuardCoordinator {
    limitConfig;
    startTime;
    constructor(config) {
        this.limitConfig = buildLimitConfig(config.tokenBudget, config.timeoutMs, config.costLimitUsd, config.maxIterations);
        this.startTime = Date.now();
    }
    beforeIteration(runMgr, abortSignal) {
        const limitState = {
            aborted: abortSignal.aborted,
            totalTokens: runMgr.tokenUsage.totalTokens,
            totalCost: runMgr.costInfo.totalCost,
            currentIteration: runMgr.currentIteration,
            startTime: this.startTime,
        };
        const limitResult = checkLimitsDetailed(this.limitConfig, limitState);
        if (limitResult.type === 'hard_stop') {
            return {
                shouldStop: true,
                forceFinalize: false,
                stopReason: limitResult.reason,
                isCancelled: limitResult.reason === 'cancelled',
            };
        }
        if (limitResult.type === 'warning') {
            return {
                shouldStop: false,
                forceFinalize: true,
                stopReason: limitResult.reason,
                isCancelled: false,
            };
        }
        return {
            shouldStop: false,
            forceFinalize: false,
            isCancelled: false,
        };
    }
}
//# sourceMappingURL=guard.js.map