{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://reachabilitylabs.org/schemas/outcome-and-deviation-receipt/v0.2",
  "title": "Outcome and Deviation Receipt v0.2",
  "description": "Append-only receipt linking an observed test result or realized change to an intact sealed source instrument.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "instrument_type",
    "receipt_id",
    "revision",
    "case_binding",
    "source",
    "observed_at",
    "evidence_refs",
    "observed",
    "actual_disposition",
    "actual_next_action",
    "path_updates",
    "realized_impact",
    "deviation",
    "recorded_by",
    "recorded_at",
    "receipt_hash"
  ],
  "properties": {
    "schema_version": { "const": "0.2" },
    "instrument_type": { "const": "outcome_and_deviation_receipt" },
    "receipt_id": { "type": "string", "minLength": 1, "maxLength": 128 },
    "revision": { "type": "string", "minLength": 1, "maxLength": 32 },
    "case_binding": { "$ref": "#/$defs/case_binding" },
    "source": { "$ref": "#/$defs/source_binding" },
    "observed_at": { "type": "string", "format": "date-time" },
    "evidence_refs": {
      "type": "array",
      "minItems": 1,
      "items": { "type": "string", "minLength": 1, "maxLength": 512 }
    },
    "observed": {
      "type": "object",
      "additionalProperties": false,
      "required": ["branch_or_change_ref", "result_or_realized_state", "comparison_to_source"],
      "properties": {
        "branch_or_change_ref": { "type": "string", "minLength": 1, "maxLength": 256 },
        "result_or_realized_state": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "comparison_to_source": { "type": "string", "minLength": 1, "maxLength": 4000 }
      }
    },
    "actual_disposition": {
      "enum": ["proceed", "hold", "redesign", "stop", "repeat", "release", "reject", "mandatory_verification_only", "other"]
    },
    "actual_next_action": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "path_updates": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/path_update" }
    },
    "realized_impact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["consumption_realized", "preservation_realized"],
      "properties": {
        "consumption_realized": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        },
        "preservation_realized": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        }
      }
    },
    "deviation": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["reason", "effect_on_interpretation", "authority", "authorized_at"],
      "properties": {
        "reason": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "effect_on_interpretation": { "type": "string", "minLength": 1, "maxLength": 4000 },
        "authority": { "type": "string", "minLength": 1, "maxLength": 256 },
        "authorized_at": { "type": "string", "format": "date-time" }
      }
    },
    "recorded_by": { "type": "string", "minLength": 1, "maxLength": 256 },
    "recorded_at": { "type": "string", "format": "date-time" },
    "receipt_hash": { "type": ["string", "null"], "pattern": "^[0-9a-f]{64}$" }
  },
  "$defs": {
    "case_binding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["case_id", "case_revision"],
      "properties": {
        "case_id": { "type": "string", "minLength": 1, "maxLength": 128 },
        "case_revision": { "type": "string", "minLength": 1, "maxLength": 64 }
      }
    },
    "source_binding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["instrument_type", "instrument_id", "revision", "seal_hash", "seal_verified"],
      "properties": {
        "instrument_type": { "enum": ["result_to_decision_test_card", "commitment_and_reversal_record"] },
        "instrument_id": { "type": "string", "minLength": 1, "maxLength": 128 },
        "revision": { "type": "string", "minLength": 1, "maxLength": 32 },
        "seal_hash": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
        "seal_verified": { "const": true }
      }
    },
    "path_standing": {
      "enum": ["open", "conditional", "unknown", "assessed_unreachable", "exactly_unreachable"]
    },
    "recovery_status": {
      "enum": ["none_identified", "candidate", "supported", "verified"]
    },
    "path_update": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path_ref", "standing", "recovery_status", "basis"],
      "properties": {
        "path_ref": { "type": "string", "minLength": 1, "maxLength": 256 },
        "standing": { "$ref": "#/$defs/path_standing" },
        "recovery_status": { "$ref": "#/$defs/recovery_status" },
        "basis": { "type": "string", "minLength": 1, "maxLength": 4000 }
      }
    }
  }
}
