diff --git a/packages/cultcache-py/PARITY.md b/packages/cultcache-py/PARITY.md index 2255bcae..66ed2bda 100644 --- a/packages/cultcache-py/PARITY.md +++ b/packages/cultcache-py/PARITY.md @@ -18,8 +18,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. Provides typed `CultNetRawSnapshotResponse` and `CultNetRawDocumentRecord` helpers plus `CultNetRawClient.fetch_snapshot_response(...)` for inspecting peer snapshots before handing them to the existing replication helpers. - `CultMeshDatabase.apply_snapshot_response(...)` and sync paths accept the - typed response while preserving the existing replication owner. + `apply_raw_document_record(...)`, `apply_raw_snapshot(...)`, + `CultMeshDatabase.apply_snapshot_response(...)`, and sync paths accept typed + records/responses while preserving the existing replication owner. `CultMeshDatabase.build_snapshot_response(...)` projects local envelopes into the same typed response shape; the older `create_snapshot_response(...)` remains the wire-dict compatibility projection. @@ -39,8 +40,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. Provides typed `CultNetShardLogResponse` and `CultNetShardLogEntry` helpers for inspecting mutation-log entries, resync pressure, and sequence cursors, while cache mutation remains owned by the existing replication helpers. - `CultMeshDatabase` and `CultMeshGameSession` can consume typed shard-log - responses directly for database sync and prediction reconciliation. + `apply_shard_log_response(...)`, `CultMeshDatabase`, and + `CultMeshGameSession` can consume typed shard-log responses directly for + database sync and prediction reconciliation. `CultMeshDatabase.build_shard_log_response(...)` creates the typed response from the package-local mutation log; `create_shard_log_response(...)` remains the raw wire-dict compatibility projection. diff --git a/packages/cultcache-py/src/cultnet_py/replication.py b/packages/cultcache-py/src/cultnet_py/replication.py index e544a1b5..7290a434 100644 --- a/packages/cultcache-py/src/cultnet_py/replication.py +++ b/packages/cultcache-py/src/cultnet_py/replication.py @@ -6,6 +6,9 @@ from typing import Any, Iterable from cultcache_py import CultCache, CultCacheEnvelope from cultcache_py.documents import DocumentDefinition +from .shard_log import CultNetShardLogResponse +from .snapshot import CultNetRawDocumentRecord, CultNetRawSnapshotResponse + @dataclass(frozen=True) class CultNetAppliedRecord: @@ -28,11 +31,12 @@ def schema_document_map(documents: Iterable[DocumentDefinition[Any]]) -> dict[st def apply_raw_document_record( cache: CultCache, documents_by_schema_id: dict[str, DocumentDefinition[Any]], - record: dict[str, Any], + record: dict[str, Any] | CultNetRawDocumentRecord, ) -> CultNetAppliedRecord: - schema_id = str(record["schemaId"]) + wire = record.to_wire() if isinstance(record, CultNetRawDocumentRecord) else record + schema_id = str(wire["schemaId"]) document = documents_by_schema_id[schema_id] - envelope = raw_record_to_envelope(document, schema_id, record) + envelope = raw_record_to_envelope(document, schema_id, wire) value = cache.put_envelope(document, envelope) return CultNetAppliedRecord( schema_id=schema_id, @@ -60,8 +64,9 @@ def raw_record_to_envelope( def apply_raw_snapshot( cache: CultCache, documents: Iterable[DocumentDefinition[Any]], - response: dict[str, Any], + response: dict[str, Any] | CultNetRawSnapshotResponse, ) -> list[CultNetAppliedRecord]: + response = response.to_wire() if isinstance(response, CultNetRawSnapshotResponse) else response if response.get("schemaVersion") != "cultnet.snapshot_response_raw.v0": raise ValueError(f"Expected cultnet.snapshot_response_raw.v0, received {response.get('schemaVersion')!r}") documents_by_schema_id = schema_document_map(documents) @@ -98,8 +103,9 @@ def apply_raw_snapshot( def apply_shard_log_response( cache: CultCache, documents: Iterable[DocumentDefinition[Any]], - response: dict[str, Any], + response: dict[str, Any] | CultNetShardLogResponse, ) -> list[CultNetAppliedRecord]: + response = response.to_wire() if isinstance(response, CultNetShardLogResponse) else response if response.get("schemaVersion") != "cultnet.shard_log_response.v0": raise ValueError(f"Expected cultnet.shard_log_response.v0, received {response.get('schemaVersion')!r}") if response.get("resyncRequired") is True: diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index 3e7a0ea0..f76cd1db 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -34,6 +34,7 @@ from cultnet_py import ( CultNetShardLogEntry, CultNetShardLogResponse, CultNetSimulationConsensusOptions, + apply_raw_document_record, apply_raw_snapshot, apply_shard_log_response, database_subscribe, @@ -47,6 +48,7 @@ from cultnet_py import ( hello, parse_message, schema_catalog_request, + schema_document_map, shard_catalog_request, shard_log_request, simulation_observation, @@ -676,6 +678,12 @@ class CultCacheTests(unittest.TestCase): applied_snapshot = apply_raw_snapshot(cache, [document], snapshot) self.assertEqual(applied_snapshot[0].record_key, "item:1") self.assertEqual(cache.get_required(document, "item:1").value, 3) + typed_snapshot = CultNetRawSnapshotResponse.from_wire(snapshot) + direct_applied = apply_raw_document_record(cache, schema_document_map([document]), typed_snapshot.documents[0]) + self.assertEqual(direct_applied.schema_id, schema_id) + self.assertEqual(direct_applied.record_key, "item:1") + typed_snapshot_applied = apply_raw_snapshot(cache, [document], typed_snapshot) + self.assertEqual(typed_snapshot_applied[0].record_key, "item:1") shard_log = { "schemaVersion": "cultnet.shard_log_response.v0", @@ -736,6 +744,15 @@ class CultCacheTests(unittest.TestCase): self.assertEqual([change.change_kind for change in applied_log], ["updated", "removed"]) self.assertIsNone(cache.get(document, "item:1")) + cache.put(document, "item:1", Item("sword", "gear", 3)) + typed_shard_log = CultNetShardLogResponse.from_wire({ + **shard_log, + "entries": shard_log["entries"][1:], + }) + typed_applied_log = apply_shard_log_response(cache, [document], typed_shard_log) + self.assertEqual([change.change_kind for change in typed_applied_log], ["updated", "removed"]) + self.assertIsNone(cache.get(document, "item:1")) + def test_cultmesh_node_syncs_snapshot_and_shard_log_through_cultnet_client(self) -> None: import msgpack # type: ignore from cultnet_py import read_frame, write_frame