{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agentdirective.org/schemas/control-pack-v1.schema.json",
  "title": "AgentDirective Control Pack v1",
  "description": "A versioned, machine-readable safeguard with applicability, questions, requirements, evidence, and verification expectations.",
  "type": "object",
  "additionalProperties": false,
  "required": ["pack_id", "version", "title", "categories", "applies_when", "failure_modes", "control_objective", "questions", "requirements", "verification", "prompt_template"],
  "properties": {
    "$schema": { "type": "string", "format": "uri-reference" },
    "pack_id": { "$ref": "#/$defs/packId" },
    "version": { "$ref": "#/$defs/semver" },
    "title": { "type": "string", "minLength": 3, "maxLength": 160 },
    "summary": { "type": "string", "minLength": 1, "maxLength": 1000 },
    "status": { "enum": ["draft", "review", "released", "deprecated"] },
    "categories": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/token" } },
    "applies_when": { "$ref": "#/$defs/condition" },
    "priority": { "type": "integer", "minimum": 1, "maximum": 5 },
    "failure_modes": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "title", "description"],
        "properties": {
          "id": { "$ref": "#/$defs/token" },
          "title": { "type": "string", "minLength": 3, "maxLength": 160 },
          "description": { "type": "string", "minLength": 1, "maxLength": 2000 }
        }
      }
    },
    "control_objective": { "type": "string", "minLength": 1, "maxLength": 3000 },
    "questions": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "type", "text"],
        "properties": {
          "id": { "$ref": "#/$defs/localId" },
          "type": { "enum": ["choice", "threshold", "scope", "confirmation", "multi_select", "unresolved"] },
          "text": { "type": "string", "minLength": 1, "maxLength": 1000 },
          "why": { "type": "string", "minLength": 1, "maxLength": 2000 },
          "required_when": { "$ref": "#/$defs/condition" },
          "choices": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "type": ["string", "number", "boolean" ] } },
          "unit": { "type": "string", "minLength": 1, "maxLength": 40 },
          "allows_custom": { "type": "boolean", "default": true },
          "allows_unresolved": { "type": "boolean", "const": true, "default": true },
          "requirement_refs": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/localId" } }
        }
      }
    },
    "requirements": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "statement"],
        "properties": {
          "id": { "$ref": "#/$defs/localId" },
          "statement": { "type": "string", "minLength": 1, "maxLength": 3000 },
          "severity": { "enum": ["low", "moderate", "high", "critical"] },
          "parameter_schema": { "type": "object" },
          "observability_refs": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/token" } },
          "test_refs": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/token" } }
        }
      }
    },
    "observability": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "required_events": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/token" } },
        "required_fields": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/token" } },
        "excluded_fields": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/token" } }
      }
    },
    "verification": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required_tests", "evidence_expected"],
      "properties": {
        "required_tests": { "type": "array", "items": { "$ref": "#/$defs/testExpectation" } },
        "evidence_expected": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "enum": ["configuration_or_policy", "implementation_location", "test_result", "telemetry_definition", "review_record", "operational_procedure", "other"] } }
      }
    },
    "prompt_template": { "type": "string", "minLength": 1, "maxLength": 5000 },
    "evidence_refs": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/sourceRef" } },
    "control_mappings": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["framework_id", "control_id", "control_title", "relationship", "rationale", "source_id"],
        "properties": {
          "framework_id": { "$ref": "#/$defs/token" },
          "control_id": { "type": "string", "pattern": "^[A-Z][A-Z0-9 .()_-]{1,63}$" },
          "control_title": { "type": "string", "minLength": 3, "maxLength": 200 },
          "relationship": { "enum": ["supports", "partially_addresses", "informs"] },
          "rationale": { "type": "string", "minLength": 1, "maxLength": 1000 },
          "source_id": { "$ref": "#/$defs/token" }
        }
      }
    },
    "incident_pattern_refs": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/token" } },
    "review": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "technical_reviewed_at": { "type": "string", "format": "date-time" },
        "evidence_reviewed_at": { "type": "string", "format": "date-time" },
        "reviewers": { "type": "array", "items": { "type": "string", "minLength": 1, "maxLength": 120 } }
      }
    }
  },
  "$defs": {
    "token": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]{1,79}$" },
    "packId": { "type": "string", "pattern": "^[A-Z]{2,10}-[0-9]{3,6}$" },
    "localId": { "type": "string", "pattern": "^[A-Z]{2,10}-[0-9]{3,6}-(?:Q|R|T|O)[0-9]{1,4}$" },
    "semver": { "type": "string", "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$" },
    "condition": {
      "oneOf": [
        { "type": "object", "additionalProperties": false, "required": ["characteristic"], "properties": { "characteristic": { "$ref": "#/$defs/token" }, "equals": { "type": ["string", "number", "boolean", "null"] }, "in": { "type": "array", "minItems": 1, "items": { "type": ["string", "number", "boolean"] } } } },
        { "type": "object", "additionalProperties": false, "required": ["all"], "properties": { "all": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/condition" } } } },
        { "type": "object", "additionalProperties": false, "required": ["any"], "properties": { "any": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/condition" } } } },
        { "type": "object", "additionalProperties": false, "required": ["not"], "properties": { "not": { "$ref": "#/$defs/condition" } } }
      ]
    },
    "testExpectation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "description"],
      "properties": { "id": { "$ref": "#/$defs/token" }, "description": { "type": "string", "minLength": 1, "maxLength": 1000 } }
    },
    "sourceRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_id", "relationship"],
      "properties": {
        "source_id": { "$ref": "#/$defs/token" },
        "relationship": { "enum": ["authoritative_guidance", "supporting_guidance", "technical_research", "incident_fact", "analogous_incident"] }
      }
    }
  }
}
