{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"$id": "https://docs.inspector.rest/schemas/ingest.schema.json",
	"title": "inspector.rest ingest envelope",
	"description": "Body of POST /api/ingest (the JSON batch). Unknown properties are ignored by receivers; a breaking change bumps `v`.",
	"type": "object",
	"required": ["v", "sdk", "sent_at", "instance_id", "seq", "batch_id", "dropped", "records"],
	"properties": {
		"v": { "const": 1, "description": "Wire format version." },
		"sdk": { "$ref": "#/definitions/sdk" },
		"sent_at": { "type": "number", "description": "Client wall clock (epoch ms) when this send attempt was assembled. The server compares it with its own receive time to correct clock skew. Re-sent batches carry a fresh value." },
		"service": { "type": ["string", "null"], "description": "Optional service name set in the SDK config." },
		"instance_id": { "type": "string", "minLength": 1, "description": "Random id of the SDK instance (one per process/inspector)." },
		"seq": { "type": "integer", "minimum": 1, "description": "Batch sequence number within the instance, starting at 1." },
		"batch_id": { "type": "string", "minLength": 1, "description": "`<instance_id>:<seq>`. A re-sent batch (retry after timeout, 5xx, 503) keeps its batch_id, so the server can discard duplicates inside a short window." },
		"dropped": { "$ref": "#/definitions/dropped" },
		"records": {
			"type": "array",
			"description": "One record per request. Empty for the quota probe.",
			"items": { "$ref": "#/definitions/record" }
		}
	},
	"definitions": {
		"sdk": {
			"type": "object",
			"required": ["name", "version"],
			"properties": {
				"name": { "type": "string", "description": "`inspector-rest-sdk-node`, `inspector-rest-sdk-php`." },
				"version": { "type": "string" },
				"runtime": { "type": ["string", "null"], "description": "Free-form runtime description, e.g. `node/24.1.0`." }
			}
		},
		"dropped": {
			"type": "object",
			"description": "What the SDK discarded since the previous batch that carried counts. Counts travel with exactly one batch (the same batch_id when re-sent).",
			"required": ["records", "text_bodies", "binary_bodies"],
			"properties": {
				"records": { "type": "integer", "minimum": 0, "description": "Whole records discarded (queue overflow, load budget, permanently rejected batches)." },
				"text_bodies": { "type": "integer", "minimum": 0, "description": "Text bodies omitted because of the load budget." },
				"binary_bodies": { "type": "integer", "minimum": 0, "description": "Binary bodies not uploaded because of the load budget or size limit (not counted when `capture_binary` is off)." }
			}
		},
		"labels": {
			"type": "object",
			"description": "Fully resolved labels of the request (runtime < config < context < request). Keys and values are non-empty strings.",
			"additionalProperties": { "type": "string" }
		},
		"headers": {
			"type": "object",
			"description": "Header names are lower-case. Repeated headers (set-cookie) are arrays.",
			"additionalProperties": {
				"anyOf": [
					{ "type": "string" },
					{ "type": "array", "items": { "type": "string" } }
				]
			}
		},
		"error": {
			"type": "object",
			"properties": {
				"phase": { "type": ["string", "null"], "description": "`network`, `http`, `parse`, `domain`, or an engine-specific phase." },
				"name": { "type": ["string", "null"], "description": "Error class name; HTTP status errors are named `HttpError`." },
				"message": { "type": ["string", "null"] },
				"code": { "type": ["string", "number", "null"] },
				"stack": { "type": ["string", "null"] },
				"request_id": { "type": ["string", "null"] },
				"status": { "type": ["integer", "null"], "description": "HTTP status for `HttpError`." },
				"details": { "description": "Free-form data returned by the `error_details` hook." },
				"cause": { "$ref": "#/definitions/error", "description": "`error.cause` chain (depth-limited)." },
				"errors": {
					"type": "array",
					"items": { "$ref": "#/definitions/error" },
					"description": "Members of an `AggregateError`."
				}
			}
		},
		"body": {
			"type": "object",
			"description": "Descriptor of a request or response body.",
			"required": ["encoding"],
			"properties": {
				"encoding": {
					"enum": ["utf8", "part", "none"],
					"description": "`utf8`: the body text is inline in `text`. `part`: raw bytes travel in the multipart part named `part`. `none`: only metadata (see `omitted`)."
				},
				"size": { "type": ["integer", "null"], "minimum": 0, "description": "Size of the original body in bytes (before truncation), null when unknown (streams)." },
				"content_type": { "type": ["string", "null"] },
				"text": { "type": "string", "description": "Body text (encoding `utf8`). Possibly truncated, see `truncated`." },
				"truncated": { "type": "boolean", "description": "`text` was cut at `max_body_bytes` (UTF-8 character boundary)." },
				"binary": { "type": "boolean", "description": "Detected as binary (content type or invalid UTF-8)." },
				"part": { "type": "string", "minLength": 1, "description": "Multipart part name (encoding `part`)." },
				"omitted": {
					"enum": ["binary", "load", "too_large", "stream"],
					"description": "Why no content is carried (encoding `none`). `binary`: binary capture is off. `load`: dropped by the load budget. `too_large`: above `max_binary_bytes`. `stream`: body not readable (stream, FormData)."
				}
			}
		},
		"request_part": {
			"type": "object",
			"properties": {
				"headers": { "$ref": "#/definitions/headers" },
				"proxy_url": { "type": ["string", "null"] },
				"body": { "anyOf": [{ "$ref": "#/definitions/body" }, { "type": "null" }] }
			}
		},
		"response_part": {
			"type": "object",
			"properties": {
				"status": { "type": "integer" },
				"headers": { "$ref": "#/definitions/headers" },
				"duration_ms": { "type": ["number", "null"], "description": "Time to response headers." },
				"read_ms": { "type": ["number", "null"], "description": "Time spent reading the body." },
				"parse_ok": { "type": ["boolean", "null"], "description": "false when the engine could not parse the body (raw body is kept)." },
				"body": { "anyOf": [{ "$ref": "#/definitions/body" }, { "type": "null" }] }
			}
		},
		"retry": {
			"type": "object",
			"properties": {
				"policy": { "description": "Retry policy name or description." },
				"stage": { "type": ["string", "null"] },
				"delay_ms": { "type": ["number", "null"] },
				"exhausted": { "type": ["boolean", "null"] }
			}
		},
		"attempt": {
			"type": "object",
			"required": ["attempt"],
			"properties": {
				"attempt": { "type": "integer", "minimum": 1 },
				"offset_ms": { "type": "number", "description": "Start of the attempt relative to the record's `started_at`." },
				"method": { "type": "string", "description": "Only when different from the record's method." },
				"url": { "type": "string", "description": "Only when different from the record's url." },
				"request": { "anyOf": [{ "$ref": "#/definitions/request_part" }, { "type": "null" }] },
				"response": { "anyOf": [{ "$ref": "#/definitions/response_part" }, { "type": "null" }] },
				"retry": { "anyOf": [{ "$ref": "#/definitions/retry" }, { "type": "null" }] },
				"error": { "anyOf": [{ "$ref": "#/definitions/error" }, { "type": "null" }] }
			}
		},
		"record": {
			"type": "object",
			"description": "One request with all its attempts. A record with the same request_id replaces the previous one, except that an `in_progress` record never replaces a complete one (batches may arrive out of order).",
			"required": ["request_id", "started_at", "in_progress", "method", "url", "labels", "outcome", "duration_ms", "attempts"],
			"properties": {
				"request_id": { "type": "string", "minLength": 1 },
				"started_at": { "type": "number", "description": "Request start, epoch ms on the client clock (sub-millisecond fraction allowed)." },
				"in_progress": { "type": "boolean", "description": "true: sent while the request is still running (after 2 s) or at close; `outcome` and `duration_ms` are null." },
				"method": { "type": "string" },
				"url": { "type": "string" },
				"labels": { "$ref": "#/definitions/labels" },
				"outcome": { "enum": ["ok", "error", null] },
				"duration_ms": { "type": ["number", "null"], "description": "Total duration measured with a monotonic clock." },
				"error": { "anyOf": [{ "$ref": "#/definitions/error" }, { "type": "null" }], "description": "Request-level error (for example `domain` errors that belong to no attempt, or the final error given at settle time)." },
				"attempts": {
					"type": "array",
					"items": { "$ref": "#/definitions/attempt" },
					"description": "Ascending by attempt number. The server derives status, outcome and error phase from the highest numbered attempt."
				}
			}
		}
	}
}
