{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://ghassan-alhamoud.com/resources/agent-handoff-contract.schema.json",
  "title": "Agent handoff contract",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "contractVersion",
    "handoffId",
    "idempotencyKey",
    "createdAt",
    "issuer",
    "assignee",
    "objective",
    "scope",
    "constraints",
    "acceptance",
    "permissions",
    "budget",
    "escalation",
    "state"
  ],
  "properties": {
    "contractVersion": {
      "const": "1.0"
    },
    "handoffId": {
      "type": "string",
      "format": "uuid"
    },
    "idempotencyKey": {
      "type": "string",
      "minLength": 8,
      "maxLength": 200
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "issuer": {
      "$ref": "#/$defs/party"
    },
    "assignee": {
      "$ref": "#/$defs/party"
    },
    "objective": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "scope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["include", "exclude"],
      "properties": {
        "include": {
          "$ref": "#/$defs/stringList"
        },
        "exclude": {
          "$ref": "#/$defs/stringList"
        }
      }
    },
    "inputs": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["name", "uri"],
        "properties": {
          "name": {
            "type": "string"
          },
          "uri": {
            "type": "string"
          },
          "digest": {
            "type": "string",
            "pattern": "^sha256:[a-f0-9]{64}$"
          }
        }
      }
    },
    "constraints": {
      "$ref": "#/$defs/stringList"
    },
    "acceptance": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "assertion", "evidenceRequired"],
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^[A-Z][A-Z0-9_-]{1,31}$"
          },
          "assertion": {
            "type": "string",
            "minLength": 1
          },
          "verificationCommand": {
            "type": "string"
          },
          "evidenceRequired": {
            "type": "boolean"
          }
        }
      }
    },
    "permissions": {
      "type": "object",
      "additionalProperties": false,
      "required": ["read", "write", "network", "secrets"],
      "properties": {
        "read": {
          "$ref": "#/$defs/stringList"
        },
        "write": {
          "$ref": "#/$defs/stringList"
        },
        "network": {
          "$ref": "#/$defs/stringList"
        },
        "secrets": {
          "$ref": "#/$defs/stringList"
        }
      }
    },
    "budget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["wallClockSeconds", "maxAttempts"],
      "properties": {
        "wallClockSeconds": {
          "type": "integer",
          "minimum": 1
        },
        "maxAttempts": {
          "type": "integer",
          "minimum": 1,
          "maximum": 20
        }
      }
    },
    "escalation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["on", "route"],
      "properties": {
        "on": {
          "$ref": "#/$defs/stringList"
        },
        "route": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "state": {
      "enum": [
        "proposed",
        "accepted",
        "running",
        "blocked",
        "verifying",
        "succeeded",
        "failed",
        "cancelled"
      ]
    },
    "attempt": {
      "type": "integer",
      "minimum": 0
    },
    "result": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "summary": {
          "type": "string"
        },
        "changedArtifacts": {
          "$ref": "#/$defs/stringList"
        },
        "evidenceUris": {
          "$ref": "#/$defs/stringList"
        },
        "unresolvedRisks": {
          "$ref": "#/$defs/stringList"
        }
      }
    }
  },
  "$defs": {
    "party": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "role"],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "role": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "stringList": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    }
  }
}
