From d4744c88c18c3413e35bfc5c202f147ce3121fa7 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 14 Jun 2026 12:06:20 +0100 Subject: [PATCH] Serve Python simulation observations --- packages/cultcache-py/PARITY.md | 4 ++ packages/cultcache-py/README.md | 19 ++++--- .../cultcache-py/src/cultmesh_py/facade.py | 4 +- .../cultcache-py/src/cultmesh_py/server.py | 22 +++++++- packages/cultcache-py/tests/test_cultcache.py | 52 ++++++++++++++++++- 5 files changed, 88 insertions(+), 13 deletions(-) diff --git a/packages/cultcache-py/PARITY.md b/packages/cultcache-py/PARITY.md index e3a2fd79..f0264c47 100644 --- a/packages/cultcache-py/PARITY.md +++ b/packages/cultcache-py/PARITY.md @@ -95,6 +95,10 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. MessagePack request/response lane used by `CultNetRawClient` and `CultMeshDiscoveryClient`. It also supports live raw database subscriptions for snapshot, put, and delete notifications on a connected client stream. + When given a `CultNetSimulationObservationHub`, it accepts + `cultnet.simulation_observation.v0` messages and replies with current + `cultnet.simulation_consensus_candidate.v0` messages through the same framed + lane. Its schema catalog includes both registered document payload schemas and shared wire-message descriptors for the CultNet/CultMesh messages it handles. Hello and payload schema descriptors advertise the raw snapshot, put, delete, diff --git a/packages/cultcache-py/README.md b/packages/cultcache-py/README.md index 327bbe6b..f1ec04ed 100644 --- a/packages/cultcache-py/README.md +++ b/packages/cultcache-py/README.md @@ -267,7 +267,15 @@ shard_log_wire = node.database.create_shard_log_response(shard_id="interop", sha peers = CultMesh.create_peer_catalog() response = peers.create_response(peer_exchange_request("pex-1", verse_id="local")) -server = CultMesh.serve_node(node, peer_catalog=peers, port=3075) +session = CultMesh.create_game_session( + node, + CultMeshGameSessionOptions( + client_authority_scopes=( + CultNetClientAuthorityScope("python-runtime", schema_ids=(note_doc.catalog_entry().schema_id,), key_prefix="input:python"), + ), + ), +) +server = CultMesh.serve_node(node, peer_catalog=peers, observation_hub=session.observation_hub, port=3075) client = CultMesh.create_verse_discovery_client("127.0.0.1", 3075) verses = client.fetch_verses(transport_version="cultmesh.v0") @@ -316,15 +324,6 @@ committed = facts.commit({ "confidence": 1.0, "hasQuorum": True, }) - -session = CultMesh.create_game_session( - node, - CultMeshGameSessionOptions( - client_authority_scopes=( - CultNetClientAuthorityScope("python-runtime", schema_ids=(note_doc.catalog_entry().schema_id,), key_prefix="input:python"), - ), - ), -) session_commits = session.submit_and_commit(typed_observation) prediction = session.predict(note_doc, "input:python:move", {"body": "predicted input"}) unsubscribe() diff --git a/packages/cultcache-py/src/cultmesh_py/facade.py b/packages/cultcache-py/src/cultmesh_py/facade.py index 1f4320b9..d0e035ca 100644 --- a/packages/cultcache-py/src/cultmesh_py/facade.py +++ b/packages/cultcache-py/src/cultmesh_py/facade.py @@ -2,7 +2,7 @@ from __future__ import annotations from pathlib import Path -from cultnet_py import CultNetRawClient +from cultnet_py import CultNetRawClient, CultNetSimulationObservationHub from .client import CultMeshPeerExchangeClient, CultMeshVerseDiscoveryClient from .node import CultMeshNode, CultMeshNodeOptions, create_node @@ -103,6 +103,7 @@ class CultMesh: *, verse_catalog: CultMeshVerseCatalog | None = None, peer_catalog: CultMeshPeerCatalog | None = None, + observation_hub: CultNetSimulationObservationHub | None = None, host: str = "127.0.0.1", port: int = 0, display_name: str | None = None, @@ -111,6 +112,7 @@ class CultMesh: node=node, verse_catalog=verse_catalog or CultMeshVerseCatalog(), peer_catalog=peer_catalog or CultMeshPeerCatalog(), + observation_hub=observation_hub, host=host, port=port, display_name=display_name, diff --git a/packages/cultcache-py/src/cultmesh_py/server.py b/packages/cultcache-py/src/cultmesh_py/server.py index eb21b9fa..ff69ae9c 100644 --- a/packages/cultcache-py/src/cultmesh_py/server.py +++ b/packages/cultcache-py/src/cultmesh_py/server.py @@ -7,7 +7,7 @@ from typing import Any import msgpack -from cultnet_py import read_frame, wire_message_schema_descriptors, write_frame +from cultnet_py import CultNetSimulationObservationHub, read_frame, wire_message_schema_descriptors, write_frame from .node import CultMeshNode from .wire import ( @@ -38,6 +38,7 @@ class CultMeshLocalServer: node: CultMeshNode verse_catalog: CultMeshVerseCatalog = field(default_factory=CultMeshVerseCatalog) peer_catalog: CultMeshPeerCatalog = field(default_factory=CultMeshPeerCatalog) + observation_hub: CultNetSimulationObservationHub | None = None host: str = "127.0.0.1" port: int = 0 display_name: str | None = None @@ -107,6 +108,9 @@ class CultMeshLocalServer: return self.verse_catalog.create_response(message) if schema_version == PEER_EXCHANGE_REQUEST: return self.peer_catalog.create_response(message) + if schema_version == "cultnet.simulation_observation.v0": + candidates = self._candidate_responses(message) + return candidates[0] if candidates else None return None def _accept_loop(self) -> None: @@ -169,6 +173,8 @@ class CultMeshLocalServer: return self._handle_raw_put(message, subscriptions) if schema_version == "cultnet.document_delete.v0": return self._handle_raw_delete(message, subscriptions) + if schema_version == "cultnet.simulation_observation.v0": + return self._candidate_responses(message) response = self.handle_message(message) return [] if response is None else [response] @@ -269,12 +275,26 @@ class CultMeshLocalServer: "cultnet.document_delete.v0", "cultnet.shard_catalog_request.v0", "cultnet.shard_log_request.v0", + "cultnet.simulation_observation.v0", + "cultnet.simulation_consensus_candidate.v0", VERSE_CATALOG_REQUEST, PEER_EXCHANGE_REQUEST, ], "supportsSchemaCatalog": True, } + def _candidate_responses(self, message: dict[str, Any]) -> list[dict[str, Any]]: + if self.observation_hub is None: + return [] + message_id = str(message.get("messageId") or "") + return [ + { + **candidate.to_wire(), + "messageId": message_id, + } + for candidate in self.observation_hub.submit_candidate_objects(message) + ] + def _schema_catalog_response(self, request: dict[str, Any]) -> dict[str, Any]: requested_schema_ids = {str(value) for value in request.get("schemaIds") or []} requested_kinds = {str(value) for value in request.get("kinds") or []} diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index d72fd096..3de41730 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -50,6 +50,7 @@ from cultnet_py import ( CultNetSimulationConsensusOptions, CultNetSimulationConsensusCandidate, CultNetSimulationObservation, + CultNetSimulationObservationHub, CultNetWitnessArtifactBundle, apply_raw_document_record, apply_raw_snapshot, @@ -2370,6 +2371,12 @@ class CultCacheTests(unittest.TestCase): node = CultMesh.create_node(runtime_id="mesh-server") node.database.register_document(document) node.database.put_raw_message(document, "note:1", {"body": "served"}, shard_id="notes", shard_epoch=1) + observation_hub = CultNetSimulationObservationHub( + CultNetSimulationConsensusOptions(minimum_witnesses=2, quorum_ratio=1.0) + ) + seen_candidates: list[CultNetSimulationConsensusCandidate] = [] + unsubscribe_candidates = observation_hub.watch_candidates(seen_candidates.append) + claim_hash = compute_simulation_claim_hash("hit", "alice", "bob", "frame:100") verses = CultMeshVerseCatalog() verses.upsert( CultMeshVerseDescriptor( @@ -2389,7 +2396,13 @@ class CultCacheTests(unittest.TestCase): ) ) - server = CultMesh.serve_node(node, verse_catalog=verses, peer_catalog=peers, display_name="Mesh Server") + server = CultMesh.serve_node( + node, + verse_catalog=verses, + peer_catalog=peers, + observation_hub=observation_hub, + display_name="Mesh Server", + ) try: raw_client = CultMesh.create_client("127.0.0.1", server.port, timeout_seconds=2.0) hello_response = raw_client.request(hello(runtime_id="probe"), expected_schema_version="cultnet.hello.v0") @@ -2412,6 +2425,34 @@ class CultCacheTests(unittest.TestCase): discovery_client = CultMesh.create_verse_discovery_client("127.0.0.1", server.port, timeout_seconds=2.0) fetched_verses = discovery_client.fetch_verses(transport_version="cultmesh.v0") fetched_peers = discovery_client.fetch_peers(verse_id="server-verse", roles=["read-replica"]) + first_candidate = raw_client.request( + simulation_observation( + message_id="obs-1", + witness_runtime_id="watcher-1", + shard_id="arena", + shard_epoch=4, + frame=100, + subject_id="bob", + claim_kind="hit", + claim_hash=claim_hash, + claim_summary="alice shot bob first", + ), + expected_schema_version="cultnet.simulation_consensus_candidate.v0", + ) + quorum_candidate = raw_client.request( + simulation_observation( + message_id="obs-2", + witness_runtime_id="watcher-2", + shard_id="arena", + shard_epoch=4, + frame=100, + subject_id="bob", + claim_kind="hit", + claim_hash=claim_hash, + claim_summary="alice shot bob first", + ), + expected_schema_version="cultnet.simulation_consensus_candidate.v0", + ) with raw_client.subscribe_database(subscription_id="sub-server", schema_ids=["mesh.server_note.v1"]) as subscription: subscription_snapshot = subscription.read_next_snapshot_response() subscription.send(document_put_raw( @@ -2433,12 +2474,14 @@ class CultCacheTests(unittest.TestCase): )) subscription_delete = subscription.read_next_change() finally: + unsubscribe_candidates() server.stop() self.assertEqual(hello_response["runtimeId"], "mesh-server") self.assertEqual(hello_response["displayName"], "Mesh Server") self.assertIn("cultnet.database_subscribe.v0", hello_response["supportedMessageVersions"]) self.assertIn("cultnet.document_put_raw.v0", hello_response["supportedMessageVersions"]) + self.assertIn("cultnet.simulation_observation.v0", hello_response["supportedMessageVersions"]) self.assertEqual(hello_response["supportedMutationContracts"][0]["documentType"], "mesh.server_note") self.assertIn("documentDelete", hello_response["supportedMutationContracts"][0]["operations"]) self.assertIn("shardLog", hello_response["supportedMutationContracts"][0]["operations"]) @@ -2472,6 +2515,13 @@ class CultCacheTests(unittest.TestCase): self.assertEqual(typed_shard_log.entries[0].raw_document.record_key, "note:1") self.assertEqual(fetched_verses[0].verse_id, "server-verse") self.assertEqual(fetched_peers[0].peer_id, "mesh-server") + self.assertEqual(first_candidate["messageId"], "obs-1") + self.assertFalse(first_candidate["hasQuorum"]) + self.assertEqual(quorum_candidate["messageId"], "obs-2") + self.assertEqual(quorum_candidate["claimHash"], claim_hash) + self.assertTrue(quorum_candidate["hasQuorum"]) + self.assertEqual(quorum_candidate["witnessCount"], 2) + self.assertEqual([candidate.claim_hash for candidate in seen_candidates], [claim_hash, claim_hash]) self.assertIsInstance(subscription_snapshot, CultNetRawSnapshotResponse) self.assertEqual(subscription_snapshot.to_wire()["schemaVersion"], "cultnet.snapshot_response_raw.v0") self.assertEqual(subscription_change.change_kind, "added")