{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rootz.global/schemas/ask-orders/leaf.schema.json",
  "title": "ASK Orders session leaf — 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"
        },
        {
          "$ref": "#/$defs/Teardown"
        },
        {
          "$ref": "#/$defs/TlsObservation"
        },
        {
          "$ref": "#/$defs/SignerReach"
        },
        {
          "$ref": "#/$defs/SignerLog"
        },
        {
          "$ref": "#/$defs/RunFacts"
        },
        {
          "$ref": "#/$defs/EgressProbe"
        },
        {
          "$ref": "#/$defs/AgentResult"
        },
        {
          "$ref": "#/$defs/GatewayExchange"
        }
      ]
    }
  },
  "$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
        },
        "mxc_policy_hash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$",
          "description": "ORD-035: the effective MXC policy hash at install"
        }
      }
    },
    "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": {
          "$comment": "ORD-033 (proposed): { unavailable } when the recorder was not permitted to read the body (bank login mode, P4).",
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/ByteCommitment"
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "unavailable"
              ],
              "properties": {
                "unavailable": {
                  "$ref": "common.schema.json#/$defs/ReasonCode"
                }
              }
            }
          ]
        },
        "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"
                }
              }
            }
          ]
        },
        "exit_code": {
          "type": "integer",
          "description": "ORD-039: the real exit code of a local broker run (status is 200 when it is 0, else 500)"
        }
      }
    },
    "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"
            }
          }
        }
      }
    },
    "Teardown": {
      "$comment": "ORD-033 (proposed): the box recorded its own teardown: a measured count by the recorder, not a proof of absence.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "target",
        "entries_removed",
        "entries_remaining"
      ],
      "properties": {
        "type": {
          "const": "teardown"
        },
        "target": {
          "enum": [
            "browser-profile"
          ]
        },
        "entries_removed": {
          "type": "integer",
          "minimum": 0
        },
        "entries_remaining": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "TlsObservation": {
      "$comment": "ORD-034: the source's certificate as presented to a SEPARATE handshake from the collector's machine (not the connection that carried the exchange). Fingerprints: sha256 of the DER certificates, leaf first.",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "host",
        "port",
        "basis",
        "authorized",
        "error",
        "protocol",
        "leaf",
        "chain",
        "subject",
        "issuer",
        "valid_to"
      ],
      "properties": {
        "type": {
          "const": "tls-observation"
        },
        "host": {
          "type": "string",
          "minLength": 1,
          "maxLength": 253
        },
        "port": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "basis": {
          "enum": [
            "parallel-handshake"
          ]
        },
        "authorized": {
          "type": "boolean"
        },
        "error": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 500
        },
        "protocol": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 32
        },
        "leaf": {
          "oneOf": [
            {
              "$ref": "common.schema.json#/$defs/Digest"
            },
            {
              "type": "null"
            }
          ]
        },
        "chain": {
          "type": "array",
          "maxItems": 10,
          "items": {
            "$ref": "common.schema.json#/$defs/Digest"
          }
        },
        "subject": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 1000
        },
        "issuer": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 1000
        },
        "valid_to": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 64
        }
      }
    },
    "SignerReach": {
      "description": "ORD-035 item 7: broker-observed reach of the checkpoint signer, right after genesis",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "agent",
        "signer",
        "keys",
        "probes"
      ],
      "properties": {
        "type": {
          "const": "signer-reach"
        },
        "agent": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "principal_sid",
            "kind",
            "mxc_policy_hash"
          ],
          "properties": {
            "principal_sid": {
              "type": "string",
              "pattern": "^S-1-[0-9-]+$"
            },
            "kind": {
              "enum": [
                "local-user",
                "appcontainer",
                "isolation-session",
                "appcontainer-in-local-user"
              ]
            },
            "mxc_policy_hash": {
              "oneOf": [
                {
                  "type": "string",
                  "pattern": "^sha256:[0-9a-f]{64}$"
                },
                {
                  "type": "null"
                }
              ]
            }
          }
        },
        "signer": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "pipe",
            "pipe_sddl",
            "service_sid"
          ],
          "properties": {
            "pipe": {
              "type": "string",
              "maxLength": 260
            },
            "pipe_sddl": {
              "type": "string",
              "maxLength": 4096
            },
            "service_sid": {
              "type": "string",
              "pattern": "^S-1-[0-9-]+$"
            }
          }
        },
        "keys": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "store_sddl",
            "checkpoint_key_name",
            "auth"
          ],
          "properties": {
            "store_sddl": {
              "type": "string",
              "maxLength": 4096
            },
            "checkpoint_key_name": {
              "type": "string",
              "maxLength": 200
            },
            "auth": {
              "type": "string",
              "maxLength": 100
            }
          }
        },
        "probes": {
          "type": "array",
          "maxItems": 16,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "from",
              "target",
              "result"
            ],
            "properties": {
              "from": {
                "const": "agent"
              },
              "target": {
                "enum": [
                  "signer-pipe",
                  "keystore"
                ]
              },
              "result": {
                "enum": [
                  "access-denied",
                  "opened",
                  "error"
                ]
              }
            }
          }
        },
        "binding_nonce": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{22,43}$",
          "description": "PROTOCOL rev 1.2: per-run binding nonce (16-32 bytes, unpadded base64url)"
        },
        "binding_nonce_salt": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{22,43}$",
          "description": "F5: salt for the recomputable binding nonce"
        }
      }
    },
    "SignerLog": {
      "description": "ORD-035 item 7: the signer's hash-chained log window for this run, just before seal",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "from",
        "to",
        "entries",
        "all_client_pids_broker"
      ],
      "properties": {
        "type": {
          "const": "signer-log"
        },
        "from": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "seq",
            "line_sha256"
          ],
          "properties": {
            "seq": {
              "type": "integer",
              "minimum": 0
            },
            "line_sha256": {
              "type": "string",
              "maxLength": 100
            }
          }
        },
        "to": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "seq",
            "line_sha256"
          ],
          "properties": {
            "seq": {
              "type": "integer",
              "minimum": 0
            },
            "line_sha256": {
              "type": "string",
              "maxLength": 100
            }
          }
        },
        "entries": {
          "type": "integer",
          "minimum": 0
        },
        "all_client_pids_broker": {
          "type": "boolean"
        }
      }
    },
    "RunFacts": {
      "description": "F7: the run's stated limits, recorded before seal",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "accepted_unmeasured",
        "profile_measured",
        "job_processes_total",
        "killed_at_drain"
      ],
      "properties": {
        "type": {
          "const": "run-facts"
        },
        "accepted_unmeasured": {
          "type": "array",
          "maxItems": 64,
          "items": {
            "type": "string",
            "maxLength": 260
          }
        },
        "profile_measured": {
          "type": "boolean"
        },
        "job_processes_total": {
          "type": "integer",
          "minimum": 0
        },
        "killed_at_drain": {
          "type": "integer",
          "minimum": 0
        },
        "notes": {
          "type": "array",
          "maxItems": 32,
          "items": {
            "type": "string",
            "maxLength": 500
          }
        },
        "agent_state": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "files",
            "digest"
          ],
          "properties": {
            "files": {
              "type": "integer",
              "minimum": 0
            },
            "digest": {
              "$ref": "common.schema.json#/$defs/Digest"
            }
          }
        },
        "gateway_log": {
          "description": "ORD-041: the gateway log's line count and head (sha256 of the last line)",
          "type": "object",
          "additionalProperties": false,
          "required": [
            "count",
            "head"
          ],
          "properties": {
            "count": {
              "type": "integer",
              "minimum": 1
            },
            "head": {
              "type": "string",
              "pattern": "^(sha256:)?[0-9a-f]{64}$"
            }
          }
        },
        "session": {
          "description": "ORD-037: the session this run belongs to",
          "type": "object",
          "additionalProperties": false,
          "required": [
            "slot",
            "binding_run_id",
            "lease"
          ],
          "properties": {
            "slot": {
              "type": "string"
            },
            "binding_run_id": {
              "type": "string"
            },
            "run_index": {
              "type": "integer",
              "minimum": 1
            },
            "workspace_before": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "tree",
                "digest"
              ],
              "properties": {
                "tree": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "additionalProperties": false,
                    "required": [
                      "path",
                      "sha256",
                      "bytes"
                    ],
                    "properties": {
                      "path": {
                        "type": "string"
                      },
                      "sha256": {
                        "type": "string",
                        "pattern": "^[0-9a-f]{64}$"
                      },
                      "bytes": {
                        "type": "integer",
                        "minimum": 0
                      }
                    }
                  }
                },
                "digest": {
                  "type": "string",
                  "pattern": "^sha256:[0-9a-f]{64}$"
                }
              }
            },
            "workspace_after": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "tree",
                "digest"
              ],
              "properties": {
                "tree": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "additionalProperties": false,
                    "required": [
                      "path",
                      "sha256",
                      "bytes"
                    ],
                    "properties": {
                      "path": {
                        "type": "string"
                      },
                      "sha256": {
                        "type": "string",
                        "pattern": "^[0-9a-f]{64}$"
                      },
                      "bytes": {
                        "type": "integer",
                        "minimum": 0
                      }
                    }
                  }
                },
                "digest": {
                  "type": "string",
                  "pattern": "^sha256:[0-9a-f]{64}$"
                }
              }
            },
            "lease": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "grant",
                "not_after",
                "budget_microusd",
                "spent_before_microusd",
                "prices"
              ],
              "properties": {
                "grant": {
                  "$ref": "common.schema.json#/$defs/Digest"
                },
                "not_after": {
                  "type": "string"
                },
                "budget_microusd": {
                  "type": "integer",
                  "minimum": 0
                },
                "spent_before_microusd": {
                  "type": "integer",
                  "minimum": 0
                },
                "prices": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "object",
                    "additionalProperties": false,
                    "required": [
                      "input",
                      "output",
                      "cache_write",
                      "cache_read"
                    ],
                    "properties": {
                      "input": {
                        "type": "integer",
                        "minimum": 0
                      },
                      "output": {
                        "type": "integer",
                        "minimum": 0
                      },
                      "cache_write": {
                        "type": "integer",
                        "minimum": 0
                      },
                      "cache_read": {
                        "type": "integer",
                        "minimum": 0
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "EgressProbe": {
      "description": "ORD-040: broker egress test under the same effective policy, before the run",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "policy_hash",
        "results"
      ],
      "properties": {
        "type": {
          "const": "egress-probe"
        },
        "policy_hash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "results": {
          "type": "array",
          "minItems": 1,
          "maxItems": 64,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "target",
              "expect",
              "observed"
            ],
            "properties": {
              "target": {
                "type": "string",
                "maxLength": 300
              },
              "expect": {
                "enum": [
                  "connect",
                  "refuse"
                ]
              },
              "observed": {
                "type": "string",
                "maxLength": 300
              }
            }
          }
        },
        "controls": {
          "type": "array",
          "maxItems": 16,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "target",
              "from",
              "expect",
              "observed"
            ],
            "properties": {
              "target": {
                "type": "string",
                "maxLength": 300
              },
              "from": {
                "type": "string",
                "maxLength": 300
              },
              "expect": {
                "const": "connect"
              },
              "observed": {
                "type": "string",
                "maxLength": 300
              }
            }
          }
        }
      }
    },
    "AgentResult": {
      "description": "ORD-040: the agent's own report; never changes a line",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "reported_by",
        "model",
        "num_turns",
        "usage",
        "total_cost_usd"
      ],
      "properties": {
        "type": {
          "const": "agent-result"
        },
        "reported_by": {
          "const": "agent"
        },
        "model": {
          "type": "string",
          "maxLength": 300
        },
        "num_turns": {
          "type": "integer",
          "minimum": 0
        },
        "usage": {
          "type": "object",
          "additionalProperties": {
            "type": "number"
          }
        },
        "total_cost_usd": {
          "type": "number",
          "minimum": 0
        }
      }
    },
    "GatewayExchange": {
      "description": "ORD-041: one gateway log line, carried verbatim",
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "line"
      ],
      "properties": {
        "type": {
          "const": "gateway-exchange"
        },
        "line": {
          "type": "string",
          "minLength": 2,
          "maxLength": 1048576
        }
      }
    }
  }
}
