{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://simdugas.github.io/retake/schema/take-v1.schema.json",
  "title": "Retake take record, format 1",
  "description": "One line (record) of a take file in format 1 (header \"format\": 1). A take is JSONL: one JSON object per line. Fields not listed here are allowed and ignored by Retake (ADR 0011 item 2); a record type not listed here is refused. Rules across lines (header first, footer last, exchange numbering) are in schema/README.md.",
  "type": "object",
  "required": ["type"],
  "properties": {
    "type": {
      "description": "The record type. Any other value refuses the take: a new record type always comes with a new format version.",
      "enum": ["header", "request", "response", "event", "stream_end", "dropped", "resource", "footer"]
    }
  },
  "allOf": [
    { "if": { "properties": { "type": { "const": "header" } } }, "then": { "$ref": "#/$defs/header" } },
    { "if": { "properties": { "type": { "const": "request" } } }, "then": { "$ref": "#/$defs/request" } },
    { "if": { "properties": { "type": { "const": "response" } } }, "then": { "$ref": "#/$defs/response" } },
    { "if": { "properties": { "type": { "const": "event" } } }, "then": { "$ref": "#/$defs/event" } },
    { "if": { "properties": { "type": { "const": "stream_end" } } }, "then": { "$ref": "#/$defs/stream_end" } },
    { "if": { "properties": { "type": { "const": "dropped" } } }, "then": { "$ref": "#/$defs/dropped" } },
    { "if": { "properties": { "type": { "const": "resource" } } }, "then": { "$ref": "#/$defs/resource" } },
    { "if": { "properties": { "type": { "const": "footer" } } }, "then": { "$ref": "#/$defs/footer" } }
  ],
  "$defs": {
    "header": {
      "description": "The first line of every take: format version and the little metadata Retake records (ADR 0001 item 4, ADR 0010 items 1-2).",
      "required": ["format", "test_id", "recorded_at"],
      "properties": {
        "format": {
          "description": "The take format version (ADR 0011). This schema describes format 1.",
          "const": 1
        },
        "test_id": {
          "description": "The runner's full test ID (ADR 0002 item 3); \"_main\" for a take recorded without a test-runner plugin, \"_session\" for requests made outside any test while a plugin was active.",
          "type": "string"
        },
        "recorded_at": {
          "description": "UTC wall-clock time the recording started, with optional millisecond precision. Used to freeze time on replay (ADR 0006).",
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]{3})?Z$"
        },
        "retake_version": {
          "description": "The Retake version that recorded the take (ADR 0010 item 1). Absent in takes recorded before the field existed: the version is then unknown, which is never stale.",
          "type": "string"
        },
        "allow_unscanned": {
          "description": "Media ranges stored unscanned with --allow-unscanned while recording (ADR 0005 item 9), e.g. \"image/*\". Absent when empty.",
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "exchange": {
      "description": "Index of the exchange the record belongs to. Records of one exchange share it; numbers are unique per request.",
      "type": "integer",
      "minimum": 0
    },
    "offset_ms": {
      "description": "Milliseconds since the response headers arrived.",
      "type": "integer",
      "minimum": 0
    },
    "headers": {
      "description": "Allow-listed headers (ADR 0005 item 2) as [name, value] pairs in the order received; names are lowercase.",
      "type": "array",
      "items": {
        "type": "array",
        "prefixItems": [{ "type": "string" }, { "type": "string" }],
        "minItems": 2,
        "maxItems": 2
      }
    },
    "body": {
      "description": "A body is stored under at most one key: body (parsed JSON, redacted), body_text (UTF-8 text that is not JSON, redacted), body_withheld (content Retake could not scan), body_blob (content stored unscanned with --allow-unscanned, in a blob file, ADR 0009 item 3), body_multipart (the parts of a multipart/form-data request), body_base64 (content stored unscanned inline by earlier builds; read, no longer written). No key means an empty body. Inside a JSON body, base64 blocks ({\"type\": \"base64\", \"media_type\": ..., \"data\": ...}) are stored in one of three forms instead of data (ADR 0009): text (text-like content decoded and redacted, see base64_text), withheld and bytes (unscannable content, hash only, see withheld_part) or blob and bytes (opted-in content in a blob file, see blob).",
      "properties": {
        "body": { "description": "The body parsed as JSON, redacted. Key order and whitespace are not preserved; number text is." },
        "body_text": { "description": "A UTF-8 body that is not JSON, redacted.", "type": "string" },
        "body_withheld": { "$ref": "#/$defs/withheld" },
        "body_blob": { "$ref": "#/$defs/blob" },
        "body_multipart": {
          "description": "A multipart/form-data request, part by part, in order (ADR 0009 item 11). The boundary is not stored; the content-type header is stored as multipart/form-data. Matching compares the parts as the JSON document {\"multipart\": [...]}.",
          "type": "array",
          "items": { "$ref": "#/$defs/multipart_part" }
        },
        "body_base64": { "description": "Content stored unscanned inline, base64-encoded (standard alphabet, padded), as builds before blob files wrote it. Read, no longer written.", "type": "string" }
      },
      "not": {
        "anyOf": [
          { "required": ["body", "body_text"] },
          { "required": ["body", "body_withheld"] },
          { "required": ["body", "body_blob"] },
          { "required": ["body", "body_multipart"] },
          { "required": ["body", "body_base64"] },
          { "required": ["body_text", "body_withheld"] },
          { "required": ["body_text", "body_blob"] },
          { "required": ["body_text", "body_multipart"] },
          { "required": ["body_text", "body_base64"] },
          { "required": ["body_withheld", "body_blob"] },
          { "required": ["body_withheld", "body_multipart"] },
          { "required": ["body_withheld", "body_base64"] },
          { "required": ["body_blob", "body_multipart"] },
          { "required": ["body_blob", "body_base64"] },
          { "required": ["body_multipart", "body_base64"] }
        ]
      }
    },
    "sha256": {
      "description": "A SHA-256 hash as sha256:<64 lowercase hex digits>.",
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "blob": {
      "description": "A reference to a blob file (ADR 0009 items 3-5): content stored unscanned with --allow-unscanned lives in <takes-dir>/_blobs/sha256-<hex>.<ext>, the extension from the media type (bin when unknown). Every referenced blob must exist and match its hash, or the take is refused (exit 2). The same three keys, next to type: base64, replace data in a base64 block of a JSON body.",
      "type": "object",
      "required": ["blob", "bytes", "media_type"],
      "properties": {
        "blob": { "$ref": "#/$defs/sha256", "description": "SHA-256 of the content (decoded, for a base64 block)." },
        "bytes": { "description": "The content's length in bytes.", "type": "integer", "minimum": 0 },
        "media_type": { "description": "The content's media type.", "type": "string" }
      }
    },
    "multipart_part": {
      "description": "One part of a multipart/form-data request. A text field (no filename, or a text-like media type) that is UTF-8 has text, redacted; any other part is a binary part with bytes and either withheld (hash only) or blob (opted in, ADR 0009 item 3). name and filename are redacted.",
      "type": "object",
      "properties": {
        "name": { "description": "The form field name.", "type": "string" },
        "filename": { "description": "The uploaded file's name.", "type": "string" },
        "media_type": { "description": "The part's content type, without parameters.", "type": "string" },
        "text": { "type": "string" },
        "withheld": { "$ref": "#/$defs/sha256" },
        "blob": { "$ref": "#/$defs/sha256" },
        "bytes": { "type": "integer", "minimum": 0 }
      },
      "oneOf": [
        { "required": ["text"], "not": { "anyOf": [{ "required": ["withheld"] }, { "required": ["blob"] }] } },
        { "required": ["withheld", "bytes", "media_type"], "not": { "anyOf": [{ "required": ["text"] }, { "required": ["blob"] }] } },
        { "required": ["blob", "bytes", "media_type"], "not": { "anyOf": [{ "required": ["text"] }, { "required": ["withheld"] }] } }
      ]
    },
    "withheld_part": {
      "description": "A base64 block of a JSON request body that Retake could not scan (ADR 0009 items 1, 6): data replaced by withheld (SHA-256 of the decoded content, or of the text when it is not valid base64) and bytes. Matching compares the hash, unless a media rule relaxes it.",
      "type": "object",
      "required": ["type", "media_type", "withheld", "bytes"],
      "properties": {
        "type": { "const": "base64" },
        "withheld": { "$ref": "#/$defs/sha256" },
        "bytes": { "type": "integer", "minimum": 0 }
      }
    },
    "base64_text": {
      "description": "A text-like base64 block (text/*, application/json, application/xml, +json, +xml) whose content decodes as UTF-8 (ADR 0009 item 7): data replaced by the decoded, redacted text. Replay re-encodes it as base64.",
      "type": "object",
      "required": ["type", "media_type", "text"],
      "properties": {
        "type": { "const": "base64" },
        "media_type": { "type": "string" },
        "text": { "type": "string" }
      }
    },
    "withheld": {
      "description": "A body Retake could not scan and did not store (ADR 0005 item 9, ADR 0009 item 6). Replaying a withheld response answers 502 retake_withheld.",
      "type": "object",
      "required": ["withheld", "bytes", "media_type"],
      "properties": {
        "withheld": { "$ref": "#/$defs/sha256", "description": "SHA-256 of the body." },
        "bytes": { "description": "The body's length in bytes.", "type": "integer", "minimum": 0 },
        "media_type": { "description": "The body's media type.", "type": "string" }
      },
      "additionalProperties": false
    },
    "request": {
      "description": "A request as sent by the client, redacted (ADR 0001 items 2-4, ADR 0005). Stored in full on every exchange.",
      "required": ["exchange", "provider", "method", "path", "headers"],
      "properties": {
        "exchange": { "$ref": "#/$defs/exchange" },
        "provider": { "description": "The provider whose route the request came in on.", "enum": ["anthropic"] },
        "method": { "description": "HTTP method, e.g. POST.", "type": "string" },
        "path": { "description": "Request path without the query, e.g. /v1/messages.", "type": "string" },
        "query": { "description": "The query string without the leading ?, redacted. Absent when there was none.", "type": "string" },
        "headers": { "$ref": "#/$defs/headers" }
      },
      "$ref": "#/$defs/body"
    },
    "response": {
      "description": "The response to the request with the same exchange number. A streamed response (stream: true) has no body: its events follow as event records, ended by a stream_end record (ADR 0004).",
      "required": ["exchange", "status", "headers"],
      "properties": {
        "exchange": { "$ref": "#/$defs/exchange" },
        "status": { "description": "HTTP status code.", "type": "integer", "minimum": 0, "maximum": 65535 },
        "headers": { "$ref": "#/$defs/headers" },
        "stream": { "description": "true for a streamed (server-sent events) response; absent otherwise.", "type": "boolean" }
      },
      "$ref": "#/$defs/body",
      "if": { "properties": { "stream": { "const": true } }, "required": ["stream"] },
      "then": {
        "not": {
          "anyOf": [
            { "required": ["body"] },
            { "required": ["body_text"] },
            { "required": ["body_withheld"] },
            { "required": ["body_blob"] },
            { "required": ["body_multipart"] },
            { "required": ["body_base64"] }
          ]
        }
      }
    },
    "event": {
      "description": "One server-sent event of a streamed response, stored when it completed (ADR 0004). data holds the event's data when it parses as JSON, data_text otherwise; neither when the event had no data line.",
      "required": ["exchange", "offset_ms"],
      "properties": {
        "exchange": { "$ref": "#/$defs/exchange" },
        "offset_ms": { "$ref": "#/$defs/offset_ms" },
        "event": { "description": "The SSE event field; absent when the event had none.", "type": "string" },
        "id": { "description": "The SSE id field; absent when the event had none.", "type": "string" },
        "data": { "description": "The event's data parsed as JSON, redacted. Never null: a JSON null is stored as data_text. Base64 blocks in it are stored as in other JSON bodies (ADR 0009 item 9): decoded text, a blob reference, or withheld (hash only), in which case the whole stream is withheld on replay." , "not": { "type": "null" } },
        "data_text": { "description": "The event's data as text when it is not JSON (e.g. [DONE]), redacted.", "type": "string" }
      },
      "not": { "required": ["data", "data_text"] }
    },
    "stream_end": {
      "description": "The end of a streamed response. A stream without one was still in flight when the recording stopped and is not replayed.",
      "required": ["exchange", "offset_ms", "outcome"],
      "properties": {
        "exchange": { "$ref": "#/$defs/exchange" },
        "offset_ms": { "$ref": "#/$defs/offset_ms" },
        "outcome": {
          "description": "complete: the provider ended the stream; upstream_error: the provider connection failed mid-stream; client_closed: the client stopped reading (reproduced on replay).",
          "enum": ["complete", "upstream_error", "client_closed"]
        }
      }
    },
    "dropped": {
      "description": "Marks an earlier exchange as not to be replayed: a transient error superseded by a successful retry (ADR 0003 item 4). Its records stay in the file.",
      "required": ["exchange", "retried_by"],
      "properties": {
        "exchange": { "$ref": "#/$defs/exchange" },
        "retried_by": { "description": "The exchange that retried it successfully.", "type": "integer", "minimum": 0 }
      }
    },
    "resource": {
      "description": "A server-side resource (file, message batch, ...) that an exchange of the recording created or deleted (ADR 0001 item 4, ADR 0003 item 9), written after that exchange's records. Informational during replay (ADR 0007 item 17). In _resource.take, records copied from other takes also carry take and test_id, and retake reap marks resources it deleted that a committed take names with action reaped.",
      "required": ["provider", "kind", "id", "action"],
      "properties": {
        "exchange": {
          "description": "The exchange that created or deleted it: in this take, or in the take named by take. Required unless action is reaped.",
          "type": "integer",
          "minimum": 0
        },
        "provider": { "description": "The provider's route prefix, e.g. anthropic.", "type": "string", "minLength": 1 },
        "kind": { "description": "The resource kind, e.g. file or message_batch.", "type": "string", "minLength": 1 },
        "id": { "description": "The provider's ID for the resource.", "type": "string", "minLength": 1 },
        "action": {
          "description": "created or deleted by the exchange; reaped: deleted by retake reap (only in _resource.take).",
          "enum": ["created", "deleted", "reaped"]
        },
        "take": {
          "description": "In _resource.take: the take the record was copied from, relative to the takes directory, with / separators.",
          "type": "string"
        },
        "test_id": { "description": "In _resource.take: that take's test ID.", "type": "string" }
      },
      "if": { "properties": { "action": { "enum": ["created", "deleted"] } } },
      "then": { "required": ["exchange"] }
    },
    "footer": {
      "description": "The last line of a finished take (ADR 0001 item 4). A take without one is incomplete and is not replayed. Shared takes (_main, _session) carry run_exit; per-test takes carry outcome.",
      "properties": {
        "run_exit": {
          "description": "Exit code of the wrapped command (128 + signal number when killed by a signal), or null when the run never exited and retake finalize completed the take after a crash (ADR 0002 item 6).",
          "type": ["integer", "null"]
        },
        "outcome": {
          "description": "How the test ended (ADR 0003 item 3). failed only appears with --keep-failed; replay refuses such a take unless --allow-failed is given.",
          "enum": ["passed", "failed"]
        },
        "unscanned": {
          "description": "Every part stored without being scanned, with --allow-unscanned (ADR 0005 item 9). Absent when empty.",
          "type": "array",
          "items": {
            "type": "object",
            "required": ["exchange", "record", "pointer", "media_type", "bytes"],
            "properties": {
              "exchange": { "$ref": "#/$defs/exchange" },
              "record": { "description": "The record holding the part: request, response, or event (a base64 block in a streamed event's data, ADR 0009 item 9; pointer is then within that event's data).", "enum": ["request", "response", "event"] },
              "pointer": { "description": "JSON Pointer to the part within the record's body; empty for the whole body.", "type": "string" },
              "media_type": { "description": "The part's media type.", "type": "string" },
              "bytes": { "description": "The part's length in bytes.", "type": "integer", "minimum": 0 },
              "blob": { "$ref": "#/$defs/sha256", "description": "The blob file holding the part (ADR 0009 item 3)." }
            }
          }
        }
      }
    }
  }
}
