From 54543b5db00f3e05aac0a7f85d5d7544fc57c634 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 22:00:13 +0100 Subject: [PATCH] Resolve Python shard discovery filters by schema alias --- .../src/cultnet_py/shard_catalog.py | 29 +++++++++++++++++-- packages/cultcache-py/tests/test_cultcache.py | 4 ++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/cultcache-py/src/cultnet_py/shard_catalog.py b/packages/cultcache-py/src/cultnet_py/shard_catalog.py index 951ad5b7..4335f617 100644 --- a/packages/cultcache-py/src/cultnet_py/shard_catalog.py +++ b/packages/cultcache-py/src/cultnet_py/shard_catalog.py @@ -60,7 +60,7 @@ class CultNetShardDescriptor: return wire def serves(self, *, schema_id: str | None = None, record_key: str | None = None) -> bool: - if schema_id is not None and self.schema_ids and schema_id not in self.schema_ids: + if schema_id is not None and self.schema_ids and not _schema_ids_match_any(self.schema_ids, (schema_id,)): return False if record_key is not None and self.key_prefix and not record_key.startswith(self.key_prefix): return False @@ -122,13 +122,38 @@ def _shard_matches( requested_schema_ids: set[str], requested_record_keys: list[str], ) -> bool: - if requested_schema_ids and not any(schema_id in shard.schema_ids for schema_id in requested_schema_ids): + if requested_schema_ids and not _schema_ids_match_any(shard.schema_ids, requested_schema_ids): return False if requested_record_keys and not any(shard.serves(record_key=record_key) for record_key in requested_record_keys): return False return True +def _schema_ids_match_any( + advertised_schema_ids: tuple[str, ...], + requested_schema_ids: set[str] | tuple[str, ...], +) -> bool: + return any( + _schema_ids_match(advertised, requested) + for advertised in advertised_schema_ids + for requested in requested_schema_ids + ) + + +def _schema_ids_match(left: str, right: str) -> bool: + if left == right: + return True + return (_infer_schema_name(left) or left) == (_infer_schema_name(right) or right) + + +def _infer_schema_name(schema_id: str) -> str | None: + marker = schema_id.rfind(".v") + if marker <= 0 or marker + 2 >= len(schema_id): + return None + version = schema_id[marker + 2:] + return schema_id[:marker] if version.isdigit() else None + + def _string_tuple(value: Any) -> tuple[str, ...]: return tuple(str(item) for item in value or []) diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index 6ed8d3bc..9138268a 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -2339,7 +2339,7 @@ class CultCacheTests(unittest.TestCase): owner_runtime_id="python-runtime", epoch=2, is_primary=True, - schema_ids=("schema-note",), + schema_ids=("schema-note.v1",), key_prefix="note:", primary_endpoints=("cultnet://127.0.0.1:3075",), read_replica_endpoints=("cultnet://127.0.0.1:3075",), @@ -2354,8 +2354,10 @@ class CultCacheTests(unittest.TestCase): "primaryEndpoints": ["cultnet://127.0.0.1:3076"], }) + self.assertTrue(primary.serves(schema_id="schema-note.v1", record_key="note:1")) self.assertTrue(primary.serves(schema_id="schema-note", record_key="note:1")) self.assertFalse(primary.serves(schema_id="schema-note", record_key="fact:1")) + self.assertEqual(catalog.list(schema_ids=["schema-note.v1"]), [primary]) self.assertEqual(catalog.list(schema_ids=["schema-note"]), [primary]) self.assertEqual(catalog.list(record_keys=["note:1"]), [primary])