/**
 * Regression Analysis Service
 *
 * Systematically analyzes PostProcessor operations to detect regressions,
 * AI code removal attempts, and other critical issues.
 * Triggers multi-agent conferences when analysis is required.
 *
 * @version 1.0.0
 * @since 2026-03-08
 */
import { getKernel } from '../../core/kernel-patterns.js';
import { createAgentDelegator } from '../../delegation/agent-delegator.js';
import { frameworkLogger } from '../../core/framework-logger.js';
import { StringRayStateManager } from '../../state/state-manager.js';
import { strRayConfigLoader } from '../../core/config-loader.js';
/**
 * Main service for systematic regression analysis
 */
export class RegressionAnalysisService {
    kernel = getKernel();
    /**
     * Determine if regression analysis is required for this context
     */
    async shouldAnalyze(context) {
        // Check kernel patterns first
        const kernelAnalysis = this.kernel.analyze(JSON.stringify(context));
        // Check for fatal assumptions that require analysis
        const fatalAssumptions = kernelAnalysis.fatalAssumptions;
        if (fatalAssumptions && fatalAssumptions.length > 0 && fatalAssumptions[0]) {
            return {
                required: true,
                reason: `Fatal assumption detected: ${fatalAssumptions[0].reason}`,
                agents: ['bug-triage-specialist', 'code-analyzer', 'code-reviewer'],
                depth: 'comprehensive',
                confidence: 0.95
            };
        }
        // Check for cascade patterns that require analysis
        const cascadePatterns = kernelAnalysis.cascadePatterns;
        if (cascadePatterns && cascadePatterns.length > 0 && cascadePatterns[0]) {
            return {
                required: true,
                reason: `Cascade pattern detected: ${cascadePatterns[0].pattern}`,
                agents: ['bug-triage-specialist', 'code-analyzer', 'code-reviewer'],
                depth: 'comprehensive',
                confidence: 0.9
            };
        }
        // Check for code removal attempts
        if (this.isCodeRemovalAttempt(context)) {
            return {
                required: true,
                reason: 'Code removal attempt detected - systematic analysis required',
                agents: ['bug-triage-specialist', 'code-analyzer', 'code-reviewer'],
                depth: 'comprehensive',
                confidence: 0.95
            };
        }
        // Check for AI degradation patterns
        const pattern = kernelAnalysis.pattern;
        if (pattern && (pattern.id === 'A10' || pattern.id === 'A15')) {
            return {
                required: true,
                reason: 'AI degradation pattern detected - quality check required',
                agents: ['bug-triage-specialist', 'code-analyzer', 'code-reviewer'],
                depth: 'comprehensive',
                confidence: 0.9
            };
        }
        // No analysis required
        return {
            required: false,
            confidence: 0.8
        };
    }
    /**
     * Invoke regression analysis with appropriate agents
     */
    async invokeAnalysis(context, decision) {
        await frameworkLogger.log('regression-analysis-service', 'invoking-analysis', 'info', {
            reason: decision.reason,
            agents: decision.agents,
            depth: decision.depth,
            confidence: decision.confidence
        });
        // Create delegation request for multi-agent conference
        const delegationRequest = {
            operation: 'regression-analysis',
            description: `Systematic regression analysis: ${decision.reason}`,
            context: {
                ...context,
                analysisDepth: decision.depth
            },
            conferenceType: 'regression-investigation'
        };
        // Create agent delegator for this operation
        const stateManager = new StringRayStateManager();
        const agentDelegator = createAgentDelegator(stateManager, strRayConfigLoader);
        // Analyze delegation strategy
        const analysis = await agentDelegator.analyzeDelegation(delegationRequest);
        // Execute delegation
        const result = await agentDelegator.executeDelegation(analysis, delegationRequest);
        if (!result.success) {
            await frameworkLogger.log('regression-analysis-service', 'analysis-failed', 'error', {
                error: result.errors?.join(', '),
                agents: analysis.agents
            });
            // If analysis fails, block the operation
            throw new Error(`Regression analysis failed: ${result.errors?.join(', ')}`);
        }
        await frameworkLogger.log('regression-analysis-service', 'analysis-completed', 'success', {
            agents: analysis.agents,
            executionTime: result.totalTime
        });
    }
    /**
     * Validate that no regression was introduced after operation
     */
    async validateNoRegression(context) {
        // Run regression validation tests
        await frameworkLogger.log('regression-analysis-service', 'post-operation-validation', 'info', { context: context.operation });
    }
    /**
     * Detect if operation is attempting to remove code
     */
    isCodeRemovalAttempt(context) {
        const operation = context.operation?.toLowerCase() || '';
        const removalKeywords = ['delete', 'remove', 'eliminate', 'strip', 'prune'];
        return removalKeywords.some(keyword => operation.includes(keyword));
    }
}
