Add typed Python witness artifact bundles

This commit is contained in:
Meta Vens 2026-06-14 05:04:06 +01:00
commit ef2501f767
6 changed files with 99 additions and 21 deletions

View file

@ -56,6 +56,10 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
`read_next_snapshot_response(...)` / `iter_snapshot_responses(...)`, while
raw message reads remain available for callers that own their own dispatch
loop.
- Python CultNet witness artifacts:
Provides typed `CultNetWitnessArtifactBundle` ownership for the C#-compatible
8-slot witness artifact payload while preserving dict helpers for existing
wire callers.
- Python CultMesh discovery client:
Fetches Verse catalogs and peer exchange responses from the live Python
endpoint over CultNet frames.

View file

@ -152,6 +152,7 @@ member order.
from cultnet_py import (
CultNetRawClient,
CultNetSimulationObservation,
CultNetWitnessArtifactBundle,
apply_raw_snapshot,
apply_shard_log_response,
compute_simulation_claim_hash,
@ -202,6 +203,8 @@ witness = witness_artifact_bundle(
artifacts=[{"role": "log", "uri": "cultcache://bundle-1/log", "mediaType": "text/plain"}],
provenance={"pipelineId": "interop", "runId": "run-1", "runtimeId": "python-runtime"},
)
typed_witness = CultNetWitnessArtifactBundle.from_wire(witness)
witness_payload = typed_witness.to_payload()
```
The peer can serve, dial, and probe the same raw-state interop lane used by the

View file

@ -32,6 +32,7 @@ EXPECTED_EXPORTS: dict[str, tuple[str, ...]] = {
"CultNetClientAuthorityScope",
"CultNetDatabaseSubscription",
"CultNetMessage",
"CultNetWitnessArtifactBundle",
"CultNetRawClient",
"CultNetRawDocumentRecord",
"CultNetRawSnapshotResponse",

View file

@ -2,6 +2,7 @@ from .client import CultNetDatabaseSubscription, CultNetRawClient
from .framing import decode_frame, encode_frame, read_frame, write_frame
from .messages import (
CultNetMessage,
CultNetWitnessArtifactBundle,
compute_simulation_claim_hash,
database_subscribe,
database_unsubscribe,
@ -48,6 +49,7 @@ from .simulation import (
__all__ = [
"CultNetMessage",
"CultNetWitnessArtifactBundle",
"CultNetDatabaseSubscription",
"CultNetDatabaseChange",
"CultNetRawClient",

View file

@ -224,6 +224,54 @@ def simulation_observation(
)
@dataclass(frozen=True)
class CultNetWitnessArtifactBundle:
bundle_id: str
witness_kind: str
captured_at: str
subject: dict[str, Any]
contracts: tuple[dict[str, Any], ...]
artifacts: tuple[dict[str, Any], ...]
provenance: dict[str, Any]
timing_witnesses: tuple[dict[str, Any], ...] = ()
@classmethod
def from_wire(cls, value: dict[str, Any]) -> "CultNetWitnessArtifactBundle":
return cls(
bundle_id=str(value.get("bundleId") or ""),
witness_kind=str(value.get("witnessKind") or ""),
captured_at=str(value.get("capturedAt") or ""),
subject=dict(value.get("subject") or {}),
contracts=tuple(dict(contract) for contract in value.get("contracts") or [] if isinstance(contract, dict)),
artifacts=tuple(dict(artifact) for artifact in value.get("artifacts") or [] if isinstance(artifact, dict)),
timing_witnesses=tuple(
dict(timing_witness)
for timing_witness in value.get("timingWitnesses") or []
if isinstance(timing_witness, dict)
),
provenance=dict(value.get("provenance") or {}),
)
@classmethod
def from_payload(cls, payload: bytes) -> "CultNetWitnessArtifactBundle":
return cls.from_wire(decode_witness_artifact_bundle_payload(payload))
def to_wire(self) -> dict[str, Any]:
return {
"bundleId": self.bundle_id,
"witnessKind": self.witness_kind,
"capturedAt": self.captured_at,
"subject": dict(self.subject),
"contracts": [dict(contract) for contract in self.contracts],
"artifacts": [dict(artifact) for artifact in self.artifacts],
"timingWitnesses": [dict(timing_witness) for timing_witness in self.timing_witnesses],
"provenance": dict(self.provenance),
}
def to_payload(self) -> bytes:
return encode_witness_artifact_bundle_payload(self)
def witness_artifact_bundle(
*,
bundle_id: str,
@ -235,28 +283,29 @@ def witness_artifact_bundle(
provenance: dict[str, Any],
timing_witnesses: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
return {
"bundleId": bundle_id,
"witnessKind": witness_kind,
"capturedAt": captured_at,
"subject": subject,
"contracts": contracts,
"artifacts": artifacts,
"timingWitnesses": timing_witnesses or [],
"provenance": provenance,
}
return CultNetWitnessArtifactBundle(
bundle_id=bundle_id,
witness_kind=witness_kind,
captured_at=captured_at,
subject=subject,
contracts=tuple(contracts),
artifacts=tuple(artifacts),
timing_witnesses=tuple(timing_witnesses or []),
provenance=provenance,
).to_wire()
def encode_witness_artifact_bundle_payload(bundle: dict[str, Any]) -> bytes:
def encode_witness_artifact_bundle_payload(bundle: dict[str, Any] | CultNetWitnessArtifactBundle) -> bytes:
wire = bundle.to_wire() if isinstance(bundle, CultNetWitnessArtifactBundle) else bundle
return msgpack.packb([
bundle["bundleId"],
bundle["witnessKind"],
bundle["capturedAt"],
bundle["subject"],
bundle["contracts"],
bundle["artifacts"],
bundle.get("timingWitnesses", []),
bundle["provenance"],
wire["bundleId"],
wire["witnessKind"],
wire["capturedAt"],
wire["subject"],
wire["contracts"],
wire["artifacts"],
wire.get("timingWitnesses", []),
wire["provenance"],
], use_bin_type=True)

View file

@ -36,6 +36,7 @@ from cultnet_py import (
CultNetSimulationConsensusOptions,
CultNetSimulationConsensusCandidate,
CultNetSimulationObservation,
CultNetWitnessArtifactBundle,
apply_raw_document_record,
apply_raw_snapshot,
apply_shard_log_response,
@ -1118,6 +1119,8 @@ class CultCacheTests(unittest.TestCase):
self.assertEqual(observation.to_message_wire(message_id="obs-2")["observation"], message["observation"])
def test_cultnet_witness_artifact_bundle_uses_csharp_slot_order(self) -> None:
import msgpack # type: ignore
bundle = witness_artifact_bundle(
bundle_id="bundle-1",
witness_kind="interop-proof",
@ -1130,12 +1133,28 @@ class CultCacheTests(unittest.TestCase):
)
payload = encode_witness_artifact_bundle_payload(bundle)
decoded = decode_witness_artifact_bundle_payload(payload)
typed = CultNetWitnessArtifactBundle.from_wire(bundle)
typed_payload = typed.to_payload()
typed_decoded = CultNetWitnessArtifactBundle.from_payload(typed_payload)
slots = msgpack.unpackb(payload, raw=False)
self.assertEqual(payload, typed_payload)
self.assertEqual(slots[0], "bundle-1")
self.assertEqual(slots[1], "interop-proof")
self.assertEqual(slots[2], "2026-06-13T00:00:03Z")
self.assertEqual(slots[3]["subjectId"], "note:python")
self.assertEqual(slots[4][0]["schemaId"], "schema-a")
self.assertEqual(slots[5][0]["uri"], "cultcache://bundle-1/log")
self.assertEqual(slots[6][0]["latencyMs"], 1.0)
self.assertEqual(slots[7]["runtimeId"], "python-test")
self.assertEqual(decoded["bundleId"], "bundle-1")
self.assertEqual(decoded["witnessKind"], "interop-proof")
self.assertEqual(decoded["subject"]["subjectId"], "note:python")
self.assertEqual(decoded["contracts"][0]["schemaId"], "schema-a")
self.assertEqual(decoded["artifacts"][0]["mediaType"], "text/plain")
self.assertEqual(decoded["provenance"]["runtimeId"], "python-test")
self.assertEqual(typed.bundle_id, "bundle-1")
self.assertEqual(typed_decoded.to_wire(), bundle)
def test_cultmesh_node_uses_cultcache_store(self) -> None:
with tempfile.TemporaryDirectory() as tmp: