{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://machinetradecraft.com/downloads/machine-tradecraft-differential-record-schema-v1.json",
  "title": "Machine Tradecraft Cross-Representation Differential Export",
  "description": "Schema version 1.0.0 for deterministic, non-scoring differential records generated by MachineTradecraft.com.",
  "type": "object",
  "required": ["generated_by", "site_version", "release_id", "differential"],
  "additionalProperties": false,
  "properties": {
    "generated_by": {"const": "MachineTradecraft.com Cross-Representation Differential Workbench"},
    "site_version": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"},
    "release_id": {"type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}-[a-z0-9-]+$"},
    "differential": {"$ref": "#/$defs/differential"}
  },
  "$defs": {
    "identifier": {"type": "string", "pattern": "^[a-z0-9-]+$"},
    "sha256": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
    "component": {
      "type": "object",
      "required": ["label", "kind", "bytes", "sha256", "preview"],
      "additionalProperties": false,
      "properties": {
        "label": {"type": "string", "minLength": 1, "maxLength": 160},
        "kind": {"type": "string", "minLength": 1, "maxLength": 80},
        "bytes": {"type": "integer", "minimum": 0},
        "sha256": {"$ref": "#/$defs/sha256"},
        "preview": {"type": "string", "maxLength": 500}
      }
    },
    "plane": {
      "type": "object",
      "required": ["evaluation_state", "domain", "method", "component_count", "components", "representation_sha256", "plane"],
      "additionalProperties": false,
      "properties": {
        "evaluation_state": {"enum": ["evaluated", "not-evaluated-locally"]},
        "domain": {"type": "string", "minLength": 1, "maxLength": 120},
        "method": {"type": "string", "minLength": 1, "maxLength": 240},
        "component_count": {"type": "integer", "minimum": 0},
        "components": {"type": "array", "items": {"$ref": "#/$defs/component"}},
        "representation_sha256": {"type": "string", "pattern": "^$|^[a-f0-9]{64}$"},
        "plane": {
          "type": "object",
          "required": ["id", "label", "description"],
          "additionalProperties": false,
          "properties": {
            "id": {"$ref": "#/$defs/identifier"},
            "label": {"type": "string", "minLength": 2, "maxLength": 120},
            "description": {"type": "string", "minLength": 10, "maxLength": 800}
          }
        }
      }
    },
    "state": {
      "type": "object",
      "required": ["id", "label", "description"],
      "additionalProperties": true,
      "properties": {
        "id": {"enum": ["same", "different", "conditionally-comparable", "not-comparable", "not-evaluated-locally", "survived", "changed", "removed", "not-evaluated"]},
        "label": {"type": "string", "minLength": 2, "maxLength": 80},
        "description": {"type": "string", "minLength": 10, "maxLength": 800},
        "explanation": {"type": "string", "minLength": 10, "maxLength": 800}
      }
    },
    "case": {
      "type": "object",
      "required": ["id", "title", "description", "topic", "fixture", "laboratory", "resources", "signal", "transformations"],
      "additionalProperties": false,
      "properties": {
        "id": {"$ref": "#/$defs/identifier"},
        "title": {"type": "string", "minLength": 3, "maxLength": 200},
        "description": {"type": "string", "minLength": 20, "maxLength": 1200},
        "topic": {"type": "object"},
        "fixture": {"type": "object"},
        "laboratory": {"type": "object"},
        "resources": {"type": "object"},
        "signal": {"type": "object"},
        "transformations": {
          "type": "array",
          "minItems": 1,
          "maxItems": 5,
          "items": {
            "type": "object",
            "required": ["id", "label", "description", "survival", "planes"],
            "additionalProperties": false,
            "properties": {
              "id": {"$ref": "#/$defs/identifier"},
              "label": {"type": "string"},
              "description": {"type": "string"},
              "survival": {"type": "object"},
              "planes": {"type": "array", "items": {"$ref": "#/$defs/plane"}}
            }
          }
        }
      }
    },
    "comparison": {
      "type": "object",
      "required": ["comparison_id", "transform", "plane", "left", "right", "state"],
      "additionalProperties": false,
      "properties": {
        "comparison_id": {"type": "string", "pattern": "^C-[0-9]{3}$"},
        "transform": {"type": "object"},
        "plane": {"type": "object"},
        "left": {"type": "object"},
        "right": {"type": "object"},
        "state": {"$ref": "#/$defs/state"}
      }
    },
    "differential": {
      "type": "object",
      "required": ["schema_version", "differential_schema_version", "site_version", "release_id", "question", "selection", "cases", "pairwise_comparisons", "comparison_counts", "signal_survival", "observations", "representation_conflicts", "external_verification_requirements", "resources", "schema_url", "method_boundary", "non_conclusions", "differential_id", "differential_sha256"],
      "additionalProperties": false,
      "properties": {
        "schema_version": {"const": 1},
        "differential_schema_version": {"const": "1.0.0"},
        "site_version": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"},
        "release_id": {"type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}-[a-z0-9-]+$"},
        "question": {"type": "string", "maxLength": 600},
        "selection": {"type": "object"},
        "cases": {"type": "array", "minItems": 2, "maxItems": 4, "items": {"$ref": "#/$defs/case"}},
        "pairwise_comparisons": {"type": "array", "items": {"$ref": "#/$defs/comparison"}},
        "comparison_counts": {
          "type": "object",
          "required": ["same", "different", "conditionally-comparable", "not-comparable", "not-evaluated-locally"],
          "additionalProperties": false,
          "properties": {
            "same": {"type": "integer", "minimum": 0},
            "different": {"type": "integer", "minimum": 0},
            "conditionally-comparable": {"type": "integer", "minimum": 0},
            "not-comparable": {"type": "integer", "minimum": 0},
            "not-evaluated-locally": {"type": "integer", "minimum": 0}
          }
        },
        "signal_survival": {"type": "array", "items": {"type": "object"}},
        "observations": {"type": "array", "minItems": 3, "items": {"type": "string"}},
        "representation_conflicts": {"type": "array", "items": {"type": "object"}},
        "external_verification_requirements": {"type": "array", "items": {"type": "object"}},
        "resources": {"type": "array", "minItems": 2, "maxItems": 4, "items": {"type": "object"}},
        "schema_url": {"const": "/downloads/machine-tradecraft-differential-record-schema-v1.json"},
        "method_boundary": {"type": "array", "minItems": 4, "items": {"type": "string"}},
        "non_conclusions": {"type": "array", "minItems": 5, "items": {"type": "string"}},
        "differential_id": {"type": "string", "pattern": "^[A-F0-9]{12}$"},
        "differential_sha256": {"$ref": "#/$defs/sha256"}
      }
    }
  }
}
