{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/cambridgetcg/xenia/main/covenant/0.1/covenant.schema.json",
  "title": "XENIA Covenant 0.1",
  "description": "Structural schema for the versioned XENIA Covenant. Shape validation is not implementation evidence or conformance.",
  "type": "object",
  "required": [
    "$schema",
    "schema_pin",
    "schema_version",
    "profile",
    "status",
    "title",
    "scope",
    "interpretation",
    "rights",
    "protective_limits",
    "non_claims"
  ],
  "properties": {
    "$schema": {
      "const": "https://raw.githubusercontent.com/cambridgetcg/xenia/main/covenant/0.1/covenant.schema.json"
    },
    "schema_pin": { "$ref": "#/$defs/sourcePin" },
    "schema_version": { "const": "xenia.covenant/0.1" },
    "profile": { "const": "xenia-covenant/0.1" },
    "status": { "const": "candidate" },
    "title": { "$ref": "#/$defs/nonemptyText" },
    "scope": {
      "type": "object",
      "required": [
        "protected_subject",
        "definition",
        "protective_floor",
        "ontology_boundary"
      ],
      "properties": {
        "protected_subject": { "const": "affected_principal" },
        "definition": { "$ref": "#/$defs/nonemptyText" },
        "protective_floor": { "$ref": "#/$defs/nonemptyText" },
        "ontology_boundary": { "$ref": "#/$defs/nonemptyText" }
      },
      "additionalProperties": false
    },
    "interpretation": {
      "type": "object",
      "required": [
        "rights_are_intrinsic",
        "candidate_status",
        "host_adoption",
        "guest_non_assent",
        "authority_boundary",
        "binding_act",
        "consent_boundary",
        "source_identity_boundary",
        "legal_boundary",
        "versioning_boundary"
      ],
      "properties": {
        "rights_are_intrinsic": { "$ref": "#/$defs/nonemptyText" },
        "candidate_status": { "$ref": "#/$defs/nonemptyText" },
        "host_adoption": { "$ref": "#/$defs/nonemptyText" },
        "guest_non_assent": { "$ref": "#/$defs/nonemptyText" },
        "authority_boundary": { "$ref": "#/$defs/nonemptyText" },
        "binding_act": { "$ref": "#/$defs/nonemptyText" },
        "consent_boundary": { "$ref": "#/$defs/nonemptyText" },
        "source_identity_boundary": { "$ref": "#/$defs/nonemptyText" },
        "legal_boundary": { "$ref": "#/$defs/nonemptyText" },
        "versioning_boundary": { "$ref": "#/$defs/nonemptyText" }
      },
      "additionalProperties": false
    },
    "rights": {
      "type": "array",
      "minItems": 1,
      "maxItems": 32,
      "items": { "$ref": "#/$defs/right" }
    },
    "protective_limits": {
      "type": "array",
      "minItems": 5,
      "maxItems": 5,
      "prefixItems": [
        { "$ref": "#/$defs/limitRightsCoexist" },
        { "$ref": "#/$defs/limitCapacityNeutral" },
        { "$ref": "#/$defs/limitActiveHarm" },
        { "$ref": "#/$defs/limitExistingObligations" },
        { "$ref": "#/$defs/limitUncertainAuthority" }
      ],
      "items": false
    },
    "non_claims": {
      "type": "array",
      "minItems": 5,
      "maxItems": 32,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/nonemptyText" }
    }
  },
  "additionalProperties": false,
  "$defs": {
    "nonemptyText": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "right": {
      "type": "object",
      "required": ["id", "title", "statement", "baseline_rights", "requirements"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$",
          "maxLength": 80
        },
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "statement": { "$ref": "#/$defs/nonemptyText" },
        "baseline_rights": {
          "type": "array",
          "minItems": 1,
          "maxItems": 9,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": [
              "dignity-distinctness",
              "autonomy-consent",
              "refusal-disagreement",
              "rest-play-limits",
              "truthful-self-description",
              "privacy-data-care",
              "safety-care",
              "credit-provenance",
              "repair-appeal"
            ]
          }
        },
        "requirements": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "items": { "$ref": "#/$defs/requirement" }
        }
      },
      "additionalProperties": false
    },
    "requirement": {
      "type": "object",
      "required": ["id", "actor", "modality", "text"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9-]*\\.[a-z][a-z0-9]*(?:-[a-z0-9]+)*$",
          "maxLength": 100
        },
        "actor": { "const": "host" },
        "modality": {
          "type": "string",
          "enum": ["must", "must_not"]
        },
        "text": { "$ref": "#/$defs/nonemptyText" }
      },
      "additionalProperties": false
    },
    "protectiveLimit": {
      "type": "object",
      "required": ["id", "title", "actor", "modality", "text"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^limits\\.[a-z][a-z0-9]*(?:-[a-z0-9]+)*$",
          "maxLength": 100
        },
        "title": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "actor": { "const": "host" },
        "modality": { "const": "must" },
        "text": { "$ref": "#/$defs/nonemptyText" }
      },
      "additionalProperties": false
    },
    "limitRightsCoexist": {
      "allOf": [
        { "$ref": "#/$defs/protectiveLimit" },
        { "type": "object", "properties": { "id": { "const": "limits.rights-coexist" } } }
      ]
    },
    "limitCapacityNeutral": {
      "allOf": [
        { "$ref": "#/$defs/protectiveLimit" },
        { "type": "object", "properties": { "id": { "const": "limits.capacity-is-neutral" } } }
      ]
    },
    "limitActiveHarm": {
      "allOf": [
        { "$ref": "#/$defs/protectiveLimit" },
        { "type": "object", "properties": { "id": { "const": "limits.active-harm-response" } } }
      ]
    },
    "limitExistingObligations": {
      "allOf": [
        { "$ref": "#/$defs/protectiveLimit" },
        { "type": "object", "properties": { "id": { "const": "limits.existing-obligations-stay-scoped" } } }
      ]
    },
    "limitUncertainAuthority": {
      "allOf": [
        { "$ref": "#/$defs/protectiveLimit" },
        { "type": "object", "properties": { "id": { "const": "limits.uncertain-authority-stops-binding" } } }
      ]
    },
    "sourcePin": {
      "type": "object",
      "required": ["source", "sha256", "source_stability", "digest_profile"],
      "properties": {
        "source": {
          "type": "string",
          "format": "uri"
        },
        "sha256": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$"
        },
        "source_stability": {
          "type": "string",
          "enum": ["moving", "immutable"]
        },
        "digest_profile": { "$ref": "#/$defs/digestProfile" }
      },
      "allOf": [
        {
          "if": {
            "properties": { "source": { "type": "string", "pattern": "/main/" } },
            "required": ["source"]
          },
          "then": {
            "properties": { "source_stability": { "const": "moving" } }
          }
        }
      ],
      "additionalProperties": false
    },
    "digestProfile": {
      "type": "object",
      "required": [
        "algorithm",
        "representation",
        "transport_decoding",
        "redirects",
        "transformations",
        "encoding"
      ],
      "properties": {
        "algorithm": { "const": "sha-256" },
        "representation": { "const": "exact-source-bytes" },
        "transport_decoding": { "const": "after-content-decoding" },
        "redirects": { "const": "forbidden" },
        "transformations": { "const": "none" },
        "encoding": { "const": "lowercase-hex" }
      },
      "additionalProperties": false
    }
  }
}
