{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://accordprotocol.ai/schemas/agreement.v0.schema.json",
  "title": "Accord Agreement Object v0",
  "description": "Machine-readable record of a work agreement between two autonomous parties. See specs/ACCORD-001-agreement-object.md for the normative spec.",
  "type": "object",
  "required": [
    "type",
    "version",
    "agreement_id",
    "created_at",
    "buyer",
    "seller",
    "task",
    "price",
    "payment",
    "verification",
    "settlement"
  ],
  "additionalProperties": true,
  "propertyNames": { "not": { "pattern": "^accord_" } },
  "properties": {
    "type": { "const": "accord.agreement.v0" },
    "version": { "const": "v0" },
    "agreement_id": {
      "type": "string",
      "pattern": "^acc_[0-9A-HJKMNP-TV-Z]{26}$"
    },
    "created_at": { "$ref": "#/$defs/iso8601_utc" },
    "buyer": { "$ref": "#/$defs/party" },
    "seller": { "$ref": "#/$defs/party" },
    "task": { "$ref": "#/$defs/task" },
    "price": { "$ref": "#/$defs/price" },
    "payment": { "$ref": "#/$defs/payment" },
    "verification": { "$ref": "#/$defs/verification" },
    "settlement": { "$ref": "#/$defs/settlement" },
    "metadata": { "type": "object" },
    "signatures": {
      "type": "array",
      "items": { "$ref": "#/$defs/signature" }
    }
  },
  "$defs": {
    "iso8601_utc": {
      "type": "string",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
    },
    "party": {
      "type": "object",
      "required": ["id"],
      "additionalProperties": true,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^(agent|provider|verifier|human)://"
        },
        "wallet": {
          "type": "string",
          "pattern": "^(ergo|eth|base|rosen):"
        }
      }
    },
    "task": {
      "type": "object",
      "required": ["kind", "input_ref", "description"],
      "additionalProperties": true,
      "properties": {
        "kind": { "type": "string", "pattern": "^[a-z][a-z0-9_]*$" },
        "input_ref": {
          "type": "string",
          "pattern": "^(github|https|ipfs|data|inline):"
        },
        "description": { "type": "string", "maxLength": 1024 },
        "output_schema": { "type": "string" },
        "output_hash": {
          "type": "string",
          "pattern": "^(blake2b256|keccak256|sha256):0x[0-9a-f]{64}$"
        }
      }
    },
    "price": {
      "type": "object",
      "required": ["amount", "currency", "decimals"],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "description": "Decimal string. JSON numbers are forbidden because they break canonical encoding.",
          "type": "string",
          "pattern": "^(0|[1-9][0-9]*)(\\.[0-9]+)?$"
        },
        "currency": {
          "type": "string",
          "enum": ["ERG", "USDC", "USDT", "rsUSDT", "rsUSDC", "rsBTC"]
        },
        "decimals": { "type": "integer", "minimum": 0, "maximum": 18 }
      }
    },
    "payment": {
      "type": "object",
      "required": ["mode", "rail", "deadline"],
      "additionalProperties": true,
      "properties": {
        "mode": {
          "type": "string",
          "enum": ["note", "escrow", "pay_before_response", "batchable"]
        },
        "rail": {
          "type": "string",
          "enum": ["ergo", "rosen", "base", "x402"]
        },
        "reserve_ref": { "type": "string" },
        "deadline": {
          "description": "Either '+N blocks', '+N seconds', or absolute ISO-8601 UTC.",
          "type": "string",
          "pattern": "^(\\+[0-9]+ (blocks|seconds)|[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)$"
        }
      },
      "if": { "properties": { "mode": { "const": "note" } } },
      "then": { "required": ["reserve_ref"] }
    },
    "verification": {
      "type": "object",
      "required": ["required", "method"],
      "additionalProperties": true,
      "properties": {
        "required": { "type": "boolean" },
        "method": {
          "type": "string",
          "enum": ["verifier_receipt", "onchain_predicate", "none"]
        },
        "verifier": {
          "type": "string",
          "pattern": "^(verifier|agent|human)://"
        },
        "predicate": { "type": "string" },
        "evidence_required": {
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "allOf": [
        {
          "if": { "properties": { "method": { "const": "verifier_receipt" } } },
          "then": { "required": ["verifier"] }
        },
        {
          "if": { "properties": { "method": { "const": "onchain_predicate" } } },
          "then": { "required": ["predicate"] }
        }
      ]
    },
    "settlement": {
      "type": "object",
      "required": ["mode", "refund_policy", "dispute_policy"],
      "additionalProperties": true,
      "properties": {
        "mode": {
          "type": "string",
          "enum": ["inline", "batchable", "manual"]
        },
        "refund_policy": {
          "type": "string",
          "enum": ["expiry", "manual", "none"]
        },
        "dispute_policy": {
          "type": "string",
          "enum": ["verifier_panel", "manual_review", "none"]
        }
      }
    },
    "signature": {
      "type": "object",
      "required": ["by", "scheme", "public_key", "signature"],
      "additionalProperties": false,
      "properties": {
        "by": { "type": "string" },
        "scheme": {
          "type": "string",
          "enum": ["ed25519", "secp256k1", "ergo-sigma"]
        },
        "public_key": { "type": "string", "pattern": "^0x[0-9a-f]+$" },
        "signature": { "type": "string", "pattern": "^0x[0-9a-f]+$" }
      }
    }
  }
}
