Compare commits

...
Sign in to create a new pull request.
Author SHA1 Message Date
Meta Vens
a00bb01f75 cultcache-ts: test a catalog-less write over a record under another schema id
StrykerJS on e8b3e4ca left `written.catalogEntry?.schemaName` -> `.schemaName`
alive: the schema-id rule always matched first in the existing fixtures, so
the name rule never ran for an envelope without a catalog entry. This fixture
stores a record under another schema id whose name is the write's label, so
only the name and type rules can match it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 00:04:38 +02:00
Meta Vens
e8b3e4ca79 cultcache-ts: test that an envelope without a catalog entry replaces and deletes
StrykerJS on 142a2e13 left two OptionalChaining survivors in isSameRecord:
nothing wrote through the store with an envelope that has no catalog entry,
which is what an external CacheBackingStore caller sends. This test pushes,
rewrites and deletes such an envelope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 23:43:10 +02:00
Meta Vens
142a2e1385 cultcache-ts: the single-file store replaces a record by schema and key
SingleFileMessagePackBackingStore.push and delete matched an existing
record by `candidate.type === entry.type`. A record read back carries its
catalog entry's schema name as its type and a fresh write carries the
definition's type, so for any definition whose type is not its schema name
a rewrite appended a duplicate and a delete removed nothing. The bug has
been present since the monorepo import (5c69bfc5).

isSameRecord now decides identity the way the cache resolves a persisted
record to a definition: same key, and the stored record's schema id is one
the written envelope answers to (its own or a declared compatible id), else
its schema name, else its type label (legacy envelopes). push and delete
share it.

Follow-up: once hands/variants-c2a merges, schemaIdentityOf (426c956a)
should own the set of ids and names an envelope answers to, and this
function should read it rather than re-derive it from the catalog entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 23:34:56 +02:00
Meta Vens
e7dea469b4 cultcache-ts: tests that a record is its schema and key, not its type label
A record read back from a v1 store carries its catalog entry's schema name
as its type; a fresh write carries the definition's type. For a definition
that names its schema (type != schemaName) the store's push and delete
matched on type, so every rewrite of a read-back key appended a duplicate
(a global then refused to load: "has multiple persisted entries") and every
delete removed nothing on disk.

These tests pin the rule and fail at this commit; the next commit fixes the
store:
- read-back then rewrite (global and keyed) stays one record and reloads;
- read-back then delete stays deleted;
- the store matches a stored record by the written schema id, by an id the
  write declares compatible, by schema name, and by type label (legacy
  envelopes), each with a fixture only that rule reaches;
- another schema at the same key, and the same schema at another key, survive;
- a TS rewrite under a relabelled definition leaves one record that Rust, C#
  and Python read as the rewritten value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 23:34:55 +02:00
2 changed files with 207 additions and 6 deletions

View file

@ -97,9 +97,7 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
async push(entry: CultCacheEnvelope): Promise<void> {
await this.#enqueue(async () => {
const existing = await this.pullAll();
const filtered = existing.filter(
(candidate) => !(candidate.type === entry.type && candidate.key === entry.key),
);
const filtered = existing.filter((candidate) => !isSameRecord(candidate, entry));
filtered.push(entry);
await this.#writeAll(filtered);
});
@ -108,9 +106,7 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
async delete(entry: CultCacheEnvelope): Promise<void> {
await this.#enqueue(async () => {
const existing = await this.pullAll();
const filtered = existing.filter(
(candidate) => !(candidate.type === entry.type && candidate.key === entry.key),
);
const filtered = existing.filter((candidate) => !isSameRecord(candidate, entry));
await this.#writeAll(filtered);
});
}
@ -270,6 +266,26 @@ function schemaIdForEnvelope(entry: CultCacheEnvelope): string {
return entry.schemaId ?? entry.type;
}
/**
* A record is its schema and its key. An envelope's `type` is a label, not the schema: a record
* read back carries its catalog entry's schema name, while one being written carries its
* definition's type, and the two differ whenever a definition names its schema. So a stored record
* is the one being written or deleted when the keys match and the stored record names the written
* envelope's schema the way the cache resolves a persisted record to a definition: by a schema id
* the written envelope answers to (its own and every id its catalog entry declares compatible),
* else by its schema name, else by its type.
*/
function isSameRecord(stored: CultCacheEnvelope, written: CultCacheEnvelope): boolean {
if (stored.key !== written.key) {
return false;
}
const writtenSchemaIds = [schemaIdForEnvelope(written), ...(written.catalogEntry?.compatibleSchemaIds ?? [])];
return writtenSchemaIds.some((schemaId) => schemaId === stored.schemaId)
|| stored.type === written.catalogEntry?.schemaName
|| stored.type === written.type;
}
function encodeCatalogEntry(entry: CultCacheSchemaCatalogEntry): unknown[] {
return [
entry.schemaId,

View file

@ -1173,6 +1173,41 @@ test("CultCache v1 MessagePack stores are readable across TS, Rust, C#, and Pyth
}
});
test("a store TS rewrote under a type that is not its schema name holds one record every runtime reads", async () => {
await buildInteropPeers();
const file = join(await mkdtemp(join(tmpdir(), "cultcache-interop-")), "relabelled.cc");
const relabelled = defineDocumentType({ ...interopNoteDocument, type: "interop-note-ts-label" });
const open = () => CultCache.builder()
.withDocumentType(relabelled)
.withGenericStore(new SingleFileMessagePackBackingStore(file))
.build();
const note: InteropNote = {
schemaVersion: "cultcache.interop_note.v1",
documentId: "note:ts-relabelled",
authorRuntimeId: "ts-relabelled",
title: "ts rewrote a CultCache note",
body: "first write",
tags: ["ts", "interop"],
};
await open().put(relabelled, note.documentId, note);
const reopened = open();
await reopened.pullAllBackingStores();
await reopened.put(relabelled, note.documentId, { ...note, body: "rewrite after read-back" });
const records = (decode(await readFile(file)) as unknown[])[2] as unknown[][];
assert.deepEqual(records.map((record) => [record[0], record[1]]), [[note.documentId, "cultcache.interop-note"]]);
const readers: Array<[string, () => Promise<any>]> = [
["rust", () => runJsonCommand("rust-read", rustInteropBinary, ["read", "--file", file], cultcacheRsRoot)],
["csharp", () => runJsonCommand("csharp-read", dotnetCommand, [csharpInteropDll, "read", "--file", file], cultLibRoot)],
["python", () => runJsonCommand("python-read", pythonCommand, ["-m", "cultcache_py.interop", "read", "--file", file], cultcachePyRoot, { PYTHONPATH: cultcachePySrc })],
];
for (const [name, read] of readers) {
const result = await read();
assert.equal(result.documentId, note.documentId, `${name} failed to read the rewritten store`);
assert.equal(result.body, "rewrite after read-back", `${name} read a stale record`);
}
});
test("CultCache interop reader accepts missing compatible trailing slots and rejects mismatched slots", async () => {
const tempDir = await mkdtemp(join(tmpdir(), "cultcache-interop-"));
const compatible = join(tempDir, "compatible.msgpack");
@ -1588,3 +1623,153 @@ test("a v1 store written at the base commit still reads byte for byte", async ()
assert.deepEqual([...envelopes[0]!.payload], [0x92, 0xa5, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x01]);
assert.deepEqual([...envelopes[1]!.payload], [0x92, 0xa4, 0x62, 0x65, 0x74, 0x61, 0x02]);
});
// A record is its schema and its key, whatever label its envelope carries. These definitions name
// their schema, so a record read back carries the schema name while a fresh write carries the type.
const labelledSnapshotDocument = defineDocumentType({
type: "market-snapshot",
schemaName: "tests.market_snapshot",
global: true,
schema: z.object({ price: z.number() }),
});
const labelledQuoteDocument = defineDocumentType({
type: "quote",
schemaName: "tests.quote",
schema: z.object({ price: z.number() }),
});
async function labelledStore(): Promise<{ file: string; open: () => CultCache }> {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const open = () => CultCache.builder()
.withDocumentType(labelledSnapshotDocument)
.withDocumentType(labelledQuoteDocument)
.withGenericStore(new SingleFileMessagePackBackingStore(file))
.build();
return { file, open };
}
async function recordsOnDisk(file: string): Promise<Array<[string, string]>> {
const snapshot = decode(await readFile(file)) as unknown[];
return (snapshot[2] as unknown[][]).map((record) => [record[0] as string, record[1] as string]);
}
function identityEnvelope(
key: string,
type: string,
schemaId: string,
schemaName: string,
compatibleSchemaIds: string[] = [schemaId],
payload: Uint8Array = Uint8Array.of(0x01),
): CultCacheEnvelope {
return {
key,
type,
schemaId,
payload,
storedAt: "2026-09-30T00:00:00Z",
catalogEntry: {
schemaId,
schemaName,
schemaVersion: `${schemaName}.v1`,
contentHash: schemaId,
canonicalSchemaJson: "",
compatibleSchemaIds,
members: [],
},
};
}
test("a record read back and rewritten, whose type is not its schema name, stays one record and reloads", async () => {
const { file, open } = await labelledStore();
const first = open();
await first.putGlobal(labelledSnapshotDocument, { price: 1 });
await first.put(labelledQuoteDocument, "ACME", { price: 1 });
const second = open();
await second.pullAllBackingStores();
await second.putGlobal(labelledSnapshotDocument, { price: 2 });
await second.put(labelledQuoteDocument, "ACME", { price: 2 });
await second.put(labelledQuoteDocument, "ACME", { price: 3 });
assert.deepEqual(await recordsOnDisk(file), [["ACME", "tests.quote"], [CultCache.GLOBAL_KEY, "tests.market_snapshot"]]);
const third = open();
await third.pullAllBackingStores();
assert.deepEqual(third.getGlobal(labelledSnapshotDocument), { price: 2 });
assert.deepEqual(third.getRequired(labelledQuoteDocument, "ACME"), { price: 3 });
});
test("a record read back and deleted, whose type is not its schema name, stays deleted", async () => {
const { file, open } = await labelledStore();
await open().put(labelledQuoteDocument, "ACME", { price: 1 });
const second = open();
await second.pullAllBackingStores();
assert.equal(await second.delete(labelledQuoteDocument, "ACME"), true);
assert.deepEqual(await recordsOnDisk(file), []);
const third = open();
await third.pullAllBackingStores();
assert.equal(third.get(labelledQuoteDocument, "ACME"), undefined);
});
test("a store replaces a record whose schema id the write names, under any schema name or label", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const store = new SingleFileMessagePackBackingStore(file);
await store.push(identityEnvelope("k", "old-label", "id-a", "old-name"));
await store.push(identityEnvelope("k", "new-label", "id-a", "new-name", [], Uint8Array.of(0x02)));
const records = await store.pullAll();
assert.deepEqual(records.map((record) => [record.key, record.schemaId, [...record.payload]]), [["k", "id-a", [0x02]]]);
});
test("a store replaces a record stored under a schema id the write declares compatible", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const store = new SingleFileMessagePackBackingStore(file);
await store.push(identityEnvelope("k", "old-label", "id-old", "old-name"));
await store.push(identityEnvelope("k", "new-label", "id-new", "new-name", ["id-new", "id-old"], Uint8Array.of(0x02)));
assert.deepEqual(await recordsOnDisk(file), [["k", "id-new"]]);
});
test("a store replaces a record whose schema id the write does not declare but whose schema name it shares", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const store = new SingleFileMessagePackBackingStore(file);
await store.push(identityEnvelope("k", "old-label", "sha256:other-runtime", "tests.shared"));
await store.push(identityEnvelope("k", "new-label", "id-new", "tests.shared", ["id-new"], Uint8Array.of(0x02)));
assert.deepEqual(await recordsOnDisk(file), [["k", "id-new"]]);
});
test("a store replaces a legacy envelope whose type label is the written envelope's type", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
await writeFile(file, encode([{ key: "k", type: "quote", payload: Uint8Array.of(0x01), storedAt: "2026-09-30T00:00:00Z" }]));
const store = new SingleFileMessagePackBackingStore(file);
await store.push(identityEnvelope("k", "quote", "id-new", "tests.quote", ["id-new"], Uint8Array.of(0x02)));
assert.deepEqual(await recordsOnDisk(file), [["k", "id-new"]]);
});
test("a store keeps a record of another schema at the same key, and the same schema at another key", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const store = new SingleFileMessagePackBackingStore(file);
await store.push(identityEnvelope("k", "label-b", "id-b", "name-b"));
await store.push(identityEnvelope("j", "label-a", "id-a", "name-a"));
await store.push(identityEnvelope("k", "label-a", "id-a", "name-a"));
await store.delete(identityEnvelope("j", "label-c", "id-c", "name-c"));
assert.deepEqual(await recordsOnDisk(file), [["j", "id-a"], ["k", "id-b"], ["k", "id-a"]]);
});
test("a store replaces and deletes a record written by an envelope that carries no catalog entry", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const store = new SingleFileMessagePackBackingStore(file);
const raw = (payload: number): CultCacheEnvelope => ({ key: "k", type: "t", payload: Uint8Array.of(payload), storedAt: "2026-09-30T00:00:00Z" });
await store.push(raw(0x01));
await store.push(raw(0x02));
assert.deepEqual((await store.pullAll()).map((record) => [record.key, [...record.payload]]), [["k", [0x02]]]);
await store.delete(raw(0x02));
assert.deepEqual(await recordsOnDisk(file), []);
});
test("an envelope with no catalog entry replaces a record stored under another schema id that carries its label", async () => {
const file = join(await mkdtemp(join(tmpdir(), "cultcache-identity-")), "store.cc");
const store = new SingleFileMessagePackBackingStore(file);
await store.push(identityEnvelope("k", "label-x", "id-x", "t"));
await store.push({ key: "k", type: "t", payload: Uint8Array.of(0x02), storedAt: "2026-09-30T00:00:00Z" });
assert.deepEqual(await recordsOnDisk(file), [["k", "t"]]);
});