{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://muthu4all.github.io/proven-autonomy/spec/schemas/autonomy-policy.schema.json",
  "title": "Proven Autonomy · Autonomy Policy",
  "description": "How far an AI agent may act on its own, per action class and per context: the level it holds now, the envelope at each level, the sampling floor, the evidence a promotion needs, what demotes it automatically, and how a new model, prompt or tool version resets trust. Draft v0.1.",
  "type": "object",
  "required": ["spec", "policy", "version", "organisation", "effective_from", "action_classes"],
  "properties": {
    "spec": { "const": "proven-autonomy/autonomy-policy/v0.1" },
    "policy": { "type": "string", "pattern": "^[a-z][a-z0-9.-]{1,80}$" },
    "version": { "$ref": "#/$defs/semver" },
    "organisation": { "type": "string" },
    "effective_from": { "type": "string", "format": "date" },
    "agent": {
      "type": "object",
      "description": "The agent this policy governs. A policy may also be shared by several agents that perform the same action classes.",
      "required": ["id"],
      "properties": {
        "id": { "type": "string" },
        "purpose": { "type": "string" }
      },
      "additionalProperties": false
    },
    "action_classes": {
      "type": "array", "minItems": 1,
      "items": { "$ref": "#/$defs/action_class" }
    },
    "context_transfer": {
      "const": "none-by-default",
      "description": "Trust earned in one context never carries over to another without that context's own evidence."
    }
  },
  "additionalProperties": false,

  "$defs": {
    "semver": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$" },
    "level": { "enum": ["L0", "L1", "L2", "L3", "L4", "L5"] },
    "rate": { "type": "number", "minimum": 0, "maximum": 1 },
    "money": {
      "type": "object", "required": ["amount", "currency"],
      "properties": {
        "amount": { "type": "number", "minimum": 0 },
        "currency": { "type": "string", "pattern": "^[A-Z]{3}$" }
      },
      "additionalProperties": false
    },
    "versions": {
      "type": "object",
      "description": "The exact versions that earned the grant. Trust is held by this combination.",
      "required": ["model", "prompt"],
      "properties": {
        "model": { "type": "string" },
        "prompt": { "type": "string" },
        "tools": { "type": "object", "additionalProperties": { "type": "string" } }
      },
      "additionalProperties": false
    },

    "action_class": {
      "type": "object",
      "required": ["id", "description", "severity", "reversibility", "autonomy_owner", "verification_window_days", "max_level", "contexts", "envelopes", "sampling_floors", "promotion_criteria", "demotion_triggers", "version_reset"],
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]{1,60}$" },
        "description": { "type": "string" },
        "severity": { "enum": ["low", "medium", "high", "critical"], "description": "Worst plausible harm of one wrong action in this class." },
        "reversibility": { "enum": ["reversible", "compensable", "irreversible"] },
        "autonomy_owner": { "type": "string", "description": "The role accountable for this class and its level." },
        "verification_window_days": { "type": "integer", "minimum": 1, "description": "How long after an action its outcome is considered settled." },
        "max_level": { "$ref": "#/$defs/level", "description": "Ceiling for this class whatever the track record, e.g. where law or policy requires a human decision." },
        "max_level_reason": { "type": "string" },
        "contexts": {
          "type": "array", "minItems": 1,
          "items": { "$ref": "#/$defs/context" }
        },
        "envelopes": {
          "type": "object",
          "description": "Hard limits per level, enforced by the gateway or tool layer, never by the agent's prompt.",
          "propertyNames": { "$ref": "#/$defs/level" },
          "additionalProperties": { "$ref": "#/$defs/envelope" },
          "minProperties": 1
        },
        "sampling_floors": {
          "type": "object",
          "description": "Minimum share of actions a person reviews at each level. Never zero.",
          "propertyNames": { "$ref": "#/$defs/level" },
          "additionalProperties": { "$ref": "#/$defs/sampling" },
          "minProperties": 1
        },
        "promotion_criteria": {
          "type": "array",
          "items": { "$ref": "#/$defs/promotion_rule" }
        },
        "demotion_triggers": {
          "type": "array", "minItems": 1,
          "items": { "$ref": "#/$defs/demotion_trigger" }
        },
        "version_reset": { "$ref": "#/$defs/version_reset" }
      },
      "additionalProperties": false
    },

    "context": {
      "type": "object",
      "required": ["id", "dimensions", "current_level", "granted_to"],
      "properties": {
        "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]{1,60}$" },
        "dimensions": {
          "type": "object",
          "description": "What makes this context distinct: country, region, segment, value band, channel, network layer, site.",
          "minProperties": 1,
          "additionalProperties": { "type": ["string", "number", "boolean"] }
        },
        "current_level": { "$ref": "#/$defs/level" },
        "since": { "type": "string", "format": "date" },
        "granted_to": { "$ref": "#/$defs/versions" },
        "frozen": { "type": "boolean", "default": false },
        "last_decision": { "type": "string", "description": "decision_id of the promotion decision that set the current level." }
      },
      "additionalProperties": false
    },

    "envelope": {
      "type": "object",
      "required": ["pre_approval"],
      "properties": {
        "pre_approval": { "enum": ["all", "flagged-and-sampled", "none"], "description": "Which actions wait for a person before they run." },
        "max_value": { "$ref": "#/$defs/money" },
        "max_actions_per_hour": { "type": "integer", "minimum": 0 },
        "max_actions_per_day": { "type": "integer", "minimum": 0 },
        "allowed_tools": { "type": "array", "items": { "type": "string" } },
        "allowed_data": { "type": "array", "items": { "type": "string" } },
        "time_window": {
          "type": "object", "required": ["days", "start", "end", "timezone"],
          "properties": {
            "days": { "type": "array", "items": { "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] }, "minItems": 1 },
            "start": { "type": "string", "pattern": "^[0-2][0-9]:[0-5][0-9]$" },
            "end": { "type": "string", "pattern": "^[0-2][0-9]:[0-5][0-9]$" },
            "timezone": { "type": "string" }
          },
          "additionalProperties": false
        },
        "segments": { "type": "array", "items": { "type": "string" } },
        "reversible_only": { "type": "boolean" },
        "exceptions_to": { "type": "string", "description": "Where the agent sends anything outside the envelope." }
      },
      "additionalProperties": false
    },

    "sampling": {
      "type": "object",
      "required": ["review", "rate"],
      "properties": {
        "review": { "enum": ["every-action", "pre-execution", "post-execution", "periodic"] },
        "rate": { "type": "number", "exclusiveMinimum": 0, "maximum": 1 },
        "stratify_by": { "type": "array", "items": { "type": "string" }, "description": "Oversample rare or high-value cases so the sample covers them." }
      },
      "additionalProperties": false
    },

    "promotion_rule": {
      "type": "object",
      "required": ["from", "to", "min_actions", "min_days_at_level", "min_verified_good_rate", "max_severity_weighted_incidents", "approvers"],
      "properties": {
        "from": { "$ref": "#/$defs/level" },
        "to": { "$ref": "#/$defs/level" },
        "min_actions": { "type": "integer", "minimum": 1, "description": "Actions with a closed verification window, in this context, at this level." },
        "min_days_at_level": { "type": "integer", "minimum": 1 },
        "min_agreement_rate": { "$ref": "#/$defs/rate", "description": "L0: proposals that match what people did. L1 and above: actions approved or upheld without edits." },
        "max_edit_rate": { "$ref": "#/$defs/rate" },
        "min_verified_good_rate": { "$ref": "#/$defs/rate", "description": "Share of settled outcomes that held up." },
        "max_severity_weighted_incidents": { "type": "number", "minimum": 0, "description": "Per 1,000 actions; weights low 1, medium 5, high 25, critical 100." },
        "max_drift": { "$ref": "#/$defs/rate", "description": "Distance between the current case mix and the baseline the evidence came from." },
        "min_review_coverage": { "$ref": "#/$defs/rate", "description": "Share of required sampled reviews actually completed." },
        "approvers": {
          "type": "array", "minItems": 1,
          "items": { "type": "string" },
          "description": "Roles that must sign. Named people sign the promotion decision itself."
        }
      },
      "if": { "properties": { "to": { "enum": ["L3", "L4", "L5"] } } },
      "then": { "properties": { "approvers": { "minItems": 2 } } },
      "additionalProperties": false
    },

    "demotion_trigger": {
      "type": "object",
      "required": ["id", "when", "action", "automatic"],
      "properties": {
        "id": { "type": "string" },
        "when": { "enum": ["severe-incident", "critical-incident", "verified-bad-rate", "severity-weighted-incidents", "drift", "envelope-breach-attempts", "review-coverage-shortfall", "complaint-rate", "regulatory-freeze", "manual-freeze"] },
        "threshold": { "type": "number", "minimum": 0 },
        "rolling_window_days": { "type": "integer", "minimum": 1 },
        "action": { "enum": ["drop-one-level", "drop-to-level", "freeze"] },
        "to_level": { "$ref": "#/$defs/level" },
        "automatic": { "const": true, "description": "Demotions never wait for a meeting." },
        "notify": { "type": "array", "items": { "type": "string" } }
      },
      "if": { "properties": { "action": { "const": "drop-to-level" } } },
      "then": { "required": ["to_level"] },
      "additionalProperties": false
    },

    "version_reset": {
      "type": "object",
      "required": ["model_change", "prompt_change", "tool_change"],
      "properties": {
        "model_change": { "$ref": "#/$defs/reset_rule" },
        "prompt_change": { "$ref": "#/$defs/reset_rule" },
        "tool_change": { "$ref": "#/$defs/reset_rule" }
      },
      "additionalProperties": false
    },
    "reset_rule": {
      "type": "object",
      "required": ["drop_levels", "canary"],
      "properties": {
        "drop_levels": { "type": "integer", "minimum": 0, "maximum": 5 },
        "floor_level": { "$ref": "#/$defs/level", "description": "A reset never drops below this level." },
        "canary": {
          "type": "object", "required": ["traffic_share", "min_actions", "min_days"],
          "properties": {
            "traffic_share": { "type": "number", "exclusiveMinimum": 0, "maximum": 1 },
            "min_actions": { "type": "integer", "minimum": 1 },
            "min_days": { "type": "integer", "minimum": 1 },
            "sampling_multiplier": { "type": "number", "minimum": 1 }
          },
          "additionalProperties": false
        },
        "restore": { "enum": ["prior-level-if-canary-meets-criteria", "normal-promotion"], "default": "prior-level-if-canary-meets-criteria" }
      },
      "additionalProperties": false
    }
  }
}
