{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://buildchain.kungfu.ai/contracts/v4-publication-rehearsal-capsule-v1.schema.json",
  "title": "Buildchain v4 Publication Rehearsal Capsule",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "coreVersion",
    "source",
    "candidate",
    "manifest",
    "config",
    "providerBindings",
    "providerBindingsRoot",
    "providerPolicy",
    "expectedObservations",
    "declaration",
    "transaction",
    "files",
    "capsuleRoot"
  ],
  "properties": {
    "schema": { "const": "buildchain-v4-publication-rehearsal-capsule/v1" },
    "coreVersion": { "const": "buildchain.release-tail/v1" },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["repository", "revision", "root"],
      "properties": {
        "repository": { "type": "string", "minLength": 1 },
        "revision": { "type": "string", "pattern": "^[0-9a-f]{40}$" },
        "root": { "$ref": "#/$defs/root" }
      }
    },
    "candidate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["root"],
      "properties": { "root": { "$ref": "#/$defs/root" } }
    },
    "manifest": { "$ref": "#/$defs/fileBinding" },
    "config": { "$ref": "#/$defs/fileBinding" },
    "providerBindings": { "$ref": "#/$defs/providerBindings" },
    "providerBindingsRoot": { "$ref": "#/$defs/root" },
    "providerPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema",
        "effectDefault",
        "allowedCapabilityIds",
        "allowedAdapters",
        "root"
      ],
      "properties": {
        "schema": {
          "const": "buildchain-v4-publication-rehearsal-provider-policy/v1"
        },
        "effectDefault": { "const": "disabled" },
        "allowedCapabilityIds": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "allowedAdapters": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$"
          }
        },
        "root": { "$ref": "#/$defs/root" }
      }
    },
    "expectedObservations": {
      "type": "object",
      "additionalProperties": false,
      "required": ["entries", "root"],
      "properties": {
        "entries": {
          "type": "array",
          "items": { "$ref": "#/$defs/observationSequence" }
        },
        "root": { "$ref": "#/$defs/root" }
      }
    },
    "declaration": { "$ref": "release-tail-capabilities-v1.schema.json" },
    "transaction": { "type": "object" },
    "files": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["role", "path", "size", "root"],
        "properties": {
          "role": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$"
          },
          "path": { "$ref": "#/$defs/path" },
          "size": { "type": "integer", "minimum": 0 },
          "root": { "$ref": "#/$defs/root" }
        }
      }
    },
    "capsuleRoot": { "$ref": "#/$defs/root" }
  },
  "$defs": {
    "root": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "path": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.(?:/|$))(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*//)(?!.*\\\\)(?!.*\/$).+$"
    },
    "providerBindings": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "artifacts", "documents", "evidence"],
      "properties": {
        "schema": {
          "const": "kungfu.buildchain.release-tail.provider-bindings/v1"
        },
        "artifacts": {
          "type": "object",
          "propertyNames": {
            "pattern": "^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$"
          },
          "additionalProperties": {
            "type": "object",
            "additionalProperties": false,
            "required": ["path", "name"],
            "properties": {
              "path": { "$ref": "#/$defs/path" },
              "name": { "type": "string", "minLength": 1 }
            }
          }
        },
        "documents": {
          "type": "object",
          "propertyNames": {
            "pattern": "^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$"
          },
          "additionalProperties": { "$ref": "#/$defs/providerDocument" }
        },
        "evidence": {
          "type": "object",
          "additionalProperties": false,
          "required": ["inputs", "output"],
          "properties": {
            "inputs": {
              "type": "array",
              "uniqueItems": true,
              "items": { "$ref": "#/$defs/path" }
            },
            "output": { "$ref": "#/$defs/path" }
          }
        }
      }
    },
    "providerDocument": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "method"],
      "properties": {
        "path": { "$ref": "#/$defs/path" },
        "method": { "enum": ["PUT", "POST"] }
      }
    },
    "fileBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "root"],
      "properties": {
        "path": { "$ref": "#/$defs/path" },
        "root": { "$ref": "#/$defs/root" }
      }
    },
    "readback": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "outcome",
        "subjectRoot",
        "targetRoot",
        "evidenceRoots",
        "providerCode"
      ],
      "properties": {
        "outcome": { "enum": ["observed", "absent", "transient", "conflict"] },
        "subjectRoot": {
          "oneOf": [{ "$ref": "#/$defs/root" }, { "const": "" }]
        },
        "targetRoot": {
          "oneOf": [{ "$ref": "#/$defs/root" }, { "const": "" }]
        },
        "evidenceRoots": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/root" }
        },
        "providerCode": { "type": "string", "minLength": 1 }
      }
    },
    "observationSequence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["operationId", "readbacks", "apply"],
      "properties": {
        "operationId": { "$ref": "#/$defs/root" },
        "readbacks": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/readback" }
        },
        "apply": {
          "type": "object",
          "additionalProperties": false,
          "required": ["outcome", "code", "classification"],
          "properties": {
            "outcome": { "enum": ["applied", "transient", "conflict"] },
            "code": { "type": "string", "minLength": 1 },
            "classification": { "type": "string", "minLength": 1 }
          }
        }
      }
    }
  }
}
