{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cortex-agent.local/schemas/dispatch/idempotency.schema.json",
  "title": "Cortex Agent Dispatch Idempotency Record",
  "description": "A durable deduplication record for a future Dispatch attempt. Phase 0 does not create records.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "idempotency_key",
    "task_id",
    "status",
    "run_id",
    "request_digest",
    "created_at",
    "updated_at",
    "expires_at"
  ],
  "properties": {
    "schema_version": { "const": 1 },
    "idempotency_key": { "type": "string", "minLength": 1, "maxLength": 200 },
    "task_id": { "type": "string", "minLength": 1 },
    "status": { "enum": ["accepted", "running", "completed", "failed", "canceled"] },
    "run_id": { "type": ["string", "null"] },
    "request_digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "created_at": { "type": "string", "format": "date-time" },
    "updated_at": { "type": "string", "format": "date-time" },
    "expires_at": { "type": "string", "format": "date-time" },
    "failure": {
      "anyOf": [
        { "type": "null" },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["code", "message"],
          "properties": {
            "code": { "type": "string", "minLength": 1 },
            "message": { "type": "string", "minLength": 1 }
          }
        }
      ]
    }
  }
}
