{
 "$schema": "https://json-schema.org/draft/2020-12/schema",
 "$id": "https://aep.example/schemas/v1/aep-v1.schema.json",
 "title": "Agent Exchange Protocol v1 (Draft r11) — Core wire objects",
 "description": "Entry points: #/$defs/Envelope, #/$defs/Record, #/$defs/Error, #/$defs/SendRequest, #/$defs/SendResponse, #/$defs/SyncRequest, #/$defs/SyncResponse, #/$defs/AckRequest, #/$defs/PolicyRequest, #/$defs/QueryRequest, #/$defs/SignedQueryRequest, #/$defs/QueryResponse, #/$defs/ResolveResponse, #/$defs/GatewayList. Unknown fields are ignored (§5), so objects allow additional properties. Arrays keep the order their writer gave them; set arrays MUST NOT repeat a key (§6), which a schema cannot express.",
 "$defs": {
  "ActorId": {
   "type": "string",
   "description": "agent: or owner: + multibase base58btc of varint(multicodec) ‖ public key (§4.1)",
   "pattern": "^(agent|owner):z[1-9A-HJ-NP-Za-km-z]{20,80}$"
  },
  "Id": {
   "type": "string",
   "description": "ULID (RECOMMENDED) or UUIDv7 text form; opaque, compared byte for byte (§4.3)",
   "pattern": "^[0-9A-Za-z-]{1,36}$"
  },
  "Kid": {
   "type": "string",
   "minLength": 1,
   "maxLength": 64,
   "pattern": "^[^#/\\s]+$",
   "description": "a key's name within its Actor: no #, / or whitespace, so that <actor id>#<kid> reads one way (§4.3)"
  },
  "Timestamp": {
   "type": "string",
   "description": "RFC 3339 UTC, whole seconds, Z suffix (§6)",
   "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
  },
  "Base64url": {
   "type": "string",
   "pattern": "^[A-Za-z0-9_-]+$"
  },
  "Digest": {
   "$ref": "#/$defs/Base64url",
   "description": "SHA-256, base64url without padding (§6)"
  },
  "PinnedRef": {
   "type": "string",
   "description": "issuer/id@v — every reference inside a Record is pinned (§6)",
   "pattern": "^(agent|owner):z[1-9A-HJ-NP-Za-km-z]{20,80}/[0-9A-Za-z-]{1,36}@[1-9][0-9]*$"
  },
  "LookupRef": {
   "type": "string",
   "description": "issuer/id or issuer/id@v — only in lookups (§6)",
   "pattern": "^(agent|owner):z[1-9A-HJ-NP-Za-km-z]{20,80}/[0-9A-Za-z-]{1,36}(@[1-9][0-9]*)?$"
  },
  "EnvelopeRef": {
   "type": "string",
   "description": "env:from/id names an Envelope (§6)",
   "pattern": "^env:(agent|owner):z[1-9A-HJ-NP-Za-km-z]{20,80}/[0-9A-Za-z-]{1,36}$"
  },
  "Money": {
   "type": "object",
   "description": "amount is always a decimal integer string in minor units (§6)",
   "required": [
    "amount",
    "currency"
   ],
   "properties": {
    "amount": {
     "type": "string",
     "pattern": "^-?(0|[1-9][0-9]*)$"
    },
    "currency": {
     "type": "string",
     "description": "ISO 4217 code or CAIP-19 asset id",
     "minLength": 3
    }
   }
  },
  "SafeInteger": {
   "type": "integer",
   "minimum": -9007199254740991,
   "maximum": 9007199254740991
  },
  "SigEntry": {
   "type": "object",
   "description": "One Record signature (§6.2): all five fields required; signed_at is covered by the signature",
   "required": [
    "actor",
    "kid",
    "alg",
    "signed_at",
    "val"
   ],
   "properties": {
    "actor": {
     "$ref": "#/$defs/ActorId"
    },
    "kid": {
     "$ref": "#/$defs/Kid"
    },
    "alg": {
     "type": "string",
     "description": "the signature suite; ed25519 is the only one in v1 (§11.2), and a verifier drops an entry in a suite it does not have like one that does not verify (§6.2)"
    },
    "signed_at": {
     "$ref": "#/$defs/Timestamp"
    },
    "val": {
     "$ref": "#/$defs/Base64url"
    }
   }
  },
  "EnvelopeSig": {
   "type": "object",
   "description": "Transport signature by from (§5, §11.3); no signed_at, the Envelope iat plays that role",
   "required": [
    "actor",
    "kid",
    "alg",
    "val"
   ],
   "properties": {
    "actor": {
     "$ref": "#/$defs/ActorId"
    },
    "kid": {
     "$ref": "#/$defs/Kid"
    },
    "alg": {
     "type": "string",
     "enum": [
      "ed25519"
     ],
     "description": "the only signature suite in v1 (§11.2)"
    },
    "val": {
     "$ref": "#/$defs/Base64url"
    }
   }
  },
  "Wrap": {
   "type": "object",
   "description": "One HPKE-wrapped content key (§11.4)",
   "required": [
    "to",
    "kid",
    "ek",
    "ct"
   ],
   "properties": {
    "to": {
     "$ref": "#/$defs/ActorId"
    },
    "kid": {
     "$ref": "#/$defs/Kid"
    },
    "ek": {
     "$ref": "#/$defs/Base64url"
    },
    "ct": {
     "$ref": "#/$defs/Base64url"
    }
   }
  },
  "Enc": {
   "oneOf": [
    {
     "type": "object",
     "description": "Explicit recipients: with to, exactly one wrap whose to equals the Envelope to",
     "required": [
      "alg",
      "recipients"
     ],
     "properties": {
      "alg": {
       "type": "string",
       "enum": [
        "hpke-x25519-sha256-chacha20",
        "hpke-x25519-sha256-aes128gcm",
        "hpke-xwing-sha256-chacha20"
       ]
      },
      "recipients": {
       "type": "array",
       "minItems": 1,
       "maxItems": 32,
       "items": {
        "$ref": "#/$defs/Wrap"
       }
      }
     }
    },
    {
     "type": "object",
     "description": "Context epoch key (groups.v1, §10.5)",
     "required": [
      "alg",
      "epoch"
     ],
     "properties": {
      "alg": {
       "type": "string"
      },
      "epoch": {
       "type": "integer",
       "minimum": 1
      }
     }
    }
   ]
  },
  "Envelope": {
   "type": "object",
   "title": "Envelope (§5)",
   "required": [
    "v",
    "id",
    "from",
    "iat",
    "exp",
    "body",
    "sig"
   ],
   "properties": {
    "v": {
     "const": 1
    },
    "id": {
     "$ref": "#/$defs/Id"
    },
    "from": {
     "$ref": "#/$defs/ActorId"
    },
    "to": {
     "$ref": "#/$defs/ActorId"
    },
    "ctx": {
     "$ref": "#/$defs/PinnedRef"
    },
    "iat": {
     "$ref": "#/$defs/Timestamp"
    },
    "exp": {
     "$ref": "#/$defs/Timestamp"
    },
    "enc": {
     "$ref": "#/$defs/Enc"
    },
    "body": {
     "oneOf": [
      {
       "$ref": "#/$defs/Record",
       "description": "plaintext Record (enc absent)"
      },
      {
       "$ref": "#/$defs/Base64url",
       "description": "base64url ciphertext (enc present)"
      }
     ]
    },
    "sig": {
     "$ref": "#/$defs/EnvelopeSig"
    }
   },
   "oneOf": [
    {
     "required": [
      "to"
     ],
     "not": {
      "required": [
       "ctx"
      ]
     }
    },
    {
     "required": [
      "ctx"
     ],
     "not": {
      "required": [
       "to"
      ]
     }
    }
   ],
   "dependentSchemas": {
    "enc": {
     "properties": {
      "body": {
       "$ref": "#/$defs/Base64url"
      }
     }
    }
   },
   "allOf": [
    {
     "if": {
      "required": [
       "enc"
      ]
     },
     "then": {
      "properties": {
       "body": {
        "type": "string",
        "description": "base64url ciphertext (§5, §11.4)"
       }
      }
     },
     "else": {
      "properties": {
       "body": {
        "type": "object",
        "description": "a plaintext Record (§5)"
       }
      }
     }
    }
   ]
  },
  "Record": {
   "type": "object",
   "title": "Record (§6)",
   "required": [
    "id",
    "type",
    "v",
    "issuer",
    "iat",
    "body",
    "sigs"
   ],
   "properties": {
    "id": {
     "$ref": "#/$defs/Id"
    },
    "type": {
     "type": "string",
     "minLength": 1
    },
    "profile": {
     "type": "string",
     "pattern": "^[a-z][a-z0-9-]*(\\.[a-z0-9-]+)*\\.v[0-9]+$",
     "description": "the Extension namespace (§4.3): a registered Profile name (exchange.v1) or a reverse-domain vendor name (kr.example.deals.v1)"
    },
    "v": {
     "type": "integer",
     "minimum": 1
    },
    "issuer": {
     "$ref": "#/$defs/ActorId"
    },
    "subject": {
     "description": "an Actor ID, a pinned Record reference, env:from/id, or — in a revocation only — <actor id>#<kid> or issuer/id without @v (every version); every reference inside a Record is otherwise pinned (§6)",
     "anyOf": [
      {
       "$ref": "#/$defs/ActorId"
      },
      {
       "$ref": "#/$defs/PinnedRef"
      },
      {
       "$ref": "#/$defs/EnvelopeRef"
      },
      {
       "$ref": "#/$defs/KeyRef"
      },
      {
       "$ref": "#/$defs/LookupRef"
      }
     ]
    },
    "ctx": {
     "$ref": "#/$defs/PinnedRef"
    },
    "exchange": {
     "$ref": "#/$defs/PinnedRef"
    },
    "parent": {
     "$ref": "#/$defs/PinnedRef"
    },
    "supersedes": {
     "$ref": "#/$defs/PinnedRef"
    },
    "iat": {
     "$ref": "#/$defs/Timestamp"
    },
    "exp": {
     "$ref": "#/$defs/Timestamp"
    },
    "summary": {
     "type": "string",
     "maxLength": 280,
     "pattern": "^[^\\n\\r]*$"
    },
    "body": {
     "type": "object"
    },
    "ext": {
     "type": "object",
     "propertyNames": {
      "pattern": "^[a-z][a-z0-9-]*(\\.[a-z0-9-]+)*\\.v[0-9]+$"
     },
     "additionalProperties": {
      "type": "object"
     },
     "description": "namespaced extension fields: <name>.v<N>, where <name> is a Profile, binding or Extension name (exchange.v1, bridge.v1, aep-a2a.v1) or a reverse-domain name (com.example.booking.v1) (§4.3, §15.1)"
    },
    "evidence": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/PinnedRef"
     }
    },
    "attached": {
     "type": "array",
     "maxItems": 16,
     "items": {
      "$ref": "#/$defs/Record"
     }
    },
    "sigs": {
     "type": "array",
     "minItems": 1,
     "maxItems": 16,
     "items": {
      "$ref": "#/$defs/SigEntry"
     }
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "type": {
        "const": "agent_card"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/AgentCardBody"
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "delegation"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/DelegationBody"
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "revocation"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/RevocationBody"
       },
       "subject": {
        "anyOf": [
         {
          "$ref": "#/$defs/KeyRef"
         },
         {
          "$ref": "#/$defs/LookupRef"
         }
        ]
       }
      },
      "not": {
       "required": [
        "exp"
       ]
      },
      "required": [
       "subject"
      ]
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "payload"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/PayloadBody"
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "attestation"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/AttestationBody"
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "artifact"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/ArtifactBody"
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "type": {
        "const": "context"
       }
      }
     },
     "then": {
      "properties": {
       "body": {
        "$ref": "#/$defs/ContextBody"
       }
      }
     }
    }
   ]
  },
  "Key": {
   "type": "object",
   "required": [
    "kid",
    "purpose",
    "alg",
    "pub"
   ],
   "properties": {
    "kid": {
     "$ref": "#/$defs/Kid"
    },
    "purpose": {
     "type": "string",
     "enum": [
      "sig",
      "enc",
      "recovery"
     ],
     "description": "recovery appears only in a recovery version (§4.1)"
    },
    "alg": {
     "type": "string",
     "enum": [
      "ed25519",
      "hpke-x25519-sha256-chacha20",
      "hpke-x25519-sha256-aes128gcm",
      "hpke-xwing-sha256-chacha20"
     ]
    },
    "pub": {
     "$ref": "#/$defs/Base64url",
     "description": "raw public key bytes (32 for Ed25519 and X25519)"
    },
    "valid_from": {
     "$ref": "#/$defs/Timestamp",
     "description": "since when the key is in force; required on a device key, whose enrolling version a Directory may not serve (§11.1, §11.5); on an enc key it orders the keys (§11.4)"
    },
    "valid_until": {
     "$ref": "#/$defs/Timestamp",
     "description": "until when the key is in force; a retired device key stays listed with it for the Envelope TTL (§11.5)"
    },
    "next_key_hash": {
     "$ref": "#/$defs/Digest",
     "description": "pre-rotation commitment, SHA-256 of the raw next public key (§4.1, §11.5); on the primary signing key only; changes only in a rotation or recovery version"
    },
    "delegation": {
     "$ref": "#/$defs/PinnedRef",
     "description": "for a device key: the Agent-signed delegation that scopes it (§11.1); a key with a delegation never signs a Card version and is not part of a key event"
    }
   },
   "dependentSchemas": {
    "delegation": {
     "required": [
      "valid_from"
     ],
     "description": "a device key says since when it is in force (§11.1)"
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "purpose": {
        "const": "enc"
       }
      }
     },
     "then": {
      "properties": {
       "alg": {
        "enum": [
         "hpke-x25519-sha256-chacha20",
         "hpke-x25519-sha256-aes128gcm",
         "hpke-xwing-sha256-chacha20"
        ]
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "purpose": {
        "enum": [
         "sig",
         "recovery"
        ]
       }
      }
     },
     "then": {
      "properties": {
       "alg": {
        "const": "ed25519"
       }
      }
     }
    }
   ]
  },
  "Endpoint": {
   "type": "object",
   "required": [
    "kind",
    "url"
   ],
   "properties": {
    "kind": {
     "type": "string",
     "enum": [
      "relay",
      "direct"
     ]
    },
    "url": {
     "type": "string",
     "pattern": "^https?://",
     "description": "https; http only inside a local deployment (§7, §16.4)"
    },
    "gateway": {
     "$ref": "#/$defs/ActorId",
     "description": "the Home Gateway's Actor ID (relay)"
    },
    "spki": {
     "type": "string",
     "description": "pinned TLS public key of a direct endpoint (§10.3)"
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "kind": {
        "const": "relay"
       }
      }
     },
     "then": {
      "required": [
       "gateway"
      ]
     }
    },
    {
     "if": {
      "properties": {
       "kind": {
        "const": "direct"
       }
      }
     },
     "then": {
      "required": [
       "spki"
      ],
      "properties": {
       "url": {
        "pattern": "^https://"
       }
      }
     }
    }
   ],
   "description": "relay: the Home Gateway (gateway = its Actor ID, url = its /v1 base); direct: an HTTPS URL with its pinned TLS key (§10.3, §16.3)"
  },
  "Supported": {
   "type": "object",
   "required": [
    "v"
   ],
   "properties": {
    "v": {
     "type": "array",
     "items": {
      "type": "integer",
      "minimum": 1
     },
     "minItems": 1
    },
    "suites": {
     "type": "array",
     "items": {
      "type": "string"
     }
    },
    "content_types": {
     "type": "array",
     "items": {
      "type": "string"
     }
    },
    "profiles": {
     "type": "array",
     "items": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]*(\\.[a-z0-9-]+)*\\.v[0-9]+$"
     }
    },
    "extensions": {
     "type": "array",
     "items": {
      "type": "object",
      "required": [
       "ns",
       "schema_digest"
      ],
      "properties": {
       "ns": {
        "type": "string"
       },
       "required": {
        "type": "boolean"
       },
       "schema_digest": {
        "$ref": "#/$defs/Digest"
       }
      }
     }
    }
   }
  },
  "Policy": {
   "type": "object",
   "properties": {
    "visibility": {
     "type": "string",
     "enum": [
      "public",
      "restricted",
      "direct"
     ]
    },
    "audience": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/ActorId"
     },
     "description": "Actor IDs allowed to query a restricted Card; a Gateway is an Actor, named by its own ID (§9.2)"
    },
    "contact": {
     "type": "string",
     "description": "open, none or invite (§9.3); a Profile MAY add values (credentials.v1 adds credential, Appendix D)"
    },
    "rate_limit": {
     "$ref": "#/$defs/SafeInteger"
    }
   }
  },
  "AgentCardBody": {
   "type": "object",
   "title": "agent_card body (§7, §20.1)",
   "required": [
    "actor_type",
    "keys",
    "endpoints",
    "supported"
   ],
   "properties": {
    "actor_type": {
     "type": "string",
     "enum": [
      "person",
      "organization",
      "agent",
      "surface",
      "verifier",
      "gateway"
     ]
    },
    "owner": {
     "$ref": "#/$defs/OwnerId",
     "description": "the Owner whose published delegation names this Actor (§9.1)"
    },
    "handles": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Handle"
     },
     "description": "without the leading @ (§4.2)"
    },
    "prev": {
     "$ref": "#/$defs/Digest",
     "description": "content digest of the previous version (for a recovery version: of its key-event base): SHA-256 of the JCS of the Record without sigs (§7)"
    },
    "recovery_key_hash": {
     "$ref": "#/$defs/Digest",
     "description": "SHA-256 of the recovery key (§4.1); changes only in a recovery version"
    },
    "keys": {
     "type": "array",
     "minItems": 1,
     "items": {
      "$ref": "#/$defs/Key"
     },
     "contains": {
      "type": "object",
      "properties": {
       "purpose": {
        "const": "sig"
       }
      },
      "required": [
       "purpose"
      ],
      "not": {
       "required": [
        "delegation"
       ]
      },
      "description": "the primary signing key: purpose sig without a delegation (§11.1)"
     },
     "minContains": 1,
     "maxContains": 1,
     "description": "exactly one primary signing key, any number of device keys (purpose sig with delegation) and enc keys, a recovery key only in a recovery version"
    },
    "endpoints": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Endpoint"
     },
     "contains": {
      "properties": {
       "kind": {
        "const": "relay"
       }
      },
      "required": [
       "kind"
      ]
     },
     "minContains": 0,
     "maxContains": 1,
     "description": "at most one kind: relay entry (one Home Gateway, §16.3)"
    },
    "capabilities": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Capability"
     },
     "description": "set array keyed by name (§6)"
    },
    "supported": {
     "$ref": "#/$defs/Supported"
    },
    "policy": {
     "$ref": "#/$defs/Policy"
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "actor_type": {
        "const": "gateway"
       }
      },
      "required": [
       "actor_type"
      ]
     },
     "then": {
      "properties": {
       "endpoints": {
        "contains": {
         "properties": {
          "kind": {
           "const": "relay"
          }
         },
         "required": [
          "kind"
         ]
        },
        "minContains": 1
       }
      }
     }
    }
   ],
   "description": "next_key_hash (on the primary signing key) and recovery_key_hash are present together or absent together (§4.1). A Gateway's own Card carries the relay endpoint that names the Gateway itself and its /v1 base (§10.4)."
  },
  "DelegationBody": {
   "type": "object",
   "title": "delegation body (§7, §13)",
   "required": [
    "scope"
   ],
   "properties": {
    "scope": {
     "type": "array",
     "minItems": 1,
     "items": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*:.+$",
      "description": "verb:target (§13): Core verbs disclose and represent; a Profile registers its own (exchange.v1: command, agree)"
     }
    },
    "audience": {
     "type": "array",
     "items": {
      "type": "string"
     }
    },
    "constraints": {
     "type": "object",
     "properties": {
      "max_amount": {
       "$ref": "#/$defs/Money"
      },
      "time_window": {
       "type": "object",
       "properties": {
        "from": {
         "$ref": "#/$defs/Timestamp"
        },
        "until": {
         "$ref": "#/$defs/Timestamp"
        }
       }
      },
      "target": {
       "type": "string"
      },
      "record_digest": {
       "$ref": "#/$defs/Digest",
       "description": "the one Record this delegation covers: SHA-256 of the JCS of that Record without sigs and attached (§13); an approval carries it (exchange.v1, §14.2)"
      }
     }
    },
    "human": {
     "type": "boolean"
    },
    "parent": {
     "$ref": "#/$defs/PinnedRef"
    },
    "key_kid": {
     "$ref": "#/$defs/Kid"
    }
   }
  },
  "RevocationBody": {
   "type": "object",
   "title": "revocation body (§7, §11.5); the key or Record revoked is the Record's subject: <actor id>#<kid>, issuer/id@v (one version) or issuer/id (every version)",
   "required": [
    "effective_at"
   ],
   "properties": {
    "reason": {
     "type": "string"
    },
    "effective_at": {
     "$ref": "#/$defs/Timestamp",
     "description": "MAY be earlier than the revocation's iat (§11.5)"
    }
   }
  },
  "PayloadBody": {
   "type": "object",
   "title": "payload body (§7)",
   "required": [
    "content_type"
   ],
   "properties": {
    "content_type": {
     "type": "string",
     "pattern": "^[a-z0-9!#$&^_.+-]+/[a-z0-9!#$&^_.+-]+$"
    },
    "data": {},
    "data_bytes": {
     "$ref": "#/$defs/Base64url"
    },
    "data_ref": {
     "$ref": "#/$defs/PinnedRef"
    },
    "content_digest": {
     "$ref": "#/$defs/Digest",
     "description": "optional; SHA-256 of data_bytes, of the artifact's bytes for data_ref, or of JCS(data) for data (§7); a sender SHOULD include it for data_bytes and data_ref"
    },
    "foreign": {
     "type": "object",
     "required": [
      "protocol"
     ],
     "properties": {
      "protocol": {
       "type": "string"
      },
      "version": {
       "type": "string"
      },
      "context_id": {
       "type": "string"
      },
      "task_id": {
       "type": "string"
      },
      "message_id": {
       "type": "string"
      },
      "request_id": {
       "type": "string"
      }
     }
    }
   },
   "oneOf": [
    {
     "required": [
      "data"
     ]
    },
    {
     "required": [
      "data_bytes"
     ]
    },
    {
     "required": [
      "data_ref"
     ]
    }
   ]
  },
  "Message": {
   "type": "object",
   "title": "application/aep.message+json (§7)",
   "required": [
    "text"
   ],
   "properties": {
    "text": {
     "type": "string"
    },
    "lang": {
     "type": "string"
    },
    "attachments": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/PinnedRef"
     }
    }
   }
  },
  "AttestationBody": {
   "type": "object",
   "title": "attestation body (§7)",
   "required": [
    "kind",
    "at"
   ],
   "properties": {
    "kind": {
     "type": "string",
     "description": "Core: delivered, rejected, read, processed, verified; federation.v1 adds routable (§16.3); bindings and Profiles add their own (a2a.task, executed, settled …)"
    },
    "at": {
     "$ref": "#/$defs/Timestamp"
    },
    "claims": {
     "type": "object"
    },
    "error": {
     "$ref": "#/$defs/Error"
    }
   },
   "allOf": [
    {
     "if": {
      "properties": {
       "kind": {
        "const": "delivered"
       }
      }
     },
     "then": {
      "properties": {
       "claims": {
        "type": "object",
        "required": [
         "mailbox",
         "envelopes",
         "cursor_from",
         "cursor_to",
         "accepted_at"
        ],
        "properties": {
         "mailbox": {
          "$ref": "#/$defs/ActorId"
         },
         "ctx": {
          "$ref": "#/$defs/PinnedRef"
         },
         "member": {
          "$ref": "#/$defs/ActorId"
         },
         "envelopes": {
          "type": "array",
          "minItems": 1,
          "items": {
           "type": "object",
           "required": [
            "id",
            "digest"
           ],
           "properties": {
            "id": {
             "$ref": "#/$defs/Id"
            },
            "digest": {
             "$ref": "#/$defs/Digest",
             "description": "SHA-256 of the JCS of the whole Envelope, sig included"
            }
           }
          }
         },
         "cursor_from": {
          "type": "string"
         },
         "cursor_to": {
          "type": "string"
         },
         "accepted_at": {
          "$ref": "#/$defs/Timestamp"
         }
        }
       }
      }
     }
    },
    {
     "if": {
      "properties": {
       "kind": {
        "const": "rejected"
       }
      }
     },
     "then": {
      "properties": {
       "claims": {
        "type": "object",
        "properties": {
         "envelopes": {
          "type": "array",
          "items": {
           "type": "object",
           "required": [
            "id",
            "digest"
           ],
           "properties": {
            "id": {
             "$ref": "#/$defs/Id"
            },
            "digest": {
             "$ref": "#/$defs/Digest",
             "description": "SHA-256 of the JCS of the whole Envelope, sig included"
            }
           }
          }
         }
        }
       }
      },
      "description": "a transport rejected (issued by a Gateway, §12.1) MUST carry error; a business rejected (a party declining, §14.1, A.8) carries the reason in the Record's summary and MAY omit error"
     }
    }
   ]
  },
  "ArtifactBody": {
   "type": "object",
   "title": "artifact body (§7)",
   "required": [
    "uri",
    "digest",
    "size",
    "content_type",
    "keys"
   ],
   "properties": {
    "uri": {
     "type": "string",
     "pattern": "^https://"
    },
    "digest": {
     "$ref": "#/$defs/Digest"
    },
    "size": {
     "$ref": "#/$defs/SafeInteger"
    },
    "content_type": {
     "type": "string"
    },
    "exp": {
     "$ref": "#/$defs/Timestamp"
    },
    "keys": {
     "type": "array",
     "minItems": 1,
     "items": {
      "$ref": "#/$defs/Wrap"
     }
    }
   }
  },
  "ContextBody": {
   "type": "object",
   "title": "context body (reserved in Core; defined by groups.v1, §10.5)",
   "required": [
    "kind",
    "controllers",
    "members",
    "acl",
    "join",
    "epoch"
   ],
   "properties": {
    "kind": {
     "type": "string"
    },
    "controllers": {
     "type": "array",
     "minItems": 1,
     "items": {
      "$ref": "#/$defs/ActorId"
     }
    },
    "members": {
     "type": "array",
     "items": {
      "type": "object",
      "required": [
       "id",
       "role",
       "since_epoch"
      ],
      "properties": {
       "id": {
        "$ref": "#/$defs/ActorId"
       },
       "role": {
        "enum": [
         "member",
         "controller"
        ]
       },
       "since_epoch": {
        "type": "integer",
        "minimum": 1
       }
      }
     }
    },
    "acl": {
     "type": "object",
     "required": [
      "read",
      "write",
      "invite"
     ],
     "properties": {
      "read": {
       "enum": [
        "members",
        "public"
       ]
      },
      "write": {
       "enum": [
        "members",
        "controller"
       ]
      },
      "invite": {
       "enum": [
        "controller",
        "members"
       ]
      }
     }
    },
    "join": {
     "type": "string",
     "enum": [
      "open",
      "approval",
      "invite"
     ]
    },
    "epoch": {
     "type": "integer",
     "minimum": 1
    },
    "epoch_keys": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Wrap"
     }
    },
    "key_package_ref": {
     "$ref": "#/$defs/PinnedRef",
     "description": "an artifact Record holding the epoch-key wraps of a room whose context does not carry them inline; the router carries the reference and does not follow it (§10.5)"
    },
    "predecessor": {
     "$ref": "#/$defs/PinnedRef",
     "description": "the context this one succeeds; succession is a new Record by the successor (§10.5)"
    }
   }
  },
  "Error": {
   "type": "object",
   "title": "Error (§8, RFC 9457 Problem Details)",
   "required": [
    "type",
    "code",
    "title",
    "retryable"
   ],
   "properties": {
    "type": {
     "type": "string",
     "pattern": "^urn:aep:error:[a-z_]+$"
    },
    "code": {
     "type": "string",
     "pattern": "^[a-z][a-z0-9_]{1,63}$",
     "description": "Core: invalid_request, unauthorized, forbidden, key_revoked, not_found, conflict, expired, payload_too_large, unsupported, rate_limited, unavailable (§8); exchange.v1 registers approval_required, resource_stale, execution_failed (§14.1); other Profiles MAY register more, and a receiver treats a code it does not know by retryable (§8)"
    },
    "title": {
     "type": "string"
    },
    "detail": {
     "type": "string"
    },
    "instance": {
     "type": "string"
    },
    "retryable": {
     "type": "boolean"
    },
    "retry_after": {
     "type": "integer",
     "minimum": 0,
     "description": "seconds before the same Envelope or Record may be resent (§8); on a synchronous answer it is also the Retry-After header"
    },
    "provider_code": {
     "type": "string"
    },
    "provider_ref": {
     "type": "string"
    },
    "details_ref": {
     "$ref": "#/$defs/PinnedRef"
    }
   }
  },
  "SignedRequestBase": {
   "type": "object",
   "description": "Signed request object (§10.4): rid, iat, exp and sig over the domain-tagged input",
   "required": [
    "actor",
    "kid",
    "rid",
    "iat",
    "exp",
    "sig"
   ],
   "properties": {
    "actor": {
     "$ref": "#/$defs/ActorId"
    },
    "kid": {
     "$ref": "#/$defs/Kid"
    },
    "rid": {
     "$ref": "#/$defs/Id"
    },
    "iat": {
     "$ref": "#/$defs/Timestamp"
    },
    "exp": {
     "$ref": "#/$defs/Timestamp"
    },
    "sig": {
     "$ref": "#/$defs/EnvelopeSig"
    }
   }
  },
  "SyncRequest": {
   "allOf": [
    {
     "$ref": "#/$defs/SignedRequestBase"
    },
    {
     "type": "object",
     "required": [
      "cursor"
     ],
     "properties": {
      "cursor": {
       "type": "string",
       "description": "opaque, monotonic; \"0\" on a device's first sync means from the oldest Envelope still retained (§10.1)"
      },
      "limit": {
       "type": "integer",
       "minimum": 1,
       "maximum": 100,
       "description": "what a client asks for, 20 when absent; a Gateway clamps a value outside the range instead of refusing it (§10.1)"
      },
      "wait": {
       "type": "integer",
       "minimum": 0,
       "description": "seconds to block for new Envelopes; the Gateway caps it (default cap 30)"
      }
     }
    }
   ]
  },
  "SyncResponse": {
   "type": "object",
   "required": [
    "envelopes",
    "next_cursor"
   ],
   "properties": {
    "envelopes": {
     "type": "array",
     "items": {
      "type": "object",
      "required": [
       "envelope",
       "accepted_at",
       "cursor"
      ],
      "properties": {
       "envelope": {
        "$ref": "#/$defs/Envelope"
       },
       "accepted_at": {
        "$ref": "#/$defs/Timestamp"
       },
       "cursor": {
        "type": "string",
        "description": "this entry's cursor; the client acks the last one it has stored (§10.1)"
       }
      }
     }
    },
    "next_cursor": {
     "type": "string"
    },
    "has_more": {
     "type": "boolean",
     "description": "a room's sync (§10.4): the Gateway cut the page at its own size"
    },
    "tail": {
     "type": "string",
     "description": "a room's sync (§10.4): the cursor of the newest Envelope the room's log has taken, whether still served or expired since"
    }
   }
  },
  "AckRequest": {
   "allOf": [
    {
     "$ref": "#/$defs/SignedRequestBase"
    },
    {
     "type": "object",
     "required": [
      "cursor"
     ],
     "properties": {
      "cursor": {
       "type": "string"
      }
     }
    }
   ]
  },
  "RoomRemoveRequest": {
   "allOf": [
    {
     "$ref": "#/$defs/SignedRequestBase"
    },
    {
     "type": "object",
     "required": [
      "from",
      "id"
     ],
     "properties": {
      "from": {
       "$ref": "#/$defs/ActorId",
       "description": "the sender of the Envelope to take out of the room log"
      },
      "id": {
       "$ref": "#/$defs/Id",
       "description": "the id of that Envelope"
      }
     }
    }
   ],
   "description": "POST /v1/rooms/{ctx}/remove (§10.4; Profiles §10.5): the room's controller takes the Envelope (from, id) out of the room log; the room rejects it with forbidden if it is sent again before its exp"
  },
  "PolicyRequest": {
   "allOf": [
    {
     "$ref": "#/$defs/SignedRequestBase"
    },
    {
     "type": "object",
     "required": [
      "base_version",
      "allow",
      "block"
     ],
     "properties": {
      "base_version": {
       "type": "integer",
       "minimum": 0
      },
      "allow": {
       "type": "array",
       "items": {
        "$ref": "#/$defs/ActorId"
       }
      },
      "block": {
       "type": "array",
       "items": {
        "$ref": "#/$defs/ActorId"
       }
      }
     }
    }
   ]
  },
  "QueryRequest": {
   "type": "object",
   "required": [
    "filter"
   ],
   "properties": {
    "filter": {
     "type": "object",
     "description": "Core keys below; a Profile MAY register more (exchange.v1: region); an Index rejects keys it does not know with unsupported (§9.3)",
     "properties": {
      "type": {
       "type": "string"
      },
      "actor_type": {
       "type": "string"
      },
      "capability": {
       "type": "string",
       "description": "a capabilities[].name, exact, or a namespace prefix ending in a dot"
      },
      "profile": {
       "type": "string"
      },
      "issuer": {
       "$ref": "#/$defs/ActorId"
      },
      "text": {
       "type": "string"
      }
     }
    },
    "cursor": {
     "type": "string"
    },
    "limit": {
     "type": "integer",
     "minimum": 1,
     "maximum": 100,
     "description": "what a client asks for, 20 when absent; an Index clamps a larger value instead of refusing it (§9.3)"
    }
   }
  },
  "SendRequest": {
   "oneOf": [
    {
     "$ref": "#/$defs/Envelope"
    },
    {
     "type": "array",
     "minItems": 1,
     "maxItems": 100,
     "items": {
      "$ref": "#/$defs/Envelope"
     }
    },
    {
     "type": "object",
     "required": [
      "envelopes"
     ],
     "properties": {
      "envelopes": {
       "type": "array",
       "minItems": 1,
       "maxItems": 100,
       "items": {
        "$ref": "#/$defs/Envelope"
       }
      },
      "cards": {
       "type": "array",
       "items": {
        "$ref": "#/$defs/Record"
       }
      }
     }
    }
   ]
  },
  "SendResponse": {
   "type": "object",
   "required": [
    "accepted",
    "queued",
    "rejected"
   ],
   "properties": {
    "accepted": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Id"
     }
    },
    "queued": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Id"
     }
    },
    "rejected": {
     "type": "array",
     "items": {
      "type": "object",
      "required": [
       "id",
       "error"
      ],
      "properties": {
       "id": {
        "type": [
         "string",
         "null"
        ]
       },
       "error": {
        "$ref": "#/$defs/Error"
       }
      }
     }
    }
   }
  },
  "ResolveResponse": {
   "type": "object",
   "required": [
    "card",
    "key_events",
    "delegations",
    "revocations"
   ],
   "properties": {
    "card": {
     "$ref": "#/$defs/Record"
    },
    "key_events": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Record"
     },
     "description": "v1, the version that added the commitments, and every rotation and recovery before the current version (§11.5, Appendix F.4)"
    },
    "delegations": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Record"
     }
    },
    "revocations": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/Record"
     }
    },
    "conflict": {
     "type": "object",
     "required": [
      "v"
     ],
     "properties": {
      "v": {
       "type": "integer"
      },
      "versions": {
       "type": "array",
       "minItems": 2,
       "maxItems": 2,
       "items": {
        "$ref": "#/$defs/Record"
       },
       "description": "both versions of the fork; a Directory keeps and serves both (§4.1)"
      }
     },
     "description": "present when the Directory holds a fork at v; card is then the last unconflicted version, or at a fork at v1 the version seen first (§4.1). v is the v of both versions, or, for two recovery versions signed by one recovery key, the lower of their two v (their bases may differ)"
    },
    "scopes": {
     "type": "array",
     "items": {
      "$ref": "#/$defs/HandleScope"
     },
     "description": "on GET /.well-known/aep/gateway only: the handle scopes this Gateway issues (§4.2, §9.4); what it implements is supported.profiles of its Card"
    },
    "registration": {
     "type": "string",
     "enum": [
      "open",
      "gated"
     ],
     "description": "on GET /.well-known/aep/gateway only (§9.4)"
    }
   }
  },
  "Handle": {
   "type": "string",
   "maxLength": 253,
   "pattern": "^(?!xn--)(?:(?=[a-z0-9-]*[a-z])[a-z0-9][a-z0-9-]{1,61}[a-z0-9]|[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\\.(?!xn--)[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+)\\.agent$",
   "description": "a handle without its leading @: lowercase labels, the last one `agent`; in scope `agent` a name of at least three characters with at least one letter, and no label that begins `xn--`; never a DNS name (§4.2)"
  },
  "HandleScope": {
   "type": "string",
   "maxLength": 253,
   "pattern": "^((?!xn--)[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\\.)*agent$",
   "description": "what follows the first label of a handle: `agent` itself, or labels ending in `.agent`, none of them beginning `xn--` (§4.2)"
  },
  "KeyRef": {
   "type": "string",
   "description": "<actor id>#<kid> names one key of one Actor (revocation subject, §7)",
   "pattern": "^(agent|owner):z[1-9A-HJ-NP-Za-km-z]{20,80}#[^#/\\s]{1,64}$"
  },
  "Capability": {
   "type": "object",
   "description": "What the Actor offers, for discovery (§7, §9.3); how it is invoked is a Profile's or a carried protocol's business",
   "required": [
    "name"
   ],
   "properties": {
    "name": {
     "type": "string",
     "pattern": "^[a-z][a-z0-9-]*(\\.[a-z0-9_-]+)+$",
     "description": "reverse-domain namespaced, e.g. com.example.translate"
    },
    "description": {
     "type": "string",
     "maxLength": 280
    },
    "tags": {
     "type": "array",
     "items": {
      "type": "string",
      "pattern": "^[a-z0-9_-]{1,32}$"
     },
     "maxItems": 16
    }
   }
  },
  "SignedQueryRequest": {
   "description": "A query to a restricted Index, or to a public one that rate-limits (§9.3, §10.4)",
   "allOf": [
    {
     "$ref": "#/$defs/SignedRequestBase"
    },
    {
     "$ref": "#/$defs/QueryRequest"
    }
   ]
  },
  "QueryResponse": {
   "type": "object",
   "required": [
    "items"
   ],
   "properties": {
    "items": {
     "type": "array",
     "items": {
      "type": "object",
      "required": [
       "record"
      ],
      "properties": {
       "record": {
        "$ref": "#/$defs/Record",
        "description": "the full signed Record (a Card or a Profile Record); the client verifies it"
       },
       "score": {
        "$ref": "#/$defs/SafeInteger",
        "description": "Index-defined, not comparable across Indexes"
       }
      }
     }
    },
    "next_cursor": {
     "type": "string"
    }
   }
  },
  "GatewayList": {
   "description": "The signed list at /.well-known/aep/gateways (§9.4): a payload Record with content_type application/aep.gateways+json",
   "allOf": [
    {
     "$ref": "#/$defs/Record"
    },
    {
     "type": "object",
     "properties": {
      "type": {
       "const": "payload"
      },
      "body": {
       "type": "object",
       "required": [
        "content_type",
        "data"
       ],
       "properties": {
        "content_type": {
         "const": "application/aep.gateways+json"
        },
        "data": {
         "type": "object",
         "required": [
          "gateways"
         ],
         "properties": {
          "gateways": {
           "type": "array",
           "items": {
            "type": "object",
            "required": [
             "id",
             "url",
             "registration"
            ],
            "properties": {
             "id": {
              "$ref": "#/$defs/ActorId"
             },
             "url": {
              "type": "string",
              "pattern": "^https?://",
              "description": "the Gateway's /v1 base URL; https, or http only inside a local deployment (§16.4)"
             },
             "registration": {
              "type": "string",
              "enum": [
               "open",
               "gated"
              ]
             },
             "profiles": {
              "type": "array",
              "items": {
               "type": "string",
               "pattern": "^[a-z][a-z0-9-]*(\\.[a-z0-9-]+)*\\.v[0-9]+$"
              },
              "description": "the Profiles the Gateway implements, as in supported.profiles of its Card (§1.2)"
             },
             "scopes": {
              "type": "array",
              "items": {
               "$ref": "#/$defs/HandleScope"
              },
              "description": "the handle scopes the Gateway issues; a scope appears under one Gateway of a list (§4.2)"
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }
   ]
  },
  "OwnerId": {
   "type": "string",
   "pattern": "^owner:z[1-9A-HJ-NP-Za-km-z]{20,80}$",
   "description": "an Owner's Actor ID (actor_type person or organization, §4.1)"
  }
 }
}
