Filter Python snapshots by shard membership

This commit is contained in:
Meta Vens 2026-06-14 05:27:09 +01:00
commit 7ad74cf10f
3 changed files with 48 additions and 0 deletions

View file

@ -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:

View file

@ -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()

View file

@ -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",