CultLib/packages/cultnet-py/src/cultnet_py/snapshot.py
Meta Vens 95493862bd Split the Python runtime into cultcache-py, cultnet-py, cultmesh-py
One distribution named cultcache-py carried three libraries. Rust and
TypeScript layer them as cultcache < cultnet < cultmesh; Python now does
the same, one package per layer, module names unchanged so every
`from cultmesh_py import ...` keeps working.

Two imports ran against that layering and are resolved by moving code to
where the dependency direction says it lives, not by adding a shim:

- cultnet_py.interop_peer imported the Verse/peer catalog, authority
  lease, stream, and simulation-fact contracts from cultmesh_py. They are
  wire contracts a bare peer has to speak without owning a node, which is
  why cultnet-rs and cultnet-ts keep them in the net layer. They now live
  in cultnet_py.cultmesh_contracts; cultmesh_py re-exports them.
- cultcache_py.benchmark imported cultnet_py. It measures framed CultNet
  parse beside cache paths, so it and compare_csharp move to cultnet_py.
  verify checks all three packages and moves to cultmesh_py.

The single 6,693-line test module is split by what each test touches: a
test using any cultmesh_py name lands in cultmesh-py, else cultnet_py in
cultnet-py, else cultcache-py. Each suite runs with only its layer and the
layers below on the path, so a test that reaches up fails to import
rather than quietly binding the packages back together. 11 / 53 / 78 =
the same 142.

Workflows install all three; the publish job is one matrix over the
three packages keyed by tag prefix. The TS test harnesses put all three
src directories on PYTHONPATH. cultcache-py bumps to 0.2.0 because it no
longer contains cultnet_py or cultmesh_py; the new packages start at
0.1.0. Nothing was ever published, so no PyPI consumer moves.

  cultcache-py  11 tests, path: cultcache-py/src
  cultnet-py    53 tests, path: + cultnet-py/src
  cultmesh-py   78 tests, path: + cultmesh-py/src
  python -m cultmesh_py.verify -> ok, all five checks
  cultnet_py.interop_peer imports with cultmesh_py absent

Not run here: the cultnet-ts interop ring and cultcache-ts store matrix
need dotnet/kotlin peers; their PYTHONPATH change is four lines.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-10 13:10:47 +02:00

123 lines
4.5 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from typing import Any
@dataclass(frozen=True)
class CultNetRawDocumentRecord:
schema_id: str
record_key: str
payload: bytes
stored_at: str | None = None
payload_encoding: str = "messagepack"
source_runtime_id: str | None = None
source_agent_id: str | None = None
source_role: str | None = None
tags: tuple[str, ...] = ()
@classmethod
def from_wire(cls, value: dict[str, Any]) -> "CultNetRawDocumentRecord":
schema_id = str(value.get("schemaId") or "")
record_key = str(value.get("recordKey") or "")
if not schema_id:
raise ValueError("raw document record schemaId must be non-empty")
if not record_key:
raise ValueError("raw document record recordKey must be non-empty")
return cls(
schema_id=schema_id,
record_key=record_key,
payload=bytes(value.get("payload") or b""),
stored_at=_optional_string(value.get("storedAt")),
payload_encoding=str(value.get("payloadEncoding") or "messagepack"),
source_runtime_id=_optional_string(value.get("sourceRuntimeId")),
source_agent_id=_optional_string(value.get("sourceAgentId")),
source_role=_optional_string(value.get("sourceRole")),
tags=tuple(str(tag) for tag in value.get("tags") or ()),
)
def to_wire(self) -> dict[str, Any]:
wire: dict[str, Any] = {
"schemaId": self.schema_id,
"recordKey": self.record_key,
"payloadEncoding": self.payload_encoding,
"payload": self.payload,
}
if self.stored_at is not None:
wire["storedAt"] = self.stored_at
if self.source_runtime_id is not None:
wire["sourceRuntimeId"] = self.source_runtime_id
if self.source_agent_id is not None:
wire["sourceAgentId"] = self.source_agent_id
if self.source_role is not None:
wire["sourceRole"] = self.source_role
if self.tags:
wire["tags"] = list(self.tags)
return wire
@dataclass(frozen=True)
class CultNetRawSnapshotResponse:
message_id: str
documents: tuple[CultNetRawDocumentRecord, ...]
shard_id: str | None = None
shard_epoch: int | None = None
shard_log_sequence: int | None = None
@classmethod
def from_wire(cls, response: dict[str, Any]) -> "CultNetRawSnapshotResponse":
if response.get("schemaVersion") != "cultnet.snapshot_response_raw.v0":
raise ValueError(f"Expected cultnet.snapshot_response_raw.v0, received {response.get('schemaVersion')!r}")
return cls(
message_id=str(response.get("messageId") or ""),
documents=tuple(
CultNetRawDocumentRecord.from_wire(document)
for document in response.get("documents") or ()
if isinstance(document, dict)
),
shard_id=_optional_string(response.get("shardId")),
shard_epoch=_optional_int(response.get("shardEpoch")),
shard_log_sequence=_optional_int(response.get("shardLogSequence")),
)
def filter(
self,
*,
schema_ids: list[str] | None = None,
record_keys: list[str] | None = None,
) -> tuple[CultNetRawDocumentRecord, ...]:
requested_schema_ids = set(schema_ids or [])
requested_record_keys = set(record_keys or [])
return tuple(
document
for document in self.documents
if (not requested_schema_ids or document.schema_id in requested_schema_ids)
and (not requested_record_keys or document.record_key in requested_record_keys)
)
def to_wire(self) -> dict[str, Any]:
wire: dict[str, Any] = {
"schemaVersion": "cultnet.snapshot_response_raw.v0",
"messageId": self.message_id,
"documents": [document.to_wire() for document in self.documents],
}
if self.shard_id is not None:
wire["shardId"] = self.shard_id
if self.shard_epoch is not None:
wire["shardEpoch"] = self.shard_epoch
if self.shard_log_sequence is not None:
wire["shardLogSequence"] = self.shard_log_sequence
return wire
def _optional_string(value: Any) -> str | None:
if value is None:
return None
text = str(value)
return text if text else None
def _optional_int(value: Any) -> int | None:
if value is None:
return None
return int(value)