Add TS catalog watch ergonomics

This commit is contained in:
Meta Vens 2026-06-16 10:11:37 +01:00
commit 4c80cafe5d
4 changed files with 94 additions and 9 deletions

View file

@ -283,6 +283,7 @@ Current progress:
- TypeScript CultMesh local catalogs now expose the same practical discovery
ergonomics as the other runtimes: sorted local views, direct `get(...)`
lookup, unsubscribe-able `watch(...)` callbacks for Verse and peer catalogs,
watch callbacks for authority leases and stream declarations/frame cursors,
plus lease listing and lookup beside the authority check.
- Kotlin now mirrors the CultMesh stream catalog shape for Android-adjacent
media, sensor, tensor, and byte streams: stream declaration, clock metadata,

View file

@ -31,6 +31,21 @@ peers.upsert({
});
const readReplicas = peers.find("public", "read-replica");
const leases = CultMesh.createAuthorityLeaseCatalog();
const stopWatchingLeases = leases.watch((lease) => {
console.log("lease changed", lease.leaseId);
});
leases.upsert({
leaseId: "lease:ts-peer",
verseId: "public",
peerId: "ts-peer",
roles: ["read-replica"],
validFrom: new Date(Date.now() - 1000),
expiresAt: new Date(Date.now() + 60_000),
});
stopWatchingLeases();
```
## Streaming Mode
@ -55,6 +70,12 @@ The mirrored API shape is `CultMeshStreamCatalog`:
import { CultMesh } from "cultmesh-ts";
const streams = CultMesh.createStreamCatalog();
const stopWatchingStreams = streams.watch((stream) => {
console.log("stream declared", stream.streamId);
});
const stopWatchingFrames = streams.watchFrames((frame) => {
console.log("latest frame", frame.streamId, frame.sequence);
});
streams.declare({
streamId: "mimir:kiyo-pro",
@ -94,9 +115,13 @@ streams.publishFrame({
fenceValue: 7n,
unavoidableCopyCount: 0,
});
stopWatchingStreams();
stopWatchingFrames();
```
Current implementation status: this package mirrors typed stream declaration,
negotiation, and latest-frame cursor contracts for non-C# clients. The native
C# path owns shared-memory rings and GPU-handle integration points. CultCache
page append/readback remains the durable recording or nonlocal fallback.
negotiation, watch callbacks, and latest-frame cursor contracts for non-C#
clients. The native C# path owns shared-memory rings and GPU-handle integration
points. CultCache page append/readback remains the durable recording or
nonlocal fallback.

View file

@ -152,6 +152,7 @@ export interface CultMeshAuthorityLease {
export class CultMeshAuthorityLeaseCatalog {
readonly #leases = new Map<string, CultMeshAuthorityLease>();
readonly #subscribers = new Set<(lease: CultMeshAuthorityLease) => void>();
public get leases(): readonly CultMeshAuthorityLease[] {
return [...this.#leases.values()].sort((left, right) =>
@ -159,6 +160,13 @@ export class CultMeshAuthorityLeaseCatalog {
);
}
public watch(callback: (lease: CultMeshAuthorityLease) => void): () => void {
this.#subscribers.add(callback);
return () => {
this.#subscribers.delete(callback);
};
}
public upsert(lease: CultMeshAuthorityLease): void {
requireNonEmpty(lease.leaseId, "lease.leaseId");
requireNonEmpty(lease.verseId, "lease.verseId");
@ -168,6 +176,9 @@ export class CultMeshAuthorityLeaseCatalog {
}
this.#leases.set(lease.leaseId, lease);
for (const subscriber of [...this.#subscribers]) {
subscriber(lease);
}
}
public get(leaseId: string): CultMeshAuthorityLease | undefined {
@ -298,6 +309,8 @@ export interface CultMeshStreamFrameHandle {
export class CultMeshStreamCatalog {
readonly #streams = new Map<string, CultMeshStreamDescriptor>();
readonly #latestFrames = new Map<string, CultMeshStreamFrameHandle>();
readonly #streamSubscribers = new Set<(stream: CultMeshStreamDescriptor) => void>();
readonly #frameSubscribers = new Set<(frame: CultMeshStreamFrameHandle) => void>();
public get streams(): readonly CultMeshStreamDescriptor[] {
return [...this.#streams.values()].sort((left, right) =>
@ -305,6 +318,20 @@ export class CultMeshStreamCatalog {
);
}
public watch(callback: (stream: CultMeshStreamDescriptor) => void): () => void {
this.#streamSubscribers.add(callback);
return () => {
this.#streamSubscribers.delete(callback);
};
}
public watchFrames(callback: (frame: CultMeshStreamFrameHandle) => void): () => void {
this.#frameSubscribers.add(callback);
return () => {
this.#frameSubscribers.delete(callback);
};
}
public declare(stream: CultMeshStreamDescriptor): CultMeshStreamDescriptor {
requireNonEmpty(stream.streamId, "stream.streamId");
requireNonEmpty(stream.verseId, "stream.verseId");
@ -315,6 +342,9 @@ export class CultMeshStreamCatalog {
}
this.#streams.set(stream.streamId, stream);
for (const subscriber of [...this.#streamSubscribers]) {
subscriber(stream);
}
return stream;
}
@ -381,6 +411,9 @@ export class CultMeshStreamCatalog {
}
this.#latestFrames.set(frame.streamId, frame);
for (const subscriber of [...this.#frameSubscribers]) {
subscriber(frame);
}
return frame;
}

View file

@ -69,15 +69,23 @@ test("CultMesh TS local authority leases do not trust peer cards by contact alon
assert.deepEqual(peers.find("local", "shard-primary"), [peer]);
assert.equal(leases.isAuthorized(peer, "shard-primary"), false);
leases.upsert({
const leaseUpdates: string[] = [];
const unsubscribeLease = leases.watch((lease) => {
leaseUpdates.push(lease.leaseId);
});
const lease = {
leaseId: "lease:voidbot-local",
verseId: "local",
peerId: "voidbot-local",
roles: ["shard-primary"],
validFrom: new Date(Date.now() - 1000),
expiresAt: new Date(Date.now() + 1000),
});
};
leases.upsert(lease);
unsubscribeLease();
leases.upsert({ ...lease, roles: ["shard-primary", "schema"] });
assert.deepEqual(leaseUpdates, ["lease:voidbot-local"]);
assert.equal(leases.get("lease:voidbot-local")?.peerId, "voidbot-local");
assert.deepEqual(leases.leases.map((lease) => lease.leaseId), [
"lease:voidbot-local",
@ -109,7 +117,15 @@ test("CultMesh TS local Verse catalog exposes sorted views and watch ergonomics"
test("CultMesh TS negotiates streaming frame body transports explicitly", () => {
const streams = CultMesh.createStreamCatalog();
streams.declare({
const streamUpdates: string[] = [];
const frameUpdates: bigint[] = [];
const unsubscribeStream = streams.watch((stream) => {
streamUpdates.push(stream.streamId);
});
const unsubscribeFrame = streams.watchFrames((frame) => {
frameUpdates.push(frame.sequence);
});
const stream = {
streamId: "mimir:kiyo-pro",
verseId: "studio",
ownerPeerId: "starfire",
@ -132,7 +148,8 @@ test("CultMesh TS negotiates streaming frame body transports explicitly", () =>
"cultcache-page",
],
maxInFlightFrames: 3,
});
} as const;
streams.declare(stream);
const negotiation = streams.negotiate("mimir:kiyo-pro", {
peerId: "fensalir",
@ -152,8 +169,9 @@ test("CultMesh TS negotiates streaming frame body transports explicitly", () =>
maxInFlightFrames: 2,
copyBudget: "zero-copy-target",
});
assert.deepEqual(streamUpdates, ["mimir:kiyo-pro"]);
streams.publishFrame({
const frame = {
streamId: "mimir:kiyo-pro",
sequence: 42n,
timestampNs: 1_000_000_000n,
@ -163,7 +181,15 @@ test("CultMesh TS negotiates streaming frame body transports explicitly", () =>
fenceHandle: "0xbeef",
fenceValue: 7n,
unavoidableCopyCount: 0,
});
} as const;
streams.publishFrame(frame);
assert.deepEqual(frameUpdates, [42n]);
assert.equal(streams.latestFrame("mimir:kiyo-pro")?.sequence, 42n);
unsubscribeStream();
unsubscribeFrame();
streams.declare({ ...stream, streamId: "mimir:kiyo-pro-alt" });
streams.publishFrame({ ...frame, sequence: 43n });
assert.deepEqual(streamUpdates, ["mimir:kiyo-pro"]);
assert.deepEqual(frameUpdates, [42n]);
});