2026-09-29 02:04:34 +02:00
|
|
|
export const STORE_FORMAT_VERSION = "cultcache.store.v1";
|
|
|
|
|
|
2026-09-29 18:27:17 +02:00
|
|
|
/**
|
|
|
|
|
* A store that can hold element ids. A reader older than element ids refuses this header: it would skip the id slots
|
|
|
|
|
* of a nested element and rewrite the element without them. This runtime writes it back only for a store it read
|
|
|
|
|
* under it; it does not decide when a document carries ids.
|
|
|
|
|
*/
|
|
|
|
|
export const STORE_FORMAT_ELEMENT_IDS = "cultcache.store.v3";
|
|
|
|
|
|
|
|
|
|
export type StoreFormat = typeof STORE_FORMAT_VERSION | typeof STORE_FORMAT_ELEMENT_IDS;
|
|
|
|
|
|
2026-09-29 02:04:34 +02:00
|
|
|
const STORE_FORMAT_PREFIX = "cultcache.store.";
|
|
|
|
|
const PERSISTED_RECORD_SLOTS = 4;
|
2026-09-30 04:15:03 +02:00
|
|
|
const STORE_SLOTS = 3;
|
2026-09-29 02:04:34 +02:00
|
|
|
|
|
|
|
|
/**
|
2026-09-29 18:27:17 +02:00
|
|
|
* True for a v1 or v3 snapshot, false for anything that is not a CultCache store header (the legacy
|
2026-09-29 02:04:34 +02:00
|
|
|
* envelope array). Any other `cultcache.store.*` header is refused by name.
|
|
|
|
|
*/
|
2026-09-29 18:27:17 +02:00
|
|
|
export function isStoreSnapshot(decoded: unknown): decoded is [StoreFormat, ...unknown[]] {
|
2026-09-29 02:04:34 +02:00
|
|
|
if (!Array.isArray(decoded) || typeof decoded[0] !== "string" || !decoded[0].startsWith(STORE_FORMAT_PREFIX)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 18:27:17 +02:00
|
|
|
if (decoded[0] !== STORE_FORMAT_VERSION && decoded[0] !== STORE_FORMAT_ELEMENT_IDS) {
|
2026-09-29 02:04:34 +02:00
|
|
|
throw new Error(
|
2026-09-29 18:27:17 +02:00
|
|
|
`CultCache store format "${decoded[0]}" is not readable; this runtime reads "${STORE_FORMAT_VERSION}" and "${STORE_FORMAT_ELEMENT_IDS}" only. ` +
|
2026-09-29 02:04:34 +02:00
|
|
|
"The store needs a runtime that resolves document variants.",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
Writers derive the catalog from the records they write; the merging is deleted
Every writer (C# single-file and directory, TypeScript, Python; Rust already did) chooses, for each schema id a record carries, one
entry that publishes it: the entry that owns the id (a registered descriptor, else an arrived entry), else one that lists it as
compatible. A registered descriptor wins over an arrived entry with the same own id. Entries are written as chosen, with no union of
compatible ids and no dependence on arrival order; an entry no record needs is not written. Two entries of one tier that share an own id
and disagree on the schema name, records of different types under one id, or a record no chosen entry publishes refuse the write with a
typed error (CultSchemaConflictException, SchemaConflictError) naming the id, the names and a record key, and the store is left as it was.
CultSchemaCatalogEntry.MergeById and the Python and TypeScript merge paths are gone. A cache-held record unchanged since the file was read
is rewritten under its registered id (C# single-file and directory), so an entry that lists an older id is not needed to publish it.
Changelogs for cultnet-py (removed export) and cultcache-py.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 10:03:34 +02:00
|
|
|
/**
|
|
|
|
|
* A write the catalog cannot describe: records of different types under one schema id, two arrived schemas that share an id and
|
|
|
|
|
* disagree on the schema name, or a record no chosen entry publishes. Nothing is written. It names the id, the names involved and
|
|
|
|
|
* a record key.
|
|
|
|
|
*/
|
|
|
|
|
export class SchemaConflictError extends Error {
|
|
|
|
|
readonly schemaId: string;
|
|
|
|
|
readonly schemaNames: string[];
|
|
|
|
|
readonly recordKey: string;
|
|
|
|
|
|
|
|
|
|
constructor(schemaId: string, schemaNames: string[], recordKey: string) {
|
|
|
|
|
super(`Schema id "${schemaId}" cannot describe record "${recordKey}": ${schemaNames.map((name) => `"${name}"`).join(", ")}.`);
|
|
|
|
|
this.name = "SchemaConflictError";
|
|
|
|
|
this.schemaId = schemaId;
|
|
|
|
|
this.schemaNames = schemaNames;
|
|
|
|
|
this.recordKey = recordKey;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Every runtime rewrites only a file that is exactly one store
A rewrite read the header of the file it replaces but not whether the file was
a store: C# ReadStoreHeader skipped one value and ignored bytes after it, and
took a non-array value or an array whose first slot is a string that is not a
store header for a headerless legacy file (so did the C# Mesh raw write, which
uses it); TypeScript's pushAll took any non-array value or non-header first slot
for v1; Rust ignored bytes after the array.
The rule, one per runtime: a file is a store on rewrite only when it is exactly
one complete MessagePack array. Bytes after it, a value that is not an array,
and a first slot that is a string but not a header this runtime reads are
refused, and the file is left untouched. An empty file and a legacy envelope
array (first slot not a string, empty included) carry no header and are still
rewritten.
- C#: ReadStoreHeader refuses non-array values, trailing bytes and a non-header
first string, with a NotSupportedException naming which.
- TypeScript: storeHeader (store-format.ts) decides the rewrite header;
@msgpack/msgpack's decode already refuses trailing bytes.
- Rust: store_header(bytes, whole) walks with a cursor; the rewrite path asks
for the whole file, the read paths keep reading as before.
- Python: push_all already reads the whole file (unpackb refuses trailing
bytes, the legacy decoder refuses anything but a list of maps); pinned by a
test.
Tests in each runtime run the same file matrix (truncated, trailing, scalar,
map, string first; empty, empty array, legacy), plus a C# unconditional commit
over a store with trailing bytes and the C# Mesh raw write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:01:58 +02:00
|
|
|
/**
|
2026-09-30 04:15:03 +02:00
|
|
|
* A store file this runtime cannot read: not exactly one complete store (truncated, bytes after it, a missing or extra slot),
|
|
|
|
|
* a header or record it does not know, or a body it cannot decode. Open, flush and every rewrite refuse a file with this
|
|
|
|
|
* error, and a refused file is left as it was. `cause` is what the reader choked on.
|
Every runtime rewrites only a file that is exactly one store
A rewrite read the header of the file it replaces but not whether the file was
a store: C# ReadStoreHeader skipped one value and ignored bytes after it, and
took a non-array value or an array whose first slot is a string that is not a
store header for a headerless legacy file (so did the C# Mesh raw write, which
uses it); TypeScript's pushAll took any non-array value or non-header first slot
for v1; Rust ignored bytes after the array.
The rule, one per runtime: a file is a store on rewrite only when it is exactly
one complete MessagePack array. Bytes after it, a value that is not an array,
and a first slot that is a string but not a header this runtime reads are
refused, and the file is left untouched. An empty file and a legacy envelope
array (first slot not a string, empty included) carry no header and are still
rewritten.
- C#: ReadStoreHeader refuses non-array values, trailing bytes and a non-header
first string, with a NotSupportedException naming which.
- TypeScript: storeHeader (store-format.ts) decides the rewrite header;
@msgpack/msgpack's decode already refuses trailing bytes.
- Rust: store_header(bytes, whole) walks with a cursor; the rewrite path asks
for the whole file, the read paths keep reading as before.
- Python: push_all already reads the whole file (unpackb refuses trailing
bytes, the legacy decoder refuses anything but a list of maps); pinned by a
test.
Tests in each runtime run the same file matrix (truncated, trailing, scalar,
map, string first; empty, empty array, legacy), plus a C# unconditional commit
over a store with trailing bytes and the C# Mesh raw write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:01:58 +02:00
|
|
|
*/
|
2026-09-30 04:15:03 +02:00
|
|
|
export class StoreUnreadableError extends Error {
|
|
|
|
|
readonly filePath: string;
|
Every runtime rewrites only a file that is exactly one store
A rewrite read the header of the file it replaces but not whether the file was
a store: C# ReadStoreHeader skipped one value and ignored bytes after it, and
took a non-array value or an array whose first slot is a string that is not a
store header for a headerless legacy file (so did the C# Mesh raw write, which
uses it); TypeScript's pushAll took any non-array value or non-header first slot
for v1; Rust ignored bytes after the array.
The rule, one per runtime: a file is a store on rewrite only when it is exactly
one complete MessagePack array. Bytes after it, a value that is not an array,
and a first slot that is a string but not a header this runtime reads are
refused, and the file is left untouched. An empty file and a legacy envelope
array (first slot not a string, empty included) carry no header and are still
rewritten.
- C#: ReadStoreHeader refuses non-array values, trailing bytes and a non-header
first string, with a NotSupportedException naming which.
- TypeScript: storeHeader (store-format.ts) decides the rewrite header;
@msgpack/msgpack's decode already refuses trailing bytes.
- Rust: store_header(bytes, whole) walks with a cursor; the rewrite path asks
for the whole file, the read paths keep reading as before.
- Python: push_all already reads the whole file (unpackb refuses trailing
bytes, the legacy decoder refuses anything but a list of maps); pinned by a
test.
Tests in each runtime run the same file matrix (truncated, trailing, scalar,
map, string first; empty, empty array, legacy), plus a C# unconditional commit
over a store with trailing bytes and the C# Mesh raw write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:01:58 +02:00
|
|
|
|
2026-09-30 04:15:03 +02:00
|
|
|
constructor(filePath: string, cause: unknown) {
|
|
|
|
|
super(`CultCache store ${filePath} is not readable: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
|
|
|
this.name = "StoreUnreadableError";
|
|
|
|
|
this.filePath = filePath;
|
Every runtime rewrites only a file that is exactly one store
A rewrite read the header of the file it replaces but not whether the file was
a store: C# ReadStoreHeader skipped one value and ignored bytes after it, and
took a non-array value or an array whose first slot is a string that is not a
store header for a headerless legacy file (so did the C# Mesh raw write, which
uses it); TypeScript's pushAll took any non-array value or non-header first slot
for v1; Rust ignored bytes after the array.
The rule, one per runtime: a file is a store on rewrite only when it is exactly
one complete MessagePack array. Bytes after it, a value that is not an array,
and a first slot that is a string but not a header this runtime reads are
refused, and the file is left untouched. An empty file and a legacy envelope
array (first slot not a string, empty included) carry no header and are still
rewritten.
- C#: ReadStoreHeader refuses non-array values, trailing bytes and a non-header
first string, with a NotSupportedException naming which.
- TypeScript: storeHeader (store-format.ts) decides the rewrite header;
@msgpack/msgpack's decode already refuses trailing bytes.
- Rust: store_header(bytes, whole) walks with a cursor; the rewrite path asks
for the whole file, the read paths keep reading as before.
- Python: push_all already reads the whole file (unpackb refuses trailing
bytes, the legacy decoder refuses anything but a list of maps); pinned by a
test.
Tests in each runtime run the same file matrix (truncated, trailing, scalar,
map, string first; empty, empty array, legacy), plus a C# unconditional commit
over a store with trailing bytes and the C# Mesh raw write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:01:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 02:04:34 +02:00
|
|
|
/** Refuses a persisted record with more slots than v1 defines, naming its key and schema id. */
|
|
|
|
|
export function requireV1RecordSlots(record: unknown[]): void {
|
|
|
|
|
if (record.length > PERSISTED_RECORD_SLOTS) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`CultCache record "${String(record[0])}" (schema "${String(record[1])}") has ${record.length} slots; ` +
|
|
|
|
|
`this runtime reads ${PERSISTED_RECORD_SLOTS}. The store needs a runtime that resolves document variants.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-30 04:15:03 +02:00
|
|
|
|
|
|
|
|
/** Refuses a store array with more or fewer slots than the header, the schema catalog and the records; a header alone is not a store. */
|
|
|
|
|
export function requireStoreSlots(decoded: unknown[]): void {
|
|
|
|
|
if (decoded.length !== STORE_SLOTS) {
|
|
|
|
|
throw new Error(`CultCache store has ${decoded.length} top-level slots; this runtime reads ${STORE_SLOTS} (header, schema catalog, records).`);
|
|
|
|
|
}
|
|
|
|
|
}
|