{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pipelinexlab.com/contracts/document-job-snapshot.v1.schema.json",
  "title": "Document job snapshot V1",
  "description": "Detached public view. The application ledger owns state transitions and diagnostics; result review does not approve a Report mutation.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "jobRef",
    "requestRef",
    "sourceRef",
    "state",
    "attempt",
    "resultRef",
    "review",
    "cancellationRequested",
    "termination",
    "diagnostics"
  ],
  "properties": {
    "schemaVersion": {
      "const": 1
    },
    "jobRef": {
      "type": "string",
      "minLength": 1
    },
    "requestRef": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "sourceRef": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "state": {
      "enum": [
        "queued",
        "running",
        "succeeded",
        "failed",
        "cancelled",
        "indeterminate"
      ]
    },
    "attempt": {
      "type": "integer",
      "minimum": 0,
      "maximum": 3
    },
    "resultRef": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "review": {
      "enum": [
        null,
        "pending",
        "accepted",
        "refused"
      ]
    },
    "cancellationRequested": {
      "type": "boolean"
    },
    "termination": {
      "enum": [
        null,
        "processor-refused",
        "attempts-exhausted",
        "external-outcome-unknown",
        "cancellation-requested"
      ]
    },
    "diagnostics": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "code",
          "path",
          "severity",
          "retryable",
          "params"
        ],
        "properties": {
          "code": {
            "type": "string",
            "pattern": "^PX_"
          },
          "path": {
            "type": "string"
          },
          "severity": {
            "enum": [
              "error",
              "warning",
              "info"
            ]
          },
          "retryable": {
            "type": "boolean"
          },
          "params": {
            "type": "object"
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "state": {
            "const": "succeeded"
          }
        },
        "required": [
          "state"
        ]
      },
      "then": {
        "properties": {
          "resultRef": {
            "type": "string",
            "pattern": "^sha256:[0-9a-f]{64}$"
          },
          "review": {
            "enum": [
              "pending",
              "accepted",
              "refused"
            ]
          }
        }
      },
      "else": {
        "properties": {
          "resultRef": {
            "type": "null"
          },
          "review": {
            "type": "null"
          }
        }
      }
    }
  ]
}
