From 12f819bffe58bda9026d457680fd354125f5b0fc Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 21:31:48 +0100 Subject: [PATCH] Resolve TS store document aliases by payload --- packages/cultmesh-ts/src/index.ts | 56 +++++++++++++++++++++- packages/cultmesh-ts/test/cultmesh.test.ts | 53 ++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index 37b6b554..6481bcbb 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -6,6 +6,7 @@ import { SingleFileMessagePackBackingStore, type AnyCultCacheDocumentDefinition, type CacheBackingStore, + type CultCacheEnvelope, type CultCacheDocumentValue, } from "cultcache-ts"; import { @@ -2425,7 +2426,7 @@ export function cultMeshDocumentFromStore( const documentId = options.documentId ?? resolvedSchemaId; const read = async () => { const records = await store.pullAll(); - const record = records.find(candidate => candidate.schemaId === resolvedSchemaId); + const record = resolveCultMeshStoreDocumentRecord(records, schema, schemaOrDefinition); if (!record) { throw new Error(`CultMesh store document '${documentId}' did not contain schema ${resolvedSchemaId}.`); } @@ -5681,6 +5682,59 @@ function cultMeshSchemasAreCompatible( return Boolean(left.type && right.type && left.type === right.type); } +function resolveCultMeshStoreDocumentRecord( + records: readonly CultCacheEnvelope[], + schema: CultMeshDocumentSchemaDescriptor, + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, +): CultCacheEnvelope | undefined { + const schemaId = schema.schemaId ?? schema.type; + return ( + records.find(candidate => schemaId !== undefined && candidate.schemaId === schemaId) ?? + records.find(candidate => + cultMeshEnvelopeMatchesSchema(candidate, schema) && + (typeof schemaOrDefinition === "string" || + cultMeshEnvelopeParsesAsDefinition(candidate, schemaOrDefinition))) ?? + (typeof schemaOrDefinition === "string" + ? undefined + : records.find(candidate => cultMeshEnvelopeParsesAsDefinition(candidate, schemaOrDefinition))) + ); +} + +function cultMeshEnvelopeMatchesSchema( + envelope: CultCacheEnvelope, + schema: CultMeshDocumentSchemaDescriptor, +): boolean { + const entry = envelope.catalogEntry; + if (!entry) { + return false; + } + if ( + schema.schemaId && + (entry.schemaId === schema.schemaId || + envelope.schemaId === schema.schemaId || + entry.compatibleSchemaIds?.includes(schema.schemaId)) + ) { + return true; + } + return Boolean( + schema.schemaName && + entry.schemaName === schema.schemaName && + (schema.schemaVersion ?? "") === (entry.schemaVersion ?? ""), + ); +} + +function cultMeshEnvelopeParsesAsDefinition( + envelope: CultCacheEnvelope, + definition: AnyCultCacheDocumentDefinition, +): boolean { + try { + definition.schema.parse(decode(envelope.payload)); + return true; + } catch { + return false; + } +} + function cultMeshSchemaLabel(schema: CultMeshDocumentSchemaDescriptor): string { return schema.schemaId ?? cultMeshSchemaNameVersionKey(schema) ?? diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 2924a81c..86f58b77 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -220,6 +220,59 @@ test("CultMesh TS document handles submit predictions through configured authori ); }); +test("CultMesh TS store document handles choose compatible foreign schema payloads", async () => { + const store = { + async pullAll() { + return [ + { + key: "note:foreign", + type: "runtime.generated.incompatible-note", + schemaId: "runtime.generated.incompatible-note.v1", + storedAt: "2026-06-27T00:00:00Z", + payload: encode({ nope: "not the requested shape" }), + catalogEntry: { + schemaId: "runtime.generated.incompatible-note.v1", + schemaName: "runtime.generated.incompatible-note", + schemaVersion: "runtime.generated.incompatible-note.v1", + contentHash: "runtime.generated.incompatible-note.v1", + canonicalSchemaJson: "{}", + compatibleSchemaIds: ["runtime.generated.incompatible-note.v1"], + }, + }, + { + key: "note:foreign", + type: "runtime.generated.cultmesh.note", + schemaId: "runtime.generated.cultmesh.note.ui.42", + storedAt: "2026-06-27T00:00:01Z", + payload: encode({ + noteId: "note:foreign", + body: "foreign schema id, local store shape", + }), + catalogEntry: { + schemaId: "runtime.generated.cultmesh.note.ui.42", + schemaName: "cultmesh.note", + schemaVersion: "cultmesh.note.v1", + contentHash: "runtime.generated.cultmesh.note.ui.42", + canonicalSchemaJson: "{}", + compatibleSchemaIds: ["runtime.generated.cultmesh.note.ui.42"], + }, + }, + ]; + }, + async push() {}, + async delete() {}, + }; + + const document = CultMesh.documentFromStore(store, noteAliasDocument, { + documentId: "note:foreign.store", + }); + + assert.deepEqual(await document.latest(), { + noteId: "note:foreign", + body: "foreign schema id, local store shape", + }); +}); + test("CultMesh TS reactive documents submit predictions from member writes", async () => { let current = { noteId: "note:reactive",