TypeScript and Python writers: records are compared for type within a tier only

A read-back record's type is its schema's name and a supplied one's is the document type, so records of different tiers are never
compared; two supplied (or two arrived) records of different types under one id still refuse the write.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 10:13:44 +02:00
commit 7a5b64ad88
3 changed files with 18 additions and 7 deletions

View file

@ -181,20 +181,24 @@ def _derive_catalog(records: list[tuple[CultCacheEnvelope, bool]]) -> list[CultC
envelope the caller supplied is registered, one read back from the file is arrived."""
entries: list[tuple[CultCacheSchemaCatalogEntry, bool]] = []
by_id: dict[str, list[CultCacheEnvelope]] = {}
tiers: dict[str, list[tuple[CultCacheEnvelope, bool]]] = {}
for envelope, registered in records:
schema_id = _schema_id_for(envelope)
supplied = envelope.catalog_entry
publishes = supplied is not None and (supplied.schema_id == schema_id or schema_id in supplied.compatible_schema_ids)
entries.append((supplied if publishes else _default_catalog_entry(envelope), registered))
by_id.setdefault(schema_id, []).append(envelope)
tiers.setdefault(schema_id, []).append((envelope, registered))
chosen: dict[str, tuple[CultCacheSchemaCatalogEntry, bool]] = {}
for schema_id in sorted(by_id):
carrying = by_id[schema_id]
record_key = min(envelope.key for envelope in carrying)
types = sorted({envelope.type for envelope in carrying})
if len(types) > 1:
raise SchemaConflictError(schema_id, types, record_key)
# Records of one tier under one id are one type. A read-back record's type is its schema's name, so tiers are not compared.
for tier in (True, False):
types = sorted({envelope.type for envelope, registered in tiers[schema_id] if registered == tier})
if len(types) > 1:
raise SchemaConflictError(schema_id, types, record_key)
own_registered = next(iter(canonical(entry for entry, registered in entries if registered and entry.schema_id == schema_id)), None)
own_arrived = canonical(entry for entry, registered in entries if not registered and entry.schema_id == schema_id)
own_registered_all = canonical(entry for entry, registered in entries if registered and entry.schema_id == schema_id)

View file

@ -367,7 +367,7 @@ class CultCacheTests(unittest.TestCase):
[["a", "tests.x", "2026-09-30T00:00:00Z", msgpack.packb({"a": 1}, use_bin_type=True)]],
], use_bin_type=True))
SingleFileMessagePackBackingStore(path).push_all(
[self._writer_record("a", "tests.new", "tests.x", self._writer_entry("tests.x", "tests.new", "fresh", ("tests.x",)))]
[self._writer_record("a", "tests.old", "tests.x", self._writer_entry("tests.x", "tests.new", "fresh", ("tests.x",)))]
)
catalog = msgpack.unpackb(path.read_bytes(), raw=False)[1]
self.assertEqual([(entry[1], entry[3]) for entry in catalog], [("tests.new", "fresh")])

View file

@ -230,6 +230,7 @@ function catalogEntriesFor(
): CultCacheSchemaCatalogEntry[] {
const entries: Array<{ entry: CultCacheSchemaCatalogEntry; registered: boolean }> = [];
const byId = new Map<string, CultCacheEnvelope[]>();
const tiers = new Map<string, Array<{ envelope: CultCacheEnvelope; registered: boolean }>>();
for (const { envelope, registered } of records) {
const schemaId = schemaIdForEnvelope(envelope);
const supplied = envelope.catalogEntry;
@ -237,6 +238,7 @@ function catalogEntriesFor(
supplied !== undefined && (supplied.schemaId === schemaId || (supplied.compatibleSchemaIds ?? []).includes(schemaId));
entries.push({ entry: publishes && supplied ? supplied : defaultEntryFor(envelope), registered });
byId.set(schemaId, [...(byId.get(schemaId) ?? []), envelope]);
tiers.set(schemaId, [...(tiers.get(schemaId) ?? []), { envelope, registered }]);
}
const lists = (entry: CultCacheSchemaCatalogEntry, schemaId: string): boolean =>
@ -245,9 +247,14 @@ function catalogEntriesFor(
for (const schemaId of [...byId.keys()].sort(compareOrdinal)) {
const carrying = byId.get(schemaId)!;
const recordKey = carrying.map((envelope) => envelope.key).sort(compareOrdinal)[0]!;
const types = [...new Set(carrying.map((envelope) => envelope.type))].sort(compareOrdinal);
if (types.length > 1) {
throw new SchemaConflictError(schemaId, types, recordKey);
// Records of one tier under one id are one type. A read-back record's type is its schema's name, so tiers are not compared.
for (const tier of [true, false]) {
const types = [
...new Set(tiers.get(schemaId)!.filter((candidate) => candidate.registered === tier).map((candidate) => candidate.envelope.type)),
].sort(compareOrdinal);
if (types.length > 1) {
throw new SchemaConflictError(schemaId, types, recordKey);
}
}
const ownRegistered = canonical(entries.filter((candidate) => candidate.registered && candidate.entry.schemaId === schemaId))[0];