{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://qwatt.org/protocol/batch.schema.json",
  "x-schema-version": "1.1.0",
  "title": "QWATT evidence batch",
  "description": "The unit of evidence that gets published and, when accepted, anchored on Stellar. Contains the accepted interval records it vouches for. `sha256` is computed over the canonical JSON of the batch with `sha256` removed. All energies are integers in milliwatt-hours so the canonical bytes are identical in every language. Defined in docs/ARCHITECTURE-FREEZE.md §2. Version 1.1.0 (week 2, V3-5) makes samples_root required.",
  "type": "object",
  "required": ["batch_id", "site_id", "node_id", "seq", "ts_start", "ts_end", "spec_version", "schema_version", "interval_count", "accepted_count", "rejected_count", "intervals", "intervals_root", "samples_root", "energy_mwh", "cumulative_mwh", "prev_hash", "sha256"],
  "additionalProperties": false,
  "properties": {
    "batch_id": { "type": "string", "pattern": "^[A-Za-z0-9_.-]+-[0-9]{6}$", "description": "`<site_id>-<seq zero-padded to 6>`." },
    "site_id": { "type": "string", "minLength": 1 },
    "node_id": { "type": "string", "minLength": 1 },
    "seq": { "type": "integer", "minimum": 0, "description": "Batch sequence per site, strictly increasing." },
    "ts_start": { "type": "string", "format": "date-time" },
    "ts_end": { "type": "string", "format": "date-time" },
    "spec_version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$", "description": "gates.spec.json version that judged every interval in this batch." },
    "schema_version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$", "description": "interval.schema.json version of the contained records." },
    "interval_count": { "type": "integer", "minimum": 0, "description": "Intervals judged in this window, accepted + rejected." },
    "accepted_count": { "type": "integer", "minimum": 0 },
    "rejected_count": { "type": "integer", "minimum": 0 },
    "intervals": { "type": "array", "items": { "$ref": "interval.schema.json" }, "description": "The accepted interval records, in sequence order. Rejected intervals are in the rejected ledger, never here." },
    "intervals_root": { "type": "string", "pattern": "^[0-9a-f]{64}$", "description": "Merkle root (SHA-256, odd node duplicated) over the canonical JSON of each interval." },
    "samples_root": { "type": "string", "pattern": "^[0-9a-f]{64}$", "description": "Required since batch schema 1.1.0 (V3-5): a fingerprint of every raw reading behind the window. The payloads are published at raw/<batch_id>.jsonl. R1, payload entry: {n, utf8} or {n, hex}. Its bytes are the UTF-8 of utf8, or hex decoded (lowercase, whole bytes); n >= 1 is the number of consecutive samples of the interval that arrived with exactly these bytes. CSV: the row as read plus \"\\n\", one entry per row, n = the meters with a reading. HTTP POST /samples: the request body, as utf8 when its bytes round-trip as UTF-8, otherwise hex; n = its samples in the interval. Modbus: the reply frames of one poll, concatenated, as hex, n 1. POST /intervals: the request body, n 1. R2, binding: SHA-256 of the stream (each entry's bytes n times, in order) equals the interval's source.raw_sha256. Exempt: an interval with no source.raw_sha256; a record posted whole (source.adapter http-json) that kept its node's raw_sha256, whose one payload (n 1, utf8) is that record as posted (the same canonical JSON apart from clock_skew_s). An accepted or gate-refused interval needs a non-empty bound payload list; a line refused at the door is published with what came with it (no payloads when those were malformed). R3, raw line: canonical JSON of {\"payloads\":[...],\"seq\":seq or null,\"ts_start\":ts_start or null,\"verdict\":\"accept\"|\"reject\"} plus \"\\n\" (seq only if a safe integer, ts_start only if a string). Exactly interval_count lines, sorted by Date.parse(ts_start) ascending (unreadable last), an accepted line before a refused one at the same start, otherwise in judgment order. R4: the intervals_root pairing (parent = SHA-256 of the two hex strings concatenated, an odd node paired with itself, no leaves = SHA-256 of no bytes) over SHA-256(bytes) of every payload entry of every raw line, in file order. Neither n nor the encoding enters a leaf." },
    "energy_mwh": { "type": "integer", "minimum": 0, "description": "Sum over accepted intervals of the counted energy (min of the two meters, or the single meter), milliwatt-hours." },
    "cumulative_mwh": { "type": "integer", "minimum": 0, "description": "Site total since genesis, after this batch." },
    "prev_hash": { "type": "string", "pattern": "^[0-9a-f]{64}$", "description": "sha256 of the previous batch, or 64 zeros for the first." },
    "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" }
  }
}
