{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Infinite Loop Configuration Schema",
  "description": "Configuration schema for configuration-driven infinite loop orchestration",
  "type": "object",
  "required": ["version", "orchestration", "generation"],

  "properties": {
    "version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$",
      "description": "Configuration schema version"
    },

    "profile": {
      "type": "string",
      "enum": ["development", "production", "research", "custom"],
      "description": "Active configuration profile"
    },

    "orchestration": {
      "type": "object",
      "required": ["max_parallel_agents", "batch_size"],
      "properties": {
        "max_parallel_agents": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10,
          "description": "Maximum number of parallel sub-agents"
        },
        "batch_size": {
          "type": "integer",
          "minimum": 1,
          "maximum": 50,
          "description": "Items per generation batch"
        },
        "infinite_mode_wave_size": {
          "type": "integer",
          "minimum": 1,
          "maximum": 20,
          "description": "Iterations per wave in infinite mode"
        },
        "context_budget_per_agent": {
          "type": "integer",
          "minimum": 10000,
          "maximum": 200000,
          "description": "Token budget per sub-agent"
        },
        "agent_timeout_ms": {
          "type": "integer",
          "minimum": 30000,
          "maximum": 600000,
          "description": "Timeout for agent execution (milliseconds)"
        },
        "enable_progressive_sophistication": {
          "type": "boolean",
          "description": "Enable increasing sophistication in infinite mode"
        }
      }
    },

    "generation": {
      "type": "object",
      "required": ["output_directory", "naming_pattern"],
      "properties": {
        "output_directory": {
          "type": "string",
          "description": "Base directory for generated outputs"
        },
        "naming_pattern": {
          "type": "string",
          "description": "File naming pattern with placeholders"
        },
        "file_format": {
          "type": "string",
          "enum": ["html", "markdown", "json", "text"],
          "description": "Output file format"
        },
        "include_metadata": {
          "type": "boolean",
          "description": "Include generation metadata in outputs"
        },
        "metadata_format": {
          "type": "string",
          "enum": ["html_comment", "yaml_frontmatter", "json_header"],
          "description": "Format for embedded metadata"
        }
      }
    },

    "quality": {
      "type": "object",
      "properties": {
        "min_uniqueness_threshold": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "description": "Minimum uniqueness score (0-1)"
        },
        "enable_validation": {
          "type": "boolean",
          "description": "Enable output validation"
        },
        "enable_review_stage": {
          "type": "boolean",
          "description": "Enable chain prompting review stage"
        },
        "max_retry_attempts": {
          "type": "integer",
          "minimum": 0,
          "maximum": 5,
          "description": "Maximum retries for failed generations"
        },
        "require_spec_compliance_check": {
          "type": "boolean",
          "description": "Require spec compliance validation"
        }
      }
    },

    "web_enhancement": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean",
          "description": "Enable web-enhanced generation"
        },
        "initial_priming_urls": {
          "type": "integer",
          "minimum": 0,
          "maximum": 10,
          "description": "URLs to fetch during initial priming"
        },
        "urls_per_iteration": {
          "type": "integer",
          "minimum": 0,
          "maximum": 5,
          "description": "URLs to fetch per iteration"
        },
        "progressive_difficulty": {
          "type": "boolean",
          "description": "Use progressive URL difficulty"
        },
        "enable_web_search_fallback": {
          "type": "boolean",
          "description": "Fall back to web search when URLs exhausted"
        },
        "cache_web_content": {
          "type": "boolean",
          "description": "Cache fetched web content"
        },
        "web_fetch_timeout_ms": {
          "type": "integer",
          "minimum": 5000,
          "maximum": 60000,
          "description": "Timeout for web fetches (milliseconds)"
        }
      }
    },

    "logging": {
      "type": "object",
      "properties": {
        "level": {
          "type": "string",
          "enum": ["debug", "info", "warn", "error"],
          "description": "Logging level"
        },
        "log_agent_outputs": {
          "type": "boolean",
          "description": "Log sub-agent outputs"
        },
        "log_web_fetches": {
          "type": "boolean",
          "description": "Log web fetch operations"
        },
        "log_config_loading": {
          "type": "boolean",
          "description": "Log configuration loading process"
        },
        "verbose": {
          "type": "boolean",
          "description": "Enable verbose logging"
        }
      }
    },

    "chain_prompting": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean",
          "description": "Enable chain prompting workflow"
        },
        "stages": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Ordered list of workflow stages"
        },
        "enable_self_correction": {
          "type": "boolean",
          "description": "Enable self-correction loops"
        },
        "pass_state_via_xml": {
          "type": "boolean",
          "description": "Use XML tags for state passing"
        }
      }
    },

    "features": {
      "type": "object",
      "properties": {
        "enable_url_strategy": {
          "type": "boolean",
          "description": "Enable URL strategy files"
        },
        "enable_theme_evolution": {
          "type": "boolean",
          "description": "Enable theme evolution across iterations"
        },
        "enable_cross_iteration_learning": {
          "type": "boolean",
          "description": "Enable learning from previous iterations"
        },
        "enable_automatic_indexing": {
          "type": "boolean",
          "description": "Auto-generate index files"
        }
      }
    },

    "limits": {
      "type": "object",
      "properties": {
        "max_iterations": {
          "type": "integer",
          "minimum": 1,
          "description": "Maximum iterations before stopping"
        },
        "max_file_size_kb": {
          "type": "integer",
          "minimum": 10,
          "description": "Maximum size per output file (KB)"
        },
        "max_total_output_mb": {
          "type": "integer",
          "minimum": 1,
          "description": "Maximum total output size (MB)"
        },
        "warn_at_iteration": {
          "type": "integer",
          "minimum": 1,
          "description": "Iteration number to show warning"
        }
      }
    }
  }
}
