{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agla.invalid/schemas/machina-external-bridge.v1.schema.json",
  "title": "AGLA MACHINA External Loss-Declaring Bridge",
  "version": "1.0.0",
  "status": "CANDIDATE_NON_RUNTIME_NO_AUTHORITY_EFFECT",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "bridge_id",
    "bridge_version",
    "bridge_kind",
    "source",
    "target",
    "mapping_direction",
    "compatibility",
    "provenance",
    "typed_mappings",
    "loss_declarations",
    "round_trip",
    "authority"
  ],
  "properties": {
    "$schema": { "type": "string" },
    "bridge_id": { "type": "string", "minLength": 1 },
    "bridge_version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$" },
    "bridge_kind": { "const": "EXTERNAL_LOSS_DECLARING_PROFILE_BRIDGE" },
    "source": { "$ref": "#/$defs/endpoint" },
    "target": { "$ref": "#/$defs/endpoint" },
    "mapping_direction": { "const": "SOURCE_ROTA_A_DUPLEX_TO_INSPECTION_ROTA" },
    "compatibility": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_range", "target_range", "bridge_interface"],
      "properties": {
        "source_range": { "type": "string" },
        "target_range": { "type": "string" },
        "bridge_interface": { "const": "MACHINA_EXTERNAL_BRIDGE_V1" }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_commit", "source_artifact_path", "qualification_status"],
      "properties": {
        "source_commit": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
        "source_artifact_path": { "type": "string" },
        "qualification_status": { "const": "S02_QUALIFIED_NON_RUNTIME" }
      }
    },
    "typed_mappings": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "wheels",
        "loci",
        "cycle_order_views",
        "axes",
        "directed_carriers",
        "pair_keys",
        "state_identity",
        "lexical_surfaces",
        "presentation_surfaces",
        "camera_surface"
      ],
      "properties": {
        "wheels": { "$ref": "#/$defs/mapping_array" },
        "loci": { "$ref": "#/$defs/mapping_array" },
        "cycle_order_views": { "$ref": "#/$defs/mapping_array" },
        "axes": { "$ref": "#/$defs/mapping_array" },
        "directed_carriers": { "$ref": "#/$defs/directed_mapping_array" },
        "pair_keys": { "$ref": "#/$defs/pair_mapping_array" },
        "state_identity": { "$ref": "#/$defs/state_mapping" },
        "lexical_surfaces": { "$ref": "#/$defs/mapping_array" },
        "presentation_surfaces": { "$ref": "#/$defs/mapping_array" },
        "camera_surface": { "$ref": "#/$defs/camera_mapping" }
      }
    },
    "loss_declarations": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/loss" }
    },
    "round_trip": {
      "type": "object",
      "additionalProperties": false,
      "required": ["structural_identity_claim", "generic_projection_claim", "source_id_recovery", "validation_evidence"],
      "properties": {
        "structural_identity_claim": { "const": "LOSSLESS" },
        "generic_projection_claim": { "const": "LOSS_DECLARED" },
        "source_id_recovery": { "const": "EXACT_FOR_ALL_TYPED_STRUCTURAL_MAPPINGS" },
        "validation_evidence": { "type": "array", "minItems": 1, "items": { "type": "string" } }
      }
    },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": ["canonical_effect", "runtime_authority", "inference_authority", "provider_closure_effect"],
      "properties": {
        "canonical_effect": { "const": "NONE" },
        "runtime_authority": { "const": false },
        "inference_authority": { "const": false },
        "provider_closure_effect": { "const": "NONE" }
      }
    }
  },
  "$defs": {
    "endpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": ["artifact_id", "schema_or_profile_id", "version", "artifact_kind", "owner", "namespace"],
      "properties": {
        "artifact_id": { "type": "string" },
        "schema_or_profile_id": { "type": "string" },
        "version": { "type": "string" },
        "artifact_kind": { "type": "string" },
        "owner": { "type": "string" },
        "namespace": { "type": "string" }
      }
    },
    "mapping": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_id", "target_id", "mapping_kind", "preservation"],
      "properties": {
        "source_id": { "type": "string" },
        "target_id": { "type": "string" },
        "mapping_kind": { "type": "string" },
        "preservation": { "enum": ["IDENTITY_PRESERVED", "SEMANTICS_PRESERVED", "REFERENCE_PRESERVED", "LOSS_DECLARED"] }
      }
    },
    "directed_mapping": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_id", "target_id", "mapping_kind", "preservation", "source_subject_id", "source_predicate_id", "target_subject_id", "target_predicate_id", "direction_preserved"],
      "properties": {
        "source_id": { "type": "string" },
        "target_id": { "type": "string" },
        "mapping_kind": { "type": "string" },
        "preservation": { "enum": ["IDENTITY_PRESERVED", "SEMANTICS_PRESERVED", "REFERENCE_PRESERVED", "LOSS_DECLARED"] },
        "source_subject_id": { "type": "string" },
        "source_predicate_id": { "type": "string" },
        "target_subject_id": { "type": "string" },
        "target_predicate_id": { "type": "string" },
        "direction_preserved": { "const": true }
      }
    },
    "pair_mapping": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_id", "target_id", "mapping_kind", "preservation", "carrier_source_ids", "stateless", "is_camera"],
      "properties": {
        "source_id": { "type": "string" },
        "target_id": { "type": "string" },
        "mapping_kind": { "type": "string" },
        "preservation": { "enum": ["IDENTITY_PRESERVED", "SEMANTICS_PRESERVED", "REFERENCE_PRESERVED", "LOSS_DECLARED"] },
        "carrier_source_ids": { "type": "array", "minItems": 2, "maxItems": 2, "uniqueItems": true, "items": { "type": "string" } },
        "stateless": { "const": true },
        "is_camera": { "const": false }
      }
    },
    "mapping_array": {
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/mapping" }
    },
    "directed_mapping_array": {
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/directed_mapping" }
    },
    "pair_mapping_array": {
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/pair_mapping" }
    },
    "state_mapping": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_fields", "target_fields", "ordered", "preservation"],
      "properties": {
        "source_fields": { "type": "array", "minItems": 3, "maxItems": 3, "items": { "type": "string" } },
        "target_fields": { "type": "array", "minItems": 3, "maxItems": 3, "items": { "type": "string" } },
        "ordered": { "const": true },
        "preservation": { "const": "IDENTITY_PRESERVED" }
      }
    },
    "camera_mapping": {
      "type": "object",
      "additionalProperties": false,
      "required": ["provider", "status", "source_refs", "target_refs", "pair_key_collapse"],
      "properties": {
        "provider": { "const": "STACK_G_EXTERNAL" },
        "status": { "enum": ["NOT_SUPPLIED", "REFERENCED_EXTERNALLY"] },
        "source_refs": { "type": "array", "items": { "type": "string" } },
        "target_refs": { "type": "array", "items": { "type": "string" } },
        "pair_key_collapse": { "const": false }
      }
    },
    "loss": {
      "type": "object",
      "additionalProperties": false,
      "required": ["loss_code", "affected_source_paths", "severity", "reversibility", "justification", "unresolved_consequences"],
      "properties": {
        "loss_code": { "type": "string", "minLength": 1 },
        "affected_source_paths": { "type": "array", "minItems": 1, "items": { "type": "string" } },
        "severity": { "enum": ["INFORMATIONAL", "LOW", "MEDIUM", "HIGH", "BLOCKING"] },
        "reversibility": { "enum": ["REVERSIBLE_BY_SOURCE_REF", "PARTIALLY_REVERSIBLE", "IRREVERSIBLE"] },
        "justification": { "type": "string", "minLength": 1 },
        "unresolved_consequences": { "type": "array", "items": { "type": "string" } }
      }
    }
  }
}
