{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://yycore.ai/schemas/corpus-evidence/manifest-v1.schema.json",
  "title": "YY Corpus Evidence manifest v1",
  "description": "Exact structural contract for manifests emitted by YY Corpus Evidence. Cross-record counts, identifier references, and manifest identity are additionally enforced by the verifier.",
  "type": "object",
  "required": [
    "schema_version",
    "path_mode",
    "source",
    "assets",
    "declarations",
    "issues",
    "summary",
    "manifest_id"
  ],
  "properties": {
    "schema_version": {
      "const": "yy-corpus-evidence/manifest/v1"
    },
    "path_mode": {
      "const": "plain"
    },
    "source": {
      "$ref": "#/$defs/source"
    },
    "assets": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/asset"
      }
    },
    "declarations": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/declaration"
      }
    },
    "issues": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/issue"
      }
    },
    "summary": {
      "$ref": "#/$defs/summary"
    },
    "manifest_id": {
      "$ref": "#/$defs/manifest_id"
    }
  },
  "additionalProperties": false,
  "$defs": {
    "non_empty_string": {
      "type": "string",
      "pattern": "\\S"
    },
    "normalized_relative_path": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?![\\s\\S]*\\\\)(?![\\s\\S]*/(?![\\s\\S]))(?![\\s\\S]*//)(?!(?:\\.|\\.\\.)(?:/|(?![\\s\\S])))(?![\\s\\S]*(?:/\\.|/\\.\\.)(?:/|(?![\\s\\S])))[\\s\\S]+"
    },
    "emitted_relative_path": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?![\\s\\S]*/(?![\\s\\S]))(?![\\s\\S]*//)(?!(?:\\.|\\.\\.)(?:/|(?![\\s\\S])))(?![\\s\\S]*(?:/\\.|/\\.\\.)(?:/|(?![\\s\\S])))[\\s\\S]+"
    },
    "source_id": {
      "type": "string",
      "pattern": "^source_v1_[0-9a-f]{32}$"
    },
    "asset_id": {
      "type": "string",
      "pattern": "^asset_v1_[0-9a-f]{32}$"
    },
    "declaration_id": {
      "type": "string",
      "pattern": "^declaration_v1_[0-9a-f]{32}$"
    },
    "manifest_id": {
      "type": "string",
      "pattern": "^manifest_v1_[0-9a-f]{32}$"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "source": {
      "type": "object",
      "required": [
        "name",
        "source_id"
      ],
      "properties": {
        "name": {
          "$ref": "#/$defs/non_empty_string"
        },
        "source_id": {
          "$ref": "#/$defs/source_id"
        }
      },
      "additionalProperties": false
    },
    "asset": {
      "type": "object",
      "required": [
        "asset_id",
        "byte_size",
        "declaration_ids",
        "effective_declaration_ids",
        "evidence_state",
        "issue_codes",
        "kind",
        "path",
        "sha256"
      ],
      "properties": {
        "asset_id": {
          "$ref": "#/$defs/asset_id"
        },
        "byte_size": {
          "anyOf": [
            {
              "type": "integer",
              "minimum": 0
            },
            {
              "type": "null"
            }
          ]
        },
        "declaration_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/declaration_id"
          },
          "uniqueItems": true
        },
        "effective_declaration_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/declaration_id"
          },
          "uniqueItems": true
        },
        "evidence_state": {
          "enum": [
            "unknown",
            "declared",
            "incomplete",
            "conflict"
          ]
        },
        "issue_codes": {
          "type": "array",
          "items": {
            "enum": [
              "metadata_unreadable",
              "unsupported_file_kind",
              "content_unreadable",
              "content_changed_during_scan"
            ]
          },
          "uniqueItems": true
        },
        "kind": {
          "enum": [
            "file",
            "symlink",
            "directory",
            "other",
            "unreadable"
          ]
        },
        "path": {
          "$ref": "#/$defs/emitted_relative_path"
        },
        "sha256": {
          "anyOf": [
            {
              "$ref": "#/$defs/sha256"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "oneOf": [
        {
          "properties": {
            "kind": {
              "const": "file"
            },
            "byte_size": {
              "type": "integer",
              "minimum": 0
            },
            "sha256": {
              "$ref": "#/$defs/sha256"
            },
            "issue_codes": {
              "const": []
            }
          }
        },
        {
          "properties": {
            "kind": {
              "const": "file"
            },
            "byte_size": {
              "type": "null"
            },
            "sha256": {
              "type": "null"
            },
            "issue_codes": {
              "enum": [
                [
                  "content_unreadable"
                ],
                [
                  "content_changed_during_scan"
                ]
              ]
            }
          }
        },
        {
          "properties": {
            "kind": {
              "const": "symlink"
            },
            "byte_size": {
              "type": "null"
            },
            "sha256": {
              "type": "null"
            },
            "issue_codes": {
              "const": []
            }
          }
        },
        {
          "properties": {
            "kind": {
              "const": "unreadable"
            },
            "byte_size": {
              "type": "null"
            },
            "sha256": {
              "type": "null"
            },
            "issue_codes": {
              "const": [
                "metadata_unreadable"
              ]
            }
          }
        },
        {
          "properties": {
            "kind": {
              "enum": [
                "directory",
                "other"
              ]
            },
            "byte_size": {
              "type": "null"
            },
            "sha256": {
              "type": "null"
            },
            "issue_codes": {
              "const": [
                "unsupported_file_kind"
              ]
            }
          }
        }
      ],
      "additionalProperties": false
    },
    "declaration": {
      "type": "object",
      "required": [
        "declaration_id",
        "scope_kind",
        "scope_path",
        "statement_kind",
        "statement_value",
        "basis",
        "declared_by",
        "declared_on",
        "evidence_files",
        "source_url",
        "note"
      ],
      "properties": {
        "declaration_id": {
          "$ref": "#/$defs/declaration_id"
        },
        "scope_kind": {
          "enum": [
            "exact",
            "prefix"
          ]
        },
        "scope_path": {
          "type": "string"
        },
        "statement_kind": {
          "enum": [
            "origin",
            "license-expression",
            "permission",
            "restriction",
            "sensitivity"
          ]
        },
        "statement_value": {
          "$ref": "#/$defs/non_empty_string"
        },
        "basis": {
          "enum": [
            "documented",
            "contractual",
            "first-party",
            "unverified"
          ]
        },
        "declared_by": {
          "$ref": "#/$defs/non_empty_string"
        },
        "declared_on": {
          "type": "string",
          "format": "date"
        },
        "evidence_files": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/normalized_relative_path"
          },
          "uniqueItems": true
        },
        "source_url": {
          "type": "string"
        },
        "note": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "scope_kind": {
                "const": "exact"
              }
            },
            "required": [
              "scope_kind"
            ]
          },
          "then": {
            "properties": {
              "scope_path": {
                "$ref": "#/$defs/normalized_relative_path"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "scope_kind": {
                "const": "prefix"
              }
            },
            "required": [
              "scope_kind"
            ]
          },
          "then": {
            "properties": {
              "scope_path": {
                "anyOf": [
                  {
                    "const": ""
                  },
                  {
                    "$ref": "#/$defs/normalized_relative_path"
                  }
                ]
              }
            }
          }
        }
      ],
      "additionalProperties": false
    },
    "declaration_issue": {
      "type": "object",
      "required": [
        "detail",
        "reason_code",
        "declaration_id"
      ],
      "properties": {
        "detail": {
          "type": "string"
        },
        "reason_code": {
          "enum": [
            "declaration_evidence_missing",
            "declaration_scope_empty"
          ]
        },
        "declaration_id": {
          "$ref": "#/$defs/declaration_id"
        }
      },
      "additionalProperties": false
    },
    "root_issue": {
      "type": "object",
      "required": [
        "detail",
        "reason_code",
        "subject"
      ],
      "properties": {
        "detail": {
          "type": "string"
        },
        "reason_code": {
          "enum": [
            "directory_unreadable",
            "source_empty"
          ]
        },
        "subject": {
          "const": "."
        }
      },
      "additionalProperties": false
    },
    "path_issue": {
      "type": "object",
      "required": [
        "detail",
        "reason_code",
        "path"
      ],
      "properties": {
        "detail": {
          "type": "string"
        },
        "reason_code": {
          "enum": [
            "directory_unreadable",
            "entry_unreadable",
            "source_root_not_walkable",
            "metadata_unreadable",
            "unsupported_file_kind",
            "content_unreadable",
            "content_changed_during_scan"
          ]
        },
        "path": {
          "$ref": "#/$defs/emitted_relative_path"
        }
      },
      "additionalProperties": false
    },
    "issue": {
      "oneOf": [
        {
          "$ref": "#/$defs/declaration_issue"
        },
        {
          "$ref": "#/$defs/root_issue"
        },
        {
          "$ref": "#/$defs/path_issue"
        }
      ]
    },
    "evidence_states": {
      "type": "object",
      "required": [
        "unknown",
        "declared",
        "incomplete",
        "conflict"
      ],
      "properties": {
        "unknown": {
          "type": "integer",
          "minimum": 0
        },
        "declared": {
          "type": "integer",
          "minimum": 0
        },
        "incomplete": {
          "type": "integer",
          "minimum": 0
        },
        "conflict": {
          "type": "integer",
          "minimum": 0
        }
      },
      "additionalProperties": false
    },
    "summary": {
      "type": "object",
      "required": [
        "asset_count",
        "issue_count",
        "evidence_states"
      ],
      "properties": {
        "asset_count": {
          "type": "integer",
          "minimum": 0
        },
        "issue_count": {
          "type": "integer",
          "minimum": 0
        },
        "evidence_states": {
          "$ref": "#/$defs/evidence_states"
        }
      },
      "additionalProperties": false
    }
  }
}
