{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://law.arxo.io/schema/package-report.schema.json",
  "type": "object",
  "properties": {
    "format": {
      "const": "law.package-report/0.1"
    },
    "kind": {
      "enum": [
        "quality",
        "compatibility",
        "benchmark",
        "inventory",
        "formalization"
      ]
    },
    "subject": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]*(?:\\.[a-z][a-z0-9_]*)*$"
        },
        "version": {
          "type": "string",
          "pattern": "^[0-9A-Za-z][0-9A-Za-z._+-]*$"
        },
        "namespace": {
          "type": "string"
        },
        "contentHash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "sourceHash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "buildHash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "replayHash": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^sha256:[0-9a-f]{64}$"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "name",
        "version",
        "namespace",
        "contentHash",
        "sourceHash",
        "buildHash",
        "replayHash"
      ],
      "additionalProperties": false
    },
    "outcome": {
      "enum": [
        "passed",
        "failed",
        "unsupported",
        "not_run",
        "skipped"
      ]
    },
    "recordedAt": {
      "type": "string",
      "format": "date-time",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\\.[0-9]+)?(?:Z|[+-][0-9]{2}:[0-9]{2})$"
    },
    "producer": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "hash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        }
      },
      "required": [
        "name",
        "hash"
      ],
      "additionalProperties": false
    },
    "configuration": {
      "type": "object"
    },
    "data": {
      "type": "object"
    }
  },
  "required": [
    "format",
    "kind",
    "subject",
    "outcome",
    "recordedAt",
    "producer",
    "configuration",
    "data"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "kind": {
            "const": "benchmark"
          },
          "outcome": {
            "const": "passed"
          }
        }
      },
      "then": {
        "properties": {
          "data": {
            "type": "object",
            "properties": {
              "format": {
                "const": "law.package-benchmark/0.1"
              },
              "adapter": {
                "const": "native-ffi"
              },
              "environment": {
                "type": "object"
              },
              "loadHandleMs": {
                "type": "number",
                "minimum": 0
              },
              "worldHash": {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              },
              "peakProcessRssBytes": {
                "type": "integer",
                "minimum": 0
              },
              "memoryScope": {
                "type": "string"
              },
              "expectationCheckerHash": {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              },
              "scenarios": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    },
                    "suite": {
                      "type": "string"
                    },
                    "testIndex": {
                      "type": "integer",
                      "minimum": 0
                    },
                    "reason": {
                      "type": "string"
                    },
                    "requestHashes": {
                      "type": "array",
                      "items": {
                        "type": "string",
                        "pattern": "^sha256:[0-9a-f]{64}$"
                      }
                    },
                    "testHash": {
                      "type": "string",
                      "pattern": "^sha256:[0-9a-f]{64}$"
                    },
                    "inputFacts": {
                      "type": "array",
                      "items": {
                        "type": "integer",
                        "minimum": 0
                      }
                    },
                    "firstObservedCallsMs": {
                      "type": "array",
                      "items": {
                        "type": "number",
                        "minimum": 0
                      }
                    },
                    "wallMs": {
                      "type": "object",
                      "properties": {
                        "n": {
                          "type": "integer",
                          "minimum": 1
                        },
                        "p50": {
                          "type": "number",
                          "minimum": 0
                        },
                        "p95": {
                          "type": "number",
                          "minimum": 0
                        }
                      },
                      "required": [
                        "n",
                        "p50",
                        "p95"
                      ],
                      "additionalProperties": false
                    },
                    "cpuMs": {
                      "type": "object",
                      "properties": {
                        "n": {
                          "type": "integer",
                          "minimum": 1
                        },
                        "p50": {
                          "type": "number",
                          "minimum": 0
                        },
                        "p95": {
                          "type": "number",
                          "minimum": 0
                        }
                      },
                      "required": [
                        "n",
                        "p50",
                        "p95"
                      ],
                      "additionalProperties": false
                    },
                    "raw": {
                      "type": "object",
                      "properties": {
                        "wallMs": {
                          "type": "array",
                          "items": {
                            "type": "number",
                            "minimum": 0
                          }
                        },
                        "cpuMs": {
                          "type": "array",
                          "items": {
                            "type": "number",
                            "minimum": 0
                          }
                        },
                        "responseBytes": {
                          "type": "array",
                          "items": {
                            "type": "integer",
                            "minimum": 0
                          }
                        },
                        "proofBytes": {
                          "type": "array",
                          "items": {
                            "type": "integer",
                            "minimum": 0
                          }
                        }
                      },
                      "required": [
                        "wallMs",
                        "cpuMs",
                        "responseBytes",
                        "proofBytes"
                      ],
                      "additionalProperties": false
                    },
                    "responseBytes": {
                      "type": "object",
                      "properties": {
                        "n": {
                          "type": "integer",
                          "minimum": 1
                        },
                        "p50": {
                          "type": "number",
                          "minimum": 0
                        },
                        "p95": {
                          "type": "number",
                          "minimum": 0
                        }
                      },
                      "required": [
                        "n",
                        "p50",
                        "p95"
                      ],
                      "additionalProperties": false
                    },
                    "proofBytes": {
                      "type": "object",
                      "properties": {
                        "n": {
                          "type": "integer",
                          "minimum": 1
                        },
                        "p50": {
                          "type": "number",
                          "minimum": 0
                        },
                        "p95": {
                          "type": "number",
                          "minimum": 0
                        }
                      },
                      "required": [
                        "n",
                        "p50",
                        "p95"
                      ],
                      "additionalProperties": false
                    },
                    "outcome": {
                      "const": "passed"
                    },
                    "successfulEvaluations": {
                      "type": "integer",
                      "minimum": 1
                    },
                    "failedEvaluations": {
                      "const": 0
                    },
                    "serialMeasuredCallsPerSecond": {
                      "type": "number",
                      "minimum": 0
                    }
                  },
                  "required": [
                    "id",
                    "title",
                    "suite",
                    "testIndex",
                    "reason",
                    "requestHashes",
                    "testHash",
                    "inputFacts",
                    "firstObservedCallsMs",
                    "wallMs",
                    "cpuMs",
                    "raw",
                    "responseBytes",
                    "proofBytes",
                    "outcome",
                    "successfulEvaluations",
                    "failedEvaluations"
                  ],
                  "additionalProperties": false
                }
              }
            },
            "required": [
              "format",
              "adapter",
              "environment",
              "loadHandleMs",
              "worldHash",
              "peakProcessRssBytes",
              "memoryScope",
              "expectationCheckerHash",
              "scenarios"
            ],
            "additionalProperties": false
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "formalization"
          },
          "outcome": {
            "const": "passed"
          }
        }
      },
      "then": {
        "properties": {
          "data": {
            "$ref": "#/$defs/FormalizationMeasure"
          }
        }
      }
    }
  ],
  "$defs": {
    "FormalizationMeasure": {
      "description": "Formalization measure document: levels over pinned publication units, aggregated units, text coverage, freshness. Shares are integer numerator/denominator pairs; floats are forbidden by the canon. Byte coverage is reported by the measure-mode run of the command line.",
      "$comment": "SPEC §33.1; errata E-0249; DECISION-0383; SPEC §208",
      "type": "object",
      "properties": {
        "format": {
          "const": "law.formalization-measure/0.1"
        },
        "spec": {
          "type": "string"
        },
        "algorithm": {
          "type": "string"
        },
        "profile": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "aggregatedMinBytes": {
              "type": "integer",
              "minimum": 0
            }
          },
          "required": [
            "name",
            "aggregatedMinBytes"
          ],
          "additionalProperties": false
        },
        "package": {
          "type": "object",
          "properties": {
            "name": {
              "type": "string"
            },
            "namespace": {
              "type": "string"
            },
            "artifactHash": {
              "type": "string",
              "pattern": "^sha256:[0-9a-f]{64}$"
            }
          },
          "required": [
            "name",
            "namespace"
          ],
          "additionalProperties": false
        },
        "levelsComputed": {
          "type": "array",
          "items": {
            "enum": [
              "SOURCE_ONLY",
              "ANCHORED",
              "EXECUTABLE"
            ]
          }
        },
        "levelsNotComputed": {
          "type": "array",
          "items": {
            "enum": [
              "STRUCTURED",
              "SEMANTIC",
              "VERIFIED"
            ]
          }
        },
        "freshness": {
          "type": "object",
          "properties": {
            "status": {
              "enum": [
                "verified",
                "unverified"
              ]
            },
            "origin": {
              "enum": [
                "sources",
                "file",
                "none"
              ]
            },
            "reason": {
              "type": "string"
            }
          },
          "required": [
            "status",
            "origin"
          ],
          "additionalProperties": false
        },
        "documents": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/FormalizationDocument"
          }
        },
        "documentsWithoutText": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "publication": {
                "type": "string"
              },
              "documentHash": {
                "type": "string"
              }
            },
            "required": [
              "publication",
              "documentHash"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "format",
        "spec",
        "algorithm",
        "profile",
        "package",
        "levelsComputed",
        "levelsNotComputed",
        "freshness",
        "documents",
        "documentsWithoutText"
      ],
      "additionalProperties": false
    },
    "FormalizationDocument": {
      "type": "object",
      "properties": {
        "publication": {
          "type": "string"
        },
        "edition": {
          "type": "string"
        },
        "documentHash": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "status": {
          "enum": [
            "measured",
            "undefined"
          ]
        },
        "reason": {
          "type": "string"
        },
        "unit": {
          "type": "object",
          "properties": {
            "path": {
              "enum": [
                "declared",
                "recognized",
                "unsupported",
                "none"
              ]
            },
            "convention": {
              "type": "string"
            },
            "locator": {
              "type": "string"
            },
            "name": {
              "type": "string"
            }
          },
          "required": [
            "path"
          ],
          "additionalProperties": false
        },
        "textCoverage": {
          "type": "object",
          "properties": {
            "covered": {
              "type": "integer",
              "minimum": 0
            },
            "total": {
              "type": "integer",
              "minimum": 0
            },
            "fragments": {
              "type": "integer",
              "minimum": 0
            }
          },
          "required": [
            "covered",
            "total",
            "fragments"
          ],
          "additionalProperties": false
        },
        "foreignFragments": {
          "type": "integer",
          "minimum": 0
        },
        "totals": {
          "type": "object",
          "properties": {
            "units": {
              "type": "integer"
            },
            "measured": {
              "type": "integer"
            },
            "excluded": {
              "type": "integer"
            }
          },
          "required": [
            "units",
            "measured",
            "excluded"
          ],
          "additionalProperties": false
        },
        "levels": {
          "type": "object",
          "properties": {
            "EXECUTABLE": {
              "type": "integer"
            },
            "ANCHORED": {
              "type": "integer"
            },
            "SOURCE_ONLY": {
              "type": "integer"
            },
            "NONE": {
              "type": "integer"
            }
          },
          "required": [
            "EXECUTABLE",
            "ANCHORED",
            "SOURCE_ONLY",
            "NONE"
          ],
          "additionalProperties": false
        },
        "readingOnlyUnits": {
          "type": "integer",
          "minimum": 0
        },
        "aggregated": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "unit": {
                "type": "string"
              },
              "bytes": {
                "type": "integer"
              },
              "derivingNodes": {
                "type": "integer"
              }
            },
            "required": [
              "unit",
              "bytes",
              "derivingNodes"
            ],
            "additionalProperties": false
          }
        },
        "excluded": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "unit": {
                "type": "string"
              },
              "bytes": {
                "type": "integer"
              }
            },
            "required": [
              "unit",
              "bytes"
            ],
            "additionalProperties": false
          }
        },
        "declaredOutOfScope": {
          "type": "integer",
          "minimum": 0
        },
        "unresolvedAnchors": {
          "type": "integer",
          "minimum": 0
        },
        "problems": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "code": {
                "enum": [
                  "SOURCE_ONLY_UNEXPLAINED",
                  "SCOPE_UNIT_MISSING",
                  "DECLARED_UNIT_MISSING"
                ]
              },
              "unit": {
                "type": "string"
              }
            },
            "required": [
              "code",
              "unit"
            ],
            "additionalProperties": false
          }
        },
        "units": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "unit": {
                "type": "string"
              },
              "bytes": {
                "type": "integer"
              },
              "level": {
                "enum": [
                  "EXECUTABLE",
                  "ANCHORED",
                  "SOURCE_ONLY",
                  "NONE"
                ]
              },
              "deriving": {
                "type": "integer"
              },
              "addressed": {
                "type": "integer"
              },
              "readingOnly": {
                "type": "integer"
              },
              "anchoring": {
                "type": "integer"
              },
              "aggregated": {
                "type": "boolean"
              }
            },
            "required": [
              "unit",
              "bytes",
              "level",
              "deriving",
              "addressed",
              "readingOnly",
              "anchoring",
              "aggregated"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "publication",
        "edition",
        "documentHash",
        "status",
        "unit",
        "textCoverage",
        "foreignFragments"
      ],
      "additionalProperties": false
    }
  }
}
