{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aruntime.com/schemas/runtime-request.v1.schema.json",
  "title": "ARuntime Runtime Request Contract v1",
  "description": "A versioned request envelope for governed AI runtime execution.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contractVersion",
    "requestId",
    "correlationId",
    "idempotencyKey",
    "timestampUtc",
    "actor",
    "taskType",
    "riskLevel",
    "input",
    "permissions",
    "contextPolicy",
    "modelRouteConstraints",
    "toolPolicy",
    "memoryPolicy",
    "budget",
    "approvalPolicy",
    "outputContract",
    "traceSettings",
    "dataClassification",
    "retentionPolicy"
  ],
  "properties": {
    "contractVersion": {
      "const": "aruntime.runtime-request.v1"
    },
    "requestId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "correlationId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "idempotencyKey": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "timestampUtc": {
      "type": "string",
      "format": "date-time",
      "pattern": "Z$",
      "description": "UTC ISO 8601 timestamp ending in Z."
    },
    "deadlineUtc": {
      "anyOf": [
        {
          "type": "string",
          "format": "date-time",
          "pattern": "Z$",
          "description": "UTC ISO 8601 timestamp ending in Z."
        },
        {
          "type": "null"
        }
      ]
    },
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "idRef"
      ],
      "properties": {
        "type": {
          "enum": [
            "user",
            "service",
            "agent",
            "system"
          ]
        },
        "idRef": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160,
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
        },
        "authenticationContext": {
          "type": "string",
          "maxLength": 200
        }
      }
    },
    "tenant": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "required": [
        "idRef"
      ],
      "properties": {
        "idRef": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160,
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
        },
        "region": {
          "type": "string",
          "maxLength": 80
        }
      }
    },
    "session": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "properties": {
        "idRef": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160,
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
        },
        "sequence": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "taskType": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "riskLevel": {
      "enum": [
        "low",
        "medium",
        "high",
        "critical"
      ]
    },
    "input": {
      "type": "object",
      "description": "Task-specific input. Avoid embedding secrets when references are sufficient.",
      "additionalProperties": true
    },
    "permissions": {
      "type": "array",
      "uniqueItems": true,
      "maxItems": 100,
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 160
      }
    },
    "contextPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "classificationMax",
        "citeSources",
        "allowExternalRetrieval"
      ],
      "properties": {
        "classificationMax": {
          "type": "string",
          "enum": [
            "public",
            "internal",
            "confidential",
            "restricted"
          ]
        },
        "citeSources": {
          "type": "boolean"
        },
        "allowExternalRetrieval": {
          "type": "boolean"
        },
        "allowedRepositories": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 200
          }
        },
        "maxContextTokens": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "modelRouteConstraints": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "allowHosted",
        "allowedRoutes",
        "maxLatencyMs"
      ],
      "properties": {
        "allowHosted": {
          "type": "boolean"
        },
        "allowedRoutes": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 160
          }
        },
        "deniedProviders": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 160
          }
        },
        "maxLatencyMs": {
          "type": "integer",
          "minimum": 1
        },
        "requiredCapabilities": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 120
          }
        }
      }
    },
    "allowedTools": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "maxLength": 160
      }
    },
    "toolPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "defaultDecision",
        "maxCalls",
        "allowSideEffects"
      ],
      "properties": {
        "defaultDecision": {
          "enum": [
            "deny",
            "allow"
          ]
        },
        "maxCalls": {
          "type": "integer",
          "minimum": 0,
          "maximum": 1000
        },
        "allowSideEffects": {
          "type": "boolean"
        },
        "requiredPermissionClasses": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 120
          }
        }
      }
    },
    "memoryPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "readScopes",
        "writeScopes",
        "writeRequiresApproval"
      ],
      "properties": {
        "readScopes": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 120
          }
        },
        "writeScopes": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 120
          }
        },
        "writeRequiresApproval": {
          "type": "boolean"
        },
        "ttlSeconds": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "maxInputTokens",
        "maxOutputTokens",
        "maxToolCalls",
        "maxCostUsd"
      ],
      "properties": {
        "maxInputTokens": {
          "type": "integer",
          "minimum": 0
        },
        "maxOutputTokens": {
          "type": "integer",
          "minimum": 0
        },
        "maxToolCalls": {
          "type": "integer",
          "minimum": 0
        },
        "maxCostUsd": {
          "type": "number",
          "minimum": 0
        },
        "currency": {
          "const": "USD"
        }
      }
    },
    "approvalPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode",
        "requiredFor"
      ],
      "properties": {
        "mode": {
          "enum": [
            "none",
            "policy-driven",
            "always"
          ]
        },
        "requiredFor": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 120
          }
        },
        "timeoutSeconds": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        },
        "authorityRefs": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 160
          }
        }
      }
    },
    "outputContract": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "format"
      ],
      "properties": {
        "format": {
          "enum": [
            "text",
            "json",
            "json-schema"
          ]
        },
        "schemaRef": {
          "type": [
            "string",
            "null"
          ],
          "format": "uri-reference"
        },
        "maxBytes": {
          "type": "integer",
          "minimum": 1
        },
        "requiresCitations": {
          "type": "boolean"
        }
      }
    },
    "traceSettings": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "level",
        "redactInputs",
        "captureToolResults"
      ],
      "properties": {
        "level": {
          "enum": [
            "none",
            "minimal",
            "standard",
            "diagnostic"
          ]
        },
        "redactInputs": {
          "type": "boolean"
        },
        "captureToolResults": {
          "type": "boolean"
        },
        "samplingDecision": {
          "enum": [
            "drop",
            "record",
            "record-and-sample"
          ]
        }
      }
    },
    "dataClassification": {
      "type": "string",
      "enum": [
        "public",
        "internal",
        "confidential",
        "restricted"
      ]
    },
    "retentionPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "policyId",
        "days",
        "storeRawInputs"
      ],
      "properties": {
        "policyId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160,
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
        },
        "days": {
          "type": "integer",
          "minimum": 0,
          "maximum": 3650
        },
        "storeRawInputs": {
          "type": "boolean"
        },
        "legalHold": {
          "type": "boolean"
        }
      }
    },
    "metadata": {
      "type": "object",
      "maxProperties": 50,
      "additionalProperties": {
        "type": [
          "string",
          "number",
          "integer",
          "boolean",
          "null"
        ]
      }
    }
  }
}
