{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://commonagent.net/composition/schema/composition.schema.json",
  "title": "Agent Composition 0.2.2-draft: composition.json",
  "description": "What an agent is made of and how its parts relate. Section 6 of the Agent Composition specification, version 0.2.2-draft.",
  "type": "object",
  "required": ["agent", "parts", "relationships"],
  "additionalProperties": false,
  "properties": {
    "$schema": { "type": "string" },
    "note": {
      "description": "A sentence for a reader, such as that this composition is an excerpt.",
      "type": "string"
    },
    "agent": {
      "description": "The agent, by the name and version its Common Agent Descriptor carries. Relationships name the agent by this name.",
      "type": "object",
      "required": ["name", "version"],
      "additionalProperties": false,
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "source": { "description": "Where the agent's source lives.", "type": "string" },
        "license": { "type": "string", "minLength": 1 }
      }
    },
    "parts": {
      "type": "array",
      "items": { "$ref": "#/$defs/part" }
    },
    "relationships": {
      "type": "array",
      "items": { "$ref": "#/$defs/relationship" }
    }
  },
  "$defs": {
    "id": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]*$" },
    "partKind": {
      "description": "The six kinds of part and the four kinds of need of section 2.",
      "enum": ["agent", "instructions", "skill", "file", "tool", "helper", "model", "compute", "key", "material"]
    },
    "relationshipName": {
      "description": "The nine relationships of section 3.",
      "enum": ["follows", "has", "routes-to", "reads", "builds-on", "stores-apart", "copied-from", "needs", "shares"]
    },
    "part": {
      "type": "object",
      "required": ["id", "kind"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "kind": { "$ref": "#/$defs/partKind" },
        "portable": {
          "description": "True for a part that works on any harness that follows the standard it names.",
          "const": true
        },
        "bound": {
          "description": "The harness a bound part is bound to.",
          "type": "string",
          "minLength": 1
        },
        "native": {
          "description": "The framework's own name for the part.",
          "type": "string",
          "minLength": 1
        },
        "path": { "type": "string", "minLength": 1 },
        "description": { "type": "string" },
        "size": { "description": "A file's size in bytes.", "type": "integer", "minimum": 0 },
        "sha256": { "description": "A file's sha256, as 64 lower-case hexadecimal digits.", "type": "string", "pattern": "^[0-9a-f]{64}$" },
        "needs": { "description": "For a compute need: what the machine must provide.", "type": "array", "items": { "type": "string", "minLength": 1 } },
        "service": { "description": "For a key: the service the key is for. Never the secret.", "type": "string", "minLength": 1 },
        "for": { "description": "For a key: what the key is used for.", "type": "string", "minLength": 1 },
        "role": { "description": "For a model: the role it plays.", "type": "string", "minLength": 1 },
        "must": { "description": "For a model: what it must be able to do.", "type": "array", "items": { "type": "string", "minLength": 1 } }
      },
      "allOf": [
        {
          "description": "Rule 2: a part of one of the six kinds says portable or bound, and a bound part names its harness.",
          "if": { "properties": { "kind": { "enum": ["agent", "instructions", "skill", "file", "tool", "helper"] } } },
          "then": {
            "oneOf": [
              { "required": ["portable"], "not": { "required": ["bound"] } },
              { "required": ["bound"], "not": { "required": ["portable"] } }
            ]
          },
          "else": {
            "description": "A need says neither portable nor bound.",
            "not": { "anyOf": [{ "required": ["portable"] }, { "required": ["bound"] }] }
          }
        },
        {
          "description": "Rule 4: a key names the service, and holds no secret.",
          "if": { "properties": { "kind": { "const": "key" } } },
          "then": { "required": ["service"] }
        },
        {
          "if": { "properties": { "kind": { "const": "compute" } } },
          "else": { "not": { "required": ["needs"] } }
        },
        {
          "if": { "properties": { "kind": { "const": "key" } } },
          "else": { "not": { "anyOf": [{ "required": ["service"] }, { "required": ["for"] }] } }
        },
        {
          "if": { "properties": { "kind": { "const": "model" } } },
          "else": { "not": { "anyOf": [{ "required": ["role"] }, { "required": ["must"] }] } }
        },
        {
          "if": { "properties": { "kind": { "const": "file" } } },
          "else": { "not": { "anyOf": [{ "required": ["size"] }, { "required": ["sha256"] }] } }
        }
      ]
    },
    "relationship": {
      "type": "object",
      "required": ["from", "rel"],
      "additionalProperties": false,
      "properties": {
        "from": { "type": "string", "minLength": 1 },
        "rel": { "$ref": "#/$defs/relationshipName" },
        "to": { "type": "string", "minLength": 1, "description": "A part id, or skills or tools for all of the agent's skills or tools." },
        "file": { "description": "For reads: the file of the other skill that is read.", "type": "string", "minLength": 1 },
        "source": { "description": "For copied-from: where the part was copied from.", "type": "string", "minLength": 1 },
        "version": { "description": "For copied-from: the version it was copied at.", "type": "string", "minLength": 1 },
        "license": { "description": "For copied-from: the license it was copied under.", "type": "string", "minLength": 1 }
      },
      "allOf": [
        {
          "if": { "properties": { "rel": { "const": "copied-from" } } },
          "then": {
            "required": ["source"],
            "not": { "required": ["to"] }
          },
          "else": {
            "required": ["to"],
            "not": { "anyOf": [{ "required": ["source"] }, { "required": ["version"] }, { "required": ["license"] }] }
          }
        },
        {
          "if": { "properties": { "rel": { "const": "reads" } } },
          "else": { "not": { "required": ["file"] } }
        }
      ]
    }
  }
}
