// tools/validation-loop-handler.ts — SPEC-369: Test Automation Loop
import { computeSpecHealth, buildValidationPlan, loadSpecContent, } from '../engine/validation-loop.js';
import { computeHealthScore } from '../engine/spec-health-scorer.js';
import { specStore } from '../storage/index.js';
import { hashProjectPath } from '../storage/base-store.js';
export async function handleSpecHealthCheck(input) {
    const projectId = hashProjectPath(input.projectPath);
    const spec = await specStore.getSpec(projectId, input.specId);
    const score = await computeSpecHealth(input.projectPath, spec ?? undefined);
    const breakdownLines = Object.entries(score.breakdown).map(([key, pts]) => {
        const detail = score.details[key] ?? '';
        return `  ${key}: ${pts}/20 — ${detail}`;
    });
    const lines = [
        `Health check for ${score.specId}: ${score.total}/100`,
        ``,
        `Breakdown:`,
        ...breakdownLines,
    ];
    // SPEC-571: include real-time health score with optional delta
    if (spec !== null) {
        const { criteria } = await loadSpecContent(spec);
        const healthResult = computeHealthScore(spec, criteria.length > 0 ? criteria : undefined);
        const storedScore = spec.healthScore;
        lines.push('');
        lines.push(`Real-time health score: ${healthResult.score}/100 (${healthResult.grade})`);
        if (input.since !== undefined && storedScore !== undefined) {
            const delta = healthResult.score - storedScore;
            const sign = delta >= 0 ? '+' : '';
            lines.push(`Score delta since ${input.since}: ${sign}${delta} pts (previous: ${storedScore})`);
        }
    }
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
import { isNativeActive, fastScanSpecs } from '../engine/core-bridge.js';
export async function handleHealthCheckAll(input) {
    if (isNativeActive()) {
        const briefs = fastScanSpecs(input.projectPath);
        if (briefs) {
            // Sort worst to best
            briefs.sort((a, b) => a.healthScore - b.healthScore);
            const lines = [
                `Health check for all ${briefs.length} specs (sorted worst to best) [NATIVE]:`,
                ``,
                ...briefs.map((b) => `  ${b.id}: ${b.healthScore}/100`),
            ];
            return {
                content: [{ type: 'text', text: lines.join('\n') }],
            };
        }
    }
    const projectId = hashProjectPath(input.projectPath);
    const specs = await specStore.listSpecs(projectId);
    if (specs.length === 0) {
        return {
            content: [{ type: 'text', text: 'No specs found in this project.' }],
        };
    }
    const scores = await Promise.all(specs.map((s) => computeSpecHealth(input.projectPath, s)));
    // Sort worst to best
    scores.sort((a, b) => a.total - b.total);
    const lines = [
        `Health check for all ${scores.length} specs (sorted worst to best):`,
        ``,
        ...scores.map((s) => `  ${s.specId}: ${s.total}/100`),
    ];
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
export function handleRunValidationLoop(input) {
    const maxIterations = input.maxIterations ?? 3;
    const testCommand = input.testCommand ?? 'pnpm test';
    const autoFix = input.autoFix ?? false;
    const result = buildValidationPlan(input.specIds, maxIterations, testCommand, autoFix);
    const planLines = result.plan.flatMap((p) => [
        `Spec: ${p.specId}`,
        ...p.steps.map((s) => `  → ${s}`),
        `  Expected: ${p.expectedOutcome}`,
        ``,
    ]);
    const lines = [result.summary, ``, `Execution plan:`, ``, ...planLines];
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=validation-loop-handler.js.map