{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "diagnostic-applicability-graph-v1.schema.json",
  "title": "Diagnostic applicability graph v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contract",
    "schema_version",
    "semantics",
    "source",
    "nodes_by_id",
    "relations_by_id",
    "indexes"
  ],
  "properties": {
    "contract": {"const": "diagnostic_applicability_graph"},
    "schema_version": {"const": 1},
    "semantics": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "condition_sets_are_conjunctive",
        "condition_sets_are_alternative_routes",
        "grounding_is_forward_necessity",
        "supporting_context_is_not_trigger",
        "residuals_are_open_world",
        "not_a_hierarchy_dag"
      ],
      "properties": {
        "condition_sets_are_conjunctive": {"const": true},
        "condition_sets_are_alternative_routes": {"const": true},
        "grounding_is_forward_necessity": {"const": true},
        "supporting_context_is_not_trigger": {"const": true},
        "residuals_are_open_world": {"const": true},
        "not_a_hierarchy_dag": {"const": true}
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "projection_path",
        "projection_sha256",
        "projection_manifest_path",
        "projection_manifest_sha256",
        "prime_catalog_path",
        "prime_catalog_sha256",
        "dsa_catalog_path",
        "dsa_catalog_sha256",
        "archetype_catalog_path",
        "archetype_catalog_sha256"
      ],
      "properties": {
        "projection_path": {"type": "string", "minLength": 1},
        "projection_sha256": {"$ref": "#/$defs/sha256"},
        "projection_manifest_path": {"type": "string", "minLength": 1},
        "projection_manifest_sha256": {"$ref": "#/$defs/sha256"},
        "prime_catalog_path": {"type": "string", "minLength": 1},
        "prime_catalog_sha256": {"$ref": "#/$defs/sha256"},
        "dsa_catalog_path": {"type": "string", "minLength": 1},
        "dsa_catalog_sha256": {"$ref": "#/$defs/sha256"},
        "archetype_catalog_path": {"type": "string", "minLength": 1},
        "archetype_catalog_sha256": {"$ref": "#/$defs/sha256"}
      }
    },
    "nodes_by_id": {
      "type": "object",
      "additionalProperties": {"$ref": "#/$defs/node"}
    },
    "relations_by_id": {
      "type": "object",
      "additionalProperties": {"$ref": "#/$defs/relation"}
    },
    "indexes": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "relations_by_source",
        "relations_by_target",
        "nodes_by_kind",
        "abstraction_ids_by_slug",
        "archetype_id_by_slug"
      ],
      "properties": {
        "relations_by_source": {"$ref": "#/$defs/string_array_map"},
        "relations_by_target": {"$ref": "#/$defs/string_array_map"},
        "nodes_by_kind": {"$ref": "#/$defs/string_array_map"},
        "abstraction_ids_by_slug": {"$ref": "#/$defs/string_array_map"},
        "archetype_id_by_slug": {
          "type": "object",
          "additionalProperties": {"type": "string", "minLength": 1}
        }
      }
    }
  },
  "$defs": {
    "sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
    "slug": {
      "type": "string",
      "pattern": "^[a-z0-9]+(?:_[a-z0-9]+)*$"
    },
    "string_array_map": {
      "type": "object",
      "additionalProperties": {
        "type": "array",
        "uniqueItems": true,
        "items": {"type": "string", "minLength": 1}
      }
    },
    "node": {
      "type": "object",
      "additionalProperties": true,
      "required": ["node_id", "node_kind"],
      "properties": {
        "node_id": {"type": "string", "minLength": 3},
        "node_kind": {
          "enum": [
            "prime",
            "domain_specific",
            "solution_archetype",
            "structural_condition",
            "condition_set",
            "supporting_context_condition",
            "non_trigger_requirement",
            "residual"
          ]
        },
        "slug": {"$ref": "#/$defs/slug"},
        "name": {"type": "string"},
        "archetype_slug": {"$ref": "#/$defs/slug"},
        "condition_id": {"type": "string", "minLength": 1},
        "condition_set_id": {"type": "string", "minLength": 1},
        "predicate": {"type": "string", "minLength": 1},
        "function": {"type": "string", "minLength": 1}
      }
    },
    "relation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "relation_id",
        "relation_type",
        "source_id",
        "target_id",
        "source_kind",
        "target_kind",
        "metadata"
      ],
      "properties": {
        "relation_id": {"type": "string", "pattern": "^apprel-[0-9a-f]{24}$"},
        "relation_type": {
          "enum": [
            "grounds_trigger",
            "grounds_supporting_context",
            "required_in",
            "diagnostic_route_for",
            "context_for",
            "requirement_for",
            "unfilled_part_of"
          ]
        },
        "source_id": {"type": "string", "minLength": 1},
        "target_id": {"type": "string", "minLength": 1},
        "source_kind": {"type": "string", "minLength": 1},
        "target_kind": {"type": "string", "minLength": 1},
        "metadata": {"type": "object"}
      }
    }
  }
}
