{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.corva.ai/fe-ai-tools/protocol-result/v1",
  "title": "Ask Corva fleet CLI result",
  "oneOf": [
    { "$ref": "#/$defs/doctor" },
    { "$ref": "#/$defs/fleetInspection" },
    { "$ref": "#/$defs/migrationValidation" },
    { "$ref": "#/$defs/migrationPlan" },
    { "$ref": "#/$defs/error" }
  ],
  "$defs": {
    "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "gitSha": { "type": "string", "pattern": "^[a-f0-9]{40,64}$" },
    "issue": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message"],
      "properties": {
        "code": {
          "enum": [
            "ALLOWED_PATHS_INVALID",
            "ALLOWED_PATH_DUPLICATE",
            "ALLOWED_PATH_INVALID",
            "APP_KEY_INVALID",
            "APP_KEY_MISMATCH",
            "APP_MANIFEST_MISSING",
            "BLOCK_REASON_REQUIRED",
            "CANON_REVISION_MISMATCH",
            "CANON_STAMP_MISMATCH",
            "CANON_STAMP_MISSING",
            "COMMAND_FAILED",
            "CHECKOUT_ROOT_MISSING",
            "CONTRACT_REVISION_MISMATCH",
            "COVERAGE_APPLICABILITY_INVALID",
            "COVERAGE_BLOCK_REASON_REQUIRED",
            "COVERAGE_CAPABILITIES_INVALID",
            "COVERAGE_EVIDENCE_INVALID",
            "COVERAGE_EVIDENCE_MISSING",
            "COVERAGE_INVALID",
            "COVERAGE_STATUS_INVALID",
            "COVERAGE_TOOLS_INVALID",
            "CONTROL_PATH_ESCAPE",
            "CONTROL_PATH_SYMLINK",
            "DEPENDENCY_ONLY_PACKAGE_REQUIRED",
            "DEPENDENCY_ONLY_PATH_INVALID",
            "DEPENDENCY_ONLY_POLICY_INVALID",
            "DEPENDENCY_ONLY_REVISION_CHANGE",
            "FIELD_TYPE_INVALID",
            "FLEET_PAUSED",
            "GIT_HEAD_MISMATCH",
            "GIT_ORIGIN_MISMATCH",
            "GIT_ORIGIN_MISSING",
            "GIT_PUSH_ORIGIN_MISMATCH",
            "GIT_REPOSITORY_REQUIRED",
            "GIT_ROOT_MISMATCH",
            "GIT_ROOT_SYMLINK",
            "GIT_WORKTREE_DIRTY",
            "IMPACT_INVALID",
            "KIT_DOWNGRADE_FORBIDDEN",
            "KIT_RANGE_INVALID",
            "KIT_RANGE_MISMATCH",
            "LOCKFILE_MISSING",
            "LOCKFILE_POLICY_MISMATCH",
            "LOCKFILE_TOOLKIT_MISMATCH",
            "OBSERVATIONS_INVALID",
            "MANIFEST_MISSING",
            "MANIFEST_TYPE_INVALID",
            "MIGRATION_BOUND_INVALID",
            "MIGRATION_ID_INVALID",
            "MIGRATION_TYPE_INVALID",
            "MIGRATION_UPGRADE_REQUIRED",
            "PACKAGE_JSON_MISSING",
            "PACKAGE_JSON_INVALID",
            "PACKAGE_MANAGER_MISMATCH",
            "PACKAGE_MANAGER_VERSION_REQUIRED",
            "PACKAGE_MANAGER_VERSION_UNSUPPORTED",
            "PAUSE_REASON_REQUIRED",
            "PROFILE_INVALID",
            "REGISTRY_ENTRY_INVALID",
            "REGISTRY_PROFILE_MISMATCH",
            "REGISTRY_REPOSITORY_MISMATCH",
            "REGISTRY_TYPE_INVALID",
            "REPOSITORIES_INVALID",
            "REPOSITORY_DUPLICATE",
            "REPOSITORY_INVALID",
            "REPOSITORY_NOT_REGISTERED",
            "REVISION_DOWNGRADE_FORBIDDEN",
            "REVISION_INVALID",
            "ROLLOUT_BLOCKED",
            "ROLLOUT_GROUP_INVALID",
            "SCHEMA_VERSION_UNSUPPORTED",
            "SEMANTIC_EVIDENCE_REQUIRED",
            "SENSITIVE_PATH_FORBIDDEN",
            "TOOLKIT_DEPENDENCY_MISSING",
            "TOOLKIT_DEPENDENCY_DUPLICATE",
            "TOOL_ROOTS_INVALID",
            "TOOL_ROOT_DUPLICATE",
            "TOOL_ROOT_ESCAPE",
            "TOOL_ROOT_MISSING",
            "TOOL_ROOT_PATH_INVALID",
            "TOOL_ROOT_README_MISSING",
            "UNKNOWN_FIELD",
            "USAGE_ERROR",
            "VERIFICATION_INVALID",
            "VERIFY_COMMAND_INVALID",
            "VERIFY_SCRIPT_MISSING",
            "WORKSPACE_MEMBER_UNDECLARED",
            "WORKSPACE_PACKAGE_JSON_MISSING",
            "WORKSPACE_ROOT_ESCAPE"
          ]
        },
        "path": { "type": "string", "minLength": 1 },
        "message": { "type": "string", "minLength": 1 }
      }
    },
    "issues": { "type": "array", "items": { "$ref": "#/$defs/issue" } },
    "dependency": {
      "type": ["object", "null"],
      "properties": {
        "field": { "enum": ["dependencies", "devDependencies", "peerDependencies"] },
        "range": { "type": "string", "minLength": 1 },
        "resolvedVersion": { "type": ["string", "null"] }
      },
      "required": ["field", "range", "resolvedVersion"],
      "additionalProperties": false
    },
    "lockfile": {
      "type": ["object", "null"],
      "properties": {
        "path": { "enum": ["yarn.lock", "package-lock.json", "pnpm-lock.yaml"] },
        "digest": { "$ref": "#/$defs/sha256" },
        "resolvedVersion": { "type": ["string", "null"] }
      },
      "required": ["path", "digest", "resolvedVersion"],
      "additionalProperties": false
    },
    "checkout": {
      "type": "object",
      "additionalProperties": false,
      "required": ["head", "origin", "pushOrigin", "clean", "issues"],
      "properties": {
        "head": { "anyOf": [{ "$ref": "#/$defs/gitSha" }, { "type": "null" }] },
        "origin": { "type": ["string", "null"] },
        "pushOrigin": { "type": ["string", "null"] },
        "clean": { "type": "boolean" },
        "issues": { "$ref": "#/$defs/issues" }
      }
    },
    "doctor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "kind", "status", "root", "manifestPath", "issues"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "kind": { "const": "doctor-result" },
        "status": { "enum": ["READY", "INVALID", "MISSING_MANIFEST"] },
        "root": { "type": "string", "minLength": 1 },
        "workspaceRoot": { "type": "string", "minLength": 1 },
        "manifestPath": { "type": "string", "minLength": 1 },
        "manifest": { "type": "object" },
        "dependency": { "$ref": "#/$defs/dependency" },
        "lockfile": { "$ref": "#/$defs/lockfile" },
        "packageManager": { "type": ["string", "null"] },
        "issues": { "$ref": "#/$defs/issues" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "READY" } } },
          "then": {
            "required": ["manifest", "dependency", "lockfile"],
            "properties": {
              "dependency": { "type": "object" },
              "lockfile": { "type": "object" },
              "issues": { "type": "array", "maxItems": 0 }
            }
          },
          "else": { "properties": { "issues": { "type": "array", "minItems": 1 } } }
        }
      ]
    },
    "fleetRepository": {
      "type": "object",
      "additionalProperties": false,
      "required": ["repository", "profile", "rolloutGroup", "status", "root", "issues"],
      "properties": {
        "repository": { "type": "string", "pattern": "^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$" },
        "profile": { "enum": ["dc-app", "platform-app"] },
        "rolloutGroup": { "enum": ["canary", "early", "standard", "blocked"] },
        "reason": { "type": "string", "minLength": 1 },
        "status": { "enum": ["READY", "INVALID", "NOT_CHECKED_OUT", "BLOCKED_BY_POLICY", "FLEET_PAUSED"] },
        "root": { "type": "string", "minLength": 1 },
        "manifestPath": { "type": "string", "minLength": 1 },
        "manifest": { "type": "object" },
        "dependency": { "$ref": "#/$defs/dependency" },
        "lockfile": { "$ref": "#/$defs/lockfile" },
        "packageManager": { "type": ["string", "null"] },
        "checkout": { "$ref": "#/$defs/checkout" },
        "issues": { "$ref": "#/$defs/issues" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "READY" } } },
          "then": {
            "required": ["manifest", "dependency", "lockfile", "checkout"],
            "properties": {
              "dependency": { "type": "object" },
              "lockfile": { "type": "object" },
              "issues": { "type": "array", "maxItems": 0 },
              "checkout": {
                "type": "object",
                "properties": {
                  "head": { "$ref": "#/$defs/gitSha" },
                  "origin": { "type": "string", "minLength": 1 },
                  "pushOrigin": { "type": "string", "minLength": 1 },
                  "clean": { "const": true },
                  "issues": { "type": "array", "maxItems": 0 }
                }
              }
            }
          }
        },
        {
          "if": { "properties": { "status": { "const": "INVALID" } } },
          "then": { "properties": { "issues": { "type": "array", "minItems": 1 } } }
        }
      ]
    },
    "fleetInspection": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "kind", "registry", "checkouts", "paused", "summary", "repositories"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "kind": { "const": "fleet-inspection-result" },
        "registry": { "type": "string", "minLength": 1 },
        "checkouts": { "type": "string", "minLength": 1 },
        "paused": { "type": "boolean" },
        "pauseReason": { "type": "string", "minLength": 1 },
        "summary": { "type": "object", "additionalProperties": { "type": "integer", "minimum": 0 } },
        "repositories": { "type": "array", "items": { "$ref": "#/$defs/fleetRepository" } }
      },
      "allOf": [
        {
          "if": { "properties": { "paused": { "const": true } } },
          "then": { "required": ["pauseReason"] }
        }
      ]
    },
    "migrationValidation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "kind", "status", "migration", "descriptorDigest", "issues"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "kind": { "const": "migration-validation-result" },
        "status": { "enum": ["VALID", "UPGRADE_REQUIRED"] },
        "migration": { "type": ["string", "null"] },
        "descriptorDigest": { "$ref": "#/$defs/sha256" },
        "issues": { "$ref": "#/$defs/issues" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "VALID" } } },
          "then": {
            "properties": {
              "migration": { "type": "string", "minLength": 1 },
              "issues": { "type": "array", "maxItems": 0 }
            }
          },
          "else": { "properties": { "issues": { "type": "array", "minItems": 1 } } }
        }
      ]
    },
    "observedRegistry": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "digest", "rolloutGroup", "paused"],
      "properties": {
        "path": { "type": "string", "minLength": 1 },
        "digest": { "$ref": "#/$defs/sha256" },
        "rolloutGroup": { "type": ["string", "null"] },
        "paused": { "type": "boolean" }
      }
    },
    "observed": {
      "type": "object",
      "additionalProperties": false,
      "required": ["head", "origin", "pushOrigin", "clean", "kitRange", "resolvedKitVersion", "lockfile", "contractRevision", "canonRevision", "manifestDigest", "packageDigest", "registry"],
      "properties": {
        "head": { "anyOf": [{ "$ref": "#/$defs/gitSha" }, { "type": "null" }] },
        "origin": { "type": ["string", "null"] },
        "pushOrigin": { "type": ["string", "null"] },
        "clean": { "type": "boolean" },
        "kitRange": { "type": ["string", "null"] },
        "resolvedKitVersion": { "type": ["string", "null"] },
        "lockfile": { "$ref": "#/$defs/lockfile" },
        "contractRevision": { "type": ["integer", "null"], "minimum": 1 },
        "canonRevision": { "type": ["integer", "null"], "minimum": 1 },
        "manifestDigest": { "anyOf": [{ "$ref": "#/$defs/sha256" }, { "type": "null" }] },
        "packageDigest": { "anyOf": [{ "$ref": "#/$defs/sha256" }, { "type": "null" }] },
        "registry": { "$ref": "#/$defs/observedRegistry" }
      }
    },
    "migrationPlan": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "kind", "status", "migration", "descriptorDigest", "root", "impact", "allowedPaths", "requiresSourceReview", "requiresLiveValidation", "observed", "issues"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "kind": { "const": "migration-plan-result" },
        "status": { "enum": ["APPLICABLE", "NOT_APPLICABLE", "BLOCKED"] },
        "migration": { "type": "string", "minLength": 1 },
        "descriptorDigest": { "$ref": "#/$defs/sha256" },
        "root": { "type": "string", "minLength": 1 },
        "impact": { "enum": ["dependency-only", "mechanical", "semantic"] },
        "allowedPaths": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
        "requiresSourceReview": { "type": "boolean" },
        "requiresLiveValidation": { "type": "boolean" },
        "observed": { "$ref": "#/$defs/observed" },
        "issues": { "$ref": "#/$defs/issues" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "APPLICABLE" } } },
          "then": {
            "properties": {
              "issues": { "type": "array", "maxItems": 0 },
              "observed": {
                "type": "object",
                "properties": {
                  "head": { "$ref": "#/$defs/gitSha" },
                  "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", "minimum": 1 },
                  "canonRevision": { "type": "integer", "minimum": 1 },
                  "manifestDigest": { "$ref": "#/$defs/sha256" },
                  "packageDigest": { "$ref": "#/$defs/sha256" },
                  "registry": {
                    "type": "object",
                    "properties": {
                      "rolloutGroup": { "enum": ["canary", "early", "standard"] },
                      "paused": { "const": false }
                    }
                  }
                }
              }
            }
          },
          "else": { "properties": { "issues": { "type": "array", "minItems": 1 } } }
        }
      ]
    },
    "error": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "kind", "status", "issues"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "kind": { "const": "error-result" },
        "status": { "const": "ERROR" },
        "issues": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/issue" }
        }
      }
    }
  }
}
