{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aiwg.io/schemas/decision/DecisionFeatureSet.v1.schema.json",
  "title": "AIWG Decision Feature Set v1",
  "description": "Versioned probabilistic decision feature-set contract with source pins, lifecycle policy binding, deterministic columns, missingness, provenance and train/serve compatibility.",
  "type": "object",
  "required": ["schemaVersion", "kind", "metadata", "spec"],
  "additionalProperties": false,
  "properties": {
    "schemaVersion": { "const": "decision-feature-export/v1" },
    "kind": { "const": "DecisionFeatureSet" },
    "metadata": {
      "type": "object",
      "required": ["id", "version", "description", "digest"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "description": { "type": "string" },
        "digest": { "$ref": "#/$defs/sha256" }
      }
    },
    "spec": {
      "type": "object",
      "required": ["pins", "compatibleServedModels", "missingValuePolicy", "ordering", "privacy", "columns"],
      "additionalProperties": false,
      "properties": {
        "pins": {
          "type": "object",
          "required": ["ruleset", "binding", "definitions", "adapters", "calibration"],
          "additionalProperties": false,
          "properties": {
            "ruleset": { "$ref": "#/$defs/pin" },
            "binding": { "$ref": "#/$defs/pin" },
            "definitions": {
              "type": "object",
              "additionalProperties": { "$ref": "#/$defs/pin" }
            },
            "adapters": {
              "type": "object",
              "additionalProperties": {
                "type": "object",
                "required": ["adapter", "adapterVersion", "requestedModel", "servedModel"],
                "additionalProperties": false,
                "properties": {
                  "adapter": { "type": "string", "minLength": 1 },
                  "adapterVersion": { "type": "string", "minLength": 1 },
                  "requestedModel": { "type": "string", "minLength": 1 },
                  "servedModel": { "type": ["string", "null"] }
                }
              }
            },
            "calibration": {
              "type": "object",
              "additionalProperties": {
                "type": "object",
                "required": ["calibrationRef", "maxAgeMs", "recordedAtEpochMs"],
                "additionalProperties": false,
                "properties": {
                  "calibrationRef": { "type": "string", "minLength": 1 },
                  "maxAgeMs": { "type": ["integer", "null"], "minimum": 1 },
                  "recordedAtEpochMs": { "type": ["integer", "null"], "minimum": 0 }
                }
              }
            }
          }
        },
        "compatibleServedModels": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": { "type": "string", "minLength": 1 }
          }
        },
        "missingValuePolicy": {
          "type": "object",
          "required": ["numeric", "categorical", "missingStateColumn", "missingNeverZero"],
          "additionalProperties": false,
          "properties": {
            "numeric": { "const": "null" },
            "categorical": { "const": "null" },
            "missingStateColumn": { "const": true },
            "missingNeverZero": { "const": true }
          }
        },
        "ordering": { "const": "definition-pins-declared-order" },
        "privacy": {
          "type": "object",
          "required": ["classification", "retentionMs", "accessScope", "exportPolicyId", "deletion", "lifecycleSurface"],
          "additionalProperties": false,
          "properties": {
            "classification": { "enum": ["internal", "confidential", "restricted"] },
            "retentionMs": { "type": "integer", "minimum": 1 },
            "accessScope": { "type": "string", "minLength": 1 },
            "exportPolicyId": { "type": "string", "minLength": 1 },
            "deletion": { "enum": ["erase", "tombstone"] },
            "lifecycleSurface": { "const": "export" }
          }
        },
        "columns": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["name", "alias", "primitive", "valueType", "nullable", "order", "source", "uncertaintyProfile", "calibrationRef", "derivation", "description"],
            "additionalProperties": false,
            "properties": {
              "name": { "type": "string", "minLength": 1 },
              "alias": { "type": "string", "minLength": 1 },
              "primitive": { "enum": ["choice", "ordinal-score", "truth-probability", "ruleset"] },
              "valueType": { "enum": ["number", "string", "boolean"] },
              "nullable": { "type": "boolean" },
              "order": { "type": "integer", "minimum": 0 },
              "domain": { "type": "array", "items": { "type": "string" } },
              "source": { "$ref": "#/$defs/provenance" },
              "uncertaintyProfile": { "type": ["string", "null"] },
              "calibrationRef": { "type": ["string", "null"] },
              "derivation": {
                "anyOf": [
                  { "type": "null" },
                  {
                    "type": "object",
                    "required": ["algorithm", "version"],
                    "additionalProperties": false,
                    "properties": {
                      "algorithm": { "type": "string", "minLength": 1 },
                      "version": { "type": "string", "minLength": 1 }
                    }
                  }
                ]
              },
              "description": { "type": "string" }
            }
          }
        }
      }
    }
  },
  "$defs": {
    "sha256": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" },
    "pin": {
      "type": "object",
      "required": ["id", "version", "digest"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "digest": { "$ref": "#/$defs/sha256" }
      }
    },
    "provenance": {
      "enum": ["provider-native", "model-self-report", "adapter-one-hot", "derived", "calibrated", "missing"]
    }
  }
}
