{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "daggry/workflow@1",
  "title": "daggry/workflow@1: the workflow file",
  "type": "object",
  "required": [
    "format",
    "steps"
  ],
  "additionalProperties": false,
  "patternProperties": {
    "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
  },
  "properties": {
    "format": {
      "const": "daggry/workflow@1"
    },
    "steps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/entry"
      }
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "minLength": 1
    },
    "validatorRef": {
      "type": "string",
      "pattern": "^(project|[A-Za-z0-9_-]+/[a-z][a-z0-9_-]*)$",
      "description": "project, or daggry/<name>, tenant/<name>, project/<name> or <namespace>/<name>."
    },
    "scopeEntry": {
      "type": "string",
      "minLength": 1,
      "description": "A glob, or an area reference of the form area:<name>.",
      "anyOf": [
        {
          "pattern": "^area:[a-z][a-z0-9_-]*$"
        },
        {
          "not": {
            "pattern": "^area:"
          }
        }
      ]
    },
    "scope": {
      "anyOf": [
        {
          "enum": [
            "nothing",
            "anywhere"
          ]
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/scopeEntry"
          }
        }
      ]
    },
    "entry": {
      "type": "object",
      "if": {
        "required": [
          "kind"
        ]
      },
      "then": {
        "$ref": "#/$defs/step"
      },
      "else": {
        "$ref": "#/$defs/loopEntry"
      }
    },
    "step": {
      "type": "object",
      "required": [
        "id",
        "kind"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": {
          "anyOf": [
            {
              "enum": [
                "command",
                "agent",
                "verify",
                "human",
                "effect"
              ]
            },
            {
              "type": "string",
              "pattern": "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$",
              "description": "An extension kind that the tenant registers. Its registered schema defines the step's other keys."
            }
          ]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "command"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/command"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "agent"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/agent"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "verify"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/verify"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "human"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/human"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "effect"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/effect"
          }
        }
      ]
    },
    "command": {
      "required": [
        "id",
        "kind",
        "run",
        "scope"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "run": {
          "type": "string",
          "minLength": 1
        },
        "scope": {
          "$ref": "#/$defs/scope"
        }
      }
    },
    "agent": {
      "required": [
        "id",
        "kind",
        "engine",
        "outputs",
        "scope"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "engine": {
          "type": "string",
          "minLength": 1,
          "description": "The identifier of an agent engine that the tenant registers."
        },
        "prompt": {
          "type": "string"
        },
        "transcript": {
          "type": "string",
          "minLength": 1
        },
        "outputs": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "freeze": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "tests": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "scope": {
          "$ref": "#/$defs/scope"
        }
      },
      "not": {
        "required": [
          "freeze",
          "tests"
        ]
      }
    },
    "verify": {
      "required": [
        "id",
        "kind",
        "validators"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "validators": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/validatorRef"
          }
        }
      }
    },
    "human": {
      "required": [
        "id",
        "kind",
        "waiting_for"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "waiting_for": {
          "enum": [
            "approval",
            "input-needed"
          ]
        }
      }
    },
    "effect": {
      "required": [
        "id",
        "kind",
        "do"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "do": {
          "type": "string",
          "pattern": "^([A-Za-z0-9_-]+/)?[a-z][a-z0-9_-]*$",
          "description": "The name of an effect that the tenant registers."
        }
      }
    },
    "loopEntry": {
      "type": "object",
      "required": [
        "id",
        "loop"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "loop": {
          "type": "object",
          "required": [
            "over",
            "as",
            "until",
            "max",
            "on_exhausted",
            "steps"
          ],
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "over": {
              "$ref": "#/$defs/id"
            },
            "as": {
              "$ref": "#/$defs/id"
            },
            "until": {
              "type": "string",
              "minLength": 1
            },
            "max": {
              "type": "integer",
              "minimum": 1
            },
            "on_exhausted": {
              "$ref": "#/$defs/id"
            },
            "steps": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/step"
              }
            }
          }
        }
      }
    }
  }
}
