# Why Sorus

> Read when deciding whether Sorus fits, or when one of its rules looks arbitrary. The claims behind its design, what each costs, when to pick it, and which package holds what.

Sorus is glitch-free reactive state for code an agent writes and a person
reviews, not for the fewest keystrokes. A read or a write in the wrong place
fails with a type error or a named error instead of half-working, so the agent
sees the mistake and fixes it. That takes more code than implicit tracking,
and it takes speed. Sorus pays both on purpose.

## Reads are explicit

With implicit tracking, `composer.text` looks the same whether or not the read
is tracked, and forgetting it produces no error, only a value that stops
updating. In Sorus an observable is not its value: it is a box, a computed
value, an atom, or a collection, named with a leading `$`, with no read or
write methods. Every place that reads hands its function a `read`, and
`read($x)` returns the value and, where reads are tracked, subscribes that
place. Without `read`, arithmetic, property access, and typed arguments fail
to compile.

```ts
import { Computed } from "@frondruntime/sorus";
import { app } from "./realm.ts";
import { $composer } from "./state.ts";

// Each read(...) returns the value and subscribes $hasText to that box.
const $hasText = Computed.make(app, (read) => read($composer).text.trim() !== "");

// The same value without read does not compile. A box is not its value:
// error TS2339, property 'text' does not exist on type 'Box<Composer>'.
const $broken = Computed.make(app, () => $composer.text.trim() !== "");
```

A component's render body has no `read`, so React hooks are the only way in
and there is no `observer` wrapper. The cost is code: every read is a call, a derived value is a `$` field
built with `Computed.make` instead of a getter, and `read` is passed down.

## A write has one place

A box changes only inside `Realm.update`, through the `write` it hands out; a
box field has no setter, and a `write` kept past its update throws. An update
is atomic: an error thrown from the recipe undoes every write before it, and
watchers run once, after the update ends.

```ts
import { Realm } from "@frondruntime/sorus";
import { app } from "./realm.ts";

const typeText = (text: string) => {
  Realm.update(app, (read, write) => {
    write($composer, { ...read($composer), text });
  });
};

const openRoom = (roomId: string) => {
  Realm.update(app, (_read, write) => {
    write($composer, { text: "", replyTo: null });
    // Throwing undoes the write above: the composer keeps its text.
    if (roomId === "") throw new Error("a room id is not blank");
    write($roomId, roomId);
  });
};
```

The cost is that familiar patterns are errors. An update in a computed value
throws, so a derived value never writes, and a write after an `await` needs a
new `Realm.update`. The
[reactive-framework-test-suite](https://github.com/johnsoncodehk/reactive-framework-test-suite)
case that expects a throwing batch to keep its writes fails by design.
[Updates](../state/updates.md) has the rest.

## Misuse is loud

A write outside `Realm.update`, a `read` outside a place, and an update inside
a computed value throw and name where to write instead. Every error carries a
literal `_tag` ([Errors](../guides/errors.md)).

Detection has limits. TypeScript accepts any object in a condition and in a
string conversion, so a forgotten `read` in `if ($done)` or a template literal
compiles: the condition is always truthy, and the string is `[object Object]`.
The other limits are in [Errors and debugging](../guides/errors.md).

## Glitch-free by construction

A watcher never sees a half-applied update, and a derived value never computes
from a stale source. A write marks its dependents, a read validates, and watchers run after the
write settles. The cost is speed: every read is tracked and every schema box
write is checked. The budget for 1.0, measured on Hermes, is the public layer
within 2× of alien-signals and the graph core within 1.6×
([performance.md](https://github.com/frondruntime/sorus/blob/main/docs/performance.md)).

## Observation is a lifecycle

A value is live while something observes it. `Source.onObserved` is the one
hook for starting and stopping work: a socket opens with the first reader and
closes with the last.
The cost is that `Source.peek` and `Realm.untracked` start no socket. See [Lifecycles](../state/lifecycles.md).

## Hermes first

A React Native app runs on Hermes, so a change counts when Hermes confirms it,
and a Bun-only win is a hint. Live bytes after a full collection
stay flat in the latest run for rows added and removed and a realm made and
dropped per screen. A workload outside the measured scenarios is not covered.

## The synchronous counterpart to Effect

Effect runs the programs; Sorus holds the state they read and write. Schema
models are Effect Schema classes in `sorus-effect`: one class decodes wire data,
checks every write to its fields, and `Program.*` ties watches and streams
to a scope. State
stays synchronous: a read returns a value, and an update finishes before it
returns.

The cost sits in `sorus-effect`. `Model.decode` provides the realm as a
service, about 1.2 µs more per instance, and `Model.Schema` reads two
internals of Effect's `Schema.Class` (`effect` 4.1.0) and throws if either
is gone.

## Why not Effect's Atom

`Atom` in `effect/reactivity` was the first base tried, and was set aside
for what it does on a write, as of `effect` 4.1.0.

- **An unbatched write shows a mixed state.** With `b` and `c` derived from
  `a` and `d` reading both, one write to `a` computes `d` twice, once from the
  new `b` and the old `c`. Sorus computes it once
  ([bench/diamond.ts](https://github.com/frondruntime/sorus/blob/main/packages/sorus/bench/diamond.ts)).
- **A failed batch keeps its writes.** A Sorus update is undone.
- **A write is not checked.** `Atom` uses Schema to store a value, not to
  check a write.

`Atom` is the better choice for values that come from an Effect or a `Stream`,
and both can live in one application. The cost is a second reactive layer,
owned by this project and not by Effect's maintainers.

## When to pick it

Sorus fits state an application models and derives: forms, lists loaded from
a server, messages streamed into rows, and sorted, totaled views over them. A write to one row reruns only that row's readers, and a view
moves that row's key instead of sorting every row. Per-frame work (a chart, a
simulation) reads through `Realm.untracked` from its own loop and keeps
per-entity state in plain arrays.

What it costs, measured:

- **Adding or removing rows in a large collection.** Each key copies the key
  order: about 120 µs at 10,000 rows on Hermes. In
  `apps/rn-bench` at 10,000 rows, 100 inserts took 14 ms against MobX's 2.7
  (2026-11-16, before the 0.2 core, not re-measured). Many new rows go in through one `write.replaceAll`.
- **The schema path.** 3.3 to 7.5× the plain path's time to build 10,000
  rows, over seven runs. Data that needs no check goes in a plain `Box.make`.

## Maturity

Sorus is before 1.0, so a release may change the API; a migration ships as a
codemod.

## Packages

| Package | For |
| --- | --- |
| `@frondruntime/sorus` | Boxes, computed values, atoms, collections, models, updates, watchers. No Effect. |
| `@frondruntime/sorus-effect` | Every name of `sorus`, plus schema-checked boxes and models, decoding, and Effect programs. `effect` is a peer dependency. |
| `@frondruntime/sorus-react` | `useValue`, `useValues`, `useSelect`, `useRealm`, `RealmProvider`. |
| `@frondruntime/sorus-core` | The graph, with no runtime dependencies. |

Install commands are on [Start](./index.md#install).

## The decision

Where a guarantee and speed conflict, the guarantee wins and the budget says
by how much. The rules ship with the package, in
[AGENTS.md](https://github.com/frondruntime/sorus/blob/main/packages/sorus/AGENTS.md),
and the [agent skills](./skills.md) carry them one task at a time.

## See also

- [Start](./index.md): a realm, boxes, a computed value, and an update.
- [Coming from MobX](./mobx.md): the Sorus call for each MobX one.
- [Errors and debugging](../guides/errors.md): what throws, and `Source.inspect`.
