{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://jos.local/schemas/clan-provenance.schema.json",
  "title": "CLAN Provenance Schema",
  "type": "array",
  "minItems": 1,
  "items": {
    "$ref": "#/$defs/trail"
  },
  "$defs": {
    "nonEmptyString": {
      "type": "string",
      "minLength": 1
    },
    "unixTimeInt": {
      "type": "integer",
      "minimum": 0
    },
    "sha256Ref": {
      "type": "string",
      "pattern": "^(sha256:)?[A-Fa-f0-9]{12,64}$"
    },
    "visibilityLevel": {
      "type": "string",
      "enum": [
        "public",
        "interclan",
        "incircle",
        "soulsafe",
        "sacred"
      ]
    },
    "consentStatus": {
      "type": "string",
      "enum": [
        "none",
        "pending",
        "confirmed"
      ]
    },
    "actorType": {
      "type": "string",
      "enum": [
        "human",
        "agent",
        "system",
        "external"
      ]
    },
    "opType": {
      "type": "string",
      "enum": [
        "created",
        "summarized",
        "redacted",
        "classified",
        "validated",
        "composed",
        "merged",
        "stamped",
        "export_validated",
        "policy_checked",
        "synced",
        "imported",
        "corrected"
      ]
    },
    "versions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "constitution_version": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "agent_prompt_version": {
          "type": "string"
        },
        "router_guard_version": {
          "type": "string"
        },
        "protocol_version": {
          "type": "string"
        }
      },
      "required": [
        "constitution_version"
      ]
    },
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "type": {
          "$ref": "#/$defs/actorType"
        },
        "id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "display": {
          "type": "string",
          "maxLength": 120
        }
      },
      "required": [
        "type",
        "id"
      ]
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "channel": {
          "type": "string",
          "enum": [
            "ui",
            "api",
            "telegram",
            "matrix",
            "dao",
            "chain",
            "import",
            "internal",
            "webhook",
            "bridge"
          ]
        },
        "request_id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "session_id": {
          "type": "string",
          "maxLength": 128
        },
        "message_id": {
          "type": "string",
          "maxLength": 128
        }
      },
      "required": [
        "channel",
        "request_id"
      ]
    },
    "context": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "circle_id": {
          "type": "string",
          "maxLength": 128
        },
        "circle_name": {
          "type": "string",
          "maxLength": 128
        },
        "gate_level": {
          "type": "string",
          "maxLength": 64
        },
        "visibility_level": {
          "$ref": "#/$defs/visibilityLevel"
        },
        "consent_status": {
          "$ref": "#/$defs/consentStatus"
        },
        "consent_event_ref": {
          "type": "string",
          "maxLength": 256
        }
      }
    },
    "operation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "op": {
          "$ref": "#/$defs/opType"
        },
        "input_hash": {
          "$ref": "#/$defs/sha256Ref"
        },
        "output_hash": {
          "$ref": "#/$defs/sha256Ref"
        },
        "notes": {
          "type": "string",
          "maxLength": 200
        }
      },
      "required": [
        "op"
      ]
    },
    "links": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "parent_artifact_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/nonEmptyString"
          },
          "maxItems": 64
        },
        "related_artifact_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/nonEmptyString"
          },
          "maxItems": 64
        },
        "external_refs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/nonEmptyString"
          },
          "maxItems": 64
        }
      }
    },
    "trail": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "event_id": {
          "allOf": [
            {
              "$ref": "#/$defs/nonEmptyString"
            },
            {
              "maxLength": 128
            }
          ]
        },
        "ts": {
          "$ref": "#/$defs/unixTimeInt"
        },
        "actor": {
          "$ref": "#/$defs/actor"
        },
        "source": {
          "$ref": "#/$defs/source"
        },
        "context": {
          "$ref": "#/$defs/context"
        },
        "operation": {
          "$ref": "#/$defs/operation"
        },
        "versions": {
          "$ref": "#/$defs/versions"
        },
        "links": {
          "$ref": "#/$defs/links"
        }
      },
      "required": [
        "event_id",
        "ts",
        "actor",
        "source",
        "operation",
        "versions"
      ]
    }
  }
}
