{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gautierdorval.com/runtime-ledger-event.schema.json",
  "title": "Runtime ledger distribution event",
  "description": "Closed, content-free event transferred asynchronously from the runtime resolver to the isolated ledger writer.",
  "allOf": [
    {
      "$ref": "#/$defs/common"
    },
    {
      "oneOf": [
        {
          "properties": {
            "operation": {
              "enum": [
                "service_index",
                "health_check",
                "list_capabilities",
                "list_intents",
                "list_context_packs"
              ]
            },
            "targetResolution": {
              "const": "not_applicable"
            }
          },
          "not": {
            "anyOf": [
              {
                "required": [
                  "intentToken"
                ]
              },
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "resourceVersion"
                ]
              },
              {
                "required": [
                  "resourceDigest"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "const": "get_intent"
            },
            "targetResolution": {
              "const": "resolved"
            }
          },
          "required": [
            "intentToken",
            "resourceVersion",
            "resourceDigest"
          ],
          "not": {
            "anyOf": [
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "const": "get_intent"
            },
            "targetResolution": {
              "const": "unresolved"
            }
          },
          "not": {
            "anyOf": [
              {
                "required": [
                  "intentToken"
                ]
              },
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "resourceVersion"
                ]
              },
              {
                "required": [
                  "resourceDigest"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "enum": [
                "get_context_pack",
                "get_pack_manifest"
              ]
            },
            "targetResolution": {
              "const": "resolved"
            }
          },
          "required": [
            "intentToken",
            "packId",
            "lang",
            "resourceVersion",
            "resourceDigest"
          ],
          "not": {
            "anyOf": [
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "enum": [
                "get_context_pack",
                "get_pack_manifest"
              ]
            },
            "targetResolution": {
              "const": "unresolved"
            }
          },
          "not": {
            "anyOf": [
              {
                "required": [
                  "intentToken"
                ]
              },
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "resourceVersion"
                ]
              },
              {
                "required": [
                  "resourceDigest"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "const": "get_attested_unit"
            },
            "targetResolution": {
              "const": "resolved"
            }
          },
          "required": [
            "unitId",
            "resourceVersion",
            "resourceDigest"
          ],
          "not": {
            "anyOf": [
              {
                "required": [
                  "intentToken"
                ]
              },
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "const": "get_attested_unit"
            },
            "targetResolution": {
              "const": "unresolved"
            }
          },
          "not": {
            "anyOf": [
              {
                "required": [
                  "intentToken"
                ]
              },
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "resourceVersion"
                ]
              },
              {
                "required": [
                  "resourceDigest"
                ]
              },
              {
                "required": [
                  "unknownOperation"
                ]
              }
            ]
          }
        },
        {
          "properties": {
            "operation": {
              "const": "unknown_operation"
            },
            "targetResolution": {
              "const": "not_applicable"
            }
          },
          "required": [
            "unknownOperation"
          ],
          "not": {
            "anyOf": [
              {
                "required": [
                  "intentToken"
                ]
              },
              {
                "required": [
                  "packId"
                ]
              },
              {
                "required": [
                  "unitId"
                ]
              },
              {
                "required": [
                  "lang"
                ]
              },
              {
                "required": [
                  "resourceVersion"
                ]
              },
              {
                "required": [
                  "resourceDigest"
                ]
              }
            ]
          }
        }
      ]
    }
  ],
  "unevaluatedProperties": false,
  "$defs": {
    "common": {
      "type": "object",
      "required": [
        "schemaVersion",
        "type",
        "eventId",
        "timestamp",
        "surface",
        "httpMethod",
        "operation",
        "targetResolution",
        "httpStatus",
        "latencyMs",
        "latencyBucket",
        "runtimeVersion",
        "corpusVersion",
        "deploymentId",
        "declaredClient",
        "networkObservation",
        "retentionPolicyId"
      ],
      "properties": {
        "schemaVersion": {
          "const": "0.2.0"
        },
        "type": {
          "const": "RuntimeLedgerEvent"
        },
        "eventId": {
          "type": "string",
          "pattern": "^evt_[a-f0-9]{32}$"
        },
        "timestamp": {
          "type": "string",
          "format": "date-time"
        },
        "surface": {
          "const": "http"
        },
        "httpMethod": {
          "enum": [
            "GET",
            "HEAD",
            "OPTIONS",
            "OTHER"
          ]
        },
        "operation": {
          "enum": [
            "service_index",
            "health_check",
            "list_capabilities",
            "list_intents",
            "get_intent",
            "list_context_packs",
            "get_context_pack",
            "get_pack_manifest",
            "get_attested_unit",
            "unknown_operation"
          ]
        },
        "targetResolution": {
          "enum": [
            "not_applicable",
            "resolved",
            "unresolved"
          ]
        },
        "intentToken": {
          "enum": [
            "representation-gap",
            "prelaunch-semantic-analysis",
            "product-doctrine-authority-separation",
            "interpretive-governance-overview",
            "situational-applicability-overview",
            "source-authority-and-integrity"
          ]
        },
        "packId": {
          "type": "string",
          "pattern": "^gd-pack-[a-z0-9-]+-(?:en|fr)-v0$"
        },
        "unitId": {
          "type": "string",
          "pattern": "^gd-[a-z0-9-]+$"
        },
        "lang": {
          "enum": [
            "fr",
            "en"
          ]
        },
        "resourceVersion": {
          "type": "string",
          "pattern": "^(?:unversioned|[0-9]+\\.[0-9]+\\.[0-9]+(?:-[a-z0-9.]+)?)$"
        },
        "resourceDigest": {
          "type": "string",
          "pattern": "^sha-256:[a-f0-9]{64}$"
        },
        "httpStatus": {
          "type": "integer",
          "minimum": 100,
          "maximum": 599
        },
        "latencyMs": {
          "type": "integer",
          "minimum": 0,
          "maximum": 300000
        },
        "latencyBucket": {
          "enum": [
            "0-9",
            "10-24",
            "25-49",
            "50-99",
            "100-249",
            "250-499",
            "500-999",
            "1000-2999",
            "3000-plus"
          ]
        },
        "errorCode": {
          "enum": [
            "runtime_binding_unavailable",
            "rate_limit_exceeded",
            "invalid_language",
            "intent_not_found",
            "pack_not_found",
            "unit_not_found",
            "route_not_found",
            "method_not_allowed",
            "cors_header_not_allowed",
            "runtime_integrity_failure",
            "url_too_long",
            "request_body_not_allowed",
            "unknown_parameter",
            "duplicate_parameter",
            "unsupported_parameter",
            "version_not_found"
          ]
        },
        "unknownOperation": {
          "enum": [
            "unknown_http_route",
            "unsupported_http_method",
            "invalid_http_request"
          ]
        },
        "runtimeVersion": {
          "type": "string",
          "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
        },
        "corpusVersion": {
          "type": "string",
          "pattern": "^[A-Za-z0-9.+_-]{1,96}$"
        },
        "deploymentId": {
          "type": "string",
          "pattern": "^(?:unknown|[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$"
        },
        "declaredClient": {
          "$ref": "#/$defs/declaredClient"
        },
        "networkObservation": {
          "$ref": "#/$defs/networkObservation"
        },
        "retentionPolicyId": {
          "const": "runtime-ledger-v2-aggregate-only"
        }
      }
    },
    "declaredClient": {
      "type": "object",
      "required": [
        "family",
        "name",
        "type",
        "confidence"
      ],
      "properties": {
        "family": {
          "enum": [
            "operator-probe",
            "openai",
            "anthropic",
            "perplexity",
            "google",
            "microsoft",
            "apple",
            "meta",
            "common-crawl",
            "amazon",
            "mistral",
            "cohere",
            "you.com",
            "bytespider",
            "generic-crawler",
            "unknown"
          ]
        },
        "name": {
          "enum": [
            "Runtime-Ledger-Activation-Probe",
            "GPTBot",
            "ChatGPT-User",
            "OAI-SearchBot",
            "ClaudeBot",
            "Claude-User",
            "Claude-SearchBot",
            "PerplexityBot",
            "Perplexity-User",
            "Google-Agent",
            "Google-GeminiNotebook",
            "Google-NotebookLM",
            "Google-CloudVertexBot",
            "GoogleOther",
            "Googlebot",
            "Bingbot",
            "Applebot",
            "Meta-ExternalAgent",
            "FacebookBot",
            "CCBot",
            "Amazonbot",
            "MistralAI",
            "Cohere",
            "YouBot",
            "Bytespider",
            "suspected-ai-crawler",
            "generic-crawler",
            "unknown"
          ]
        },
        "type": {
          "enum": [
            "synthetic_probe",
            "ai_training",
            "ai_agent",
            "ai_search",
            "ai_policy",
            "ai_crawler",
            "training_crawler",
            "search_crawler",
            "crawler",
            "suspected_ai_crawler",
            "generic_crawler",
            "unknown"
          ]
        },
        "confidence": {
          "enum": [
            "declared",
            "suspected",
            "unknown"
          ]
        }
      },
      "additionalProperties": false
    },
    "networkObservation": {
      "type": "object",
      "required": [
        "class",
        "source",
        "confidence"
      ],
      "properties": {
        "class": {
          "enum": [
            "verified_bot",
            "unknown"
          ]
        },
        "source": {
          "enum": [
            "cloudflare_verified_bot",
            "cloudflare_verified_bot_category",
            "none"
          ]
        },
        "confidence": {
          "enum": [
            "verified",
            "none"
          ]
        }
      },
      "additionalProperties": false
    }
  },
  "not": {
    "anyOf": [
      {
        "required": [
          "rawIp"
        ]
      },
      {
        "required": [
          "ip"
        ]
      },
      {
        "required": [
          "hashedIp"
        ]
      },
      {
        "required": [
          "query"
        ]
      },
      {
        "required": [
          "prompt"
        ]
      },
      {
        "required": [
          "response"
        ]
      },
      {
        "required": [
          "conversation"
        ]
      },
      {
        "required": [
          "cookie"
        ]
      },
      {
        "required": [
          "email"
        ]
      },
      {
        "required": [
          "username"
        ]
      },
      {
        "required": [
          "rawUserAgent"
        ]
      },
      {
        "required": [
          "userAgent"
        ]
      },
      {
        "required": [
          "referrerUrl"
        ]
      },
      {
        "required": [
          "requestBody"
        ]
      },
      {
        "required": [
          "authorizationHeader"
        ]
      },
      {
        "required": [
          "personalIdentity"
        ]
      },
      {
        "required": [
          "asn"
        ]
      },
      {
        "required": [
          "country"
        ]
      },
      {
        "required": [
          "cfRay"
        ]
      }
    ]
  }
}
