{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://codeagent.local/schemas/plugin-manifest-v1.json",
  "title": "CodeAgent Declarative Plugin Manifest v1",
  "description": "Editor-facing structural schema. CodeAgent's validatePluginManifest API remains authoritative for semantic, permission, reference, digest, and signature checks.",
  "type": "object",
  "additionalProperties": false,
  "required": ["id", "name", "version"],
  "properties": {
    "manifestVersion": { "const": 1 },
    "id": { "$ref": "#/$defs/id" },
    "name": { "$ref": "#/$defs/name" },
    "version": {
      "type": "string",
      "minLength": 1,
      "maxLength": 64,
      "pattern": "^[0-9A-Za-z][0-9A-Za-z.+-]{0,63}$"
    },
    "description": { "$ref": "#/$defs/description" },
    "instructions": { "type": "string", "minLength": 1, "maxLength": 40000 },
    "publisher": { "$ref": "#/$defs/publisher" },
    "permissions": { "$ref": "#/$defs/permissions" },
    "contributions": {
      "type": "array",
      "maxItems": 16,
      "uniqueItems": true,
      "items": {
        "enum": ["instructions", "tools", "skills", "commands", "agents", "ui", "mcp"]
      }
    },
    "tools": {
      "type": "array",
      "minItems": 1,
      "maxItems": 16,
      "items": { "$ref": "#/$defs/tool" }
    },
    "skills": {
      "type": "array",
      "minItems": 1,
      "maxItems": 16,
      "items": { "$ref": "#/$defs/skill" }
    },
    "commands": {
      "type": "array",
      "minItems": 1,
      "maxItems": 24,
      "items": { "$ref": "#/$defs/command" }
    },
    "agents": {
      "type": "array",
      "minItems": 1,
      "maxItems": 16,
      "items": { "$ref": "#/$defs/agent" }
    },
    "ui": {
      "type": "array",
      "minItems": 1,
      "maxItems": 12,
      "items": { "$ref": "#/$defs/ui" }
    },
    "mcp": {
      "type": "array",
      "minItems": 1,
      "maxItems": 8,
      "items": { "$ref": "#/$defs/mcp" }
    },
    "sandbox": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode"],
      "properties": { "mode": { "const": "declarative" } }
    },
    "signature": { "$ref": "#/$defs/signature" }
  },
  "$defs": {
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 64,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9-]{0,63}$"
    },
    "name": { "type": "string", "minLength": 1, "maxLength": 120 },
    "description": { "type": "string", "minLength": 1, "maxLength": 2000 },
    "toolName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$"
    },
    "permission": {
      "enum": [
        "workspace.read",
        "workspace.write",
        "shell.execute",
        "network.access",
        "credentials.read",
        "mcp.start",
        "git.read",
        "git.write",
        "artifact.read",
        "artifact.write"
      ]
    },
    "permissions": {
      "type": "array",
      "maxItems": 16,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/permission" }
    },
    "toolNames": {
      "type": "array",
      "maxItems": 32,
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/toolName" }
    },
    "publisher": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": {
        "name": { "$ref": "#/$defs/name" },
        "url": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500,
          "format": "uri",
          "pattern": "^https://"
        }
      }
    },
    "tool": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "target", "permissions"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "$ref": "#/$defs/name" },
        "description": { "$ref": "#/$defs/description" },
        "target": { "$ref": "#/$defs/toolName" },
        "permissions": { "$ref": "#/$defs/permissions" }
      }
    },
    "skill": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "tools", "permissions", "instructions"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "$ref": "#/$defs/name" },
        "description": { "$ref": "#/$defs/description" },
        "tools": { "$ref": "#/$defs/toolNames" },
        "permissions": { "$ref": "#/$defs/permissions" },
        "instructions": { "type": "string", "minLength": 1, "maxLength": 20000 }
      }
    },
    "command": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "title", "prompt"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "title": { "$ref": "#/$defs/name" },
        "description": { "$ref": "#/$defs/description" },
        "prompt": { "type": "string", "minLength": 1, "maxLength": 12000 }
      }
    },
    "agent": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "tools", "permissions", "instructions"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "$ref": "#/$defs/name" },
        "description": { "$ref": "#/$defs/description" },
        "tools": { "$ref": "#/$defs/toolNames" },
        "permissions": { "$ref": "#/$defs/permissions" },
        "instructions": { "type": "string", "minLength": 1, "maxLength": 20000 }
      }
    },
    "uiText": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "text"],
      "properties": {
        "kind": { "const": "text" },
        "text": { "type": "string", "minLength": 1, "maxLength": 4000 }
      }
    },
    "uiKeyValue": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "label", "value"],
      "properties": {
        "kind": { "const": "key_value" },
        "label": { "$ref": "#/$defs/name" },
        "value": { "type": "string", "minLength": 1, "maxLength": 500 }
      }
    },
    "uiBadge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "label", "tone"],
      "properties": {
        "kind": { "const": "badge" },
        "label": { "$ref": "#/$defs/name" },
        "tone": { "enum": ["neutral", "success", "warning"] }
      }
    },
    "uiCommand": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "label", "commandId"],
      "properties": {
        "kind": { "const": "command" },
        "label": { "$ref": "#/$defs/name" },
        "commandId": { "$ref": "#/$defs/id" }
      }
    },
    "uiAgent": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "label", "agentId"],
      "properties": {
        "kind": { "const": "agent" },
        "label": { "$ref": "#/$defs/name" },
        "agentId": { "$ref": "#/$defs/id" }
      }
    },
    "ui": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "title", "placements", "elements"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "title": { "$ref": "#/$defs/name" },
        "description": { "$ref": "#/$defs/description" },
        "placements": {
          "type": "array",
          "minItems": 1,
          "maxItems": 2,
          "uniqueItems": true,
          "items": { "enum": ["plugin-manager", "command-palette"] }
        },
        "elements": {
          "type": "array",
          "minItems": 1,
          "maxItems": 24,
          "items": {
            "oneOf": [
              { "$ref": "#/$defs/uiText" },
              { "$ref": "#/$defs/uiKeyValue" },
              { "$ref": "#/$defs/uiBadge" },
              { "$ref": "#/$defs/uiCommand" },
              { "$ref": "#/$defs/uiAgent" }
            ]
          }
        }
      }
    },
    "environment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "label"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,127}$"
        },
        "label": { "$ref": "#/$defs/name" },
        "description": { "type": "string", "minLength": 1, "maxLength": 500 },
        "required": { "type": "boolean" },
        "secret": { "type": "boolean" }
      }
    },
    "mcp": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "command", "permissions"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "$ref": "#/$defs/name" },
        "description": { "$ref": "#/$defs/description" },
        "command": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500,
          "pattern": "^[^\\u0000\\r\\n]+$"
        },
        "args": {
          "type": "array",
          "maxItems": 32,
          "items": { "type": "string", "maxLength": 1000 }
        },
        "environment": {
          "type": "array",
          "maxItems": 16,
          "items": { "$ref": "#/$defs/environment" }
        },
        "permissions": {
          "type": "array",
          "minItems": 1,
          "maxItems": 1,
          "items": { "const": "mcp.start" }
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "publicKey", "value"],
      "properties": {
        "algorithm": { "const": "ed25519" },
        "publicKey": { "type": "string", "minLength": 1, "maxLength": 4096 },
        "value": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256,
          "pattern": "^[A-Za-z0-9+/]+={0,2}$"
        }
      }
    }
  }
}
