{
  "$id": "https://schemas.theaiplatform.app/miniapps/source-manifest.v2.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$comment": "Use readSourceManifestV2 for NFC normalization and cross-field target/artifact closure validation.",
  "title": "SourceManifestV2",
  "description": "Provider-neutral exact-byte source manifest accepted by the TAP Registry import boundary.",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schemaVersion": {
      "const": 2
    },
    "versionLabel": {
      "$ref": "#/$defs/versionLabel"
    },
    "localDisplay": {
      "$ref": "#/$defs/localDisplay"
    },
    "targets": {
      "type": "array",
      "minItems": 1,
      "maxItems": 10000,
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/target"
      }
    },
    "artifacts": {
      "type": "array",
      "minItems": 1,
      "maxItems": 10000,
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/artifact"
      }
    },
    "permissions": {
      "$ref": "#/$defs/canonicalNames"
    },
    "runtimeEffects": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/runtimeEffect"
      }
    }
  },
  "required": [
    "schemaVersion",
    "versionLabel",
    "targets",
    "artifacts",
    "permissions",
    "runtimeEffects"
  ],
  "$defs": {
    "canonicalName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$"
    },
    "canonicalNames": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/canonicalName"
      }
    },
    "canonicalText": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048,
      "pattern": "^(?!\\s)(?!.*\\s$)(?!.*[\\p{Cc}\\p{Cf}\\p{Zl}\\p{Zp}\\uD800-\\uDFFF]).+$"
    },
    "versionLabel": {
      "allOf": [
        {
          "$ref": "#/$defs/canonicalText"
        },
        {
          "type": "string",
          "maxLength": 256
        }
      ]
    },
    "artifactPath": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))[A-Za-z0-9][A-Za-z0-9._/-]*$",
      "not": {
        "enum": ["manifest.tap.json", "package.json"]
      }
    },
    "digest": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "length": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "path": {
          "$ref": "#/$defs/artifactPath"
        },
        "digest": {
          "$ref": "#/$defs/digest"
        },
        "length": {
          "$ref": "#/$defs/length"
        }
      },
      "required": ["path", "digest", "length"]
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "target": {
          "$ref": "#/$defs/canonicalName"
        },
        "path": {
          "$ref": "#/$defs/artifactPath"
        },
        "digest": {
          "$ref": "#/$defs/digest"
        },
        "length": {
          "$ref": "#/$defs/length"
        }
      },
      "required": ["target", "path", "digest", "length"]
    },
    "runtimeEffect": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "$ref": "#/$defs/canonicalName"
        },
        "resources": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/canonicalText"
          }
        }
      },
      "required": ["kind", "resources"]
    },
    "localDisplay": {
      "type": "object",
      "additionalProperties": false,
      "minProperties": 1,
      "properties": {
        "name": {
          "allOf": [
            {
              "$ref": "#/$defs/canonicalText"
            },
            {
              "type": "string",
              "maxLength": 256
            }
          ]
        },
        "slug": {
          "allOf": [
            {
              "$ref": "#/$defs/canonicalText"
            },
            {
              "type": "string",
              "maxLength": 128
            }
          ]
        }
      }
    }
  }
}
