{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.hyperbook.com/reproducibility_contract.v0.schema.json",
  "title": "Reproducibility Contract v0",
  "description": "Evidence, execution conditions, and review status. Validation does not itself establish scientific or engineering correctness.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contract_version",
    "scope",
    "artifacts",
    "measurement",
    "environment",
    "inputs",
    "acceptance",
    "execution",
    "review"
  ],
  "properties": {
    "contract_version": {
      "const": "0"
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "system",
        "claims",
        "excluded_claims"
      ],
      "properties": {
        "system": {
          "type": "string",
          "minLength": 1
        },
        "claims": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "excluded_claims": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "artifacts": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/artifact"
      }
    },
    "measurement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "coordinate_frame",
        "units",
        "sampling",
        "metrics"
      ],
      "properties": {
        "coordinate_frame": {
          "type": "string",
          "minLength": 1
        },
        "units": {
          "type": "string",
          "minLength": 1
        },
        "sampling": {
          "type": "string",
          "minLength": 1
        },
        "metrics": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "name",
              "definition",
              "denominator"
            ],
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1
              },
              "definition": {
                "type": "string",
                "minLength": 1
              },
              "denominator": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        }
      }
    },
    "environment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "engine",
        "dependencies",
        "operating_system",
        "hardware",
        "random_seed"
      ],
      "properties": {
        "engine": {
          "type": "string",
          "minLength": 1
        },
        "dependencies": {
          "type": "string",
          "minLength": 1
        },
        "operating_system": {
          "type": "string",
          "minLength": 1
        },
        "hardware": {
          "type": "string",
          "minLength": 1
        },
        "random_seed": {
          "type": [
            "integer",
            "string",
            "null"
          ]
        }
      }
    },
    "inputs": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/input"
      }
    },
    "acceptance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "criteria"
      ],
      "properties": {
        "status": {
          "enum": [
            "not_evaluated",
            "author_verified",
            "independently_verified",
            "failed"
          ]
        },
        "criteria": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "metric",
              "operator",
              "threshold",
              "rationale"
            ],
            "properties": {
              "metric": {
                "type": "string",
                "minLength": 1
              },
              "operator": {
                "enum": [
                  "<",
                  "<=",
                  "=",
                  ">=",
                  ">"
                ]
              },
              "threshold": {
                "type": "number"
              },
              "rationale": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "command",
        "expected_result",
        "fresh_log_artifact",
        "negative_controls"
      ],
      "properties": {
        "command": {
          "type": "string",
          "minLength": 1
        },
        "expected_result": {
          "type": "string",
          "minLength": 1
        },
        "fresh_log_artifact": {
          "$ref": "#/$defs/input"
        },
        "negative_controls": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "review": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "records"
      ],
      "properties": {
        "records": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "reviewer_role",
              "reviewer",
              "reviewed_at",
              "result",
              "notes",
              "verified_commit"
            ],
            "properties": {
              "reviewer_role": {
                "enum": [
                  "author",
                  "independent"
                ]
              },
              "reviewer": {
                "type": "string",
                "minLength": 1
              },
              "reviewed_at": {
                "type": "string",
                "format": "date-time"
              },
              "result": {
                "enum": [
                  "not_run",
                  "passed",
                  "failed",
                  "inconclusive"
                ]
              },
              "notes": {
                "type": "string"
              },
              "verified_commit": {
                "type": "string",
                "pattern": "^[a-f0-9]{7,40}$"
              }
            }
          }
        }
      }
    }
  },
  "$defs": {
    "sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "artifact": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "role",
        "visibility",
        "sha256",
        "byte_size",
        "source_revision"
      ],
      "properties": {
        "role": {
          "enum": [
            "visual_model",
            "collision_model",
            "trajectory",
            "controller",
            "report"
          ]
        },
        "visibility": {
          "enum": [
            "public",
            "restricted"
          ]
        },
        "public_uri": {
          "type": "string",
          "format": "uri"
        },
        "access_method": {
          "type": "string",
          "minLength": 1
        },
        "sha256": {
          "$ref": "#/$defs/sha256"
        },
        "byte_size": {
          "type": "integer",
          "minimum": 0
        },
        "source_revision": {
          "type": "string",
          "minLength": 1
        },
        "visual_collision_mapping": {
          "type": "string"
        }
      },
      "anyOf": [
        {
          "required": [
            "public_uri"
          ]
        },
        {
          "required": [
            "access_method"
          ]
        }
      ]
    },
    "input": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "sha256",
        "byte_size"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "sha256": {
          "$ref": "#/$defs/sha256"
        },
        "byte_size": {
          "type": "integer",
          "minimum": 0
        },
        "uri": {
          "type": "string",
          "format": "uri"
        }
      }
    }
  }
}