{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/BichengWang/ai-decision-system/schema/jevdecision/answer.v2.json",
  "title": "jevdecision answer record, schema 2",
  "$comment": "Canonicalization: RFC 8785 (JCS). record_sha256 is the lowercase hex SHA-256 of the RFC 8785 canonical form of this whole record without its record_sha256 member, so it covers every other field. Readers reject duplicate object keys. Integers beyond 2^53-1 and non-finite numbers cannot be recorded. jev-decision-verify re-parses `response` with `questions`, compares `answers`, `id`, `usage` and `model`, and recomputes the digest; the schema checks shape only.",
  "type": "object",
  "properties": {
    "schema_version": {
      "const": 2
    },
    "produced_by": {
      "$ref": "#/$defs/producedBy"
    },
    "backend": {
      "enum": [
        "jev",
        "openai"
      ]
    },
    "model": {
      "description": "The response's own model field when it has one, otherwise the requested model; any JSON value is passed through."
    },
    "id": {
      "description": "The response's own id field (any JSON value), or null."
    },
    "usage": {
      "type": "object"
    },
    "answers": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/answer"
      }
    },
    "questions": {
      "type": "object",
      "minProperties": 1,
      "propertyNames": {
        "pattern": "^[A-Za-z_][A-Za-z0-9_-]{0,63}$"
      },
      "additionalProperties": {
        "$ref": "#/$defs/question"
      }
    },
    "response": {
      "type": "object",
      "description": "The raw provider response as received and parsed from JSON."
    },
    "record_sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    }
  },
  "required": [
    "schema_version",
    "produced_by",
    "backend",
    "model",
    "id",
    "usage",
    "answers",
    "questions",
    "response",
    "record_sha256"
  ],
  "additionalProperties": false,
  "$defs": {
    "probability": {
      "type": "number",
      "minimum": 0,
      "maximum": 1
    },
    "nullableProbability": {
      "type": [
        "number",
        "null"
      ],
      "minimum": 0,
      "maximum": 1
    },
    "answer": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "refusal"
            }
          },
          "required": [
            "type"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "noul"
            },
            "probability": {
              "$ref": "#/$defs/probability"
            }
          },
          "required": [
            "type",
            "probability"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "choice"
            },
            "choice": {
              "type": "string"
            },
            "probabilities": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/probability"
              }
            },
            "confidence": {
              "$ref": "#/$defs/nullableProbability"
            }
          },
          "required": [
            "type",
            "choice",
            "probabilities",
            "confidence"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "score"
            },
            "score": {
              "type": "number",
              "minimum": 0
            },
            "level": {
              "type": "string"
            },
            "levels": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "minItems": 2,
              "maxItems": 10
            },
            "probabilities": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/nullableProbability"
              }
            },
            "confidence": {
              "$ref": "#/$defs/nullableProbability"
            }
          },
          "required": [
            "type",
            "score",
            "level",
            "levels",
            "probabilities",
            "confidence"
          ],
          "additionalProperties": false
        }
      ]
    },
    "producedBy": {
      "type": "object",
      "properties": {
        "name": {
          "const": "jevdecision"
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "name",
        "version"
      ],
      "additionalProperties": false
    },
    "text": {
      "type": "string",
      "pattern": "\\S"
    },
    "question": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "noul"
            },
            "instructions": {
              "$ref": "#/$defs/text"
            }
          },
          "required": [
            "type",
            "instructions"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "choice"
            },
            "instructions": {
              "$ref": "#/$defs/text"
            },
            "criteria": {
              "type": "object",
              "minProperties": 1,
              "maxProperties": 255,
              "propertyNames": {
                "$ref": "#/$defs/text"
              },
              "additionalProperties": {
                "$ref": "#/$defs/text"
              }
            }
          },
          "required": [
            "type",
            "instructions",
            "criteria"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "const": "score"
            },
            "instructions": {
              "$ref": "#/$defs/text"
            },
            "criteria": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/text"
              },
              "minItems": 2,
              "maxItems": 10,
              "uniqueItems": true
            }
          },
          "required": [
            "type",
            "instructions",
            "criteria"
          ],
          "additionalProperties": false
        }
      ]
    }
  }
}
