{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://iarpg.com/campaign-comparison.schema.json",
  "title": "IARPG Browser-Local Campaign Branch Comparison Record",
  "description": "A compact, deterministic comparison of two or more fictional campaign records replayed from the immutable 2.0.16 baseline.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "$schema",
    "record_type",
    "release",
    "version",
    "comparison_id",
    "baseline",
    "source_dataset",
    "branches",
    "relationship_divergences",
    "turn_divergences",
    "counts",
    "truth_boundary",
    "safety_boundary",
    "generated_by"
  ],
  "properties": {
    "$schema": {
      "const": "https://iarpg.com/campaign-comparison.schema.json"
    },
    "record_type": {
      "const": "campaign-branch-comparison"
    },
    "release": {
      "const": "IARPG-OPS-2"
    },
    "version": {
      "const": "1.0.0"
    },
    "comparison_id": {
      "type": "string",
      "pattern": "^iarpg-campaign-comparison-[2-9][0-9]*-branches$"
    },
    "baseline": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "campaign_id",
        "baseline_version",
        "relationship_count"
      ],
      "properties": {
        "campaign_id": {
          "$ref": "#/$defs/id"
        },
        "baseline_version": {
          "const": "2.0.16-wip"
        },
        "relationship_count": {
          "const": 27
        }
      }
    },
    "source_dataset": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "campaign_state",
        "alliance_network"
      ],
      "properties": {
        "campaign_state": {
          "const": "/campaign-state.json"
        },
        "alliance_network": {
          "const": "/alliance-network.json"
        }
      }
    },
    "branches": {
      "type": "array",
      "minItems": 2,
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/branch"
      }
    },
    "relationship_divergences": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/relationshipDivergence"
      }
    },
    "turn_divergences": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/turnDivergence"
      }
    },
    "counts": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "branches",
        "divergent_relationships",
        "divergent_domains",
        "turn_divergence_positions",
        "evidence_events_referenced",
        "correction_paths_visible"
      ],
      "properties": {
        "branches": {
          "type": "integer",
          "minimum": 2
        },
        "divergent_relationships": {
          "type": "integer",
          "minimum": 0,
          "maximum": 27
        },
        "divergent_domains": {
          "type": "integer",
          "minimum": 0,
          "maximum": 8
        },
        "turn_divergence_positions": {
          "type": "integer",
          "minimum": 0
        },
        "evidence_events_referenced": {
          "type": "integer",
          "minimum": 0
        },
        "correction_paths_visible": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "truth_boundary": {
      "type": "string",
      "minLength": 80
    },
    "safety_boundary": {
      "type": "string",
      "minLength": 80
    },
    "generated_by": {
      "const": "IARPG browser-local deterministic campaign archive workspace"
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]*$"
    },
    "branch": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "branch_id",
        "source_record_id",
        "source_record_version",
        "replay_turn",
        "max_turn",
        "return_points",
        "unknown_extension_paths"
      ],
      "properties": {
        "branch_id": {
          "type": "string",
          "pattern": "^branch-[a-z][a-z0-9-]*$"
        },
        "source_record_id": {
          "type": "string",
          "pattern": "^iarpg-campaign-record-turn-[0-9]+$"
        },
        "source_record_version": {
          "enum": [
            "2.0.16-wip",
            "2.0.17-wip",
            "2.0.18-wip",
            "2.0.19-wip",
            "2.0.20-wip",
            "2.0.21-wip"
          ]
        },
        "replay_turn": {
          "type": "integer",
          "minimum": 0
        },
        "max_turn": {
          "type": "integer",
          "minimum": 0
        },
        "return_points": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "integer",
            "minimum": 0
          }
        },
        "unknown_extension_paths": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^/"
          }
        }
      }
    },
    "domainPosture": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "domain",
        "cooperation"
      ],
      "properties": {
        "domain": {
          "$ref": "#/$defs/id"
        },
        "cooperation": {
          "enum": [
            "minimal",
            "bounded",
            "formal",
            "integrated",
            "contested",
            "suspended"
          ]
        }
      }
    },
    "compactState": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "version",
        "effective_turn",
        "lifecycle_stage",
        "domain_postures",
        "burden_distribution",
        "source_dependency",
        "target_dependency",
        "consultation_state",
        "evidence_confidence",
        "community_impact",
        "reason_code",
        "evidence_event_ids",
        "reversible",
        "supersedes"
      ],
      "properties": {
        "version": {
          "type": "integer",
          "minimum": 1
        },
        "effective_turn": {
          "type": "integer",
          "minimum": 0
        },
        "lifecycle_stage": {
          "$ref": "#/$defs/id"
        },
        "domain_postures": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/domainPosture"
          }
        },
        "burden_distribution": {
          "enum": [
            "balanced",
            "source-heavy",
            "target-heavy",
            "contested"
          ]
        },
        "source_dependency": {
          "enum": [
            "low",
            "medium",
            "high"
          ]
        },
        "target_dependency": {
          "enum": [
            "low",
            "medium",
            "high"
          ]
        },
        "consultation_state": {
          "enum": [
            "unavailable",
            "available",
            "routine",
            "active",
            "suspended",
            "requested"
          ]
        },
        "evidence_confidence": {
          "enum": [
            "low",
            "medium",
            "high"
          ]
        },
        "community_impact": {
          "enum": [
            "beneficial",
            "mixed",
            "strained",
            "harmful"
          ]
        },
        "reason_code": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_]+$"
        },
        "evidence_event_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "reversible": {
          "type": "boolean"
        },
        "supersedes": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    },
    "branchState": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "branch_id",
        "state"
      ],
      "properties": {
        "branch_id": {
          "type": "string",
          "pattern": "^branch-[a-z][a-z0-9-]*$"
        },
        "state": {
          "$ref": "#/$defs/compactState"
        }
      }
    },
    "relationshipDivergence": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "relationship_id",
        "relationship_label",
        "relationship_type",
        "domains",
        "branch_states"
      ],
      "properties": {
        "relationship_id": {
          "$ref": "#/$defs/id"
        },
        "relationship_label": {
          "type": "string",
          "minLength": 3
        },
        "relationship_type": {
          "$ref": "#/$defs/id"
        },
        "domains": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "branch_states": {
          "type": "array",
          "minItems": 2,
          "items": {
            "$ref": "#/$defs/branchState"
          }
        }
      }
    },
    "correctionPath": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": true,
      "required": [
        "trigger",
        "action"
      ],
      "properties": {
        "trigger": {
          "type": "string",
          "minLength": 10
        },
        "action": {
          "type": "string",
          "minLength": 10
        }
      }
    },
    "branchEvent": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "branch_id",
        "turn",
        "scenario_id",
        "response_id",
        "reason_codes",
        "evidence_event_ids",
        "consultation_decision",
        "correction_path",
        "mission_brief_ids"
      ],
      "properties": {
        "branch_id": {
          "type": "string",
          "pattern": "^branch-[a-z][a-z0-9-]*$"
        },
        "turn": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        },
        "scenario_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "response_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "reason_codes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "evidence_event_ids": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "consultation_decision": {
          "type": "string",
          "minLength": 10
        },
        "correction_path": {
          "$ref": "#/$defs/correctionPath"
        },
        "mission_brief_ids": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "turnDivergence": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "position",
        "branch_events"
      ],
      "properties": {
        "position": {
          "type": "integer",
          "minimum": 1
        },
        "branch_events": {
          "type": "array",
          "minItems": 2,
          "items": {
            "$ref": "#/$defs/branchEvent"
          }
        }
      }
    }
  }
}
