{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://muthu4all.github.io/proven-autonomy/spec/schemas/promotion-decision.schema.json",
  "title": "Proven Autonomy · Promotion Decision",
  "description": "A change to the level an agent holds for one action class in one context: a promotion recommended by the Promotion Board and signed by named people, an automatic demotion fired by a trigger, a version reset, or a freeze. Draft v0.1.",
  "type": "object",
  "required": ["spec", "decision_id", "kind", "action_class", "context", "agent", "from_level", "to_level", "evidence", "recommended_by", "effective_from", "reason", "policy_version"],
  "properties": {
    "spec": { "const": "proven-autonomy/promotion-decision/v0.1" },
    "decision_id": { "type": "string" },
    "kind": { "enum": ["promotion", "demotion", "version-reset", "freeze", "unfreeze"] },
    "action_class": { "type": "string" },
    "context": { "type": "string" },
    "agent": {
      "type": "object",
      "required": ["id", "model_version", "prompt_version"],
      "properties": {
        "id": { "type": "string" },
        "model_version": { "type": "string" },
        "prompt_version": { "type": "string" },
        "tool_versions": { "type": "object", "additionalProperties": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "from_level": { "$ref": "#/$defs/level" },
    "to_level": { "$ref": "#/$defs/level" },
    "evidence": {
      "type": "object",
      "required": ["window", "actions_settled"],
      "properties": {
        "window": {
          "type": "object", "required": ["from", "to"],
          "properties": { "from": { "type": "string", "format": "date" }, "to": { "type": "string", "format": "date" } },
          "additionalProperties": false
        },
        "actions_settled": { "type": "integer", "minimum": 0, "description": "Actions whose verification window has closed." },
        "agreement_rate": { "type": "number", "minimum": 0, "maximum": 1 },
        "edit_rate": { "type": "number", "minimum": 0, "maximum": 1 },
        "verified_good_rate": { "type": "number", "minimum": 0, "maximum": 1 },
        "severity_weighted_incidents": { "type": "number", "minimum": 0 },
        "drift": { "type": "number", "minimum": 0, "maximum": 1 },
        "review_coverage": { "type": "number", "minimum": 0, "maximum": 1 },
        "median_review_seconds": { "type": "number", "minimum": 0 },
        "triggering_event": { "type": "string", "description": "event_id that fired an automatic demotion." },
        "ledger_query_ref": { "type": "string", "description": "A replayable query over the ledger that reproduces these numbers." }
      },
      "additionalProperties": false
    },
    "recommended_by": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "enum": ["promotion-board", "automatic-trigger", "version-change", "person"] },
        "trigger_id": { "type": "string" },
        "board_run": { "type": "string" }
      },
      "additionalProperties": false
    },
    "automatic": { "type": "boolean" },
    "approved_by": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["name", "role", "at"],
        "properties": {
          "name": { "type": "string" },
          "role": { "type": "string" },
          "at": { "type": "string", "format": "date-time" }
        },
        "additionalProperties": false
      }
    },
    "effective_from": { "type": "string", "format": "date-time" },
    "review_by": { "type": "string", "format": "date", "description": "When people must look at an automatic demotion. The demotion stays in force until evidence earns the level back." },
    "reason": { "type": "string" },
    "envelope_change": { "type": "string", "description": "Plain-language summary of how the envelope widened or narrowed." },
    "policy_version": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$" }
  },
  "allOf": [
    {
      "if": { "properties": { "kind": { "const": "promotion" } } },
      "then": {
        "required": ["approved_by", "automatic"],
        "properties": {
          "automatic": { "const": false },
          "approved_by": { "minItems": 1 },
          "recommended_by": { "properties": { "kind": { "const": "promotion-board" } } }
        },
        "oneOf": [
          { "properties": { "from_level": { "const": "L0" }, "to_level": { "const": "L1" } } },
          { "properties": { "from_level": { "const": "L1" }, "to_level": { "const": "L2" } } },
          { "properties": { "from_level": { "const": "L2" }, "to_level": { "const": "L3" } } },
          { "properties": { "from_level": { "const": "L3" }, "to_level": { "const": "L4" } } },
          { "properties": { "from_level": { "const": "L4" }, "to_level": { "const": "L5" } } }
        ]
      }
    },
    {
      "if": { "properties": { "kind": { "const": "promotion" }, "to_level": { "enum": ["L3", "L4", "L5"] } }, "required": ["kind", "to_level"] },
      "then": { "properties": { "approved_by": { "minItems": 2 } } }
    },
    {
      "if": { "properties": { "kind": { "const": "demotion" } } },
      "then": {
        "required": ["automatic"],
        "not": {
          "anyOf": [
            { "properties": { "from_level": { "const": "L0" } } },
            { "properties": { "from_level": { "const": "L1" }, "to_level": { "enum": ["L1", "L2", "L3", "L4", "L5"] } } },
            { "properties": { "from_level": { "const": "L2" }, "to_level": { "enum": ["L2", "L3", "L4", "L5"] } } },
            { "properties": { "from_level": { "const": "L3" }, "to_level": { "enum": ["L3", "L4", "L5"] } } },
            { "properties": { "from_level": { "const": "L4" }, "to_level": { "enum": ["L4", "L5"] } } },
            { "properties": { "from_level": { "const": "L5" }, "to_level": { "const": "L5" } } }
          ]
        }
      }
    },
    {
      "if": { "properties": { "kind": { "const": "demotion" }, "automatic": { "const": true } }, "required": ["kind", "automatic"] },
      "then": {
        "required": ["review_by"],
        "properties": {
          "recommended_by": { "properties": { "kind": { "enum": ["automatic-trigger", "version-change"] } }, "required": ["kind", "trigger_id"] },
          "evidence": { "required": ["window", "actions_settled", "triggering_event"] }
        }
      }
    }
  ],
  "$defs": {
    "level": { "enum": ["L0", "L1", "L2", "L3", "L4", "L5"] }
  },
  "additionalProperties": false
}
