{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://simdugas.github.io/retake/schema/retake-toml.schema.json",
  "title": "retake.toml",
  "description": "Retake's project configuration: redaction (ADR 0005), extra recorded headers (ADR 0005 item 2, ADR 0007 item 16) and replay matching rules (ADR 0008). Unknown keys are an error. The file has no version key (ADR 0011 item 7). Some checks happen only when Retake loads the file: regexes must compile, unbounded user patterns need max_len, names must be unique.",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "redact": {
      "description": "Redaction patterns, applied to everything before it is written (ADR 0005).",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "builtins": {
          "description": "false turns every built-in pattern off. Default true.",
          "type": "boolean",
          "default": true
        },
        "disable": {
          "description": "Built-in patterns to turn off, by name.",
          "type": "array",
          "items": {
            "enum": [
              "anthropic-api-key",
              "openai-api-key",
              "aws-access-key-id",
              "github-token",
              "slack-token",
              "google-api-key",
              "pem-private-key"
            ]
          }
        },
        "patterns": {
          "description": "Your own patterns, tried before the built-ins.",
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "regex"],
            "properties": {
              "name": {
                "description": "1-40 characters of a-z, 0-9 and -, not starting with -. Used in the placeholder, e.g. <redacted:employee-id:1>. Unique, and not retake-token.",
                "type": "string",
                "pattern": "^[a-z0-9][a-z0-9-]{0,39}$",
                "not": { "const": "retake-token" }
              },
              "regex": {
                "description": "A Rust regex (no look-around or backreferences).",
                "type": "string"
              },
              "max_len": {
                "description": "Longest match in bytes. Required when the regex has no upper bound (e.g. uses + or *): it sizes the window that catches secrets split across stream events.",
                "type": "integer",
                "minimum": 1
              }
            }
          }
        }
      }
    },
    "headers": {
      "description": "Headers recorded in addition to the built-in allow-list. Credential headers (authorization, x-api-key, cookie, ...) and x-retake-test are never recorded.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "request": {
          "description": "Request headers to record.",
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "match"],
            "properties": {
              "name": { "$ref": "#/$defs/header_name" },
              "match": {
                "description": "Whether the header must be equal for a recorded request to match on replay (ADR 0007 item 16). Required.",
                "type": "boolean"
              }
            }
          }
        },
        "response": {
          "description": "Response headers to record and replay.",
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name"],
            "properties": {
              "name": { "$ref": "#/$defs/header_name" }
            }
          }
        }
      }
    },
    "match": {
      "description": "Normalization rules for replay matching (ADR 0008).",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "unordered": {
          "description": "Raw paths (JSON Pointers, * for any key or index) of arrays compared in any order.",
          "type": "array",
          "items": { "$ref": "#/$defs/raw_path" }
        },
        "builtins": {
          "description": "Turn built-in normalizers on or off, or re-scope them.",
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "uuid": { "$ref": "#/$defs/builtin", "description": "UUIDs. On by default." },
            "iso-timestamp": { "$ref": "#/$defs/builtin", "description": "ISO 8601 timestamps. On by default, except in user messages." },
            "temp-path": { "$ref": "#/$defs/builtin", "description": "Temporary file paths. On by default." },
            "iso-date": { "$ref": "#/$defs/builtin", "description": "ISO 8601 dates. Off by default." },
            "unix-timestamp": { "$ref": "#/$defs/builtin", "description": "Unix timestamps. Off by default." },
            "crlf": { "type": "boolean", "description": "CRLF compared as LF. On by default." },
            "embedded-json": { "type": "boolean", "description": "Strings in tool results that hold JSON compared as JSON. On by default." },
            "unordered-tool-definitions": { "type": "boolean", "description": "Tool definitions compared in any order. On by default." }
          }
        },
        "rules": {
          "description": "Your own rules: a regex replacement (pattern and replace), a numeric tolerance, or a relaxed binary rule (media and match, ADR 0009 item 2).",
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "select"],
            "properties": {
              "name": {
                "description": "1-40 characters of a-z, 0-9 and -; not the name of a built-in. Shown in the replay summary.",
                "type": "string",
                "pattern": "^[a-z0-9-]{1,40}$"
              },
              "select": { "$ref": "#/$defs/selectors" },
              "except": { "$ref": "#/$defs/selectors", "description": "Selectors excluded from select." },
              "pattern": { "description": "A Rust regex; each match is replaced by a numbered placeholder.", "type": "string" },
              "replace": {
                "description": "The placeholder kind: matches become <KIND_1>, <KIND_2>, ...",
                "type": "string",
                "pattern": "^[A-Za-z0-9_-]{1,40}$"
              },
              "tolerance": {
                "description": "Absolute tolerance for JSON numbers in the selected values.",
                "type": "number",
                "minimum": 0
              },
              "media": {
                "description": "A media type or range (image/png, image/*, */*): binary parts (withheld or in a blob) of this range in the selected locations match any content of the range (ADR 0009 item 2). Goes with match = \"any\".",
                "type": "string",
                "pattern": "^[A-Za-z0-9.+_*-]+/[A-Za-z0-9.+_*-]+$"
              },
              "match": {
                "description": "How a media rule compares binary parts; the only value is any.",
                "const": "any"
              },
              "tests": { "$ref": "#/$defs/tests" }
            },
            "oneOf": [
              { "required": ["pattern", "replace"], "not": { "anyOf": [{ "required": ["tolerance"] }, { "required": ["media"] }, { "required": ["match"] }] } },
              { "required": ["tolerance"], "not": { "anyOf": [{ "required": ["pattern"] }, { "required": ["replace"] }, { "required": ["media"] }, { "required": ["match"] }] } },
              { "required": ["media", "match"], "not": { "anyOf": [{ "required": ["pattern"] }, { "required": ["replace"] }, { "required": ["tolerance"] }] } }
            ]
          }
        }
      }
    }
  },
  "$defs": {
    "header_name": {
      "description": "A header name (case-insensitive).",
      "type": "string",
      "pattern": "^\\s*[A-Za-z0-9!#$%&'*+.^_`|~-]+\\s*$"
    },
    "raw_path": {
      "description": "A JSON Pointer into the request body, where a segment * stands for any key or index, e.g. /messages/*/content.",
      "type": "string",
      "pattern": "^/"
    },
    "selector": {
      "description": "all, a region (system, user_messages, assistant_messages, tool_results, tool_call_args, tool_definitions), or a raw path starting with /.",
      "type": "string",
      "anyOf": [
        { "enum": ["all", "system", "user_messages", "assistant_messages", "tool_results", "tool_call_args", "tool_definitions"] },
        { "$ref": "#/$defs/raw_path" }
      ]
    },
    "selectors": {
      "description": "One selector or a non-empty list of them.",
      "anyOf": [
        { "$ref": "#/$defs/selector" },
        { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/selector" } }
      ]
    },
    "tests": {
      "description": "Globs on test IDs (* any run of characters, ? any one) limiting a rule to some tests. Rules scoped this way skip _main and _session takes.",
      "type": "array",
      "items": { "type": "string", "minLength": 1 }
    },
    "builtin": {
      "anyOf": [
        { "type": "boolean" },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "enabled": { "description": "Default true.", "type": "boolean" },
            "select": { "$ref": "#/$defs/selectors" },
            "except": { "$ref": "#/$defs/selectors" },
            "tests": { "$ref": "#/$defs/tests" }
          }
        }
      ]
    }
  }
}
