{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "daggry/tenant@1",
  "title": "daggry/tenant@1: the tenant config",
  "type": "object",
  "required": [
    "format"
  ],
  "additionalProperties": false,
  "patternProperties": {
    "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
  },
  "properties": {
    "format": {
      "const": "daggry/tenant@1"
    },
    "approvers": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "integer",
        "minimum": 1
      },
      "description": "The numeric GitHub ids of the people who can start, steer and approve runs."
    },
    "workflows": {
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/name"
      },
      "additionalProperties": {
        "type": "object",
        "required": [
          "source"
        ],
        "additionalProperties": false,
        "patternProperties": {
          "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
        },
        "properties": {
          "source": {
            "type": "object",
            "required": [
              "workspace",
              "path",
              "revision"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "workspace": {
                "$ref": "#/$defs/workspace"
              },
              "path": {
                "$ref": "#/$defs/name"
              },
              "revision": {
                "$ref": "#/$defs/name"
              }
            }
          }
        }
      }
    },
    "orchestration": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "concurrency_cap": {
          "type": "integer",
          "minimum": 1
        },
        "lease_seconds": {
          "type": "integer",
          "minimum": 1
        },
        "attempts": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "max": {
              "type": "integer",
              "minimum": 1
            }
          }
        },
        "retry": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "backoff_seconds": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "parking": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "max_seconds": {
              "type": "integer",
              "minimum": 1
            }
          }
        }
      }
    },
    "personas": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[a-z][a-z0-9_-]*$": {
          "$ref": "#/$defs/persona"
        },
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      }
    },
    "agents": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "engines": {
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/name"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "credential"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "credential": {
                "type": "object",
                "required": [
                  "ref"
                ],
                "additionalProperties": false,
                "patternProperties": {
                  "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
                },
                "properties": {
                  "ref": {
                    "$ref": "#/$defs/name"
                  }
                }
              }
            }
          }
        },
        "approval": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "require": {
              "type": "array",
              "items": {
                "enum": [
                  "plan",
                  "effect"
                ]
              }
            }
          }
        }
      }
    },
    "runners": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "images": {
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/name"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "image"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "image": {
                "$ref": "#/$defs/name"
              },
              "tools": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/label"
                }
              },
              "default": {
                "type": "boolean"
              }
            }
          }
        },
        "classes": {
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/name"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "module",
              "network",
              "model_credential"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "module": {
                "enum": [
                  "deterministic-runner",
                  "agent-runner"
                ]
              },
              "network": {
                "enum": [
                  "none",
                  "open"
                ]
              },
              "model_credential": {
                "type": "boolean"
              }
            },
            "if": {
              "properties": {
                "module": {
                  "const": "deterministic-runner"
                }
              }
            },
            "then": {
              "properties": {
                "model_credential": {
                  "const": false
                }
              }
            }
          }
        }
      }
    },
    "validators": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "scripts": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^(?!(project|daggry|coverage|no_regression|baseline_proof)$)[a-z][a-z0-9_-]*$": {
              "$ref": "#/$defs/scriptValidator"
            }
          }
        }
      }
    },
    "judges": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "scripts": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^(?!(project|daggry|coverage|no_regression|baseline_proof)$)[a-z][a-z0-9_-]*$": {
              "$ref": "#/$defs/scriptJudge"
            }
          }
        }
      }
    },
    "projects": {
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/workspace"
      },
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "patternProperties": {
          "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
        },
        "properties": {
          "workflows": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "allowed": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/name"
                }
              },
              "default": {
                "$ref": "#/$defs/name"
              }
            }
          },
          "triggers": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[a-z][a-z0-9_]*$": {
                "type": "object",
                "required": [
                  "on",
                  "workflow"
                ],
                "additionalProperties": false,
                "patternProperties": {
                  "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
                },
                "properties": {
                  "on": {
                    "enum": [
                      "issue-labeled",
                      "issue-typed",
                      "pull-request-opened",
                      "schedule"
                    ]
                  },
                  "label": {
                    "$ref": "#/$defs/name",
                    "description": "With issue-labeled: the label that starts a run."
                  },
                  "issue_type": {
                    "$ref": "#/$defs/name",
                    "description": "With issue-typed: the issue type that starts a run."
                  },
                  "cron": {
                    "$ref": "#/$defs/name",
                    "description": "With schedule: a five-field cron expression, in UTC."
                  },
                  "workflow": {
                    "$ref": "#/$defs/name",
                    "description": "The name of a workflow in the workflows section."
                  },
                  "objective": {
                    "$ref": "#/$defs/name",
                    "description": "The standing objective of every run this trigger starts. Required with schedule. With an issue or a pull request, it comes before their text."
                  }
                },
                "allOf": [
                  {
                    "if": {
                      "properties": {
                        "on": {
                          "const": "issue-labeled"
                        }
                      },
                      "required": [
                        "on"
                      ]
                    },
                    "then": {
                      "required": [
                        "label"
                      ]
                    }
                  },
                  {
                    "if": {
                      "properties": {
                        "on": {
                          "const": "issue-typed"
                        }
                      },
                      "required": [
                        "on"
                      ]
                    },
                    "then": {
                      "required": [
                        "issue_type"
                      ]
                    }
                  },
                  {
                    "if": {
                      "properties": {
                        "on": {
                          "const": "schedule"
                        }
                      },
                      "required": [
                        "on"
                      ]
                    },
                    "then": {
                      "required": [
                        "cron",
                        "objective"
                      ]
                    }
                  }
                ]
              }
            },
            "description": "What starts a run on this workspace, keyed by trigger name."
          },
          "personas": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "allowed": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/personaName"
                },
                "uniqueItems": true
              }
            }
          },
          "agents": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "engines": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/name"
                }
              },
              "approval": {
                "type": "object",
                "additionalProperties": false,
                "patternProperties": {
                  "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
                },
                "properties": {
                  "require": {
                    "type": "array",
                    "items": {
                      "enum": [
                        "plan",
                        "effect"
                      ]
                    }
                  }
                }
              }
            }
          },
          "areas": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_-]*$"
            }
          },
          "image": {
            "$ref": "#/$defs/name"
          },
          "trust_project": {
            "type": "boolean",
            "default": true,
            "description": "False: only validators and judges the tenant config declares count as evidence. A project's own are previews."
          }
        }
      }
    },
    "extensions": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "adapters": {
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/formatName"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "version"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "version": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "effects": {
          "type": "object",
          "propertyNames": {
            "$ref": "#/$defs/effectName"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "version"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "version": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "step_kinds": {
          "type": "object",
          "propertyNames": {
            "type": "string",
            "pattern": "^[^/@]+/[^/@]+$"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "module",
              "schema"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "module": {
                "enum": [
                  "deterministic-runner",
                  "agent-runner",
                  "control-plane"
                ]
              },
              "schema": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "artifact_kinds": {
          "type": "object",
          "propertyNames": {
            "type": "string",
            "pattern": "^(?!evidence/|daggry/)[^/@]+/[^/@]+@[0-9]+$"
          },
          "additionalProperties": {
            "type": "object",
            "required": [
              "schema"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "schema": {
                "$ref": "#/$defs/name"
              }
            }
          }
        }
      }
    }
  },
  "$defs": {
    "name": {
      "type": "string",
      "minLength": 1
    },
    "label": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]*$"
    },
    "workspace": {
      "type": "string",
      "minLength": 1
    },
    "path": {
      "type": "string",
      "pattern": "^(?!/)(?!.*(^|/)\\.\\.(/|$))\\S+$"
    },
    "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."
    },
    "validatorName": {
      "type": "string",
      "pattern": "^(?!(project|daggry|coverage|no_regression|baseline_proof)$)[a-z][a-z0-9_-]*$"
    },
    "scriptValidator": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "source": {
          "type": "object",
          "required": [
            "workspace",
            "revision"
          ],
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "workspace": {
              "$ref": "#/$defs/workspace"
            },
            "revision": {
              "$ref": "#/$defs/name"
            }
          }
        },
        "run": {
          "$ref": "#/$defs/command"
        },
        "role": {
          "$ref": "#/$defs/label",
          "default": "custom"
        },
        "output": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_-]*$",
          "default": "exit-code",
          "description": "How Daggry reads the output: exit-code (default), document, or the name of a tool format such as junit or sarif."
        },
        "output_file": {
          "$ref": "#/$defs/path",
          "description": "The file the validator writes its output to. Absent: standard output."
        },
        "timeout": {
          "type": "integer",
          "minimum": 1
        },
        "env": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
          }
        },
        "needs": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "tools": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/label"
              }
            },
            "runner": {
              "$ref": "#/$defs/name"
            }
          }
        }
      },
      "required": [
        "source",
        "run"
      ]
    },
    "scriptJudge": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "source": {
          "type": "object",
          "required": [
            "workspace",
            "revision"
          ],
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "workspace": {
              "$ref": "#/$defs/workspace"
            },
            "revision": {
              "$ref": "#/$defs/name"
            }
          }
        },
        "run": {
          "$ref": "#/$defs/command"
        },
        "timeout": {
          "type": "integer",
          "minimum": 1
        }
      },
      "required": [
        "source",
        "run"
      ]
    },
    "personaName": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_-]*$"
    },
    "persona": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "description": {
          "$ref": "#/$defs/name",
          "description": "One or two sentences that say what the persona is for."
        },
        "engine": {
          "$ref": "#/$defs/name",
          "description": "The name of an engine in agents.engines of the tenant config."
        },
        "source": {
          "type": "object",
          "required": [
            "workspace",
            "revision"
          ],
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "workspace": {
              "$ref": "#/$defs/workspace"
            },
            "revision": {
              "$ref": "#/$defs/name"
            }
          },
          "description": "The pinned workspace and revision that hold the instruction file. Required with instructions.path."
        },
        "instructions": {
          "type": "object",
          "additionalProperties": false,
          "minProperties": 1,
          "maxProperties": 1,
          "properties": {
            "text": {
              "type": "string",
              "minLength": 1,
              "description": "The instructions, inline."
            },
            "path": {
              "$ref": "#/$defs/path",
              "description": "The instruction file, relative to the root of the persona's source."
            }
          },
          "description": "The persona's instructions: exactly one of text and path."
        },
        "transcript": {
          "$ref": "#/$defs/path",
          "description": "For a persona on the replay engine: the recorded transcript it plays back, in place of instructions."
        }
      },
      "required": [
        "engine"
      ],
      "oneOf": [
        {
          "required": [
            "instructions"
          ]
        },
        {
          "required": [
            "transcript"
          ]
        }
      ],
      "if": {
        "anyOf": [
          {
            "required": [
              "transcript"
            ]
          },
          {
            "required": [
              "instructions"
            ],
            "properties": {
              "instructions": {
                "required": [
                  "path"
                ]
              }
            }
          }
        ]
      },
      "then": {
        "required": [
          "source"
        ]
      }
    },
    "effectName": {
      "type": "string",
      "pattern": "^([A-Za-z0-9_-]+/)?[a-z][a-z0-9_-]*$"
    },
    "formatName": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_-]*$"
    }
  }
}
