Add Python CultMesh session watches
This commit is contained in:
parent
2186fd540c
commit
e65cba954a
4 changed files with 47 additions and 3 deletions
|
|
@ -124,6 +124,10 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
|
|||
The live interop harness runs a Python CultMesh session client, writes a local
|
||||
prediction, pulls the live Python peer shard log, applies the authoritative
|
||||
entry, and verifies the session reports the record as reconciled.
|
||||
- Python CultMesh session watches:
|
||||
`CultMeshGameSession.watch_candidates(...)` and
|
||||
`watch_simulation_facts(...)` expose the same gameplay-facing observation and
|
||||
committed-fact watch points as the C# session facade.
|
||||
- Python package health:
|
||||
`cultcache-py-verify --json` checks the public CultCache/CultNet/CultMesh
|
||||
export surface, `py.typed` markers, a live local CultMesh framed-wire smoke
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
from typing import Any, Callable
|
||||
|
||||
from cultcache_py.documents import DocumentDefinition
|
||||
from cultnet_py import (
|
||||
|
|
@ -16,6 +16,7 @@ from cultnet_py import (
|
|||
)
|
||||
|
||||
from .node import CultMeshNode
|
||||
from .node import CultMeshDatabaseChange
|
||||
from .simulation import (
|
||||
CultMeshSimulationFact,
|
||||
CultMeshSimulationFactCommit,
|
||||
|
|
@ -79,6 +80,18 @@ class CultMeshGameSession:
|
|||
self._predicted_keys.add((schema_id, key))
|
||||
return CultMeshPrediction(key=key, schema_id=schema_id, document=document, value=value)
|
||||
|
||||
def watch_candidates(
|
||||
self,
|
||||
callback: Callable[[CultNetSimulationConsensusCandidate], None],
|
||||
) -> Callable[[], None]:
|
||||
return self.observation_hub.watch_candidates(callback)
|
||||
|
||||
def watch_simulation_facts(
|
||||
self,
|
||||
callback: Callable[[CultMeshDatabaseChange], None],
|
||||
) -> Callable[[], None]:
|
||||
return self.node.database.watch(callback, document=simulation_fact_document)
|
||||
|
||||
def apply_shard_log_response(
|
||||
self,
|
||||
response: dict[str, Any] | CultNetShardLogResponse,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
from typing import Any, Callable
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -183,6 +183,19 @@ class CultNetSimulationObservationHub:
|
|||
|
||||
def __post_init__(self) -> None:
|
||||
self._consensus = CultNetSimulationConsensus(self.options)
|
||||
self._candidate_subscribers: list[Callable[[CultNetSimulationConsensusCandidate], None]] = []
|
||||
|
||||
def watch_candidates(
|
||||
self,
|
||||
callback: Callable[[CultNetSimulationConsensusCandidate], None],
|
||||
) -> Callable[[], None]:
|
||||
self._candidate_subscribers.append(callback)
|
||||
|
||||
def unsubscribe() -> None:
|
||||
if callback in self._candidate_subscribers:
|
||||
self._candidate_subscribers.remove(callback)
|
||||
|
||||
return unsubscribe
|
||||
|
||||
def submit_candidate_objects(
|
||||
self,
|
||||
|
|
@ -190,11 +203,15 @@ class CultNetSimulationObservationHub:
|
|||
) -> list[CultNetSimulationConsensusCandidate]:
|
||||
observation = _observation_wire(observation_or_message)
|
||||
self.observations.append(observation)
|
||||
return [
|
||||
candidates = [
|
||||
candidate
|
||||
for candidate in self._consensus.build_candidate_objects(self.observations)
|
||||
if _candidate_matches_observation(candidate.to_wire(), observation)
|
||||
]
|
||||
for candidate in candidates:
|
||||
for callback in list(self._candidate_subscribers):
|
||||
callback(candidate)
|
||||
return candidates
|
||||
|
||||
def submit(self, observation_or_message: dict[str, Any] | CultNetSimulationObservation) -> list[dict[str, Any]]:
|
||||
return [candidate.to_wire() for candidate in self.submit_candidate_objects(observation_or_message)]
|
||||
|
|
|
|||
|
|
@ -1678,16 +1678,26 @@ class CultCacheTests(unittest.TestCase):
|
|||
consensus_options=CultNetSimulationConsensusOptions(minimum_witnesses=2, quorum_ratio=1.0)
|
||||
),
|
||||
)
|
||||
seen_candidates: list[CultNetSimulationConsensusCandidate] = []
|
||||
seen_facts: list[CultMeshDatabaseChange] = []
|
||||
unsubscribe_candidates = session.watch_candidates(seen_candidates.append)
|
||||
unsubscribe_facts = session.watch_simulation_facts(seen_facts.append)
|
||||
|
||||
self.assertEqual(session.submit_and_commit(CultNetSimulationObservation.from_wire(first)), [])
|
||||
typed_candidates = session.submit_observation_candidates(CultNetSimulationObservation.from_wire(second))
|
||||
commits = session.commit_quorum_candidate_objects(typed_candidates)
|
||||
replay = session.commit_quorum_candidates(session.submit_observation(second))
|
||||
unsubscribe_candidates()
|
||||
unsubscribe_facts()
|
||||
|
||||
self.assertEqual(len(typed_candidates), 1)
|
||||
self.assertTrue(typed_candidates[0].has_quorum)
|
||||
self.assertEqual(len(commits), 1)
|
||||
self.assertEqual(replay, [])
|
||||
self.assertEqual([candidate.claim_hash for candidate in seen_candidates], [claim_hash, claim_hash, claim_hash])
|
||||
self.assertEqual(len(seen_facts), 1)
|
||||
self.assertEqual(seen_facts[0].change_kind, "added")
|
||||
self.assertEqual(seen_facts[0].value.claim_hash, claim_hash)
|
||||
stored = node.get_required(simulation_fact_document, commits[0].key)
|
||||
self.assertEqual(stored.claim_hash, claim_hash)
|
||||
self.assertEqual(stored.witness_count, 2)
|
||||
|
|
|
|||
Loading…
Reference in a new issue