What problem does it solve?
Multi-instance TanStack AI deployments risk race conditions when multiple workers mutate shared chat state concurrently, and persistence stores alone do not provide mutual exclusion for critical sections.
Core Features & Use Cases
- LockStore contract: Define a
withLock(key, fn) interface that serializes owners per key and passes an AbortSignal into critical sections.
- InMemoryLockStore: Use the built-in per-key promise chain for single-process coordination, or implement a distributed store with
defineLock for multi-instance deployments.
- Middleware integration: Wire
withLocks into the ChatMiddleware chain to expose the LocksCapability to downstream middleware such as sandbox.
- Use Case: A sandbox middleware running across multiple Cloudflare workers acquires a per-thread lease before mutating shared state, aborting work automatically when the lease is lost.
Quick Start
Add withLocks with an InMemoryLockStore to my TanStack AI chat middleware chain so sandbox operations are mutually exclusive per thread.