{
  "$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"
    },
    "workflows": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "name",
          "source"
        ],
        "additionalProperties": false,
        "patternProperties": {
          "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
        },
        "properties": {
          "name": {
            "$ref": "#/$defs/name"
          },
          "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
            }
          }
        }
      }
    },
    "agents": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "engines": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "credential"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/name"
              },
              "credential": {
                "type": "object",
                "required": [
                  "ref"
                ],
                "additionalProperties": false,
                "patternProperties": {
                  "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
                },
                "properties": {
                  "ref": {
                    "$ref": "#/$defs/name"
                  }
                }
              }
            }
          }
        },
        "roster": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "engine"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/name"
              },
              "engine": {
                "$ref": "#/$defs/name"
              },
              "instructions": {
                "$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": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "image"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/name"
              },
              "image": {
                "$ref": "#/$defs/name"
              },
              "tools": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/label"
                }
              },
              "default": {
                "type": "boolean"
              }
            }
          }
        },
        "classes": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "module",
              "network",
              "model_credential"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/name"
              },
              "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": {
            "^[a-z][a-z0-9_-]*$": {
              "$ref": "#/$defs/scriptValidator"
            }
          }
        },
        "native": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "version",
              "digest"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "type": "string",
                "pattern": "^(?!daggry/|tenant/|project/)[A-Za-z0-9_-]+/[a-z][a-z0-9_-]*$"
              },
              "version": {
                "$ref": "#/$defs/name"
              },
              "digest": {
                "type": "string",
                "pattern": "^[0-9a-f]{64}$"
              }
            }
          }
        }
      }
    },
    "projects": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "workspace"
        ],
        "additionalProperties": false,
        "patternProperties": {
          "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
        },
        "properties": {
          "workspace": {
            "$ref": "#/$defs/workspace"
          },
          "binding": {
            "$ref": "#/$defs/label"
          },
          "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"
              }
            }
          },
          "agents": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "engines": {
                "type": "array",
                "items": {
                  "$ref": "#/$defs/name"
                }
              },
              "roster": {
                "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"
          },
          "validators": {
            "type": "object",
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "admit": {
                "anyOf": [
                  {
                    "enum": [
                      "all",
                      "none"
                    ]
                  },
                  {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                      "$ref": "#/$defs/validatorName"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "extensions": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
      },
      "properties": {
        "bindings": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "version"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/label"
              },
              "version": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "adapters": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "version"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/adapterName"
              },
              "version": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "effects": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "version"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "name": {
                "$ref": "#/$defs/effectName"
              },
              "version": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "step_kinds": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "kind",
              "module",
              "schema"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "kind": {
                "type": "string",
                "pattern": "^[^/@]+/[^/@]+$"
              },
              "module": {
                "enum": [
                  "deterministic-runner",
                  "agent-runner",
                  "control-plane"
                ]
              },
              "schema": {
                "$ref": "#/$defs/name"
              }
            }
          }
        },
        "artifact_kinds": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "kind",
              "schema"
            ],
            "additionalProperties": false,
            "patternProperties": {
              "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
            },
            "properties": {
              "kind": {
                "type": "string",
                "pattern": "^(?!evidence/|daggry/)[^/@]+/[^/@]+@[0-9]+$"
              },
              "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
    },
    "adapterName": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$"
    },
    "effectName": {
      "type": "string",
      "pattern": "^([A-Za-z0-9_-]+/)?[a-z][a-z0-9_-]*$"
    },
    "path": {
      "type": "string",
      "pattern": "^(?!/)(?!.*(^|/)\\.\\.(/|$))\\S+$"
    },
    "command": {
      "anyOf": [
        {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1
        },
        {
          "$ref": "#/$defs/path"
        }
      ]
    },
    "validatorName": {
      "type": "string",
      "pattern": "^[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"
        },
        "kind": {
          "$ref": "#/$defs/label"
        },
        "result": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$": true
          },
          "properties": {
            "contract": {
              "anyOf": [
                {
                  "enum": [
                    "exit-code",
                    "document"
                  ]
                },
                {
                  "type": "string",
                  "pattern": "^[A-Za-z0-9_-]+/[A-Za-z0-9_-]+$"
                }
              ]
            },
            "path": {
              "$ref": "#/$defs/path"
            }
          },
          "required": [
            "contract"
          ]
        },
        "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",
        "kind"
      ]
    }
  }
}
