{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.dharma-ai.io/skill-bundle/v2",
  "title": "Signed Organization Skill Bundle",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "bundleId",
    "organizationId",
    "version",
    "operation",
    "skills",
    "riskClass",
    "targetSelectors",
    "activationPolicy",
    "rollbackBundleId",
    "evaluationReceiptId",
    "bundleHash",
    "createdAt",
    "signature"
  ],
  "properties": {
    "schema": {
      "const": "dharma.skill-bundle/v2"
    },
    "bundleId": {
      "type": "string",
      "format": "uuid"
    },
    "organizationId": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "type": "string"
    },
    "operation": {
      "type": "string",
      "enum": [
        "install",
        "clear"
      ]
    },
    "skills": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "skillId",
          "version",
          "repository",
          "commit",
          "contentHash",
          "path"
        ],
        "properties": {
          "skillId": {
            "type": "string",
            "minLength": 1
          },
          "version": {
            "type": "string"
          },
          "repository": {
            "type": "string",
            "pattern": "^https://github\\.com/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+(?:\\.git)?$"
          },
          "commit": {
            "type": "string"
          },
          "contentHash": {
            "type": "string",
            "pattern": "^(sha256|blake3):[A-Fa-f0-9]{32,128}$"
          },
          "path": {
            "type": "string",
            "minLength": 1,
            "maxLength": 1024,
            "pattern": "^(?!/)(?!.*\\\\)(?!.*//)(?!\\.{1,2}(?:/|$))(?!.*(?:/)\\.{1,2}(?:/|$))(?:[^/]+/)*[^/]+$"
          },
          "files": {
            "type": "array",
            "minItems": 1,
            "maxItems": 32,
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "path",
                "contentBase64",
                "sha256"
              ],
              "properties": {
                "path": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 1024,
                  "pattern": "^(?!/)(?!.*\\\\)(?!.*//)(?!\\.{1,2}(?:/|$))(?!.*(?:/)\\.{1,2}(?:/|$))(?:[^/]+/)*[^/]+$"
                },
                "contentBase64": {
                  "type": "string",
                  "maxLength": 1398104,
                  "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
                },
                "sha256": {
                  "type": "string",
                  "pattern": "^sha256:[A-Fa-f0-9]{64}$"
                }
              }
            }
          }
        }
      }
    },
    "riskClass": {
      "type": "string",
      "enum": [
        "R0",
        "R1",
        "R2",
        "R3",
        "R4"
      ]
    },
    "targetSelectors": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "organizationAgentIds",
        "deviceIds",
        "workspaceIds",
        "providers"
      ],
      "properties": {
        "organizationAgentIds": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "format": "uuid" }
        },
        "deviceIds": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "format": "uuid" }
        },
        "workspaceIds": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "format": "uuid" }
        },
        "providers": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "enum": ["codex", "claude", "agy", "hermes"] }
        }
      }
    },
    "activationPolicy": {
      "type": "string",
      "enum": [
        "next_task",
        "next_session",
        "host_restart",
        "immediate_safe_reload"
      ]
    },
    "rollbackBundleId": {
      "type": [
        "string",
        "null"
      ],
      "format": "uuid"
    },
    "evaluationReceiptId": {
      "type": "string",
      "minLength": 1
    },
    "bundleHash": {
      "type": "string",
      "pattern": "^(sha256|blake3):[A-Fa-f0-9]{32,128}$"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "expiresAt": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "signature": {
      "type": "string",
      "minLength": 1
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "operation": {
            "const": "clear"
          }
        }
      },
      "then": {
        "properties": {
          "skills": {
            "type": "array",
            "maxItems": 0
          }
        }
      },
      "else": {
        "properties": {
          "skills": {
            "type": "array",
            "minItems": 1
          }
        }
      }
    }
  ]
}
