{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://inferenslab.org/result-publication-schema.json",
  "title": "InferensLab governed result publication package",
  "description": "Schema for a published InferensLab result package. The schema is a publication contract and is not itself evidence of execution or a result.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "result_id",
    "program_id",
    "protocol",
    "execution_status",
    "results_status",
    "published_on",
    "evidence_state",
    "source_snapshots",
    "unit_accounting",
    "metrics",
    "outcomes",
    "review",
    "limitations",
    "claims_allowed",
    "claims_forbidden",
    "provenance"
  ],
  "properties": {
    "schema_version": {
      "const": "1.0.0"
    },
    "result_id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9-]{2,127}$"
    },
    "program_id": {
      "type": "string",
      "minLength": 1
    },
    "protocol": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "version",
        "artifact",
        "artifact_sha256"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+\\.\\d+$"
        },
        "artifact": {
          "type": "string",
          "pattern": "^/protocols/.+\\.json$"
        },
        "artifact_sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        }
      }
    },
    "execution_status": {
      "enum": [
        "completed",
        "halted"
      ]
    },
    "results_status": {
      "enum": [
        "results-published",
        "null-results-published",
        "contradictory-results-published",
        "non-identifiable-published"
      ]
    },
    "published_on": {
      "type": "string",
      "format": "date"
    },
    "evidence_state": {
      "type": "string",
      "minLength": 1
    },
    "source_snapshots": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id",
          "uri",
          "sha256",
          "captured_at",
          "scope"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "uri": {
            "type": "string",
            "format": "uri-reference"
          },
          "sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "captured_at": {
            "type": "string",
            "format": "date-time"
          },
          "scope": {
            "type": "string"
          }
        }
      }
    },
    "unit_accounting": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "planned",
        "observed",
        "excluded",
        "halted",
        "denominator_complete"
      ],
      "properties": {
        "planned": {
          "type": "integer",
          "minimum": 1
        },
        "observed": {
          "type": "integer",
          "minimum": 0
        },
        "excluded": {
          "type": "integer",
          "minimum": 0
        },
        "halted": {
          "type": "integer",
          "minimum": 0
        },
        "denominator_complete": {
          "type": "boolean"
        },
        "exclusion_reasons": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "stopping_rule_applied": {
          "type": "string"
        }
      }
    },
    "metrics": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id",
          "value",
          "denominator",
          "interpretation"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "value": {
            "type": [
              "number",
              "integer",
              "string",
              "null"
            ]
          },
          "denominator": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 0
          },
          "uncertainty": {
            "type": [
              "object",
              "null"
            ]
          },
          "interpretation": {
            "type": "string"
          },
          "status": {
            "enum": [
              "observed",
              "null",
              "contradictory",
              "not-identifiable"
            ]
          }
        }
      }
    },
    "outcomes": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "id",
          "status",
          "statement",
          "evidence_refs"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "status": {
            "enum": [
              "supported",
              "not-supported",
              "null",
              "contradictory",
              "not-identifiable"
            ]
          },
          "statement": {
            "type": "string"
          },
          "evidence_refs": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "causal_claim": {
            "type": "boolean",
            "default": false
          },
          "counterfactual_ref": {
            "type": [
              "string",
              "null"
            ]
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "causal_claim": {
                  "const": true
                }
              },
              "required": [
                "causal_claim"
              ]
            },
            "then": {
              "required": [
                "counterfactual_ref"
              ],
              "properties": {
                "counterfactual_ref": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          }
        ]
      }
    },
    "review": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "reviewer_count",
        "rubric_version",
        "disagreements_published"
      ],
      "properties": {
        "reviewer_count": {
          "type": "integer",
          "minimum": 1
        },
        "rubric_version": {
          "type": "string"
        },
        "agreement_metric": {
          "type": [
            "string",
            "null"
          ]
        },
        "agreement_value": {
          "type": [
            "number",
            "null"
          ]
        },
        "agreement_threshold": {
          "type": [
            "number",
            "null"
          ]
        },
        "disagreements_published": {
          "type": "boolean"
        },
        "adjudication_policy": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    },
    "limitations": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string"
      }
    },
    "claims_allowed": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "claims_forbidden": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string"
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "evidence_bundle_uri",
        "evidence_bundle_sha256",
        "generated_from",
        "code_commit",
        "environment"
      ],
      "properties": {
        "evidence_bundle_uri": {
          "type": "string",
          "format": "uri-reference",
          "description": "URI of the immutable evidence bundle or manifest whose bytes are hashed below."
        },
        "evidence_bundle_sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$",
          "description": "SHA-256 of the external immutable evidence bundle or manifest. This avoids a self-referential hash inside result.json."
        },
        "generated_from": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string"
          }
        },
        "code_commit": {
          "type": "string",
          "pattern": "^[0-9a-f]{40}$"
        },
        "environment": {
          "type": "string"
        },
        "deployment_attestation": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    }
  }
}
