{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kingy.ai/schemas/local-lab-run-receipt-v1.json",
  "title": "Kingy AI Local Lab run receipt",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "runId",
    "model",
    "artifact",
    "runtime",
    "environment",
    "configuration",
    "measurements",
    "evaluation",
    "timestamp",
    "evidenceState",
    "reviewer",
    "rawEvidence",
    "review",
    "receiptHash"
  ],
  "properties": {
    "schemaVersion": { "const": "1.0.0" },
    "runId": { "type": "string", "pattern": "^[a-z0-9][a-z0-9._-]{7,127}$" },
    "model": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "repository", "revision"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "repository": { "type": "string", "pattern": "^[^/]+/[^/]+$" },
        "revision": { "$ref": "#/$defs/gitRevision" }
      }
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["filename", "repository", "revision", "sha256", "sizeBytes", "quantization", "contentHashSource"],
      "properties": {
        "filename": { "type": "string", "minLength": 1 },
        "repository": { "type": "string", "pattern": "^[^/]+/[^/]+$" },
        "revision": { "$ref": "#/$defs/gitRevision" },
        "sha256": { "$ref": "#/$defs/sha256" },
        "sizeBytes": { "type": "integer", "minimum": 1 },
        "quantization": { "type": "string", "minLength": 1 },
        "contentHashSource": { "type": "string", "minLength": 1 }
      }
    },
    "runtime": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version", "commit", "command"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "commit": { "$ref": "#/$defs/gitRevision" },
        "command": { "type": "array", "minItems": 1, "items": { "type": "string" } }
      }
    },
    "environment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "os",
        "hardware",
        "hardwareId",
        "gpuUuid",
        "driverVersion",
        "memoryBytes",
        "environmentLog",
        "cloudProvider",
        "cloudRegion",
        "podId",
        "allocatedVcpu",
        "cpu",
        "hostVisibleCpuCount",
        "hostVisibleMemoryBytes",
        "allocationSource",
        "gpuMemoryMiB",
        "cudaDriverCapability",
        "cudaToolkit"
      ],
      "properties": {
        "os": { "type": "string", "minLength": 1 },
        "hardware": { "type": "string", "minLength": 1 },
        "hardwareId": { "type": "string", "minLength": 1 },
        "gpuUuid": { "type": "string", "pattern": "^GPU-[A-Za-z0-9-]+$" },
        "driverVersion": { "type": "string", "minLength": 1 },
        "memoryBytes": { "type": "integer", "minimum": 1 },
        "environmentLog": { "type": "string", "minLength": 1 },
        "cloudProvider": { "type": "string", "minLength": 1 },
        "cloudRegion": { "type": "string", "minLength": 1 },
        "podId": { "type": "string", "minLength": 1 },
        "allocatedVcpu": { "type": "integer", "minimum": 1 },
        "cpu": { "type": "string", "minLength": 1 },
        "hostVisibleCpuCount": { "type": "integer", "minimum": 1 },
        "hostVisibleMemoryBytes": { "type": "integer", "minimum": 1 },
        "allocationSource": { "type": "string", "minLength": 1 },
        "gpuMemoryMiB": { "type": "integer", "minimum": 1 },
        "cudaDriverCapability": { "type": "string", "minLength": 1 },
        "cudaToolkit": { "type": "string", "minLength": 1 }
      }
    },
    "configuration": {
      "type": "object",
      "additionalProperties": false,
      "required": ["contextTokens", "kvCacheType", "batchSize", "microBatchSize", "parallelism", "gpuLayersRequested", "offload", "phase"],
      "properties": {
        "contextTokens": { "type": "integer", "minimum": 1 },
        "kvCacheType": { "enum": ["f16", "q8_0", "q4_0"] },
        "batchSize": { "type": "integer", "minimum": 1 },
        "microBatchSize": { "type": "integer", "minimum": 1 },
        "parallelism": { "type": "integer", "minimum": 1 },
        "gpuLayersRequested": { "type": "integer", "minimum": 0 },
        "offload": { "type": "string", "minLength": 1 },
        "phase": { "enum": ["fixed", "boundary"] }
      }
    },
    "measurements": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "warmupRuns",
        "measuredRuns",
        "peakMemoryBytes",
        "peakMemoryMiB",
        "loadSeconds",
        "promptTokensPerSecond",
        "generationTokensPerSecond",
        "timeToFirstTokenSeconds",
        "outcome",
        "actualPromptTokens",
        "actualGenerationTokens",
        "rawRepeats",
        "failureDetail",
        "statistics"
      ],
      "properties": {
        "warmupRuns": { "type": "integer", "minimum": 0 },
        "measuredRuns": { "type": "integer", "minimum": 0 },
        "peakMemoryBytes": { "type": "integer", "minimum": 1 },
        "peakMemoryMiB": { "type": "integer", "minimum": 1 },
        "loadSeconds": { "type": "number", "exclusiveMinimum": 0 },
        "promptTokensPerSecond": { "type": "number", "minimum": 0 },
        "generationTokensPerSecond": { "type": "number", "minimum": 0 },
        "timeToFirstTokenSeconds": { "type": "number", "minimum": 0 },
        "outcome": { "enum": ["success", "oom", "runtime-error"] },
        "actualPromptTokens": { "type": "array", "items": { "type": "integer", "minimum": 0 } },
        "actualGenerationTokens": { "type": "array", "items": { "type": "integer", "minimum": 0 } },
        "rawRepeats": { "type": "array", "items": { "$ref": "#/$defs/rawRepeat" } },
        "failureDetail": { "type": "string" },
        "statistics": { "$ref": "#/$defs/statistics" }
      }
    },
    "evaluation": { "type": ["object", "null"] },
    "timestamp": { "type": "string", "format": "date-time" },
    "evidenceState": { "const": "observed" },
    "reviewer": { "type": "string", "minLength": 1 },
    "rawEvidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["requestLog", "serverLog", "memoryLog", "archiveUrl", "archiveSha256"],
      "properties": {
        "requestLog": { "type": "string", "minLength": 1 },
        "serverLog": { "type": "string", "minLength": 1 },
        "memoryLog": { "type": "string", "minLength": 1 },
        "archiveUrl": { "type": "string", "pattern": "^/data/evidence/[^?#]+$" },
        "archiveSha256": { "$ref": "#/$defs/sha256" }
      }
    },
    "review": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "reviewedAt", "checks", "limitations"],
      "properties": {
        "status": { "const": "passed" },
        "reviewedAt": { "type": "string", "format": "date-time" },
        "checks": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 } },
        "limitations": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 } }
      }
    },
    "receiptHash": { "type": "string", "pattern": "^sha256:[a-f0-9]{64}$" }
  },
  "allOf": [
    {
      "if": { "type": "object", "properties": { "measurements": { "type": "object", "properties": { "outcome": { "const": "success" } }, "required": ["outcome"] } } },
      "then": {
        "type": "object",
        "properties": {
          "measurements": {
            "type": "object",
            "properties": {
              "warmupRuns": { "type": "integer", "minimum": 1 },
              "measuredRuns": { "type": "integer", "minimum": 3 },
              "promptTokensPerSecond": { "type": "number", "exclusiveMinimum": 0 },
              "generationTokensPerSecond": { "type": "number", "exclusiveMinimum": 0 },
              "timeToFirstTokenSeconds": { "type": "number", "exclusiveMinimum": 0 },
              "actualPromptTokens": { "type": "array", "minItems": 3, "items": { "const": 512 } },
              "actualGenerationTokens": { "type": "array", "minItems": 3, "items": { "const": 128 } },
              "rawRepeats": { "type": "array", "minItems": 3 },
              "failureDetail": { "type": "string", "const": "" },
              "statistics": { "type": "object", "$ref": "#/$defs/successStatistics" }
            }
          }
        }
      },
      "else": {
        "type": "object",
        "properties": {
          "measurements": {
            "type": "object",
            "properties": {
              "measuredRuns": { "type": "integer", "const": 0 },
              "promptTokensPerSecond": { "type": "number", "const": 0 },
              "generationTokensPerSecond": { "type": "number", "const": 0 },
              "timeToFirstTokenSeconds": { "type": "number", "const": 0 },
              "actualPromptTokens": { "type": "array", "maxItems": 0 },
              "actualGenerationTokens": { "type": "array", "maxItems": 0 },
              "rawRepeats": { "type": "array", "maxItems": 0 },
              "failureDetail": { "type": "string", "minLength": 1 },
              "statistics": { "type": "object", "$ref": "#/$defs/failureStatistics" }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "gitRevision": { "type": "string", "pattern": "^[a-f0-9]{40}$" },
    "metricSpread": {
      "type": "object",
      "additionalProperties": false,
      "required": ["min", "median", "max"],
      "properties": {
        "min": { "type": "number", "minimum": 0 },
        "median": { "type": "number", "minimum": 0 },
        "max": { "type": "number", "minimum": 0 }
      }
    },
    "statistics": {
      "type": "object",
      "additionalProperties": false,
      "required": ["promptTokensPerSecond", "generationTokensPerSecond", "timeToFirstTokenSeconds"],
      "properties": {
        "promptTokensPerSecond": { "anyOf": [{ "$ref": "#/$defs/metricSpread" }, { "type": "null" }] },
        "generationTokensPerSecond": { "anyOf": [{ "$ref": "#/$defs/metricSpread" }, { "type": "null" }] },
        "timeToFirstTokenSeconds": { "anyOf": [{ "$ref": "#/$defs/metricSpread" }, { "type": "null" }] }
      }
    },
    "successStatistics": {
      "type": "object",
      "properties": {
        "promptTokensPerSecond": { "$ref": "#/$defs/metricSpread" },
        "generationTokensPerSecond": { "$ref": "#/$defs/metricSpread" },
        "timeToFirstTokenSeconds": { "$ref": "#/$defs/metricSpread" }
      }
    },
    "failureStatistics": {
      "type": "object",
      "properties": {
        "promptTokensPerSecond": { "type": "null" },
        "generationTokensPerSecond": { "type": "null" },
        "timeToFirstTokenSeconds": { "type": "null" }
      }
    },
    "rawRepeat": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label", "startedAt", "timeToFirstTokenSeconds", "elapsedSeconds", "eventCount", "final"],
      "properties": {
        "label": { "type": "string", "minLength": 1 },
        "startedAt": { "type": "string", "format": "date-time" },
        "timeToFirstTokenSeconds": { "type": "number", "exclusiveMinimum": 0 },
        "elapsedSeconds": { "type": "number", "exclusiveMinimum": 0 },
        "eventCount": { "type": "integer", "minimum": 1 },
        "final": {
          "type": "object",
          "required": ["tokens_predicted", "tokens_evaluated", "timings"],
          "properties": {
            "tokens_predicted": { "const": 128 },
            "tokens_evaluated": { "const": 512 },
            "timings": {
              "type": "object",
              "required": ["prompt_n", "prompt_per_second", "predicted_n", "predicted_per_second"],
              "properties": {
                "prompt_n": { "const": 512 },
                "prompt_per_second": { "type": "number", "exclusiveMinimum": 0 },
                "predicted_n": { "const": 128 },
                "predicted_per_second": { "type": "number", "exclusiveMinimum": 0 }
              },
              "additionalProperties": true
            }
          },
          "additionalProperties": true
        }
      }
    }
  }
}
