{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://quality.santi020k.com/quality-impact-plan.schema.json",
  "title": "Quality impact plan",
  "description": "Revision-bound plan. Verification additionally recomputes selection, dependency closure, batches, and the plan identity.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema_version", "quality_version", "plan_id", "config_path", "config", "source", "full_requested", "mode", "changed_paths", "reasons", "selected", "skipped", "batches"],
  "properties": {
    "schema_version": { "const": 1 },
    "quality_version": { "type": "string", "minLength": 1 },
    "plan_id": { "$ref": "#/$defs/planId" },
    "config_path": { "$ref": "quality-impact.schema.json#/$defs/filePath" },
    "config": { "$ref": "quality-impact.schema.json" },
    "source": { "$ref": "#/$defs/source" },
    "full_requested": { "type": "boolean" },
    "mode": { "enum": ["affected", "full"] },
    "changed_paths": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/gitPath" } },
    "reasons": { "type": "array", "uniqueItems": true, "items": { "$ref": "#/$defs/fullReason" } },
    "selected": { "type": "array", "items": { "$ref": "#/$defs/plannedNode" } },
    "skipped": { "type": "array", "items": { "$ref": "#/$defs/skippedNode" } },
    "batches": {
      "type": "array",
      "items": {
        "type": "array",
        "minItems": 1,
        "uniqueItems": true,
        "items": { "$ref": "quality-impact.schema.json#/$defs/nodeId" }
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "mode": { "const": "affected" } }, "required": ["mode"] },
      "then": { "properties": { "reasons": { "type": "array", "maxItems": 0 } } },
      "else": { "properties": { "reasons": { "type": "array", "minItems": 1 } } }
    },
    {
      "if": { "properties": { "full_requested": { "const": true } }, "required": ["full_requested"] },
      "then": { "properties": { "mode": { "const": "full" } } }
    }
  ],
  "$defs": {
    "commitId": { "type": "string", "pattern": "^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$(?![\\s\\S])" },
    "planId": { "type": "string", "pattern": "^git-blob:(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$(?![\\s\\S])" },
    "gitPath": {
      "type": "string",
      "minLength": 1,
      "description": "Git-relative path. Literal glob and control characters are data, not configuration patterns.",
      "not": { "pattern": "\\u0000|^/|//|/(?![\\s\\S])|(^|/)\\.{1,2}(?:/|(?![\\s\\S]))" }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["requested_base", "requested_head", "comparison", "base", "head", "merge_base"],
      "properties": {
        "requested_base": { "type": ["string", "null"] },
        "requested_head": { "type": "string" },
        "comparison": { "enum": ["merge-base", "direct"] },
        "base": { "anyOf": [{ "$ref": "#/$defs/commitId" }, { "type": "null" }] },
        "head": { "anyOf": [{ "$ref": "#/$defs/commitId" }, { "type": "null" }] },
        "merge_base": { "anyOf": [{ "$ref": "#/$defs/commitId" }, { "type": "null" }] }
      }
    },
    "fullReason": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind"],
          "properties": { "kind": { "enum": ["explicit_full", "head_unavailable", "base_unavailable", "merge_base_unavailable", "comparison_unavailable", "policy_changed"] } }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "path"],
          "properties": {
            "kind": { "enum": ["global_input", "unclassified_input"] },
            "path": { "$ref": "#/$defs/gitPath" }
          }
        }
      ]
    },
    "selectionReason": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind"],
          "properties": { "kind": { "enum": ["full", "always"] } }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "path"],
          "properties": { "kind": { "const": "input" }, "path": { "$ref": "#/$defs/gitPath" } }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "node"],
          "properties": {
            "kind": { "enum": ["dependent", "prerequisite"] },
            "node": { "$ref": "quality-impact.schema.json#/$defs/nodeId" }
          }
        }
      ]
    },
    "plannedNode": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "command", "args", "depends_on", "reasons"],
      "properties": {
        "id": { "$ref": "quality-impact.schema.json#/$defs/nodeId" },
        "command": { "$ref": "quality-impact.schema.json#/$defs/command" },
        "args": { "$ref": "quality-impact.schema.json#/$defs/args" },
        "working_directory": { "anyOf": [{ "$ref": "quality-impact.schema.json#/$defs/workingDirectory" }, { "type": "null" }] },
        "depends_on": { "$ref": "quality-impact.schema.json#/$defs/dependencies" },
        "reasons": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "$ref": "#/$defs/selectionReason" } }
      }
    },
    "skippedNode": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "reason"],
      "properties": {
        "id": { "$ref": "quality-impact.schema.json#/$defs/nodeId" },
        "reason": { "type": "string", "minLength": 1 }
      }
    }
  }
}
