{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "sks.mad-sks-sql-plane-capability.v2",
  "type": "object",
  "required": [
    "schema",
    "revision",
    "mission_id",
    "cycle_id",
    "project_root_hash",
    "project_ref",
    "target_environment",
    "allowed_schemas",
    "runtime_session_id",
    "operator_intent_hash",
    "operator_ack_hash",
    "scope",
    "transport",
    "issued_at",
    "expires_at",
    "closed_at",
    "status",
    "counters"
  ],
  "properties": {
    "schema": { "const": "sks.mad-sks-sql-plane-capability.v2" },
    "revision": { "type": "integer", "minimum": 1 },
    "mission_id": { "type": "string", "pattern": "^M-" },
    "cycle_id": { "type": "string", "pattern": "^mad-sks-sql-plane-" },
    "project_root_hash": { "type": "string", "minLength": 8 },
    "project_ref": { "type": "string", "minLength": 1 },
    "target_environment": { "enum": ["local", "branch", "preview", "production"] },
    "allowed_schemas": { "type": "array", "items": { "type": "string" } },
    "codex_thread_id": { "type": ["string", "null"] },
    "runtime_session_id": { "type": "string", "minLength": 1 },
    "operator_intent_hash": { "type": "string", "minLength": 8 },
    "operator_ack_hash": { "type": "string", "minLength": 8 },
    "scope": {
      "type": "object",
      "required": ["sql_plane", "control_plane", "operations"],
      "properties": {
        "sql_plane": { "const": "all_mutations" },
        "control_plane": { "const": "deny" },
        "operations": {
          "type": "array",
          "items": {
            "enum": [
              "create",
              "alter",
              "drop",
              "drop_database_sql",
              "insert",
              "update",
              "delete",
              "all_row_update",
              "all_row_delete",
              "truncate",
              "migration_apply",
              "direct_execute_sql",
              "rls_policy_change",
              "function_or_trigger_change",
              "index_change",
              "unknown_sql_mutation"
            ]
          }
        }
      },
      "additionalProperties": false
    },
    "transport": {
      "type": "object",
      "required": ["profile_path", "profile_sha256", "server_url_redacted", "features", "write_capable"],
      "properties": {
        "profile_path": { "type": "string", "minLength": 1 },
        "profile_sha256": { "type": "string", "minLength": 8 },
        "server_url_redacted": { "type": "string", "minLength": 1 },
        "features": {
          "type": "array",
          "prefixItems": [{ "const": "database" }],
          "minItems": 1,
          "maxItems": 1
        },
        "write_capable": { "const": true }
      },
      "additionalProperties": false
    },
    "issued_at": { "type": "string" },
    "expires_at": { "type": "string" },
    "closed_at": { "type": ["string", "null"] },
    "status": { "enum": ["issued", "transport_ready", "active", "closing", "closed", "revoked", "expired", "quarantined"] },
    "counters": {
      "type": "object",
      "required": ["attempts", "reserved", "succeeded", "failed"],
      "properties": {
        "attempts": { "type": "integer", "minimum": 0 },
        "reserved": { "type": "integer", "minimum": 0 },
        "succeeded": { "type": "integer", "minimum": 0 },
        "failed": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": true
}
