{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/usewrit-app/json-schema/usewrit-app-run-event-schema.json",
  "title": "RunEvent",
  "description": "One SSE frame's JSON payload, tagged by its `event` field (which also matches the SSE `event:` name). `finished` and `error` are stream-closing.",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/usewrit-app-runs-api-openapi.yml#/components/schemas/RunEvent",
  "oneOf": [
    {
      "$ref": "#/$defs/RunEventStarted"
    },
    {
      "$ref": "#/$defs/RunEventStep"
    },
    {
      "$ref": "#/$defs/RunEventProgress"
    },
    {
      "$ref": "#/$defs/RunEventFinished"
    },
    {
      "$ref": "#/$defs/RunEventError"
    }
  ],
  "$defs": {
    "RunEventError": {
      "type": "object",
      "description": "TERMINAL: the run failed around the step loop. `message` is an operator-facing classification, never a raw secret/value. Stream-closing.",
      "required": [
        "event",
        "run_id",
        "message"
      ],
      "properties": {
        "event": {
          "type": "string",
          "const": "error"
        },
        "run_id": {
          "type": "integer",
          "format": "int64"
        },
        "message": {
          "type": "string"
        }
      }
    },
    "RunEventFinished": {
      "type": "object",
      "description": "TERMINAL: the run reached a final status. Stream-closing.",
      "required": [
        "event",
        "run_id",
        "status"
      ],
      "properties": {
        "event": {
          "type": "string",
          "const": "finished"
        },
        "run_id": {
          "type": "integer",
          "format": "int64"
        },
        "status": {
          "$ref": "#/$defs/RunStatus"
        }
      }
    },
    "RunEventProgress": {
      "type": "object",
      "description": "Coarse progress hint — `completed` of `total` steps done.",
      "required": [
        "event",
        "run_id",
        "completed",
        "total"
      ],
      "properties": {
        "event": {
          "type": "string",
          "const": "progress"
        },
        "run_id": {
          "type": "integer",
          "format": "int64"
        },
        "completed": {
          "type": "integer"
        },
        "total": {
          "type": "integer"
        }
      }
    },
    "RunEventStarted": {
      "type": "object",
      "description": "The run is registered and about to drive its steps. `total_steps` counts the raw steps the engine will iterate (a synthetic subscription seed may report 0).",
      "required": [
        "event",
        "run_id",
        "total_steps"
      ],
      "properties": {
        "event": {
          "type": "string",
          "const": "started"
        },
        "run_id": {
          "type": "integer",
          "format": "int64"
        },
        "total_steps": {
          "type": "integer"
        }
      }
    },
    "RunEventStep": {
      "type": "object",
      "description": "A step transitioned. `index` is 0-based into the raw step list; `step_type` is the recorded step kind — never a value.",
      "required": [
        "event",
        "run_id",
        "index",
        "step_type",
        "status"
      ],
      "properties": {
        "event": {
          "type": "string",
          "const": "step"
        },
        "run_id": {
          "type": "integer",
          "format": "int64"
        },
        "index": {
          "type": "integer"
        },
        "step_type": {
          "type": "string"
        },
        "status": {
          "$ref": "#/$defs/StepStatus"
        }
      }
    },
    "RunStatus": {
      "type": "string",
      "description": "Terminal/in-flight run status (serde snake_case of `engine::RunStatus`). Treat as an open string enum in clients.",
      "enum": [
        "running",
        "success",
        "failed",
        "cancelled",
        "timeout",
        "captcha_required",
        "twofa_required"
      ]
    },
    "StepStatus": {
      "type": "string",
      "description": "Coarse per-step lifecycle marker.",
      "enum": [
        "running",
        "succeeded",
        "failed",
        "skipped"
      ]
    }
  }
}