/**
 * `olam flywheel diversity-check` — cross-persona Jaccard similarity CLI.
 *
 * Reads persona-prompt files from a glob; computes pairwise Jaccard; reports
 * pairs exceeding the threshold (default 0.7). Used by /10x:learn write-path
 * to detect silent convergence collapse across K1 personas.
 *
 * Exit codes:
 *   0 = no convergence (all pairs below threshold)
 *   1 = convergence detected (one or more pairs above threshold)
 *   2 = unexpected error (no matching files / read failure)
 *
 * Plan reference: docs/plans/olam-flywheel-cli-namespace/phase-a-tasks.md § A8
 */
import { readFileSync } from 'node:fs';
import { basename } from 'node:path';
import { globSync } from 'node:fs';
import { crossPersonaCheck, formatRedivergencePrompt, DIVERSITY_THRESHOLD, } from '@olam/core/src/lib/diversity-check.js';
export function registerFlywheelDiversityCheck(parent) {
    parent
        .command('diversity-check')
        .description('Cross-persona Jaccard similarity guard — flag persona pairs above threshold')
        .requiredOption('--glob <pattern>', 'glob pattern matching persona-prompt files')
        .option('--threshold <0.0-1.0>', 'Jaccard threshold (default 0.7)')
        .option('--json', 'emit findings as JSON instead of human-readable')
        .action((opts) => {
        const threshold = opts.threshold !== undefined ? Number.parseFloat(opts.threshold) : DIVERSITY_THRESHOLD;
        if (!Number.isFinite(threshold) || threshold < 0 || threshold > 1) {
            process.stderr.write(`[diversity-check-reject] --threshold must be 0.0-1.0; got ${JSON.stringify(opts.threshold)}\n`);
            process.exit(2);
        }
        // Use Node 22+'s native glob (no extra dep).
        const files = globSync(opts.glob);
        if (files.length === 0) {
            process.stderr.write(`[diversity-check-reject] no files matched glob: ${opts.glob}\n`);
            process.exit(2);
        }
        const personas = new Map();
        for (const filepath of files) {
            try {
                const body = readFileSync(filepath, 'utf8');
                if (body.trim().length > 0) {
                    personas.set(basename(filepath, '.md'), body);
                }
            }
            catch (err) {
                process.stderr.write(`[diversity-check-error] cannot read ${filepath}: ${err instanceof Error ? err.message : 'unknown'}\n`);
                process.exit(2);
            }
        }
        const flagged = crossPersonaCheck(personas, threshold);
        if (opts.json) {
            process.stdout.write(JSON.stringify({ threshold, persona_count: personas.size, flagged }) + '\n');
        }
        else {
            process.stdout.write(`[diversity-check] ${personas.size} personas; threshold ${threshold}; ${flagged.length} pair(s) over threshold\n`);
            for (const { persona_a, persona_b, score } of flagged) {
                process.stdout.write(`\n${formatRedivergencePrompt(persona_a, persona_b, score, undefined, undefined, threshold)}\n`);
            }
        }
        process.exit(flagged.length > 0 ? 1 : 0);
    });
}
//# sourceMappingURL=diversity-check.js.map