"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.predictViolations = predictViolations;
exports.getMostLikelyViolation = getMostLikelyViolation;
function predictViolations(runState, config) {
    const predictions = [];
    const now = Date.now();
    if (!runState.startedAt || runState.status !== 'running') {
        return predictions;
    }
    const elapsed = (now - runState.startedAt) / 1000;
    if (elapsed > 0) {
        const durationMs = now - runState.startedAt;
        const timeRemaining = config.runMaxDurationMs - durationMs;
        if (timeRemaining < config.runMaxDurationMs * 0.1) {
            predictions.push({
                willViolate: timeRemaining <= 0,
                policy: 'timeout',
                estimatedTime: Math.max(0, timeRemaining / 1000),
                confidence: timeRemaining < 0 ? 1.0 : 0.8,
                currentValue: durationMs,
                threshold: config.runMaxDurationMs,
                rate: durationMs / elapsed
            });
        }
    }
    if (runState.tokenCount > 0 && elapsed > 0) {
        const tokenRate = runState.tokenCount / elapsed;
        const tokensRemaining = config.runMaxTokens - runState.tokenCount;
        const timeToLimit = tokensRemaining / tokenRate;
        if (tokensRemaining < config.runMaxTokens * 0.2) {
            predictions.push({
                willViolate: tokensRemaining <= 0,
                policy: 'token_cap',
                estimatedTime: Math.max(0, timeToLimit),
                confidence: tokensRemaining < 0 ? 1.0 : Math.min(0.9, 1 - (tokensRemaining / config.runMaxTokens)),
                currentValue: runState.tokenCount,
                threshold: config.runMaxTokens,
                rate: tokenRate
            });
        }
    }
    if (runState.lastTokenAt) {
        const idleTime = (now - runState.lastTokenAt) / 1000;
        const idleRemaining = (config.runIdleMs / 1000) - idleTime;
        if (idleRemaining < (config.runIdleMs / 1000) * 0.3) {
            predictions.push({
                willViolate: idleRemaining <= 0,
                policy: 'idle',
                estimatedTime: Math.max(0, idleRemaining),
                confidence: idleRemaining < 0 ? 1.0 : 0.7,
                currentValue: idleTime * 1000,
                threshold: config.runIdleMs,
                rate: 1000
            });
        }
    }
    else if (runState.startedAt) {
        const timeSinceStart = (now - runState.startedAt) / 1000;
        const idleRemaining = (config.runIdleMs / 1000) - timeSinceStart;
        if (idleRemaining < (config.runIdleMs / 1000) * 0.3) {
            predictions.push({
                willViolate: idleRemaining <= 0,
                policy: 'idle',
                estimatedTime: Math.max(0, idleRemaining),
                confidence: idleRemaining < 0 ? 1.0 : 0.6,
                currentValue: timeSinceStart * 1000,
                threshold: config.runIdleMs,
                rate: 1000
            });
        }
    }
    if (runState.cost && runState.cost > 0 && elapsed > 0) {
        const costRate = runState.cost / elapsed;
        const maxCost = config.runMaxCost || 1.0;
        const costRemaining = maxCost - runState.cost;
        const timeToLimit = costRemaining / costRate;
        if (costRemaining < maxCost * 0.2) {
            predictions.push({
                willViolate: costRemaining <= 0,
                policy: 'cost',
                estimatedTime: Math.max(0, timeToLimit),
                confidence: costRemaining < 0 ? 1.0 : Math.min(0.9, 1 - (costRemaining / maxCost)),
                currentValue: runState.cost,
                threshold: maxCost,
                rate: costRate
            });
        }
    }
    return predictions.sort((a, b) => a.estimatedTime - b.estimatedTime);
}
function getMostLikelyViolation(runState, config) {
    const predictions = predictViolations(runState, config);
    if (predictions.length === 0) {
        return null;
    }
    predictions.sort((a, b) => {
        if (b.confidence !== a.confidence) {
            return b.confidence - a.confidence;
        }
        return a.estimatedTime - b.estimatedTime;
    });
    return predictions[0] || null;
}
//# sourceMappingURL=predictive.js.map