// tools/audit-specs-drift.ts — SPEC-744: audit_specs_drift tool handler
import { z } from 'zod';
import { safeTracked } from './safe-handler.js';
import { auditSpecsDrift } from '../engine/spec-audit/index.js';
export function registerAuditSpecsDriftTool(server) {
    server.registerTool('audit_specs_drift', {
        description: 'Run a two-tier drift audit over all done specs. ' +
            'Tier-1: deterministic checks (missing files, broken refs). ' +
            'Tier-2: LLM-based semantic drift for ambiguous cases (budget-capped). ' +
            'Produces a prioritised P0/P1/P2 markdown report in external Planu project data ' +
            'and appends a drift_review entry to external pending state. ' +
            'Specs superseded by newer specs (per SPEC-746 graph) are excluded.',
        inputSchema: {
            projectPath: z.string().min(1).max(4096).describe('Absolute path to project root.'),
            budget: z
                .number()
                .min(0)
                .max(100)
                .optional()
                .describe('Max USD to spend on tier-2 LLM calls (default: 20).'),
            model: z
                .enum(['haiku', 'sonnet'])
                .optional()
                .describe('LLM model for tier-2 (default: haiku).'),
        },
    }, safeTracked('audit_specs_drift', async (args) => {
        const { projectPath, budget, model } = args;
        const report = await auditSpecsDrift({ projectRoot: projectPath, budget, model });
        const p0 = report.findings.filter((f) => f.priority === 'P0').length;
        const p1 = report.findings.filter((f) => f.priority === 'P1').length;
        const p2 = report.findings.filter((f) => f.priority === 'P2').length;
        return {
            content: [
                {
                    type: 'text',
                    text: [
                        `# Spec Drift Audit Complete`,
                        ``,
                        `**Specs scanned**: ${report.scanned} / ${report.totalSpecs}`,
                        `**Skipped (superseded)**: ${report.skippedSuperseded.length}`,
                        ``,
                        `| Priority | Count |`,
                        `|----------|-------|`,
                        `| P0 — broken refs | ${p0} |`,
                        `| P1 — signature drift | ${p1} |`,
                        `| P2 — doc drift | ${p2} |`,
                        ``,
                        `**Budget used**: $${report.budgetUsedUsd.toFixed(4)}`,
                        `**Report**: \`${report.reportPath}\``,
                        ``,
                        p0 + p1 + p2 === 0
                            ? '✓ No drift detected.'
                            : `⚠ ${p0 + p1 + p2} total issues found. Review external Planu pending state for the drift_review entry.`,
                    ].join('\n'),
                },
            ],
        };
    }));
}
//# sourceMappingURL=audit-specs-drift.js.map