{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://martinloop.com/telemetry-envelope.v1.json",
  "title": "MartinLoop Telemetry Envelope v1",
  "description": "Envelope POSTed by the Martin CLI to the telemetry-ingest endpoint. Authenticated with an API token that has the telemetry:write or runs:write scope.",
  "type": "object",
  "required": ["envelopeVersion", "loop"],
  "additionalProperties": false,
  "properties": {
    "envelopeVersion": { "type": "string", "const": "1" },
    "sentAt": { "type": "string", "format": "date-time" },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "type": { "type": "string", "enum": ["cli", "mcp", "ci", "other"] },
        "version": { "type": "string" }
      }
    },
    "workspaceId": {
      "type": "string",
      "format": "uuid",
      "description": "Optional. Must match the workspace of the API token. If omitted, the token's workspace is used."
    },
    "loop": {
      "type": "object",
      "required": ["loopId", "status"],
      "additionalProperties": false,
      "properties": {
        "loopId": { "type": "string", "minLength": 1 },
        "projectId": { "type": ["string", "null"] },
        "status": { "type": "string", "description": "success | budget | policy | error | running | canceled" },
        "lifecycleState": { "type": ["string", "null"] },
        "task": { "type": ["string", "null"] },
        "budget": { "type": ["object", "null"] },
        "cost": { "type": ["object", "null"] },
        "artifacts": { "type": ["array", "object", "null"] },
        "attempts": { "type": ["array", "integer", "null"] },
        "metadata": { "type": ["object", "null"] },
        "receiptScope": { "type": ["object", "null"] },
        "receiptIntegrity": { "type": ["object", "null"] },
        "createdAt": { "type": ["string", "null"], "format": "date-time" },
        "updatedAt": { "type": ["string", "null"], "format": "date-time" }
      }
    }
  }
}