{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dunscaith.phpboyscout.uk/reference/job-file.v1.schema.json",
  "title": "dunscaith job file, version 1",
  "description": "The file dunscaith submit and dunscaith validate read (dunscaith spec 0002). The decoder is the authority; this schema is for editors and yaml-language-server.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "command", "nodes", "gpusPerNode"],
  "properties": {
    "version": {"const": 1},
    "name": {"type": "string", "pattern": "^[^\\x00]*$"},
    "command": {
      "type": "array",
      "minItems": 1,
      "prefixItems": [{"type": "string", "minLength": 1}],
      "items": {"type": "string", "pattern": "^[^\\x00]*$"}
    },
    "env": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
        "not": {"anyOf": [{"pattern": "^DISPATCH_"}, {"const": "DUNSCAITH_S3_CREDS"}]}
      },
      "additionalProperties": {"type": "string", "pattern": "^[^\\x00]*$"}
    },
    "workDir": {"$ref": "#/$defs/path"},
    "nodes": {"type": "integer", "minimum": 1},
    "gpusPerNode": {"type": "integer", "minimum": 0},
    "procsPerNode": {"type": "integer", "minimum": 0},
    "timeLimit": {"$ref": "#/$defs/positiveDuration"},
    "checkpoint": {
      "type": "string",
      "anyOf": [
        {"pattern": "^/[^\\x00]*$", "not": {"$ref": "#/$defs/dotSegment"}},
        {"pattern": "^s3://[^/?#@%\\x00]+(/([^?#%\\x00]|%[0-9A-Fa-f]{2})*)?$"}
      ]
    },
    "recovery": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "rules": {"type": "array", "items": {"$ref": "#/$defs/rule"}}
      }
    },
    "extensions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "dispatch.Image": {
          "type": "object",
          "additionalProperties": false,
          "properties": {"ref": {"type": "string", "pattern": "^[^\\x00]*$"}}
        },
        "dispatch.Output": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "stdout": {"$ref": "#/$defs/path"},
            "stderr": {"$ref": "#/$defs/path"}
          }
        },
        "dispatch.SharedCheckpoint": {
          "type": "object",
          "additionalProperties": false
        },
        "kubeflow.RuntimeRef": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "kind": {"type": "string"},
            "name": {"type": "string"}
          }
        },
        "slurm.Placement": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "partition": {"type": "string"},
            "account": {"type": "string"},
            "qos": {"type": "string"},
            "reservation": {"type": "string"},
            "constraint": {"type": "string"}
          }
        },
        "slurm.Resources": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "gpuType": {"type": "string"},
            "cpusPerTask": {"type": "integer"},
            "memPerNodeMiB": {"type": "integer"},
            "exclusive": {"type": "boolean"}
          }
        }
      }
    }
  },
  "$defs": {
    "path": {
      "description": "An absolute path, or one under $HOME/, which the adapter resolves on the cluster, in its plain form.",
      "type": "string",
      "pattern": "^(/|\\$HOME/)[^\\x00]*$",
      "not": {"$ref": "#/$defs/dotSegment"}
    },
    "dotSegment": {
      "description": "A . or .. path segment, which a path in its plain form never has.",
      "pattern": "(^|/)\\.\\.?(/|$)"
    },
    "duration": {
      "description": "A Go duration string such as 90m, 12h or 36h30m; units ns, us, ms, s, m, h.",
      "type": "string",
      "pattern": "^\\+?(([0-9]+(\\.[0-9]*)?|\\.[0-9]+)(ns|us|µs|μs|ms|s|m|h))+$"
    },
    "positiveDuration": {
      "allOf": [{"$ref": "#/$defs/duration"}],
      "not": {"pattern": "^\\+?(([0.]+)(ns|us|µs|μs|ms|s|m|h))+$"}
    },
    "cause": {
      "enum": ["preempted", "node-lost", "out-of-memory", "time-limit", "non-zero-exit", "launch-failed", "no-capacity", "held", "released", "requeued", "unknown"]
    },
    "rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["on", "action"],
      "properties": {
        "on": {"type": "array", "minItems": 1, "uniqueItems": true, "items": {"$ref": "#/$defs/cause"}},
        "action": {"enum": ["resubmit", "restart", "requeue", "hold", "escalate"]},
        "budget": {"type": "integer", "minimum": 1},
        "for": {"$ref": "#/$defs/positiveDuration"}
      },
      "allOf": [
        {
          "if": {"properties": {"action": {"enum": ["resubmit", "restart", "requeue"]}}},
          "then": {
            "required": ["budget"],
            "not": {"required": ["for"]},
            "properties": {"on": {"items": {"not": {"const": "unknown"}}}}
          }
        },
        {
          "if": {"properties": {"action": {"const": "hold"}}},
          "then": {"required": ["for"], "not": {"required": ["budget"]}}
        },
        {
          "if": {"properties": {"action": {"const": "escalate"}}},
          "then": {"not": {"anyOf": [{"required": ["budget"]}, {"required": ["for"]}]}}
        }
      ]
    }
  }
}
