{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "CainTalosEnvelope",
  "description": "Cryptographically signed authorization envelope exchanged between CAIN-42 decision engine and TALOS verify kernel.",
  "type": "object",
  "required": [
    "envelope_id",
    "decision_id",
    "request_id",
    "protocol_version",
    "schema_version",
    "principal_did",
    "tenant_id",
    "agent_id",
    "action",
    "target",
    "resource_scope",
    "policy_version",
    "policy_digest",
    "decision",
    "reason_code",
    "iat",
    "exp",
    "nonce",
    "kid",
    "signature_algorithm",
    "signature"
  ],
  "properties": {
    "envelope_id": {
      "type": "string",
      "pattern": "^env-[0-9a-f-]{36}$"
    },
    "decision_id": {
      "type": "string",
      "pattern": "^dec-[0-9a-f-]{36}$"
    },
    "request_id": {
      "type": "string"
    },
    "protocol_version": {
      "type": "string",
      "const": "cain42.talos.v1"
    },
    "schema_version": {
      "type": "string",
      "const": "cain42.talos.envelope.v1"
    },
    "principal_did": {
      "type": "string",
      "pattern": "^did:cain:[a-zA-Z0-9_.-]+:[a-zA-Z0-9_.-]+$"
    },
    "tenant_id": {
      "type": "string"
    },
    "agent_id": {
      "type": "string"
    },
    "action": {
      "type": "string"
    },
    "target": {
      "type": "string"
    },
    "resource_scope": {
      "type": "object",
      "properties": {
        "allowed_read_paths": {
          "type": "array",
          "items": { "type": "string" }
        },
        "allowed_write_paths": {
          "type": "array",
          "items": { "type": "string" }
        },
        "allow_network": {
          "type": "boolean"
        },
        "allowed_tcp_ports": {
          "type": "array",
          "items": { "type": "integer" }
        },
        "max_memory_bytes": {
          "type": "integer"
        },
        "timeout_seconds": {
          "type": "integer"
        }
      }
    },
    "policy_version": {
      "type": "string"
    },
    "policy_digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "decision": {
      "type": "string",
      "enum": ["ALLOW", "DENY", "REQUIRE_APPROVAL"]
    },
    "reason_code": {
      "type": "string"
    },
    "iat": {
      "type": "integer"
    },
    "exp": {
      "type": "integer"
    },
    "nonce": {
      "type": "string"
    },
    "kid": {
      "type": "string"
    },
    "signature_algorithm": {
      "type": "string",
      "enum": ["Ed25519"]
    },
    "approval_reference": {
      "type": ["string", "null"]
    },
    "constraints": {
      "type": "object"
    },
    "signature": {
      "type": "string"
    }
  },
  "additionalProperties": false
}
