CultLib/packages/cultnet-ts/src/replication.ts

410 lines
14 KiB
TypeScript
Raw Normal View History

import type {
AnyCultCacheDocumentDefinition,
CultCache,
CultCacheDocumentValue,
CultCacheEnvelope,
Publish the non-Rust packages and make the licensing uniform Rust consumers pin a CultLib revision through cargo git dependencies, which is how Ghostlight, Epiphany, CodexConnector and Odin already consume the core stack. npm and pip cannot take a package from a subdirectory of a git repo at all, which is why the TypeScript and Python consumers vendored forks instead: there was nothing else to depend on. This gives them a versioned artifact. Tagging cultcache-ts-v* or cultcache-py-v* builds, tests and publishes. The tag version must match the manifest version or the workflow refuses rather than shipping a mislabelled artifact. PyPI uses trusted publishing, so no long-lived token is stored; npm needs an NPM_TOKEN secret. cultcache-ts is now @gamecult/cultcache-ts. The scope is not cosmetic: it makes the package unambiguously org-owned on a public registry, and it is what lets CultLib hold the name rather than racing for an unscoped one. CultLib's own cultmesh-ts and cultnet-ts and their imports move with it; all three packages typecheck and cultcache-ts passes 12 of 13 tests, the exception being the cross-runtime store test that shells out to Python, which cannot run on this machine because its Python has no msgpack. Licensing was inconsistent with the repo. cultcache-py declared itself "Private GameCult infrastructure" and cultcache-rs and cultmesh-rs declared nothing, inside a repo whose LICENSE is MIT and whose every other package says MIT. All three now say MIT. That was load-bearing here: a package that calls itself private is not one to push to a public registry. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-04 04:41:44 +02:00
} from "@gamecult/cultcache-ts";
import {
SchemaConflictError,
decodeDocumentPayload,
documentPayloadBytes,
documentPayloadFromBytes,
encodeDocumentPayload,
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
schemaIdentityOf,
Publish the non-Rust packages and make the licensing uniform Rust consumers pin a CultLib revision through cargo git dependencies, which is how Ghostlight, Epiphany, CodexConnector and Odin already consume the core stack. npm and pip cannot take a package from a subdirectory of a git repo at all, which is why the TypeScript and Python consumers vendored forks instead: there was nothing else to depend on. This gives them a versioned artifact. Tagging cultcache-ts-v* or cultcache-py-v* builds, tests and publishes. The tag version must match the manifest version or the workflow refuses rather than shipping a mislabelled artifact. PyPI uses trusted publishing, so no long-lived token is stored; npm needs an NPM_TOKEN secret. cultcache-ts is now @gamecult/cultcache-ts. The scope is not cosmetic: it makes the package unambiguously org-owned on a public registry, and it is what lets CultLib hold the name rather than racing for an unscoped one. CultLib's own cultmesh-ts and cultnet-ts and their imports move with it; all three packages typecheck and cultcache-ts passes 12 of 13 tests, the exception being the cross-runtime store test that shells out to Python, which cannot run on this machine because its Python has no msgpack. Licensing was inconsistent with the repo. cultcache-py declared itself "Private GameCult infrastructure" and cultcache-rs and cultmesh-rs declared nothing, inside a repo whose LICENSE is MIT and whose every other package says MIT. All three now say MIT. That was load-bearing here: a package that calls itself private is not one to push to a public registry. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-04 04:41:44 +02:00
} from "@gamecult/cultcache-ts";
import {
type CultNetDocumentDeleteMessage,
type CultNetDocumentPutMessage,
type CultNetDocumentPutRawMessage,
type CultNetDocumentRecord,
type CultNetRawDocumentRecord,
type CultNetSnapshotRequestMessage,
type CultNetSnapshotResponseMessage,
type CultNetSnapshotResponseRawMessage,
} from "./contracts";
export interface CultNetDocumentBinding<
TDefinition extends AnyCultCacheDocumentDefinition = AnyCultCacheDocumentDefinition,
> {
definition: TDefinition;
payloadSchemaVersion?:
| string
| ((value: CultCacheDocumentValue<TDefinition>) => string | undefined);
}
export function defineCultNetDocumentBinding<
TDefinition extends AnyCultCacheDocumentDefinition,
>(
binding: CultNetDocumentBinding<TDefinition>,
): CultNetDocumentBinding<TDefinition> {
return Object.freeze({ ...binding });
}
export class CultNetDocumentRegistry {
readonly #bindings = new Map<string, CultNetDocumentBinding>();
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
// The binding that owns each schema id, and every binding listing an id as compatible, in registration order.
readonly #owners = new Map<string, CultNetDocumentBinding>();
readonly #listers = new Map<string, CultNetDocumentBinding[]>();
constructor(bindings: Iterable<CultNetDocumentBinding> = []) {
for (const binding of bindings) {
this.register(binding);
}
}
register(binding: CultNetDocumentBinding): this {
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
// One definition owns a type, a schema id and a schema name, derived as CultCache derives them. Another definition
// claiming any of them is refused, so a schema never resolves to two types.
const identity = schemaIdentityOf(binding.definition);
for (const holder of this.#bindings.values()) {
if (holder.definition === binding.definition) {
continue;
}
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
const held = schemaIdentityOf(holder.definition);
const claimed =
holder.definition.type === binding.definition.type ? `type "${binding.definition.type}"`
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
: held.schemaId === identity.schemaId ? `schema id "${identity.schemaId}"`
: held.schemaName === identity.schemaName ? `schema name "${identity.schemaName}"`
: undefined;
if (claimed) {
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
throw new SchemaConflictError(identity.schemaId, [held.schemaName, identity.schemaName], "",
`CultNet ${claimed} is already bound to type "${holder.definition.type}"; type "${binding.definition.type}" (schema id "${identity.schemaId}") cannot also be bound.`);
}
}
this.#bindings.set(binding.definition.type, binding);
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
this.#owners.set(identity.schemaId, binding);
for (const schemaId of identity.compatibleSchemaIds) {
const listers = this.#listers.get(schemaId) ?? [];
if (schemaId !== identity.schemaId && !listers.some(lister => lister.definition === binding.definition)) {
this.#listers.set(schemaId, [...listers, binding]);
}
}
return this;
}
get(documentType: string): CultNetDocumentBinding | undefined {
return this.#bindings.get(documentType);
}
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
// The binding a record under this schema id belongs to: the id's owner, else the one binding that lists it as
// compatible. An id nothing declares resolves to nothing. An id that several bindings list and none owns is refused,
// as the cache refuses it, whatever order the bindings were registered in.
getBySchemaId(schemaId: string): CultNetDocumentBinding | undefined {
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
const owner = this.#owners.get(schemaId);
if (owner) {
return owner;
}
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
const listers = this.#listers.get(schemaId) ?? [];
if (listers.length > 1) {
const types = listers.map(lister => lister.definition.type).sort();
throw new SchemaConflictError(schemaId, listers.map(lister => schemaIdentityOf(lister.definition).schemaName).sort(), "",
`CultNet schema id "${schemaId}" is owned by no bound type and declared compatible by several (${types.join(", ")}), ` +
"so it resolves to none. Bind the type that owns the id, or declare it on one type.");
}
return listers[0];
}
createDocumentPutMessage<TDefinition extends AnyCultCacheDocumentDefinition>(
binding: CultNetDocumentBinding<TDefinition>,
messageId: string,
documentKey: string,
value: CultCacheDocumentValue<TDefinition>,
options: {
storedAt?: string;
sourceRuntimeId?: string;
sourceAgentId?: string;
sourceRole?: string;
tags?: string[];
} = {},
): CultNetDocumentPutMessage<CultCacheDocumentValue<TDefinition>> {
const parsed = binding.definition.schema.parse(value);
return {
schemaVersion: "cultnet.document_put.v0",
messageId,
document: {
schemaId: schemaIdForBinding(binding),
recordKey: documentKey,
storedAt: options.storedAt ?? new Date().toISOString(),
payload: parsed,
// Absent optional fields must be omitted, not set to undefined: MessagePack encodes undefined as nil and the peer rejects it.
...(options.sourceRuntimeId !== undefined && { sourceRuntimeId: options.sourceRuntimeId }),
...(options.sourceAgentId !== undefined && { sourceAgentId: options.sourceAgentId }),
...(options.sourceRole !== undefined && { sourceRole: options.sourceRole }),
...(options.tags !== undefined && { tags: options.tags }),
},
};
}
createDocumentDeleteMessage(
messageId: string,
schemaId: string,
recordKey: string,
): CultNetDocumentDeleteMessage {
return {
schemaVersion: "cultnet.document_delete.v0",
messageId,
schemaId,
recordKey,
};
}
createRawDocumentPutMessageFromEnvelope(
messageId: string,
envelope: CultCacheEnvelope,
): CultNetDocumentPutRawMessage {
return {
schemaVersion: "cultnet.document_put_raw.v0",
messageId,
document: this.#createRawDocumentRecord(envelope),
};
}
createRawDocumentPutMessage<TDefinition extends AnyCultCacheDocumentDefinition>(
binding: CultNetDocumentBinding<TDefinition>,
messageId: string,
documentKey: string,
value: CultCacheDocumentValue<TDefinition>,
options: {
storedAt?: string;
sourceRuntimeId?: string;
sourceAgentId?: string;
sourceRole?: string;
tags?: string[];
} = {},
): CultNetDocumentPutRawMessage {
return {
schemaVersion: "cultnet.document_put_raw.v0",
messageId,
document: {
schemaId: schemaIdForBinding(binding),
recordKey: documentKey,
storedAt: options.storedAt ?? new Date().toISOString(),
payloadEncoding: "messagepack",
payload: encodeDocumentPayload(binding.definition, value),
sourceRuntimeId: options.sourceRuntimeId,
sourceAgentId: options.sourceAgentId,
sourceRole: options.sourceRole,
tags: options.tags,
},
};
}
createSnapshotResponse(
cache: CultCache,
messageId: string,
filter?: CultNetSnapshotRequestMessage,
): CultNetSnapshotResponseMessage {
const requestedSchemaIds = filter?.schemaIds ? new Set(filter.schemaIds) : undefined;
const requestedKeys = filter?.recordKeys ? new Set(filter.recordKeys) : undefined;
const documents: CultNetDocumentRecord[] = [];
for (const envelope of cache.snapshot()) {
const binding = this.#requireBinding(envelope.type);
const schemaId = schemaIdForEnvelope(envelope, binding);
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
if (requestedSchemaIds && !answersTo(schemaId, binding, requestedSchemaIds)) {
continue;
}
if (requestedKeys && !requestedKeys.has(envelope.key)) {
continue;
}
const payload = decodeDocumentPayload(binding.definition, envelope.payload);
documents.push({
schemaId,
recordKey: envelope.key,
storedAt: envelope.storedAt,
payload,
});
}
return {
schemaVersion: "cultnet.snapshot_response.v0",
messageId,
documents,
};
}
createRawSnapshotResponse(
cache: CultCache,
messageId: string,
filter?: CultNetSnapshotRequestMessage,
): CultNetSnapshotResponseRawMessage {
const requestedSchemaIds = filter?.schemaIds ? new Set(filter.schemaIds) : undefined;
const requestedKeys = filter?.recordKeys ? new Set(filter.recordKeys) : undefined;
const documents: CultNetRawDocumentRecord[] = [];
for (const envelope of cache.snapshot()) {
const binding = this.#requireBinding(envelope.type);
const schemaId = schemaIdForEnvelope(envelope, binding);
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
if (requestedSchemaIds && !answersTo(schemaId, binding, requestedSchemaIds)) {
continue;
}
if (requestedKeys && !requestedKeys.has(envelope.key)) {
continue;
}
documents.push(this.#createRawDocumentRecord(envelope));
}
return {
schemaVersion: "cultnet.snapshot_response_raw.v0",
messageId,
documents,
};
}
async applyDocumentPutMessage(
cache: CultCache,
message: CultNetDocumentPutMessage,
): Promise<unknown> {
const binding = this.#requireSchemaBinding(message.document.schemaId);
return cache.put(
binding.definition,
message.document.recordKey,
binding.definition.schema.parse(message.document.payload),
);
}
async applyDocumentDeleteMessage(
cache: CultCache,
message: CultNetDocumentDeleteMessage,
): Promise<boolean> {
const binding = this.#requireSchemaBinding(message.schemaId);
return cache.delete(binding.definition, message.recordKey);
}
async applyRawDocumentPutMessage(
cache: CultCache,
message: CultNetDocumentPutRawMessage,
): Promise<unknown> {
const { binding, envelope } = this.#rawPutEnvelope(message);
return cache.putEnvelope(binding.definition, envelope);
}
/**
* The snapshot response that would serve this raw put's document alone: the
* record `applyRawDocumentPutMessage` would store, as `createRawSnapshotResponse`
* would serve it. A server uses it to judge a put by the size it would be
* served at. Throws, as the put would, when no binding accepts the document.
*/
createRawSnapshotResponseForPut(
message: CultNetDocumentPutRawMessage,
messageId: string,
): CultNetSnapshotResponseRawMessage {
const { envelope } = this.#rawPutEnvelope(message);
return {
schemaVersion: "cultnet.snapshot_response_raw.v0",
messageId,
documents: [this.#createRawDocumentRecord(envelope)],
};
}
#rawPutEnvelope(message: CultNetDocumentPutRawMessage): {
binding: CultNetDocumentBinding;
envelope: CultCacheEnvelope;
} {
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
const binding = this.#requireSchemaBinding(message.document.schemaId);
return {
binding,
envelope: {
key: message.document.recordKey,
type: binding.definition.type,
schemaId: schemaIdForBinding(binding),
payload: documentPayloadFromBytes(new Uint8Array(documentPayloadBytes(message.document.payload))),
storedAt: message.document.storedAt,
},
};
}
async applySnapshotResponse(
cache: CultCache,
response: CultNetSnapshotResponseMessage,
): Promise<void> {
for (const document of response.documents) {
await this.applyDocumentPutMessage(cache, {
schemaVersion: "cultnet.document_put.v0",
messageId: response.messageId,
document,
});
}
}
async applyRawSnapshotResponse(
cache: CultCache,
response: CultNetSnapshotResponseRawMessage,
): Promise<void> {
for (const document of response.documents) {
await this.applyRawDocumentPutMessage(cache, {
schemaVersion: "cultnet.document_put_raw.v0",
messageId: response.messageId,
document,
});
}
}
#requireBinding(documentType: string): CultNetDocumentBinding {
const binding = this.get(documentType);
if (!binding) {
throw new Error(`No CultNet document binding is registered for "${documentType}".`);
}
return binding;
}
#requireSchemaBinding(schemaId: string): CultNetDocumentBinding {
const binding = this.getBySchemaId(schemaId);
if (!binding) {
throw new Error(`No CultNet document binding is registered for schema "${schemaId}".`);
}
return binding;
}
#createRawDocumentRecord(envelope: CultCacheEnvelope): CultNetRawDocumentRecord {
const binding = this.#requireBinding(envelope.type);
return {
schemaId: schemaIdForEnvelope(envelope, binding),
recordKey: envelope.key,
storedAt: envelope.storedAt,
payloadEncoding: "messagepack",
payload: documentPayloadFromBytes(new Uint8Array(documentPayloadBytes(envelope.payload))),
};
}
}
function schemaIdForBinding(binding: CultNetDocumentBinding): string {
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
return schemaIdentityOf(binding.definition).schemaId;
}
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
// A held record matches a snapshot filter when the filter names the id it carries or any id its binding answers to.
function answersTo(
schemaId: string,
binding: CultNetDocumentBinding,
requestedSchemaIds: Set<string>,
): boolean {
CultNet TS: the registry resolves only ids a definition declares Soul F1, F2 and F3 on the registry. The F2 principle (operator, 2026-09-30): "CultCache never destroys or relabels a record it doesn't understand; keeps it byte-for-byte, refuses writes that would overwrite it." - F1: the registry derives every binding's identity with cultcache-ts's schemaIdentityOf, so it stamps, refuses and resolves exactly as the cache does. P1 (A with schema name "p1.shared", B with schema id "p1.shared") is now refused by both. - F2a: an id nobody owns resolves to the one binding that lists it as compatible. Several listers and no owner is a SchemaConflictError naming the id and the listers, whatever order they were registered in; an owner beats every lister. This is refused at resolution, as the cache refuses it at load, not at registration: refusing at registration would make the outcome depend on whether the owner was registered before the listers. - F2b: version inference is deleted. "p3.note.v7" no longer resolves to the type named "p3.note" and gets relabelled "p3.note.v1". - F2c: a schema version string, and a schema name, are not ids. Snapshot filters, typed puts and deletes, and raw puts match only declared ids. - F3: a raw record under an id no binding declares is refused. The registry no longer tries each binding's parser and stores the record under the first that accepts it (P5: an identity parser registered first took everything). - The dead resolvePayloadSchemaVersion helper is gone. Tests: the three shape- and alias-routing tests are replaced by P1, P2, P3/P4 and P5 as behaviour tests. The one-type-per-schema test registers two bindings ahead of the holder (F6/M11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 19:49:41 +02:00
return requestedSchemaIds.has(schemaId)
|| schemaIdentityOf(binding.definition).compatibleSchemaIds.some(candidate => requestedSchemaIds.has(candidate));
}
function schemaIdForEnvelope(
envelope: CultCacheEnvelope,
binding: CultNetDocumentBinding,
): string {
return envelope.schemaId ?? schemaIdForBinding(binding);
}
function resolvePayloadSchemaVersion<TDefinition extends AnyCultCacheDocumentDefinition>(
binding: CultNetDocumentBinding<TDefinition>,
value: CultCacheDocumentValue<TDefinition>,
): string | undefined {
if (typeof binding.payloadSchemaVersion === "function") {
return binding.payloadSchemaVersion(value);
}
return binding.payloadSchemaVersion;
}