{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Capsule Contract v0.1",
  "type": "object",
  "required": ["module", "version", "identity", "effects", "telemetry", "policy"],
  "properties": {
    "capsule": {
      "type": ["string", "number"]
    },
    "module": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "type": "string",
      "minLength": 1
    },
    "purpose": {
      "type": "string"
    },
    "identity": {
      "type": "object",
      "required": ["kind", "cryptographic", "credentials", "attestation"],
      "properties": {
        "id": {
          "type": "string"
        },
        "kind": {
          "enum": ["agent", "llm_agent", "service"]
        },
        "cryptographic": {
          "enum": ["required", true]
        },
        "credentials": {
          "enum": ["short_lived", "short-lived"]
        },
        "attestation": {
          "enum": ["required", true]
        },
        "owner": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "inputs": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "outputs": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "effects": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "anyOf": [
          { "required": ["read"] },
          { "required": ["write"] },
          { "required": ["call"] },
          { "required": ["emit"] },
          { "required": ["task"] },
          { "required": ["effect"] }
        ],
        "properties": {
          "read": { "type": "string" },
          "write": { "type": "string" },
          "call": { "type": "string" },
          "emit": { "type": "string" },
          "task": { "type": "string" },
          "effect": { "type": "string" },
          "resource": { "type": "string" },
          "max_amount_usd": { "type": "integer", "minimum": 0 },
          "approval_required": { "type": "boolean" },
          "approval_required_above_usd": { "type": "integer", "minimum": 0 },
          "if": { "type": "string" },
          "condition": { "type": "string" }
        },
        "additionalProperties": true
      }
    },
    "telemetry": {
      "type": "object",
      "required": ["required", "tamper_evident"],
      "properties": {
        "required": { "const": true },
        "tamper_evident": { "const": true },
        "trace": { "type": "string" },
        "export": {
          "type": "array",
          "items": { "type": "string" }
        },
        "must_emit": {
          "type": "array",
          "items": { "type": "string" }
        },
        "include": {
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "additionalProperties": true
    },
    "policy": {
      "type": "object",
      "required": ["default"],
      "properties": {
        "id": { "type": "string" },
        "default": { "const": "deny" },
        "approver": { "type": "string" },
        "escalation": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["if", "require"],
            "properties": {
              "if": { "type": "string" },
              "require": {
                "type": "array",
                "contains": { "const": "human_approval" },
                "items": { "type": "string" }
              }
            },
            "additionalProperties": true
          }
        }
      },
      "additionalProperties": true
    },
    "runtime": {
      "type": "object",
      "properties": {
        "image": { "type": "string" },
        "command": {
          "type": "array",
          "items": { "type": "string" }
        },
        "workdir": { "type": "string" },
        "doorkeeper_endpoint": { "type": "string" },
        "backend_preference": {
          "type": "array",
          "items": { "enum": ["docker", "seatbelt"] }
        }
      },
      "additionalProperties": true
    },
    "memory": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
