Add typed Python subscription snapshots

This commit is contained in:
Meta Vens 2026-06-14 04:40:30 +01:00
commit 8dc0a3ffe1
4 changed files with 22 additions and 7 deletions

View file

@ -47,8 +47,10 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
- Python CultNet database subscription changes:
Provides typed `CultNetDatabaseChange` parsing plus
`CultNetDatabaseSubscription.read_next_change(...)` / `iter_changes(...)` for
live raw database subscriptions. Snapshot frames remain raw and feed the
existing snapshot replication helpers.
live raw database subscriptions. Initial snapshot frames can be read as typed
`CultNetRawSnapshotResponse` values through `read_next_snapshot_response(...)`
/ `iter_snapshot_responses(...)`, while raw message reads remain available
for callers that own their own dispatch loop.
- Python CultMesh discovery client:
Fetches Verse catalogs and peer exchange responses from the live Python
endpoint over CultNet frames.

View file

@ -179,7 +179,7 @@ shard_request = shard_catalog_request(message_id="shards", schema_ids=["cultnet.
shard_log = client.fetch_shard_log_response(shard_id="interop", shard_epoch=1, after_sequence=0)
# log_changes = apply_shard_log_response(cache, [note_doc], shard_log.to_wire())
with client.subscribe_database(subscription_id="ui", schema_ids=["cultnet.interop-note"]) as live:
initial_snapshot = live.read_next()
initial_snapshot = live.read_next_snapshot_response()
claim_hash = compute_simulation_claim_hash("frame:42", "subject:player-1", "hit")
observation = simulation_observation(
message_id="obs-1",

View file

@ -294,12 +294,22 @@ class CultNetDatabaseSubscription:
message = self.read_next()
return CultNetDatabaseChange.from_wire(message)
def read_next_snapshot_response(self) -> CultNetRawSnapshotResponse:
message = self.read_next()
return CultNetRawSnapshotResponse.from_wire(message)
def iter_messages(self, *, max_messages: int | None = None) -> Any:
received = 0
while max_messages is None or received < max_messages:
yield self.read_next()
received += 1
def iter_snapshot_responses(self, *, max_messages: int | None = None) -> Any:
received = 0
while max_messages is None or received < max_messages:
yield self.read_next_snapshot_response()
received += 1
def iter_changes(self, *, max_messages: int | None = None) -> Any:
received = 0
while max_messages is None or received < max_messages:

View file

@ -624,13 +624,15 @@ class CultCacheTests(unittest.TestCase):
payload=document.encode_payload(Item("orb", "gear", 8)),
)
with client.subscribe_database(subscription_id="sub-1", schema_ids=[schema_id]) as subscription:
snapshot = subscription.read_next()
snapshot = subscription.read_next_snapshot_response()
subscription.send(put)
change = subscription.read_next_change()
thread.join(2.0)
self.assertFalse(server_error)
self.assertEqual(snapshot["schemaVersion"], "cultnet.snapshot_response_raw.v0")
self.assertIsInstance(snapshot, CultNetRawSnapshotResponse)
self.assertEqual(snapshot.message_id, "cultnet-python-subscribe")
self.assertEqual(snapshot.to_wire()["schemaVersion"], "cultnet.snapshot_response_raw.v0")
self.assertEqual(change.change_kind, "added")
self.assertEqual(change.record_key, "item:sub")
self.assertEqual(received_versions, [
@ -1920,7 +1922,7 @@ class CultCacheTests(unittest.TestCase):
fetched_verses = discovery_client.fetch_verses(transport_version="cultmesh.v0")
fetched_peers = discovery_client.fetch_peers(verse_id="server-verse", roles=["read-replica"])
with raw_client.subscribe_database(subscription_id="sub-server", schema_ids=["mesh.server_note.v1"]) as subscription:
subscription_snapshot = subscription.read_next()
subscription_snapshot = subscription.read_next_snapshot_response()
subscription.send(document_put_raw(
message_id="server-put",
key="note:2",
@ -1965,7 +1967,8 @@ class CultCacheTests(unittest.TestCase):
self.assertEqual(typed_shard_log.entries[0].put["document"]["recordKey"], "note:1")
self.assertEqual(fetched_verses[0].verse_id, "server-verse")
self.assertEqual(fetched_peers[0].peer_id, "mesh-server")
self.assertEqual(subscription_snapshot["schemaVersion"], "cultnet.snapshot_response_raw.v0")
self.assertIsInstance(subscription_snapshot, CultNetRawSnapshotResponse)
self.assertEqual(subscription_snapshot.to_wire()["schemaVersion"], "cultnet.snapshot_response_raw.v0")
self.assertEqual(subscription_change.change_kind, "added")
self.assertEqual(subscription_change.record_key, "note:2")
self.assertEqual(subscription_change.document["recordKey"], "note:2")