{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://launch.go.phpboyscout.uk/schema/launch.schema.json",
  "title": "launch.yaml",
  "description": "go/launch's configuration, spec 0001 D3.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version"],
  "anyOf": [{"required": ["train"]}, {"required": ["serve"]}],
  "properties": {
    "version": {"const": 1},
    "train": {
      "type": "object",
      "additionalProperties": false,
      "required": ["command"],
      "properties": {
        "command": {"$ref": "#/$defs/command"},
        "procsPerNode": {"type": "integer", "minimum": 1},
        "rendezvous": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "port": {"$ref": "#/$defs/portOrZero"},
            "resumePort": {"$ref": "#/$defs/portOrZero"},
            "joinTimeout": {"type": "string"}
          }
        },
        "env": {"$ref": "#/$defs/env"}
      }
    },
    "checkpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": ["root", "framework"],
      "properties": {
        "root": {"type": "string", "minLength": 1},
        "framework": {"enum": ["hf", "dcp"]},
        "resumeFlag": {"type": "string", "minLength": 1},
        "publish": {"type": "string", "minLength": 1},
        "creds": {"type": "string", "minLength": 1}
      }
    },
    "signal": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "grace": {"type": "string"}
      }
    },
    "heartbeat": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "timeout": {"type": "string"},
        "startup": {"type": "string"},
        "ncclTimeout": {"type": "string"},
        "phases": {
          "type": "object",
          "additionalProperties": {"type": "string"}
        }
      }
    },
    "gpu": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "health": {"enum": ["required", "optional", "off"]}
      }
    },
    "serve": {
      "type": "object",
      "additionalProperties": false,
      "required": ["command", "model"],
      "properties": {
        "command": {"$ref": "#/$defs/command"},
        "model": {
          "type": "object",
          "additionalProperties": false,
          "required": ["name", "path"],
          "properties": {
            "name": {"type": "string", "minLength": 1},
            "path": {"type": "string", "minLength": 1}
          }
        },
        "adapters": {
          "type": "object",
          "additionalProperties": {"type": "string", "minLength": 1}
        },
        "lora": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "runtime": {"type": "boolean"},
            "maxLoras": {"type": "integer", "minimum": 1},
            "maxLoraRank": {"enum": [1, 8, 16, 32, 64, 128, 256, 320, 512]},
            "maxCpuLoras": {"type": "integer", "minimum": 1}
          }
        },
        "env": {"$ref": "#/$defs/env"},
        "port": {"type": "integer", "minimum": 1, "maximum": 65535},
        "ready": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "timeout": {"type": "string"},
            "interval": {"type": "string"}
          }
        },
        "stopGrace": {"type": "string"}
      }
    }
  },
  "$defs": {
    "command": {
      "type": "array",
      "minItems": 1,
      "items": {"type": "string", "minLength": 1}
    },
    "env": {
      "type": "object",
      "additionalProperties": {"type": "string"}
    },
    "portOrZero": {"type": "integer", "minimum": 0, "maximum": 65535}
  }
}
