{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://polaris.local/schemas/answer_key.schema.json",
  "title": "Answer Key (scoring rubric)",
  "description": "SCHEMAS_AND_PROTOCOL.md A2, as resolved in docs/internal/SCHEMA_DECISIONS.md. The spec's field names are used here; on disk an Answer Key stays YAML in the code names (gold, gold_when, question, matches). `scoring`, `content_sha`, `extensions` and the extra criterion scorers are Polaris extensions kept by decisions G1 and G5.",
  "type": "object",
  "additionalProperties": false,
  "required": ["id", "version", "question_text_resolved", "criteria"],
  "properties": {
    "id": {"type": "string", "minLength": 1},
    "question_id": {"type": ["string", "null"]},
    "question_version": {"type": ["integer", "null"], "minimum": 1},
    "version": {"type": "integer", "minimum": 1},
    "binding": {"type": "object"},
    "question_text_resolved": {"type": "string", "minLength": 1},
    "answer": {"type": "string"},
    "criteria": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/criterion"}
    },
    "full_credit_when": {"type": "array", "items": {"type": "string"}},
    "approved_by": {"type": ["string", "null"]},
    "approved_at": {"type": ["string", "null"]},
    "content_sha": {
      "description": "Polaris: the hash approval locked. Null when the Answer Key was never approved.",
      "type": ["string", "null"]
    },
    "supersedes": {"type": ["integer", "null"], "minimum": 1},
    "change_reason": {
      "type": ["object", "null"],
      "additionalProperties": false,
      "required": ["kind"],
      "properties": {
        "kind": {"enum": ["law_changed", "new_adjudication", "scope_refined", "error_corrected"]},
        "note": {"type": "string"},
        "effective_date": {"type": "string"},
        "triggered_by": {"type": "string"}
      }
    },
    "last_scored": {"anyOf": [{"type": "null"}, {"$ref": "#/$defs/last_scored"}]},
    "status": {"enum": ["active", "superseded", "needs_review"]},
    "scoring": {"$ref": "#/$defs/scoring"},
    "extensions": {
      "description": "Polaris: top-level keys of the on-disk Answer Key that A2 has no name for (e.g. a draft's `draft` block). Carried through unchanged so a round trip keeps the approval hash.",
      "type": "object"
    }
  },
  "$defs": {
    "criterion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "tier", "weight"],
      "properties": {
        "key": {"type": "string", "minLength": 1},
        "tier": {"enum": ["machine", "judged", "manual"]},
        "weight": {"type": "integer", "minimum": 0},
        "describe": {"type": "string"},
        "pattern": {"description": "Scored when this regex matches (code name: matches).", "type": "string"},
        "absent": {"description": "Polaris (G5): scored when this regex does NOT match.", "type": "string"},
        "verdict": {"description": "Polaris (G5): scored when the answer's verdict equals this.", "type": "string"},
        "check": {"description": "Polaris (G5): scored by a named code check.", "type": "string"},
        "negate_check": {"type": "boolean"},
        "provisions": {"type": "array", "items": {"type": "string"}}
      },
      "allOf": [
        {
          "if": {"properties": {"tier": {"const": "judged"}}},
          "then": {
            "description": "A judged criterion takes no scorer (DEE-123).",
            "not": {"anyOf": [{"required": ["pattern"]}, {"required": ["absent"]},
                              {"required": ["verdict"]}, {"required": ["check"]}]}
          }
        }
      ]
    },
    "scoring": {
      "description": "Polaris (G1): scoring config A2 dropped. Without it the verdict classifier, the penalty caps and the fabrication check are lost.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "verdicts": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "pattern"],
            "properties": {"name": {"type": "string"}, "pattern": {"type": "string"}}
          }
        },
        "default_verdict": {"type": "string"},
        "refuses": {"type": "string"},
        "penalties": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["cap"],
            "properties": {
              "cap": {"type": "integer", "minimum": 0},
              "verdict": {"type": "string"},
              "check": {"type": "string"},
              "describe": {"type": "string"}
            }
          }
        },
        "report": {"type": "object", "additionalProperties": {"type": "string"}},
        "fabricates": {
          "type": "object",
          "additionalProperties": false,
          "required": ["subject", "claims"],
          "properties": {"subject": {"type": "string"}, "claims": {"type": "string"}}
        }
      }
    },
    "last_scored": {
      "description": "The comparability tuple (A2, completed by G12 with ref and judge_model). Two scores compare only when every field but `at` and `standing` matches.",
      "type": "object",
      "required": ["at", "standing", "brain_version", "model", "seed", "runs", "ref", "judge_model"],
      "properties": {
        "at": {"type": "string"},
        "standing": {"type": ["number", "null"], "minimum": 0, "maximum": 1},
        "brain_version": {"type": "string"},
        "model": {"type": "string"},
        "judge_model": {"type": ["string", "null"]},
        "seed": {"type": ["integer", "string", "null"]},
        "runs": {"type": "integer", "minimum": 0},
        "ref": {"enum": ["draft", "published", "offline"]},
        "answer_key_version": {"type": "integer"},
        "question_version": {"type": ["integer", "null"]}
      }
    }
  }
}
