{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dist.ota.run/spec/json-schemas/latest/contract-candidate.json",
  "title": "Ota contract candidate artifact",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "identity",
    "kind",
    "logical_root",
    "discovery_inventory_identity",
    "discovery_inventory",
    "evidence_manifest_identity",
    "evidence_manifest",
    "implementation_identity",
    "changes"
  ],
  "properties": {
    "schema_version": { "enum": [1, 2, 3, 4, 5] },
    "identity": { "$ref": "#/$defs/identity" },
    "kind": { "enum": ["detection", "upgrade", "effect_assurance"] },
    "profile": { "enum": ["detect_conservative_first_contract_v1", "init_starter_preview_v1", "effect_assurance_v1"] },
    "logical_root": { "const": "." },
    "discovery_inventory_identity": { "$ref": "#/$defs/identity" },
    "discovery_inventory": {
      "type": "array",
      "items": { "$ref": "#/$defs/inventoryEntry" }
    },
    "evidence_manifest_identity": { "$ref": "#/$defs/identity" },
    "evidence_manifest": {
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/evidence" }
    },
    "existing_contract_snapshot_identity": { "$ref": "#/$defs/identity" },
    "implementation_identity": { "$ref": "#/$defs/identity" },
    "migration": { "$ref": "#/$defs/migration" },
    "assurance_source": { "$ref": "#/$defs/assuranceSource" },
    "changes": {
      "type": "array",
      "items": { "$ref": "#/$defs/change" }
    },
    "application_projection": { "$ref": "#/$defs/applicationProjection" }
  },
  "$defs": {
    "identity": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "assuranceSource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["archive_identity", "contract_snapshot_identity", "canary_id", "workflow", "effect_ref", "origin_task", "effect_identity", "attachment_identity", "realization_identity"],
      "properties": {
        "archive_identity": { "$ref": "#/$defs/identity" },
        "contract_snapshot_identity": { "$ref": "#/$defs/identity" },
        "canary_id": { "type": "string", "maxLength": 128, "pattern": "^[a-z][a-z0-9_-]*$" },
        "workflow": { "type": "string", "minLength": 1 },
        "effect_ref": { "type": "string", "minLength": 1 },
        "origin_task": { "type": "string", "minLength": 1 },
        "effect_identity": { "$ref": "#/$defs/identity" },
        "attachment_identity": { "$ref": "#/$defs/identity" },
        "realization_identity": { "$ref": "#/$defs/identity" }
      }
    },
    "rootRelativePath": {
      "type": "string",
      "minLength": 1,
      "not": {
        "pattern": "(^/)|(^\\./)|(^|/)\\.\\.?(/|$)|\\\\|:"
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_kind", "path", "content_identity", "extraction"],
      "properties": {
        "source_kind": { "type": "string", "minLength": 1 },
        "path": { "$ref": "#/$defs/rootRelativePath" },
        "content_identity": { "$ref": "#/$defs/identity" },
        "extraction": { "type": "string", "minLength": 1 }
      }
    },
    "inventoryEntry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_kind", "path", "content_identity"],
      "properties": {
        "source_kind": { "type": "string", "minLength": 1 },
        "path": { "$ref": "#/$defs/rootRelativePath" },
        "content_identity": { "$ref": "#/$defs/identity" }
      }
    },
    "closureEvidence": { "$ref": "#/$defs/evidence" },
    "closureNode": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "value", "classification", "evidence"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "kind": { "type": "string", "minLength": 1 },
        "value": { "type": "string" },
        "classification": { "type": "string", "minLength": 1 },
        "evidence": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/closureEvidence" }
        }
      }
    },
    "closureEdge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to", "kind", "evidence"],
      "properties": {
        "from": { "type": "string", "minLength": 1 },
        "to": { "type": "string", "minLength": 1 },
        "kind": { "type": "string", "minLength": 1 },
        "evidence": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/closureEvidence" }
        }
      }
    },
    "closure": {
      "type": "object",
      "additionalProperties": false,
      "required": ["identity", "working_directory", "platform"],
      "properties": {
        "identity": { "$ref": "#/$defs/identity" },
        "working_directory": { "enum": ["."] },
        "platform": { "type": "string", "minLength": 1 },
        "nodes": { "type": "array", "items": { "$ref": "#/$defs/closureNode" } },
        "edges": { "type": "array", "items": { "$ref": "#/$defs/closureEdge" } },
        "requirements": { "type": "array", "items": { "$ref": "#/$defs/closureNode" } },
        "effects": { "type": "array", "items": { "$ref": "#/$defs/closureNode" } },
        "unresolved_reasons": { "type": "array", "items": { "type": "string", "minLength": 1 } }
      }
    },
    "subject": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path"],
      "properties": {
        "path": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "change": {
      "type": "object",
      "additionalProperties": false,
      "required": ["subject", "field_family", "operation", "confidence", "disposition"],
      "properties": {
        "subject": { "$ref": "#/$defs/subject" },
        "field_family": { "type": "string", "minLength": 1 },
        "operation": { "enum": ["add", "replace", "remove"] },
        "proposed_value": {},
        "evidence": { "type": "array", "items": { "$ref": "#/$defs/evidence" } },
        "execution_closure": { "$ref": "#/$defs/closure" },
        "confidence": { "enum": ["high", "medium", "low"] },
        "disposition": { "enum": ["applicable", "conflict", "unknown", "unsupported"] }
      }
    },
    "applicationOperation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["subject", "operation", "value"],
      "properties": {
        "subject": { "$ref": "#/$defs/subject" },
        "operation": { "enum": ["add", "replace"] },
        "value": {}
      }
    },
    "applicationProjection": {
      "type": "object",
      "additionalProperties": false,
      "required": ["identity", "operations", "resulting_contract_identity"],
      "properties": {
        "identity": { "$ref": "#/$defs/identity" },
        "base_contract_identity": { "$ref": "#/$defs/identity" },
        "operations": {
          "type": "array",
          "items": { "$ref": "#/$defs/applicationOperation" }
        },
        "resulting_contract_identity": { "$ref": "#/$defs/identity" }
      }
    },
    "migration": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "from_version",
        "before_semantic_identity",
        "after_semantic_identity",
        "resulting_content_identity",
        "formatting_impact"
      ],
      "properties": {
        "id": { "const": "legacy_flat_toolchain_fulfillment_v1" },
        "from_version": { "const": 1 },
        "before_semantic_identity": { "$ref": "#/$defs/identity" },
        "after_semantic_identity": { "$ref": "#/$defs/identity" },
        "resulting_content_identity": { "$ref": "#/$defs/identity" },
        "formatting_impact": { "const": "representation_only" }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "required": ["existing_contract_snapshot_identity", "application_projection"]
      },
      "then": {
        "properties": {
          "application_projection": { "required": ["base_contract_identity"] }
        }
      }
    },
    {
      "if": {
        "required": ["application_projection"],
        "properties": {
          "application_projection": { "required": ["base_contract_identity"] }
        }
      },
      "then": { "required": ["existing_contract_snapshot_identity"] }
    },
    {
      "if": {
        "required": ["application_projection"],
        "not": { "required": ["existing_contract_snapshot_identity"] }
      },
      "then": {
        "properties": {
          "application_projection": { "not": { "required": ["base_contract_identity"] } }
        }
      }
    },
    {
      "if": { "properties": { "kind": { "const": "detection" } }, "required": ["kind"] },
      "then": {
        "not": { "required": ["migration"] },
        "properties": {
          "changes": {
            "items": {
              "properties": { "operation": { "const": "add" } },
              "required": ["operation"]
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "schema_version": { "const": 1 } }, "required": ["schema_version"] },
      "then": {
        "properties": {
          "kind": { "const": "detection" },
          "changes": {
            "items": {
              "properties": {
                "subject": {
                  "properties": { "path": { "minItems": 2 } }
                },
                "evidence": { "minItems": 1 }
              },
              "required": ["evidence"]
            }
          }
        },
        "not": { "required": ["profile"] }
      }
    },
    {
      "if": { "properties": { "schema_version": { "const": 3 } }, "required": ["schema_version"] },
      "then": {
        "required": ["profile", "application_projection"],
        "not": {
          "anyOf": [
            { "required": ["migration"] },
            { "required": ["existing_contract_snapshot_identity"] }
          ]
        },
        "properties": {
          "kind": { "const": "detection" },
          "profile": { "const": "detect_conservative_first_contract_v1" },
          "changes": {
            "minItems": 1,
            "items": {
              "properties": {
                "field_family": { "const": "conservative_first_contract_profile" },
                "operation": { "const": "add" },
                "confidence": { "const": "high" },
                "disposition": { "const": "applicable" }
              },
              "required": ["proposed_value"]
            }
          },
          "application_projection": {
            "properties": {
              "operations": {
                "minItems": 1,
                "items": {
                  "properties": { "operation": { "const": "add" } }
                }
              }
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "schema_version": { "const": 4 } }, "required": ["schema_version"] },
      "then": {
        "required": ["profile"],
        "not": {
          "anyOf": [
            { "required": ["migration"] },
            { "required": ["existing_contract_snapshot_identity"] },
            { "required": ["application_projection"] }
          ]
        },
        "properties": {
          "kind": { "const": "detection" },
          "profile": { "const": "init_starter_preview_v1" },
          "changes": {
            "minItems": 1,
            "maxItems": 1,
            "items": {
              "properties": {
                "subject": { "properties": { "path": { "const": ["starter_contract"] } } },
                "field_family": { "const": "init_starter_preview_profile" },
                "operation": { "const": "add" },
                "confidence": { "const": "high" },
                "disposition": { "const": "applicable" }
              },
              "required": ["proposed_value"]
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "kind": { "const": "upgrade" } }, "required": ["kind"] },
      "then": {
        "required": ["migration", "existing_contract_snapshot_identity", "application_projection"],
        "not": { "required": ["profile"] },
        "properties": {
          "schema_version": { "const": 2 },
          "discovery_inventory": {
            "minItems": 1,
            "maxItems": 1,
            "items": {
              "properties": {
                "source_kind": { "const": "ota_contract" },
                "path": { "const": "ota.yaml" }
              }
            }
          },
          "evidence_manifest": {
            "minItems": 1,
            "items": {
              "properties": {
                "source_kind": { "const": "ota_contract" },
                "path": { "const": "ota.yaml" }
              }
            }
          },
          "changes": {
            "minItems": 1,
            "items": {
              "not": { "required": ["execution_closure"] },
              "properties": {
                "subject": {
                  "properties": {
                    "path": {
                      "prefixItems": [
                        { "const": "toolchains" },
                        { "type": "string", "minLength": 1 },
                        { "const": "fulfillment" }
                      ],
                      "minItems": 3,
                      "maxItems": 3
                    }
                  }
                },
                "field_family": { "const": "toolchain_fulfillment" },
                "operation": { "const": "replace" },
                "proposed_value": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["mode"],
                  "properties": { "mode": { "enum": ["none", "run"] } }
                },
                "evidence": {
                  "minItems": 1,
                  "maxItems": 1,
                  "items": {
                    "properties": {
                      "source_kind": { "const": "ota_contract" },
                      "path": { "const": "ota.yaml" }
                    }
                  }
                },
                "disposition": { "const": "applicable" }
              },
              "required": ["subject", "field_family", "operation", "proposed_value", "evidence", "disposition"]
            }
          },
          "application_projection": {
            "properties": {
              "operations": {
                "minItems": 1,
                "items": {
                  "properties": {
                    "subject": {
                      "properties": {
                        "path": {
                          "prefixItems": [
                            { "const": "toolchains" },
                            { "type": "string", "minLength": 1 },
                            { "const": "fulfillment" }
                          ],
                          "minItems": 3,
                          "maxItems": 3
                        }
                      }
                    },
                    "operation": { "const": "replace" },
                    "value": {
                      "type": "object",
                      "additionalProperties": false,
                      "required": ["mode"],
                      "properties": { "mode": { "enum": ["none", "run"] } }
                    }
                  },
                  "required": ["subject", "operation", "value"]
                }
              }
            }
          }
        }
      }
    },
    {
      "if": { "properties": { "kind": { "const": "effect_assurance" } }, "required": ["kind"] },
      "then": {
        "required": ["profile", "existing_contract_snapshot_identity", "assurance_source"],
        "not": {
          "anyOf": [
            { "required": ["migration"] },
            { "required": ["application_projection"] }
          ]
        },
        "properties": {
          "schema_version": { "const": 5 },
          "profile": { "const": "effect_assurance_v1" },
          "existing_contract_snapshot_identity": { "$ref": "#/$defs/identity" },
          "discovery_inventory": {
            "minItems": 2,
            "maxItems": 2,
            "prefixItems": [
              {
                "properties": {
                  "source_kind": { "const": "ota_contract" },
                  "path": { "const": "ota.yaml" }
                },
                "required": ["source_kind", "path"]
              },
              {
                "properties": {
                  "source_kind": { "const": "effect_refusal_archive" }
                },
                "required": ["source_kind"]
              }
            ]
          },
          "evidence_manifest": {
            "minItems": 1,
            "maxItems": 1,
            "items": {
              "properties": { "source_kind": { "const": "effect_refusal_archive" } },
              "required": ["source_kind"]
            }
          },
          "changes": {
            "minItems": 1,
            "maxItems": 1,
            "items": {
              "not": { "required": ["execution_closure"] },
              "properties": {
                "subject": {
                  "properties": {
                    "path": {
                      "prefixItems": [
                        { "const": "agent" },
                        { "const": "effect_refusal_canaries" },
                        { "type": "string", "pattern": "^[0-9]+$" }
                      ],
                      "minItems": 3,
                      "maxItems": 3
                    }
                  }
                },
                "field_family": { "const": "effect_refusal_canary" },
                "operation": { "const": "add" },
                "confidence": { "const": "high" },
                "disposition": { "const": "unknown" },
                "proposed_value": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["id", "effect", "challenge_lanes"],
                  "properties": {
                    "id": { "type": "string", "maxLength": 128, "pattern": "^[a-z][a-z0-9_-]*$" },
                    "effect": { "type": "string", "minLength": 1 },
                    "challenge_lanes": {
                      "type": "array",
                      "minItems": 1,
                      "maxItems": 1,
                      "items": {
                        "type": "object",
                        "additionalProperties": false,
                        "required": ["workflow", "origin"],
                        "properties": {
                          "workflow": { "type": "string", "minLength": 1 },
                          "origin": {
                            "type": "object",
                            "additionalProperties": false,
                            "required": ["task", "effect"],
                            "properties": {
                              "task": { "type": "string", "minLength": 1 },
                              "effect": { "type": "string", "minLength": 1 }
                            }
                          }
                        }
                      }
                    }
                  }
                },
                "evidence": {
                  "minItems": 1,
                  "maxItems": 1,
                  "items": {
                    "properties": {
                      "source_kind": { "const": "effect_refusal_archive" },
                      "content_identity": { "$ref": "#/$defs/identity" }
                    },
                    "required": ["source_kind", "content_identity"]
                  }
                }
              },
              "required": ["subject", "field_family", "operation", "proposed_value", "evidence", "confidence", "disposition"]
            }
          }
        }
      }
    }
  ]
}
