{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://choiceproof.dev/schemas/semantic-intent-binding-v0.1.schema.json",
  "title": "ChoiceProof Semantic Intent Binding v0.1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "enterpriseScopeHash",
    "semanticKeyHash",
    "normalizationProfile",
    "action",
    "normalizedIntentHash",
    "relevantConfigHash",
    "semanticFingerprintHash",
    "revision",
    "bindingHash"
  ],
  "properties": {
    "schemaVersion": {
      "const": "choiceproof.semanticIntentBinding.v0.1"
    },
    "enterpriseScopeHash": { "$ref": "#/$defs/contentHash" },
    "semanticKeyHash": { "$ref": "#/$defs/contentHash" },
    "normalizationProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version", "manifestHash"],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 2,
          "maxLength": 128,
          "pattern": "^[a-z][a-z0-9_.:-]+$"
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]*$"
        },
        "manifestHash": { "$ref": "#/$defs/contentHash" }
      }
    },
    "action": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "effectKind", "effectMode", "targetHash"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 2,
          "maxLength": 128,
          "pattern": "^[a-z][a-z0-9_.:-]+$"
        },
        "effectKind": {
          "type": "string",
          "minLength": 2,
          "maxLength": 128,
          "pattern": "^[a-z][a-z0-9_.:/-]+$"
        },
        "effectMode": {
          "enum": ["one_time", "recurring", "contingent", "unspecified"]
        },
        "targetHash": {
          "oneOf": [{ "$ref": "#/$defs/contentHash" }, { "type": "null" }]
        }
      }
    },
    "normalizedIntentHash": { "$ref": "#/$defs/contentHash" },
    "relevantConfigHash": { "$ref": "#/$defs/contentHash" },
    "semanticFingerprintHash": { "$ref": "#/$defs/contentHash" },
    "revision": { "$ref": "#/$defs/positiveUint64String" },
    "supersedes": { "$ref": "#/$defs/supersessionReference" },
    "bindingHash": { "$ref": "#/$defs/contentHash" }
  },
  "$comment": "The runtime semanticIntentBindingV01Schema additionally enforces supersedes.revision + 1 == revision and a changed semantic fingerprint; those cross-field arithmetic and inequality checks are not expressible portably in this JSON Schema profile.",
  "allOf": [
    {
      "if": {
        "properties": { "revision": { "const": "1" } },
        "required": ["revision"]
      },
      "then": { "not": { "required": ["supersedes"] } },
      "else": { "required": ["supersedes"] }
    }
  ],
  "$defs": {
    "contentHash": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "positiveUint64String": {
      "type": "string",
      "$comment": "The second branch is the exact lexical upper bound 18446744073709551615; values above uint64 max are invalid.",
      "oneOf": [
        { "pattern": "^[1-9][0-9]{0,18}$" },
        {
          "pattern": "^(?:1[0-7][0-9]{18}|18[0-3][0-9]{17}|184[0-3][0-9]{16}|1844[0-5][0-9]{15}|18446[0-6][0-9]{14}|184467[0-3][0-9]{13}|1844674[0-3][0-9]{12}|184467440[0-6][0-9]{10}|1844674407[0-2][0-9]{9}|18446744073[0-6][0-9]{8}|1844674407370[0-8][0-9]{6}|18446744073709[0-4][0-9]{5}|184467440737095[0-4][0-9]{4}|18446744073709550[0-9]{3}|18446744073709551[0-5][0-9]{2}|1844674407370955160[0-9]|1844674407370955161[0-4]|18446744073709551615)$"
        }
      ]
    },
    "supersessionReference": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "attemptId",
        "recordId",
        "eventId",
        "receiptHash",
        "revision",
        "semanticFingerprintHash",
        "bindingHash"
      ],
      "properties": {
        "attemptId": {
          "type": "string",
          "pattern": "^attempt_[0-9a-f]{64}$"
        },
        "recordId": {
          "type": "string",
          "pattern": "^record_[0-9a-f]{64}$"
        },
        "eventId": {
          "type": "string",
          "pattern": "^evt_[0-9a-f]{64}$"
        },
        "receiptHash": {
          "type": "string",
          "pattern": "^0x[0-9a-f]{64}$"
        },
        "revision": { "$ref": "#/$defs/positiveUint64String" },
        "semanticFingerprintHash": { "$ref": "#/$defs/contentHash" },
        "bindingHash": { "$ref": "#/$defs/contentHash" }
      }
    }
  }
}
