{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.anguskit.com/schema/anguscopilot/1.0.0/quota.json",
  "title": "Quota Configuration",
  "description": "QuotaGuard 策略配置块。支持租户级预算、Capability 级限流、超时控制与告警。W12 增量字段：operators[]、priority.preemption。",
  "type": "object",
  "properties": {

    "tenantBudget": {
      "description": "租户级预算（FinOps）。格式：'<金额><币种>/<周期>'，如 '100USD/day'、'1000CNY/month'。 [stable]",
      "type": "string",
      "pattern": "^\\d+(?:\\.\\d+)?(USD|EUR|CNY|JPY)\\/(minute|hour|day|week|month)$",
      "examples": ["100USD/day", "1000CNY/month"]
    },

    "perCallTimeoutMs": {
      "description": "单次 Capability 调用超时（毫秒）。 [stable]",
      "type": "integer",
      "minimum": 100,
      "maximum": 600000,
      "default": 30000
    },

    "operators": {
      "description": "QuotaGuard 启用的算子列表（W12 新增；E10 约束：必须同时包含 bulkhead 与 rate-limit）。 [beta]",
      "type": "array",
      "items": {
        "type": "string",
        "enum": ["bulkhead", "rate-limit", "budget", "time-limit"]
      },
      "uniqueItems": true,
      "minItems": 2,
      "default": ["bulkhead", "rate-limit", "budget", "time-limit"],
      "$comment": "E10 跨字段约束：必须同时包含 bulkhead 和 rate-limit。",
      "allOf": [
        { "contains": { "const": "bulkhead" } },
        { "contains": { "const": "rate-limit" } }
      ]
    },

    "rateLimits": {
      "description": "通用限流配置（覆盖范围由 scope 决定）。 [stable]",
      "type": "array",
      "items": {
        "type": "object",
        "required": ["scope", "qps"],
        "properties": {
          "scope": {
            "type": "string",
            "enum": ["tenant", "agent", "capability", "model"]
          },
          "qps": { "type": "number", "exclusiveMinimum": 0 },
          "burst": { "type": "integer", "minimum": 1 },
          "concurrency": { "type": "integer", "minimum": 1 }
        },
        "patternProperties": { "^x-": true },
        "unevaluatedProperties": false
      }
    },

    "tokenBudget": {
      "description": "Token 级别预算。 [stable]",
      "type": "object",
      "properties": {
        "perCallMaxTokens": { "type": "integer", "minimum": 1 },
        "perSessionMaxTokens": { "type": "integer", "minimum": 1 },
        "perDayMaxTokens": { "type": "integer", "minimum": 1 }
      },
      "patternProperties": { "^x-": true },
      "unevaluatedProperties": false
    },

    "alerts": {
      "description": "配额阈值告警。 [stable]",
      "type": "array",
      "items": {
        "type": "object",
        "required": ["threshold", "channel"],
        "properties": {
          "threshold": {
            "description": "百分比阈值，命中则触发 QuotaThresholdEvent。 [stable]",
            "type": "integer",
            "minimum": 1,
            "maximum": 100
          },
          "channel": {
            "type": "string",
            "enum": ["slack", "lark", "dingtalk", "email", "webhook"]
          },
          "target": { "type": "string" }
        },
        "patternProperties": { "^x-": true },
        "unevaluatedProperties": false
      }
    },

    "priority": {
      "description": "优先级队列与抢占策略（W12 新增；与 §4.9.3 对齐）。 [beta]",
      "type": "object",
      "properties": {
        "enabled": { "type": "boolean", "default": false },
        "queues": {
          "description": "按优先级独立排队的级别。 [beta]",
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["CRITICAL", "HIGH", "NORMAL", "LOW"]
          },
          "uniqueItems": true,
          "default": ["CRITICAL", "HIGH", "NORMAL", "LOW"]
        },
        "preemption": {
          "description": "抢占策略（默认禁用，需显式开启）。 [beta]",
          "type": "object",
          "properties": {
            "enabled": { "type": "boolean", "default": false },
            "criticalCanPreemptLow": {
              "description": "preemption.enabled=true 时强制为 true（E9 约束）。 [beta]",
              "type": "boolean",
              "default": true
            }
          },
          "patternProperties": { "^x-": true },
          "$comment": "E9 跨字段约束：preemption.enabled=true 时必须 criticalCanPreemptLow=true。",
          "if": {
            "properties": { "enabled": { "const": true } },
            "required": ["enabled"]
          },
          "then": {
            "properties": {
              "criticalCanPreemptLow": { "const": true }
            },
            "required": ["criticalCanPreemptLow"]
          },
          "unevaluatedProperties": false
        }
      },
      "patternProperties": { "^x-": true },
      "unevaluatedProperties": false
    },

    "fallbackOnExceeded": {
      "description": "超额时的降级策略。 [stable]",
      "type": "string",
      "enum": ["reject", "queue", "fallback-model", "best-effort"],
      "default": "reject"
    }
  },
  "patternProperties": {
    "^x-": true
  },
  "unevaluatedProperties": false
}
