{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/mmerterden/multi-agent-pipeline/pipeline/schemas/phases.schema.json",
  "title": "Phase contract",
  "description": "phases.json: the phase set, the modes that run a subset of it and the phase numbers other code compares against.",
  "type": "object",
  "additionalProperties": false,
  "required": ["contractVersion", "phaseSchema", "phases", "totalMaxTokens", "modes", "thresholds"],
  "properties": {
    "$schema": { "type": "string" },
    "$id": { "type": "string" },
    "description": { "type": "string" },
    "contractVersion": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$" },
    "phaseSchema": { "type": "integer", "minimum": 1 },
    "phaseSchemaNote": { "type": "string" },
    "legacyPhaseNames": {
      "type": "object",
      "properties": { "note": { "type": "string" } },
      "patternProperties": { "^[0-9]+$": { "type": "string" } },
      "additionalProperties": false
    },
    "phases": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "name", "doc", "maxTokens"],
        "properties": {
          "id": { "type": "integer", "minimum": 0 },
          "name": { "type": "string", "minLength": 1 },
          "doc": { "type": "string", "pattern": "^phase-[0-9]+-[a-z-]+\\.md$" },
          "maxTokens": { "type": "integer", "minimum": 1 },
          "was": { "type": "array", "items": { "type": "integer", "minimum": 0 } }
        }
      }
    },
    "totalMaxTokens": { "type": "integer", "minimum": 1 },
    "modes": {
      "type": "object",
      "minProperties": 1,
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "required": ["phases", "autopilot"],
        "properties": {
          "phases": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "integer", "minimum": 0 },
            "uniqueItems": true
          },
          "autopilot": { "type": "boolean" }
        }
      }
    },
    "thresholds": {
      "type": "object",
      "additionalProperties": false,
      "required": ["waitingFromPhase", "mcpAllowedThroughPhase"],
      "properties": {
        "waitingFromPhase": { "type": "integer", "minimum": 0 },
        "mcpAllowedThroughPhase": { "type": "integer", "minimum": 0 },
        "note": { "type": "string" }
      }
    }
  }
}
