{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mif-spec.dev/schema/ontology/ontology.schema.json",
  "title": "MIF Ontology Schema",
  "description": "JSON Schema for validating ontology definition files with cognitive triad namespace hierarchy",
  "type": "object",
  "properties": {
    "ontology": {
      "type": "object",
      "description": "Ontology metadata",
      "required": ["id", "version"],
      "properties": {
        "id": {
          "type": "string",
          "description": "Unique identifier for the ontology",
          "pattern": "^[a-z][a-z0-9-]*$"
        },
        "version": {
          "type": "string",
          "description": "Semantic version string",
          "pattern": "^\\d+\\.\\d+\\.\\d+.*$"
        },
        "description": {
          "type": "string",
          "description": "Human-readable description"
        },
        "schema_url": {
          "type": "string",
          "description": "URL to external schema definition",
          "format": "uri"
        },
        "extends": {
          "type": "array",
          "description": "List of ontology IDs this ontology extends (inherits traits and relationships)",
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9-]*$"
          }
        }
      }
    },
    "namespaces": {
      "type": "object",
      "description": "Hierarchical namespace definitions following cognitive triad",
      "additionalProperties": {
        "$ref": "#/$defs/namespace"
      }
    },
    "entity_types": {
      "type": "array",
      "description": "Custom entity type definitions",
      "items": {
        "$ref": "#/$defs/entityType"
      }
    },
    "traits": {
      "type": "object",
      "description": "Reusable trait/mixin definitions",
      "additionalProperties": {
        "$ref": "#/$defs/trait"
      }
    },
    "relationships": {
      "type": "object",
      "description": "Relationship type definitions",
      "additionalProperties": {
        "$ref": "#/$defs/relationship"
      }
    },
    "discovery": {
      "type": "object",
      "description": "Entity discovery configuration",
      "properties": {
        "enabled": {
          "type": "boolean",
          "description": "Whether discovery is enabled",
          "default": false
        },
        "confidence_threshold": {
          "type": "number",
          "description": "Minimum confidence for suggestions",
          "minimum": 0,
          "maximum": 1,
          "default": 0.8
        },
        "content_patterns": {
          "type": "array",
          "description": "Patterns matched against user prompts and memory content",
          "items": {
            "$ref": "#/$defs/contentPattern"
          }
        },
        "file_patterns": {
          "type": "array",
          "description": "Patterns matched against file paths being edited",
          "items": {
            "$ref": "#/$defs/filePattern"
          }
        },
        "patterns": {
          "type": "array",
          "description": "Unified patterns array (alternative to separate content/file arrays)",
          "items": {
            "$ref": "#/$defs/unifiedPattern"
          }
        }
      }
    }
  },
  "$defs": {
    "namespace": {
      "type": "object",
      "description": "Namespace definition with optional children for hierarchical structure",
      "properties": {
        "description": {
          "type": "string",
          "description": "Namespace description"
        },
        "type_hint": {
          "type": "string",
          "description": "Default memory type for this namespace",
          "enum": ["semantic", "episodic", "procedural"]
        },
        "replaces": {
          "type": "string",
          "description": "Base namespace this replaces (for migration)"
        },
        "children": {
          "type": "object",
          "description": "Child namespaces forming hierarchical structure",
          "additionalProperties": {
            "$ref": "#/$defs/namespace"
          }
        }
      }
    },
    "entityType": {
      "type": "object",
      "description": "Entity type definition",
      "required": ["name", "base"],
      "properties": {
        "name": {
          "type": "string",
          "description": "Entity type name",
          "pattern": "^[a-z][a-z0-9-]*$"
        },
        "description": {
          "type": "string",
          "description": "Entity type description"
        },
        "base": {
          "type": "string",
          "description": "Base memory type",
          "enum": ["semantic", "episodic", "procedural"]
        },
        "traits": {
          "type": "array",
          "description": "Traits this entity type includes",
          "items": {
            "type": "string"
          }
        },
        "subtype_of": {
          "type": "array",
          "description": "Names of parent entity types this type specializes (subsumption). A subtype is substitutable for any of its supertypes wherever the supertype is admissible (e.g. a relationship endpoint domain); its schema must remain compatible with each parent's (additive). Parents may be declared in this ontology or in an ontology it extends, and the subtype_of graph must be acyclic.",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string"
          }
        },
        "aliases": {
          "type": "array",
          "description": "Synonyms and label variations for this entity type (SKOS altLabel analog), as a structured field distinct from description. Embedding-based classifiers concatenate aliases into the type's positive embedding document alongside description and exemplars.",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "exemplars": {
          "type": "array",
          "description": "Curated canonical example phrases or instances of this entity type (SKOS example analog; 2-5 recommended), distinct from description. A second, example-grounded positive embedding signal for classifiers.",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "negative_examples": {
          "type": "array",
          "description": "Curated near-miss examples drawn from this ontology's most confusable type pairs: texts that resemble this entity type but do NOT denote it. For decision-boundary sharpening only; never concatenated into the positive embedding document. Must be human-curated, not auto-mined.",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "schema": {
          "type": "object",
          "description": "JSON Schema for entity fields",
          "properties": {
            "required": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "properties": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/propertySchema"
              }
            }
          }
        }
      }
    },
    "trait": {
      "type": "object",
      "description": "Trait/mixin definition",
      "properties": {
        "description": {
          "type": "string"
        },
        "extends": {
          "type": "array",
          "description": "List of trait names this trait extends (inherits fields from)",
          "items": {
            "type": "string"
          }
        },
        "fields": {
          "type": "object",
          "description": "Fields provided by this trait",
          "additionalProperties": {
            "$ref": "#/$defs/propertySchema"
          }
        },
        "requires": {
          "type": "array",
          "description": "MIF fields this trait requires",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "relationship": {
      "type": "object",
      "description": "Relationship type definition",
      "properties": {
        "description": {
          "type": "string"
        },
        "from": {
          "type": "array",
          "description": "Entity types that can be the source",
          "items": {
            "type": "string"
          }
        },
        "to": {
          "type": "array",
          "description": "Entity types that can be the target",
          "items": {
            "type": "string"
          }
        },
        "symmetric": {
          "type": "boolean",
          "description": "Whether the relationship is symmetric",
          "default": false
        }
      }
    },
    "contentPattern": {
      "type": "object",
      "description": "Pattern matched against user prompts and memory content",
      "required": ["pattern"],
      "properties": {
        "pattern": {
          "type": "string",
          "description": "Regex pattern to match in content"
        },
        "namespace": {
          "type": "string",
          "description": "Namespace to suggest (hierarchical path)"
        },
        "namespaces": {
          "type": "array",
          "description": "Multiple namespaces to suggest",
          "items": { "type": "string" }
        },
        "suggest_entity": {
          "type": "string",
          "description": "Entity type to suggest when pattern matches"
        },
        "context": {
          "type": "string",
          "description": "Human-readable context hint"
        }
      }
    },
    "unifiedPattern": {
      "type": "object",
      "description": "Unified pattern with content_pattern or file_pattern discriminator",
      "properties": {
        "content_pattern": {
          "type": "string",
          "description": "Regex pattern to match in content"
        },
        "file_pattern": {
          "type": "string",
          "description": "Glob pattern to match file paths"
        },
        "suggest_namespace": {
          "type": "string",
          "description": "Namespace to suggest"
        },
        "suggest_entity": {
          "type": "string",
          "description": "Entity type to suggest"
        }
      }
    },
    "filePattern": {
      "type": "object",
      "description": "Pattern matched against file paths being edited",
      "required": ["pattern"],
      "properties": {
        "pattern": {
          "type": "string",
          "description": "Regex pattern to match in file paths"
        },
        "namespaces": {
          "type": "array",
          "description": "Namespaces to suggest (hierarchical paths)",
          "items": {
            "type": "string"
          }
        },
        "context": {
          "type": "string",
          "description": "Human-readable context hint for this pattern"
        },
        "suggest_entity": {
          "type": "string",
          "description": "Entity type to suggest when pattern matches"
        }
      }
    },
    "propertySchema": {
      "type": "object",
      "description": "JSON Schema property definition",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "string",
            "number",
            "integer",
            "boolean",
            "array",
            "object",
            "null"
          ]
        },
        "description": {
          "type": "string"
        },
        "format": {
          "type": "string",
          "description": "Format hint (e.g., 'uri', 'date-time')"
        },
        "pattern": {
          "type": "string",
          "description": "Regex pattern for string validation"
        },
        "items": {
          "type": "object",
          "description": "Schema for array items"
        },
        "enum": {
          "type": "array",
          "description": "Allowed values"
        }
      }
    }
  }
}
