# Why zwire

> Why zwire combines zero-global scopes, rebaseable optimistic layers, incremental Top-K collections, and a shared TypeScript/Rust wire protocol.

Modern interactive applications increasingly span more than one runtime: a TypeScript frontend (React, Vue, Solid, or Svelte) talking to an SSR edge worker, a Rust backend, or a Rust WebAssembly engine in the browser. Most state libraries force you to maintain separate mental models, ad-hoc optimistic rollback logic, brittle list sorting, and custom JSON sync glue across each boundary.

`zwire` implements one **3-layer star-topology reactive architecture** identically in **TypeScript (`@zwire/*`)** and **Rust (`zwire`)**, bound together by a language-neutral JSON wire specification ([`spec/protocol.md`](/protocol/specification/)) and shared conformance suite ([`spec/conformance.json`](/spec/v3/conformance.json)).

## What makes `zwire` different

1. **Zero-global per-request scopes (`core`)** — There is no module-level mutable reactive state. Every graph runs inside an isolated `ReactiveScope` (TypeScript) or `Scope` / `SyncScope` (Rust). Concurrent SSR requests (`createScope({ mode: 'ssr' })` / `Scope::ssr()`) can never leak listeners or batch depth across requests, and unwatched `computed` nodes use forward-only dependency links so abandoned derived values are garbage-collected (`64/64` `WeakRef` verified in V8 and JSC).
2. **Rebaseable optimistic layers & STM drafts (`tx`)** — Overlapping optimistic mutations stack in creation order and **automatically rebase** when authoritative server pushes arrive mid-flight. You get surgical single-layer `revert()`, zero-flicker atomic `settle()` / `try_settle()`, and read-set verified STM `Draft`s.
3. **Exact incremental Top-K collections (`collections`)** — A 2-level Chunked Sorted Block Index (`BLOCK_TARGET = 128`) with deterministic key tie-breaking and `O(1)` boundary pruning keeps sorted Top-K views fast under heavy churn (`0.51 ms` / `406 KB` build and `0.038 µs/update` on 50k rows in V8; `171.9 ns/op` in Rust `PureReactiveCollection`).
4. **Strict 2-tier star topology (0B unused extension cost)** — Core (`@zwire/core`, `zwire-core`) never imports companion subpaths, UI adapters, or companion crates, and companions never import each other. Cross-feature composition happens solely through five lazy extension slots, keeping tree-shaken bundles tiny and Rust crates `#![forbid(unsafe_code)]`, `#![no_std]` (`alloc`), and zero-dependency.
5. **One open wire protocol across languages (`protocol`)** — Deterministic RFC-8259 hydration (`hydratable`/`dehydrate`/`hydrate` with out-of-order delete-on-consume buffering and lossless 64-bit integer safe-integer boundaries), coalesced echo-safe `SyncBridge`, plus optional `zwire-wasm` and `zwire-serde` bridges.

## When another tool is a better fit

`zwire` is engineered for correctness, scope isolation, transactional optimistic UX, sorted collections, and cross-language parity. Be honest about trade-offs ([full benchmark report](/architecture/benchmarks/)):

- **Simple client-only app state with zero SSR/optimistic/collection needs**: A minimal single-file signal or Zustand/Jotai store may be all you need if you never require per-request isolation, rebaseable transactions, or incremental Top-K.
- **Full HTTP cache & query-key orchestration**: `@zwire/core/resource` provides a lean `NotReady`-aware async primitive, not a full server-cache suite with pagination plugins, background refetch policies, or mutation caches like TanStack Query.
- **Legacy deeply-nested mutable drafts**: `@zwire/core/store` provides `createDeepStore` and `reconcile` for opt-in ergonomics, but flat normalized atoms, `createObjectStore`, and `createCollection` are dramatically faster (see [ADR-001](/architecture/adr-001/) and [Benchmarks](/architecture/benchmarks/)).

## What automated verification proves

Every pull request and release in `zwire` runs:

- **Cross-language conformance**: Both TypeScript (`Node 26` V8 + `Bun 1.3` JSC) and Rust (`1.100.0`, Edition 2024) execute the exact same test vectors in [`spec/conformance.json`](/spec/v3/conformance.json) covering optimistic rebase, STM read-set conflicts, Top-K pruning, and RFC-8259 64-bit integer wire round-trips.
- **Correctness & GC regression gates**: 15 engine gates including diamond glitch-freedom, early cutoff, exception-safe batch flushing, SSR multi-scope isolation, and `WeakRef` unwatched-computed garbage collection.
- **Producer & consumer declaration parity**: `tsrs` and stock `tsc` emit matching `.d.ts` surfaces across all 5 packages, verified under both `NodeNext` and `Bundler` consumers on Node 20, 22, 24, 26, and Bun.
- **Rust safety & portability**: `#![forbid(unsafe_code)]`, Clippy pedantic/nursery/cargo (`-D warnings`), `#![no_std]` + `wasm32-unknown-unknown` compilation, offline crate packaging, and live cross-process Rust ↔ TypeScript sync ([`examples/rust-ts-sync`](/examples/rust-ts-sync/)).
