/**
 * Nexus Prime — File Priors
 *
 * Computes execution advantage from file history:
 *   - frequently successful files for similar tasks
 *   - common co-change sets
 *   - retry hotspots
 */
/**
 * Compute file priors from historical run data.
 */
export function computeFilePriors(history) {
    const fileStats = new Map();
    for (const entry of history) {
        let stats = fileStats.get(entry.filePath);
        if (!stats) {
            stats = { successes: 0, verified: 0, total: 0, coChangeCounts: new Map(), retries: 0 };
            fileStats.set(entry.filePath, stats);
        }
        stats.total++;
        if (entry.wasSuccessful) {
            stats.successes++;
            stats.lastSuccessRunId = entry.runId;
        }
        if (entry.wasVerified) {
            stats.verified++;
            stats.lastVerifiedRunId = entry.runId;
        }
        if (entry.wasRetry)
            stats.retries++;
        for (const co of entry.coChangedWith) {
            stats.coChangeCounts.set(co, (stats.coChangeCounts.get(co) ?? 0) + 1);
        }
    }
    const priors = [];
    for (const [filePath, stats] of fileStats) {
        const coChangeFiles = [...stats.coChangeCounts.entries()]
            .sort((a, b) => b[1] - a[1])
            .slice(0, 5)
            .map(([f]) => f);
        priors.push({
            filePath,
            successRate: stats.total > 0 ? stats.successes / stats.total : 0,
            verifiedRate: stats.total > 0 ? stats.verified / stats.total : 0,
            coChangeFiles,
            retryCount: stats.retries,
            lastSuccessfulRunId: stats.lastSuccessRunId,
            lastVerifiedRunId: stats.lastVerifiedRunId,
        });
    }
    return priors.sort((a, b) => (b.successRate + b.verifiedRate * 0.75) - (a.successRate + a.verifiedRate * 0.75)
        || a.retryCount - b.retryCount);
}
