"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KERNEL_DRIVER_DEFAULT_CAUTION_REMEDIATION_CADENCE_GENERATIONS = exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS = void 0;
exports.getContextHealthRemediationLevel = getContextHealthRemediationLevel;
exports.resetContextHealthRoundState = resetContextHealthRoundState;
exports.resolveCriticalCountdownRemaining = resolveCriticalCountdownRemaining;
exports.consumeCriticalCountdown = consumeCriticalCountdown;
exports.resolveCautionRemediationCadenceGenerations = resolveCautionRemediationCadenceGenerations;
exports.decideKernelDriverContextHealth = decideKernelDriverContextHealth;
exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS = 5;
exports.KERNEL_DRIVER_DEFAULT_CAUTION_REMEDIATION_CADENCE_GENERATIONS = 10;
const stateByDialogKey = new Map();
function getContextHealthRemediationLevel(snapshot) {
    if (snapshot?.kind !== 'available') {
        return undefined;
    }
    return snapshot.level === 'caution' || snapshot.level === 'critical' ? snapshot.level : undefined;
}
function getOrCreateState(dialogKey) {
    const existing = stateByDialogKey.get(dialogKey);
    if (existing)
        return existing;
    const created = {};
    stateByDialogKey.set(dialogKey, created);
    return created;
}
function resetContextHealthRoundState(dialogKey) {
    stateByDialogKey.delete(dialogKey);
}
function resolveCriticalCountdownRemaining(dialogKey, snapshot) {
    if (!snapshot || snapshot.kind !== 'available') {
        resetContextHealthRoundState(dialogKey);
        return exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS;
    }
    if (snapshot.level !== 'critical') {
        if (snapshot.level === 'healthy') {
            resetContextHealthRoundState(dialogKey);
            return exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS;
        }
        const state = getOrCreateState(dialogKey);
        state.criticalCountdownRemaining = undefined;
        return exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS;
    }
    const state = getOrCreateState(dialogKey);
    if (state.lastSeenLevel !== 'critical' ||
        typeof state.criticalCountdownRemaining !== 'number' ||
        !Number.isFinite(state.criticalCountdownRemaining)) {
        state.lastSeenLevel = 'critical';
        state.criticalCountdownRemaining = exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS;
    }
    const remaining = Math.floor(state.criticalCountdownRemaining);
    return remaining > 0 ? remaining : 0;
}
function consumeCriticalCountdown(dialogKey) {
    const state = getOrCreateState(dialogKey);
    const currentRaw = typeof state.criticalCountdownRemaining === 'number' &&
        Number.isFinite(state.criticalCountdownRemaining)
        ? Math.floor(state.criticalCountdownRemaining)
        : exports.KERNEL_DRIVER_DEFAULT_CRITICAL_COUNTDOWN_GENERATIONS;
    const current = currentRaw > 0 ? currentRaw : 0;
    const next = Math.max(0, current - 1);
    state.lastSeenLevel = 'critical';
    state.criticalCountdownRemaining = next;
    return next;
}
function resolveCautionRemediationCadenceGenerations(configured) {
    if (typeof configured !== 'number' || !Number.isFinite(configured)) {
        return exports.KERNEL_DRIVER_DEFAULT_CAUTION_REMEDIATION_CADENCE_GENERATIONS;
    }
    const normalized = Math.floor(configured);
    if (normalized <= 0) {
        return exports.KERNEL_DRIVER_DEFAULT_CAUTION_REMEDIATION_CADENCE_GENERATIONS;
    }
    return normalized;
}
function decideKernelDriverContextHealth(args) {
    const { snapshot, dialogKey } = args;
    if (!snapshot || snapshot.kind !== 'available') {
        return { kind: 'proceed' };
    }
    if (snapshot.level === 'healthy') {
        return { kind: 'proceed' };
    }
    if (snapshot.level === 'caution') {
        const state = getOrCreateState(dialogKey);
        const cadence = resolveCautionRemediationCadenceGenerations(args.cautionRemediationCadenceGenerations);
        const enteringCaution = state.lastSeenLevel !== 'caution';
        state.lastSeenLevel = 'caution';
        state.criticalCountdownRemaining = undefined;
        if (enteringCaution) {
            state.cautionPromptDue = true;
            state.cautionGenerationsSincePrompt = 0;
        }
        else if (state.cautionPromptDue !== true) {
            const previous = typeof state.cautionGenerationsSincePrompt === 'number' &&
                Number.isFinite(state.cautionGenerationsSincePrompt)
                ? Math.max(0, Math.floor(state.cautionGenerationsSincePrompt))
                : 0;
            const next = previous + 1;
            state.cautionGenerationsSincePrompt = next;
            if (next >= cadence) {
                state.cautionPromptDue = true;
            }
        }
        const shouldInjectPrompt = state.cautionPromptDue === true && !args.hadUserPromptThisGen && args.canInjectPromptThisGen;
        if (!shouldInjectPrompt) {
            return { kind: 'proceed' };
        }
        state.cautionPromptDue = false;
        state.cautionGenerationsSincePrompt = 0;
        return { kind: 'continue', reason: 'caution_soft_remediation' };
    }
    if (snapshot.level === 'critical') {
        const state = getOrCreateState(dialogKey);
        state.lastSeenLevel = 'critical';
        state.cautionPromptDue = undefined;
        state.cautionGenerationsSincePrompt = undefined;
        if (args.criticalCountdownRemaining <= 0) {
            return { kind: 'continue', reason: 'critical_force_new_course' };
        }
        if (args.userPromptCriticalRemediationAlreadyApplied === true) {
            return { kind: 'proceed' };
        }
        return args.hadUserPromptThisGen || args.hadUserPromptInImmediateToolChain === true
            ? { kind: 'continue', reason: 'critical_user_prompt_remediation' }
            : { kind: 'continue', reason: 'critical_countdown_remediation' };
    }
    const _exhaustive = snapshot.level;
    return _exhaustive;
}
