diff --git a/packages/cultcache-py/CHANGELOG.md b/packages/cultcache-py/CHANGELOG.md index 1b950aa8..30b60850 100644 --- a/packages/cultcache-py/CHANGELOG.md +++ b/packages/cultcache-py/CHANGELOG.md @@ -4,7 +4,7 @@ ### Changed -- `parse_stored_at` (was `_parse_stored_at`) is public: cultmesh-py judges a peer's `storedAt` by it. A peer's `document_put_raw` whose `storedAt` is missing, empty or not a stamp is refused `message_invalid` (field `document`) and nothing is minted or stored (variants:ruling:raw-put-without-stamp-refused). +- `parse_stored_at` (was `_parse_stored_at`) is public: cultmesh-py judges a peer's `storedAt` by it. A peer's `document_put_raw` whose `storedAt` is missing, empty or not a stamp is refused `message_invalid` (field `document`) and nothing is minted or stored (variants:ruling:raw-put-without-stamp-refused). A subscribed server's stampless put now raises `ValueError` from `CultMeshClient.read_next_change` instead of being stored under a minted stamp. ### Added diff --git a/packages/cultmesh-py/src/cultmesh_py/node.py b/packages/cultmesh-py/src/cultmesh_py/node.py index 994bc4b6..ff6ef8f2 100644 --- a/packages/cultmesh-py/src/cultmesh_py/node.py +++ b/packages/cultmesh-py/src/cultmesh_py/node.py @@ -665,10 +665,10 @@ class CultMeshDatabase: ): return None try: - prepared = self._raw_put_envelope(message) + document = self._raw_put_document(message) except Exception: # noqa: BLE001 - any other malformed shape return None - return None if prepared is None else (prepared[0].catalog_entry().schema_id, prepared[1].key) + return None if document is None else (document.catalog_entry().schema_id, document_record["recordKey"]) def raw_delete_target(self, message: dict[str, Any]) -> tuple[str, str] | None: """The canonical schema id and record key a raw delete names, or None when its schema is not registered.""" @@ -707,21 +707,27 @@ class CultMeshDatabase: documents=(self._served_record(prepared[1]),), ).to_wire() - def _raw_put_envelope( - self, message: dict[str, Any] - ) -> tuple[DocumentDefinition[Any], CultCacheEnvelope] | None: + def _raw_put_document(self, message: dict[str, Any]) -> DocumentDefinition[Any] | None: + """The registered document a raw put names, or None when its record is not a map or its schema is not registered.""" document_record = message.get("document") if not isinstance(document_record, dict): return None + schema_id = str(document_record.get("schemaId") or "") + try: + return resolve_document_for_raw_record(schema_document_map(self.documents), schema_id, document_record) + except KeyError: + return None + + def _raw_put_envelope( + self, message: dict[str, Any] + ) -> tuple[DocumentDefinition[Any], CultCacheEnvelope] | None: + document = self._raw_put_document(message) + if document is None: + return None + document_record = message["document"] # A peer's put is stored under the stamp it carries, so one that is no stamp is refused: nothing is minted for it. if parse_stored_at(document_record.get("storedAt")) is None: raise ValueError("A raw put must carry a storedAt stamp.") - schema_id = str(document_record.get("schemaId") or "") - documents_by_schema_id = schema_document_map(self.documents) - try: - document = resolve_document_for_raw_record(documents_by_schema_id, schema_id, document_record) - except KeyError: - return None # One id: the local schema id, under which the cache stores the record. return document, CultCacheEnvelope( key=str(document_record["recordKey"]), diff --git a/packages/cultmesh-py/tests/test_remote_write_authority.py b/packages/cultmesh-py/tests/test_remote_write_authority.py index 9a2f6fc8..7397577d 100644 --- a/packages/cultmesh-py/tests/test_remote_write_authority.py +++ b/packages/cultmesh-py/tests/test_remote_write_authority.py @@ -102,19 +102,30 @@ class StamplessPutTests(unittest.TestCase): node = create_node(Path(directory) / "node.cc", runtime_id="stampless-server") node.database.register_document(NOTE) server = CultMeshLocalServer(node=node, port=0, write_authority=CultMeshGrantAllWrites()) + sized = CultMeshLocalServer(node=node, port=0, write_authority=CultMeshGrantAllWrites(), max_snapshot_bytes=10**6) + rudp = CultMeshLocalServer(node=node, port=0, write_authority=CultMeshGrantAllWrites()) for label, stamp in (("missing", None), ("empty", ""), ("garbage", "yesterday"), ("no zone", "2026-11-07T00:00:00")): with self.subTest(label): message = put_of(KEY_A, "peer") del message["document"]["storedAt"] if stamp is not None: message["document"]["storedAt"] = stamp - [refusal] = server._handle_connection_message(message, {}, peer=PEER) - self.assertEqual(refusal["code"], "message_invalid") - self.assertEqual(refusal["details"]["field"], "document") + # The size-limited and RUDP paths measure the document as a snapshot would serve it, so they must refuse first. + for limited in (server, sized, rudp): + [refusal] = limited._handle_connection_message(message, {}, over_rudp=limited is rudp, peer=PEER) + self.assertEqual(refusal["code"], "message_invalid") + self.assertEqual(refusal["details"]["field"], "document") with self.assertRaises(ValueError): node.database.apply_raw_put_message(message) self.assertIsNone(node.cache.get(NOTE, KEY_A)) self.assertNotIn(SHARD_ID, node.database._shard_logs) + # An aliased put with a bad stamp still asks the authority about the canonical id, and is refused before the stamp is judged. + authority = Recording(lambda request: CultMeshRemoteWriteDecision.refuse("no")) + guarded = CultMeshLocalServer(node=node, port=0, write_authority=authority) + aliased = put_of(KEY_A, "peer", schema_id=ALIAS, stored_at="yesterday") + [refusal] = guarded._handle_connection_message(aliased, {}, peer=PEER) + self.assertEqual(refusal["code"], "write_not_authorized") + self.assertEqual(authority.requests[0].schema_id, SCHEMA_ID) # The refused puts left the file readable: the next put of any key is stored. replies = server._handle_connection_message(put_of(KEY_B, "stamped"), {}, peer=PEER) self.assertEqual(replies, [])