{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:agent-platform:guardrail-runtime:bundle:v1",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schemaVersion": {
      "const": "agent-platform.guardrail-runtime.bundle/v1"
    },
    "bundleId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
    },
    "revision": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time",
      "maxLength": 35,
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,6})?(Z|[+-]\\d{2}:\\d{2})$"
    },
    "contentProfile": {
      "enum": [
        "agentgateway-1.4.1-text-webhook-v1",
        "agentgateway-1.5.0-text-webhook-v1",
        "agentgateway-1.5.0-chat-webhook-v2"
      ]
    },
    "effectivePolicies": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/Policy"
      },
      "minItems": 1,
      "maxItems": 32
    },
    "providers": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "id": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              },
              "revision": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              },
              "protocol": {
                "const": "agent-platform.guardrail-runtime.detector/v1"
              },
              "url": {
                "type": "string",
                "minLength": 1,
                "maxLength": 2048
              },
              "credentialRef": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              },
              "maxResponseBytes": {
                "type": "integer",
                "minimum": 64,
                "maximum": 131072
              },
              "maxInputBytes": {
                "type": "integer",
                "minimum": 1,
                "maximum": 131072
              }
            },
            "required": [
              "id",
              "revision",
              "protocol",
              "url",
              "credentialRef",
              "maxResponseBytes",
              "maxInputBytes"
            ]
          },
          {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "id": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              },
              "revision": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              },
              "protocol": {
                "enum": [
                  "openai.chat-completions.classifier/v1",
                  "deepseek.chat-completions.classifier/v1"
                ]
              },
              "url": {
                "type": "string",
                "minLength": 1,
                "maxLength": 2048
              },
              "credentialRef": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              },
              "maxResponseBytes": {
                "type": "integer",
                "minimum": 64,
                "maximum": 131072
              },
              "maxInputBytes": {
                "type": "integer",
                "minimum": 1,
                "maximum": 131072
              },
              "model": {
                "type": "string",
                "minLength": 1,
                "maxLength": 200
              },
              "modelRevision": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128,
                "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
              }
            },
            "required": [
              "id",
              "revision",
              "protocol",
              "url",
              "credentialRef",
              "maxResponseBytes",
              "maxInputBytes",
              "model",
              "modelRevision"
            ]
          }
        ]
      },
      "minItems": 0,
      "maxItems": 32
    },
    "limits": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "maxBodyBytes": {
          "type": "integer",
          "minimum": 256,
          "maximum": 2097152
        },
        "maxMessages": {
          "type": "integer",
          "minimum": 1,
          "maximum": 64
        },
        "maxMatches": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1024
        },
        "maxConcurrency": {
          "type": "integer",
          "minimum": 1,
          "maximum": 256
        },
        "queueSize": {
          "const": 0
        },
        "maxInFlightBytes": {
          "type": "integer",
          "minimum": 256,
          "maximum": 33554432
        }
      },
      "required": [
        "maxBodyBytes",
        "maxMessages",
        "maxMatches",
        "maxConcurrency",
        "queueSize"
      ]
    }
  },
  "required": [
    "schemaVersion",
    "bundleId",
    "revision",
    "createdAt",
    "contentProfile",
    "effectivePolicies",
    "providers",
    "limits"
  ],
  "$defs": {
    "Rule": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "revision": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "mode": {
              "enum": [
                "enforce",
                "observe"
              ]
            },
            "onMatch": {
              "enum": [
                "deny",
                "redact"
              ]
            },
            "input": {
              "enum": [
                "original"
              ]
            },
            "timeoutMs": {
              "type": "integer",
              "minimum": 1,
              "maximum": 5000
            },
            "kind": {
              "const": "regex"
            },
            "expression": {
              "type": "string",
              "minLength": 1,
              "maxLength": 4096
            },
            "replacement": {
              "type": "string",
              "maxLength": 128
            },
            "engine": {
              "const": "google-re2-1.1.20251105"
            }
          },
          "required": [
            "id",
            "revision",
            "mode",
            "onMatch",
            "input",
            "timeoutMs",
            "kind",
            "expression",
            "replacement",
            "engine"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "revision": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "mode": {
              "enum": [
                "enforce",
                "observe"
              ]
            },
            "onMatch": {
              "enum": [
                "deny",
                "redact"
              ]
            },
            "input": {
              "enum": [
                "original"
              ]
            },
            "timeoutMs": {
              "type": "integer",
              "minimum": 1,
              "maximum": 5000
            },
            "kind": {
              "const": "stop_list"
            },
            "terms": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1,
                "maxLength": 256
              },
              "minItems": 1,
              "maxItems": 512
            },
            "normalization": {
              "const": "none"
            },
            "case": {
              "enum": [
                "sensitive",
                "unicode-simple-fold"
              ]
            },
            "matching": {
              "enum": [
                "substring",
                "token"
              ]
            },
            "replacement": {
              "type": "string",
              "maxLength": 128
            }
          },
          "required": [
            "id",
            "revision",
            "mode",
            "onMatch",
            "input",
            "timeoutMs",
            "kind",
            "terms",
            "normalization",
            "case",
            "matching",
            "replacement"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "revision": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "mode": {
              "enum": [
                "enforce",
                "observe"
              ]
            },
            "onMatch": {
              "enum": [
                "deny",
                "redact"
              ]
            },
            "input": {
              "enum": [
                "original"
              ]
            },
            "timeoutMs": {
              "type": "integer",
              "minimum": 1,
              "maximum": 5000
            },
            "kind": {
              "const": "external_pipeline"
            },
            "providerRef": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            }
          },
          "required": [
            "id",
            "revision",
            "mode",
            "onMatch",
            "input",
            "timeoutMs",
            "kind",
            "providerRef"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "id": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "revision": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "mode": {
              "enum": [
                "enforce",
                "observe"
              ]
            },
            "onMatch": {
              "enum": [
                "deny",
                "redact"
              ]
            },
            "input": {
              "enum": [
                "original"
              ]
            },
            "timeoutMs": {
              "type": "integer",
              "minimum": 1,
              "maximum": 5000
            },
            "kind": {
              "const": "llm_classifier"
            },
            "providerRef": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
            },
            "prompt": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "id",
                "revision",
                "instruction"
              ],
              "properties": {
                "id": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 128,
                  "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
                },
                "revision": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 128,
                  "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
                },
                "instruction": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 8192
                }
              }
            },
            "maxCompletionTokens": {
              "type": "integer",
              "minimum": 32,
              "maximum": 4096
            }
          },
          "required": [
            "id",
            "revision",
            "mode",
            "onMatch",
            "input",
            "timeoutMs",
            "kind",
            "providerRef",
            "prompt",
            "maxCompletionTokens"
          ]
        }
      ]
    },
    "Stage": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "deadlineMs": {
          "type": "integer",
          "minimum": 1,
          "maximum": 5000
        },
        "baseline": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/Rule"
          },
          "minItems": 1,
          "maxItems": 64
        },
        "additional": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/Rule"
          },
          "minItems": 0,
          "maxItems": 64
        }
      },
      "required": [
        "deadlineMs",
        "baseline",
        "additional"
      ]
    },
    "Policy": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
        },
        "revision": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]*$"
        },
        "request": {
          "$ref": "#/$defs/Stage"
        },
        "response": {
          "$ref": "#/$defs/Stage"
        }
      },
      "required": [
        "id",
        "revision",
        "request",
        "response"
      ]
    }
  }
}
