{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.samirac.com/schemas/common-definitions.schema.json",
  "title": "SAQ Execution Contract Common Definitions",
  "description": "Reusable definitions for the SAQ Execution Contract Public Draft v0.1 machine-readable companion package.",
  "$defs": {
    "identifier": {
      "type": "string",
      "minLength": 3,
      "maxLength": 256,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9:._/@-]*$"
    },
    "digest": {
      "type": "string",
      "pattern": "^(sha256|sha384|sha512):[A-Fa-f0-9]{32,128}$"
    },
    "reasonCode": {
      "type": "string",
      "pattern": "^[A-Z][A-Z0-9_]{2,63}$"
    },
    "lifecycleState": {
      "type": "string",
      "enum": [
        "DRAFT",
        "ASSEMBLING",
        "VALIDATING",
        "ADMISSIBLE",
        "CONDITIONAL",
        "DENIED",
        "ESCALATED",
        "DEFERRED",
        "EXPIRED",
        "AUTHORIZED",
        "EXECUTING",
        "EXECUTED",
        "FAILED",
        "RECEIPTED",
        "CORRECTED",
        "CLOSED",
        "REVOKED"
      ]
    },
    "requester": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "requester_id",
        "requester_type",
        "authentication_context"
      ],
      "properties": {
        "requester_id": {
          "$ref": "#/$defs/identifier"
        },
        "requester_type": {
          "type": "string",
          "enum": [
            "human",
            "service",
            "workflow",
            "system",
            "organization"
          ]
        },
        "organization": {
          "$ref": "#/$defs/identifier"
        },
        "session": {
          "$ref": "#/$defs/identifier"
        },
        "authentication_context": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "actor_id",
        "actor_type"
      ],
      "properties": {
        "actor_id": {
          "$ref": "#/$defs/identifier"
        },
        "actor_type": {
          "type": "string",
          "enum": [
            "human",
            "ai_agent",
            "model",
            "service",
            "workflow",
            "composite"
          ]
        },
        "runtime_id": {
          "$ref": "#/$defs/identifier"
        },
        "model_id": {
          "$ref": "#/$defs/identifier"
        },
        "agent_id": {
          "$ref": "#/$defs/identifier"
        },
        "delegated_by": {
          "$ref": "#/$defs/identifier"
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "proposedAction": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "action_type",
        "parameters",
        "action_digest",
        "requested_effect",
        "reversibility"
      ],
      "properties": {
        "action_type": {
          "$ref": "#/$defs/identifier"
        },
        "parameters": {
          "type": "object",
          "additionalProperties": true
        },
        "action_digest": {
          "$ref": "#/$defs/digest"
        },
        "requested_effect": {
          "type": "string",
          "minLength": 1,
          "maxLength": 2000
        },
        "reversibility": {
          "type": "string",
          "enum": [
            "reversible",
            "limited",
            "irreversible",
            "unknown"
          ]
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "target_type",
        "target_id",
        "target_digest"
      ],
      "properties": {
        "target_type": {
          "$ref": "#/$defs/identifier"
        },
        "target_id": {
          "$ref": "#/$defs/identifier"
        },
        "target_digest": {
          "$ref": "#/$defs/digest"
        },
        "resource_scope": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "jurisdiction": {
          "type": "string",
          "minLength": 1
        },
        "data_classification": {
          "type": "string",
          "enum": [
            "public",
            "internal",
            "confidential",
            "restricted",
            "regulated"
          ]
        },
        "attributes": {
          "type": "object",
          "additionalProperties": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "authorityClaim": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "authority_type",
        "issuer",
        "subject",
        "scope",
        "constraints",
        "issued_at",
        "valid_until",
        "revocation_source",
        "status"
      ],
      "properties": {
        "authority_type": {
          "$ref": "#/$defs/identifier"
        },
        "issuer": {
          "$ref": "#/$defs/identifier"
        },
        "subject": {
          "$ref": "#/$defs/identifier"
        },
        "scope": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/identifier"
          }
        },
        "constraints": {
          "type": "object",
          "additionalProperties": true
        },
        "issued_at": {
          "type": "string",
          "format": "date-time"
        },
        "valid_until": {
          "type": "string",
          "format": "date-time"
        },
        "revocation_source": {
          "$ref": "#/$defs/identifier"
        },
        "status": {
          "type": "string",
          "enum": [
            "ACTIVE",
            "SUSPENDED",
            "REVOKED"
          ]
        },
        "revoked_at": {
          "type": "string",
          "format": "date-time"
        },
        "proof": {
          "type": "object",
          "additionalProperties": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "delegationHop": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "delegator",
        "delegate",
        "scope",
        "depth",
        "conditions",
        "proof"
      ],
      "properties": {
        "delegator": {
          "$ref": "#/$defs/identifier"
        },
        "delegate": {
          "$ref": "#/$defs/identifier"
        },
        "scope": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/identifier"
          }
        },
        "depth": {
          "type": "integer",
          "minimum": 0
        },
        "conditions": {
          "type": "object",
          "additionalProperties": true
        },
        "proof": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": true
        }
      }
    },
    "stateSnapshot": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state_source",
        "observed_at",
        "retrieved_at",
        "state_version",
        "state_digest",
        "freshness_requirement_seconds",
        "validation_result"
      ],
      "properties": {
        "state_source": {
          "$ref": "#/$defs/identifier"
        },
        "observed_at": {
          "type": "string",
          "format": "date-time"
        },
        "retrieved_at": {
          "type": "string",
          "format": "date-time"
        },
        "state_version": {
          "type": "string",
          "minLength": 1
        },
        "state_digest": {
          "$ref": "#/$defs/digest"
        },
        "freshness_requirement_seconds": {
          "type": "integer",
          "minimum": 0
        },
        "validation_result": {
          "type": "string",
          "enum": [
            "VALID",
            "INVALID",
            "UNKNOWN",
            "CONFLICTING"
          ]
        },
        "attributes": {
          "type": "object",
          "additionalProperties": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "evidenceItem": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "evidence_id",
        "type",
        "source",
        "provenance",
        "observed_at",
        "retrieved_at",
        "digest",
        "validation_state",
        "freshness_requirement_seconds"
      ],
      "properties": {
        "evidence_id": {
          "$ref": "#/$defs/identifier"
        },
        "type": {
          "$ref": "#/$defs/identifier"
        },
        "source": {
          "$ref": "#/$defs/identifier"
        },
        "provenance": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": true
        },
        "observed_at": {
          "type": "string",
          "format": "date-time"
        },
        "retrieved_at": {
          "type": "string",
          "format": "date-time"
        },
        "digest": {
          "$ref": "#/$defs/digest"
        },
        "validation_state": {
          "type": "string",
          "enum": [
            "VALID",
            "INVALID",
            "UNKNOWN",
            "CONFLICTING"
          ]
        },
        "freshness_requirement_seconds": {
          "type": "integer",
          "minimum": 0
        },
        "attributes": {
          "type": "object",
          "additionalProperties": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "policyBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "policy_id",
        "policy_version",
        "issuer",
        "effective_at",
        "jurisdiction",
        "rule_set_digest"
      ],
      "properties": {
        "policy_id": {
          "$ref": "#/$defs/identifier"
        },
        "policy_version": {
          "type": "string",
          "minLength": 1
        },
        "issuer": {
          "$ref": "#/$defs/identifier"
        },
        "effective_at": {
          "type": "string",
          "format": "date-time"
        },
        "jurisdiction": {
          "type": "string",
          "minLength": 1
        },
        "rule_set_digest": {
          "$ref": "#/$defs/digest"
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "riskAndConsequence": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "risk_class",
        "impact_domains",
        "reversibility",
        "affected_subjects",
        "escalation_threshold"
      ],
      "properties": {
        "risk_class": {
          "type": "string",
          "enum": [
            "LOW",
            "MODERATE",
            "HIGH",
            "CRITICAL"
          ]
        },
        "impact_domains": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "reversibility": {
          "type": "string",
          "enum": [
            "reversible",
            "limited",
            "irreversible",
            "unknown"
          ]
        },
        "value_at_risk": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "amount",
            "currency"
          ],
          "properties": {
            "amount": {
              "type": "number",
              "minimum": 0
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            }
          }
        },
        "affected_subjects": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          },
          "uniqueItems": true
        },
        "escalation_threshold": {
          "type": "object",
          "additionalProperties": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "approvalRequirement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "approval_type",
        "minimum_count"
      ],
      "properties": {
        "approval_type": {
          "$ref": "#/$defs/identifier"
        },
        "minimum_count": {
          "type": "integer",
          "minimum": 1
        },
        "permitted_roles": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          },
          "uniqueItems": true
        },
        "independent_of_requester": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "constraints": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "amount_limit": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "maximum",
            "currency"
          ],
          "properties": {
            "maximum": {
              "type": "number",
              "minimum": 0
            },
            "currency": {
              "type": "string",
              "pattern": "^[A-Z]{3}$"
            }
          }
        },
        "target_limit": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          },
          "uniqueItems": true
        },
        "time_window": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "valid_from",
            "valid_until"
          ],
          "properties": {
            "valid_from": {
              "type": "string",
              "format": "date-time"
            },
            "valid_until": {
              "type": "string",
              "format": "date-time"
            }
          }
        },
        "tool_limit": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          },
          "uniqueItems": true
        },
        "data_limit": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "geography": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "rate_limit": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "maximum",
            "period_seconds"
          ],
          "properties": {
            "maximum": {
              "type": "integer",
              "minimum": 1
            },
            "period_seconds": {
              "type": "integer",
              "minimum": 1
            }
          }
        },
        "approval_requirements": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/approvalRequirement"
          }
        },
        "permitted_effects": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "admissibilityDecision": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "outcome",
        "reason_codes",
        "decided_by",
        "decided_at",
        "evidence_digest",
        "state_digest",
        "policy_digest"
      ],
      "properties": {
        "outcome": {
          "type": "string",
          "enum": [
            "ADMISSIBLE",
            "CONDITIONAL",
            "DENIED",
            "ESCALATED",
            "DEFERRED",
            "EXPIRED"
          ]
        },
        "reason_codes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reasonCode"
          },
          "uniqueItems": true
        },
        "decided_by": {
          "$ref": "#/$defs/identifier"
        },
        "decided_at": {
          "type": "string",
          "format": "date-time"
        },
        "evidence_digest": {
          "$ref": "#/$defs/digest"
        },
        "state_digest": {
          "$ref": "#/$defs/digest"
        },
        "policy_digest": {
          "$ref": "#/$defs/digest"
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true
          }
        },
        "decision_digest": {
          "$ref": "#/$defs/digest"
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "outcome": {
                "enum": [
                  "CONDITIONAL",
                  "DENIED",
                  "ESCALATED",
                  "DEFERRED",
                  "EXPIRED"
                ]
              }
            }
          },
          "then": {
            "properties": {
              "reason_codes": {
                "minItems": 1
              }
            }
          }
        }
      ]
    },
    "transitionRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "from_state",
        "to_state",
        "event",
        "timestamp",
        "responsible_component",
        "reason_codes",
        "contract_digest"
      ],
      "properties": {
        "from_state": {
          "$ref": "#/$defs/lifecycleState"
        },
        "to_state": {
          "$ref": "#/$defs/lifecycleState"
        },
        "event": {
          "$ref": "#/$defs/identifier"
        },
        "timestamp": {
          "type": "string",
          "format": "date-time"
        },
        "responsible_component": {
          "$ref": "#/$defs/identifier"
        },
        "responsible_authority": {
          "$ref": "#/$defs/identifier"
        },
        "reason_codes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reasonCode"
          },
          "uniqueItems": true
        },
        "policy_version": {
          "type": "string"
        },
        "evidence_digest": {
          "$ref": "#/$defs/digest"
        },
        "state_digest": {
          "$ref": "#/$defs/digest"
        },
        "contract_digest": {
          "$ref": "#/$defs/digest"
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "lifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "current_state",
        "revision",
        "transition_history"
      ],
      "properties": {
        "current_state": {
          "$ref": "#/$defs/lifecycleState"
        },
        "prior_state": {
          "$ref": "#/$defs/lifecycleState"
        },
        "revision": {
          "type": "integer",
          "minimum": 1
        },
        "transition_history": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/transitionRecord"
          }
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "algorithm",
        "key_id",
        "value"
      ],
      "properties": {
        "algorithm": {
          "type": "string",
          "minLength": 1
        },
        "key_id": {
          "$ref": "#/$defs/identifier"
        },
        "value": {
          "type": "string",
          "minLength": 16
        }
      }
    },
    "correctionRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "trigger",
        "detected_at",
        "correction_type",
        "authority",
        "related_receipts",
        "result",
        "residual_risk",
        "closure_state"
      ],
      "properties": {
        "trigger": {
          "$ref": "#/$defs/reasonCode"
        },
        "detected_at": {
          "type": "string",
          "format": "date-time"
        },
        "correction_type": {
          "type": "string",
          "enum": [
            "REVOCATION",
            "REVERSAL",
            "COMPENSATION",
            "SUSPENSION",
            "REMEDIATION",
            "ESCALATION",
            "STATE_REALIGNMENT"
          ]
        },
        "authority": {
          "$ref": "#/$defs/identifier"
        },
        "related_receipts": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          },
          "minItems": 1,
          "uniqueItems": true
        },
        "result": {
          "type": "object",
          "additionalProperties": true
        },
        "residual_risk": {
          "type": "string",
          "enum": [
            "NONE",
            "LOW",
            "MODERATE",
            "HIGH",
            "CRITICAL"
          ]
        },
        "closure_state": {
          "type": "string",
          "enum": [
            "OPEN",
            "MONITORING",
            "CLOSED"
          ]
        },
        "extensions": {
          "type": "object",
          "additionalProperties": true
        }
      }
    }
  }
}
