{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aruntime.com/schemas/tool-contract.v1.schema.json",
  "title": "ARuntime Tool Contract v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contractVersion",
    "toolId",
    "version",
    "description",
    "inputSchema",
    "outputSchema",
    "authenticationRef",
    "permissionClass",
    "sideEffectClass",
    "dataClassification",
    "egressPolicy",
    "timeoutMs",
    "retryPolicy",
    "idempotency",
    "concurrency",
    "approvalTriggers",
    "compensation",
    "errorTaxonomy",
    "rateLimits",
    "observability",
    "auditFields",
    "deprecation"
  ],
  "properties": {
    "contractVersion": {
      "const": "aruntime.tool-contract.v1"
    },
    "toolId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+(?:[-+][A-Za-z0-9.-]+)?$"
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1000
    },
    "inputSchema": {
      "type": "object"
    },
    "outputSchema": {
      "type": "object"
    },
    "authenticationRef": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    },
    "permissionClass": {
      "enum": [
        "read",
        "reversible-write",
        "irreversible",
        "financial-high-impact",
        "external-communication",
        "code-execution",
        "administrative"
      ]
    },
    "sideEffectClass": {
      "enum": [
        "none",
        "reversible",
        "compensatable",
        "irreversible"
      ]
    },
    "dataClassification": {
      "type": "string",
      "enum": [
        "public",
        "internal",
        "confidential",
        "restricted"
      ]
    },
    "egressPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "allowed",
        "destinations"
      ],
      "properties": {
        "allowed": {
          "type": "boolean"
        },
        "destinations": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 200
          }
        }
      }
    },
    "timeoutMs": {
      "type": "integer",
      "minimum": 1,
      "maximum": 3600000
    },
    "retryPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "maxAttempts",
        "backoff",
        "retryableErrors"
      ],
      "properties": {
        "maxAttempts": {
          "type": "integer",
          "minimum": 1,
          "maximum": 20
        },
        "backoff": {
          "enum": [
            "none",
            "fixed",
            "exponential",
            "exponential-jitter"
          ]
        },
        "retryableErrors": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "maxLength": 120
          }
        }
      }
    },
    "idempotency": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "supported",
        "keyLocation",
        "deduplicationWindowSeconds"
      ],
      "properties": {
        "supported": {
          "type": "boolean"
        },
        "keyLocation": {
          "type": [
            "string",
            "null"
          ]
        },
        "deduplicationWindowSeconds": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "concurrency": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode",
        "maxConcurrent"
      ],
      "properties": {
        "mode": {
          "enum": [
            "parallel",
            "serialized-by-resource",
            "serialized-global"
          ]
        },
        "maxConcurrent": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "approvalTriggers": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 200
      }
    },
    "compensation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "supported",
        "toolRef"
      ],
      "properties": {
        "supported": {
          "type": "boolean"
        },
        "toolRef": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 160
        },
        "deadlineSeconds": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        }
      }
    },
    "errorTaxonomy": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "code",
          "retryable",
          "meaning"
        ],
        "properties": {
          "code": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
          },
          "retryable": {
            "type": "boolean"
          },
          "meaning": {
            "type": "string",
            "maxLength": 500
          }
        }
      }
    },
    "rateLimits": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "requests",
        "perSeconds"
      ],
      "properties": {
        "requests": {
          "type": "integer",
          "minimum": 1
        },
        "perSeconds": {
          "type": "integer",
          "minimum": 1
        },
        "scope": {
          "enum": [
            "actor",
            "tenant",
            "resource",
            "global"
          ]
        }
      }
    },
    "observability": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "requiredSpans",
        "requiredMetrics"
      ],
      "properties": {
        "requiredSpans": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "requiredMetrics": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "logPayloads": {
          "type": "boolean"
        }
      }
    },
    "auditFields": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "type": "string",
        "maxLength": 120
      }
    },
    "deprecation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status"
      ],
      "properties": {
        "status": {
          "enum": [
            "active",
            "deprecated",
            "retired"
          ]
        },
        "replacementRef": {
          "type": [
            "string",
            "null"
          ]
        },
        "sunsetUtc": {
          "anyOf": [
            {
              "type": "string",
              "format": "date-time",
              "pattern": "Z$",
              "description": "UTC ISO 8601 timestamp ending in Z."
            },
            {
              "type": "null"
            }
          ]
        }
      }
    }
  }
}
