"use strict";
// Predictive Next-Action Engine.
//
// Mines completed evidence (chat tool sequences, learning candidates, ambient
// signals) for short Markov-style patterns:
//
//   "after the user does X, they usually do Y within N steps"
//
// Emits NextActionSuggestion records that Mission Control can render as
// Accept / Dismiss buttons. Suggestions never execute on their own — they pass
// through the Trust Ladder. A capability at rung 4 (act) may auto-execute,
// everything else surfaces a card.
//
// Pure pattern miner — no LLM calls. Fast, deterministic, explainable.
// LLM-shaped suggestions can be layered on top later by feeding the same
// evidence stream into a small router model.
Object.defineProperty(exports, "__esModule", { value: true });
exports.mineNextActions = mineNextActions;
exports.suggestNextAfter = suggestNextAfter;
const DEFAULT_WINDOW = 3;
const DEFAULT_MIN_SAMPLES = 3;
const DEFAULT_MIN_CONFIDENCE = 0.4;
const DEFAULT_LIMIT = 10;
/**
 * Scan a chronological list of action events and return the top N
 * "after X usually Y" suggestions, sorted by confidence × sampleSize.
 */
function mineNextActions(events, options = {}) {
    const windowSize = options.windowSize ?? DEFAULT_WINDOW;
    const minSamples = options.minSamples ?? DEFAULT_MIN_SAMPLES;
    const minConfidence = options.minConfidence ?? DEFAULT_MIN_CONFIDENCE;
    const limit = options.limit ?? DEFAULT_LIMIT;
    // count[trigger][predicted] = transitions
    const count = new Map();
    const triggerTotal = new Map();
    // capability lookup: predicted key → most recent capability seen
    const capabilityFor = new Map();
    for (let i = 0; i < events.length; i++) {
        const trigger = events[i];
        capabilityFor.set(trigger.key, trigger.capability);
        const seen = new Set();
        for (let j = i + 1; j < Math.min(events.length, i + 1 + windowSize); j++) {
            const next = events[j];
            if (next.key === trigger.key || seen.has(next.key))
                continue;
            seen.add(next.key);
            const inner = count.get(trigger.key) ?? new Map();
            inner.set(next.key, (inner.get(next.key) ?? 0) + 1);
            count.set(trigger.key, inner);
            triggerTotal.set(trigger.key, (triggerTotal.get(trigger.key) ?? 0) + 1);
            capabilityFor.set(next.key, next.capability);
        }
    }
    const suggestions = [];
    for (const [trigger, inner] of count.entries()) {
        const total = triggerTotal.get(trigger) ?? 0;
        for (const [predicted, transitions] of inner.entries()) {
            if (transitions < minSamples)
                continue;
            const confidence = total > 0 ? transitions / total : 0;
            if (confidence < minConfidence)
                continue;
            suggestions.push({
                trigger,
                predicted,
                confidence,
                sampleSize: transitions,
                capability: capabilityFor.get(predicted),
                rationale: `Observed ${transitions}/${total} times within ${windowSize} steps after \`${trigger}\``,
            });
        }
    }
    suggestions.sort((a, b) => b.confidence * b.sampleSize - a.confidence * a.sampleSize);
    return suggestions.slice(0, limit);
}
/**
 * Convenience: given the latest action and a precomputed suggestion set,
 * return the highest-confidence next action keyed off it. Used by Mission
 * Control to surface a card right after a tool call completes.
 */
function suggestNextAfter(latestKey, suggestions) {
    return suggestions.find((s) => s.trigger === latestKey);
}
//# sourceMappingURL=predictiveEngine.js.map