{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://oms.local/schemas/dispatch-payload.schema.json",
  "title": "OMS canonical dispatch payload",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "dispatch_id", "goal_id", "goal_version", "ticket_id", "ticket_version", "lease_token", "attempt",
    "worker_record_id", "worker_id", "session_id", "role_id", "model_id", "engine", "provider_mode",
    "worktree_id", "worktree_path", "worktree_branch", "worktree_base_commit",
    "prior_failure_evidence", "prior_reflection_evidence", "capabilities",
    "budget_snapshot", "acceptance_criteria", "evidence_requirements", "task_body", "operation_nonce",
    "policy_version", "policy_digest", "created_at", "updated_at", "delivery_event_id", "accepted_at",
    "acknowledgement", "result", "executor_evidence", "resolution", "resolved_at"
  ],
  "properties": {
    "dispatch_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "goal_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "goal_version": { "type": "integer", "minimum": 0 },
    "ticket_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "ticket_version": { "type": "integer", "minimum": 0 },
    "lease_token": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "attempt": { "type": "integer", "minimum": 1 },
    "worker_record_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "worker_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "session_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "role_id": { "enum": ["oms.ticket-executor", "oms.goal-long-executor"] },
    "model_id": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$" },
    "engine": { "enum": ["claude", "codex"] },
    "provider_mode": { "enum": ["claude_interactive", "codex_goal"] },
    "worktree_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "worktree_path": { "type": "string", "pattern": "^/", "maxLength": 4096 },
    "worktree_branch": { "type": "string", "minLength": 1, "maxLength": 255 },
    "worktree_base_commit": { "type": "string", "pattern": "^[a-f0-9]{40}(?:[a-f0-9]{24})?$" },
    "worktree_start_commit": { "type": "string", "pattern": "^[a-f0-9]{40}(?:[a-f0-9]{24})?$" },
    "prior_failure_evidence": {
      "type": ["object", "null"], "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9.-]{0,132}[a-z0-9])?\\.json$" }
      }
    },
    "prior_reflection_evidence": {
      "type": ["object", "null"], "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9.-]{0,132}[a-z0-9])?\\.json$" }
      }
    },
    "capabilities": {
      "type": "array", "minItems": 2, "maxItems": 2,
      "items": { "enum": ["repository-read", "ticket-worktree-write"] }
    },
    "budget_snapshot": {
      "type": "object", "additionalProperties": false,
      "required": ["profile", "remaining_attempts", "policy_digest"],
      "properties": {
        "profile": { "const": "subscription-only" },
        "remaining_attempts": { "type": "integer", "minimum": 1 },
        "policy_digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
      }
    },
    "acceptance_criteria": { "type": "array", "minItems": 1, "maxItems": 32, "items": { "type": "string", "minLength": 1, "maxLength": 4096 } },
    "evidence_requirements": { "type": "array", "minItems": 1, "maxItems": 32, "items": { "type": "string", "minLength": 1, "maxLength": 4096 } },
    "task_body": { "type": "string", "minLength": 1, "maxLength": 32768 },
    "operation_nonce": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "dispatch_intent_sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "advisor_evidence": {
      "type": "object", "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9.-]{0,132}[a-z0-9])?\\.json$" }
      }
    },
    "policy_version": { "const": 2 },
    "policy_digest": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "created_at": { "type": "string", "minLength": 20, "maxLength": 24 },
    "updated_at": { "type": "string", "minLength": 20, "maxLength": 24 },
    "delivery_event_id": { "type": ["string", "null"], "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
    "accepted_at": { "type": ["string", "null"], "minLength": 20, "maxLength": 24 },
    "acknowledgement": {
      "type": ["object", "null"], "additionalProperties": false,
      "required": ["dispatch_id", "worker_record_id", "session_id", "operation_nonce", "acknowledged_at"],
      "properties": {
        "dispatch_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "worker_record_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "session_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "operation_nonce": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "acknowledged_at": { "type": "string", "minLength": 20, "maxLength": 24 }
      }
    },
    "result": {
      "type": ["object", "null"], "additionalProperties": false,
      "required": ["dispatch_id", "goal_id", "goal_version", "worker_record_id", "session_id", "ticket_id", "attempt", "lease_token", "worktree_id", "worktree_path", "worktree_commit", "status", "summary", "operation_nonce", "reported_at"],
      "properties": {
        "dispatch_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "goal_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "goal_version": { "type": "integer", "minimum": 0 },
        "worker_record_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "session_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "ticket_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "attempt": { "type": "integer", "minimum": 1 },
        "lease_token": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "worktree_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "worktree_path": { "type": "string", "pattern": "^/", "maxLength": 4096 },
        "worktree_commit": { "type": "string", "pattern": "^[a-f0-9]{40}(?:[a-f0-9]{24})?$" },
        "status": { "enum": ["done", "blocked"] },
        "summary": { "type": "string", "minLength": 1, "maxLength": 8192 },
        "operation_nonce": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "reported_at": { "type": "string", "minLength": 20, "maxLength": 24 }
      }
    },
    "executor_evidence": {
      "type": ["object", "null"], "additionalProperties": false,
      "required": ["evidence_id", "sha256", "file_name"],
      "properties": {
        "evidence_id": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$" },
        "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "file_name": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9.-]{0,132}[a-z0-9])?\\.json$" }
      }
    },
    "resolution": { "enum": [null, "done", "blocked"] },
    "resolved_at": { "type": ["string", "null"], "minLength": 20, "maxLength": 24 }
  }
}
