{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aips.kivanura.org/spec/aips/lifecycle/0.1/aips-lifecycle-0.1.schema.json",
  "title": "AIPS Lifecycle Closure & Change Impact Profile 0.1",
  "description": "Version-specific evidence record for cross-artifact consistency, reclassification, supersession and downstream change impact. This record proves consistency; it does not replace authoritative Product artifacts.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "@context",
    "type",
    "recordId",
    "eventType",
    "productIdentity",
    "targetProductVersion",
    "currentState",
    "artifactRefs",
    "consistencyChecks",
    "closureDecision"
  ],
  "properties": {
    "@context": {
      "oneOf": [
        {
          "type": "string",
          "format": "uri"
        },
        {
          "type": "object"
        },
        {
          "type": "array",
          "minItems": 1
        }
      ]
    },
    "type": {
      "const": "LifecycleConsistencyRecord"
    },
    "recordId": {
      "type": "string",
      "minLength": 1
    },
    "eventType": {
      "enum": [
        "PUBLICATION",
        "SUPERSESSION",
        "DEPENDENCY_CHANGE",
        "RECLASSIFICATION",
        "RETIREMENT",
        "TRUST_INVALIDATION",
        "CONSUMER_CONTEXT_CHANGE"
      ]
    },
    "productIdentity": {
      "type": "string",
      "minLength": 1
    },
    "targetProductVersion": {
      "type": "string",
      "minLength": 1
    },
    "previousState": {
      "$ref": "#/$defs/ProductState"
    },
    "currentState": {
      "$ref": "#/$defs/ProductState"
    },
    "artifactRefs": {
      "$ref": "#/$defs/ArtifactRefs"
    },
    "impactAnalysis": {
      "$ref": "#/$defs/ImpactAnalysis"
    },
    "consistencyChecks": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/ConsistencyCheck"
      }
    },
    "closureDecision": {
      "$ref": "#/$defs/ClosureDecision"
    },
    "notes": {
      "type": "string"
    }
  },
  "$defs": {
    "Currentness": {
      "enum": [
        "CURRENT",
        "STALE",
        "REVALIDATION_REQUIRED",
        "INVALID",
        "NOT_APPLICABLE"
      ]
    },
    "ProductState": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "productFamily",
        "aiQualificationStatus",
        "marketplaceStatus",
        "trustCurrentness"
      ],
      "properties": {
        "productFamily": {
          "enum": [
            "AIProduct",
            "ModelProduct",
            "AIEnabledProduct",
            "OtherProduct"
          ]
        },
        "aiQualificationStatus": {
          "$ref": "#/$defs/Currentness"
        },
        "aiProductType": {
          "enum": [
            "Primary",
            "Derived"
          ]
        },
        "derivationTopology": {
          "enum": [
            "SingleUpstream",
            "Composite"
          ]
        },
        "marketplaceStatus": {
          "enum": [
            "Active",
            "Restricted",
            "Suspended",
            "Historical",
            "Retired",
            "NotListed"
          ]
        },
        "trustCurrentness": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "qualification",
            "dependencies",
            "governance",
            "assurance"
          ],
          "properties": {
            "qualification": {
              "$ref": "#/$defs/Currentness"
            },
            "dependencies": {
              "$ref": "#/$defs/Currentness"
            },
            "governance": {
              "$ref": "#/$defs/Currentness"
            },
            "assurance": {
              "$ref": "#/$defs/Currentness"
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "productFamily": {
                "const": "AIProduct"
              },
              "aiQualificationStatus": {
                "const": "CURRENT"
              }
            },
            "required": [
              "productFamily",
              "aiQualificationStatus"
            ]
          },
          "then": {
            "required": [
              "aiProductType"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "productFamily": {
                "not": {
                  "const": "AIProduct"
                }
              }
            },
            "required": [
              "productFamily"
            ]
          },
          "then": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "aiProductType"
                  ]
                },
                {
                  "required": [
                    "derivationTopology"
                  ]
                }
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "aiProductType": {
                "const": "Derived"
              }
            },
            "required": [
              "aiProductType"
            ]
          },
          "then": {
            "required": [
              "derivationTopology"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "aiProductType": {
                "const": "Primary"
              }
            },
            "required": [
              "aiProductType"
            ]
          },
          "then": {
            "not": {
              "required": [
                "derivationTopology"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "marketplaceStatus": {
                "enum": [
                  "Active",
                  "Restricted"
                ]
              }
            },
            "required": [
              "marketplaceStatus"
            ]
          },
          "then": {
            "properties": {
              "productFamily": {
                "const": "AIProduct"
              },
              "aiQualificationStatus": {
                "const": "CURRENT"
              },
              "trustCurrentness": {
                "properties": {
                  "qualification": {
                    "const": "CURRENT"
                  },
                  "dependencies": {
                    "const": "CURRENT"
                  },
                  "governance": {
                    "const": "CURRENT"
                  },
                  "assurance": {
                    "const": "CURRENT"
                  }
                }
              }
            }
          }
        }
      ]
    },
    "ArtifactRefs": {
      "type": "object",
      "additionalProperties": false,
      "minProperties": 1,
      "properties": {
        "aiProductVersionRef": {
          "type": "string"
        },
        "aqeRef": {
          "type": "string"
        },
        "aiprodRef": {
          "type": "string"
        },
        "aipdsRef": {
          "type": "string"
        },
        "governanceProfileRef": {
          "type": "string"
        },
        "dppRef": {
          "type": "string"
        },
        "marketplaceListingRef": {
          "type": "string"
        },
        "covpRefs": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        },
        "pmddRef": {
          "type": "string"
        },
        "pdepRecordRef": {
          "type": "string"
        }
      }
    },
    "DownstreamImpact": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "productVersionRef",
        "bindingState",
        "impact",
        "rationale"
      ],
      "properties": {
        "productVersionRef": {
          "type": "string",
          "minLength": 1
        },
        "bindingState": {
          "enum": [
            "PINNED_PREVIOUS_VERSION",
            "ADOPTING_NEW_VERSION",
            "UNPINNED",
            "MATERIAL_DEPENDENCY"
          ]
        },
        "impact": {
          "enum": [
            "NO_IMPACT",
            "REVALIDATION_REQUIRED",
            "RECLASSIFICATION_POSSIBLE",
            "REDEPLOYMENT_REQUIRED",
            "RETIREMENT_REVIEW"
          ]
        },
        "reentryStage": {
          "type": "integer",
          "minimum": 0,
          "maximum": 11
        },
        "rationale": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "ImpactAnalysis": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "triggerRef",
        "triggerType",
        "affectedDownstream",
        "requiredActions"
      ],
      "properties": {
        "triggerRef": {
          "type": "string",
          "minLength": 1
        },
        "triggerType": {
          "enum": [
            "DEPENDENCY_VERSION_CHANGE",
            "AI_PRIMITIVE_REMOVAL",
            "PRODUCT_FAMILY_CHANGE",
            "APT_CHANGE",
            "GOVERNANCE_CHANGE",
            "ASSURANCE_CHANGE",
            "CONSUMER_USE_CHANGE"
          ]
        },
        "affectedDownstream": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/DownstreamImpact"
          }
        },
        "requiredActions": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    "ConsistencyCheck": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "check",
        "status",
        "artifactRefs",
        "rationale"
      ],
      "properties": {
        "check": {
          "enum": [
            "IDENTITY_VERSION",
            "AQE_APT",
            "AIPROD_SEMANTICS",
            "AIPDS_BINDING",
            "DPP_TRUST",
            "MARKETPLACE_PROJECTION",
            "COVP_BINDING",
            "PMDD_BINDING",
            "GOVERNANCE_BINDING",
            "SUPERSESSION_HISTORY",
            "DOWNSTREAM_IMPACT"
          ]
        },
        "status": {
          "enum": [
            "PASS",
            "WARN",
            "FAIL"
          ]
        },
        "artifactRefs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "rationale": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "ClosureDecision": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "evaluatedAt",
        "evaluationMethod",
        "blockers"
      ],
      "properties": {
        "status": {
          "enum": [
            "CONSISTENT",
            "CONDITIONAL",
            "INCONSISTENT",
            "HISTORICAL_CLOSED"
          ]
        },
        "evaluatedAt": {
          "type": "string",
          "format": "date-time"
        },
        "evaluationMethod": {
          "type": "string",
          "minLength": 1
        },
        "blockers": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "RECLASSIFICATION"
          }
        },
        "required": [
          "eventType"
        ]
      },
      "then": {
        "required": [
          "previousState",
          "impactAnalysis"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "DEPENDENCY_CHANGE",
              "SUPERSESSION"
            ]
          }
        },
        "required": [
          "eventType"
        ]
      },
      "then": {
        "required": [
          "impactAnalysis"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "closureDecision": {
            "properties": {
              "status": {
                "const": "CONSISTENT"
              }
            },
            "required": [
              "status"
            ]
          }
        },
        "required": [
          "closureDecision"
        ]
      },
      "then": {
        "properties": {
          "consistencyChecks": {
            "not": {
              "contains": {
                "properties": {
                  "status": {
                    "const": "FAIL"
                  }
                },
                "required": [
                  "status"
                ]
              }
            }
          },
          "closureDecision": {
            "properties": {
              "blockers": {
                "maxItems": 0
              }
            }
          }
        }
      }
    }
  ]
}
