{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.connectsoft.ai/reference/schemas/run-event-envelope.schema.json",
  "title": "RunEventEnvelope (ADR-0014 port-level envelope)",
  "description": "Envelope of every Factory integration event published through the ConnectSoft.Extensions.MessagingModel port. Trusted identity fields are set by the producer from its persisted state; traceId is diagnostic only and never an authorization or idempotency key.",
  "type": "object",
  "required": ["eventId", "eventType", "schemaVersion", "occurredAt", "producer", "tenantId", "correlationId", "payload"],
  "additionalProperties": false,
  "properties": {
    "eventId": { "type": "string", "format": "uuid", "description": "Deduplication key within the consumer's inbox retention window." },
    "eventType": { "type": "string", "pattern": "^[A-Z][A-Za-z0-9]+$", "description": "e.g. RunAccepted" },
    "topic": { "type": "string", "pattern": "^[a-z][a-z0-9-]*\\.[a-z][a-z0-9-]*\\.v[0-9]+\\.[a-z][a-z0-9-]*$", "description": "context.area.vN.event-name; context = producing bounded context slug." },
    "schemaVersion": { "type": "integer", "minimum": 1 },
    "occurredAt": { "type": "string", "format": "date-time" },
    "producer": { "type": "object", "required": ["service"], "properties": { "service": { "type": "string" }, "instance": { "type": "string" }, "boundedContext": { "type": "string" } } },
    "tenantId": { "type": "string" },
    "projectId": { "type": "string" },
    "runId": { "type": "string", "format": "uuid" },
    "stepId": { "type": "string", "format": "uuid" },
    "attemptId": { "type": "string", "format": "uuid" },
    "aggregate": { "type": "object", "properties": { "type": { "type": "string" }, "id": { "type": "string" }, "version": { "type": "integer" } } },
    "correlationId": { "type": "string" },
    "causationId": { "type": "string", "description": "eventId or commandId that caused this event." },
    "traceId": { "type": "string", "description": "W3C trace id for diagnostics only." },
    "orderingKey": { "type": "string", "description": "Partition/ordering key; runId for run-scoped events." },
    "classification": { "enum": ["public", "internal", "confidential", "restricted"], "default": "internal" },
    "payload": { "type": "object", "description": "Event-specific body; defined per event in the platform events pages." }
  }
}
