2026-10-23 01:38:16 +02:00
import { decode } from "@msgpack/msgpack" ;
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-10-23 04:11:53 +02:00
* True for a decoded array that leads with a ` cultcache.store.* ` stamp , false for the legacy envelope array . It judges the
* shape only : the verdict on the stamp is { @link refuseUnreadStoreBytes } , which a reader calls on the bytes before it decodes ,
* so the stamp of a decoded array it is given is one this runtime reads .
2026-09-29 02:04:34 +02:00
* /
2026-09-29 18:27:17 +02:00
export function isStoreSnapshot ( decoded : unknown ) : decoded is [ StoreFormat , . . . unknown [ ] ] {
2026-10-23 04:11:53 +02:00
return Array . isArray ( decoded ) && typeof decoded [ 0 ] === "string" && decoded [ 0 ] . startsWith ( STORE_FORMAT_PREFIX ) ;
2026-10-22 22:26:00 +02:00
}
2026-10-23 01:38:16 +02:00
/ * *
* Refuses a store whose header names a format this runtime does not read , judging the header alone : the leading array ' s first
* element , when it is a complete string . The reader calls it before it decodes the body , so a newer store is refused by name
* whether or not its body is whole . Bytes with no such header ( a legacy array , an empty , truncated or garbled file ) are not
* refused here .
* /
export function refuseUnreadStoreBytes ( bytes : Uint8Array ) : void {
const header = peekHeader ( bytes ) ;
if ( header !== undefined && header . startsWith ( STORE_FORMAT_PREFIX ) ) {
refuseUnreadFormat ( header ) ;
}
}
// The first element of a leading non-empty MessagePack array when it is a complete string; anything else is undefined. The
// string is decoded by the decoder a read uses, so a stamp gets one verdict from the peek and from the whole decode.
function peekHeader ( bytes : Uint8Array ) : string | undefined {
const view = new DataView ( bytes . buffer , bytes . byteOffset , bytes . byteLength ) ;
const lead = bytes [ 0 ] ;
let offset : number ;
if ( lead === undefined ) return undefined ;
if ( lead >= 0x91 && lead <= 0x9f ) offset = 1 ;
else if ( lead === 0xdc && bytes . length >= 3 && view . getUint16 ( 1 ) > 0 ) offset = 3 ;
else if ( lead === 0xdd && bytes . length >= 5 && view . getUint32 ( 1 ) > 0 ) offset = 5 ;
else return undefined ;
const start = offset ;
const tag = bytes [ offset ] ;
let length : number ;
if ( tag === undefined ) return undefined ;
if ( tag >= 0xa0 && tag <= 0xbf ) { length = tag & 0x1f ; offset += 1 ; }
else if ( tag === 0xd9 && bytes . length >= offset + 2 ) { length = bytes [ offset + 1 ] ! ; offset += 2 ; }
else if ( tag === 0xda && bytes . length >= offset + 3 ) { length = view . getUint16 ( offset + 1 ) ; offset += 3 ; }
else if ( tag === 0xdb && bytes . length >= offset + 5 ) { length = view . getUint32 ( offset + 1 ) ; offset += 5 ; }
else return undefined ;
if ( bytes . length < offset + length ) return undefined ;
const header = decode ( bytes . subarray ( start , offset + length ) ) ;
return typeof header === "string" ? header : undefined ;
}
2026-10-23 00:44:59 +02:00
/** The one verdict on a stamp: this runtime reads v1 and v3, and any other `cultcache.store.*` header is refused by name. */
2026-10-22 22:26:00 +02:00
function refuseUnreadFormat ( header : string ) : void {
2026-10-23 00:44:59 +02:00
if ( header !== STORE_FORMAT_VERSION && header !== STORE_FORMAT_ELEMENT_IDS ) {
2026-09-29 02:04:34 +02:00
throw new Error (
2026-10-23 00:44:59 +02:00
` CultCache store format ${ describeHeader ( header ) } is not one this runtime reads; it reads " ${ STORE_FORMAT_VERSION } " and " ${ STORE_FORMAT_ELEMENT_IDS } " only. ` ,
2026-09-29 02:04:34 +02:00
) ;
}
}
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
TypeScript: a definition listing an id registers beside the one owning it; registration refuses typed
Owner beats lister, as in C# and Python. The cache kept one definition per id,
own or compatible, and refused a second with a plain Error, so a v1 definition
could not stay registered beside a v2 declaring v1's id (Soul F8, cult-cache.ts
registration). Now a definition owns its schema id and any number may list it:
a record resolves to the owner, else to the one lister. Several listers and no
owner name no single definition, and the load is refused with
SchemaConflictError naming the id, the record key and the listers.
Every registration refusal is now SchemaConflictError (Soul F10): a second owner
of one id, a second definition with one schema name, and a different definition
for one type. Each names the new definition's id and both schema names.
SchemaConflictError takes an optional message for these uses.
Tests: an owner and a lister register in both orders and each reads its own
records, and a lister alone reads the owner's id; two listers register in both
orders, a record under their id is refused typed without its owner and is the
owner's with it, registered before or after them; each of the three refusals is
a SchemaConflictError in both registration orders.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 15:31:31 +02:00
* a record key . A cache throws it too , with an empty record key , when a definition cannot register beside one it already holds ,
* and when a record ' s schema id names no single registered definition .
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
* /
export class SchemaConflictError extends Error {
readonly schemaId : string ;
readonly schemaNames : string [ ] ;
readonly recordKey : string ;
TypeScript: a definition listing an id registers beside the one owning it; registration refuses typed
Owner beats lister, as in C# and Python. The cache kept one definition per id,
own or compatible, and refused a second with a plain Error, so a v1 definition
could not stay registered beside a v2 declaring v1's id (Soul F8, cult-cache.ts
registration). Now a definition owns its schema id and any number may list it:
a record resolves to the owner, else to the one lister. Several listers and no
owner name no single definition, and the load is refused with
SchemaConflictError naming the id, the record key and the listers.
Every registration refusal is now SchemaConflictError (Soul F10): a second owner
of one id, a second definition with one schema name, and a different definition
for one type. Each names the new definition's id and both schema names.
SchemaConflictError takes an optional message for these uses.
Tests: an owner and a lister register in both orders and each reads its own
records, and a lister alone reads the owner's id; two listers register in both
orders, a record under their id is refused typed without its owner and is the
owner's with it, registered before or after them; each of the three refusals is
a SchemaConflictError in both registration orders.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 15:31:31 +02:00
constructor ( schemaId : string , schemaNames : string [ ] , recordKey : string , message? : string ) {
super ( message ? ? ` Schema id " ${ schemaId } " cannot describe record " ${ recordKey } ": ${ schemaNames . map ( ( name ) = > ` " ${ name } " ` ) . join ( ", " ) } . ` ) ;
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
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-30 18:35:19 +02:00
/ * *
* A store header as a refusal may show it : echoed in the shape ` cultcache.store.v<digits> ` ,
* and otherwise described only by its length , since the bytes are the store ' s .
* /
function describeHeader ( header : string ) : string {
return / ^ c u l t c a c h e \ . s t o r e \ . v [ 0 - 9 ] + $ / u . t e s t ( h e a d e r )
? ` " ${ header } " `
: ` an unrecognised ${ STORE_FORMAT_PREFIX } * header of ${ new TextEncoder ( ) . encode ( header ) . length } bytes ` ;
}
2026-09-30 19:54:51 +02:00
/ * *
* A record key or schema id as a refusal may show it : a string is an identity and is named ;
* anything else is described by its type , since the value is the store ' s .
* /
function describeIdentity ( value : unknown ) : string {
if ( typeof value === "string" ) {
return ` " ${ value } " ` ;
}
const kind = Array . isArray ( value )
? "array"
: value instanceof Uint8Array
? "bytes"
: value === null
? "nil"
: typeof value ;
return ` < ${ kind } > ` ;
}
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 (
2026-09-30 19:54:51 +02:00
` CultCache record ${ describeIdentity ( record [ 0 ] ) } (schema ${ describeIdentity ( record [ 1 ] ) } ) has ${ record . length } slots, more than ` +
2026-09-30 17:04:39 +02:00
` the ${ PERSISTED_RECORD_SLOTS } of a ${ STORE_FORMAT_VERSION } record, so this is not a valid store. ` ,
2026-09-29 02:04:34 +02:00
) ;
}
}
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). ` ) ;
}
}