// engine/self-healing/healer.ts — SPEC-474: Self-Healing Loop
import { applyTypescriptFix } from './strategies/typescript-fix.js';
import { applyLintFix } from './strategies/lint-fix.js';
import { applyTestFix } from './strategies/test-fix.js';
import { applyCoverageFix } from './strategies/coverage-fix.js';
const MAX_ATTEMPTS = 3;
/**
 * Maps a failure type to the appropriate fix strategy name.
 */
export function identifyFixStrategy(failureType) {
    switch (failureType) {
        case 'typecheck':
            return 'typescript';
        case 'lint':
            return 'lint';
        case 'test':
            return 'tests';
        case 'coverage':
            return 'coverage';
        default:
            return 'typescript';
    }
}
/**
 * Dispatches to the appropriate strategy based on strategy name.
 */
function runStrategy(strategyName, projectPath, details) {
    switch (strategyName) {
        case 'typescript':
            return applyTypescriptFix(projectPath, details);
        case 'lint':
            return applyLintFix(projectPath, details);
        case 'tests':
            return applyTestFix(projectPath, details);
        case 'coverage':
            return applyCoverageFix(projectPath, details);
    }
}
/**
 * Runs the self-healing loop for a spec with a given failure context.
 * Tries the primary strategy up to MAX_ATTEMPTS times.
 * Returns a full HealingResult describing what was fixed or still failing.
 */
export async function runHealingLoop(specId, projectPath, failureContext) {
    const strategy = identifyFixStrategy(failureContext.type);
    const attempts = [];
    const fixedIssues = [];
    const remainingIssues = [];
    let healed = false;
    for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
        const result = await runStrategy(strategy, projectPath, failureContext.details);
        if (result.fixed) {
            attempts.push({
                strategy,
                attempt,
                status: 'ok',
                details: result.reason,
            });
            if (result.fixedIssues !== undefined) {
                fixedIssues.push(...result.fixedIssues);
            }
            healed = true;
            break;
        }
        else {
            attempts.push({
                strategy,
                attempt,
                status: 'fail',
                details: result.reason,
            });
            remainingIssues.push(`Attempt ${attempt}: ${result.reason}`);
        }
    }
    // Determine final status
    let finalStatus;
    if (healed) {
        finalStatus = 'healed';
    }
    else if (fixedIssues.length > 0) {
        finalStatus = 'partial';
    }
    else {
        finalStatus = 'still_failing';
    }
    if (!healed) {
        remainingIssues.push(failureContext.details);
    }
    return {
        specId,
        initialFailure: failureContext.details,
        attempts,
        finalStatus,
        fixedIssues,
        remainingIssues,
    };
}
//# sourceMappingURL=healer.js.map