{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://ctxpkg.org/schemas/context-package-v1.schema.json",
  "title": "Context Package v1",
  "description": "Schema for .ctxpkg files \u2014 portable, versioned, verifiable AI-agent context.",
  "type": "object",
  "required": [
    "manifest",
    "content"
  ],
  "additionalProperties": false,
  "properties": {
    "manifest": {
      "$ref": "#/$defs/Manifest"
    },
    "content": {
      "$ref": "#/$defs/Content"
    }
  },
  "$defs": {
    "Manifest": {
      "type": "object",
      "required": [
        "schema_version",
        "name",
        "version",
        "description",
        "created_at",
        "layers",
        "integrity",
        "provenance"
      ],
      "additionalProperties": false,
      "properties": {
        "schema_version": {
          "type": "integer",
          "const": 1
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[a-zA-Z0-9._-]+$"
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64,
          "pattern": "^[a-zA-Z0-9_+-][a-zA-Z0-9._+-]*$"
        },
        "description": {
          "type": "string"
        },
        "author": {
          "type": "string"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        },
        "updated_at": {
          "type": "string",
          "format": "date-time"
        },
        "layers": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/LayerType"
          }
        },
        "dependencies": {
          "type": "array",
          "default": [],
          "items": {
            "$ref": "#/$defs/Dependency"
          }
        },
        "tags": {
          "type": "array",
          "default": [],
          "items": {
            "type": "string"
          }
        },
        "integrity": {
          "$ref": "#/$defs/Integrity"
        },
        "provenance": {
          "$ref": "#/$defs/Provenance"
        },
        "compatibility": {
          "$ref": "#/$defs/Compatibility"
        },
        "stats": {
          "$ref": "#/$defs/Stats"
        }
      }
    },
    "LayerType": {
      "type": "string",
      "enum": [
        "knowledge",
        "graph",
        "session",
        "patterns",
        "gotchas"
      ]
    },
    "Dependency": {
      "type": "object",
      "required": [
        "name",
        "version_req"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "version_req": {
          "type": "string"
        },
        "optional": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "Integrity": {
      "type": "object",
      "required": [
        "sha256",
        "content_hash",
        "byte_size"
      ],
      "additionalProperties": false,
      "properties": {
        "sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "content_hash": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "byte_size": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "Provenance": {
      "type": "object",
      "required": [
        "tool",
        "tool_version"
      ],
      "additionalProperties": false,
      "properties": {
        "tool": {
          "type": "string"
        },
        "tool_version": {
          "type": "string"
        },
        "project_hash": {
          "type": "string"
        },
        "source_session_id": {
          "type": "string"
        }
      }
    },
    "Compatibility": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "min_lean_ctx_version": {
          "type": "string"
        },
        "target_languages": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "target_frameworks": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "Stats": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "knowledge_facts": {
          "type": "integer",
          "default": 0
        },
        "graph_nodes": {
          "type": "integer",
          "default": 0
        },
        "graph_edges": {
          "type": "integer",
          "default": 0
        },
        "pattern_count": {
          "type": "integer",
          "default": 0
        },
        "gotcha_count": {
          "type": "integer",
          "default": 0
        },
        "compression_ratio": {
          "type": "number",
          "default": 0
        }
      }
    },
    "Content": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "knowledge": {
          "$ref": "#/$defs/KnowledgeLayer"
        },
        "graph": {
          "$ref": "#/$defs/GraphLayer"
        },
        "session": {
          "$ref": "#/$defs/SessionLayer"
        },
        "patterns": {
          "$ref": "#/$defs/PatternsLayer"
        },
        "gotchas": {
          "$ref": "#/$defs/GotchasLayer"
        }
      }
    },
    "KnowledgeLayer": {
      "type": "object",
      "required": [
        "facts",
        "patterns",
        "insights",
        "exported_at"
      ],
      "properties": {
        "facts": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/KnowledgeFact"
          }
        },
        "patterns": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/ProjectPattern"
          }
        },
        "insights": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/ConsolidatedInsight"
          }
        },
        "exported_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "KnowledgeFact": {
      "type": "object",
      "required": [
        "category",
        "key",
        "value",
        "confidence",
        "source"
      ],
      "properties": {
        "category": {
          "type": "string"
        },
        "key": {
          "type": "string"
        },
        "value": {
          "type": "string"
        },
        "confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "source": {
          "type": "string"
        },
        "imported_from": {
          "type": "string"
        }
      }
    },
    "ProjectPattern": {
      "type": "object",
      "required": [
        "name",
        "description"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "file_patterns": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "examples": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "ConsolidatedInsight": {
      "type": "object",
      "required": [
        "summary"
      ],
      "properties": {
        "summary": {
          "type": "string"
        },
        "supporting_facts": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        }
      }
    },
    "GraphLayer": {
      "type": "object",
      "required": [
        "nodes",
        "edges",
        "exported_at"
      ],
      "properties": {
        "nodes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/GraphNode"
          }
        },
        "edges": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/GraphEdge"
          }
        },
        "exported_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "GraphNode": {
      "type": "object",
      "required": [
        "kind",
        "name",
        "file_path"
      ],
      "properties": {
        "kind": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "file_path": {
          "type": "string"
        },
        "line_start": {
          "type": "integer"
        },
        "line_end": {
          "type": "integer"
        },
        "metadata": {
          "type": "string"
        }
      }
    },
    "GraphEdge": {
      "type": "object",
      "required": [
        "source_path",
        "source_name",
        "target_path",
        "target_name",
        "kind"
      ],
      "properties": {
        "source_path": {
          "type": "string"
        },
        "source_name": {
          "type": "string"
        },
        "target_path": {
          "type": "string"
        },
        "target_name": {
          "type": "string"
        },
        "kind": {
          "type": "string"
        },
        "metadata": {
          "type": "string"
        }
      }
    },
    "SessionLayer": {
      "type": "object",
      "required": [
        "findings",
        "decisions",
        "next_steps",
        "files_touched",
        "exported_at"
      ],
      "properties": {
        "task_description": {
          "type": "string"
        },
        "findings": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/SessionFinding"
          }
        },
        "decisions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/SessionDecision"
          }
        },
        "next_steps": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "files_touched": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "exported_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "SessionFinding": {
      "type": "object",
      "required": [
        "summary"
      ],
      "properties": {
        "summary": {
          "type": "string"
        },
        "file": {
          "type": "string"
        },
        "line": {
          "type": "integer"
        }
      }
    },
    "SessionDecision": {
      "type": "object",
      "required": [
        "summary"
      ],
      "properties": {
        "summary": {
          "type": "string"
        },
        "rationale": {
          "type": "string"
        }
      }
    },
    "PatternsLayer": {
      "type": "object",
      "required": [
        "patterns",
        "exported_at"
      ],
      "properties": {
        "patterns": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/ProjectPattern"
          }
        },
        "exported_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "GotchasLayer": {
      "type": "object",
      "required": [
        "gotchas",
        "exported_at"
      ],
      "properties": {
        "gotchas": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/Gotcha"
          }
        },
        "exported_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "Gotcha": {
      "type": "object",
      "required": [
        "id",
        "category",
        "severity",
        "trigger",
        "resolution",
        "confidence"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "category": {
          "type": "string"
        },
        "severity": {
          "type": "string",
          "enum": [
            "critical",
            "high",
            "medium",
            "low"
          ]
        },
        "trigger": {
          "type": "string"
        },
        "resolution": {
          "type": "string"
        },
        "file_patterns": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        }
      }
    }
  }
}