From 5dc144db1febe7fc1203a0928484e4c52349f0e6 Mon Sep 17 00:00:00 2001 From: Matthew Ghent Date: Thu, 22 Oct 2026 06:42:24 -0400 Subject: [PATCH 01/14] scope connection to one registry for multi-registry tenants Adds an optional registry_id to BenchlingConnection, resolved through GET /api/v3/registry/{id}. When set, entity schemas and dropdowns are filtered to that registry, and results schemas to its owning organization. On a tenant with several registries and no registry_id, lookups that match objects in more than one registry raise AmbiguousRegistryError, and creating a schema or dropdown fails during validation. registry_id and organization_id raise a clear error in place of a bare assert. Single-registry tenants without registry_id behave as before. Closes #43 Co-Authored-By: Claude Opus 5.5 --- docs/reference/benchling-connection.md | 7 + liminal/connection/benchling_connection.py | 4 + liminal/connection/benchling_service.py | 98 ++++++- liminal/dropdowns/api.py | 3 +- liminal/dropdowns/operations.py | 10 + liminal/dropdowns/utils.py | 33 ++- liminal/entity_schemas/api.py | 4 +- .../entity_schemas/entity_schema_models.py | 11 +- liminal/entity_schemas/operations.py | 16 ++ liminal/results_schemas/api.py | 5 +- .../models/results_schema_model.py | 12 +- liminal/results_schemas/utils.py | 6 +- liminal/tests/test_registry_scoping.py | 245 ++++++++++++++++++ 13 files changed, 418 insertions(+), 36 deletions(-) create mode 100644 liminal/tests/test_registry_scoping.py diff --git a/docs/reference/benchling-connection.md b/docs/reference/benchling-connection.md index fa899e1..e33a46c 100644 --- a/docs/reference/benchling-connection.md +++ b/docs/reference/benchling-connection.md @@ -43,6 +43,13 @@ connection = BenchlingConnection( Whether your Benchling tenant has access to fieldsets. +- **registry_id: Optional[str] = None** + + The API id of the registry to scope this connection to, for example `src_abc123`. Only needed when the tenant has more than one registry. When set, Liminal reads and writes only the entity schemas and dropdowns in that registry, and the results schemas owned by the registry's organization. Without it, Liminal works as before on single-registry tenants. On a tenant with several registries, Liminal raises an error rather than guess when a lookup matches objects in more than one registry, or when it would need to create a schema or dropdown. + + !!! tip + For a tenant with several registries, give each registry its own Liminal directory (`env.py` and `versions/`), with one `BenchlingConnection` per tenant that sets that registry's `registry_id`. Each registry then keeps its own revision history in its own `liminal_remote` schema. + - **config_flags: TenantConfigFlags = TenantConfigFlags()** Set of config flags that are configured on the tenant level, that defines specific features of the tenant. These can be updated on Benchling's end by contacting their support team. diff --git a/liminal/connection/benchling_connection.py b/liminal/connection/benchling_connection.py index eddfb47..29029bc 100644 --- a/liminal/connection/benchling_connection.py +++ b/liminal/connection/benchling_connection.py @@ -45,6 +45,9 @@ class BenchlingConnection(BaseModel): DEPRECATED: Liminal no longer uses Playwright to log into Benchling's internal API, so this is ignored. It will be removed in v6. fieldsets: bool = False Whether your Benchling tenant has access to fieldsets. + registry_id: str | None = None + The API id of the registry to scope this connection to, e.g. src_abc123. Only needed when the tenant has more than one registry. + When set, Liminal reads and writes only the schemas and dropdowns in this registry, and the result schemas owned by its organization. config_flags: TenantConfigFlags = TenantConfigFlags() Set of config flags that are configured on the tenant level. These can be updated on Benchling's end by contacting their support team. """ @@ -59,6 +62,7 @@ class BenchlingConnection(BaseModel): internal_api_admin_password: str | None = None playwright_data_dir: str | None = None fieldsets: bool = False + registry_id: str | None = None config_flags: TenantConfigFlags = TenantConfigFlags() @model_validator(mode="before") diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index 3fe3cc3..97bbe89 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -1,9 +1,10 @@ import logging import warnings -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, NamedTuple from benchling_sdk.auth.client_credentials_oauth2 import ClientCredentialsOAuth2 from benchling_sdk.benchling import Benchling, BenchlingApiClientDecorator +from benchling_sdk.errors import BenchlingError from benchling_sdk.helpers.retry_helpers import RetryStrategy from sqlalchemy import create_engine from sqlalchemy.engine import Engine @@ -28,6 +29,17 @@ REMOTE_REVISION_ID_FIELD_WH_NAME = "revision_id" +class RegistryRef(NamedTuple): + """The registry a connection writes to, and the organization that owns it.""" + + id: str + owner_id: str + + +class AmbiguousRegistryError(ValueError): + """A lookup matched objects that may sit in different registries, and the connection doesn't name one.""" + + class BenchlingService(Benchling): """ Class that creates a connection object that can be used to connect to Benchling's API and/or Postgres warehouse. @@ -64,6 +76,8 @@ def __init__( ) self.connection = connection self._session: Session | None = None + self._registry: RegistryRef | None = None + self._registry_count: int | None = None self.use_api = use_api self.benchling_tenant = connection.tenant_name if use_api: @@ -103,17 +117,83 @@ def session(self) -> Session: @property def registry_id(self) -> str: - # This assumes there is only one registry (which has always been the case at DynoTx) - registries = self.registry.registries() - assert len(registries) == 1 - return registries[0].id + """The id of the registry this connection writes to. Raises if it can't be determined.""" + return self._get_registry().id @property def organization_id(self) -> str: - # This assumes there is only one registry (which has always been the case at DynoTx) - registries = self.registry.registries() - assert len(registries) == 1 - return registries[0].owner.id + """The id of the organization that owns this connection's registry. Raises if it can't be determined.""" + return self._get_registry().owner_id + + @property + def _is_registry_scoped(self) -> bool: + """Whether this connection is scoped to one registry through `registry_id`.""" + return self.connection.registry_id is not None + + @property + def _has_unresolved_registry(self) -> bool: + """Whether the tenant has more than one registry and this connection doesn't name one. + Lookups by name can then match objects in several registries.""" + return self._find_registry() is None + + def _is_in_registry_scope(self, record: dict[str, Any]) -> bool: + """Whether a v3 API record belongs to this connection's registry or its organization. + Always True when the connection isn't scoped to a registry.""" + if not self._is_registry_scoped: + return True + registry_id = (record.get("registry") or {}).get("id") + if registry_id: + return registry_id == self.registry_id + organization_id = (record.get("organization") or {}).get("id") + if organization_id: + return organization_id == self.organization_id + return False + + def _ensure_unambiguous(self, matches: list[Any], description: str) -> None: + """Raises when a lookup matches more than one object on a tenant whose registry isn't set, + because the matches may sit in different registries and Liminal would have to guess.""" + if len(matches) > 1 and self._has_unresolved_registry: + raise AmbiguousRegistryError( + f"Tenant {self.benchling_tenant}: found {len(matches)} {description}. " + "The tenant has more than one registry, so set registry_id on the BenchlingConnection to choose one." + ) + + def _find_registry(self) -> RegistryRef | None: + """Resolves the connection's registry once. Returns None when the tenant has several registries + and the connection doesn't set registry_id.""" + if self._registry_count is None: + if self.connection.registry_id is not None: + self._registry = self._get_registry_by_id(self.connection.registry_id) + self._registry_count = 1 + else: + registries = self.registry.registries() + self._registry_count = len(registries) + if len(registries) == 1: + self._registry = RegistryRef( + registries[0].id, registries[0].owner.id + ) + return self._registry + + def _get_registry_by_id(self, registry_id: str) -> RegistryRef: + from liminal.utils import rate_limited_get_v3 + + try: + response = rate_limited_get_v3(self, f"/api/v3/registry/{registry_id}") + except BenchlingError as e: + raise ValueError( + f"Tenant {self.benchling_tenant}: registry {registry_id} not found ({e.status_code})." + ) from e + registry = response.parsed + return RegistryRef(registry["id"], registry["owner"]["id"]) + + def _get_registry(self) -> RegistryRef: + registry = self._find_registry() + if registry is None: + raise ValueError( + f"Tenant {self.benchling_tenant} has {self._registry_count} registries. " + "Set registry_id on the BenchlingConnection to choose one." + ) + return registry def __enter__(self) -> Session: self._session = self.get_session() diff --git a/liminal/dropdowns/api.py b/liminal/dropdowns/api.py index 068b55c..fcc2ad0 100644 --- a/liminal/dropdowns/api.py +++ b/liminal/dropdowns/api.py @@ -27,9 +27,10 @@ def list_dropdowns( benchling_service: BenchlingService, include_archived: bool = True ) -> list[dict[str, Any]]: """Fetch dropdowns from the v3 API.""" - return list_all_items_v3( + dropdowns = list_all_items_v3( benchling_service, f"{_DROPDOWN_API_PATH}/items", include_archived ) + return [d for d in dropdowns if benchling_service._is_in_registry_scope(d)] def list_dropdown_options( diff --git a/liminal/dropdowns/operations.py b/liminal/dropdowns/operations.py index 1797fd6..b09827c 100644 --- a/liminal/dropdowns/operations.py +++ b/liminal/dropdowns/operations.py @@ -66,6 +66,16 @@ def execute(self, benchling_service: BenchlingService) -> dict[str, Dropdown]: f"Dropdown {self.dropdown_name} is different in code versus Benchling." ) + def validate(self, benchling_service: BenchlingService) -> None: + if ( + benchling_service._has_unresolved_registry + and not dropdown_exists_in_benchling(benchling_service, self.dropdown_name) + ): + raise ValueError( + f"{self.__class__.__name__} {self.dropdown_name}: Tenant {benchling_service.benchling_tenant} has more than one registry. " + "Set registry_id on the BenchlingConnection to choose where to create this dropdown." + ) + def describe_operation(self) -> str: return f"{self.dropdown_name}: Creating dropdown with options {self.dropdown_options}." diff --git a/liminal/dropdowns/utils.py b/liminal/dropdowns/utils.py index 830e462..040a1b2 100644 --- a/liminal/dropdowns/utils.py +++ b/liminal/dropdowns/utils.py @@ -30,29 +30,40 @@ def get_benchling_dropdown_name_to_classname_map( def get_benchling_dropdown_summaries( benchling_service: BenchlingService, ) -> list[DropdownSummary]: - return [ + summaries = [ dropdown for sublist in benchling_service.dropdowns.list() for dropdown in sublist ] + if not benchling_service._is_registry_scoped: + return summaries + # The v2 dropdown summary has no registry, so scope it by the ids the v3 list returns. + scoped_ids = { + d["id"] for d in list_dropdowns(benchling_service, include_archived=True) + } + return [d for d in summaries if d.id in scoped_ids] def get_benchling_dropdown_summary_by_name( benchling_service: BenchlingService, name: str ) -> DropdownSummary: - for dropdown in get_benchling_dropdown_summaries(benchling_service): - if dropdown.name == name: - return dropdown + matches = [ + d for d in get_benchling_dropdown_summaries(benchling_service) if d.name == name + ] + benchling_service._ensure_unambiguous(matches, f"dropdowns named {name}") + if matches: + return matches[0] raise Exception(f"Dropdown {name} not found in given list.") def get_benchling_dropdown_by_name( benchling_service: BenchlingService, name: str ) -> Dropdown: - dropdown = None - for d in get_benchling_dropdown_summaries(benchling_service): - if d.name == name: - dropdown = d + matches = [ + d for d in get_benchling_dropdown_summaries(benchling_service) if d.name == name + ] + benchling_service._ensure_unambiguous(matches, f"dropdowns named {name}") + dropdown = matches[-1] if matches else None if dropdown is None: raise Exception( f"Dropdown {name} not found in Benchling {benchling_service.benchling_tenant}." @@ -96,6 +107,12 @@ def get_benchling_dropdowns_dict( include_archived=include_archived, dropdown_names_for_options=dropdown_names_for_options, ) + names = [dropdown["name"] for dropdown in dropdowns] + duplicate_names = sorted({n for n in names if names.count(n) > 1}) + benchling_service._ensure_unambiguous( + [d for d in dropdowns if d["name"] in duplicate_names], + f"dropdowns sharing the names {duplicate_names}", + ) return { dropdown["name"]: _convert_dropdown_from_v3_to_v2_sdk( dropdown, include_archived diff --git a/liminal/entity_schemas/api.py b/liminal/entity_schemas/api.py index ded2a16..6b83b79 100644 --- a/liminal/entity_schemas/api.py +++ b/liminal/entity_schemas/api.py @@ -50,7 +50,7 @@ def list_entity_schemas( for future in as_completed(futures): entity_schemas.extend(future.result()) - return entity_schemas + return [s for s in entity_schemas if benchling_service._is_in_registry_scope(s)] @lru_cache @@ -61,6 +61,8 @@ def list_entity_schemas_cached( s for schemas in benchling_service.schemas.list_entity_schemas() for s in schemas + if not benchling_service._is_registry_scoped + or s.registry_id == benchling_service.registry_id ] diff --git a/liminal/entity_schemas/entity_schema_models.py b/liminal/entity_schemas/entity_schema_models.py index faf7e12..d0e5869 100644 --- a/liminal/entity_schemas/entity_schema_models.py +++ b/liminal/entity_schemas/entity_schema_models.py @@ -531,14 +531,11 @@ def get_one( ) -> EntitySchemaModel: if schemas_data is None: schemas_data = list_entity_schemas(benchling_service, include_archived=True) - schema = next( - ( - schema - for schema in schemas_data - if schema["systemName"] == wh_schema_name - ), - None, + matches = [s for s in schemas_data if s["systemName"] == wh_schema_name] + benchling_service._ensure_unambiguous( + matches, f"entity schemas with warehouse name {wh_schema_name}" ) + schema = matches[0] if matches else None if schema is None: raise ValueError( f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." diff --git a/liminal/entity_schemas/operations.py b/liminal/entity_schemas/operations.py index a592a9d..5c4f2d7 100644 --- a/liminal/entity_schemas/operations.py +++ b/liminal/entity_schemas/operations.py @@ -106,6 +106,22 @@ def validate(self, benchling_service: BenchlingService) -> None: f"{self.__class__.__name__} {self._validated_schema_properties.warehouse_name}: On field {field.warehouse_name}, unit {field.unit_name} not found in Benchling Unit Dictionary as a valid unit. Please check the field definition or your Unit Dictionary." ) + if benchling_service._has_unresolved_registry: + self._validate_registry_for_create(benchling_service) + + def _validate_registry_for_create( + self, benchling_service: BenchlingService + ) -> None: + """Fails before any write when creating this schema would need a registry the connection doesn't name.""" + wh_name = self._validated_schema_properties.warehouse_name + try: + EntitySchemaModel.get_one(benchling_service, wh_name) + except ValueError: + raise ValueError( + f"{self.__class__.__name__} {wh_name}: Tenant {benchling_service.benchling_tenant} has more than one registry. " + "Set registry_id on the BenchlingConnection to choose where to create this schema." + ) + def _validate_create(self, benchling_service: BenchlingService) -> None: all_schemas = list_entity_schemas(benchling_service, include_archived=True) if self._validated_schema_properties.name in [ diff --git a/liminal/results_schemas/api.py b/liminal/results_schemas/api.py index d7671e7..62f1b04 100644 --- a/liminal/results_schemas/api.py +++ b/liminal/results_schemas/api.py @@ -14,7 +14,10 @@ def list_results_schemas( benchling_service: BenchlingService, ) -> list[dict[str, Any]]: """Fetch result schemas from the v3 API.""" - return list_all_items_v3(benchling_service, f"{_RESULT_SCHEMA_API_PATH}/items") + result_schemas = list_all_items_v3( + benchling_service, f"{_RESULT_SCHEMA_API_PATH}/items" + ) + return [s for s in result_schemas if benchling_service._is_in_registry_scope(s)] def list_result_schema_field_definitions( diff --git a/liminal/results_schemas/models/results_schema_model.py b/liminal/results_schemas/models/results_schema_model.py index 18558de..0ade738 100644 --- a/liminal/results_schemas/models/results_schema_model.py +++ b/liminal/results_schemas/models/results_schema_model.py @@ -136,15 +136,11 @@ def get_one( """ if schemas_data is None: schemas_data = cls.get_all_json(benchling_service) - schema = next( - ( - schema - for schema in schemas_data - if schema["systemName"] == wh_schema_name - # and schema["registryId"] == benchling_service.registry_id - ), - None, + matches = [s for s in schemas_data if s["systemName"] == wh_schema_name] + benchling_service._ensure_unambiguous( + matches, f"results schemas with warehouse name {wh_schema_name}" ) + schema = matches[0] if matches else None if schema is None: raise ValueError( f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." diff --git a/liminal/results_schemas/utils.py b/liminal/results_schemas/utils.py index ca8511e..c705198 100644 --- a/liminal/results_schemas/utils.py +++ b/liminal/results_schemas/utils.py @@ -101,5 +101,9 @@ def get_benchling_results_schemas( benchling_service: BenchlingService, ) -> list[AssayResultSchema]: return [ - s for loe in benchling_service.schemas.list_assay_result_schemas() for s in loe + s + for loe in benchling_service.schemas.list_assay_result_schemas() + for s in loe + if not benchling_service._is_registry_scoped + or s.organization.id == benchling_service.organization_id ] diff --git a/liminal/tests/test_registry_scoping.py b/liminal/tests/test_registry_scoping.py new file mode 100644 index 0000000..0b40e98 --- /dev/null +++ b/liminal/tests/test_registry_scoping.py @@ -0,0 +1,245 @@ +from types import SimpleNamespace +from typing import Any +from unittest.mock import patch + +import pytest +from benchling_sdk.errors import BenchlingError + +from liminal.connection.benchling_connection import BenchlingConnection +from liminal.connection.benchling_service import ( + AmbiguousRegistryError, + BenchlingService, +) +from liminal.dropdowns.api import list_dropdowns +from liminal.dropdowns.operations import CreateDropdown +from liminal.dropdowns.utils import get_benchling_dropdowns_dict +from liminal.entity_schemas.api import list_entity_schemas +from liminal.entity_schemas.entity_schema_models import EntitySchemaModel +from liminal.enums import BenchlingEntitySchemaEndpointType +from liminal.results_schemas.api import list_results_schemas + +MAIN = SimpleNamespace(id="src_main", name="Main", owner=SimpleNamespace(id="ent_main")) +OTHER = SimpleNamespace( + id="src_other", name="Other", owner=SimpleNamespace(id="ent_other") +) + + +_TENANT_REGISTRIES: list[Any] = [] + + +@pytest.fixture(autouse=True) +def _fake_registry_api(monkeypatch: pytest.MonkeyPatch) -> None: + """Serves the v2 registry list and the v3 registry lookup from _TENANT_REGISTRIES.""" + monkeypatch.setattr( + BenchlingService, + "registry", + property(lambda _self: SimpleNamespace(registries=lambda: _TENANT_REGISTRIES)), + ) + + def fake_get(_service: Any, url: str) -> Any: + registry_id = url.rsplit("/", 1)[-1] + for r in _TENANT_REGISTRIES: + if r.id == registry_id: + return SimpleNamespace(parsed={"id": r.id, "owner": {"id": r.owner.id}}) + raise BenchlingError( + status_code=404, headers={}, json=None, content=None, parsed=None + ) + + monkeypatch.setattr("liminal.utils.rate_limited_get_v3", fake_get) + + +def _service(registries: list[Any], registry_id: str | None = None) -> BenchlingService: + _TENANT_REGISTRIES[:] = registries + connection = BenchlingConnection( + tenant_name="test-tenant", + api_client_id="test-id", + api_client_secret="test-secret", + registry_id=registry_id, + ) + return BenchlingService(connection, use_api=False) + + +def _schema(system_name: str, registry: Any) -> dict[str, Any]: + return { + "id": f"ts_{system_name}_{registry.id}", + "systemName": system_name, + "registry": {"id": registry.id}, + "organization": {"id": registry.owner.id}, + } + + +def _dropdown(name: str, registry: Any) -> dict[str, Any]: + return { + "id": f"sfs_{name}_{registry.id}", + "name": name, + "registry": {"id": registry.id}, + "archived": False, + "options": [], + } + + +class TestRegistryResolution: + def test_single_registry_without_name_is_unchanged(self) -> None: + service = _service([MAIN]) + assert service.registry_id == "src_main" + assert service.organization_id == "ent_main" + assert not service._is_registry_scoped + assert not service._has_unresolved_registry + + def test_several_registries_without_name_raise_clear_error(self) -> None: + service = _service([MAIN, OTHER]) + assert service._has_unresolved_registry + with pytest.raises(ValueError, match="Set registry_id"): + service.registry_id + with pytest.raises(ValueError, match="Set registry_id"): + service.organization_id + + def test_registry_id_selects_registry_and_owner(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_other") + assert service.registry_id == "src_other" + assert service.organization_id == "ent_other" + assert not service._has_unresolved_registry + + def test_unknown_registry_id_raises(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_missing") + with pytest.raises(ValueError, match="registry src_missing not found"): + service.registry_id + + def test_registry_id_is_fetched_from_v3(self) -> None: + connection = BenchlingConnection( + tenant_name="test-tenant", + api_client_id="test-id", + api_client_secret="test-secret", + registry_id="src_other", + ) + service = BenchlingService(connection, use_api=False) + response = SimpleNamespace( + parsed={"id": "src_other", "owner": {"id": "ent_other"}} + ) + with patch("liminal.utils.rate_limited_get_v3", return_value=response) as get: + assert service.organization_id == "ent_other" + assert service.registry_id == "src_other" + get.assert_called_once_with(service, "/api/v3/registry/src_other") + + +class TestScopeFilter: + def test_unscoped_keeps_every_record(self) -> None: + service = _service([MAIN, OTHER]) + assert service._is_in_registry_scope(_schema("plasmid", OTHER)) + assert service._is_in_registry_scope({"id": "no_owner"}) + + def test_scoped_matches_registry_then_organization(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_main") + assert service._is_in_registry_scope(_schema("plasmid", MAIN)) + assert not service._is_in_registry_scope(_schema("plasmid", OTHER)) + assert service._is_in_registry_scope({"organization": {"id": "ent_main"}}) + assert not service._is_in_registry_scope({"organization": {"id": "ent_other"}}) + assert not service._is_in_registry_scope({"id": "no_owner"}) + + +class TestListFunctions: + def test_entity_schemas_are_scoped(self) -> None: + records = [_schema("plasmid", MAIN), _schema("plasmid", OTHER)] + first_endpoint = next(iter(BenchlingEntitySchemaEndpointType)) + + def fake_list(_service: Any, endpoint: Any, _include_archived: bool) -> list: + return records if endpoint == first_endpoint else [] + + with patch( + "liminal.entity_schemas.api._list_entity_schemas_for_endpoint", + side_effect=fake_list, + ): + scoped = list_entity_schemas( + _service([MAIN, OTHER], registry_id="src_main") + ) + unscoped = list_entity_schemas(_service([MAIN, OTHER])) + + assert [s["registry"]["id"] for s in scoped] == ["src_main"] + assert len(unscoped) == 2 + + def test_dropdowns_are_scoped(self) -> None: + records = [_dropdown("Species", MAIN), _dropdown("Species", OTHER)] + with patch("liminal.dropdowns.api.list_all_items_v3", return_value=records): + scoped = list_dropdowns(_service([MAIN, OTHER], registry_id="src_other")) + assert [d["registry"]["id"] for d in scoped] == ["src_other"] + + def test_results_schemas_are_scoped_by_organization(self) -> None: + records = [ + { + "id": "assaysch_1", + "systemName": "assay", + "organization": {"id": "ent_main"}, + }, + { + "id": "assaysch_2", + "systemName": "assay", + "organization": {"id": "ent_other"}, + }, + ] + with patch( + "liminal.results_schemas.api.list_all_items_v3", return_value=records + ): + scoped = list_results_schemas( + _service([MAIN, OTHER], registry_id="src_main") + ) + assert [s["id"] for s in scoped] == ["assaysch_1"] + + +class TestAmbiguousLookups: + @pytest.fixture(autouse=True) + def _skip_field_fetch(self) -> Any: + with ( + patch( + "liminal.entity_schemas.entity_schema_models.attach_entity_schema_field_definitions", + side_effect=lambda _service, schema: schema, + ), + patch.object(EntitySchemaModel, "model_validate", side_effect=lambda s: s), + ): + yield + + def test_repeated_name_on_unresolved_tenant_raises(self) -> None: + schemas = [_schema("plasmid", MAIN), _schema("plasmid", OTHER)] + with pytest.raises(AmbiguousRegistryError, match="2 entity schemas"): + EntitySchemaModel.get_one(_service([MAIN, OTHER]), "plasmid", schemas) + + def test_repeated_name_on_single_registry_tenant_is_unchanged(self) -> None: + schemas = [_schema("plasmid", MAIN), _schema("plasmid", MAIN)] + schema: Any = EntitySchemaModel.get_one(_service([MAIN]), "plasmid", schemas) + assert schema is schemas[0] + + def test_dropdowns_dict_raises_on_shared_names(self) -> None: + records = [_dropdown("Species", MAIN), _dropdown("Species", OTHER)] + with ( + patch( + "liminal.dropdowns.utils.list_dropdowns_with_options", + return_value=records, + ), + pytest.raises(AmbiguousRegistryError, match="Species"), + ): + get_benchling_dropdowns_dict(_service([MAIN, OTHER])) + + +class TestCreateValidation: + def test_create_dropdown_without_registry_fails_before_writing(self) -> None: + with ( + patch( + "liminal.dropdowns.operations.dropdown_exists_in_benchling", + return_value=False, + ), + pytest.raises(ValueError, match="choose where to create this dropdown"), + ): + CreateDropdown("Species", ["Human"]).validate(_service([MAIN, OTHER])) + + def test_create_dropdown_validation_is_unchanged_for_single_registry(self) -> None: + with patch( + "liminal.dropdowns.operations.dropdown_exists_in_benchling" + ) as exists: + CreateDropdown("Species", ["Human"]).validate(_service([MAIN])) + exists.assert_not_called() + + def test_unarchiving_existing_dropdown_still_validates(self) -> None: + with patch( + "liminal.dropdowns.operations.dropdown_exists_in_benchling", + return_value=True, + ): + CreateDropdown("Species", ["Human"]).validate(_service([MAIN, OTHER])) From 2e289952b9a1795b22339a2819c4e422e8834922 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Mon, 2 Nov 2026 18:19:50 -0500 Subject: [PATCH 02/14] raise error in BenchlingService init for multiple registries no registry_id, move validation to base helpers, clean and simplify implementation --- docs/reference/benchling-connection.md | 2 +- liminal/connection/benchling_service.py | 119 ++++--- liminal/dropdowns/api.py | 8 +- liminal/dropdowns/operations.py | 10 - liminal/dropdowns/utils.py | 10 - liminal/entity_schemas/api.py | 8 +- .../entity_schemas/entity_schema_models.py | 11 +- liminal/entity_schemas/operations.py | 16 - liminal/orm/base_model.py | 4 +- liminal/orm/base_results_model.py | 4 +- liminal/results_schemas/api.py | 10 +- .../models/results_schema_model.py | 11 +- liminal/results_schemas/utils.py | 5 +- liminal/tests/test_benchling_service.py | 14 + liminal/tests/test_registry_scoping.py | 292 ++++++++---------- liminal/utils.py | 26 +- 16 files changed, 253 insertions(+), 297 deletions(-) diff --git a/docs/reference/benchling-connection.md b/docs/reference/benchling-connection.md index e33a46c..5f50631 100644 --- a/docs/reference/benchling-connection.md +++ b/docs/reference/benchling-connection.md @@ -45,7 +45,7 @@ connection = BenchlingConnection( - **registry_id: Optional[str] = None** - The API id of the registry to scope this connection to, for example `src_abc123`. Only needed when the tenant has more than one registry. When set, Liminal reads and writes only the entity schemas and dropdowns in that registry, and the results schemas owned by the registry's organization. Without it, Liminal works as before on single-registry tenants. On a tenant with several registries, Liminal raises an error rather than guess when a lookup matches objects in more than one registry, or when it would need to create a schema or dropdown. + The API id of the registry to scope this connection to, for example `src_abc123`. Only needed when the tenant has more than one registry. When set, Liminal reads and writes only the entity schemas and dropdowns in that registry, and the results schemas owned by the registry's organization. For a tenant with multiple registries and no `registry_id` set, Liminal raises an error in `BenchlingService`. !!! tip For a tenant with several registries, give each registry its own Liminal directory (`env.py` and `versions/`), with one `BenchlingConnection` per tenant that sets that registry's `registry_id`. Each registry then keeps its own revision history in its own `liminal_remote` schema. diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index 97bbe89..47a3723 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -1,11 +1,12 @@ import logging import warnings -from typing import TYPE_CHECKING, Any, NamedTuple +from functools import cached_property +from typing import TYPE_CHECKING, Any from benchling_sdk.auth.client_credentials_oauth2 import ClientCredentialsOAuth2 from benchling_sdk.benchling import Benchling, BenchlingApiClientDecorator -from benchling_sdk.errors import BenchlingError from benchling_sdk.helpers.retry_helpers import RetryStrategy +from pydantic import BaseModel from sqlalchemy import create_engine from sqlalchemy.engine import Engine from sqlalchemy.orm import Session, configure_mappers @@ -29,7 +30,7 @@ REMOTE_REVISION_ID_FIELD_WH_NAME = "revision_id" -class RegistryRef(NamedTuple): +class RegistryRef(BaseModel): """The registry a connection writes to, and the organization that owns it.""" id: str @@ -76,8 +77,6 @@ def __init__( ) self.connection = connection self._session: Session | None = None - self._registry: RegistryRef | None = None - self._registry_count: int | None = None self.use_api = use_api self.benchling_tenant = connection.tenant_name if use_api: @@ -109,91 +108,79 @@ def __init__( raise ValueError( "use_db is True but warehouse_connection_string not provided in BenchlingConnection." ) + if self.has_ambiguous_registries(): + raise ValueError( + f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " + "Liminal does not allow ambiguous registries. Set registry_id on the BenchlingConnection to choose one." + ) @property def session(self) -> Session: """Returns a session made by the sessionmaker""" return self.get_session() - @property + @cached_property def registry_id(self) -> str: """The id of the registry this connection writes to. Raises if it can't be determined.""" - return self._get_registry().id + return self._registry_ref.id - @property + @cached_property def organization_id(self) -> str: """The id of the organization that owns this connection's registry. Raises if it can't be determined.""" - return self._get_registry().owner_id + return self._registry_ref.owner_id - @property - def _is_registry_scoped(self) -> bool: - """Whether this connection is scoped to one registry through `registry_id`.""" - return self.connection.registry_id is not None + @cached_property + def _registries(self) -> list[RegistryRef]: + """Every registry on the tenant.""" + from liminal.utils import list_all_items_v3 + + return [ + RegistryRef(id=r["id"], owner_id=r["owner"]["id"]) + for r in list_all_items_v3(self, "/api/v3/registry") + ] @property - def _has_unresolved_registry(self) -> bool: - """Whether the tenant has more than one registry and this connection doesn't name one. - Lookups by name can then match objects in several registries.""" - return self._find_registry() is None + def _registry_ref(self) -> RegistryRef: + if len(self._registries) != 1: + if self.connection.registry_id is None: + raise ValueError( + f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " + "Set registry_id on the BenchlingConnection to choose one." + ) + else: + registry_ref = next( + ( + r + for r in self._registries + if r.id == self.connection.registry_id + ), + None, + ) + if registry_ref is None: + raise ValueError( + f"Tenant {self.benchling_tenant}: registry {self.connection.registry_id} not found." + ) + return registry_ref + else: + return self._registries[0] - def _is_in_registry_scope(self, record: dict[str, Any]) -> bool: + def is_in_registry_scope(self, record: dict[str, Any]) -> bool: """Whether a v3 API record belongs to this connection's registry or its organization. - Always True when the connection isn't scoped to a registry.""" - if not self._is_registry_scoped: - return True + Always True when the connection is scoped to a registry.""" registry_id = (record.get("registry") or {}).get("id") if registry_id: return registry_id == self.registry_id + return False + + def is_in_organization_scope(self, record: dict[str, Any]) -> bool: + """Whether a v3 API record belongs to this connection's organization.""" organization_id = (record.get("organization") or {}).get("id") if organization_id: return organization_id == self.organization_id return False - def _ensure_unambiguous(self, matches: list[Any], description: str) -> None: - """Raises when a lookup matches more than one object on a tenant whose registry isn't set, - because the matches may sit in different registries and Liminal would have to guess.""" - if len(matches) > 1 and self._has_unresolved_registry: - raise AmbiguousRegistryError( - f"Tenant {self.benchling_tenant}: found {len(matches)} {description}. " - "The tenant has more than one registry, so set registry_id on the BenchlingConnection to choose one." - ) - - def _find_registry(self) -> RegistryRef | None: - """Resolves the connection's registry once. Returns None when the tenant has several registries - and the connection doesn't set registry_id.""" - if self._registry_count is None: - if self.connection.registry_id is not None: - self._registry = self._get_registry_by_id(self.connection.registry_id) - self._registry_count = 1 - else: - registries = self.registry.registries() - self._registry_count = len(registries) - if len(registries) == 1: - self._registry = RegistryRef( - registries[0].id, registries[0].owner.id - ) - return self._registry - - def _get_registry_by_id(self, registry_id: str) -> RegistryRef: - from liminal.utils import rate_limited_get_v3 - - try: - response = rate_limited_get_v3(self, f"/api/v3/registry/{registry_id}") - except BenchlingError as e: - raise ValueError( - f"Tenant {self.benchling_tenant}: registry {registry_id} not found ({e.status_code})." - ) from e - registry = response.parsed - return RegistryRef(registry["id"], registry["owner"]["id"]) - - def _get_registry(self) -> RegistryRef: - registry = self._find_registry() - if registry is None: - raise ValueError( - f"Tenant {self.benchling_tenant} has {self._registry_count} registries. " - "Set registry_id on the BenchlingConnection to choose one." - ) - return registry + def has_ambiguous_registries(self) -> bool: + return len(self._registries) > 1 and self.connection.registry_id is None def __enter__(self) -> Session: self._session = self.get_session() diff --git a/liminal/dropdowns/api.py b/liminal/dropdowns/api.py index fcc2ad0..cbc338b 100644 --- a/liminal/dropdowns/api.py +++ b/liminal/dropdowns/api.py @@ -27,10 +27,12 @@ def list_dropdowns( benchling_service: BenchlingService, include_archived: bool = True ) -> list[dict[str, Any]]: """Fetch dropdowns from the v3 API.""" - dropdowns = list_all_items_v3( - benchling_service, f"{_DROPDOWN_API_PATH}/items", include_archived + return list_all_items_v3( + benchling_service, + f"{_DROPDOWN_API_PATH}/items", + include_archived, + registry_scoped=True, ) - return [d for d in dropdowns if benchling_service._is_in_registry_scope(d)] def list_dropdown_options( diff --git a/liminal/dropdowns/operations.py b/liminal/dropdowns/operations.py index b09827c..1797fd6 100644 --- a/liminal/dropdowns/operations.py +++ b/liminal/dropdowns/operations.py @@ -66,16 +66,6 @@ def execute(self, benchling_service: BenchlingService) -> dict[str, Dropdown]: f"Dropdown {self.dropdown_name} is different in code versus Benchling." ) - def validate(self, benchling_service: BenchlingService) -> None: - if ( - benchling_service._has_unresolved_registry - and not dropdown_exists_in_benchling(benchling_service, self.dropdown_name) - ): - raise ValueError( - f"{self.__class__.__name__} {self.dropdown_name}: Tenant {benchling_service.benchling_tenant} has more than one registry. " - "Set registry_id on the BenchlingConnection to choose where to create this dropdown." - ) - def describe_operation(self) -> str: return f"{self.dropdown_name}: Creating dropdown with options {self.dropdown_options}." diff --git a/liminal/dropdowns/utils.py b/liminal/dropdowns/utils.py index 040a1b2..a8ce009 100644 --- a/liminal/dropdowns/utils.py +++ b/liminal/dropdowns/utils.py @@ -35,8 +35,6 @@ def get_benchling_dropdown_summaries( for sublist in benchling_service.dropdowns.list() for dropdown in sublist ] - if not benchling_service._is_registry_scoped: - return summaries # The v2 dropdown summary has no registry, so scope it by the ids the v3 list returns. scoped_ids = { d["id"] for d in list_dropdowns(benchling_service, include_archived=True) @@ -50,7 +48,6 @@ def get_benchling_dropdown_summary_by_name( matches = [ d for d in get_benchling_dropdown_summaries(benchling_service) if d.name == name ] - benchling_service._ensure_unambiguous(matches, f"dropdowns named {name}") if matches: return matches[0] raise Exception(f"Dropdown {name} not found in given list.") @@ -62,7 +59,6 @@ def get_benchling_dropdown_by_name( matches = [ d for d in get_benchling_dropdown_summaries(benchling_service) if d.name == name ] - benchling_service._ensure_unambiguous(matches, f"dropdowns named {name}") dropdown = matches[-1] if matches else None if dropdown is None: raise Exception( @@ -107,12 +103,6 @@ def get_benchling_dropdowns_dict( include_archived=include_archived, dropdown_names_for_options=dropdown_names_for_options, ) - names = [dropdown["name"] for dropdown in dropdowns] - duplicate_names = sorted({n for n in names if names.count(n) > 1}) - benchling_service._ensure_unambiguous( - [d for d in dropdowns if d["name"] in duplicate_names], - f"dropdowns sharing the names {duplicate_names}", - ) return { dropdown["name"]: _convert_dropdown_from_v3_to_v2_sdk( dropdown, include_archived diff --git a/liminal/entity_schemas/api.py b/liminal/entity_schemas/api.py index 6b83b79..ce11cab 100644 --- a/liminal/entity_schemas/api.py +++ b/liminal/entity_schemas/api.py @@ -50,19 +50,18 @@ def list_entity_schemas( for future in as_completed(futures): entity_schemas.extend(future.result()) - return [s for s in entity_schemas if benchling_service._is_in_registry_scope(s)] + return entity_schemas @lru_cache -def list_entity_schemas_cached( +def list_entity_schemas_v2_cached( benchling_service: BenchlingService, ) -> list[EntitySchema]: return [ s for schemas in benchling_service.schemas.list_entity_schemas() for s in schemas - if not benchling_service._is_registry_scoped - or s.registry_id == benchling_service.registry_id + if s.registry_id == benchling_service.registry_id ] @@ -76,6 +75,7 @@ def _list_entity_schemas_for_endpoint( benchling_service, f"{_entity_schema_specific_api_path(endpoint)}/items", include_archived, + registry_scoped=True, ) diff --git a/liminal/entity_schemas/entity_schema_models.py b/liminal/entity_schemas/entity_schema_models.py index d0e5869..faf7e12 100644 --- a/liminal/entity_schemas/entity_schema_models.py +++ b/liminal/entity_schemas/entity_schema_models.py @@ -531,11 +531,14 @@ def get_one( ) -> EntitySchemaModel: if schemas_data is None: schemas_data = list_entity_schemas(benchling_service, include_archived=True) - matches = [s for s in schemas_data if s["systemName"] == wh_schema_name] - benchling_service._ensure_unambiguous( - matches, f"entity schemas with warehouse name {wh_schema_name}" + schema = next( + ( + schema + for schema in schemas_data + if schema["systemName"] == wh_schema_name + ), + None, ) - schema = matches[0] if matches else None if schema is None: raise ValueError( f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." diff --git a/liminal/entity_schemas/operations.py b/liminal/entity_schemas/operations.py index 5c4f2d7..a592a9d 100644 --- a/liminal/entity_schemas/operations.py +++ b/liminal/entity_schemas/operations.py @@ -106,22 +106,6 @@ def validate(self, benchling_service: BenchlingService) -> None: f"{self.__class__.__name__} {self._validated_schema_properties.warehouse_name}: On field {field.warehouse_name}, unit {field.unit_name} not found in Benchling Unit Dictionary as a valid unit. Please check the field definition or your Unit Dictionary." ) - if benchling_service._has_unresolved_registry: - self._validate_registry_for_create(benchling_service) - - def _validate_registry_for_create( - self, benchling_service: BenchlingService - ) -> None: - """Fails before any write when creating this schema would need a registry the connection doesn't name.""" - wh_name = self._validated_schema_properties.warehouse_name - try: - EntitySchemaModel.get_one(benchling_service, wh_name) - except ValueError: - raise ValueError( - f"{self.__class__.__name__} {wh_name}: Tenant {benchling_service.benchling_tenant} has more than one registry. " - "Set registry_id on the BenchlingConnection to choose where to create this schema." - ) - def _validate_create(self, benchling_service: BenchlingService) -> None: all_schemas = list_entity_schemas(benchling_service, include_archived=True) if self._validated_schema_properties.name in [ diff --git a/liminal/orm/base_model.py b/liminal/orm/base_model.py index 10937ca..48f36d3 100644 --- a/liminal/orm/base_model.py +++ b/liminal/orm/base_model.py @@ -14,7 +14,7 @@ from liminal.base.base_dropdown import BaseDropdown from liminal.base.base_validation_filters import BaseValidatorFilters from liminal.connection.benchling_service import BenchlingService -from liminal.entity_schemas.api import list_entity_schemas_cached +from liminal.entity_schemas.api import list_entity_schemas_v2_cached from liminal.enums import BenchlingNamingStrategy from liminal.enums.benchling_entity_type import BenchlingEntityType from liminal.enums.sequence_constraint import SequenceConstraint @@ -308,7 +308,7 @@ def get_id(cls, benchling_service: BenchlingService) -> str: str The id of the schema. """ - all_schemas = list_entity_schemas_cached(benchling_service) + all_schemas = list_entity_schemas_v2_cached(benchling_service) schemas_found_by_name = [ s for s in all_schemas if s.name == cls.__schema_properties__.name diff --git a/liminal/orm/base_results_model.py b/liminal/orm/base_results_model.py index 6a4fe80..a1e2496 100644 --- a/liminal/orm/base_results_model.py +++ b/liminal/orm/base_results_model.py @@ -16,7 +16,7 @@ from liminal.orm.base import Base from liminal.orm.base_tables.user import User from liminal.orm.results_schema_properties import ResultsSchemaProperties -from liminal.results_schemas.utils import get_benchling_results_schemas +from liminal.results_schemas.utils import list_benchling_results_schemas_v2_cached from liminal.validation import BenchlingValidatorReport T = TypeVar("T") @@ -99,7 +99,7 @@ def get_id(cls, benchling_service: BenchlingService) -> str: str The id of the results schema. """ - all_schemas = get_benchling_results_schemas(benchling_service) + all_schemas = list_benchling_results_schemas_v2_cached(benchling_service) schemas_found_by_name = [ s for s in all_schemas if s.name == cls.__schema_properties__.name diff --git a/liminal/results_schemas/api.py b/liminal/results_schemas/api.py index 62f1b04..9c89ce7 100644 --- a/liminal/results_schemas/api.py +++ b/liminal/results_schemas/api.py @@ -13,11 +13,13 @@ def list_results_schemas( benchling_service: BenchlingService, ) -> list[dict[str, Any]]: - """Fetch result schemas from the v3 API.""" - result_schemas = list_all_items_v3( - benchling_service, f"{_RESULT_SCHEMA_API_PATH}/items" + """Fetch result schemas from the v3 API. Result schemas carry an organization but no registry, + so they are scoped to the organization that owns the connection's registry.""" + return list_all_items_v3( + benchling_service, + f"{_RESULT_SCHEMA_API_PATH}/items", + organization_scoped=True, ) - return [s for s in result_schemas if benchling_service._is_in_registry_scope(s)] def list_result_schema_field_definitions( diff --git a/liminal/results_schemas/models/results_schema_model.py b/liminal/results_schemas/models/results_schema_model.py index 0ade738..8ba37b1 100644 --- a/liminal/results_schemas/models/results_schema_model.py +++ b/liminal/results_schemas/models/results_schema_model.py @@ -136,11 +136,14 @@ def get_one( """ if schemas_data is None: schemas_data = cls.get_all_json(benchling_service) - matches = [s for s in schemas_data if s["systemName"] == wh_schema_name] - benchling_service._ensure_unambiguous( - matches, f"results schemas with warehouse name {wh_schema_name}" + schema = next( + ( + schema + for schema in schemas_data + if schema["systemName"] == wh_schema_name + ), + None, ) - schema = matches[0] if matches else None if schema is None: raise ValueError( f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." diff --git a/liminal/results_schemas/utils.py b/liminal/results_schemas/utils.py index c705198..9de8762 100644 --- a/liminal/results_schemas/utils.py +++ b/liminal/results_schemas/utils.py @@ -97,13 +97,12 @@ def get_converted_results_schemas( @lru_cache -def get_benchling_results_schemas( +def list_benchling_results_schemas_v2_cached( benchling_service: BenchlingService, ) -> list[AssayResultSchema]: return [ s for loe in benchling_service.schemas.list_assay_result_schemas() for s in loe - if not benchling_service._is_registry_scoped - or s.organization.id == benchling_service.organization_id + if s.organization.id == benchling_service.organization_id ] diff --git a/liminal/tests/test_benchling_service.py b/liminal/tests/test_benchling_service.py index ff80d36..4271271 100644 --- a/liminal/tests/test_benchling_service.py +++ b/liminal/tests/test_benchling_service.py @@ -1,5 +1,9 @@ +from collections.abc import Iterator +from types import SimpleNamespace from typing import cast +from unittest.mock import patch +import pytest from benchling_api_client.v2.benchling_client import BenchlingApiClient from liminal.connection.benchling_connection import BenchlingConnection @@ -17,6 +21,16 @@ def _connection() -> BenchlingConnection: ) +@pytest.fixture(autouse=True) +def _single_registry_tenant() -> Iterator[None]: + """BenchlingService lists the tenant's registries on init, so serve one instead of calling Benchling.""" + page = SimpleNamespace( + parsed={"items": [{"id": "src_main", "owner": {"id": "ent_main"}}]} + ) + with patch("liminal.utils.rate_limited_get_v3", return_value=page): + yield + + class TestBenchlingServiceClientDecorator: def test_default_timeout_is_sdk_default(self) -> None: service = BenchlingService(_connection(), use_db=False) diff --git a/liminal/tests/test_registry_scoping.py b/liminal/tests/test_registry_scoping.py index 0b40e98..ca298db 100644 --- a/liminal/tests/test_registry_scoping.py +++ b/liminal/tests/test_registry_scoping.py @@ -1,20 +1,15 @@ +from collections.abc import Callable, Iterator +from contextlib import contextmanager from types import SimpleNamespace from typing import Any from unittest.mock import patch import pytest -from benchling_sdk.errors import BenchlingError from liminal.connection.benchling_connection import BenchlingConnection -from liminal.connection.benchling_service import ( - AmbiguousRegistryError, - BenchlingService, -) +from liminal.connection.benchling_service import BenchlingService from liminal.dropdowns.api import list_dropdowns -from liminal.dropdowns.operations import CreateDropdown -from liminal.dropdowns.utils import get_benchling_dropdowns_dict from liminal.entity_schemas.api import list_entity_schemas -from liminal.entity_schemas.entity_schema_models import EntitySchemaModel from liminal.enums import BenchlingEntitySchemaEndpointType from liminal.results_schemas.api import list_results_schemas @@ -27,25 +22,36 @@ _TENANT_REGISTRIES: list[Any] = [] -@pytest.fixture(autouse=True) -def _fake_registry_api(monkeypatch: pytest.MonkeyPatch) -> None: - """Serves the v2 registry list and the v3 registry lookup from _TENANT_REGISTRIES.""" - monkeypatch.setattr( - BenchlingService, - "registry", - property(lambda _self: SimpleNamespace(registries=lambda: _TENANT_REGISTRIES)), +def _registry_page() -> Any: + return SimpleNamespace( + parsed={ + "items": [ + {"id": r.id, "owner": {"id": r.owner.id}} for r in _TENANT_REGISTRIES + ] + } ) + +@contextmanager +def _v3_api(handler: Callable[[str], list[dict[str, Any]]]) -> Iterator[None]: + """Serves the v3 registry list from _TENANT_REGISTRIES and every other v3 list from handler.""" + def fake_get(_service: Any, url: str) -> Any: - registry_id = url.rsplit("/", 1)[-1] - for r in _TENANT_REGISTRIES: - if r.id == registry_id: - return SimpleNamespace(parsed={"id": r.id, "owner": {"id": r.owner.id}}) - raise BenchlingError( - status_code=404, headers={}, json=None, content=None, parsed=None - ) + if url.startswith("/api/v3/registry?"): + return _registry_page() + return SimpleNamespace(parsed={"items": handler(url)}) - monkeypatch.setattr("liminal.utils.rate_limited_get_v3", fake_get) + with patch("liminal.utils.rate_limited_get_v3", side_effect=fake_get): + yield + + +@pytest.fixture(autouse=True) +def _fake_registry_api(monkeypatch: pytest.MonkeyPatch) -> None: + """Serves the v3 registry list from _TENANT_REGISTRIES.""" + monkeypatch.setattr( + "liminal.utils.rate_limited_get_v3", + lambda _service, _url: _registry_page(), + ) def _service(registries: list[Any], registry_id: str | None = None) -> BenchlingService: @@ -79,167 +85,125 @@ def _dropdown(name: str, registry: Any) -> dict[str, Any]: class TestRegistryResolution: - def test_single_registry_without_name_is_unchanged(self) -> None: + def test_single_registry_without_name_resolves(self) -> None: service = _service([MAIN]) assert service.registry_id == "src_main" assert service.organization_id == "ent_main" - assert not service._is_registry_scoped - assert not service._has_unresolved_registry - def test_several_registries_without_name_raise_clear_error(self) -> None: - service = _service([MAIN, OTHER]) - assert service._has_unresolved_registry - with pytest.raises(ValueError, match="Set registry_id"): - service.registry_id - with pytest.raises(ValueError, match="Set registry_id"): - service.organization_id + def test_several_registries_without_name_fail_on_init(self) -> None: + with pytest.raises(ValueError, match="does not allow ambiguous registries"): + _service([MAIN, OTHER]) + + def test_single_registry_with_name_is_not_ambiguous(self) -> None: + service = _service([MAIN], registry_id="src_main") + assert not service.has_ambiguous_registries() + assert service.registry_id == "src_main" + + def test_several_registries_with_name_are_not_ambiguous(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_main") + assert not service.has_ambiguous_registries() def test_registry_id_selects_registry_and_owner(self) -> None: service = _service([MAIN, OTHER], registry_id="src_other") assert service.registry_id == "src_other" assert service.organization_id == "ent_other" - assert not service._has_unresolved_registry - def test_unknown_registry_id_raises(self) -> None: + def test_unknown_registry_id_raises_on_access_not_init(self) -> None: service = _service([MAIN, OTHER], registry_id="src_missing") with pytest.raises(ValueError, match="registry src_missing not found"): service.registry_id - def test_registry_id_is_fetched_from_v3(self) -> None: - connection = BenchlingConnection( - tenant_name="test-tenant", - api_client_id="test-id", - api_client_secret="test-secret", - registry_id="src_other", - ) - service = BenchlingService(connection, use_api=False) - response = SimpleNamespace( - parsed={"id": "src_other", "owner": {"id": "ent_other"}} - ) - with patch("liminal.utils.rate_limited_get_v3", return_value=response) as get: - assert service.organization_id == "ent_other" - assert service.registry_id == "src_other" - get.assert_called_once_with(service, "/api/v3/registry/src_other") + def test_registries_are_listed_once(self) -> None: + with patch( + "liminal.utils.rate_limited_get_v3", return_value=_registry_page() + ) as get: + service = _service([MAIN, OTHER], registry_id="src_other") + service.registry_id + service.organization_id + get.assert_called_once() class TestScopeFilter: - def test_unscoped_keeps_every_record(self) -> None: - service = _service([MAIN, OTHER]) - assert service._is_in_registry_scope(_schema("plasmid", OTHER)) - assert service._is_in_registry_scope({"id": "no_owner"}) - - def test_scoped_matches_registry_then_organization(self) -> None: + def test_registry_scope_matches_registry_only(self) -> None: service = _service([MAIN, OTHER], registry_id="src_main") - assert service._is_in_registry_scope(_schema("plasmid", MAIN)) - assert not service._is_in_registry_scope(_schema("plasmid", OTHER)) - assert service._is_in_registry_scope({"organization": {"id": "ent_main"}}) - assert not service._is_in_registry_scope({"organization": {"id": "ent_other"}}) - assert not service._is_in_registry_scope({"id": "no_owner"}) + assert service.is_in_registry_scope(_schema("plasmid", MAIN)) + assert not service.is_in_registry_scope(_schema("plasmid", OTHER)) + assert not service.is_in_registry_scope({"organization": {"id": "ent_main"}}) + assert not service.is_in_registry_scope({"id": "no_owner"}) + def test_single_registry_without_name_scopes_to_it(self) -> None: + service = _service([MAIN]) + assert service.is_in_registry_scope(_schema("plasmid", MAIN)) + assert not service.is_in_registry_scope(_schema("plasmid", OTHER)) -class TestListFunctions: - def test_entity_schemas_are_scoped(self) -> None: - records = [_schema("plasmid", MAIN), _schema("plasmid", OTHER)] - first_endpoint = next(iter(BenchlingEntitySchemaEndpointType)) - def fake_list(_service: Any, endpoint: Any, _include_archived: bool) -> list: - return records if endpoint == first_endpoint else [] +def _results_schema(registry: Any) -> dict[str, Any]: + return { + "id": f"assaysch_{registry.id}", + "systemName": "assay", + "organization": {"id": registry.owner.id}, + } - with patch( - "liminal.entity_schemas.api._list_entity_schemas_for_endpoint", - side_effect=fake_list, - ): - scoped = list_entity_schemas( - _service([MAIN, OTHER], registry_id="src_main") - ) - unscoped = list_entity_schemas(_service([MAIN, OTHER])) - - assert [s["registry"]["id"] for s in scoped] == ["src_main"] - assert len(unscoped) == 2 - - def test_dropdowns_are_scoped(self) -> None: - records = [_dropdown("Species", MAIN), _dropdown("Species", OTHER)] - with patch("liminal.dropdowns.api.list_all_items_v3", return_value=records): - scoped = list_dropdowns(_service([MAIN, OTHER], registry_id="src_other")) - assert [d["registry"]["id"] for d in scoped] == ["src_other"] - - def test_results_schemas_are_scoped_by_organization(self) -> None: - records = [ - { - "id": "assaysch_1", - "systemName": "assay", - "organization": {"id": "ent_main"}, - }, - { - "id": "assaysch_2", - "systemName": "assay", - "organization": {"id": "ent_other"}, - }, - ] - with patch( - "liminal.results_schemas.api.list_all_items_v3", return_value=records - ): - scoped = list_results_schemas( - _service([MAIN, OTHER], registry_id="src_main") - ) - assert [s["id"] for s in scoped] == ["assaysch_1"] - - -class TestAmbiguousLookups: - @pytest.fixture(autouse=True) - def _skip_field_fetch(self) -> Any: - with ( - patch( - "liminal.entity_schemas.entity_schema_models.attach_entity_schema_field_definitions", - side_effect=lambda _service, schema: schema, - ), - patch.object(EntitySchemaModel, "model_validate", side_effect=lambda s: s), - ): - yield - - def test_repeated_name_on_unresolved_tenant_raises(self) -> None: - schemas = [_schema("plasmid", MAIN), _schema("plasmid", OTHER)] - with pytest.raises(AmbiguousRegistryError, match="2 entity schemas"): - EntitySchemaModel.get_one(_service([MAIN, OTHER]), "plasmid", schemas) - - def test_repeated_name_on_single_registry_tenant_is_unchanged(self) -> None: - schemas = [_schema("plasmid", MAIN), _schema("plasmid", MAIN)] - schema: Any = EntitySchemaModel.get_one(_service([MAIN]), "plasmid", schemas) - assert schema is schemas[0] - - def test_dropdowns_dict_raises_on_shared_names(self) -> None: - records = [_dropdown("Species", MAIN), _dropdown("Species", OTHER)] - with ( - patch( - "liminal.dropdowns.utils.list_dropdowns_with_options", - return_value=records, - ), - pytest.raises(AmbiguousRegistryError, match="Species"), - ): - get_benchling_dropdowns_dict(_service([MAIN, OTHER])) - - -class TestCreateValidation: - def test_create_dropdown_without_registry_fails_before_writing(self) -> None: - with ( - patch( - "liminal.dropdowns.operations.dropdown_exists_in_benchling", - return_value=False, - ), - pytest.raises(ValueError, match="choose where to create this dropdown"), - ): - CreateDropdown("Species", ["Human"]).validate(_service([MAIN, OTHER])) - - def test_create_dropdown_validation_is_unchanged_for_single_registry(self) -> None: - with patch( - "liminal.dropdowns.operations.dropdown_exists_in_benchling" - ) as exists: - CreateDropdown("Species", ["Human"]).validate(_service([MAIN])) - exists.assert_not_called() - def test_unarchiving_existing_dropdown_still_validates(self) -> None: - with patch( - "liminal.dropdowns.operations.dropdown_exists_in_benchling", - return_value=True, - ): - CreateDropdown("Species", ["Human"]).validate(_service([MAIN, OTHER])) +def _list_entity_schemas(service: BenchlingService) -> list[dict[str, Any]]: + first_endpoint = next(iter(BenchlingEntitySchemaEndpointType)) + records = [_schema("plasmid", MAIN), _schema("plasmid", OTHER)] + + def handler(url: str) -> list[dict[str, Any]]: + return records if url.startswith(f"/api/v3/{first_endpoint.value}/") else [] + + with _v3_api(handler): + return list_entity_schemas(service) + + +def _list_dropdowns(service: BenchlingService) -> list[dict[str, Any]]: + records = [_dropdown("Species", MAIN), _dropdown("Species", OTHER)] + with _v3_api(lambda _url: records): + return list_dropdowns(service) + + +def _list_results_schemas(service: BenchlingService) -> list[dict[str, Any]]: + records = [_results_schema(MAIN), _results_schema(OTHER)] + with _v3_api(lambda _url: records): + return list_results_schemas(service) + + +# Each lister returns records from both MAIN and OTHER. `owner_of` reads the scoping value off a +# record and `scope_of` gives the value a registry should produce: the registry id for entity +# schemas and dropdowns, the owning organization id for results schemas. +_LISTERS = pytest.mark.parametrize( + "lister, owner_of, scope_of", + [ + (_list_entity_schemas, lambda r: r["registry"]["id"], lambda reg: reg.id), + (_list_dropdowns, lambda r: r["registry"]["id"], lambda reg: reg.id), + ( + _list_results_schemas, + lambda r: r["organization"]["id"], + lambda reg: reg.owner.id, + ), + ], + ids=["entity_schemas", "dropdowns", "results_schemas"], +) + + +class TestListFunctions: + @_LISTERS + def test_one_registry_without_name_keeps_only_that_registry( + self, lister: Callable, owner_of: Callable, scope_of: Callable + ) -> None: + results = lister(_service([MAIN])) + assert [owner_of(r) for r in results] == [scope_of(MAIN)] + + @_LISTERS + def test_two_registries_without_name_raise( + self, lister: Callable, owner_of: Callable, scope_of: Callable + ) -> None: + with pytest.raises(ValueError, match="does not allow ambiguous registries"): + lister(_service([MAIN, OTHER])) + + @_LISTERS + def test_two_registries_with_name_keep_only_that_registry( + self, lister: Callable, owner_of: Callable, scope_of: Callable + ) -> None: + results = lister(_service([MAIN, OTHER], registry_id="src_other")) + assert [owner_of(r) for r in results] == [scope_of(OTHER)] diff --git a/liminal/utils.py b/liminal/utils.py index 1a34b41..77e60f0 100644 --- a/liminal/utils.py +++ b/liminal/utils.py @@ -181,8 +181,15 @@ def list_all_items_v3( url: str, include_archived: bool = False, page_size: int | None = MAX_PAGE_SIZE, + registry_scoped: bool = False, + organization_scoped: bool = False, ) -> list[dict[str, Any]]: - """Fetch every item from a v3 list endpoint, following nextToken pagination.""" + """Fetch every item from a v3 list endpoint, following nextToken pagination. + + With registry_scoped, drops items outside the connection's registry. With organization_scoped, + drops items outside the organization that owns the connection's registry. Only use them on + endpoints whose items carry a registry or an organization respectively, and never while + resolving the registry itself.""" query_params = [f"archived.anyOf={'true,false' if include_archived else 'false'}"] if page_size: query_params.append(f"pageSize={page_size}") @@ -198,9 +205,20 @@ def list_all_items_v3( if parsed_response is None: raise ValueError(f"No response body returned for {url}.") if isinstance(parsed_response, list): - return items + parsed_response - items.extend(parsed_response.get("items", [])) - next_token = parsed_response.get("nextToken") + page_items = parsed_response + next_token = None + else: + page_items = parsed_response.get("items", []) + next_token = parsed_response.get("nextToken") + if registry_scoped: + page_items = [ + i for i in page_items if benchling_service.is_in_registry_scope(i) + ] + if organization_scoped: + page_items = [ + i for i in page_items if benchling_service.is_in_organization_scope(i) + ] + items.extend(page_items) if not next_token: return items From 29b7302eb9805fa5b7452e29719f618616490277 Mon Sep 17 00:00:00 2001 From: Matthew Ghent Date: Tue, 3 Nov 2026 07:18:54 -0500 Subject: [PATCH 03/14] fix registry listing and entity schema scoping - list registries from /api/v3/registry/items, without the archived filter that endpoint rejects - entity schema records carry an organization but no registry, so is_in_registry_scope falls back to the registry's organization - check for ambiguous registries only when use_api is set - check registry_id against the registry list on single-registry tenants - tests use record shapes that match the v3 API, and check the registry list request --- liminal/connection/benchling_service.py | 46 +++++++++++-------------- liminal/tests/test_registry_scoping.py | 39 ++++++++++++++++----- liminal/utils.py | 8 +++-- 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index 47a3723..f8f82e0 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -108,7 +108,7 @@ def __init__( raise ValueError( "use_db is True but warehouse_connection_string not provided in BenchlingConnection." ) - if self.has_ambiguous_registries(): + if use_api and self.has_ambiguous_registries(): raise ValueError( f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " "Liminal does not allow ambiguous registries. Set registry_id on the BenchlingConnection to choose one." @@ -136,41 +136,37 @@ def _registries(self) -> list[RegistryRef]: return [ RegistryRef(id=r["id"], owner_id=r["owner"]["id"]) - for r in list_all_items_v3(self, "/api/v3/registry") + for r in list_all_items_v3( + self, "/api/v3/registry/items", include_archived=None + ) ] @property def _registry_ref(self) -> RegistryRef: - if len(self._registries) != 1: - if self.connection.registry_id is None: + if self.connection.registry_id is not None: + registry_ref = next( + (r for r in self._registries if r.id == self.connection.registry_id), + None, + ) + if registry_ref is None: raise ValueError( - f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " - "Set registry_id on the BenchlingConnection to choose one." + f"Tenant {self.benchling_tenant}: registry {self.connection.registry_id} not found." ) - else: - registry_ref = next( - ( - r - for r in self._registries - if r.id == self.connection.registry_id - ), - None, - ) - if registry_ref is None: - raise ValueError( - f"Tenant {self.benchling_tenant}: registry {self.connection.registry_id} not found." - ) - return registry_ref - else: - return self._registries[0] + return registry_ref + if len(self._registries) != 1: + raise ValueError( + f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " + "Set registry_id on the BenchlingConnection to choose one." + ) + return self._registries[0] def is_in_registry_scope(self, record: dict[str, Any]) -> bool: - """Whether a v3 API record belongs to this connection's registry or its organization. - Always True when the connection is scoped to a registry.""" + """Whether a v3 API record belongs to this connection's registry. + Records without a registry, such as entity schemas, are matched on the registry's organization.""" registry_id = (record.get("registry") or {}).get("id") if registry_id: return registry_id == self.registry_id - return False + return self.is_in_organization_scope(record) def is_in_organization_scope(self, record: dict[str, Any]) -> bool: """Whether a v3 API record belongs to this connection's organization.""" diff --git a/liminal/tests/test_registry_scoping.py b/liminal/tests/test_registry_scoping.py index ca298db..4fab940 100644 --- a/liminal/tests/test_registry_scoping.py +++ b/liminal/tests/test_registry_scoping.py @@ -37,7 +37,7 @@ def _v3_api(handler: Callable[[str], list[dict[str, Any]]]) -> Iterator[None]: """Serves the v3 registry list from _TENANT_REGISTRIES and every other v3 list from handler.""" def fake_get(_service: Any, url: str) -> Any: - if url.startswith("/api/v3/registry?"): + if url.startswith("/api/v3/registry/items"): return _registry_page() return SimpleNamespace(parsed={"items": handler(url)}) @@ -54,7 +54,9 @@ def _fake_registry_api(monkeypatch: pytest.MonkeyPatch) -> None: ) -def _service(registries: list[Any], registry_id: str | None = None) -> BenchlingService: +def _service( + registries: list[Any], registry_id: str | None = None, use_api: bool = True +) -> BenchlingService: _TENANT_REGISTRIES[:] = registries connection = BenchlingConnection( tenant_name="test-tenant", @@ -62,14 +64,14 @@ def _service(registries: list[Any], registry_id: str | None = None) -> Benchling api_client_secret="test-secret", registry_id=registry_id, ) - return BenchlingService(connection, use_api=False) + return BenchlingService(connection, use_api=use_api) def _schema(system_name: str, registry: Any) -> dict[str, Any]: + """Entity schema records carry the owning organization but no registry.""" return { "id": f"ts_{system_name}_{registry.id}", "systemName": system_name, - "registry": {"id": registry.id}, "organization": {"id": registry.owner.id}, } @@ -108,6 +110,16 @@ def test_registry_id_selects_registry_and_owner(self) -> None: assert service.registry_id == "src_other" assert service.organization_id == "ent_other" + def test_unknown_registry_id_on_single_registry_raises(self) -> None: + service = _service([MAIN], registry_id="src_missing") + with pytest.raises(ValueError, match="registry src_missing not found"): + service.registry_id + + def test_without_api_registries_are_not_listed_on_init(self) -> None: + with patch("liminal.utils.rate_limited_get_v3") as get: + _service([MAIN, OTHER], use_api=False) + get.assert_not_called() + def test_unknown_registry_id_raises_on_access_not_init(self) -> None: service = _service([MAIN, OTHER], registry_id="src_missing") with pytest.raises(ValueError, match="registry src_missing not found"): @@ -121,14 +133,19 @@ def test_registries_are_listed_once(self) -> None: service.registry_id service.organization_id get.assert_called_once() + # The registry list rejects an archived filter. + url = get.call_args.args[1] + assert url.startswith("/api/v3/registry/items") + assert "archived" not in url class TestScopeFilter: - def test_registry_scope_matches_registry_only(self) -> None: + def test_registry_scope_matches_registry_then_organization(self) -> None: service = _service([MAIN, OTHER], registry_id="src_main") + assert service.is_in_registry_scope(_dropdown("Species", MAIN)) + assert not service.is_in_registry_scope(_dropdown("Species", OTHER)) assert service.is_in_registry_scope(_schema("plasmid", MAIN)) assert not service.is_in_registry_scope(_schema("plasmid", OTHER)) - assert not service.is_in_registry_scope({"organization": {"id": "ent_main"}}) assert not service.is_in_registry_scope({"id": "no_owner"}) def test_single_registry_without_name_scopes_to_it(self) -> None: @@ -169,12 +186,16 @@ def _list_results_schemas(service: BenchlingService) -> list[dict[str, Any]]: # Each lister returns records from both MAIN and OTHER. `owner_of` reads the scoping value off a -# record and `scope_of` gives the value a registry should produce: the registry id for entity -# schemas and dropdowns, the owning organization id for results schemas. +# record and `scope_of` gives the value a registry should produce: the registry id for dropdowns, +# the owning organization id for entity schemas and results schemas. _LISTERS = pytest.mark.parametrize( "lister, owner_of, scope_of", [ - (_list_entity_schemas, lambda r: r["registry"]["id"], lambda reg: reg.id), + ( + _list_entity_schemas, + lambda r: r["organization"]["id"], + lambda reg: reg.owner.id, + ), (_list_dropdowns, lambda r: r["registry"]["id"], lambda reg: reg.id), ( _list_results_schemas, diff --git a/liminal/utils.py b/liminal/utils.py index 77e60f0..9d06f2d 100644 --- a/liminal/utils.py +++ b/liminal/utils.py @@ -179,7 +179,7 @@ def rate_limited_get_v3(benchling_service: BenchlingService, url: str) -> Any: def list_all_items_v3( benchling_service: BenchlingService, url: str, - include_archived: bool = False, + include_archived: bool | None = False, page_size: int | None = MAX_PAGE_SIZE, registry_scoped: bool = False, organization_scoped: bool = False, @@ -190,7 +190,11 @@ def list_all_items_v3( drops items outside the organization that owns the connection's registry. Only use them on endpoints whose items carry a registry or an organization respectively, and never while resolving the registry itself.""" - query_params = [f"archived.anyOf={'true,false' if include_archived else 'false'}"] + query_params = ( + [] + if include_archived is None + else [f"archived.anyOf={'true,false' if include_archived else 'false'}"] + ) if page_size: query_params.append(f"pageSize={page_size}") base_url = f"{url}?{'&'.join(query_params)}" From 193df74e119826a82e168a16221fbf539a04ef69 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Tue, 3 Nov 2026 16:31:11 -0500 Subject: [PATCH 04/14] simplify logic for is_in_registry_scope --- liminal/connection/benchling_service.py | 7 ++++--- liminal/entity_schemas/api.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index f8f82e0..f828782 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -161,12 +161,13 @@ def _registry_ref(self) -> RegistryRef: return self._registries[0] def is_in_registry_scope(self, record: dict[str, Any]) -> bool: - """Whether a v3 API record belongs to this connection's registry. - Records without a registry, such as entity schemas, are matched on the registry's organization.""" + """Whether a v3 API record belongs to this connection's registry or its organization. + Always True when the connection is scoped to a registry. + """ registry_id = (record.get("registry") or {}).get("id") if registry_id: return registry_id == self.registry_id - return self.is_in_organization_scope(record) + return False def is_in_organization_scope(self, record: dict[str, Any]) -> bool: """Whether a v3 API record belongs to this connection's organization.""" diff --git a/liminal/entity_schemas/api.py b/liminal/entity_schemas/api.py index ce11cab..1cbade8 100644 --- a/liminal/entity_schemas/api.py +++ b/liminal/entity_schemas/api.py @@ -75,7 +75,7 @@ def _list_entity_schemas_for_endpoint( benchling_service, f"{_entity_schema_specific_api_path(endpoint)}/items", include_archived, - registry_scoped=True, + organization_scoped=True, ) From 858c7b76c102f745f38eccaf8594d524ba9c2a27 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Tue, 3 Nov 2026 16:37:45 -0500 Subject: [PATCH 05/14] update tests with change to is_in_registry_scope --- liminal/connection/benchling_service.py | 4 +--- liminal/tests/test_registry_scoping.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index f828782..eb903f4 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -161,9 +161,7 @@ def _registry_ref(self) -> RegistryRef: return self._registries[0] def is_in_registry_scope(self, record: dict[str, Any]) -> bool: - """Whether a v3 API record belongs to this connection's registry or its organization. - Always True when the connection is scoped to a registry. - """ + """Whether a v3 API record belongs to this connection's registry.""" registry_id = (record.get("registry") or {}).get("id") if registry_id: return registry_id == self.registry_id diff --git a/liminal/tests/test_registry_scoping.py b/liminal/tests/test_registry_scoping.py index 4fab940..00f096e 100644 --- a/liminal/tests/test_registry_scoping.py +++ b/liminal/tests/test_registry_scoping.py @@ -140,18 +140,23 @@ def test_registries_are_listed_once(self) -> None: class TestScopeFilter: - def test_registry_scope_matches_registry_then_organization(self) -> None: + def test_registry_scope_matches_registry(self) -> None: service = _service([MAIN, OTHER], registry_id="src_main") assert service.is_in_registry_scope(_dropdown("Species", MAIN)) assert not service.is_in_registry_scope(_dropdown("Species", OTHER)) - assert service.is_in_registry_scope(_schema("plasmid", MAIN)) - assert not service.is_in_registry_scope(_schema("plasmid", OTHER)) + assert not service.is_in_registry_scope(_schema("plasmid", MAIN)) assert not service.is_in_registry_scope({"id": "no_owner"}) + def test_organization_scope_matches_organization(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_main") + assert service.is_in_organization_scope(_schema("plasmid", MAIN)) + assert not service.is_in_organization_scope(_schema("plasmid", OTHER)) + assert not service.is_in_organization_scope({"id": "no_owner"}) + def test_single_registry_without_name_scopes_to_it(self) -> None: service = _service([MAIN]) - assert service.is_in_registry_scope(_schema("plasmid", MAIN)) - assert not service.is_in_registry_scope(_schema("plasmid", OTHER)) + assert service.is_in_organization_scope(_schema("plasmid", MAIN)) + assert not service.is_in_organization_scope(_schema("plasmid", OTHER)) def _results_schema(registry: Any) -> dict[str, Any]: From a46c0bb4d5a13df87db668144e4699826a849210 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:15:45 -0500 Subject: [PATCH 06/14] test --- liminal/results_schemas/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/liminal/results_schemas/utils.py b/liminal/results_schemas/utils.py index 9de8762..be75d84 100644 --- a/liminal/results_schemas/utils.py +++ b/liminal/results_schemas/utils.py @@ -66,7 +66,7 @@ def get_converted_results_schemas( benchling_service: BenchlingService, include_archived: bool = False ) -> list[tuple[ResultsSchemaProperties, dict[str, BaseFieldProperties]]]: """This functions gets all Results Schema schemas from Benchling and converts them to our internal representation of a schema and its fields. - It parses the Results Schema and creates ResultsSchemaProperties and a list of FieldProperties for each field in the schema. + It parses the Results Schema and creates ResultsSchemaProperties and a list of FieldProperties for each field in the schema """ results_schemas = ResultsSchemaModel.get_all(benchling_service) dropdown_id_to_name_map = get_benchling_dropdown_id_name_map(benchling_service) From b082899af792f4e26f92140dbf52098b16ef9500 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:16:08 -0500 Subject: [PATCH 07/14] undo test --- liminal/results_schemas/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/liminal/results_schemas/utils.py b/liminal/results_schemas/utils.py index be75d84..9de8762 100644 --- a/liminal/results_schemas/utils.py +++ b/liminal/results_schemas/utils.py @@ -66,7 +66,7 @@ def get_converted_results_schemas( benchling_service: BenchlingService, include_archived: bool = False ) -> list[tuple[ResultsSchemaProperties, dict[str, BaseFieldProperties]]]: """This functions gets all Results Schema schemas from Benchling and converts them to our internal representation of a schema and its fields. - It parses the Results Schema and creates ResultsSchemaProperties and a list of FieldProperties for each field in the schema + It parses the Results Schema and creates ResultsSchemaProperties and a list of FieldProperties for each field in the schema. """ results_schemas = ResultsSchemaModel.get_all(benchling_service) dropdown_id_to_name_map = get_benchling_dropdown_id_name_map(benchling_service) From 49f0fd9914e2c9845408fc1ddcfef0bff115bd57 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:26:42 -0500 Subject: [PATCH 08/14] add name to registry_ref, clearer error message --- liminal/connection/benchling_service.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index eb903f4..891548c 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -33,6 +33,7 @@ class RegistryRef(BaseModel): """The registry a connection writes to, and the organization that owns it.""" + name: str id: str owner_id: str @@ -111,7 +112,8 @@ def __init__( if use_api and self.has_ambiguous_registries(): raise ValueError( f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " - "Liminal does not allow ambiguous registries. Set registry_id on the BenchlingConnection to choose one." + "Liminal does not allow ambiguous registries. Set registry_id on the BenchlingConnection to choose one. " + f"Registries found: {self._registries}" ) @property @@ -135,7 +137,7 @@ def _registries(self) -> list[RegistryRef]: from liminal.utils import list_all_items_v3 return [ - RegistryRef(id=r["id"], owner_id=r["owner"]["id"]) + RegistryRef(id=r["id"], name=r["name"], owner_id=r["owner"]["id"]) for r in list_all_items_v3( self, "/api/v3/registry/items", include_archived=None ) From d35c41915054b835002049dae5fedba21744659f Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:31:20 -0500 Subject: [PATCH 09/14] update docs --- docs/getting-started/prerequisites.md | 2 +- docs/reference/benchling-connection.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/getting-started/prerequisites.md b/docs/getting-started/prerequisites.md index e3a5567..87c02ac 100644 --- a/docs/getting-started/prerequisites.md +++ b/docs/getting-started/prerequisites.md @@ -1,4 +1,4 @@ -1. **Benchling App with Admin Privileges**: Liminal builds on top of Benchling's LIMS system. You will need to have a Benchling App with admin privileges to your Benchling tenant(s). Liminal needs admin privileges in order to manipulate Benchling schemas through the Benchling API and SDK. Ensure the app is configured as an admin on all your entity schemas and dropdowns. [Follow instructions here](https://docs.benchling.com/docs/getting-started-benchling-apps#installing-your-app) to install a Benchling App with admin privileges. +1. **Benchling App with Admin Privileges**: Liminal builds on top of Benchling's LIMS system. You will need to have a Benchling App with admin privileges to your Benchling tenant(s). Liminal needs admin privileges in order to manipulate Benchling schemas through the Benchling API and SDK. Ensure the app is configured as an admin on your registry and on all entity schemas and dropdowns within the registry. [Follow instructions here](https://docs.benchling.com/docs/getting-started-benchling-apps#installing-your-app) to install a Benchling App with admin privileges. 2. **Python**: Liminal supports Python versions 3.10 through 3.13 for package users. diff --git a/docs/reference/benchling-connection.md b/docs/reference/benchling-connection.md index 5f50631..8a5fd10 100644 --- a/docs/reference/benchling-connection.md +++ b/docs/reference/benchling-connection.md @@ -48,7 +48,7 @@ connection = BenchlingConnection( The API id of the registry to scope this connection to, for example `src_abc123`. Only needed when the tenant has more than one registry. When set, Liminal reads and writes only the entity schemas and dropdowns in that registry, and the results schemas owned by the registry's organization. For a tenant with multiple registries and no `registry_id` set, Liminal raises an error in `BenchlingService`. !!! tip - For a tenant with several registries, give each registry its own Liminal directory (`env.py` and `versions/`), with one `BenchlingConnection` per tenant that sets that registry's `registry_id`. Each registry then keeps its own revision history in its own `liminal_remote` schema. + For a tenant with several registries, give each registry its own Liminal directory (`env.py` and `versions/`), with one `BenchlingConnection` per tenant that defines that registry's `registry_id`. Each registry then keeps its own revision history in its own `liminal_remote` schema. - **config_flags: TenantConfigFlags = TenantConfigFlags()** From d3405d044ea3cff0236c50307d8b3cb04036cfb6 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:37:24 -0500 Subject: [PATCH 10/14] fix tests --- liminal/tests/test_benchling_service.py | 6 +++++- liminal/tests/test_registry_scoping.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/liminal/tests/test_benchling_service.py b/liminal/tests/test_benchling_service.py index 4271271..6e43f79 100644 --- a/liminal/tests/test_benchling_service.py +++ b/liminal/tests/test_benchling_service.py @@ -25,7 +25,11 @@ def _connection() -> BenchlingConnection: def _single_registry_tenant() -> Iterator[None]: """BenchlingService lists the tenant's registries on init, so serve one instead of calling Benchling.""" page = SimpleNamespace( - parsed={"items": [{"id": "src_main", "owner": {"id": "ent_main"}}]} + parsed={ + "items": [ + {"id": "src_main", "name": "Main", "owner": {"id": "ent_main"}} + ] + } ) with patch("liminal.utils.rate_limited_get_v3", return_value=page): yield diff --git a/liminal/tests/test_registry_scoping.py b/liminal/tests/test_registry_scoping.py index 00f096e..23f0c47 100644 --- a/liminal/tests/test_registry_scoping.py +++ b/liminal/tests/test_registry_scoping.py @@ -26,7 +26,8 @@ def _registry_page() -> Any: return SimpleNamespace( parsed={ "items": [ - {"id": r.id, "owner": {"id": r.owner.id}} for r in _TENANT_REGISTRIES + {"id": r.id, "name": r.name, "owner": {"id": r.owner.id}} + for r in _TENANT_REGISTRIES ] } ) From ca0bfa0fceaeb8f0fb3800c1b63cac6c143ff353 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:38:49 -0500 Subject: [PATCH 11/14] lint --- liminal/tests/test_benchling_service.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/liminal/tests/test_benchling_service.py b/liminal/tests/test_benchling_service.py index 6e43f79..afef995 100644 --- a/liminal/tests/test_benchling_service.py +++ b/liminal/tests/test_benchling_service.py @@ -26,9 +26,7 @@ def _single_registry_tenant() -> Iterator[None]: """BenchlingService lists the tenant's registries on init, so serve one instead of calling Benchling.""" page = SimpleNamespace( parsed={ - "items": [ - {"id": "src_main", "name": "Main", "owner": {"id": "ent_main"}} - ] + "items": [{"id": "src_main", "name": "Main", "owner": {"id": "ent_main"}}] } ) with patch("liminal.utils.rate_limited_get_v3", return_value=page): From 67114f9b066ed08f6b997f625fe28cb75966707e Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 08:56:12 -0500 Subject: [PATCH 12/14] simplifed dropdown util functions to use the v3 api instead of the v2 sdk --- liminal/base/base_dropdown.py | 14 ++--- liminal/dropdowns/operations.py | 6 +-- liminal/dropdowns/utils.py | 54 ++++++------------- .../entity_schemas/entity_schema_models.py | 12 ++--- 4 files changed, 30 insertions(+), 56 deletions(-) diff --git a/liminal/base/base_dropdown.py b/liminal/base/base_dropdown.py index 6ffe625..1bf922b 100644 --- a/liminal/base/base_dropdown.py +++ b/liminal/base/base_dropdown.py @@ -2,7 +2,7 @@ from typing import Any from liminal.connection.benchling_service import BenchlingService -from liminal.dropdowns.utils import get_benchling_dropdown_summaries +from liminal.dropdowns.utils import get_benchling_dropdown_id_by_name class BaseDropdown(ABC): @@ -52,15 +52,9 @@ def get_id(cls, benchling_service: BenchlingService) -> str: str The id of the dropdown. """ - all_dropdowns = get_benchling_dropdown_summaries(benchling_service) - dropdowns_found_by_name = [ - d for d in all_dropdowns if d.name == cls.__benchling_name__ - ] - if len(dropdowns_found_by_name) == 0: - raise ValueError(f"No dropdowns found with name '{cls.__benchling_name__}'") - else: - dropdown = dropdowns_found_by_name[0] - return dropdown.id + return get_benchling_dropdown_id_by_name( + benchling_service, cls.__benchling_name__ + ) @classmethod def validate(cls, *values: str | None) -> None: diff --git a/liminal/dropdowns/operations.py b/liminal/dropdowns/operations.py index caa0d3b..c148243 100644 --- a/liminal/dropdowns/operations.py +++ b/liminal/dropdowns/operations.py @@ -23,7 +23,7 @@ from liminal.dropdowns.utils import ( dropdown_exists_in_benchling, get_benchling_dropdown_by_name, - get_benchling_dropdown_summary_by_name, + get_benchling_dropdown_id_by_name, ) LOGGER = logging.getLogger(__name__) @@ -118,7 +118,7 @@ def __init__(self, dropdown_name: str, new_dropdown_name: str) -> None: self.new_dropdown_name = new_dropdown_name def execute(self, benchling_service: BenchlingService) -> dict[str, Any]: - dropdown = get_benchling_dropdown_summary_by_name( + dropdown_id = get_benchling_dropdown_id_by_name( benchling_service, self.dropdown_name ) if dropdown_exists_in_benchling(benchling_service, self.new_dropdown_name): @@ -126,7 +126,7 @@ def execute(self, benchling_service: BenchlingService) -> dict[str, Any]: f"Dropdown {self.new_dropdown_name} already exists in Benchling." ) return update_dropdown_name( - benchling_service, dropdown.id, self.new_dropdown_name + benchling_service, dropdown_id, self.new_dropdown_name ) def describe_operation(self) -> str: diff --git a/liminal/dropdowns/utils.py b/liminal/dropdowns/utils.py index a8ce009..1454ad1 100644 --- a/liminal/dropdowns/utils.py +++ b/liminal/dropdowns/utils.py @@ -1,7 +1,7 @@ from typing import Any from benchling_sdk.models import ArchiveRecord -from benchling_sdk.models import Dropdown, DropdownOption, DropdownSummary +from benchling_sdk.models import Dropdown, DropdownOption from liminal.connection import BenchlingService from liminal.dropdowns.api import list_dropdowns, list_dropdowns_with_options @@ -11,7 +11,7 @@ def get_benchling_dropdown_id_name_map( benchling_service: BenchlingService, ) -> dict[str, str]: - return {d.id: d.name for d in get_benchling_dropdown_summaries(benchling_service)} + return {d["id"]: d["name"] for d in list_dropdowns(benchling_service)} def get_benchling_dropdown_name_to_classname_map( @@ -27,44 +27,26 @@ def get_benchling_dropdown_name_to_classname_map( } -def get_benchling_dropdown_summaries( - benchling_service: BenchlingService, -) -> list[DropdownSummary]: - summaries = [ - dropdown - for sublist in benchling_service.dropdowns.list() - for dropdown in sublist - ] - # The v2 dropdown summary has no registry, so scope it by the ids the v3 list returns. - scoped_ids = { - d["id"] for d in list_dropdowns(benchling_service, include_archived=True) - } - return [d for d in summaries if d.id in scoped_ids] - - -def get_benchling_dropdown_summary_by_name( +def get_benchling_dropdown_id_by_name( benchling_service: BenchlingService, name: str -) -> DropdownSummary: - matches = [ - d for d in get_benchling_dropdown_summaries(benchling_service) if d.name == name - ] - if matches: - return matches[0] - raise Exception(f"Dropdown {name} not found in given list.") +) -> str: + """Finds the id of a dropdown by name in the connection's registry. Names are unique within a registry.""" + dropdown = next( + (d for d in list_dropdowns(benchling_service) if d["name"] == name), None + ) + if dropdown is None: + raise ValueError( + f"Dropdown {name} not found in Benchling tenant {benchling_service.benchling_tenant}." + ) + return dropdown["id"] def get_benchling_dropdown_by_name( benchling_service: BenchlingService, name: str ) -> Dropdown: - matches = [ - d for d in get_benchling_dropdown_summaries(benchling_service) if d.name == name - ] - dropdown = matches[-1] if matches else None - if dropdown is None: - raise Exception( - f"Dropdown {name} not found in Benchling {benchling_service.benchling_tenant}." - ) - return benchling_service.dropdowns.get_by_id(dropdown.id) + return benchling_service.dropdowns.get_by_id( + get_benchling_dropdown_id_by_name(benchling_service, name) + ) def _convert_dropdown_from_v3_to_v2_sdk( @@ -114,9 +96,7 @@ def get_benchling_dropdowns_dict( def dropdown_exists_in_benchling( benchling_service: BenchlingService, name: str ) -> bool: - return any( - d.name == name for d in get_benchling_dropdown_summaries(benchling_service) - ) + return any(d["name"] == name for d in list_dropdowns(benchling_service)) def get_schemas_with_dropdown(dropdown_name: str) -> list[str]: diff --git a/liminal/entity_schemas/entity_schema_models.py b/liminal/entity_schemas/entity_schema_models.py index 17ac2d8..f877bd4 100644 --- a/liminal/entity_schemas/entity_schema_models.py +++ b/liminal/entity_schemas/entity_schema_models.py @@ -16,7 +16,7 @@ from liminal.base.properties.base_name_template import BaseNameTemplate from liminal.base.properties.base_schema_properties import BaseSchemaProperties from liminal.connection.benchling_service import BenchlingService -from liminal.dropdowns.utils import get_benchling_dropdown_summary_by_name +from liminal.dropdowns.utils import get_benchling_dropdown_id_by_name from liminal.entity_schemas.api import ( attach_entity_schema_field_definitions, list_entity_schemas, @@ -165,11 +165,11 @@ def update_from_props( raise ValueError( "Benchling SDK must be provided to update dropdown field." ) - dropdown_summary_id = get_benchling_dropdown_summary_by_name( + dropdown_id = get_benchling_dropdown_id_by_name( benchling_service, update_props.dropdown_link - ).id + ) self.linkDefinition = FieldLinkDefinitionModel( - id=dropdown_summary_id, + id=dropdown_id, typename=BenchlingLinkDefinitionType.DROPDOWN, ) self.displayPrecision = ( @@ -304,9 +304,9 @@ def from_benchling_props( ) dropdown_id = None if field_props.dropdown_link is not None: - dropdown_id = get_benchling_dropdown_summary_by_name( + dropdown_id = get_benchling_dropdown_id_by_name( benchling_service, field_props.dropdown_link - ).id + ) unit_id = None if field_props.unit_name is not None and field_type.supports_unit(): unit_id = get_unit_id_from_name(benchling_service, field_props.unit_name) From af192d1259460ddd1eb082569940745de513f061 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 09:02:22 -0500 Subject: [PATCH 13/14] clarify tenant_registry_name error messaging --- liminal/connection/benchling_service.py | 10 ++++++++++ liminal/dropdowns/utils.py | 2 +- liminal/entity_schemas/entity_schema_models.py | 2 +- liminal/migrate/components.py | 8 ++++---- liminal/results_schemas/models/results_schema_model.py | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index 891548c..50a798a 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -116,6 +116,11 @@ def __init__( f"Registries found: {self._registries}" ) + @property + def tenant_registry_name(self) -> str: + """The name of the tenant and registry this service is connected to. Raises if it can't be determined.""" + return f"[Tenant {self.benchling_tenant}: Registry {self.registry_name}]" + @property def session(self) -> Session: """Returns a session made by the sessionmaker""" @@ -126,6 +131,11 @@ def registry_id(self) -> str: """The id of the registry this connection writes to. Raises if it can't be determined.""" return self._registry_ref.id + @cached_property + def registry_name(self) -> str: + """The name of the registry this connection writes to. Raises if it can't be determined.""" + return self._registry_ref.name + @cached_property def organization_id(self) -> str: """The id of the organization that owns this connection's registry. Raises if it can't be determined.""" diff --git a/liminal/dropdowns/utils.py b/liminal/dropdowns/utils.py index 1454ad1..3fb94eb 100644 --- a/liminal/dropdowns/utils.py +++ b/liminal/dropdowns/utils.py @@ -36,7 +36,7 @@ def get_benchling_dropdown_id_by_name( ) if dropdown is None: raise ValueError( - f"Dropdown {name} not found in Benchling tenant {benchling_service.benchling_tenant}." + f"Dropdown {name} not found in Benchling {benchling_service.tenant_registry_name}." ) return dropdown["id"] diff --git a/liminal/entity_schemas/entity_schema_models.py b/liminal/entity_schemas/entity_schema_models.py index f877bd4..901fbf1 100644 --- a/liminal/entity_schemas/entity_schema_models.py +++ b/liminal/entity_schemas/entity_schema_models.py @@ -554,7 +554,7 @@ def get_one( ) if schema is None: raise ValueError( - f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." + f"Schema {wh_schema_name} not found in Benchling {benchling_service.tenant_registry_name}." ) schema_with_fields = attach_entity_schema_field_definitions( benchling_service, schema diff --git a/liminal/migrate/components.py b/liminal/migrate/components.py index 428dca6..53c373a 100644 --- a/liminal/migrate/components.py +++ b/liminal/migrate/components.py @@ -30,11 +30,11 @@ def get_full_migration_operations( ] if len(dropdown_operations) == 0: print( - f"[bold]Dropdowns are in sync for {benchling_service.benchling_tenant} tenant!" + f"[bold]Dropdowns are in sync for {benchling_service.tenant_registry_name}!" ) else: print( - f"[bold]Dropdowns are out of sync for {benchling_service.benchling_tenant} tenant." + f"[bold]Dropdowns are out of sync for {benchling_service.tenant_registry_name}." ) if not entity_schema_names or len(entity_schema_names) > 0: @@ -48,11 +48,11 @@ def get_full_migration_operations( ] if len(entity_schema_operations) == 0: print( - f"[bold]Entity schemas are in sync for {benchling_service.benchling_tenant} tenant!" + f"[bold]Entity schemas are in sync for {benchling_service.tenant_registry_name}!" ) else: print( - f"[bold]Entity schemas are out of sync for {benchling_service.benchling_tenant} tenant." + f"[bold]Entity schemas are out of sync for {benchling_service.tenant_registry_name}." ) all_operations: list[CompareOperation] = ( diff --git a/liminal/results_schemas/models/results_schema_model.py b/liminal/results_schemas/models/results_schema_model.py index 1f34924..9306cdc 100644 --- a/liminal/results_schemas/models/results_schema_model.py +++ b/liminal/results_schemas/models/results_schema_model.py @@ -147,7 +147,7 @@ def get_one( ) if schema is None: raise ValueError( - f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." + f"Schema {wh_schema_name} not found in Benchling {benchling_service.tenant_registry_name}." ) return cls.model_validate(schema) From 63dfb05d699f85f859fd69522a9f8efbe6f175e9 Mon Sep 17 00:00:00 2001 From: Nirmit Damania Date: Fri, 13 Nov 2026 09:13:27 -0500 Subject: [PATCH 14/14] switch from cached_property to property --- liminal/connection/benchling_service.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index 50a798a..a11161d 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -126,17 +126,17 @@ def session(self) -> Session: """Returns a session made by the sessionmaker""" return self.get_session() - @cached_property + @property def registry_id(self) -> str: """The id of the registry this connection writes to. Raises if it can't be determined.""" return self._registry_ref.id - @cached_property + @property def registry_name(self) -> str: """The name of the registry this connection writes to. Raises if it can't be determined.""" return self._registry_ref.name - @cached_property + @property def organization_id(self) -> str: """The id of the organization that owns this connection's registry. Raises if it can't be determined.""" return self._registry_ref.owner_id