Skip to content
ContractSpec
Start here
AI index

Contracts Runtime Core

The framework-neutral, in-house engine for the canonical ContractSpec data-fetching protocol. It owns caching, the durable offline queue, conflict resolution, and the ports (Transport, Storage, reachability) — with no @tanstack/* dependency and no React coupling. It depends only on @lssm-tech/lib.contracts-spec and@lssm-tech/lib.observability.

Installation

bun add @lssm-tech/lib.contracts-runtime-core

What it provides

Transport port

One execute(envelope, ctx) port (+ optional subscribe()realtime seam). REST, MCP, WebSocket, and in-memory are interchangeable adapters behind it.

Query + cache engine

In-house cache store keyed by createQueryKey, request dedup, retry/backoff, background revalidation, GC, and the canonicalQueryState machine.

Offline + conflict

Durable mutation queue with replay-on-reconnect, optimistic apply/rollback, etag conflict detection, and a per-contract conflict-resolver registry (server-wins default; CRDT via crdt-loro injection).

Storage + reachability

Storage port with secure-by-default platform variants (/storage/indexeddb web, /storage/async-storage native) and a reachability input port the engine reads but never imports integrations for.

Engine setup

typescript
import {
  createDataEngine,
  createDataEngineFromEnv,
  createMemoryStorage,
  createStaticReachabilitySource,
  type Transport,
  type DataEngine,
} from '@lssm-tech/lib.contracts-runtime-core';

const engine: DataEngine = createDataEngine({
  transport,            // required: a Transport adapter (REST/MCP/in-memory)
  localOffline,         // optional: 'online-only' | 'memory-cache' | 'offline-capable'
  env,                  // optional: host env (e.g. process.env); reads CONTRACTSPEC_LOCAL_OFFLINE
  flag,                 // optional: feature-flag override (wins over env + default)
  advanced: {           // optional: low-level hatches used inside the selected mode
    storage,            //   durable Storage port (defaults to in-memory)
    reachability,       //   ReachabilitySource (defaults to always-online)
    contextProvider,    //   fresh auth/correlation ctx per replay
    refreshAuth,        //   () => Promise<boolean> for auth-expiry resume
  },
});

// On/off trigger from the host environment in a single call. Precedence is
// flag > env > localOffline default; the core never reads global process.env
// itself — createDataEngineFromEnv is the only seam that injects it:
//   CONTRACTSPEC_LOCAL_OFFLINE=offline-capable | memory-cache | online-only | on | off
const fromEnv: DataEngine = createDataEngineFromEnv({ transport });

// Surface: query / getState / subscribe / invalidateTags / refresh /
//          emitConflict / mutate / drain / resume / dispose (+ events, maintenance)

Related reading

For the full protocol, client hooks, server adapters, and rendering story, read the Data Fetching spec.