{
  "$defs": {
    "IsoDate": {
      "description": "ISO 8601 date (YYYY-MM-DD).",
      "format": "date",
      "type": "string"
    },
    "PathRef": {
      "description": "Path-based cross-reference relative to .corpospec/ root.\nPattern: `^[a-z0-9_-]+(/[a-z0-9_.-]+)+$`",
      "pattern": "^[a-z0-9_-]+(/[a-z0-9_.-]+)+$",
      "type": "string"
    },
    "RunbookExecution": {
      "description": "Last execution of the runbook.",
      "properties": {
        "executed_at": {
          "$ref": "#/$defs/IsoDate"
        },
        "executed_by": {
          "$ref": "#/$defs/PathRef"
        },
        "incident_ref": {
          "anyOf": [
            {
              "$ref": "#/$defs/PathRef"
            },
            {
              "type": "null"
            }
          ],
          "description": "PathRef to the incident this execution responded to."
        },
        "mttr_minutes": {
          "description": "Time-to-resolve (minutes).",
          "format": "double",
          "type": [
            "number",
            "null"
          ]
        },
        "notes": {
          "type": "string"
        },
        "successful": {
          "type": "boolean"
        }
      },
      "required": [
        "executed_at",
        "executed_by",
        "successful",
        "notes"
      ],
      "type": "object"
    },
    "RunbookStatus": {
      "description": "Lifecycle.",
      "oneOf": [
        {
          "enum": [
            "draft",
            "approved",
            "in_revision",
            "retired"
          ],
          "type": "string"
        },
        {
          "const": "broken",
          "description": "Quarantined — known broken.",
          "type": "string"
        }
      ]
    },
    "RunbookStep": {
      "description": "One runbook step.",
      "properties": {
        "action": {
          "type": "string"
        },
        "command": {
          "description": "Concrete command / pseudo-command (e.g. `kubectl rollout\nrestart deploy/api`).",
          "type": [
            "string",
            "null"
          ]
        },
        "expected_observable": {
          "description": "Expected observable (e.g. \"p99 latency drops below 500ms\").",
          "type": "string"
        },
        "kind": {
          "$ref": "#/$defs/StepKind"
        },
        "ordinal": {
          "format": "int32",
          "type": "integer"
        },
        "rollback": {
          "description": "Rollback action if the step fails.",
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "ordinal",
        "kind",
        "action",
        "expected_observable"
      ],
      "type": "object"
    },
    "RunbookTrigger": {
      "description": "Trigger type.",
      "oneOf": [
        {
          "const": "alert",
          "description": "Pager alert (Prometheus, Sentry, CloudWatch).",
          "type": "string"
        },
        {
          "const": "scheduled",
          "description": "Scheduled cron / manual cadence.",
          "type": "string"
        },
        {
          "const": "manual",
          "description": "Manual on-demand.",
          "type": "string"
        },
        {
          "const": "event",
          "description": "Event-driven (webhook, message-bus).",
          "type": "string"
        },
        {
          "const": "incident_response",
          "description": "Incident-response only.",
          "type": "string"
        }
      ]
    },
    "Semver": {
      "description": "Semantic version string (MAJOR.MINOR.PATCH).",
      "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$",
      "type": "string"
    },
    "StepKind": {
      "description": "Step kind.",
      "oneOf": [
        {
          "const": "diagnose",
          "description": "Inspect (diagnose; no side effect).",
          "type": "string"
        },
        {
          "const": "mitigate",
          "description": "Mitigate (interim fix, doesn't address root cause).",
          "type": "string"
        },
        {
          "const": "fix",
          "description": "Fix (resolves the underlying issue).",
          "type": "string"
        },
        {
          "const": "communicate",
          "description": "Communicate (notify customers, status page).",
          "type": "string"
        },
        {
          "const": "escalate",
          "description": "Escalate (page senior engineer / leadership).",
          "type": "string"
        },
        {
          "const": "verify",
          "description": "Verify (confirm resolution).",
          "type": "string"
        },
        {
          "const": "postmortem",
          "description": "Postmortem (capture lessons).",
          "type": "string"
        }
      ]
    }
  },
  "$id": "https://corpospec.com/schemas/v0.16.0/runbook.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "additionalProperties": false,
  "description": "Operational runbook.",
  "properties": {
    "dashboard_ref": {
      "anyOf": [
        {
          "$ref": "#/$defs/PathRef"
        },
        {
          "type": "null"
        }
      ],
      "description": "PathRef to monitoring / dashboard."
    },
    "entity": {
      "$ref": "#/$defs/PathRef"
    },
    "id": {
      "$ref": "#/$defs/PathRef"
    },
    "last_drilled": {
      "$ref": "#/$defs/IsoDate"
    },
    "name": {
      "type": "string"
    },
    "next_drill_due": {
      "$ref": "#/$defs/IsoDate"
    },
    "owner": {
      "$ref": "#/$defs/PathRef"
    },
    "parent_sop": {
      "anyOf": [
        {
          "$ref": "#/$defs/PathRef"
        },
        {
          "type": "null"
        }
      ],
      "description": "PathRef to any related SOP."
    },
    "recent_executions": {
      "description": "Recent executions for MTTR analysis.",
      "items": {
        "$ref": "#/$defs/RunbookExecution"
      },
      "type": "array"
    },
    "status": {
      "$ref": "#/$defs/RunbookStatus"
    },
    "steps": {
      "items": {
        "$ref": "#/$defs/RunbookStep"
      },
      "type": "array"
    },
    "system": {
      "$ref": "#/$defs/PathRef",
      "description": "PathRef to the system / service this runbook applies to."
    },
    "trigger": {
      "$ref": "#/$defs/RunbookTrigger"
    },
    "trigger_summary": {
      "description": "Symptom / alert that this runbook resolves.",
      "type": "string"
    },
    "version": {
      "$ref": "#/$defs/Semver"
    }
  },
  "required": [
    "id",
    "entity",
    "system",
    "name",
    "version",
    "trigger",
    "trigger_summary",
    "owner",
    "steps",
    "status",
    "recent_executions",
    "last_drilled",
    "next_drill_due"
  ],
  "title": "Runbook",
  "type": "object",
  "x-corpospec-pillar": "operations"
}