From 9f869adb6082b16c7e8a1190b5098f2c591be694 Mon Sep 17 00:00:00 2001 From: Libby Date: Wed, 11 Nov 2026 14:05:55 +0000 Subject: [PATCH] cultcache-py: refuse duplicate record keys and non-string canonical schema; writer replaces by key Co-Authored-By: Claude Sonnet 5.5 --- .../cultcache-py/src/cultcache_py/stores.py | 13 ++++++++--- packages/cultcache-py/tests/test_cultcache.py | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/cultcache-py/src/cultcache_py/stores.py b/packages/cultcache-py/src/cultcache_py/stores.py index 77d671ad..a7800d0a 100644 --- a/packages/cultcache-py/src/cultcache_py/stores.py +++ b/packages/cultcache-py/src/cultcache_py/stores.py @@ -248,8 +248,9 @@ def _derive_catalog(records: list[tuple[CultCacheEnvelope, bool]]) -> list[CultC def _encode_snapshot( supplied: list[CultCacheEnvelope], arrived: list[CultCacheEnvelope], format_version: str ) -> list[Any]: - replaced = {(envelope.type, envelope.key) for envelope in supplied} - kept = [envelope for envelope in arrived if (envelope.type, envelope.key) not in replaced] + # A key names one record whatever its type, so a supplied record replaces the arrived one by key alone. + replaced = {envelope.key for envelope in supplied} + kept = [envelope for envelope in arrived if envelope.key not in replaced] envelopes = [*kept, *supplied] catalog = [ _encode_catalog_entry(entry) @@ -400,6 +401,7 @@ def _decode_snapshot(decoded: Any) -> tuple[str, list[CultCacheEnvelope]] | None catalog_by_schema_id.setdefault(compatible_schema_id, entry) envelopes: list[CultCacheEnvelope] = [] + seen: set[str] = set() for raw_record in decoded[2]: if not isinstance(raw_record, list) or len(raw_record) < 4: raise ValueError("CultCache persisted records must be MessagePack arrays") @@ -412,6 +414,9 @@ def _decode_snapshot(decoded: Any) -> tuple[str, list[CultCacheEnvelope]] | None key, schema_id, stored_at, payload = raw_record if not isinstance(key, str) or not key: raise ValueError("CultCache persisted records must declare a key") + if key in seen: + raise ValueError(f"CultCache record {key!r} appears more than once") + seen.add(key) if not isinstance(schema_id, str) or not schema_id: raise ValueError("CultCache persisted records must declare a schema id") if not isinstance(stored_at, str) or not stored_at: @@ -484,12 +489,14 @@ def _decode_catalog_entry(value: Any) -> CultCacheSchemaCatalogEntry: raise ValueError(f'CultCache schema catalog entry "{schema_id}" has invalid compatible schema ids') if not isinstance(members, list): raise ValueError(f'CultCache schema catalog entry "{schema_id}" has invalid members') + if not isinstance(canonical_schema_json, str): + raise ValueError(f'CultCache schema catalog entry "{schema_id}" has an invalid canonical schema') return CultCacheSchemaCatalogEntry( schema_id=schema_id, schema_name=schema_name, schema_version=schema_version if isinstance(schema_version, str) and schema_version else f"{schema_name}.v1", content_hash=content_hash if isinstance(content_hash, str) and content_hash else schema_id, - canonical_schema_json=canonical_schema_json if isinstance(canonical_schema_json, str) else "", + canonical_schema_json=canonical_schema_json, compatible_schema_ids=tuple(compatible_schema_ids), members=tuple(_decode_catalog_member(member) for member in members), ) diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index 92892b51..a8c02f0f 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -1604,6 +1604,28 @@ class CultCacheTests(unittest.TestCase): self.assertIsNotNone(refused.exception.__cause__, f"{vector} {operation}") self.assertEqual(store_path.read_bytes(), content, f"{vector} {operation} rewrote a file it cannot read") + def test_a_supplied_record_replaces_an_arrived_one_by_key_alone(self) -> None: + import msgpack # type: ignore + + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "store.msgpack" + store = SingleFileMessagePackBackingStore(path) + store.push(CultCacheEnvelope(key="k", type="first", payload=b"\x90", stored_at="2026-09-30T00:00:00Z")) + store.push(CultCacheEnvelope(key="k", type="second", payload=b"\x90", stored_at="2026-09-30T00:00:01Z")) + records = msgpack.unpackb(path.read_bytes(), raw=False)[2] + self.assertEqual([record[0] for record in records], ["k"]) + self.assertEqual([envelope.type for envelope in store.pull_all()], ["second"]) + + def test_a_catalog_entry_with_a_non_string_canonical_schema_is_unreadable(self) -> None: + # Direct probe of the c0 vector; its manifest row is added by the TS cut. + vector = self._C2A_VECTORS.parent / "document-variants-c0" / "current-catalog-bad-canonical.msgpack" + content = vector.read_bytes() + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "store.msgpack" + path.write_bytes(content) + with self.assertRaises(StoreUnreadableError): + SingleFileMessagePackBackingStore(path).pull_all() + self.assertEqual(path.read_bytes(), content) if __name__ == "__main__":