{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rootz.global/schemas/ask-orders/origin-receipt.schema.json",
  "title": "ASK Orders origin receipt — the portable bundle a recipient verifies: who authorised it, what was asked, what it was made from, and the process it ran under",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "kind",
    "subject",
    "inclusion",
    "checkpoint",
    "orders",
    "manifest",
    "authority",
    "genesis",
    "materials",
    "inputs",
    "keys"
  ],
  "properties": {
    "kind": {
      "const": "ask-orders.origin-receipt.v1"
    },
    "subject": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "request",
        "response"
      ],
      "properties": {
        "request": {
          "oneOf": [
            {
              "$ref": "leaf.schema.json"
            },
            {
              "$ref": "#/$defs/Withheld"
            }
          ]
        },
        "response": {
          "$ref": "leaf.schema.json"
        }
      }
    },
    "inclusion": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "request",
        "response"
      ],
      "properties": {
        "request": {
          "$ref": "#/$defs/InclusionProof"
        },
        "response": {
          "$ref": "#/$defs/InclusionProof"
        }
      }
    },
    "checkpoint": {
      "$ref": "checkpoint.schema.json"
    },
    "orders": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "order.schema.json"
      }
    },
    "manifest": {
      "oneOf": [
        {
          "$ref": "controls-manifest.schema.json"
        },
        {
          "$ref": "#/$defs/Withheld"
        }
      ]
    },
    "authority": {
      "$ref": "#/$defs/AuthorityEvidence"
    },
    "materials": {
      "description": "What was fetched before the response — not what the answer entailed.",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "leaf",
          "inclusion"
        ],
        "properties": {
          "leaf": {
            "oneOf": [
              {
                "$ref": "leaf.schema.json"
              },
              {
                "$ref": "#/$defs/Withheld"
              }
            ]
          },
          "inclusion": {
            "$ref": "#/$defs/InclusionProof"
          },
          "ingestion": {
            "description": "Evidence of how much of this material the model actually READ. Optional: receipts issued before this check existed stay valid. head_sentinel is a marker placed at the START of the material (the part a sliding window drops first) that the model is required to echo back; it is direct evidence, not an estimate.",
            "type": "object",
            "additionalProperties": false,
            "required": [
              "submitted_tokens_est",
              "ingested_tokens",
              "head_sentinel"
            ],
            "properties": {
              "submitted_tokens_est": { "type": "integer", "minimum": 0 },
              "ingested_tokens": { "type": "integer", "minimum": 0 },
              "head_sentinel": { "enum": ["present", "dropped", "unreported"] }
            }
          }
        }
      }
    },
    "checks": {
      "description": "The checks this receipt was issued to satisfy, by verifier check id (e.g. \"materials.ingested\"). ABSENT means the receipt predates check profiles: a verifier then reports a newer check as not-in-profile (age), never as a gap the issuer left. PRESENT and naming a check whose evidence is missing IS a gap. Declaring less is the weaker claim, so this can only cost an issuer, never buy them anything.",
      "type": "array",
      "items": { "type": "string" }
    },
    "keys": {
      "type": "object",
      "description": "JWKS of every public key needed to verify the bundle."
    },
    "genesis": {
      "description": "The session's genesis leaf and its inclusion proof: what was installed and the effective policy hash at install (ORD-015).",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "leaf",
        "inclusion"
      ],
      "properties": {
        "leaf": {
          "$ref": "leaf.schema.json"
        },
        "inclusion": {
          "$ref": "#/$defs/InclusionProof"
        }
      }
    },
    "inputs": {
      "description": "Origin receipts the subject request declared as inputs (keep the chain alive, ORD-015). A withheld input is null.",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "digest",
          "receipt"
        ],
        "properties": {
          "digest": {
            "$ref": "common.schema.json#/$defs/Digest"
          },
          "receipt": {
            "oneOf": [
              {
                "$ref": "#"
              },
              {
                "type": "null"
              }
            ]
          }
        }
      }
    },
    "source_tls": {
      "description": "ORD-034 (optional): the source's TLS observation(s) for the request's host, from the session log, each with its inclusion proof. Withheld with the request when a receipt is redacted.",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "leaf",
          "inclusion"
        ],
        "properties": {
          "leaf": {
            "oneOf": [
              {
                "$ref": "leaf.schema.json"
              },
              {
                "$ref": "#/$defs/Withheld"
              }
            ]
          },
          "inclusion": {
            "$ref": "#/$defs/InclusionProof"
          }
        }
      }
    },
    "checkpoint_key_binding": {
      "description": "ORD-020 (optional): TPM binding evidence for the checkpoint key; its digest is checkpoint.body.signer.evidence. null or absent when the key has no hardware binding.",
      "oneOf": [
        {
          "$ref": "#/$defs/TpmBindingEvidence"
        },
        {
          "type": "null"
        }
      ]
    },
    "signer_evidence": {
      "description": "ORD-035 item 7 (optional): signer-reach and signer-log leaves with inclusion proofs; owner-attested only",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "reach",
        "log"
      ],
      "properties": {
        "reach": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "leaf",
            "inclusion"
          ],
          "properties": {
            "leaf": {
              "$ref": "leaf.schema.json"
            },
            "inclusion": {
              "$ref": "#/$defs/InclusionProof"
            }
          }
        },
        "log": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "leaf",
            "inclusion"
          ],
          "properties": {
            "leaf": {
              "$ref": "leaf.schema.json"
            },
            "inclusion": {
              "$ref": "#/$defs/InclusionProof"
            }
          }
        }
      }
    },
    "run_facts": {
      "description": "F7 (optional): run-facts leaf with inclusion proof; stated limits, never an upgrade",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "leaf",
        "inclusion"
      ],
      "properties": {
        "leaf": {
          "$ref": "leaf.schema.json"
        },
        "inclusion": {
          "$ref": "#/$defs/InclusionProof"
        }
      }
    },
    "tpm_enrollment": {
      "description": "ORD-038 (optional): the TPM enrollment record named by checkpoint_key_binding.tpm.endorsement_certificate, inline",
      "type": "object",
      "required": [
        "body",
        "proofs"
      ],
      "properties": {
        "body": {
          "type": "object",
          "required": [
            "kind"
          ],
          "properties": {
            "kind": {
              "const": "rootz.tpm-enrollment.v1"
            }
          }
        },
        "proofs": {
          "type": "array",
          "minItems": 1
        }
      }
    },
    "egress_probe": {
      "description": "ORD-040: the egress-probe leaf with inclusion proof",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "leaf",
        "inclusion"
      ],
      "properties": {
        "leaf": {
          "$ref": "leaf.schema.json"
        },
        "inclusion": {
          "$ref": "#/$defs/InclusionProof"
        }
      }
    },
    "agent_result": {
      "description": "ORD-040: the agent-result leaf with inclusion proof",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "leaf",
        "inclusion"
      ],
      "properties": {
        "leaf": {
          "$ref": "leaf.schema.json"
        },
        "inclusion": {
          "$ref": "#/$defs/InclusionProof"
        }
      }
    },
    "gateway_exchanges": {
      "description": "ORD-041: every gateway log line, in order, each with its inclusion proof",
      "type": "array",
      "maxItems": 10000,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "leaf",
          "inclusion"
        ],
        "properties": {
          "leaf": {
            "$ref": "leaf.schema.json"
          },
          "inclusion": {
            "$ref": "#/$defs/InclusionProof"
          }
        }
      }
    },
    "policy_document": {
      "description": "ORD-041: the approved canonical policy document (base64); its digest must equal order.policy.digest",
      "type": "string",
      "maxLength": 1048576,
      "pattern": "^[A-Za-z0-9+/]*={0,2}$"
    },
    "grant": {
      "description": "ORD-037: the officer-signed session grant (its body digest is the grant id) and its proof",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "body",
        "proof"
      ],
      "properties": {
        "body": {
          "type": "object"
        },
        "proof": {
          "type": "object"
        }
      }
    },
    "request_authorizations": {
      "description": "S8 8e (7c): officer-signed per-request step-up authorizations; each body digest is the id a gateway line names in authz.step_up",
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "body",
          "proof"
        ],
        "properties": {
          "body": {
            "type": "object"
          },
          "proof": {
            "type": "object"
          }
        }
      }
    }
  },
  "$defs": {
    "InclusionProof": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "leaf_index",
        "tree_size",
        "path"
      ],
      "properties": {
        "leaf_index": {
          "type": "integer",
          "minimum": 0
        },
        "tree_size": {
          "type": "integer",
          "minimum": 1
        },
        "path": {
          "type": "array",
          "items": {
            "$ref": "common.schema.json#/$defs/Digest"
          }
        }
      }
    },
    "AuthorityEvidence": {
      "description": "Snapshot of the corporate chain at order time. Offline, chain facts are 'asserted'; with an RPC they become 'verified'.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "root",
        "hops",
        "legal_anchor"
      ],
      "properties": {
        "root": {
          "$ref": "common.schema.json#/$defs/Caip10"
        },
        "legal_anchor": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "jurisdiction",
            "registry_ids"
          ],
          "properties": {
            "jurisdiction": {
              "type": "string",
              "maxLength": 20
            },
            "registry_ids": {
              "type": "array",
              "items": {
                "type": "string",
                "maxLength": 200
              }
            }
          }
        },
        "hops": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "from",
              "to",
              "evidence",
              "valid_from",
              "valid_until"
            ],
            "properties": {
              "from": {
                "$ref": "common.schema.json#/$defs/Caip10"
              },
              "to": {
                "$ref": "common.schema.json#/$defs/Account"
              },
              "evidence": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "type",
                  "ref"
                ],
                "properties": {
                  "type": {
                    "enum": [
                      "safe-exec-transaction",
                      "eip191-signature",
                      "transaction-calldata"
                    ]
                  },
                  "ref": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 400
                  },
                  "message": {
                    "type": "string",
                    "maxLength": 2000
                  }
                }
              },
              "valid_from": {
                "$ref": "common.schema.json#/$defs/Time"
              },
              "valid_until": {
                "oneOf": [
                  {
                    "$ref": "common.schema.json#/$defs/Time"
                  },
                  {
                    "type": "null"
                  }
                ]
              }
            }
          }
        }
      }
    },
    "Withheld": {
      "description": "A part the issuer withheld (M15): only its digest is public. For a leaf, its RFC 9162 Merkle leaf hash; for the manifest, its object digest.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "withheld"
      ],
      "properties": {
        "withheld": {
          "$ref": "common.schema.json#/$defs/Digest"
        }
      }
    },
    "TpmBindingEvidence": {
      "description": "ORD-020: evidence that the checkpoint key lives in a TPM (types.ts TpmBindingEvidence). The AK is anchored only if a relying party pins it; this evidence never claims an endorsement chain it lacks.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "tpm",
        "ak",
        "key",
        "certify",
        "quote"
      ],
      "properties": {
        "type": {
          "const": "tpm2-certify-quote"
        },
        "tpm": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "manufacturer",
            "vendor",
            "endorsement_certificate",
            "endorsement_note"
          ],
          "properties": {
            "manufacturer": {
              "type": "string"
            },
            "vendor": {
              "type": "string"
            },
            "endorsement_certificate": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "enrollment",
                    "ek_cert_sha256"
                  ],
                  "properties": {
                    "enrollment": {
                      "type": "string",
                      "pattern": "^sha256:[A-Za-z0-9_-]{43}$"
                    },
                    "ek_cert_sha256": {
                      "type": "string",
                      "pattern": "^[0-9a-f]{64}$"
                    }
                  }
                }
              ],
              "description": "ORD-038: null, or the TPM enrollment record this evidence relies on"
            },
            "endorsement_note": {
              "type": "string"
            }
          }
        },
        "ak": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "jwk"
          ],
          "properties": {
            "jwk": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "kty",
                "crv",
                "x",
                "y"
              ],
              "properties": {
                "kty": {
                  "const": "EC"
                },
                "crv": {
                  "const": "P-256"
                },
                "x": {
                  "type": "string"
                },
                "y": {
                  "type": "string"
                }
              }
            },
            "tpmt_public": {
              "type": "string",
              "pattern": "^[A-Za-z0-9+/=_-]+$",
              "description": "optional: the AK's TPMT_PUBLIC, base64"
            }
          }
        },
        "key": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "tpmt_public"
          ],
          "properties": {
            "tpmt_public": {
              "type": "string",
              "pattern": "^[A-Za-z0-9+/=_-]+$"
            }
          }
        },
        "certify": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "attest",
            "signature"
          ],
          "properties": {
            "attest": {
              "type": "string",
              "pattern": "^[A-Za-z0-9+/=_-]+$"
            },
            "signature": {
              "type": "string",
              "pattern": "^[A-Za-z0-9+/=_-]+$"
            }
          }
        },
        "quote": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "attest",
            "signature",
            "bank",
            "pcrs"
          ],
          "properties": {
            "attest": {
              "type": "string",
              "pattern": "^[A-Za-z0-9+/=_-]+$"
            },
            "signature": {
              "type": "string",
              "pattern": "^[A-Za-z0-9+/=_-]+$"
            },
            "bank": {
              "const": "sha256"
            },
            "pcrs": {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}
