{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/source-audit/source-audit-report.v1.schema.json",
  "title": "RootBlocks Generated Source Audit Report v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "auditId",
    "auditedArtifact",
    "standard",
    "profile",
    "generator",
    "sourceManifest",
    "zoneManifest",
    "sourceMapCoverage",
    "regressionCoverage",
    "driftResult",
    "buildTrace",
    "evidence",
    "releaseGate"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.source-audit-report/v1"
    },
    "auditId": {
      "$ref": "#/$defs/id"
    },
    "auditedArtifact": {
      "$ref": "#/$defs/auditedArtifact"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "profile": {
      "$ref": "#/$defs/profileRef"
    },
    "generator": {
      "$ref": "#/$defs/generator"
    },
    "sourceManifest": {
      "$ref": "#/$defs/sourceManifest"
    },
    "zoneManifest": {
      "$ref": "#/$defs/zoneManifest"
    },
    "sourceMapCoverage": {
      "$ref": "#/$defs/sourceMapCoverage"
    },
    "regressionCoverage": {
      "$ref": "#/$defs/regressionCoverage"
    },
    "driftResult": {
      "$ref": "#/$defs/driftResult"
    },
    "buildTrace": {
      "$ref": "#/$defs/buildTrace"
    },
    "evidence": {
      "$ref": "#/$defs/evidence"
    },
    "releaseGate": {
      "$ref": "#/$defs/releaseGate"
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@*-]+$"
    },
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "profileRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["profileId", "version", "path", "hash"],
      "properties": {
        "profileId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "path": {
          "type": "string",
          "pattern": "^profiles/architecture/.+\\.json$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "auditedArtifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "id", "path", "hash"],
      "properties": {
        "kind": {
          "enum": ["structured-source-project", "buildpack-project", "template", "generated-workflow"]
        },
        "id": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "generator": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "id", "version", "ruleSetRefs", "mappingSetRefs", "querySetRefs"],
      "properties": {
        "mode": {
          "enum": ["builderx-compiler", "agent-codegen-governed", "hybrid"]
        },
        "id": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "model": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "ruleSetRefs": {
          "$ref": "#/$defs/refList"
        },
        "mappingSetRefs": {
          "$ref": "#/$defs/refList"
        },
        "querySetRefs": {
          "$ref": "#/$defs/refList"
        }
      }
    },
    "refList": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/id"
      },
      "uniqueItems": true
    },
    "sourceManifest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "hash", "integrity", "files"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/inventory/generated-source\\.(json|yaml|yml)$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "integrity": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "algorithm", "checkedAt"],
          "properties": {
            "status": {
              "const": "matched"
            },
            "algorithm": {
              "enum": ["sha256", "sha384", "sha512"]
            },
            "checkedAt": {
              "type": "string",
              "format": "date-time"
            }
          }
        },
        "files": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/sourceFile"
          },
          "contains": {
            "properties": {
              "role": {
                "const": "generated-source"
              },
              "sourceMapRequired": {
                "const": true
              }
            },
            "required": ["role", "sourceMapRequired"]
          }
        }
      }
    },
    "sourceFile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "role", "hash", "hashStatus", "owner", "zoneId", "sourceMapRequired"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^(src|tests|builderx|docs)/.+$"
        },
        "role": {
          "enum": ["generated-source", "generated-test", "builderx-metadata", "source-map", "docs", "custom-source"]
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "hashStatus": {
          "const": "matched"
        },
        "owner": {
          "enum": ["builderx", "rootblocks-console", "governed-agent", "manual"]
        },
        "zoneId": {
          "$ref": "#/$defs/id"
        },
        "sourceMapRequired": {
          "type": "boolean"
        },
        "sourceMapRefs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      }
    },
    "zoneManifest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "generatedZones", "customZones", "extensionZones"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/inventory/generated-zones\\.(json|yaml|yml)$"
        },
        "generatedZones": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/generatedZone"
          }
        },
        "customZones": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/customZone"
          }
        },
        "extensionZones": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/extensionZone"
          }
        }
      }
    },
    "generatedZone": {
      "type": "object",
      "additionalProperties": false,
      "required": ["zoneId", "pathPattern", "owner", "editPolicy", "driftPolicy"],
      "properties": {
        "zoneId": {
          "$ref": "#/$defs/id"
        },
        "pathPattern": {
          "$ref": "#/$defs/path"
        },
        "owner": {
          "enum": ["builderx", "rootblocks-console", "governed-agent"]
        },
        "editPolicy": {
          "const": "generated-only"
        },
        "driftPolicy": {
          "enum": ["block", "allow-with-accepted-evidence", "warn-with-accepted-evidence"]
        }
      }
    },
    "customZone": {
      "type": "object",
      "additionalProperties": false,
      "required": ["zoneId", "pathPattern", "owner", "editPolicy", "driftPolicy"],
      "properties": {
        "zoneId": {
          "$ref": "#/$defs/id"
        },
        "pathPattern": {
          "$ref": "#/$defs/path"
        },
        "owner": {
          "const": "manual"
        },
        "editPolicy": {
          "const": "custom-owned"
        },
        "driftPolicy": {
          "enum": ["preserve", "manual-review"]
        }
      }
    },
    "extensionZone": {
      "type": "object",
      "additionalProperties": false,
      "required": ["zoneId", "pathPattern", "owner", "editPolicy", "driftPolicy"],
      "properties": {
        "zoneId": {
          "$ref": "#/$defs/id"
        },
        "pathPattern": {
          "$ref": "#/$defs/path"
        },
        "owner": {
          "enum": ["manual", "governed-agent"]
        },
        "editPolicy": {
          "const": "extension"
        },
        "driftPolicy": {
          "enum": ["merge-with-markers", "manual-review"]
        }
      }
    },
    "sourceMapCoverage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "status", "criticalNodeCount", "coveredCriticalNodeCount", "missingCriticalNodes", "sourceMapRefs"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/coverage/.+\\.source-map-coverage\\.(json|yaml|yml)$"
        },
        "status": {
          "const": "passed"
        },
        "criticalNodeCount": {
          "type": "integer",
          "minimum": 1
        },
        "coveredCriticalNodeCount": {
          "type": "integer",
          "minimum": 1
        },
        "missingCriticalNodes": {
          "type": "array",
          "maxItems": 0,
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "sourceMapRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/coverageSourceMapRef"
          }
        }
      }
    },
    "coverageSourceMapRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mapId", "path", "hash"],
      "properties": {
        "mapId": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "type": "string",
          "pattern": "^builderx/source-maps/.+\\.(json|yaml|yml)$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "regressionCoverage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "status", "criticalFlowCount", "coveredCriticalFlowCount", "missingCriticalFlows", "regressionSuiteRefs"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/coverage/.+\\.regression-coverage\\.(json|yaml|yml)$"
        },
        "status": {
          "const": "passed"
        },
        "criticalFlowCount": {
          "type": "integer",
          "minimum": 1
        },
        "coveredCriticalFlowCount": {
          "type": "integer",
          "minimum": 1
        },
        "missingCriticalFlows": {
          "type": "array",
          "maxItems": 0,
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "regressionSuiteRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/regressionSuiteRef"
          }
        }
      }
    },
    "regressionSuiteRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["suiteId", "path", "hash", "criticalCaseCount"],
      "properties": {
        "suiteId": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "type": "string",
          "pattern": "^builderx/regression/.+\\.(json|yaml|yml)$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "criticalCaseCount": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "driftResult": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "status", "generatedOnlyChanges", "customZoneChanges", "checkedAt"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/drift/.+\\.drift-result\\.(json|yaml|yml)$"
        },
        "status": {
          "enum": ["clean", "accepted"]
        },
        "generatedOnlyChanges": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/driftChange"
          }
        },
        "customZoneChanges": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/driftChange"
          }
        },
        "acceptedDriftEvidence": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/evidenceRef"
          }
        },
        "checkedAt": {
          "type": "string",
          "format": "date-time"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "generatedOnlyChanges": {
                "minItems": 1
              }
            },
            "required": ["generatedOnlyChanges"]
          },
          "then": {
            "required": ["acceptedDriftEvidence"],
            "properties": {
              "status": {
                "const": "accepted"
              },
              "acceptedDriftEvidence": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "clean"
              }
            },
            "required": ["status"]
          },
          "then": {
            "properties": {
              "generatedOnlyChanges": {
                "maxItems": 0
              }
            }
          }
        }
      ]
    },
    "driftChange": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "zoneId", "changeType", "oldHash", "newHash"],
      "properties": {
        "path": {
          "$ref": "#/$defs/path"
        },
        "zoneId": {
          "$ref": "#/$defs/id"
        },
        "changeType": {
          "enum": ["added", "modified", "deleted"]
        },
        "oldHash": {
          "$ref": "#/$defs/hash"
        },
        "newHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "buildTrace": {
      "type": "object",
      "additionalProperties": false,
      "required": ["commands", "outputs"],
      "properties": {
        "commands": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/buildCommand"
          }
        },
        "outputs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/buildOutput"
          }
        }
      }
    },
    "buildCommand": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "command", "workingDirectory", "sourceManifestHash", "standardBundleHash", "profileHash"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "command": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        },
        "workingDirectory": {
          "enum": [".", "src", "tests"]
        },
        "sourceManifestHash": {
          "$ref": "#/$defs/hash"
        },
        "standardBundleHash": {
          "$ref": "#/$defs/hash"
        },
        "profileHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "buildOutput": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "path", "hash", "producedByCommand", "rebuildableFromSource"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": {
          "enum": ["binary", "package", "container-image", "buildpack", "native-package", "sbom", "test-report", "coverage-report"]
        },
        "path": {
          "type": "string",
          "pattern": "^dist/.+"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "producedByCommand": {
          "$ref": "#/$defs/id"
        },
        "rebuildableFromSource": {
          "const": true
        }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["evidenceRefs", "redaction", "sourceToBuildTrace"],
      "properties": {
        "evidenceRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/evidenceRef"
          }
        },
        "redaction": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "rules", "rawPayloadPolicy"],
          "properties": {
            "status": {
              "enum": ["applied", "not-needed"]
            },
            "rules": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "string",
                "minLength": 1
              }
            },
            "rawPayloadPolicy": {
              "const": "redacted-only"
            }
          }
        },
        "sourceToBuildTrace": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["sourcePath", "sourceHash", "outputId", "outputHash", "evidenceRef"],
            "properties": {
              "sourcePath": {
                "$ref": "#/$defs/path"
              },
              "sourceHash": {
                "$ref": "#/$defs/hash"
              },
              "outputId": {
                "$ref": "#/$defs/id"
              },
              "outputHash": {
                "$ref": "#/$defs/hash"
              },
              "evidenceRef": {
                "$ref": "#/$defs/id"
              }
            }
          }
        }
      }
    },
    "evidenceRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["evidenceId", "path", "hash"],
      "properties": {
        "evidenceId": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "type": "string",
          "pattern": "^evidence/.+\\.(json|yaml|yml)$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "releaseGate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "status", "blocking", "evidencePath"],
      "properties": {
        "name": {
          "const": "source-audit"
        },
        "status": {
          "const": "passed"
        },
        "blocking": {
          "const": true
        },
        "evidencePath": {
          "type": "string",
          "pattern": "^evidence/.+\\.source-audit\\.(json|yaml|yml)$"
        }
      }
    }
  }
}
