From 03a6613eb9ec655f076946cd91f03fd1abe3c25b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 01:44:23 +0100 Subject: [PATCH] cultmesh-py raw put stamps from the cache clock; store-less cache restamps nothing; docs-snippet test reads CRLF Co-Authored-By: Claude Sonnet 5.5 --- packages/cultcache-py/CHANGELOG.md | 4 +-- .../cultcache-py/src/cultcache_py/cache.py | 13 +++------- packages/cultcache-py/tests/test_clock.py | 26 +++++++++++++------ packages/cultcache-py/tests/test_update.py | 10 +++++-- packages/cultcache-ts/test/first-call.test.ts | 2 +- packages/cultmesh-py/src/cultmesh_py/node.py | 1 + packages/cultmesh-py/tests/test_cultmesh.py | 17 ++++++++++++ 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/packages/cultcache-py/CHANGELOG.md b/packages/cultcache-py/CHANGELOG.md index 70b706e0..4ee0efaf 100644 --- a/packages/cultcache-py/CHANGELOG.md +++ b/packages/cultcache-py/CHANGELOG.md @@ -20,8 +20,8 @@ ### Changed -- A store's restamp of a replacing write (`stored_at_over`) reads no clock: the staged stamp when both parse and it is strictly later, else the replaced stamp plus one microsecond (variants cultlib-gaps ruling `sibling-restamp-staged-derived`, as Rust and TypeScript do). A replaced stamp that does not parse leaves the staged stamp as written; before, such a write was stamped at now. `mint_stored_at` is removed. -- A write that replaces a record is stored at a later `stored_at` than the record it replaces, in the single-file store, the JSONL store and a store-less cache, with no exception for identical content (`variants:ruling:py-rs-update-follow-csharp-restamp`); the cache holds the stamp the store wrote. A stored_at is read only in the shape the runtimes write (an RFC 3339 date-time with T, a fraction of 1 to 9 digits, Z or a numeric offset), the same on every Python; any other text, or an instant outside `datetime`'s range, is not a stamp and a write over it is stamped at now. A write over a record stamped at the last instant `datetime` can hold (9999-12-31T23:59:59.999999Z) is refused with `ValueError` and writes nothing. +- A store's restamp of a replacing write (`stored_at_over`) reads no clock: the staged stamp when both parse and it is strictly later, else the replaced stamp plus one microsecond (variants cultlib-gaps ruling `sibling-restamp-staged-derived`, as Rust and TypeScript do). A replaced stamp that does not parse leaves the staged stamp as written; before, such a write was stamped at now. `mint_stored_at` is removed. A store-less cache restamps nothing: a replacing write holds the stamp it was given, as the Rust and TypeScript caches do (it was restamped later before). +- A write that replaces a record is stored at a later `stored_at` than the record it replaces, in the single-file store and the JSONL store, with no exception for identical content (`variants:ruling:py-rs-update-follow-csharp-restamp`); the cache holds the stamp the store wrote. A stored_at is read only in the shape the runtimes write (an RFC 3339 date-time with T, a fraction of 1 to 9 digits, Z or a numeric offset), the same on every Python; any other text, or an instant outside `datetime`'s range, is not a stamp and a write over it is stamped at now. A write over a record stamped at the last instant `datetime` can hold (9999-12-31T23:59:59.999999Z) is refused with `ValueError` and writes nothing. - `store_write_lock` looks for a link at the store path again once the lock is held, so a link put at the path while a writer waited on the lock is refused with `StoreLinkedError` and the lock released, instead of being replaced. `JsonLinesBackingStore` takes no cross-process lock (only its thread lock, under which it already refuses a link), so it has no lock wait to re-check across. Pinned by a test that puts the link while a foreign process holds the lock. - `CultCache.is_writable` does not look at store paths: a link at a store path is not predicted, and the write refuses it with `StoreLinkedError`. Pinned by a test through a cache attached to a linked store. - `JsonLinesBackingStore` writes LF line endings on Windows. It wrote CRLF there, through `write_text` newline translation, before the commit_store_file change (#154, adbbb780). diff --git a/packages/cultcache-py/src/cultcache_py/cache.py b/packages/cultcache-py/src/cultcache_py/cache.py index bdf0480a..aaf21ff5 100644 --- a/packages/cultcache-py/src/cultcache_py/cache.py +++ b/packages/cultcache-py/src/cultcache_py/cache.py @@ -19,7 +19,6 @@ from .stores import ( SingleFileMessagePackBackingStore, parse_stored_at, retyped, - stored_at_over, written_owns, ) @@ -259,7 +258,8 @@ class CultCache: def clock(self, clock: Callable[[], datetime]) -> None: self._clock = clock - def _now(self) -> str: + def now_stamp(self) -> str: + """The stored_at a fresh write is stamped with now, from this cache's clock.""" return self._clock().astimezone(timezone.utc).isoformat() @_mutating @@ -555,7 +555,7 @@ class CultCache: payload=document.encode_payload(value), schema_id=catalog_entry.schema_id, catalog_entry=catalog_entry, - stored_at=self._now(), + stored_at=self.now_stamp(), ) return self._write(document, [(envelope, value)], batch=False, expected=expected) @@ -673,11 +673,6 @@ class CultCache: stamps[records[0][0].key] = store.push(records[0][0]) if legacy_key is not None: self._retire_legacy_global(store, type, legacy_key) - else: - for envelope, _ in records: - held_at = next((held for held in (by_type.get(envelope.key) for by_type in self._state.envelopes.values()) if held is not None), None) - if held_at is not None: - stamps[envelope.key] = stored_at_over(envelope.stored_at, held_at.stored_at) self._state.legacy_global_keys.pop(type, None) values = self._state.values.setdefault(type, {}) envelopes = self._state.envelopes.setdefault(type, {}) @@ -744,7 +739,7 @@ class CultCache: raise CultCacheError(f"Global document {document.type} must use key {GLOBAL_KEY}") self._refuse_unread([key]) entry = document.catalog_entry() - probe = CultCacheEnvelope.create(key=key, type=document.type, payload=b"", schema_id=entry.schema_id, catalog_entry=entry, stored_at=self._now()) + probe = CultCacheEnvelope.create(key=key, type=document.type, payload=b"", schema_id=entry.schema_id, catalog_entry=entry, stored_at=self.now_stamp()) return self._refuse_retype(document, [probe]) def _refuse_unread(self, keys: list[str]) -> None: diff --git a/packages/cultcache-py/tests/test_clock.py b/packages/cultcache-py/tests/test_clock.py index a3d29037..7ecb5ba7 100644 --- a/packages/cultcache-py/tests/test_clock.py +++ b/packages/cultcache-py/tests/test_clock.py @@ -4,7 +4,7 @@ import shutil import tempfile import unittest from dataclasses import asdict, dataclass -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone from pathlib import Path from cultcache_py import CultCache, SingleFileMessagePackBackingStore, define_document_type @@ -49,13 +49,11 @@ class CacheClockTests(unittest.TestCase): self.assertEqual((self.dir / "a.cc").read_bytes(), (self.dir / "b.cc").read_bytes()) def test_a_pinned_clock_still_stamps_a_replacing_write_later(self) -> None: - for store in (True, False): - with self.subTest(store=store): - cache = self.pinned_cache("later.cc", store=store) - cache.put(settings_doc, "app", Settings("green", 1)) - self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2026-03-04T05:06:07+00:00") - cache.put(settings_doc, "app", Settings("blue", 2)) - self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2026-03-04T05:06:07.000001+00:00") + cache = self.pinned_cache("later.cc") + cache.put(settings_doc, "app", Settings("green", 1)) + self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2026-03-04T05:06:07+00:00") + cache.put(settings_doc, "app", Settings("blue", 2)) + self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2026-03-04T05:06:07.000001+00:00") def test_the_clock_property_is_settable_and_every_write_reads_it(self) -> None: cache = self.pinned_cache("set.cc") @@ -63,6 +61,18 @@ class CacheClockTests(unittest.TestCase): cache.put(settings_doc, "app", Settings("green", 1)) self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2030-01-01T00:00:00+00:00") + def test_a_non_utc_clock_stamps_in_utc(self) -> None: + cache = self.pinned_cache("zone.cc", lambda: datetime(2026, 3, 4, 5, 6, 7, tzinfo=timezone(timedelta(hours=5)))) + cache.put(settings_doc, "app", Settings("green", 1)) + self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2026-03-04T00:06:07+00:00") + self.assertEqual(cache.now_stamp(), "2026-03-04T00:06:07+00:00") + + def test_a_store_less_replacing_write_keeps_the_stamp_it_was_given(self) -> None: + cache = self.pinned_cache("none", store=False) + cache.put(settings_doc, "app", Settings("green", 1)) + cache.put(settings_doc, "app", Settings("blue", 2)) + self.assertEqual(cache.get_envelope(settings_doc, "app").stored_at, "2026-03-04T05:06:07+00:00") + def test_a_restamp_reads_no_clock(self) -> None: # stored_at_over takes no clock: the staged stamp when later, else the replaced stamp plus a microsecond. self.assertEqual(stored_at_over("2000-01-01T00:00:00Z", "2001-01-01T00:00:00Z"), "2001-01-01T00:00:00.000001+00:00") diff --git a/packages/cultcache-py/tests/test_update.py b/packages/cultcache-py/tests/test_update.py index 2c7c9aa2..7f291646 100644 --- a/packages/cultcache-py/tests/test_update.py +++ b/packages/cultcache-py/tests/test_update.py @@ -633,11 +633,17 @@ class StoreRestampTests(Fixture): self.assertTrue(self.later_than_alpha(held)) self.assertEqual(held, self.stamp_on_disk(SingleFileMessagePackBackingStore(path))) - def test_a_store_less_cache_stamps_a_replacing_write_later(self) -> None: + def test_a_store_less_cache_keeps_the_stamp_it_was_given(self) -> None: + # No store restamps: put and update alike hold the stamp as written, as the Rust and TypeScript caches do. cache = self.cache(store=False) cache.put_envelope(item_doc, self.envelope(1)) cache.put_envelope(item_doc, self.envelope(2, "2000-01-01T00:00:00Z")) - self.assertTrue(self.later_than_alpha(cache.get_envelope(item_doc, "k").stored_at)) + self.assertEqual(cache.get_envelope(item_doc, "k").stored_at, "2000-01-01T00:00:00Z") + cache.update(item_doc, "k", lambda item: Item(item.name, 3)) + self.assertNotEqual(cache.get_envelope(item_doc, "k").stored_at, "") + cache.clock = lambda: datetime(2031, 1, 1, tzinfo=timezone.utc) + cache.put(item_doc, "k", Item("n", 4)) + self.assertEqual(cache.get_envelope(item_doc, "k").stored_at, "2031-01-01T00:00:00+00:00") 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 diff --git a/packages/cultcache-ts/test/first-call.test.ts b/packages/cultcache-ts/test/first-call.test.ts index 9ef3d308..c3e03e42 100644 --- a/packages/cultcache-ts/test/first-call.test.ts +++ b/packages/cultcache-ts/test/first-call.test.ts @@ -92,7 +92,7 @@ test("update with retry: restock twice reads value 20 through a second cache", a // The docs show the region between `// snippet:begin` and `end` markers verbatim, the package import aside. async function assertDocsShow(region: "" | "update:") { // Compiled to dist-test/test/first-call.test.js, so the package root is two levels up. - const source = await readFile(join(__dirname, "..", "..", "test", "first-call.test.ts"), "utf8"); + const source = (await readFile(join(__dirname, "..", "..", "test", "first-call.test.ts"), "utf8")).replace(/\r\n/g, "\n"); const snippet = source .split(`// snippet:${region}begin\n`)[1] .split(`// snippet:${region}end\n`)[0] diff --git a/packages/cultmesh-py/src/cultmesh_py/node.py b/packages/cultmesh-py/src/cultmesh_py/node.py index eb439683..a28ed5b7 100644 --- a/packages/cultmesh-py/src/cultmesh_py/node.py +++ b/packages/cultmesh-py/src/cultmesh_py/node.py @@ -569,6 +569,7 @@ class CultMeshDatabase: payload=registered.encode_payload(parsed), schema_id=catalog_entry.schema_id, catalog_entry=catalog_entry, + stored_at=self.cache.now_stamp(), ) value = self.cache.put_envelope(registered, envelope) # The stamp the write was stored at, which a replacing write mints later than the record it replaces: the cache holds it. diff --git a/packages/cultmesh-py/tests/test_cultmesh.py b/packages/cultmesh-py/tests/test_cultmesh.py index 4595f29c..845c9a82 100644 --- a/packages/cultmesh-py/tests/test_cultmesh.py +++ b/packages/cultmesh-py/tests/test_cultmesh.py @@ -797,12 +797,28 @@ class CultMeshTests(unittest.TestCase): self.assertEqual(node.get_required(document, "note:second").body, "second catalog publication") self.assertIsInstance(node.get_required(alias, "note:second"), UiNote) + def attach_store(self, cache) -> None: + directory = tempfile.TemporaryDirectory() + self.addCleanup(directory.cleanup) + cache.add_generic_store(SingleFileMessagePackBackingStore(Path(directory.name) / "store.cc")) + cache.pull_all_backing_stores() + + def test_raw_put_stamps_from_the_cache_clock(self) -> None: + document = define_database_entry_type("mesh.clock_item", [("name", 0), ("category", 1), ("value", 2, 0)], cls=Item) + node = create_node(runtime_id="python-node") + node.register_document(document) + node.database.cache.clock = lambda: datetime(2026, 3, 4, 5, 6, 7, tzinfo=UTC) + wire = node.put_raw_message(document, "item:clock", Item("pinned", "gear", 1)).to_wire() + self.assertEqual(wire["document"]["storedAt"], "2026-03-04T05:06:07+00:00") + self.assertEqual(node.database.cache.get_required_envelope(document, "item:clock").stored_at, "2026-03-04T05:06:07+00:00") + def test_raw_put_publishes_and_logs_the_stamp_the_store_wrote(self) -> None: document = define_database_entry_type("mesh.stamp_item", [("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() + self.attach_store(cache) # a store restamps a replacing write; a store-less cache holds the stamp as given future = "2099-01-01T00:00:00.000000+00:00" cache.put_envelope( document, @@ -828,6 +844,7 @@ class CultMeshTests(unittest.TestCase): node.register_document(document) cache = node.database.cache entry = document.catalog_entry() + self.attach_store(cache) # a store restamps a replacing write; a store-less cache holds the stamp as given 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)