{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/devlollc/gladiator/schemas/result.v1.json",
  "title": "GLADIATOR canonical contest result, v1",
  "description": "One contest run of one category. Never 'the best tool': the best observed evidence for a task, a context and a date, with interval, cost, conflicts and limits. A rank without an interval is invalid by construction.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "run_id",
    "category",
    "promise",
    "task",
    "population",
    "context",
    "observed_at",
    "protocol_version",
    "protocol_lock_sha256",
    "status",
    "blocks_replicated",
    "participants",
    "runner",
    "funding",
    "conflicts",
    "limits",
    "valid_until",
    "evidence_uri",
    "raw_data_sha256",
    "reproduce_cmd",
    "result_sha256"
  ],
  "properties": {
    "directive": {
      "type": "string",
      "minLength": 1,
      "description": "TORNEO-008: the instruction a consumer must follow, served FIRST, before any number. Says whether a winner may be named and what to report instead. A reader that truncates the payload still gets the rule."
    },
    "objective_and_population": {
      "type": "string",
      "minLength": 1,
      "description": "TORNEO-008: what was measured, on which frozen population, at which date. Served SECOND, so scope arrives before any estimate."
    },
    "metric": {
      "type": "string",
      "minLength": 1,
      "description": "Exact name of the primary metric, from contest.yaml metrics.primary."
    },
    "metric_plain_label": {
      "type": "string",
      "minLength": 1,
      "description": "Plain-language subtitle of the primary metric (TORNEO-005 vocabulary), served immediately under `metric`. The exact name is never served alone."
    },
    "schema_version": {
      "const": "result.v1"
    },
    "run_id": {
      "type": "string",
      "pattern": "^[A-Z0-9][A-Z0-9-]{2,63}$"
    },
    "category": {
      "type": "string",
      "minLength": 1
    },
    "promise": {
      "type": "string",
      "minLength": 1,
      "description": "The goal the tools themselves promise, in one sentence."
    },
    "task": {
      "type": "string",
      "minLength": 1
    },
    "population": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "description",
        "size",
        "frozen_at",
        "contains_personal_data"
      ],
      "properties": {
        "description": {
          "type": "string",
          "minLength": 1
        },
        "size": {
          "type": "integer",
          "minimum": 1
        },
        "frozen_at": {
          "$ref": "#/$defs/utc_datetime"
        },
        "contains_personal_data": {
          "type": "boolean"
        }
      }
    },
    "context": {
      "type": "string",
      "minLength": 1
    },
    "observed_at": {
      "$ref": "#/$defs/utc_datetime"
    },
    "protocol_version": {
      "type": "string",
      "minLength": 1
    },
    "protocol_lock_sha256": {
      "$ref": "#/$defs/sha256"
    },
    "status": {
      "enum": [
        "OK",
        "LOCAL_VALIDITY",
        "INDETERMINATE"
      ],
      "description": "OK: replicated ranking. LOCAL_VALIDITY: single block, no current rank claim. INDETERMINATE: precision insufficient, no rank published."
    },
    "blocks_replicated": {
      "type": "integer",
      "minimum": 0
    },
    "participants": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/participant"
      }
    },
    "runner": {
      "type": "string",
      "minLength": 1
    },
    "funding": {
      "type": "string",
      "minLength": 1
    },
    "conflicts": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "limits": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "valid_until": {
      "$ref": "#/$defs/utc_datetime"
    },
    "superseded_by": {
      "type": [
        "string",
        "null"
      ],
      "description": "run_id of the superseding run, when a fresher replication exists."
    },
    "evidence_uri": {
      "type": "string",
      "minLength": 1
    },
    "raw_data_sha256": {
      "$ref": "#/$defs/sha256"
    },
    "reproduce_cmd": {
      "type": "string",
      "minLength": 1
    },
    "result_sha256": {
      "$ref": "#/$defs/sha256"
    },
    "license": {
      "const": "CC-BY-4.0",
      "description": "TORNEO-019: applies only to published measurements and rankings, with third-party archives excluded as stated in attribution."
    },
    "citation": {
      "type": "string",
      "minLength": 1,
      "description": "Derived run, observation date, status, original scope and canonical result URL; no new scientific validity."
    },
    "attribution": {
      "const": "TORNEO. CC-BY-4.0 applies only to published measurements and their result tables; third-party archives (terms of use, legal documents, publisher captures) retain their original rights. Licensing does not establish publication or scientific validity."
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "const": "INDETERMINATE"
          }
        }
      },
      "then": {
        "properties": {
          "participants": {
            "items": {
              "not": {
                "required": [
                  "rank"
                ]
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "utc_datetime": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "interval": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "low",
        "high",
        "confidence",
        "method"
      ],
      "properties": {
        "low": {
          "type": "number",
          "minimum": 0
        },
        "high": {
          "type": "number",
          "minimum": 0
        },
        "confidence": {
          "type": "number",
          "exclusiveMinimum": 0,
          "exclusiveMaximum": 1
        },
        "method": {
          "enum": [
            "wilson",
            "bootstrap_percentile"
          ],
          "description": "wilson: closed-form interval for an independent-success proportion (e.g. found-and-verified rate). bootstrap_percentile: percentile bootstrap for ratio/weighted metrics (e.g. micro-averaged WER), resampled by declared unit; requires method_params with the frozen sample count and seed so a third party can recompute the exact interval."
        },
        "method_params": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "bootstrap_samples": {
              "type": "integer",
              "minimum": 100
            },
            "seed": {
              "type": [
                "integer",
                "string"
              ]
            },
            "resample_unit": {
              "type": "string"
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "method": {
                "const": "bootstrap_percentile"
              }
            }
          },
          "then": {
            "required": [
              "method_params"
            ],
            "properties": {
              "method_params": {
                "required": [
                  "bootstrap_samples",
                  "seed"
                ]
              }
            }
          }
        }
      ]
    },
    "outcome": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "estimate",
        "n"
      ],
      "properties": {
        "estimate": {
          "type": "number",
          "minimum": 0
        },
        "interval": {
          "$ref": "#/$defs/interval"
        },
        "n": {
          "type": "integer",
          "minimum": 0
        },
        "successes": {
          "type": "integer",
          "minimum": 0
        },
        "unit": {
          "type": "string"
        }
      }
    },
    "participant": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "status"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9_-]*$"
        },
        "vendor": {
          "type": "string"
        },
        "status": {
          "enum": [
            "OK",
            "INDETERMINATE",
            "EXCLUDED",
            "INSUFFICIENT_EVIDENCE"
          ]
        },
        "status_reason": {
          "type": "string"
        },
        "outcome": {
          "$ref": "#/$defs/outcome"
        },
        "rank": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "best",
            "worst"
          ],
          "properties": {
            "best": {
              "type": "integer",
              "minimum": 1
            },
            "worst": {
              "type": "integer",
              "minimum": 1
            }
          }
        },
        "blocks_observed": {
          "type": "integer",
          "minimum": 0
        },
        "cost": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "total_usd": {
              "type": "number",
              "minimum": 0
            },
            "per_unit_usd": {
              "type": [
                "number",
                "null"
              ],
              "minimum": 0
            },
            "unit": {
              "type": "string"
            }
          }
        },
        "latency": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "p50_ms": {
              "type": "number",
              "minimum": 0
            },
            "p95_ms": {
              "type": "number",
              "minimum": 0
            }
          }
        },
        "reliability": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "error_rate": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "incidents": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "access": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "api": {
              "type": "boolean"
            },
            "cli": {
              "type": "boolean"
            },
            "mcp": {
              "type": "boolean"
            },
            "auth": {
              "type": "string"
            },
            "setup_minutes": {
              "type": "number",
              "minimum": 0
            }
          }
        },
        "constraints": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "tos_benchmark_clause": {
              "type": "boolean"
            },
            "data_residency": {
              "type": "string"
            },
            "rate_limits": {
              "type": "string"
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "required": [
              "rank"
            ]
          },
          "then": {
            "required": [
              "outcome"
            ],
            "properties": {
              "outcome": {
                "required": [
                  "estimate",
                  "interval",
                  "n"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "OK"
              }
            }
          },
          "then": {
            "required": [
              "outcome"
            ],
            "properties": {
              "outcome": {
                "required": [
                  "estimate",
                  "interval",
                  "n"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "enum": [
                  "INDETERMINATE",
                  "EXCLUDED",
                  "INSUFFICIENT_EVIDENCE"
                ]
              }
            }
          },
          "then": {
            "not": {
              "required": [
                "rank"
              ]
            }
          }
        }
      ]
    }
  },
  "dependentRequired": {
    "license": [
      "citation",
      "attribution"
    ],
    "citation": [
      "license",
      "attribution"
    ],
    "attribution": [
      "license",
      "citation"
    ]
  }
}
