// tools/reconcile-rules.ts — MCP tool: reconcile_rules (SPEC-189)
// Compares CLAUDE.md conventions against .claude/rules/ files and auto-patches drift.
import { z } from 'zod';
import { reconcileRules } from '../engine/rules-reconciler.js';
import { safeTracked } from './safe-handler.js';
// ---------------------------------------------------------------------------
// Zod schema
// ---------------------------------------------------------------------------
const reconcileRulesSchema = {
    projectPath: z
        .string()
        .max(4096)
        .describe('Absolute path to the project root containing CLAUDE.md and .claude/rules/'),
};
// ---------------------------------------------------------------------------
// Formatter
// ---------------------------------------------------------------------------
function formatReconciliation(result) {
    const { rulesInSync, newRulesNeeded, staleRules, updatedRules, healthScore } = result;
    const lines = [];
    lines.push('# Rules Reconciliation Report');
    lines.push('');
    lines.push(`## Health Score: ${healthScore}/100`);
    lines.push('');
    lines.push(`- Status: **${rulesInSync ? 'IN SYNC' : 'DRIFT DETECTED'}**`);
    lines.push('');
    if (updatedRules.length > 0) {
        lines.push('## Auto-Generated Rules Files');
        lines.push('');
        lines.push('The following files were created in `.claude/rules/` for uncovered convention categories:');
        lines.push('');
        for (const f of updatedRules) {
            lines.push(`  - ${f}`);
        }
        lines.push('');
    }
    if (newRulesNeeded.length > 0 && updatedRules.length === 0) {
        lines.push('## Missing Coverage');
        lines.push('');
        lines.push('Convention categories in CLAUDE.md with no rules file coverage:');
        lines.push('');
        for (const cat of newRulesNeeded) {
            lines.push(`  - ${cat}`);
        }
        lines.push('');
    }
    if (staleRules.length > 0) {
        lines.push('## Potentially Stale Rules');
        lines.push('');
        lines.push('These files have no categories matching current CLAUDE.md conventions — review manually:');
        lines.push('');
        for (const f of staleRules) {
            lines.push(`  - ${f}`);
        }
        lines.push('');
    }
    if (rulesInSync && updatedRules.length === 0) {
        lines.push('All convention categories are covered by .claude/rules/ files. No drift detected.');
        lines.push('');
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export function handleReconcileRules(args) {
    const result = reconcileRules(args.projectPath);
    const text = formatReconciliation(result);
    const isError = result.healthScore === 0 && !result.rulesInSync;
    return { content: [{ type: 'text', text }], isError };
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
export function registerReconcileRulesTool(server) {
    server.registerTool('reconcile_rules', {
        description: 'Compare CLAUDE.md conventions against existing .claude/rules/ files and auto-patch drift. Generates new rules files for convention categories not yet covered. Returns a health score (0-100) and list of stale or missing rules.',
        inputSchema: reconcileRulesSchema,
        annotations: {
            readOnlyHint: false,
            destructiveHint: false,
            title: 'Reconcile Rules',
        },
    }, safeTracked('reconcile_rules', (args) => Promise.resolve(handleReconcileRules(args))));
}
//# sourceMappingURL=reconcile-rules.js.map