{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:atmem:delegated-context-provider:v1",
  "title": "AtMem delegated context-provider result v1",
  "description": "An externally authorized, signed context result. It does not prove host delivery.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contract_id",
    "provider",
    "binding",
    "decision",
    "context",
    "receipt",
    "created_at",
    "expires_at",
    "nonce",
    "idempotency_key",
    "source_refs",
    "withhold_reason",
    "signature"
  ],
  "properties": {
    "contract_id": {"const": "atmem.delegated-context-provider.v1"},
    "provider": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "version", "instance_id"],
      "properties": {
        "id": {"$ref": "#/$defs/id"},
        "version": {"$ref": "#/$defs/id"},
        "instance_id": {"$ref": "#/$defs/id"}
      }
    },
    "binding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["run_id", "turn_id", "session_id", "agent_id", "user_id", "workspace_id"],
      "properties": {
        "run_id": {"$ref": "#/$defs/id"},
        "turn_id": {"$ref": "#/$defs/id"},
        "session_id": {"$ref": "#/$defs/id"},
        "agent_id": {"$ref": "#/$defs/id"},
        "user_id": {"$ref": "#/$defs/id"},
        "workspace_id": {"$ref": "#/$defs/id"}
      }
    },
    "decision": {"enum": ["inject", "withhold"]},
    "context": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/context"}]},
    "receipt": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "contract_id", "sha256"],
      "properties": {
        "id": {"$ref": "#/$defs/longId"},
        "contract_id": {"$ref": "#/$defs/id"},
        "sha256": {"$ref": "#/$defs/sha256"}
      }
    },
    "created_at": {"$ref": "#/$defs/utcTimestamp"},
    "expires_at": {"$ref": "#/$defs/utcTimestamp"},
    "nonce": {"type": "string", "pattern": "^[A-Za-z0-9_-]{22,128}$"},
    "idempotency_key": {"type": "string", "pattern": "^dcp-[0-9a-f]{64}$"},
    "source_refs": {
      "type": "array",
      "maxItems": 32,
      "uniqueItems": true,
      "items": {"$ref": "#/$defs/longId"}
    },
    "withhold_reason": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/withholdReason"}]},
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "profile", "key_id", "signed_payload_sha256", "value_base64"],
      "properties": {
        "algorithm": {"const": "ed25519"},
        "profile": {"const": "ed25519-jcs-subset-v1"},
        "key_id": {"$ref": "#/$defs/id"},
        "signed_payload_sha256": {"$ref": "#/$defs/sha256"},
        "value_base64": {"type": "string", "pattern": "^[A-Za-z0-9+/]{86}==$"}
      }
    }
  },
  "allOf": [
    {
      "if": {"properties": {"decision": {"const": "inject"}}, "required": ["decision"]},
      "then": {"properties": {"context": {"$ref": "#/$defs/context"}, "withhold_reason": {"type": "null"}}}
    },
    {
      "if": {"properties": {"decision": {"const": "withhold"}}, "required": ["decision"]},
      "then": {"properties": {"context": {"type": "null"}, "withhold_reason": {"$ref": "#/$defs/withholdReason"}}}
    }
  ],
  "$defs": {
    "id": {"type": "string", "minLength": 1, "maxLength": 256, "pattern": "^\\S(?:.*\\S)?$"},
    "longId": {"type": "string", "minLength": 1, "maxLength": 512, "pattern": "^\\S(?:.*\\S)?$"},
    "sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
    "utcTimestamp": {"type": "string", "format": "date-time", "pattern": "Z$"},
    "context": {
      "type": "object",
      "additionalProperties": false,
      "required": ["encoding", "media_type", "bytes_base64", "byte_length", "sha256"],
      "properties": {
        "encoding": {"const": "base64"},
        "media_type": {"const": "text/plain; charset=utf-8"},
        "bytes_base64": {
          "type": "string",
          "minLength": 4,
          "maxLength": 349528,
          "pattern": "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
        },
        "byte_length": {"type": "integer", "minimum": 1, "maximum": 262144},
        "sha256": {"$ref": "#/$defs/sha256"}
      }
    },
    "withholdReason": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "retryable"],
      "properties": {
        "code": {"type": "string", "pattern": "^[A-Z][A-Z0-9_]{2,63}$"},
        "retryable": {"type": "boolean"}
      }
    }
  }
}
