{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://workspai.dev/schemas/workspace-intelligence/workspace-knowledge-graph.v1.json",
  "title": "Workspai Evidence-backed Workspace Knowledge Graph",
  "description": "Portable, provider-neutral workspace entities, relations and proof paths derived from and structurally bound to the canonical Workspace Model. The embedded project topology is the model's graph projection.",
  "type": "object",
  "required": [
    "schemaVersion",
    "generatedAt",
    "source",
    "workspace",
    "projectTopology",
    "entities",
    "relations",
    "proofs",
    "providers",
    "quality",
    "diagnostics"
  ],
  "properties": {
    "schemaVersion": { "const": "workspace-knowledge-graph.v1" },
    "generatedAt": { "type": "string", "format": "date-time" },
    "source": {
      "type": "object",
      "required": ["kind", "artifact", "hashAlgorithm", "hash"],
      "properties": {
        "kind": {
          "const": "workspace-model",
          "description": "The canonical Workspace Model is the sole source-of-truth authority for an integrated CLI knowledge graph."
        },
        "artifact": {
          "const": ".workspai/reports/workspace-model.json",
          "description": "Canonical model artifact whose stable structural projection is identified by source.hash."
        },
        "hashAlgorithm": { "const": "sha256" },
        "hash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$",
          "description": "SHA-256 of the stable structural Workspace Model projection. Volatile timestamps, run correlation, live evidence and freshness fields are intentionally excluded."
        },
        "inputs": {
          "type": "object",
          "required": ["schemaVersion", "strategy", "hashAlgorithm", "hash", "scopes"],
          "properties": {
            "schemaVersion": { "const": "workspace-knowledge-graph-inputs.v1" },
            "strategy": { "const": "hybrid-git-content-v2" },
            "hashAlgorithm": { "const": "sha256" },
            "hash": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
            "scopes": {
              "type": "array",
              "items": {
                "type": "object",
                "required": [
                  "kind",
                  "id",
                  "strategy",
                  "hash",
                  "fileCount",
                  "fileLimit",
                  "truncated"
                ],
                "properties": {
                  "kind": { "enum": ["workspace", "project"] },
                  "id": { "type": "string", "minLength": 1 },
                  "strategy": { "enum": ["git-worktree-v2", "content-merkle-v1"] },
                  "hash": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
                  "fileCount": { "type": "integer", "minimum": 0 },
                  "fileLimit": { "type": "integer", "minimum": 1 },
                  "truncated": { "type": "boolean" },
                  "eligibleFileCount": { "type": "integer", "minimum": 0 },
                  "eligibleFileCountExact": { "type": "boolean" },
                  "inventoryMode": { "enum": ["complete", "emergency-bounded"] },
                  "inventoryStrategy": {
                    "enum": ["git-index-worktree-v1", "filesystem-bfs-v1"]
                  },
                  "selection": {
                    "type": "object",
                    "required": [
                      "strategy",
                      "semanticFileCount",
                      "semanticFileBudget",
                      "deepFileCount",
                      "deepFileBudget",
                      "sourceExtractionFileBudget"
                    ],
                    "properties": {
                      "strategy": { "const": "component-language-round-robin-v1" },
                      "semanticFileCount": { "type": "integer", "minimum": 0 },
                      "semanticFileBudget": { "type": "integer", "minimum": 1 },
                      "deepFileCount": { "type": "integer", "minimum": 0 },
                      "deepFileBudget": { "type": "integer", "minimum": 1 },
                      "sourceExtractionFileBudget": { "type": "integer", "minimum": 1 }
                    },
                    "additionalProperties": false
                  }
                },
                "additionalProperties": false
              }
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "workspace": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "profile": { "type": "string" }
      },
      "additionalProperties": false
    },
    "projectTopology": { "$ref": "workspace-dependency-graph.v1.json" },
    "entities": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["id", "kind", "label", "identity", "attributes", "proofIds"],
        "properties": {
          "id": { "type": "string", "minLength": 1 },
          "kind": {
            "enum": [
              "workspace",
              "project",
              "service",
              "api",
              "endpoint",
              "schema",
              "protocol",
              "language",
              "package",
              "runtime-unit",
              "lifecycle-stage",
              "module",
              "file",
              "symbol",
              "database",
              "queue",
              "container",
              "deployment",
              "pipeline",
              "environment",
              "document",
              "decision",
              "test-suite",
              "owner"
            ]
          },
          "label": { "type": "string", "minLength": 1 },
          "projectId": { "type": "string", "minLength": 1 },
          "identity": {
            "type": "object",
            "required": ["key", "scope", "aliases", "fingerprint"],
            "properties": {
              "key": { "type": "string", "minLength": 1 },
              "scope": { "enum": ["workspace", "project"] },
              "aliases": { "type": "array", "items": { "type": "string" } },
              "fingerprint": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
            },
            "additionalProperties": false
          },
          "attributes": {
            "type": "object",
            "additionalProperties": {
              "type": ["string", "number", "boolean", "null", "array"],
              "items": { "type": ["string", "number", "boolean", "null"] }
            }
          },
          "proofIds": {
            "type": "array",
            "items": { "type": "string", "minLength": 1 },
            "uniqueItems": true
          }
        },
        "additionalProperties": false
      }
    },
    "relations": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["id", "from", "to", "kind", "derivation", "trust", "confidence", "proofIds"],
        "properties": {
          "id": { "type": "string", "minLength": 1 },
          "from": { "type": "string", "minLength": 1 },
          "to": { "type": "string", "minLength": 1 },
          "kind": {
            "enum": [
              "contains",
              "defines",
              "imports",
              "depends-on",
              "exposes",
              "implements",
              "implements-protocol",
              "uses-language",
              "equivalent-to",
              "calls",
              "reads-from",
              "writes-to",
              "publishes",
              "consumes",
              "deploys",
              "runs-on",
              "routes-to",
              "documents",
              "decided-by",
              "tests",
              "owns",
              "generated-by",
              "generated-from",
              "configured-by",
              "references"
            ]
          },
          "derivation": { "enum": ["extracted", "inferred", "authored"] },
          "trust": { "enum": ["authoritative", "corroborated", "observed", "ambiguous"] },
          "confidence": { "enum": ["high", "medium", "low"] },
          "proofIds": {
            "type": "array",
            "items": { "type": "string", "minLength": 1 },
            "minItems": 1,
            "uniqueItems": true
          }
        },
        "additionalProperties": false
      }
    },
    "proofs": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "provider",
          "artifact",
          "observedAt",
          "derivation",
          "trust",
          "confidence",
          "freshness"
        ],
        "properties": {
          "id": { "type": "string", "minLength": 1 },
          "provider": { "type": "string", "minLength": 1 },
          "artifact": {
            "type": "string",
            "minLength": 1,
            "not": { "pattern": "^(?:/|[A-Za-z]:[\\\\/])" }
          },
          "pointer": { "type": "string" },
          "line": { "type": "integer", "minimum": 1 },
          "column": { "type": "integer", "minimum": 1 },
          "contentHash": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
          "observedAt": { "type": "string", "format": "date-time" },
          "derivation": { "enum": ["extracted", "inferred", "authored"] },
          "trust": { "enum": ["authoritative", "corroborated", "observed", "ambiguous"] },
          "confidence": { "enum": ["high", "medium", "low"] },
          "freshness": { "enum": ["fresh", "stale", "unknown"] },
          "detail": { "type": "string" }
        },
        "additionalProperties": false
      }
    },
    "providers": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "version",
          "status",
          "permission",
          "discoveredEntities",
          "discoveredRelations",
          "proofCount",
          "diagnostics"
        ],
        "properties": {
          "id": { "type": "string", "minLength": 1 },
          "version": { "type": "string", "minLength": 1 },
          "status": { "enum": ["passed", "partial", "skipped", "failed"] },
          "permission": { "enum": ["filesystem-read", "git-read", "runtime-probe", "network"] },
          "discoveredEntities": { "type": "integer", "minimum": 0 },
          "discoveredRelations": { "type": "integer", "minimum": 0 },
          "proofCount": { "type": "integer", "minimum": 0 },
          "diagnostics": { "type": "array", "items": { "type": "string" } },
          "inputCoverage": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "scope",
                "scopeId",
                "tier",
                "status",
                "eligibleFiles",
                "suppliedFiles",
                "selectionStrategy"
              ],
              "properties": {
                "scope": { "enum": ["workspace", "project"] },
                "scopeId": { "type": "string", "minLength": 1 },
                "tier": {
                  "enum": ["complete-inventory", "adaptive-semantic", "adaptive-deep", "derived"]
                },
                "status": { "enum": ["complete", "bounded", "not-applicable"] },
                "eligibleFiles": { "type": "integer", "minimum": 0 },
                "suppliedFiles": { "type": "integer", "minimum": 0 },
                "fileBudget": { "type": "integer", "minimum": 1 },
                "selectionStrategy": {
                  "enum": ["complete", "component-language-round-robin-v1", "derived"]
                }
              },
              "additionalProperties": false
            }
          }
        },
        "additionalProperties": false
      }
    },
    "quality": {
      "type": "object",
      "required": [
        "entityCount",
        "relationCount",
        "proofCount",
        "entityProofCoverageRatio",
        "relationProofCoverageRatio",
        "providerSuccessRatio",
        "conflictCount",
        "unknownCount",
        "portable",
        "secretValuesEmitted"
      ],
      "properties": {
        "entityCount": { "type": "integer", "minimum": 0 },
        "relationCount": { "type": "integer", "minimum": 0 },
        "proofCount": { "type": "integer", "minimum": 0 },
        "entityProofCoverageRatio": { "type": "number", "minimum": 0, "maximum": 1 },
        "relationProofCoverageRatio": { "type": "number", "minimum": 0, "maximum": 1 },
        "providerSuccessRatio": { "type": "number", "minimum": 0, "maximum": 1 },
        "conflictCount": { "type": "integer", "minimum": 0 },
        "unknownCount": { "type": "integer", "minimum": 0 },
        "bindingCoverage": {
          "type": "object",
          "required": [
            "apiImplementation",
            "apiRuntimeRegistration",
            "projectTests",
            "projectDeployment",
            "projectOwnership"
          ],
          "properties": {
            "apiImplementation": { "$ref": "#/$defs/bindingCoverage" },
            "apiRuntimeRegistration": { "$ref": "#/$defs/bindingCoverage" },
            "projectTests": { "$ref": "#/$defs/bindingCoverage" },
            "projectDeployment": { "$ref": "#/$defs/bindingCoverage" },
            "projectOwnership": { "$ref": "#/$defs/bindingCoverage" }
          },
          "additionalProperties": false
        },
        "completeness": {
          "type": "object",
          "required": ["status", "inventory", "providers"],
          "properties": {
            "status": { "enum": ["complete", "bounded"] },
            "inventory": {
              "type": "object",
              "required": [
                "scopeCount",
                "completeScopes",
                "boundedScopes",
                "eligibleFiles",
                "indexedFiles",
                "eligibleFileCountExact"
              ],
              "properties": {
                "scopeCount": { "type": "integer", "minimum": 0 },
                "completeScopes": { "type": "integer", "minimum": 0 },
                "boundedScopes": { "type": "integer", "minimum": 0 },
                "eligibleFiles": { "type": "integer", "minimum": 0 },
                "indexedFiles": { "type": "integer", "minimum": 0 },
                "eligibleFileCountExact": { "type": "boolean" }
              },
              "additionalProperties": false
            },
            "providers": {
              "type": "object",
              "required": ["complete", "bounded", "notApplicable", "failed"],
              "properties": {
                "complete": { "type": "integer", "minimum": 0 },
                "bounded": { "type": "integer", "minimum": 0 },
                "notApplicable": { "type": "integer", "minimum": 0 },
                "failed": { "type": "integer", "minimum": 0 }
              },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        },
        "portable": { "const": true },
        "secretValuesEmitted": { "const": false }
      },
      "additionalProperties": false
    },
    "diagnostics": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["code", "severity", "message"],
        "properties": {
          "code": { "type": "string", "minLength": 1 },
          "severity": { "enum": ["info", "warning", "error"] },
          "message": { "type": "string", "minLength": 1 },
          "entityIds": { "type": "array", "items": { "type": "string" } },
          "relationIds": { "type": "array", "items": { "type": "string" } },
          "recommendation": { "type": "string" }
        },
        "additionalProperties": false
      }
    }
  },
  "$defs": {
    "bindingCoverage": {
      "type": "object",
      "required": ["eligibleCount", "boundCount", "unknownCount", "coverageRatio"],
      "properties": {
        "eligibleCount": { "type": "integer", "minimum": 0 },
        "boundCount": { "type": "integer", "minimum": 0 },
        "unknownCount": { "type": "integer", "minimum": 0 },
        "coverageRatio": {
          "type": ["number", "null"],
          "minimum": 0,
          "maximum": 1
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
