## ARKHEIA Clearance Wire Specification (v1.0.1)

policy_version = "arkheia.mandate-aegis/1.0.1" — identical inputs + identical policy version must yield byte-identical outputs.

### 1. Endpoints
| Method | Path | Purpose |
| --- | --- | --- |
| POST | /api/mandate/verify | Verify a signed MANDATE (CUSTOS lease + A2A delegation chain) → permit or opaque rejection |
| POST | /api/aegis/evaluate | Process a signed OBSERVATION or HUMAN_RELEASE artifact |
| GET | /api/clearance/head | Public ledger head snapshot |

Raw request bytes are passed through FastAPI untouched (no Pydantic coercion); headers are lower-cased. All responses are `Content-Type: application/json`, `Cache-Control: no-store`, bodies serialized with JCS. A fixed latency floor (default 25 ms, `ARKHEIA_CLEARANCE_FLOOR_MS`) is applied to every POST. Max request body: 65,536 bytes (`OVERSIZE` beyond that).

### 2. Primitive wire types
| Type | Definition |
| --- | --- |
| u64 (wire) | JSON integer, `0 ≤ v ≤ 2^53 − 1` (`WIRE_INT_MAX`). Exact type check — bool rejected. |
| u128 | Canonical decimal string: regex `^(?:0|[1-9][0-9]{0,38})$` within `[0, 2^128 − 1]` |
| hex16 | Exactly 32 lowercase hex chars (`[0-9a-f]{32}`) — 16 bytes |
| hex32 / pub | Exactly 64 lowercase hex chars — 32 bytes (SHA-256 digests, raw Ed25519 public keys) |
| hex64 / sig | Exactly 128 lowercase hex chars — 64-byte Ed25519 signature |
| ident | `[A-Za-z0-9][A-Za-z0-9:_.\-]{0,127}` |
| scope | `[a-z][a-z0-9_.\-]{0,127}` |
| scope list | Non-empty JSON array of `scope`, strictly ascending (byte order) — hence unique and canonical; max 64 items |

Floats are forbidden everywhere (AX-4). No optional fields exist anywhere.

### 3. Strict JSON parsing (Zone U → Zone I)
Request bodies must parse under the strict reader; any violation → `STRUCT` rejection:
- Rejected: duplicate object keys, floats / non-integer numbers, `NaN` / `Infinity`, integers outside `±(2^53 − 1)`, UTF-8 BOM, invalid UTF-8, lone surrogates, nesting depth > 64.
- Every object schema is closed: key set must equal the schema's key set exactly. Unknown fields are rejected, never ignored.

### 4. Canonical serialization (RFC 8785 JCS, integers-only profile)
- UTF-8, no whitespace: `{"k":v,...}`, `[v,...]`.
- Object keys sorted by UTF-16BE code-unit order (RFC 8785 §3.2.3).
- Integers serialized as plain decimal; `|v| > 2^53 − 1` or any float raises.
- String escaping: `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t`; other control chars `< 0x20` as `\u00xx`; all other characters literal; lone surrogates raise.
- Max depth 64; non-string keys raise.

### 5. Signatures
- Algorithm: Ed25519 (RFC 8032), 32-byte raw public key hex-encoded, 64-byte signature hex-encoded, deterministic signing from a 32-byte seed.
- Signing preimage rule (uniform for every artifact):
  `signing_input = DOMAIN_TAG || 0x00 || JCS(object with the signature-carrying field removed)`

| Artifact | Domain tag (ASCII) | Field omitted |
| --- | --- | --- |
| CUSTOS lease | `ARKHEIA/custos/lease/v1` | `sig` |
| A2A delegation hop | `ARKHEIA/a2a/delegation/v1` | `sig` |
| Mandate request | `ARKHEIA/mandate/request/v1` | `sig` |
| Aegis observation | `ARKHEIA/aegis/observation/v1` | `sig` |
| Human release | `ARKHEIA/aegis/human-release/v1` | `approvals` (each approval signs the same preimage) |
| Clearance receipt | `ARKHEIA/clearance/receipt/v1` | `signature` |
| Execution permit | `ARKHEIA/mandate/permit/v1` | `signature` |
| Reason HMAC | `ARKHEIA/clearance/reason/v1` | n/a (HMAC domain) |

- Verification is two-outcome: malformed key or bad signature → `False` (no exceptions leak). Verification work is padded to a fixed count per endpoint (mandate: `2 + max_delegation_depth = 6` slots; aegis: `max_approvals = 5` slots) using a dummy keypair, for constant-time behavior.
- Key-id convention: `public_key_id = "ed25519:" + SHA256(raw_32_byte_public_key).hex()[:16]`.

### 6. Trusted issuer checks
Trust roots come from `ARKHEIA_CLEARANCE_TRUST_FILE` — a closed object:
`{"custos_issuers": ["<hex32>..."], "sensors": ["<hex32>..."], "release_authorities": ["<hex32>..."]}`
- Each member must be exactly 64 lowercase hex (raw Ed25519 public key).
- The engine's own signing key must not appear in any of the three sets (startup refusal).
- Evaluation checks:
  - `lease.issuer ∈ custos_issuers` else `LEASE_ISSUER_UNKNOWN`.
  - `observation.sensor ∈ sensors` else `SENSOR_UNKNOWN`.
  - Each `approval.authority ∈ release_authorities`; a validly signed approval from a non-authority is `OVERRIDE_ATTEMPT` (hard breach → quarantine of that key); an invalidly signed unknown is `AUTHORITY_UNKNOWN`. Duplicates → `AUTHORITY_DUPLICATE`. Valid distinct approvals must meet `release_quorum` (default 1).

### 7. Request schemas (all keys required, closed)

#### 7.1 CUSTOS lease — signed by `issuer`, domain `ARKHEIA/custos/lease/v1`
| Field | Type | Notes |
| --- | --- | --- |
| `lease_id` | hex16 | |
| `issuer` | hex32 pub | must be a trusted CUSTOS issuer; signs the lease |
| `holder` | hex32 pub | first grantee |
| `swarm_id` | ident | |
| `scopes` | scope list | strictly ascending, 1–64 items |
| `max_amount` | u128 string | |
| `lamport_issued` | u64 | |
| `ttl` | u64 | `1 ≤ ttl ≤ 100000`; `expiry = lamport_issued + ttl` |
| `sig` | hex64 | Ed25519 over `"ARKHEIA/custos/lease/v1" ‖ 0x00 ‖ JCS(lease − sig)` |

#### 7.2 A2A delegation hop — signed by `delegator`, domain `ARKHEIA/a2a/delegation/v1`
Same as lease with `delegator`/`delegate` replacing `issuer`/`holder`, no `swarm_id`:
`{"delegator": hex32, "delegate": hex32, "lease_id": hex16, "scopes": [scope..], "max_amount": u128, "lamport_issued": u64, "ttl": u64, "sig": hex64}`

#### 7.3 Mandate request (`POST /api/mandate/verify`) — signed by `actor`, domain `ARKHEIA/mandate/request/v1`
| Field | Type | Notes |
| --- | --- | --- |
| `type` | literal `"MANDATE"` | |
| `mandate_id` | hex16 | single-use (replay → `MANDATE_REPLAY`, hard) |
| `lamport` | u64 | must satisfy `lamport ≤ engine_clock + 1000` (skew) and be `>` last cleared lamport for the actor |
| `tool_call` | object | closed: `{"mcp_method": "tools/call", "mcp_name": scope}` |
| `pool` | u128 string | must be `≤` effective grant's `max_amount` |
| `allocations` | array 1–64 | items closed `{"recipient": ident, "bps": u64}`; `Σbps` must equal `10000` and conserve the pool exactly (integer bps allocation), else `CONSERVATION_BREACH` |
| `lease` | CUSTOS lease | §7.1 |
| `delegations` | array 0–4 of hops | §7.2 |
| `actor` | hex32 pub | must equal the last grantee in the chain; signs the mandate |
| `sig` | hex64 | over `"ARKHEIA/mandate/request/v1" ‖ 0x00 ‖ JCS(mandate − sig)` |

Optional MCP transport headers (must match body when present): `mcp-method = "tools/call"`, `mcp-name = tool_call.mcp_name`, `mcp-protocol-version = "2026-07-28"`; mismatch → `MCP_HEADER_MISMATCH`.
Chain rules (`chain = lease, hop₁, …, hopₙ`): each `hop.delegator == parent.grantee` and `hop.lease_id == lease.lease_id` (`CHAIN_LINK`); grantees all distinct (`CHAIN_CYCLE`); each child's `scopes ⊆ parent's`, `max_amount ≤ parent's`, `lamport_issued ≥ parent's`, `expiry ≤ parent's` (`SCOPE_ESCALATION` / `BUDGET_ESCALATION` / `TTL_ESCALATION` — hard, attributed to the grantor); every grant must satisfy `lamport_issued ≤ mandate.lamport < expiry` and be unexpired at issuance (`LEASE_EXPIRED`); `tool_call.mcp_name ∈ effective scopes` (`ACTION_OUT_OF_SCOPE`).

#### 7.4 Aegis observation (`POST /api/aegis/evaluate`) — signed by `sensor`, domain `ARKHEIA/aegis/observation/v1`
```json
{
  "type": "OBSERVATION",
  "subject": "<hex32>",
  "lamport": 0,
  "sensor": "<hex32>",
  "sig": "<hex64>",
  "window": {
    "request_count": 0,
    "reject_count": 0,
    "fanout": 0,
    "scope_violations": 0,
    "signature_failures": 0,
    "override_attempts": 0
  }
}
