{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rootz.global/schemas/ask-orders/leaf.schema.json",
  "title": "ASK Orders session leaf \u2014 one entry in the append-only session log",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "kind",
    "session",
    "seq",
    "at",
    "event"
  ],
  "properties": {
    "kind": {
      "const": "ask-orders.leaf.v1"
    },
    "session": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_-]{16,64}$"
    },
    "seq": {
      "type": "integer",
      "minimum": 0
    },
    "at": {
      "$ref": "common.schema.json#/$defs/Time"
    },
    "event": {
      "oneOf": [
        {
          "$ref": "#/$defs/Genesis"
        },
        {
          "$ref": "#/$defs/PolicyChange"
        },
        {
          "$ref": "#/$defs/Request"
        },
        {
          "$ref": "#/$defs/Response"
        },
        {
          "$ref": "#/$defs/Refusal"
        },
        {
          "$ref": "#/$defs/Capture"
        }
      ]
    }
  },
  "$defs": {
    "Genesis": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "order",
        "manifest",
        "openshell_policy_hash"
      ],
      "properties": {
        "type": {
          "const": "genesis"
        },
        "order": {
          "$ref": "common.schema.json#/$defs/Digest"
        },
        "manifest": {
          "$ref": "common.schema.json#/$defs/Digest"
        },
        "openshell_policy_hash": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    },
    "PolicyChange": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "order",
        "manifest"
      ],
      "properties": {
        "type": {
          "const": "policy.change"
        },
        "order": {
          "$ref": "common.schema.json#/$defs/Digest"
        },
        "manifest": {
          "$ref": "common.schema.json#/$defs/Digest"
        }
      }
    },
    "Request": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "request_id",
        "manifest",
        "method",
        "host",
        "path",
        "body",
        "decision",
        "reason"
      ],
      "properties": {
        "type": {
          "const": "request"
        },
        "request_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "manifest": {
          "$ref": "common.schema.json#/$defs/Digest"
        },
        "method": {
          "type": "string",
          "pattern": "^[A-Z]{3,10}$"
        },
        "host": {
          "type": "string",
          "minLength": 1,
          "maxLength": 253
        },
        "path": {
          "type": "string",
          "maxLength": 8192
        },
        "query": {
          "type": "string",
          "maxLength": 8192,
          "description": "The raw query string without the leading ?, present only when the request had one."
        },
        "body": {
          "$ref": "common.schema.json#/$defs/ByteCommitment"
        },
        "decision": {
          "enum": [
            "allow",
            "deny"
          ]
        },
        "inputs": {
          "description": "Digests of origin receipts the requester declared as inputs (ORD-015).",
          "type": "array",
          "items": {
            "$ref": "common.schema.json#/$defs/Digest"
          }
        },
        "reason": {
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/ReasonCode"
            },
            {
              "type": "null"
            }
          ]
        }
      }
    },
    "Response": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "request_id",
        "status",
        "body"
      ],
      "properties": {
        "type": {
          "const": "response"
        },
        "request_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "status": {
          "type": "integer",
          "minimum": 100,
          "maximum": 599
        },
        "body": {
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/ByteCommitment"
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "unavailable"
              ],
              "properties": {
                "unavailable": {
                  "$ref": "common.schema.json#/$defs/ReasonCode"
                }
              }
            }
          ]
        }
      }
    },
    "Refusal": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "what",
        "reason"
      ],
      "properties": {
        "type": {
          "const": "refusal"
        },
        "what": {
          "enum": [
            "policy.install",
            "policy.change",
            "request"
          ]
        },
        "subject": {
          "type": "string",
          "maxLength": 500
        },
        "reason": {
          "$ref": "common.schema.json#/$defs/ReasonCode"
        }
      }
    },
    "Capture": {
      "$comment": "ORD-032: an Archive Free capture record as a session event (the leaf `at` is the device clock).",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "capture_scheme",
        "source",
        "capture_kind",
        "body",
        "base",
        "private"
      ],
      "properties": {
        "type": {
          "const": "capture"
        },
        "capture_scheme": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9.-]{0,60}/[0-9]+$"
        },
        "source": {
          "type": "string",
          "minLength": 1,
          "maxLength": 60
        },
        "capture_kind": {
          "enum": [
            "transcript",
            "subagent-transcript",
            "tool-result",
            "memory",
            "file-history",
            "prompt-history",
            "other"
          ]
        },
        "body": {
          "$ref": "common.schema.json#/$defs/ByteCommitment"
        },
        "base": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "common.schema.json#/$defs/ByteCommitment"
            }
          ]
        },
        "private": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "path": {
              "$ref": "common.schema.json#/$defs/Digest"
            },
            "host": {
              "$ref": "common.schema.json#/$defs/Digest"
            },
            "mtime_ms": {
              "$ref": "common.schema.json#/$defs/Digest"
            }
          }
        }
      }
    }
  }
}
