{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/extensions/private-extension-manifest.v1.schema.json",
  "title": "RootBlocks Private Extension Manifest v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "extensionId",
    "extensionKind",
    "version",
    "createdAt",
    "owner",
    "targetGlobalStandard",
    "dependencies",
    "assets",
    "importPolicy",
    "conflictPolicy",
    "publication",
    "evidence",
    "hashes"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.private-extension-manifest/v1"
    },
    "extensionId": {
      "$ref": "#/$defs/id"
    },
    "extensionKind": {
      "enum": ["organization-component-extension", "project-component-extension", "personal-component-extension"]
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    },
    "owner": {
      "$ref": "#/$defs/owner"
    },
    "targetGlobalStandard": {
      "$ref": "#/$defs/globalStandardRef"
    },
    "dependencies": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/dependencyRef"
      }
    },
    "assets": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/componentAsset"
      }
    },
    "importPolicy": {
      "$ref": "#/$defs/importPolicy"
    },
    "conflictPolicy": {
      "$ref": "#/$defs/conflictPolicy"
    },
    "conflictRecords": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/conflictRecord"
      }
    },
    "publication": {
      "$ref": "#/$defs/publication"
    },
    "evidence": {
      "$ref": "#/$defs/evidencePolicy"
    },
    "hashes": {
      "$ref": "#/$defs/hashBlock"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "extensionKind": {
            "const": "organization-component-extension"
          }
        },
        "required": ["extensionKind"]
      },
      "then": {
        "properties": {
          "owner": {
            "properties": {
              "scope": {
                "const": "organization"
              },
              "visibility": {
                "enum": ["organization-private", "shared", "public-candidate"]
              }
            }
          },
          "publication": {
            "properties": {
              "defaultVisibility": {
                "const": "organization-private"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "extensionKind": {
            "const": "project-component-extension"
          }
        },
        "required": ["extensionKind"]
      },
      "then": {
        "properties": {
          "owner": {
            "properties": {
              "scope": {
                "const": "project"
              },
              "visibility": {
                "enum": ["project-local", "shared"]
              }
            }
          },
          "publication": {
            "properties": {
              "defaultVisibility": {
                "const": "project-local"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "extensionKind": {
            "const": "personal-component-extension"
          }
        },
        "required": ["extensionKind"]
      },
      "then": {
        "properties": {
          "owner": {
            "properties": {
              "scope": {
                "const": "personal"
              },
              "visibility": {
                "const": "personal-private"
              }
            }
          },
          "publication": {
            "properties": {
              "defaultVisibility": {
                "const": "personal-private"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "publication": {
            "properties": {
              "status": {
                "enum": ["approved", "published", "deprecated", "revoked"]
              }
            },
            "required": ["status"]
          }
        },
        "required": ["publication"]
      },
      "then": {
        "properties": {
          "publication": {
            "required": ["approvedAt", "approvedBy", "approvalEvidenceRef"]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "publication": {
            "properties": {
              "status": {
                "enum": ["published", "deprecated", "revoked"]
              }
            },
            "required": ["status"]
          }
        },
        "required": ["publication"]
      },
      "then": {
        "properties": {
          "publication": {
            "required": ["publishedAt"]
          }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "ownerId": {
      "type": "string",
      "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_.:-]{1,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_./@-]+$"
    },
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": ["scope", "ownerId", "visibility"],
      "properties": {
        "scope": {
          "enum": ["organization", "project", "personal"]
        },
        "ownerId": {
          "$ref": "#/$defs/ownerId"
        },
        "projectId": {
          "$ref": "#/$defs/ownerId"
        },
        "visibility": {
          "enum": ["organization-private", "project-local", "personal-private", "shared", "public-candidate"]
        }
      }
    },
    "globalStandardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "manifestHash"],
      "properties": {
        "bundleId": {
          "const": "rootblocks-global-standard"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "dependencyRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "id", "version", "hash"],
      "properties": {
        "kind": {
          "enum": ["global-standard", "private-extension"]
        },
        "id": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "componentAsset": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "assetKind",
        "componentId",
        "version",
        "manifestPath",
        "manifestHash",
        "owner",
        "sourceMode",
        "buildPack",
        "packageArtifacts"
      ],
      "properties": {
        "assetKind": {
          "const": "component"
        },
        "componentId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestPath": {
          "type": "string",
          "pattern": "^components/.+\\.component\\.(json|yaml|yml)$"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "owner": {
          "$ref": "#/$defs/owner"
        },
        "sourceMode": {
          "enum": ["builderx-compiler", "agent-generated", "manual-governed", "imported"]
        },
        "buildPack": {
          "$ref": "#/$defs/buildPack"
        },
        "packageArtifacts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/packageArtifact"
          }
        }
      }
    },
    "buildPack": {
      "type": "object",
      "additionalProperties": false,
      "required": ["buildPackId", "version", "path", "hash", "exposedWorkflowIds"],
      "properties": {
        "buildPackId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "path": {
          "type": "string",
          "pattern": "^(builderx|components)/.+\\.buildpack\\.(yaml|yml|json)$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "exposedWorkflowIds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      }
    },
    "packageArtifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "language", "packageName", "version", "sourcePath", "hash"],
      "properties": {
        "kind": {
          "enum": [
            "builderx-buildpack",
            "nuget",
            "npm",
            "pypi",
            "maven",
            "gradle",
            "cargo",
            "go-module",
            "composer",
            "gem",
            "docker-oci",
            "custom"
          ]
        },
        "language": {
          "enum": ["csharp", "typescript", "javascript", "python", "java", "kotlin", "go", "rust", "php", "ruby", "custom"]
        },
        "packageName": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9@][a-zA-Z0-9_.:/@-]{1,160}$"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^(src|components|builderx|packages)/.+$"
        },
        "artifactRef": {
          "type": "string",
          "pattern": "^(dist|artifacts|packages|builderx)/.+$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "importPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode",
        "tool",
        "allowedOperations",
        "allowedAssetKinds",
        "blockedAssetKinds",
        "genericAssetImport",
        "importCommand",
        "syncCommand"
      ],
      "properties": {
        "mode": {
          "const": "components-only"
        },
        "tool": {
          "const": "rbstd"
        },
        "allowedOperations": {
          "const": ["components import", "components sync"]
        },
        "allowedAssetKinds": {
          "const": ["component"]
        },
        "blockedAssetKinds": {
          "const": [
            "schema",
            "rule",
            "template",
            "golden",
            "policy",
            "documentation",
            "semantic-engine",
            "architecture-profile",
            "recipe",
            "compatibility",
            "bundle",
            "registry"
          ]
        },
        "genericAssetImport": {
          "const": "forbidden"
        },
        "importCommand": {
          "type": "string",
          "pattern": "^rbstd components import .+$"
        },
        "syncCommand": {
          "type": "string",
          "pattern": "^rbstd components sync .+$"
        }
      }
    },
    "conflictPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "globalAssetMutation",
        "globalComponentIdConflict",
        "sameComponentId",
        "scopePrecedence",
        "shadowingGlobalAssets",
        "revokedComponent",
        "incompatibleGlobal",
        "evidenceRequiredForPrecedence"
      ],
      "properties": {
        "globalAssetMutation": {
          "const": "fail-closed"
        },
        "globalComponentIdConflict": {
          "const": "block-private-replacement"
        },
        "sameComponentId": {
          "enum": ["block", "prefer-more-specific-scope-with-evidence"]
        },
        "scopePrecedence": {
          "const": ["personal", "project", "organization", "global"]
        },
        "shadowingGlobalAssets": {
          "const": "forbidden"
        },
        "revokedComponent": {
          "const": "quarantine-and-remove-from-effective-standard"
        },
        "incompatibleGlobal": {
          "const": "block-publish-and-sync"
        },
        "evidenceRequiredForPrecedence": {
          "const": true
        }
      }
    },
    "conflictRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": ["componentId", "decision", "candidateScopes", "evidenceRef"],
      "properties": {
        "componentId": {
          "$ref": "#/$defs/id"
        },
        "decision": {
          "enum": ["blocked", "prefer-more-specific-scope-with-evidence"]
        },
        "candidateScopes": {
          "type": "array",
          "minItems": 2,
          "items": {
            "enum": ["organization", "project", "personal", "global"]
          }
        },
        "evidenceRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "publication": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "defaultVisibility", "publicByDefault"],
      "properties": {
        "status": {
          "enum": ["draft", "approved", "published", "deprecated", "revoked"]
        },
        "defaultVisibility": {
          "enum": ["organization-private", "project-local", "personal-private"]
        },
        "publicByDefault": {
          "const": false
        },
        "approvedAt": {
          "type": "string",
          "format": "date-time"
        },
        "approvedBy": {
          "$ref": "#/$defs/ownerId"
        },
        "approvalEvidenceRef": {
          "$ref": "#/$defs/path"
        },
        "publishedAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "required",
        "usageEvidenceSchema",
        "recordExtensionId",
        "recordExtensionVersion",
        "recordExtensionHash",
        "recordOwner",
        "recordVisibility",
        "recordTargetGlobalStandard",
        "recordComponentHashes",
        "recordConflictDecisions",
        "recordImportSyncCommands",
        "evidenceRefs"
      ],
      "properties": {
        "required": {
          "const": true
        },
        "usageEvidenceSchema": {
          "const": "rootblocks.evidence/v1"
        },
        "recordExtensionId": {
          "const": true
        },
        "recordExtensionVersion": {
          "const": true
        },
        "recordExtensionHash": {
          "const": true
        },
        "recordOwner": {
          "const": true
        },
        "recordVisibility": {
          "const": true
        },
        "recordTargetGlobalStandard": {
          "const": true
        },
        "recordComponentHashes": {
          "const": true
        },
        "recordConflictDecisions": {
          "const": true
        },
        "recordImportSyncCommands": {
          "const": true
        },
        "evidenceRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/path"
          }
        }
      }
    },
    "hashBlock": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "manifestHash"],
      "properties": {
        "algorithm": {
          "enum": ["sha256", "sha384", "sha512"]
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        }
      }
    }
  }
}
