diff --git a/packages/cultcache-py/PARITY.md b/packages/cultcache-py/PARITY.md index 0c80f31c..1987531e 100644 --- a/packages/cultcache-py/PARITY.md +++ b/packages/cultcache-py/PARITY.md @@ -76,8 +76,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. for snapshot, put, and delete notifications on a connected client stream. Its schema catalog includes both registered document payload schemas and shared wire-message descriptors for the CultNet/CultMesh messages it handles. - Its shard catalog advertises the database-owned latest shard epoch so - discovery and shard-log catch-up agree on the same authority. + Its shard catalog advertises the database-owned shard ids, schema ids, and + latest shard epochs so discovery and shard-log catch-up agree on the same + authority. - Python CultMesh reactive catalogs: Verse and peer catalogs expose sorted local views, `get(...)`, response application, and `watch(...)` callbacks for local discovery updates. Verse diff --git a/packages/cultcache-py/src/cultmesh_py/node.py b/packages/cultcache-py/src/cultmesh_py/node.py index f78c7375..d7dce238 100644 --- a/packages/cultcache-py/src/cultmesh_py/node.py +++ b/packages/cultcache-py/src/cultmesh_py/node.py @@ -450,6 +450,24 @@ class CultMeshDatabase: def shard_epoch(self, shard_id: str) -> int: return self._latest_shard_epoch(shard_id) or 0 + def shard_ids(self) -> list[str]: + return sorted(self._shard_logs) + + def shard_schema_ids(self, shard_id: str) -> list[str]: + schema_ids: set[str] = set() + for entry in self._shard_logs.get(shard_id, []): + put = entry.get("put") + if isinstance(put, dict) and isinstance(put.get("document"), dict): + schema_id = put["document"].get("schemaId") + if schema_id: + schema_ids.add(str(schema_id)) + delete = entry.get("delete") + if isinstance(delete, dict): + schema_id = delete.get("schemaId") + if schema_id: + schema_ids.add(str(schema_id)) + return sorted(schema_ids) + def create_snapshot_response( self, *, diff --git a/packages/cultcache-py/src/cultmesh_py/server.py b/packages/cultcache-py/src/cultmesh_py/server.py index d6adc060..cf8fe31e 100644 --- a/packages/cultcache-py/src/cultmesh_py/server.py +++ b/packages/cultcache-py/src/cultmesh_py/server.py @@ -303,15 +303,19 @@ class CultMeshLocalServer: def _shard_catalog_response(self, request: dict[str, Any]) -> dict[str, Any]: requested_schema_ids = {str(value) for value in request.get("schemaIds") or []} - schema_ids = [document.catalog_entry().schema_id for document in self.node.documents] - if requested_schema_ids: - schema_ids = [schema_id for schema_id in schema_ids if schema_id in requested_schema_ids] + document_schema_ids = [document.catalog_entry().schema_id for document in self.node.documents] + shard_ids = self.node.database.shard_ids() or ["primary"] shards = [] - if schema_ids: + for shard_id in shard_ids: + schema_ids = self.node.database.shard_schema_ids(shard_id) or document_schema_ids + if requested_schema_ids: + schema_ids = [schema_id for schema_id in schema_ids if schema_id in requested_schema_ids] + if not schema_ids: + continue shards.append({ - "shardId": "primary", + "shardId": shard_id, "ownerRuntimeId": self.node.runtime_id, - "epoch": self.node.database.shard_epoch("primary"), + "epoch": self.node.database.shard_epoch(shard_id), "isPrimary": True, "schemaIds": schema_ids, "keyPrefix": "", diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index 8087a9d9..f7e66b33 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -1304,6 +1304,9 @@ class CultCacheTests(unittest.TestCase): source.database.put_raw_message(document, "note:2", {"body": "other"}, shard_id="other", shard_epoch=1) source.database.delete_raw_message(document, "note:1", shard_id="notes", shard_epoch=3) + self.assertEqual(source.database.shard_ids(), ["notes", "other"]) + self.assertEqual(source.database.shard_schema_ids("notes"), ["mesh.log_note.v1"]) + typed_response = source.database.build_shard_log_response( message_id="log-1", shard_id="notes", @@ -1942,7 +1945,7 @@ class CultCacheTests(unittest.TestCase): ) node = CultMesh.create_node(runtime_id="mesh-server") node.database.register_document(document) - node.database.put_raw_message(document, "note:1", {"body": "served"}, shard_id="primary", shard_epoch=1) + node.database.put_raw_message(document, "note:1", {"body": "served"}, shard_id="notes", shard_epoch=1) verses = CultMeshVerseCatalog() verses.upsert( CultMeshVerseDescriptor( @@ -1979,8 +1982,8 @@ class CultCacheTests(unittest.TestCase): shard_catalog = raw_client.fetch_shard_catalog(schema_ids=["mesh.server_note.v1"]) synced_shard_catalog = CultNetShardCatalog() synced_shards = raw_client.sync_shard_catalog(synced_shard_catalog, schema_ids=["mesh.server_note.v1"]) - shard_log = raw_client.fetch_shard_log(shard_id="primary", shard_epoch=1) - typed_shard_log = raw_client.fetch_shard_log_response(shard_id="primary", shard_epoch=1) + shard_log = raw_client.fetch_shard_log(shard_id="notes", shard_epoch=1) + typed_shard_log = raw_client.fetch_shard_log_response(shard_id="notes", shard_epoch=1) discovery_client = CultMesh.create_verse_discovery_client("127.0.0.1", server.port, timeout_seconds=2.0) fetched_verses = discovery_client.fetch_verses(transport_version="cultmesh.v0") fetched_peers = discovery_client.fetch_peers(verse_id="server-verse", roles=["read-replica"]) @@ -2024,9 +2027,10 @@ class CultCacheTests(unittest.TestCase): self.assertEqual(typed_snapshot_response.documents[0].schema_id, "mesh.server_note.v1") self.assertEqual(shard_catalog["shards"][0]["schemaIds"], ["mesh.server_note.v1"]) self.assertEqual(shard_catalog["shards"][0]["epoch"], 1) - self.assertEqual(synced_shards[0].shard_id, "primary") + self.assertEqual(shard_catalog["shards"][0]["shardId"], "notes") + self.assertEqual(synced_shards[0].shard_id, "notes") self.assertEqual(synced_shards[0].epoch, 1) - self.assertEqual(synced_shard_catalog.get("primary"), synced_shards[0]) + self.assertEqual(synced_shard_catalog.get("notes"), synced_shards[0]) self.assertEqual(shard_log["entries"][0]["changeKind"], "added") self.assertEqual(typed_shard_log.last_sequence, 1) self.assertEqual(typed_shard_log.entries[0].put["document"]["recordKey"], "note:1")