{
  "$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": "^(?!(daggry|coverage|no_regression|baseline_proof)$)[a-z][a-z0-9_-]*$",
      "description": "project, or the name of a validator you define. project, daggry, coverage, no_regression and baseline_proof are reserved."
    },
    "judgeRef": {
      "type": "string",
      "pattern": "^(?!(project|daggry|baseline_proof)$)[a-z][a-z0-9_-]*$",
      "description": "coverage, no_regression, or the name of a judge you declare."
    },
    "personaRef": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_-]*$",
      "description": "The name of a persona that the project config or the tenant config defines."
    },
    "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/commandStep"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "agent"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/agentStep"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "verify"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/verifyStep"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "human"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/humanStep"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "effect"
              }
            }
          },
          "then": {
            "$ref": "#/$defs/effectStep"
          }
        }
      ]
    },
    "loopEntry": {
      "type": "object",
      "required": [
        "id",
        "loop"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "loop": {
          "type": "object",
          "required": [
            "until",
            "max",
            "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
            },
            "steps": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/step"
              }
            }
          },
          "dependentRequired": {
            "over": [
              "as"
            ],
            "as": [
              "over"
            ]
          }
        }
      }
    },
    "commandStep": {
      "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": {
          "$ref": "#/$defs/command"
        },
        "scope": {
          "$ref": "#/$defs/scope"
        }
      }
    },
    "agentStep": {
      "required": [
        "id",
        "kind",
        "persona",
        "outputs",
        "scope"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "persona": {
          "$ref": "#/$defs/personaRef"
        },
        "outputs": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "freeze": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "scope": {
          "$ref": "#/$defs/scope"
        }
      }
    },
    "verifyStep": {
      "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"
          }
        },
        "judges": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/judgeRef"
          },
          "description": "Judges to run after the default judge. Absent: only the default judge. The order is the order of the verdicts."
        }
      }
    },
    "humanStep": {
      "required": [
        "id",
        "kind",
        "of"
      ],
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": true,
        "of": {
          "$ref": "#/$defs/id",
          "description": "The step or loop this checkpoint is about. A yes continues the run. Any other reply sends the run back to it with that reply."
        }
      }
    },
    "effectStep": {
      "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. Daggry ships open-pr and merge-pr. A tenant can register more."
        },
        "auto_when": {
          "type": "string",
          "minLength": 1,
          "description": "A gate expression. When it holds, the effect runs with no person. When it does not, the run waits for a person's approval first."
        }
      }
    },
    "command": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      },
      "description": "A program and its arguments. The first string is the program. No shell reads the list."
    }
  }
}
