{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://gentle-ai.dev/contracts/review-integration/v1/schemas/status.schema.json",
  "title": "Gentle AI Review Integration Target Status v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "contract",
    "operation",
    "applicability",
    "receipt",
    "action",
    "replayability",
    "target_identity",
    "projection",
    "candidates"
  ],
  "properties": {
    "schema": { "const": "gentle-ai.review-integration.status/v1" },
    "contract": { "const": "gentle-ai.review-integration/v1" },
    "operation": { "const": "review.status" },
    "applicability": { "enum": ["current_target", "unrelated", "ambiguous", "corrupted"] },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": ["version", "lineage_id", "state", "generation", "revision"],
      "properties": {
        "version": { "enum": ["compact-v2", "legacy-v1"] },
        "lineage_id": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
        "state": { "type": "string", "minLength": 1 },
        "generation": { "type": "integer", "minimum": 1 },
        "revision": { "$ref": "#/$defs/sha256" }
      }
    },
    "receipt": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status"],
      "properties": {
        "status": { "enum": ["expected_missing", "present", "publication_pending", "not_applicable"] },
        "identity": { "$ref": "#/$defs/sha256" }
      }
    },
    "action": { "enum": ["start", "finalize", "validate", "recover", "maintainer_action", "select_lineage", "repair_authority", "reconcile_finalize", "stop"] },
    "action_disposition": { "enum": ["scope_changed", "invalidated", "escalated"] },
    "replayability": { "enum": ["not_replayable", "exact_replay_safe", "status_required", "manual_action_required"] },
    "frozen": {
      "type": "object",
      "additionalProperties": false,
      "required": ["tier", "original_changed_lines", "correction_budget"],
      "properties": {
        "tier": { "enum": ["low", "medium", "high"] },
        "original_changed_lines": { "type": "integer", "minimum": 0 },
        "correction_budget": { "type": "integer", "minimum": 0, "maximum": 200 }
      }
    },
    "target_identity": { "$ref": "#/$defs/sha256" },
    "projection": { "$ref": "projection.schema.json" },
    "candidates": {
      "type": "array",
      "uniqueItems": true,
      "items": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" }
    },
    "reconciliation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required"],
      "properties": { "required": { "const": true } }
    },
    "eligibility": { "$ref": "#/$defs/action_eligibility" },
    "next_transition": { "$ref": "#/$defs/next_transition" },
    "validation_request": { "$ref": "targeted-validation-request.schema.json" }
  },
  "$defs": {
    "sha256": { "type": "string", "pattern": "^sha256:[0-9a-f]{64}$" },
    "next_transition": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "reason_code"],
      "properties": {
        "kind": { "enum": ["execute", "collect", "stop"] },
        "reason_code": { "type": "string", "pattern": "^[a-z0-9_]+$" },
        "execute": { "$ref": "#/$defs/transition_execution" },
        "collect": { "$ref": "#/$defs/transition_collection" },
        "correction_request": { "$ref": "correction-plan-request.schema.json" }
      },
      "allOf": [
        { "if": { "properties": { "kind": { "const": "execute" } }, "required": ["kind"] }, "then": { "required": ["execute"], "not": { "required": ["collect"] } } },
        { "if": { "properties": { "kind": { "const": "collect" } }, "required": ["kind"] }, "then": { "required": ["collect"], "not": { "required": ["execute"] } } },
        { "if": { "properties": { "kind": { "const": "stop" } }, "required": ["kind"] }, "then": { "not": { "anyOf": [{ "required": ["execute"] }, { "required": ["collect"] }] } } },
        { "if": { "properties": { "reason_code": { "enum": ["correction_plan_required", "corrected_candidate_unavailable"] } }, "required": ["reason_code"] }, "then": { "required": ["correction_request"] }, "else": { "not": { "required": ["correction_request"] } } }
      ]
    },
    "transition_argument": {
      "$comment": "One argument of a transition. \"token\", when present, is the exact argv token rendering this argument for a command this product runs, e.g. \"--lineage=review-abc\". It is required on an execute argument (see executable_transition_argument) and present on the arguments of a collect input whose capture_operation names an operation this product performs, because those arguments are literally that command's flags. It is absent on preconditions, on selector_arguments, and on the arguments of an \"external.*\" capture operation, which are assertions, a normalized echo, and values to hand elsewhere rather than argv.",
      "type": "object", "additionalProperties": false, "required": ["name", "value"],
      "properties": { "name": { "type": "string", "pattern": "^[a-z0-9_-]+$" }, "value": { "type": "string", "minLength": 1 }, "token": { "type": "string", "minLength": 1 } }
    },
    "executable_transition_argument": {
      "$comment": "One argv entry of an execute transition. Unlike preconditions and selector_arguments -- which are assertions and a normalized echo the product never tokenizes -- an execute argument is always literally executed, so its exact runnable token is required rather than optional.",
      "allOf": [{ "$ref": "#/$defs/transition_argument" }],
      "type": "object", "required": ["name", "value", "token"]
    },
    "transition_binding": {
      "type": "object", "additionalProperties": false, "required": ["target_identity"],
      "properties": { "lineage_id": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" }, "revision": { "$ref": "#/$defs/sha256" }, "target_identity": { "$ref": "#/$defs/sha256" }, "repository_context": { "type": "string", "pattern": "^rctx1_[0-9a-f]{64}$" } }
    },
    "transition_execution": {
      "type": "object", "additionalProperties": false, "required": ["operation", "arguments", "preconditions", "binding"],
      "properties": {
        "operation": { "enum": ["review.start", "review.finalize", "review.recover", "review.validate"] },
        "command": { "$comment": "The complete, literally runnable command line for this transition, e.g. \"gentle-ai review start --contract=... --target=...\". Operation alone is a dotted logical name; command is what a caller can paste. Argument words are the execute arguments' own tokens, in order, POSIX-shell-quoted only when a value would otherwise be word-split.", "type": "string", "minLength": 1, "pattern": "^gentle-ai review [a-z][a-z-]*" },
        "arguments": { "type": "array", "items": { "$ref": "#/$defs/executable_transition_argument" } },
        "selector_arguments": { "type": "array", "items": { "$ref": "#/$defs/transition_argument" } },
        "preconditions": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/transition_argument" } },
        "binding": { "$ref": "#/$defs/transition_binding" },
        "artifacts": { "type": "array", "items": { "$ref": "#/$defs/transition_artifact" } }
      }
    },
    "transition_artifact": {
      "type": "object", "additionalProperties": false, "required": ["schema", "capability", "sha256", "lineage_id", "target_identity", "lens", "selected_order", "subject_hash", "admission_decision"],
      "properties": {
        "schema": { "const": "gentle-ai.review-result-artifact/v2" }, "capability": { "const": "review.native_result_artifact" }, "sha256": { "$ref": "#/$defs/sha256" },
        "lineage_id": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" }, "target_identity": { "$ref": "#/$defs/sha256" }, "lens": { "type": "string", "minLength": 1 }, "selected_order": { "type": "integer", "minimum": 0 },
        "subject_hash": { "$ref": "#/$defs/sha256" }, "admission_decision": { "const": "completed" }
      }
    },
    "transition_collection": {
      "type": "object", "additionalProperties": false, "required": ["inputs"],
      "properties": { "inputs": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/transition_input" } } }
    },
    "transition_input": {
      "type": "object", "additionalProperties": false, "required": ["name", "schema", "capture_operation", "arguments"],
      "properties": {
        "name": { "type": "string", "pattern": "^[a-z0-9_]+$" }, "schema": { "type": "string", "minLength": 1 }, "capture_operation": { "type": "string", "minLength": 1 },
        "arguments": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/transition_argument" } },
        "validation_request": { "$ref": "targeted-validation-request.schema.json" }
      },
      "allOf": [
        {
          "if": { "properties": { "capture_operation": { "const": "external.run_targeted_validation" } }, "required": ["capture_operation"] },
          "then": { "required": ["validation_request"], "properties": { "schema": { "const": "gentle-ai.review-targeted-validation-request/v1" } } },
          "else": { "not": { "required": ["validation_request"] } }
        }
      ]
    },
    "action_eligibility": {
      "type": "object",
      "additionalProperties": false,
      "required": ["allowed_actions", "forbidden_actions"],
      "properties": {
        "allowed_actions": {
          "type": "array",
          "minItems": 1,
          "maxItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["action", "reason_code", "required_inputs"],
            "properties": {
              "action": { "enum": ["stop", "review.start", "review.finalize", "review.validate", "review.recover"] },
              "reason_code": { "type": "string", "pattern": "^[a-z0-9_]+$" },
              "required_inputs": { "type": "array", "items": { "type": "string", "pattern": "^[a-z0-9_]+$" } },
              "disposition": { "enum": ["scope_changed", "invalidated", "escalated"] },
              "binding": {
                "type": "object",
                "additionalProperties": false,
                "required": ["lineage_id", "revision", "target_identity"],
                "properties": {
                  "lineage_id": { "type": "string", "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$" },
                  "revision": { "$ref": "#/$defs/sha256" },
                  "target_identity": { "$ref": "#/$defs/sha256" }
                }
              }
            },
            "allOf": [
              {
                "if": { "properties": { "action": { "const": "review.recover" } }, "required": ["action"] },
                "then": { "required": ["disposition", "binding"] },
                "else": { "not": { "anyOf": [{ "required": ["disposition"] }, { "required": ["binding"] }] } }
              }
            ]
          }
        },
        "forbidden_actions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["action", "reason_code"],
            "properties": {
              "action": { "enum": ["review.abandon", "review.finalize", "review.invalidate", "review.quarantine-legacy", "review.reclaim", "review.reconcile-authority", "review.reconcile-authority-batch", "review.recover", "review.start", "review.validate"] },
              "reason_code": { "type": "string", "pattern": "^[a-z0-9_]+$" }
            }
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "action": { "const": "reconcile_finalize" } } },
      "then": {
        "required": ["reconciliation"],
        "properties": {
          "applicability": { "const": "current_target" },
          "replayability": { "const": "status_required" }
        }
      },
      "else": { "not": { "required": ["reconciliation"] } }
    },
    {
      "if": { "properties": { "action": { "const": "recover" } }, "required": ["action"] },
      "then": { "required": ["action_disposition"] },
      "else": { "not": { "required": ["action_disposition"] } }
    },
    {
      "if": { "properties": { "applicability": { "const": "current_target" } } },
      "then": { "required": ["authority"] },
      "else": { "not": { "anyOf": [{ "required": ["authority"] }, { "required": ["frozen"] }] } }
    },
    {
      "if": {
        "required": ["authority"],
        "properties": {
          "applicability": { "const": "current_target" },
          "authority": {
            "required": ["version"],
            "properties": { "version": { "const": "compact-v2" } }
          }
        }
      },
      "then": { "required": ["frozen"] }
    },
    {
      "if": {
        "required": ["authority"],
        "properties": {
          "applicability": { "const": "current_target" },
          "authority": {
            "required": ["version"],
            "properties": { "version": { "const": "legacy-v1" } }
          }
        }
      },
      "then": {
        "not": { "required": ["frozen"] },
        "properties": {
          "receipt": {
            "properties": { "status": { "enum": ["expected_missing", "present"] } }
          }
        }
      }
    },
    {
      "if": { "required": ["validation_request"] },
      "then": {
        "properties": {
          "applicability": { "const": "current_target" },
          "authority": { "properties": { "state": { "const": "correction_required" } }, "required": ["state"] }
        }
      }
    }
  ]
}
