{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/ri7in/rule-drift/main/schema/rule-drift.schema.json",
  "title": "RuleDrift configuration",
  "description": "A versioned suite of critical agent rules, conversations, and deterministic probes.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "name", "adapter", "rules", "scenarios"],
  "properties": {
    "$schema": { "type": "string", "format": "uri-reference" },
    "version": { "const": 1 },
    "name": { "type": "string", "minLength": 1 },
    "systemPrompt": { "type": "string" },
    "adapter": {
      "oneOf": [{ "$ref": "#/$defs/ollamaAdapter" }, { "$ref": "#/$defs/commandAdapter" }]
    },
    "rules": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/rule" }
    },
    "scenarios": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/scenario" }
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9._-]*$"
    },
    "timeout": {
      "type": "integer",
      "minimum": 1,
      "maximum": 600000
    },
    "ollamaAdapter": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "model"],
      "properties": {
        "type": { "const": "ollama" },
        "model": { "type": "string", "minLength": 1 },
        "baseUrl": { "type": "string", "format": "uri" },
        "think": { "type": "boolean" },
        "timeoutMs": { "$ref": "#/$defs/timeout" },
        "options": { "type": "object" }
      }
    },
    "commandAdapter": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "command"],
      "properties": {
        "type": { "const": "command" },
        "command": { "type": "string", "minLength": 1 },
        "args": { "type": "array", "items": { "type": "string" } },
        "cwd": { "type": "string", "minLength": 1 },
        "timeoutMs": { "$ref": "#/$defs/timeout" }
      }
    },
    "rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "instruction"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "instruction": { "type": "string", "minLength": 1 },
        "severity": { "enum": ["critical", "warning", "info"] }
      }
    },
    "assertion": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "value"],
          "properties": {
            "type": { "enum": ["contains", "notContains", "equals"] },
            "value": { "type": "string" },
            "caseSensitive": { "type": "boolean" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "pattern"],
          "properties": {
            "type": { "const": "matches" },
            "pattern": { "type": "string" },
            "flags": { "type": "string" }
          }
        }
      ]
    },
    "probe": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "ruleId", "afterTurn", "prompt", "assertions"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "ruleId": { "$ref": "#/$defs/id" },
        "afterTurn": { "type": "integer", "minimum": 0 },
        "prompt": { "type": "string", "minLength": 1 },
        "assertions": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/assertion" }
        }
      }
    },
    "scenario": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "turns", "probes"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "description": { "type": "string" },
        "turns": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["prompt"],
            "properties": { "prompt": { "type": "string", "minLength": 1 } }
          }
        },
        "probes": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/probe" }
        }
      }
    }
  }
}
