{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://stemmar.org/spec/0.2/akp-manifest.schema.json",
  "title": "AKP package manifest",
  "description": "Normative schema for manifest.json in an AKP artifact. SPEC-0001 §3. Where this schema and the prose disagree, this schema is correct for structure and the prose is correct for meaning.",
  "type": "object",
  "required": ["akp", "id", "version", "assets", "knowledge", "integrity"],
  "additionalProperties": false,
  "properties": {
    "akp": {
      "description": "The specification version this artifact was written against.",
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+$"
    },
    "id": {
      "description": "Reverse-DNS package identifier. Lowercase, at least two labels.",
      "type": "string",
      "pattern": "^[a-z]([a-z0-9-]*[a-z0-9])?(\\.[a-z]([a-z0-9-]*[a-z0-9])?)+$",
      "maxLength": 253
    },
    "version": {
      "description": "SemVer 2.0.0.",
      "type": "string",
      "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
    },
    "name": { "type": "string", "maxLength": 200 },
    "assets": {
      "description": "Every source document, byte-identical to what the producer was given.",
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/asset" }
    },
    "knowledge": {
      "description": "The JSON Lines files carrying knowledge objects.",
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/knowledgeFile" }
    },
    "integrity": {
      "description": "The integrity record holding the Merkle leaves over canonical content.",
      "type": "object",
      "required": ["path", "digest"],
      "additionalProperties": false,
      "properties": {
        "path": { "$ref": "#/$defs/path" },
        "digest": { "$ref": "#/$defs/digest" }
      }
    },
    "derived": {
      "description": "Paths holding derived artifacts. Never canonical; deleting them leaves identity unchanged. SPEC-0001 §8.",
      "type": "array",
      "items": {
        "type": "object",
        "required": ["path", "kind"],
        "properties": {
          "path": { "$ref": "#/$defs/path" },
          "kind": { "type": "string", "enum": ["embedding", "index", "retrieval-state"] },
          "profile": {
            "description": "How the derived data was produced. Canonical, unlike the data itself.",
            "type": "object"
          }
        }
      }
    },
    "diagnostics": {
      "description": "Extraction failures and unsupported content. SPEC-0001 §4.3. Presence does not affect validity.",
      "type": "array",
      "items": { "$ref": "#/$defs/diagnostic" }
    },
    "criticalExtensions": {
      "description": "Extension keys a consumer MUST understand or refuse the artifact. SPEC-0001 §9.",
      "type": "array",
      "items": { "$ref": "#/$defs/extensionKey" },
      "uniqueItems": true
    },
    "extensions": {
      "description": "Namespaced extension data. Unknown keys MUST round-trip byte-for-byte.",
      "type": "object",
      "propertyNames": { "$ref": "#/$defs/extensionKey" }
    }
  },
  "$defs": {
    "digest": {
      "description": "BLAKE3-256, lowercase hex, algorithm-prefixed. SPEC-0001 §7.1.",
      "type": "string",
      "pattern": "^blake3:[0-9a-f]{64}$"
    },
    "path": {
      "description": "Container-relative path. No absolute paths, no traversal, no backslashes.",
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "pattern": "^(?!/)(?!.*\\.\\.)[^\\\\]+$"
    },
    "assetId": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{1,64}$"
    },
    "extensionKey": {
      "description": "Reverse-DNS. A key with no dot is reserved for the specification.",
      "type": "string",
      "pattern": "^[a-z]([a-z0-9-]*[a-z0-9])?(\\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$"
    },
    "asset": {
      "type": "object",
      "required": ["id", "path", "mediaType", "digest", "octets"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/assetId" },
        "path": { "$ref": "#/$defs/path" },
        "mediaType": {
          "description": "The media type as received. SPEC-0001 §4.1 lists what a producer must accept.",
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9!#$&^_.+-]{0,126}/[a-z0-9][a-z0-9!#$&^_.+-]{0,126}$"
        },
        "digest": { "$ref": "#/$defs/digest" },
        "octets": { "type": "integer", "minimum": 0 },
        "name": {
          "description": "The original filename, for a human. Never used to identify the format.",
          "type": "string",
          "maxLength": 255
        },
        "charset": {
          "description": "For text media types where it is not UTF-8.",
          "type": "string",
          "maxLength": 40
        }
      }
    },
    "knowledgeFile": {
      "type": "object",
      "required": ["path", "count", "digest"],
      "additionalProperties": false,
      "properties": {
        "path": { "$ref": "#/$defs/path" },
        "count": {
          "description": "Objects in the file. A reader MUST verify this against the lines it parses.",
          "type": "integer",
          "minimum": 0
        },
        "digest": { "$ref": "#/$defs/digest" }
      }
    },
    "diagnostic": {
      "type": "object",
      "required": ["severity", "code", "message"],
      "additionalProperties": false,
      "properties": {
        "severity": { "type": "string", "enum": ["info", "warning", "error"] },
        "code": {
          "description": "Stable, dotted, machine-readable. e.g. pdf.no-text-layer",
          "type": "string",
          "pattern": "^[a-z0-9]+(\\.[a-z0-9-]+)+$"
        },
        "anchor": { "$ref": "#/$defs/anchor" },
        "message": { "type": "string", "maxLength": 2000 }
      }
    },
    "anchor": {
      "description": "A reference into an asset. SPEC-0001 §6.",
      "type": "string",
      "pattern": "^akp:asset/[A-Za-z0-9_-]{1,64}#[a-z]{1,32}=[A-Za-z0-9\\-._~,:%]{1,256}(&[a-z]{1,32}=[A-Za-z0-9\\-._~,:%]{1,256})*$",
      "maxLength": 2048
    }
  }
}
