diff --git a/packages/cultcache-py/PARITY.md b/packages/cultcache-py/PARITY.md index 886449b2..e3a2fd79 100644 --- a/packages/cultcache-py/PARITY.md +++ b/packages/cultcache-py/PARITY.md @@ -83,7 +83,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. - Python CultMesh facade: Exposes C#-matching client entrypoints for Verse discovery, peer exchange, raw CultNet client construction, local nodes, catalogs, sessions, streams, - authority leases, local TCP node serving, and simulation fact commits. + authority leases, local TCP node serving, simulation fact commits, and the + durable shard-log node option that attaches a file-backed authoritative log + store beside the cache file by default. - Python CultMesh authority leases: Lease objects own validity and peer/role/shard coverage checks; the catalog owns sorted listing, lookup, upsert, and authorization delegation, matching @@ -186,5 +188,6 @@ node --test packages\cultnet-ts\dist-test\test\interop\cultnet-interop.test.js catch-up with snapshot recovery, a file-backed replica cursor store, and schema-v0 write forwarding. It also has an explicit package-level background pull loop for caller-provided shard descriptors. Its authoritative shard-log - store is file-backed and supports compaction watermarks, but it is still + store is file-backed, supports compaction watermarks, and can be attached by + `CultMeshNodeOptions(enable_durable_shard_logs=True)`, but it is still package-local rather than the full C# daemon/server stack. diff --git a/packages/cultcache-py/README.md b/packages/cultcache-py/README.md index fde545a4..327bbe6b 100644 --- a/packages/cultcache-py/README.md +++ b/packages/cultcache-py/README.md @@ -226,7 +226,6 @@ from cultcache_py import define_database_entry_type from cultnet_py import ( CultNetClientAuthorityScope, CultNetFileShardReplicaCursorStore, - CultNetFileShardMutationLogStore, CultNetRawClient, CultNetSchemaShardLogFetcher, CultNetSchemaShardSnapshotFetcher, @@ -234,7 +233,7 @@ from cultnet_py import ( CultNetShardReplicator, CultNetShardReplicatorOptions, ) -from cultmesh_py import CultMesh, CultMeshGameSessionOptions, peer_exchange_request +from cultmesh_py import CultMesh, CultMeshGameSessionOptions, CultMeshNodeOptions, peer_exchange_request note_doc = define_database_entry_type( "mesh.note", @@ -242,9 +241,12 @@ note_doc = define_database_entry_type( name="name", indexes={"kind": "kind"}, ) -node = CultMesh.start_node("mesh.cc", runtime_id="python-runtime") +node = CultMesh.start_node( + "mesh.cc", + runtime_id="python-runtime", + options=CultMeshNodeOptions(enable_durable_shard_logs=True), +) node.database.register_document(note_doc) -node.database.use_shard_mutation_log_store(CultNetFileShardMutationLogStore("mesh.shard-logs")) unsubscribe = node.database.watch_record(note_doc, "note:1", lambda change: print(change.change_kind)) unsubscribe_named = node.database.watch_by_name(note_doc, "intro", lambda change: print(change.record_key)) unsubscribe_kind = node.database.watch_by_index(note_doc, "kind", "guide", lambda change: print(change.record_key)) diff --git a/packages/cultcache-py/src/cultcache_py/verify.py b/packages/cultcache-py/src/cultcache_py/verify.py index 7bb5eb28..4c0f7d95 100644 --- a/packages/cultcache-py/src/cultcache_py/verify.py +++ b/packages/cultcache-py/src/cultcache_py/verify.py @@ -100,6 +100,7 @@ EXPECTED_EXPORTS: dict[str, tuple[str, ...]] = { "CultMeshGameSessionOptions", "CultMeshLocalServer", "CultMeshNode", + "CultMeshNodeOptions", "CultMeshPeerCard", "CultMeshPeerCatalog", "CultMeshPeerExchangeClient", diff --git a/packages/cultcache-py/src/cultmesh_py/__init__.py b/packages/cultcache-py/src/cultmesh_py/__init__.py index 3d1c779e..3815d370 100644 --- a/packages/cultcache-py/src/cultmesh_py/__init__.py +++ b/packages/cultcache-py/src/cultmesh_py/__init__.py @@ -1,6 +1,6 @@ from .client import CultMeshDiscoveryClient, CultMeshPeerExchangeClient, CultMeshVerseDiscoveryClient from .facade import CultMesh -from .node import CultMeshDatabase, CultMeshDatabaseChange, CultMeshNode, create_node +from .node import CultMeshDatabase, CultMeshDatabaseChange, CultMeshNode, CultMeshNodeOptions, create_node from .server import CultMeshLocalServer from .session import ( CultMeshGameSession, @@ -41,6 +41,7 @@ from .wire import ( __all__ = [ "CultMeshNode", + "CultMeshNodeOptions", "CultMeshDatabase", "CultMeshDatabaseChange", "CultMesh", diff --git a/packages/cultcache-py/src/cultmesh_py/facade.py b/packages/cultcache-py/src/cultmesh_py/facade.py index 1e5aa062..1f4320b9 100644 --- a/packages/cultcache-py/src/cultmesh_py/facade.py +++ b/packages/cultcache-py/src/cultmesh_py/facade.py @@ -5,7 +5,7 @@ from pathlib import Path from cultnet_py import CultNetRawClient from .client import CultMeshPeerExchangeClient, CultMeshVerseDiscoveryClient -from .node import CultMeshNode, create_node +from .node import CultMeshNode, CultMeshNodeOptions, create_node from .server import CultMeshLocalServer from .session import CultMeshGameSession, CultMeshGameSessionOptions from .simulation import CultMeshSimulationFactCommitter @@ -19,12 +19,38 @@ from .wire import ( class CultMesh: @staticmethod - def create_node(cache_path: str | Path | None = None, *, runtime_id: str = "python-runtime") -> CultMeshNode: - return create_node(cache_path, runtime_id=runtime_id) + def create_node( + cache_path: str | Path | None = None, + *, + runtime_id: str = "python-runtime", + options: CultMeshNodeOptions | None = None, + enable_durable_shard_logs: bool | None = None, + shard_log_path: str | Path | None = None, + ) -> CultMeshNode: + return create_node( + cache_path, + runtime_id=runtime_id, + options=options, + enable_durable_shard_logs=enable_durable_shard_logs, + shard_log_path=shard_log_path, + ) @staticmethod - def start_node(cache_path: str | Path | None = None, *, runtime_id: str = "python-runtime") -> CultMeshNode: - return create_node(cache_path, runtime_id=runtime_id) + def start_node( + cache_path: str | Path | None = None, + *, + runtime_id: str = "python-runtime", + options: CultMeshNodeOptions | None = None, + enable_durable_shard_logs: bool | None = None, + shard_log_path: str | Path | None = None, + ) -> CultMeshNode: + return create_node( + cache_path, + runtime_id=runtime_id, + options=options, + enable_durable_shard_logs=enable_durable_shard_logs, + shard_log_path=shard_log_path, + ) @staticmethod def create_verse_catalog() -> CultMeshVerseCatalog: diff --git a/packages/cultcache-py/src/cultmesh_py/node.py b/packages/cultcache-py/src/cultmesh_py/node.py index 156123d9..b567ca82 100644 --- a/packages/cultcache-py/src/cultmesh_py/node.py +++ b/packages/cultcache-py/src/cultmesh_py/node.py @@ -9,6 +9,7 @@ from cultcache_py import CultCache, CultCacheEnvelope, SingleFileMessagePackBack from cultcache_py.documents import DocumentDefinition, extract_value from cultnet_py import ( CultNetAppliedRecord, + CultNetFileShardMutationLogStore, CultNetMessage, CultNetRawClient, CultNetRawDocumentRecord, @@ -220,6 +221,12 @@ class CultMeshNode: ) +@dataclass(frozen=True) +class CultMeshNodeOptions: + enable_durable_shard_logs: bool = False + shard_log_path: str | Path | None = None + + class CultMeshDatabase: def __init__(self, node: CultMeshNode) -> None: self._node = node @@ -886,8 +893,54 @@ class CultMeshDatabase: return previous -def create_node(cache_path: str | Path | None = None, *, runtime_id: str = "python-runtime") -> CultMeshNode: +def create_node( + cache_path: str | Path | None = None, + *, + runtime_id: str = "python-runtime", + options: CultMeshNodeOptions | None = None, + enable_durable_shard_logs: bool | None = None, + shard_log_path: str | Path | None = None, +) -> CultMeshNode: + resolved_options = _resolve_node_options( + options, + enable_durable_shard_logs=enable_durable_shard_logs, + shard_log_path=shard_log_path, + ) cache = CultCache() if cache_path is not None: cache.add_generic_store(SingleFileMessagePackBackingStore(cache_path)) - return CultMeshNode(cache=cache, runtime_id=runtime_id) + node = CultMeshNode(cache=cache, runtime_id=runtime_id) + if resolved_options.enable_durable_shard_logs: + node.database.use_shard_mutation_log_store(CultNetFileShardMutationLogStore( + _resolve_shard_log_path(cache_path, resolved_options.shard_log_path) + )) + return node + + +def _resolve_node_options( + options: CultMeshNodeOptions | None, + *, + enable_durable_shard_logs: bool | None, + shard_log_path: str | Path | None, +) -> CultMeshNodeOptions: + if options is None: + return CultMeshNodeOptions( + enable_durable_shard_logs=enable_durable_shard_logs is True, + shard_log_path=shard_log_path, + ) + return CultMeshNodeOptions( + enable_durable_shard_logs=options.enable_durable_shard_logs + if enable_durable_shard_logs is None + else enable_durable_shard_logs, + shard_log_path=shard_log_path if shard_log_path is not None else options.shard_log_path, + ) + + +def _resolve_shard_log_path(cache_path: str | Path | None, shard_log_path: str | Path | None) -> Path: + if shard_log_path is not None: + return Path(shard_log_path) + if cache_path is None: + raise ValueError("cache_path or shard_log_path is required when durable shard logs are enabled") + path = Path(cache_path) + directory = path.parent if path.parent != Path("") else Path.cwd() + return directory / f"{path.stem}.cultmesh" / "shard-logs" diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index cb638184..d72fd096 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -80,6 +80,7 @@ from cultmesh_py import ( CultMeshDatabase, CultMeshDatabaseChange, CultMeshGameSessionOptions, + CultMeshNodeOptions, CultMeshPeerCard, CultMeshPeerCatalog, CultMeshSimulationFact, @@ -1206,6 +1207,37 @@ class CultCacheTests(unittest.TestCase): reopened.database.pull() self.assertEqual(reopened.database.get(document, "note:1")["body"], "hello") + def test_cultmesh_node_options_attach_default_durable_shard_log_store(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + document = define_database_entry_type( + "mesh.durable_option_note", + [("body", 0)], + schema_id="mesh.durable_option_note.v1", + ) + cache_path = Path(tmp) / "world.cc" + node = CultMesh.create_node( + cache_path, + runtime_id="durable-option-primary", + options=CultMeshNodeOptions(enable_durable_shard_logs=True), + ) + node.database.register_document(document) + node.database.put_raw_message(document, "note:1", {"body": "stored"}, shard_id="notes", shard_epoch=5) + + log_path = Path(tmp) / "world.cultmesh" / "shard-logs" + self.assertTrue(log_path.exists()) + + reopened = CultMesh.start_node( + cache_path, + runtime_id="durable-option-primary", + enable_durable_shard_logs=True, + ) + reopened.database.register_document(document) + response = reopened.database.build_shard_log_response(shard_id="notes", shard_epoch=5) + + self.assertEqual(reopened.database.shard_ids(), ["notes"]) + self.assertEqual([entry.sequence for entry in response.entries], [1]) + self.assertEqual(response.entries[0].raw_document.record_key, "note:1") + def test_cultmesh_database_watchers_observe_local_changes_and_unsubscribe(self) -> None: document = define_database_entry_type("mesh.watch", [("body", 0)]) node = CultMesh.create_node(runtime_id="mesh-watch")