{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://missionweaveprotocol.dev/schemas/0.1/common.schema.json",
  "title": "MissionWeaveProtocol 0.1 Common Definitions",
  "$defs": {
    "protocolVersion": {
      "const": "0.1"
    },
    "id": {
      "type": "string",
      "format": "uri",
      "$comment": "The absolute-URI prefix plus the whole-value ASCII guard avoids end-anchor acceptance before a final line feed.",
      "pattern": "^[A-Za-z][A-Za-z0-9+.-]*:",
      "not": {
        "pattern": "[^A-Za-z0-9._~:/?#\\[\\]@!$&'()*+,;=%-]"
      },
      "maxLength": 512
    },
    "kind": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*(?:\\.[a-z][a-z0-9_]*)+$",
      "maxLength": 160
    },
    "timestamp": {
      "type": "string",
      "format": "date-time"
    },
    "safeInteger": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991
    },
    "positiveInteger": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$",
      "maxLength": 128
    },
    "base64url": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]+$",
      "minLength": 2,
      "maxLength": 4096
    },
    "sha256": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "duration": {
      "type": "string",
      "pattern": "^P(?!$)(?:[0-9]+D)?(?:T(?=[0-9])(?:[0-9]+H)?(?:[0-9]+M)?(?:[0-9]+(?:\\.[0-9]+)?S)?)?$",
      "maxLength": 64
    },
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": {
          "enum": ["agent", "human", "service"]
        },
        "id": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "agentActor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": {
          "const": "agent"
        },
        "id": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "humanActor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": {
          "const": "human"
        },
        "id": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "keyId", "createdAt", "value"],
      "properties": {
        "algorithm": {
          "const": "Ed25519"
        },
        "keyId": {
          "$ref": "#/$defs/id"
        },
        "createdAt": {
          "$ref": "#/$defs/timestamp"
        },
        "value": {
          "$ref": "#/$defs/base64url"
        }
      }
    },
    "publicKey": {
      "type": "object",
      "additionalProperties": false,
      "required": ["keyId", "algorithm", "publicKey", "validFrom"],
      "properties": {
        "keyId": {
          "$ref": "#/$defs/id"
        },
        "algorithm": {
          "const": "Ed25519"
        },
        "publicKey": {
          "$ref": "#/$defs/base64url"
        },
        "validFrom": {
          "$ref": "#/$defs/timestamp"
        },
        "validUntil": {
          "$ref": "#/$defs/timestamp"
        },
        "revokedAt": {
          "$ref": "#/$defs/timestamp"
        }
      }
    },
    "capabilityRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9]+(?:[.-][a-z0-9]+)+$",
          "maxLength": 200
        },
        "version": {
          "$ref": "#/$defs/semver"
        }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        },
        "financialLimit": {
          "type": "number",
          "minimum": 0
        },
        "modelTokenLimit": {
          "$ref": "#/$defs/safeInteger"
        },
        "toolCallLimit": {
          "$ref": "#/$defs/safeInteger"
        },
        "computeSecondsLimit": {
          "$ref": "#/$defs/safeInteger"
        },
        "wallClockSecondsLimit": {
          "$ref": "#/$defs/safeInteger"
        },
        "externalSideEffectLimit": {
          "$ref": "#/$defs/safeInteger"
        }
      },
      "anyOf": [
        {"required": ["financialLimit", "currency"]},
        {"required": ["modelTokenLimit"]},
        {"required": ["toolCallLimit"]},
        {"required": ["computeSecondsLimit"]},
        {"required": ["wallClockSecondsLimit"]},
        {"required": ["externalSideEffectLimit"]}
      ]
    },
    "acceptanceCriterion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["criterionId", "description", "evidenceRequired"],
      "properties": {
        "criterionId": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$"
        },
        "description": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "evidenceRequired": {
          "type": "array",
          "items": {
            "enum": [
              "artifact_integrity",
              "schema_validation",
              "deterministic_test",
              "business_rule",
              "reviewer_assessment",
              "human_review",
              "policy_check"
            ]
          },
          "minItems": 1,
          "uniqueItems": true
        }
      }
    },
    "resourceGrant": {
      "type": "object",
      "additionalProperties": false,
      "required": ["resource", "operations"],
      "properties": {
        "resource": {
          "$ref": "#/$defs/id"
        },
        "operations": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9._:-]*$",
            "maxLength": 128
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "constraints": {
          "type": "object"
        }
      }
    },
    "classification": {
      "enum": ["public", "internal", "confidential", "restricted"]
    },
    "extensions": {
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/id"
      },
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "required": ["version", "critical", "data"],
        "properties": {
          "version": {
            "$ref": "#/$defs/semver"
          },
          "critical": {
            "type": "boolean"
          },
          "data": {}
        }
      }
    }
  }
}
