{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gentle-ai.dev/contracts/review-integration/v2/schemas/start.schema.json",
  "title": "Gentle AI negotiated native Git review START response v3",
  "type": "object",
  "additionalProperties": false,
  "dependentRequired": {
    "target_mode": ["target_identity", "base_tree", "candidate_tree"],
    "target_identity": ["target_mode", "base_tree", "candidate_tree"],
    "base_tree": ["candidate_tree"],
    "candidate_tree": ["base_tree"],
    "changed_path_manifest": ["base_tree", "candidate_tree"]
  },
  "allOf": [
    {
      "if": {"properties": {"selected_lenses": {"minItems": 1}}, "required": ["selected_lenses"]},
      "then": {"required": ["base_tree", "candidate_tree", "changed_path_manifest"]}
    },
    {
      "if": {
        "properties": {"action": {"enum": ["created", "resumed"]}, "state": {"const": "reviewing"}},
        "required": ["action", "state"]
      },
      "then": {"required": ["repository_context"]},
      "else": {"not": {"required": ["repository_context"]}}
    }
  ],
  "required": [
    "schema", "contract", "operation", "action", "lenses_required", "lineage_id", "state", "risk_level",
    "selected_lenses", "projection", "changed_files", "changed_lines", "correction_budget", "risk_reasons", "artifact_subjects"
  ],
  "properties": {
    "schema": {"const": "gentle-ai.review-integration.start/v3"},
    "contract": {"const": "gentle-ai.review-integration/v2"},
    "operation": {"const": "review.start"},
    "action": {"enum": ["created", "resumed", "reuse-receipt", "blocked-scope-action"]},
    "lenses_required": {"type": "boolean"},
    "lineage_id": {"type": "string", "minLength": 1},
    "state": {
      "enum": [
        "unreviewed", "reviewing", "judges_confirmed", "findings_frozen", "evidence_classified", "fix_required",
        "fixing", "fix_validating", "correction_required", "validating", "ready_final_verification", "final_verifying",
        "approved", "escalated", "invalidated"
      ]
    },
    "risk_level": {"enum": ["low", "medium", "high"]},
    "selected_lenses": {
      "type": "array", "maxItems": 4, "uniqueItems": true,
      "items": {"enum": ["review-risk", "review-resilience", "review-readability", "review-reliability"]}
    },
    "projection": {"enum": ["workspace", "staged"]},
    "target_mode": {"const": "base-workspace-overlay"},
    "target_identity": {"$ref": "#/$defs/sha256"},
    "base_tree": {"$ref": "#/$defs/git_tree"},
    "candidate_tree": {"$ref": "#/$defs/git_tree"},
    "changed_files": {"type": "integer", "minimum": 0},
    "changed_lines": {"type": "integer", "minimum": 0},
    "correction_budget": {"type": "integer", "minimum": 0, "maximum": 200},
    "risk_reasons": {"type": "array", "minItems": 1, "uniqueItems": true, "items": {"$ref": "#/$defs/risk_reason"}},
    "artifact_subjects": {"type": "array", "maxItems": 4, "items": {"$ref": "artifact-subject.schema.json"}},
    "changed_path_manifest": {"type": "array", "uniqueItems": true, "items": {"$ref": "#/$defs/changed_path"}},
    "repository_context": {
      "type": "object", "additionalProperties": false,
      "required": ["capability", "handle", "revision", "target_identity"],
      "properties": {
        "capability": {"const": "review.opaque_repository_context"},
        "handle": {"type": "string", "pattern": "^rctx1_[0-9a-f]{64}$"},
        "revision": {"$ref": "#/$defs/sha256"},
        "target_identity": {"$ref": "#/$defs/sha256"},
        "event_id": {"$ref": "#/$defs/sha256"},
        "outcome": {"enum": ["applied", "pending", "blocked_conflict", "durability_limited"]}
      },
      "dependentRequired": {"event_id": ["outcome"], "outcome": ["event_id"]}
    }
  },
  "$defs": {
    "sha256": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"},
    "git_tree": {"type": "string", "pattern": "^[0-9a-f]{40}([0-9a-f]{24})?$"},
    "changed_path": {
      "type": "object", "additionalProperties": false,
      "required": ["path", "status", "old_mode", "new_mode", "deleted", "type_changed", "mode_only", "intended_untracked"],
      "properties": {
        "path": {"type": "string", "minLength": 1},
        "status": {"enum": ["A", "D", "M", "T"]},
        "old_mode": {"type": "string", "pattern": "^[0-7]{6}$"},
        "new_mode": {"type": "string", "pattern": "^[0-7]{6}$"},
        "deleted": {"type": "boolean"},
        "type_changed": {"type": "boolean"},
        "mode_only": {"type": "boolean"},
        "intended_untracked": {"type": "boolean"}
      }
    },
    "risk_reason": {
      "type": "object", "additionalProperties": false, "required": ["code"],
      "properties": {
        "code": {
          "enum": [
            "configuration_change", "empty_content", "executable_change", "executable_mode", "hot_path", "large_change",
            "non_executable_only", "process_boundary", "process_scan_limit", "service_token", "shell_source"
          ]
        },
        "signal": {"enum": ["auth", "update", "security", "payments", "permissions", "shell_process"]},
        "path": {"type": "string", "minLength": 1},
        "old_mode": {"type": "string", "pattern": "^[0-7]{6}$"},
        "new_mode": {"type": "string", "pattern": "^[0-7]{6}$"}
      }
    }
  }
}
