{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://odexa.io/schemas/1.2.0-draft.3/observation.schema.json",
  "title": "Origin-declared observation coverage and known expectation records",
  "$comment": "Structural only. Verified same-origin acquisition, exact policy bytes, monotonic history, time/scope, authenticated intake and trusted expectation provenance require semantic checks. A declaration is not complete traffic capture or authority to access.",
  "$ref": "#/$defs/registry",
  "$defs": {
    "registry": {
      "type": "object",
      "properties": {
        "protocol_version": {
          "const": "1.2.0-draft.3"
        },
        "profile": {
          "const": "observation_coverage_v1"
        },
        "registry_id": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "origin": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "revision": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "policy_id": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "policy_revision": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "policy_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "issued_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "expires_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "boundaries": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/boundary"
          },
          "minItems": 0,
          "maxItems": 128,
          "uniqueItems": true
        }
      },
      "required": [
        "protocol_version",
        "profile",
        "registry_id",
        "origin",
        "revision",
        "policy_id",
        "policy_revision",
        "policy_digest",
        "issued_at",
        "expires_at",
        "boundaries"
      ],
      "additionalProperties": false
    },
    "boundary": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "kind": {
          "enum": [
            "gateway",
            "agent",
            "collector",
            "unobserved"
          ]
        },
        "observer_id": {
          "anyOf": [
            {
              "type": "string",
              "format": "uri",
              "pattern": "^https://",
              "maxLength": 4096
            },
            {
              "type": "null"
            }
          ]
        },
        "binding": {
          "anyOf": [
            {
              "$ref": "https://odexa.io/schemas/1.2.0-draft.3/free.schema.json#/$defs/binding"
            },
            {
              "type": "null"
            }
          ]
        },
        "resource_ids": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128
          },
          "minItems": 1,
          "maxItems": 512,
          "uniqueItems": true
        },
        "actions": {
          "type": "array",
          "items": {
            "enum": [
              "retrieve",
              "store",
              "transform",
              "redistribute"
            ]
          },
          "minItems": 1,
          "maxItems": 4,
          "uniqueItems": true
        },
        "purposes": {
          "type": "array",
          "items": {
            "enum": [
              "search_indexing",
              "public_retrieval",
              "internal_knowledge",
              "model_training",
              "model_fine_tuning",
              "redistribution"
            ]
          },
          "minItems": 1,
          "maxItems": 6,
          "uniqueItems": true
        },
        "starts_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "ends_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "state": {
          "enum": [
            "instrumented",
            "partial",
            "unknown",
            "unobserved"
          ]
        },
        "event_profiles": {
          "type": "array",
          "items": {
            "enum": [
              "core",
              "storage_sessions_v1"
            ]
          },
          "minItems": 0,
          "maxItems": 2,
          "uniqueItems": true
        }
      },
      "required": [
        "id",
        "kind",
        "observer_id",
        "binding",
        "resource_ids",
        "actions",
        "purposes",
        "starts_at",
        "ends_at",
        "state",
        "event_profiles"
      ],
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "unobserved"
              }
            }
          },
          "then": {
            "properties": {
              "observer_id": {
                "type": "null"
              },
              "binding": {
                "type": "null"
              },
              "event_profiles": {
                "type": "array",
                "maxItems": 0
              },
              "state": {
                "enum": [
                  "unobserved",
                  "unknown"
                ]
              }
            }
          },
          "else": {
            "properties": {
              "observer_id": {
                "type": "string",
                "format": "uri",
                "pattern": "^https://",
                "maxLength": 4096
              },
              "binding": {
                "$ref": "https://odexa.io/schemas/1.2.0-draft.3/free.schema.json#/$defs/binding"
              },
              "event_profiles": {
                "type": "array",
                "minItems": 1
              },
              "state": {
                "enum": [
                  "instrumented",
                  "partial",
                  "unknown"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "gateway"
              }
            }
          },
          "then": {
            "properties": {
              "event_profiles": {
                "const": [
                  "core"
                ]
              }
            }
          }
        }
      ]
    },
    "subject": {
      "type": "object",
      "properties": {
        "kind": {
          "enum": [
            "delivery",
            "action",
            "storage_start",
            "storage_checkpoint",
            "storage_cessation",
            "report"
          ]
        },
        "id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "reporter_id": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "ingress_id": {
          "anyOf": [
            {
              "type": "string",
              "format": "uri",
              "pattern": "^https://",
              "maxLength": 4096
            },
            {
              "type": "null"
            }
          ]
        },
        "checkpoint_index": {
          "anyOf": [
            {
              "type": "integer",
              "minimum": 1,
              "maximum": 9007199254740991
            },
            {
              "type": "null"
            }
          ]
        },
        "payload_digest": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^sha256:[0-9a-f]{64}$"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "kind",
        "id",
        "reporter_id",
        "ingress_id",
        "checkpoint_index",
        "payload_digest"
      ],
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "delivery"
              }
            }
          },
          "then": {
            "properties": {
              "ingress_id": {
                "type": "string",
                "format": "uri",
                "pattern": "^https://",
                "maxLength": 4096
              }
            }
          },
          "else": {
            "properties": {
              "ingress_id": {
                "type": "null"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "storage_checkpoint"
              }
            }
          },
          "then": {
            "properties": {
              "checkpoint_index": {
                "type": "integer",
                "minimum": 1,
                "maximum": 9007199254740991
              }
            }
          },
          "else": {
            "properties": {
              "checkpoint_index": {
                "type": "null"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "const": "report"
              }
            }
          },
          "then": {
            "properties": {
              "payload_digest": {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              }
            }
          },
          "else": {
            "properties": {
              "payload_digest": {
                "type": "null"
              }
            }
          }
        }
      ]
    },
    "expectation": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
        },
        "boundary_id": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "resource_url": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "action": {
          "enum": [
            "retrieve",
            "store",
            "transform",
            "redistribute"
          ]
        },
        "purpose": {
          "enum": [
            "search_indexing",
            "public_retrieval",
            "internal_knowledge",
            "model_training",
            "model_fine_tuning",
            "redistribution"
          ]
        },
        "agreement_id": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
            },
            {
              "type": "null"
            }
          ]
        },
        "expected_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "due_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "subject": {
          "$ref": "#/$defs/subject"
        },
        "event_profile": {
          "enum": [
            "core",
            "storage_sessions_v1"
          ]
        },
        "basis": {
          "enum": [
            "instrumented_ingress",
            "gateway_admission",
            "accepted_duty",
            "report_outbox"
          ]
        }
      },
      "required": [
        "id",
        "boundary_id",
        "resource_url",
        "action",
        "purpose",
        "agreement_id",
        "expected_at",
        "due_at",
        "subject",
        "event_profile",
        "basis"
      ],
      "additionalProperties": false
    },
    "query": {
      "type": "object",
      "properties": {
        "url": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "maxLength": 4096
        },
        "action": {
          "enum": [
            "retrieve",
            "store",
            "transform",
            "redistribute"
          ]
        },
        "purpose": {
          "enum": [
            "search_indexing",
            "public_retrieval",
            "internal_knowledge",
            "model_training",
            "model_fine_tuning",
            "redistribution"
          ]
        },
        "starts_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        },
        "ends_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
        }
      },
      "required": [
        "url",
        "action",
        "purpose",
        "starts_at",
        "ends_at"
      ],
      "additionalProperties": false
    }
  }
}
