Add durable TypeScript provider sessions

This commit is contained in:
Meta Vens 2026-07-14 23:13:13 +01:00
commit 18cebf740f
8 changed files with 1905 additions and 2 deletions

View file

@ -0,0 +1,122 @@
# 4. Keep A TypeScript Provider Alive
A provider process should declare desired typed publications and domain command
handlers once. It should not rebuild discovery registration, lease renewal,
reconnect, republish, receipt delivery, and withdrawal loops in a renderer or
scheduled export script.
`CultMeshProviderSession` owns that lifecycle. The application still owns its
state and command transaction. A transport connector owns the physical CultNet
route. Eve advertisements and surfaces are ordinary typed publications; the
session does not import Eve or learn provider gameplay.
```ts
import {
CultMeshProviderSession,
type CultMeshProviderReceiptStore,
type CultMeshProviderTransport,
} from "cultmesh-ts";
export async function runProvider(
transport: CultMeshProviderTransport,
receipts: CultMeshProviderReceiptStore,
) {
const provider = new CultMeshProviderSession({
identity: {
providerId: "voidbot.swarm",
serviceInstanceId: "voidbot-worker-7",
endpointId: "odin:voidbot-worker-7",
verseId: "voidbot.local",
},
transport,
receiptStore: receipts,
publications: [
{
publicationId: "voidbot.swarm.advertisement",
documentType: "gamecult.eve.provider_advertisement",
schemaId: "gamecult.eve.provider_advertisement.v1",
recordKey: "voidbot.swarm",
value: providerAdvertisement,
},
{
publicationId: "voidbot.swarm.surface",
documentType: "gamecult.eve.surface_state",
schemaId: "gamecult.eve.surface_state.v1",
recordKey: "voidbot.swarm",
value: currentSurface,
},
],
commandHandlers: {
"swarm.set_heat": async command =>
applyHeatCommandTransaction(command.commandId, command.payload),
},
});
await provider.start();
await provider.upsertPublication({
publicationId: "voidbot.swarm.surface",
documentType: "gamecult.eve.surface_state",
schemaId: "gamecult.eve.surface_state.v1",
recordKey: "voidbot.swarm",
value: nextSurface,
});
process.once("SIGTERM", () => {
provider.stop().catch(error => {
console.error("CultMesh provider shutdown completed with cleanup failures.", error);
process.exitCode = 1;
});
});
}
```
The four identities are deliberately distinct:
- `providerId` names the product capability clients select;
- `serviceInstanceId` names the running owner incarnation;
- `endpointId` names the stable discovery/session route.
- `verseId` names the discovery and state-sharing domain.
Do not replace any of them with a process id, socket address, body producer id,
or renderer id.
## Receipt Rule
The receipt store is required and must be durable in a daemon. The in-memory
store exported by the package is for tests and disposable tools only. The
domain command handler must apply its mutation idempotently using `commandId`;
the provider session deduplicates delivery and republishes the stored typed
receipt, but it cannot make an unrelated database mutation atomic by wishing
at it harder. `commandId` is the idempotency key: later delivery of the same
provider, service-instance, and command kind returns the original receipt even
if a sender incorrectly changes the payload. Reusing the ID for another kind
or identity is rejected as a conflict.
The store is also the receipt outbox. `put` commits the receipt before any wire
publication. `listPending` supplies receipts after startup or reconnect, and
`markPublished` removes one only after the active connection accepts it. Scope
the store to the provider and service-instance identity. If the store is
unavailable, the session enters `degraded` and stops accepting commands until a
renewal can drain the outbox again; a store failure is not a transport failure.
## Verification Checkpoint
Use an injected scheduler and transport fault double to prove the actual
lifecycle:
1. initial registration publishes every desired document in stable order;
2. lease renewal occurs before expiry;
3. a failed route produces `reconnecting`, then republishes current desired
state on the replacement connection;
4. duplicate command delivery runs one handler transaction and republishes the
same receipt;
5. failed receipt transport reconnects and replays the durable outbox exactly
until `markPublished` succeeds;
6. shutdown drains command persistence, withdraws the lease and remaining
publications, and closes even when cleanup reports failures;
7. stopping during registration cannot let the stale connection publish;
8. observers and conflicting command IDs cannot become lifecycle authorities.
The package suite contains this timeline in
`packages/cultmesh-ts/test/provider-session.test.ts`.

View file

@ -9,6 +9,7 @@ Follow the chapters in order:
1. [Persist typed application state](01-persist-typed-state.md)
2. [Connect a client by stable identity](02-connect-by-identity.md)
3. [Publish one Eve surface to multiple runtimes](03-publish-an-eve-surface.md)
4. [Keep a TypeScript provider alive](04-keep-a-typescript-provider-alive.md)
The intended application shape is small:
@ -33,4 +34,3 @@ native projection. CultMesh owns discovery and connection continuity.
pull request.
- A setup step that depends on repository internals is an API defect, not
acceptable tutorial folklore.

View file

@ -496,6 +496,54 @@ after bounded replay. The Aetheria/EveUnity witness proves provider
discovery, live SoA state, commands, receipts, and assets through the migrated
session path.
### Phase 2 TypeScript provider-lifecycle slice
**Owner:** `CultMeshProviderSession` owns one TypeScript provider's discovery
registration, lease renewal, physical reconnect, desired typed publication
replay, command dispatch, receipt publication, and explicit withdrawal. It is
keyed by provider, service-instance, endpoint, and Verse identity; those
identities are never collapsed.
**Inputs:** an injected `CultMeshProviderTransport`, stable provider identity,
lease/reconnect policy, desired typed publications, domain command handlers, a
required durable receipt store, and an injected scheduler.
**Outputs:** watchable connecting/active/reconnecting/withdrawing/stopped
state, current lease evidence, ordered document publication, a durable typed
receipt outbox, and withdrawal of the lease plus remaining publications.
**Derived state:** reconnect attempt, backoff, current physical connection, and
the in-flight duplicate-command set. Pending receipt delivery is derived from
the durable receipt store, not volatile session memory. Provider state, Eve surface semantics,
command effects, endpoint discovery evidence, and body-producer identity are
not derived by the session.
**Forbidden writers:** renderers, HTML exporters, and scheduled projection
scripts may not register providers, renew leases, reconnect, publish receipts,
or withdraw provider state. A transport connection cannot mutate provider
domain state or treat successful delivery as authorization.
**Shared paths:** initial connect and reconnect both register, subscribe, and
replay the same desired publication set. Live publication updates use one
serialized lane, so an older update cannot arrive after a newer one. Duplicate
command deliveries share one in-flight transaction and later deliveries reuse
the durable receipt. A receipt is stored before publication and marked only
after acceptance; reconnect drains pending receipts before command intake.
**Cut line:** TypeScript one-shot RUDP publication helpers remain low-level
compatibility surfaces, not provider lifecycle owners. VoidBot's swarm renderer
is the first identified obsolete owner; its provider catalog mutation,
publication, reconnect, and receipt paths must be deleted when it adopts this
session. That consumer migration is still open and must not run both owners.
**Verification layer:** deterministic TypeScript tests prove initial
registration, renewal, stable publication order, reconnect and replay,
duplicate receipt reuse, explicit withdrawal, and the negative stop-during-
registration timeline, durable receipt replay, observer isolation, conflicting
command IDs, and exception-safe shutdown. The empty-consumer tarball smoke proves the session is
present in the installable `cultmesh-ts` package. A live Odin transport adapter
and the VoidBot migration remain required for the production proof.
## Phase 3: Authority as a Mandatory Boundary
### Cut first