{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/KazuCocoa/renma/blob/main/docs/schemas/trust-graph-v2.schema.json",
  "title": "Renma Trust Graph v2",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "summary", "nodes", "edges", "findings"],
  "properties": {
    "schemaVersion": { "const": "renma.trustGraph.v2" },
    "summary": { "$ref": "#/$defs/summary" },
    "nodes": { "type": "array", "items": { "$ref": "#/$defs/node" } },
    "edges": { "type": "array", "items": { "$ref": "#/$defs/edge" } },
    "findings": { "type": "array", "items": { "$ref": "#/$defs/finding" } }
  },
  "$defs": {
    "nodeTypes": {
      "enum": [
        "asset",
        "owner",
        "lifecycle_status",
        "security_profile",
        "effective_policy",
        "diagnostic"
      ]
    },
    "edgeTypes": {
      "enum": [
        "owned_by",
        "has_lifecycle_status",
        "declares_dependency",
        "references",
        "owns_local_resource",
        "statically_references",
        "inherits_owner",
        "selects_security_profile",
        "inherits_policy",
        "has_effective_policy",
        "has_diagnostic"
      ]
    },
    "severity": {
      "enum": ["critical", "high", "medium", "low", "error", "warning", "info"]
    },
    "summary": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "assetCount",
        "nodeCount",
        "edgeCount",
        "findingCount",
        "nodeTypeCounts",
        "edgeTypeCounts",
        "findingSeverityCounts",
        "riskClassCounts"
      ],
      "properties": {
        "assetCount": { "type": "integer", "minimum": 0 },
        "nodeCount": { "type": "integer", "minimum": 0 },
        "edgeCount": { "type": "integer", "minimum": 0 },
        "findingCount": { "type": "integer", "minimum": 0 },
        "nodeTypeCounts": { "$ref": "#/$defs/nodeCounts" },
        "edgeTypeCounts": { "$ref": "#/$defs/edgeCounts" },
        "findingSeverityCounts": { "$ref": "#/$defs/severityCounts" },
        "riskClassCounts": { "$ref": "#/$defs/riskCounts" }
      }
    },
    "nodeCounts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "asset",
        "owner",
        "lifecycle_status",
        "security_profile",
        "effective_policy",
        "diagnostic"
      ],
      "properties": {
        "asset": { "type": "integer", "minimum": 0 },
        "owner": { "type": "integer", "minimum": 0 },
        "lifecycle_status": { "type": "integer", "minimum": 0 },
        "security_profile": { "type": "integer", "minimum": 0 },
        "effective_policy": { "type": "integer", "minimum": 0 },
        "diagnostic": { "type": "integer", "minimum": 0 }
      }
    },
    "edgeCounts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "owned_by",
        "has_lifecycle_status",
        "declares_dependency",
        "references",
        "owns_local_resource",
        "statically_references",
        "inherits_owner",
        "selects_security_profile",
        "inherits_policy",
        "has_effective_policy",
        "has_diagnostic"
      ],
      "properties": {
        "owned_by": { "type": "integer", "minimum": 0 },
        "has_lifecycle_status": { "type": "integer", "minimum": 0 },
        "declares_dependency": { "type": "integer", "minimum": 0 },
        "references": { "type": "integer", "minimum": 0 },
        "owns_local_resource": { "type": "integer", "minimum": 0 },
        "statically_references": { "type": "integer", "minimum": 0 },
        "inherits_owner": { "type": "integer", "minimum": 0 },
        "selects_security_profile": { "type": "integer", "minimum": 0 },
        "inherits_policy": { "type": "integer", "minimum": 0 },
        "has_effective_policy": { "type": "integer", "minimum": 0 },
        "has_diagnostic": { "type": "integer", "minimum": 0 }
      }
    },
    "severityCounts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "critical",
        "high",
        "medium",
        "low",
        "error",
        "warning",
        "info"
      ],
      "properties": {
        "critical": { "type": "integer", "minimum": 0 },
        "high": { "type": "integer", "minimum": 0 },
        "medium": { "type": "integer", "minimum": 0 },
        "low": { "type": "integer", "minimum": 0 },
        "error": { "type": "integer", "minimum": 0 },
        "warning": { "type": "integer", "minimum": 0 },
        "info": { "type": "integer", "minimum": 0 }
      }
    },
    "riskCounts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["violation", "suspicious", "advisory", "unclassified"],
      "properties": {
        "violation": { "type": "integer", "minimum": 0 },
        "suspicious": { "type": "integer", "minimum": 0 },
        "advisory": { "type": "integer", "minimum": 0 },
        "unclassified": { "type": "integer", "minimum": 0 }
      }
    },
    "evidence": {
      "type": "object",
      "required": ["path", "startLine", "endLine", "snippet"],
      "properties": {
        "path": { "type": "string" },
        "startLine": { "type": "integer" },
        "endLine": { "type": "integer" },
        "snippet": { "type": "string" }
      },
      "additionalProperties": false
    },
    "inheritedFrom": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "sourcePath"],
      "properties": {
        "id": { "type": "string" },
        "sourcePath": { "type": "string" }
      }
    },
    "edgeProperties": {
      "type": "object",
      "properties": {
        "policySources": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "enum": [
              "local",
              "security_profile",
              "repository_config",
              "owning_skill"
            ]
          }
        },
        "ownershipSource": { "enum": ["declared", "inherited"] },
        "status": {
          "enum": [
            "experimental",
            "stable",
            "suspended",
            "revoked",
            "deprecated",
            "archived"
          ]
        },
        "statusReason": { "type": "string", "minLength": 1 },
        "statusChangedAt": { "type": "string" },
        "inheritedFrom": { "$ref": "#/$defs/inheritedFrom" }
      },
      "additionalProperties": true
    },
    "node": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "label"],
      "properties": {
        "id": { "type": "string" },
        "type": { "$ref": "#/$defs/nodeTypes" },
        "label": { "type": "string" },
        "properties": { "type": "object" },
        "evidence": { "type": "array", "items": { "$ref": "#/$defs/evidence" } }
      }
    },
    "edge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "from", "to", "type"],
      "properties": {
        "id": { "type": "string" },
        "from": { "type": "string" },
        "to": { "type": "string" },
        "type": { "$ref": "#/$defs/edgeTypes" },
        "properties": { "$ref": "#/$defs/edgeProperties" },
        "evidence": { "type": "array", "items": { "$ref": "#/$defs/evidence" } }
      },
      "allOf": [
        {
          "if": {
            "properties": { "type": { "const": "owned_by" } },
            "required": ["type"]
          },
          "then": {
            "required": ["properties"],
            "properties": {
              "properties": {
                "type": "object",
                "required": ["ownershipSource"],
                "allOf": [
                  {
                    "if": {
                      "type": "object",
                      "properties": {
                        "ownershipSource": { "const": "inherited" }
                      },
                      "required": ["ownershipSource"]
                    },
                    "then": {
                      "type": "object",
                      "required": ["inheritedFrom"]
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": { "const": "has_effective_policy" }
            },
            "required": ["type"]
          },
          "then": {
            "required": ["properties"],
            "properties": {
              "properties": {
                "type": "object",
                "required": ["policySources"],
                "allOf": [
                  {
                    "if": {
                      "type": "object",
                      "properties": {
                        "policySources": {
                          "type": "array",
                          "contains": { "const": "owning_skill" }
                        }
                      },
                      "required": ["policySources"]
                    },
                    "then": {
                      "type": "object",
                      "required": ["inheritedFrom"]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    },
    "finding": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source", "severity", "message"],
      "properties": {
        "source": { "enum": ["finding", "diagnostic"] },
        "severity": { "$ref": "#/$defs/severity" },
        "message": { "type": "string" },
        "path": { "type": "string" },
        "code": { "type": "string" },
        "id": { "type": "string" },
        "title": { "type": "string" },
        "riskClass": { "enum": ["violation", "suspicious", "advisory"] },
        "evidence": { "$ref": "#/$defs/evidence" }
      }
    }
  }
}
