{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://ctxpkg.org/schemas/context-package-v2.schema.json",
  "title": ".ctxpkg v2 Package",
  "description": "Schema for .ctxpkg v2 context packages — graph-native, composable AI-agent context.",
  "type": "object",
  "required": ["manifest", "content"],
  "properties": {
    "manifest": { "$ref": "#/$defs/Manifest" },
    "content": { "$ref": "#/$defs/Content" }
  },
  "$defs": {
    "Manifest": {
      "type": "object",
      "required": ["schema_version", "name", "version", "description", "created_at", "layers", "integrity", "provenance"],
      "properties": {
        "schema_version": {
          "type": "integer",
          "enum": [1, 2],
          "description": "1 for v1, 2 for v2 packages"
        },
        "conformance_level": {
          "type": "integer",
          "enum": [1, 2, 3],
          "description": "1=Basic, 2=Graph, 3=Cognitive"
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[a-zA-Z0-9._@/-]+$",
          "description": "Package name, optionally scoped as @scope/name"
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64,
          "description": "SemVer version string"
        },
        "description": {
          "type": "string",
          "description": "Human-readable package description"
        },
        "author": {
          "type": "string"
        },
        "scope": {
          "type": "string",
          "description": "Namespace prefix (e.g., @company, @verified)"
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        },
        "updated_at": {
          "type": "string",
          "format": "date-time"
        },
        "layers": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["knowledge", "graph", "session", "patterns", "gotchas"]
          },
          "uniqueItems": true,
          "description": "Legacy v1 layer indicators"
        },
        "dependencies": {
          "type": "array",
          "items": { "$ref": "#/$defs/Dependency" }
        },
        "tags": {
          "type": "array",
          "items": { "type": "string" }
        },
        "integrity": { "$ref": "#/$defs/Integrity" },
        "provenance": { "$ref": "#/$defs/Provenance" },
        "compatibility": { "$ref": "#/$defs/Compatibility" },
        "stats": { "$ref": "#/$defs/Stats" },
        "signature": { "$ref": "#/$defs/Signature" },
        "graph_summary": { "$ref": "#/$defs/GraphSummary" },
        "marketplace": { "$ref": "#/$defs/Marketplace" }
      }
    },
    "Content": {
      "type": "object",
      "properties": {
        "knowledge": { "$ref": "#/$defs/KnowledgeLayer" },
        "graph": { "$ref": "#/$defs/GraphLayer" },
        "session": { "$ref": "#/$defs/SessionLayer" },
        "patterns": { "$ref": "#/$defs/PatternsLayer" },
        "gotchas": { "$ref": "#/$defs/GotchasLayer" },
        "context_graph": { "$ref": "#/$defs/ContextGraph" }
      }
    },
    "ContextGraph": {
      "type": "object",
      "required": ["format", "nodes", "edges"],
      "properties": {
        "format": {
          "type": "string",
          "const": "ctxpkg-graph-v2"
        },
        "nodes": {
          "type": "array",
          "items": { "$ref": "#/$defs/ContextNode" }
        },
        "edges": {
          "type": "array",
          "items": { "$ref": "#/$defs/ContextEdge" }
        }
      }
    },
    "ContextNode": {
      "type": "object",
      "required": ["id", "type", "content"],
      "properties": {
        "id": {
          "type": "string",
          "description": "Unique identifier within this package"
        },
        "type": {
          "type": "string",
          "description": "Node type (fact, gotcha, decision, code_symbol, etc.)"
        },
        "content": {
          "type": "string",
          "description": "Node content / value"
        },
        "activation": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "default": 1.0,
          "description": "Activation energy (Phi score)"
        },
        "category": { "type": "string" },
        "source": { "type": "string" },
        "created_at": { "type": "string", "format": "date-time" },
        "decay_half_life_days": {
          "type": "integer",
          "minimum": 0,
          "description": "Temporal decay half-life in days"
        },
        "blob_ref": {
          "type": "string",
          "description": "Reference to content-addressable blob (sha256-...)"
        },
        "file_path": { "type": "string" },
        "line_start": { "type": "integer", "minimum": 0 },
        "line_end": { "type": "integer", "minimum": 0 },
        "confidence": {
          "type": "number",
          "minimum": 0,
          "maximum": 1
        },
        "supersedes": {
          "type": "string",
          "description": "ID of node this supersedes"
        }
      }
    },
    "ContextEdge": {
      "type": "object",
      "required": ["from", "to", "type"],
      "properties": {
        "from": {
          "type": "string",
          "description": "Source node ID"
        },
        "to": {
          "type": "string",
          "description": "Target node ID"
        },
        "type": {
          "type": "string",
          "description": "Edge type (supports, contradicts, co_activated, etc.)"
        },
        "weight": {
          "type": "number",
          "default": 1.0,
          "description": "Edge weight"
        },
        "coactivations": {
          "type": "integer",
          "minimum": 0,
          "default": 0,
          "description": "Hebbian co-activation counter"
        },
        "metadata": { "type": "string" }
      }
    },
    "GraphSummary": {
      "type": "object",
      "properties": {
        "node_count": { "type": "integer", "minimum": 0 },
        "edge_count": { "type": "integer", "minimum": 0 },
        "node_types": {
          "type": "array",
          "items": { "type": "string" }
        },
        "activation_mean": { "type": "number" },
        "freshness": { "type": "string", "format": "date-time" }
      }
    },
    "Marketplace": {
      "type": "object",
      "properties": {
        "categories": {
          "type": "array",
          "items": { "type": "string" }
        },
        "badges": {
          "type": "array",
          "items": { "type": "string" }
        },
        "license": { "type": "string" }
      }
    },
    "Dependency": {
      "type": "object",
      "required": ["name", "version_req"],
      "properties": {
        "name": { "type": "string" },
        "version_req": { "type": "string" },
        "optional": { "type": "boolean", "default": false }
      }
    },
    "Integrity": {
      "type": "object",
      "required": ["sha256", "content_hash", "byte_size"],
      "properties": {
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "content_hash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "byte_size": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "Provenance": {
      "type": "object",
      "required": ["tool", "tool_version"],
      "properties": {
        "tool": { "type": "string" },
        "tool_version": { "type": "string" },
        "project_hash": { "type": "string" },
        "source_session_id": { "type": "string" }
      }
    },
    "Compatibility": {
      "type": "object",
      "properties": {
        "min_lean_ctx_version": { "type": "string" },
        "target_languages": {
          "type": "array",
          "items": { "type": "string" }
        },
        "target_frameworks": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "Signature": {
      "type": "object",
      "required": ["algorithm", "public_key", "value"],
      "properties": {
        "algorithm": {
          "type": "string",
          "enum": ["ed25519"]
        },
        "public_key": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "value": {
          "type": "string",
          "pattern": "^[a-f0-9]{128}$"
        }
      }
    },
    "Stats": {
      "type": "object",
      "properties": {
        "knowledge_facts": { "type": "integer", "minimum": 0 },
        "graph_nodes": { "type": "integer", "minimum": 0 },
        "graph_edges": { "type": "integer", "minimum": 0 },
        "pattern_count": { "type": "integer", "minimum": 0 },
        "gotcha_count": { "type": "integer", "minimum": 0 },
        "compression_ratio": { "type": "number" }
      }
    },
    "KnowledgeLayer": {
      "type": "object",
      "required": ["facts", "patterns", "insights", "exported_at"],
      "properties": {
        "facts": { "type": "array" },
        "patterns": { "type": "array" },
        "insights": { "type": "array" },
        "exported_at": { "type": "string", "format": "date-time" }
      }
    },
    "GraphLayer": {
      "type": "object",
      "required": ["nodes", "edges", "exported_at"],
      "properties": {
        "nodes": { "type": "array" },
        "edges": { "type": "array" },
        "exported_at": { "type": "string", "format": "date-time" }
      }
    },
    "SessionLayer": {
      "type": "object",
      "required": ["findings", "decisions", "next_steps", "files_touched", "exported_at"],
      "properties": {
        "task_description": { "type": "string" },
        "findings": { "type": "array" },
        "decisions": { "type": "array" },
        "next_steps": { "type": "array", "items": { "type": "string" } },
        "files_touched": { "type": "array", "items": { "type": "string" } },
        "exported_at": { "type": "string", "format": "date-time" }
      }
    },
    "PatternsLayer": {
      "type": "object",
      "required": ["patterns", "exported_at"],
      "properties": {
        "patterns": { "type": "array" },
        "exported_at": { "type": "string", "format": "date-time" }
      }
    },
    "GotchasLayer": {
      "type": "object",
      "required": ["gotchas", "exported_at"],
      "properties": {
        "gotchas": { "type": "array" },
        "exported_at": { "type": "string", "format": "date-time" }
      }
    }
  }
}
