{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://barglabs.ai/dunstan/spec/schema/survey-0.1-draft.schema.json",
  "title": "Dunstan survey record, DRAFT 0.1",
  "description": "DRAFT, informative, for the operator to check (spec/claim-format.md, \"DRAFT survey 0.1\"). A read-only survey of a repository's recent pull requests: the selection, one entry per selected pull request with the record a check wrote for it or the reason there is none, and an aggregate that is a pure function of the entries. Each embedded record is a 0.1 or DRAFT 0.2.0 record and is validated against its own schema when the survey is verified. It changes neither record schema.",
  "type": "object",
  "required": ["format", "checker", "selection", "entries", "aggregate", "rerun", "digest"],
  "additionalProperties": false,
  "properties": {
    "format": { "const": "dunstan-survey-0.1-draft" },
    "checker": { "$ref": "record-0.1.schema.json#/$defs/predicate/properties/checker" },
    "selection": {
      "type": "object",
      "required": ["parameters", "reader", "listing", "selected"],
      "additionalProperties": false,
      "properties": {
        "parameters": { "$ref": "#/$defs/parameters" },
        "reader": {
          "description": "Whether the survey read GitHub with a token (GITHUB_TOKEN was set). The aggregate's remedy lines are worded for it. The token is never recorded.",
          "type": "object",
          "required": ["authenticated"],
          "additionalProperties": false,
          "properties": { "authenticated": { "type": "boolean" } }
        },
        "listing": { "$ref": "#/$defs/listing" },
        "selected": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/number" }
        }
      }
    },
    "entries": {
      "description": "One per selected pull request, in selection order. Verify checks that their pull request numbers are exactly `selection.selected`, which holds no number twice.",
      "type": "array",
      "uniqueItems": true,
      "items": { "$ref": "#/$defs/entry" }
    },
    "aggregate": { "$ref": "#/$defs/aggregate" },
    "rerun": {
      "type": "object",
      "required": ["offline", "online"],
      "additionalProperties": false,
      "properties": {
        "offline": { "type": "string", "minLength": 1 },
        "online": { "type": "string", "minLength": 1 }
      }
    },
    "digest": {
      "description": "SHA-256 of the JCS bytes of the survey record without this member.",
      "type": "object",
      "required": ["sha256"],
      "additionalProperties": false,
      "properties": { "sha256": { "$ref": "record-0.1.schema.json#/$defs/sha256" } }
    }
  },
  "$defs": {
    "number": { "type": "integer", "minimum": 1 },
    "count": { "type": "integer", "minimum": 0 },
    "login": { "type": "string", "pattern": "^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})(?:\\[bot\\])?$" },
    "parameters": {
      "type": "object",
      "required": ["repository", "last", "since", "authors", "report", "advisory"],
      "additionalProperties": false,
      "properties": {
        "repository": { "$ref": "record-0.1.schema.json#/$defs/repository" },
        "last": { "type": "integer", "minimum": 1, "maximum": 200 },
        "since": {
          "oneOf": [
            { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" },
            { "type": "null" }
          ]
        },
        "authors": { "type": "array", "items": { "$ref": "#/$defs/login" } },
        "report": {
          "oneOf": [
            {
              "type": "object",
              "required": ["kind"],
              "additionalProperties": false,
              "properties": { "kind": { "const": "pr-body" } }
            },
            {
              "type": "object",
              "required": ["kind", "author"],
              "additionalProperties": false,
              "properties": {
                "kind": { "const": "pr-comment" },
                "author": { "$ref": "#/$defs/login" }
              }
            }
          ]
        },
        "advisory": { "type": "boolean" }
      }
    },
    "listingPage": {
      "type": "object",
      "required": ["page", "locator", "sha256", "readAt", "items", "duplicates"],
      "additionalProperties": false,
      "properties": {
        "page": { "$ref": "#/$defs/number" },
        "locator": { "type": "string", "minLength": 1 },
        "sha256": { "$ref": "record-0.1.schema.json#/$defs/sha256" },
        "readAt": { "$ref": "record-0.1.schema.json#/$defs/timestamp" },
        "items": { "$ref": "#/$defs/count" },
        "duplicates": {
          "description": "How many of this page's items repeat a pull request number an earlier item of the same read already held. In the read used, they are not in `pullRequests`.",
          "$ref": "#/$defs/count"
        },
        "error": { "type": "string", "pattern": "^(?:http_[1-5][0-9]{2}|network|parse)$" }
      }
    },
    "listing": {
      "description": "The read of the listing the selection is from, the last one, and the reads abandoned before it. A read is abandoned at its first page that repeats an earlier item, unless it is the third read, which is never abandoned.",
      "type": "object",
      "required": ["pageBound", "pages", "abandoned", "pullRequests", "duplicates", "end"],
      "additionalProperties": false,
      "properties": {
        "pageBound": { "type": "integer", "minimum": 1 },
        "pages": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/listingPage" }
        },
        "abandoned": {
          "description": "One item per abandoned read, in order: its pages, the last of them the page that repeated an earlier item. No pull request list; abandoned reads contribute nothing to the selection. Empty when the first read was used.",
          "type": "array",
          "maxItems": 2,
          "items": {
            "type": "object",
            "required": ["pages"],
            "additionalProperties": false,
            "properties": {
              "pages": {
                "type": "array",
                "minItems": 1,
                "items": { "$ref": "#/$defs/listingPage" }
              }
            }
          }
        },
        "duplicates": {
          "description": "How many listed items repeated an earlier one: the sum of the pages' duplicates. The pages' items are the length of `pullRequests` plus this.",
          "$ref": "#/$defs/count"
        },
        "pullRequests": {
          "description": "Each listed pull request once, at its first occurrence, in listing order. Verify refuses a number listed twice.",
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "object",
            "required": ["number", "createdAt", "author", "merged"],
            "additionalProperties": false,
            "properties": {
              "number": { "$ref": "#/$defs/number" },
              "createdAt": { "$ref": "record-0.1.schema.json#/$defs/timestamp" },
              "author": { "oneOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }] },
              "merged": { "type": "boolean" }
            }
          }
        },
        "end": {
          "enum": [
            "selection_full",
            "since_reached",
            "listing_ended",
            "listing_unreadable",
            "page_bound"
          ]
        }
      }
    },
    "noRecordReason": {
      "enum": [
        "pull_request_unreadable",
        "pull_request_moved",
        "rate_limited",
        "report_unavailable"
      ]
    },
    "entry": {
      "oneOf": [
        {
          "type": "object",
          "required": ["pullRequest", "record"],
          "additionalProperties": false,
          "properties": {
            "pullRequest": { "$ref": "#/$defs/number" },
            "record": {
              "description": "The record `dunstan check` writes for this pull request, as written: a 0.1 record, or a DRAFT 0.2.0 record when the survey ran the advisory reader. Verify validates it against its own schema.",
              "type": "object"
            }
          }
        },
        {
          "type": "object",
          "required": ["pullRequest", "record", "reason"],
          "additionalProperties": false,
          "properties": {
            "pullRequest": { "$ref": "#/$defs/number" },
            "record": { "type": "null" },
            "reason": { "$ref": "#/$defs/noRecordReason" }
          }
        }
      ]
    },
    "columns": {
      "type": "object",
      "required": ["supported", "unverifiable", "notRecordCheckable", "contradicted"],
      "properties": {
        "supported": { "$ref": "#/$defs/count" },
        "unverifiable": { "$ref": "#/$defs/count" },
        "notRecordCheckable": { "$ref": "#/$defs/count" },
        "contradicted": { "$ref": "#/$defs/count" }
      }
    },
    "fixed": { "type": "string", "pattern": "^[01]\\.[0-9]{4}$" },
    "rate": {
      "type": "object",
      "required": ["numerator", "denominator", "value", "wilson95"],
      "additionalProperties": false,
      "properties": {
        "numerator": { "$ref": "#/$defs/count" },
        "denominator": { "$ref": "#/$defs/count" },
        "value": { "oneOf": [{ "$ref": "#/$defs/fixed" }, { "type": "null" }] },
        "wilson95": {
          "oneOf": [
            {
              "type": "object",
              "required": ["low", "high"],
              "additionalProperties": false,
              "properties": {
                "low": { "$ref": "#/$defs/fixed" },
                "high": { "$ref": "#/$defs/fixed" }
              }
            },
            { "type": "null" }
          ]
        }
      }
    },
    "aggregate": {
      "type": "object",
      "required": [
        "mapping",
        "pullRequests",
        "claims",
        "byType",
        "cannotSettle",
        "contradictedOfSettled",
        "unsettled"
      ],
      "additionalProperties": false,
      "properties": {
        "mapping": {
          "type": "object",
          "required": ["version"],
          "additionalProperties": false,
          "properties": { "version": { "type": "string", "minLength": 1 } }
        },
        "pullRequests": {
          "type": "object",
          "required": [
            "listed",
            "selected",
            "withRecord",
            "blockFound",
            "blockMissing",
            "blockInvalid",
            "blockAmbiguous",
            "noRecord"
          ],
          "additionalProperties": false,
          "properties": {
            "listed": { "$ref": "#/$defs/count" },
            "selected": { "$ref": "#/$defs/count" },
            "withRecord": { "$ref": "#/$defs/count" },
            "blockFound": { "$ref": "#/$defs/count" },
            "blockMissing": { "$ref": "#/$defs/count" },
            "blockInvalid": { "$ref": "#/$defs/count" },
            "blockAmbiguous": { "$ref": "#/$defs/count" },
            "noRecord": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["reason", "count"],
                "additionalProperties": false,
                "properties": {
                  "reason": { "$ref": "#/$defs/noRecordReason" },
                  "count": { "$ref": "#/$defs/count" }
                }
              }
            }
          }
        },
        "claims": {
          "allOf": [{ "$ref": "#/$defs/columns" }],
          "required": ["total"],
          "properties": { "total": { "$ref": "#/$defs/count" } },
          "unevaluatedProperties": false
        },
        "byType": {
          "type": "array",
          "items": {
            "allOf": [{ "$ref": "#/$defs/columns" }],
            "required": ["type"],
            "properties": {
              "type": { "enum": ["T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "R"] }
            },
            "unevaluatedProperties": false
          }
        },
        "cannotSettle": { "$ref": "#/$defs/rate" },
        "contradictedOfSettled": { "$ref": "#/$defs/rate" },
        "unsettled": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["reason", "column", "count", "remedy"],
            "additionalProperties": false,
            "properties": {
              "reason": { "$ref": "record-0.1.schema.json#/$defs/claimReason" },
              "column": { "enum": ["unverifiable", "notRecordCheckable"] },
              "count": { "type": "integer", "minimum": 1 },
              "remedy": { "oneOf": [{ "type": "string", "minLength": 1 }, { "type": "null" }] }
            }
          }
        },
        "advisory": {
          "type": "object",
          "required": ["pullRequests", "notes"],
          "additionalProperties": false,
          "properties": {
            "pullRequests": { "$ref": "#/$defs/count" },
            "notes": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["note", "count"],
                "additionalProperties": false,
                "properties": {
                  "note": { "type": "string", "minLength": 1 },
                  "count": { "type": "integer", "minimum": 1 }
                }
              }
            }
          }
        }
      }
    }
  }
}
