// engine/compact/threshold-evaluator.ts — SPEC-922: Evaluate compact mode thresholds
const DEFAULT_THRESHOLDS = {
    enterAt: 0.7,
    exitAt: 0.5,
    compactBudgetTokens: 200,
    verboseBudgetTokens: 500,
    schemaVersion: 1,
};
/** Load thresholds from registry with defaults as fallback. */
export function loadThresholds(registry) {
    const t = { ...DEFAULT_THRESHOLDS, ...registry };
    // Clamp to [0,1]
    t.enterAt = Math.max(0, Math.min(1, t.enterAt));
    t.exitAt = Math.max(0, Math.min(1, t.exitAt));
    // Enforce enterAt > exitAt (swap + warn)
    if (t.enterAt <= t.exitAt) {
        const tmp = t.enterAt;
        t.enterAt = t.exitAt;
        t.exitAt = tmp;
        console.warn(`[Planu] Compact thresholds invalid: enterAt (${t.exitAt}) <= exitAt (${t.enterAt}). Swapped.`);
    }
    // Ensure positive budgets
    t.compactBudgetTokens = Math.max(50, t.compactBudgetTokens);
    t.verboseBudgetTokens = Math.max(50, t.verboseBudgetTokens);
    return t;
}
/** Evaluate compact mode based on context usage, previous mode, and thresholds. */
export function evaluateCompactMode(contextUsed, prevMode, thresholds) {
    // No header → fail-open to verbose
    if (contextUsed === undefined) {
        return {
            mode: 'verbose',
            tokenBudget: thresholds.verboseBudgetTokens,
            reason: 'no_header',
        };
    }
    const clamped = Math.max(0, Math.min(1, contextUsed));
    // Above enter threshold → compact
    if (clamped >= thresholds.enterAt) {
        return {
            mode: 'compact',
            tokenBudget: thresholds.compactBudgetTokens,
            contextUsed: clamped,
            reason: 'header_above_enter',
        };
    }
    // Below exit threshold → verbose
    if (clamped < thresholds.exitAt) {
        return {
            mode: 'verbose',
            tokenBudget: thresholds.verboseBudgetTokens,
            contextUsed: clamped,
            reason: 'header_below_exit',
        };
    }
    // Between exit and enter → hysteresis: keep current mode
    const mode = prevMode ?? 'verbose';
    return {
        mode,
        tokenBudget: mode === 'compact' ? thresholds.compactBudgetTokens : thresholds.verboseBudgetTokens,
        contextUsed: clamped,
        reason: 'hysteresis_keep',
    };
}
//# sourceMappingURL=threshold-evaluator.js.map