{
  "FEATURE_METADATA": {
    "feature_name": "AI Agent Architect System",
    "module_path": "modules/agentArchitect",
    "version": "1.0.0",
    "status": "blueprint",
    "depends_on": [
      "modules/dialogflow",
      "modules/masterAgent",
      "embeddingEngine.js",
      "modules/dialogflow/services/publish.service.js"
    ],
    "new_components": [
      "AgentArchitectJob (MongoDB model)",
      "AgentArchitectVersion (MongoDB model)",
      "modelConnector.service.js",
      "promptEngine.service.js",
      "architectPipeline.service.js",
      "validationEngine.service.js",
      "qualityScorer.service.js",
      "refinementEngine.service.js",
      "agentArchitect.controller.js",
      "agentArchitect.routes.js"
    ],
    "integration_points": [
      "DfAgent, DfIntent, DfEntity (create/update via existing services)",
      "MasterAgent, MasterAgentChild, MasterRouteRule (create/update via existing services)",
      "publishService (publish generated agent after approval)",
      "seedWABFRoutingAgent pattern (link nluAgentId after generation)"
    ]
  },

  "FEATURE_OVERVIEW": {
    "summary": "AI Agent Architect System is an AI-powered backend pipeline that converts a plain English user prompt into a production-ready child agent, master agent, or routing configuration. It uses a GPT-class generative model as an internal thinking engine — acting as an agent planner, intent architect, entity architect, routing engineer, and QA analyst simultaneously. The user never needs to manually design intents, entities, routing rules, or test cases. The system infers, generates, validates, scores, and refines all components automatically before saving.",
    "what_it_replaces": "Manual seed script authoring, manual intent/entity design, manual routing rule design, manual test case writing.",
    "what_it_does_not_replace": "Existing runtime execution platform (dialogRuntime, masterAgent orchestration). Generated configs flow INTO the existing platform — the platform itself is unchanged."
  },

  "SYSTEM_OBJECTIVES": [
    "Convert a weak user prompt into a comprehensive, validated agent configuration through multi-stage AI reasoning.",
    "Generate complete child agents (intents, entities, slots, responses, fallbacks, escalations) from a single sentence.",
    "Generate complete master agents (child registry, intent routing, keyword fallback, dependency rules, conflict resolution) from a multi-agent description.",
    "Auto-detect missing intents, entities, edge cases, and routing gaps without user intervention.",
    "Produce embedding-ready training phrases for the master NLU routing agent automatically.",
    "Support iterative refinement: if quality score is below threshold, automatically run improvement passes.",
    "Support human-in-the-loop: allow preview, edit, accept, regenerate before final save.",
    "Support improvement of existing agents: feed existing config into AI and get improvement suggestions.",
    "Maintain full generation history, prompt history, and versioning for auditability.",
    "Produce machine-parseable structured output compatible with existing DfAgent/DfIntent/DfEntity/MasterAgent schemas."
  ],

  "SUPPORTED_USE_CASES": [
    { "id": "UC-01", "mode": "CREATE_CHILD_AGENT",     "example_prompt": "Create a WhatsApp onboarding agent for business account setup" },
    { "id": "UC-02", "mode": "CREATE_CHILD_AGENT",     "example_prompt": "Build a sales support chatbot for e-commerce order queries" },
    { "id": "UC-03", "mode": "CREATE_CHILD_AGENT",     "example_prompt": "Create a template creation helper agent for WhatsApp messaging" },
    { "id": "UC-04", "mode": "CREATE_MASTER_AGENT",    "example_prompt": "Create a master agent for onboarding, templates, campaigns, chatbot" },
    { "id": "UC-05", "mode": "CREATE_MASTER_AGENT",    "example_prompt": "Build a hotel booking master agent with room booking, F&B, and concierge children" },
    { "id": "UC-06", "mode": "IMPROVE_CHILD_AGENT",    "example_prompt": "Add more intents to my existing onboarding agent" },
    { "id": "UC-07", "mode": "IMPROVE_CHILD_AGENT",    "example_prompt": "Improve entity coverage in my template creation agent" },
    { "id": "UC-08", "mode": "IMPROVE_MASTER_AGENT",   "example_prompt": "Fix weak routing in my master agent — 'template' is going to wrong child" },
    { "id": "UC-09", "mode": "GENERATE_ROUTING_RULES", "example_prompt": "Generate routing rules for my 4 child agents" },
    { "id": "UC-10", "mode": "GENERATE_TEST_CASES",    "example_prompt": "Generate test cases for my onboarding agent" },
    { "id": "UC-11", "mode": "GENERATE_TEST_CONVERSATIONS", "example_prompt": "Generate 20 multi-turn test conversations for my master agent" },
    { "id": "UC-12", "mode": "FIX_ROUTING_ISSUES",     "example_prompt": "My campaign agent gets triggered when user asks about templates" },
    { "id": "UC-13", "mode": "EXPAND_INTENTS_ENTITIES","example_prompt": "Add Hindi language variants to all my intents" }
  ],

  "USER_EXPERIENCE_FLOW": {
    "modes": {
      "quick_generate": {
        "steps": [
          "1. User enters prompt in UI text box",
          "2. System shows: Understood. Generating [X] type agent for domain [Y]...",
          "3. Background job runs all 8 pipeline stages",
          "4. Progress bar shows stage completion",
          "5. Preview panel shows generated agent summary",
          "6. User sees: quality score, detected gaps, confidence summary",
          "7. User clicks Approve → agent saved to platform",
          "8. User clicks Regenerate → full pipeline reruns"
        ]
      },
      "advanced_generate": {
        "steps": [
          "1. User enters prompt",
          "2. System shows Stage 1 output: expanded requirements — user can edit before continuing",
          "3. User approves/edits expanded requirements",
          "4. System runs Stage 3-4: architecture + intent/entity draft",
          "5. User reviews intent/entity list — can add/remove/rename",
          "6. System runs Stage 5-6: validation + refinement",
          "7. User reviews quality score + gaps report",
          "8. System runs Stage 7: test cases + sample conversations",
          "9. User does final review of complete agent config",
          "10. User approves → agent saved and published"
        ]
      },
      "refine_existing": {
        "steps": [
          "1. User selects existing agent from platform",
          "2. User enters improvement request",
          "3. System feeds existing config + request to AI",
          "4. AI identifies gaps and proposes additions",
          "5. User reviews diff: added/modified/removed items",
          "6. User approves → updated agent saved as new version"
        ]
      }
    }
  },

  "HIGH_LEVEL_ARCHITECTURE": {
    "entry_point": "POST /agent-architect/generate → AgentArchitectController",
    "layer_1_request": "UserRequestInterpreter → detects mode, domain, target type",
    "layer_2_planning": "PromptEnhancementEngine → expands user prompt into full requirements spec",
    "layer_3_ai": "GenerativeAIOrchestrator → calls ModelConnector for multi-pass generation",
    "layer_4_pipeline": "ChildAgentPipeline or MasterAgentPipeline → orchestrates sub-generators",
    "layer_5_validation": "ValidationEngine + QualityScorer → checks output completeness",
    "layer_6_refinement": "RefinementEngine → auto-improves if quality < threshold",
    "layer_7_output": "VersioningLayer → stores draft version, exposes preview API",
    "layer_8_save": "On approval: saves to DfAgent/DfIntent/DfEntity/MasterAgent via existing services",
    "job_queue": "All generation jobs run as background async jobs (Bull/custom queue). HTTP returns job_id immediately.",
    "state_storage": "AgentArchitectJob (MongoDB) stores all intermediate stages, outputs, and scores"
  },

  "CORE_MODULES": {
    "M01_UserRequestInterpreter": {
      "file": "modules/agentArchitect/services/requestInterpreter.service.js",
      "responsibility": "Parse raw user prompt. Detect pipeline mode, domain, target type (child/master/improve/test). Extract named entities like existing agent names. Return structured RequestSpec.",
      "inputs": "{ prompt: string, userId, tenantId, existingAgentId? }",
      "outputs": "{ mode: PipelineMode, domain: string, targetType: 'child'|'master'|'improve'|'routing'|'test', entityHints: [], existingConfig?: object, confidence: float }",
      "logic": "Lightweight LLM call with classification prompt. Fallback: keyword detection for mode (create/improve/generate/fix)."
    },
    "M02_AgentTypeDetector": {
      "file": "part of requestInterpreter.service.js",
      "responsibility": "Given domain description, classify as child agent or master agent. If 'for X, Y, Z agents' → master. If single domain → child.",
      "rules": [
        "Explicit keywords: 'master agent', 'orchestrate', 'route between' → MASTER",
        "Multiple domain names → MASTER",
        "Single focused domain → CHILD",
        "Existing agent reference + improvement keyword → IMPROVE"
      ]
    },
    "M03_PromptEnhancementEngine": {
      "file": "modules/agentArchitect/services/promptEnhancer.service.js",
      "responsibility": "Expand minimal user prompt into a rich requirements specification. Infer likely intents, entities, edge cases, validations, dependencies, and conversation patterns.",
      "inputs": "RequestSpec",
      "outputs": "EnhancedSpec { domain, purpose, likely_intents[], likely_entities[], likely_validations[], likely_edge_cases[], dependency_hints[], conversation_patterns[], tone, fallback_policy }",
      "model_call": "PLAN stage — system prompt: Agent Planner role. Single LLM call returning structured JSON.",
      "example_expansion": "Input: 'Create WhatsApp onboarding agent' → Output: 7 sub-intent groups, 5 entity types, 3 validation rules, 2 flow paths (manual/auto), 4 edge cases, 2 dependency notes"
    },
    "M04_GenerativeAIOrchestrator": {
      "file": "modules/agentArchitect/services/aiOrchestrator.service.js",
      "responsibility": "Coordinates all LLM calls across pipeline stages. Manages call order, retries, timeouts, token budgeting, and stage-level caching.",
      "sub_calls": [
        "planCall — Stage 2 requirement expansion",
        "architectCall — Stage 3 architecture design",
        "generateCall — Stage 4 full config generation",
        "validateCall — Stage 5 validation audit",
        "refineCall — Stage 6 improvement pass",
        "testGenCall — Stage 7 test + conversation generation"
      ],
      "retry_policy": "max 3 retries per call, exponential backoff 1s/2s/4s",
      "fallback": "If model unavailable: mark stage as NEEDS_HUMAN_REVIEW, continue pipeline with partial output"
    },
    "M05_ChildAgentGenerationPipeline": {
      "file": "modules/agentArchitect/pipelines/childAgentPipeline.js",
      "responsibility": "Full pipeline for generating a new child agent. Coordinates intent generator, entity generator, slot-fill rules, response templates, fallback rules, escalation rules, completion rules.",
      "stages": ["expand", "architect", "generate_intents", "generate_entities", "generate_responses", "validate", "refine", "test_gen", "finalize"],
      "output": "ChildAgentConfig { agentMeta, intents[], entities[], slotRules[], responses[], fallbacks[], escalations[], completionRules[], sampleConversations[], testCases[] }"
    },
    "M06_MasterAgentGenerationPipeline": {
      "file": "modules/agentArchitect/pipelines/masterAgentPipeline.js",
      "responsibility": "Full pipeline for generating a master agent and its routing configuration. Generates child registry, NLU routing intents (for seedWABFRoutingAgent pattern), keyword fallback rules, dependency rules, conflict resolution, session state model.",
      "stages": ["expand", "identify_children", "generate_child_registry", "generate_nlu_routing_intents", "generate_keyword_rules", "generate_dependency_rules", "generate_conflict_rules", "validate", "refine", "test_gen"],
      "output": "MasterAgentConfig { masterMeta, childRegistry[], nluRoutingIntents[], keywordRules[], dependencyRules[], conflictRules[], sessionStateModel, testCases[], routingConversations[] }"
    },
    "M07_IntentGenerationEngine": {
      "file": "modules/agentArchitect/services/intentGenerator.service.js",
      "responsibility": "Given domain + purpose, generate a complete set of intents. Each intent includes name, description, training phrases (20-25), parameters, outputContexts, responses, and embedding_label.",
      "ai_role": "Intent Architect",
      "generation_rules": [
        "Minimum 8 intents per child agent",
        "At least 1 fallback intent",
        "At least 1 escalation/handoff intent",
        "Training phrases must include paraphrases, typo variants, casual language",
        "No two intents with overlapping core semantic meaning",
        "Each intent must have a distinct embedding_label for centroid computation"
      ],
      "output_per_intent": "{ name, description, trainingPhrases[], parameters[], inputContexts[], outputContexts[], responses[], isFallback, priority, embedding_label }"
    },
    "M08_EntityGenerationEngine": {
      "file": "modules/agentArchitect/services/entityGenerator.service.js",
      "responsibility": "Generate entities (custom + system references) for the agent domain. Each entity includes entries with synonyms, regex patterns if applicable, and slot-fill validation rules.",
      "ai_role": "Entity Architect",
      "generation_rules": [
        "Identify all slot types needed from intent parameters",
        "For each slot: determine if system entity (sys.date, sys.phone, sys.city) or custom",
        "Custom entities: generate entries with 5-10 synonyms per value",
        "System entities: register DfEntity records (required for extraction pipeline)",
        "Generate validation rules: required/optional, format constraints, error messages"
      ]
    },
    "M09_RoutingRuleGenerationEngine": {
      "file": "modules/agentArchitect/services/routingRuleGenerator.service.js",
      "responsibility": "Generate MasterRouteRule documents for a master agent. Produces priority-tiered keyword rules and intent-condition rules. Also generates NLU routing intents for the embedding-based routing agent.",
      "ai_role": "Routing Architect",
      "output": "{ keywordRules[], intentRules[], conflictRules[], dependencyRules[], continuationRules[], clarificationRule, nluRoutingIntents[] }"
    },
    "M10_ValidationEngine": {
      "file": "modules/agentArchitect/services/validationEngine.service.js",
      "responsibility": "Validate generated agent config for completeness, correctness, and quality. Returns a ValidationReport with pass/fail items and severity levels.",
      "checks": [
        { "id": "V-01", "name": "Intent count minimum", "severity": "error",   "rule": "Child agent must have >= 5 non-fallback intents" },
        { "id": "V-02", "name": "Fallback intent exists", "severity": "error",  "rule": "Exactly 1 isFallback=true intent required" },
        { "id": "V-03", "name": "Training phrase count", "severity": "warning","rule": "Each intent should have >= 10 training phrases" },
        { "id": "V-04", "name": "Entity slot coverage",  "severity": "error",  "rule": "All intent parameters must reference a defined entity" },
        { "id": "V-05", "name": "Overlapping intents",   "severity": "warning","rule": "No two intents should share > 40% of training phrases semantically" },
        { "id": "V-06", "name": "Missing escalation",    "severity": "warning","rule": "Agent should have at least 1 human handoff / escalation intent" },
        { "id": "V-07", "name": "Response coverage",     "severity": "error",  "rule": "Every intent must have at least 1 response defined" },
        { "id": "V-08", "name": "Routing rule coverage", "severity": "error",  "rule": "Every child agent must have at least 1 keyword routing rule in master" },
        { "id": "V-09", "name": "Dependency completeness","severity": "warning","rule": "If dependency declared, blocked task must be queued correctly" },
        { "id": "V-10", "name": "Missing conflict rules", "severity": "warning","rule": "Shared ambiguous keywords (template, keyword, publish) must have conflict resolution rules" },
        { "id": "V-11", "name": "NLU routing coverage",  "severity": "warning","rule": "Each child agent must have >= 3 NLU routing intents with >= 15 training phrases each" },
        { "id": "V-12", "name": "System entity registration", "severity": "error", "rule": "All sys.* entities used in parameters must have DfEntity records" }
      ]
    },
    "M11_ConflictAmbiguityDetector": {
      "file": "part of validationEngine.service.js",
      "responsibility": "Detect semantic overlap between intents across the same agent (child) or across children (master routing). Flag overlapping training phrases.",
      "method": "Compute pairwise cosine similarity between intent training phrase embeddings. Flag pairs with similarity > 0.75 as potential conflicts.",
      "output": "ConflictReport { conflicts[], ambiguous_keywords[], suggested_disambiguations[] }"
    },
    "M12_RefinementEngine": {
      "file": "modules/agentArchitect/services/refinementEngine.service.js",
      "responsibility": "Take a ValidationReport + QualityScore and automatically improve weak areas by running targeted AI generation sub-calls.",
      "triggers": [
        "quality_score < 70 → full refinement pass",
        "V-03 fails → expand training phrases for flagged intents",
        "V-05 fails → deduplicate/rename overlapping intents",
        "V-08 fails → generate missing routing rules",
        "V-11 fails → generate additional NLU routing intents"
      ],
      "max_refinement_iterations": 3,
      "output": "RefinedConfig (merged with original, with change_summary)"
    },
    "M13_TestCaseGenerationEngine": {
      "file": "modules/agentArchitect/services/testCaseGenerator.service.js",
      "responsibility": "Generate structured test cases and multi-turn conversation scripts for the generated agent. Produces both unit-level intent tests and E2E conversation flows.",
      "ai_role": "QA Validator + Conversation Tester",
      "output_types": [
        "IntentTestCases: { input, expected_intent, expected_entities, expected_target_agent }",
        "ConversationScripts: multi-turn with expected_intent per turn, expected_state_updates, validation_notes",
        "RegressionTests: known failure patterns explicitly marked"
      ]
    },
    "M14_SampleConversationEngine": {
      "file": "part of testCaseGenerator.service.js",
      "responsibility": "Generate realistic multi-turn conversation examples showing the happy path, error recovery, slot-filling, context switching, and edge cases.",
      "output": "Array of ConversationScript (same schema as wa-masterConversationSuite-part*.json)"
    },
    "M15_QualityScoringEngine": {
      "file": "modules/agentArchitect/services/qualityScorer.service.js",
      "responsibility": "Score generated agent config across multiple quality dimensions. Return composite score 0-100.",
      "dimensions": [
        { "id": "QS-01", "name": "Intent Completeness",       "weight": 15, "scoring": "intents_count / expected_minimum * 100, capped at 100" },
        { "id": "QS-02", "name": "Training Phrase Coverage",  "weight": 15, "scoring": "avg phrases per intent / 20 * 100" },
        { "id": "QS-03", "name": "Entity Coverage",           "weight": 10, "scoring": "slots_with_entities / total_slots * 100" },
        { "id": "QS-04", "name": "Routing Quality",           "weight": 15, "scoring": "validated_rules / total_rules * 100" },
        { "id": "QS-05", "name": "Disambiguation Quality",    "weight": 10, "scoring": "1 - (conflict_count / total_intent_pairs), scaled" },
        { "id": "QS-06", "name": "Fallback/Escalation Cover", "weight": 10, "scoring": "has_fallback * 50 + has_escalation * 50" },
        { "id": "QS-07", "name": "Test Readiness",            "weight": 10, "scoring": "test_cases_count / 20 * 100" },
        { "id": "QS-08", "name": "Response Quality",          "weight": 10, "scoring": "intents_with_responses / total_intents * 100" },
        { "id": "QS-09", "name": "Edge Case Handling",        "weight": 5,  "scoring": "edge_case_intents_present * 100" }
      ],
      "threshold_auto_approve": 85,
      "threshold_auto_refine": 65,
      "threshold_require_human": 50
    },
    "M16_VersioningLayer": {
      "file": "modules/agentArchitect/services/versioningLayer.service.js",
      "responsibility": "Store each generation stage output as a versioned snapshot. Support diff between versions. Support rollback to any stage.",
      "version_states": ["DRAFT", "REFINED", "VALIDATED", "APPROVED", "SAVED"]
    },
    "M17_HumanReviewLayer": {
      "file": "modules/agentArchitect/services/humanReview.service.js",
      "responsibility": "Present generated config to user for review. Track accept/reject/edit decisions per section. Support partial regeneration of rejected sections.",
      "review_sections": ["agentMeta", "intents", "entities", "responses", "routingRules", "testCases"]
    },
    "M18_FeedbackLearningLayer": {
      "file": "modules/agentArchitect/services/feedbackLearning.service.js",
      "responsibility": "Store user feedback (accepted/rejected/edited) per generation. Use accepted examples as few-shot examples in future prompts for same domain.",
      "storage": "AgentArchitectFeedback (MongoDB collection)"
    },
    "M19_ModelConnector": {
      "file": "modules/agentArchitect/services/modelConnector.service.js",
      "responsibility": "Abstract GPT-like API calls. Handle auth, retries, timeouts, rate limiting, cost logging, structured output parsing.",
      "see_section": "MODEL_CONNECTOR_DESIGN"
    },
    "M20_FailsafeLayer": {
      "file": "part of aiOrchestrator.service.js",
      "responsibility": "If any pipeline stage fails: mark stage FAILED, store partial output, continue pipeline with degraded mode, flag section for human review.",
      "see_section": "FAILSAFE_AND_ERROR_HANDLING"
    }
  },

  "INTERNAL_AI_PIPELINE": {
    "stages": [
      {
        "stage": 1,
        "name": "UNDERSTAND",
        "ai_role": "Request Classifier",
        "model_call": true,
        "input": "raw user prompt",
        "output": "{ mode, domain, targetType, entityHints, confidence }",
        "prompt_template_key": "CLASSIFY_REQUEST",
        "max_tokens": 300,
        "fallback": "keyword classification if model unavailable"
      },
      {
        "stage": 2,
        "name": "EXPAND",
        "ai_role": "Agent Planner",
        "model_call": true,
        "input": "RequestSpec + domain context",
        "output": "EnhancedSpec with inferred intents, entities, validations, edge cases",
        "prompt_template_key": "EXPAND_REQUIREMENTS",
        "max_tokens": 1500,
        "fallback": "use domain template from built-in domain knowledge library"
      },
      {
        "stage": 3,
        "name": "ARCHITECT",
        "ai_role": "System Architect",
        "model_call": true,
        "input": "EnhancedSpec",
        "output": "ArchitecturePlan { intent_groups[], entity_types[], state_model, routing_strategy, dependency_map }",
        "prompt_template_key": "DESIGN_ARCHITECTURE",
        "max_tokens": 2000
      },
      {
        "stage": 4,
        "name": "GENERATE",
        "ai_role": "Intent Architect + Entity Architect",
        "model_call": true,
        "input": "ArchitecturePlan",
        "output": "DraftConfig { intents[], entities[], responses[], slotRules[], routingRules[] }",
        "prompt_template_key": "GENERATE_FULL_CONFIG",
        "max_tokens": 4000,
        "note": "Largest call. Split into sub-calls if token budget exceeded: intents first, then entities, then routing."
      },
      {
        "stage": 5,
        "name": "VALIDATE",
        "ai_role": "QA Validator",
        "model_call": "partial",
        "input": "DraftConfig",
        "output": "ValidationReport { errors[], warnings[], missing_items[], conflict_pairs[] }",
        "prompt_template_key": "VALIDATE_AGENT_CONFIG",
        "programmatic_checks": "ValidationEngine.runChecks(config) — no model needed for structural checks",
        "model_call_for": "semantic overlap detection, missing edge cases identification",
        "max_tokens": 1000
      },
      {
        "stage": 6,
        "name": "REFINE",
        "ai_role": "Refinement Reviewer",
        "model_call": "conditional",
        "condition": "quality_score < 70 OR ValidationReport has errors",
        "input": "DraftConfig + ValidationReport",
        "output": "RefinedConfig + change_summary",
        "prompt_template_key": "REFINE_WEAK_AREAS",
        "max_tokens": 2500,
        "max_iterations": 3
      },
      {
        "stage": 7,
        "name": "TEST_GENERATE",
        "ai_role": "Conversation Tester",
        "model_call": true,
        "input": "RefinedConfig",
        "output": "TestPack { intentTests[], conversationScripts[], edgeCaseTests[], regressionTests[] }",
        "prompt_template_key": "GENERATE_TEST_PACK",
        "max_tokens": 3000
      },
      {
        "stage": 8,
        "name": "FINALIZE",
        "ai_role": "none — programmatic",
        "model_call": false,
        "input": "RefinedConfig + TestPack + QualityScore",
        "output": "FinalConfig — mapped to DfAgent/DfIntent/DfEntity/MasterAgent DB schemas",
        "action": "Store as AgentArchitectVersion with state=VALIDATED. Expose via preview API."
      }
    ]
  },

  "MODEL_CONNECTOR_DESIGN": {
    "file": "modules/agentArchitect/services/modelConnector.service.js",
    "supported_providers": ["openai", "anthropic", "azure-openai", "custom-compatible"],
    "config_source": "process.env.AI_ARCHITECT_PROVIDER, AI_ARCHITECT_API_KEY, AI_ARCHITECT_MODEL, AI_ARCHITECT_BASE_URL",
    "default_model": "gpt-4o or claude-opus-4-6 depending on provider config",
    "interface": {
      "call": "async call({ role, promptTemplateKey, variables, maxTokens, responseFormat }) → { content, usage, model, latency_ms }",
      "callStructured": "async callStructured({ ... }) → parsed JSON object (validates schema before returning)",
      "isAvailable": "sync → boolean (checks API key + provider configured)"
    },
    "retry_policy": { "max_retries": 3, "backoff_ms": [1000, 2000, 4000], "retry_on": ["rate_limit", "timeout", "server_error"] },
    "timeout_ms": 30000,
    "structured_output": "Use JSON mode or function calling depending on provider. Always validate returned JSON against expected schema before returning to caller.",
    "cost_tracking": { "log_per_call": true, "fields": ["model", "prompt_tokens", "completion_tokens", "cost_usd", "job_id", "stage"] },
    "logging": "Each call logged to AgentArchitectCallLog collection with: job_id, stage, prompt_hash, response_hash, tokens, latency, error_if_any",
    "security": "API key stored in environment variables only. Never stored in DB or logs. Prompt content logged with hash only, not raw content."
  },

  "PROMPT_TEMPLATE_SYSTEM": {
    "file": "modules/agentArchitect/prompts/",
    "template_files": [
      "classifyRequest.prompt.js",
      "expandRequirements.prompt.js",
      "designArchitecture.prompt.js",
      "generateFullConfig.prompt.js",
      "generateIntents.prompt.js",
      "generateEntities.prompt.js",
      "generateRoutingRules.prompt.js",
      "validateAgentConfig.prompt.js",
      "refineWeakAreas.prompt.js",
      "generateTestPack.prompt.js",
      "improveExistingAgent.prompt.js",
      "generateMasterAgent.prompt.js"
    ],
    "template_structure": {
      "system_prompt": "Defines AI role (e.g., 'You are an expert Agent Architect...'). Never changes per request.",
      "task_prompt": "Defines the specific task. Contains {{variable}} placeholders filled by PromptEngine.",
      "examples_section": "2-3 few-shot examples. Loaded from FeedbackLearning store for domain if available.",
      "output_schema": "Explicit JSON schema the model must follow. Included in prompt as instruction.",
      "constraints": "Hard rules: min counts, required fields, naming conventions."
    },
    "sample_system_prompts": {
      "CLASSIFY_REQUEST": "You are an intelligent request classifier for an AI-powered agent generation platform. Your job is to analyze a user prompt and determine what type of agent or configuration they want to build. Always respond with a valid JSON object matching the specified schema.",
      "EXPAND_REQUIREMENTS": "You are an expert conversational AI Agent Planner. Your job is to take a brief user description and expand it into a comprehensive requirements specification. Think deeply about: missing intents, edge cases, entity types, validation rules, conversation flows, fallback handling, escalation paths. Do not ask for clarification — infer intelligently from the domain.",
      "DESIGN_ARCHITECTURE": "You are a senior Dialogue System Architect. Given a requirements specification, produce a detailed architectural plan for the agent. Identify all intent groups, entity types, state transitions, routing strategy, and dependency relationships.",
      "GENERATE_FULL_CONFIG": "You are an expert Intent Architect and Entity Architect. Generate a complete production-ready agent configuration. Include 20-25 training phrases per intent covering paraphrases, casual language, and typo variants. All entities must have 5+ synonyms per value. All responses must be natural and helpful.",
      "VALIDATE_AGENT_CONFIG": "You are a QA Validator for dialogue systems. Review the provided agent configuration and identify: missing intents, weak training coverage, overlapping intents, missing entity definitions, incomplete slot validation, missing fallback paths, missing escalation, edge cases not handled.",
      "REFINE_WEAK_AREAS": "You are a Refinement Reviewer for dialogue systems. You have identified quality issues in the generated agent. Your job is to fix ALL identified issues and return a corrected configuration. Be specific — add the missing items, fix the overlapping items, expand the thin items.",
      "GENERATE_TEST_PACK": "You are a Conversation Test Designer. Generate a comprehensive test pack for the provided agent configuration. Include: unit intent tests (one input per intent), multi-turn conversation scripts (happy path, error recovery, slot-filling), adversarial tests (typos, short replies, ambiguous inputs), and regression tests for known failure patterns."
    }
  },

  "CHILD_AGENT_GENERATION_FLOW": {
    "trigger": "mode = CREATE_CHILD_AGENT",
    "pipeline": "childAgentPipeline.js",
    "stages": [
      { "step": 1, "action": "RequestInterpreter extracts domain, purpose, tone hints" },
      { "step": 2, "action": "PromptEnhancer expands to EnhancedSpec with 8-15 intent group suggestions" },
      { "step": 3, "action": "IntentGenerator produces all intents with training phrases" },
      { "step": 4, "action": "EntityGenerator produces all entities with entries + synonyms" },
      { "step": 5, "action": "For each intent: map parameters to generated entities" },
      { "step": 6, "action": "Generate response templates for each intent (3 variants per intent for randomization)" },
      { "step": 7, "action": "Generate slot-fill validation rules (required/optional, format regex, error message)" },
      { "step": 8, "action": "Generate fallback intent + escalation/handoff intent" },
      { "step": 9, "action": "Generate completion conditions (when is this agent's task done)" },
      { "step": 10, "action": "ValidationEngine.runChecks — all 12 checks" },
      { "step": 11, "action": "QualityScorer.score — composite 0-100" },
      { "step": 12, "action": "If score < 70: RefinementEngine.refine (max 3 iterations)" },
      { "step": 13, "action": "TestCaseGenerator.generateIntentTests + generateConversations" },
      { "step": 14, "action": "Map to DfAgent + DfIntent + DfEntity DB schemas" },
      { "step": 15, "action": "Store as AgentArchitectVersion(state=VALIDATED)" }
    ],
    "output_schema_mapping": {
      "agentMeta": "DfAgent { tenantId, name, description, language, timezone, status:'draft', settings }",
      "intents": "DfIntent[] { tenantId, agentId, name, action, trainingPhrases, parameters, inputContexts, outputContexts, responses, isFallback, priority }",
      "entities": "DfEntity[] { tenantId, agentId, name, type, entries[{value, synonyms}], regexPattern }"
    }
  },

  "MASTER_AGENT_GENERATION_FLOW": {
    "trigger": "mode = CREATE_MASTER_AGENT",
    "pipeline": "masterAgentPipeline.js",
    "stages": [
      { "step": 1, "action": "RequestInterpreter identifies child agents list from prompt" },
      { "step": 2, "action": "PromptEnhancer expands to MasterEnhancedSpec: child domains, routing requirements, dependency model, conflict zones" },
      { "step": 3, "action": "For each child agent: verify it exists in DB OR mark as to-be-created (CREATE_CHILD_AGENT job per child)" },
      { "step": 4, "action": "RoutingRuleGenerator produces: keyword rules per child (P=100-85), conflict rules (P=80-75), dependency block rules (P=110), explicit switch rules (P=105), clarification fallback (P=10)" },
      { "step": 5, "action": "NLU Routing Intent Generator: for each child produces 5-7 routing intents, each with 20-25 training phrases, action=childAlias, inputContexts=[]" },
      { "step": 6, "action": "SessionStateModel generator: defines active_child_agent, child_agent_status_map, dependency_status, pending_task_queue fields" },
      { "step": 7, "action": "DependencyRuleGenerator: identifies hard/soft deps, produces dependency_chain_for_ordering" },
      { "step": 8, "action": "ConflictResolutionGenerator: identifies shared ambiguous keywords across children, produces P=75-80 conflict rules" },
      { "step": 9, "action": "ValidationEngine checks: routing coverage, conflict rule completeness, NLU intent coverage" },
      { "step": 10, "action": "QualityScorer scores routing quality, disambiguation quality" },
      { "step": 11, "action": "If quality < 70: RefinementEngine improves weak routing sections" },
      { "step": 12, "action": "TestCaseGenerator produces routing test conversations (same schema as wa-masterConversationSuite)" },
      { "step": 13, "action": "Map to MasterAgent + MasterAgentChild + MasterRouteRule + WABF-pattern NLU routing DfAgent schemas" },
      { "step": 14, "action": "Store as AgentArchitectVersion(state=VALIDATED)" }
    ],
    "on_approve": [
      "Create MasterAgent document",
      "Create MasterAgentChild documents",
      "Create MasterRouteRule documents (bulk insert)",
      "Create routing DfAgent + DfIntents (using seedWABFRoutingAgent pattern)",
      "Publish routing DfAgent (generates USE embeddings)",
      "Set master.nluAgentId = routing agent ID",
      "Set master.settings.intentThreshold = 0.50"
    ]
  },

  "INTENT_AND_ENTITY_GENERATION_FLOW": {
    "intent_generation_rules": [
      "Each intent name follows convention: verb_noun (e.g., start_onboarding, check_eligibility)",
      "Training phrases must cover: formal phrasing, casual phrasing, short phrasing, multi-word paraphrase, typo variant (1 per intent), Hindi/regional variant if domain requires",
      "No two intents in same agent may share training phrases verbatim",
      "Fallback intent always named 'default_fallback' or '[domain]_fallback'",
      "Escalation intent named 'escalate_to_human' or '[domain]_escalate'",
      "inputContexts used for context-gated intents (slot-filling continuation, mid-flow eligibility checks)",
      "outputContexts used to pass context to next intent in flow (lifespan 3-5 for slot sequences)"
    ],
    "entity_generation_rules": [
      "System entities (sys.city, sys.date, sys.phone, sys.number, sys.email) — create DfEntity records with type:'system', entries:[] to register them in snapshot",
      "Custom list entities — generate 5-10 synonym entries per value",
      "Custom regex entities — generate regex pattern + test examples",
      "Entity names follow convention: @entity_name (e.g., @onboarding_method, @template_category)",
      "Every intent parameter must map to exactly one entity"
    ]
  },

  "ROUTING_RULE_GENERATION_FLOW": {
    "priority_tiers_generated": [
      { "tier": "P=115", "type": "troubleshoot_override",    "description": "Domain-specific error phrases that must override everything" },
      { "tier": "P=110", "type": "dependency_block",         "description": "Block target child if prerequisite not met" },
      { "tier": "P=105", "type": "explicit_switch",          "description": "User explicitly says 'go to X', 'switch to X'" },
      { "tier": "P=100-85","type": "domain_keyword",          "description": "Core keywords per child, highest-priority child first" },
      { "tier": "P=85",  "type": "active_child_continuation","description": "One rule per child for mid-flow continuation" },
      { "tier": "P=80-75","type": "conflict_resolution",      "description": "Shared ambiguous keyword inside specific child context" },
      { "tier": "P=65-60","type": "domain_troubleshoot",      "description": "Issue/error language per domain" },
      { "tier": "P=10",  "type": "clarification_fallback",   "description": "Minimal clarification when nothing matches" }
    ],
    "nlu_routing_intent_generation": {
      "per_child_intent_count": "5-7 intents",
      "phrases_per_intent": "20-25",
      "naming_convention": "route.[domain].[sub_intent] (e.g., route.wabf.template.start)",
      "action_field": "exact child alias string",
      "inputContexts": "always [] — masterNlu filters to global only"
    }
  },

  "VALIDATION_ENGINE_DESIGN": {
    "file": "modules/agentArchitect/services/validationEngine.service.js",
    "method": "runChecks(config: AgentConfig | MasterAgentConfig) → ValidationReport",
    "programmatic_checks": ["V-01", "V-02", "V-03", "V-04", "V-07", "V-08", "V-12"],
    "ai_assisted_checks": ["V-05 (semantic overlap)", "V-06 (missing escalation heuristic)", "V-09", "V-10", "V-11"],
    "report_structure": {
      "errors": "Array<{ check_id, message, affected_item, fix_suggestion }>",
      "warnings": "Array<{ check_id, message, affected_item, fix_suggestion }>",
      "passed": "Array<check_id>",
      "summary": "{ error_count, warning_count, pass_rate }"
    }
  },

  "QUALITY_SCORING_ENGINE": {
    "method": "score(config, validationReport) → QualityScore { composite, breakdown, grade, recommendation }",
    "grades": {
      "A": "85-100 — auto-approve eligible",
      "B": "70-84 — approve with minor warnings",
      "C": "55-69 — auto-refine triggered",
      "D": "below 55 — requires human review, major issues"
    },
    "composite_formula": "weighted_sum(dimension_scores) where weights sum to 100"
  },

  "REFINEMENT_LOOP_DESIGN": {
    "trigger_conditions": ["quality_score < 70", "ValidationReport.errors.length > 0"],
    "max_iterations": 3,
    "per_iteration": [
      "Build refinement prompt: include ValidationReport errors + current config",
      "Call REFINE_WEAK_AREAS prompt → get updated sections",
      "Merge updated sections into config (additive merge — never delete user-approved items)",
      "Re-run ValidationEngine",
      "Re-score with QualityScorer",
      "If score improved AND >= 70: stop refining",
      "If max_iterations reached: mark remaining errors as NEEDS_HUMAN_REVIEW"
    ],
    "change_tracking": "Each refinement iteration stores a diff (added_intents, modified_intents, added_entities, modified_rules) in AgentArchitectJob.refinement_history"
  },

  "VERSIONING_AND_PREVIEW_FLOW": {
    "version_model": "AgentArchitectVersion",
    "version_states": ["DRAFT", "REFINED", "VALIDATED", "HUMAN_APPROVED", "SAVED_TO_PLATFORM"],
    "preview_api": "GET /agent-architect/jobs/:jobId/preview → returns FinalConfig formatted for UI display",
    "diff_api": "GET /agent-architect/jobs/:jobId/diff?from=v1&to=v2 → returns change summary",
    "rollback": "POST /agent-architect/jobs/:jobId/rollback/:versionId → restore any previous version",
    "history": "GET /agent-architect/jobs/:jobId/history → all versions with timestamps and scores"
  },

  "EXISTING_AGENT_IMPROVEMENT_FLOW": {
    "trigger": "mode = IMPROVE_CHILD_AGENT or IMPROVE_MASTER_AGENT",
    "steps": [
      "1. Load existing DfAgent + DfIntents + DfEntities from DB → serialize to AgentConfig",
      "2. Load user improvement request",
      "3. Build IMPROVE prompt: existing config + improvement request + ValidationReport of current agent",
      "4. AI returns: { add_intents[], modify_intents[], add_entities[], modify_rules[], reasoning }",
      "5. ValidationEngine validates the proposed changes (do not break existing valid items)",
      "6. QualityScorer scores new combined config",
      "7. Store as new AgentArchitectVersion (improvement type)",
      "8. Show diff to user: added/modified items highlighted",
      "9. On approval: update DfIntent/DfEntity records, re-publish agent"
    ],
    "safety_rule": "Never auto-delete existing intents or entities during improvement. Only ADD or MODIFY. Deletion requires explicit user confirmation."
  },

  "DATA_MODELS_AND_STORAGE_REQUIREMENTS": {
    "AgentArchitectJob": {
      "collection": "agent_architect_jobs",
      "fields": {
        "_id": "ObjectId",
        "tenantId": "String",
        "userId": "String",
        "mode": "enum: PipelineMode",
        "status": "enum: QUEUED|RUNNING|STAGE_N|COMPLETED|FAILED|AWAITING_REVIEW",
        "current_stage": "Number 1-8",
        "prompt": "String — original user input",
        "request_spec": "Object — output of Stage 1",
        "enhanced_spec": "Object — output of Stage 2",
        "architecture_plan": "Object — output of Stage 3",
        "draft_config": "Object — output of Stage 4",
        "validation_report": "Object — output of Stage 5",
        "refined_config": "Object — output of Stage 6",
        "test_pack": "Object — output of Stage 7",
        "final_config": "Object — output of Stage 8",
        "quality_score": "Object — QualityScore",
        "refinement_history": "Array<{ iteration, changes, score_before, score_after }>",
        "saved_agent_id": "ObjectId — set after SAVE",
        "saved_master_agent_id": "ObjectId — set after SAVE",
        "error_log": "Array<{ stage, message, timestamp }>",
        "createdAt": "Date",
        "updatedAt": "Date"
      }
    },
    "AgentArchitectVersion": {
      "collection": "agent_architect_versions",
      "fields": {
        "_id": "ObjectId",
        "jobId": "ObjectId",
        "tenantId": "String",
        "version_number": "Number",
        "state": "enum: DRAFT|REFINED|VALIDATED|HUMAN_APPROVED|SAVED_TO_PLATFORM",
        "config": "Object — full AgentConfig or MasterAgentConfig at this version",
        "quality_score": "Object",
        "validation_report": "Object",
        "change_summary": "String",
        "created_by_stage": "Number",
        "createdAt": "Date"
      }
    },
    "AgentArchitectCallLog": {
      "collection": "agent_architect_call_logs",
      "fields": {
        "jobId": "ObjectId",
        "stage": "Number",
        "prompt_template_key": "String",
        "prompt_hash": "String — SHA256 of prompt (not raw for security)",
        "response_hash": "String",
        "model": "String",
        "prompt_tokens": "Number",
        "completion_tokens": "Number",
        "cost_usd": "Number",
        "latency_ms": "Number",
        "success": "Boolean",
        "error": "String?",
        "createdAt": "Date"
      }
    },
    "AgentArchitectFeedback": {
      "collection": "agent_architect_feedback",
      "fields": {
        "jobId": "ObjectId",
        "tenantId": "String",
        "domain": "String",
        "section": "String",
        "action": "enum: ACCEPTED|REJECTED|EDITED",
        "original_value": "Object",
        "edited_value": "Object?",
        "createdAt": "Date"
      }
    }
  },

  "BACKGROUND_JOB_ORCHESTRATION": {
    "queue_library": "Bull (Redis-backed) or internal async job queue",
    "job_name": "agent-architect-pipeline",
    "queue_config": { "concurrency": 3, "attempts": 2, "backoff": 5000 },
    "stage_execution": "Each stage runs sequentially within job. Stage output stored to AgentArchitectJob document after completion.",
    "progress_events": "Job emits progress events: { stage, stage_name, percent_complete } — pushed via SSE or WebSocket to UI",
    "recovery": "If job crashes mid-stage, resume from last completed stage using stored intermediate data",
    "timeout_per_stage_ms": 60000,
    "api_flow": {
      "POST_generate": "Creates AgentArchitectJob, enqueues job, returns { jobId, status: QUEUED }",
      "GET_status": "Returns { status, current_stage, stage_name, percent_complete }",
      "GET_preview": "Available after stage 8. Returns FinalConfig preview.",
      "POST_approve": "Triggers save flow: creates DfAgent/DfIntent/DfEntity/MasterAgent from FinalConfig"
    }
  },

  "FAILSAFE_AND_ERROR_HANDLING": {
    "model_unavailable": "Skip AI-assisted stages. Run programmatic checks only. Mark AI-dependent outputs as NEEDS_HUMAN_REVIEW.",
    "model_returns_invalid_json": "Retry up to 3 times. On third failure: use last valid partial output + mark section as NEEDS_HUMAN_REVIEW.",
    "model_timeout": "Treat as failure. Retry with reduced max_tokens. If still fails: fallback to domain template library.",
    "validation_all_errors": "If refinement hits max_iterations and errors remain: store as PARTIALLY_VALIDATED. Allow user to manually fix flagged items.",
    "save_failure": "If saving to platform fails (DB error): store FinalConfig in AgentArchitectVersion. User can retry save without regenerating.",
    "partial_output_policy": "Never discard partial output. Store every completed stage even if pipeline fails. Allow resume.",
    "domain_template_library": {
      "description": "Built-in minimal templates for common domains as last-resort fallback",
      "domains": ["whatsapp_onboarding", "template_creation", "campaign_management", "chatbot_builder", "customer_support", "booking", "e-commerce"],
      "file": "modules/agentArchitect/templates/domainTemplates.js"
    }
  },

  "SECURITY_AND_ACCESS_CONTROL": {
    "api_key_storage": "Environment variables only (AI_ARCHITECT_API_KEY). Never in DB, never in logs.",
    "prompt_logging": "Log prompt_hash (SHA256) only, not raw prompt content. Raw content available only in debug mode for admins.",
    "role_based_access": {
      "generate": "requires role: agent_architect_user or admin",
      "approve_save": "requires role: agent_architect_approver or admin",
      "view_logs": "requires role: admin",
      "configure_model": "requires role: admin"
    },
    "audit_log": "Every generation job, approval, and save action logged to audit_logs collection with userId, tenantId, timestamp, action.",
    "tenant_isolation": "All generated configs tagged with tenantId. Cross-tenant access blocked at query level.",
    "generated_config_safety": "Generated configs validated against schema before save. No arbitrary code injection possible — configs are data structures, not executable code."
  },

  "API_ENDPOINT_DESIGN": {
    "base_path": "/agent-architect",
    "endpoints": [
      {
        "method": "POST",
        "path": "/generate",
        "description": "Start a new agent generation job",
        "body": "{ prompt: string, mode?: PipelineMode, existingAgentId?: string, options?: { quickMode: boolean, autoApprove: boolean } }",
        "response": "{ jobId, status: 'QUEUED', estimatedDurationMs }"
      },
      {
        "method": "GET",
        "path": "/jobs/:jobId/status",
        "description": "Poll generation job progress",
        "response": "{ jobId, status, current_stage, stage_name, percent_complete, quality_score?, error? }"
      },
      {
        "method": "GET",
        "path": "/jobs/:jobId/preview",
        "description": "Get full preview of generated agent config",
        "response": "{ jobId, finalConfig, qualityScore, validationReport, testPack }"
      },
      {
        "method": "POST",
        "path": "/jobs/:jobId/approve",
        "description": "Approve and save generated config to platform",
        "body": "{ section_overrides?: object }",
        "response": "{ savedAgentId?, savedMasterAgentId?, status: 'SAVED' }"
      },
      {
        "method": "POST",
        "path": "/jobs/:jobId/regenerate-section",
        "description": "Regenerate a specific section of the config",
        "body": "{ section: 'intents'|'entities'|'routing'|'tests', feedback?: string }",
        "response": "{ jobId, section, newContent, quality_delta }"
      },
      {
        "method": "POST",
        "path": "/improve",
        "description": "Improve an existing agent",
        "body": "{ agentId: string, improvementRequest: string, mode: 'IMPROVE_CHILD_AGENT'|'IMPROVE_MASTER_AGENT' }",
        "response": "{ jobId, status: 'QUEUED' }"
      },
      {
        "method": "GET",
        "path": "/jobs/:jobId/history",
        "description": "Get all versions of a generation job",
        "response": "Array<AgentArchitectVersion>"
      },
      {
        "method": "POST",
        "path": "/jobs/:jobId/rollback/:versionId",
        "description": "Rollback to a previous version",
        "response": "{ status: 'ROLLED_BACK', activeVersionId }"
      },
      {
        "method": "GET",
        "path": "/admin/config",
        "description": "Get current model connector configuration (admin only)",
        "response": "{ provider, model, intentThreshold, features }"
      },
      {
        "method": "PUT",
        "path": "/admin/config",
        "description": "Update model connector configuration (admin only)",
        "body": "{ provider?, model?, intentThreshold?, features? }"
      }
    ]
  },

  "ADMIN_CONFIGURATION_REQUIREMENTS": {
    "env_variables": {
      "AI_ARCHITECT_PROVIDER": "openai | anthropic | azure-openai | custom",
      "AI_ARCHITECT_API_KEY": "API key for the provider",
      "AI_ARCHITECT_MODEL": "e.g., gpt-4o, claude-opus-4-6",
      "AI_ARCHITECT_BASE_URL": "Optional — for custom or Azure endpoints",
      "AI_ARCHITECT_MAX_TOKENS_DEFAULT": "Default 4000",
      "AI_ARCHITECT_QUALITY_THRESHOLD": "Default 70 — below this triggers auto-refinement",
      "AI_ARCHITECT_AUTO_APPROVE_THRESHOLD": "Default 85 — above this skip human review",
      "AI_ARCHITECT_MAX_REFINEMENT_ITERATIONS": "Default 3",
      "AI_ARCHITECT_QUEUE_CONCURRENCY": "Default 3 parallel jobs"
    },
    "db_config_collection": "agent_architect_admin_config (per-tenant overrides)",
    "feature_flags": {
      "ENABLE_AUTO_APPROVE": "Skip human review if score >= AUTO_APPROVE_THRESHOLD",
      "ENABLE_TEST_GENERATION": "Run Stage 7 test pack generation",
      "ENABLE_FEEDBACK_LEARNING": "Use approved examples as few-shot in future prompts",
      "ENABLE_COST_ALERTS": "Alert if monthly cost exceeds configured threshold"
    }
  },

  "TESTING_AND_QA_FRAMEWORK": {
    "unit_tests": [
      "requestInterpreter.test.js — classify 20 prompt types",
      "promptEnhancer.test.js — verify expansion includes min intent/entity counts",
      "intentGenerator.test.js — verify phrase count, naming conventions, no duplicates",
      "entityGenerator.test.js — verify synonym count, system entity registration",
      "validationEngine.test.js — all 12 checks with pass/fail fixtures",
      "qualityScorer.test.js — score computation against known configs",
      "refinementEngine.test.js — verify score improves after refinement",
      "modelConnector.test.js — mock provider responses, verify retry logic"
    ],
    "integration_tests": [
      "Full CREATE_CHILD_AGENT pipeline with mock model → verify DB records created correctly",
      "Full CREATE_MASTER_AGENT pipeline with mock model → verify MasterAgent + routing agent seeded",
      "IMPROVE_CHILD_AGENT — feed existing agent, verify additive changes only",
      "Refinement loop — inject known-bad config, verify auto-improve reaches grade B+"
    ],
    "model_output_stability_tests": [
      "Run same prompt 5 times → verify intent names are stable (cosine similarity of naming > 0.85)",
      "Verify generated training phrases are unique across intents",
      "Verify JSON output is always valid (no truncation)"
    ]
  },

  "PERFORMANCE_AND_MONITORING": {
    "target_latencies": {
      "classify_stage": "< 3s",
      "expand_stage": "< 8s",
      "generate_stage": "< 20s",
      "validate_stage": "< 5s",
      "total_pipeline": "< 90s for full 8-stage run"
    },
    "metrics_to_track": [
      "job_completion_rate (target > 95%)",
      "avg_quality_score_at_first_generation",
      "avg_refinement_iterations_needed",
      "auto_approve_rate (score >= 85)",
      "human_reject_rate (user rejects and regenerates)",
      "model_call_cost_per_job_usd",
      "stage_failure_rate per stage"
    ],
    "monitoring": "Expose metrics via GET /agent-architect/admin/metrics. Store daily rollups in agent_architect_metrics collection.",
    "alerting": "Alert if job_completion_rate < 90% over 1h window, or model API error rate > 10%."
  },

  "SAMPLE_INTERNAL_PROMPTS": {
    "EXPAND_REQUIREMENTS_SAMPLE": {
      "system": "You are an expert conversational AI Agent Planner. Expand the user description into a comprehensive requirements specification. Return valid JSON only.",
      "user": "User wants to build: 'WhatsApp onboarding agent for business account setup'",
      "expected_output_structure": {
        "domain": "whatsapp_business_onboarding",
        "purpose": "Guide businesses through WhatsApp Business API registration and account setup",
        "tone": "professional, helpful, step-by-step",
        "likely_intent_groups": [
          "start onboarding fresh",
          "continue existing onboarding",
          "check eligibility / qualification",
          "check readiness / prerequisites",
          "manual setup guidance",
          "embedded/auto signup guidance",
          "account verification status",
          "phone number setup",
          "App ID / Phone Number ID collection",
          "business verification (Meta portfolio)"
        ],
        "likely_entities": ["@onboarding_method (auto/manual)", "@business_type", "@phone_type", "@verification_status"],
        "likely_validations": ["Phone number must not already be on WhatsApp", "Facebook Business Manager required", "Business must not be in prohibited category"],
        "likely_edge_cases": ["User already onboarded asks eligibility mid-flow", "User provides phone number ID instead of phone number", "User abandons mid-flow and resumes"],
        "dependency_hints": ["Onboarding must complete before templates or campaigns"],
        "conversation_patterns": ["slot-fill: method selection → document collection → verification → completion"],
        "fallback_policy": "after 3 failed attempts escalate to human support"
      }
    },
    "GENERATE_FULL_CONFIG_SAMPLE": {
      "system": "You are an Intent Architect. Generate a complete DfAgent configuration. Return valid JSON matching the provided schema exactly.",
      "user": "Generate all intents for domain: whatsapp_business_onboarding with these intent groups: [start_onboarding, continue_onboarding, check_eligibility, check_readiness, manual_setup, auto_setup, verification_status, escalate]. Each intent must have 20+ training phrases covering formal, casual, short, paraphrase, and typo variants.",
      "output_schema": "{ intents: [ { name, description, trainingPhrases: [{text}], parameters: [{name, entityName, required, prompt}], inputContexts, outputContexts, responses: [{type, value}], isFallback, priority } ] }"
    },
    "VALIDATE_AGENT_CONFIG_SAMPLE": {
      "system": "You are a QA Validator for dialogue systems. Review the config and return a validation report.",
      "user": "Review this agent config for: missing intents, weak coverage, overlapping training phrases, missing entities, incomplete responses.",
      "expected_output_structure": {
        "errors": [{ "check_id": "V-04", "message": "Intent 'provide_phone_number_id' references entity '@phone_number_id' which is not defined", "fix_suggestion": "Add DfEntity for @phone_number_id with type:custom" }],
        "warnings": [{ "check_id": "V-03", "message": "Intent 'check_eligibility' has only 8 training phrases (recommended: 20+)", "fix_suggestion": "Add 12 more paraphrase variants" }],
        "passed": ["V-01", "V-02", "V-07"],
        "summary": { "error_count": 1, "warning_count": 1, "pass_rate": 0.75 }
      }
    }
  },

  "SAMPLE_INPUT_OUTPUT_FLOWS": {
    "flow_1": {
      "user_prompt": "Create a WhatsApp onboarding agent",
      "stage_1_output": "{ mode: CREATE_CHILD_AGENT, domain: whatsapp_onboarding, targetType: child, confidence: 0.95 }",
      "stage_2_output": "EnhancedSpec with 10 intent groups, 4 entity types, 3 validations, 4 edge cases",
      "stage_4_output": "10 intents × 22 phrases avg = 220 training phrases total. 4 entities with synonyms.",
      "stage_5_output": "ValidationReport: 0 errors, 2 warnings (V-03 on 2 intents)",
      "stage_6_output": "RefinementEngine adds 14 training phrases to 2 weak intents. Score: 64 → 78.",
      "final_quality_score": "82 (Grade B — approve with minor warnings)",
      "on_approve": "DfAgent created, 10 DfIntents created, 4 DfEntities created, agent published"
    },
    "flow_2": {
      "user_prompt": "Create a master agent for onboarding, templates, campaigns, chatbot",
      "stage_1_output": "{ mode: CREATE_MASTER_AGENT, domain: whatsapp_business_suite, children: [onboarding, template, campaign, chatbot] }",
      "stage_3_output": "Architecture: 4 children, dependency chain onboarding→template→campaign, conflict zones: template/campaign, keyword/chatbot",
      "stage_4_output": "22 keyword routing rules across 9 priority tiers. 27 NLU routing intents (27 × 21 phrases = 567 total).",
      "final_quality_score": "88 (Grade A — auto-approve eligible)",
      "on_approve": "MasterAgent created, 4 MasterAgentChild records, 22 MasterRouteRules, WABF routing DfAgent seeded, nluAgentId linked"
    }
  },

  "IMPLEMENTATION_PHASE_PLAN": {
    "phase_1": {
      "name": "Core Infrastructure",
      "duration_estimate": "1-2 weeks",
      "deliverables": [
        "AgentArchitectJob + AgentArchitectVersion + AgentArchitectCallLog models",
        "modelConnector.service.js with OpenAI/Anthropic support",
        "promptEngine.service.js with all 12 prompt templates",
        "requestInterpreter.service.js",
        "Background job queue setup",
        "Basic API endpoints: /generate, /jobs/:id/status"
      ]
    },
    "phase_2": {
      "name": "Child Agent Generation Pipeline",
      "duration_estimate": "1-2 weeks",
      "deliverables": [
        "promptEnhancer.service.js",
        "intentGenerator.service.js",
        "entityGenerator.service.js",
        "childAgentPipeline.js (all 15 steps)",
        "validationEngine.service.js (all 12 checks)",
        "qualityScorer.service.js",
        "API endpoints: /jobs/:id/preview, /jobs/:id/approve"
      ]
    },
    "phase_3": {
      "name": "Master Agent Generation Pipeline",
      "duration_estimate": "1-2 weeks",
      "deliverables": [
        "routingRuleGenerator.service.js",
        "masterAgentPipeline.js (all 14 steps)",
        "NLU routing intent generator",
        "Dependency rule generator",
        "Conflict resolution rule generator",
        "Auto-seed routing DfAgent on approve"
      ]
    },
    "phase_4": {
      "name": "Refinement + Test Generation",
      "duration_estimate": "1 week",
      "deliverables": [
        "refinementEngine.service.js",
        "testCaseGenerator.service.js",
        "sampleConversationEngine (integrated with testCaseGenerator)",
        "API endpoints: /jobs/:id/regenerate-section"
      ]
    },
    "phase_5": {
      "name": "Improvement Mode + Versioning + Human Review",
      "duration_estimate": "1 week",
      "deliverables": [
        "versioningLayer.service.js",
        "humanReview.service.js",
        "improveExistingAgent pipeline",
        "API endpoints: /improve, /jobs/:id/history, /jobs/:id/rollback/:versionId"
      ]
    },
    "phase_6": {
      "name": "Feedback Learning + Admin + Monitoring",
      "duration_estimate": "1 week",
      "deliverables": [
        "feedbackLearning.service.js",
        "Admin config endpoints",
        "Metrics collection + monitoring",
        "Domain template library (fallback)",
        "Full test suite (unit + integration)"
      ]
    }
  }
}
