{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://torneo.ai/schemas/decision.v1",
  "title": "decision.v1 envelope",
  "description": "Decision envelope of the decide operation (torneo_platform.decide.decide): applicability of each declared constraint first, then EVIDENCE or ABSTAIN with a stable reason_code from answer_rule.v3. An abstention removes the verdict only: measured evidence, reservations and what is missing stay. rank_policy is always intervals_only.",
  "type": "object",
  "required": [
    "schema",
    "answer_rule_version",
    "applicability",
    "verdict",
    "reason_code",
    "row",
    "category",
    "evidence_id",
    "run_id",
    "observed_at",
    "valid_until",
    "as_of",
    "replacement",
    "evidence",
    "reservations",
    "missing",
    "rank_policy",
    "unique_winner",
    "directive"
  ],
  "additionalProperties": false,
  "properties": {
    "schema": {
      "const": "decision.v1"
    },
    "answer_rule_version": {
      "const": "answer_rule.v3"
    },
    "applicability": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "constraint",
          "declared",
          "status"
        ],
        "additionalProperties": false,
        "properties": {
          "constraint": {
            "type": "string"
          },
          "declared": true,
          "status": {
            "enum": [
              "measured",
              "not_measured",
              "out_of_scope"
            ]
          }
        }
      }
    },
    "verdict": {
      "enum": [
        "ABSTAIN",
        "EVIDENCE"
      ]
    },
    "reason_code": {
      "type": "string",
      "pattern": "^(LIFECYCLE_INVALID|FRESHNESS_UNPROVEN|WITNESS_UNAVAILABLE|NO_EVIDENCE|FIXTURE_NOT_EVIDENCE|REVOKED|SUSPENDED|SUPERSEDED|VERIFY_BLOCKED|STALE|TOO_OLD_FOR_CONSUMER|OUT_OF_SCOPE:.+|NOT_MEASURED:.+|INDETERMINATE|LOCAL_VALIDITY|EVIDENCE)$"
    },
    "row": {
      "enum": [
        "0",
        "0b",
        "0c",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12"
      ]
    },
    "category": {
      "type": "string"
    },
    "evidence_id": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "run_id": {
      "anyOf": [
        {
          "type": "string",
          "minLength": 1
        },
        {
          "type": "null"
        }
      ]
    },
    "observed_at": {
      "type": [
        "string",
        "null"
      ]
    },
    "valid_until": {
      "type": [
        "string",
        "null"
      ]
    },
    "as_of": {
      "type": "string",
      "minLength": 1
    },
    "replacement": {
      "type": [
        "string",
        "null"
      ]
    },
    "evidence": {
      "anyOf": [
        {
          "$ref": "#/$defs/evidence"
        },
        {
          "type": "null"
        }
      ]
    },
    "reservations": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "missing": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "rank_policy": {
      "const": "intervals_only"
    },
    "unique_winner": {
      "type": "boolean"
    },
    "directive": {
      "type": "string",
      "minLength": 1
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "row": {
            "const": "0"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "LIFECYCLE_INVALID"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "0b"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "FRESHNESS_UNPROVEN"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "0c"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "WITNESS_UNAVAILABLE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "1"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "enum": [
              "NO_EVIDENCE",
              "FIXTURE_NOT_EVIDENCE"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "2"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "REVOKED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "3"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "SUSPENDED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "4"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "SUPERSEDED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "5"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "VERIFY_BLOCKED"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "6"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "STALE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "7"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "TOO_OLD_FOR_CONSUMER"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "8"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "type": "string",
            "pattern": "^OUT_OF_SCOPE:.+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "9"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "type": "string",
            "pattern": "^NOT_MEASURED:.+$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "10"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "INDETERMINATE"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "11"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          },
          "reason_code": {
            "const": "LOCAL_VALIDITY"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "12"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "verdict": {
            "const": "EVIDENCE"
          },
          "reason_code": {
            "const": "EVIDENCE"
          },
          "evidence": {
            "type": "object"
          },
          "evidence_id": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "row": {
            "const": "4"
          }
        },
        "required": [
          "row"
        ]
      },
      "then": {
        "properties": {
          "replacement": {
            "type": "string",
            "minLength": 1
          }
        }
      },
      "else": {
        "properties": {
          "replacement": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "verdict": {
            "const": "ABSTAIN"
          }
        },
        "required": [
          "verdict"
        ]
      },
      "then": {
        "properties": {
          "unique_winner": {
            "const": false
          },
          "directive": {
            "const": "Do not order the tools and do not name a winner: there is no current evidence for this category. Report that the evidence is insufficient, and why."
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "reason_code": {
            "const": "NO_EVIDENCE"
          }
        },
        "required": [
          "reason_code"
        ]
      },
      "then": {
        "properties": {
          "evidence": {
            "type": "null"
          },
          "evidence_id": {
            "type": "null"
          },
          "run_id": {
            "type": "null"
          }
        }
      }
    }
  ],
  "$defs": {
    "evidence": {
      "type": "object",
      "required": [
        "evidence_id",
        "result_sha256",
        "run_id",
        "category",
        "status",
        "observed_at",
        "valid_until",
        "freshness",
        "verification",
        "lifecycle",
        "replacement",
        "participants",
        "limits",
        "evidence_uri",
        "reproduce_cmd"
      ],
      "additionalProperties": false,
      "properties": {
        "evidence_id": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "result_sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "run_id": {
          "type": "string",
          "minLength": 1
        },
        "category": {
          "type": [
            "string",
            "null"
          ]
        },
        "status": {
          "type": [
            "string",
            "null"
          ]
        },
        "observed_at": {
          "type": [
            "string",
            "null"
          ]
        },
        "valid_until": {
          "type": [
            "string",
            "null"
          ]
        },
        "freshness": {
          "enum": [
            "CURRENT",
            "STALE",
            "SUPERSEDED",
            null
          ]
        },
        "verification": {
          "type": [
            "string",
            "null"
          ]
        },
        "lifecycle": {
          "enum": [
            "ACTIVE",
            "REVOKED",
            "SUSPENDED",
            "SUPERSEDED"
          ]
        },
        "replacement": {
          "type": [
            "string",
            "null"
          ]
        },
        "participants": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name",
              "vendor",
              "status",
              "estimate",
              "interval",
              "n"
            ],
            "additionalProperties": false,
            "properties": {
              "name": {
                "type": "string"
              },
              "vendor": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "status": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "estimate": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "interval": {
                "anyOf": [
                  {
                    "type": "object",
                    "required": [
                      "low",
                      "high",
                      "confidence",
                      "method"
                    ],
                    "properties": {
                      "low": {
                        "type": "number",
                        "minimum": 0
                      },
                      "high": {
                        "type": "number",
                        "minimum": 0
                      },
                      "confidence": {
                        "type": "number"
                      },
                      "method": {
                        "type": "string"
                      }
                    }
                  },
                  {
                    "type": "null"
                  }
                ]
              },
              "n": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "rank": {
                "type": "object",
                "required": [
                  "best",
                  "worst"
                ],
                "additionalProperties": false,
                "properties": {
                  "best": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "worst": {
                    "type": "integer",
                    "minimum": 1
                  }
                }
              }
            }
          }
        },
        "limits": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "evidence_uri": {
          "type": [
            "string",
            "null"
          ]
        },
        "reproduce_cmd": {
          "type": [
            "string",
            "null"
          ]
        },
        "attestations": {
          "type": "array",
          "description": "Absent when the surface did not consult the attestations (evidence.summarize); shown, never changes the verdict.",
          "items": {
            "type": "object",
            "required": [
              "attestation_id",
              "verdict",
              "reasons",
              "claim",
              "key_id",
              "valid_from",
              "valid_until"
            ],
            "additionalProperties": false,
            "properties": {
              "attestation_id": {
                "type": "string",
                "pattern": "^[0-9a-f]{64}$"
              },
              "verdict": {
                "type": "string",
                "minLength": 1
              },
              "reasons": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "claim": true,
              "key_id": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "valid_from": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "valid_until": {
                "type": [
                  "string",
                  "null"
                ]
              }
            }
          }
        }
      }
    }
  }
}
