Commit Python CultMesh simulation facts
This commit is contained in:
parent
6243011d46
commit
9440691e70
5 changed files with 244 additions and 2 deletions
|
|
@ -210,7 +210,7 @@ python -m cultnet_py.interop_peer probe --runtime-id python-prober --discovery-p
|
|||
|
||||
`cultmesh_py` includes a local cache-backed node, schema-v0 helpers for the
|
||||
CultMesh Verse catalog and peer exchange wire messages, local authority lease
|
||||
checks, and stream transport negotiation:
|
||||
checks, stream transport negotiation, and committed simulation fact documents:
|
||||
|
||||
```python
|
||||
from cultcache_py import define_database_entry_type
|
||||
|
|
@ -238,6 +238,22 @@ node.sync_snapshot(raw_client, schema_ids=[note_doc.catalog_entry().schema_id])
|
|||
node.sync_shard_log(raw_client, shard_id="interop", shard_epoch=1)
|
||||
|
||||
streams = CultMesh.create_stream_catalog()
|
||||
|
||||
claim_hash = "accepted-claim-hash"
|
||||
facts = CultMesh.create_simulation_fact_committer(node)
|
||||
committed = facts.commit({
|
||||
"shardId": "arena",
|
||||
"shardEpoch": 4,
|
||||
"frame": 100,
|
||||
"subjectId": "bob",
|
||||
"claimKind": "hit",
|
||||
"claimHash": claim_hash,
|
||||
"witnessCount": 2,
|
||||
"supportWeight": 2.0,
|
||||
"totalWeight": 2.0,
|
||||
"confidence": 1.0,
|
||||
"hasQuorum": True,
|
||||
})
|
||||
```
|
||||
|
||||
## Wire Parity
|
||||
|
|
@ -267,7 +283,8 @@ Current receipts:
|
|||
- `packages/cultcache-py/tests/test_cultcache.py` covers Python CultMesh
|
||||
Verse catalog, peer exchange, authority lease, stream negotiation, CultNet
|
||||
helper shapes, raw snapshot/shard-log application, simulation claim hashing,
|
||||
and witness artifact bundle payload slots.
|
||||
committed simulation fact payload slots, and witness artifact bundle payload
|
||||
slots.
|
||||
- `cultcache_py`, `cultnet_py`, and `cultmesh_py` ship `py.typed` markers so
|
||||
downstream type checkers can inspect the package surface instead of treating
|
||||
the runtime as an untyped xenos swamp.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,14 @@
|
|||
from .client import CultMeshDiscoveryClient
|
||||
from .facade import CultMesh
|
||||
from .node import CultMeshNode, create_node
|
||||
from .simulation import (
|
||||
SIMULATION_FACT_DOCUMENT_TYPE,
|
||||
SIMULATION_FACT_SCHEMA_VERSION,
|
||||
CultMeshSimulationFact,
|
||||
CultMeshSimulationFactCommit,
|
||||
CultMeshSimulationFactCommitter,
|
||||
simulation_fact_document,
|
||||
)
|
||||
from .wire import (
|
||||
PEER_EXCHANGE_REQUEST,
|
||||
PEER_EXCHANGE_RESPONSE,
|
||||
|
|
@ -29,6 +37,9 @@ __all__ = [
|
|||
"CultMesh",
|
||||
"CultMeshPeerCard",
|
||||
"CultMeshPeerCatalog",
|
||||
"CultMeshSimulationFact",
|
||||
"CultMeshSimulationFactCommit",
|
||||
"CultMeshSimulationFactCommitter",
|
||||
"CultMeshAuthorityLease",
|
||||
"CultMeshAuthorityLeaseCatalog",
|
||||
"CultMeshDiscoveryClient",
|
||||
|
|
@ -42,11 +53,14 @@ __all__ = [
|
|||
"CultMeshVerseDescriptor",
|
||||
"PEER_EXCHANGE_REQUEST",
|
||||
"PEER_EXCHANGE_RESPONSE",
|
||||
"SIMULATION_FACT_DOCUMENT_TYPE",
|
||||
"SIMULATION_FACT_SCHEMA_VERSION",
|
||||
"VERSE_CATALOG_REQUEST",
|
||||
"VERSE_CATALOG_RESPONSE",
|
||||
"create_node",
|
||||
"peer_from_wire",
|
||||
"peer_exchange_request",
|
||||
"simulation_fact_document",
|
||||
"verse_catalog_request",
|
||||
"verse_from_wire",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||
from pathlib import Path
|
||||
|
||||
from .node import CultMeshNode, create_node
|
||||
from .simulation import CultMeshSimulationFactCommitter
|
||||
from .wire import (
|
||||
CultMeshAuthorityLeaseCatalog,
|
||||
CultMeshPeerCatalog,
|
||||
|
|
@ -35,3 +36,7 @@ class CultMesh:
|
|||
@staticmethod
|
||||
def create_stream_catalog() -> CultMeshStreamCatalog:
|
||||
return CultMeshStreamCatalog()
|
||||
|
||||
@staticmethod
|
||||
def create_simulation_fact_committer(node: CultMeshNode) -> CultMeshSimulationFactCommitter:
|
||||
return CultMeshSimulationFactCommitter(node)
|
||||
|
|
|
|||
135
packages/cultcache-py/src/cultmesh_py/simulation.py
Normal file
135
packages/cultcache-py/src/cultmesh_py/simulation.py
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
import hashlib
|
||||
from typing import Any
|
||||
|
||||
from cultcache_py import define_database_entry_type
|
||||
from cultcache_py.documents import DocumentDefinition
|
||||
|
||||
from .node import CultMeshNode
|
||||
|
||||
SIMULATION_FACT_DOCUMENT_TYPE = "gamecult.mesh.simulation_fact"
|
||||
SIMULATION_FACT_SCHEMA_VERSION = "gamecult.mesh.simulation_fact.v1"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CultMeshSimulationFact:
|
||||
fact_id: str
|
||||
shard_id: str
|
||||
shard_epoch: int
|
||||
frame: int
|
||||
subject_id: str
|
||||
claim_kind: str
|
||||
claim_hash: str
|
||||
claim_summary: str | None
|
||||
witness_count: int
|
||||
support_weight: float
|
||||
total_weight: float
|
||||
confidence: float
|
||||
committed_at: str
|
||||
|
||||
@staticmethod
|
||||
def compute_fact_id(candidate: dict[str, Any]) -> str:
|
||||
canonical = "\x1f".join([
|
||||
_candidate_string(candidate, "shardId", "shard_id"),
|
||||
str(_candidate_int(candidate, "shardEpoch", "shard_epoch")),
|
||||
str(_candidate_int(candidate, "frame", "frame")),
|
||||
_candidate_string(candidate, "subjectId", "subject_id"),
|
||||
_candidate_string(candidate, "claimKind", "claim_kind"),
|
||||
])
|
||||
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||
|
||||
@staticmethod
|
||||
def create_record_key(candidate: dict[str, Any]) -> str:
|
||||
return f"simulation:{CultMeshSimulationFact.compute_fact_id(candidate)}"
|
||||
|
||||
@staticmethod
|
||||
def from_candidate(candidate: dict[str, Any], *, committed_at: str | None = None) -> "CultMeshSimulationFact":
|
||||
return CultMeshSimulationFact(
|
||||
fact_id=CultMeshSimulationFact.compute_fact_id(candidate),
|
||||
shard_id=_candidate_string(candidate, "shardId", "shard_id"),
|
||||
shard_epoch=_candidate_int(candidate, "shardEpoch", "shard_epoch"),
|
||||
frame=_candidate_int(candidate, "frame", "frame"),
|
||||
subject_id=_candidate_string(candidate, "subjectId", "subject_id"),
|
||||
claim_kind=_candidate_string(candidate, "claimKind", "claim_kind"),
|
||||
claim_hash=_candidate_string(candidate, "claimHash", "claim_hash"),
|
||||
claim_summary=_candidate_optional_string(candidate, "claimSummary", "claim_summary"),
|
||||
witness_count=_candidate_int(candidate, "witnessCount", "witness_count"),
|
||||
support_weight=_candidate_float(candidate, "supportWeight", "support_weight"),
|
||||
total_weight=_candidate_float(candidate, "totalWeight", "total_weight"),
|
||||
confidence=_candidate_float(candidate, "confidence", "confidence"),
|
||||
committed_at=committed_at or datetime.now(UTC).isoformat(),
|
||||
)
|
||||
|
||||
|
||||
simulation_fact_document: DocumentDefinition[CultMeshSimulationFact] = define_database_entry_type(
|
||||
SIMULATION_FACT_DOCUMENT_TYPE,
|
||||
[
|
||||
("fact_id", 0),
|
||||
("shard_id", 1),
|
||||
("shard_epoch", 2),
|
||||
("frame", 3),
|
||||
("subject_id", 4),
|
||||
("claim_kind", 5),
|
||||
("claim_hash", 6),
|
||||
("claim_summary", 7, None),
|
||||
("witness_count", 8),
|
||||
("support_weight", 9),
|
||||
("total_weight", 10),
|
||||
("confidence", 11),
|
||||
("committed_at", 12),
|
||||
],
|
||||
cls=CultMeshSimulationFact,
|
||||
name="fact_id",
|
||||
indexes={"shard": "shard_id", "frame": "frame", "subject": "subject_id", "claim_kind": "claim_kind"},
|
||||
schema_name=SIMULATION_FACT_DOCUMENT_TYPE,
|
||||
schema_version=SIMULATION_FACT_SCHEMA_VERSION,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CultMeshSimulationFactCommit:
|
||||
key: str
|
||||
fact: CultMeshSimulationFact
|
||||
|
||||
|
||||
@dataclass
|
||||
class CultMeshSimulationFactCommitter:
|
||||
node: CultMeshNode
|
||||
document: DocumentDefinition[CultMeshSimulationFact] = simulation_fact_document
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if all(document.type != self.document.type for document in self.node.documents):
|
||||
self.node.register_document(self.document)
|
||||
|
||||
def commit(self, candidate: dict[str, Any], *, committed_at: str | None = None) -> CultMeshSimulationFactCommit:
|
||||
if not bool(candidate.get("hasQuorum") if "hasQuorum" in candidate else candidate.get("has_quorum")):
|
||||
raise ValueError("Simulation candidate cannot be committed before quorum")
|
||||
key = CultMeshSimulationFact.create_record_key(candidate)
|
||||
fact = CultMeshSimulationFact.from_candidate(candidate, committed_at=committed_at)
|
||||
self.node.put(self.document, key, fact)
|
||||
return CultMeshSimulationFactCommit(key=key, fact=fact)
|
||||
|
||||
|
||||
def _candidate_string(candidate: dict[str, Any], camel: str, snake: str) -> str:
|
||||
value = candidate.get(camel, candidate.get(snake, ""))
|
||||
if value is None:
|
||||
return ""
|
||||
return str(value)
|
||||
|
||||
|
||||
def _candidate_optional_string(candidate: dict[str, Any], camel: str, snake: str) -> str | None:
|
||||
value = candidate.get(camel, candidate.get(snake))
|
||||
if value is None:
|
||||
return None
|
||||
return str(value)
|
||||
|
||||
|
||||
def _candidate_int(candidate: dict[str, Any], camel: str, snake: str) -> int:
|
||||
return int(candidate.get(camel, candidate.get(snake, 0)))
|
||||
|
||||
|
||||
def _candidate_float(candidate: dict[str, Any], camel: str, snake: str) -> float:
|
||||
return float(candidate.get(camel, candidate.get(snake, 0.0)))
|
||||
|
|
@ -46,6 +46,7 @@ from cultmesh_py import (
|
|||
CultMesh,
|
||||
CultMeshPeerCard,
|
||||
CultMeshPeerCatalog,
|
||||
CultMeshSimulationFact,
|
||||
CultMeshAuthorityLease,
|
||||
CultMeshAuthorityLeaseCatalog,
|
||||
CultMeshDiscoveryClient,
|
||||
|
|
@ -57,6 +58,7 @@ from cultmesh_py import (
|
|||
CultMeshVerseCompatibility,
|
||||
CultMeshVerseDescriptor,
|
||||
peer_exchange_request,
|
||||
simulation_fact_document,
|
||||
verse_catalog_request,
|
||||
)
|
||||
|
||||
|
|
@ -795,6 +797,75 @@ class CultCacheTests(unittest.TestCase):
|
|||
self.assertIsInstance(CultMesh.create_authority_lease_catalog(), CultMeshAuthorityLeaseCatalog)
|
||||
self.assertIsInstance(CultMesh.create_stream_catalog(), CultMeshStreamCatalog)
|
||||
|
||||
def test_cultmesh_simulation_fact_uses_csharp_slot_contract(self) -> None:
|
||||
import msgpack # type: ignore
|
||||
|
||||
candidate = {
|
||||
"shardId": "arena",
|
||||
"shardEpoch": 4,
|
||||
"frame": 100,
|
||||
"subjectId": "bob",
|
||||
"claimKind": "hit",
|
||||
"claimHash": compute_simulation_claim_hash("hit", "alice", "bob", "frame:100"),
|
||||
"claimSummary": "alice shot bob first",
|
||||
"witnessCount": 2,
|
||||
"supportWeight": 2.0,
|
||||
"totalWeight": 2.0,
|
||||
"confidence": 1.0,
|
||||
"hasQuorum": True,
|
||||
}
|
||||
|
||||
fact = CultMeshSimulationFact.from_candidate(candidate, committed_at="2026-06-13T00:00:00Z")
|
||||
payload = simulation_fact_document.encode_payload(fact)
|
||||
slots = msgpack.unpackb(payload, raw=False)
|
||||
|
||||
self.assertEqual(CultMeshSimulationFact.create_record_key(candidate), f"simulation:{fact.fact_id}")
|
||||
self.assertEqual(slots[0], fact.fact_id)
|
||||
self.assertEqual(slots[1], "arena")
|
||||
self.assertEqual(slots[2], 4)
|
||||
self.assertEqual(slots[3], 100)
|
||||
self.assertEqual(slots[4], "bob")
|
||||
self.assertEqual(slots[5], "hit")
|
||||
self.assertEqual(slots[6], candidate["claimHash"])
|
||||
self.assertEqual(slots[7], "alice shot bob first")
|
||||
self.assertEqual(slots[8], 2)
|
||||
self.assertEqual(slots[9], 2.0)
|
||||
self.assertEqual(slots[10], 2.0)
|
||||
self.assertEqual(slots[11], 1.0)
|
||||
self.assertEqual(slots[12], "2026-06-13T00:00:00Z")
|
||||
|
||||
def test_cultmesh_simulation_fact_committer_rejects_without_quorum_and_stores_fact(self) -> None:
|
||||
candidate = {
|
||||
"shardId": "arena",
|
||||
"shardEpoch": 4,
|
||||
"frame": 100,
|
||||
"subjectId": "bob",
|
||||
"claimKind": "hit",
|
||||
"claimHash": compute_simulation_claim_hash("hit", "alice", "bob", "frame:100"),
|
||||
"claimSummary": "alice shot bob first",
|
||||
"witnessCount": 2,
|
||||
"supportWeight": 2.0,
|
||||
"totalWeight": 2.0,
|
||||
"confidence": 1.0,
|
||||
"hasQuorum": True,
|
||||
}
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
node = CultMesh.create_node(Path(tmp) / "facts.cc", runtime_id="mesh-facts")
|
||||
committer = CultMesh.create_simulation_fact_committer(node)
|
||||
|
||||
rejected = dict(candidate)
|
||||
rejected["hasQuorum"] = False
|
||||
with self.assertRaisesRegex(ValueError, "before quorum"):
|
||||
committer.commit(rejected)
|
||||
|
||||
committed = committer.commit(candidate, committed_at="2026-06-13T00:00:00Z")
|
||||
stored = node.get_required(simulation_fact_document, committed.key)
|
||||
|
||||
self.assertEqual(stored.claim_hash, candidate["claimHash"])
|
||||
self.assertEqual(stored.committed_at, "2026-06-13T00:00:00Z")
|
||||
self.assertEqual(committed.fact.fact_id, stored.fact_id)
|
||||
|
||||
def test_cultmesh_verse_catalog_response_matches_schema_v0_wire_shape(self) -> None:
|
||||
import msgpack # type: ignore
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue