Add Python one-call document snapshot sync

This commit is contained in:
Meta Vens 2026-06-27 22:56:17 +01:00
commit 854165e943
2 changed files with 111 additions and 0 deletions

View file

@ -180,6 +180,23 @@ class CultMeshNode:
shard_epoch=shard_epoch,
)
def sync_document(
self,
client: CultNetRawClient,
document: DocumentDefinition[Any],
key: str,
*,
shard_id: str | None = None,
shard_epoch: int | None = None,
) -> Any:
return self.database.sync_document(
client,
document,
key,
shard_id=shard_id,
shard_epoch=shard_epoch,
)
def sync_shard_log(
self,
client: CultNetRawClient,
@ -630,6 +647,29 @@ class CultMeshDatabase:
)
return self.apply_snapshot_response(response)
def sync_document(
self,
client: CultNetRawClient,
document: DocumentDefinition[Any],
key: str,
*,
shard_id: str | None = None,
shard_epoch: int | None = None,
) -> Any:
if not key:
raise ValueError("key must be non-empty")
requested = document
registered = self._resolve_document_alias(document)
schema_id = registered.catalog_entry().schema_id
self.sync_snapshot(
client,
schema_ids=[schema_id],
record_keys=[key],
shard_id=shard_id,
shard_epoch=shard_epoch,
)
return self.get_required(requested, key)
def sync_shard_log(
self,
client: CultNetRawClient,

View file

@ -2054,6 +2054,77 @@ class CultCacheTests(unittest.TestCase):
self.assertEqual(log_changes[0].change_kind, "updated")
self.assertEqual(node.get_required(document, "item:node").value, 9)
def test_cultmesh_node_sync_document_returns_requested_alias_type(self) -> None:
@dataclass
class CanonicalNote:
body: str
@dataclass
class UiNote:
body: str
document = define_database_entry_type(
"mesh.sync_alias_note",
[("body", 0)],
cls=CanonicalNote,
schema_id="mesh.sync_alias_note.v1",
schema_name="mesh.sync_alias_note",
schema_version="mesh.sync_alias_note.v1",
)
alias = define_database_entry_type(
"mesh.sync_alias_note.ui",
[("body", 0)],
cls=UiNote,
schema_id="mesh.sync_alias_note.v1",
schema_name="mesh.sync_alias_note",
schema_version="mesh.sync_alias_note.v1",
)
node = create_node(runtime_id="python-node")
node.register_document(document)
requests: list[dict[str, Any]] = []
class SnapshotClient:
def fetch_snapshot_response(
self,
*,
schema_ids: list[str] | None = None,
record_keys: list[str] | None = None,
shard_id: str | None = None,
shard_epoch: int | None = None,
) -> dict[str, Any]:
requests.append({
"schema_ids": schema_ids,
"record_keys": record_keys,
"shard_id": shard_id,
"shard_epoch": shard_epoch,
})
return {
"schemaVersion": "cultnet.snapshot_response_raw.v0",
"messageId": "sync-document",
"documents": [
{
"schemaId": document.catalog_entry().schema_id,
"recordKey": "note:remote",
"storedAt": "2026-06-27T00:00:00Z",
"payloadEncoding": "messagepack",
"payload": document.encode_payload(CanonicalNote("synced-alias")),
}
],
}
synced = node.sync_document(SnapshotClient(), alias, "note:remote")
self.assertIsInstance(synced, UiNote)
self.assertEqual(synced.body, "synced-alias")
self.assertEqual(node.get_required(document, "note:remote").body, "synced-alias")
self.assertIsInstance(node.get_required(alias, "note:remote"), UiNote)
self.assertEqual(requests, [{
"schema_ids": ["mesh.sync_alias_note.v1"],
"record_keys": ["note:remote"],
"shard_id": None,
"shard_epoch": None,
}])
def test_cultmesh_node_emits_raw_put_and_delete_messages_for_local_writes(self) -> None:
document = define_database_entry_type(
"mesh.emit_item",