{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/organization/organization-registry.v1.schema.json",
  "title": "RootBlocks Organization Standard Registry v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "registryId",
    "organization",
    "version",
    "generatedAt",
    "globalStandard",
    "acceptedAssetKinds",
    "blockedAssetKinds",
    "stores",
    "permissions",
    "lifecyclePolicy",
    "conflictPolicy",
    "localSync",
    "evidence"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.organization-registry/v1"
    },
    "registryId": {
      "$ref": "#/$defs/id"
    },
    "organization": {
      "$ref": "#/$defs/organization"
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "generatedAt": {
      "type": "string",
      "format": "date-time"
    },
    "globalStandard": {
      "$ref": "#/$defs/globalStandard"
    },
    "acceptedAssetKinds": {
      "const": ["component"]
    },
    "blockedAssetKinds": {
      "const": [
        "schema",
        "rule",
        "template",
        "golden",
        "policy",
        "documentation",
        "semantic-engine",
        "architecture-profile",
        "recipe",
        "compatibility",
        "bundle",
        "registry"
      ]
    },
    "stores": {
      "$ref": "#/$defs/stores"
    },
    "permissions": {
      "$ref": "#/$defs/permissions"
    },
    "lifecyclePolicy": {
      "$ref": "#/$defs/lifecyclePolicy"
    },
    "conflictPolicy": {
      "$ref": "#/$defs/conflictPolicy"
    },
    "localSync": {
      "$ref": "#/$defs/localSyncPolicy"
    },
    "evidence": {
      "$ref": "#/$defs/evidencePolicy"
    }
  },
  "$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_./@-]+$"
    },
    "uri": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "scope": {
      "enum": ["organization", "project", "personal"]
    },
    "visibility": {
      "enum": ["organization-private", "project-local", "personal-private", "shared", "public-candidate"]
    },
    "lifecycleState": {
      "enum": ["draft", "approved", "published", "deprecated", "revoked"]
    },
    "organization": {
      "type": "object",
      "additionalProperties": false,
      "required": ["organizationId", "displayName", "defaultVisibility"],
      "properties": {
        "organizationId": {
          "$ref": "#/$defs/ownerId"
        },
        "displayName": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "defaultVisibility": {
          "const": "organization-private"
        },
        "trustDomain": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "globalStandard": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "bundleId",
        "version",
        "manifestHash",
        "distributionIndexPath",
        "latestCompatibleCheckedAt",
        "compatibilityDecision"
      ],
      "properties": {
        "bundleId": {
          "const": "rootblocks-global-standard"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "distributionIndexPath": {
          "$ref": "#/$defs/path"
        },
        "latestCompatibleCheckedAt": {
          "type": "string",
          "format": "date-time"
        },
        "compatibilityDecision": {
          "enum": ["allow", "warn"]
        }
      }
    },
    "stores": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "componentsOnly",
        "genericExternalAssetStore",
        "storePaths",
        "organization",
        "project",
        "personal"
      ],
      "properties": {
        "componentsOnly": {
          "const": true
        },
        "genericExternalAssetStore": {
          "const": "forbidden"
        },
        "storePaths": {
          "type": "object",
          "additionalProperties": false,
          "required": ["organization", "project", "personal"],
          "properties": {
            "organization": {
              "$ref": "#/$defs/path"
            },
            "project": {
              "$ref": "#/$defs/path"
            },
            "personal": {
              "$ref": "#/$defs/path"
            }
          }
        },
        "organization": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/componentRecord"
          }
        },
        "project": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/componentRecord"
          }
        },
        "personal": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/componentRecord"
          }
        }
      }
    },
    "componentRecord": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "assetKind",
        "componentId",
        "version",
        "owner",
        "source",
        "manifest",
        "visibility",
        "lifecycle",
        "targetGlobalStandard",
        "buildPack",
        "packageArtifacts",
        "localSync",
        "evidence"
      ],
      "properties": {
        "assetKind": {
          "const": "component"
        },
        "componentId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "displayName": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "owner": {
          "$ref": "#/$defs/owner"
        },
        "source": {
          "$ref": "#/$defs/componentSource"
        },
        "manifest": {
          "$ref": "#/$defs/componentManifestRef"
        },
        "visibility": {
          "$ref": "#/$defs/visibility"
        },
        "lifecycle": {
          "$ref": "#/$defs/componentLifecycle"
        },
        "targetGlobalStandard": {
          "$ref": "#/$defs/globalStandardTarget"
        },
        "buildPack": {
          "$ref": "#/$defs/buildPack"
        },
        "packageArtifacts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/packageArtifact"
          }
        },
        "localSync": {
          "$ref": "#/$defs/componentSyncState"
        },
        "evidence": {
          "$ref": "#/$defs/componentEvidence"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "owner": {
                "properties": {
                  "scope": {
                    "const": "organization"
                  }
                },
                "required": ["scope"]
              }
            },
            "required": ["owner"]
          },
          "then": {
            "properties": {
              "visibility": {
                "enum": ["organization-private", "shared", "public-candidate"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "owner": {
                "properties": {
                  "scope": {
                    "const": "project"
                  }
                },
                "required": ["scope"]
              }
            },
            "required": ["owner"]
          },
          "then": {
            "properties": {
              "visibility": {
                "enum": ["project-local", "shared"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "owner": {
                "properties": {
                  "scope": {
                    "const": "personal"
                  }
                },
                "required": ["scope"]
              }
            },
            "required": ["owner"]
          },
          "then": {
            "properties": {
              "visibility": {
                "enum": ["personal-private"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "lifecycle": {
                "properties": {
                  "state": {
                    "enum": ["approved", "published", "deprecated", "revoked"]
                  }
                },
                "required": ["state"]
              }
            },
            "required": ["lifecycle"]
          },
          "then": {
            "properties": {
              "lifecycle": {
                "required": ["approvedAt", "approvedBy"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "lifecycle": {
                "properties": {
                  "state": {
                    "enum": ["published", "deprecated", "revoked"]
                  }
                },
                "required": ["state"]
              }
            },
            "required": ["lifecycle"]
          },
          "then": {
            "properties": {
              "lifecycle": {
                "required": ["publishedAt"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "lifecycle": {
                "properties": {
                  "state": {
                    "const": "revoked"
                  }
                },
                "required": ["state"]
              }
            },
            "required": ["lifecycle"]
          },
          "then": {
            "properties": {
              "lifecycle": {
                "required": ["revokedAt", "revokedBy", "revocationReason"]
              }
            }
          }
        }
      ]
    },
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": ["scope", "ownerId"],
      "properties": {
        "scope": {
          "$ref": "#/$defs/scope"
        },
        "ownerId": {
          "$ref": "#/$defs/ownerId"
        },
        "projectId": {
          "$ref": "#/$defs/ownerId"
        }
      }
    },
    "componentSource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["producer", "mode", "sourceRef"],
      "properties": {
        "producer": {
          "enum": ["rootblocks-console", "darkfactory", "builderx", "human", "agent", "external"]
        },
        "mode": {
          "enum": ["builderx-compiler", "agent-generated", "manual-governed", "imported"]
        },
        "sourceRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "componentManifestRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "path", "hash"],
      "properties": {
        "schemaVersion": {
          "const": "rootblocks.component/v1"
        },
        "path": {
          "type": "string",
          "pattern": "^components/.+\\.component\\.(json|yaml|yml)$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "componentLifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "createdAt", "createdBy"],
      "properties": {
        "state": {
          "$ref": "#/$defs/lifecycleState"
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "createdBy": {
          "$ref": "#/$defs/ownerId"
        },
        "approvedAt": {
          "type": "string",
          "format": "date-time"
        },
        "approvedBy": {
          "$ref": "#/$defs/ownerId"
        },
        "publishedAt": {
          "type": "string",
          "format": "date-time"
        },
        "deprecatedAt": {
          "type": "string",
          "format": "date-time"
        },
        "revokedAt": {
          "type": "string",
          "format": "date-time"
        },
        "revokedBy": {
          "$ref": "#/$defs/ownerId"
        },
        "revocationReason": {
          "enum": ["security", "compatibility", "replaced", "policy", "owner-request"]
        }
      }
    },
    "globalStandardTarget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "manifestHash"],
      "properties": {
        "bundleId": {
          "const": "rootblocks-global-standard"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "buildPack": {
      "type": "object",
      "additionalProperties": false,
      "required": ["buildPackId", "version", "path", "categoryPath", "exposedWorkflowIds"],
      "properties": {
        "buildPackId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "path": {
          "type": "string",
          "pattern": "^(builderx|components)/.+\\.buildpack\\.(yaml|yml|json)$"
        },
        "categoryPath": {
          "type": "array",
          "minItems": 2,
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "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"
        }
      }
    },
    "componentSyncState": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "version", "hash", "evidenceRef"],
      "properties": {
        "state": {
          "enum": ["pending", "synced", "stale", "quarantined"]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        },
        "lastSyncedAt": {
          "type": "string",
          "format": "date-time"
        },
        "evidenceRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "componentEvidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "registryVersion", "globalStandardVersion", "componentHash", "evidenceRefs"],
      "properties": {
        "required": {
          "const": true
        },
        "registryVersion": {
          "$ref": "#/$defs/semver"
        },
        "globalStandardVersion": {
          "$ref": "#/$defs/semver"
        },
        "componentHash": {
          "$ref": "#/$defs/hash"
        },
        "evidenceRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/path"
          }
        }
      }
    },
    "permissions": {
      "type": "object",
      "additionalProperties": false,
      "required": ["roles", "operations"],
      "properties": {
        "roles": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/role"
          }
        },
        "operations": {
          "type": "object",
          "additionalProperties": false,
          "required": ["create", "approve", "publish", "sync", "deprecate", "revoke"],
          "properties": {
            "create": {
              "$ref": "#/$defs/operationPermission"
            },
            "approve": {
              "$ref": "#/$defs/operationPermission"
            },
            "publish": {
              "$ref": "#/$defs/operationPermission"
            },
            "sync": {
              "$ref": "#/$defs/operationPermission"
            },
            "deprecate": {
              "$ref": "#/$defs/operationPermission"
            },
            "revoke": {
              "$ref": "#/$defs/operationPermission"
            }
          }
        }
      }
    },
    "role": {
      "type": "object",
      "additionalProperties": false,
      "required": ["roleId", "scope"],
      "properties": {
        "roleId": {
          "enum": ["registry-owner", "component-author", "component-approver", "component-publisher", "component-consumer", "auditor"]
        },
        "scope": {
          "enum": ["organization", "project", "personal"]
        }
      }
    },
    "operationPermission": {
      "type": "object",
      "additionalProperties": false,
      "required": ["allowedRoles", "requiresHumanApproval", "evidenceRequired"],
      "properties": {
        "allowedRoles": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["registry-owner", "component-author", "component-approver", "component-publisher", "component-consumer", "auditor"]
          },
          "uniqueItems": true
        },
        "requiresHumanApproval": {
          "type": "boolean"
        },
        "evidenceRequired": {
          "const": true
        }
      }
    },
    "lifecyclePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["states", "initialState", "terminalStates", "transitions"],
      "properties": {
        "states": {
          "const": ["draft", "approved", "published", "deprecated", "revoked"]
        },
        "initialState": {
          "const": "draft"
        },
        "terminalStates": {
          "const": ["revoked"]
        },
        "transitions": {
          "type": "array",
          "minItems": 5,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["from", "to", "operation"],
            "properties": {
              "from": {
                "$ref": "#/$defs/lifecycleState"
              },
              "to": {
                "$ref": "#/$defs/lifecycleState"
              },
              "operation": {
                "enum": ["create", "approve", "publish", "deprecate", "revoke"]
              }
            }
          }
        }
      }
    },
    "conflictPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "globalAssetMutation",
        "allowedExtensionKinds",
        "scopePrecedence",
        "sameComponentId",
        "globalComponentIdConflict",
        "incompatibleGlobal",
        "revokedComponent"
      ],
      "properties": {
        "globalAssetMutation": {
          "const": "fail-closed"
        },
        "allowedExtensionKinds": {
          "const": ["component"]
        },
        "scopePrecedence": {
          "const": ["personal", "project", "organization", "global"]
        },
        "sameComponentId": {
          "enum": ["block", "prefer-more-specific-scope-with-evidence"]
        },
        "globalComponentIdConflict": {
          "const": "block-private-replacement"
        },
        "incompatibleGlobal": {
          "const": "block-publish-and-sync"
        },
        "revokedComponent": {
          "const": "quarantine-and-remove-from-effective-standard"
        }
      }
    },
    "localSyncPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "tool",
        "importCommand",
        "syncCommand",
        "refreshBeforeUse",
        "offlineBehavior",
        "evidenceRequired",
        "syncStatePath",
        "syncedFields"
      ],
      "properties": {
        "tool": {
          "const": "rbstd"
        },
        "importCommand": {
          "type": "string",
          "pattern": "^rbstd components import .+$"
        },
        "syncCommand": {
          "type": "string",
          "pattern": "^rbstd components sync .+$"
        },
        "refreshBeforeUse": {
          "const": true
        },
        "offlineBehavior": {
          "enum": ["use-current-if-valid", "block-if-missing", "block-if-stale-critical"]
        },
        "evidenceRequired": {
          "const": true
        },
        "syncStatePath": {
          "$ref": "#/$defs/path"
        },
        "syncedFields": {
          "const": [
            "ownerId",
            "componentId",
            "version",
            "source",
            "hash",
            "visibility",
            "approvalState",
            "targetGlobalBundle",
            "buildPack",
            "packageArtifacts"
          ]
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "required",
        "recordRegistryVersion",
        "recordGlobalStandard",
        "recordComponentHashes",
        "recordSyncDecision",
        "recordPermissionDecision"
      ],
      "properties": {
        "required": {
          "const": true
        },
        "recordRegistryVersion": {
          "const": true
        },
        "recordGlobalStandard": {
          "const": true
        },
        "recordComponentHashes": {
          "const": true
        },
        "recordSyncDecision": {
          "const": true
        },
        "recordPermissionDecision": {
          "const": true
        }
      }
    }
  }
}
