Add typed Python CultMesh response builders
This commit is contained in:
parent
27499e766c
commit
6b42878931
4 changed files with 145 additions and 42 deletions
|
|
@ -20,6 +20,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
|
|||
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.
|
||||
`CultMeshDatabase.build_snapshot_response(...)` projects local envelopes into
|
||||
the same typed response shape; the older `create_snapshot_response(...)`
|
||||
remains the wire-dict compatibility projection.
|
||||
- Python CultNet schema catalog:
|
||||
Exposes shared wire-message schema descriptors from the package surface so
|
||||
both the interop peer and CultMesh local server describe the CultNet/CultMesh
|
||||
|
|
@ -38,6 +41,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
|
|||
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.
|
||||
`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.
|
||||
- Python CultNet database subscription changes:
|
||||
Provides typed `CultNetDatabaseChange` parsing plus
|
||||
`CultNetDatabaseSubscription.read_next_change(...)` / `iter_changes(...)` for
|
||||
|
|
@ -74,7 +80,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
|
|||
Change notifications carry previous values when the database can observe the
|
||||
old record before local or replicated application.
|
||||
It can project local envelopes into schema-v0 raw snapshot responses and apply
|
||||
those responses back through the shared CultNet replication path.
|
||||
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.
|
||||
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:
|
||||
|
|
|
|||
|
|
@ -169,15 +169,15 @@ message = parse_message(decode_frame(encode_frame(payload)))
|
|||
|
||||
client = CultNetRawClient("127.0.0.1", 3075)
|
||||
catalog = client.fetch_schema_catalog(kinds=["wire_message"])
|
||||
snapshot = client.fetch_snapshot(schema_ids=["cultnet.interop-note"])
|
||||
snapshot = client.fetch_snapshot_response(schema_ids=["cultnet.interop-note"])
|
||||
shard_catalog = client.fetch_shard_catalog(schema_ids=["cultnet.interop-note"])
|
||||
# With a local CultCache and the matching registered document definitions:
|
||||
# applied = apply_raw_snapshot(cache, [note_doc], snapshot)
|
||||
# applied = apply_raw_snapshot(cache, [note_doc], snapshot.to_wire())
|
||||
|
||||
subscription = database_subscribe(subscription_id="ui", schema_ids=["cultnet.interop-note"])
|
||||
shard_request = shard_catalog_request(message_id="shards", schema_ids=["cultnet.interop-note"])
|
||||
shard_log = client.fetch_shard_log(shard_id="interop", shard_epoch=1, after_sequence=0)
|
||||
# log_changes = apply_shard_log_response(cache, [note_doc], shard_log)
|
||||
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()
|
||||
claim_hash = compute_simulation_claim_hash("frame:42", "subject:player-1", "hit")
|
||||
|
|
@ -241,8 +241,11 @@ put_message = node.database.put_raw_message(
|
|||
shard_id="interop",
|
||||
shard_epoch=1,
|
||||
)
|
||||
snapshot_response = node.database.create_snapshot_response(schema_ids=[note_doc.catalog_entry().schema_id])
|
||||
snapshot_response = node.database.build_snapshot_response(schema_ids=[note_doc.catalog_entry().schema_id])
|
||||
snapshot_wire = node.database.create_snapshot_response(schema_ids=[note_doc.catalog_entry().schema_id])
|
||||
delete_message = node.database.delete_raw_message(note_doc, "note:2", shard_id="interop", shard_epoch=1)
|
||||
shard_log_response = node.database.build_shard_log_response(shard_id="interop", shard_epoch=1)
|
||||
shard_log_wire = node.database.create_shard_log_response(shard_id="interop", shard_epoch=1)
|
||||
|
||||
peers = CultMesh.create_peer_catalog()
|
||||
response = peers.create_response(peer_exchange_request("pex-1", verse_id="local"))
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ from cultnet_py import (
|
|||
CultNetAppliedRecord,
|
||||
CultNetMessage,
|
||||
CultNetRawClient,
|
||||
CultNetRawDocumentRecord,
|
||||
CultNetRawSnapshotResponse,
|
||||
CultNetShardLogEntry,
|
||||
CultNetShardLogResponse,
|
||||
apply_raw_snapshot as apply_cultnet_raw_snapshot,
|
||||
apply_shard_log_response as apply_cultnet_shard_log_response,
|
||||
|
|
@ -117,7 +119,26 @@ class CultMeshNode:
|
|||
shard_epoch: int | None = None,
|
||||
shard_log_sequence: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
return self.database.create_snapshot_response(
|
||||
return self.build_snapshot_response(
|
||||
message_id=message_id,
|
||||
schema_ids=schema_ids,
|
||||
record_keys=record_keys,
|
||||
shard_id=shard_id,
|
||||
shard_epoch=shard_epoch,
|
||||
shard_log_sequence=shard_log_sequence,
|
||||
).to_wire()
|
||||
|
||||
def build_snapshot_response(
|
||||
self,
|
||||
*,
|
||||
message_id: str = "",
|
||||
schema_ids: list[str] | None = None,
|
||||
record_keys: list[str] | None = None,
|
||||
shard_id: str | None = None,
|
||||
shard_epoch: int | None = None,
|
||||
shard_log_sequence: int | None = None,
|
||||
) -> CultNetRawSnapshotResponse:
|
||||
return self.database.build_snapshot_response(
|
||||
message_id=message_id,
|
||||
schema_ids=schema_ids,
|
||||
record_keys=record_keys,
|
||||
|
|
@ -172,7 +193,24 @@ class CultMeshNode:
|
|||
after_sequence: int = 0,
|
||||
limit: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
return self.database.create_shard_log_response(
|
||||
return self.build_shard_log_response(
|
||||
shard_id=shard_id,
|
||||
message_id=message_id,
|
||||
shard_epoch=shard_epoch,
|
||||
after_sequence=after_sequence,
|
||||
limit=limit,
|
||||
).to_wire()
|
||||
|
||||
def build_shard_log_response(
|
||||
self,
|
||||
*,
|
||||
shard_id: str,
|
||||
message_id: str = "",
|
||||
shard_epoch: int | None = None,
|
||||
after_sequence: int = 0,
|
||||
limit: int | None = None,
|
||||
) -> CultNetShardLogResponse:
|
||||
return self.database.build_shard_log_response(
|
||||
shard_id=shard_id,
|
||||
message_id=message_id,
|
||||
shard_epoch=shard_epoch,
|
||||
|
|
@ -419,35 +457,49 @@ class CultMeshDatabase:
|
|||
shard_epoch: int | None = None,
|
||||
shard_log_sequence: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
return self.build_snapshot_response(
|
||||
message_id=message_id,
|
||||
schema_ids=schema_ids,
|
||||
record_keys=record_keys,
|
||||
shard_id=shard_id,
|
||||
shard_epoch=shard_epoch,
|
||||
shard_log_sequence=shard_log_sequence,
|
||||
).to_wire()
|
||||
|
||||
def build_snapshot_response(
|
||||
self,
|
||||
*,
|
||||
message_id: str = "",
|
||||
schema_ids: list[str] | None = None,
|
||||
record_keys: list[str] | None = None,
|
||||
shard_id: str | None = None,
|
||||
shard_epoch: int | None = None,
|
||||
shard_log_sequence: int | None = None,
|
||||
) -> CultNetRawSnapshotResponse:
|
||||
requested_schema_ids = set(schema_ids or [])
|
||||
requested_record_keys = set(record_keys or [])
|
||||
documents = []
|
||||
documents: list[CultNetRawDocumentRecord] = []
|
||||
for envelope in self.cache.snapshot_envelopes():
|
||||
schema_id = envelope.schema_id or envelope.type
|
||||
if requested_schema_ids and schema_id not in requested_schema_ids:
|
||||
continue
|
||||
if requested_record_keys and envelope.key not in requested_record_keys:
|
||||
continue
|
||||
documents.append({
|
||||
"schemaId": schema_id,
|
||||
"recordKey": envelope.key,
|
||||
"storedAt": envelope.stored_at,
|
||||
"payloadEncoding": "messagepack",
|
||||
"payload": envelope.payload,
|
||||
})
|
||||
documents.append(CultNetRawDocumentRecord(
|
||||
schema_id=schema_id,
|
||||
record_key=envelope.key,
|
||||
stored_at=envelope.stored_at,
|
||||
payload_encoding="messagepack",
|
||||
payload=envelope.payload,
|
||||
))
|
||||
|
||||
response: dict[str, Any] = {
|
||||
"schemaVersion": "cultnet.snapshot_response_raw.v0",
|
||||
"messageId": message_id,
|
||||
"documents": documents,
|
||||
}
|
||||
if shard_id is not None:
|
||||
response["shardId"] = shard_id
|
||||
if shard_epoch is not None:
|
||||
response["shardEpoch"] = shard_epoch
|
||||
if shard_log_sequence is not None:
|
||||
response["shardLogSequence"] = shard_log_sequence
|
||||
return response
|
||||
return CultNetRawSnapshotResponse(
|
||||
message_id=message_id,
|
||||
documents=tuple(documents),
|
||||
shard_id=shard_id,
|
||||
shard_epoch=shard_epoch,
|
||||
shard_log_sequence=shard_log_sequence,
|
||||
)
|
||||
|
||||
def pull(self) -> None:
|
||||
self.cache.pull_all_backing_stores()
|
||||
|
|
@ -495,12 +547,29 @@ class CultMeshDatabase:
|
|||
after_sequence: int = 0,
|
||||
limit: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
return self.build_shard_log_response(
|
||||
shard_id=shard_id,
|
||||
message_id=message_id,
|
||||
shard_epoch=shard_epoch,
|
||||
after_sequence=after_sequence,
|
||||
limit=limit,
|
||||
).to_wire()
|
||||
|
||||
def build_shard_log_response(
|
||||
self,
|
||||
*,
|
||||
shard_id: str,
|
||||
message_id: str = "",
|
||||
shard_epoch: int | None = None,
|
||||
after_sequence: int = 0,
|
||||
limit: int | None = None,
|
||||
) -> CultNetShardLogResponse:
|
||||
if not shard_id:
|
||||
raise ValueError("shard_id must be non-empty")
|
||||
if after_sequence < 0:
|
||||
raise ValueError("after_sequence must be non-negative")
|
||||
entries = [
|
||||
dict(entry)
|
||||
CultNetShardLogEntry.from_wire(entry)
|
||||
for entry in self._shard_logs.get(shard_id, [])
|
||||
if int(entry.get("sequence") or 0) > after_sequence
|
||||
]
|
||||
|
|
@ -508,18 +577,16 @@ class CultMeshDatabase:
|
|||
if limit < 0:
|
||||
raise ValueError("limit must be non-negative")
|
||||
entries = entries[:limit]
|
||||
response: dict[str, Any] = {
|
||||
"schemaVersion": "cultnet.shard_log_response.v0",
|
||||
"messageId": message_id,
|
||||
"shardId": shard_id,
|
||||
"entries": entries,
|
||||
"resyncRequired": False,
|
||||
}
|
||||
if shard_epoch is not None:
|
||||
response["shardEpoch"] = shard_epoch
|
||||
response_shard_epoch = shard_epoch
|
||||
else:
|
||||
response["shardEpoch"] = self._latest_shard_epoch(shard_id) or 0
|
||||
return response
|
||||
response_shard_epoch = self._latest_shard_epoch(shard_id) or 0
|
||||
return CultNetShardLogResponse(
|
||||
message_id=message_id,
|
||||
shard_id=shard_id,
|
||||
shard_epoch=response_shard_epoch,
|
||||
entries=tuple(entries),
|
||||
)
|
||||
|
||||
def apply_snapshot_response(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -1194,6 +1194,14 @@ class CultCacheTests(unittest.TestCase):
|
|||
source.database.put(document, "note:1", {"body": "include"})
|
||||
source.database.put(document, "note:2", {"body": "skip"})
|
||||
|
||||
typed_response = source.database.build_snapshot_response(
|
||||
message_id="snapshot-1",
|
||||
schema_ids=["mesh.snapshot_note.v1"],
|
||||
record_keys=["note:1"],
|
||||
shard_id="notes",
|
||||
shard_epoch=4,
|
||||
shard_log_sequence=7,
|
||||
)
|
||||
response = source.database.create_snapshot_response(
|
||||
message_id="snapshot-1",
|
||||
schema_ids=["mesh.snapshot_note.v1"],
|
||||
|
|
@ -1203,6 +1211,11 @@ class CultCacheTests(unittest.TestCase):
|
|||
shard_log_sequence=7,
|
||||
)
|
||||
|
||||
self.assertIsInstance(typed_response, CultNetRawSnapshotResponse)
|
||||
self.assertEqual(typed_response.message_id, "snapshot-1")
|
||||
self.assertEqual(typed_response.documents[0].record_key, "note:1")
|
||||
self.assertEqual(typed_response.to_wire(), response)
|
||||
self.assertEqual(source.build_snapshot_response(schema_ids=["mesh.snapshot_note.v1"]).documents[0].record_key, "note:1")
|
||||
self.assertEqual(response["schemaVersion"], "cultnet.snapshot_response_raw.v0")
|
||||
self.assertEqual(response["messageId"], "snapshot-1")
|
||||
self.assertEqual(response["shardId"], "notes")
|
||||
|
|
@ -1221,7 +1234,7 @@ class CultCacheTests(unittest.TestCase):
|
|||
|
||||
typed_target = CultMesh.create_node(runtime_id="mesh-snapshot-typed-target")
|
||||
typed_target.database.register_document(document)
|
||||
typed_applied = typed_target.database.apply_snapshot_response(CultNetRawSnapshotResponse.from_wire(response))
|
||||
typed_applied = typed_target.database.apply_snapshot_response(typed_response)
|
||||
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"})
|
||||
|
||||
|
|
@ -1238,6 +1251,13 @@ 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)
|
||||
|
||||
typed_response = source.database.build_shard_log_response(
|
||||
message_id="log-1",
|
||||
shard_id="notes",
|
||||
shard_epoch=3,
|
||||
after_sequence=1,
|
||||
limit=2,
|
||||
)
|
||||
response = source.database.create_shard_log_response(
|
||||
message_id="log-1",
|
||||
shard_id="notes",
|
||||
|
|
@ -1246,6 +1266,11 @@ class CultCacheTests(unittest.TestCase):
|
|||
limit=2,
|
||||
)
|
||||
|
||||
self.assertIsInstance(typed_response, CultNetShardLogResponse)
|
||||
self.assertEqual(typed_response.message_id, "log-1")
|
||||
self.assertEqual([entry.sequence for entry in typed_response.entries], [2, 3])
|
||||
self.assertEqual(typed_response.to_wire(), response)
|
||||
self.assertEqual([entry.sequence for entry in source.build_shard_log_response(shard_id="notes").entries], [1, 2, 3])
|
||||
self.assertEqual(response["schemaVersion"], "cultnet.shard_log_response.v0")
|
||||
self.assertEqual(response["messageId"], "log-1")
|
||||
self.assertEqual(response["shardId"], "notes")
|
||||
|
|
@ -1274,7 +1299,7 @@ class CultCacheTests(unittest.TestCase):
|
|||
typed_target = CultMesh.create_node(runtime_id="mesh-log-typed-target")
|
||||
typed_target.database.register_document(document)
|
||||
typed_target.database.put(document, "note:1", {"body": "first"})
|
||||
typed_applied = typed_target.database.apply_shard_log_response(CultNetShardLogResponse.from_wire(response))
|
||||
typed_applied = typed_target.database.apply_shard_log_response(typed_response)
|
||||
self.assertEqual([record.change_kind for record in typed_applied], ["updated", "removed"])
|
||||
self.assertIsNone(typed_target.database.get(document, "note:1"))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue