From 7ad74cf10f1c3e6d756e6465dccff3b4047ec180 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 14 Jun 2026 05:27:09 +0100 Subject: [PATCH] Filter Python snapshots by shard membership --- packages/cultcache-py/PARITY.md | 2 ++ packages/cultcache-py/src/cultmesh_py/node.py | 25 +++++++++++++++++++ packages/cultcache-py/tests/test_cultcache.py | 21 ++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/packages/cultcache-py/PARITY.md b/packages/cultcache-py/PARITY.md index 1987531e..1c3c35c3 100644 --- a/packages/cultcache-py/PARITY.md +++ b/packages/cultcache-py/PARITY.md @@ -99,6 +99,8 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. those responses back through the shared CultNet replication path. Typed builders own the local response shape and raw dict methods serialize that shape for compatibility with older callers and wire handlers. + Shard-scoped raw snapshots filter by shard-log membership when the database + has an authoritative log for the requested shard. Raw shard-scoped writes append a package-local mutation log that can be projected into schema-v0 shard-log responses for peer catch-up. - Python CultMesh committed simulation facts: diff --git a/packages/cultcache-py/src/cultmesh_py/node.py b/packages/cultcache-py/src/cultmesh_py/node.py index d7dce238..39109aae 100644 --- a/packages/cultcache-py/src/cultmesh_py/node.py +++ b/packages/cultcache-py/src/cultmesh_py/node.py @@ -499,6 +499,11 @@ class CultMeshDatabase: ) -> CultNetRawSnapshotResponse: requested_schema_ids = set(schema_ids or []) requested_record_keys = set(record_keys or []) + shard_record_keys = ( + self._live_shard_record_keys(shard_id) + if shard_id is not None and shard_id in self._shard_logs + else None + ) documents: list[CultNetRawDocumentRecord] = [] for envelope in self.cache.snapshot_envelopes(): schema_id = envelope.schema_id or envelope.type @@ -506,6 +511,8 @@ class CultMeshDatabase: continue if requested_record_keys and envelope.key not in requested_record_keys: continue + if shard_record_keys is not None and (schema_id, envelope.key) not in shard_record_keys: + continue documents.append(CultNetRawDocumentRecord( schema_id=schema_id, record_key=envelope.key, @@ -722,6 +729,24 @@ class CultMeshDatabase: return delete["shardEpoch"] return None + def _live_shard_record_keys(self, shard_id: str) -> set[tuple[str, str]]: + records: set[tuple[str, 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): + document = put["document"] + schema_id = document.get("schemaId") + record_key = document.get("recordKey") + if schema_id and record_key: + records.add((str(schema_id), str(record_key))) + delete = entry.get("delete") + if isinstance(delete, dict): + schema_id = delete.get("schemaId") + record_key = delete.get("recordKey") + if schema_id and record_key: + records.discard((str(schema_id), str(record_key))) + return records + def _document_for_schema(self, schema_id: str) -> DocumentDefinition[Any] | None: for document in self.documents: entry = document.catalog_entry() diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index f7e66b33..aea4bef3 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -1291,6 +1291,27 @@ class CultCacheTests(unittest.TestCase): self.assertEqual([(record.schema_id, record.record_key) for record in typed_applied], [("mesh.snapshot_note.v1", "note:1")]) self.assertEqual(typed_target.database.get_required(document, "note:1"), {"body": "include"}) + def test_cultmesh_database_filters_snapshot_by_logged_shard_membership(self) -> None: + document = define_database_entry_type( + "mesh.sharded_snapshot_note", + [("body", 0)], + schema_id="mesh.sharded_snapshot_note.v1", + ) + source = CultMesh.create_node(runtime_id="mesh-sharded-snapshot-source") + source.database.register_document(document) + source.database.put_raw_message(document, "note:notes", {"body": "include"}, shard_id="notes", shard_epoch=2) + source.database.put_raw_message(document, "note:other", {"body": "skip"}, shard_id="other", shard_epoch=1) + + response = source.database.build_snapshot_response( + schema_ids=["mesh.sharded_snapshot_note.v1"], + shard_id="notes", + shard_epoch=2, + ) + + self.assertEqual(response.shard_id, "notes") + self.assertEqual(response.shard_epoch, 2) + self.assertEqual([record.record_key for record in response.documents], ["note:notes"]) + def test_cultmesh_database_creates_shard_log_response_from_raw_mutations(self) -> None: document = define_database_entry_type( "mesh.log_note",