{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Takoform project and Form lifecycle authority",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "projectStatus", "currentEpoch", "states", "legacy", "currentForms", "proposals"],
  "properties": {
    "format": { "const": "takoform.form-lifecycle@v2" },
    "projectStatus": { "const": "experimental" },
    "currentEpoch": { "const": "forms.takoform.com/v1alpha2" },
    "states": {
      "const": ["proposal", "experimental", "stable", "legacy"]
    },
    "legacy": { "$ref": "#/$defs/legacyBaseline" },
    "currentForms": {
      "type": "array",
      "items": { "$ref": "#/$defs/formLifecycle" }
    },
    "proposals": {
      "type": "array",
      "items": { "$ref": "#/$defs/proposal" }
    }
  },
  "$defs": {
    "nonEmpty": { "type": "string", "minLength": 1 },
    "digest": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "relativePath": {
      "type": "string",
      "minLength": 1,
      "allOf": [
        { "not": { "pattern": "^/" } },
        { "not": { "pattern": "(^|/)\\.\\.(/|$)" } },
        { "not": { "pattern": "//" } }
      ]
    },
    "legacyBaseline": {
      "type": "object",
      "additionalProperties": false,
      "required": ["apiVersion", "decision", "epochDecision", "releaseSources", "releaseSourceInventory", "historicalAdmissionRoots", "newCreatePolicy", "retainedCapabilities"],
      "properties": {
        "apiVersion": { "const": "forms.takoform.com/v1alpha1" },
        "decision": { "const": "spec/decisions/0004-takoform-is-an-experimental-specification.md" },
        "epochDecision": { "const": "spec/decisions/0006-v1alpha2-restarts-form-lines.md" },
        "releaseSources": { "const": "forms/releases" },
        "releaseSourceInventory": {
          "type": "object",
          "additionalProperties": false,
          "required": ["format", "count", "digest"],
          "properties": {
            "format": { "const": "takoform.legacy-release-inventory@v1" },
            "count": { "type": "integer", "minimum": 1 },
            "digest": { "$ref": "#/$defs/digest" }
          }
        },
        "historicalAdmissionRoots": {
          "const": ["admission/v1", "admission/v3", "admission/v4"]
        },
        "newCreatePolicy": { "const": "host-policy" },
        "retainedCapabilities": {
          "const": ["read", "observe", "delete", "recovery", "migration"]
        }
      }
    },
    "proposal": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "document", "owner", "consumer", "intendedHosts", "workload", "portableBoundary", "portableFields", "hostDecisions", "lifecycleRisks", "securityBoundary", "priorArt", "existingAbstractionGap"],
      "properties": {
        "id": { "$ref": "#/$defs/nonEmpty" },
        "document": { "$ref": "#/$defs/relativePath" },
        "owner": { "$ref": "#/$defs/nonEmpty" },
        "consumer": { "$ref": "#/$defs/nonEmpty" },
        "intendedHosts": { "$ref": "#/$defs/nonEmptyUniqueArray" },
        "workload": { "$ref": "#/$defs/nonEmpty" },
        "portableBoundary": { "$ref": "#/$defs/nonEmpty" },
        "portableFields": { "$ref": "#/$defs/nonEmptyUniqueArray" },
        "hostDecisions": { "$ref": "#/$defs/nonEmptyUniqueArray" },
        "lifecycleRisks": {
          "type": "object",
          "additionalProperties": false,
          "required": ["replacement", "dataLoss", "delete", "import", "drift"],
          "properties": {
            "replacement": { "$ref": "#/$defs/nonEmpty" },
            "dataLoss": { "$ref": "#/$defs/nonEmpty" },
            "delete": { "$ref": "#/$defs/nonEmpty" },
            "import": { "$ref": "#/$defs/nonEmpty" },
            "drift": { "$ref": "#/$defs/nonEmpty" }
          }
        },
        "securityBoundary": {
          "type": "object",
          "additionalProperties": false,
          "required": ["credentials", "network", "artifacts", "secrets"],
          "properties": {
            "credentials": { "$ref": "#/$defs/nonEmpty" },
            "network": { "$ref": "#/$defs/nonEmpty" },
            "artifacts": { "$ref": "#/$defs/nonEmpty" },
            "secrets": { "$ref": "#/$defs/nonEmpty" }
          }
        },
        "priorArt": {
          "type": "array",
          "minItems": 5,
          "maxItems": 5,
          "prefixItems": [
            { "$ref": "#/$defs/occiReview" },
            { "$ref": "#/$defs/cimiReview" },
            { "$ref": "#/$defs/toscaReview" },
            { "$ref": "#/$defs/kubernetesReview" },
            { "$ref": "#/$defs/terraformReview" }
          ],
          "items": false
        },
        "existingAbstractionGap": { "$ref": "#/$defs/nonEmpty" }
      }
    },
    "priorArtReview": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "applicability", "finding"],
      "properties": {
        "name": { "type": "string" },
        "applicability": { "enum": ["applicable", "not-applicable"] },
        "finding": { "$ref": "#/$defs/nonEmpty" }
      }
    },
    "occiReview": { "allOf": [{ "$ref": "#/$defs/priorArtReview" }, { "properties": { "name": { "const": "OCCI" } } }] },
    "cimiReview": { "allOf": [{ "$ref": "#/$defs/priorArtReview" }, { "properties": { "name": { "const": "CIMI" } } }] },
    "toscaReview": { "allOf": [{ "$ref": "#/$defs/priorArtReview" }, { "properties": { "name": { "const": "TOSCA" } } }] },
    "kubernetesReview": { "allOf": [{ "$ref": "#/$defs/priorArtReview" }, { "properties": { "name": { "const": "Kubernetes/Crossplane" } } }] },
    "terraformReview": { "allOf": [{ "$ref": "#/$defs/priorArtReview" }, { "properties": { "name": { "const": "Terraform/OpenTofu" } } }] },
    "nonEmptyUniqueArray": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/nonEmpty" }
    },
    "formRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["apiVersion", "kind", "definitionVersion", "schemaDigest"],
      "properties": {
        "apiVersion": { "const": "forms.takoform.com/v1alpha2" },
        "kind": { "type": "string", "pattern": "^[A-Z][A-Za-z0-9]{0,63}$" },
        "definitionVersion": { "type": "string", "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$" },
        "schemaDigest": { "$ref": "#/$defs/digest" }
      }
    },
    "transition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "decision"],
      "properties": {
        "state": { "enum": ["proposal", "experimental", "stable", "legacy"] },
        "decision": { "$ref": "#/$defs/relativePath" }
      }
    },
    "evidenceRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["subject", "evidence"],
      "properties": {
        "subject": { "$ref": "#/$defs/nonEmpty" },
        "maintainer": { "$ref": "#/$defs/nonEmpty" },
        "evidence": { "$ref": "#/$defs/relativePath" }
      }
    },
    "lifecycleEvidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["definition", "fixtures", "hostImplementations", "realConsumers", "knownLimitations", "compatibility", "migration", "securityReview", "documentation", "publicationPlan"],
      "properties": {
        "definition": { "$ref": "#/$defs/relativePath" },
        "fixtures": { "$ref": "#/$defs/relativePath" },
        "hostImplementations": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/evidenceRef" } },
        "realConsumers": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/evidenceRef" } },
        "knownLimitations": { "$ref": "#/$defs/relativePath" },
        "compatibility": { "$ref": "#/$defs/relativePath" },
        "migration": { "$ref": "#/$defs/relativePath" },
        "securityReview": { "$ref": "#/$defs/relativePath" },
        "documentation": { "$ref": "#/$defs/relativePath" },
        "publicationPlan": { "$ref": "#/$defs/relativePath" },
        "lifecycleAgreement": { "$ref": "#/$defs/relativePath" },
        "interoperability": { "$ref": "#/$defs/relativePath" },
        "independentPublication": { "$ref": "#/$defs/relativePath" },
        "deprecationExercise": { "$ref": "#/$defs/relativePath" },
        "securityRevocationExercise": { "$ref": "#/$defs/relativePath" }
      }
    },
    "legacyDisposition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["reason", "successorOrAlternative", "newCreatePolicy", "retainedCapabilities", "migration"],
      "properties": {
        "reason": { "$ref": "#/$defs/nonEmpty" },
        "successorOrAlternative": { "$ref": "#/$defs/nonEmpty" },
        "newCreatePolicy": { "const": "host-policy" },
        "retainedCapabilities": { "const": ["read", "observe", "delete", "recovery", "migration"] },
        "migration": { "$ref": "#/$defs/relativePath" }
      }
    },
    "formLifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": ["proposalId", "state", "owner", "formRef", "packageDigest", "packagePath", "history", "evidence"],
      "properties": {
        "proposalId": { "$ref": "#/$defs/nonEmpty" },
        "state": { "enum": ["experimental", "stable", "legacy"] },
        "owner": { "$ref": "#/$defs/nonEmpty" },
        "formRef": { "$ref": "#/$defs/formRef" },
        "packageDigest": { "$ref": "#/$defs/digest" },
        "packagePath": { "$ref": "#/$defs/relativePath" },
        "history": { "type": "array", "minItems": 2, "items": { "$ref": "#/$defs/transition" } },
        "evidence": { "$ref": "#/$defs/lifecycleEvidence" },
        "legacy": { "$ref": "#/$defs/legacyDisposition" }
      },
      "allOf": [
        {
          "if": { "properties": { "state": { "const": "experimental" } }, "required": ["state"] },
          "then": {
            "properties": {
              "formRef": { "properties": { "definitionVersion": { "pattern": "^0\\." } } },
              "history": { "prefixItems": [{ "properties": { "state": { "const": "proposal" } } }, { "properties": { "state": { "const": "experimental" } } }], "minItems": 2, "maxItems": 2 }
            },
            "not": { "required": ["legacy"] }
          }
        },
        {
          "if": { "properties": { "state": { "const": "stable" } }, "required": ["state"] },
          "then": {
            "properties": {
              "formRef": { "properties": { "definitionVersion": { "pattern": "^[1-9][0-9]*\\." } } },
              "history": { "prefixItems": [{ "properties": { "state": { "const": "proposal" } } }, { "properties": { "state": { "const": "experimental" } } }, { "properties": { "state": { "const": "stable" } } }], "minItems": 3, "maxItems": 3 },
              "evidence": { "required": ["lifecycleAgreement", "interoperability", "independentPublication", "deprecationExercise", "securityRevocationExercise"] }
            },
            "not": { "required": ["legacy"] }
          }
        },
        {
          "if": { "properties": { "state": { "const": "legacy" } }, "required": ["state"] },
          "then": { "required": ["legacy"] }
        }
      ]
    }
  }
}
