{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://yarramate.org/schema/reconciliation-report/v1",
  "title": "YarraMate reconciliation report",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "workspace", "summary", "findings"],
  "properties": {
    "format": { "const": "yarramate/reconciliation-report/v1" },
    "workspace": { "$ref": "#/$defs/id" },
    "summary": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "evidenceDocuments",
        "observations",
        "confirmed",
        "findings",
        "contradicted",
        "unknown",
        "notObserved",
        "subjectsWithoutEvidence"
      ],
      "properties": {
        "evidenceDocuments": { "type": "integer", "minimum": 0 },
        "observations": { "type": "integer", "minimum": 0 },
        "confirmed": { "type": "integer", "minimum": 0 },
        "findings": { "type": "integer", "minimum": 0 },
        "contradicted": { "type": "integer", "minimum": 0 },
        "unknown": { "type": "integer", "minimum": 0 },
        "notObserved": { "type": "integer", "minimum": 0 },
        "subjectsWithoutEvidence": { "type": "integer", "minimum": 0 },
        "unsupportedAbsences": {
        "type": "integer",
        "minimum": 0,
        "description": "not-observed observations that name no search. A not-observed asserts a negative about a tree nobody read exhaustively, and is the one result whose message nothing can check; this counts the ones offering a reader nothing to re-run."
      },
      "staleAttestations": { "type": "integer", "minimum": 0 },
        "unconfirmedAttestations": { "type": "integer", "minimum": 0 },
        "expectationsCompared": { "type": "integer", "minimum": 0 },
        "expectationsWithoutObservation": { "type": "integer", "minimum": 0 },
        "artifactsInScope": {
          "type": "integer",
          "minimum": 0,
          "description": "Files the declared coverage scope selected (ADR 0130). Present exactly when coverage was assessed, so a report without it is one that never looked, not one that found nothing."
        },
        "unclaimedArtifacts": {
          "type": "integer",
          "minimum": 0,
          "description": "Artifacts in scope that no observation's repo: locator claims (ADR 0130). Present exactly when coverage was assessed."
        }
      }
    },
    "findings": {
      "type": "array",
      "items": { "$ref": "#/$defs/finding" }
    },
    "unobservedSubjects": {
      "type": "array",
      "items": { "$ref": "#/$defs/subjectIdentity" }
    },
    "unobservedExpectations": {
      "type": "array",
      "items": { "$ref": "#/$defs/unobservedExpectation" }
    },
    "coverageScope": {
      "description": "The coverage patterns as the manifest declared them, echoed so the report is honest about what it was asked to look at (ADR 0130). Present exactly when coverage was assessed.",
      "type": "array",
      "items": { "type": "string", "minLength": 1 }
    },
    "unclaimedArtifacts": {
      "description": "Repository-relative paths of in-scope artifacts no observation claims, sorted (ADR 0130). Absence, never accusation: no finding is fabricated and check --strict does not read this. Present when the count is positive.",
      "type": "array",
      "items": { "type": "string", "minLength": 1 }
    },
    "notes": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 }
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
    },
    "qualifiedIdentity": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*(?:~[a-z][a-z0-9-]*)?$"
    },
    "evidenceIdentity": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*@[0-9]+\\.[0-9]+$"
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": { "enum": ["subject", "claim"] },
        "id": { "$ref": "#/$defs/qualifiedIdentity" }
      }
    },
    "evidenceLocator": {
      "type": "object",
      "additionalProperties": false,
      "required": ["uri"],
      "properties": {
        "uri": { "type": "string", "minLength": 1 },
        "message": { "type": "string", "minLength": 1 }
      }
    },
    "subjectIdentity": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
    },
    "assertedRelationship": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to", "kind"],
      "properties": {
        "from": { "$ref": "#/$defs/subjectIdentity" },
        "to": { "$ref": "#/$defs/subjectIdentity" },
        "kind": { "type": "string", "pattern": "^\\S+$" },
        "name": { "type": "string", "minLength": 1 }
      }
    },
    "observationKey": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._/-]*$"
    },
    "declaredSource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["document", "path", "pointer", "line", "column"],
      "properties": {
        "document": { "$ref": "#/$defs/id" },
        "path": { "type": "string", "minLength": 1 },
        "pointer": { "type": "string", "minLength": 1 },
        "line": { "type": "integer", "minimum": 1 },
        "column": { "type": "integer", "minimum": 1 }
      }
    },
    "expectation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["provider", "key", "expected", "observed", "declared"],
      "properties": {
        "provider": { "$ref": "#/$defs/id" },
        "key": { "$ref": "#/$defs/observationKey" },
        "expected": { "type": "string", "minLength": 1 },
        "observed": { "type": "string", "minLength": 1 },
        "declared": { "$ref": "#/$defs/declaredSource" }
      }
    },
    "unobservedExpectation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["claim", "subject", "provider", "key", "expected", "declared"],
      "properties": {
        "claim": { "$ref": "#/$defs/qualifiedIdentity" },
        "subject": { "$ref": "#/$defs/subjectIdentity" },
        "provider": { "$ref": "#/$defs/id" },
        "key": { "$ref": "#/$defs/observationKey" },
        "expected": { "type": "string", "minLength": 1 },
        "declared": { "$ref": "#/$defs/declaredSource" }
      }
    },
    "finding": {
      "oneOf": [
        { "$ref": "#/$defs/evidenceFinding" },
        { "$ref": "#/$defs/staleAttestationFinding" },
        { "$ref": "#/$defs/unconfirmedAttestationFinding" }
      ]
    },
    "unconfirmedAttestationFinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["target", "result", "attestation", "provider", "declared"],
      "properties": {
        "target": {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "id"],
          "properties": {
            "type": { "const": "subject" },
            "id": { "$ref": "#/$defs/subjectIdentity" }
          }
        },
        "result": { "const": "unconfirmed-attestation" },
        "attestation": {
          "type": "object",
          "additionalProperties": false,
          "required": ["topic", "by", "recordedBy", "on"],
          "properties": {
            "topic": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" },
            "by": { "type": "string", "minLength": 1 },
            "recordedBy": { "type": "string", "minLength": 1 },
            "on": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
            }
          }
        },
        "provider": { "const": "model" },
        "declared": { "$ref": "#/$defs/declaredSource" }
      }
    },
    "evidenceFinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "target",
        "result",
        "provider",
        "evidenceDocument",
        "evidence"
      ],
      "properties": {
        "target": { "$ref": "#/$defs/target" },
        "asserted": { "$ref": "#/$defs/assertedRelationship" },
        "expectation": { "$ref": "#/$defs/expectation" },
        "result": {
          "enum": ["contradicted", "unknown", "not-observed"]
        },
        "provider": { "$ref": "#/$defs/id" },
        "evidenceDocument": { "$ref": "#/$defs/evidenceIdentity" },
        "evidence": { "$ref": "#/$defs/evidenceLocator" }
      }
    },
    "staleAttestationFinding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["target", "result", "attestation", "provider", "evidence"],
      "properties": {
        "target": {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "id"],
          "properties": {
            "type": { "const": "subject" },
            "id": { "$ref": "#/$defs/subjectIdentity" }
          }
        },
        "result": { "const": "stale-attestation" },
        "attestation": {
          "type": "object",
          "additionalProperties": false,
          "required": ["topic", "by", "on"],
          "properties": {
            "topic": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" },
            "by": { "type": "string", "minLength": 1 },
            "on": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
            }
          }
        },
        "provider": { "const": "git" },
        "changedAt": { "type": "string", "minLength": 1 },
        "evidence": { "$ref": "#/$defs/evidenceLocator" }
      }
    }
  }
}
