Pre-beta, under heavy active development: the API changes often and nothing here is stable yet.

Spar

Spar, for agents

This file is everything an agent needs to write a Spar app. It is put together by bun run llms from this guide, the principles, the API reference, the recipes and the source of the showcase app, so the examples are code that is built and tested, not sketches. When this file and the code disagree, the code is right and this file is stale: run bun run llms.

What Spar is

Spar is a UI framework for people who are all in on Effect. It is not general purpose and does not try to be. A Spar app is an Effect program front to back: components are Effects, state lives in Schema-described stores, the backend is an Effect RPC group, and one trace follows a click from the page through the backend and back. The framework sees every state change and every backend call, logs each one, and shows them in devtools that people use in the page and agents use over MCP or from a shell.

If a thing can be done two ways, Spar picks one and refuses the other, at compile time or at build time. Do not look for the escape hatch. There is not meant to be one.

Read this first

"Routes, data and no waterfalls", the part right after this guide, is the one way to load data: declare a query, ask for it in the component that needs it, pass values down as reactive props. Never fetch in a component's Effect and never wait there.

Three rules that break the build most often:

  1. Never write a bare (props) => ... as an enhancer inside compose. Wrap it: withEffectProps((props) => Effect.gen(...)) when it runs an Effect, withHandlers((props) => ({ ... })) when it returns plain values. A bare one makes props unknown in every enhancer after it (TS18046), and the errors that follow (TS2339, an Effect<unknown, unknown, unknown> that is not a Handler) all go away with the wrap.
  2. A UI write (UiState.set, UiState.update) is not an Effect, so Effect.all, Effect.andThen, Effect.tap and Effect.forEach do not take it (TS2739: Type 'UiWrite' is missing the following properties from type 'Effect'). Yield it inside Effect.gen, or give a handler a list of actions: onReset: [UiState.set(props.name, ""), UiState.set(props.open, false)].
  3. A handler (an event prop, a withKey action) takes an Effect<void> with no error and no requirement, a UI write, or a list of them. A store command (store.commands.added(payload)) already fits. Something that fails or needs a service gets handled first: Effect.catch, or provide it. When what to run depends on a value, return the action from an Effect instead of yielding it: an action (a callback prop's result, a list) cannot be yielded, and Spar runs what you return. onEnter: Effect.gen(function* () { const now = yield* props.palette.get; return props.onRun(now.active) }). Returning nothing runs nothing.

The shape of an app

src/
  api.ts            the RPC contract shared with the backend: groups, payloads, errors
  routes.ts         Router.route(path, Schema) values
  SomeStore.ts      Store.Tag: state Schema, actions, selectors
  Some.tsx          components: compose(...enhancers)((props) => <jsx />)
  main.ts           App.mount with the layers
  server/           what runs on the server, or stands in for it
server/             the backend's handlers

A component is one expression: compose(...enhancers)((props) => <jsx />). The enhancers add props (a store's values and commands, UI state, a query, a key press); the view comes last, inline, and its props are inferred from them. The view never runs an Effect; an enhancer never writes markup. Child components are written in JSX inside the view. Your own behaviour, written inside compose, is withEffectProps((props) => effect) for props made by an Effect or withHandlers((props) => values) for plain values. Written as a named function, give its parameter a type: const withDraft = (props: { readonly id: string }) => Effect.succeed({ ... }). A group of enhancers is compose(...) without a view. compose takes up to 10 enhancers; group some with compose(...) beyond that.

If TypeScript says 'props' is of type 'unknown' or that a property you added does not exist, an untyped inline function among the enhancers is the cause (the build says so too, with SPAR_ENHANCER_UNTYPED): wrap it in withEffectProps or withHandlers.

Three other errors to avoid:

  • For and Show are imported from "spar" like everything else.
  • In a .tsx file a generic arrow is read as a JSX tag: write <A,>(item: A) => ... with the comma, or use a function.
  • exactOptionalPropertyTypes is on: never pass undefined to an optional prop or attribute. Leave it out ({...(title === undefined ? {} : { title })}) or give a value (title ?? "").

For reads its item type from each. When each is an inline expression such as Reactive.map(props.rows, (all) => all.filter(...)), TypeScript cannot infer it; compute the list in an enhancer and pass props.shown instead.

The one way to do each thing

You want toDo thisNot this
Make a componentcompose(...enhancers)((props) => <jsx />) in a .tsx fileA plain function returning JSX
Take props from the parentwithProps({ name: Schema }) as the first enhancerUntyped props
Make props with an EffectwithEffectProps(Effect.fnUntraced(function* (props) { ... return { ... } }))(props) => Effect.gen(...) written inline among enhancers: every enhancer after it sees props as unknown
Read a storewithStore(Store, (store) => ({ count: store.select.count, onAdd: store.commands.added() }))Reading a store inside the view
Hold state only the screen cares aboutwithUiState(name, Schema, initial)Ref, SubscriptionRef, let, a module variable
Add a behaviour of your ownA named function with a typed parameter, (props: { readonly id: string }) => Effect.succeed({ ... }), or compose(a, b) with no viewCopying the same enhancers into every component; an untyped inline function among enhancers
Use a child componentWrite it in JSX in the view: <Dropdown label="Sort" onPick={pick} />Passing elements down as props
Hold data the app cares aboutA Store.Tag with actions and selectorsUI state seeded from a store, a service with a Ref
Put a store where one component uses itSomeStore.with(Component), then mount the resultWiring a layer at the root for a store one screen owns
Share a store across the appSomeStore.layer in the layers given to App.mountA global
Change app statestore.commands.<action>(payload)Writing to state anywhere else
Change UI stateUiState.set or UiState.update in a handlerWriting UI state from a command, a stream or a service
Show a list<For each={props.items} by={(item) => item.id}>{(item) => <li>...</li>}</For>.map inside JSX
Show a tree (a component inside itself)const Row: View.Component<Props> = compose(...)(... <Row node={child} /> ...), a Schema.suspend SchemaFlattening the tree by hand; leaving the type off
Show one thing or another<Show when={props.open}>...</Show>A ternary inside JSX
Load data from the serverA Query.Tag, and withQuery(name, TheQuery, (props) => input) where it is neededFetching in a component and passing the result down
Use loaded data in an actionAn Effect that reads TheQuery.read(input) and then sends a command with the resultProps or arguments that carry server data
Stop a second run while one is goingRun the work with FiberHandle.run(handle, effect, { onlyIfMissing: true })Disabling the button and hoping nothing else starts it
Run a flow with a wait in itOne Effect: a command, Effect.sleep or the call, a command with the resultA lock, a queue, a flag in UI state
Call your backendBackend.rpc(Group, url) and call the clientfetch, axios, tRPC, a hand-rolled client
Call someone else's HTTP APIAn Effect HttpClient call, or an HttpApi client, with Backend.layer providedfetch, FetchHttpClient.layer
Send something that must not be lostOutbox.channel(name, schema, send): saved on the device, retried with an idempotency keyCalling the server from a handler and hoping
Keep a store across a reloadSomeStore.layerSaved, with a KeyValueStore; other tabs that save it are followedWriting to localStorage by hand
Handle a failure a component can haveView.recover(ErrorSchema, FallbackView)catchAll that hides it; leaving it (compile error)
Show something while a component loadsView.pending(LoadingView)A loading flag in UI state
NavigateRouter.route(path, Schema), router.link(route, params), router.go(route, params)location.href, string paths
Build a formForm.make(Schema, initial), form.field(name), form.submit or form.submitThenUI state for each input plus hand validation
React to a key presswithKey("Escape", (props) => UiState.set(props.open, false))addEventListener
Run something while mountedwithEffect((props) => effect); Events.onWindow / onDocument inside itA module-level listener
Focus or measure an elementconst input = yield* Handle.make (an Effect, not a function), passed as a Props.Handledocument.querySelector
Open a dialog, menu, popover or tooltipThe ones in @spar/ui, or View.portal with OverlayA positioned div
Note something that happened, for the logTimeline.note(kind, name, detail)console.log
Test a storeProvide its layer and call commands in it.effectMounting a page to test a reducer
Test a componentspar/testing: Testing.mount, then find and click by roleReaching into nodes by class name

What is refused, and why

Compile errors (the proofs are the files in apps/demo/forbidden, each of which must fail to compile):

  • A component from a plain function, a view without a Schema for its props, UI state, a store, a selector or route params without a Schema. Everything declared has a Schema so the devtools and agents can read it as data.
  • A component whose Effect needs a service nobody provided, or can fail with an error nobody recovers, mounted anyway. Needs and failures are settled at mount.
  • A backend error from the shared contract that the component does not handle.
  • A reducer returning an object literal with a literal-typed field needs its return type written ((state: State): State => ...), or TypeScript widens the literal and the action is rejected.
  • UI state made in a service, written from a command, a stream or a service listener, or seeded from a store. UI state belongs to one component and changes only in its handlers.
  • A component that can still fail, written as a JSX tag (TS2604 / TS2786: "cannot be used as a JSX component"). Recover its error inside it with View.recover, or mount it with View.child(component, props) from an enhancer. If its error type shows as unknown, look for the first error in that component instead: something there was not typed.
  • A service passed into a view, a modal outside a portal, View.pending placed before View.recover, a route mounted with the wrong page.

Build errors (the Vite plugin stops the build and names the file, line and rule):

  • views-need-a-schema: JSX outside View.make.
  • jsx-only-in-views: JSX inside an expression or a call, such as .map or a ternary in a hole.
  • no-effect-run-in-views: Effect.run* in a view file.
  • state-only-through-spar: Ref.make, SubscriptionRef.make, SynchronizedRef.make, MutableRef.make, TxRef.make, let, var. State the framework did not make is state the log cannot show and the devtools cannot rewind.
  • backend-only-through-spar: fetch, XMLHttpRequest, WebSocket, EventSource, sendBeacon, FetchHttpClient.layer, and imports of axios, ky, got, superagent, graphql-request, openapi-fetch, @trpc/client, @apollo/client, socket.io-client. A call the framework did not make is a call missing from the trace.

The two -through-spar rules look at app source. They skip node_modules, tests, config files, scripts/ and any server/ folder (server code is not page state).

House rules the linter holds everywhere: no loops, no mutation, no comments, no casts, type not interface, no nested ifs.

UI state and app state

UI state is what one component remembers about its own screen: a menu is open, what was typed, which tab is showing. It is made with UiState.make in that component's Effect, lives in one tree by component path, and goes away when the component unmounts. Nothing else can write it.

App state is what the app knows: the agents, the notes, the cart. It lives in a store. A store is a Schema for the state, named actions with pure reducers, and named selectors. It changes only through store.commands.<action>(payload), and every command is an entry in the store's log.

Keep in the state only what cannot be worked out from the rest. A total, a count, a "has any", a filtered or sorted list is a selector over the state, never a field that every reducer has to remember to update: total: Store.selector(Schema.Int, (state: State) => state.counts.reduce((sum, c) => sum + c.count, 0)). A second copy of a value is a value that can disagree with the first.

A store does not have to be declared at the root. SomeStore.with(Component) makes one unit of a store and the component that uses it. Mount that unit twice and there are two stores, each with its own state, each registered in the same root registry the devtools read. So it is still model, view, update: one registry, one log, every change an action. It only does not look like it, because the store and its view sit in the same file.

Flows and server data

Read the "State, events that interleave, and server data" part below before writing any store with async work. The short version:

  • A store accepts every command; there are no guards. Work that takes time is an Effect on a service (or an enhancer): it sends a command, does the work, and sends a command with the result. Every command is in the log and the trace.
  • Server data is a Query.Tag. Views never fetch and props never carry server data: an Effect that needs it reads TheQuery.read(input) itself.
  • When only the newest run matters (a search box, a filter), run it with FiberHandle.run(handle, effect), which stops the one before; or load it with a Query.
  • When a second press must not start the work again (Pay, Save, Send), run it with FiberHandle.run(handle, effect, { onlyIfMissing: true }), which ignores the repeat.
  • To change one row of a long list in a reducer, return Store.withRow(state.rows, index, row): the list rewrites that row only.
  • A query's value is null while a new input loads. To keep the last rows on screen while the next page or filter loads, show the handle's latest; for "load more", use TheQuery.pages.
  • A query with no input is asked for with no argument, yield* TheQuery.ask(). Giving it an input, such as one made from another query's value, does not compile (SPAR_NEEDLESS_WAIT).
  • A draft the user is typing is never overwritten with a saved or loaded value. Keep both.
  • Spar reports two timing mistakes while the app runs (SPAR_DUPLICATE_CALL, SPAR_TYPING_REPLACED). After a change, run the app and read the log for noticed and waited entries; each names its fix.
  • A store's layer needs no SecureRandom from you. A UI write (UiState.set, update) can be yielded inside Effect.gen. Give every reducer its state type: (state: State, payload) => ....
  • A form field's problem is shown by TextField, Select and Checkbox with the role alert.

The backend

Declare the contract once, in a file both sides import:

export class Agents extends RpcGroup.make(
  Rpc.make("Describe", {
    payload: { name: Schema.String },
    success: Started,
    error: NoRecord,
  }),
  Rpc.make("Start", {
    payload: { name: Schema.String, idempotencyKey: Schema.String },
    error: ServerUnreachable,
  }),
  Rpc.make("Started", { success: Schema.Array(Schema.String) }),
  Rpc.make("Users", { success: Schema.Array(User) }),
  Rpc.make("Lookup", { payload: { kind: Schema.String, id: Schema.String }, success: Found }),
) {}

(apps/sidebar/src/api.ts)

On the page, Backend.rpc(Agents, url) gives a typed client. Its calls are spans in the trace of whatever caused them, the trace context travels to the backend in the request, and the backend's spans join the same trace. Provide Backend.layer once, where the app is mounted.

Devtools

sparDevtools({ backends }) from spar/devtools/vite adds the tools on the dev server only. In the page they give the inspector, the one log with rewind, step and fork, the trace waterfall across page and backend, and the stores with their outbox. No collector has to run to see a trace.

An agent gets the same over MCP (bun run --filter @spar/devtools-mcp start, then connect to http://127.0.0.1:4188/mcp) or from a shell (bun run --filter @spar/devtools-mcp cli call <tool> '<json>'). A useful loop when debugging:

  1. get_timeline to see what happened, in order.
  2. step with by: -1 to walk back; each answer has the moment and the spans it caused.
  3. diff_states to see what an action changed in a store.
  4. fork, then run_action or set_ui_state to try something else from that point.
  5. switch_branch with branch: 0 to return to what really happened.

Working in this repo

  • bun run showcase starts the showcase app, its backend and the devtools.
  • bun run check is everything that must pass: format, lint, types, tests with 100% coverage, the forbidden-code proofs, the production build check.
  • bun run bench reruns the benchmarks and rewrites docs/benchmarks.md and docs/benchmarks.html.
  • Browser suites are moon tasks: bun run moon run sidebar:features sidebar:walkthrough sidebar:devtools sidebar:traced demo:browser stories:play ssr:browser --concurrency 1.
  • Only run tools through package scripts or moon tasks.