{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.corva.ai/fe-ai-tools/execution-packet/v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "kind", "status", "migration", "descriptorDigest", "root", "impact", "allowedPaths", "requiresSourceReview", "requiresLiveValidation", "observed", "issues", "branch", "pullRequest", "packetDigest"],
  "properties": {
    "schemaVersion": { "const": 1 }, "kind": { "const": "migration-execution-packet" },
    "status": { "enum": ["APPLICABLE", "BLOCKED", "NOT_APPLICABLE"] },
    "migration": { "type": "string" }, "descriptorDigest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "root": { "type": "string" }, "impact": { "enum": ["dependency-only", "mechanical", "semantic"] },
    "allowedPaths": { "type": "array", "minItems": 1, "uniqueItems": true, "items": { "type": "string" } },
    "requiresSourceReview": { "type": "boolean" }, "requiresLiveValidation": { "type": "boolean" },
    "observed": {
      "type": "object", "additionalProperties": false,
      "required": ["head", "origin", "pushOrigin", "clean", "kitRange", "resolvedKitVersion", "lockfile", "contractRevision", "canonRevision", "manifestDigest", "packageDigest", "registry"],
      "properties": {
        "head": { "type": ["string", "null"], "pattern": "^[a-f0-9]{40,64}$" },
        "origin": { "type": ["string", "null"] }, "pushOrigin": { "type": ["string", "null"] },
        "clean": { "type": "boolean" }, "kitRange": { "type": ["string", "null"] },
        "resolvedKitVersion": { "type": ["string", "null"] },
        "lockfile": {
          "type": ["object", "null"], "additionalProperties": false,
          "required": ["path", "digest", "resolvedVersion"],
          "properties": {
            "path": { "enum": ["yarn.lock", "package-lock.json", "pnpm-lock.yaml"] },
            "digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "resolvedVersion": { "type": ["string", "null"] }
          }
        },
        "contractRevision": { "type": ["integer", "null"], "minimum": 1 },
        "canonRevision": { "type": ["integer", "null"], "minimum": 1 },
        "manifestDigest": { "type": ["string", "null"], "pattern": "^[a-f0-9]{64}$" },
        "packageDigest": { "type": ["string", "null"], "pattern": "^[a-f0-9]{64}$" },
        "registry": {
          "type": "object", "additionalProperties": false,
          "required": ["path", "digest", "rolloutGroup", "paused"],
          "properties": {
            "path": { "type": "string", "minLength": 1 },
            "digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "rolloutGroup": { "type": ["string", "null"] }, "paused": { "type": "boolean" }
          }
        }
      }
    },
    "issues": {
      "type": "array",
      "items": {
        "type": "object", "additionalProperties": false, "required": ["code", "message"],
        "properties": {
          "code": { "type": "string", "pattern": "^[A-Z][A-Z0-9_]+$" },
          "path": { "type": "string", "minLength": 1 }, "message": { "type": "string", "minLength": 1 }
        }
      }
    },
    "branch": { "type": "string", "pattern": "^ai-tools/[a-z0-9][a-z0-9.-]+$" },
    "pullRequest": {
      "type": "object", "additionalProperties": false, "required": ["draft", "autoMerge", "title"],
      "properties": { "draft": { "const": true }, "autoMerge": { "const": false }, "title": { "type": "string", "minLength": 1 } }
    },
    "packetDigest": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "APPLICABLE" } } },
      "then": {
        "properties": {
          "issues": { "type": "array", "maxItems": 0 },
          "observed": {
            "type": "object",
            "properties": {
              "head": { "type": "string", "pattern": "^[a-f0-9]{40,64}$" },
              "origin": { "type": "string", "minLength": 1 },
              "pushOrigin": { "type": "string", "minLength": 1 }, "clean": { "const": true },
              "kitRange": { "type": "string", "minLength": 1 },
              "resolvedKitVersion": { "type": "string", "minLength": 1 },
              "lockfile": { "type": "object" }, "contractRevision": { "type": "integer" },
              "canonRevision": { "type": "integer" },
              "manifestDigest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
              "packageDigest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
              "registry": { "type": "object", "properties": { "rolloutGroup": { "enum": ["canary", "early", "standard"] }, "paused": { "const": false } } }
            }
          }
        }
      },
      "else": { "properties": { "issues": { "type": "array", "minItems": 1 } } }
    }
  ]
}
