{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://scoperail.fokalabs.io/method/pending-action.schema.json",
  "title": "ScopeRail pending action",
  "description": "A reviewable action proposal. Creation of this object does not mean that the action was authorized or executed.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "id",
    "idempotencyKey",
    "toolId",
    "toolVersion",
    "status",
    "requestedBy",
    "scope",
    "authorizationRef",
    "proposedInput",
    "expectedOutcome",
    "risk",
    "checkpoint",
    "evidenceRefs",
    "createdAt",
    "expiresAt"
  ],
  "properties": {
    "id": {
      "type": "string",
      "minLength": 8,
      "maxLength": 160
    },
    "idempotencyKey": {
      "type": "string",
      "minLength": 8,
      "maxLength": 160
    },
    "toolId": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9._-]{2,79}$"
    },
    "toolVersion": {
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    },
    "status": {
      "type": "string",
      "enum": [
        "proposed",
        "pending_confirmation",
        "confirmed",
        "executing",
        "succeeded",
        "failed",
        "cancelled",
        "expired"
      ]
    },
    "requestedBy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "subjectRef"
      ],
      "properties": {
        "subjectRef": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "roleRefs": {
          "type": "array",
          "maxItems": 24,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          }
        }
      }
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "resourceType",
        "resourceRef"
      ],
      "properties": {
        "tenantRef": {
          "type": "string",
          "maxLength": 200
        },
        "siteRef": {
          "type": "string",
          "maxLength": 200
        },
        "resourceType": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        },
        "resourceRef": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    },
    "authorizationRef": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240,
      "description": "Reference to a host-side authorization decision. Never include an access token."
    },
    "proposedInput": {
      "type": "object",
      "description": "Validated input shown to the person before confirmation."
    },
    "expectedOutcome": {
      "type": "string",
      "minLength": 3,
      "maxLength": 1000
    },
    "risk": {
      "type": "string",
      "enum": [
        "read_only",
        "low_reversible",
        "consequential",
        "prohibited"
      ]
    },
    "checkpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "required",
        "reason"
      ],
      "properties": {
        "required": {
          "type": "boolean"
        },
        "reason": {
          "type": "string",
          "minLength": 3,
          "maxLength": 500
        },
        "confirmationText": {
          "type": "string",
          "maxLength": 500
        },
        "confirmedBy": {
          "type": "string",
          "maxLength": 200
        },
        "confirmedAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "evidenceRefs": {
      "type": "array",
      "maxItems": 64,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 240
      }
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "expiresAt": {
      "type": "string",
      "format": "date-time"
    },
    "executedAt": {
      "type": "string",
      "format": "date-time"
    },
    "resultRef": {
      "type": "string",
      "maxLength": 240
    },
    "failure": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "code",
        "recoverable"
      ],
      "properties": {
        "code": {
          "type": "string",
          "minLength": 1,
          "maxLength": 120
        },
        "recoverable": {
          "type": "boolean"
        },
        "recoveryHint": {
          "type": "string",
          "maxLength": 500
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "const": "succeeded"
          }
        },
        "required": [
          "status"
        ]
      },
      "then": {
        "required": [
          "executedAt",
          "resultRef"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "failed"
          }
        },
        "required": [
          "status"
        ]
      },
      "then": {
        "required": [
          "failure"
        ]
      }
    }
  ]
}
