diff --git a/packages/cultcache-py/src/cultcache_py/stores.py b/packages/cultcache-py/src/cultcache_py/stores.py index c2c3352f..f081417c 100644 --- a/packages/cultcache-py/src/cultcache_py/stores.py +++ b/packages/cultcache-py/src/cultcache_py/stores.py @@ -776,15 +776,15 @@ def _schema_id_for(envelope: CultCacheEnvelope) -> str: _STORED_AT_SHAPE = re.compile( - r"([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?(?:(Z)|([+-])([0-9]{2}):([0-9]{2}))" + r"([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?(?:(Z)|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))" ) def _parse_stored_at(text: Any) -> datetime | None: """The UTC instant a stored_at text names, or None. Only the shape the runtimes write is a stamp: an RFC 3339 date-time with T, - an optional fraction of 1 to 9 digits (cut to the microsecond) and Z or a numeric offset. Checked here, not left to + an optional fraction of 1 to 9 digits (cut to the microsecond) and Z or a numeric offset of hours 00-23 and minutes 00-59. Checked here, not left to fromisoformat, whose accepted set differs by interpreter (3.10 and 3.13) and from Rust and TypeScript. An instant outside - datetime's range, or an offset of a day or more, is not a stamp.""" + datetime's range is not a stamp.""" if not isinstance(text, str): return None shape = _STORED_AT_SHAPE.fullmatch(text) diff --git a/packages/cultcache-py/tests/test_update.py b/packages/cultcache-py/tests/test_update.py index 78725ce3..34f030a8 100644 --- a/packages/cultcache-py/tests/test_update.py +++ b/packages/cultcache-py/tests/test_update.py @@ -641,7 +641,8 @@ class StoreRestampTests(Fixture): def test_only_the_stamp_shape_the_runtimes_write_parses(self) -> None: """An RFC 3339 date-time with T, a fraction of 1 to 9 digits and Z or a numeric offset, on every interpreter (the shape is - checked here, not by fromisoformat): Rust and TypeScript read the same set.""" + checked here, not by fromisoformat), so a stamp reads the same on 3.10 and 3.13. The edges follow RFC 3339: offset hours 00-23, + minutes 00-59.""" utc = timezone.utc accepted = { "2026-01-01T00:00:00Z": datetime(2026, 1, 1, tzinfo=utc), @@ -650,6 +651,7 @@ class StoreRestampTests(Fixture): "2026-01-01T00:00:00.1234567+00:00": datetime(2026, 1, 1, 0, 0, 0, 123456, utc), "2026-01-01T05:30:00+05:30": datetime(2026, 1, 1, tzinfo=utc), "2025-12-31T19:00:00-05:00": datetime(2026, 1, 1, tzinfo=utc), + "2026-01-01T23:59:00+23:59": datetime(2026, 1, 1, 0, 0, tzinfo=utc), } for text, instant in accepted.items(): with self.subTest(text=text): @@ -658,7 +660,8 @@ class StoreRestampTests(Fixture): "2026-01-01", "20260101T000000Z", "2026-W01-1T00:00:00Z", "2026-01-01 00:00:00Z", "2026-01-01T00:00:00,5Z", "2026-01-01T00Z", "2026-01-01T00:00:00", "2026-01-01T00:00:00.Z", "2026-01-01T00:00:00.1234567890Z", "2026-01-01T00:00:00z", " 2026-01-01T00:00:00Z", "2026-01-01T00:00:00Z ", "2026-01-01T24:00:00Z", - "2026-13-01T00:00:00Z", "2026-01-01T00:00:00+24:00", "2026-01-01T00:00:00+0000", "\u0662026-01-01T00:00:00Z", + "2026-13-01T00:00:00Z", "2026-01-01T00:00:00+24:00", "2026-01-01T00:00:00+05:60", "2026-01-01T00:00:00+05:75", "2026-01-01T00:00Z", "2026-01-01T00:00+00:00", + "026-01-01T00:00:00Z", "12026-01-01T00:00:00Z", "2026-01-01T00:00:00+5:00", "2026-01-01T00:00:00+0000", "\u0662026-01-01T00:00:00Z", "", "garbage", ): with self.subTest(text=text): diff --git a/packages/cultmesh-py/src/cultmesh_py/node.py b/packages/cultmesh-py/src/cultmesh_py/node.py index 41cd4aec..5ad917ae 100644 --- a/packages/cultmesh-py/src/cultmesh_py/node.py +++ b/packages/cultmesh-py/src/cultmesh_py/node.py @@ -685,10 +685,11 @@ class CultMeshDatabase: value = self.cache.put_envelope(document, envelope) change_kind = "added" if previous is None else "updated" change = self._publish_local_change(document, record_key, change_kind, value, previous) - self._append_shard_log_put(CultNetMessage( - "cultnet.document_put_raw.v0", - {key: value for key, value in message.items() if key != "schemaVersion"}, - ), change_kind) + # The log carries the stamp the cache holds, which a replacing put mints later than the record it replaces, not the peer's. + stored_at = self.cache.get_required_envelope(document, record_key).stored_at + logged = {key: value for key, value in message.items() if key != "schemaVersion"} + logged["document"] = {**message["document"], "storedAt": stored_at} + self._append_shard_log_put(CultNetMessage("cultnet.document_put_raw.v0", logged), change_kind) return change def raw_put_served_alone(self, message: dict[str, Any], *, message_id: str) -> dict[str, Any] | None: diff --git a/packages/cultmesh-py/tests/test_cultmesh.py b/packages/cultmesh-py/tests/test_cultmesh.py index a1e0c35c..4595f29c 100644 --- a/packages/cultmesh-py/tests/test_cultmesh.py +++ b/packages/cultmesh-py/tests/test_cultmesh.py @@ -822,6 +822,26 @@ class CultMeshTests(unittest.TestCase): self.assertEqual(wire["document"]["storedAt"], held) self.assertEqual(node.database._shard_logs["mesh"][-1]["put"]["document"]["storedAt"], held) + def test_applied_raw_put_logs_the_stamp_the_cache_holds(self) -> None: + document = define_database_entry_type("mesh.applied_stamp", [("name", 0), ("category", 1), ("value", 2, 0)], cls=Item) + node = create_node(runtime_id="python-node") + node.register_document(document) + cache = node.database.cache + entry = document.catalog_entry() + + def envelope(value: int, stored_at: str) -> CultCacheEnvelope: + return CultCacheEnvelope("item:applied", document.type, document.encode_payload(Item("n", "gear", value)), stored_at, entry.schema_id, entry) + + cache.put_envelope(document, envelope(1, "2099-01-01T00:00:00.000000+00:00")) + peer = node.put_raw_message(document, "item:peer", Item("peer", "gear", 2)).to_wire() # a well-formed put to replay + peer["document"] = {**peer["document"], "recordKey": "item:applied", "storedAt": "2000-01-01T00:00:00Z", + "payload": envelope(2, "x").payload} + peer["shardId"], peer["shardEpoch"] = "mesh", 1 + node.database.apply_raw_put_message(peer) + held = cache.get_required_envelope(document, "item:applied").stored_at + self.assertGreater(held, "2099-01-01T00:00:00.000000+00:00") + self.assertEqual(node.database._shard_logs["mesh"][-1]["put"]["document"]["storedAt"], held) + def test_cultmesh_node_emits_raw_put_and_delete_messages_for_local_writes(self) -> None: document = define_database_entry_type( "mesh.emit_item",