{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:awoooi:source-control-primary-rollback-adr-v1",
  "title": "Source Control Primary Rollback ADR v1",
  "description": "定義 GitHub primary cutover 前必備的 rollback ADR 草案、回退觸發條件、逐 repo owner review 與仍然禁止事項。此契約不授權 primary switch、refs sync 或 repo 修改。",
  "type": "object",
  "required": [
    "schema_version",
    "status",
    "date",
    "mode",
    "runtime_execution_authorized",
    "source_indexes",
    "summary",
    "rollback_principles",
    "cutover_preconditions",
    "repo_rollback_plans",
    "rollback_owner_handoff",
    "rollback_triggers",
    "validation_windows",
    "acceptance_rules",
    "forbidden_actions"
  ],
  "properties": {
    "schema_version": {
      "const": "source_control_primary_rollback_adr_v1"
    },
    "status": {
      "type": "string",
      "enum": ["draft_waiting_owner_review"]
    },
    "date": {
      "type": "string"
    },
    "mode": {
      "type": "string",
      "enum": ["rollback_adr_only"]
    },
    "runtime_execution_authorized": {
      "type": "boolean",
      "const": false
    },
    "source_indexes": {
      "type": "array",
      "items": {"type": "string"},
      "minItems": 1
    },
    "summary": {
      "type": "object",
      "required": [
        "candidate_repo_count",
        "in_scope_repo_count",
        "external_scope_count",
        "repo_rollback_plan_count",
        "owner_approved_count",
        "dry_run_completed_count",
        "active_cutover_count",
        "rollback_owner_handoff_package_ready",
        "rollback_owner_handoff_completion_percent",
        "rollback_owner_handoff_check_count",
        "rollback_owner_handoff_packet_field_count",
        "rollback_owner_request_dispatch_authorized",
        "rollback_owner_response_received_count",
        "rollback_owner_response_accepted_count",
        "rollback_owner_response_rejected_count",
        "rollback_execution_authorized",
        "github_primary_switch_authorized",
        "gitea_disable_authorized",
        "action_buttons_allowed"
      ],
      "properties": {
        "candidate_repo_count": {"type": "integer", "minimum": 0},
        "in_scope_repo_count": {"type": "integer", "minimum": 0},
        "external_scope_count": {"type": "integer", "minimum": 0},
        "repo_rollback_plan_count": {"type": "integer", "minimum": 0},
        "owner_approved_count": {"type": "integer", "minimum": 0},
        "dry_run_completed_count": {"type": "integer", "minimum": 0},
        "active_cutover_count": {"type": "integer", "minimum": 0},
        "rollback_owner_handoff_package_ready": {"type": "boolean"},
        "rollback_owner_handoff_completion_percent": {"type": "integer", "minimum": 0, "maximum": 100},
        "rollback_owner_handoff_check_count": {"type": "integer", "minimum": 0},
        "rollback_owner_handoff_packet_field_count": {"type": "integer", "minimum": 0},
        "rollback_owner_request_dispatch_authorized": {"type": "boolean", "const": false},
        "rollback_owner_response_received_count": {"type": "integer", "minimum": 0},
        "rollback_owner_response_accepted_count": {"type": "integer", "minimum": 0},
        "rollback_owner_response_rejected_count": {"type": "integer", "minimum": 0},
        "rollback_execution_authorized": {"type": "boolean", "const": false},
        "github_primary_switch_authorized": {"type": "boolean", "const": false},
        "gitea_disable_authorized": {"type": "boolean", "const": false},
        "action_buttons_allowed": {"type": "boolean", "const": false}
      },
      "additionalProperties": false
    },
    "rollback_principles": {
      "type": "array",
      "items": {"type": "string"},
      "minItems": 1
    },
    "cutover_preconditions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "gate_id",
          "title",
          "required_evidence",
          "current_status",
          "execution_authorized"
        ],
        "properties": {
          "gate_id": {"type": "string"},
          "title": {"type": "string"},
          "required_evidence": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1
          },
          "current_status": {
            "type": "string",
            "enum": ["missing", "draft_only", "waiting_owner_review", "blocked"]
          },
          "execution_authorized": {"type": "boolean", "const": false}
        },
        "additionalProperties": false
      },
      "minItems": 1
    },
    "repo_rollback_plans": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "repo_key",
          "github_repo",
          "source_key",
          "scope_status",
          "risk",
          "rollback_state",
          "primary_ready",
          "fallback_role",
          "required_owner_decisions",
          "rollback_evidence_required",
          "rollback_triggers",
          "manual_recovery_outline",
          "execution_authorized",
          "still_forbidden"
        ],
        "properties": {
          "repo_key": {"type": "string"},
          "github_repo": {"type": "string"},
          "source_key": {"type": "string"},
          "scope_status": {
            "type": "string",
            "enum": ["in_scope", "external_scope_review"]
          },
          "risk": {
            "type": "string",
            "enum": ["LOW", "MEDIUM", "HIGH"]
          },
          "rollback_state": {
            "type": "string",
            "enum": [
              "draft_waiting_owner_review",
              "scope_review_only"
            ]
          },
          "primary_ready": {"type": "boolean", "const": false},
          "fallback_role": {"type": "string"},
          "required_owner_decisions": {
            "type": "array",
            "items": {"type": "string"}
          },
          "rollback_evidence_required": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1
          },
          "rollback_triggers": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1
          },
          "manual_recovery_outline": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1
          },
          "execution_authorized": {"type": "boolean", "const": false},
          "still_forbidden": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1
          }
        },
        "additionalProperties": false
      },
      "minItems": 1
    },
    "rollback_owner_handoff": {
      "type": "object",
      "required": [
        "status",
        "package_ready",
        "handoff_completion_percent",
        "repo_template_count",
        "preflight_checks",
        "handoff_packet",
        "repo_owner_response_templates",
        "post_handoff_invariants"
      ],
      "properties": {
        "status": {"type": "string", "enum": ["ready_not_dispatched"]},
        "package_ready": {"type": "boolean"},
        "handoff_completion_percent": {"type": "integer", "minimum": 0, "maximum": 100},
        "repo_template_count": {"type": "integer", "minimum": 0},
        "preflight_checks": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["check_id", "title", "completion_state", "current_status"],
            "properties": {
              "check_id": {"type": "string"},
              "title": {"type": "string"},
              "completion_state": {"type": "string", "enum": ["defined_not_dispatched"]},
              "current_status": {"type": "string"}
            },
            "additionalProperties": false
          },
          "minItems": 1
        },
        "handoff_packet": {
          "type": "object",
          "required": [
            "request_id",
            "stage_id",
            "prerequisite_gates",
            "requested_repo_templates",
            "recipient_role_or_team",
            "required_response_fields",
            "validation_window_refs",
            "allowed_evidence_refs",
            "forbidden_inputs",
            "not_approval",
            "request_dispatch_authorized"
          ],
          "properties": {
            "request_id": {"type": "string"},
            "stage_id": {"type": "string"},
            "prerequisite_gates": {
              "type": "array",
              "items": {"type": "string"},
              "minItems": 1
            },
            "requested_repo_templates": {
              "type": "array",
              "items": {"type": "string"},
              "minItems": 1
            },
            "recipient_role_or_team": {"type": "string"},
            "required_response_fields": {
              "type": "array",
              "items": {"type": "string"},
              "minItems": 1
            },
            "validation_window_refs": {
              "type": "array",
              "items": {"type": "string"},
              "minItems": 1
            },
            "allowed_evidence_refs": {
              "type": "array",
              "items": {"type": "string"},
              "minItems": 1
            },
            "forbidden_inputs": {
              "type": "array",
              "items": {"type": "string"},
              "minItems": 1
            },
            "not_approval": {"type": "boolean"},
            "request_dispatch_authorized": {"type": "boolean", "const": false}
          },
          "additionalProperties": false
        },
        "repo_owner_response_templates": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "repo_key",
              "github_repo",
              "rollback_owner_status",
              "fallback_role_confirmation_required",
              "trigger_review_required",
              "validation_window_refs",
              "required_response_fields",
              "response_received",
              "response_accepted",
              "primary_ready",
              "execution_authorized"
            ],
            "properties": {
              "repo_key": {"type": "string"},
              "github_repo": {"type": "string"},
              "rollback_owner_status": {"type": "string", "enum": ["waiting_owner_response"]},
              "fallback_role_confirmation_required": {"type": "boolean"},
              "trigger_review_required": {"type": "boolean"},
              "validation_window_refs": {
                "type": "array",
                "items": {"type": "string"},
                "minItems": 1
              },
              "required_response_fields": {
                "type": "array",
                "items": {"type": "string"},
                "minItems": 1
              },
              "response_received": {"type": "boolean"},
              "response_accepted": {"type": "boolean"},
              "primary_ready": {"type": "boolean", "const": false},
              "execution_authorized": {"type": "boolean", "const": false}
            },
            "additionalProperties": false
          },
          "minItems": 1
        },
        "post_handoff_invariants": {
          "type": "array",
          "items": {"type": "string"},
          "minItems": 1
        }
      },
      "additionalProperties": false
    },
    "rollback_triggers": {
      "type": "array",
      "items": {"type": "string"},
      "minItems": 1
    },
    "validation_windows": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "window_id",
          "title",
          "required_checks",
          "failure_handling",
          "execution_authorized"
        ],
        "properties": {
          "window_id": {"type": "string"},
          "title": {"type": "string"},
          "required_checks": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1
          },
          "failure_handling": {"type": "string"},
          "execution_authorized": {"type": "boolean", "const": false}
        },
        "additionalProperties": false
      },
      "minItems": 1
    },
    "acceptance_rules": {
      "type": "array",
      "items": {"type": "string"},
      "minItems": 1
    },
    "forbidden_actions": {
      "type": "array",
      "items": {"type": "string"},
      "minItems": 1
    }
  },
  "additionalProperties": false
}
