/**
 * lifecycle.ts
 *
 * Compaction lifecycle state machine for the compaction engine.
 *
 * Provides:
 * - Valid transition map for the 9-state machine
 * - Pure transition helpers (no side-effects)
 * - Strategy selection logic based on trigger and token pressure
 */
// ---------------------------------------------------------------------------
// Transition map
// ---------------------------------------------------------------------------
/**
 * Valid state transitions for the compaction lifecycle machine.
 *
 * Key   = current state
 * Value = set of states reachable from current
 */
const VALID_TRANSITIONS = new Map([
    ['idle', new Set(['checking_threshold'])],
    ['checking_threshold', new Set(['microcompact', 'collapse', 'autocompact', 'reactive_compact', 'done', 'failed'])],
    ['microcompact', new Set(['boundary_commit', 'failed'])],
    ['collapse', new Set(['boundary_commit', 'failed'])],
    ['autocompact', new Set(['boundary_commit', 'failed'])],
    ['reactive_compact', new Set(['boundary_commit', 'failed'])],
    ['boundary_commit', new Set(['done', 'failed'])],
    ['done', new Set(['idle'])],
    ['failed', new Set(['idle'])],
]);
// ---------------------------------------------------------------------------
// Transition helpers
// ---------------------------------------------------------------------------
/**
 * Returns whether a transition from `from` to `to` is valid.
 */
export function canTransition(from, to) {
    return VALID_TRANSITIONS.get(from)?.has(to) ?? false;
}
/**
 * Returns all states reachable from `from`.
 */
export function reachableFrom(from) {
    return VALID_TRANSITIONS.get(from) ?? new Set();
}
/**
 * Validates and applies a state transition.
 *
 * @returns `{ ok: true, state }` on success, `{ ok: false, reason }` on rejection.
 */
export function applyTransition(current, target) {
    if (canTransition(current, target)) {
        return { ok: true, state: target };
    }
    return {
        ok: false,
        reason: `Invalid compaction transition: ${current} → ${target}`,
    };
}
// ---------------------------------------------------------------------------
// Terminal / operational helpers
// ---------------------------------------------------------------------------
/** Returns true if the state is a terminal completion state (done | failed). */
export function isTerminal(state) {
    return state === 'done' || state === 'failed';
}
/** Returns true if the state represents active compaction work in progress. */
export function isCompacting(state) {
    return (state === 'microcompact' ||
        state === 'collapse' ||
        state === 'autocompact' ||
        state === 'reactive_compact' ||
        state === 'boundary_commit');
}
/**
 * Selects the appropriate compaction strategy based on trigger and token pressure.
 *
 * Selection priority:
 * 1. `reactive`     — any prompt-too-long error (emergency, must shrink now)
 * 2. `microcompact` — token pressure < 50% of context window (light touch)
 * 3. `autocompact`  — token pressure 50–85% (standard auto-compaction)
 * 4. `collapse`     — token pressure > 85% or manual trigger (aggressive shrink)
 */
export function selectStrategy(params) {
    const { trigger, currentTokens, contextWindow, isPromptTooLong } = params;
    if (isPromptTooLong === true || trigger === 'prompt_too_long') {
        return 'reactive';
    }
    const pressure = currentTokens / contextWindow;
    if (pressure < 0.5) {
        return 'microcompact';
    }
    if (pressure < 0.85) {
        return 'autocompact';
    }
    // High pressure or manual trigger — aggressive collapse
    return 'collapse';
}
/**
 * Maps a chosen CompactionStrategy to its corresponding lifecycle state.
 */
export function strategyToState(strategy) {
    switch (strategy) {
        case 'microcompact': return 'microcompact';
        case 'collapse': return 'collapse';
        case 'autocompact': return 'autocompact';
        case 'reactive': return 'reactive_compact';
        default: {
            const _exhaustive = strategy;
            throw new Error(`Unknown compaction strategy: ${_exhaustive}`);
        }
    }
}
