{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://uwmd.org/schemas/uw-receipt.schema.json",
  "title": "UW Verification Receipt 1.1",
  "description": "A detached JSON document binding a canonical digest of an underwriting record to the deterministic outputs a named calc pack produced from it. Normative definition: UW_RECEIPT.md (RFCs 0016 and 0088). After source/subject/dependency preflights, supplied signatures authenticate before financial recomputation or refusal; the wire shape is unchanged. A receipt attests that the stated outputs follow from the record's canonical financial content — it attests nothing about whether that content is true.",
  "type": "object",
  "required": ["receipt_version", "subject", "computation", "policy", "issued_at", "issuer"],
  "additionalProperties": false,
  "properties": {
    "receipt_version": {
      "const": "1.1",
      "description": "Only admitted receipt version, independent of format/protocol/package versions. inputs_provenance remains optional (UW_RECEIPT §10)."
    },
    "subject": {
      "type": "object",
      "description": "Which record this receipt is about, and by which canonical form it is identified.",
      "required": [
        "representation",
        "representation_version",
        "canonicalization",
        "canonicalization_version",
        "digest"
      ],
      "additionalProperties": false,
      "properties": {
        "representation": {
          "enum": ["uw-lite-markdown", "uwx-markdown"],
          "description": "Source representation identifier (UW_LITE_SPEC / RFC 0017)."
        },
        "representation_version": {
          "type": "string",
          "minLength": 1
        },
        "canonicalization": {
          "enum": ["uw-lite-financial", "uw-envelope-semantic"],
          "description": "Named canonical form the digest covers. `uw-lite-financial` is UW_LITE_SPEC §6; `uw-envelope-semantic` is the Document Envelope 2.0 semantic value under RFC 8785."
        },
        "canonicalization_version": {
          "type": "string",
          "minLength": 1,
          "description": "Version of the canonicalization rules, versioned independently of `representation_version` — the rules for producing canonical bytes can change without the source grammar changing, so an implementation MUST NOT stamp one from the other. Digests computed under different versions are not comparable; see UW_RECEIPT §5.5 (`RCP-10`)."
        },
        "digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$",
          "description": "SHA-256 over the exact UTF-8 bytes of the named canonical form."
        }
      }
    },
    "computation": {
      "type": "object",
      "description": "Which deterministic pack ran, on which engine, and what it produced.",
      "required": ["pack", "pack_version", "engine", "engine_version", "rule_set", "results", "results_digest"],
      "additionalProperties": false,
      "properties": {
        "pack": {
          "type": "string",
          "minLength": 1,
          "description": "Calc pack manifest id, e.g. org.uwmd.pack.multifamily."
        },
        "pack_version": {
          "type": "string",
          "minLength": 1,
          "description": "Semver of the pack manifest. A pack with no version cannot be named here."
        },
        "engine": {
          "type": "string",
          "minLength": 1
        },
        "engine_version": {
          "type": "string",
          "minLength": 1
        },
        "protocol_version": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+\\.\\d+$",
          "description": "UW Protocol version the issuer computed under. Optional: a receipt issued before this field existed cannot retroactively claim one, so absence means 'unstated' rather than 'non-conforming'. When present it answers whether the stated values are quantized per §VIII.5 without requiring the reader to know the issuing engine's release history — which is exactly what a third-party issuer's engine_version cannot convey."
        },
        "rule_set": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+$",
          "description": "The rule set the issuer computed and validated under, named by Format version: the subject record's declared uw_version (UW_PROTOCOL §XII.5, RFC 0111 D5; UW_RECEIPT §5.2.2). A verifier selects the rule set from the record and fails a receipt naming another (RCP-13)."
        },
        "results": {
          "type": "array",
          "minItems": 1,
          "description": "Exactly the outputs the named pack declares — no more, no fewer. Never a place for hand-entered numbers.",
          "items": { "$ref": "#/$defs/result" }
        },
        "results_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$",
          "description": "SHA-256 over the RFC 8785 canonicalization of `results`. A cheap corruption check only — a verifier MUST recompute rather than trust it."
        }
      }
    },
    "policy": {
      "type": "object",
      "description": "Which policy set was applied, and what it found.",
      "required": ["policy_set", "policy_set_version", "validation"],
      "additionalProperties": false,
      "properties": {
        "policy_set": { "type": "string", "minLength": 1 },
        "policy_set_version": { "type": "string", "minLength": 1 },
        "validation": {
          "type": "object",
          "required": ["errors", "warnings"],
          "additionalProperties": false,
          "properties": {
            "errors": { "type": "integer", "minimum": 0 },
            "warnings": { "type": "integer", "minimum": 0 }
          }
        }
      }
    },
    "inputs_provenance": {
      "type": "array",
      "description": "Artifacts beyond the subject record that the computation depended on (receipt_version 1.1, UW_RECEIPT §10). One section shared by RFC 0022 (market-data observation sets) and RFC 0021 (composite child records), discriminated by `source`, because a verifier handles both identically: resolve, compare digest, and keep 'cannot find it' (RCP-11, unverifiable) distinct from 'does not match' (RCP-12, failed). Optional — a record that consumed no external input omits it.",
      "items": {
        "type": "object",
        "required": ["source", "document_id", "digest"],
        "additionalProperties": false,
        "properties": {
          "source": {
            "type": "string",
            "minLength": 1,
            "description": "Kind of referenced artifact. `market_data` (RFC 0022) and `child_record` (RFC 0021) are registered; the union is deliberately open, and an implementation MUST carry an unrecognized source and report it as unresolvable rather than rejecting the receipt."
          },
          "document_id": {
            "type": "string",
            "minLength": 1,
            "description": "Identity of the referenced artifact. MUST be unique within this array — two entries for one id would make the digest comparison order-dependent."
          },
          "as_of": {
            "type": "string",
            "description": "Vintage of the referenced artifact. Optional here because not every source has one; a `market_data` entry always carries it in practice, since market-data-v1 refuses to parse without `as_of`."
          },
          "digest": {
            "type": "string",
            "pattern": "^sha256:[0-9a-f]{64}$",
            "description": "SHA-256 over the referenced artifact's canonical form."
          }
        }
      }
    },
    "issued_at": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 instant of issuance."
    },
    "issuer": {
      "type": "string",
      "minLength": 1,
      "description": "Identity of the issuing tool, e.g. uwmd-cli@1.1.3. Not an assertion of authority."
    },
    "signature": {
      "description": "Optional detached signature over the RFC 8785 canonicalization of this receipt with `signature` set to null. Same wire shape as RFC 0010 block signatures. Key distribution is out of scope.",
      "oneOf": [
        { "type": "null" },
        {
          "type": "object",
          "required": ["algorithm", "key_id", "value"],
          "additionalProperties": false,
          "properties": {
            "algorithm": { "type": "string", "minLength": 1 },
            "key_id": { "type": "string", "minLength": 1 },
            "value": { "type": "string", "minLength": 1 }
          }
        }
      ]
    }
  },
  "$defs": {
    "result": {
      "type": "object",
      "required": ["calc_id", "value", "computed"],
      "additionalProperties": false,
      "properties": {
        "calc_id": { "type": "string", "minLength": 1 },
        "value": {
          "type": ["number", "string", "boolean", "null"],
          "description": "The pack's output. MUST be null when `computed` is false."
        },
        "unit": { "type": "string" },
        "computed": {
          "type": "boolean",
          "description": "False when the pack declares this output but the record lacks the inputs to produce it. Distinguishes 'not computed' from 'computed to null'."
        }
      },
      "allOf": [
        {
          "if": { "properties": { "computed": { "const": false } }, "required": ["computed"] },
          "then": { "properties": { "value": { "type": "null" } } }
        }
      ]
    }
  }
}
