import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import * as fs from "fs/promises";
import * as path from "path";
import { v4 as uuidv4 } from "uuid";
import { logger } from "../../../core/monitoring/logger.js";
class CompoundingEngineeringManager {
  knowledgeBase;
  baseDir;
  projectId;
  constructor(baseDir = "./.compounding", projectId) {
    this.baseDir = baseDir;
    this.projectId = projectId || "default";
    this.knowledgeBase = {
      totalFeatures: 0,
      learningsByCategory: {
        planning: [],
        implementation: [],
        testing: [],
        deployment: []
      },
      bestPractices: [],
      automatedHooks: [],
      specializedAgents: [],
      customCommands: [],
      metrics: {
        developmentVelocityTrend: [],
        errorRateReduction: 0,
        codeReuseIncrease: 0,
        onboardingTimeReduction: 0
      }
    };
  }
  /**
   * Initialize the compounding system
   */
  async initialize() {
    await fs.mkdir(this.baseDir, { recursive: true });
    await this.loadExistingKnowledge();
    logger.info("Compounding Engineering Manager initialized", {
      totalFeatures: this.knowledgeBase.totalFeatures,
      baseDir: this.baseDir
    });
  }
  /**
   * Capture learning from a feature development session
   */
  async captureFeatureLearning(featureName, sessionData, agentOutputs, userFeedback) {
    const learningId = uuidv4();
    logger.info("Capturing feature learning", {
      featureName,
      learningId,
      agentCount: agentOutputs.length
    });
    const learning = await this.extractLearningsFromSession(
      learningId,
      featureName,
      sessionData,
      agentOutputs,
      userFeedback
    );
    await this.storeLearning(learning);
    await this.updateCompoundedKnowledge(learning);
    await this.generateArtifacts();
    return learningId;
  }
  /**
   * Extract actionable learnings from development session
   */
  async extractLearningsFromSession(id, featureName, sessionData, agentOutputs, userFeedback) {
    const successes = this.identifySuccesses(sessionData, agentOutputs);
    const failures = this.identifyFailures(sessionData, agentOutputs);
    const patterns = this.extractPatterns(sessionData, agentOutputs);
    const agentLearnings = this.analyzeAgentBehavior(agentOutputs);
    const automationOpportunities = this.identifyAutomationOpportunities(
      sessionData,
      agentOutputs
    );
    return {
      id,
      featureName,
      timestamp: Date.now(),
      developmentPhase: this.inferDevelopmentPhase(sessionData),
      successes,
      failures,
      patterns,
      agentLearnings,
      automationOpportunities
    };
  }
  /**
   * Identify what worked well in the session
   */
  identifySuccesses(sessionData, agentOutputs) {
    const successes = [];
    for (const output of agentOutputs) {
      if (output.success && output.strategy) {
        successes.push({
          strategy: output.strategy,
          impact: this.assessImpact(output),
          reusability: this.assessReusability(output),
          description: output.description || "Successful agent execution"
        });
      }
    }
    if (sessionData.codePatterns) {
      for (const pattern of sessionData.codePatterns) {
        if (pattern.successful) {
          successes.push({
            strategy: `Code pattern: ${pattern.name}`,
            impact: "high",
            reusability: "universal",
            description: pattern.description
          });
        }
      }
    }
    return successes;
  }
  /**
   * Identify failures and their resolutions
   */
  identifyFailures(sessionData, agentOutputs) {
    const failures = [];
    for (const output of agentOutputs) {
      if (output.errors && output.errors.length > 0) {
        for (const error of output.errors) {
          failures.push({
            issue: error.message || "Agent execution failed",
            cause: error.cause || "Unknown cause",
            solution: error.resolution || "Manual intervention required",
            prevention: this.generatePreventionStrategy(error)
          });
        }
      }
    }
    if (sessionData.buildErrors) {
      for (const error of sessionData.buildErrors) {
        failures.push({
          issue: `Build error: ${error.type}`,
          cause: error.details,
          solution: error.fix,
          prevention: "Add pre-build validation hook"
        });
      }
    }
    return failures;
  }
  /**
   * Extract reusable patterns from the session
   */
  extractPatterns(sessionData, agentOutputs) {
    const patterns = [];
    if (agentOutputs.length > 1) {
      const coordinationPattern = this.analyzeCoordinationPattern(agentOutputs);
      if (coordinationPattern) {
        patterns.push(coordinationPattern);
      }
    }
    if (sessionData.codeStructure) {
      const structurePattern = this.analyzeCodeStructurePattern(sessionData.codeStructure);
      if (structurePattern) {
        patterns.push(structurePattern);
      }
    }
    return patterns;
  }
  /**
   * Analyze agent behavior for improvements
   */
  analyzeAgentBehavior(agentOutputs) {
    const commonMistakes = [];
    const effectivePrompts = [];
    const toolUsagePatterns = [];
    const coordinationInsights = [];
    for (const output of agentOutputs) {
      if (output.retries && output.retries.length > 0) {
        commonMistakes.push(...output.retries.map((r) => r.reason));
      }
      if (output.success && output.promptUsed) {
        effectivePrompts.push(output.promptUsed);
      }
      if (output.toolsUsed) {
        toolUsagePatterns.push(...output.toolsUsed);
      }
      if (output.coordination) {
        coordinationInsights.push(output.coordination.insight);
      }
    }
    return {
      commonMistakes: [...new Set(commonMistakes)],
      effectivePrompts: [...new Set(effectivePrompts)],
      toolUsagePatterns: [...new Set(toolUsagePatterns)],
      coordinationInsights: [...new Set(coordinationInsights)]
    };
  }
  /**
   * Identify tasks that can be automated
   */
  identifyAutomationOpportunities(sessionData, agentOutputs) {
    const opportunities = [];
    const taskFrequency = this.analyzeTaskFrequency(agentOutputs);
    for (const [task, frequency] of Object.entries(taskFrequency)) {
      if (frequency > 2) {
        opportunities.push({
          task,
          frequency: "often",
          complexity: "simple",
          implementation: "hook"
        });
      }
    }
    if (sessionData.manualSteps) {
      for (const step of sessionData.manualSteps) {
        opportunities.push({
          task: step.description,
          frequency: "sometimes",
          complexity: step.complexity || "simple",
          implementation: "command"
        });
      }
    }
    return opportunities;
  }
  /**
   * Update compounded knowledge with new learning
   */
  async updateCompoundedKnowledge(learning) {
    this.knowledgeBase.learningsByCategory[learning.developmentPhase].push(learning);
    this.knowledgeBase.totalFeatures++;
    await this.updateBestPractices(learning);
    await this.updateMetrics(learning);
    await this.saveKnowledge();
  }
  /**
   * Generate artifacts (hooks, commands, agents) from accumulated learnings
   */
  async generateArtifacts() {
    await this.generateHooks();
    await this.generateSpecializedAgents();
    await this.generateCustomCommands();
  }
  /**
   * Generate automation hooks
   */
  async generateHooks() {
    const allOpportunities = Object.values(this.knowledgeBase.learningsByCategory).flat().flatMap((learning) => learning.automationOpportunities).filter((opp) => opp.implementation === "hook");
    const hookCounts = /* @__PURE__ */ new Map();
    for (const opp of allOpportunities) {
      hookCounts.set(opp.task, (hookCounts.get(opp.task) || 0) + 1);
    }
    for (const [task, count] of hookCounts) {
      if (count >= 3 && !this.knowledgeBase.automatedHooks.includes(task)) {
        await this.createHook(task);
        this.knowledgeBase.automatedHooks.push(task);
      }
    }
  }
  /**
   * Create an automation hook
   */
  async createHook(task) {
    const hookName = task.replace(/\s+/g, "-").toLowerCase();
    const hookPath = path.join(this.baseDir, "hooks", `${hookName}.ts`);
    await fs.mkdir(path.dirname(hookPath), { recursive: true });
    const hookContent = `
/**
 * Auto-generated hook for: ${task}
 * Generated by Compounding Engineering Pattern
 */

export async function ${hookName.replace(/-/g, "")}Hook() {
  // TODO: Implement automation for: ${task}
  console.log('Executing automated hook: ${task}');
}
    `;
    await fs.writeFile(hookPath, hookContent);
    logger.info("Generated automation hook", { task, hookPath });
  }
  /**
   * Load existing knowledge base
   */
  async loadExistingKnowledge() {
    const knowledgePath = path.join(this.baseDir, "knowledge.json");
    try {
      const content = await fs.readFile(knowledgePath, "utf-8");
      this.knowledgeBase = JSON.parse(content);
    } catch (error) {
      logger.info("Starting fresh knowledge base");
    }
  }
  /**
   * Save knowledge base to disk
   */
  async saveKnowledge() {
    const knowledgePath = path.join(this.baseDir, "knowledge.json");
    await fs.writeFile(
      knowledgePath,
      JSON.stringify(this.knowledgeBase, null, 2)
    );
  }
  /**
   * Get current compounding metrics
   */
  getCompoundingMetrics() {
    const totalLearnings = Object.values(this.knowledgeBase.learningsByCategory).flat().length;
    const automationLevel = this.knowledgeBase.automatedHooks.length / Math.max(totalLearnings, 1);
    const recentLearnings = totalLearnings > 5 ? totalLearnings / 5 : totalLearnings;
    return {
      totalFeatures: this.knowledgeBase.totalFeatures,
      automationLevel,
      learningVelocity: recentLearnings,
      knowledgeReuse: this.knowledgeBase.bestPractices.length
    };
  }
  // Helper methods
  assessImpact(output) {
    if (output.linesChanged > 100) return "high";
    if (output.linesChanged > 20) return "medium";
    return "low";
  }
  assessReusability(output) {
    if (output.pattern === "generic") return "universal";
    if (output.domain) return "domain_specific";
    return "feature_specific";
  }
  generatePreventionStrategy(error) {
    return `Add validation for: ${error.type || "unknown error type"}`;
  }
  inferDevelopmentPhase(sessionData) {
    if (sessionData.phase) return sessionData.phase;
    if (sessionData.testsRun) return "testing";
    if (sessionData.codeGenerated) return "implementation";
    return "planning";
  }
  analyzeCoordinationPattern(agentOutputs) {
    return {
      name: "Multi-agent coordination",
      context: `${agentOutputs.length} agents worked together`,
      solution: "Successful multi-agent coordination pattern",
      examples: agentOutputs.map((a) => a.role || "unknown")
    };
  }
  analyzeCodeStructurePattern(structure) {
    return {
      name: "Code structure pattern",
      context: structure.type,
      solution: structure.pattern,
      examples: structure.examples || []
    };
  }
  analyzeTaskFrequency(agentOutputs) {
    const frequency = {};
    for (const output of agentOutputs) {
      if (output.task) {
        frequency[output.task] = (frequency[output.task] || 0) + 1;
      }
    }
    return frequency;
  }
  async updateBestPractices(learning) {
    for (const success of learning.successes) {
      if (success.impact === "high" && success.reusability === "universal") {
        const existing = this.knowledgeBase.bestPractices.find(
          (bp) => bp.practice === success.strategy
        );
        if (existing) {
          existing.evidence.push(learning.featureName);
          existing.confidence = Math.min(existing.confidence + 0.1, 1);
        } else {
          this.knowledgeBase.bestPractices.push({
            category: learning.developmentPhase,
            practice: success.strategy,
            evidence: [learning.featureName],
            confidence: 0.7
          });
        }
      }
    }
  }
  async updateMetrics(learning) {
    const velocityScore = learning.successes.length - learning.failures.length;
    this.knowledgeBase.metrics.developmentVelocityTrend.push(velocityScore);
    if (this.knowledgeBase.metrics.developmentVelocityTrend.length > 10) {
      this.knowledgeBase.metrics.developmentVelocityTrend.shift();
    }
  }
  async generateSpecializedAgents() {
  }
  async generateCustomCommands() {
  }
}
var compounding_engineering_pattern_default = CompoundingEngineeringManager;
export {
  CompoundingEngineeringManager,
  compounding_engineering_pattern_default as default
};
//# sourceMappingURL=compounding-engineering-pattern.js.map
